Timer interrupt and delivering ticks
Reads the handoff of a tick from the callback to the main loop and the limits of latest-value handoff, through the reading lesson and the previously checked practice kit, with steps and limitations
Before you start
Objective: read how a tick is passed from the callback to the main loop, and the limits of latest-value handoff Prerequisites: the timing-budget lesson and an understanding of polling Estimated practice time: 45 minutes, a time proposed by the authors and not yet measured with real learners
Check your understanding: explain why a tick is skipped and why this cannot be observed from the log. Use the earlier results to support your study. A successful compile or simulation does not confirm the board or real flight
View the whole learning path · Review sensors and communication
STM32 kit: practise Timer Interrupt and tick handoff
What it is for: It follows on from the polling kit towards dividing work between the timer interrupt (ISR) and the main loop. It suits learners who can read C/C++, GPIO/serial and unsigned timestamps. The goal is to keep the ISR short, receive the shared data as a consistent pair, and separate work that was not received in time from log rows that were not sent in time, before building on a drone firmware structure
Reference NUCLEO-F446RE / MB1136, STM32duino 3.0.0, TIM2 Timer Interrupt requests an update 100 Hz (10 ms). Compiled for the ARM target via the program 21,652 bytes / RAM 1,688 bytes , with no warnings, together with host arithmetic tests and a log checker on Windows/Ubuntu. It has not been uploaded, and the ISR/timing has not been measured on a real board. It is not a flight controller port and not a result of RTOS/DMA/real flight
Diagram of the ISR → main data handoff and synthetic examples, not real board measurements — Open the full-size Timer Interrupt diagram
Steps for practising Timer Interrupt on a PC
- Download the STM32 Timer Interrupt kit (ZIP) , extract it to a new folder, read the README, and keep the name
stm32_interrupt/sketch matching. Check the compile results and options and SHA manifest - Read the callback: it records
micros()and only increments the tick. The LED, CSV, serial and heavy work are in main, with no delay, buffer waiting or I2C in the ISR - Read main: it keeps PRIMASK, briefly disables IRQs to copy the tick and timestamp, then restores the original mask.
volatilealone does not make the pair consistent. When several new ticks arrive, main acts once with the latest value and counts the ones it does not handle individually - Run
python test_analyze.pyandpython analyze_interrupt.py synthetic/missed.csvand compare with the synthetic answer key for the four cases and the blank worksheet The ZIP contains normal/missed/wrap/dropped and the GNU C++11 host-test commands - Use IDE 2/CLI with core 3.0.0, select Nucleo-64 / F446RE, generic Serial, USB None, Smallest (-Os), Newlib Nano, then Verify/Compile as in the README. Do not use compile success in place of concurrency or timing results on the board
Reading the Interrupt results and basic tips
| CSV value | Meaning |
|---|---|
| tick | callbacks invoked since boot, as a uint32; not all hardware update events |
| service_us | time from the timestamp last recorded by the ISR to when main starts the LED task, after the snapshot/handoff, in µs |
| work_us | LED task time, excluding the snapshot/handoff, CSV formatting and serial sending |
| missed | callbacks that main does not handle individually, e.g. tick 1 → 4 has missed 2 |
| dropped | cumulative count of log rows that could not be sent; not the number of missed callbacks |
service_us is not the IRQ latency from the hardware deadline: this kit does not measure the time from when the timer should update to when the ISR is entered. Several updates during IRQ disable may merge into a single callback, and tick/missed cannot detect events merged that way, so do not conclude from a low service or missed 0 that the timer missed nothing
missed.csv has tick 1 → 4 → 5, total missed 2, maximum service 500 µs and maximum work 2,500 µs, exceeding the 2 ms sample budget in one row; dropped.csv has tick 1 → 3 and dropped 1: task 2 was done but there is no row sent. These values are invented data for teaching . When rows are lost, the analyzer can total missed only for the rows it sees. A counter value of 0 does not prove that collection began at boot or that no data was lost after the last row
Change main's tasks one at a time, use availableForWrite() and do not wait for serial space. Log formatting/sending still affects receiving the next tick even though it is not in work_us. The scope of work_us differs from the polling kit, so they cannot be compared directly as a benchmark. The LED toggles every 50 times main runs, so it is about 500 ms when every tick is received, and the period is not guaranteed when there is a backlog
TIM2 must not be reused by another library. Separate the files when a reset/drop counter wraps, and keep service/work below 2^31 µs (about 35.8 minutes). Use an unsigned delta for tick wrap; the host tests check numbers/boundaries and do not simulate ISR concurrency. When you have a board, do capture/logic analyzer work separately as in the README. No ESC/motor/IMU/radio is connected in this exercise
Reference the official HardwareTimer, implementation pin 3.0.0 and ST UM1724 for the MB1136
Continue learning and review
Review STM32 and timing budgets · Continue with the data queue from the ISR · Back to the advanced overview
Practise reading ticks and separating missed from dropped
Objective: read the snapshot and latest-value handoff, and separate callbacks that main does not handle individually from log rows that could not be sent Prerequisites: C/C++, unsigned integers and the Polling Timing lesson Suggested time: 45–60 minutes, not yet measured with learners. You can read the CSV/JSON and fill in the tables without a board
Uses the same four synthetic CSV cases, 11 rows in total; the nine handoff calls are read from the host test and then calculated. There are no new runs or board measurements. The original images and diagrams are kept
Download how to practise Timer Interrupt, the 8-question Interrupt worksheet, the original data and the announced problems, the blank pair-row table, the blank handoff table and the blank report form before comparing with the answer key with reasoning, the pair-row answer key and the handoff answer key Check the source/SHA-256 from manifest
1. Read the callback and snapshot · I1–I2
Separate the work in the ISR from that in main, then read the sequence savedMask → disable IRQ → copy tick/timestamp → restore mask. Hint: volatile alone does not guarantee a two-field snapshot, and the IRQ state the caller already had should be restored. Reading the source is not a concurrency test result on the board
2. Trace the handoff and the pair rows · I3–I5
Fill in the nine calls, separating the initial of normal from wrap, then calculate all seven row pairs, separating per-row missed from cumulative dropped. Hint: Ready acts once on the latest value; Idle/Invalid issue no new missed, so leave the result cell blank. A task whose row could not be sent may still have been done, but for service/work in a row not in the CSV, answer that it is unknown
3. Wrap and measurement points · I6–I7
Calculate tick 4,294,967,294→1 with uint32, then check the delta at the handoff's 2^31 boundary, and compare the work problems 1,999/2,000/2,001 µs with the 2,000 budget. Hint: use the sign > ; a value equal to the boundary does not exceed the budget, and service from the ISR timestamp to main starting the LED is not the IRQ latency from the hardware deadline. The work boundary differs from polling, so do not compare it directly as a benchmark
4. Write a report within the limits of observation · I8
Choose one case and cite the file, row, pair and unit. Separate what is seen → what is still unknown → additional evidence . Hint: tick counts invoked callbacks; several hardware updates may merge into a single callback while IRQs are disabled, so missed 0 is not used to certify that the timer caught every event. The earlier compile result confirms the target build, while upload/LED/Serial/timing on the board must be checked separately
Self-check: 2 proposed per question (1 answer + 1 reason/unit/boundary), 16 in total, for choosing what to review, separate from the original worksheet and not learner results. This round checked the sources, bytes, numbers and explanations; no new analyzer/host tests/compile/upload/SITL were run. When you can explain it, continue to the data queue from the ISR
Choose the next practice kit or review
Back to the table of contents of the 10 MCU practice kits to choose a problem according to your goal and the prerequisites you should know, or take the 12-question comprehension assessment to find topics to review
After checking the answer key, record the answers you corrected, the reasons, and what the evidence still does not confirm, then choose the next kit that helps answer that question. The worksheet score and the assessment are separate; use them to choose your own review