STM32、FreeRTOS—— SVC 和 PendSV

I went through the xTaskStartSechudler() function, which ends up triggering the SVC instruction.
and i came to know that it used in only in this function.
If i am not wrong , is SVC used to launch the first Task ?
So, in my understanding SVC is used only one time, and subsequent task switching in and out is carried out in the PendSV handler. am i right ?
Why can cant we use pendsv itself instead of SVC ?

Yes you are correct, unless you are using the version that supports the
MPU the SVC instruction is only used once.

Old versions don’t use the SVC, and instead use the PendSV,
and there was a really good reason for changing that I can’t recall the exact details of but it was something to do with having to add extra code into the PendSV instruction so it knew whether it had to save a context or not, and also loosing a bit of stack space.
If you use SVC to start the first task then the SVC handler resets the stack to the start of the stack and just has to restore the context of the first task to run.
If you are using the PendSV handler then you can’t reset the stack (because you are using it?), and you have to test whether or not a task was running when the PendSV handler was called (so the context is not saved if it is the first call because there is no task context to save).

就是因为刚开始运行第一个任务时,不需要保存寄存器的状态到一个并不存在的任务块中,只需要重置然后将第一个任务的状态恢复到寄存器中,而切换任务时需要保存寄存器的状态,然后恢复寄存器的状态到当前任务。

最重要的是:

相比 SVC, PendSV 异常后可能不会马上响应, 等到其他高优先级中断处理后才响应。
用于上下文切换,同时保证其他中断可以被及时响应处理。

这可以让FreeRTOS管理一组连续的优先级比如5 ~15,这些优先级被用于需要使用FreeRTOS的API的中断处理函数,
因为:

  1. 任务调度器的是运行在PendSV中的,
  2. 任务调度器运行时通过BASEPRI阻塞5 ~ 15这些低优先级的中断,
  3. 这些中断即使被触发,也会被操作系统的任务调度器暂时挂起,等待任务切换结束才会被响应;
  4. 但是在任务正常运行时期,5 ~ 15优先级的中断会被立即响应,运行环境的保存由硬件完成,中断响应结束后由硬件自动恢复上下文。

而1 ~ 4是高于操作系统的中断,且不使用FreeRTOS的API,这些中断会在任务调度器运行期间被立即响应。

NVIC支持中断嵌套,PendSV期间未被屏蔽的1 ~ 4 优先级中断仍然可以被立即响应,上下文的保存恢复由硬件自动完成。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值