Home | Projects | Notes > Real-Time Operating Systems (RTOS) > Exercise: Queues and Timers (09_Queues_and_Timers
)
09_Queues_and_Timers
)
Write a FreeRTOS application that takes input (commands) from the user over USART and handles LED and real-time clock (RTC) peripheral of the microcontroller.
This application implements:
Processing user sent commands over USART
Handling of FreeRTOS queues
Print queue
Input data queue
FreeRTOS software timers
Menu task
Sends the pointer to the message to be printed on the console to the "print queue".
It does not directly control the USART peripheral.
LED task
RTC task
Print task
Receives from the "print queue" the pointer to the message to be printed.
The only task to control the USART peripheral to handle writing data to the console
Command handling task
Whenever the USART ISR detects a new line character \n
, it will send a notification to the "command handling task." Subsequently, the "command handling task" will dequeue the entire command from the "input data queue" and format the data into a command. And then, this command will notify the relevant tasks.
Enable USART data reception (byte-by-byte) in IT mode (HAL_UART_Receive_IT()
)
Byte-by-byte reception since the length of the data to be received is not known.
Implement USART receive complete callback (HAL_UART_RxCpltCallback()
which is defined as a __weak
function in stm32f4xx_hal_uart.c
) in main.c
. (Since this callback is called from the USART interrupt handler, it will run in the handler mode of the processor.)
Store the data byte into input data queue
When \n
is detected, notify the command handling task (cmd_task
)
Create a new project
Create main.c
, task_hanlder.c
, led_effect.c
, and rtc.c
In the main function, create:
5 tasks (use the same priority for all tasks
xxxxxxxxxx
31/* Example */
2
3status = xTaskCreate(menu_task, "menu_task", 250, NULL, 2, &handle_menu_task);
2 queues
Go to the "Device Configuration Tool" and check the followings:
In the "GPIO" section, check if PD12
, PD13
, PD14
, and PD15
, are configured as:
"Output Push Pull" mode
"No pull-up and pull-down"
In the "USART2" section,
Change mode to "Asynchronous"
"NVIC Settings" tab
In the "SYS" section, change the timebase to anything other than the SysTick timer. (e.g., TIM6)
In the "NVIC" section,
Enable the USART2 global interrupt and give it a preemption priority of 5. (Any value >= 5)
Make sure that the "SysTick interrupt", "PendSV interrupt" and "SVC interrupt" generations are disabled, and the "USART2 global interrupt" generation is enabled.
In the "RTC" section,
Activate the clock source and change the "Hour Format" to "Hourformat 12"
Now, save the configuration and generate the code.
In this project, we are using USART2 for communication. Therefore, we are not going to be using the SEGGER SystemView for this project. Go ahead and exclude the Project/Common/ThirdParty/SEGGER/
folder from build.
Make sure to remove or comment out
#include "SEGGER_SYSVIEW_FreeRTOS.h
from theFreeRTOSConfig.h
file to remove the compilation errors.
Create (or import FreeRTOSConfig.h
) file into the project. And, make sure the following items are enabled:
xxxxxxxxxx
71/* Project/Core/Inc/FreeRTOSConfig.h */
2
3...
4
5...
6// Since we are using the software timer
7
Import the include paths settings.
See the source code: https://github.com/kyungjae-lee/freertos-projects/tree/main/Workspace/09_Queues_and_Timers.
What can be used for inter task communication?
Let's say a queue is full, and a task1 of priority 5 was blocked on a queue while its attempt to write on that queue, now lets say task2 of priority 3 removes a data item from that queue. Do you think the moment task 2 removes a data item, it will be preempted by task 1?
Can the Queue APIs be called from ISR in FreeRTOS?
Are the Queue APIs ending with "FromISR" allowed to do task yielding?
Can a task block on a queue indefinitely?
Can a task choose not to block on a queue?
Describe what will happen when the following code gets executed (Assume : 32bit Processor):
xQueueCreate( 5, sizeof(uint32_t) )
Can we use Queue for synchronization between tasks or between task and an interrupt?
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/