FreeRTOS二值信号量

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
", data);
        }

        vTaskDelay(10);
    }
}

实验现象
1

原文地址:https://www.cnblogs.com/zhangxuechao/p/11709476.html