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