on
2021-12-14
7:34 AM
- edited on
2025-03-17
6:53 AM
by
Laurids_PETERSE
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.
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:
Which is the exact flow as we anticipated:
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:
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:
And as we can see upon building and debugging, it is the expected flow:
This was it for the queue article, hope you’ve enjoyed it!
Adding queue to a ThreadX application consists of: