Lab Checkoff

Dependencies:   SDFileSystem TextLCD mbed-rtos mbed wave_player FATFileSystem

Committer:
doubster
Date:
Wed Nov 13 20:00:28 2013 +0000
Revision:
0:67dbd54e60d4
Lab Checkoff

Who changed what in which revision?

UserRevisionLine numberNew contents of line
doubster 0:67dbd54e60d4 1 /*----------------------------------------------------------------------------
doubster 0:67dbd54e60d4 2 * RL-ARM - RTX
doubster 0:67dbd54e60d4 3 *----------------------------------------------------------------------------
doubster 0:67dbd54e60d4 4 * Name: HAL_CM0.C
doubster 0:67dbd54e60d4 5 * Purpose: Hardware Abstraction Layer for Cortex-M0
doubster 0:67dbd54e60d4 6 * Rev.: V4.60
doubster 0:67dbd54e60d4 7 *----------------------------------------------------------------------------
doubster 0:67dbd54e60d4 8 *
doubster 0:67dbd54e60d4 9 * Copyright (c) 1999-2009 KEIL, 2009-2012 ARM Germany GmbH
doubster 0:67dbd54e60d4 10 * All rights reserved.
doubster 0:67dbd54e60d4 11 * Redistribution and use in source and binary forms, with or without
doubster 0:67dbd54e60d4 12 * modification, are permitted provided that the following conditions are met:
doubster 0:67dbd54e60d4 13 * - Redistributions of source code must retain the above copyright
doubster 0:67dbd54e60d4 14 * notice, this list of conditions and the following disclaimer.
doubster 0:67dbd54e60d4 15 * - Redistributions in binary form must reproduce the above copyright
doubster 0:67dbd54e60d4 16 * notice, this list of conditions and the following disclaimer in the
doubster 0:67dbd54e60d4 17 * documentation and/or other materials provided with the distribution.
doubster 0:67dbd54e60d4 18 * - Neither the name of ARM nor the names of its contributors may be used
doubster 0:67dbd54e60d4 19 * to endorse or promote products derived from this software without
doubster 0:67dbd54e60d4 20 * specific prior written permission.
doubster 0:67dbd54e60d4 21 *
doubster 0:67dbd54e60d4 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
doubster 0:67dbd54e60d4 23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
doubster 0:67dbd54e60d4 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
doubster 0:67dbd54e60d4 25 * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
doubster 0:67dbd54e60d4 26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
doubster 0:67dbd54e60d4 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
doubster 0:67dbd54e60d4 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
doubster 0:67dbd54e60d4 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
doubster 0:67dbd54e60d4 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
doubster 0:67dbd54e60d4 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
doubster 0:67dbd54e60d4 32 * POSSIBILITY OF SUCH DAMAGE.
doubster 0:67dbd54e60d4 33 *---------------------------------------------------------------------------*/
doubster 0:67dbd54e60d4 34
doubster 0:67dbd54e60d4 35 #include "rt_TypeDef.h"
doubster 0:67dbd54e60d4 36 #include "RTX_Config.h"
doubster 0:67dbd54e60d4 37 #include "rt_System.h"
doubster 0:67dbd54e60d4 38 #include "rt_HAL_CM.h"
doubster 0:67dbd54e60d4 39 #include "rt_Task.h"
doubster 0:67dbd54e60d4 40 #include "rt_MemBox.h"
doubster 0:67dbd54e60d4 41
doubster 0:67dbd54e60d4 42
doubster 0:67dbd54e60d4 43 /*----------------------------------------------------------------------------
doubster 0:67dbd54e60d4 44 * Functions
doubster 0:67dbd54e60d4 45 *---------------------------------------------------------------------------*/
doubster 0:67dbd54e60d4 46
doubster 0:67dbd54e60d4 47
doubster 0:67dbd54e60d4 48 /*--------------------------- rt_set_PSP ------------------------------------*/
doubster 0:67dbd54e60d4 49
doubster 0:67dbd54e60d4 50 __asm void rt_set_PSP (U32 stack) {
doubster 0:67dbd54e60d4 51 MSR PSP,R0
doubster 0:67dbd54e60d4 52 BX LR
doubster 0:67dbd54e60d4 53 }
doubster 0:67dbd54e60d4 54
doubster 0:67dbd54e60d4 55
doubster 0:67dbd54e60d4 56 /*--------------------------- rt_get_PSP ------------------------------------*/
doubster 0:67dbd54e60d4 57
doubster 0:67dbd54e60d4 58 __asm U32 rt_get_PSP (void) {
doubster 0:67dbd54e60d4 59 MRS R0,PSP
doubster 0:67dbd54e60d4 60 BX LR
doubster 0:67dbd54e60d4 61 }
doubster 0:67dbd54e60d4 62
doubster 0:67dbd54e60d4 63
doubster 0:67dbd54e60d4 64 /*--------------------------- os_set_env ------------------------------------*/
doubster 0:67dbd54e60d4 65
doubster 0:67dbd54e60d4 66 __asm void os_set_env (void) {
doubster 0:67dbd54e60d4 67 /* Switch to Unprivileged/Privileged Thread mode, use PSP. */
doubster 0:67dbd54e60d4 68 MOV R0,SP ; PSP = MSP
doubster 0:67dbd54e60d4 69 MSR PSP,R0
doubster 0:67dbd54e60d4 70 LDR R0,=__cpp(&os_flags)
doubster 0:67dbd54e60d4 71 LDRB R0,[R0]
doubster 0:67dbd54e60d4 72 LSLS R0,#31
doubster 0:67dbd54e60d4 73 BNE PrivilegedE
doubster 0:67dbd54e60d4 74 MOVS R0,#0x03 ; Unprivileged Thread mode, use PSP
doubster 0:67dbd54e60d4 75 MSR CONTROL,R0
doubster 0:67dbd54e60d4 76 BX LR
doubster 0:67dbd54e60d4 77 PrivilegedE
doubster 0:67dbd54e60d4 78 MOVS R0,#0x02 ; Privileged Thread mode, use PSP
doubster 0:67dbd54e60d4 79 MSR CONTROL,R0
doubster 0:67dbd54e60d4 80 BX LR
doubster 0:67dbd54e60d4 81
doubster 0:67dbd54e60d4 82 ALIGN
doubster 0:67dbd54e60d4 83 }
doubster 0:67dbd54e60d4 84
doubster 0:67dbd54e60d4 85
doubster 0:67dbd54e60d4 86 /*--------------------------- _alloc_box ------------------------------------*/
doubster 0:67dbd54e60d4 87
doubster 0:67dbd54e60d4 88 __asm void *_alloc_box (void *box_mem) {
doubster 0:67dbd54e60d4 89 /* Function wrapper for Unprivileged/Privileged mode. */
doubster 0:67dbd54e60d4 90 LDR R3,=__cpp(rt_alloc_box)
doubster 0:67dbd54e60d4 91 MOV R12,R3
doubster 0:67dbd54e60d4 92 MRS R3,IPSR
doubster 0:67dbd54e60d4 93 LSLS R3,#24
doubster 0:67dbd54e60d4 94 BNE PrivilegedA
doubster 0:67dbd54e60d4 95 MRS R3,CONTROL
doubster 0:67dbd54e60d4 96 LSLS R3,#31
doubster 0:67dbd54e60d4 97 BEQ PrivilegedA
doubster 0:67dbd54e60d4 98 SVC 0
doubster 0:67dbd54e60d4 99 BX LR
doubster 0:67dbd54e60d4 100 PrivilegedA
doubster 0:67dbd54e60d4 101 BX R12
doubster 0:67dbd54e60d4 102
doubster 0:67dbd54e60d4 103 ALIGN
doubster 0:67dbd54e60d4 104 }
doubster 0:67dbd54e60d4 105
doubster 0:67dbd54e60d4 106
doubster 0:67dbd54e60d4 107 /*--------------------------- _free_box -------------------------------------*/
doubster 0:67dbd54e60d4 108
doubster 0:67dbd54e60d4 109 __asm int _free_box (void *box_mem, void *box) {
doubster 0:67dbd54e60d4 110 /* Function wrapper for Unprivileged/Privileged mode. */
doubster 0:67dbd54e60d4 111 LDR R3,=__cpp(rt_free_box)
doubster 0:67dbd54e60d4 112 MOV R12,R3
doubster 0:67dbd54e60d4 113 MRS R3,IPSR
doubster 0:67dbd54e60d4 114 LSLS R3,#24
doubster 0:67dbd54e60d4 115 BNE PrivilegedF
doubster 0:67dbd54e60d4 116 MRS R3,CONTROL
doubster 0:67dbd54e60d4 117 LSLS R3,#31
doubster 0:67dbd54e60d4 118 BEQ PrivilegedF
doubster 0:67dbd54e60d4 119 SVC 0
doubster 0:67dbd54e60d4 120 BX LR
doubster 0:67dbd54e60d4 121 PrivilegedF
doubster 0:67dbd54e60d4 122 BX R12
doubster 0:67dbd54e60d4 123
doubster 0:67dbd54e60d4 124 ALIGN
doubster 0:67dbd54e60d4 125 }
doubster 0:67dbd54e60d4 126
doubster 0:67dbd54e60d4 127
doubster 0:67dbd54e60d4 128 /*-------------------------- SVC_Handler ------------------------------------*/
doubster 0:67dbd54e60d4 129
doubster 0:67dbd54e60d4 130 __asm void SVC_Handler (void) {
doubster 0:67dbd54e60d4 131 PRESERVE8
doubster 0:67dbd54e60d4 132
doubster 0:67dbd54e60d4 133 IMPORT SVC_Count
doubster 0:67dbd54e60d4 134 IMPORT SVC_Table
doubster 0:67dbd54e60d4 135 IMPORT rt_stk_check
doubster 0:67dbd54e60d4 136
doubster 0:67dbd54e60d4 137 MRS R0,PSP ; Read PSP
doubster 0:67dbd54e60d4 138 LDR R1,[R0,#24] ; Read Saved PC from Stack
doubster 0:67dbd54e60d4 139 SUBS R1,R1,#2 ; Point to SVC Instruction
doubster 0:67dbd54e60d4 140 LDRB R1,[R1] ; Load SVC Number
doubster 0:67dbd54e60d4 141 CMP R1,#0
doubster 0:67dbd54e60d4 142 BNE SVC_User ; User SVC Number > 0
doubster 0:67dbd54e60d4 143
doubster 0:67dbd54e60d4 144 MOV LR,R4
doubster 0:67dbd54e60d4 145 LDMIA R0,{R0-R3,R4} ; Read R0-R3,R12 from stack
doubster 0:67dbd54e60d4 146 MOV R12,R4
doubster 0:67dbd54e60d4 147 MOV R4,LR
doubster 0:67dbd54e60d4 148 BLX R12 ; Call SVC Function
doubster 0:67dbd54e60d4 149
doubster 0:67dbd54e60d4 150 MRS R3,PSP ; Read PSP
doubster 0:67dbd54e60d4 151 STMIA R3!,{R0-R2} ; Store return values
doubster 0:67dbd54e60d4 152
doubster 0:67dbd54e60d4 153 LDR R3,=__cpp(&os_tsk)
doubster 0:67dbd54e60d4 154 LDMIA R3!,{R1,R2} ; os_tsk.run, os_tsk.new
doubster 0:67dbd54e60d4 155 CMP R1,R2
doubster 0:67dbd54e60d4 156 BEQ SVC_Exit ; no task switch
doubster 0:67dbd54e60d4 157
doubster 0:67dbd54e60d4 158 SUBS R3,#8
doubster 0:67dbd54e60d4 159 CMP R1,#0 ; Runtask deleted?
doubster 0:67dbd54e60d4 160 BEQ SVC_Next
doubster 0:67dbd54e60d4 161
doubster 0:67dbd54e60d4 162 MRS R0,PSP ; Read PSP
doubster 0:67dbd54e60d4 163 SUBS R0,R0,#32 ; Adjust Start Address
doubster 0:67dbd54e60d4 164 STR R0,[R1,#TCB_TSTACK] ; Update os_tsk.run->tsk_stack
doubster 0:67dbd54e60d4 165 STMIA R0!,{R4-R7} ; Save old context (R4-R7)
doubster 0:67dbd54e60d4 166 MOV R4,R8
doubster 0:67dbd54e60d4 167 MOV R5,R9
doubster 0:67dbd54e60d4 168 MOV R6,R10
doubster 0:67dbd54e60d4 169 MOV R7,R11
doubster 0:67dbd54e60d4 170 STMIA R0!,{R4-R7} ; Save old context (R8-R11)
doubster 0:67dbd54e60d4 171
doubster 0:67dbd54e60d4 172 PUSH {R2,R3}
doubster 0:67dbd54e60d4 173 BL rt_stk_check ; Check for Stack overflow
doubster 0:67dbd54e60d4 174 POP {R2,R3}
doubster 0:67dbd54e60d4 175
doubster 0:67dbd54e60d4 176 SVC_Next
doubster 0:67dbd54e60d4 177 STR R2,[R3] ; os_tsk.run = os_tsk.new
doubster 0:67dbd54e60d4 178
doubster 0:67dbd54e60d4 179 LDR R0,[R2,#TCB_TSTACK] ; os_tsk.new->tsk_stack
doubster 0:67dbd54e60d4 180 ADDS R0,R0,#16 ; Adjust Start Address
doubster 0:67dbd54e60d4 181 LDMIA R0!,{R4-R7} ; Restore new Context (R8-R11)
doubster 0:67dbd54e60d4 182 MOV R8,R4
doubster 0:67dbd54e60d4 183 MOV R9,R5
doubster 0:67dbd54e60d4 184 MOV R10,R6
doubster 0:67dbd54e60d4 185 MOV R11,R7
doubster 0:67dbd54e60d4 186 MSR PSP,R0 ; Write PSP
doubster 0:67dbd54e60d4 187 SUBS R0,R0,#32 ; Adjust Start Address
doubster 0:67dbd54e60d4 188 LDMIA R0!,{R4-R7} ; Restore new Context (R4-R7)
doubster 0:67dbd54e60d4 189
doubster 0:67dbd54e60d4 190 SVC_Exit
doubster 0:67dbd54e60d4 191 MOVS R0,#:NOT:0xFFFFFFFD ; Set EXC_RETURN value
doubster 0:67dbd54e60d4 192 MVNS R0,R0
doubster 0:67dbd54e60d4 193 BX R0 ; RETI to Thread Mode, use PSP
doubster 0:67dbd54e60d4 194
doubster 0:67dbd54e60d4 195 /*------------------- User SVC ------------------------------*/
doubster 0:67dbd54e60d4 196
doubster 0:67dbd54e60d4 197 SVC_User
doubster 0:67dbd54e60d4 198 PUSH {R4,LR} ; Save Registers
doubster 0:67dbd54e60d4 199 LDR R2,=SVC_Count
doubster 0:67dbd54e60d4 200 LDR R2,[R2]
doubster 0:67dbd54e60d4 201 CMP R1,R2
doubster 0:67dbd54e60d4 202 BHI SVC_Done ; Overflow
doubster 0:67dbd54e60d4 203
doubster 0:67dbd54e60d4 204 LDR R4,=SVC_Table-4
doubster 0:67dbd54e60d4 205 LSLS R1,R1,#2
doubster 0:67dbd54e60d4 206 LDR R4,[R4,R1] ; Load SVC Function Address
doubster 0:67dbd54e60d4 207 MOV LR,R4
doubster 0:67dbd54e60d4 208
doubster 0:67dbd54e60d4 209 LDMIA R0,{R0-R3,R4} ; Read R0-R3,R12 from stack
doubster 0:67dbd54e60d4 210 MOV R12,R4
doubster 0:67dbd54e60d4 211 BLX LR ; Call SVC Function
doubster 0:67dbd54e60d4 212
doubster 0:67dbd54e60d4 213 MRS R4,PSP ; Read PSP
doubster 0:67dbd54e60d4 214 STMIA R4!,{R0-R3} ; Function return values
doubster 0:67dbd54e60d4 215 SVC_Done
doubster 0:67dbd54e60d4 216 POP {R4,PC} ; RETI
doubster 0:67dbd54e60d4 217
doubster 0:67dbd54e60d4 218 ALIGN
doubster 0:67dbd54e60d4 219 }
doubster 0:67dbd54e60d4 220
doubster 0:67dbd54e60d4 221
doubster 0:67dbd54e60d4 222 /*-------------------------- PendSV_Handler ---------------------------------*/
doubster 0:67dbd54e60d4 223
doubster 0:67dbd54e60d4 224 __asm void PendSV_Handler (void) {
doubster 0:67dbd54e60d4 225 PRESERVE8
doubster 0:67dbd54e60d4 226
doubster 0:67dbd54e60d4 227 BL __cpp(rt_pop_req)
doubster 0:67dbd54e60d4 228
doubster 0:67dbd54e60d4 229 Sys_Switch
doubster 0:67dbd54e60d4 230 LDR R3,=__cpp(&os_tsk)
doubster 0:67dbd54e60d4 231 LDMIA R3!,{R1,R2} ; os_tsk.run, os_tsk.new
doubster 0:67dbd54e60d4 232 CMP R1,R2
doubster 0:67dbd54e60d4 233 BEQ Sys_Exit ; no task switch
doubster 0:67dbd54e60d4 234
doubster 0:67dbd54e60d4 235 SUBS R3,#8
doubster 0:67dbd54e60d4 236
doubster 0:67dbd54e60d4 237 MRS R0,PSP ; Read PSP
doubster 0:67dbd54e60d4 238 SUBS R0,R0,#32 ; Adjust Start Address
doubster 0:67dbd54e60d4 239 STR R0,[R1,#TCB_TSTACK] ; Update os_tsk.run->tsk_stack
doubster 0:67dbd54e60d4 240 STMIA R0!,{R4-R7} ; Save old context (R4-R7)
doubster 0:67dbd54e60d4 241 MOV R4,R8
doubster 0:67dbd54e60d4 242 MOV R5,R9
doubster 0:67dbd54e60d4 243 MOV R6,R10
doubster 0:67dbd54e60d4 244 MOV R7,R11
doubster 0:67dbd54e60d4 245 STMIA R0!,{R4-R7} ; Save old context (R8-R11)
doubster 0:67dbd54e60d4 246
doubster 0:67dbd54e60d4 247 PUSH {R2,R3}
doubster 0:67dbd54e60d4 248 BL rt_stk_check ; Check for Stack overflow
doubster 0:67dbd54e60d4 249 POP {R2,R3}
doubster 0:67dbd54e60d4 250
doubster 0:67dbd54e60d4 251 STR R2,[R3] ; os_tsk.run = os_tsk.new
doubster 0:67dbd54e60d4 252
doubster 0:67dbd54e60d4 253 LDR R0,[R2,#TCB_TSTACK] ; os_tsk.new->tsk_stack
doubster 0:67dbd54e60d4 254 ADDS R0,R0,#16 ; Adjust Start Address
doubster 0:67dbd54e60d4 255 LDMIA R0!,{R4-R7} ; Restore new Context (R8-R11)
doubster 0:67dbd54e60d4 256 MOV R8,R4
doubster 0:67dbd54e60d4 257 MOV R9,R5
doubster 0:67dbd54e60d4 258 MOV R10,R6
doubster 0:67dbd54e60d4 259 MOV R11,R7
doubster 0:67dbd54e60d4 260 MSR PSP,R0 ; Write PSP
doubster 0:67dbd54e60d4 261 SUBS R0,R0,#32 ; Adjust Start Address
doubster 0:67dbd54e60d4 262 LDMIA R0!,{R4-R7} ; Restore new Context (R4-R7)
doubster 0:67dbd54e60d4 263
doubster 0:67dbd54e60d4 264 Sys_Exit
doubster 0:67dbd54e60d4 265 MOVS R0,#:NOT:0xFFFFFFFD ; Set EXC_RETURN value
doubster 0:67dbd54e60d4 266 MVNS R0,R0
doubster 0:67dbd54e60d4 267 BX R0 ; RETI to Thread Mode, use PSP
doubster 0:67dbd54e60d4 268
doubster 0:67dbd54e60d4 269 ALIGN
doubster 0:67dbd54e60d4 270 }
doubster 0:67dbd54e60d4 271
doubster 0:67dbd54e60d4 272
doubster 0:67dbd54e60d4 273 /*-------------------------- SysTick_Handler --------------------------------*/
doubster 0:67dbd54e60d4 274
doubster 0:67dbd54e60d4 275 __asm void SysTick_Handler (void) {
doubster 0:67dbd54e60d4 276 PRESERVE8
doubster 0:67dbd54e60d4 277
doubster 0:67dbd54e60d4 278 BL __cpp(rt_systick)
doubster 0:67dbd54e60d4 279 B Sys_Switch
doubster 0:67dbd54e60d4 280
doubster 0:67dbd54e60d4 281 ALIGN
doubster 0:67dbd54e60d4 282 }
doubster 0:67dbd54e60d4 283
doubster 0:67dbd54e60d4 284
doubster 0:67dbd54e60d4 285 /*-------------------------- OS_Tick_Handler --------------------------------*/
doubster 0:67dbd54e60d4 286
doubster 0:67dbd54e60d4 287 __asm void OS_Tick_Handler (void) {
doubster 0:67dbd54e60d4 288 PRESERVE8
doubster 0:67dbd54e60d4 289
doubster 0:67dbd54e60d4 290 BL __cpp(os_tick_irqack)
doubster 0:67dbd54e60d4 291 BL __cpp(rt_systick)
doubster 0:67dbd54e60d4 292 B Sys_Switch
doubster 0:67dbd54e60d4 293
doubster 0:67dbd54e60d4 294 ALIGN
doubster 0:67dbd54e60d4 295 }
doubster 0:67dbd54e60d4 296
doubster 0:67dbd54e60d4 297
doubster 0:67dbd54e60d4 298 /*----------------------------------------------------------------------------
doubster 0:67dbd54e60d4 299 * end of file
doubster 0:67dbd54e60d4 300 *---------------------------------------------------------------------------*/
doubster 0:67dbd54e60d4 301
doubster 0:67dbd54e60d4 302