cmsis-rtos Semaphore

2020-09-14  本文已影响0人  骑上我心爱的小蜗牛

大佬传送门

osSemaphoreId sync_sem_id;

osSemaphoreDef(sync_sem);

sync_sem_id = osSemaphoreCreate(osSemaphore(sync_sem), 10);

val=osSemaphoreWait(sync_sem_id, osWaitForever);

#define osWaitForever     0xFFFFFFFF     ///< wait forever timeout value
int32_t osSemaphoreWait(osSemaphoreId semaphore_id, uint32_t millisec);
* 返回值:int32_t ,正常返回当前count数,失败返回-1。
* 如果需要阻塞延时,参数应该设置为CMSIS-RTOS API提供的宏定义 osWaitForever
* millisec等待的时间

osSemaphoreRelease(sync_sem_id);

#include "cmsis_os.h"

osSemaphoreId sync_sem_id;
osSemaphoreDef(sync_sem);
int main(void)
{

     sync_sem_id = osSemaphoreCreate(osSemaphore(sync_sem), 10);
    /* initialize CMSIS-RTOS */
    osKernelInitialize(); // initialize CMSIS-RTOS
    //CMSIS thread create
    TaskTest_Id = osThreadCreate(osThread(test_thread), NULL);
    /* start thread execution */
    osKernelStart();
    while(1)
    {
    osDelay(3);  
        val=osSemaphoreWait(sync_sem_id, osWaitForever);
    printf("2\r\n");
    if(val>0)
      {
        printf("val=%d\r\n",val);
//      osSemaphoreRelease(sync_sem_id); 
      }
    }
}

void test_thread(void const *arg)
{
    /* test code begin */
    while(1){
//  osSemaphoreWait (sync_sem_id, osWaitForever);
//  printf("1\r\n");
//      osSemaphoreRelease(sync_sem_id);
//      osDelay(1000);
    }
    /* test code end */
}
2
val=10
2
val=9
2
val=8
2
val=7
2
val=6
2
val=5
2
val=4
2
val=3
2
val=2
2
val=1
上一篇下一篇

猜你喜欢

热点阅读