C12 · Lesson 15 · Embedded lab
Register fields, timer settings, baud rates, debouncing and capture, in C and Python
Firmware engineers write a surprising amount of code that never runs on the target: scripts that work out timer settings, check baud-rate errors, decode register dumps, and replay captured signals through the same logic the firmware uses, to see what it would have done. This lab writes those tools in Python for the first part of the course (register fields, timers, the UART's divisor, debouncing and input capture) and then measures a button press on the board itself in C.
- The board measures how long the button on P4 is held, timing the press with the free-running cycle counter, and prints it in milliseconds.
- The button is pressed from 0.1 s to 0.35 s, and the program prints 250.
- The timeline shows the pin going low and high again; the print happens at the release.
Predict
The program above times a press with a 32-bit cycle counter at 10 MHz. What's the longest press it can time correctly?
cycles at 10 MHz is 429 s, about 7 minutes. Unsigned subtraction gives the right answer across one wrap of the counter, but not two. A 16-bit timer at 10 MHz would wrap every 6.55 ms, far too short for a person.
Name it
The tools
| Task | Inputs | Output |
|---|---|---|
| Register fields | a register value, a field's position and width | the field, or the register with a new field |
| Timer settings | a wanted frequency | PRESCALE, TOP and the frequency they really give |
| Baud rates | the wanted rates | BAUDDIV and each rate's error |
| Debouncing | sampled pin levels | the presses a counter debouncer accepts |
| Capture | captured 16-bit counts | the periods between them |
Each is a few lines of Python, and each mirrors code from the earlier lessons, so the same logic can be checked on a laptop before it's trusted on the board.
Check
Write get_field(reg, pos, width) and set_field(reg, pos, width, value) for 32-bit registers. set_field replaces the field and leaves every other bit alone, and raises ValueError if the value doesn't fit in the field.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write timer_settings(f, f_clk=10_000_000): the timer wraps at , with PRESCALE P and TOP T each from 0 to 65535. Return (P, T, actual frequency) for the settings closest to f, choosing the smallest P among equally close ones (it gives the finest resolution).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write baud_table(rates, f_clk=10_000_000): for each rate, BAUDDIV = the nearest whole number to . Return a list of (rate, BAUDDIV, error in percent), where the error is (actual − wanted)/wanted × 100.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write debounce(samples, n): samples are a button's levels (1 released, 0 pressed) read at a fixed rate. The debouncer's state starts at the first sample and changes only after n samples in a row differ from it. Return how many presses (changes of state to 0) it accepts.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write periods(captures, f_clk, bits=16): captures are successive values of a bits-bit capture register on the same edge. Return the periods between them in seconds, allowing for the counter wrapping between captures.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
The button on P4 (to ground) is pressed once, for 50 ms. Print how long it was held, in whole milliseconds (rounded), using the free-running SYS_CYCLES counter at 10 MHz.
C on the course's RISC-V board (10 MHz, no floating point). #include "board.h" for the registers; print(x) shows a number. Tab indents; press Esc and then Tab to move on.
Explain why
Why is it worth writing Python versions of firmware logic such as a debouncer, rather than only testing on the board?
On the board, a test depends on what the button, the wiring and the timing happen to do that day, and a rare bounce pattern or wrap of a counter may never show up. A Python copy of the same logic can be fed exactly the inputs you choose: recorded samples from a real button, worst cases built by hand, or millions of random sequences. The results are repeatable and fast, so bugs in the logic itself are found and fixed before the hardware is involved. Then the board test only has to confirm the hardware and timing, which is a much smaller question.
Practice
Keep practicing
One kind of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.