1 /*----------------------------------------------------------------------------
3 *----------------------------------------------------------------------------
5 * Purpose: Hardware Abstraction Layer for Cortex-M3
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 MOVNE R0,#0x02 /* Privileged Thread mode, use PSP */
106 MOVEQ R0,#0x03 /* Unprivileged Thread mode, use PSP */
111 .size os_set_env, .-os_set_env
114 /*--------------------------- _alloc_box ------------------------------------*/
116 # void *_alloc_box (void *box_mem);
117 /* Function wrapper for Unprivileged/Privileged mode. */
120 .type _alloc_box, %function
126 LDR R12,=rt_alloc_box
139 .size _alloc_box, .-_alloc_box
142 /*--------------------------- _free_box -------------------------------------*/
144 # int _free_box (void *box_mem, void *box);
145 /* Function wrapper for Unprivileged/Privileged mode. */
148 .type _free_box, %function
167 .size _free_box, .-_free_box
170 /*-------------------------- SVC_Handler ------------------------------------*/
172 # void SVC_Handler (void);
175 .type SVC_Handler, %function
181 MRS R0,PSP /* Read PSP */
182 LDR R1,[R0,#24] /* Read Saved PC from Stack */
183 LDRB R1,[R1,#-2] /* Load SVC Number */
186 LDM R0,{R0-R3,R12} /* Read R0-R3,R12 from stack */
187 BLX R12 /* Call SVC Function */
189 MRS R12,PSP /* Read PSP */
190 STM R12,{R0-R2} /* Store return values */
193 LDM R3,{R1,R2} /* os_tsk.run, os_tsk.new */
195 BEQ SVC_Exit /* no task switch */
197 CBZ R1,SVC_Next /* Runtask deleted? */
198 STMDB R12!,{R4-R11} /* Save Old context */
199 STR R12,[R1,#TCB_TSTACK] /* Update os_tsk.run->tsk_stack */
202 BL rt_stk_check /* Check for Stack overflow */
206 STR R2,[R3] /* os_tsk.run = os_tsk.new */
208 LDR R12,[R2,#TCB_TSTACK] /* os_tsk.new->tsk_stack */
209 LDMIA R12!,{R4-R11} /* Restore New Context */
210 MSR PSP,R12 /* Write PSP */
213 MVN LR,#~0xFFFFFFFD /* set EXC_RETURN value */
216 /*------------------- User SVC ------------------------------*/
219 PUSH {R4,LR} /* Save Registers */
223 BHI SVC_Done /* Overflow */
226 LDR R4,[R4,R1,LSL #2] /* Load SVC Function Address */
228 LDM R0,{R0-R3,R12} /* Read R0-R3,R12 from stack */
229 BLX R4 /* Call SVC Function */
232 STM R12,{R0-R3} /* Function return values */
234 POP {R4,PC} /* RETI */
237 .size SVC_Handler, .-SVC_Handler
240 /*-------------------------- PendSV_Handler ---------------------------------*/
242 # void PendSV_Handler (void);
245 .type PendSV_Handler, %function
246 .global PendSV_Handler
256 LDM R3,{R1,R2} /* os_tsk.run, os_tsk.new */
260 MRS R12,PSP /* Read PSP */
261 STMDB R12!,{R4-R11} /* Save Old context */
262 STR R12,[R1,#TCB_TSTACK] /* Update os_tsk.run->tsk_stack */
265 BL rt_stk_check /* Check for Stack overflow */
268 STR R2,[R3] /* os_tsk.run = os_tsk.new */
270 LDR R12,[R2,#TCB_TSTACK] /* os_tsk.new->tsk_stack */
271 LDMIA R12!,{R4-R11} /* Restore New Context */
272 MSR PSP,R12 /* Write PSP */
275 MVN LR,#~0xFFFFFFFD /* set EXC_RETURN value */
276 BX LR /* Return to Thread Mode */
279 .size PendSV_Handler, .-PendSV_Handler
282 /*-------------------------- SysTick_Handler --------------------------------*/
284 # void SysTick_Handler (void);
287 .type SysTick_Handler, %function
288 .global SysTick_Handler
297 .size SysTick_Handler, .-SysTick_Handler
300 /*-------------------------- OS_Tick_Handler --------------------------------*/
302 # void OS_Tick_Handler (void);
305 .type OS_Tick_Handler, %function
306 .global OS_Tick_Handler
316 .size OS_Tick_Handler, .-OS_Tick_Handler
321 /*----------------------------------------------------------------------------
323 *---------------------------------------------------------------------------*/