]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/CCS/ARM_Cortex-R4/port.c
Add additional critical section to the default tickless implementations.
[freertos] / FreeRTOS / Source / portable / CCS / ARM_Cortex-R4 / 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 /* FreeRTOS includes. */\r
66 #include "FreeRTOS.h"\r
67 #include "task.h"\r
68 \r
69 /*-----------------------------------------------------------*/\r
70 \r
71 /* Count of the critical section nesting depth. */\r
72 unsigned portLONG ulCriticalNesting = 9999;\r
73 \r
74 /*-----------------------------------------------------------*/\r
75 \r
76 /* Registers required to configure the RTI. */\r
77 #define portRTI_GCTRL_REG               ( * ( ( volatile unsigned long * ) 0xFFFFFC00 ) )\r
78 #define portRTI_TBCTRL_REG      ( * ( ( volatile unsigned long * ) 0xFFFFFC04 ) )\r
79 #define portRTI_COMPCTRL_REG    ( * ( ( volatile unsigned long * ) 0xFFFFFC0C ) )\r
80 #define portRTI_CNT0_FRC0_REG   ( * ( ( volatile unsigned long * ) 0xFFFFFC10 ) )\r
81 #define portRTI_CNT0_UC0_REG    ( * ( ( volatile unsigned long * ) 0xFFFFFC14 ) )\r
82 #define portRTI_CNT0_CPUC0_REG  ( * ( ( volatile unsigned long * ) 0xFFFFFC18 ) )\r
83 #define portRTI_CNT0_COMP0_REG  ( * ( ( volatile unsigned long * ) 0xFFFFFC50 ) )\r
84 #define portRTI_CNT0_UDCP0_REG  ( * ( ( volatile unsigned long * ) 0xFFFFFC54 ) )\r
85 #define portRTI_SETINTENA_REG   ( * ( ( volatile unsigned long * ) 0xFFFFFC80 ) )\r
86 #define portRTI_CLEARINTENA_REG ( * ( ( volatile unsigned long * ) 0xFFFFFC84 ) )\r
87 #define portRTI_INTFLAG_REG     ( * ( ( volatile unsigned long * ) 0xFFFFFC88 ) )\r
88 \r
89 \r
90 /* Constants required to set up the initial stack of each task. */\r
91 #define portINITIAL_SPSR                ( ( portSTACK_TYPE ) 0x1F )\r
92 #define portINITIAL_FPSCR               ( ( portSTACK_TYPE ) 0x00 )\r
93 #define portINSTRUCTION_SIZE    ( ( portSTACK_TYPE ) 0x04 )\r
94 #define portTHUMB_MODE_BIT              ( ( portSTACK_TYPE ) 0x20 )\r
95 \r
96 /* The number of words on the stack frame between the saved Top Of Stack and\r
97 R0 (in which the parameters are passed. */\r
98 #define portSPACE_BETWEEN_TOS_AND_PARAMETERS    ( 12 )\r
99 \r
100 /*-----------------------------------------------------------*/\r
101 \r
102 /* vPortStartFirstSTask() is defined in portASM.asm */\r
103 extern void vPortStartFirstTask( void );\r
104 \r
105 /*-----------------------------------------------------------*/\r
106 \r
107 /* Saved as part of the task context.  Set to pdFALSE if the task does not\r
108 require an FPU context. */\r
109 unsigned long ulTaskHasFPUContext = 0;\r
110 \r
111 /*-----------------------------------------------------------*/\r
112 \r
113 \r
114 /*\r
115  * See header file for description.\r
116  */\r
117 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
118 {\r
119 portSTACK_TYPE *pxOriginalTOS;\r
120 \r
121         pxOriginalTOS = pxTopOfStack;\r
122 \r
123         #if __TI_VFP_SUPPORT__\r
124         {\r
125                 /* Ensure the stack is correctly aligned on exit. */\r
126                 pxTopOfStack--;\r
127         }\r
128         #endif\r
129 \r
130         /* Setup the initial stack of the task.  The stack is set exactly as\r
131         expected by the portRESTORE_CONTEXT() macro. */\r
132 \r
133         /* First on the stack is the return address - which is the start of the as\r
134         the task has not executed yet.  The offset is added to make the return\r
135         address appear as it would within an IRQ ISR. */\r
136         *pxTopOfStack = ( portSTACK_TYPE ) pxCode + portINSTRUCTION_SIZE;\r
137         pxTopOfStack--;\r
138 \r
139         *pxTopOfStack = ( portSTACK_TYPE ) 0x00000000;  /* R14 */\r
140         pxTopOfStack--;\r
141         *pxTopOfStack = ( portSTACK_TYPE ) pxOriginalTOS; /* Stack used when task starts goes in R13. */\r
142         pxTopOfStack--;\r
143 \r
144         #ifdef portPRELOAD_TASK_REGISTERS\r
145         {\r
146                 *pxTopOfStack = ( portSTACK_TYPE ) 0x12121212;  /* R12 */\r
147                 pxTopOfStack--;\r
148                 *pxTopOfStack = ( portSTACK_TYPE ) 0x11111111;  /* R11 */\r
149                 pxTopOfStack--;\r
150                 *pxTopOfStack = ( portSTACK_TYPE ) 0x10101010;  /* R10 */\r
151                 pxTopOfStack--;\r
152                 *pxTopOfStack = ( portSTACK_TYPE ) 0x09090909;  /* R9 */\r
153                 pxTopOfStack--;\r
154                 *pxTopOfStack = ( portSTACK_TYPE ) 0x08080808;  /* R8 */\r
155                 pxTopOfStack--;\r
156                 *pxTopOfStack = ( portSTACK_TYPE ) 0x07070707;  /* R7 */\r
157                 pxTopOfStack--;\r
158                 *pxTopOfStack = ( portSTACK_TYPE ) 0x06060606;  /* R6 */\r
159                 pxTopOfStack--;\r
160                 *pxTopOfStack = ( portSTACK_TYPE ) 0x05050505;  /* R5 */\r
161                 pxTopOfStack--;\r
162                 *pxTopOfStack = ( portSTACK_TYPE ) 0x04040404;  /* R4 */\r
163                 pxTopOfStack--;\r
164                 *pxTopOfStack = ( portSTACK_TYPE ) 0x03030303;  /* R3 */\r
165                 pxTopOfStack--;\r
166                 *pxTopOfStack = ( portSTACK_TYPE ) 0x02020202;  /* R2 */\r
167                 pxTopOfStack--;\r
168                 *pxTopOfStack = ( portSTACK_TYPE ) 0x01010101;  /* R1 */\r
169                 pxTopOfStack--;\r
170         }\r
171         #else\r
172         {\r
173                 pxTopOfStack -= portSPACE_BETWEEN_TOS_AND_PARAMETERS;\r
174         }\r
175         #endif\r
176 \r
177         /* Function parameters are passed in R0. */\r
178         *pxTopOfStack = ( portSTACK_TYPE ) pvParameters; /* R0 */\r
179         pxTopOfStack--;\r
180 \r
181         /* Set the status register for system mode, with interrupts enabled. */\r
182         *pxTopOfStack = ( portSTACK_TYPE ) ( ( _get_CPSR() & ~0xFF ) | portINITIAL_SPSR );\r
183 \r
184         if( ( ( unsigned long ) pxCode & 0x01UL ) != 0x00 )\r
185         {\r
186                 /* The task will start in thumb mode. */\r
187                 *pxTopOfStack |= portTHUMB_MODE_BIT;\r
188         }\r
189 \r
190         #ifdef __TI_VFP_SUPPORT__\r
191         {\r
192                 pxTopOfStack--;\r
193 \r
194                 /* The last thing on the stack is the tasks ulUsingFPU value, which by\r
195                 default is set to indicate that the stack frame does not include FPU\r
196                 registers. */\r
197                 *pxTopOfStack = pdFALSE;\r
198         }\r
199         #endif\r
200 \r
201         return pxTopOfStack;\r
202 }\r
203 /*-----------------------------------------------------------*/\r
204 \r
205 static void prvSetupTimerInterrupt(void)\r
206 {\r
207         /* Disable timer 0. */\r
208         portRTI_GCTRL_REG &= 0xFFFFFFFEUL;\r
209 \r
210         /* Use the internal counter. */\r
211         portRTI_TBCTRL_REG = 0x00000000U;\r
212 \r
213         /* COMPSEL0 will use the RTIFRC0 counter. */\r
214         portRTI_COMPCTRL_REG = 0x00000000U;\r
215 \r
216         /* Initialise the counter and the prescale counter registers. */\r
217         portRTI_CNT0_UC0_REG =  0x00000000U;\r
218         portRTI_CNT0_FRC0_REG =  0x00000000U;\r
219 \r
220         /* Set Prescalar for RTI clock. */\r
221         portRTI_CNT0_CPUC0_REG = 0x00000001U;\r
222         portRTI_CNT0_COMP0_REG = ( configCPU_CLOCK_HZ / 2 ) / configTICK_RATE_HZ;\r
223         portRTI_CNT0_UDCP0_REG = ( configCPU_CLOCK_HZ / 2 ) / configTICK_RATE_HZ;\r
224 \r
225         /* Clear interrupts. */\r
226         portRTI_INTFLAG_REG =  0x0007000FU;\r
227         portRTI_CLEARINTENA_REG = 0x00070F0FU;\r
228 \r
229         /* Enable the compare 0 interrupt. */\r
230         portRTI_SETINTENA_REG = 0x00000001U;\r
231         portRTI_GCTRL_REG |= 0x00000001U;\r
232 }\r
233 /*-----------------------------------------------------------*/\r
234 \r
235 /*\r
236  * See header file for description.\r
237  */\r
238 portBASE_TYPE xPortStartScheduler(void)\r
239 {\r
240         /* Start the timer that generates the tick ISR. */\r
241         prvSetupTimerInterrupt();\r
242 \r
243         /* Reset the critical section nesting count read to execute the first task. */\r
244         ulCriticalNesting = 0;\r
245 \r
246         /* Start the first task.  This is done from portASM.asm as ARM mode must be\r
247         used. */\r
248         vPortStartFirstTask();\r
249 \r
250         /* Should not get here! */\r
251         return pdFAIL;\r
252 }\r
253 /*-----------------------------------------------------------*/\r
254 \r
255 /*\r
256  * See header file for description.\r
257  */\r
258 void vPortEndScheduler(void)\r
259 {\r
260         /* It is unlikely that the port will require this function as there\r
261         is nothing to return to. */\r
262 }\r
263 /*-----------------------------------------------------------*/\r
264 \r
265 #if configUSE_PREEMPTION == 0\r
266 \r
267         /* The cooperative scheduler requires a normal IRQ service routine to\r
268          * simply increment the system tick. */\r
269         __interrupt void vPortNonPreemptiveTick( void )\r
270         {\r
271                 /* clear clock interrupt flag */\r
272                 portRTI_INTFLAG_REG = 0x00000001;\r
273 \r
274                 /* Increment the tick count - this may make a delaying task ready\r
275                 to run - but a context switch is not performed. */\r
276                 xTaskIncrementTick();\r
277         }\r
278 \r
279  #else\r
280 \r
281         /*\r
282          **************************************************************************\r
283          * The preemptive scheduler ISR is written in assembler and can be found\r
284          * in the portASM.asm file. This will only get used if portUSE_PREEMPTION\r
285          * is set to 1 in portmacro.h\r
286          **************************************************************************\r
287          */\r
288         void vPortPreemptiveTick( void );\r
289 \r
290 #endif\r
291 /*-----------------------------------------------------------*/\r
292 \r
293 \r
294 /*\r
295  * Disable interrupts, and keep a count of the nesting depth.\r
296  */\r
297 void vPortEnterCritical( void )\r
298 {\r
299         /* Disable interrupts as per portDISABLE_INTERRUPTS(); */\r
300         portDISABLE_INTERRUPTS();\r
301 \r
302         /* Now interrupts are disabled ulCriticalNesting can be accessed\r
303         directly.  Increment ulCriticalNesting to keep a count of how many times\r
304         portENTER_CRITICAL() has been called. */\r
305         ulCriticalNesting++;\r
306 }\r
307 /*-----------------------------------------------------------*/\r
308 \r
309 /*\r
310  * Decrement the critical nesting count, and if it has reached zero, re-enable\r
311  * interrupts.\r
312  */\r
313 void vPortExitCritical( void )\r
314 {\r
315         if( ulCriticalNesting > 0 )\r
316         {\r
317                 /* Decrement the nesting count as we are leaving a critical section. */\r
318                 ulCriticalNesting--;\r
319 \r
320                 /* If the nesting level has reached zero then interrupts should be\r
321                 re-enabled. */\r
322                 if( ulCriticalNesting == 0 )\r
323                 {\r
324                         /* Enable interrupts as per portENABLE_INTERRUPTS(). */\r
325                         portENABLE_INTERRUPTS();\r
326                 }\r
327         }\r
328 }\r
329 /*-----------------------------------------------------------*/\r
330 \r
331 #if __TI_VFP_SUPPORT__\r
332 \r
333         void vPortTaskUsesFPU( void )\r
334         {\r
335         extern void vPortInitialiseFPSCR( void );\r
336 \r
337                 /* A task is registering the fact that it needs an FPU context.  Set the\r
338                 FPU flag (saved as part of the task context. */\r
339                 ulTaskHasFPUContext = pdTRUE;\r
340 \r
341                 /* Initialise the floating point status register. */\r
342                 vPortInitialiseFPSCR();\r
343         }\r
344 \r
345 #endif /* __TI_VFP_SUPPORT__ */\r
346 \r
347 /*-----------------------------------------------------------*/\r
348 \r