中断屏蔽寄存器

PRIMASK、FAULTMASK和BASEPRI

法一:CM3 还专门设置了一条CPS指令

CPSID I ;PRIMASK=1 ;关中断
CPSIE I ;PRIMASK=0 ;开中断
CPSID F ;FAULTMASK=1 ;关异常
CPSIE F ;FAULTMASK=0 ;开异常

法二:使用MRS/MSR指令

MRS R0, BASEPRI   ;读取 BASEPRI 到 R0 中
MRS R0, FAULTMASK ;似上
MRS R0, PRIMASK ;似上 MOVS R0, #1
MSR BASEPRI, R0 ;写入 R0 到 BASEPRI 中
MSR FAULTMASK, R0 ;似上
MSR PRIMASK, R0 ;似上

临界段代码

临界段代码也叫做临界区,是指那些必须完整运行,不能被打断的代码段,比如有的外设的初始化需要严格的时序,初始化过程中不能被打断

//进入临界段
#define taskENTER_CRITICAL() portENTER_CRITICAL() //任务级临界段代码保护
#define taskENTER_CRITICAL_FROM_ISR() portSET_INTERRUPT_MASK_FROM_ISR() //退出临界段
#define taskEXIT_CRITICAL() portEXIT_CRITICAL() //中断级临界段代码保护
#define taskEXIT_CRITICAL_FROM_ISR( x ) portCLEAR_INTERRUPT_MASK_FROM_ISR( x )
#define portENTER_CRITICAL()        vPortEnterCritical();
#define portEXIT_CRITICAL() vPortExitCritical();
void vPortEnterCritical( void )
{
portDISABLE_INTERRUPTS();
uxCriticalNesting++; /* This is not the interrupt safe version of the enter critical function so
assert() if it is being called from an interrupt context. Only API
functions that end in "FromISR" can be used in an interrupt. Only assert if
the critical nesting count is 1 to protect against recursive calls if the
assert function also uses a critical section. */
if( uxCriticalNesting == 1 )
{
configASSERT( ( portNVIC_INT_CTRL_REG & portVECTACTIVE_MASK ) == 0 );
}
} void vPortExitCritical( void )
{
configASSERT( uxCriticalNesting );
uxCriticalNesting--;
if( uxCriticalNesting == 0 )
{
portENABLE_INTERRUPTS();
}
}
05-11 08:31