API函数
//创建
#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
#define xQueueCreate( uxQueueLength, uxItemSize )
xQueueGenericCreate( ( uxQueueLength ), ( uxItemSize ), ( queueQUEUE_TYPE_BASE ) )
#endif
QueueHandle_t xQueueGenericCreate( const UBaseType_t uxQueueLength,
const UBaseType_t uxItemSize, const uint8_t ucQueueType )
#define xQueueSend( xQueue, pvItemToQueue, xTicksToWait )
xQueueGenericSend( ( xQueue ), ( pvItemToQueue ), ( xTicksToWait ), queueSEND_TO_BACK )
BaseType_t xQueueGenericSend( QueueHandle_t xQueue, const void * const pvItemToQueue,
TickType_t xTicksToWait, const BaseType_t xCopyPosition )
#define xQueueReceive( xQueue, pvBuffer, xTicksToWait )
xQueueGenericReceive( ( xQueue ), ( pvBuffer ), ( xTicksToWait ), pdFALSE )
BaseType_t xQueueGenericReceive( QueueHandle_t xQueue, void * const pvBuffer,
TickType_t xTicksToWait, const BaseType_t xJustPeeking )
//队列剩余大小
UBaseType_t uxQueueSpacesAvailable( const QueueHandle_t xQueue )
//队列使用大小
UBaseType_t uxQueueMessagesWaiting( const QueueHandle_t xQueue )
使用举例
QueueHandle_t Key_Queue //按键值消息队列句柄
void start_task(void *pvParameters)
{
Key_Queue = xQueueCreate(1, sizeof(u8));
if(Key_Queue == 0)
{
printf("xQueueCreate err\r\n");
}
}
void key_task(void *pvParameters)
{
BaseType_t ret;
while(1)
{
key = KEY_Scan(0); //扫描按键
if((Key_Queue != 0) && (key)) //消息队列Key_Queue创建成功,并且按键被按下
{
ret = xQueueSend(Key_Queue, &key, 10);
if(ret != pdPASS) //发送按键值
{
printf("xQueueSend err\r\n");
}
}
vTaskDelay(10);
}
}
void recv_task(void *pvParameters)
{
while(1)
{
if(Key_Queue != 0)
{
if(xQueueReceive(Key_Queue, &key, portMAX_DELAY))
{
printf("recv_task key %d\r\n", key);
}
}
}
}
实验现象
点击按键
中断相关API函数
#define xQueueSendFromISR( xQueue, pvItemToQueue, pxHigherPriorityTaskWoken )
xQueueGenericSendFromISR( ( xQueue ), ( pvItemToQueue ),
( pxHigherPriorityTaskWoken ), queueSEND_TO_BACK )
BaseType_t xQueueGenericSendFromISR( QueueHandle_t xQueue, const void * const pvItemToQueue,
BaseType_t * const pxHigherPriorityTaskWoken, const BaseType_t xCopyPosition )
BaseType_t xQueueReceiveFromISR( QueueHandle_t xQueue, void * const pvBuffer,
BaseType_t * const pxHigherPriorityTaskWoken )
使用举例
void start_task(void *pvParameters)
{
Message_Queue = xQueueCreate(1, sizeof(u8));
if(Message_Queue == 0)
{
printf("xQueueCreate err\r\n");
}
}
void USART1_IRQHandler(void)
{
u8 Res;
BaseType_t xHigherPriorityTaskWoken;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
Res = USART_ReceiveData(USART1);
xQueueSendFromISR(Message_Queue, &Res, &xHigherPriorityTaskWoken);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken); //如果需要的话进行一次任务切换
}
}
void TIM2_IRQHandler(void)
{
u8 Res;
BaseType_t ret;
BaseType_t xTaskWokenByReceive = pdFALSE;
if(TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
ret = xQueueReceiveFromISR(Message_Queue, &Res, &xTaskWokenByReceive);
if(ret == pdTRUE)
{
printf("xQueueReceiveFromISR Res %c\r\n", Res);
}
portYIELD_FROM_ISR(xTaskWokenByReceive); //如果需要的话进行一次任务切换
}
}
实验现象