Eric Ebert
/
CoOS
CoOS Demonstrator adapted to mbed Hardware.
port.c@0:57690853989a, 2010-12-03 (annotated)
- Committer:
- ericebert
- Date:
- Fri Dec 03 19:45:30 2010 +0000
- Revision:
- 0:57690853989a
Some basic LED-Flashing works in the CoOS-RTOS using Tasks
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ericebert | 0:57690853989a | 1 | /** |
ericebert | 0:57690853989a | 2 | ******************************************************************************* |
ericebert | 0:57690853989a | 3 | * @file prot.c |
ericebert | 0:57690853989a | 4 | * @version V1.1.3 |
ericebert | 0:57690853989a | 5 | * @date 2010.04.26 |
ericebert | 0:57690853989a | 6 | * @brief Compiler adapter for CooCox CoOS kernel. |
ericebert | 0:57690853989a | 7 | ******************************************************************************* |
ericebert | 0:57690853989a | 8 | * @copy |
ericebert | 0:57690853989a | 9 | * |
ericebert | 0:57690853989a | 10 | * INTERNAL FILE,DON'T PUBLIC. |
ericebert | 0:57690853989a | 11 | * |
ericebert | 0:57690853989a | 12 | * <h2><center>© COPYRIGHT 2010 CooCox </center></h2> |
ericebert | 0:57690853989a | 13 | ******************************************************************************* |
ericebert | 0:57690853989a | 14 | */ |
ericebert | 0:57690853989a | 15 | |
ericebert | 0:57690853989a | 16 | /*---------------------------- Include ---------------------------------------*/ |
ericebert | 0:57690853989a | 17 | #include <coocox.h> |
ericebert | 0:57690853989a | 18 | |
ericebert | 0:57690853989a | 19 | |
ericebert | 0:57690853989a | 20 | /** |
ericebert | 0:57690853989a | 21 | ****************************************************************************** |
ericebert | 0:57690853989a | 22 | * @brief Plus a byte integers and Saved into memory cell |
ericebert | 0:57690853989a | 23 | * @param[in] data byte integers. |
ericebert | 0:57690853989a | 24 | * @param[out] None |
ericebert | 0:57690853989a | 25 | * @retval Returns Original value. |
ericebert | 0:57690853989a | 26 | * |
ericebert | 0:57690853989a | 27 | * @par Description |
ericebert | 0:57690853989a | 28 | * @details This function is called to Plus a byte integers |
ericebert | 0:57690853989a | 29 | * and Saved into memory cell. |
ericebert | 0:57690853989a | 30 | ****************************************************************************** |
ericebert | 0:57690853989a | 31 | */ |
ericebert | 0:57690853989a | 32 | __asm U8 Inc8 (volatile U8 *data) |
ericebert | 0:57690853989a | 33 | { |
ericebert | 0:57690853989a | 34 | PUSH {R1} |
ericebert | 0:57690853989a | 35 | CPSID I |
ericebert | 0:57690853989a | 36 | LDRB R1,[R0] |
ericebert | 0:57690853989a | 37 | ADDS R1,#1 |
ericebert | 0:57690853989a | 38 | STRB R1,[R0] |
ericebert | 0:57690853989a | 39 | CPSIE I |
ericebert | 0:57690853989a | 40 | SUBS R1,#1 |
ericebert | 0:57690853989a | 41 | MOVS R0,R1 |
ericebert | 0:57690853989a | 42 | POP {R1} |
ericebert | 0:57690853989a | 43 | BX LR |
ericebert | 0:57690853989a | 44 | ALIGN |
ericebert | 0:57690853989a | 45 | } |
ericebert | 0:57690853989a | 46 | |
ericebert | 0:57690853989a | 47 | |
ericebert | 0:57690853989a | 48 | /** |
ericebert | 0:57690853989a | 49 | ****************************************************************************** |
ericebert | 0:57690853989a | 50 | * @brief Decrease a byte integers and Saved into memory cell |
ericebert | 0:57690853989a | 51 | * @param[in] data byte integers. |
ericebert | 0:57690853989a | 52 | * @param[out] None |
ericebert | 0:57690853989a | 53 | * @retval Returns Original value. |
ericebert | 0:57690853989a | 54 | * |
ericebert | 0:57690853989a | 55 | * @par Description |
ericebert | 0:57690853989a | 56 | * @details This function is called to Decrease a byte integers |
ericebert | 0:57690853989a | 57 | * and Saved into memory cell. |
ericebert | 0:57690853989a | 58 | ****************************************************************************** |
ericebert | 0:57690853989a | 59 | */ |
ericebert | 0:57690853989a | 60 | __asm U8 Dec8 (volatile U8 *data) |
ericebert | 0:57690853989a | 61 | { |
ericebert | 0:57690853989a | 62 | PUSH {R1} |
ericebert | 0:57690853989a | 63 | CPSID I |
ericebert | 0:57690853989a | 64 | LDRB R1,[R0] |
ericebert | 0:57690853989a | 65 | SUBS R1,#1 |
ericebert | 0:57690853989a | 66 | STRB R1,[R0] |
ericebert | 0:57690853989a | 67 | CPSIE I |
ericebert | 0:57690853989a | 68 | MOVS R0,R1 |
ericebert | 0:57690853989a | 69 | POP {R1} |
ericebert | 0:57690853989a | 70 | BX LR |
ericebert | 0:57690853989a | 71 | ALIGN |
ericebert | 0:57690853989a | 72 | } |
ericebert | 0:57690853989a | 73 | |
ericebert | 0:57690853989a | 74 | /** |
ericebert | 0:57690853989a | 75 | ****************************************************************************** |
ericebert | 0:57690853989a | 76 | * @brief ENABLE Interrupt |
ericebert | 0:57690853989a | 77 | * @param[in] None |
ericebert | 0:57690853989a | 78 | * @param[out] None |
ericebert | 0:57690853989a | 79 | * @retval None |
ericebert | 0:57690853989a | 80 | * |
ericebert | 0:57690853989a | 81 | * @par Description |
ericebert | 0:57690853989a | 82 | * @details This function is called to ENABLE Interrupt. |
ericebert | 0:57690853989a | 83 | ****************************************************************************** |
ericebert | 0:57690853989a | 84 | */ |
ericebert | 0:57690853989a | 85 | __asm void IRQ_ENABLE_RESTORE(void) |
ericebert | 0:57690853989a | 86 | { CPSIE I |
ericebert | 0:57690853989a | 87 | BX LR |
ericebert | 0:57690853989a | 88 | } |
ericebert | 0:57690853989a | 89 | |
ericebert | 0:57690853989a | 90 | /** |
ericebert | 0:57690853989a | 91 | ****************************************************************************** |
ericebert | 0:57690853989a | 92 | * @brief Close Interrupt |
ericebert | 0:57690853989a | 93 | * @param[in] None |
ericebert | 0:57690853989a | 94 | * @param[out] None |
ericebert | 0:57690853989a | 95 | * @retval None |
ericebert | 0:57690853989a | 96 | * |
ericebert | 0:57690853989a | 97 | * @par Description |
ericebert | 0:57690853989a | 98 | * @details This function is called to close Interrupt. |
ericebert | 0:57690853989a | 99 | ****************************************************************************** |
ericebert | 0:57690853989a | 100 | */ |
ericebert | 0:57690853989a | 101 | __asm void IRQ_DISABLE_SAVE(void) |
ericebert | 0:57690853989a | 102 | { CPSID I |
ericebert | 0:57690853989a | 103 | BX LR |
ericebert | 0:57690853989a | 104 | } |
ericebert | 0:57690853989a | 105 | |
ericebert | 0:57690853989a | 106 | /** |
ericebert | 0:57690853989a | 107 | ****************************************************************************** |
ericebert | 0:57690853989a | 108 | * @brief Set environment for Coocox OS running |
ericebert | 0:57690853989a | 109 | * @param[in] pstk stack pointer |
ericebert | 0:57690853989a | 110 | * @param[out] None |
ericebert | 0:57690853989a | 111 | * @retval None. |
ericebert | 0:57690853989a | 112 | * |
ericebert | 0:57690853989a | 113 | * @par Description |
ericebert | 0:57690853989a | 114 | * @details This function is called to Set environment |
ericebert | 0:57690853989a | 115 | * for Coocox OS running. |
ericebert | 0:57690853989a | 116 | ****************************************************************************** |
ericebert | 0:57690853989a | 117 | */ |
ericebert | 0:57690853989a | 118 | __asm void SetEnvironment(OS_STK *pstk) |
ericebert | 0:57690853989a | 119 | { |
ericebert | 0:57690853989a | 120 | SUBS R0,#28 |
ericebert | 0:57690853989a | 121 | MSR PSP, R0 ; Mov new stack point to PSP |
ericebert | 0:57690853989a | 122 | BX LR |
ericebert | 0:57690853989a | 123 | ALIGN |
ericebert | 0:57690853989a | 124 | } |
ericebert | 0:57690853989a | 125 | |
ericebert | 0:57690853989a | 126 | |
ericebert | 0:57690853989a | 127 | /** |
ericebert | 0:57690853989a | 128 | ****************************************************************************** |
ericebert | 0:57690853989a | 129 | * @brief Do ready work to Switch Context for task change |
ericebert | 0:57690853989a | 130 | * @param[in] None |
ericebert | 0:57690853989a | 131 | * @param[out] None |
ericebert | 0:57690853989a | 132 | * @retval None. |
ericebert | 0:57690853989a | 133 | * |
ericebert | 0:57690853989a | 134 | * @par Description |
ericebert | 0:57690853989a | 135 | * @details This function is called to Do ready work to |
ericebert | 0:57690853989a | 136 | * Switch Context for task change |
ericebert | 0:57690853989a | 137 | ****************************************************************************** |
ericebert | 0:57690853989a | 138 | */ |
ericebert | 0:57690853989a | 139 | __asm void SwitchContext(void) |
ericebert | 0:57690853989a | 140 | { |
ericebert | 0:57690853989a | 141 | LDR R0, =0xE000ED04 ; Trigger the PendSV exception (causes context switch) |
ericebert | 0:57690853989a | 142 | LDR R1, =0x10000000 |
ericebert | 0:57690853989a | 143 | STR R1, [R0] |
ericebert | 0:57690853989a | 144 | BX LR |
ericebert | 0:57690853989a | 145 | ALIGN |
ericebert | 0:57690853989a | 146 | } |
ericebert | 0:57690853989a | 147 | |
ericebert | 0:57690853989a | 148 | |
ericebert | 0:57690853989a | 149 | /** |
ericebert | 0:57690853989a | 150 | ****************************************************************************** |
ericebert | 0:57690853989a | 151 | * @brief Switch Context for task change |
ericebert | 0:57690853989a | 152 | * @param[in] None |
ericebert | 0:57690853989a | 153 | * @param[out] None |
ericebert | 0:57690853989a | 154 | * @retval None. |
ericebert | 0:57690853989a | 155 | * |
ericebert | 0:57690853989a | 156 | * @par Description |
ericebert | 0:57690853989a | 157 | * @details This function is called to Switch Context for task change. |
ericebert | 0:57690853989a | 158 | ****************************************************************************** |
ericebert | 0:57690853989a | 159 | */ |
ericebert | 0:57690853989a | 160 | #if CFG_CHIP_TYPE == 2 |
ericebert | 0:57690853989a | 161 | extern "C" __asm void PendSV_Handler() |
ericebert | 0:57690853989a | 162 | { |
ericebert | 0:57690853989a | 163 | IMPORT TCBRunning |
ericebert | 0:57690853989a | 164 | IMPORT TCBNext |
ericebert | 0:57690853989a | 165 | IMPORT OSSchedLock |
ericebert | 0:57690853989a | 166 | LDR R3,=TCBRunning |
ericebert | 0:57690853989a | 167 | LDR R1,[R3] ; R1 == running tcb |
ericebert | 0:57690853989a | 168 | LDR R2,=TCBNext |
ericebert | 0:57690853989a | 169 | LDR R2,[R2] ; R2 == next tcb |
ericebert | 0:57690853989a | 170 | |
ericebert | 0:57690853989a | 171 | CMP R1,R2 |
ericebert | 0:57690853989a | 172 | BEQ exitPendSV |
ericebert | 0:57690853989a | 173 | MRS R0, PSP ; Get PSP point (can not use PUSH,in ISR,SP is MSP ) |
ericebert | 0:57690853989a | 174 | |
ericebert | 0:57690853989a | 175 | SUBS R0,R0,#32 |
ericebert | 0:57690853989a | 176 | STR R0,[R1] ; Save orig PSP |
ericebert | 0:57690853989a | 177 | ; Store r4-r11,r0 -= regCnt * 4,r0 is new stack |
ericebert | 0:57690853989a | 178 | ; top point (addr h->l r11,r10,...,r5,r4) |
ericebert | 0:57690853989a | 179 | STMIA R0!,{R4-R7} ; Save old context (R4-R7) |
ericebert | 0:57690853989a | 180 | MOV R4,R8 |
ericebert | 0:57690853989a | 181 | MOV R5,R9 |
ericebert | 0:57690853989a | 182 | MOV R6,R10 |
ericebert | 0:57690853989a | 183 | MOV R7,R11 |
ericebert | 0:57690853989a | 184 | STMIA R0!,{R4-R7} ; Save old context (R8-R11) |
ericebert | 0:57690853989a | 185 | |
ericebert | 0:57690853989a | 186 | |
ericebert | 0:57690853989a | 187 | popStk |
ericebert | 0:57690853989a | 188 | STR R2, [R3] ; TCBRunning = TCBNext; |
ericebert | 0:57690853989a | 189 | LDR R0, [R2] ; Get SP of task that be switch into. |
ericebert | 0:57690853989a | 190 | |
ericebert | 0:57690853989a | 191 | ADDS R0, R0,#16 |
ericebert | 0:57690853989a | 192 | LDMIA R0!,{R4-R7} ; Restore new Context (R8-R11) |
ericebert | 0:57690853989a | 193 | MOV R8,R4 |
ericebert | 0:57690853989a | 194 | MOV R9,R5 |
ericebert | 0:57690853989a | 195 | MOV R10,R6 |
ericebert | 0:57690853989a | 196 | MOV R11,R7 |
ericebert | 0:57690853989a | 197 | SUBS R0,R0,#32 |
ericebert | 0:57690853989a | 198 | LDMIA R0!,{R4-R7} ; Restore new Context (R4-R7) |
ericebert | 0:57690853989a | 199 | ADDS R0, R0,#16 |
ericebert | 0:57690853989a | 200 | MSR PSP, R0 ; Mov new stack point to PSP |
ericebert | 0:57690853989a | 201 | |
ericebert | 0:57690853989a | 202 | exitPendSV |
ericebert | 0:57690853989a | 203 | LDR R3,=OSSchedLock |
ericebert | 0:57690853989a | 204 | MOVS R0, #0x0 |
ericebert | 0:57690853989a | 205 | STRB R0, [R3] |
ericebert | 0:57690853989a | 206 | MOVS R0,#4 |
ericebert | 0:57690853989a | 207 | RSBS R0,#0 ; =0xFFFFFFFC,Ensure exception return uses process stack |
ericebert | 0:57690853989a | 208 | BX R0 ; Exit interrupt |
ericebert | 0:57690853989a | 209 | ALIGN |
ericebert | 0:57690853989a | 210 | } |
ericebert | 0:57690853989a | 211 | #endif |
ericebert | 0:57690853989a | 212 | |
ericebert | 0:57690853989a | 213 | |
ericebert | 0:57690853989a | 214 | #if CFG_CHIP_TYPE == 1 |
ericebert | 0:57690853989a | 215 | extern "C" __asm void PendSV_Handler() |
ericebert | 0:57690853989a | 216 | { |
ericebert | 0:57690853989a | 217 | IMPORT TCBRunning |
ericebert | 0:57690853989a | 218 | IMPORT TCBNext |
ericebert | 0:57690853989a | 219 | IMPORT OSSchedLock |
ericebert | 0:57690853989a | 220 | LDR R3,=TCBRunning |
ericebert | 0:57690853989a | 221 | LDR R1,[R3] ; R1 == running tcb |
ericebert | 0:57690853989a | 222 | LDR R2,=TCBNext |
ericebert | 0:57690853989a | 223 | LDR R2,[R2] ; R2 == next tcb |
ericebert | 0:57690853989a | 224 | |
ericebert | 0:57690853989a | 225 | CMP R1,R2 |
ericebert | 0:57690853989a | 226 | BEQ exitPendSV |
ericebert | 0:57690853989a | 227 | MRS R0, PSP ; Get PSP point (can not use PUSH,in ISR,SP is MSP ) |
ericebert | 0:57690853989a | 228 | STMDB R0!,{R4-R11} ; Store r4-r11,r0 -= regCnt * 4,r0 is new stack |
ericebert | 0:57690853989a | 229 | ; top point (addr h->l r11,r10,...,r5,r4) |
ericebert | 0:57690853989a | 230 | STR R0,[R1] ; Save orig PSP |
ericebert | 0:57690853989a | 231 | popStk |
ericebert | 0:57690853989a | 232 | STR R2, [R3] ; TCBRunning = TCBNext; |
ericebert | 0:57690853989a | 233 | LDR R0, [R2] ; Get SP of task that be switch into. |
ericebert | 0:57690853989a | 234 | LDMIA R0!,{R4-R11} ; POP (R4-R11),R0 += regCnt * 4 |
ericebert | 0:57690853989a | 235 | MSR PSP, R0 ; Mov new stack point to PSP |
ericebert | 0:57690853989a | 236 | |
ericebert | 0:57690853989a | 237 | exitPendSV |
ericebert | 0:57690853989a | 238 | LDR R3,=OSSchedLock |
ericebert | 0:57690853989a | 239 | MOVS R0, #0x0 |
ericebert | 0:57690853989a | 240 | STRB R0, [R3] |
ericebert | 0:57690853989a | 241 | ORR LR, LR, #0x04 ; Ensure exception return uses process stack |
ericebert | 0:57690853989a | 242 | BX LR ; Exit interrupt |
ericebert | 0:57690853989a | 243 | |
ericebert | 0:57690853989a | 244 | ALIGN |
ericebert | 0:57690853989a | 245 | } |
ericebert | 0:57690853989a | 246 | #endif |
ericebert | 0:57690853989a | 247 | |
ericebert | 0:57690853989a | 248 |