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