freerots系统配置参考: http://www.javashuo.com/article/p-tkjzlcdb-na.html
其它保持默认。
代码实现:
//将二值信号量设置为全局变量: //osSemaphoreId myBinarySem01Handle; //osStaticSemaphoreDef_t myBinarySem01ControlBlock; //赋值添加关键字保存在.h文件中 extern osSemaphoreId myBinarySem01Handle; extern osStaticSemaphoreDef_t myBinarySem01ControlBlock;
用到的信号量函数为:具体查看代码说明
/** Semaphore Management Functions **/ osSemaphoreId osSemaphoreCreate (const osSemaphoreDef_t *semaphore_def, int32_t count);//创建信号量 osStatus osSemaphoreRelease (osSemaphoreId semaphore_id);//释放信号量 int32_t osSemaphoreWait (osSemaphoreId semaphore_id, uint32_t millisec);//等待获取信号量 osStatus osSemaphoreDelete (osSemaphoreId semaphore_id);//信号量删除
二值信号量使用:
1. 信号发生
BaseType_t xHigherPriorityTaskWoken = pdFALSE; ........... if(myBinarySem01Handle != NULL)//正确创建二值信号量后进入判断 { osSemaphoreRelease(myBinarySem01Handle);////释放二值信号量 portYIELD_FROM_ISR(xHigherPriorityTaskWoken );//需要的话进行一次任务切换,不在中断中使用 portYIELD(); }
2.信号接收
void MYTask(void const * argument) { /* USER CODE BEGIN StartTask02 */ BaseType_t err = pdFALSE; /* Infinite loop */ for(;;) { if(myBinarySem01Handle != NULL)//正确创建二值信号量后进入判断 { err = osSemaphoreWait(myBinarySem01Handle,osWaitForever); if(err == osOK) { //HAL_UART_Transmit_DMA(&huart1,(uint8_t *)str2,sizeof(str2)); .............. } } osDelay(10); } /* USER CODE END StartTask02 */ }
搜索
复制