xref: /Kernel-v10.6.2/portable/CCS/ARM_CM3/portasm.asm (revision ef7b253b56c9788077f5ecd6c9deb4021923d646)
1;/*
2; * FreeRTOS Kernel V10.6.2
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    .thumb
30
31    .ref pxCurrentTCB
32    .ref vTaskSwitchContext
33    .ref ulMaxSyscallInterruptPriority
34
35    .def xPortPendSVHandler
36    .def ulPortGetIPSR
37    .def vPortSVCHandler
38    .def vPortStartFirstTask
39
40NVICOffsetConst:                    .word   0xE000ED08
41CPACRConst:                         .word   0xE000ED88
42pxCurrentTCBConst:                  .word   pxCurrentTCB
43ulMaxSyscallInterruptPriorityConst: .word ulMaxSyscallInterruptPriority
44
45; -----------------------------------------------------------
46
47    .align 4
48ulPortGetIPSR: .asmfunc
49    mrs r0, ipsr
50    bx r14
51    .endasmfunc
52 ; -----------------------------------------------------------
53
54    .align 4
55vPortSetInterruptMask: .asmfunc
56    push {r0}
57    ldr r0, ulMaxSyscallInterruptPriorityConst
58    msr basepri, r0
59    pop {r0}
60    bx r14
61    .endasmfunc
62; -----------------------------------------------------------
63
64    .align 4
65xPortPendSVHandler: .asmfunc
66    mrs r0, psp
67    isb
68
69    ;/* Get the location of the current TCB. */
70    ldr r3, pxCurrentTCBConst
71    ldr r2, [r3]
72
73    ;/* Save the core registers. */
74    stmdb r0!, {r4-r11}
75
76    ;/* Save the new top of stack into the first member of the TCB. */
77    str r0, [r2]
78
79    stmdb sp!, {r3, r14}
80    ldr r0, ulMaxSyscallInterruptPriorityConst
81    ldr r1, [r0]
82    msr basepri, r1
83    dsb
84    isb
85    bl vTaskSwitchContext
86    mov r0, #0
87    msr basepri, r0
88    ldmia sp!, {r3, r14}
89
90    ;/* The first item in pxCurrentTCB is the task top of stack. */
91    ldr r1, [r3]
92    ldr r0, [r1]
93
94    ;/* Pop the core registers. */
95    ldmia r0!, {r4-r11}
96
97    msr psp, r0
98    isb
99    bx r14
100    .endasmfunc
101
102; -----------------------------------------------------------
103
104    .align 4
105vPortSVCHandler: .asmfunc
106    ;/* Get the location of the current TCB. */
107    ldr r3, pxCurrentTCBConst
108    ldr r1, [r3]
109    ldr r0, [r1]
110    ;/* Pop the core registers. */
111    ldmia r0!, {r4-r11}
112    msr psp, r0
113    isb
114    mov r0, #0
115    msr basepri, r0
116    orr r14, #0xd
117    bx r14
118    .endasmfunc
119
120; -----------------------------------------------------------
121
122    .align 4
123vPortStartFirstTask: .asmfunc
124    ;/* Use the NVIC offset register to locate the stack. */
125    ldr r0, NVICOffsetConst
126    ldr r0, [r0]
127    ldr r0, [r0]
128    ;/* Set the msp back to the start of the stack. */
129    msr msp, r0
130    ;/* Clear the bit that indicates the FPU is in use in case the FPU was used
131    ;before the scheduler was started - which would otherwise result in the
132    ;unnecessary leaving of space in the SVC stack for lazy saving of FPU
133    ;registers. */
134    mov r0, #0
135    msr control, r0
136    ;/* Call SVC to start the first task. */
137    cpsie i
138    cpsie f
139    dsb
140    isb
141    svc #0
142    .endasmfunc
143
144; -----------------------------------------------------------
145