]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/ThirdParty/GCC/nrf52840-dk/port_cmsis.c
Remove the FreeRTOS-IoT-Libraries from FreeRTOS-Plus as it was an old copy with a...
[freertos] / FreeRTOS / Source / portable / ThirdParty / GCC / nrf52840-dk / port_cmsis.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 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. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\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 #ifdef SOFTDEVICE_PRESENT\r
37 #include "nrf_soc.h"\r
38 #include "app_util.h"\r
39 #include "app_util_platform.h"\r
40 #endif\r
41 \r
42 #if !(__FPU_USED) && !(__LINT__)\r
43     #error This port can only be used when the project options are configured to enable hardware floating point support.\r
44 #endif\r
45 \r
46 #if configMAX_SYSCALL_INTERRUPT_PRIORITY == 0\r
47     #error configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to 0. See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html\r
48 #endif\r
49 \r
50 /* Constants used to detect a Cortex-M7 r0p1 core, which should use the ARM_CM7\r
51 r0p1 port. */\r
52 #define portCORTEX_M4_r0p1_ID               ( 0x410FC241UL )\r
53 \r
54 /* Constants required to check the validity of an interrupt priority. */\r
55 #define portFIRST_USER_INTERRUPT_NUMBER     ( 16 )\r
56 #define portMAX_8_BIT_VALUE                 ( ( uint8_t ) 0xff )\r
57 #define portTOP_BIT_OF_BYTE                 ( ( uint8_t ) 0x80 )\r
58 \r
59 /* Constants required to set up the initial stack. */\r
60 #define portINITIAL_XPSR                    (((xPSR_Type){.b.T = 1}).w)\r
61 #define portINITIAL_EXEC_RETURN             ( 0xfffffffd )\r
62 \r
63 /* Let the user override the pre-loading of the initial LR with the address of\r
64 prvTaskExitError() in case is messes up unwinding of the stack in the\r
65 debugger. */\r
66 #ifdef configTASK_RETURN_ADDRESS\r
67     #define portTASK_RETURN_ADDRESS configTASK_RETURN_ADDRESS\r
68 #else\r
69     #define portTASK_RETURN_ADDRESS prvTaskExitError\r
70 #endif\r
71 \r
72 /* Each task maintains its own interrupt status in the critical nesting\r
73 variable. */\r
74 static UBaseType_t uxCriticalNesting = 0;\r
75 \r
76 /*\r
77  * Setup the timer to generate the tick interrupts.  The implementation in this\r
78  * file is weak to allow application writers to change the timer used to\r
79  * generate the tick interrupt.\r
80  */\r
81 extern void vPortSetupTimerInterrupt( void );\r
82 \r
83 /*\r
84  * Exception handlers.\r
85  */\r
86 void xPortSysTickHandler( void );\r
87 \r
88 /*\r
89  * Start first task is a separate function so it can be tested in isolation.\r
90  */\r
91 extern void vPortStartFirstTask( void );\r
92 \r
93 /*\r
94  * Function to enable the VFP.\r
95  */\r
96 static void vPortEnableVFP( void );\r
97 \r
98 /*\r
99  * Used to catch tasks that attempt to return from their implementing function.\r
100  */\r
101 static void prvTaskExitError( void );\r
102 \r
103 /*-----------------------------------------------------------*/\r
104 \r
105 /*\r
106  * Used by the portASSERT_IF_INTERRUPT_PRIORITY_INVALID() macro to ensure\r
107  * FreeRTOS API functions are not called from interrupts that have been assigned\r
108  * a priority above configMAX_SYSCALL_INTERRUPT_PRIORITY.\r
109  */\r
110 #if ( configASSERT_DEFINED == 1 )\r
111      static uint8_t ucMaxSysCallPriority = 0;\r
112      static uint32_t ulMaxPRIGROUPValue = 0;\r
113 #endif /* configASSERT_DEFINED */\r
114 \r
115 /*-----------------------------------------------------------*/\r
116 \r
117 /*\r
118  * See header file for description.\r
119  */\r
120 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
121 {\r
122     /* Simulate the stack frame as it would be created by a context switch\r
123     interrupt. */\r
124 \r
125     /* Offset added to account for the way the MCU uses the stack on entry/exit\r
126     of interrupts, and to ensure alignment. */\r
127     pxTopOfStack--;\r
128 \r
129     *pxTopOfStack = portINITIAL_XPSR;   /* xPSR */\r
130     pxTopOfStack--;\r
131     *pxTopOfStack = ( StackType_t ) pxCode; /* PC */\r
132     pxTopOfStack--;\r
133     *pxTopOfStack = ( StackType_t ) portTASK_RETURN_ADDRESS;    /* LR */\r
134 \r
135     /* Save code space by skipping register initialisation. */\r
136     pxTopOfStack -= 5;  /* R12, R3, R2 and R1. */\r
137     *pxTopOfStack = ( StackType_t ) pvParameters;   /* R0 */\r
138 \r
139     /* A save method is being used that requires each task to maintain its\r
140     own exec return value. */\r
141     pxTopOfStack--;\r
142     *pxTopOfStack = portINITIAL_EXEC_RETURN;\r
143 \r
144     pxTopOfStack -= 8;  /* R11, R10, R9, R8, R7, R6, R5 and R4. */\r
145 \r
146     return pxTopOfStack;\r
147 }\r
148 /*-----------------------------------------------------------*/\r
149 \r
150 static void prvTaskExitError( void )\r
151 {\r
152     /* A function that implements a task must not exit or attempt to return to\r
153     its caller as there is nothing to return to.  If a task wants to exit it\r
154     should instead call vTaskDelete( NULL ).\r
155 \r
156     Artificially force an assert() to be triggered if configASSERT() is\r
157     defined, then stop here so application writers can catch the error. */\r
158     configASSERT( uxCriticalNesting == ~0UL );\r
159     portDISABLE_INTERRUPTS();\r
160     for ( ;; );\r
161 }\r
162 \r
163 \r
164 /*-----------------------------------------------------------*/\r
165 \r
166 /*\r
167  * See header file for description.\r
168  */\r
169 BaseType_t xPortStartScheduler( void )\r
170 {\r
171     /* configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to 0.\r
172     See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */\r
173     configASSERT( configMAX_SYSCALL_INTERRUPT_PRIORITY );\r
174 \r
175     /* This port is designed for nRF52, this is Cortex-M4 r0p1. */\r
176     configASSERT( SCB->CPUID == portCORTEX_M4_r0p1_ID );\r
177 \r
178     #if ( configASSERT_DEFINED == 1 )\r
179     {\r
180         volatile uint32_t ulOriginalPriority;\r
181         volatile uint8_t * const pucFirstUserPriorityRegister = &NVIC->IP[0];\r
182         volatile uint8_t ucMaxPriorityValue;\r
183 \r
184         /* Determine the maximum priority from which ISR safe FreeRTOS API\r
185         functions can be called.  ISR safe functions are those that end in\r
186         "FromISR".  FreeRTOS maintains separate thread and ISR API functions to\r
187         ensure interrupt entry is as fast and simple as possible.\r
188 \r
189         Save the interrupt priority value that is about to be clobbered. */\r
190         ulOriginalPriority = *pucFirstUserPriorityRegister;\r
191 \r
192         /* Determine the number of priority bits available.  First write to all\r
193         possible bits. */\r
194         *pucFirstUserPriorityRegister = portMAX_8_BIT_VALUE;\r
195 \r
196         /* Read the value back to see how many bits stuck. */\r
197         ucMaxPriorityValue = *pucFirstUserPriorityRegister;\r
198 \r
199         /* Use the same mask on the maximum system call priority. */\r
200         ucMaxSysCallPriority = configMAX_SYSCALL_INTERRUPT_PRIORITY & ucMaxPriorityValue;\r
201 \r
202         /* Calculate the maximum acceptable priority group value for the number\r
203         of bits read back. */\r
204         ulMaxPRIGROUPValue = SCB_AIRCR_PRIGROUP_Msk >> SCB_AIRCR_PRIGROUP_Pos;\r
205         while ( ( ucMaxPriorityValue & portTOP_BIT_OF_BYTE ) == portTOP_BIT_OF_BYTE )\r
206         {\r
207             ulMaxPRIGROUPValue--;\r
208             ucMaxPriorityValue <<= ( uint8_t ) 0x01;\r
209         }\r
210 \r
211         /* Remove any bits that are more than actually existing. */\r
212         ulMaxPRIGROUPValue &= SCB_AIRCR_PRIGROUP_Msk >> SCB_AIRCR_PRIGROUP_Pos;\r
213 \r
214         /* Restore the clobbered interrupt priority register to its original\r
215         value. */\r
216         *pucFirstUserPriorityRegister = ulOriginalPriority;\r
217     }\r
218     #endif /* conifgASSERT_DEFINED */\r
219 \r
220     /* Make PendSV the lowest priority interrupts. */\r
221     NVIC_SetPriority(PendSV_IRQn, configKERNEL_INTERRUPT_PRIORITY);\r
222 \r
223     /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
224     here already. */\r
225     vPortSetupTimerInterrupt();\r
226 \r
227     /* Initialise the critical nesting count ready for the first task. */\r
228     uxCriticalNesting = 0;\r
229 \r
230     /* Ensure the VFP is enabled - it should be anyway. */\r
231     vPortEnableVFP();\r
232 \r
233     /* Lazy save always. */\r
234     FPU->FPCCR |= FPU_FPCCR_ASPEN_Msk | FPU_FPCCR_LSPEN_Msk;\r
235 \r
236     /* Finally this port requires SEVONPEND to be active */\r
237     SCB->SCR |= SCB_SCR_SEVONPEND_Msk;\r
238 \r
239     /* Start the first task. */\r
240     vPortStartFirstTask();\r
241 \r
242     /* Should never get here as the tasks will now be executing!  Call the task\r
243     exit error function to prevent compiler warnings about a static function\r
244     not being called in the case that the application writer overrides this\r
245     functionality by defining configTASK_RETURN_ADDRESS. */\r
246     prvTaskExitError();\r
247 \r
248     /* Should not get here! */\r
249     return 0;\r
250 }\r
251 /*-----------------------------------------------------------*/\r
252 \r
253 void vPortEndScheduler( void )\r
254 {\r
255     /* Not implemented in ports where there is nothing to return to.\r
256     Artificially force an assert. */\r
257     configASSERT( uxCriticalNesting == 1000UL );\r
258 }\r
259 /*-----------------------------------------------------------*/\r
260 \r
261 void vPortEnterCritical( void )\r
262 {\r
263     portDISABLE_INTERRUPTS();\r
264     uxCriticalNesting++;\r
265 \r
266     /* This is not the interrupt safe version of the enter critical function so\r
267     assert() if it is being called from an interrupt context.  Only API\r
268     functions that end in "FromISR" can be used in an interrupt.  Only assert if\r
269     the critical nesting count is 1 to protect against recursive calls if the\r
270     assert function also uses a critical section. */\r
271     if ( uxCriticalNesting == 1 )\r
272     {\r
273         configASSERT( ( SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk ) == 0 );\r
274     }\r
275 }\r
276 /*-----------------------------------------------------------*/\r
277 \r
278 void vPortExitCritical( void )\r
279 {\r
280     configASSERT( uxCriticalNesting );\r
281     uxCriticalNesting--;\r
282     if ( uxCriticalNesting == 0 )\r
283     {\r
284         portENABLE_INTERRUPTS();\r
285     }\r
286 }\r
287 \r
288 /*-----------------------------------------------------------*/\r
289 \r
290 /* This is a naked function. */\r
291 static void vPortEnableVFP( void )\r
292 {\r
293     SCB->CPACR |= 0xf << 20;\r
294 }\r
295 /*-----------------------------------------------------------*/\r
296 \r
297 #if ( configASSERT_DEFINED == 1 )\r
298 \r
299     void vPortValidateInterruptPriority( void )\r
300     {\r
301     uint32_t ulCurrentInterrupt;\r
302     uint8_t ucCurrentPriority;\r
303     IPSR_Type ipsr;\r
304 \r
305         /* Obtain the number of the currently executing interrupt. */\r
306         ipsr.w = __get_IPSR();\r
307         ulCurrentInterrupt = ipsr.b.ISR;\r
308 \r
309         /* Is the interrupt number a user defined interrupt? */\r
310         if ( ulCurrentInterrupt >= portFIRST_USER_INTERRUPT_NUMBER )\r
311         {\r
312             /* Look up the interrupt's priority. */\r
313             ucCurrentPriority = NVIC->IP[ ulCurrentInterrupt - portFIRST_USER_INTERRUPT_NUMBER ];\r
314 \r
315             /* The following assertion will fail if a service routine (ISR) for\r
316             an interrupt that has been assigned a priority above\r
317             configMAX_SYSCALL_INTERRUPT_PRIORITY calls an ISR safe FreeRTOS API\r
318             function.  ISR safe FreeRTOS API functions must *only* be called\r
319             from interrupts that have been assigned a priority at or below\r
320             configMAX_SYSCALL_INTERRUPT_PRIORITY.\r
321 \r
322             Numerically low interrupt priority numbers represent logically high\r
323             interrupt priorities, therefore the priority of the interrupt must\r
324             be set to a value equal to or numerically *higher* than\r
325             configMAX_SYSCALL_INTERRUPT_PRIORITY.\r
326 \r
327             Interrupts that use the FreeRTOS API must not be left at their\r
328             default priority of zero as that is the highest possible priority,\r
329             which is guaranteed to be above configMAX_SYSCALL_INTERRUPT_PRIORITY,\r
330             and therefore also guaranteed to be invalid.\r
331 \r
332             FreeRTOS maintains separate thread and ISR API functions to ensure\r
333             interrupt entry is as fast and simple as possible.\r
334 \r
335             The following links provide detailed information:\r
336             http://www.freertos.org/RTOS-Cortex-M3-M4.html\r
337             http://www.freertos.org/FAQHelp.html */\r
338             configASSERT( ucCurrentPriority >= ucMaxSysCallPriority );\r
339         }\r
340 \r
341         /* Priority grouping:  The interrupt controller (NVIC) allows the bits\r
342         that define each interrupt's priority to be split between bits that\r
343         define the interrupt's pre-emption priority bits and bits that define\r
344         the interrupt's sub-priority.  For simplicity all bits must be defined\r
345         to be pre-emption priority bits.  The following assertion will fail if\r
346         this is not the case (if some bits represent a sub-priority).\r
347 \r
348         If the application only uses CMSIS libraries for interrupt\r
349         configuration then the correct setting can be achieved on all Cortex-M\r
350         devices by calling NVIC_SetPriorityGrouping( 0 ); before starting the\r
351         scheduler.  Note however that some vendor specific peripheral libraries\r
352         assume a non-zero priority group setting, in which cases using a value\r
353         of zero will result in unpredicable behaviour. */\r
354         configASSERT( NVIC_GetPriorityGrouping() <= ulMaxPRIGROUPValue );\r
355     }\r
356 \r
357 #endif /* configASSERT_DEFINED */\r