หน่วยความจำและอินพุต/เอาต์พุต (I/O)
แนะนำใหม่
7:37
ไมโครโปรเซสเซอร์และระบบฝังตัว
The digital logic course built gates and flip-flops; this course builds the machine that runs programs. Embedded work is verified from three directions.…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| firmware/ˈfɜːmweə/ | Software stored in a device to control its hardware and basic operation. |
| memory-mapped I/O/ˈmeməri mæpt aɪ əʊ/ | อินพุต/เอาต์พุตที่จับคู่กับหน่วยความจำ |
| register/ˈredʒɪstə/ | A group of storage elements holding a binary word, often loading its bits together on a clock event. |
| base address/beɪs əˈdres/ | The first address of a peripheral's block; each of its registers sits at a fixed offset from it. |
| offsets/ˈɒfsets/ | The distance in bytes from a base address to a particular register. |
| isolated I/O/ˈaɪsəleɪtɪd aɪ əʊ/ | A design with a separate address space for peripherals, reached only by special instructions such as IN and OUT. |
| GPIO/ˌdʒiː piː aɪ ˈəʊ/ | GPIO (General Purpose Input/Output) |
| read–modify–write/riːd ˈmɒdɪfaɪ raɪt/ | Reading a register, changing some of its bits and writing the whole value back. |
| bit mask/bɪt mæsk/ | A value with 1s only in the bit positions to be set, cleared, toggled or tested. |
| polling/ˈpəʊlɪŋ/ | การตรวจสอบสถานะ (Polling) |
| Interrupts/ˈɪntərʌpts/ |
ปุ่มไฟ LED และเซนเซอร์ในฐานะหน่วยความจำ
- ปุ่ม ไฟ LED และเซนเซอร์วัดอุณหภูมิของโหนดเซนเซอร์ทั้งหมดอยู่ภายนอก CPU CPU จะสื่อสารกับอุปกรณ์เหล่านี้ทุกตัวด้วยการอ่านและเขียนที่อยู่
- หากทำได้ดี เฟิร์มแวร์จะเป็นตารางชื่อที่ชัดเจน; หากทำไม่ดี จะเป็นกองตัวเลขมหัศจรรย์ บทเรียนนี้แสดงวิธีที่ชัดเจน
I/O แบบแมปหน่วยความจำ
- With memory-mapped I/O, each peripheral register appears at an address. Reading a data register receives a value; writing one sends it.
- Each peripheral has a base address, and its registers sit at named offsets from it.

With memory-mapped I/O, the CPU uses ordinary load and store instructions to talk to a peripheral.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The peripheral's registers sit at addresses, so a read or write to that address reaches the hardware instead of RAM.
พอร์ต GPIO
- พอร์ต GPIO มีรีจิสเตอร์ทิศทาง (หนึ่งบิตต่อพิน: อินพุตหรือเอาท์พุต) และรีจิสเตอร์ข้อมูล (ระดับของพิน)
- ตั้งค่าทิศทางครั้งเดียวตอนเริ่มระบบ แล้วอ่านหรือเขียนรีจิสเตอร์ข้อมูลเท่าที่ต้องการ

A polling loop while (!(ADC_STATUS & 1u)) { } never ends, although the scope shows DONE going high. The most likely cause:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Without volatile the optimiser may load the register once and loop on the stale copy. volatile forces a real read on every lap.
การโพล
- Polling คือการรอให้เพอร์เฟอรัลทำงานเสร็จโดยอ่านสถานะ bit ใน loop จนกว่า "done" bit จะถูก set ขึ้น
- It is simple, but every lap of the loop is CPU time spent asking. Waiting $T$ seconds costs $T \times f_{\text{clock}}$ cycles.

A pin stays an input no matter what you write to its data register. The likely cause is
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The direction register decides which way the pin works; the data register only carries the level. Set the direction first.
ตัวอย่างวิธีทำ: ค่าใช้จ่ายจากการรอคอย
- At 84 MHz with a 4-cycle loop, the CPU runs 21 million laps a second. A 1 ms wait is 21 000 laps, or 84 000 cycles.
- นั่นคือ 100% ของเวลา CPU ทั้งมิลลิวินาที โดยไม่เกิดประโยชน์อะไร Interrupts ในบทถัดไปจะขจัดค่าใช้จ่ายนี้ออก
GPIO_DATA reads 0x2D. After GPIO_DATA &= ~(1u << 0); it reads:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
~(1u << 0) has every bit 1 except bit 0. AND keeps the other bits and clears bit 0: 0x2D becomes 0x2C.
With pins 0, 2 and 5 high, GPIO_DATA = (1u << 3); turns on pin 3 and leaves the others as they were.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Plain = writes the whole register: only pin 3 stays high and pins 0, 2 and 5 go low. Use |= to change one bit.
ตัวอย่างวิธีทำ: แผนที่ของโหนด
- GPIO อยู่ที่
0x4000_0000, UART อยู่ที่0x4001_0000, Timer อยู่ที่0x4002_0000, ADC อยู่ที่0x4003_0000. Each driver ใช้ฐานบวกด้วย offset ที่มีชื่อเฉพาะ - หากพิมพ์ผิด เช่น ใช้
0x4001_0004แทนฐานของ UART จะทำให้ every register เลื่อนไป 4 ไบต์ การอ่าน ID register กลับมาของแต่ละ peripheral หลังเปิดเครื่องจะช่วยตรวจจับความผิดพลาดนี้ได้ทันที
A 4-cycle polling loop at 84 MHz waits 1 ms. How many CPU cycles does it use?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
$84 \times 10^{6} \div 4 = 21 \times 10^{6}$ laps a second; 1 ms is 21 000 laps × 4 cycles = 84 000 cycles, all of that millisecond.
การ์ดนี้ได้รับการแก้ไขแล้ว จึงแสดงเป็นภาษาอังกฤษจนกว่าคำแปลจะได้รับการอัปเดต
Worked example: the cost of asking
- Laps per second: at 84 MHz with a 4-cycle loop, the CPU runs $84 \times 10^6/4 = 21$ million laps a second.
- A 1 ms wait: $21 \times 10^6 \times 10^{-3} = 21\,000$ laps, or 84 000 cycles.
- The timeout above: 100 000 laps is $100\,000/(21 \times 10^6) = 4.8$ ms, a safe limit for a conversion that takes 1 ms.
- During the wait the CPU does nothing useful: 100% busy, 0% work. Interrupts 中断, in the next lesson, remove that cost.
Fewer polling checks do not mean a less busy CPU
$cycles=checks\times C,\quad checks\approx T f_{clock}/C,\quad cycles\approx T f_{clock}$
Start with the worked 1 ms wait at 84 MHz and four cycles per polling lap. The loop makes 21000 checks and occupies 84000 cycles. Increase the lap cost to reduce the number of checks without freeing the CPU during the wait. Put readiness between checks to see detection lag, or remove the ready event and watch the timeout stop the wait.
The same 4-cycle loop at 84 MHz waits 250 µs for a sensor. How many laps does it run?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
21 million laps a second × 250 µs = 5250 laps.
At 84 MHz with a 4-cycle loop, how many laps give a 2 ms timeout?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
21 million laps a second, so $21 \times 10^6 \times 0.002 = 42\,000$ laps.
สรุปทบทวน
- Peripherals เป็นหน่วยความจำ: มีที่อยู่ฐานและ offset ที่มีชื่อเฉพาะ
- GPIO: ตั้ง direction ครั้งเดียว แล้วจึงอ่านหรือเขียนข้อมูล
- Poll ด้วย timeout และตรวจสอบแผนที่ด้วยการอ่านกลับ (read-back check)
The UART's base is 0x4001_0000 and its status register sits at offset 0x04. A driver that wrongly uses 0x4001_0004 as the base reads "status" from
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Every register shifts by four bytes, so the status read lands at 0x4001_0008, a different register. An ID read-back at start-up catches it.
การ์ดนี้เป็นการ์ดใหม่ จึงแสดงเป็นภาษาอังกฤษจนกว่าคำแปลจะพร้อม
Four common slips
- Forgetting the direction register. The pin stays an input, and writes to it do nothing.
- Leaving out
volatile. The optimiser reads a status register once, and the polling 轮询 loop never ends. - Writing
=where you meant|=. One LED goes on and every other output goes off. - Polling without a timeout. A dead sensor then hangs the whole product.
The one safeguard every polling loop must carry is
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
A timeout bounds the wait, so a dead sensor returns an error instead of hanging the product. The watchdog is the outer safety net, not the loop guard.
Match each register to its job.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
A driver is mostly these registers at a base address plus named offsets.
การ์ดนี้เป็นการ์ดใหม่ จึงแสดงเป็นภาษาอังกฤษจนกว่าคำแปลจะพร้อม
Recap
- Peripherals are memory: a base address 基地址 plus named offsets, reached through
volatilepointers.
| Peripheral | Base address |
|---|---|
| GPIO | 0x4000_0000 |
| UART | 0x4001_0000 |
| Timer | 0x4002_0000 |
| ADC | 0x4003_0000 |
- Bits: set with
|= mask, clear with&= ~mask, toggle with^= mask, test with& mask. - GPIO: set the direction once, then read or write the data. Poll with a timeout, and prove the map with a read-back of each ID register.
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้