简体   繁体   English

FreeRTOS中的抢占

[英]Preemption in FreeRTOS

I am starting to use FreeRTOS and I would like a interrupt to preempt whatever task was about to run and run the task I need to run critically. 我开始使用FreeRTOS,我想要一个中断来抢占任何即将运行的任务,并运行我需要批判运行的任务。

Is there a way to do this in FreeRTOS? 有没有办法在FreeRTOS中做到这一点? (Is this achieved through task priority?) (这是通过任务优先级来实现的吗?)

NO! 没有! Both the above answers are DANGEROUS. 以上两个答案都是危险的。

Do NOT use taskENTER_CRITICAL() or taskEXIT_CRITICAL() inside an ISR - it is unusual to need a critical section in an ISR but if you do then use taskENTER_CRITICAL_FROM_ISR()/taskEXIT_CRITICAL_FROM_ISR() . 不要在ISR中使用taskENTER_CRITICAL()或taskEXIT_CRITICAL() - 在ISR中需要一个关键部分是不常见的,但是如果你这样做则使用taskENTER_CRITICAL_FROM_ISR()/ taskEXIT_CRITICAL_FROM_ISR() (possible the AVR32 port is an exception to that rule?) (可能AVR32端口是该规则的一个例外吗?)

Do NOT use xTaskResumeFromISR() to synchronise a task with an interrupt. 不要使用xTaskResumeFromISR()将任务与中断同步。 The link already posted to the documentation for that function even says this. 已发布到该功能文档的链接甚至说明了这一点。

If my understanding of your question is correct you want the ability to have an interrupt unblock a task, and then if that task is the highest priority task in that is able to run, have the interrupt return directly to the unblocked task. 如果我对您的问题的理解是正确的,您希望能够让中断解锁任务,然后如果该任务是能够运行的最高优先级任务,则让中断直接返回到未阻止的任务。 If my understanding is right then there is an example of how to do that in an efficient way on the following page: http://www.freertos.org/RTOS_Task_Notification_As_Counting_Semaphore.html 如果我的理解是正确的,那么在下一页上有一个如何以有效的方式做到这一点的例子: http//www.freertos.org/RTOS_Task_Notification_As_Counting_Semaphore.html

You can use xTaskResumeFromISR to do this. 您可以使用xTaskResumeFromISR执行此操作。

There is a number of conditions to be met for the yielded task not to be interrupted by other tasks (like it's priority must be high enough) and a number of other conditions to be met to ensure that no interrupt can go un-serviced (like the yielded task must guarantee to be done before the next interrupt) 生成的任务有许多条件不被其他任务中断(例如它的优先级必须足够高)以及要满足的许多其他条件,以确保不会中断任何中断(如产生的任务必须保证在下一次中断之前完成)

1. enable preemption: 1.启用抢占:

This is very simple to do. 这很简单。

All the configuration options of FreeRTOS are under "FreeRTOSConfig.h" FreeRTOS的所有配置选项都在“FreeRTOSConfig.h”下

#define configUSE_PREEMPTION                    1

You can set this to 1 to use the preemptive RTOS scheduler, or 0 to use the cooperative RTOS scheduler. 您可以将此值设置为1以使用抢占式RTOS调度程序,或者将0设置为使用协作RTOS调度程序。

Check this link for more info 请查看此链接以获取更多信息

2. Use critical section inside ISR 2.在ISR中使用关键部分

void taskENTER_CRITICAL( void );
//action

void taskEXIT_CRITICAL( void );

RTOS wont do anythis extra inside this critical part RTOS在这个关键部分内不会做任何额外的事情

ref: here 参考: 这里

The short answer would be: Yes, this is achieved through task priority. 简短的回答是:是的,这是通过任务优先级来实现的。

The FreeRTOS kernel will consider swapping in any task in ready-state after an ISR has completed so it would preempt the current running task if a higher priority task is now ready. 在ISR完成后,FreeRTOS内核将考虑交换任何处于就绪状态的任务,因此如果现在准备好更高优先级的任务,它将抢占当前正在运行的任务。

It should be mentioned that this is really only true if the handler is called through FreeRTOS. 应该提到的是,只有通过FreeRTOS调用处理程序时才会出现这种情况。 On a Cortex-A processor there is a common IRQ entry-point in the IRQ or FIQ exception handler which is most likely handled by FreeRTOS or otherwise by an IRQ dispatcher which is easily wrapped by FreeRTOS, usually by a function in the port-layer called vApplicationIRQHandler(). 在Cortex-A处理器上,IRQ或FIQ异常处理程序中有一个常见的IRQ入口点,很可能由FreeRTOS处理,或者由IRQ调度程序处理,IRQ调度程序很容易被FreeRTOS包装,通常由端口层中的函数处理。名为vApplicationIRQHandler()。

On a Cortex-M this is not necessarily the case as the vector is usually manipulated by the vendor's MCU API. 在Cortex-M上不一定是这种情况,因为矢量通常由供应商的MCU API操纵。 On a Cortex-M I'd safe-guard against this using portYIELD_FROM_ISR() in the ISR which should be implemented to provide the kernel with an opportunity to perform a context switch. 在Cortex-M上,我使用ISR中的portYIELD_FROM_ISR()来保护它,这应该被实现为内核提供执行上下文切换的机会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM