1/*
2 * FreeRTOS Kernel V11.1.0
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
26 *
27 */
28
29#include <FreeRTOSConfig.h>
30
31    RSEG    CODE:CODE(2)
32    thumb
33
34    EXTERN pxCurrentTCB
35    EXTERN vTaskSwitchContext
36
37    PUBLIC xPortPendSVHandler
38    PUBLIC vPortSVCHandler
39    PUBLIC vPortStartFirstTask
40    PUBLIC vPortEnableVFP
41
42
43/*-----------------------------------------------------------*/
44
45xPortPendSVHandler:
46    mrs r0, psp
47    isb
48    /* Get the location of the current TCB. */
49    ldr r3, =pxCurrentTCB
50    ldr r2, [r3]
51
52    /* Is the task using the FPU context?  If so, push high vfp registers. */
53    tst r14, #0x10
54    it eq
55    vstmdbeq r0!, {s16-s31}
56
57    /* Save the core registers. */
58    stmdb r0!, {r4-r11, r14}
59
60    /* Save the new top of stack into the first member of the TCB. */
61    str r0, [r2]
62
63    stmdb sp!, {r0, r3}
64    mov r0, #configMAX_SYSCALL_INTERRUPT_PRIORITY
65    cpsid i
66    msr basepri, r0
67    dsb
68    isb
69    cpsie i
70    bl vTaskSwitchContext
71    mov r0, #0
72    msr basepri, r0
73    ldmia sp!, {r0, r3}
74
75    /* The first item in pxCurrentTCB is the task top of stack. */
76    ldr r1, [r3]
77    ldr r0, [r1]
78
79    /* Pop the core registers. */
80    ldmia r0!, {r4-r11, r14}
81
82    /* Is the task using the FPU context?  If so, pop the high vfp registers
83    too. */
84    tst r14, #0x10
85    it eq
86    vldmiaeq r0!, {s16-s31}
87
88    msr psp, r0
89    isb
90    #ifdef WORKAROUND_PMU_CM001 /* XMC4000 specific errata */
91        #if WORKAROUND_PMU_CM001 == 1
92            push { r14 }
93            pop { pc }
94        #endif
95    #endif
96
97    bx r14
98
99
100/*-----------------------------------------------------------*/
101
102vPortSVCHandler:
103    /* Get the location of the current TCB. */
104    ldr r3, =pxCurrentTCB
105    ldr r1, [r3]
106    ldr r0, [r1]
107    /* Pop the core registers. */
108    ldmia r0!, {r4-r11, r14}
109    msr psp, r0
110    isb
111    mov r0, #0
112    msr basepri, r0
113    bx r14
114
115/*-----------------------------------------------------------*/
116
117vPortStartFirstTask
118    /* Use the NVIC offset register to locate the stack. */
119    ldr r0, =0xE000ED08
120    ldr r0, [r0]
121    ldr r0, [r0]
122    /* Set the msp back to the start of the stack. */
123    msr msp, r0
124    /* Clear the bit that indicates the FPU is in use in case the FPU was used
125    before the scheduler was started - which would otherwise result in the
126    unnecessary leaving of space in the SVC stack for lazy saving of FPU
127    registers. */
128    mov r0, #0
129    msr control, r0
130    /* Call SVC to start the first task. */
131    cpsie i
132    cpsie f
133    dsb
134    isb
135    svc 0
136
137/*-----------------------------------------------------------*/
138
139vPortEnableVFP:
140    /* The FPU enable bits are in the CPACR. */
141    ldr.w r0, =0xE000ED88
142    ldr r1, [r0]
143
144    /* Enable CP10 and CP11 coprocessors, then save back. */
145    orr r1, r1, #( 0xf << 20 )
146    str r1, [r0]
147    bx  r14
148
149
150
151    END
152