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