]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/IAR/ARM_CM0/port.c
Update version number to 8.1.2 after moving the defaulting of configUSE_PORT_OPTIMISE...
[freertos] / FreeRTOS / Source / portable / IAR / ARM_CM0 / port.c
1 /*\r
2     FreeRTOS V8.1.2 - 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 /*-----------------------------------------------------------\r
67  * Implementation of functions defined in portable.h for the ARM CM0 port.\r
68  *----------------------------------------------------------*/\r
69 \r
70 /* IAR includes. */\r
71 #include "intrinsics.h"\r
72 \r
73 /* Scheduler includes. */\r
74 #include "FreeRTOS.h"\r
75 #include "task.h"\r
76 \r
77 /* Constants required to manipulate the NVIC. */\r
78 #define portNVIC_SYSTICK_CTRL           ( ( volatile uint32_t *) 0xe000e010 )\r
79 #define portNVIC_SYSTICK_LOAD           ( ( volatile uint32_t *) 0xe000e014 )\r
80 #define portNVIC_SYSPRI2                        ( ( volatile uint32_t *) 0xe000ed20 )\r
81 #define portNVIC_SYSTICK_CLK            0x00000004\r
82 #define portNVIC_SYSTICK_INT            0x00000002\r
83 #define portNVIC_SYSTICK_ENABLE         0x00000001\r
84 #define portMIN_INTERRUPT_PRIORITY      ( 255UL )\r
85 #define portNVIC_PENDSV_PRI                     ( portMIN_INTERRUPT_PRIORITY << 16UL )\r
86 #define portNVIC_SYSTICK_PRI            ( portMIN_INTERRUPT_PRIORITY << 24UL )\r
87 \r
88 /* Constants required to set up the initial stack. */\r
89 #define portINITIAL_XPSR                        ( 0x01000000 )\r
90 \r
91 /* For backward compatibility, ensure configKERNEL_INTERRUPT_PRIORITY is\r
92 defined.  The value 255 should also ensure backward compatibility.\r
93 FreeRTOS.org versions prior to V4.3.0 did not include this definition. */\r
94 #ifndef configKERNEL_INTERRUPT_PRIORITY\r
95         #define configKERNEL_INTERRUPT_PRIORITY 0\r
96 #endif\r
97 \r
98 /* Each task maintains its own interrupt status in the critical nesting\r
99 variable. */\r
100 static UBaseType_t uxCriticalNesting = 0xaaaaaaaa;\r
101 \r
102 /*\r
103  * Setup the timer to generate the tick interrupts.\r
104  */\r
105 static void prvSetupTimerInterrupt( void );\r
106 \r
107 /*\r
108  * Exception handlers.\r
109  */\r
110 void xPortSysTickHandler( void );\r
111 \r
112 /*\r
113  * Start first task is a separate function so it can be tested in isolation.\r
114  */\r
115 extern void vPortStartFirstTask( void );\r
116 \r
117 /*\r
118  * Used to catch tasks that attempt to return from their implementing function.\r
119  */\r
120 static void prvTaskExitError( void );\r
121 \r
122 /*-----------------------------------------------------------*/\r
123 \r
124 /*\r
125  * See header file for description.\r
126  */\r
127 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
128 {\r
129         /* Simulate the stack frame as it would be created by a context switch\r
130         interrupt. */\r
131         pxTopOfStack--; /* Offset added to account for the way the MCU uses the stack on entry/exit of interrupts. */\r
132         *pxTopOfStack = portINITIAL_XPSR;       /* xPSR */\r
133         pxTopOfStack--;\r
134         *pxTopOfStack = ( StackType_t ) pxCode; /* PC */\r
135         pxTopOfStack--;\r
136         *pxTopOfStack = ( StackType_t ) prvTaskExitError;       /* LR */\r
137         pxTopOfStack -= 5;      /* R12, R3, R2 and R1. */\r
138         *pxTopOfStack = ( StackType_t ) pvParameters;   /* R0 */\r
139         pxTopOfStack -= 8; /* R11..R4. */\r
140 \r
141         return pxTopOfStack;\r
142 }\r
143 /*-----------------------------------------------------------*/\r
144 \r
145 static void prvTaskExitError( void )\r
146 {\r
147         /* A function that implements a task must not exit or attempt to return to\r
148         its caller as there is nothing to return to.  If a task wants to exit it\r
149         should instead call vTaskDelete( NULL ).\r
150 \r
151         Artificially force an assert() to be triggered if configASSERT() is\r
152         defined, then stop here so application writers can catch the error. */\r
153         configASSERT( uxCriticalNesting == ~0UL );\r
154         portDISABLE_INTERRUPTS();\r
155         for( ;; );\r
156 }\r
157 /*-----------------------------------------------------------*/\r
158 \r
159 /*\r
160  * See header file for description.\r
161  */\r
162 BaseType_t xPortStartScheduler( void )\r
163 {\r
164         /* Make PendSV and SysTick the lowest priority interrupts. */\r
165         *(portNVIC_SYSPRI2) |= portNVIC_PENDSV_PRI;\r
166         *(portNVIC_SYSPRI2) |= portNVIC_SYSTICK_PRI;\r
167 \r
168         /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
169         here already. */\r
170         prvSetupTimerInterrupt();\r
171 \r
172         /* Initialise the critical nesting count ready for the first task. */\r
173         uxCriticalNesting = 0;\r
174 \r
175         /* Start the first task. */\r
176         vPortStartFirstTask();\r
177 \r
178         /* Should not get here! */\r
179         return 0;\r
180 }\r
181 /*-----------------------------------------------------------*/\r
182 \r
183 void vPortEndScheduler( void )\r
184 {\r
185         /* Not implemented in ports where there is nothing to return to.\r
186         Artificially force an assert. */\r
187         configASSERT( uxCriticalNesting == 1000UL );\r
188 }\r
189 /*-----------------------------------------------------------*/\r
190 \r
191 void vPortYield( void )\r
192 {\r
193         /* Set a PendSV to request a context switch. */\r
194         *(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;\r
195 \r
196         /* Barriers are normally not required but do ensure the code is completely\r
197         within the specified behaviour for the architecture. */\r
198         __DSB();\r
199         __ISB();\r
200 }\r
201 /*-----------------------------------------------------------*/\r
202 \r
203 void vPortEnterCritical( void )\r
204 {\r
205         portDISABLE_INTERRUPTS();\r
206         uxCriticalNesting++;\r
207         __DSB();\r
208         __ISB();\r
209 }\r
210 /*-----------------------------------------------------------*/\r
211 \r
212 void vPortExitCritical( void )\r
213 {\r
214         configASSERT( uxCriticalNesting );\r
215         uxCriticalNesting--;\r
216         if( uxCriticalNesting == 0 )\r
217         {\r
218                 portENABLE_INTERRUPTS();\r
219         }\r
220 }\r
221 /*-----------------------------------------------------------*/\r
222 \r
223 void xPortSysTickHandler( void )\r
224 {\r
225 uint32_t ulPreviousMask;\r
226 \r
227         ulPreviousMask = portSET_INTERRUPT_MASK_FROM_ISR();\r
228         {\r
229                 /* Increment the RTOS tick. */\r
230                 if( xTaskIncrementTick() != pdFALSE )\r
231                 {\r
232                         /* Pend a context switch. */\r
233                         *(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;\r
234                 }\r
235         }\r
236         portCLEAR_INTERRUPT_MASK_FROM_ISR( ulPreviousMask );\r
237 }\r
238 /*-----------------------------------------------------------*/\r
239 \r
240 /*\r
241  * Setup the systick timer to generate the tick interrupts at the required\r
242  * frequency.\r
243  */\r
244 static void prvSetupTimerInterrupt( void )\r
245 {\r
246         /* Configure SysTick to interrupt at the requested rate. */\r
247         *(portNVIC_SYSTICK_LOAD) = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;\r
248         *(portNVIC_SYSTICK_CTRL) = portNVIC_SYSTICK_CLK | portNVIC_SYSTICK_INT | portNVIC_SYSTICK_ENABLE;\r
249 }\r
250 /*-----------------------------------------------------------*/\r
251 \r