]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/ThirdParty/GCC/Wiced_CY/port.c
Remove the FreeRTOS-IoT-Libraries from FreeRTOS-Plus as it was an old copy with a...
[freertos] / FreeRTOS / Source / portable / ThirdParty / GCC / Wiced_CY / port.c
1 /*\r
2  * FreeRTOS Kernel V10.2.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.\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 /* Standard includes. */\r
29 #include <stdlib.h>\r
30 \r
31 /* Scheduler includes. */\r
32 #include "FreeRTOS.h"\r
33 #include "task.h"\r
34 \r
35 #if configUSE_PORT_OPTIMISED_TASK_SELECTION == 1\r
36         /* Check the configuration. */\r
37         #if( configMAX_PRIORITIES > 32 )\r
38                 #error configUSE_PORT_OPTIMISED_TASK_SELECTION can only be set to 1 when configMAX_PRIORITIES is less than or equal to 32.  It is very rare that a system requires more than 10 to 15 difference priorities as tasks that share a priority will time slice.\r
39         #endif\r
40 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */\r
41 \r
42 #ifndef configSETUP_TICK_INTERRUPT\r
43         #error configSETUP_TICK_INTERRUPT() must be defined in FreeRTOSConfig.h to call the function that sets up the tick interrupt.\r
44 #endif\r
45 \r
46 #ifndef configCLEAR_TICK_INTERRUPT\r
47         #error configCLEAR_TICK_INTERRUPT must be defined in FreeRTOSConfig.h to clear which ever interrupt was used to generate the tick interrupt.\r
48 #endif\r
49 \r
50 /* A critical section is exited when the critical section nesting count reaches\r
51 this value. */\r
52 #define portNO_CRITICAL_NESTING                 ( ( uint32_t ) 0 )\r
53 \r
54 /* Tasks are not created with a floating point context, but can be given a\r
55 floating point context after they have been created.  A variable is stored as\r
56 part of the tasks context that holds portNO_FLOATING_POINT_CONTEXT if the task\r
57 does not have an FPU context, or any other value if the task does have an FPU\r
58 context. */\r
59 #define portNO_FLOATING_POINT_CONTEXT   ( ( StackType_t ) 0 )\r
60 \r
61 /* Constants required to setup the initial task context. */\r
62 #define portINITIAL_SPSR                                ( ( StackType_t ) 0x1f ) /* System mode, ARM mode, IRQ enabled FIQ enabled. */\r
63 #define portTHUMB_MODE_BIT                              ( ( StackType_t ) 0x20 )\r
64 #define portTHUMB_MODE_ADDRESS                  ( 0x01UL )\r
65 \r
66 /* Masks all bits in the APSR other than the mode bits. */\r
67 #define portAPSR_MODE_BITS_MASK                 ( 0x1F )\r
68 \r
69 /* The value of the mode bits in the APSR when the CPU is executing in user\r
70 mode. */\r
71 #define portAPSR_USER_MODE                              ( 0x10 )\r
72 \r
73 /* Let the user override the pre-loading of the initial LR with the address of\r
74 prvTaskExitError() in case it messes up unwinding of the stack in the\r
75 debugger. */\r
76 #ifdef configTASK_RETURN_ADDRESS\r
77         #define portTASK_RETURN_ADDRESS configTASK_RETURN_ADDRESS\r
78 #else\r
79         #define portTASK_RETURN_ADDRESS prvTaskExitError\r
80 #endif\r
81 \r
82 /*-----------------------------------------------------------*/\r
83 \r
84 /*\r
85  * Starts the first task executing.  This function is necessarily written in\r
86  * assembly code so is implemented in portASM.s.\r
87  */\r
88 extern void vPortRestoreTaskContext( void );\r
89 \r
90 /*\r
91  * Used to catch tasks that attempt to return from their implementing function.\r
92  */\r
93 static void prvTaskExitError( void );\r
94 \r
95 /*-----------------------------------------------------------*/\r
96 \r
97 /* A variable is used to keep track of the critical section nesting.  This\r
98 variable has to be stored as part of the task context and must be initialised to\r
99 a non zero value to ensure interrupts don't inadvertently become unmasked before\r
100 the scheduler starts.  As it is stored as part of the task context it will\r
101 automatically be set to 0 when the first task is started. */\r
102 volatile uint32_t ulCriticalNesting = 9999UL;\r
103 \r
104 /* Saved as part of the task context.  If ulPortTaskHasFPUContext is non-zero then\r
105 a floating point context must be saved and restored for the task. */\r
106 volatile uint32_t ulPortTaskHasFPUContext = pdFALSE;\r
107 \r
108 /* Set to 1 to pend a context switch from an ISR. */\r
109 volatile uint32_t ulPortYieldRequired = pdFALSE;\r
110 \r
111 /* Counts the interrupt nesting depth.  A context switch is only performed if\r
112 if the nesting depth is 0. */\r
113 volatile uint32_t ulPortInterruptNesting = 0UL;\r
114 \r
115 /* Used in the asm file to clear an interrupt. */\r
116 __attribute__(( used )) const uint32_t ulICCEOIR = configEOI_ADDRESS;\r
117 \r
118 /*-----------------------------------------------------------*/\r
119 \r
120 /*\r
121  * See header file for description.\r
122  */\r
123 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
124 {\r
125         /* Setup the initial stack of the task.  The stack is set exactly as\r
126         expected by the portRESTORE_CONTEXT() macro.\r
127 \r
128         The fist real value on the stack is the status register, which is set for\r
129         system mode, with interrupts enabled.  A few NULLs are added first to ensure\r
130         GDB does not try decoding a non-existent return address. */\r
131         *pxTopOfStack = ( StackType_t ) NULL;\r
132         pxTopOfStack--;\r
133         *pxTopOfStack = ( StackType_t ) NULL;\r
134         pxTopOfStack--;\r
135         *pxTopOfStack = ( StackType_t ) NULL;\r
136         pxTopOfStack--;\r
137         *pxTopOfStack = ( StackType_t ) portINITIAL_SPSR;\r
138 \r
139         if( ( ( uint32_t ) pxCode & portTHUMB_MODE_ADDRESS ) != 0x00UL )\r
140         {\r
141                 /* The task will start in THUMB mode. */\r
142                 *pxTopOfStack |= portTHUMB_MODE_BIT;\r
143         }\r
144 \r
145         pxTopOfStack--;\r
146 \r
147         /* Next the return address, which in this case is the start of the task. */\r
148         *pxTopOfStack = ( StackType_t ) pxCode;\r
149         pxTopOfStack--;\r
150 \r
151         /* Next all the registers other than the stack pointer. */\r
152         *pxTopOfStack = ( StackType_t ) portTASK_RETURN_ADDRESS;        /* R14 */\r
153         pxTopOfStack--;\r
154         *pxTopOfStack = ( StackType_t ) 0x12121212;     /* R12 */\r
155         pxTopOfStack--;\r
156         *pxTopOfStack = ( StackType_t ) 0x11111111;     /* R11 */\r
157         pxTopOfStack--;\r
158         *pxTopOfStack = ( StackType_t ) 0x10101010;     /* R10 */\r
159         pxTopOfStack--;\r
160         *pxTopOfStack = ( StackType_t ) 0x09090909;     /* R9 */\r
161         pxTopOfStack--;\r
162         *pxTopOfStack = ( StackType_t ) 0x08080808;     /* R8 */\r
163         pxTopOfStack--;\r
164         *pxTopOfStack = ( StackType_t ) 0x07070707;     /* R7 */\r
165         pxTopOfStack--;\r
166         *pxTopOfStack = ( StackType_t ) 0x06060606;     /* R6 */\r
167         pxTopOfStack--;\r
168         *pxTopOfStack = ( StackType_t ) 0x05050505;     /* R5 */\r
169         pxTopOfStack--;\r
170         *pxTopOfStack = ( StackType_t ) 0x04040404;     /* R4 */\r
171         pxTopOfStack--;\r
172         *pxTopOfStack = ( StackType_t ) 0x03030303;     /* R3 */\r
173         pxTopOfStack--;\r
174         *pxTopOfStack = ( StackType_t ) 0x02020202;     /* R2 */\r
175         pxTopOfStack--;\r
176         *pxTopOfStack = ( StackType_t ) 0x01010101;     /* R1 */\r
177         pxTopOfStack--;\r
178         *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */\r
179         pxTopOfStack--;\r
180 \r
181         /* The task will start with a critical nesting count of 0 as interrupts are\r
182         enabled. */\r
183         *pxTopOfStack = portNO_CRITICAL_NESTING;\r
184         pxTopOfStack--;\r
185 \r
186         /* The task will start without a floating point context.  A task that uses\r
187         the floating point hardware must call vPortTaskUsesFPU() before executing\r
188         any floating point instructions. */\r
189         *pxTopOfStack = portNO_FLOATING_POINT_CONTEXT;\r
190 \r
191         return pxTopOfStack;\r
192 }\r
193 /*-----------------------------------------------------------*/\r
194 \r
195 static void prvTaskExitError( void )\r
196 {\r
197         /* A function that implements a task must not exit or attempt to return to\r
198         its caller as there is nothing to return to.  If a task wants to exit it\r
199         should instead call vTaskDelete( NULL ).\r
200 \r
201         Artificially force an assert() to be triggered if configASSERT() is\r
202         defined, then stop here so application writers can catch the error. */\r
203         configASSERT( ulPortInterruptNesting == ~0UL );\r
204         portDISABLE_INTERRUPTS();\r
205         for( ;; );\r
206 }\r
207 /*-----------------------------------------------------------*/\r
208 \r
209 BaseType_t xPortStartScheduler( void )\r
210 {\r
211 uint32_t ulAPSR;\r
212 \r
213         /* Only continue if the CPU is not in User mode.  The CPU must be in a\r
214         Privileged mode for the scheduler to start. */\r
215         __asm volatile ( "MRS %0, APSR" : "=r" ( ulAPSR ) );\r
216         ulAPSR &= portAPSR_MODE_BITS_MASK;\r
217         configASSERT( ulAPSR != portAPSR_USER_MODE );\r
218 \r
219         if( ulAPSR != portAPSR_USER_MODE )\r
220         {\r
221                 /* Start the timer that generates the tick ISR. */\r
222                 portDISABLE_INTERRUPTS();\r
223                 configSETUP_TICK_INTERRUPT();\r
224 \r
225                 /* Start the first task executing. */\r
226                 vPortRestoreTaskContext();\r
227         }\r
228 \r
229         /* Will only get here if vTaskStartScheduler() was called with the CPU in\r
230         a non-privileged mode or the binary point register was not set to its lowest\r
231         possible value.  prvTaskExitError() is referenced to prevent a compiler\r
232         warning about it being defined but not referenced in the case that the user\r
233         defines their own exit address. */\r
234         ( void ) prvTaskExitError;\r
235         return 0;\r
236 }\r
237 /*-----------------------------------------------------------*/\r
238 \r
239 void vPortEndScheduler( void )\r
240 {\r
241         /* Not implemented in ports where there is nothing to return to.\r
242         Artificially force an assert. */\r
243         configASSERT( ulCriticalNesting == 1000UL );\r
244 }\r
245 /*-----------------------------------------------------------*/\r
246 \r
247 void vPortEnterCritical( void )\r
248 {\r
249         portDISABLE_INTERRUPTS();\r
250 \r
251         /* Now interrupts are disabled ulCriticalNesting can be accessed\r
252         directly.  Increment ulCriticalNesting to keep a count of how many times\r
253         portENTER_CRITICAL() has been called. */\r
254         ulCriticalNesting++;\r
255 \r
256         /* This is not the interrupt safe version of the enter critical function so\r
257         assert() if it is being called from an interrupt context.  Only API\r
258         functions that end in "FromISR" can be used in an interrupt.  Only assert if\r
259         the critical nesting count is 1 to protect against recursive calls if the\r
260         assert function also uses a critical section. */\r
261         if( ulCriticalNesting == 1 )\r
262         {\r
263                 configASSERT( ulPortInterruptNesting == 0 );\r
264         }\r
265 }\r
266 /*-----------------------------------------------------------*/\r
267 \r
268 void vPortExitCritical( void )\r
269 {\r
270         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
271         {\r
272                 /* Decrement the nesting count as the critical section is being\r
273                 exited. */\r
274                 ulCriticalNesting--;\r
275 \r
276                 /* If the nesting level has reached zero then all interrupt\r
277                 priorities must be re-enabled. */\r
278                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
279                 {\r
280                         /* Critical nesting has reached zero so all interrupt priorities\r
281                         should be unmasked. */\r
282                         portENABLE_INTERRUPTS();\r
283                 }\r
284         }\r
285 }\r
286 /*-----------------------------------------------------------*/\r
287 \r
288 void FreeRTOS_Tick_Handler( void )\r
289 {\r
290 uint32_t ulInterruptStatus;\r
291 \r
292         ulInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();\r
293 \r
294         /* Increment the RTOS tick. */\r
295         if( xTaskIncrementTick() != pdFALSE )\r
296         {\r
297                 ulPortYieldRequired = pdTRUE;\r
298         }\r
299 \r
300         portCLEAR_INTERRUPT_MASK_FROM_ISR( ulInterruptStatus );\r
301 \r
302         configCLEAR_TICK_INTERRUPT();\r
303 }\r
304 /*-----------------------------------------------------------*/\r
305 \r
306 void vPortTaskUsesFPU( void )\r
307 {\r
308 #if configFPU == 1\r
309 uint32_t ulInitialFPSCR = 0;\r
310 \r
311         /* A task is registering the fact that it needs an FPU context.  Set the\r
312         FPU flag (which is saved as part of the task context). */\r
313         ulPortTaskHasFPUContext = pdTRUE;\r
314 \r
315         /* Initialise the floating point status register. */\r
316         __asm volatile ( "FMXR  FPSCR, %0" :: "r" (ulInitialFPSCR) );\r
317 #else\r
318         /* If FreeRTOS was built without FPU support but a task is using the FPU, we have a problem */\r
319         configASSERT( 0 );\r
320 #endif\r
321 }\r
322 /*-----------------------------------------------------------*/\r
323 \r
324 \r