]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/Tasking/ARM_CM4F/port.c
Update version numbers in preparation for V8.2.0 release candidate 1.
[freertos] / FreeRTOS / Source / portable / Tasking / ARM_CM4F / port.c
1 /*\r
2     FreeRTOS V8.2.0rc1 - 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     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
12 \r
13     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
14     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
15     >>!   obliged to provide the source code for proprietary components     !<<\r
16     >>!   outside of the FreeRTOS kernel.                                   !<<\r
17 \r
18     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
19     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
20     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
21     link: http://www.freertos.org/a00114.html\r
22 \r
23     1 tab == 4 spaces!\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    Having a problem?  Start by reading the FAQ "My application does   *\r
28      *    not run, what could be wrong?".  Have you defined configASSERT()?  *\r
29      *                                                                       *\r
30      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
31      *                                                                       *\r
32     ***************************************************************************\r
33 \r
34     ***************************************************************************\r
35      *                                                                       *\r
36      *    FreeRTOS provides completely free yet professionally developed,    *\r
37      *    robust, strictly quality controlled, supported, and cross          *\r
38      *    platform software that is more than just the market leader, it     *\r
39      *    is the industry's de facto standard.                               *\r
40      *                                                                       *\r
41      *    Help yourself get started quickly while simultaneously helping     *\r
42      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
43      *    tutorial book, reference manual, or both:                          *\r
44      *    http://www.FreeRTOS.org/Documentation                              *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     ***************************************************************************\r
49      *                                                                       *\r
50      *   Investing in training allows your team to be as productive as       *\r
51      *   possible as early as possible, lowering your overall development    *\r
52      *   cost, and enabling you to bring a more robust product to market     *\r
53      *   earlier than would otherwise be possible.  Richard Barry is both    *\r
54      *   the architect and key author of FreeRTOS, and so also the world's   *\r
55      *   leading authority on what is the world's most popular real time     *\r
56      *   kernel for deeply embedded MCU designs.  Obtaining your training    *\r
57      *   from Richard ensures your team will gain directly from his in-depth *\r
58      *   product knowledge and years of usage experience.  Contact Real Time *\r
59      *   Engineers Ltd to enquire about the FreeRTOS Masterclass, presented  *\r
60      *   by Richard Barry:  http://www.FreeRTOS.org/contact\r
61      *                                                                       *\r
62     ***************************************************************************\r
63 \r
64     ***************************************************************************\r
65      *                                                                       *\r
66      *    You are receiving this top quality software for free.  Please play *\r
67      *    fair and reciprocate by reporting any suspected issues and         *\r
68      *    participating in the community forum:                              *\r
69      *    http://www.FreeRTOS.org/support                                    *\r
70      *                                                                       *\r
71      *    Thank you!                                                         *\r
72      *                                                                       *\r
73     ***************************************************************************\r
74 \r
75     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
76     license and Real Time Engineers Ltd. contact details.\r
77 \r
78     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
79     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
80     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
81 \r
82     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
83     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
84 \r
85     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
86     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
87     licenses offer ticketed support, indemnification and commercial middleware.\r
88 \r
89     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
90     engineered and independently SIL3 certified version for use in safety and\r
91     mission critical applications that require provable dependability.\r
92 \r
93     1 tab == 4 spaces!\r
94 */\r
95 \r
96 /*-----------------------------------------------------------\r
97  * Implementation of functions defined in portable.h for the ARM CM4F port.\r
98  *----------------------------------------------------------*/\r
99 \r
100 /* Scheduler includes. */\r
101 #include "FreeRTOS.h"\r
102 #include "task.h"\r
103 \r
104 /* Constants required to manipulate the NVIC. */\r
105 #define portNVIC_SYSTICK_CTRL           ( ( volatile uint32_t * ) 0xe000e010 )\r
106 #define portNVIC_SYSTICK_LOAD           ( ( volatile uint32_t * ) 0xe000e014 )\r
107 #define portNVIC_SYSPRI2                        ( ( volatile uint32_t * ) 0xe000ed20 )\r
108 #define portNVIC_SYSTICK_CLK            0x00000004\r
109 #define portNVIC_SYSTICK_INT            0x00000002\r
110 #define portNVIC_SYSTICK_ENABLE         0x00000001\r
111 #define portNVIC_PENDSV_PRI                     ( ( ( uint32_t ) configKERNEL_INTERRUPT_PRIORITY ) << 16 )\r
112 #define portNVIC_SYSTICK_PRI            ( ( ( uint32_t ) configKERNEL_INTERRUPT_PRIORITY ) << 24 )\r
113 \r
114 /* Masks off all bits but the VECTACTIVE bits in the ICSR register. */\r
115 #define portVECTACTIVE_MASK                     ( 0xFFUL )\r
116 \r
117 /* Constants required to manipulate the VFP. */\r
118 #define portFPCCR                                       ( ( volatile uint32_t * ) 0xe000ef34 ) /* Floating point context control register. */\r
119 #define portASPEN_AND_LSPEN_BITS        ( 0x3UL << 30UL )\r
120 \r
121 /* Constants required to set up the initial stack. */\r
122 #define portINITIAL_XPSR                        ( 0x01000000 )\r
123 #define portINITIAL_EXEC_RETURN         ( 0xfffffffd )\r
124 \r
125 /* Let the user override the pre-loading of the initial LR with the address of\r
126 prvTaskExitError() in case is messes up unwinding of the stack in the\r
127 debugger. */\r
128 #ifdef configTASK_RETURN_ADDRESS\r
129         #define portTASK_RETURN_ADDRESS configTASK_RETURN_ADDRESS\r
130 #else\r
131         #define portTASK_RETURN_ADDRESS prvTaskExitError\r
132 #endif\r
133 \r
134 /* The priority used by the kernel is assigned to a variable to make access\r
135 from inline assembler easier. */\r
136 const uint32_t ulKernelPriority = configKERNEL_INTERRUPT_PRIORITY;\r
137 \r
138 /* Each task maintains its own interrupt status in the critical nesting\r
139 variable. */\r
140 static uint32_t ulCriticalNesting = 0xaaaaaaaaUL;\r
141 \r
142 /*\r
143  * Setup the timer to generate the tick interrupts.\r
144  */\r
145 static void prvSetupTimerInterrupt( void );\r
146 \r
147 /*\r
148  * Exception handlers.\r
149  */\r
150 void SysTick_Handler( void );\r
151 \r
152 /*\r
153  * Functions defined in port_asm.asm.\r
154  */\r
155 extern void vPortEnableVFP( void );\r
156 extern void vPortStartFirstTask( void );\r
157 \r
158 /*\r
159  * Used to catch tasks that attempt to return from their implementing function.\r
160  */\r
161 static void prvTaskExitError( void );\r
162 \r
163 /* This exists purely to allow the const to be used from within the\r
164 port_asm.asm assembly file. */\r
165 const uint32_t ulMaxSyscallInterruptPriorityConst = configMAX_SYSCALL_INTERRUPT_PRIORITY;\r
166 \r
167 /*-----------------------------------------------------------*/\r
168 \r
169 /*\r
170  * See header file for description.\r
171  */\r
172 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
173 {\r
174         /* Simulate the stack frame as it would be created by a context switch\r
175         interrupt. */\r
176 \r
177         /* Offset added to account for the way the MCU uses the stack on entry/exit\r
178         of interrupts, and to ensure alignment. */\r
179         pxTopOfStack--;\r
180 \r
181         *pxTopOfStack = portINITIAL_XPSR;       /* xPSR */\r
182         pxTopOfStack--;\r
183         *pxTopOfStack = ( StackType_t ) pxCode; /* PC */\r
184         pxTopOfStack--;\r
185         *pxTopOfStack = ( StackType_t ) portTASK_RETURN_ADDRESS;        /* LR */\r
186 \r
187         /* Save code space by skipping register initialisation. */\r
188         pxTopOfStack -= 5;      /* R12, R3, R2 and R1. */\r
189         *pxTopOfStack = ( StackType_t ) pvParameters;   /* R0 */\r
190 \r
191         /* A save method is being used that requires each task to maintain its\r
192         own exec return value. */\r
193         pxTopOfStack--;\r
194         *pxTopOfStack = portINITIAL_EXEC_RETURN;\r
195 \r
196         pxTopOfStack -= 8;      /* R11, R10, R9, R8, R7, R6, R5 and R4. */\r
197 \r
198         return pxTopOfStack;\r
199 }\r
200 /*-----------------------------------------------------------*/\r
201 \r
202 static void prvTaskExitError( void )\r
203 {\r
204         /* A function that implements a task must not exit or attempt to return to\r
205         its caller as there is nothing to return to.  If a task wants to exit it\r
206         should instead call vTaskDelete( NULL ).\r
207 \r
208         Artificially force an assert() to be triggered if configASSERT() is\r
209         defined, then stop here so application writers can catch the error. */\r
210         configASSERT( ulCriticalNesting == ~0UL );\r
211         portDISABLE_INTERRUPTS();\r
212         for( ;; );\r
213 }\r
214 /*-----------------------------------------------------------*/\r
215 \r
216 /*\r
217  * See header file for description.\r
218  */\r
219 BaseType_t xPortStartScheduler( void )\r
220 {\r
221         /* configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to 0.\r
222         See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */\r
223         configASSERT( ( configMAX_SYSCALL_INTERRUPT_PRIORITY ) );\r
224 \r
225         /* Make PendSV and SysTick the lowest priority interrupts. */\r
226         *(portNVIC_SYSPRI2) |= portNVIC_PENDSV_PRI;\r
227         *(portNVIC_SYSPRI2) |= portNVIC_SYSTICK_PRI;\r
228 \r
229         /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
230         here already. */\r
231         prvSetupTimerInterrupt();\r
232 \r
233         /* Initialise the critical nesting count ready for the first task. */\r
234         ulCriticalNesting = 0;\r
235 \r
236         /* Ensure the VFP is enabled - it should be anyway. */\r
237         vPortEnableVFP();\r
238 \r
239         /* Lazy save always. */\r
240         *( portFPCCR ) |= portASPEN_AND_LSPEN_BITS;\r
241 \r
242         /* Start the first task. */\r
243         vPortStartFirstTask();\r
244 \r
245         /* Should not get here! */\r
246         return 0;\r
247 }\r
248 /*-----------------------------------------------------------*/\r
249 \r
250 void vPortEndScheduler( void )\r
251 {\r
252         /* Not implemented in ports where there is nothing to return to.\r
253         Artificially force an assert. */\r
254         configASSERT( ulCriticalNesting == 1000UL );\r
255 }\r
256 /*-----------------------------------------------------------*/\r
257 \r
258 void vPortYield( void )\r
259 {\r
260         /* Set a PendSV to request a context switch. */\r
261         *(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;\r
262 \r
263         /* Barriers are normally not required but do ensure the code is completely\r
264         within the specified behaviour for the architecture. */\r
265         __DSB();\r
266         __ISB();\r
267 }\r
268 /*-----------------------------------------------------------*/\r
269 \r
270 void vPortEnterCritical( void )\r
271 {\r
272         portDISABLE_INTERRUPTS();\r
273         ulCriticalNesting++;\r
274         __DSB();\r
275         __ISB();\r
276 \r
277         /* This is not the interrupt safe version of the enter critical function so\r
278         assert() if it is being called from an interrupt context.  Only API\r
279         functions that end in "FromISR" can be used in an interrupt.  Only assert if\r
280         the critical nesting count is 1 to protect against recursive calls if the\r
281         assert function also uses a critical section. */\r
282         if( ulCriticalNesting == 1 )\r
283         {\r
284                 configASSERT( ( ( *(portNVIC_INT_CTRL) ) & portVECTACTIVE_MASK ) == 0 );\r
285         }\r
286 }\r
287 /*-----------------------------------------------------------*/\r
288 \r
289 void vPortExitCritical( void )\r
290 {\r
291         configASSERT( ulCriticalNesting );\r
292         ulCriticalNesting--;\r
293         if( ulCriticalNesting == 0 )\r
294         {\r
295                 portENABLE_INTERRUPTS();\r
296         }\r
297 }\r
298 /*-----------------------------------------------------------*/\r
299 \r
300 void SysTick_Handler( void )\r
301 {\r
302 uint32_t ulDummy;\r
303 \r
304         ulDummy = portSET_INTERRUPT_MASK_FROM_ISR();\r
305         {\r
306                 if( xTaskIncrementTick() != pdFALSE )\r
307                 {\r
308                         /* Pend a context switch. */\r
309                         *(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;\r
310                 }\r
311         }\r
312         portCLEAR_INTERRUPT_MASK_FROM_ISR( ulDummy );\r
313 }\r
314 /*-----------------------------------------------------------*/\r
315 \r
316 /*\r
317  * Setup the systick timer to generate the tick interrupts at the required\r
318  * frequency.\r
319  */\r
320 void prvSetupTimerInterrupt( void )\r
321 {\r
322         /* Configure SysTick to interrupt at the requested rate. */\r
323         *(portNVIC_SYSTICK_LOAD) = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;\r
324         *(portNVIC_SYSTICK_CTRL) = portNVIC_SYSTICK_CLK | portNVIC_SYSTICK_INT | portNVIC_SYSTICK_ENABLE;\r
325 }\r
326 /*-----------------------------------------------------------*/\r
327 \r