Depuración e integración
IntroducidoEn esta lección
| English | Significado |
|---|---|
| watchdog/ˈwɒtʃdɒɡ/ | Perro guardián |
| debounce/ˈdebaʊns/ | anulación de rebotes |
"It froze twice last night"
- Every firmware bug starts as a vague report. Firmware is tested on the exact hardware it ships on, and the fault rarely appears when someone is watching.
- Without a method you often only move a bug instead of fixing it. This lesson gives that method.
The debug loop
- Reproduce the fault with the smallest input. Isolate it at a boundary with an LED, a breakpoint or one UART print.
- Fix one thing, then re-verify. Two changes at once leave you unsure which one helped.

Ordenar el bucle de depuración.
Dificultad 3/5 · 200 monedas + un pequeño bono en el primer intento de hoy
Sin una reproducción no se puede saber si una corrección funcionó; corregir más de una cosa oculta cuál fue la efectiva.
Bisection
- Put a probe in the middle of the suspect code. Whether the probe fires before the fault tells you which half holds the bug.
- Each probe halves the range, so $\log_{2}(n)$ probes find one instruction among $n$: ten for 1024.
La búsqueda por bisección examina 512 instrucciones sospechosas. ¿Cuántas sondas necesita como máximo?
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
Cada sonda reduce la mitad del rango: $\log_{2} 512 = 9$.
Una región sospechosa contiene 4096 instrucciones. ¿Cuántas ejecuciones de bisección se requieren para encontrar la defectuosa?
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
$2^{12} = 4096$, por lo tanto 12 ejecuciones. Duplicar el código añade solo una ejecución más.
The watchdog
- A watchdog 看门狗 is a counter that resets the chip if it runs out. The firmware must "kick" it (restart the count) regularly.
- A hung system then reboots by design instead of freezing. Kick it only from the main loop, never from an ISR.
¿Por qué está mal reiniciar el watchdog dentro de un ISR?
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
Si un ISR reinicia el watchdog, el bucle principal puede colgarse mientras las interrupciones lo mantienen activo, y el reinicio nunca ocurre.
Debounce
- A pressed button bounces: its contact opens and closes for a few milliseconds. Debounce 消抖 accepts a new state only after N equal readings in a row.
- It combines a timer tick, a GPIO read and a state decision: the whole course in one routine.
Worked example: a debounce trace
- Samples every 10 ms read 1, 0, 0, 1. Rule: two equal readings in a row.
- At 10 ms there is one 0; at 20 ms a second 0, so a stable 0 is declared. The 1 at 30 ms is a single sample and is ignored.
One contrary reading does not erase the qualified state
$N\text{ equal sampled readings span }(N-1)\Delta t$
Step through the worked 1,0,0,1 trace at 10 ms intervals with two equal readings required. The first qualified 0 appears at 20 ms; the later single 1 does not reverse it. Edit samples, compare a one-sample pulse and an alternating input, and change N to distinguish input chatter from accepted output changes. Startup remains unqualified until a state earns enough consecutive readings.
Llegan las muestras 1, 0, 0, 1 cada 10 ms, comenzando en 0 ms. Con la regla de dos seguidos iguales, ¿cuándo se declara un estado estable?
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
En 10 ms hay un 0 y sin veredicto; en 20 ms el segundo 0 forma un estable 0. El 1 en 30 ms es una muestra única.
La regla requiere tres lecturas iguales seguidas, muestreadas cada 5 ms desde 0 ms. Las lecturas son 0, 1, 0, 1, 1, 1. ¿A qué tiempo, en ms, se declara un estable 1?
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
Los 1s en 15, 20 y 25 ms son los primeros tres seguidos, así que el veredicto llega en 25 ms.
Worked example: bisecting the freeze
- The suspect region is 1024 instructions. Make instruction 512 light the LED: dark at the freeze means the first half; lit means the second.
- Each run halves the region: 512, 256, 128 … after ten runs, one instruction is left, because $2^{10} = 1024$.
Three common slips
- Fixing two things at once. One helps, one breaks something else, and now there are three bugs.
- Testing on another board. Debug on the same board revision that failed.
- Turning off the watchdog "for now". The field failure it would have survived is exactly the one that ships.
Depurar: buena práctica desactivar el watchdog y mantenerlo apagado tras corregir el error.
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
Un producto sin watchdog falla en lugar de reiniciar. Si debe estar apagado, enciéndalo antes del lanzamiento.
Recap
- Reproduce, bisect, fix one thing, re-verify.
- $\log_{2}(n)$ probes find the fault among $n$ instructions:
| Instructions $n$ | 1024 | about 1 million |
|---|---|---|
| Probes | 10 | 20 |
- Debounce needs N equal readings in a row; kick the watchdog from the main loop only.
¿Qué salió mal? Solo el equipo del sitio ve esta información.