FreeRTOS互斥信号量

API函数

#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
    #define xSemaphoreCreateMutex() xQueueCreateMutex( queueQUEUE_TYPE_MUTEX )
#endif

QueueHandle_t xQueueCreateMutex( const uint8_t ucQueueType )

举例

//高优先级任务的任务函数
void high_task(void *pvParameters)
{
    while(1)
    {
        vTaskDelay(500);    //延时500ms,也就是500个时钟节拍   
        printf("high task Pend Sem
");
        xSemaphoreTake(MutexSemaphore,portMAX_DELAY);   //获取互斥信号量
        printf("high task Running!
");
        xSemaphoreGive(MutexSemaphore);                 //释放信号量
        vTaskDelay(500);    //延时500ms,也就是500个时钟节拍  
    }
}

//中等优先级任务的任务函数
void middle_task(void *pvParameters)
{
    while(1)
    {
        printf("middle task Running!
");
        vTaskDelay(1000);   //延时1s,也就是1000个时钟节拍 
    }
}

//低优先级任务的任务函数
void low_task(void *pvParameters)
{
    static u32 times;

    while(1)
    {
        xSemaphoreTake(MutexSemaphore,portMAX_DELAY);   //获取互斥信号量
        printf("low task Running!
");
        for(times=0;times<5000000;times++)              //模拟低优先级任务占用互斥信号量
        {
            taskYIELD();                                //发起任务调度
        }
        xSemaphoreGive(MutexSemaphore);                 //释放互斥信号量
        vTaskDelay(1000);   //延时1s,也就是1000个时钟节拍 
    }
}

实验现象
1

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