]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/Tasking/ARM_CM4F/port.c
Add xEventGroupClearBitsFromISR() and xEventGroupGetBitsFromISR() functions.
[freertos] / FreeRTOS / Source / portable / Tasking / ARM_CM4F / port.c
1 /*\r
2     FreeRTOS V7.6.0 - Copyright (C) 2013 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     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
28     >>! a combined work that includes FreeRTOS without being obliged to provide\r
29     >>! the source code for proprietary components outside of the FreeRTOS\r
30     >>! kernel.\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /*-----------------------------------------------------------\r
67  * Implementation of functions defined in portable.h for the ARM CM4F port.\r
68  *----------------------------------------------------------*/\r
69 \r
70 /* Scheduler includes. */\r
71 #include "FreeRTOS.h"\r
72 #include "task.h"\r
73 \r
74 /* Constants required to manipulate the NVIC. */\r
75 #define portNVIC_SYSTICK_CTRL           ( ( volatile uint32_t * ) 0xe000e010 )\r
76 #define portNVIC_SYSTICK_LOAD           ( ( volatile uint32_t * ) 0xe000e014 )\r
77 #define portNVIC_SYSPRI2                        ( ( volatile uint32_t * ) 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_PENDSV_PRI                     ( ( ( uint32_t ) configKERNEL_INTERRUPT_PRIORITY ) << 16 )\r
82 #define portNVIC_SYSTICK_PRI            ( ( ( uint32_t ) configKERNEL_INTERRUPT_PRIORITY ) << 24 )\r
83 \r
84 /* Constants required to manipulate the VFP. */\r
85 #define portFPCCR                                       ( ( volatile uint32_t * ) 0xe000ef34 ) /* Floating point context control register. */\r
86 #define portASPEN_AND_LSPEN_BITS        ( 0x3UL << 30UL )\r
87 \r
88 /* Constants required to set up the initial stack. */\r
89 #define portINITIAL_XPSR                        ( 0x01000000 )\r
90 #define portINITIAL_EXEC_RETURN         ( 0xfffffffd )\r
91 \r
92 /* Let the user override the pre-loading of the initial LR with the address of\r
93 prvTaskExitError() in case is messes up unwinding of the stack in the\r
94 debugger. */\r
95 #ifdef configTASK_RETURN_ADDRESS\r
96         #define portTASK_RETURN_ADDRESS configTASK_RETURN_ADDRESS\r
97 #else\r
98         #define portTASK_RETURN_ADDRESS prvTaskExitError\r
99 #endif\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 uint32_t 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 uint32_t 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 /*\r
126  * Used to catch tasks that attempt to return from their implementing function.\r
127  */\r
128 static void prvTaskExitError( void );\r
129 \r
130 /* This exists purely to allow the const to be used from within the\r
131 port_asm.asm assembly file. */\r
132 const uint32_t ulMaxSyscallInterruptPriorityConst = configMAX_SYSCALL_INTERRUPT_PRIORITY;\r
133 \r
134 /*-----------------------------------------------------------*/\r
135 \r
136 /*\r
137  * See header file for description.\r
138  */\r
139 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
140 {\r
141         /* Simulate the stack frame as it would be created by a context switch\r
142         interrupt. */\r
143 \r
144         /* Offset added to account for the way the MCU uses the stack on entry/exit\r
145         of interrupts, and to ensure alignment. */\r
146         pxTopOfStack--;\r
147 \r
148         *pxTopOfStack = portINITIAL_XPSR;       /* xPSR */\r
149         pxTopOfStack--;\r
150         *pxTopOfStack = ( StackType_t ) pxCode; /* PC */\r
151         pxTopOfStack--;\r
152         *pxTopOfStack = ( StackType_t ) portTASK_RETURN_ADDRESS;        /* LR */\r
153 \r
154         /* Save code space by skipping register initialisation. */\r
155         pxTopOfStack -= 5;      /* R12, R3, R2 and R1. */\r
156         *pxTopOfStack = ( StackType_t ) pvParameters;   /* R0 */\r
157 \r
158         /* A save method is being used that requires each task to maintain its\r
159         own exec return value. */\r
160         pxTopOfStack--;\r
161         *pxTopOfStack = portINITIAL_EXEC_RETURN;\r
162 \r
163         pxTopOfStack -= 8;      /* R11, R10, R9, R8, R7, R6, R5 and R4. */\r
164 \r
165         return pxTopOfStack;\r
166 }\r
167 /*-----------------------------------------------------------*/\r
168 \r
169 static void prvTaskExitError( void )\r
170 {\r
171         /* A function that implements a task must not exit or attempt to return to\r
172         its caller as there is nothing to return to.  If a task wants to exit it\r
173         should instead call vTaskDelete( NULL ).\r
174 \r
175         Artificially force an assert() to be triggered if configASSERT() is\r
176         defined, then stop here so application writers can catch the error. */\r
177         configASSERT( ulCriticalNesting == ~0UL );\r
178         portDISABLE_INTERRUPTS();\r
179         for( ;; );\r
180 }\r
181 /*-----------------------------------------------------------*/\r
182 \r
183 /*\r
184  * See header file for description.\r
185  */\r
186 BaseType_t xPortStartScheduler( void )\r
187 {\r
188         /* configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to 0.\r
189         See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */\r
190         configASSERT( ( configMAX_SYSCALL_INTERRUPT_PRIORITY ) );\r
191 \r
192         /* Make PendSV and SysTick the lowest priority interrupts. */\r
193         *(portNVIC_SYSPRI2) |= portNVIC_PENDSV_PRI;\r
194         *(portNVIC_SYSPRI2) |= portNVIC_SYSTICK_PRI;\r
195 \r
196         /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
197         here already. */\r
198         prvSetupTimerInterrupt();\r
199 \r
200         /* Initialise the critical nesting count ready for the first task. */\r
201         ulCriticalNesting = 0;\r
202 \r
203         /* Ensure the VFP is enabled - it should be anyway. */\r
204         vPortEnableVFP();\r
205 \r
206         /* Lazy save always. */\r
207         *( portFPCCR ) |= portASPEN_AND_LSPEN_BITS;\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         /* Not implemented in ports where there is nothing to return to.\r
220         Artificially force an assert. */\r
221         configASSERT( ulCriticalNesting == 1000UL );\r
222 }\r
223 /*-----------------------------------------------------------*/\r
224 \r
225 void vPortYield( void )\r
226 {\r
227         /* Set a PendSV to request a context switch. */\r
228         *(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;\r
229 \r
230         /* Barriers are normally not required but do ensure the code is completely\r
231         within the specified behaviour for the architecture. */\r
232         __DSB();\r
233         __ISB();\r
234 }\r
235 /*-----------------------------------------------------------*/\r
236 \r
237 void vPortEnterCritical( void )\r
238 {\r
239         portDISABLE_INTERRUPTS();\r
240         ulCriticalNesting++;\r
241         __DSB();\r
242         __ISB();\r
243 }\r
244 /*-----------------------------------------------------------*/\r
245 \r
246 void vPortExitCritical( void )\r
247 {\r
248         configASSERT( ulCriticalNesting );\r
249         ulCriticalNesting--;\r
250         if( ulCriticalNesting == 0 )\r
251         {\r
252                 portENABLE_INTERRUPTS();\r
253         }\r
254 }\r
255 /*-----------------------------------------------------------*/\r
256 \r
257 void SysTick_Handler( void )\r
258 {\r
259 uint32_t ulDummy;\r
260 \r
261         ulDummy = portSET_INTERRUPT_MASK_FROM_ISR();\r
262         {\r
263                 if( xTaskIncrementTick() != pdFALSE )\r
264                 {\r
265                         /* Pend a context switch. */\r
266                         *(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;\r
267                 }\r
268         }\r
269         portCLEAR_INTERRUPT_MASK_FROM_ISR( ulDummy );\r
270 }\r
271 /*-----------------------------------------------------------*/\r
272 \r
273 /*\r
274  * Setup the systick timer to generate the tick interrupts at the required\r
275  * frequency.\r
276  */\r
277 void prvSetupTimerInterrupt( void )\r
278 {\r
279         /* Configure SysTick to interrupt at the requested rate. */\r
280         *(portNVIC_SYSTICK_LOAD) = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;\r
281         *(portNVIC_SYSTICK_CTRL) = portNVIC_SYSTICK_CLK | portNVIC_SYSTICK_INT | portNVIC_SYSTICK_ENABLE;\r
282 }\r
283 /*-----------------------------------------------------------*/\r
284 \r