Author | strongerHuang
WeChat Official Account | Embedded Column
Embedded Column
1
Create a mutex Task A occupies the resource Use the resource (send data) Task A releases the resource Higher priority Task B occupies the resource Use the resource Task B releases the resource In turn, higher priority tasks occupy the resource · · ·
Code:
// Create mutex resourceSemaphoreHandle_t xSemaphore = NULL;xSemaphore = xSemaphoreCreateMutex();
void TaskA(void *pvParameters){ for(;;) { // Occupy resource if(xSemaphoreTake(xSemaphore, 10 ) == pdTRUE) { // Use resource (send data) USART_SendNByte(); // Release resource xSemaphoreGive(xSemaphore); } }}
Embedded Column
2
Create a queue (send data queue) Create a task (UART send data task) Task A adds to the queue Task B adds to the queue · · · On the other side, tasks read queue data in order and use UART to send it out.
Code:
QueueHandle_t xQueue;xQueue = xQueueCreate(QUEUE_LENGTH, QUEUE_ITEM_SIZE);
xTaskCreate(UART_Send_Task, "UART_Send", STACK_SIZE, NULL, TASK_PRIORITY, NULL);
void TaskA(void *pvParameters){ for(;;) { // Task related operations
// Add to queue xQueueSend(xQueue, &TaskA_Buf, 10) }}
void TaskB(void *pvParameters){ for(;;) { // Task related operations
// Add to queue xQueueSend(xQueue, &TaskB_Buf, 10) }}
void UART_Send_Task(void *pvParameters){ for(;;) { // Loop read queue BUF if(xQueueReceive(xQueue, &Buf, 10) == pdTRUE) { USART_SendNByte(&Buf); } }}
The above two methods are commonly used and relatively simple. I hope they are helpful to everyone.
Note:The code is for learning and understanding principles only. In projects, code should be added, deleted, or modified according to actual situations.
Reply “STM32” or “FreeRTOS” or “Embedded Software Design and Development” to read more related articles.
Click “Read Original” to see more shares. Please feel free to share, collect, like, and view.