【原文地址: http://blog.csdn.net/fovwin/article/details/11021155 】
個人筆記:
1. ARM有默認的規定,傳入的參數從左至右依次放入R0-R4中;
2. 請參考《編譯器用戶指南》和《ARM Cortex-M3權威指南》;
------------------------------------------------------------------------------------------------------
1
2 |
|
#define OS_ENTER_CRITICAL() __asm("CPSID I")
#define OS_EXIT_CRITICAL() __asm("CPSIE I") |
1
2 3 4 |
|
__asm
{ //原汁原味的彙編語句 } |
1
2 3 4 5 |
|
__asm uint32_t __get_PSP(void)
{ mrs r0, psp bx lr } |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
/* define compiler specific symbols */
#if defined ( __CC_ARM ) #define __ASM __asm /*!< asm keyword for ARM Compiler */ #define __INLINE __inline /*!< inline keyword for ARM Compiler */ #elif defined ( __ICCARM__ ) #define __ASM __asm /*!< asm keyword for IAR Compiler */ #define __INLINE inline /*!< inline keyword for IAR Compiler. Only avaiable in High optimization mode! */ #elif defined ( __GNUC__ ) #define __ASM __asm /*!< asm keyword for GNU Compiler */ #define __INLINE inline /*!< inline keyword for GNU Compiler */ #elif defined ( __TASKING__ ) #define __ASM __asm /*!< asm keyword for TASKING Compiler */ #define __INLINE inline /*!< inline keyword for TASKING Compiler */ #endif |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
/* ################### Compiler specific Intrinsics ########################### */ #if defined ( __CC_ARM ) /*------------------RealView Compiler -----------------*/ /* ARM armcc specific functions */ #elif (defined (__ICCARM__)) /*------------------ ICC Compiler -------------------*/ /* IAR iccarm specific functions */ #elif (defined (__GNUC__)) /*------------------ GNU Compiler ---------------------*/ #elif (defined (__TASKING__)) /*------------------ TASKING Compiler ---------------------*/ /* TASKING carm specific functions */ /* * The CMSIS functions have been implemented as intrinsics in the compiler. * Please use "carm -?i" to get an up to date list of all instrinsics, * Including the CMSIS ones. */ #endif |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
/**
* @brief Return the Process Stack Pointer * * @return ProcessStackPointer * * Return the actual process stack pointer */ __ASM uint32_t __get_PSP(void) { mrs r0, psp bx lr } /** * @brief Set the Process Stack Pointer * * @param topOfProcStack Process Stack Pointer * * Assign the value ProcessStackPointer to the MSP * (process stack pointer) Cortex processor register */ __ASM void __set_PSP(uint32_t topOfProcStack) { msr psp, r0 bx lr } |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
;定義幾個常量,類似C語言中的#define預處理指令。
NVIC_INT_CTRL EQU 0xE000ED04 ; 中斷控制寄存器 NVIC_SYSPRI14 EQU 0xE000ED22 ; PendSV優先級寄存器的地址 NVIC_PENDSV_PRI EQU 0x000000FF ; PendSV中斷的優先級爲255(最低) NVIC_PENDSVSET EQU 0x10000000 ; 觸發軟件中斷的值,位28爲1. ;******************************************************************************************************** ; START MULTITASKING ; void OSStartHighRdy(void) ; ; Note(s) : 1) This function triggers a PendSV exception (essentially, causes a context switch) to cause ; the first task to start. ; ; 2) OSStartHighRdy() MUST: ; a) Setup PendSV exception priority to lowest; ; b) Set initial PSP to 0, to tell context switcher this is first run; ; c) Set the main stack to OSRunning ; d) Trigger PendSV exception; ; e) Enable interrupts (tasks will run with interrupts enabled). ;******************************************************************************************************** OSStartHighRdy ;設置PendSV中斷的優先級 LDR R4, =NVIC_SYSPRI14 ; set the PendSV exception priority LDR R5, =NVIC_PENDSV_PRI STR R5, [R4] ;設置PSP爲0 MOV R4, #0 ; set the PSP to 0 for initial context switch call MSR PSP, R4 ;設置OSRunning爲TRUE LDR R4, =OSRunning ; OSRunning = TRUE MOV R5, #1 STRB R5, [R4] ;觸發PendSV中斷 LDR R4, =NVIC_INT_CTRL ;rigger the PendSV exception (causes context switch) LDR R5, =NVIC_PENDSVSET STR R5, [R4] CPSIE I ;enable interrupts at processor level ;死循環,應該不會到這裏 OSStartHang B OSStartHang ;should never get here |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
#define NVIC_INT_CTRL *((OS_CPU_SR *)0xE000ED04) //中斷控制寄存器ICSR
#define NVIC_PENDSVSET 0x10000000 //觸發軟件中斷的值,位28爲1. #define OS_TASK_SW() NVIC_INT_CTRL = NVIC_PENDSVSET #define OSIntCtxSw() NVIC_INT_CTRL = NVIC_PENDSVSET #define OS_ENTER_CRITICAL() __asm("CPSID I"); #define OS_EXIT_CRITICAL() __asm("CPSIE I"); #define NVIC_SYSPRI14 *((OS_CPU_SR *)0xE000ED22) //PendSV優先級寄存器的地址 #define NVIC_PENDSV_PRI 0x000000FF //PendSV中斷的優先級爲255(最低) #define SET_PENDSV_FF() NVIC_SYSPRI14=NVIC_PENDSV_PRI void OSStartHighRdy(void) { SET_PENDSV_FF(); //這裏寫FF是因爲把它的中斷優先級設置爲最低255 __set_PSP(0); OSRunning = 1; OS_TASK_SW(); OS_EXIT_CRITICAL(); } |
扯遠了,看下一個:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
/**
* @brief Return the Main Stack Pointer * * @return Main Stack Pointer * * Return the current value of the MSP (main stack pointer) * Cortex processor register */ __ASM uint32_t __get_MSP(void) { mrs r0, msp bx lr } /** * @brief Set the Main Stack Pointer * * @param topOfMainStack Main Stack Pointer * * Assign the value mainStackPointer to the MSP * (main stack pointer) Cortex processor register */ __ASM void __set_MSP(uint32_t mainStackPointer) { msr msp, r0 bx lr } |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
/**
* @brief Reverse byte order in unsigned short value * * @param value value to reverse * @return reversed value * * Reverse byte order in unsigned short value */ __ASM uint32_t __REV16(uint16_t value) { rev16 r0, r0 bx lr } /** * @brief Reverse byte order in signed short value with sign extension to integer * * @param value value to reverse * @return reversed value * * Reverse byte order in signed short value with sign extension to integer */ __ASM int32_t __REVSH(int16_t value) { revsh r0, r0 bx lr } |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
|
/**
* @brief Remove the exclusive lock created by ldrex * * Removes the exclusive lock which is created by ldrex. */ __ASM void __CLREX(void) { clrex } /** * @brief Return the Base Priority value * * @return BasePriority * * Return the content of the base priority register */ __ASM uint32_t __get_BASEPRI(void) { mrs r0, basepri bx lr } /** * @brief Set the Base Priority value * * @param basePri BasePriority * * Set the base priority register */ __ASM void __set_BASEPRI(uint32_t basePri) { msr basepri, r0 bx lr } /** * @brief Return the Priority Mask value * * @return PriMask * * Return state of the priority mask bit from the priority mask register */ __ASM uint32_t __get_PRIMASK(void) { mrs r0, primask bx lr } /** * @brief Set the Priority Mask value * * @param priMask PriMask * * Set the priority mask bit in the priority mask register */ __ASM void __set_PRIMASK(uint32_t priMask) { msr primask, r0 bx lr } /** * @brief Return the Fault Mask value * * @return FaultMask * * Return the content of the fault mask register */ __ASM uint32_t __get_FAULTMASK(void) { mrs r0, faultmask bx lr } /** * @brief Set the Fault Mask value * * @param faultMask faultMask value * * Set the fault mask register */ __ASM void __set_FAULTMASK(uint32_t faultMask) { msr faultmask, r0 bx lr } /** * @brief Return the Control Register value * * @return Control value * * Return the content of the control register */ __ASM uint32_t __get_CONTROL(void) { mrs r0, control bx lr } /** * @brief Set the Control Register value * * @param control Control value * * Set the control register */ __ASM void __set_CONTROL(uint32_t control) { msr control, r0 bx lr } |