]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/ARM_CM0/port.c
Clear up a few compiler warnings.
[freertos] / FreeRTOS / Source / portable / GCC / ARM_CM0 / port.c
1 /*\r
2     FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 /*-----------------------------------------------------------\r
66  * Implementation of functions defined in portable.h for the ARM CM0 port.\r
67  *----------------------------------------------------------*/\r
68 \r
69 /* Scheduler includes. */\r
70 #include "FreeRTOS.h"\r
71 #include "task.h"\r
72 \r
73 /* Constants required to manipulate the NVIC. */\r
74 #define portNVIC_SYSTICK_CTRL           ( ( volatile unsigned long *) 0xe000e010 )\r
75 #define portNVIC_SYSTICK_LOAD           ( ( volatile unsigned long *) 0xe000e014 )\r
76 #define portNVIC_INT_CTRL                       ( ( volatile unsigned long *) 0xe000ed04 )\r
77 #define portNVIC_SYSPRI2                        ( ( volatile unsigned long *) 0xe000ed20 )\r
78 #define portNVIC_SYSTICK_CLK            0x00000004\r
79 #define portNVIC_SYSTICK_INT            0x00000002\r
80 #define portNVIC_SYSTICK_ENABLE         0x00000001\r
81 #define portNVIC_PENDSVSET                      0x10000000\r
82 #define portMIN_INTERRUPT_PRIORITY      ( 255UL )\r
83 #define portNVIC_PENDSV_PRI                     ( portMIN_INTERRUPT_PRIORITY << 16UL )\r
84 #define portNVIC_SYSTICK_PRI            ( portMIN_INTERRUPT_PRIORITY << 24UL )\r
85 \r
86 /* Constants required to set up the initial stack. */\r
87 #define portINITIAL_XPSR                        ( 0x01000000 )\r
88 \r
89 /* Each task maintains its own interrupt status in the critical nesting\r
90 variable. */\r
91 static unsigned portBASE_TYPE uxCriticalNesting = 0xaaaaaaaa;\r
92 \r
93 /*\r
94  * Setup the timer to generate the tick interrupts.\r
95  */\r
96 static void prvSetupTimerInterrupt( void );\r
97 \r
98 /*\r
99  * Exception handlers.\r
100  */\r
101 void xPortPendSVHandler( void ) __attribute__ (( naked ));\r
102 void xPortSysTickHandler( void );\r
103 void vPortSVCHandler( void ) __attribute__ (( naked ));\r
104 \r
105 /*\r
106  * Start first task is a separate function so it can be tested in isolation.\r
107  */\r
108 static void vPortStartFirstTask( void ) __attribute__ (( naked ));\r
109 \r
110 /*\r
111  * Used to catch tasks that attempt to return from their implementing function.\r
112  */\r
113 static void prvTaskExitError( void );\r
114 \r
115 /*-----------------------------------------------------------*/\r
116 \r
117 /*\r
118  * See header file for description.\r
119  */\r
120 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE 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         pxTopOfStack--; /* Offset added to account for the way the MCU uses the stack on entry/exit of interrupts. */\r
125         *pxTopOfStack = portINITIAL_XPSR;       /* xPSR */\r
126         pxTopOfStack--;\r
127         *pxTopOfStack = ( portSTACK_TYPE ) pxCode;      /* PC */\r
128         pxTopOfStack--;\r
129         *pxTopOfStack = ( portSTACK_TYPE ) prvTaskExitError;    /* LR */\r
130         pxTopOfStack -= 5;      /* R12, R3, R2 and R1. */\r
131         *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;        /* R0 */\r
132         pxTopOfStack -= 8; /* R11..R4. */\r
133 \r
134         return pxTopOfStack;\r
135 }\r
136 /*-----------------------------------------------------------*/\r
137 \r
138 static void prvTaskExitError( void )\r
139 {\r
140         /* A function that implements a task must not exit or attempt to return to\r
141         its caller as there is nothing to return to.  If a task wants to exit it \r
142         should instead call vTaskDelete( NULL ).\r
143         \r
144         Artificially force an assert() to be triggered if configASSERT() is \r
145         defined, then stop here so application writers can catch the error. */\r
146         configASSERT( uxCriticalNesting == ~0UL );\r
147         portDISABLE_INTERRUPTS();       \r
148         for( ;; );\r
149 }\r
150 /*-----------------------------------------------------------*/\r
151 \r
152 void vPortSVCHandler( void )\r
153 {\r
154         __asm volatile (\r
155                                         "       ldr     r3, pxCurrentTCBConst2          \n" /* Restore the context. */\r
156                                         "       ldr r1, [r3]                                    \n" /* Use pxCurrentTCBConst to get the pxCurrentTCB address. */\r
157                                         "       ldr r0, [r1]                                    \n" /* The first item in pxCurrentTCB is the task top of stack. */\r
158                                         "       add r0, r0, #16                                 \n" /* Move to the high registers. */\r
159                                         "       ldmia r0!, {r4-r7}                              \n" /* Pop the high registers. */\r
160                                         "       mov r8, r4                                              \n"\r
161                                         "       mov r9, r5                                              \n"\r
162                                         "       mov r10, r6                                             \n"\r
163                                         "       mov r11, r7                                             \n"\r
164                                         "                                                                       \n"\r
165                                         "       msr psp, r0                                             \n" /* Remember the new top of stack for the task. */\r
166                                         "                                                                       \n"\r
167                                         "       sub r0, r0, #32                                 \n" /* Go back for the low registers that are not automatically restored. */\r
168                                         "       ldmia r0!, {r4-r7}              \n" /* Pop low registers.  */\r
169                                         "       mov r1, r14                                             \n" /* OR R14 with 0x0d. */\r
170                                         "       movs r0, #0x0d                                  \n"\r
171                                         "       orr r1, r0                                              \n"\r
172                                         "       bx r1                                                   \n"\r
173                                         "                                                                       \n"\r
174                                         "       .align 2                                                \n"\r
175                                         "pxCurrentTCBConst2: .word pxCurrentTCB \n"\r
176                                 );\r
177 }\r
178 /*-----------------------------------------------------------*/\r
179 \r
180 void vPortStartFirstTask( void )\r
181 {\r
182         /* The MSP stack is not reset as, unlike on M3/4 parts, there is no vector\r
183         table offset register that can be used to locate the initial stack value.\r
184         Not all M0 parts have the application vector table at address 0. */\r
185         __asm volatile(\r
186                                         " cpsie i                       \n" /* Globally enable interrupts. */\r
187                                         " svc 0                         \n" /* System call to start first task. */\r
188                                         " nop                           \n"\r
189                                   );\r
190 }\r
191 /*-----------------------------------------------------------*/\r
192 \r
193 /*\r
194  * See header file for description.\r
195  */\r
196 portBASE_TYPE xPortStartScheduler( void )\r
197 {\r
198         /* Make PendSV, CallSV and SysTick the same priroity as the kernel. */\r
199         *(portNVIC_SYSPRI2) |= portNVIC_PENDSV_PRI;\r
200         *(portNVIC_SYSPRI2) |= portNVIC_SYSTICK_PRI;\r
201 \r
202         /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
203         here already. */\r
204         prvSetupTimerInterrupt();\r
205 \r
206         /* Initialise the critical nesting count ready for the first task. */\r
207         uxCriticalNesting = 0;\r
208 \r
209         /* Start the first task. */\r
210         vPortStartFirstTask();\r
211 \r
212         /* Should not get here! */\r
213         return 0;\r
214 }\r
215 /*-----------------------------------------------------------*/\r
216 \r
217 void vPortEndScheduler( void )\r
218 {\r
219   /* It is unlikely that the CM0 port will require this function as there\r
220     is nothing to return to.  */\r
221 }\r
222 /*-----------------------------------------------------------*/\r
223 \r
224 void vPortYield( void )\r
225 {\r
226         /* Set a PendSV to request a context switch. */\r
227         *( portNVIC_INT_CTRL ) = portNVIC_PENDSVSET;\r
228 \r
229         /* Barriers are normally not required but do ensure the code is completely\r
230         within the specified behaviour for the architecture. */\r
231         __asm volatile( "dsb" );\r
232         __asm volatile( "isb" );\r
233 }\r
234 /*-----------------------------------------------------------*/\r
235 \r
236 void vPortEnterCritical( void )\r
237 {\r
238     portDISABLE_INTERRUPTS();\r
239     uxCriticalNesting++;\r
240         __asm volatile( "dsb" );\r
241         __asm volatile( "isb" );\r
242 }\r
243 /*-----------------------------------------------------------*/\r
244 \r
245 void vPortExitCritical( void )\r
246 {\r
247     uxCriticalNesting--;\r
248     if( uxCriticalNesting == 0 )\r
249     {\r
250         portENABLE_INTERRUPTS();\r
251     }\r
252 }\r
253 /*-----------------------------------------------------------*/\r
254 \r
255 unsigned long ulSetInterruptMaskFromISR( void )\r
256 {\r
257         __asm volatile(\r
258                                         " mrs r0, PRIMASK       \n"\r
259                                         " cpsid i                       \n"\r
260                                         " bx lr                           "\r
261                                   );\r
262 \r
263         /* To avoid compiler warnings.  This line will never be reached. */\r
264         return 0;\r
265 }\r
266 /*-----------------------------------------------------------*/\r
267 \r
268 void vClearInterruptMaskFromISR( unsigned long ulMask )\r
269 {\r
270         __asm volatile(\r
271                                         " msr PRIMASK, r0       \n"\r
272                                         " bx lr                           "\r
273                                   );\r
274                                   \r
275         /* Just to avoid compiler warning. */\r
276         ( void ) ulMask;\r
277 }\r
278 /*-----------------------------------------------------------*/\r
279 \r
280 void xPortPendSVHandler( void )\r
281 {\r
282         /* This is a naked function. */\r
283 \r
284         __asm volatile\r
285         (\r
286         "       mrs r0, psp                                                     \n"\r
287         "                                                                               \n"\r
288         "       ldr     r3, pxCurrentTCBConst                   \n" /* Get the location of the current TCB. */\r
289         "       ldr     r2, [r3]                                                \n"\r
290         "                                                                               \n"\r
291         "       sub r0, r0, #32                                         \n" /* Make space for the remaining low registers. */\r
292         "       str r0, [r2]                                            \n" /* Save the new top of stack. */\r
293         "       stmia r0!, {r4-r7}                                      \n" /* Store the low registers that are not saved automatically. */\r
294         "       mov r4, r8                                                      \n" /* Store the high registers. */\r
295         "       mov r5, r9                                                      \n"\r
296         "       mov r6, r10                                                     \n"\r
297         "       mov r7, r11                                                     \n"\r
298         "       stmia r0!, {r4-r7}                      \n"\r
299         "                                                                               \n"\r
300         "       push {r3, r14}                                          \n"\r
301         "       cpsid i                                                         \n"\r
302         "       bl vTaskSwitchContext                           \n"\r
303         "       cpsie i                                                         \n"\r
304         "       pop {r2, r3}                                            \n" /* lr goes in r3. r2 now holds tcb pointer. */\r
305         "                                                                               \n"\r
306         "       ldr r1, [r2]                                            \n"\r
307         "       ldr r0, [r1]                                            \n" /* The first item in pxCurrentTCB is the task top of stack. */\r
308         "       add r0, r0, #16                                         \n" /* Move to the high registers. */\r
309         "       ldmia r0!, {r4-r7}                                      \n" /* Pop the high registers. */\r
310         "       mov r8, r4                                                      \n"\r
311         "       mov r9, r5                                                      \n"\r
312         "       mov r10, r6                                                     \n"\r
313         "       mov r11, r7                                                     \n"\r
314         "                                                                               \n"\r
315         "       msr psp, r0                                                     \n" /* Remember the new top of stack for the task. */\r
316         "                                                                               \n"\r
317         "       sub r0, r0, #32                                         \n" /* Go back for the low registers that are not automatically restored. */\r
318         "       ldmia r0!, {r4-r7}                      \n" /* Pop low registers.  */\r
319         "                                                                               \n"\r
320         "       bx r3                                                           \n"\r
321         "                                                                               \n"\r
322         "       .align 2                                                        \n"\r
323         "pxCurrentTCBConst: .word pxCurrentTCB    "\r
324         );\r
325 }\r
326 /*-----------------------------------------------------------*/\r
327 \r
328 void xPortSysTickHandler( void )\r
329 {\r
330 unsigned long ulPreviousMask;\r
331 \r
332         ulPreviousMask = portSET_INTERRUPT_MASK_FROM_ISR();\r
333         {\r
334                 /* Increment the RTOS tick. */\r
335                 if( xTaskIncrementTick() != pdFALSE )\r
336                 {\r
337                         /* Pend a context switch. */\r
338                         *(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;\r
339                 }\r
340         }\r
341         portCLEAR_INTERRUPT_MASK_FROM_ISR( ulPreviousMask );\r
342 }\r
343 /*-----------------------------------------------------------*/\r
344 \r
345 /*\r
346  * Setup the systick timer to generate the tick interrupts at the required\r
347  * frequency.\r
348  */\r
349 void prvSetupTimerInterrupt( void )\r
350 {\r
351         /* Configure SysTick to interrupt at the requested rate. */\r
352         *(portNVIC_SYSTICK_LOAD) = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;\r
353         *(portNVIC_SYSTICK_CTRL) = portNVIC_SYSTICK_CLK | portNVIC_SYSTICK_INT | portNVIC_SYSTICK_ENABLE;\r
354 }\r
355 /*-----------------------------------------------------------*/\r
356 \r