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