זיכרון וI/O
הוצג
7:37
מעבדים מיקרו ומערכות embedded
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ɪ əʊ/ | I/O ממופה לזיכרון |
| 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ɪ ˈəʊ/ | |
| 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ɪŋ/ | חיפוק |
| Interrupts/ˈɪntərʌpts/ |
כפתורים, נורות LED וחיישנים כזיכרון
- הכפתור, נורת ה-LED וחיישן הטמפרטורה של נקודת חיישן ממוקמים כולם מחוץ ל-CPU. ה-CPU תקשורת עם כל אחד מהם באמצעות קריאה וכתיבה לכתובות.
- אם עשה זאת היטב, הפירמוויר הוא טבלת שמות נקייה; אם לא, מדובר בערימת מספרים קסומים. שיעור זה מראה את הדרך הנקייה.
I/O ממוקם בזיכרון (Memory-mapped I/O)
- עם I/O ממוקד בזיכרון, כל רגיסטר של תוספת (peripheral) מופיע בכתובת מסוימת. קריאה מרגיסטר נתונים מקבלת ערך; כתיבה אליו שולחת אותו.
- לכל תוספת יש כתובת בסיס, ורגיסטריה ממוקמים בהזזה (offset) עם שם מסוים ממנה.

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)
- סריקה מחכה למכשיר חיצוני על ידי קריאת ביט הסטטוס שלו במעגל חוזר עד שהביט "הסתיים" מוגדר.
- היא פשוטה, אך כל סיבוב במעגל הוא זמן מעבד המושקע בבקשה. המתנה של $T$ שניות עולה $T \times f_{\text{clock}}$ מחזורי שעון.

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.
דוגמה מפורטת: העלות של הבקשה
- במהירות של 84 MHz ומעגל של 4 מחזור, המעבד מבצע 21 מיליון סיבובים בשנייה. המתנה של 1 מילי-שניות היא 21 000 סיבובים, או 84 000 מחזורי שעון.
- זהו 100% מזמן המעבד לאורך כל המילי-שנייה, תוך ביצוע ללא שימוש מועיל. מקטעים (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, טיימר בכתובת0x4002_0000, ADC בכתובת0x4003_0000. כל דרייבר משתמש בכתובת הבסיס שלו בתוספת הזזה (offset) שמית. - טעות הקלדה כמו שימוש ב-
0x4001_0004במקום בכתובת הבסיס של ה-UART תזיז את כל הרשמות בארבעה בייטים. קריאת רשמת הזיהוי של כל מכשיר חיצוני לאחר ההפעלה תגלה זאת מיידית.
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.
סיכום
- התקנים חיצוניים הם זיכרון: כתובת בסיס פלוז סטיות שמותיות.
- GPIO: הגדר את הכיוון פעם אחת, ולאחר מכן קרא או כתוב את הנתונים.
- צף עם זמן הגבלה, והוכח את המפה עם בדיקת קריאה חוזרת.
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.
מה השתבש? רק צוות האתר רואה זאת.