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