Ever wondered how microcontrollers handle multiple tasks at the same time? This project shows how timer interrupts and task scheduling work on the STM32F407ZGT6 RT-Spark board. Two timers blink LEDs at different rates while a button interrupt responds instantly to presses — all running together using a simple foreground/background system.
HardwareSetup
The project uses the RT-Thread RT-Spark development board built around the STM32F407ZGT6 microcontroller. Two onboard LEDs are connected to pins PF11 (Red) and PF12 (Blue), a user button is connected to PA0, and a debug pin is set up on PE0 for timing measurements.
SoftwareArchitecture
The system uses a foreground/background architecture. Timer interrupts run in the background as ISRs, setting flags when tasks are ready. The main loop in the foreground checks these flags and executes the corresponding tasks. TIM2 runs at 1Hz with the highest priority, TIM3 runs at 2Hz with medium priority, and the button EXTI interrupt has the lowest priority among the three.
ImplementationTIM2 and TIM3 were configured in STM32CubeMX with their respective prescaler and auto-reload values to achieve the target frequencies. The HAL_TIM_PeriodElapsedCallback function handles both timer ISRs, toggling LEDs and setting task flags. The main loop processes Task A when TIM2 fires, Task B when TIM3 fires, and Task C runs a periodic 10-second check. A button interrupt on PA0 toggles both LEDs immediately when pressed, demonstrating event-driven response.
This video shows the RT-Spark board in action — the red and blue LEDs blinking at different rates using timer interrupts, and both LEDs toggling instantly when the button is pressed.
TimingMeasurement
During testing, the red LED blinked every 1 second as triggered by TIM2, while the blue LED blinked every 0.5 seconds as triggered by TIM3. When the user button was pressed, both LEDs responded immediately, confirming that the external interrupt works correctly. The foreground tasks executed properly whenever the timer flags were set.
AnalysisThe measured TLatency of 24.94 µs shows the small delay between the interrupt firing and the task starting. The TISR of 4.31 µs confirms that the ISR executes very quickly. The total response time of 50.02443 ms is close to the expected 50 ms task delay. The worst-case response time is 120 ms when all tasks are running simultaneously. Using a preemptive scheduler would reduce this latency significantly since higher priority tasks could run immediately without waiting for other tasks to finish.



Comments