FreeRTOS常见知识点
1、临界段代码
临界段代码也叫做临界区,是指那些必须完整运行,不能被打断的代码段,比如某些外设的初始化需要严格的时序,且不能被打断。FreeRTOS提供的解决方案是在进入临界区时关闭中断,在离开时恢复中断,这就是临界段代码保护。
FreeRTOS提供了四个API来实现临界段代码保护:taskENTER_CRITICAL()、taskEXIT_CRITICAL()、taskENTER_CRITICAL_FROM_ISR()、taskEXIT_CRITICAL_FROM_ISR()。可以从名字上看出这四个API成两对,前两个是任务级的临界段代码保护,后两个是中断级的临界段代码保护。他们都在task.h中定义,且都是宏定义。
/**
* task. h
*
* Macro to mark the start of a critical code region. Preemptive context
* switches cannot occur when in a critical region.
*
* NOTE: This may alter the stack (depending on the portable implementation)
* so must be used with care!
*
* \defgroup taskENTER_CRITICAL taskENTER_CRITICAL
* \ingroup SchedulerControl
*/
#define taskENTER_CRITICAL() portENTER_CRITICAL()
#define taskENTER_CRITICAL_FROM_ISR() portSET_INTERRUPT_MASK_FROM_ISR()
/**
* task. h
*
* Macro to mark the end of a critical code region. Preemptive context
* switches cannot occur when in a critical region.
*
* NOTE: This may alter the stack (depending on the portable implementation)
* so must be used with care!
*
* \defgroup taskEXIT_CRITICAL taskEXIT_CRITICAL
* \ingroup SchedulerControl
*/
#define taskEXIT_CRITICAL()