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_CM3.C
doubster 0:67dbd54e60d4 5 * Purpose: Hardware Abstraction Layer for Cortex-M3
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 MOVNE R0,#0x02 ; Privileged Thread mode, use PSP
doubster 0:67dbd54e60d4 74 MOVEQ R0,#0x03 ; Unprivileged Thread mode, use PSP
doubster 0:67dbd54e60d4 75 MSR CONTROL,R0
doubster 0:67dbd54e60d4 76 BX LR
doubster 0:67dbd54e60d4 77
doubster 0:67dbd54e60d4 78 ALIGN
doubster 0:67dbd54e60d4 79 }
doubster 0:67dbd54e60d4 80
doubster 0:67dbd54e60d4 81
doubster 0:67dbd54e60d4 82 /*--------------------------- _alloc_box ------------------------------------*/
doubster 0:67dbd54e60d4 83
doubster 0:67dbd54e60d4 84 __asm void *_alloc_box (void *box_mem) {
doubster 0:67dbd54e60d4 85 /* Function wrapper for Unprivileged/Privileged mode. */
doubster 0:67dbd54e60d4 86 LDR R12,=__cpp(rt_alloc_box)
doubster 0:67dbd54e60d4 87 MRS R3,IPSR
doubster 0:67dbd54e60d4 88 LSLS R3,#24
doubster 0:67dbd54e60d4 89 BXNE R12
doubster 0:67dbd54e60d4 90 MRS R3,CONTROL
doubster 0:67dbd54e60d4 91 LSLS R3,#31
doubster 0:67dbd54e60d4 92 BXEQ R12
doubster 0:67dbd54e60d4 93 SVC 0
doubster 0:67dbd54e60d4 94 BX LR
doubster 0:67dbd54e60d4 95
doubster 0:67dbd54e60d4 96 ALIGN
doubster 0:67dbd54e60d4 97 }
doubster 0:67dbd54e60d4 98
doubster 0:67dbd54e60d4 99
doubster 0:67dbd54e60d4 100 /*--------------------------- _free_box -------------------------------------*/
doubster 0:67dbd54e60d4 101
doubster 0:67dbd54e60d4 102 __asm int _free_box (void *box_mem, void *box) {
doubster 0:67dbd54e60d4 103 /* Function wrapper for Unprivileged/Privileged mode. */
doubster 0:67dbd54e60d4 104 LDR R12,=__cpp(rt_free_box)
doubster 0:67dbd54e60d4 105 MRS R3,IPSR
doubster 0:67dbd54e60d4 106 LSLS R3,#24
doubster 0:67dbd54e60d4 107 BXNE R12
doubster 0:67dbd54e60d4 108 MRS R3,CONTROL
doubster 0:67dbd54e60d4 109 LSLS R3,#31
doubster 0:67dbd54e60d4 110 BXEQ R12
doubster 0:67dbd54e60d4 111 SVC 0
doubster 0:67dbd54e60d4 112 BX LR
doubster 0:67dbd54e60d4 113
doubster 0:67dbd54e60d4 114 ALIGN
doubster 0:67dbd54e60d4 115 }
doubster 0:67dbd54e60d4 116
doubster 0:67dbd54e60d4 117
doubster 0:67dbd54e60d4 118 /*-------------------------- SVC_Handler ------------------------------------*/
doubster 0:67dbd54e60d4 119
doubster 0:67dbd54e60d4 120 __asm void SVC_Handler (void) {
doubster 0:67dbd54e60d4 121 PRESERVE8
doubster 0:67dbd54e60d4 122
doubster 0:67dbd54e60d4 123 IMPORT SVC_Count
doubster 0:67dbd54e60d4 124 IMPORT SVC_Table
doubster 0:67dbd54e60d4 125 IMPORT rt_stk_check
doubster 0:67dbd54e60d4 126
doubster 0:67dbd54e60d4 127 MRS R0,PSP ; Read PSP
doubster 0:67dbd54e60d4 128 LDR R1,[R0,#24] ; Read Saved PC from Stack
doubster 0:67dbd54e60d4 129 LDRB R1,[R1,#-2] ; Load SVC Number
doubster 0:67dbd54e60d4 130 CBNZ R1,SVC_User
doubster 0:67dbd54e60d4 131
doubster 0:67dbd54e60d4 132 LDM R0,{R0-R3,R12} ; Read R0-R3,R12 from stack
doubster 0:67dbd54e60d4 133 BLX R12 ; Call SVC Function
doubster 0:67dbd54e60d4 134
doubster 0:67dbd54e60d4 135 MRS R12,PSP ; Read PSP
doubster 0:67dbd54e60d4 136 STM R12,{R0-R2} ; Store return values
doubster 0:67dbd54e60d4 137
doubster 0:67dbd54e60d4 138 LDR R3,=__cpp(&os_tsk)
doubster 0:67dbd54e60d4 139 LDM R3,{R1,R2} ; os_tsk.run, os_tsk.new
doubster 0:67dbd54e60d4 140 CMP R1,R2
doubster 0:67dbd54e60d4 141 BEQ SVC_Exit ; no task switch
doubster 0:67dbd54e60d4 142
doubster 0:67dbd54e60d4 143 CBZ R1,SVC_Next ; Runtask deleted?
doubster 0:67dbd54e60d4 144 STMDB R12!,{R4-R11} ; Save Old context
doubster 0:67dbd54e60d4 145 STR R12,[R1,#TCB_TSTACK] ; Update os_tsk.run->tsk_stack
doubster 0:67dbd54e60d4 146
doubster 0:67dbd54e60d4 147 PUSH {R2,R3}
doubster 0:67dbd54e60d4 148 BL rt_stk_check ; Check for Stack overflow
doubster 0:67dbd54e60d4 149 POP {R2,R3}
doubster 0:67dbd54e60d4 150
doubster 0:67dbd54e60d4 151 SVC_Next
doubster 0:67dbd54e60d4 152 STR R2,[R3] ; os_tsk.run = os_tsk.new
doubster 0:67dbd54e60d4 153
doubster 0:67dbd54e60d4 154 LDR R12,[R2,#TCB_TSTACK] ; os_tsk.new->tsk_stack
doubster 0:67dbd54e60d4 155 LDMIA R12!,{R4-R11} ; Restore New Context
doubster 0:67dbd54e60d4 156 MSR PSP,R12 ; Write PSP
doubster 0:67dbd54e60d4 157
doubster 0:67dbd54e60d4 158 SVC_Exit
doubster 0:67dbd54e60d4 159 MVN LR,#:NOT:0xFFFFFFFD ; set EXC_RETURN value
doubster 0:67dbd54e60d4 160 BX LR
doubster 0:67dbd54e60d4 161
doubster 0:67dbd54e60d4 162 /*------------------- User SVC ------------------------------*/
doubster 0:67dbd54e60d4 163
doubster 0:67dbd54e60d4 164 SVC_User
doubster 0:67dbd54e60d4 165 PUSH {R4,LR} ; Save Registers
doubster 0:67dbd54e60d4 166 LDR R2,=SVC_Count
doubster 0:67dbd54e60d4 167 LDR R2,[R2]
doubster 0:67dbd54e60d4 168 CMP R1,R2
doubster 0:67dbd54e60d4 169 BHI SVC_Done ; Overflow
doubster 0:67dbd54e60d4 170
doubster 0:67dbd54e60d4 171 LDR R4,=SVC_Table-4
doubster 0:67dbd54e60d4 172 LDR R4,[R4,R1,LSL #2] ; Load SVC Function Address
doubster 0:67dbd54e60d4 173
doubster 0:67dbd54e60d4 174 LDM R0,{R0-R3,R12} ; Read R0-R3,R12 from stack
doubster 0:67dbd54e60d4 175 BLX R4 ; Call SVC Function
doubster 0:67dbd54e60d4 176
doubster 0:67dbd54e60d4 177 MRS R12,PSP
doubster 0:67dbd54e60d4 178 STM R12,{R0-R3} ; Function return values
doubster 0:67dbd54e60d4 179 SVC_Done
doubster 0:67dbd54e60d4 180 POP {R4,PC} ; RETI
doubster 0:67dbd54e60d4 181
doubster 0:67dbd54e60d4 182 ALIGN
doubster 0:67dbd54e60d4 183 }
doubster 0:67dbd54e60d4 184
doubster 0:67dbd54e60d4 185
doubster 0:67dbd54e60d4 186 /*-------------------------- PendSV_Handler ---------------------------------*/
doubster 0:67dbd54e60d4 187
doubster 0:67dbd54e60d4 188 __asm void PendSV_Handler (void) {
doubster 0:67dbd54e60d4 189 PRESERVE8
doubster 0:67dbd54e60d4 190
doubster 0:67dbd54e60d4 191 BL __cpp(rt_pop_req)
doubster 0:67dbd54e60d4 192
doubster 0:67dbd54e60d4 193 Sys_Switch
doubster 0:67dbd54e60d4 194 LDR R3,=__cpp(&os_tsk)
doubster 0:67dbd54e60d4 195 LDM R3,{R1,R2} ; os_tsk.run, os_tsk.new
doubster 0:67dbd54e60d4 196 CMP R1,R2
doubster 0:67dbd54e60d4 197 BEQ Sys_Exit
doubster 0:67dbd54e60d4 198
doubster 0:67dbd54e60d4 199 MRS R12,PSP ; Read PSP
doubster 0:67dbd54e60d4 200 STMDB R12!,{R4-R11} ; Save Old context
doubster 0:67dbd54e60d4 201 STR R12,[R1,#TCB_TSTACK] ; Update os_tsk.run->tsk_stack
doubster 0:67dbd54e60d4 202
doubster 0:67dbd54e60d4 203 PUSH {R2,R3}
doubster 0:67dbd54e60d4 204 BL rt_stk_check ; Check for Stack overflow
doubster 0:67dbd54e60d4 205 POP {R2,R3}
doubster 0:67dbd54e60d4 206
doubster 0:67dbd54e60d4 207 STR R2,[R3] ; os_tsk.run = os_tsk.new
doubster 0:67dbd54e60d4 208
doubster 0:67dbd54e60d4 209 LDR R12,[R2,#TCB_TSTACK] ; os_tsk.new->tsk_stack
doubster 0:67dbd54e60d4 210 LDMIA R12!,{R4-R11} ; Restore New Context
doubster 0:67dbd54e60d4 211 MSR PSP,R12 ; Write PSP
doubster 0:67dbd54e60d4 212
doubster 0:67dbd54e60d4 213 Sys_Exit
doubster 0:67dbd54e60d4 214 MVN LR,#:NOT:0xFFFFFFFD ; set EXC_RETURN value
doubster 0:67dbd54e60d4 215 BX LR ; Return to Thread Mode
doubster 0:67dbd54e60d4 216
doubster 0:67dbd54e60d4 217 ALIGN
doubster 0:67dbd54e60d4 218 }
doubster 0:67dbd54e60d4 219
doubster 0:67dbd54e60d4 220
doubster 0:67dbd54e60d4 221 /*-------------------------- SysTick_Handler --------------------------------*/
doubster 0:67dbd54e60d4 222
doubster 0:67dbd54e60d4 223 __asm void SysTick_Handler (void) {
doubster 0:67dbd54e60d4 224 PRESERVE8
doubster 0:67dbd54e60d4 225
doubster 0:67dbd54e60d4 226 BL __cpp(rt_systick)
doubster 0:67dbd54e60d4 227 B Sys_Switch
doubster 0:67dbd54e60d4 228
doubster 0:67dbd54e60d4 229 ALIGN
doubster 0:67dbd54e60d4 230 }
doubster 0:67dbd54e60d4 231
doubster 0:67dbd54e60d4 232
doubster 0:67dbd54e60d4 233 /*-------------------------- OS_Tick_Handler --------------------------------*/
doubster 0:67dbd54e60d4 234
doubster 0:67dbd54e60d4 235 __asm void OS_Tick_Handler (void) {
doubster 0:67dbd54e60d4 236 PRESERVE8
doubster 0:67dbd54e60d4 237
doubster 0:67dbd54e60d4 238 BL __cpp(os_tick_irqack)
doubster 0:67dbd54e60d4 239 BL __cpp(rt_systick)
doubster 0:67dbd54e60d4 240 B Sys_Switch
doubster 0:67dbd54e60d4 241
doubster 0:67dbd54e60d4 242 ALIGN
doubster 0:67dbd54e60d4 243 }
doubster 0:67dbd54e60d4 244
doubster 0:67dbd54e60d4 245
doubster 0:67dbd54e60d4 246 /*----------------------------------------------------------------------------
doubster 0:67dbd54e60d4 247 * end of file
doubster 0:67dbd54e60d4 248 *---------------------------------------------------------------------------*/
doubster 0:67dbd54e60d4 249
doubster 0:67dbd54e60d4 250