1 /*----------------------------------------------------------------------------
3 *----------------------------------------------------------------------------
5 * Purpose: Hardware Abstraction Layer for Cortex-M0
7 *----------------------------------------------------------------------------
9 * Copyright (c) 1999-2009 KEIL, 2009-2012 ARM Germany GmbH
10 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are met:
13 * - Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * - Neither the name of ARM nor the names of its contributors may be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *---------------------------------------------------------------------------*/
35 #include "rt_TypeDef.h"
37 #include "rt_System.h"
38 #include "rt_HAL_CM.h"
40 #include "rt_MemBox.h"
43 /*----------------------------------------------------------------------------
45 *---------------------------------------------------------------------------*/
48 /*--------------------------- rt_set_PSP ------------------------------------*/
50 __asm void rt_set_PSP (U32 stack) {
56 /*--------------------------- rt_get_PSP ------------------------------------*/
58 __asm U32 rt_get_PSP (void) {
64 /*--------------------------- os_set_env ------------------------------------*/
66 __asm void os_set_env (void) {
67 /* Switch to Unprivileged/Privileged Thread mode, use PSP. */
70 LDR R0,=__cpp(&os_flags)
74 MOVS R0,#0x03 ; Unprivileged Thread mode, use PSP
78 MOVS R0,#0x02 ; Privileged Thread mode, use PSP
86 /*--------------------------- _alloc_box ------------------------------------*/
88 __asm void *_alloc_box (void *box_mem) {
89 /* Function wrapper for Unprivileged/Privileged mode. */
90 LDR R3,=__cpp(rt_alloc_box)
107 /*--------------------------- _free_box -------------------------------------*/
109 __asm int _free_box (void *box_mem, void *box) {
110 /* Function wrapper for Unprivileged/Privileged mode. */
111 LDR R3,=__cpp(rt_free_box)
128 /*-------------------------- SVC_Handler ------------------------------------*/
130 __asm void SVC_Handler (void) {
137 MRS R0,PSP ; Read PSP
138 LDR R1,[R0,#24] ; Read Saved PC from Stack
139 SUBS R1,R1,#2 ; Point to SVC Instruction
140 LDRB R1,[R1] ; Load SVC Number
142 BNE SVC_User ; User SVC Number > 0
145 LDMIA R0,{R0-R3,R4} ; Read R0-R3,R12 from stack
148 BLX R12 ; Call SVC Function
150 MRS R3,PSP ; Read PSP
151 STMIA R3!,{R0-R2} ; Store return values
153 LDR R3,=__cpp(&os_tsk)
154 LDMIA R3!,{R1,R2} ; os_tsk.run, os_tsk.new
156 BEQ SVC_Exit ; no task switch
159 CMP R1,#0 ; Runtask deleted?
162 MRS R0,PSP ; Read PSP
163 SUBS R0,R0,#32 ; Adjust Start Address
164 STR R0,[R1,#TCB_TSTACK] ; Update os_tsk.run->tsk_stack
165 STMIA R0!,{R4-R7} ; Save old context (R4-R7)
170 STMIA R0!,{R4-R7} ; Save old context (R8-R11)
173 BL rt_stk_check ; Check for Stack overflow
177 STR R2,[R3] ; os_tsk.run = os_tsk.new
179 LDR R0,[R2,#TCB_TSTACK] ; os_tsk.new->tsk_stack
180 ADDS R0,R0,#16 ; Adjust Start Address
181 LDMIA R0!,{R4-R7} ; Restore new Context (R8-R11)
186 MSR PSP,R0 ; Write PSP
187 SUBS R0,R0,#32 ; Adjust Start Address
188 LDMIA R0!,{R4-R7} ; Restore new Context (R4-R7)
191 MOVS R0,#:NOT:0xFFFFFFFD ; Set EXC_RETURN value
193 BX R0 ; RETI to Thread Mode, use PSP
195 /*------------------- User SVC ------------------------------*/
198 PUSH {R4,LR} ; Save Registers
202 BHI SVC_Done ; Overflow
206 LDR R4,[R4,R1] ; Load SVC Function Address
209 LDMIA R0,{R0-R3,R4} ; Read R0-R3,R12 from stack
211 BLX LR ; Call SVC Function
213 MRS R4,PSP ; Read PSP
214 STMIA R4!,{R0-R3} ; Function return values
222 /*-------------------------- PendSV_Handler ---------------------------------*/
224 __asm void PendSV_Handler (void) {
230 LDR R3,=__cpp(&os_tsk)
231 LDMIA R3!,{R1,R2} ; os_tsk.run, os_tsk.new
233 BEQ Sys_Exit ; no task switch
237 MRS R0,PSP ; Read PSP
238 SUBS R0,R0,#32 ; Adjust Start Address
239 STR R0,[R1,#TCB_TSTACK] ; Update os_tsk.run->tsk_stack
240 STMIA R0!,{R4-R7} ; Save old context (R4-R7)
245 STMIA R0!,{R4-R7} ; Save old context (R8-R11)
248 BL rt_stk_check ; Check for Stack overflow
251 STR R2,[R3] ; os_tsk.run = os_tsk.new
253 LDR R0,[R2,#TCB_TSTACK] ; os_tsk.new->tsk_stack
254 ADDS R0,R0,#16 ; Adjust Start Address
255 LDMIA R0!,{R4-R7} ; Restore new Context (R8-R11)
260 MSR PSP,R0 ; Write PSP
261 SUBS R0,R0,#32 ; Adjust Start Address
262 LDMIA R0!,{R4-R7} ; Restore new Context (R4-R7)
265 MOVS R0,#:NOT:0xFFFFFFFD ; Set EXC_RETURN value
267 BX R0 ; RETI to Thread Mode, use PSP
273 /*-------------------------- SysTick_Handler --------------------------------*/
275 __asm void SysTick_Handler (void) {
285 /*-------------------------- OS_Tick_Handler --------------------------------*/
287 __asm void OS_Tick_Handler (void) {
290 BL __cpp(os_tick_irqack)
298 /*----------------------------------------------------------------------------
300 *---------------------------------------------------------------------------*/