FreeRTOS--二元信号量--任务间的同步
了解了二元信号量相关的一些概念,可以设计相关程序进行验证,基于STM32Cube生成的代码。
TASK1比TASK2优先级高,验证的初衷是TASK1S首先运行,而TASK2由于无法获取信号量而阻塞,5S之后TASK1释放信号量,TASK2获得信号量解除阻塞可以执行
由于FreeRTOS的新旧版本的API不同,导致现象不同于预期,问题就在xSemaphoreCreateBinary与vSemaphoreCreateBinary的区别
用vSemaphoreCreateBinary创建的二元信号量,初始值为“满”,因为创建的同时释放了信号量
1 #define vSemaphoreCreateBinary( xSemaphore ) \ 2 { \ 3 ( xSemaphore ) = xQueueGenericCreate( ( unsigned portBASE_TYPE ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_BINARY_SEMAPHORE ); \ 4 if( ( xSemaphore ) != NULL ) \ 5 { \ 6 ( void ) xSemaphoreGive( ( xSemaphore ) ); \ 7 } \ 8 }
而xSemaphoreCreateBinary创建二元信号量,初始值为“空 ”
1 osSemaphoreDef(myBinarySem); 2 myBinarySemHandle = osSemaphoreCreate(osSemaphore(myBinarySem), 1); 3 4 osThreadDef(vTask1, Task1, osPriorityAboveNormal, 0, 128); 5 vTask1Handle = osThreadCreate(osThread(vTask1), NULL); 6 7 /* definition and creation of vTask2 */ 8 osThreadDef(vTask2, Task2, osPriorityNormal, 0, 128); 9 vTask2Handle = osThreadCreate(osThread(vTask2), NULL); 10 11 /* Task1 function */ 12 void Task1(void const * argument) 13 { 14 15 /* USER CODE BEGIN Task1 */ 16 static uint32_t cnt = 0; 17 /* Infinite loop */ 18 for(;;) 19 { 20 Debug_Printf("Task1 is running,will be in the ready state!\n"); 21 osDelay(1000); 22 cnt++; 23 if(cnt == 5) 24 { 25 osSemaphoreRelease(myBinarySemHandle); 26 cnt = 0; 27 } 28 } 29 /* USER CODE END Task1 */ 30 } 31 32 /* Task2 function */ 33 void Task2(void const * argument) 34 { 35 /* USER CODE BEGIN Task2 */ 36 /* Infinite loop */ 37 for(;;) 38 { 39 if(osOK == osSemaphoreWait(myBinarySemHandle,osWaitForever)) 40 Debug_Printf("Task2 is running!\n"); 41 //osDelay(50); 42 } 43 /* USER CODE END Task2 */ 44 }
当使用vSemaphoreCreateBinary如下打印信息,TASK2一开始并没有阻塞
当使用xSemaphoreCreateBinary如下打印信息,前5S,TAKS2一直被阻塞