]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/Tasking/ARM_CM4F/port.c
Update version number ready to release the FAT file system demo.
[freertos] / FreeRTOS / Source / portable / Tasking / ARM_CM4F / port.c
1 /*\r
2     FreeRTOS V7.4.2 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT\r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not it can be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
67     Integrity Systems, who sell the code with commercial support,\r
68     indemnification and middleware, under the OpenRTOS brand.\r
69 \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
71     engineered and independently SIL3 certified version for use in safety and\r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 /*-----------------------------------------------------------\r
76  * Implementation of functions defined in portable.h for the ARM CM4F port.\r
77  *----------------------------------------------------------*/\r
78 \r
79 /* Scheduler includes. */\r
80 #include "FreeRTOS.h"\r
81 #include "task.h"\r
82 \r
83 /* Constants required to manipulate the NVIC. */\r
84 #define portNVIC_SYSTICK_CTRL           ( ( volatile unsigned long * ) 0xe000e010 )\r
85 #define portNVIC_SYSTICK_LOAD           ( ( volatile unsigned long * ) 0xe000e014 )\r
86 #define portNVIC_SYSPRI2                        ( ( volatile unsigned long * ) 0xe000ed20 )\r
87 #define portNVIC_SYSTICK_CLK            0x00000004\r
88 #define portNVIC_SYSTICK_INT            0x00000002\r
89 #define portNVIC_SYSTICK_ENABLE         0x00000001\r
90 #define portNVIC_PENDSV_PRI                     ( ( ( unsigned long ) configKERNEL_INTERRUPT_PRIORITY ) << 16 )\r
91 #define portNVIC_SYSTICK_PRI            ( ( ( unsigned long ) configKERNEL_INTERRUPT_PRIORITY ) << 24 )\r
92 \r
93 /* Constants required to manipulate the VFP. */\r
94 #define portFPCCR                                       ( ( volatile unsigned long * ) 0xe000ef34 ) /* Floating point context control register. */\r
95 #define portASPEN_AND_LSPEN_BITS        ( 0x3UL << 30UL )\r
96 \r
97 /* Constants required to set up the initial stack. */\r
98 #define portINITIAL_XPSR                        ( 0x01000000 )\r
99 #define portINITIAL_EXEC_RETURN         ( 0xfffffffd )\r
100 \r
101 /* The priority used by the kernel is assigned to a variable to make access\r
102 from inline assembler easier. */\r
103 const unsigned long ulKernelPriority = configKERNEL_INTERRUPT_PRIORITY;\r
104 \r
105 /* Each task maintains its own interrupt status in the critical nesting\r
106 variable. */\r
107 static unsigned long ulCriticalNesting = 0xaaaaaaaaUL;\r
108 \r
109 /*\r
110  * Setup the timer to generate the tick interrupts.\r
111  */\r
112 static void prvSetupTimerInterrupt( void );\r
113 \r
114 /*\r
115  * Exception handlers.\r
116  */\r
117 void SysTick_Handler( void );\r
118 \r
119 /*\r
120  * Functions defined in port_asm.asm.\r
121  */\r
122 extern void vPortEnableVFP( void );\r
123 extern void vPortStartFirstTask( void );\r
124 \r
125 /* This exists purely to allow the const to be used from within the\r
126 port_asm.asm assembly file. */\r
127 const unsigned long ulMaxSyscallInterruptPriorityConst = configMAX_SYSCALL_INTERRUPT_PRIORITY;\r
128 \r
129 /*-----------------------------------------------------------*/\r
130 \r
131 /*\r
132  * See header file for description.\r
133  */\r
134 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
135 {\r
136         /* Simulate the stack frame as it would be created by a context switch\r
137         interrupt. */\r
138 \r
139         /* Offset added to account for the way the MCU uses the stack on entry/exit\r
140         of interrupts, and to ensure alignment. */\r
141         pxTopOfStack--;\r
142 \r
143         *pxTopOfStack = portINITIAL_XPSR;       /* xPSR */\r
144         pxTopOfStack--;\r
145         *pxTopOfStack = ( portSTACK_TYPE ) pxCode;      /* PC */\r
146         pxTopOfStack--;\r
147         *pxTopOfStack = 0;      /* LR */\r
148 \r
149         /* Save code space by skipping register initialisation. */\r
150         pxTopOfStack -= 5;      /* R12, R3, R2 and R1. */\r
151         *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;        /* R0 */\r
152 \r
153         /* A save method is being used that requires each task to maintain its\r
154         own exec return value. */\r
155         pxTopOfStack--;\r
156         *pxTopOfStack = portINITIAL_EXEC_RETURN;\r
157 \r
158         pxTopOfStack -= 8;      /* R11, R10, R9, R8, R7, R6, R5 and R4. */\r
159 \r
160         return pxTopOfStack;\r
161 }\r
162 /*-----------------------------------------------------------*/\r
163 \r
164 /*\r
165  * See header file for description.\r
166  */\r
167 portBASE_TYPE xPortStartScheduler( void )\r
168 {\r
169         /* configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to 0.\r
170         See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */\r
171         configASSERT( ( configMAX_SYSCALL_INTERRUPT_PRIORITY ) );\r
172 \r
173         /* Make PendSV and SysTick the lowest priority interrupts. */\r
174         *(portNVIC_SYSPRI2) |= portNVIC_PENDSV_PRI;\r
175         *(portNVIC_SYSPRI2) |= portNVIC_SYSTICK_PRI;\r
176 \r
177         /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
178         here already. */\r
179         prvSetupTimerInterrupt();\r
180 \r
181         /* Initialise the critical nesting count ready for the first task. */\r
182         ulCriticalNesting = 0;\r
183 \r
184         /* Ensure the VFP is enabled - it should be anyway. */\r
185         vPortEnableVFP();\r
186 \r
187         /* Lazy save always. */\r
188         *( portFPCCR ) |= portASPEN_AND_LSPEN_BITS;\r
189 \r
190         /* Start the first task. */\r
191         vPortStartFirstTask();\r
192 \r
193         /* Should not get here! */\r
194         return 0;\r
195 }\r
196 /*-----------------------------------------------------------*/\r
197 \r
198 void vPortEndScheduler( void )\r
199 {\r
200         /* It is unlikely that the CM4F port will require this function as there\r
201         is nothing to return to.  */\r
202 }\r
203 /*-----------------------------------------------------------*/\r
204 \r
205 void vPortYield( void )\r
206 {\r
207         /* Set a PendSV to request a context switch. */\r
208         *(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;\r
209 \r
210         /* Barriers are normally not required but do ensure the code is completely\r
211         within the specified behaviour for the architecture. */\r
212         __DSB();\r
213         __ISB();\r
214 }\r
215 /*-----------------------------------------------------------*/\r
216 \r
217 void vPortEnterCritical( void )\r
218 {\r
219         portDISABLE_INTERRUPTS();\r
220         ulCriticalNesting++;\r
221         __DSB();\r
222         __ISB();\r
223 }\r
224 /*-----------------------------------------------------------*/\r
225 \r
226 void vPortExitCritical( void )\r
227 {\r
228         ulCriticalNesting--;\r
229         if( ulCriticalNesting == 0 )\r
230         {\r
231                 portENABLE_INTERRUPTS();\r
232         }\r
233 }\r
234 /*-----------------------------------------------------------*/\r
235 \r
236 void SysTick_Handler( void )\r
237 {\r
238 unsigned long ulDummy;\r
239 \r
240         /* If using preemption, also force a context switch. */\r
241         #if configUSE_PREEMPTION == 1\r
242                 *(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;\r
243         #endif\r
244 \r
245         ulDummy = portSET_INTERRUPT_MASK_FROM_ISR();\r
246         {\r
247                 vTaskIncrementTick();\r
248         }\r
249         portCLEAR_INTERRUPT_MASK_FROM_ISR( ulDummy );\r
250 }\r
251 /*-----------------------------------------------------------*/\r
252 \r
253 /*\r
254  * Setup the systick timer to generate the tick interrupts at the required\r
255  * frequency.\r
256  */\r
257 void prvSetupTimerInterrupt( void )\r
258 {\r
259         /* Configure SysTick to interrupt at the requested rate. */\r
260         *(portNVIC_SYSTICK_LOAD) = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;\r
261         *(portNVIC_SYSTICK_CTRL) = portNVIC_SYSTICK_CLK | portNVIC_SYSTICK_INT | portNVIC_SYSTICK_ENABLE;\r
262 }\r
263 /*-----------------------------------------------------------*/\r
264 \r