IntroductionIn this era embedded systems must handle multiple tasks at the same time while maintaining timing. This project, a time driven foreground/background scheduling system is built on STM32F407ZGT6 microcontroller using the RT-Thread RT-Spark development board and demonstrate the use of hardware timers, interrupt service routines, cyclic executive scheduling and event triggered interrupts.
Hardware SetupComponents Used- RT-Thread RT-Sparkk Development Board
- STM32CubeIDE
- STM32CubeMX
Pin Configuration
This project uses a foreground/Background system architecture.
Background Tasks (Interrupts)TIM2 Interrupt (1 Hz)
- Toggles the Red LED
- Sets the
task_adc_readyflag - Increments the system tick counter
- TIM2 Interrupt (1 Hz)Toggles the Red LEDSets the
task_adc_readyflagIncrements the system tick counter
TIM3 Interrupt (2 Hz)
- Toggles the Blue LED
- Sets the
task_display_readyflag - TIM3 Interrupt (2 Hz)Toggles the Blue LEDSets the
task_display_readyflag
EXTI Interrupt (Button Press)
- Immediately toggles both LEDs
- EXTI Interrupt (Button Press)Immediately toggles both LEDs
Task A
- Triggered by TIM2
- Simulates ADC processing
- Execution Time: 50 ms
- Task ATriggered by TIM2Simulates ADC processingExecution Time: 50 ms
Task B
- Triggered by TIM3
- Simulates display update
- Execution Time: 20 ms
- Task BTriggered by TIM3Simulates display updateExecution Time: 20 ms
TIM2
- Prescaler: 8399
- Auto Reload Register: 9999
- Frequency: 1 Hz
- Priority: 0 (Highest)
TIM3
- Clock Source: Internal Clock
- Prescaler: 8399
- Auto Reload Register: 4999
- Frequency: 2 Hz
- Priority: 1
The timer callback function determines which timer generated the interrupt and executes the appropriate background task.
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM2)
{
HAL_GPIO_TogglePin(LED_RED_GPIO_Port, LED_RED_Pin);
task_adc_ready = 1;
tick_count++;
}
if(htim->Instance == TIM3)
{
HAL_GPIO_TogglePin(LED_BLUE_GPIO_Port, LED_BLUE_Pin);
task_display_ready = 1;
}
}The user button was configure as an external interrupt source.
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{ if(GPIO_Pin == GPIO_PIN_0)
{
HAL_GPIO_TogglePin(LED_RED_GPIO_Port, LED_RED_Pin);
HAL_GPIO_TogglePin(LED_BLUE_GPIO_Port, LED_BLUE_Pin);
}
}
ResultAfter uploading the firmware to the RT-Spark board, the following behavior was observed:
- Red LED blinked every 1 second
- Blue LED blinked every 0.5 second
- Task A executed when the TIM2 interrupt flag was set
- Task B executed when the TIM3 interrupt flag was set
- Button presses immediately toggled both LEDs
- Interrupt priorities functioned correctly
Measured latency for task A was approximately 72ms. This latency happens because the cyclic executive architecture requires the main loop to complete any currently running tasks.The response time was calculated as:
TResponse = TLatency + TTask
TResponse = 72 ms + 50 ms = 122 ms
Github repo link: https://github.com/Dian-qit/QUITORIAN_Timer_Interrupts_Lab1




Comments