FreeRTOS二值信号量

本文详细介绍了FreeRTOS中二值信号量的创建、发送及接收等操作,并通过一个具体的UART中断处理实例展示了如何在任务间同步使用二值信号量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

API函数

#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
    #define xSemaphoreCreateBinary() xQueueGenericCreate( ( UBaseType_t ) 1, 
                                semSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_BINARY_SEMAPHORE )

    QueueHandle_t xQueueGenericCreate( const UBaseType_t uxQueueLength, 
                                    const UBaseType_t uxItemSize, const uint8_t ucQueueType )
#endif

#define xSemaphoreGive( xSemaphore )        
xQueueGenericSend( ( QueueHandle_t ) ( xSemaphore ), NULL, semGIVE_BLOCK_TIME, queueSEND_TO_BACK )
BaseType_t xQueueGenericSend( QueueHandle_t xQueue, const void * const pvItemToQueue, 
                            TickType_t xTicksToWait, const BaseType_t xCopyPosition )

#define xSemaphoreGiveFromISR( xSemaphore, pxHigherPriorityTaskWoken )  
xQueueGiveFromISR( ( QueueHandle_t ) ( xSemaphore ), ( pxHigherPriorityTaskWoken ) )
BaseType_t xQueueGiveFromISR( QueueHandle_t xQueue, BaseType_t * const pxHigherPriorityTaskWoken )

#define xSemaphoreTake( xSemaphore, xBlockTime )        
xQueueGenericReceive( ( QueueHandle_t ) ( xSemaphore ), NULL, ( xBlockTime ), pdFALSE )
BaseType_t xQueueGenericReceive( QueueHandle_t xQueue, void * const pvBuffer, 
                                TickType_t xTicksToWait, const BaseType_t xJustPeeking )

#define xSemaphoreTakeFromISR( xSemaphore, pxHigherPriorityTaskWoken )  
xQueueReceiveFromISR( ( QueueHandle_t ) ( xSemaphore ), NULL, ( pxHigherPriorityTaskWoken ) )
BaseType_t xQueueReceiveFromISR( QueueHandle_t xQueue, void * const pvBuffer, 
                                BaseType_t * const pxHigherPriorityTaskWoken )

使用举例

u8 data;

void start_task(void *pvParameters)
{
    //创建二值信号量
    BinarySemaphore = xSemaphoreCreateBinary();
}

void USART1_IRQHandler(void)
{
    if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
    {
        data = USART_ReceiveData(USART1);

        xSemaphoreGiveFromISR(BinarySemaphore, &xHigherPriorityTaskWoken);  //释放二值信号量

        portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
    }
}

void data_task(void *pvParameters)
{
    BaseType_t ret;

    while(1)
    {
        ret = xSemaphoreTake(BinarySemaphore,portMAX_DELAY);
        if(ret == pdTRUE)
        {
            printf("data %c\r\n", data);
        }

        vTaskDelay(10);
    }
}

实验现象
1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值