[CMSIS-RTOS2]rtos 基本概念

特点:

利于采用更加面向对象的设计方法。

多任务处理

相较于过程化的C, 需要考虑的是任务线程设计及线程间的数据流。

有助于项目管理、代码重用和软件测试

rtos需要额外的内存,中断响应变慢。

基本元素:

任务调度器: 支持round-robin、抢占式的多任务调度。

时间管理:

内存管理:mem pool

线程间通信:event group、semaphore、mutex、message

线程:

----------------------------------------------------------------

任务调度:

调度器使用SysTick timer来生成周期性中断。

调度器为每个线程分配一个时间片。每个线程轮流执行。

线程具有优先级属性,高优先级抢占低优先级。只有高优先级线程进入等待状态,低优先级线程才能被调度到。

调度时,需要将当前线程变量状态保存到线程stack中。所有当前线程的runtime信息保存在tread control block。

tread control block 包含线程的状态信息。

线程的三个状态  running、 ready、 wait

----------------------------------------------------------------

rtos的启动:

main函数中调用osKernelInitialize()来setup tros。

在这之前,不能调用任何rtos function。

osKernelInitialize完成后,可以创建线程和其他对象。

之后调用osKernelStart,任务调度器开始进行任务调度。

可以在启动rtos前进行各外设和硬件的初始化。

------------------------------------------------------------------

线程属性:

osThreadId : 线程id

osThreadAttr_t: 线程参数

thread_id = osThreadNew(threadFunc, threadAttr);

或 thread_id = osThreadNew(threadFunc, param, threadAttr);

线程可以在任意线程内创建。不能在isr中创建。不能在os init前创建。

线程函数: void threadFunc(void * argument)

线程状态:

osThreadInactive  创建后未运行

osThreadReady  : ready for run

osThreadRunning : running

osThreadBlocked :  delayed, wait for an event or suspended. 

osThreadTerminated : 终止的, 但是资源未释放。适用于joinable 线程。

osThreadError :线程不存在,不能被调度

osThreadReserved 保留

-----------------------------------------------------------------

线程优先级:

osPriorityNone     No priority (not initialized).

osPriorityIdle       Reserved for Idle thread.

osPriorityLow      Priority: low.

osPriorityLow1    Priority: low + 1.

。。。。。。

osPriorityLow7     Priority: low + 7.

osPriorityBelowNormal    Priority: below normal.

osPriorityBelowNormal1    Priority: below normal + 1.

。。。。。。

osPriorityBelowNormal7    Priority: below normal + 7.

osPriorityNormal       Priority: normal.

osPriorityNormal1      Priority: normal + 1.

。。。。。。

osPriorityNormal7    Priority: normal + 7.

osPriorityAboveNormal     Priority: above normal.

osPriorityAboveNormal1    Priority: above normal + 1.

。。。。。

osPriorityAboveNormal7     Priority: above normal + 7.

osPriorityHigh      Priority: high.

osPriorityHigh1     Priority: high + 1.

。。。。。。

osPriorityHigh7     Priority: high + 7.

osPriorityRealtime      Priority: realtime.

osPriorityRealtime1     Priority: realtime + 1.

。。。。。。

osPriorityRealtime7      Priority: realtime + 7.

osPriorityISR      Reserved for ISR deferred thread. 最高优先级。 用户线程不能使用。

osPriorityError    System cannot determine priority or illegal priority.

osPriorityReserved     Prevents enum down-size compiler optimization.

---------------------------------------------------------------------

线程 接口:

 

const char * osThreadGetName(osThreadId_t thread_id)

osThreadId_t osThreadGetId(void )

osThreadState_t osThreadGetStat(osThreadId_t thread_id)

osThreadSetPriority

osThreadGetPriority

osThreadYield  将运行权限转给同级别ready线程,如果没有则继续执行。

osThreadSuspend  ,立刻挂起到block state, 直到调用osThreadResume 恢复执行。

osThreadResume    将线程状态置回ready 状态。

osThreadDetach    将线程属性置成osThreadDetached.  调用后线程不能被osThreadJoin join。 当detached线程终止,所有资源被释放。对已经detach的线程执行osThreadDetach,这个操作为undefined。 

osThreadJoin      等待一个线程结束。 这个线程必须是joinable的。默认情况下线程创建后为osThreadDetached状态?。

join和detach的区别: join被调用后, 调用的线程会被block,直到线程的执行完成。join返回后,线程对象可以被销毁。

当detach调用后,线程对象可以被销毁,同时os执行的线程可以继续。想要知道线程何时结束需要其他机制。https://blog.csdn.net/xibeichengf/article/details/71173543

osThreadExit 终止调用的线程。

osThreadTerminate  将线程id指定的线程从active list中移除。线程被终止并运行其他线程,如果没有其他线程可执行,则线程不被终止,返回osThreadError. Joinable 线程不会被销毁,而是返回osThreadTerminated 直到调用osThreadJoin。

osThreadGetStackSize

osThreadGetStackSpace  返回未使用的stack space。

osThreadGetCount  返回active threads 数量。

osThreadEnumerate 说明文档描述不清。有疑问。

-----------------------------------------------------------------------------------------

multiple instances:同一段线程代码可以创建多个线程实例。 例如用于控制uart的线程,可以通过传递param来决定哪个线程控制哪个uart。

----------------------------------------------------------------------------------------

joinable thread: 线程可以被创建为joinable thread, 在运行时消耗一块内存,完成工作后terminate,释放内存。joinable thread 在创建时,设置attr_bits = osThreadJoinable

--------------------------------------------------------------------------------------

时间管理:

osDelay : 令调用线程进入WAIT_DELAY状态。延时结束时,线程被置为ready状态,

osDelayUntil(ticks)  absolute delay, 延时到指定tick。 与之相关的timer函数有:

osKernelGetTickCount(void)

osKernelGetTickFreq(void)

osKernelGetSysTimerCount(void)

 osKernelGetSysTimerFreq(void)

虚拟Timer:

osTimerId_t osTimerNew (osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr)
const char * osTimerGetName (osTimerId_t timer_id)
osStatus_t osTimerStart (osTimerId_t timer_id, uint32_t ticks)
osStatus_t osTimerStop (osTimerId_t timer_id)
uint32_t osTimerIsRunning (osTimerId_t timer_id)
osStatus_t osTimerDelete (osTimerId_t timer_id)

例子:

one_shot_id = osTimerNew(one_shot_Callback, osTimerOnce, (void *)0, NULL);

periodic_id = osTimerNew(periodic_Callback, osTimerPeriodic, (void *)5, NULL);

osTimerStart(one_shot_id, 500U);

osTimerStart(periodic_id, 1500U);

osTimerDelete(one_shot_id);

osTimerDelete(periodic_id);

 

Idle Thread:

系统内无其他线程运行时运行的默认线程,拥有最低优先级。 可以在其中放一些功能,比如让系统进入低功耗模式。 等SysTick timer中断唤醒系统,任务调度器运行处于ready的线程,如果没有ready线程则再次进入idle线程进入低功耗模式。

--------------------------------------------------------------------------------------------------------------------

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值