软定时
osTimerId_t myTimer03Handle;
const osTimerAttr_t myTimer03_attributes = {
. name = "myTimer03"
} ;
myTimer03Handle = osTimerNew ( Callback03, osTimerPeriodic, NULL , & myTimer03_attributes) ;
osTimerStart ( myTimer02Handle, 800 ) ;
void Callback03 ( void * argument)
{
printf ( "stim 03\n" ) ;
osTimerStop ( myTimer03Handle) ;
}
信号(二值)
myBinarySem02Handle = osSemaphoreNew ( 1 , 0 , & myBinarySem02_attributes) ;
if ( osOK== osSemaphoreAcquire ( myBinarySem02Handle, osWaitForever) )
{
}
if ( osOK== osSemaphoreRelease ( myBinarySem02Handle) )
{
}
信号 (计数)
osSemaphoreId_t myCountingSem01Handle;
const osSemaphoreAttr_t myCountingSem01_attributes = {
. name = "myCountingSem01"
} ;
myCountingSem01Handle = osSemaphoreNew ( 5 , 5 , & myCountingSem01_attributes) ;
if ( osOK== osSemaphoreAcquire ( myCountingSem01Handle, osWaitForever ) )
{
printf ( "count acq num:%d \n" , osSemaphoreGetCount ( myCountingSem01Handle) ) ;
}
if ( osOK== osSemaphoreRelease ( myCountingSem01Handle) )
{
printf ( "count release num:%d \n" , osSemaphoreGetCount ( myCountingSem01Handle) ) ;
}
信号 (互斥)
a (低)和b(最高),c(中) 任务,存在优先级差别,当低优先级的任务a被高一级的任务c打断,进入任务c里,而不是进入最高的b任务里,导致优先级发生翻转,这时采用互斥会尽量的避免优先级反转现象。(本质:暂时提升低优先级任务的优先级等级)
获取和释放需要在一个任务里配合使用。
osMutexId_t myMutex01Handle;
const osMutexAttr_t myMutex01_attributes = {
. name = "myMutex01"
} ;
myRecursiveMutex01Handle = osMutexNew ( & myRecursiveMutex01_attributes) ;
osMutexAcquire ( myMutex01Handle, osWaitForever) ;
任务内容
osMutexRelease ( myMutex01Handle) ;
事件组
# define EVENT_f1 ( 0x01 << 0 )
# define EVENT_f2 ( 0x01 << 1 )
osEventFlagsId_t myEvent01Handle;
const osEventFlagsAttr_t myEvent01_attributes = {
. name = "myEvent01"
} ;
myEvent01Handle = osEventFlagsNew ( & myEvent01_attributes) ;
osEventFlagsSet ( myEvent01Handle, EVENT_f1 ) ;
osEventFlagsSet ( myEvent01Handle, EVENT_f2 ) ;
osEventFlagsWait ( myEvent01Handle, EVENT_f1 | EVENT_f1 , osFlagsWaitAll, osWaitForever) ;
消息队列
typdef struct
{
uint8_t bufs[ 10 ] ;
} udty;
udty uds;
osMessageQueueId_t myQueue01Handle;
const osMessageQueueAttr_t myQueue01_attributes = {
. name = "myQueue01"
} ;
myQueue01Handle = osMessageQueueNew ( 16 , sizeof ( udty) , & myQueue01_attributes) ;
if ( osMessageQueuePut ( myQueue01Handle, ( void * ) & uds. busfs, 0 , 0 ) == osOK)
{
}
uint8_t chbuf[ 10 ] ;
if ( osMessageQueueGet ( myQueue01Handle, ( void * ) & chbuf, NULL , osWaitForever) == osOK)
{
printf ( "date:%s\n" , chbuf) ;
}
保护访问的资源不被打断
任务中使用
taskENTER_CRITICAL ( ) ;
taskEXIT_CRITICAL ( ) ;
中断中使用
uint32_t ulReturn;
ulReturn = taskENTER_CRITICAL_FROM_ISR ( ) ;
taskEXIT_CRITICAL_FROM_ISR ( ulReturn ) ;
任务资源状态
uint8_t List_sysyBuffer[ 300 ] ;
vTaskList ( ( char * ) List_sysyBuffer) ;
osThreadYield ( ) ;
osDelay ( uint32_t ticks) ;
osThreadSuspend ( osThreadId_t thread_id) ;
osThreadResume ( osThreadId_t thread_id) ;
osThreadGetPriority ( osThreadId_t thread_id) ;
在cmsis_os2. h 里有对应的任务创建等。