]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/RVDS/ARM_CM0/port.c
646dd8e61c501b1e9b461baa8a6ef9b72db444b5
[freertos] / FreeRTOS / Source / portable / RVDS / ARM_CM0 / port.c
1 /*\r
2  * FreeRTOS Kernel V10.2.1\r
3  * Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /*-----------------------------------------------------------\r
29  * Implementation of functions defined in portable.h for the ARM CM0 port.\r
30  *----------------------------------------------------------*/\r
31 \r
32 /* Scheduler includes. */\r
33 #include "FreeRTOS.h"\r
34 #include "task.h"\r
35 \r
36 /* Constants required to manipulate the NVIC. */\r
37 #define portNVIC_SYSTICK_CTRL                   ( ( volatile uint32_t * ) 0xe000e010 )\r
38 #define portNVIC_SYSTICK_LOAD                   ( ( volatile uint32_t * ) 0xe000e014 )\r
39 #define portNVIC_SYSTICK_CURRENT_VALUE  ( ( volatile uint32_t * ) 0xe000e018 )\r
40 #define portNVIC_INT_CTRL                       ( ( volatile uint32_t *) 0xe000ed04 )\r
41 #define portNVIC_SYSPRI2                        ( ( volatile uint32_t *) 0xe000ed20 )\r
42 #define portNVIC_SYSTICK_CLK            0x00000004\r
43 #define portNVIC_SYSTICK_INT            0x00000002\r
44 #define portNVIC_SYSTICK_ENABLE         0x00000001\r
45 #define portNVIC_PENDSVSET                      0x10000000\r
46 #define portMIN_INTERRUPT_PRIORITY      ( 255UL )\r
47 #define portNVIC_PENDSV_PRI                     ( portMIN_INTERRUPT_PRIORITY << 16UL )\r
48 #define portNVIC_SYSTICK_PRI            ( portMIN_INTERRUPT_PRIORITY << 24UL )\r
49 \r
50 /* Constants required to set up the initial stack. */\r
51 #define portINITIAL_XPSR                        ( 0x01000000 )\r
52 \r
53 /* Constants used with memory barrier intrinsics. */\r
54 #define portSY_FULL_READ_WRITE          ( 15 )\r
55 \r
56 /* Each task maintains its own interrupt status in the critical nesting\r
57 variable. */\r
58 static UBaseType_t uxCriticalNesting = 0xaaaaaaaa;\r
59 \r
60 /*\r
61  * Setup the timer to generate the tick interrupts.\r
62  */\r
63 static void prvSetupTimerInterrupt( void );\r
64 \r
65 /*\r
66  * Exception handlers.\r
67  */\r
68 void xPortPendSVHandler( void );\r
69 void xPortSysTickHandler( void );\r
70 void vPortSVCHandler( void );\r
71 \r
72 /*\r
73  * Start first task is a separate function so it can be tested in isolation.\r
74  */\r
75 static void prvPortStartFirstTask( void );\r
76 \r
77 /*\r
78  * Used to catch tasks that attempt to return from their implementing function.\r
79  */\r
80 static void prvTaskExitError( void );\r
81 \r
82 /*-----------------------------------------------------------*/\r
83 \r
84 /*\r
85  * See header file for description.\r
86  */\r
87 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
88 {\r
89         /* Simulate the stack frame as it would be created by a context switch\r
90         interrupt. */\r
91         pxTopOfStack--; /* Offset added to account for the way the MCU uses the stack on entry/exit of interrupts. */\r
92         *pxTopOfStack = portINITIAL_XPSR;       /* xPSR */\r
93         pxTopOfStack--;\r
94         *pxTopOfStack = ( StackType_t ) pxCode; /* PC */\r
95         pxTopOfStack--;\r
96         *pxTopOfStack = ( StackType_t ) prvTaskExitError;       /* LR */\r
97         pxTopOfStack -= 5;      /* R12, R3, R2 and R1. */\r
98         *pxTopOfStack = ( StackType_t ) pvParameters;   /* R0 */\r
99         pxTopOfStack -= 8; /* R11..R4. */\r
100 \r
101         return pxTopOfStack;\r
102 }\r
103 /*-----------------------------------------------------------*/\r
104 \r
105 static void prvTaskExitError( void )\r
106 {\r
107         /* A function that implements a task must not exit or attempt to return to\r
108         its caller as there is nothing to return to.  If a task wants to exit it\r
109         should instead call vTaskDelete( NULL ).\r
110 \r
111         Artificially force an assert() to be triggered if configASSERT() is\r
112         defined, then stop here so application writers can catch the error. */\r
113         configASSERT( uxCriticalNesting == ~0UL );\r
114         portDISABLE_INTERRUPTS();\r
115         for( ;; );\r
116 }\r
117 /*-----------------------------------------------------------*/\r
118 \r
119 void vPortSVCHandler( void )\r
120 {\r
121         /* This function is no longer used, but retained for backward\r
122         compatibility. */\r
123 }\r
124 /*-----------------------------------------------------------*/\r
125 \r
126 __asm void prvPortStartFirstTask( void )\r
127 {\r
128         extern pxCurrentTCB;\r
129 \r
130         PRESERVE8\r
131 \r
132         /* The MSP stack is not reset as, unlike on M3/4 parts, there is no vector\r
133         table offset register that can be used to locate the initial stack value.\r
134         Not all M0 parts have the application vector table at address 0. */\r
135 \r
136         ldr     r3, =pxCurrentTCB       /* Obtain location of pxCurrentTCB. */\r
137         ldr r1, [r3]\r
138         ldr r0, [r1]                    /* The first item in pxCurrentTCB is the task top of stack. */\r
139         adds r0, #32                    /* Discard everything up to r0. */\r
140         msr psp, r0                             /* This is now the new top of stack to use in the task. */\r
141         movs r0, #2                             /* Switch to the psp stack. */\r
142         msr CONTROL, r0\r
143         isb\r
144         pop {r0-r5}                             /* Pop the registers that are saved automatically. */\r
145         mov lr, r5                              /* lr is now in r5. */\r
146         pop {r3}                                /* The return address is now in r3. */\r
147         pop {r2}                                /* Pop and discard the XPSR. */\r
148         cpsie i                                 /* The first task has its context and interrupts can be enabled. */\r
149         bx r3                                   /* Finally, jump to the user defined task code. */\r
150 \r
151         ALIGN\r
152 }\r
153 /*-----------------------------------------------------------*/\r
154 \r
155 /*\r
156  * See header file for description.\r
157  */\r
158 BaseType_t xPortStartScheduler( void )\r
159 {\r
160         /* Make PendSV, CallSV and SysTick the same priority as the kernel. */\r
161         *(portNVIC_SYSPRI2) |= portNVIC_PENDSV_PRI;\r
162         *(portNVIC_SYSPRI2) |= portNVIC_SYSTICK_PRI;\r
163 \r
164         /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
165         here already. */\r
166         prvSetupTimerInterrupt();\r
167 \r
168         /* Initialise the critical nesting count ready for the first task. */\r
169         uxCriticalNesting = 0;\r
170 \r
171         /* Start the first task. */\r
172         prvPortStartFirstTask();\r
173 \r
174         /* Should not get here! */\r
175         return 0;\r
176 }\r
177 /*-----------------------------------------------------------*/\r
178 \r
179 void vPortEndScheduler( void )\r
180 {\r
181         /* Not implemented in ports where there is nothing to return to.\r
182         Artificially force an assert. */\r
183         configASSERT( uxCriticalNesting == 1000UL );\r
184 }\r
185 /*-----------------------------------------------------------*/\r
186 \r
187 void vPortYield( void )\r
188 {\r
189         /* Set a PendSV to request a context switch. */\r
190         *( portNVIC_INT_CTRL ) = portNVIC_PENDSVSET;\r
191 \r
192         /* Barriers are normally not required but do ensure the code is completely\r
193         within the specified behaviour for the architecture. */\r
194         __dsb( portSY_FULL_READ_WRITE );\r
195         __isb( portSY_FULL_READ_WRITE );\r
196 }\r
197 /*-----------------------------------------------------------*/\r
198 \r
199 void vPortEnterCritical( void )\r
200 {\r
201     portDISABLE_INTERRUPTS();\r
202     uxCriticalNesting++;\r
203         __dsb( portSY_FULL_READ_WRITE );\r
204         __isb( portSY_FULL_READ_WRITE );\r
205 }\r
206 /*-----------------------------------------------------------*/\r
207 \r
208 void vPortExitCritical( void )\r
209 {\r
210         configASSERT( uxCriticalNesting );\r
211     uxCriticalNesting--;\r
212     if( uxCriticalNesting == 0 )\r
213     {\r
214         portENABLE_INTERRUPTS();\r
215     }\r
216 }\r
217 /*-----------------------------------------------------------*/\r
218 \r
219 __asm uint32_t ulSetInterruptMaskFromISR( void )\r
220 {\r
221         mrs r0, PRIMASK\r
222         cpsid i\r
223         bx lr\r
224 }\r
225 /*-----------------------------------------------------------*/\r
226 \r
227 __asm void vClearInterruptMaskFromISR( uint32_t ulMask )\r
228 {\r
229         msr PRIMASK, r0\r
230         bx lr\r
231 }\r
232 /*-----------------------------------------------------------*/\r
233 \r
234 __asm void xPortPendSVHandler( void )\r
235 {\r
236         extern vTaskSwitchContext\r
237         extern pxCurrentTCB\r
238 \r
239         PRESERVE8\r
240 \r
241         mrs r0, psp\r
242 \r
243         ldr     r3, =pxCurrentTCB       /* Get the location of the current TCB. */\r
244         ldr     r2, [r3]\r
245 \r
246         subs r0, #32                    /* Make space for the remaining low registers. */\r
247         str r0, [r2]                    /* Save the new top of stack. */\r
248         stmia r0!, {r4-r7}              /* Store the low registers that are not saved automatically. */\r
249         mov r4, r8                              /* Store the high registers. */\r
250         mov r5, r9\r
251         mov r6, r10\r
252         mov r7, r11\r
253         stmia r0!, {r4-r7}\r
254 \r
255         push {r3, r14}\r
256         cpsid i\r
257         bl vTaskSwitchContext\r
258         cpsie i\r
259         pop {r2, r3}                    /* lr goes in r3. r2 now holds tcb pointer. */\r
260 \r
261         ldr r1, [r2]\r
262         ldr r0, [r1]                    /* The first item in pxCurrentTCB is the task top of stack. */\r
263         adds r0, #16                    /* Move to the high registers. */\r
264         ldmia r0!, {r4-r7}              /* Pop the high registers. */\r
265         mov r8, r4\r
266         mov r9, r5\r
267         mov r10, r6\r
268         mov r11, r7\r
269 \r
270         msr psp, r0                             /* Remember the new top of stack for the task. */\r
271 \r
272         subs r0, #32                    /* Go back for the low registers that are not automatically restored. */\r
273         ldmia r0!, {r4-r7}      /* Pop low registers.  */\r
274 \r
275         bx r3\r
276         ALIGN\r
277 }\r
278 /*-----------------------------------------------------------*/\r
279 \r
280 void xPortSysTickHandler( void )\r
281 {\r
282 uint32_t ulPreviousMask;\r
283 \r
284         ulPreviousMask = portSET_INTERRUPT_MASK_FROM_ISR();\r
285         {\r
286                 /* Increment the RTOS tick. */\r
287                 if( xTaskIncrementTick() != pdFALSE )\r
288                 {\r
289                         /* Pend a context switch. */\r
290                         *(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;\r
291                 }\r
292         }\r
293         portCLEAR_INTERRUPT_MASK_FROM_ISR( ulPreviousMask );\r
294 }\r
295 /*-----------------------------------------------------------*/\r
296 \r
297 /*\r
298  * Setup the systick timer to generate the tick interrupts at the required\r
299  * frequency.\r
300  */\r
301 void prvSetupTimerInterrupt( void )\r
302 {\r
303         /* Stop and reset the SysTick. */\r
304         *(portNVIC_SYSTICK_CTRL) = 0UL;\r
305         *(portNVIC_SYSTICK_CURRENT_VALUE) = 0UL;\r
306 \r
307         /* Configure SysTick to interrupt at the requested rate. */\r
308         *(portNVIC_SYSTICK_LOAD) = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;\r
309         *(portNVIC_SYSTICK_CTRL) = portNVIC_SYSTICK_CLK | portNVIC_SYSTICK_INT | portNVIC_SYSTICK_ENABLE;\r
310 }\r
311 /*-----------------------------------------------------------*/\r
312 \r