mbed rtos - modified for the final project of the course "Advanced Operating Systems"
Fork of mbed-rtos by
rtx/LPC1768/ARM/HAL_CM3.c@6:350b53afb889, 2012-11-23 (annotated)
- Committer:
- emilmont
- Date:
- Fri Nov 23 09:57:31 2012 +0000
- Revision:
- 6:350b53afb889
- Child:
- 11:db1fc233faa9
Merge RTOS C++ API and RTX under the same library; Update RTX to version 4.60; Add proper Thread destructor;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilmont | 6:350b53afb889 | 1 | /*---------------------------------------------------------------------------- |
emilmont | 6:350b53afb889 | 2 | * RL-ARM - RTX |
emilmont | 6:350b53afb889 | 3 | *---------------------------------------------------------------------------- |
emilmont | 6:350b53afb889 | 4 | * Name: HAL_CM3.C |
emilmont | 6:350b53afb889 | 5 | * Purpose: Hardware Abstraction Layer for Cortex-M3 |
emilmont | 6:350b53afb889 | 6 | * Rev.: V4.60 |
emilmont | 6:350b53afb889 | 7 | *---------------------------------------------------------------------------- |
emilmont | 6:350b53afb889 | 8 | * |
emilmont | 6:350b53afb889 | 9 | * Copyright (c) 1999-2009 KEIL, 2009-2012 ARM Germany GmbH |
emilmont | 6:350b53afb889 | 10 | * All rights reserved. |
emilmont | 6:350b53afb889 | 11 | * Redistribution and use in source and binary forms, with or without |
emilmont | 6:350b53afb889 | 12 | * modification, are permitted provided that the following conditions are met: |
emilmont | 6:350b53afb889 | 13 | * - Redistributions of source code must retain the above copyright |
emilmont | 6:350b53afb889 | 14 | * notice, this list of conditions and the following disclaimer. |
emilmont | 6:350b53afb889 | 15 | * - Redistributions in binary form must reproduce the above copyright |
emilmont | 6:350b53afb889 | 16 | * notice, this list of conditions and the following disclaimer in the |
emilmont | 6:350b53afb889 | 17 | * documentation and/or other materials provided with the distribution. |
emilmont | 6:350b53afb889 | 18 | * - Neither the name of ARM nor the names of its contributors may be used |
emilmont | 6:350b53afb889 | 19 | * to endorse or promote products derived from this software without |
emilmont | 6:350b53afb889 | 20 | * specific prior written permission. |
emilmont | 6:350b53afb889 | 21 | * |
emilmont | 6:350b53afb889 | 22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
emilmont | 6:350b53afb889 | 23 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
emilmont | 6:350b53afb889 | 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
emilmont | 6:350b53afb889 | 25 | * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE |
emilmont | 6:350b53afb889 | 26 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
emilmont | 6:350b53afb889 | 27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
emilmont | 6:350b53afb889 | 28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
emilmont | 6:350b53afb889 | 29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
emilmont | 6:350b53afb889 | 30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
emilmont | 6:350b53afb889 | 31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
emilmont | 6:350b53afb889 | 32 | * POSSIBILITY OF SUCH DAMAGE. |
emilmont | 6:350b53afb889 | 33 | *---------------------------------------------------------------------------*/ |
emilmont | 6:350b53afb889 | 34 | |
emilmont | 6:350b53afb889 | 35 | #include "rt_TypeDef.h" |
emilmont | 6:350b53afb889 | 36 | #include "RTX_Config.h" |
emilmont | 6:350b53afb889 | 37 | #include "rt_System.h" |
emilmont | 6:350b53afb889 | 38 | #include "rt_HAL_CM.h" |
emilmont | 6:350b53afb889 | 39 | #include "rt_Task.h" |
emilmont | 6:350b53afb889 | 40 | #include "rt_MemBox.h" |
emilmont | 6:350b53afb889 | 41 | |
emilmont | 6:350b53afb889 | 42 | |
emilmont | 6:350b53afb889 | 43 | /*---------------------------------------------------------------------------- |
emilmont | 6:350b53afb889 | 44 | * Functions |
emilmont | 6:350b53afb889 | 45 | *---------------------------------------------------------------------------*/ |
emilmont | 6:350b53afb889 | 46 | |
emilmont | 6:350b53afb889 | 47 | |
emilmont | 6:350b53afb889 | 48 | /*--------------------------- rt_set_PSP ------------------------------------*/ |
emilmont | 6:350b53afb889 | 49 | |
emilmont | 6:350b53afb889 | 50 | __asm void rt_set_PSP (U32 stack) { |
emilmont | 6:350b53afb889 | 51 | MSR PSP,R0 |
emilmont | 6:350b53afb889 | 52 | BX LR |
emilmont | 6:350b53afb889 | 53 | } |
emilmont | 6:350b53afb889 | 54 | |
emilmont | 6:350b53afb889 | 55 | |
emilmont | 6:350b53afb889 | 56 | /*--------------------------- rt_get_PSP ------------------------------------*/ |
emilmont | 6:350b53afb889 | 57 | |
emilmont | 6:350b53afb889 | 58 | __asm U32 rt_get_PSP (void) { |
emilmont | 6:350b53afb889 | 59 | MRS R0,PSP |
emilmont | 6:350b53afb889 | 60 | BX LR |
emilmont | 6:350b53afb889 | 61 | } |
emilmont | 6:350b53afb889 | 62 | |
emilmont | 6:350b53afb889 | 63 | |
emilmont | 6:350b53afb889 | 64 | /*--------------------------- os_set_env ------------------------------------*/ |
emilmont | 6:350b53afb889 | 65 | |
emilmont | 6:350b53afb889 | 66 | __asm void os_set_env (void) { |
emilmont | 6:350b53afb889 | 67 | /* Switch to Unprivileged/Privileged Thread mode, use PSP. */ |
emilmont | 6:350b53afb889 | 68 | MOV R0,SP ; PSP = MSP |
emilmont | 6:350b53afb889 | 69 | MSR PSP,R0 |
emilmont | 6:350b53afb889 | 70 | LDR R0,=__cpp(&os_flags) |
emilmont | 6:350b53afb889 | 71 | LDRB R0,[R0] |
emilmont | 6:350b53afb889 | 72 | LSLS R0,#31 |
emilmont | 6:350b53afb889 | 73 | MOVNE R0,#0x02 ; Privileged Thread mode, use PSP |
emilmont | 6:350b53afb889 | 74 | MOVEQ R0,#0x03 ; Unprivileged Thread mode, use PSP |
emilmont | 6:350b53afb889 | 75 | MSR CONTROL,R0 |
emilmont | 6:350b53afb889 | 76 | BX LR |
emilmont | 6:350b53afb889 | 77 | |
emilmont | 6:350b53afb889 | 78 | ALIGN |
emilmont | 6:350b53afb889 | 79 | } |
emilmont | 6:350b53afb889 | 80 | |
emilmont | 6:350b53afb889 | 81 | |
emilmont | 6:350b53afb889 | 82 | /*--------------------------- _alloc_box ------------------------------------*/ |
emilmont | 6:350b53afb889 | 83 | |
emilmont | 6:350b53afb889 | 84 | __asm void *_alloc_box (void *box_mem) { |
emilmont | 6:350b53afb889 | 85 | /* Function wrapper for Unprivileged/Privileged mode. */ |
emilmont | 6:350b53afb889 | 86 | LDR R12,=__cpp(rt_alloc_box) |
emilmont | 6:350b53afb889 | 87 | MRS R3,IPSR |
emilmont | 6:350b53afb889 | 88 | LSLS R3,#24 |
emilmont | 6:350b53afb889 | 89 | BXNE R12 |
emilmont | 6:350b53afb889 | 90 | MRS R3,CONTROL |
emilmont | 6:350b53afb889 | 91 | LSLS R3,#31 |
emilmont | 6:350b53afb889 | 92 | BXEQ R12 |
emilmont | 6:350b53afb889 | 93 | SVC 0 |
emilmont | 6:350b53afb889 | 94 | BX LR |
emilmont | 6:350b53afb889 | 95 | |
emilmont | 6:350b53afb889 | 96 | ALIGN |
emilmont | 6:350b53afb889 | 97 | } |
emilmont | 6:350b53afb889 | 98 | |
emilmont | 6:350b53afb889 | 99 | |
emilmont | 6:350b53afb889 | 100 | /*--------------------------- _free_box -------------------------------------*/ |
emilmont | 6:350b53afb889 | 101 | |
emilmont | 6:350b53afb889 | 102 | __asm int _free_box (void *box_mem, void *box) { |
emilmont | 6:350b53afb889 | 103 | /* Function wrapper for Unprivileged/Privileged mode. */ |
emilmont | 6:350b53afb889 | 104 | LDR R12,=__cpp(rt_free_box) |
emilmont | 6:350b53afb889 | 105 | MRS R3,IPSR |
emilmont | 6:350b53afb889 | 106 | LSLS R3,#24 |
emilmont | 6:350b53afb889 | 107 | BXNE R12 |
emilmont | 6:350b53afb889 | 108 | MRS R3,CONTROL |
emilmont | 6:350b53afb889 | 109 | LSLS R3,#31 |
emilmont | 6:350b53afb889 | 110 | BXEQ R12 |
emilmont | 6:350b53afb889 | 111 | SVC 0 |
emilmont | 6:350b53afb889 | 112 | BX LR |
emilmont | 6:350b53afb889 | 113 | |
emilmont | 6:350b53afb889 | 114 | ALIGN |
emilmont | 6:350b53afb889 | 115 | } |
emilmont | 6:350b53afb889 | 116 | |
emilmont | 6:350b53afb889 | 117 | |
emilmont | 6:350b53afb889 | 118 | /*-------------------------- SVC_Handler ------------------------------------*/ |
emilmont | 6:350b53afb889 | 119 | |
emilmont | 6:350b53afb889 | 120 | __asm void SVC_Handler (void) { |
emilmont | 6:350b53afb889 | 121 | PRESERVE8 |
emilmont | 6:350b53afb889 | 122 | |
emilmont | 6:350b53afb889 | 123 | IMPORT SVC_Count |
emilmont | 6:350b53afb889 | 124 | IMPORT SVC_Table |
emilmont | 6:350b53afb889 | 125 | IMPORT rt_stk_check |
emilmont | 6:350b53afb889 | 126 | |
emilmont | 6:350b53afb889 | 127 | MRS R0,PSP ; Read PSP |
emilmont | 6:350b53afb889 | 128 | LDR R1,[R0,#24] ; Read Saved PC from Stack |
emilmont | 6:350b53afb889 | 129 | LDRB R1,[R1,#-2] ; Load SVC Number |
emilmont | 6:350b53afb889 | 130 | CBNZ R1,SVC_User |
emilmont | 6:350b53afb889 | 131 | |
emilmont | 6:350b53afb889 | 132 | LDM R0,{R0-R3,R12} ; Read R0-R3,R12 from stack |
emilmont | 6:350b53afb889 | 133 | BLX R12 ; Call SVC Function |
emilmont | 6:350b53afb889 | 134 | |
emilmont | 6:350b53afb889 | 135 | MRS R12,PSP ; Read PSP |
emilmont | 6:350b53afb889 | 136 | STM R12,{R0-R2} ; Store return values |
emilmont | 6:350b53afb889 | 137 | |
emilmont | 6:350b53afb889 | 138 | LDR R3,=__cpp(&os_tsk) |
emilmont | 6:350b53afb889 | 139 | LDM R3,{R1,R2} ; os_tsk.run, os_tsk.new |
emilmont | 6:350b53afb889 | 140 | CMP R1,R2 |
emilmont | 6:350b53afb889 | 141 | BEQ SVC_Exit ; no task switch |
emilmont | 6:350b53afb889 | 142 | |
emilmont | 6:350b53afb889 | 143 | CBZ R1,SVC_Next ; Runtask deleted? |
emilmont | 6:350b53afb889 | 144 | STMDB R12!,{R4-R11} ; Save Old context |
emilmont | 6:350b53afb889 | 145 | STR R12,[R1,#TCB_TSTACK] ; Update os_tsk.run->tsk_stack |
emilmont | 6:350b53afb889 | 146 | |
emilmont | 6:350b53afb889 | 147 | PUSH {R2,R3} |
emilmont | 6:350b53afb889 | 148 | BL rt_stk_check ; Check for Stack overflow |
emilmont | 6:350b53afb889 | 149 | POP {R2,R3} |
emilmont | 6:350b53afb889 | 150 | |
emilmont | 6:350b53afb889 | 151 | SVC_Next |
emilmont | 6:350b53afb889 | 152 | STR R2,[R3] ; os_tsk.run = os_tsk.new |
emilmont | 6:350b53afb889 | 153 | |
emilmont | 6:350b53afb889 | 154 | LDR R12,[R2,#TCB_TSTACK] ; os_tsk.new->tsk_stack |
emilmont | 6:350b53afb889 | 155 | LDMIA R12!,{R4-R11} ; Restore New Context |
emilmont | 6:350b53afb889 | 156 | MSR PSP,R12 ; Write PSP |
emilmont | 6:350b53afb889 | 157 | |
emilmont | 6:350b53afb889 | 158 | SVC_Exit |
emilmont | 6:350b53afb889 | 159 | MVN LR,#:NOT:0xFFFFFFFD ; set EXC_RETURN value |
emilmont | 6:350b53afb889 | 160 | BX LR |
emilmont | 6:350b53afb889 | 161 | |
emilmont | 6:350b53afb889 | 162 | /*------------------- User SVC ------------------------------*/ |
emilmont | 6:350b53afb889 | 163 | |
emilmont | 6:350b53afb889 | 164 | SVC_User |
emilmont | 6:350b53afb889 | 165 | PUSH {R4,LR} ; Save Registers |
emilmont | 6:350b53afb889 | 166 | LDR R2,=SVC_Count |
emilmont | 6:350b53afb889 | 167 | LDR R2,[R2] |
emilmont | 6:350b53afb889 | 168 | CMP R1,R2 |
emilmont | 6:350b53afb889 | 169 | BHI SVC_Done ; Overflow |
emilmont | 6:350b53afb889 | 170 | |
emilmont | 6:350b53afb889 | 171 | LDR R4,=SVC_Table-4 |
emilmont | 6:350b53afb889 | 172 | LDR R4,[R4,R1,LSL #2] ; Load SVC Function Address |
emilmont | 6:350b53afb889 | 173 | |
emilmont | 6:350b53afb889 | 174 | LDM R0,{R0-R3,R12} ; Read R0-R3,R12 from stack |
emilmont | 6:350b53afb889 | 175 | BLX R4 ; Call SVC Function |
emilmont | 6:350b53afb889 | 176 | |
emilmont | 6:350b53afb889 | 177 | MRS R12,PSP |
emilmont | 6:350b53afb889 | 178 | STM R12,{R0-R3} ; Function return values |
emilmont | 6:350b53afb889 | 179 | SVC_Done |
emilmont | 6:350b53afb889 | 180 | POP {R4,PC} ; RETI |
emilmont | 6:350b53afb889 | 181 | |
emilmont | 6:350b53afb889 | 182 | ALIGN |
emilmont | 6:350b53afb889 | 183 | } |
emilmont | 6:350b53afb889 | 184 | |
emilmont | 6:350b53afb889 | 185 | |
emilmont | 6:350b53afb889 | 186 | /*-------------------------- PendSV_Handler ---------------------------------*/ |
emilmont | 6:350b53afb889 | 187 | |
emilmont | 6:350b53afb889 | 188 | __asm void PendSV_Handler (void) { |
emilmont | 6:350b53afb889 | 189 | PRESERVE8 |
emilmont | 6:350b53afb889 | 190 | |
emilmont | 6:350b53afb889 | 191 | BL __cpp(rt_pop_req) |
emilmont | 6:350b53afb889 | 192 | |
emilmont | 6:350b53afb889 | 193 | Sys_Switch |
emilmont | 6:350b53afb889 | 194 | LDR R3,=__cpp(&os_tsk) |
emilmont | 6:350b53afb889 | 195 | LDM R3,{R1,R2} ; os_tsk.run, os_tsk.new |
emilmont | 6:350b53afb889 | 196 | CMP R1,R2 |
emilmont | 6:350b53afb889 | 197 | BEQ Sys_Exit |
emilmont | 6:350b53afb889 | 198 | |
emilmont | 6:350b53afb889 | 199 | MRS R12,PSP ; Read PSP |
emilmont | 6:350b53afb889 | 200 | STMDB R12!,{R4-R11} ; Save Old context |
emilmont | 6:350b53afb889 | 201 | STR R12,[R1,#TCB_TSTACK] ; Update os_tsk.run->tsk_stack |
emilmont | 6:350b53afb889 | 202 | |
emilmont | 6:350b53afb889 | 203 | PUSH {R2,R3} |
emilmont | 6:350b53afb889 | 204 | BL rt_stk_check ; Check for Stack overflow |
emilmont | 6:350b53afb889 | 205 | POP {R2,R3} |
emilmont | 6:350b53afb889 | 206 | |
emilmont | 6:350b53afb889 | 207 | STR R2,[R3] ; os_tsk.run = os_tsk.new |
emilmont | 6:350b53afb889 | 208 | |
emilmont | 6:350b53afb889 | 209 | LDR R12,[R2,#TCB_TSTACK] ; os_tsk.new->tsk_stack |
emilmont | 6:350b53afb889 | 210 | LDMIA R12!,{R4-R11} ; Restore New Context |
emilmont | 6:350b53afb889 | 211 | MSR PSP,R12 ; Write PSP |
emilmont | 6:350b53afb889 | 212 | |
emilmont | 6:350b53afb889 | 213 | Sys_Exit |
emilmont | 6:350b53afb889 | 214 | MVN LR,#:NOT:0xFFFFFFFD ; set EXC_RETURN value |
emilmont | 6:350b53afb889 | 215 | BX LR ; Return to Thread Mode |
emilmont | 6:350b53afb889 | 216 | |
emilmont | 6:350b53afb889 | 217 | ALIGN |
emilmont | 6:350b53afb889 | 218 | } |
emilmont | 6:350b53afb889 | 219 | |
emilmont | 6:350b53afb889 | 220 | |
emilmont | 6:350b53afb889 | 221 | /*-------------------------- SysTick_Handler --------------------------------*/ |
emilmont | 6:350b53afb889 | 222 | |
emilmont | 6:350b53afb889 | 223 | __asm void SysTick_Handler (void) { |
emilmont | 6:350b53afb889 | 224 | PRESERVE8 |
emilmont | 6:350b53afb889 | 225 | |
emilmont | 6:350b53afb889 | 226 | BL __cpp(rt_systick) |
emilmont | 6:350b53afb889 | 227 | B Sys_Switch |
emilmont | 6:350b53afb889 | 228 | |
emilmont | 6:350b53afb889 | 229 | ALIGN |
emilmont | 6:350b53afb889 | 230 | } |
emilmont | 6:350b53afb889 | 231 | |
emilmont | 6:350b53afb889 | 232 | |
emilmont | 6:350b53afb889 | 233 | /*-------------------------- OS_Tick_Handler --------------------------------*/ |
emilmont | 6:350b53afb889 | 234 | |
emilmont | 6:350b53afb889 | 235 | __asm void OS_Tick_Handler (void) { |
emilmont | 6:350b53afb889 | 236 | PRESERVE8 |
emilmont | 6:350b53afb889 | 237 | |
emilmont | 6:350b53afb889 | 238 | BL __cpp(os_tick_irqack) |
emilmont | 6:350b53afb889 | 239 | BL __cpp(rt_systick) |
emilmont | 6:350b53afb889 | 240 | B Sys_Switch |
emilmont | 6:350b53afb889 | 241 | |
emilmont | 6:350b53afb889 | 242 | ALIGN |
emilmont | 6:350b53afb889 | 243 | } |
emilmont | 6:350b53afb889 | 244 | |
emilmont | 6:350b53afb889 | 245 | |
emilmont | 6:350b53afb889 | 246 | /*---------------------------------------------------------------------------- |
emilmont | 6:350b53afb889 | 247 | * end of file |
emilmont | 6:350b53afb889 | 248 | *---------------------------------------------------------------------------*/ |
emilmont | 6:350b53afb889 | 249 |