]> begriffs open source - freertos/blob - portable/Tasking/ARM_CM4F/port.c
Add SPDX-License-Identifier: MIT to MIT licensed files.
[freertos] / portable / Tasking / ARM_CM4F / port.c
1 /*\r
2  * FreeRTOS Kernel <DEVELOPMENT BRANCH>\r
3  * Copyright (C) 2021 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * SPDX-License-Identifier: MIT
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
8  * this software and associated documentation files (the "Software"), to deal in\r
9  * the Software without restriction, including without limitation the rights to\r
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
11  * the Software, and to permit persons to whom the Software is furnished to do so,\r
12  * subject to the following conditions:\r
13  *\r
14  * The above copyright notice and this permission notice shall be included in all\r
15  * copies or substantial portions of the Software.\r
16  *\r
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
19  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
20  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
21  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
23  *\r
24  * https://www.FreeRTOS.org\r
25  * https://github.com/FreeRTOS\r
26  *\r
27  */\r
28 \r
29 /*-----------------------------------------------------------\r
30 * Implementation of functions defined in portable.h for the ARM CM4F port.\r
31 *----------------------------------------------------------*/\r
32 \r
33 /* Scheduler includes. */\r
34 #include "FreeRTOS.h"\r
35 #include "task.h"\r
36 \r
37 /* Constants required to manipulate the NVIC. */\r
38 #define portNVIC_SYSTICK_CTRL       ( ( volatile uint32_t * ) 0xe000e010 )\r
39 #define portNVIC_SYSTICK_LOAD       ( ( volatile uint32_t * ) 0xe000e014 )\r
40 #define portNVIC_SHPR3_REG          ( ( volatile uint32_t * ) 0xe000ed20 )\r
41 #define portNVIC_SYSTICK_CLK        0x00000004\r
42 #define portNVIC_SYSTICK_INT        0x00000002\r
43 #define portNVIC_SYSTICK_ENABLE     0x00000001\r
44 #define portNVIC_PENDSV_PRI         ( ( ( uint32_t ) configKERNEL_INTERRUPT_PRIORITY ) << 16 )\r
45 #define portNVIC_SYSTICK_PRI        ( ( ( uint32_t ) configKERNEL_INTERRUPT_PRIORITY ) << 24 )\r
46 \r
47 /* Masks off all bits but the VECTACTIVE bits in the ICSR register. */\r
48 #define portVECTACTIVE_MASK         ( 0xFFUL )\r
49 \r
50 /* Constants required to manipulate the VFP. */\r
51 #define portFPCCR                   ( ( volatile uint32_t * ) 0xe000ef34 ) /* Floating point context control register. */\r
52 #define portASPEN_AND_LSPEN_BITS    ( 0x3UL << 30UL )\r
53 \r
54 /* Constants required to set up the initial stack. */\r
55 #define portINITIAL_XPSR            ( 0x01000000 )\r
56 #define portINITIAL_EXC_RETURN      ( 0xfffffffd )\r
57 \r
58 /* Let the user override the pre-loading of the initial LR with the address of\r
59  * prvTaskExitError() in case it messes up unwinding of the stack in the\r
60  * debugger. */\r
61 #ifdef configTASK_RETURN_ADDRESS\r
62     #define portTASK_RETURN_ADDRESS    configTASK_RETURN_ADDRESS\r
63 #else\r
64     #define portTASK_RETURN_ADDRESS    prvTaskExitError\r
65 #endif\r
66 \r
67 /* For strict compliance with the Cortex-M spec the task start address should\r
68  * have bit-0 clear, as it is loaded into the PC on exit from an ISR. */\r
69 #define portSTART_ADDRESS_MASK    ( ( StackType_t ) 0xfffffffeUL )\r
70 \r
71 /* The priority used by the kernel is assigned to a variable to make access\r
72  * from inline assembler easier. */\r
73 const uint32_t ulKernelPriority = configKERNEL_INTERRUPT_PRIORITY;\r
74 \r
75 /* Each task maintains its own interrupt status in the critical nesting\r
76  * variable. */\r
77 static uint32_t ulCriticalNesting = 0xaaaaaaaaUL;\r
78 \r
79 /*\r
80  * Setup the timer to generate the tick interrupts.\r
81  */\r
82 static void prvSetupTimerInterrupt( void );\r
83 \r
84 /*\r
85  * Exception handlers.\r
86  */\r
87 void SysTick_Handler( void );\r
88 \r
89 /*\r
90  * Functions defined in port_asm.asm.\r
91  */\r
92 extern void vPortEnableVFP( void );\r
93 extern void vPortStartFirstTask( void );\r
94 \r
95 /*\r
96  * Used to catch tasks that attempt to return from their implementing function.\r
97  */\r
98 static void prvTaskExitError( void );\r
99 \r
100 /* This exists purely to allow the const to be used from within the\r
101  * port_asm.asm assembly file. */\r
102 const uint32_t ulMaxSyscallInterruptPriorityConst = configMAX_SYSCALL_INTERRUPT_PRIORITY;\r
103 \r
104 /*-----------------------------------------------------------*/\r
105 \r
106 /*\r
107  * See header file for description.\r
108  */\r
109 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,\r
110                                      TaskFunction_t pxCode,\r
111                                      void * pvParameters )\r
112 {\r
113     /* Simulate the stack frame as it would be created by a context switch\r
114      * interrupt. */\r
115 \r
116     /* Offset added to account for the way the MCU uses the stack on entry/exit\r
117      * of interrupts, and to ensure alignment. */\r
118     pxTopOfStack--;\r
119 \r
120     *pxTopOfStack = portINITIAL_XPSR;                                    /* xPSR */\r
121     pxTopOfStack--;\r
122     *pxTopOfStack = ( ( StackType_t ) pxCode ) & portSTART_ADDRESS_MASK; /* PC */\r
123     pxTopOfStack--;\r
124     *pxTopOfStack = ( StackType_t ) portTASK_RETURN_ADDRESS;             /* LR */\r
125 \r
126     /* Save code space by skipping register initialisation. */\r
127     pxTopOfStack -= 5;                            /* R12, R3, R2 and R1. */\r
128     *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */\r
129 \r
130     /* A save method is being used that requires each task to maintain its\r
131      * own exec return value. */\r
132     pxTopOfStack--;\r
133     *pxTopOfStack = portINITIAL_EXC_RETURN;\r
134 \r
135     pxTopOfStack -= 8; /* R11, R10, R9, R8, R7, R6, R5 and R4. */\r
136 \r
137     return pxTopOfStack;\r
138 }\r
139 /*-----------------------------------------------------------*/\r
140 \r
141 static void prvTaskExitError( void )\r
142 {\r
143     /* A function that implements a task must not exit or attempt to return to\r
144      * its caller as there is nothing to return to.  If a task wants to exit it\r
145      * should instead call vTaskDelete( NULL ).\r
146      *\r
147      * Artificially force an assert() to be triggered if configASSERT() is\r
148      * defined, then stop here so application writers can catch the error. */\r
149     configASSERT( ulCriticalNesting == ~0UL );\r
150     portDISABLE_INTERRUPTS();\r
151 \r
152     for( ; ; )\r
153     {\r
154     }\r
155 }\r
156 /*-----------------------------------------------------------*/\r
157 \r
158 /*\r
159  * See header file for description.\r
160  */\r
161 BaseType_t xPortStartScheduler( void )\r
162 {\r
163     /* configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to 0.\r
164      * See https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */\r
165     configASSERT( ( configMAX_SYSCALL_INTERRUPT_PRIORITY ) );\r
166 \r
167     /* Make PendSV and SysTick the lowest priority interrupts. */\r
168     *( portNVIC_SHPR3_REG ) |= portNVIC_PENDSV_PRI;\r
169     *( portNVIC_SHPR3_REG ) |= portNVIC_SYSTICK_PRI;\r
170 \r
171     /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
172      * here already. */\r
173     prvSetupTimerInterrupt();\r
174 \r
175     /* Initialise the critical nesting count ready for the first task. */\r
176     ulCriticalNesting = 0;\r
177 \r
178     /* Ensure the VFP is enabled - it should be anyway. */\r
179     vPortEnableVFP();\r
180 \r
181     /* Lazy save always. */\r
182     *( portFPCCR ) |= portASPEN_AND_LSPEN_BITS;\r
183 \r
184     /* Start the first task. */\r
185     vPortStartFirstTask();\r
186 \r
187     /* Should not get here! */\r
188     return 0;\r
189 }\r
190 /*-----------------------------------------------------------*/\r
191 \r
192 void vPortEndScheduler( void )\r
193 {\r
194     /* Not implemented in ports where there is nothing to return to.\r
195      * Artificially force an assert. */\r
196     configASSERT( ulCriticalNesting == 1000UL );\r
197 }\r
198 /*-----------------------------------------------------------*/\r
199 \r
200 void vPortYield( void )\r
201 {\r
202     /* Set a PendSV to request a context switch. */\r
203     *( portNVIC_INT_CTRL ) = portNVIC_PENDSVSET;\r
204 \r
205     /* Barriers are normally not required but do ensure the code is completely\r
206      * within the specified behaviour for the architecture. */\r
207     __DSB();\r
208     __ISB();\r
209 }\r
210 /*-----------------------------------------------------------*/\r
211 \r
212 void vPortEnterCritical( void )\r
213 {\r
214     portDISABLE_INTERRUPTS();\r
215     ulCriticalNesting++;\r
216     __DSB();\r
217     __ISB();\r
218 \r
219     /* This is not the interrupt safe version of the enter critical function so\r
220      * assert() if it is being called from an interrupt context.  Only API\r
221      * functions that end in "FromISR" can be used in an interrupt.  Only assert if\r
222      * the critical nesting count is 1 to protect against recursive calls if the\r
223      * assert function also uses a critical section. */\r
224     if( ulCriticalNesting == 1 )\r
225     {\r
226         configASSERT( ( ( *( portNVIC_INT_CTRL ) ) & portVECTACTIVE_MASK ) == 0 );\r
227     }\r
228 }\r
229 /*-----------------------------------------------------------*/\r
230 \r
231 void vPortExitCritical( void )\r
232 {\r
233     configASSERT( ulCriticalNesting );\r
234     ulCriticalNesting--;\r
235 \r
236     if( ulCriticalNesting == 0 )\r
237     {\r
238         portENABLE_INTERRUPTS();\r
239     }\r
240 }\r
241 /*-----------------------------------------------------------*/\r
242 \r
243 void SysTick_Handler( void )\r
244 {\r
245     uint32_t ulDummy;\r
246 \r
247     ulDummy = portSET_INTERRUPT_MASK_FROM_ISR();\r
248     {\r
249         if( xTaskIncrementTick() != pdFALSE )\r
250         {\r
251             /* Pend a context switch. */\r
252             *( portNVIC_INT_CTRL ) = portNVIC_PENDSVSET;\r
253         }\r
254     }\r
255     portCLEAR_INTERRUPT_MASK_FROM_ISR( ulDummy );\r
256 }\r
257 /*-----------------------------------------------------------*/\r
258 \r
259 /*\r
260  * Setup the systick timer to generate the tick interrupts at the required\r
261  * frequency.\r
262  */\r
263 void prvSetupTimerInterrupt( void )\r
264 {\r
265     /* Configure SysTick to interrupt at the requested rate. */\r
266     *( portNVIC_SYSTICK_LOAD ) = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;\r
267     *( portNVIC_SYSTICK_CTRL ) = portNVIC_SYSTICK_CLK | portNVIC_SYSTICK_INT | portNVIC_SYSTICK_ENABLE;\r
268 }\r
269 /*-----------------------------------------------------------*/\r