]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/rtos/rtx/TARGET_CORTEX_M/TARGET_M3/TOOLCHAIN_GCC/HAL_CM3.s
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / rtos / rtx / TARGET_CORTEX_M / TARGET_M3 / TOOLCHAIN_GCC / HAL_CM3.s
1 /*----------------------------------------------------------------------------
2  *      RL-ARM - RTX
3  *----------------------------------------------------------------------------
4  *      Name:    HAL_CM3.S
5  *      Purpose: Hardware Abstraction Layer for Cortex-M3
6  *      Rev.:    V4.60
7  *----------------------------------------------------------------------------
8  *
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.
21  *
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  *---------------------------------------------------------------------------*/
34
35         .file   "HAL_CM3.S"
36         .syntax unified
37
38         .equ    TCB_TSTACK, 40
39
40
41 /*----------------------------------------------------------------------------
42  *      Functions
43  *---------------------------------------------------------------------------*/
44
45         .thumb
46
47         .section ".text"
48         .align  2
49
50
51 /*--------------------------- rt_set_PSP ------------------------------------*/
52
53 #       void rt_set_PSP (U32 stack);
54
55         .thumb_func
56         .type   rt_set_PSP, %function
57         .global rt_set_PSP
58 rt_set_PSP:
59         .fnstart
60         .cantunwind
61
62         MSR     PSP,R0
63         BX      LR
64
65         .fnend
66         .size   rt_set_PSP, .-rt_set_PSP
67
68
69 /*--------------------------- rt_get_PSP ------------------------------------*/
70
71 #       U32 rt_get_PSP (void);
72
73         .thumb_func
74         .type   rt_get_PSP, %function
75         .global rt_get_PSP
76 rt_get_PSP:
77         .fnstart
78         .cantunwind
79
80         MRS     R0,PSP
81         BX      LR
82
83         .fnend
84         .size   rt_get_PSP, .-rt_get_PSP
85
86
87 /*--------------------------- os_set_env ------------------------------------*/
88
89 #       void os_set_env (void);
90         /* Switch to Unprivileged/Privileged Thread mode, use PSP. */
91
92         .thumb_func
93         .type   os_set_env, %function
94         .global os_set_env
95 os_set_env:
96         .fnstart
97         .cantunwind
98
99         MOV     R0,SP                   /* PSP = MSP */
100         MSR     PSP,R0
101         LDR     R0,=os_flags
102         LDRB    R0,[R0]
103         LSLS    R0,#31
104         ITE     NE
105         MOVNE   R0,#0x02                /* Privileged Thread mode, use PSP */
106         MOVEQ   R0,#0x03                /* Unprivileged Thread mode, use PSP */
107         MSR     CONTROL,R0
108         BX      LR
109
110         .fnend
111         .size   os_set_env, .-os_set_env
112
113
114 /*--------------------------- _alloc_box ------------------------------------*/
115
116 #      void *_alloc_box (void *box_mem);
117        /* Function wrapper for Unprivileged/Privileged mode. */
118
119         .thumb_func
120         .type   _alloc_box, %function
121         .global _alloc_box
122 _alloc_box:
123         .fnstart
124         .cantunwind
125
126         LDR     R12,=rt_alloc_box
127         MRS     R3,IPSR
128         LSLS    R3,#24
129         IT      NE
130         BXNE    R12
131         MRS     R3,CONTROL
132         LSLS    R3,#31
133         IT      EQ
134         BXEQ    R12
135         SVC     0
136         BX      LR
137
138         .fnend
139         .size   _alloc_box, .-_alloc_box
140
141
142 /*--------------------------- _free_box -------------------------------------*/
143
144 #       int _free_box (void *box_mem, void *box);
145         /* Function wrapper for Unprivileged/Privileged mode. */
146
147         .thumb_func
148         .type   _free_box, %function
149         .global _free_box
150 _free_box:
151         .fnstart
152         .cantunwind
153
154         LDR     R12,=rt_free_box
155         MRS     R3,IPSR
156         LSLS    R3,#24
157         IT      NE
158         BXNE    R12
159         MRS     R3,CONTROL
160         LSLS    R3,#31
161         IT      EQ
162         BXEQ    R12
163         SVC     0
164         BX      LR
165
166         .fnend
167         .size   _free_box, .-_free_box
168
169
170 /*-------------------------- SVC_Handler ------------------------------------*/
171
172 #       void SVC_Handler (void);
173
174         .thumb_func
175         .type   SVC_Handler, %function
176         .global SVC_Handler
177 SVC_Handler:
178         .fnstart
179         .cantunwind
180
181         MRS     R0,PSP                  /* Read PSP */
182         LDR     R1,[R0,#24]             /* Read Saved PC from Stack */
183         LDRB    R1,[R1,#-2]             /* Load SVC Number */
184         CBNZ    R1,SVC_User
185
186         LDM     R0,{R0-R3,R12}          /* Read R0-R3,R12 from stack */
187         BLX     R12                     /* Call SVC Function */
188
189         MRS     R12,PSP                 /* Read PSP */
190         STM     R12,{R0-R2}             /* Store return values */
191
192         LDR     R3,=os_tsk
193         LDM     R3,{R1,R2}              /* os_tsk.run, os_tsk.new */
194         CMP     R1,R2
195         BEQ     SVC_Exit                /* no task switch */
196
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 */
200
201         PUSH    {R2,R3}
202         BL      rt_stk_check            /* Check for Stack overflow */
203         POP     {R2,R3}
204
205 SVC_Next:
206         STR     R2,[R3]                 /* os_tsk.run = os_tsk.new */
207
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 */
211
212 SVC_Exit:
213         MVN     LR,#~0xFFFFFFFD         /* set EXC_RETURN value */
214         BX      LR
215
216         /*------------------- User SVC ------------------------------*/
217
218 SVC_User:
219         PUSH    {R4,LR}                 /* Save Registers */
220         LDR     R2,=SVC_Count
221         LDR     R2,[R2]
222         CMP     R1,R2
223         BHI     SVC_Done                /* Overflow */
224
225         LDR     R4,=SVC_Table-4
226         LDR     R4,[R4,R1,LSL #2]       /* Load SVC Function Address */
227
228         LDM     R0,{R0-R3,R12}          /* Read R0-R3,R12 from stack */
229         BLX     R4                      /* Call SVC Function */
230
231         MRS     R12,PSP
232         STM     R12,{R0-R3}             /* Function return values */
233 SVC_Done:
234         POP     {R4,PC}                 /* RETI */
235
236         .fnend
237         .size   SVC_Handler, .-SVC_Handler
238
239
240 /*-------------------------- PendSV_Handler ---------------------------------*/
241
242 #       void PendSV_Handler (void);
243
244         .thumb_func
245         .type   PendSV_Handler, %function
246         .global PendSV_Handler
247         .global Sys_Switch
248 PendSV_Handler:
249         .fnstart
250         .cantunwind
251
252         BL      rt_pop_req
253
254 Sys_Switch:
255         LDR     R3,=os_tsk
256         LDM     R3,{R1,R2}              /* os_tsk.run, os_tsk.new */
257         CMP     R1,R2
258         BEQ     Sys_Exit
259
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 */
263
264         PUSH    {R2,R3}
265         BL      rt_stk_check            /* Check for Stack overflow */
266         POP     {R2,R3}
267
268         STR     R2,[R3]                 /* os_tsk.run = os_tsk.new */
269
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 */
273
274 Sys_Exit:
275         MVN     LR,#~0xFFFFFFFD         /* set EXC_RETURN value */
276         BX      LR                      /* Return to Thread Mode */
277
278         .fnend
279         .size   PendSV_Handler, .-PendSV_Handler
280
281
282 /*-------------------------- SysTick_Handler --------------------------------*/
283
284 #       void SysTick_Handler (void);
285
286         .thumb_func
287         .type   SysTick_Handler, %function
288         .global SysTick_Handler
289 SysTick_Handler:
290         .fnstart
291         .cantunwind
292
293         BL      rt_systick
294         B       Sys_Switch
295
296         .fnend
297         .size   SysTick_Handler, .-SysTick_Handler
298
299
300 /*-------------------------- OS_Tick_Handler --------------------------------*/
301
302 #       void OS_Tick_Handler (void);
303
304         .thumb_func
305         .type   OS_Tick_Handler, %function
306         .global OS_Tick_Handler
307 OS_Tick_Handler:
308         .fnstart
309         .cantunwind
310
311         BL      os_tick_irqack
312         BL      rt_systick
313         B       Sys_Switch
314
315         .fnend
316         .size   OS_Tick_Handler, .-OS_Tick_Handler
317
318
319         .end
320
321 /*----------------------------------------------------------------------------
322  * end of file
323  *---------------------------------------------------------------------------*/