Home | Projects | Notes > Real-Time Operating Systems (RTOS) > Exercise: Task Delay (03_LED_Tasks_Delay
, 04_LED_Tasks_Periodic
)
03_LED_Tasks_Delay
, 04_LED_Tasks_Periodic
)
03_LED_Tasks_Delay
)Toggle 3 LEDs of the STM32F407 Discovery board with the duration as shown in the graph below.
Create 3 FreeRTOS tasks of the same priority to handle three different LDEs.
Use vTaskDelay()
for delay implementation.
Create a project.
Link Common
folder into the project. (Make sure to uncheck Exclude resource from build
option. Otherwise, you'll end up with a bunch of "undefined reference" errors.)
Import "Include paths" by using previously saved configuration files.
Copy FreeRTOSConfig.h
from an earlier project into Project/Core/Inc/
, and update it according to your project's requirement.
Make sure:
xxxxxxxxxx
21/* FreeRTOSConfig.h */
2// necessary to use vTaskDelay() API
Copy main.c
from an earlier project (i.e., Toggle LEDs) and update it to your needs.
xxxxxxxxxx
31/* main */
2// HAL_Delay(1000); // previously (blocking delay)
3vTaskDelay(pdMS_TO_TICKS(1000)); // non-blocking delay
Copy from an earlier project's (i.e., Toggle LEDs) main.h
the user defined macros:
xxxxxxxxxx
51/* USER CODE BEGIN Private defines */
2
3
4
5/* USER CODE END Private defines */
In Project/Core/Src/stm32f4xx_hal_msp.c
, add `vInitPrioGroupValue();
as shown in L15. And, include FreeRTOSConfig.h
.
xxxxxxxxxx
231/* stm32f4xx_hal_msp.c */
2
3/* USER CODE BEGIN Includes */
4
5/* USER CODE END Includes */
6
7void HAL_MspInit(void)
8{
9 /* USER CODE BEGIN MspInit 0 */
10
11 /* USER CODE END MspInit 0 */
12
13 __HAL_RCC_SYSCFG_CLK_ENABLE();
14 __HAL_RCC_PWR_CLK_ENABLE();
15
16 HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_0);
17
18 /* System interrupt init*/
19
20 /* USER CODE BEGIN MspInit 1 */
21 vInitPrioGroupValue();
22 /* USER CODE END MspInit 1 */
23}
Do the "Clock Setting", "Timebase Source Selection", the "Priority Group Setting", and disable the code generation of SysTick, SVC, and PendSV handlers. (See the previous exercises for more detailed information.)
If the clock setting (HCLK = 168) is not properly done, the system cannot handle the baudrate of 500000 and you won't be able to see anything (or even get it to work) on SystemView application.
Flash the program onto the board, analyze the trace on SystemView.
Unlike in the exercise "Toggle LEDs" where we used for
/while
loop-based delay, CPU load is much less now with the blocking delay. (CPU is running the idle task most of the time in this project, but we can make the CPU to sleep instead of running the idle task to save power.)
04_LED_Tasks_Periodic
)Toggle 3 LEDs of the STM32F407 Discovery board with the duration as shown in the graph below.
Create 3 FreeRTOS periodic tasks of the same priority to handle three different LDEs.
Use vTaskDelayUntil()
for delay implementation.
Use the project "LED_Block_Task", and replace the vTaskDelay()
APIs with vTaskDelayUntil()
and do the necessary variable declarations and initializations. Consult the FreeRTOS documentation, it has an example that shows how this API should be used.
xxxxxxxxxx
181/* main.c */
2
3...
4/* USER CODE BEGIN 4 */
5
6static void led_green_task_handler(void *parameters)
7{
8 // Create an initialize a variable to record the last wakeup time
9 TickType_t last_wakeup_time = xTaskGetTickCount();
10
11 while (1)
12 {
13 SEGGER_SYSVIEW_PrintfTarget("Toggling green LED");
14 HAL_GPIO_TogglePin(GPIOD, LED_GREEN_PIN);
15 vTaskDelayUntil(&last_wakeup_time, pdMS_TO_TICKS(1000));
16 }
17}
18...
L8, L9, L15 are updated from the previous project.
Flash the program onto the board, analyze the trace on SystemView.
Nayak, K. (2022). Mastering RTOS: Hands on FreeRTOS and STM32Fx with Debugging [Video file]. Retrieved from https://www.udemy.com/course/mastering-rtos-hands-on-with-freertos-arduino-and-stm32fx/