cancel
Showing results for 
Search instead for 
Did you mean: 

How do ThreadX Message Queues work on STM32 part 3

B.Montanari
ST Employee

How do ThreadX Message Queues work on STM32?

Welcome back to the third part of the "How do ThreadX Message Queues work on STM32" article. 

1. How to add Sender2 thread

Now that the first demo is completed, let’s add our Sender2 thread and evaluate the behavior. To do so, let’s add these code lines. To ease the process, here is the full code with the Sender2 additional code added, so you can simply replace the previous one:
 
/* USER CODE BEGIN PV */
uint8_t thread_sender1[THREAD_STACK_SIZE];
uint8_t thread_sender2[THREAD_STACK_SIZE];
uint8_t thread_receiver[THREAD_STACK_SIZE];
uint8_t   queue_stack[QUEUE_STACK_SIZE];
TX_THREAD sender1_ptr;
TX_THREAD sender2_ptr;
TX_THREAD receiver_ptr;
TX_QUEUE  queue_ptr;
/* USER CODE END PV */
/* USER CODE BEGIN PFP */
VOID Sender1 (ULONG initial_input);
VOID Sender2 (ULONG initial_input);
VOID Receiver (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(&sender2_ptr,"Sender2",Sender2, 0, thread_sender2,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 1 */
VOID Sender2 (ULONG initial_input){
	/* Infinite loop */
	uint32_t message2=2;
	for(;;)	{
	    printf("Sender2\r\n");
	    tx_queue_send(&queue_ptr,&message2,TX_NO_WAIT);
	    printf("Sender2 delay\r\n");
		/* Sleep for 2seconds */
		tx_thread_sleep(200);
	}
}
As you can see, the Sender2 will always send the message ‘2’ and the Sender1 will continue to increase its message value by one at every interaction.
Press Ctrl+B to build and enter in debug mode. By allowing the project to run, we’ll see that the order printed is:
204.png
Which is the exact flow as we anticipated:
205.png
Now, as the final change, modify the priority and preemption priority of the Receiver thread to see how we can change things a bit.
Just to recall, this is how the priority and preemption threshold parameters works:
  • priority: Numerical priority of thread. Legal values range from 0 through (TX_MAX_PRIORITES-1), where a value of 0 represents the highest priority.
  • preempt_threshold: Highest priority level (0 through (TX_MAX_PRIORITIES-1)) of disabled preemption. Only priorities higher than this level are allowed to preempt this thread. This value must be less than or equal to the specified priority. A value equal to the thread priority disables preemption-threshold.
The Receiver thread should be updated to be like this:
tx_thread_create(&receiver_ptr,"Receiver",Receiver, 0, thread_receiver,THREAD_STACK_SIZE,10,10,1,TX_AUTO_START);
Now that the Receiver thread has a higher priority, we know it will be the first thread to be called when the application starts, plus it can preempt the Sender1 and Sender2 threads whenever the queue is available and it can only be preempted by a priority 10 thread, which is not present on the demo, thus we can expect this to happen:
206.png
And as we can see upon building and debugging, it is the expected flow:
207.png
This was it for the queue article, hope you’ve enjoyed it!

2. Useful links:

3. Conclusion: 

Adding queue to a ThreadX application consists of:
    • Adding the ThreadX component in STM32CubeMX/STM32CubeIDE
    • Adapt ThreadX configuration in STM32CubeMX/ STM32CubeIDE
    • Generate the code from STM32CubeMX/ STM32CubeIDE
      1. This will update the project structure with all needed files
    • Use ThreadX API to create ThreadX components thread and queue
    • Have fun 🙂

 
Version history
Last update:
‎2021-12-14 07:34 AM
Updated by: