]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/rtos/rtx/TARGET_CORTEX_M/TARGET_M4/TOOLCHAIN_ARM/HAL_CM4.c
Merge commit '1fe4406f374291ab2e86e95a97341fd9c475fcb8'
[qmk_firmware.git] / tmk_core / tool / mbed / mbed-sdk / libraries / rtos / rtx / TARGET_CORTEX_M / TARGET_M4 / TOOLCHAIN_ARM / HAL_CM4.c
1 /*----------------------------------------------------------------------------
2  *      RL-ARM - RTX
3  *----------------------------------------------------------------------------
4  *      Name:    HAL_CM4.C
5  *      Purpose: Hardware Abstraction Layer for Cortex-M4
6  *      Rev.:    V4.70
7  *----------------------------------------------------------------------------
8  *
9  * Copyright (c) 1999-2009 KEIL, 2009-2013 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 #include "rt_TypeDef.h"
36 #include "RTX_Conf.h"
37 #include "rt_System.h"
38 #include "rt_HAL_CM.h"
39 #include "rt_Task.h"
40 #include "rt_MemBox.h"
41
42
43 /*----------------------------------------------------------------------------
44  *      Functions
45  *---------------------------------------------------------------------------*/
46
47
48 /*--------------------------- rt_set_PSP ------------------------------------*/
49
50 __asm void rt_set_PSP (U32 stack) {
51         MSR     PSP,R0
52         BX      LR
53 }
54
55
56 /*--------------------------- rt_get_PSP ------------------------------------*/
57
58 __asm U32 rt_get_PSP (void) {
59         MRS     R0,PSP
60         BX      LR
61 }
62
63
64 /*--------------------------- os_set_env ------------------------------------*/
65
66 __asm void os_set_env (void) {
67    /* Switch to Unprivileged/Privileged Thread mode, use PSP. */
68         MOV     R0,SP                   ; PSP = MSP
69         MSR     PSP,R0
70         LDR     R0,=__cpp(&os_flags)
71         LDRB    R0,[R0]
72         LSLS    R0,#31
73         MOVNE   R0,#0x02                ; Privileged Thread mode, use PSP
74         MOVEQ   R0,#0x03                ; Unprivileged Thread mode, use PSP
75         MSR     CONTROL,R0
76         BX      LR
77
78         ALIGN
79 }
80
81
82 /*--------------------------- _alloc_box ------------------------------------*/
83
84 __asm void *_alloc_box (void *box_mem) {
85    /* Function wrapper for Unprivileged/Privileged mode. */
86         LDR     R12,=__cpp(rt_alloc_box)
87         MRS     R3,IPSR
88         LSLS    R3,#24
89         BXNE    R12
90         MRS     R3,CONTROL
91         LSLS    R3,#31
92         BXEQ    R12
93         SVC     0
94         BX      LR
95
96         ALIGN
97 }
98
99
100 /*--------------------------- _free_box -------------------------------------*/
101
102 __asm int _free_box (void *box_mem, void *box) {
103    /* Function wrapper for Unprivileged/Privileged mode. */
104         LDR     R12,=__cpp(rt_free_box)
105         MRS     R3,IPSR
106         LSLS    R3,#24
107         BXNE    R12
108         MRS     R3,CONTROL
109         LSLS    R3,#31
110         BXEQ    R12
111         SVC     0
112         BX      LR
113
114         ALIGN
115 }
116
117
118 /*-------------------------- SVC_Handler ------------------------------------*/
119
120 __asm void SVC_Handler (void) {
121         PRESERVE8
122
123         IMPORT  SVC_Count
124         IMPORT  SVC_Table
125         IMPORT  rt_stk_check
126
127 #ifdef  IFX_XMC4XXX
128         EXPORT  SVC_Handler_Veneer
129 SVC_Handler_Veneer
130 #endif
131
132         MRS     R0,PSP                  ; Read PSP
133         LDR     R1,[R0,#24]             ; Read Saved PC from Stack
134         LDRB    R1,[R1,#-2]             ; Load SVC Number
135         CBNZ    R1,SVC_User
136
137         LDM     R0,{R0-R3,R12}          ; Read R0-R3,R12 from stack
138         PUSH    {R4,LR}                 ; Save EXC_RETURN
139         BLX     R12                     ; Call SVC Function
140         POP     {R4,LR}                 ; Restore EXC_RETURN
141
142         MRS     R12,PSP                 ; Read PSP
143         STM     R12,{R0-R2}             ; Store return values
144
145         LDR     R3,=__cpp(&os_tsk)
146         LDM     R3,{R1,R2}              ; os_tsk.run, os_tsk.new
147         CMP     R1,R2
148 #ifdef  IFX_XMC4XXX
149         PUSHEQ  {LR}
150         POPEQ   {PC}
151 #else
152         BXEQ    LR                      ; RETI, no task switch
153 #endif
154
155         CBZ     R1,SVC_Next             ; Runtask deleted?
156         TST     LR,#0x10                ; is it extended frame?
157         VSTMDBEQ R12!,{S16-S31}         ; yes, stack also VFP hi-regs
158         MOVEQ   R0,#0x01                ; os_tsk->stack_frame val
159         MOVNE   R0,#0x00
160         STRB    R0,[R1,#TCB_STACKF]     ; os_tsk.run->stack_frame = val
161         STMDB   R12!,{R4-R11}           ; Save Old context
162         STR     R12,[R1,#TCB_TSTACK]    ; Update os_tsk.run->tsk_stack
163
164         PUSH    {R2,R3}
165         BL      rt_stk_check            ; Check for Stack overflow
166         POP     {R2,R3}
167
168 SVC_Next
169         STR     R2,[R3]                 ; os_tsk.run = os_tsk.new
170
171         LDR     R12,[R2,#TCB_TSTACK]    ; os_tsk.new->tsk_stack
172         LDMIA   R12!,{R4-R11}           ; Restore New Context
173         LDRB    R0,[R2,#TCB_STACKF]     ; Stack Frame
174         CMP     R0,#0                   ; Basic/Extended Stack Frame
175         VLDMIANE R12!,{S16-S31}         ; restore VFP hi-registers
176         MVNNE   LR,#:NOT:0xFFFFFFED     ; set EXC_RETURN value
177         MVNEQ   LR,#:NOT:0xFFFFFFFD
178         MSR     PSP,R12                 ; Write PSP
179
180 SVC_Exit
181 #ifdef  IFX_XMC4XXX
182         PUSH    {LR}
183         POP     {PC}
184 #else
185         BX      LR
186 #endif
187
188         /*------------------- User SVC ------------------------------*/
189
190 SVC_User
191         PUSH    {R4,LR}                 ; Save Registers
192         LDR     R2,=SVC_Count
193         LDR     R2,[R2]
194         CMP     R1,R2
195         BHI     SVC_Done                ; Overflow
196
197         LDR     R4,=SVC_Table-4
198         LDR     R4,[R4,R1,LSL #2]       ; Load SVC Function Address
199
200         LDM     R0,{R0-R3,R12}          ; Read R0-R3,R12 from stack
201         BLX     R4                      ; Call SVC Function
202
203         MRS     R12,PSP
204         STM     R12,{R0-R3}             ; Function return values
205 SVC_Done
206         POP     {R4,PC}                 ; RETI
207
208         ALIGN
209 }
210
211
212 /*-------------------------- PendSV_Handler ---------------------------------*/
213
214 __asm void PendSV_Handler (void) {
215         PRESERVE8
216
217 #ifdef  IFX_XMC4XXX
218         EXPORT  PendSV_Handler_Veneer
219 PendSV_Handler_Veneer
220 #endif
221
222         PUSH    {R4,LR}                 ; Save EXC_RETURN
223         BL      __cpp(rt_pop_req)
224
225 Sys_Switch
226         POP     {R4,LR}                 ; Restore EXC_RETURN
227
228         LDR     R3,=__cpp(&os_tsk)
229         LDM     R3,{R1,R2}              ; os_tsk.run, os_tsk.new
230         CMP     R1,R2
231 #ifdef  IFX_XMC4XXX
232         PUSHEQ  {LR}
233         POPEQ   {PC}
234 #else
235         BXEQ    LR                      ; RETI, no task switch
236 #endif
237
238         MRS     R12,PSP                 ; Read PSP
239         TST     LR,#0x10                ; is it extended frame?
240         VSTMDBEQ R12!,{S16-S31}         ; yes, stack also VFP hi-regs
241         MOVEQ   R0,#0x01                ; os_tsk->stack_frame val
242         MOVNE   R0,#0x00
243         STRB    R0,[R1,#TCB_STACKF]     ; os_tsk.run->stack_frame = val
244         STMDB   R12!,{R4-R11}           ; Save Old context
245         STR     R12,[R1,#TCB_TSTACK]    ; Update os_tsk.run->tsk_stack
246
247         PUSH    {R2,R3}
248         BL      rt_stk_check            ; Check for Stack overflow
249         POP     {R2,R3}
250
251         STR     R2,[R3]                 ; os_tsk.run = os_tsk.new
252
253         LDR     R12,[R2,#TCB_TSTACK]    ; os_tsk.new->tsk_stack
254         LDMIA   R12!,{R4-R11}           ; Restore New Context
255         LDRB    R0,[R2,#TCB_STACKF]     ; Stack Frame
256         CMP     R0,#0                   ; Basic/Extended Stack Frame
257         VLDMIANE R12!,{S16-S31}         ; restore VFP hi-regs
258         MVNNE   LR,#:NOT:0xFFFFFFED     ; set EXC_RETURN value
259         MVNEQ   LR,#:NOT:0xFFFFFFFD
260         MSR     PSP,R12                 ; Write PSP
261
262 Sys_Exit
263 #ifdef  IFX_XMC4XXX
264         PUSH    {LR}
265         POP     {PC}
266 #else
267         BX      LR                      ; Return to Thread Mode
268 #endif
269
270         ALIGN
271 }
272
273
274 /*-------------------------- SysTick_Handler --------------------------------*/
275
276 __asm void SysTick_Handler (void) {
277         PRESERVE8
278
279 #ifdef  IFX_XMC4XXX
280         EXPORT  SysTick_Handler_Veneer
281 SysTick_Handler_Veneer
282 #endif
283
284         PUSH    {R4,LR}                 ; Save EXC_RETURN
285         BL      __cpp(rt_systick)
286         B       Sys_Switch
287
288         ALIGN
289 }
290
291
292 /*-------------------------- OS_Tick_Handler --------------------------------*/
293
294 __asm void OS_Tick_Handler (void) {
295         PRESERVE8
296
297         PUSH    {R4,LR}                 ; Save EXC_RETURN
298         BL      __cpp(os_tick_irqack)
299         BL      __cpp(rt_systick)
300         B       Sys_Switch
301
302         ALIGN
303 }
304
305
306 /*----------------------------------------------------------------------------
307  * end of file
308  *---------------------------------------------------------------------------*/
309