]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/Tasking/ARM_CM4F/port.c
Add additional critical section to the default tickless implementations.
[freertos] / FreeRTOS / Source / portable / Tasking / ARM_CM4F / 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 CM4F 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_SYSPRI2                        ( ( volatile unsigned long * ) 0xe000ed20 )\r
77 #define portNVIC_SYSTICK_CLK            0x00000004\r
78 #define portNVIC_SYSTICK_INT            0x00000002\r
79 #define portNVIC_SYSTICK_ENABLE         0x00000001\r
80 #define portNVIC_PENDSV_PRI                     ( ( ( unsigned long ) configKERNEL_INTERRUPT_PRIORITY ) << 16 )\r
81 #define portNVIC_SYSTICK_PRI            ( ( ( unsigned long ) configKERNEL_INTERRUPT_PRIORITY ) << 24 )\r
82 \r
83 /* Constants required to manipulate the VFP. */\r
84 #define portFPCCR                                       ( ( volatile unsigned long * ) 0xe000ef34 ) /* Floating point context control register. */\r
85 #define portASPEN_AND_LSPEN_BITS        ( 0x3UL << 30UL )\r
86 \r
87 /* Constants required to set up the initial stack. */\r
88 #define portINITIAL_XPSR                        ( 0x01000000 )\r
89 #define portINITIAL_EXEC_RETURN         ( 0xfffffffd )\r
90 \r
91 /* The priority used by the kernel is assigned to a variable to make access\r
92 from inline assembler easier. */\r
93 const unsigned long ulKernelPriority = configKERNEL_INTERRUPT_PRIORITY;\r
94 \r
95 /* Each task maintains its own interrupt status in the critical nesting\r
96 variable. */\r
97 static unsigned long ulCriticalNesting = 0xaaaaaaaaUL;\r
98 \r
99 /*\r
100  * Setup the timer to generate the tick interrupts.\r
101  */\r
102 static void prvSetupTimerInterrupt( void );\r
103 \r
104 /*\r
105  * Exception handlers.\r
106  */\r
107 void SysTick_Handler( void );\r
108 \r
109 /*\r
110  * Functions defined in port_asm.asm.\r
111  */\r
112 extern void vPortEnableVFP( void );\r
113 extern void vPortStartFirstTask( void );\r
114 \r
115 /* This exists purely to allow the const to be used from within the\r
116 port_asm.asm assembly file. */\r
117 const unsigned long ulMaxSyscallInterruptPriorityConst = configMAX_SYSCALL_INTERRUPT_PRIORITY;\r
118 \r
119 /*-----------------------------------------------------------*/\r
120 \r
121 /*\r
122  * See header file for description.\r
123  */\r
124 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
125 {\r
126         /* Simulate the stack frame as it would be created by a context switch\r
127         interrupt. */\r
128 \r
129         /* Offset added to account for the way the MCU uses the stack on entry/exit\r
130         of interrupts, and to ensure alignment. */\r
131         pxTopOfStack--;\r
132 \r
133         *pxTopOfStack = portINITIAL_XPSR;       /* xPSR */\r
134         pxTopOfStack--;\r
135         *pxTopOfStack = ( portSTACK_TYPE ) pxCode;      /* PC */\r
136         pxTopOfStack--;\r
137         *pxTopOfStack = 0;      /* LR */\r
138 \r
139         /* Save code space by skipping register initialisation. */\r
140         pxTopOfStack -= 5;      /* R12, R3, R2 and R1. */\r
141         *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;        /* R0 */\r
142 \r
143         /* A save method is being used that requires each task to maintain its\r
144         own exec return value. */\r
145         pxTopOfStack--;\r
146         *pxTopOfStack = portINITIAL_EXEC_RETURN;\r
147 \r
148         pxTopOfStack -= 8;      /* R11, R10, R9, R8, R7, R6, R5 and R4. */\r
149 \r
150         return pxTopOfStack;\r
151 }\r
152 /*-----------------------------------------------------------*/\r
153 \r
154 /*\r
155  * See header file for description.\r
156  */\r
157 portBASE_TYPE xPortStartScheduler( void )\r
158 {\r
159         /* configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to 0.\r
160         See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */\r
161         configASSERT( ( configMAX_SYSCALL_INTERRUPT_PRIORITY ) );\r
162 \r
163         /* Make PendSV and SysTick the lowest priority interrupts. */\r
164         *(portNVIC_SYSPRI2) |= portNVIC_PENDSV_PRI;\r
165         *(portNVIC_SYSPRI2) |= portNVIC_SYSTICK_PRI;\r
166 \r
167         /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
168         here already. */\r
169         prvSetupTimerInterrupt();\r
170 \r
171         /* Initialise the critical nesting count ready for the first task. */\r
172         ulCriticalNesting = 0;\r
173 \r
174         /* Ensure the VFP is enabled - it should be anyway. */\r
175         vPortEnableVFP();\r
176 \r
177         /* Lazy save always. */\r
178         *( portFPCCR ) |= portASPEN_AND_LSPEN_BITS;\r
179 \r
180         /* Start the first task. */\r
181         vPortStartFirstTask();\r
182 \r
183         /* Should not get here! */\r
184         return 0;\r
185 }\r
186 /*-----------------------------------------------------------*/\r
187 \r
188 void vPortEndScheduler( void )\r
189 {\r
190         /* It is unlikely that the CM4F port will require this function as there\r
191         is nothing to return to.  */\r
192 }\r
193 /*-----------------------------------------------------------*/\r
194 \r
195 void vPortYield( void )\r
196 {\r
197         /* Set a PendSV to request a context switch. */\r
198         *(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;\r
199 \r
200         /* Barriers are normally not required but do ensure the code is completely\r
201         within the specified behaviour for the architecture. */\r
202         __DSB();\r
203         __ISB();\r
204 }\r
205 /*-----------------------------------------------------------*/\r
206 \r
207 void vPortEnterCritical( void )\r
208 {\r
209         portDISABLE_INTERRUPTS();\r
210         ulCriticalNesting++;\r
211         __DSB();\r
212         __ISB();\r
213 }\r
214 /*-----------------------------------------------------------*/\r
215 \r
216 void vPortExitCritical( void )\r
217 {\r
218         ulCriticalNesting--;\r
219         if( ulCriticalNesting == 0 )\r
220         {\r
221                 portENABLE_INTERRUPTS();\r
222         }\r
223 }\r
224 /*-----------------------------------------------------------*/\r
225 \r
226 void SysTick_Handler( void )\r
227 {\r
228 unsigned long ulDummy;\r
229 \r
230         ulDummy = portSET_INTERRUPT_MASK_FROM_ISR();\r
231         {\r
232                 if( xTaskIncrementTick() != pdFALSE )\r
233                 {\r
234                         /* Pend a context switch. */\r
235                         *(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;\r
236                 }\r
237         }\r
238         portCLEAR_INTERRUPT_MASK_FROM_ISR( ulDummy );\r
239 }\r
240 /*-----------------------------------------------------------*/\r
241 \r
242 /*\r
243  * Setup the systick timer to generate the tick interrupts at the required\r
244  * frequency.\r
245  */\r
246 void prvSetupTimerInterrupt( void )\r
247 {\r
248         /* Configure SysTick to interrupt at the requested rate. */\r
249         *(portNVIC_SYSTICK_LOAD) = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;\r
250         *(portNVIC_SYSTICK_CTRL) = portNVIC_SYSTICK_CLK | portNVIC_SYSTICK_INT | portNVIC_SYSTICK_ENABLE;\r
251 }\r
252 /*-----------------------------------------------------------*/\r
253 \r