How do ThreadX Message Queues work on STM32 part 2
This is the part 2 of the article to explain how the ThreadX Message Queue work on STM32 with working examples. Welcome back!
1. STM32CubeMX – step by step demo:
Launch STM32CubeMX (version 6.18.1). Once the application opens, click File > New Project. For this demo, use the NUCLEO-H723ZG board as the starting point.
Enable USART3 and configure it to 115200 baud rate and them make sure that the PD8/PD9 pins are selecting for the TX/RX pins

Time to add the software pack:
Browse and locate the AzureRTOS pack in the component selection window:
Open the RTOS ThreadX and check the Core box and click OK
This will add the Software Pack in the lower part of the Categories:
Now, by clicking on it, you can add the RTOS ThreadX box, this will show the configuration options for the AzureRTOS application

By default, the HAL driver will use the Systick as its primary time base, but this timer should be left to the AzureRTOS only.
If the HAL library doesn’t have a separate time source, the compilation will fail because both libraries want to use the SysTick_Handler interrupt. To prevent this, we can simply select a different time base for the HAL by clicking in the System Core/SYS and selecting the time base Source as TIM6:
In Project Manager, create a project name, select STM32CubeIDE as the integrated development environment (IDE), and then click Generate Code.

The project now has a new set of folders, this is the detail of its structure:
The last part is to create our demo code. Just to recap, this first portion of the demo will use a single sender and receiver, using a queue to send a message:
Open Core\Src \app_threadx.c – Here we’ll create the 2 threads using the command tx_thread_create and the message queue, by using tx_queue_create.
The Sender1 thread will send the queue message, increment the content and then sleep for 2 seconds, to avoid spamming the terminal. The Receiver thread is always in suspended mode waiting for the queue to be received. You can copy and paste the code below, please use the USER CODE to locate the proper region to paste it:
/* USER CODE BEGIN Includes */
#include "main.h"
#include "stdio.h"
/* USER CODE END Includes */
/* USER CODE BEGIN PD */
#define QUEUE_STACK_SIZE 128
#define THREAD_STACK_SIZE 512
/* USER CODE END PD */
/* USER CODE BEGIN PV */
TX_QUEUE queue_ptr;
TX_THREAD receiver_ptr;
TX_THREAD sender1_ptr;
uint8_t queue_stack[QUEUE_STACK_SIZE];
uint8_t thread_receiver[THREAD_STACK_SIZE];
uint8_t thread_sender1[THREAD_STACK_SIZE];
/* USER CODE END PV */
/* USER CODE BEGIN PFP */
VOID Receiver(ULONG initial_input);
VOID Sender1(ULONG initial_input);
/* USER CODE END PFP */
UINT App_ThreadX_Init(VOID *memory_ptr)
{
UINT ret = TX_SUCCESS;
TX_BYTE_POOL *byte_pool = (TX_BYTE_POOL*)memory_ptr;
/* USER CODE BEGIN App_ThreadX_MEM_POOL */
(void)byte_pool;
/* USER CODE END App_ThreadX_MEM_POOL */
/* USER CODE BEGIN App_ThreadX_Init */
tx_queue_create(&queue_ptr, "Queue0", 1, queue_stack, 128);
tx_thread_create(&sender1_ptr, "Sender1", Sender1, 0, thread_sender1, THREAD_STACK_SIZE, 15, 15, 1, TX_AUTO_START);
tx_thread_create(&receiver_ptr, "Receiver", Receiver, 0, thread_receiver, THREAD_STACK_SIZE, 15, 15, 1, TX_AUTO_START);
/* USER CODE END App_ThreadX_Init */
return ret;
}
/* USER CODE BEGIN 2 */
VOID Sender1(ULONG initial_input)
{
/* Infinite loop */
uint32_t message1 = 0;
for (;;)
{
printf("Sender1\r\n");
tx_queue_send(&queue_ptr, &message1, TX_NO_WAIT);
printf("Sender1 delay\r\n");
message1++;
/* Sleep for 2seconds */
tx_thread_sleep(200);
}
}
VOID Receiver(ULONG initial_input)
{
uint32_t message_received;
for (;;)
{
tx_queue_receive(&queue_ptr, &message_received, TX_WAIT_FOREVER);
printf("message received:%lu \r\n", message_received);
}
}
/* USER CODE END 2 */
Open Core\Src \main.c – you can copy and paste the code below:
/* USER CODE BEGIN 0 */
void __io_putchar(char ch)
{
// Code to write character 'ch' on the UART
HAL_UART_Transmit(&huart3, (uint8_t *)&ch, 1, 10);
}
/* USER CODE END 0 */
From this point, press Ctrl+B to build and you should see 0 errors and 0 warnings.
Connect the board to the computer and enter in debug mode by first clicking in the project name, then menu Run/Debug As/STM32. In Edit Configuration window, click Debug:
Once in the Debug perspective, click in the resume button to enjoy your ThreadX application with mutex working. You can also use the Window/Show View/ThreadX/ThreadX Thread List and do it again for the ThreadX Queue to enhance the overall debugging options (the thread list will be automatically loaded once the application runs for a bit and is paused):
Now, the last step is to enable the console inside the STM32CubeIDE to see the print messages. Locate the Console tab and click Command Shell Console:
In the pop-up window, change the Connection Type to Serial Port and click on New… to add the VCOM related to your STLINK:
You can give the port a name, so it is easier for later usage as well, in this case, it will be labelled as NUCLEO_H723ZI:
By clicking finish, the message on the console will indicate it is now connected.
Here is the debug view when adding a breakpoint in the Receiver Thread with the views for threads and queues enabled. It is possible to see the size of the queue as 32, and this is because upon the queue creation it was set to have a single word (32bit long or 4 byte long) with a stack size of 128 bytes, so 128/4 = 32. You can also check that the Message Queue tab is showing that currently, the queue has 1 message there, thus the Free size is 31:
By removing the breakpoint, adding the console view and hitting F8, we can see the code running with the messages being displayed:
On the third and last part , we'll add the second sender thread and explore the priority change effects.
See you soon!
https://community.st.com/s/article/how-do-threadx-message-queues-work-on-stm32-part-3
Edited by
