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