]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/CCS/ARM_Cortex-R4/port.c
Add tickless idle support in Cortex-M ports.
[freertos] / FreeRTOS / Source / portable / CCS / ARM_Cortex-R4 / port.c
1 /*\r
2     FreeRTOS V7.2.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3 \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     >>>NOTE<<< The modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     ***************************************************************************\r
45      *                                                                       *\r
46      *    Having a problem?  Start by reading the FAQ "My application does   *\r
47      *    not run, what could be wrong?                                      *\r
48      *                                                                       *\r
49      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53 \r
54     http://www.FreeRTOS.org - Documentation, training, latest information,\r
55     license and contact details.\r
56 \r
57     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
58     including FreeRTOS+Trace - an indispensable productivity tool.\r
59 \r
60     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell\r
61     the code with commercial support, indemnification, and middleware, under\r
62     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
63     provide a safety engineered and independently SIL3 certified version under\r
64     the SafeRTOS brand: http://www.SafeRTOS.com.\r
65 */\r
66 \r
67 /* FreeRTOS includes. */\r
68 #include "FreeRTOS.h"\r
69 #include "task.h"\r
70 \r
71 /*-----------------------------------------------------------*/\r
72 \r
73 /* Count of the critical section nesting depth. */\r
74 unsigned portLONG ulCriticalNesting = 9999;\r
75 \r
76 /*-----------------------------------------------------------*/\r
77 \r
78 /* Registers required to configure the RTI. */\r
79 #define portRTI_GCTRL_REG               ( * ( ( volatile unsigned long * ) 0xFFFFFC00 ) )\r
80 #define portRTI_TBCTRL_REG      ( * ( ( volatile unsigned long * ) 0xFFFFFC04 ) )\r
81 #define portRTI_COMPCTRL_REG    ( * ( ( volatile unsigned long * ) 0xFFFFFC0C ) )\r
82 #define portRTI_CNT0_FRC0_REG   ( * ( ( volatile unsigned long * ) 0xFFFFFC10 ) )\r
83 #define portRTI_CNT0_UC0_REG    ( * ( ( volatile unsigned long * ) 0xFFFFFC14 ) )\r
84 #define portRTI_CNT0_CPUC0_REG  ( * ( ( volatile unsigned long * ) 0xFFFFFC18 ) )\r
85 #define portRTI_CNT0_COMP0_REG  ( * ( ( volatile unsigned long * ) 0xFFFFFC50 ) )\r
86 #define portRTI_CNT0_UDCP0_REG  ( * ( ( volatile unsigned long * ) 0xFFFFFC54 ) )\r
87 #define portRTI_SETINTENA_REG   ( * ( ( volatile unsigned long * ) 0xFFFFFC80 ) )\r
88 #define portRTI_CLEARINTENA_REG ( * ( ( volatile unsigned long * ) 0xFFFFFC84 ) )\r
89 #define portRTI_INTFLAG_REG     ( * ( ( volatile unsigned long * ) 0xFFFFFC88 ) )\r
90 \r
91 \r
92 /* Constants required to set up the initial stack of each task. */\r
93 #define portINITIAL_SPSR                ( ( portSTACK_TYPE ) 0x1F )\r
94 #define portINITIAL_FPSCR               ( ( portSTACK_TYPE ) 0x00 )\r
95 #define portINSTRUCTION_SIZE    ( ( portSTACK_TYPE ) 0x04 )\r
96 #define portTHUMB_MODE_BIT              ( ( portSTACK_TYPE ) 0x20 )\r
97 \r
98 /* The number of words on the stack frame between the saved Top Of Stack and\r
99 R0 (in which the parameters are passed. */\r
100 #define portSPACE_BETWEEN_TOS_AND_PARAMETERS    ( 12 )\r
101 \r
102 /*-----------------------------------------------------------*/\r
103 \r
104 /* vPortStartFirstSTask() is defined in portASM.asm */\r
105 extern void vPortStartFirstTask( void );\r
106 \r
107 /*-----------------------------------------------------------*/\r
108 \r
109 /* Saved as part of the task context.  Set to pdFALSE if the task does not\r
110 require an FPU context. */\r
111 unsigned long ulTaskHasFPUContext = 0;\r
112 \r
113 /*-----------------------------------------------------------*/\r
114 \r
115 \r
116 /*\r
117  * See header file for description.\r
118  */\r
119 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
120 {\r
121 portSTACK_TYPE *pxOriginalTOS;\r
122 \r
123         pxOriginalTOS = pxTopOfStack;\r
124 \r
125         #if __TI_VFP_SUPPORT__\r
126         {\r
127                 /* Ensure the stack is correctly aligned on exit. */\r
128                 pxTopOfStack--;\r
129         }\r
130         #endif\r
131 \r
132         /* Setup the initial stack of the task.  The stack is set exactly as\r
133         expected by the portRESTORE_CONTEXT() macro. */\r
134 \r
135         /* First on the stack is the return address - which is the start of the as\r
136         the task has not executed yet.  The offset is added to make the return\r
137         address appear as it would within an IRQ ISR. */\r
138         *pxTopOfStack = ( portSTACK_TYPE ) pxCode + portINSTRUCTION_SIZE;\r
139         pxTopOfStack--;\r
140 \r
141         *pxTopOfStack = ( portSTACK_TYPE ) 0x00000000;  /* R14 */\r
142         pxTopOfStack--;\r
143         *pxTopOfStack = ( portSTACK_TYPE ) pxOriginalTOS; /* Stack used when task starts goes in R13. */\r
144         pxTopOfStack--;\r
145 \r
146         #ifdef portPRELOAD_TASK_REGISTERS\r
147         {\r
148                 *pxTopOfStack = ( portSTACK_TYPE ) 0x12121212;  /* R12 */\r
149                 pxTopOfStack--;\r
150                 *pxTopOfStack = ( portSTACK_TYPE ) 0x11111111;  /* R11 */\r
151                 pxTopOfStack--;\r
152                 *pxTopOfStack = ( portSTACK_TYPE ) 0x10101010;  /* R10 */\r
153                 pxTopOfStack--;\r
154                 *pxTopOfStack = ( portSTACK_TYPE ) 0x09090909;  /* R9 */\r
155                 pxTopOfStack--;\r
156                 *pxTopOfStack = ( portSTACK_TYPE ) 0x08080808;  /* R8 */\r
157                 pxTopOfStack--;\r
158                 *pxTopOfStack = ( portSTACK_TYPE ) 0x07070707;  /* R7 */\r
159                 pxTopOfStack--;\r
160                 *pxTopOfStack = ( portSTACK_TYPE ) 0x06060606;  /* R6 */\r
161                 pxTopOfStack--;\r
162                 *pxTopOfStack = ( portSTACK_TYPE ) 0x05050505;  /* R5 */\r
163                 pxTopOfStack--;\r
164                 *pxTopOfStack = ( portSTACK_TYPE ) 0x04040404;  /* R4 */\r
165                 pxTopOfStack--;\r
166                 *pxTopOfStack = ( portSTACK_TYPE ) 0x03030303;  /* R3 */\r
167                 pxTopOfStack--;\r
168                 *pxTopOfStack = ( portSTACK_TYPE ) 0x02020202;  /* R2 */\r
169                 pxTopOfStack--;\r
170                 *pxTopOfStack = ( portSTACK_TYPE ) 0x01010101;  /* R1 */\r
171                 pxTopOfStack--;\r
172         }\r
173         #else\r
174         {\r
175                 pxTopOfStack -= portSPACE_BETWEEN_TOS_AND_PARAMETERS;\r
176         }\r
177         #endif\r
178 \r
179         /* Function parameters are passed in R0. */\r
180         *pxTopOfStack = ( portSTACK_TYPE ) pvParameters; /* R0 */\r
181         pxTopOfStack--;\r
182 \r
183         /* Set the status register for system mode, with interrupts enabled. */\r
184         *pxTopOfStack = ( portSTACK_TYPE ) ( ( _get_CPSR() & ~0xFF ) | portINITIAL_SPSR );\r
185 \r
186         if( ( ( unsigned long ) pxCode & 0x01UL ) != 0x00 )\r
187         {\r
188                 /* The task will start in thumb mode. */\r
189                 *pxTopOfStack |= portTHUMB_MODE_BIT;\r
190         }\r
191 \r
192         #ifdef __TI_VFP_SUPPORT__\r
193         {\r
194                 pxTopOfStack--;\r
195 \r
196                 /* The last thing on the stack is the tasks ulUsingFPU value, which by\r
197                 default is set to indicate that the stack frame does not include FPU\r
198                 registers. */\r
199                 *pxTopOfStack = pdFALSE;\r
200         }\r
201         #endif\r
202 \r
203         return pxTopOfStack;\r
204 }\r
205 /*-----------------------------------------------------------*/\r
206 \r
207 static void prvSetupTimerInterrupt(void)\r
208 {\r
209         /* Disable timer 0. */\r
210         portRTI_GCTRL_REG &= 0xFFFFFFFEUL;\r
211 \r
212         /* Use the internal counter. */\r
213         portRTI_TBCTRL_REG = 0x00000000U;\r
214 \r
215         /* COMPSEL0 will use the RTIFRC0 counter. */\r
216         portRTI_COMPCTRL_REG = 0x00000000U;\r
217 \r
218         /* Initialise the counter and the prescale counter registers. */\r
219         portRTI_CNT0_UC0_REG =  0x00000000U;\r
220         portRTI_CNT0_FRC0_REG =  0x00000000U;\r
221 \r
222         /* Set Prescalar for RTI clock. */\r
223         portRTI_CNT0_CPUC0_REG = 0x00000001U;\r
224         portRTI_CNT0_COMP0_REG = ( configCPU_CLOCK_HZ / 2 ) / configTICK_RATE_HZ;\r
225         portRTI_CNT0_UDCP0_REG = ( configCPU_CLOCK_HZ / 2 ) / configTICK_RATE_HZ;\r
226 \r
227         /* Clear interrupts. */\r
228         portRTI_INTFLAG_REG =  0x0007000FU;\r
229         portRTI_CLEARINTENA_REG = 0x00070F0FU;\r
230 \r
231         /* Enable the compare 0 interrupt. */\r
232         portRTI_SETINTENA_REG = 0x00000001U;\r
233         portRTI_GCTRL_REG |= 0x00000001U;\r
234 }\r
235 /*-----------------------------------------------------------*/\r
236 \r
237 /*\r
238  * See header file for description.\r
239  */\r
240 portBASE_TYPE xPortStartScheduler(void)\r
241 {\r
242         /* Start the timer that generates the tick ISR. */\r
243         prvSetupTimerInterrupt();\r
244 \r
245         /* Reset the critical section nesting count read to execute the first task. */\r
246         ulCriticalNesting = 0;\r
247 \r
248         /* Start the first task.  This is done from portASM.asm as ARM mode must be\r
249         used. */\r
250         vPortStartFirstTask();\r
251 \r
252         /* Should not get here! */\r
253         return pdFAIL;\r
254 }\r
255 /*-----------------------------------------------------------*/\r
256 \r
257 /*\r
258  * See header file for description.\r
259  */\r
260 void vPortEndScheduler(void)\r
261 {\r
262         /* It is unlikely that the port will require this function as there\r
263         is nothing to return to. */\r
264 }\r
265 /*-----------------------------------------------------------*/\r
266 \r
267 #if configUSE_PREEMPTION == 0\r
268 \r
269         /* The cooperative scheduler requires a normal IRQ service routine to\r
270          * simply increment the system tick. */\r
271         __interrupt void vPortNonPreemptiveTick( void )\r
272         {\r
273                 /* clear clock interrupt flag */\r
274                 RTI->INTFLAG = 0x00000001;\r
275 \r
276                 /* Increment the tick count - this may make a delaying task ready\r
277                 to run - but a context switch is not performed. */\r
278                 vTaskIncrementTick();\r
279         }\r
280 \r
281  #else\r
282 \r
283         /*\r
284          **************************************************************************\r
285          * The preemptive scheduler ISR is written in assembler and can be found\r
286          * in the portASM.asm file. This will only get used if portUSE_PREEMPTION\r
287          * is set to 1 in portmacro.h\r
288          **************************************************************************\r
289          */\r
290         void vPortPreemptiveTick( void );\r
291 \r
292 #endif\r
293 /*-----------------------------------------------------------*/\r
294 \r
295 \r
296 /*\r
297  * Disable interrupts, and keep a count of the nesting depth.\r
298  */\r
299 void vPortEnterCritical( void )\r
300 {\r
301         /* Disable interrupts as per portDISABLE_INTERRUPTS(); */\r
302         portDISABLE_INTERRUPTS();\r
303 \r
304         /* Now interrupts are disabled ulCriticalNesting can be accessed\r
305         directly.  Increment ulCriticalNesting to keep a count of how many times\r
306         portENTER_CRITICAL() has been called. */\r
307         ulCriticalNesting++;\r
308 }\r
309 /*-----------------------------------------------------------*/\r
310 \r
311 /*\r
312  * Decrement the critical nesting count, and if it has reached zero, re-enable\r
313  * interrupts.\r
314  */\r
315 void vPortExitCritical( void )\r
316 {\r
317         if( ulCriticalNesting > 0 )\r
318         {\r
319                 /* Decrement the nesting count as we are leaving a critical section. */\r
320                 ulCriticalNesting--;\r
321 \r
322                 /* If the nesting level has reached zero then interrupts should be\r
323                 re-enabled. */\r
324                 if( ulCriticalNesting == 0 )\r
325                 {\r
326                         /* Enable interrupts as per portENABLE_INTERRUPTS(). */\r
327                         portENABLE_INTERRUPTS();\r
328                 }\r
329         }\r
330 }\r
331 /*-----------------------------------------------------------*/\r
332 \r
333 #if __TI_VFP_SUPPORT__\r
334 \r
335         void vPortTaskUsesFPU( void )\r
336         {\r
337         extern void vPortInitialiseFPSCR( void );\r
338 \r
339                 /* A task is registering the fact that it needs an FPU context.  Set the\r
340                 FPU flag (saved as part of the task context. */\r
341                 ulTaskHasFPUContext = pdTRUE;\r
342 \r
343                 /* Initialise the floating point status register. */\r
344                 vPortInitialiseFPSCR();\r
345         }\r
346 \r
347 #endif /* __TI_VFP_SUPPORT__ */\r
348 \r
349 /*-----------------------------------------------------------*/\r
350 \r