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