protmaro.h代碼解析
類型重定義
/* Type definitions. */
#define portCHAR char
#define portFLOAT float
#define portDOUBLE double
#define portLONG long
#define portSHORT short
#define portSTACK_TYPE uint32_t
#define portBASE_TYPE long
typedef portSTACK_TYPE StackType_t;
typedef long BaseType_t;
typedef unsigned long UBaseType_t;
#if( configUSE_16_BIT_TICKS == 1 )
typedef uint16_t TickType_t;
#define portMAX_DELAY ( TickType_t ) 0xffff
#else
typedef uint32_t TickType_t;
#define portMAX_DELAY ( TickType_t ) 0xffffffffUL
/* 32-bit tick type on a 32-bit architecture, so reads of the tick count do
not need to be guarded with a critical section. */
#define portTICK_TYPE_IS_ATOMIC 1
#endif
如果定義 configUSE_16_BIT_TICKS 爲 1 則表示TickType_t配置最大計數值爲16 bit。 portTICK_TYPE_IS_ATOMIC 爲1 表示32位數操作是否爲原子操作。否則需要進入臨界區才能對32位數進行操作。
構架指定
/* Architecture specifics. */
#define portSTACK_GROWTH ( -1 ) //定義堆棧生長的方向
#define portTICK_PERIOD_MS ( ( TickType_t ) 1000 / configTICK_RATE_HZ ) //調度時間片的大小
#define portBYTE_ALIGNMENT 8 //內存對齊方式,不知道爲什麼是8byte 猜測與AAPCS 要求堆棧必須8字節對齊 有關
/* Constants used with memory barrier intrinsics. */
#define portSY_FULL_READ_WRITE ( 15 ) //與內存隔離指令一起使用的條件參數
其中隔離指令的參數爲指令的option,爲確保完成所有操作必須將值設置爲0xF
定義臨界區相關的函數
/*-----------------------------------------------------------*/
//設置屏蔽中斷有消極
static portFORCE_INLINE void vPortSetBASEPRI( uint32_t ulBASEPRI )
{
__asm
{
/* Barrier instructions are not used as this function is only used to
lower the BASEPRI value. */
msr basepri, ulBASEPRI
}
}
/*-----------------------------------------------------------*/
//設置關閉屏蔽優先級爲0及大於0的中斷
static portFORCE_INLINE void vPortRaiseBASEPRI( void )
{
uint32_t ulNewBASEPRI = configMAX_SYSCALL_INTERRUPT_PRIORITY;
__asm
{
/* Set BASEPRI to the max syscall priority to effect a critical
section. */
msr basepri, ulNewBASEPRI
dsb
isb
}
}
/*-----------------------------------------------------------*/
//開啓所有的中斷
static portFORCE_INLINE void vPortClearBASEPRIFromISR( void )
{
__asm
{
/* Set BASEPRI to 0 so no interrupts are masked. This function is only
used to lower the mask in an interrupt, so memory barriers are not
used. */
msr basepri, #0
}
}
/*-----------------------------------------------------------*/
//設置關閉屏蔽優先級爲0及大於0的中斷,並返回更改之前的中斷優先級
static portFORCE_INLINE uint32_t ulPortRaiseBASEPRI( void )
{
uint32_t ulReturn, ulNewBASEPRI = configMAX_SYSCALL_INTERRUPT_PRIORITY;
__asm
{
/* Set BASEPRI to the max syscall priority to effect a critical
section. */
mrs ulReturn, basepri
msr basepri, ulNewBASEPRI
dsb
isb
}
return ulReturn;
}
/*-----------------------------------------------------------*/
//檢查當前是否在中斷狀態,通過ipsr獲取中斷異常號來實現
static portFORCE_INLINE BaseType_t xPortIsInsideInterrupt( void )
{
uint32_t ulCurrentInterrupt;
BaseType_t xReturn;
/* Obtain the number of the currently executing interrupt. */
__asm
{
mrs ulCurrentInterrupt, ipsr
}
if( ulCurrentInterrupt == 0 )
{
xReturn = pdFALSE;
}
else
{
xReturn = pdTRUE;
}
return xReturn;
}