]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/ARM_CM0/port.c
Update FreeRTOS version number to V7.5.3
[freertos] / FreeRTOS / Source / portable / GCC / ARM_CM0 / port.c
1 /*\r
2     FreeRTOS V7.5.3 - Copyright (C) 2013 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 distribute\r
28     >>! a combined work that includes FreeRTOS without being obliged to provide\r
29     >>! the source code for proprietary components outside of the FreeRTOS\r
30     >>! 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 /* Scheduler includes. */\r
71 #include "FreeRTOS.h"\r
72 #include "task.h"\r
73 \r
74 /* Constants required to manipulate the NVIC. */\r
75 #define portNVIC_SYSTICK_CTRL           ( ( volatile unsigned long *) 0xe000e010 )\r
76 #define portNVIC_SYSTICK_LOAD           ( ( volatile unsigned long *) 0xe000e014 )\r
77 #define portNVIC_INT_CTRL                       ( ( volatile unsigned long *) 0xe000ed04 )\r
78 #define portNVIC_SYSPRI2                        ( ( volatile unsigned long *) 0xe000ed20 )\r
79 #define portNVIC_SYSTICK_CLK            0x00000004\r
80 #define portNVIC_SYSTICK_INT            0x00000002\r
81 #define portNVIC_SYSTICK_ENABLE         0x00000001\r
82 #define portNVIC_PENDSVSET                      0x10000000\r
83 #define portMIN_INTERRUPT_PRIORITY      ( 255UL )\r
84 #define portNVIC_PENDSV_PRI                     ( portMIN_INTERRUPT_PRIORITY << 16UL )\r
85 #define portNVIC_SYSTICK_PRI            ( portMIN_INTERRUPT_PRIORITY << 24UL )\r
86 \r
87 /* Constants required to set up the initial stack. */\r
88 #define portINITIAL_XPSR                        ( 0x01000000 )\r
89 \r
90 /* Let the user override the pre-loading of the initial LR with the address of\r
91 prvTaskExitError() in case is messes up unwinding of the stack in the\r
92 debugger. */\r
93 #ifdef configTASK_RETURN_ADDRESS\r
94         #define portTASK_RETURN_ADDRESS configTASK_RETURN_ADDRESS\r
95 #else\r
96         #define portTASK_RETURN_ADDRESS prvTaskExitError\r
97 #endif\r
98 \r
99 /* Each task maintains its own interrupt status in the critical nesting\r
100 variable. */\r
101 static unsigned portBASE_TYPE uxCriticalNesting = 0xaaaaaaaa;\r
102 \r
103 /*\r
104  * Setup the timer to generate the tick interrupts.\r
105  */\r
106 static void prvSetupTimerInterrupt( void );\r
107 \r
108 /*\r
109  * Exception handlers.\r
110  */\r
111 void xPortPendSVHandler( void ) __attribute__ (( naked ));\r
112 void xPortSysTickHandler( void );\r
113 void vPortSVCHandler( void ) __attribute__ (( naked ));\r
114 \r
115 /*\r
116  * Start first task is a separate function so it can be tested in isolation.\r
117  */\r
118 static void vPortStartFirstTask( void ) __attribute__ (( naked ));\r
119 \r
120 /*\r
121  * Used to catch tasks that attempt to return from their implementing function.\r
122  */\r
123 static void prvTaskExitError( void );\r
124 \r
125 /*-----------------------------------------------------------*/\r
126 \r
127 /*\r
128  * See header file for description.\r
129  */\r
130 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
131 {\r
132         /* Simulate the stack frame as it would be created by a context switch\r
133         interrupt. */\r
134         pxTopOfStack--; /* Offset added to account for the way the MCU uses the stack on entry/exit of interrupts. */\r
135         *pxTopOfStack = portINITIAL_XPSR;       /* xPSR */\r
136         pxTopOfStack--;\r
137         *pxTopOfStack = ( portSTACK_TYPE ) pxCode;      /* PC */\r
138         pxTopOfStack--;\r
139         *pxTopOfStack = ( portSTACK_TYPE ) portTASK_RETURN_ADDRESS;     /* LR */\r
140         pxTopOfStack -= 5;      /* R12, R3, R2 and R1. */\r
141         *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;        /* R0 */\r
142         pxTopOfStack -= 8; /* R11..R4. */\r
143 \r
144         return pxTopOfStack;\r
145 }\r
146 /*-----------------------------------------------------------*/\r
147 \r
148 static void prvTaskExitError( void )\r
149 {\r
150         /* A function that implements a task must not exit or attempt to return to\r
151         its caller as there is nothing to return to.  If a task wants to exit it \r
152         should instead call vTaskDelete( NULL ).\r
153         \r
154         Artificially force an assert() to be triggered if configASSERT() is \r
155         defined, then stop here so application writers can catch the error. */\r
156         configASSERT( uxCriticalNesting == ~0UL );\r
157         portDISABLE_INTERRUPTS();       \r
158         for( ;; );\r
159 }\r
160 /*-----------------------------------------------------------*/\r
161 \r
162 void vPortSVCHandler( void )\r
163 {\r
164         __asm volatile (\r
165                                         "       ldr     r3, pxCurrentTCBConst2          \n" /* Restore the context. */\r
166                                         "       ldr r1, [r3]                                    \n" /* Use pxCurrentTCBConst to get the pxCurrentTCB address. */\r
167                                         "       ldr r0, [r1]                                    \n" /* The first item in pxCurrentTCB is the task top of stack. */\r
168                                         "       add r0, r0, #16                                 \n" /* Move to the high registers. */\r
169                                         "       ldmia r0!, {r4-r7}                              \n" /* Pop the high registers. */\r
170                                         "       mov r8, r4                                              \n"\r
171                                         "       mov r9, r5                                              \n"\r
172                                         "       mov r10, r6                                             \n"\r
173                                         "       mov r11, r7                                             \n"\r
174                                         "                                                                       \n"\r
175                                         "       msr psp, r0                                             \n" /* Remember the new top of stack for the task. */\r
176                                         "                                                                       \n"\r
177                                         "       sub r0, r0, #32                                 \n" /* Go back for the low registers that are not automatically restored. */\r
178                                         "       ldmia r0!, {r4-r7}              \n" /* Pop low registers.  */\r
179                                         "       mov r1, r14                                             \n" /* OR R14 with 0x0d. */\r
180                                         "       movs r0, #0x0d                                  \n"\r
181                                         "       orr r1, r0                                              \n"\r
182                                         "       bx r1                                                   \n"\r
183                                         "                                                                       \n"\r
184                                         "       .align 2                                                \n"\r
185                                         "pxCurrentTCBConst2: .word pxCurrentTCB \n"\r
186                                 );\r
187 }\r
188 /*-----------------------------------------------------------*/\r
189 \r
190 void vPortStartFirstTask( void )\r
191 {\r
192         /* The MSP stack is not reset as, unlike on M3/4 parts, there is no vector\r
193         table offset register that can be used to locate the initial stack value.\r
194         Not all M0 parts have the application vector table at address 0. */\r
195         __asm volatile(\r
196                                         " cpsie i                       \n" /* Globally enable interrupts. */\r
197                                         " svc 0                         \n" /* System call to start first task. */\r
198                                         " nop                           \n"\r
199                                   );\r
200 }\r
201 /*-----------------------------------------------------------*/\r
202 \r
203 /*\r
204  * See header file for description.\r
205  */\r
206 portBASE_TYPE xPortStartScheduler( void )\r
207 {\r
208         /* Make PendSV, CallSV and SysTick the same priroity as the kernel. */\r
209         *(portNVIC_SYSPRI2) |= portNVIC_PENDSV_PRI;\r
210         *(portNVIC_SYSPRI2) |= portNVIC_SYSTICK_PRI;\r
211 \r
212         /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
213         here already. */\r
214         prvSetupTimerInterrupt();\r
215 \r
216         /* Initialise the critical nesting count ready for the first task. */\r
217         uxCriticalNesting = 0;\r
218 \r
219         /* Start the first task. */\r
220         vPortStartFirstTask();\r
221 \r
222         /* Should not get here! */\r
223         return 0;\r
224 }\r
225 /*-----------------------------------------------------------*/\r
226 \r
227 void vPortEndScheduler( void )\r
228 {\r
229   /* It is unlikely that the CM0 port will require this function as there\r
230     is nothing to return to.  */\r
231 }\r
232 /*-----------------------------------------------------------*/\r
233 \r
234 void vPortYield( void )\r
235 {\r
236         /* Set a PendSV to request a context switch. */\r
237         *( portNVIC_INT_CTRL ) = portNVIC_PENDSVSET;\r
238 \r
239         /* Barriers are normally not required but do ensure the code is completely\r
240         within the specified behaviour for the architecture. */\r
241         __asm volatile( "dsb" );\r
242         __asm volatile( "isb" );\r
243 }\r
244 /*-----------------------------------------------------------*/\r
245 \r
246 void vPortEnterCritical( void )\r
247 {\r
248     portDISABLE_INTERRUPTS();\r
249     uxCriticalNesting++;\r
250         __asm volatile( "dsb" );\r
251         __asm volatile( "isb" );\r
252 }\r
253 /*-----------------------------------------------------------*/\r
254 \r
255 void vPortExitCritical( void )\r
256 {\r
257     uxCriticalNesting--;\r
258     if( uxCriticalNesting == 0 )\r
259     {\r
260         portENABLE_INTERRUPTS();\r
261     }\r
262 }\r
263 /*-----------------------------------------------------------*/\r
264 \r
265 unsigned long ulSetInterruptMaskFromISR( void )\r
266 {\r
267         __asm volatile(\r
268                                         " mrs r0, PRIMASK       \n"\r
269                                         " cpsid i                       \n"\r
270                                         " bx lr                           "\r
271                                   );\r
272 \r
273         /* To avoid compiler warnings.  This line will never be reached. */\r
274         return 0;\r
275 }\r
276 /*-----------------------------------------------------------*/\r
277 \r
278 void vClearInterruptMaskFromISR( unsigned long ulMask )\r
279 {\r
280         __asm volatile(\r
281                                         " msr PRIMASK, r0       \n"\r
282                                         " bx lr                           "\r
283                                   );\r
284                                   \r
285         /* Just to avoid compiler warning. */\r
286         ( void ) ulMask;\r
287 }\r
288 /*-----------------------------------------------------------*/\r
289 \r
290 void xPortPendSVHandler( void )\r
291 {\r
292         /* This is a naked function. */\r
293 \r
294         __asm volatile\r
295         (\r
296         "       mrs r0, psp                                                     \n"\r
297         "                                                                               \n"\r
298         "       ldr     r3, pxCurrentTCBConst                   \n" /* Get the location of the current TCB. */\r
299         "       ldr     r2, [r3]                                                \n"\r
300         "                                                                               \n"\r
301         "       sub r0, r0, #32                                         \n" /* Make space for the remaining low registers. */\r
302         "       str r0, [r2]                                            \n" /* Save the new top of stack. */\r
303         "       stmia r0!, {r4-r7}                                      \n" /* Store the low registers that are not saved automatically. */\r
304         "       mov r4, r8                                                      \n" /* Store the high registers. */\r
305         "       mov r5, r9                                                      \n"\r
306         "       mov r6, r10                                                     \n"\r
307         "       mov r7, r11                                                     \n"\r
308         "       stmia r0!, {r4-r7}                      \n"\r
309         "                                                                               \n"\r
310         "       push {r3, r14}                                          \n"\r
311         "       cpsid i                                                         \n"\r
312         "       bl vTaskSwitchContext                           \n"\r
313         "       cpsie i                                                         \n"\r
314         "       pop {r2, r3}                                            \n" /* lr goes in r3. r2 now holds tcb pointer. */\r
315         "                                                                               \n"\r
316         "       ldr r1, [r2]                                            \n"\r
317         "       ldr r0, [r1]                                            \n" /* The first item in pxCurrentTCB is the task top of stack. */\r
318         "       add r0, r0, #16                                         \n" /* Move to the high registers. */\r
319         "       ldmia r0!, {r4-r7}                                      \n" /* Pop the high registers. */\r
320         "       mov r8, r4                                                      \n"\r
321         "       mov r9, r5                                                      \n"\r
322         "       mov r10, r6                                                     \n"\r
323         "       mov r11, r7                                                     \n"\r
324         "                                                                               \n"\r
325         "       msr psp, r0                                                     \n" /* Remember the new top of stack for the task. */\r
326         "                                                                               \n"\r
327         "       sub r0, r0, #32                                         \n" /* Go back for the low registers that are not automatically restored. */\r
328         "       ldmia r0!, {r4-r7}                      \n" /* Pop low registers.  */\r
329         "                                                                               \n"\r
330         "       bx r3                                                           \n"\r
331         "                                                                               \n"\r
332         "       .align 2                                                        \n"\r
333         "pxCurrentTCBConst: .word pxCurrentTCB    "\r
334         );\r
335 }\r
336 /*-----------------------------------------------------------*/\r
337 \r
338 void xPortSysTickHandler( void )\r
339 {\r
340 unsigned long ulPreviousMask;\r
341 \r
342         ulPreviousMask = portSET_INTERRUPT_MASK_FROM_ISR();\r
343         {\r
344                 /* Increment the RTOS tick. */\r
345                 if( xTaskIncrementTick() != pdFALSE )\r
346                 {\r
347                         /* Pend a context switch. */\r
348                         *(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;\r
349                 }\r
350         }\r
351         portCLEAR_INTERRUPT_MASK_FROM_ISR( ulPreviousMask );\r
352 }\r
353 /*-----------------------------------------------------------*/\r
354 \r
355 /*\r
356  * Setup the systick timer to generate the tick interrupts at the required\r
357  * frequency.\r
358  */\r
359 void prvSetupTimerInterrupt( void )\r
360 {\r
361         /* Configure SysTick to interrupt at the requested rate. */\r
362         *(portNVIC_SYSTICK_LOAD) = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;\r
363         *(portNVIC_SYSTICK_CTRL) = portNVIC_SYSTICK_CLK | portNVIC_SYSTICK_INT | portNVIC_SYSTICK_ENABLE;\r
364 }\r
365 /*-----------------------------------------------------------*/\r
366 \r