Memoria y E/S
Introducido
7:37
Microprocesadores y sistemas embebidos
The digital logic course built gates and flip-flops; this course builds the machine that runs programs. Embedded work is verified from three directions.…
Narración en inglés · subtítulos en inglés y 中文 que puedes cambiar
En esta lección
| English | Significado |
|---|---|
| memory-mapped I/O/ˈmeməri mæpt aɪ əʊ/ | E/S mapeada en memoria |
| 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 (Entrada/Salida de Propósito General) |
| 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ɪŋ/ | Sondeo |
Botones, LEDs y sensores como memoria
- El botón, el LED y el sensor de temperatura de un nodo se encuentran fuera de la CPU. La CPU se comunica con cada uno de ellos leyendo y escribiendo direcciones.
- Bien hecho, el firmware es una tabla limpia de nombres; mal hecho, un montón de números mágicos. Esta lección muestra la forma limpia.
E/S mapeada en memoria
- Con E/S mapeada en memoria, cada registro periférico aparece en una dirección. Leer un registro de datos recibe un valor; escribirlo lo envía.
- Cada periférico tiene una dirección base, y sus registros están situados en desplazamientos nombrados desde ella.

With memory-mapped I/O, the CPU uses ordinary load and store instructions to talk to a peripheral.
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
The peripheral's registers sit at addresses, so a read or write to that address reaches the hardware instead of RAM.
Puerto GPIO
- Un puerto GPIO tiene un registro de dirección (un bit por pin: entrada o salida) y un registro de datos (el nivel del pin).
- Establezca la dirección una vez al iniciar, luego lea o escriba el registro de datos tantas veces como sea necesario.

A polling loop while (!(ADC_STATUS & 1u)) { } never ends, although the scope shows DONE going high. The most likely cause:
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
Without volatile the optimiser may load the register once and loop on the stale copy. volatile forces a real read on every lap.
Muestreo
- El muestreo espera un periférico leyendo su bit de estado en un bucle hasta que se establece el bit de "completado".
- Es simple, pero cada vuelta del bucle consume tiempo de CPU. Esperar $T$ segundos cuesta $T \times f_{\text{clock}}$ ciclos.

A pin stays an input no matter what you write to its data register. The likely cause is
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
The direction register decides which way the pin works; the data register only carries the level. Set the direction first.
Ejemplo resuelto: el costo de preguntar
- A 84 MHz con un bucle de 4 ciclo, la CPU ejecuta 21 millones de vueltas por segundo. Un tiempo de espera de 1 ms equivale a 21 000 vueltas, o 84 000 ciclos.
- Eso representa 100% de la CPU durante todo el milisegundo, sin realizar ninguna tarea útil. Las interrupciones, en la siguiente lección, eliminan ese costo.
GPIO_DATA reads 0x2D. After GPIO_DATA &= ~(1u << 0); it reads:
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
~(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.
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
Plain = writes the whole register: only pin 3 stays high and pins 0, 2 and 5 go low. Use |= to change one bit.
Ejemplo resuelto: el mapa del nodo
- GPIO en
0x4000_0000, UART en0x4001_0000, temporizador en0x4002_0000, ADC en0x4003_0000. Cada controlador usa su base más los desplazamientos nombrados. - Un error tipográfico como
0x4001_0004para la base del UART desplaza cada registro cuatro bytes. Leer el registro de ID de cada periférico tras el arranque lo detecta al instante.
A 4-cycle polling loop at 84 MHz waits 1 ms. How many CPU cycles does it use?
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
$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.
Esta tarjeta se corrigió, por eso aparece en inglés hasta que se actualice su traducción.
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?
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
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?
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
21 million laps a second, so $21 \times 10^6 \times 0.002 = 42\,000$ laps.
Resumen
- Los periféricos son memoria: una dirección base más desplazamientos nombrados.
- GPIO: configure la dirección una sola vez, luego lea o escriba los datos.
- Realice sondas con tiempo límite y verifique el mapa mediante una lectura de confirmación.
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
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
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.
Esta tarjeta es nueva, por eso aparece en inglés hasta que su traducción esté lista.
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
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
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.
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
A driver is mostly these registers at a base address plus named offsets.
Esta tarjeta es nueva, por eso aparece en inglés hasta que su traducción esté lista.
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.
¿Qué salió mal? Solo el equipo del sitio ve esta información.