C1215 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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?

Name it

The tools

TaskInputsOutput
Register fieldsa register value, a field's position and widththe field, or the register with a new field
Timer settingsa wanted frequencyPRESCALE, TOP and the frequency they really give
Baud ratesthe wanted ratesBAUDDIV and each rate's error
Debouncingsampled pin levelsthe presses a counter debouncer accepts
Capturecaptured 16-bit countsthe 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 fclk/((P+1)(T+1)), 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 fclk/rate. 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?

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.