2 FreeRTOS V7.0.2 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
54 /* Standard includes. */
\r
58 /* TriCore specific includes. */
\r
60 #include <machine/intrinsics.h>
\r
61 #include <machine/cint.h>
\r
62 #include <machine/wdtcon.h>
\r
64 /* Kernel includes. */
\r
65 #include "FreeRTOS.h"
\r
69 #if configCHECK_FOR_STACK_OVERFLOW > 0
\r
70 #error "Stack checking cannot be used with this port, as, unlike most ports, the pxTopOfStack member of the TCB is consumed CSA. CSA starvation, loosely equivalent to stack overflow, will result in a trap exception."
\r
71 /* The stack pointer is accessible using portCSA_TO_ADDRESS( portCSA_TO_ADDRESS( pxCurrentTCB->pxTopOfStack )[ 0 ] )[ 2 ]; */
\r
72 #endif /* configCHECK_FOR_STACK_OVERFLOW */
\r
75 /*-----------------------------------------------------------*/
\r
77 /* System register Definitions. */
\r
78 #define portSYSTEM_PROGRAM_STATUS_WORD ( 0x000008FFUL ) /* Supervisor Mode, MPU Register Set 0 and Call Depth Counting disabled. */
\r
79 #define portINITIAL_PRIVILEGED_PROGRAM_STATUS_WORD ( 0x000014FFUL ) /* IO Level 1, MPU Register Set 1 and Call Depth Counting disabled. */
\r
80 #define portINITIAL_UNPRIVILEGED_PROGRAM_STATUS_WORD ( 0x000010FFUL ) /* IO Level 0, MPU Register Set 1 and Call Depth Counting disabled. */
\r
81 #define portINITIAL_PCXI_UPPER_CONTEXT_WORD ( 0x00C00000UL ) /* The lower 20 bits identify the CSA address. */
\r
82 #define portINITIAL_SYSCON ( 0x00000000UL ) /* MPU Disable. */
\r
84 /* CSA manipulation macros. */
\r
85 #define portCSA_FCX_MASK ( 0x000FFFFFUL )
\r
87 /* OS Interrupt and Trap mechanisms. */
\r
88 #define portRESTORE_PSW_MASK ( ~( 0x000000FFUL ) )
\r
89 #define portSYSCALL_TRAP ( 6 )
\r
91 /* Each CSA contains 16 words of data. */
\r
92 #define portNUM_WORDS_IN_CSA ( 16 )
\r
94 /* The interrupt enable bit in the PCP_SRC register. */
\r
95 #define portENABLE_CPU_INTERRUPT ( 1U << 12U )
\r
96 /*-----------------------------------------------------------*/
\r
99 * Perform any hardware configuration necessary to generate the tick interrupt.
\r
101 static void prvSystemTickHandler( int ) __attribute__((longcall));
\r
102 static void prvSetupTimerInterrupt( void );
\r
105 * Trap handler for yields.
\r
107 static void prvTrapYield( int iTrapIdentification );
\r
110 * Priority 1 interrupt handler for yields pended from an interrupt.
\r
112 static void prvInterruptYield( int iTrapIdentification );
\r
114 /*-----------------------------------------------------------*/
\r
116 /* This reference is required by the save/restore context macros. */
\r
117 extern volatile unsigned long *pxCurrentTCB;
\r
119 /* Precalculate the compare match value at compile time. */
\r
120 static const unsigned long ulCompareMatchValue = ( configPERIPHERAL_CLOCK_HZ / configTICK_RATE_HZ );
\r
122 /*-----------------------------------------------------------*/
\r
124 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE * pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
126 unsigned long *pulUpperCSA = NULL;
\r
127 unsigned long *pulLowerCSA = NULL;
\r
129 /* 16 Address Registers (4 Address registers are global), 16 Data
\r
130 Registers, and 3 System Registers.
\r
132 There are 3 registers that track the CSAs.
\r
133 FCX points to the head of globally free set of CSAs.
\r
134 PCX for the task needs to point to Lower->Upper->NULL arrangement.
\r
135 LCX points to the last free CSA so that corrective action can be taken.
\r
137 Need two CSAs to store the context of a task.
\r
138 The upper context contains D8-D15, A10-A15, PSW and PCXI->NULL.
\r
139 The lower context contains D0-D7, A2-A7, A11 and PCXI->UpperContext.
\r
140 The pxCurrentTCB->pxTopOfStack points to the Lower Context RSLCX matching the initial BISR.
\r
141 The Lower Context points to the Upper Context ready for the return from the interrupt handler.
\r
143 The Real stack pointer for the task is stored in the A10 which is restored
\r
144 with the upper context. */
\r
146 /* Have to disable interrupts here because the CSAs are going to be
\r
148 portENTER_CRITICAL();
\r
150 /* DSync to ensure that buffering is not a problem. */
\r
153 /* Consume two free CSAs. */
\r
154 pulLowerCSA = portCSA_TO_ADDRESS( _mfcr( $FCX ) );
\r
155 if( NULL != pulLowerCSA )
\r
157 /* The Lower Links to the Upper. */
\r
158 pulUpperCSA = portCSA_TO_ADDRESS( pulLowerCSA[ 0 ] );
\r
161 /* Check that we have successfully reserved two CSAs. */
\r
162 if( ( NULL != pulLowerCSA ) && ( NULL != pulUpperCSA ) )
\r
164 /* Remove the two consumed CSAs from the free CSA list. */
\r
167 _mtcr( $FCX, pulUpperCSA[ 0 ] );
\r
173 /* Simply trigger a context list depletion trap. */
\r
177 portEXIT_CRITICAL();
\r
179 /* Clear the upper CSA. */
\r
180 memset( pulUpperCSA, 0, portNUM_WORDS_IN_CSA * sizeof( unsigned long ) );
\r
182 /* Upper Context. */
\r
183 pulUpperCSA[ 2 ] = ( unsigned long )pxTopOfStack; /* A10; Stack Return aka Stack Pointer */
\r
184 pulUpperCSA[ 1 ] = portSYSTEM_PROGRAM_STATUS_WORD; /* PSW */
\r
186 /* Clear the lower CSA. */
\r
187 memset( pulLowerCSA, 0, portNUM_WORDS_IN_CSA * sizeof( unsigned long ) );
\r
189 /* Lower Context. */
\r
190 pulLowerCSA[ 8 ] = ( unsigned long ) pvParameters; /* A4; Address Type Parameter Register */
\r
191 pulLowerCSA[ 1 ] = ( unsigned long ) pxCode; /* A11; Return Address aka RA */
\r
193 /* PCXI pointing to the Upper context. */
\r
194 pulLowerCSA[ 0 ] = ( portINITIAL_PCXI_UPPER_CONTEXT_WORD | ( unsigned long ) portADDRESS_TO_CSA( pulUpperCSA ) );
\r
196 /* Save the link to the CSA in the top of stack. */
\r
197 pxTopOfStack = (unsigned long * ) portADDRESS_TO_CSA( pulLowerCSA );
\r
199 /* DSync to ensure that buffering is not a problem. */
\r
202 return pxTopOfStack;
\r
204 /*-----------------------------------------------------------*/
\r
206 long xPortStartScheduler( void )
\r
208 extern void vTrapInstallHandlers( void );
\r
209 unsigned long ulMFCR = 0UL;
\r
210 unsigned long *pulUpperCSA = NULL;
\r
211 unsigned long *pulLowerCSA = NULL;
\r
213 /* Interrupts at or below configMAX_SYSCALL_INTERRUPT_PRIORITY are disable
\r
214 when this function is called. */
\r
216 /* Set-up the timer interrupt. */
\r
217 prvSetupTimerInterrupt();
\r
219 /* Install the Trap Handlers. */
\r
220 vTrapInstallHandlers();
\r
222 /* Install the Syscall Handler for yield calls. */
\r
223 if( 0 == _install_trap_handler( portSYSCALL_TRAP, prvTrapYield ) )
\r
225 /* Failed to install the yield handler, force an assert. */
\r
226 configASSERT( ( ( volatile void * ) NULL ) );
\r
229 /* Enable then install the priority 1 interrupt for pending context
\r
230 switches from an ISR. See mod_SRC in the TriCore manual. */
\r
231 CPU_SRC0.reg = ( portENABLE_CPU_INTERRUPT ) | ( configKERNEL_YIELD_PRIORITY );
\r
232 if( 0 == _install_int_handler( configKERNEL_YIELD_PRIORITY, prvInterruptYield, 0 ) )
\r
234 /* Failed to install the yield handler, force an assert. */
\r
235 configASSERT( ( ( volatile void * ) NULL ) );
\r
240 /* Load the initial SYSCON. */
\r
241 _mtcr( $SYSCON, portINITIAL_SYSCON );
\r
244 /* ENDINIT has already been applied in the 'cstart.c' code. */
\r
246 /* Clear the PSW.CDC to enable the use of an RFE without it generating an
\r
247 exception because this code is not genuinely in an exception. */
\r
248 ulMFCR = _mfcr( $PSW );
\r
249 ulMFCR &= portRESTORE_PSW_MASK;
\r
251 _mtcr( $PSW, ulMFCR );
\r
254 /* Finally, perform the equivalent of a portRESTORE_CONTEXT() */
\r
255 pulLowerCSA = portCSA_TO_ADDRESS( ( *pxCurrentTCB ) );
\r
256 pulUpperCSA = portCSA_TO_ADDRESS( pulLowerCSA[0] );
\r
258 _mtcr( $PCXI, *pxCurrentTCB );
\r
264 /* Return to the first task selected to execute. */
\r
265 __asm volatile( "rfe" );
\r
267 /* Will not get here. */
\r
270 /*-----------------------------------------------------------*/
\r
272 static void prvSetupTimerInterrupt( void )
\r
274 /* Set-up the clock divider. */
\r
277 /* Wait until access to Endint protected register is enabled. */
\r
278 while( 0 != ( WDT_CON0.reg & 0x1UL ) );
\r
280 /* RMC == 1 so STM Clock == FPI */
\r
281 STM_CLC.reg = ( 1UL << 8 );
\r
285 /* Determine how many bits are used without changing other bits in the CMCON register. */
\r
286 STM_CMCON.reg &= ~( 0x1fUL );
\r
287 STM_CMCON.reg |= ( 0x1fUL - __CLZ( configPERIPHERAL_CLOCK_HZ / configTICK_RATE_HZ ) );
\r
289 /* Take into account the current time so a tick doesn't happen immediately. */
\r
290 STM_CMP0.reg = ulCompareMatchValue + STM_TIM0.reg;
\r
292 if( 0 != _install_int_handler( configKERNEL_INTERRUPT_PRIORITY, prvSystemTickHandler, 0 ) )
\r
294 /* Set-up the interrupt. */
\r
295 STM_SRC0.reg = ( configKERNEL_INTERRUPT_PRIORITY | 0x00005000UL );
\r
297 /* Enable the Interrupt. */
\r
298 STM_ISRR.reg &= ~( 0x03UL );
\r
299 STM_ISRR.reg |= 0x1UL;
\r
300 STM_ISRR.reg &= ~( 0x07UL );
\r
301 STM_ICR.reg |= 0x1UL;
\r
305 /* Failed to install the Tick Interrupt. */
\r
306 configASSERT( ( ( volatile void * ) NULL ) );
\r
309 /*-----------------------------------------------------------*/
\r
311 static void prvSystemTickHandler( int iArg )
\r
313 unsigned long ulSavedInterruptMask;
\r
314 unsigned long *pxUpperCSA = NULL;
\r
315 unsigned long xUpperCSA = 0UL;
\r
316 extern volatile unsigned long *pxCurrentTCB;
\r
320 /* Just to avoid compiler warnings about unused parameters. */
\r
323 /* Clear the interrupt source. */
\r
324 STM_ISRR.reg = 1UL;
\r
326 /* Reload the Compare Match register for X ticks into the future.
\r
328 If critical section or interrupt nesting budgets are exceeded, then
\r
329 it is possible that the calculated next compare match value is in the
\r
330 past. If this occurs (unlikely), it is possible that the resulting
\r
331 time slippage will exceed a single tick period. Any adverse effect of
\r
332 this is time bounded by the fact that only the first n bits of the 56 bit
\r
333 STM timer are being used for a compare match, so another compare match
\r
334 will occur after an overflow in just those n bits (not the entire 56 bits).
\r
335 As an example, if the peripheral clock is 75MHz, and the tick rate is 1KHz,
\r
336 a missed tick could result in the next tick interrupt occurring within a
\r
337 time that is 1.7 times the desired period. The fact that this is greater
\r
338 than a single tick period is an effect of using a timer that cannot be
\r
339 automatically reset, in hardware, by the occurrence of a tick interrupt.
\r
340 Changing the tick source to a timer that has an automatic reset on compare
\r
341 match (such as a GPTA timer) will reduce the maximum possible additional
\r
342 period to exactly 1 times the desired period. */
\r
343 STM_CMP0.reg += ulCompareMatchValue;
\r
345 /* Kernel API calls require Critical Sections. */
\r
346 ulSavedInterruptMask = portSET_INTERRUPT_MASK_FROM_ISR();
\r
348 /* Increment the Tick. */
\r
349 vTaskIncrementTick();
\r
351 portCLEAR_INTERRUPT_MASK_FROM_ISR( ulSavedInterruptMask );
\r
353 #if configUSE_PREEMPTION == 1
\r
355 /* Save the context of a task.
\r
356 The upper context is automatically saved when entering a trap or interrupt.
\r
357 Need to save the lower context as well and copy the PCXI CSA ID into
\r
358 pxCurrentTCB->pxTopOfStack. Only Lower Context CSA IDs may be saved to the
\r
361 Call vTaskSwitchContext to select the next task, note that this changes the
\r
362 value of pxCurrentTCB so that it needs to be reloaded.
\r
364 Call vPortSetMPURegisterSetOne to change the MPU mapping for the task
\r
365 that has just been switched in.
\r
367 Load the context of the task.
\r
368 Need to restore the lower context by loading the CSA from
\r
369 pxCurrentTCB->pxTopOfStack into PCXI (effectively changing the call stack).
\r
370 In the Interrupt handler post-amble, RSLCX will restore the lower context
\r
371 of the task. RFE will restore the upper context of the task, jump to the
\r
372 return address and restore the previous state of interrupts being
\r
373 enabled/disabled. */
\r
376 xUpperCSA = _mfcr( $PCXI );
\r
377 pxUpperCSA = portCSA_TO_ADDRESS( xUpperCSA );
\r
378 *pxCurrentTCB = pxUpperCSA[ 0 ];
\r
379 vTaskSwitchContext();
\r
380 pxUpperCSA[ 0 ] = *pxCurrentTCB;
\r
381 CPU_SRC0.bits.SETR = 0;
\r
387 /*-----------------------------------------------------------*/
\r
390 * When a task is deleted, it is yielded permanently until the IDLE task
\r
391 * has an opportunity to reclaim the memory that that task was using.
\r
392 * Typically, the memory used by a task is the TCB and Stack but in the
\r
393 * TriCore this includes the CSAs that were consumed as part of the Call
\r
394 * Stack. These CSAs can only be returned to the Globally Free Pool when
\r
395 * they are not part of the current Call Stack, hence, delaying the
\r
396 * reclamation until the IDLE task is freeing the task's other resources.
\r
397 * This function uses the head of the linked list of CSAs (from when the
\r
398 * task yielded for the last time) and finds the tail (the very bottom of
\r
399 * the call stack) and inserts this list at the head of the Free list,
\r
400 * attaching the existing Free List to the tail of the reclaimed call stack.
\r
402 * NOTE: the IDLE task needs processing time to complete this function
\r
403 * and in heavily loaded systems, the Free CSAs may be consumed faster
\r
404 * than they can be freed assuming that tasks are being spawned and
\r
405 * deleted frequently.
\r
407 void vPortReclaimCSA( unsigned long *pxTCB )
\r
409 unsigned long pxHeadCSA, pxTailCSA, pxFreeCSA;
\r
410 unsigned long *pulNextCSA;
\r
412 /* A pointer to the first CSA in the list of CSAs consumed by the task is
\r
413 stored in the first element of the tasks TCB structure (where the stack
\r
414 pointer would be on a traditional stack based architecture). */
\r
415 pxHeadCSA = ( *pxTCB ) & portCSA_FCX_MASK;
\r
417 /* Mask off everything in the CSA link field other than the address. If
\r
418 the address is NULL, then the CSA is not linking anywhere and there is
\r
420 pxTailCSA = pxHeadCSA;
\r
422 /* Convert the link value to contain just a raw address and store this
\r
423 in a local variable. */
\r
424 pulNextCSA = portCSA_TO_ADDRESS( pxTailCSA );
\r
426 /* Iterate over the CSAs that were consumed as part of the task. The
\r
427 first field in the CSA is the pointer to then next CSA. Mask off
\r
428 everything in the pointer to the next CSA, other than the link address.
\r
429 If this is NULL, then the CSA currently being pointed to is the last in
\r
431 while( 0UL != ( pulNextCSA[ 0 ] & portCSA_FCX_MASK ) )
\r
433 /* Clear all bits of the pointer to the next in the chain, other
\r
434 than the address bits themselves. */
\r
435 pulNextCSA[ 0 ] = pulNextCSA[ 0 ] & portCSA_FCX_MASK;
\r
437 /* Move the pointer to point to the next CSA in the list. */
\r
438 pxTailCSA = pulNextCSA[ 0 ];
\r
440 /* Update the local pointer to the CSA. */
\r
441 pulNextCSA = portCSA_TO_ADDRESS( pxTailCSA );
\r
446 /* Look up the current free CSA head. */
\r
448 pxFreeCSA = _mfcr( $FCX );
\r
450 /* Join the current Free onto the Tail of what is being reclaimed. */
\r
451 portCSA_TO_ADDRESS( pxTailCSA )[ 0 ] = pxFreeCSA;
\r
453 /* Move the head of the reclaimed into the Free. */
\r
455 _mtcr( $FCX, pxHeadCSA );
\r
460 /*-----------------------------------------------------------*/
\r
462 void vPortEndScheduler( void )
\r
464 /* Nothing to do. Unlikely to want to end. */
\r
466 /*-----------------------------------------------------------*/
\r
468 static void prvTrapYield( int iTrapIdentification )
\r
470 unsigned long *pxUpperCSA = NULL;
\r
471 unsigned long xUpperCSA = 0UL;
\r
472 extern volatile unsigned long *pxCurrentTCB;
\r
474 switch( iTrapIdentification )
\r
476 case portSYSCALL_TASK_YIELD:
\r
477 /* Save the context of a task.
\r
478 The upper context is automatically saved when entering a trap or interrupt.
\r
479 Need to save the lower context as well and copy the PCXI CSA ID into
\r
480 pxCurrentTCB->pxTopOfStack. Only Lower Context CSA IDs may be saved to the
\r
483 Call vTaskSwitchContext to select the next task, note that this changes the
\r
484 value of pxCurrentTCB so that it needs to be reloaded.
\r
486 Call vPortSetMPURegisterSetOne to change the MPU mapping for the task
\r
487 that has just been switched in.
\r
489 Load the context of the task.
\r
490 Need to restore the lower context by loading the CSA from
\r
491 pxCurrentTCB->pxTopOfStack into PCXI (effectively changing the call stack).
\r
492 In the Interrupt handler post-amble, RSLCX will restore the lower context
\r
493 of the task. RFE will restore the upper context of the task, jump to the
\r
494 return address and restore the previous state of interrupts being
\r
495 enabled/disabled. */
\r
498 xUpperCSA = _mfcr( $PCXI );
\r
499 pxUpperCSA = portCSA_TO_ADDRESS( xUpperCSA );
\r
500 *pxCurrentTCB = pxUpperCSA[ 0 ];
\r
501 vTaskSwitchContext();
\r
502 pxUpperCSA[ 0 ] = *pxCurrentTCB;
\r
503 CPU_SRC0.bits.SETR = 0;
\r
508 /* Unimplemented trap called. */
\r
509 configASSERT( ( ( volatile void * ) NULL ) );
\r
513 /*-----------------------------------------------------------*/
\r
515 static void prvInterruptYield( int iId )
\r
517 unsigned long *pxUpperCSA = NULL;
\r
518 unsigned long xUpperCSA = 0UL;
\r
519 extern volatile unsigned long *pxCurrentTCB;
\r
521 /* Just to remove compiler warnings. */
\r
524 /* Save the context of a task.
\r
525 The upper context is automatically saved when entering a trap or interrupt.
\r
526 Need to save the lower context as well and copy the PCXI CSA ID into
\r
527 pxCurrentTCB->pxTopOfStack. Only Lower Context CSA IDs may be saved to the
\r
530 Call vTaskSwitchContext to select the next task, note that this changes the
\r
531 value of pxCurrentTCB so that it needs to be reloaded.
\r
533 Call vPortSetMPURegisterSetOne to change the MPU mapping for the task
\r
534 that has just been switched in.
\r
536 Load the context of the task.
\r
537 Need to restore the lower context by loading the CSA from
\r
538 pxCurrentTCB->pxTopOfStack into PCXI (effectively changing the call stack).
\r
539 In the Interrupt handler post-amble, RSLCX will restore the lower context
\r
540 of the task. RFE will restore the upper context of the task, jump to the
\r
541 return address and restore the previous state of interrupts being
\r
542 enabled/disabled. */
\r
545 xUpperCSA = _mfcr( $PCXI );
\r
546 pxUpperCSA = portCSA_TO_ADDRESS( xUpperCSA );
\r
547 *pxCurrentTCB = pxUpperCSA[ 0 ];
\r
548 vTaskSwitchContext();
\r
549 pxUpperCSA[ 0 ] = *pxCurrentTCB;
\r
550 CPU_SRC0.bits.SETR = 0;
\r
554 /*-----------------------------------------------------------*/
\r
556 unsigned long uxPortSetInterruptMaskFromISR( void )
\r
558 unsigned long uxReturn = 0UL;
\r
561 uxReturn = _mfcr( $ICR );
\r
562 _mtcr( $ICR, ( ( uxReturn & ~portCCPN_MASK ) | configMAX_SYSCALL_INTERRUPT_PRIORITY ) );
\r
566 /* Return just the interrupt mask bits. */
\r
567 return ( uxReturn & portCCPN_MASK );
\r
569 /*-----------------------------------------------------------*/
\r