FreeRTOS实时操作系统临界段保护
jiang_2018 人气:0临界段保护场合
FreeRTOS中临界段保护有2种场合,中断和非中断,通过关中断(或者关部分中断)来实现临界保护。
非中断场合
task.h 中
#define taskENTER_CRITICAL()portENTER_CRITICAL() #define taskEXIT_CRITICAL()portEXIT_CRITICAL()
portmacro.h中
#define portENTER_CRITICAL()vPortEnterCritical() #define portEXIT_CRITICAL()vPortExitCritical()
port.c中
//这个值用来记录中断嵌套次数,在调度器启动时会被重新初始化为0//vTaskStartScheduler()->xPortStartScheduler()->uxCriticalNesting = 0。static UBaseType_t uxCriticalNesting = 0xaaaaaaaa;void vPortEnterCritical( void ){portDISABLE_INTERRUPTS();//关中断uxCriticalNesting++;__dsb( portSY_FULL_READ_WRITE );__isb( portSY_FULL_READ_WRITE );if( uxCriticalNesting == 1 ){configASSERT( ( portNVIC_INT_CTRL_REG & portVECTACTIVE_MASK ) == 0 );}}void vPortExitCritical( void ){configASSERT( uxCriticalNesting );uxCriticalNesting--;if( uxCriticalNesting == 0 ) //没有嵌套的话就可以开中断了{portENABLE_INTERRUPTS();}}//这个值用来记录中断嵌套次数,在调度器启动时会被重新初始化为0 //vTaskStartScheduler()->xPortStartScheduler()->uxCriticalNesting = 0。 static UBaseType_t uxCriticalNesting = 0xaaaaaaaa; void vPortEnterCritical( void ) { portDISABLE_INTERRUPTS();//关中断 uxCriticalNesting++; __dsb( portSY_FULL_READ_WRITE ); __isb( portSY_FULL_READ_WRITE ); if( uxCriticalNesting == 1 ) { configASSERT( ( portNVIC_INT_CTRL_REG & portVECTACTIVE_MASK ) == 0 ); } } void vPortExitCritical( void ) { configASSERT( uxCriticalNesting ); uxCriticalNesting--; if( uxCriticalNesting == 0 ) //没有嵌套的话就可以开中断了 { portENABLE_INTERRUPTS(); } }
portmacro.h中
#define portDISABLE_INTERRUPTS()ulPortSetInterruptMask() #define portENABLE_INTERRUPTS()vPortClearInterruptMask( 0 )
port.c中
__asm uint32_t ulPortSetInterruptMask( void ) { PRESERVE8 mrs r0, basepri //r0作为函数返回值 mov r1, #configMAX_SYSCALL_INTERRUPT_PRIORITY msr basepri, r1 bx r14 } /*-----------------------------------------------------------*/ __asm void vPortClearInterruptMask( uint32_t ulNewMask ) { PRESERVE8 msr basepri, r0 bx r14 }
中断场合
task.h
#define taskENTER_CRITICAL_FROM_ISR() portSET_INTERRUPT_MASK_FROM_ISR() #define taskEXIT_CRITICAL_FROM_ISR( x ) portCLEAR_INTERRUPT_MASK_FROM_ISR( x )
portmacro.h中
下面这2个函数在前面的port.c
中已经实现,只是这里形参是x而不是0
#define portSET_INTERRUPT_MASK_FROM_ISR()ulPortSetInterruptMask() #define portCLEAR_INTERRUPT_MASK_FROM_ISR(x)vPortClearInterruptMask(x)
使用例子
加载全部内容