]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/IAR/ARM_CA9/port.c
Demo application related:
[freertos] / FreeRTOS / Source / portable / IAR / ARM_CA9 / port.c
1 /*\r
2     FreeRTOS V8.0.1 - Copyright (C) 2014 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS kernel.                                   !<<\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /* Standard includes. */\r
67 #include <stdlib.h>\r
68 \r
69 /* IAR includes. */\r
70 #include <intrinsics.h>\r
71 \r
72 /* Scheduler includes. */\r
73 #include "FreeRTOS.h"\r
74 #include "task.h"\r
75 \r
76 #ifndef configINTERRUPT_CONTROLLER_BASE_ADDRESS\r
77         #error configINTERRUPT_CONTROLLER_BASE_ADDRESS must be defined.  See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html\r
78 #endif\r
79 \r
80 #ifndef configINTERRUPT_CONTROLLER_CPU_INTERFACE_OFFSET\r
81         #error configINTERRUPT_CONTROLLER_CPU_INTERFACE_OFFSET must be defined.  See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html\r
82 #endif\r
83 \r
84 #ifndef configUNIQUE_INTERRUPT_PRIORITIES\r
85         #error configUNIQUE_INTERRUPT_PRIORITIES must be defined.  See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html\r
86 #endif\r
87 \r
88 #ifndef configSETUP_TICK_INTERRUPT\r
89         #error configSETUP_TICK_INTERRUPT() must be defined.  See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html\r
90 #endif /* configSETUP_TICK_INTERRUPT */\r
91 \r
92 #ifndef configMAX_API_CALL_INTERRUPT_PRIORITY\r
93         #error configMAX_API_CALL_INTERRUPT_PRIORITY must be defined.  See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html\r
94 #endif\r
95 \r
96 #if configMAX_API_CALL_INTERRUPT_PRIORITY == 0\r
97         #error configMAX_API_CALL_INTERRUPT_PRIORITY must not be set to 0\r
98 #endif\r
99 \r
100 #if configMAX_API_CALL_INTERRUPT_PRIORITY > configUNIQUE_INTERRUPT_PRIORITIES\r
101         #error configMAX_API_CALL_INTERRUPT_PRIORITY must be less than or equal to configUNIQUE_INTERRUPT_PRIORITIES as the lower the numeric priority value the higher the logical interrupt priority\r
102 #endif\r
103 \r
104 #if configUSE_PORT_OPTIMISED_TASK_SELECTION == 1\r
105         /* Check the configuration. */\r
106         #if( configMAX_PRIORITIES > 32 )\r
107                 #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
108         #endif\r
109 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */\r
110 \r
111 /* In case security extensions are implemented. */\r
112 #if configMAX_API_CALL_INTERRUPT_PRIORITY <= ( configUNIQUE_INTERRUPT_PRIORITIES / 2 )\r
113         #error configMAX_API_CALL_INTERRUPT_PRIORITY must be greater than ( configUNIQUE_INTERRUPT_PRIORITIES / 2 )\r
114 #endif\r
115 \r
116 #ifndef configCLEAR_TICK_INTERRUPT\r
117         #define configCLEAR_TICK_INTERRUPT()\r
118 #endif\r
119 \r
120 /* A critical section is exited when the critical section nesting count reaches\r
121 this value. */\r
122 #define portNO_CRITICAL_NESTING                 ( ( uint32_t ) 0 )\r
123 \r
124 /* In all GICs 255 can be written to the priority mask register to unmask all\r
125 (but the lowest) interrupt priority. */\r
126 #define portUNMASK_VALUE                                ( 0xFFUL )\r
127 \r
128 /* Tasks are not created with a floating point context, but can be given a\r
129 floating point context after they have been created.  A variable is stored as\r
130 part of the tasks context that holds portNO_FLOATING_POINT_CONTEXT if the task\r
131 does not have an FPU context, or any other value if the task does have an FPU\r
132 context. */\r
133 #define portNO_FLOATING_POINT_CONTEXT   ( ( StackType_t ) 0 )\r
134 \r
135 /* Constants required to setup the initial task context. */\r
136 #define portINITIAL_SPSR                                ( ( StackType_t ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */\r
137 #define portTHUMB_MODE_BIT                              ( ( StackType_t ) 0x20 )\r
138 #define portTHUMB_MODE_ADDRESS                  ( 0x01UL )\r
139 \r
140 /* Used by portASSERT_IF_INTERRUPT_PRIORITY_INVALID() when ensuring the binary\r
141 point is zero. */\r
142 #define portBINARY_POINT_BITS                   ( ( uint8_t ) 0x03 )\r
143 \r
144 /* Masks all bits in the APSR other than the mode bits. */\r
145 #define portAPSR_MODE_BITS_MASK                 ( 0x1F )\r
146 \r
147 /* The value of the mode bits in the APSR when the CPU is executing in user\r
148 mode. */\r
149 #define portAPSR_USER_MODE                              ( 0x10 )\r
150 \r
151 /* Macro to unmask all interrupt priorities. */\r
152 #define portCLEAR_INTERRUPT_MASK()                                                                                      \\r
153 {                                                                                                                                                       \\r
154         __disable_irq();                                                                                                                \\r
155         portICCPMR_PRIORITY_MASK_REGISTER = portUNMASK_VALUE;                                   \\r
156         __asm(  "DSB            \n"                                                                                                     \\r
157                         "ISB            \n" );                                                                                          \\r
158         __enable_irq();                                                                                                                 \\r
159 }\r
160 \r
161 /*-----------------------------------------------------------*/\r
162 \r
163 /*\r
164  * Starts the first task executing.  This function is necessarily written in\r
165  * assembly code so is implemented in portASM.s.\r
166  */\r
167 extern void vPortRestoreTaskContext( void );\r
168 \r
169 /*\r
170  * Used to catch tasks that attempt to return from their implementing function.\r
171  */\r
172 static void prvTaskExitError( void );\r
173 \r
174 /*-----------------------------------------------------------*/\r
175 \r
176 /* A variable is used to keep track of the critical section nesting.  This\r
177 variable has to be stored as part of the task context and must be initialised to\r
178 a non zero value to ensure interrupts don't inadvertently become unmasked before\r
179 the scheduler starts.  As it is stored as part of the task context it will\r
180 automatically be set to 0 when the first task is started. */\r
181 volatile uint32_t ulCriticalNesting = 9999UL;\r
182 \r
183 /* Saved as part of the task context.  If ulPortTaskHasFPUContext is non-zero\r
184 then a floating point context must be saved and restored for the task. */\r
185 uint32_t ulPortTaskHasFPUContext = pdFALSE;\r
186 \r
187 /* Set to 1 to pend a context switch from an ISR. */\r
188 uint32_t ulPortYieldRequired = pdFALSE;\r
189 \r
190 /* Counts the interrupt nesting depth.  A context switch is only performed if\r
191 if the nesting depth is 0. */\r
192 uint32_t ulPortInterruptNesting = 0UL;\r
193 \r
194 \r
195 /*-----------------------------------------------------------*/\r
196 \r
197 /*\r
198  * See header file for description.\r
199  */\r
200 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
201 {\r
202         /* Setup the initial stack of the task.  The stack is set exactly as\r
203         expected by the portRESTORE_CONTEXT() macro.\r
204 \r
205         The fist real value on the stack is the status register, which is set for\r
206         system mode, with interrupts enabled.  A few NULLs are added first to ensure\r
207         GDB does not try decoding a non-existent return address. */\r
208         *pxTopOfStack = NULL;\r
209         pxTopOfStack--;\r
210         *pxTopOfStack = NULL;\r
211         pxTopOfStack--;\r
212         *pxTopOfStack = NULL;\r
213         pxTopOfStack--;\r
214         *pxTopOfStack = ( StackType_t ) portINITIAL_SPSR;\r
215 \r
216         if( ( ( uint32_t ) pxCode & portTHUMB_MODE_ADDRESS ) != 0x00UL )\r
217         {\r
218                 /* The task will start in THUMB mode. */\r
219                 *pxTopOfStack |= portTHUMB_MODE_BIT;\r
220         }\r
221 \r
222         pxTopOfStack--;\r
223 \r
224         /* Next the return address, which in this case is the start of the task. */\r
225         *pxTopOfStack = ( StackType_t ) pxCode;\r
226         pxTopOfStack--;\r
227 \r
228         /* Next all the registers other than the stack pointer. */\r
229         *pxTopOfStack = ( StackType_t ) prvTaskExitError;       /* R14 */\r
230         pxTopOfStack--;\r
231         *pxTopOfStack = ( StackType_t ) 0x12121212;     /* R12 */\r
232         pxTopOfStack--;\r
233         *pxTopOfStack = ( StackType_t ) 0x11111111;     /* R11 */\r
234         pxTopOfStack--;\r
235         *pxTopOfStack = ( StackType_t ) 0x10101010;     /* R10 */\r
236         pxTopOfStack--;\r
237         *pxTopOfStack = ( StackType_t ) 0x09090909;     /* R9 */\r
238         pxTopOfStack--;\r
239         *pxTopOfStack = ( StackType_t ) 0x08080808;     /* R8 */\r
240         pxTopOfStack--;\r
241         *pxTopOfStack = ( StackType_t ) 0x07070707;     /* R7 */\r
242         pxTopOfStack--;\r
243         *pxTopOfStack = ( StackType_t ) 0x06060606;     /* R6 */\r
244         pxTopOfStack--;\r
245         *pxTopOfStack = ( StackType_t ) 0x05050505;     /* R5 */\r
246         pxTopOfStack--;\r
247         *pxTopOfStack = ( StackType_t ) 0x04040404;     /* R4 */\r
248         pxTopOfStack--;\r
249         *pxTopOfStack = ( StackType_t ) 0x03030303;     /* R3 */\r
250         pxTopOfStack--;\r
251         *pxTopOfStack = ( StackType_t ) 0x02020202;     /* R2 */\r
252         pxTopOfStack--;\r
253         *pxTopOfStack = ( StackType_t ) 0x01010101;     /* R1 */\r
254         pxTopOfStack--;\r
255         *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */\r
256         pxTopOfStack--;\r
257 \r
258         /* The task will start with a critical nesting count of 0 as interrupts are\r
259         enabled. */\r
260         *pxTopOfStack = portNO_CRITICAL_NESTING;\r
261         pxTopOfStack--;\r
262 \r
263         /* The task will start without a floating point context.  A task that uses\r
264         the floating point hardware must call vPortTaskUsesFPU() before executing\r
265         any floating point instructions. */\r
266         *pxTopOfStack = portNO_FLOATING_POINT_CONTEXT;\r
267 \r
268         return pxTopOfStack;\r
269 }\r
270 /*-----------------------------------------------------------*/\r
271 \r
272 static void prvTaskExitError( void )\r
273 {\r
274         /* A function that implements a task must not exit or attempt to return to\r
275         its caller as there is nothing to return to.  If a task wants to exit it\r
276         should instead call vTaskDelete( NULL ).\r
277 \r
278         Artificially force an assert() to be triggered if configASSERT() is\r
279         defined, then stop here so application writers can catch the error. */\r
280         configASSERT( ulPortInterruptNesting == ~0UL );\r
281         portDISABLE_INTERRUPTS();\r
282         for( ;; );\r
283 }\r
284 /*-----------------------------------------------------------*/\r
285 \r
286 BaseType_t xPortStartScheduler( void )\r
287 {\r
288 uint32_t ulAPSR;\r
289 \r
290         /* Only continue if the CPU is not in User mode.  The CPU must be in a\r
291         Privileged mode for the scheduler to start. */\r
292         __asm volatile ( "MRS %0, APSR" : "=r" ( ulAPSR ) );\r
293         ulAPSR &= portAPSR_MODE_BITS_MASK;\r
294         configASSERT( ulAPSR != portAPSR_USER_MODE );\r
295 \r
296         if( ulAPSR != portAPSR_USER_MODE )\r
297         {\r
298                 /* Only continue if the binary point value is set to its lowest possible\r
299                 setting.  See the comments in vPortValidateInterruptPriority() below for\r
300                 more information. */\r
301                 configASSERT( ( portICCBPR_BINARY_POINT_REGISTER & portBINARY_POINT_BITS ) <= portMAX_BINARY_POINT_VALUE );\r
302 \r
303                 if( ( portICCBPR_BINARY_POINT_REGISTER & portBINARY_POINT_BITS ) <= portMAX_BINARY_POINT_VALUE )\r
304                 {\r
305                         /* Start the timer that generates the tick ISR. */\r
306                         configSETUP_TICK_INTERRUPT();\r
307 \r
308                         __enable_irq();\r
309                         vPortRestoreTaskContext();\r
310                 }\r
311         }\r
312 \r
313         /* Will only get here if xTaskStartScheduler() was called with the CPU in\r
314         a non-privileged mode or the binary point register was not set to its lowest\r
315         possible value. */\r
316         return 0;\r
317 }\r
318 /*-----------------------------------------------------------*/\r
319 \r
320 void vPortEndScheduler( void )\r
321 {\r
322         /* Not implemented in ports where there is nothing to return to.\r
323         Artificially force an assert. */\r
324         configASSERT( ulCriticalNesting == 1000UL );\r
325 }\r
326 /*-----------------------------------------------------------*/\r
327 \r
328 void vPortEnterCritical( void )\r
329 {\r
330         /* Disable interrupts as per portDISABLE_INTERRUPTS();  */\r
331         ulPortSetInterruptMask();\r
332 \r
333         /* Now interrupts are disabled ulCriticalNesting can be accessed\r
334         directly.  Increment ulCriticalNesting to keep a count of how many times\r
335         portENTER_CRITICAL() has been called. */\r
336         ulCriticalNesting++;\r
337         \r
338         /* This is not the interrupt safe version of the enter critical function so\r
339         assert() if it is being called from an interrupt context.  Only API \r
340         functions that end in "FromISR" can be used in an interrupt.  Only assert if\r
341         the critical nesting count is 1 to protect against recursive calls if the\r
342         assert function also uses a critical section. */\r
343         if( ulCriticalNesting == 1 )\r
344         {\r
345                 configASSERT( ulPortInterruptNesting == 0 );\r
346         }\r
347 }\r
348 /*-----------------------------------------------------------*/\r
349 \r
350 void vPortExitCritical( void )\r
351 {\r
352         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
353         {\r
354                 /* Decrement the nesting count as the critical section is being\r
355                 exited. */\r
356                 ulCriticalNesting--;\r
357 \r
358                 /* If the nesting level has reached zero then all interrupt\r
359                 priorities must be re-enabled. */\r
360                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
361                 {\r
362                         /* Critical nesting has reached zero so all interrupt priorities\r
363                         should be unmasked. */\r
364                         portCLEAR_INTERRUPT_MASK();\r
365                 }\r
366         }\r
367 }\r
368 /*-----------------------------------------------------------*/\r
369 \r
370 void FreeRTOS_Tick_Handler( void )\r
371 {\r
372         /* Set interrupt mask before altering scheduler structures.   The tick\r
373         handler runs at the lowest priority, so interrupts cannot already be masked,\r
374         so there is no need to save and restore the current mask value. */\r
375         __disable_irq();\r
376         portICCPMR_PRIORITY_MASK_REGISTER = ( uint32_t ) ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT );\r
377         __asm(  "DSB            \n"\r
378                         "ISB            \n" );\r
379         __enable_irq();\r
380 \r
381         /* Increment the RTOS tick. */\r
382         if( xTaskIncrementTick() != pdFALSE )\r
383         {\r
384                 ulPortYieldRequired = pdTRUE;\r
385         }\r
386 \r
387         /* Ensure all interrupt priorities are active again. */\r
388         portCLEAR_INTERRUPT_MASK();\r
389         configCLEAR_TICK_INTERRUPT();\r
390 }\r
391 /*-----------------------------------------------------------*/\r
392 \r
393 void vPortTaskUsesFPU( void )\r
394 {\r
395 uint32_t ulInitialFPSCR = 0;\r
396 \r
397         /* A task is registering the fact that it needs an FPU context.  Set the\r
398         FPU flag (which is saved as part of the task context). */\r
399         ulPortTaskHasFPUContext = pdTRUE;\r
400 \r
401         /* Initialise the floating point status register. */\r
402         __asm( "FMXR    FPSCR, %0" :: "r" (ulInitialFPSCR) );\r
403 }\r
404 /*-----------------------------------------------------------*/\r
405 \r
406 void vPortClearInterruptMask( uint32_t ulNewMaskValue )\r
407 {\r
408         if( ulNewMaskValue == pdFALSE )\r
409         {\r
410                 portCLEAR_INTERRUPT_MASK();\r
411         }\r
412 }\r
413 /*-----------------------------------------------------------*/\r
414 \r
415 uint32_t ulPortSetInterruptMask( void )\r
416 {\r
417 uint32_t ulReturn;\r
418 \r
419         __disable_irq();\r
420         if( portICCPMR_PRIORITY_MASK_REGISTER == ( uint32_t ) ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT ) )\r
421         {\r
422                 /* Interrupts were already masked. */\r
423                 ulReturn = pdTRUE;\r
424         }\r
425         else\r
426         {\r
427                 ulReturn = pdFALSE;\r
428                 portICCPMR_PRIORITY_MASK_REGISTER = ( uint32_t ) ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT );\r
429                 __asm(  "DSB            \n"\r
430                                 "ISB            \n" );\r
431         }\r
432         __enable_irq();\r
433 \r
434         return ulReturn;\r
435 }\r
436 /*-----------------------------------------------------------*/\r
437 \r
438 #if( configASSERT_DEFINED == 1 )\r
439 \r
440         void vPortValidateInterruptPriority( void )\r
441         {\r
442                 /* The following assertion will fail if a service routine (ISR) for\r
443                 an interrupt that has been assigned a priority above\r
444                 configMAX_SYSCALL_INTERRUPT_PRIORITY calls an ISR safe FreeRTOS API\r
445                 function.  ISR safe FreeRTOS API functions must *only* be called\r
446                 from interrupts that have been assigned a priority at or below\r
447                 configMAX_SYSCALL_INTERRUPT_PRIORITY.\r
448 \r
449                 Numerically low interrupt priority numbers represent logically high\r
450                 interrupt priorities, therefore the priority of the interrupt must\r
451                 be set to a value equal to or numerically *higher* than\r
452                 configMAX_SYSCALL_INTERRUPT_PRIORITY.\r
453 \r
454                 FreeRTOS maintains separate thread and ISR API functions to ensure\r
455                 interrupt entry is as fast and simple as possible.\r
456 \r
457                 The following links provide detailed information:\r
458                 http://www.freertos.org/RTOS-Cortex-M3-M4.html\r
459                 http://www.freertos.org/FAQHelp.html */\r
460                 configASSERT( portICCRPR_RUNNING_PRIORITY_REGISTER >= ( uint32_t ) ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT ) );\r
461 \r
462                 /* Priority grouping:  The interrupt controller (GIC) allows the bits\r
463                 that define each interrupt's priority to be split between bits that\r
464                 define the interrupt's pre-emption priority bits and bits that define\r
465                 the interrupt's sub-priority.  For simplicity all bits must be defined\r
466                 to be pre-emption priority bits.  The following assertion will fail if\r
467                 this is not the case (if some bits represent a sub-priority).\r
468 \r
469                 The priority grouping is configured by the GIC's binary point register\r
470                 (ICCBPR).  Writting 0 to ICCBPR will ensure it is set to its lowest\r
471                 possible value (which may be above 0). */\r
472                 configASSERT( ( portICCBPR_BINARY_POINT_REGISTER & portBINARY_POINT_BITS ) <= portMAX_BINARY_POINT_VALUE );\r
473         }\r
474 \r
475 #endif /* configASSERT_DEFINED */\r
476 \r
477 \r