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