]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/RVDS/ARM_CM0/port.c
Starting point for Keil Cortex-M0 port.
[freertos] / FreeRTOS / Source / portable / RVDS / 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 /* Constants used with memory barrier intrinsics. */\r
90 #define portSY_FULL_READ_WRITE          ( 15 )\r
91 \r
92 /* Each task maintains its own interrupt status in the critical nesting\r
93 variable. */\r
94 static unsigned portBASE_TYPE uxCriticalNesting = 0xaaaaaaaa;\r
95 \r
96 /*\r
97  * Setup the timer to generate the tick interrupts.\r
98  */\r
99 static void prvSetupTimerInterrupt( void );\r
100 \r
101 /*\r
102  * Exception handlers.\r
103  */\r
104 void xPortPendSVHandler( void );\r
105 void xPortSysTickHandler( void );\r
106 void vPortSVCHandler( void );\r
107 \r
108 /*\r
109  * Start first task is a separate function so it can be tested in isolation.\r
110  */\r
111 static void vPortStartFirstTask( void );\r
112 \r
113 /*-----------------------------------------------------------*/\r
114 \r
115 /*\r
116  * See header file for description.\r
117  */\r
118 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
119 {\r
120         /* Simulate the stack frame as it would be created by a context switch\r
121         interrupt. */\r
122         pxTopOfStack--; /* Offset added to account for the way the MCU uses the stack on entry/exit of interrupts. */\r
123         *pxTopOfStack = portINITIAL_XPSR;       /* xPSR */\r
124         pxTopOfStack--;\r
125         *pxTopOfStack = ( portSTACK_TYPE ) pxCode;      /* PC */\r
126         pxTopOfStack -= 6;      /* LR, R12, R3..R1 */\r
127         *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;        /* R0 */\r
128         pxTopOfStack -= 8; /* R11..R4. */\r
129 \r
130         return pxTopOfStack;\r
131 }\r
132 /*-----------------------------------------------------------*/\r
133 \r
134 __asm void vPortSVCHandler( void )\r
135 {\r
136         extern pxCurrentTCB;\r
137 \r
138         PRESERVE8\r
139 \r
140         ldr     r3, =pxCurrentTCB       /* Restore the context. */\r
141         ldr r1, [r3]                    /* Use pxCurrentTCBConst to get the pxCurrentTCB address. */\r
142         ldr r0, [r1]                    /* The first item in pxCurrentTCB is the task top of stack. */\r
143         adds r0, #16                    /* Move to the high registers. */\r
144         ldmia r0!, {r4-r7}              /* Pop the high registers. */\r
145         mov r8, r4\r
146         mov r9, r5\r
147         mov r10, r6\r
148         mov r11, r7\r
149 \r
150         msr psp, r0                             /* Remember the new top of stack for the task. */\r
151 \r
152         subs r0, #32                    /* Go back for the low registers that are not automatically restored. */\r
153         ldmia r0!, {r4-r7}      /* Pop low registers.  */\r
154         mov r1, r14                             /* OR R14 with 0x0d. */\r
155         movs r0, #0x0d\r
156         orrs r1, r0\r
157         bx r1\r
158         nop\r
159 }\r
160 /*-----------------------------------------------------------*/\r
161 \r
162 __asm void vPortStartFirstTask( void )\r
163 {\r
164         PRESERVE8\r
165 \r
166         movs r0, #0x00  /* Locate the top of stack. */\r
167         ldr r0, [r0]\r
168         msr msp, r0             /* Set the msp back to the start of the stack. */\r
169         cpsie i                 /* Globally enable interrupts. */\r
170         svc 0                   /* System call to start first task. */\r
171         nop\r
172 }\r
173 /*-----------------------------------------------------------*/\r
174 \r
175 /*\r
176  * See header file for description.\r
177  */\r
178 portBASE_TYPE xPortStartScheduler( void )\r
179 {\r
180         /* Make PendSV, CallSV and SysTick the same priroity as the kernel. */\r
181         *(portNVIC_SYSPRI2) |= portNVIC_PENDSV_PRI;\r
182         *(portNVIC_SYSPRI2) |= portNVIC_SYSTICK_PRI;\r
183 \r
184         /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
185         here already. */\r
186         prvSetupTimerInterrupt();\r
187 \r
188         /* Initialise the critical nesting count ready for the first task. */\r
189         uxCriticalNesting = 0;\r
190 \r
191         /* Start the first task. */\r
192         vPortStartFirstTask();\r
193 \r
194         /* Should not get here! */\r
195         return 0;\r
196 }\r
197 /*-----------------------------------------------------------*/\r
198 \r
199 void vPortEndScheduler( void )\r
200 {\r
201   /* It is unlikely that the CM0 port will require this function as there\r
202     is nothing to return to.  */\r
203 }\r
204 /*-----------------------------------------------------------*/\r
205 \r
206 void vPortYield( void )\r
207 {\r
208         /* Set a PendSV to request a context switch. */\r
209         *( portNVIC_INT_CTRL ) = portNVIC_PENDSVSET;\r
210 \r
211         /* Barriers are normally not required but do ensure the code is completely\r
212         within the specified behaviour for the architecture. */\r
213         __dsb( portSY_FULL_READ_WRITE );\r
214         __isb( portSY_FULL_READ_WRITE );\r
215 }\r
216 /*-----------------------------------------------------------*/\r
217 \r
218 void vPortEnterCritical( void )\r
219 {\r
220     portDISABLE_INTERRUPTS();\r
221     uxCriticalNesting++;\r
222         __dsb( portSY_FULL_READ_WRITE );\r
223         __isb( portSY_FULL_READ_WRITE );\r
224 }\r
225 /*-----------------------------------------------------------*/\r
226 \r
227 void vPortExitCritical( void )\r
228 {\r
229     uxCriticalNesting--;\r
230     if( uxCriticalNesting == 0 )\r
231     {\r
232         portENABLE_INTERRUPTS();\r
233     }\r
234 }\r
235 /*-----------------------------------------------------------*/\r
236 \r
237 __asm void xPortPendSVHandler( void )\r
238 {\r
239         extern vTaskSwitchContext\r
240         extern pxCurrentTCB\r
241 \r
242         PRESERVE8\r
243 \r
244         mrs r0, psp\r
245 \r
246         ldr     r3, =pxCurrentTCB       /* Get the location of the current TCB. */\r
247         ldr     r2, [r3]\r
248 \r
249         subs r0, #32                    /* Make space for the remaining low registers. */\r
250         str r0, [r2]                    /* Save the new top of stack. */\r
251         stmia r0!, {r4-r7}              /* Store the low registers that are not saved automatically. */\r
252         mov r4, r8                              /* Store the high registers. */\r
253         mov r5, r9\r
254         mov r6, r10\r
255         mov r7, r11\r
256         stmia r0!, {r4-r7}\r
257 \r
258         push {r3, r14}\r
259         cpsid i\r
260         bl vTaskSwitchContext\r
261         cpsie i\r
262         pop {r2, r3}                    /* lr goes in r3. r2 now holds tcb pointer. */\r
263 \r
264         ldr r1, [r2]\r
265         ldr r0, [r1]                    /* The first item in pxCurrentTCB is the task top of stack. */\r
266         adds r0, #16                    /* Move to the high registers. */\r
267         ldmia r0!, {r4-r7}              /* Pop the high registers. */\r
268         mov r8, r4\r
269         mov r9, r5\r
270         mov r10, r6\r
271         mov r11, r7\r
272 \r
273         msr psp, r0                             /* Remember the new top of stack for the task. */\r
274 \r
275         subs r0, #32                    /* Go back for the low registers that are not automatically restored. */\r
276         ldmia r0!, {r4-r7}      /* Pop low registers.  */\r
277 \r
278         bx r3\r
279 }\r
280 /*-----------------------------------------------------------*/\r
281 \r
282 void xPortSysTickHandler( void )\r
283 {\r
284 unsigned long ulDummy;\r
285 \r
286         ulDummy = portSET_INTERRUPT_MASK_FROM_ISR();\r
287         {\r
288                 /* Increment the RTOS tick. */\r
289                 if( xTaskIncrementTick() != pdFALSE )\r
290                 {\r
291                         /* Pend a context switch. */\r
292                         *(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;\r
293                 }\r
294         }\r
295         portCLEAR_INTERRUPT_MASK_FROM_ISR( ulDummy );\r
296 }\r
297 /*-----------------------------------------------------------*/\r
298 \r
299 /*\r
300  * Setup the systick timer to generate the tick interrupts at the required\r
301  * frequency.\r
302  */\r
303 void prvSetupTimerInterrupt( void )\r
304 {\r
305         /* Configure SysTick to interrupt at the requested rate. */\r
306         *(portNVIC_SYSTICK_LOAD) = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;\r
307         *(portNVIC_SYSTICK_CTRL) = portNVIC_SYSTICK_CLK | portNVIC_SYSTICK_INT | portNVIC_SYSTICK_ENABLE;\r
308 }\r
309 /*-----------------------------------------------------------*/\r
310 \r