Files
luban-lite-t3e-pro/packages/third-party/FreeRTOS-Wrapper/test/test_queue_dynamic.c
刘可亮 7bbc029dae v1.0.0
2023-08-30 16:21:18 +08:00

99 lines
2.2 KiB
C

/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-08-24 yangjie the first version
* 2020-10-17 Meco Man translate to English comment
*/
/*
* Demo: queue
*
* This demo demonstrates dynamically creating a queue and using it to send data between two tasks.
*
*/
#include <FreeRTOS.h>
#include <queue.h>
#include <task.h>
#define TASK_PRIORITY (FINSH_THREAD_PRIORITY + 1)
#define QUEUE_LENGTH 10
#define ITEM_SIZE sizeof( uint32_t )
/* queue handler */
static QueueHandle_t xQueue = NULL;
static TaskHandle_t TaskHandle1 = NULL;
static TaskHandle_t TaskHandle2 = NULL;
static void vTask1Code(void *pvParameters)
{
BaseType_t xReturn;
uint32_t num = 0;
while (1)
{
xReturn = xQueueReceive(xQueue, &num, portMAX_DELAY);
if (xReturn != pdPASS)
{
rt_kprintf("Task 1 receive from queue failed\n");
}
else
{
rt_kprintf("Task 1 receive data %d from queue\n", num);
}
if (num >= 100)
{
return;
}
}
}
static void vTask2Code(void * pvParameters)
{
BaseType_t xReturn;
uint32_t num = 0;
while (1)
{
xReturn = xQueueSendToBack(xQueue, &num, 0);
if (xReturn != pdPASS)
{
rt_kprintf("Task 2 send to queue failed\n");
}
num += 1;
if (num >= 100)
{
return;
}
}
}
int queue_dynamic(void)
{
/* Create a queue dynamically */
xQueue = xQueueCreate(QUEUE_LENGTH, ITEM_SIZE);
if (xQueue == NULL)
{
rt_kprintf("create dynamic queue failed.\n");
return -1;
}
xTaskCreate( vTask1Code, "Task1", configMINIMAL_STACK_SIZE, NULL, TASK_PRIORITY + 1, &TaskHandle1 );
if (TaskHandle1 == NULL)
{
rt_kprintf("Create task 1 failed\n");
return -1;
}
xTaskCreate( vTask2Code, "Task2", configMINIMAL_STACK_SIZE, NULL, TASK_PRIORITY, &TaskHandle2 );
if (TaskHandle2 == NULL)
{
rt_kprintf("Create task 2 failed\n");
return -1;
}
return 0;
}
MSH_CMD_EXPORT(queue_dynamic, queue sample);