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 *---------------------------------------------------------------------------*/
41 /*----------------------------------------------------------------------------
43 *---------------------------------------------------------------------------*/
51 /*--------------------------- rt_set_PSP ------------------------------------*/
53 # void rt_set_PSP (U32 stack);
56 .type rt_set_PSP, %function
66 .size rt_set_PSP, .-rt_set_PSP
69 /*--------------------------- rt_get_PSP ------------------------------------*/
71 # U32 rt_get_PSP (void);
74 .type rt_get_PSP, %function
84 .size rt_get_PSP, .-rt_get_PSP
87 /*--------------------------- os_set_env ------------------------------------*/
89 # void os_set_env (void);
90 /* Switch to Unprivileged/Privileged Thread mode, use PSP. */
93 .type os_set_env, %function
99 MOV R0,SP /* PSP = MSP */
105 MOVS R0,#0x03 /* Unprivileged Thread mode, use PSP */
109 MOVS R0,#0x02 /* Privileged Thread mode, use PSP */
114 .size os_set_env, .-os_set_env
117 /*--------------------------- _alloc_box ------------------------------------*/
119 # void *_alloc_box (void *box_mem);
120 /* Function wrapper for Unprivileged/Privileged mode. */
123 .type _alloc_box, %function
143 .size _alloc_box, .-_alloc_box
146 /*--------------------------- _free_box -------------------------------------*/
148 # int _free_box (void *box_mem, void *box);
149 /* Function wrapper for Unprivileged/Privileged mode. */
152 .type _free_box, %function
172 .size _free_box, .-_free_box
175 /*-------------------------- SVC_Handler ------------------------------------*/
177 # void SVC_Handler (void);
180 .type SVC_Handler, %function
186 MRS R0,PSP /* Read PSP */
187 LDR R1,[R0,#24] /* Read Saved PC from Stack */
188 SUBS R1,R1,#2 /* Point to SVC Instruction */
189 LDRB R1,[R1] /* Load SVC Number */
191 BNE SVC_User /* User SVC Number > 0 */
194 LDMIA R0,{R0-R3,R4} /* Read R0-R3,R12 from stack */
197 BLX R12 /* Call SVC Function */
199 MRS R3,PSP /* Read PSP */
200 STMIA R3!,{R0-R2} /* Store return values */
203 LDMIA R3!,{R1,R2} /* os_tsk.run, os_tsk.new */
205 BEQ SVC_Exit /* no task switch */
208 CMP R1,#0 /* Runtask deleted? */
211 MRS R0,PSP /* Read PSP */
212 SUBS R0,R0,#32 /* Adjust Start Address */
213 STR R0,[R1,#TCB_TSTACK] /* Update os_tsk.run->tsk_stack */
214 STMIA R0!,{R4-R7} /* Save old context (R4-R7) */
219 STMIA R0!,{R4-R7} /* Save old context (R8-R11) */
222 BL rt_stk_check /* Check for Stack overflow */
226 STR R2,[R3] /* os_tsk.run = os_tsk.new */
228 LDR R0,[R2,#TCB_TSTACK] /* os_tsk.new->tsk_stack */
229 ADDS R0,R0,#16 /* Adjust Start Address */
230 LDMIA R0!,{R4-R7} /* Restore new Context (R8-R11) */
235 MSR PSP,R0 /* Write PSP */
236 SUBS R0,R0,#32 /* Adjust Start Address */
237 LDMIA R0!,{R4-R7} /* Restore new Context (R4-R7) */
240 MOVS R0,#~0xFFFFFFFD /* Set EXC_RETURN value */
242 BX R0 /* RETI to Thread Mode, use PSP */
244 /*------------------- User SVC ------------------------------*/
247 PUSH {R4,LR} /* Save Registers */
251 BHI SVC_Done /* Overflow */
255 LDR R4,[R4,R1] /* Load SVC Function Address */
258 LDMIA R0,{R0-R3,R4} /* Read R0-R3,R12 from stack */
260 BLX LR /* Call SVC Function */
262 MRS R4,PSP /* Read PSP */
263 STMIA R4!,{R0-R3} /* Function return values */
265 POP {R4,PC} /* RETI */
268 .size SVC_Handler, .-SVC_Handler
271 /*-------------------------- PendSV_Handler ---------------------------------*/
273 # void PendSV_Handler (void);
276 .type PendSV_Handler, %function
277 .global PendSV_Handler
287 LDMIA R3!,{R1,R2} /* os_tsk.run, os_tsk.new */
289 BEQ Sys_Exit /* no task switch */
293 MRS R0,PSP /* Read PSP */
294 SUBS R0,R0,#32 /* Adjust Start Address */
295 STR R0,[R1,#TCB_TSTACK] /* Update os_tsk.run->tsk_stack */
296 STMIA R0!,{R4-R7} /* Save old context (R4-R7) */
301 STMIA R0!,{R4-R7} /* Save old context (R8-R11) */
304 BL rt_stk_check /* Check for Stack overflow */
307 STR R2,[R3] /* os_tsk.run = os_tsk.new */
309 LDR R0,[R2,#TCB_TSTACK] /* os_tsk.new->tsk_stack */
310 ADDS R0,R0,#16 /* Adjust Start Address */
311 LDMIA R0!,{R4-R7} /* Restore new Context (R8-R11) */
316 MSR PSP,R0 /* Write PSP */
317 SUBS R0,R0,#32 /* Adjust Start Address */
318 LDMIA R0!,{R4-R7} /* Restore new Context (R4-R7) */
321 MOVS R0,#~0xFFFFFFFD /* Set EXC_RETURN value */
323 BX R0 /* RETI to Thread Mode, use PSP */
326 .size PendSV_Handler, .-PendSV_Handler
329 /*-------------------------- SysTick_Handler --------------------------------*/
331 # void SysTick_Handler (void);
334 .type SysTick_Handler, %function
335 .global SysTick_Handler
344 .size SysTick_Handler, .-SysTick_Handler
347 /*-------------------------- OS_Tick_Handler --------------------------------*/
349 # void OS_Tick_Handler (void);
352 .type OS_Tick_Handler, %function
353 .global OS_Tick_Handler
363 .size OS_Tick_Handler, .-OS_Tick_Handler
368 /*----------------------------------------------------------------------------
370 *---------------------------------------------------------------------------*/