2 * FreeRTOS Kernel V10.3.1
\r
3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
27 /* Standard includes. */
\r
31 /* TriCore specific includes. */
\r
33 #include <machine/intrinsics.h>
\r
34 #include <machine/cint.h>
\r
35 #include <machine/wdtcon.h>
\r
37 /* Kernel includes. */
\r
38 #include "FreeRTOS.h"
\r
42 #if configCHECK_FOR_STACK_OVERFLOW > 0
\r
43 #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
44 /* The stack pointer is accessible using portCSA_TO_ADDRESS( portCSA_TO_ADDRESS( pxCurrentTCB->pxTopOfStack )[ 0 ] )[ 2 ]; */
\r
45 #endif /* configCHECK_FOR_STACK_OVERFLOW */
\r
48 /*-----------------------------------------------------------*/
\r
50 /* System register Definitions. */
\r
51 #define portSYSTEM_PROGRAM_STATUS_WORD ( 0x000008FFUL ) /* Supervisor Mode, MPU Register Set 0 and Call Depth Counting disabled. */
\r
52 #define portINITIAL_PRIVILEGED_PROGRAM_STATUS_WORD ( 0x000014FFUL ) /* IO Level 1, MPU Register Set 1 and Call Depth Counting disabled. */
\r
53 #define portINITIAL_UNPRIVILEGED_PROGRAM_STATUS_WORD ( 0x000010FFUL ) /* IO Level 0, MPU Register Set 1 and Call Depth Counting disabled. */
\r
54 #define portINITIAL_PCXI_UPPER_CONTEXT_WORD ( 0x00C00000UL ) /* The lower 20 bits identify the CSA address. */
\r
55 #define portINITIAL_SYSCON ( 0x00000000UL ) /* MPU Disable. */
\r
57 /* CSA manipulation macros. */
\r
58 #define portCSA_FCX_MASK ( 0x000FFFFFUL )
\r
60 /* OS Interrupt and Trap mechanisms. */
\r
61 #define portRESTORE_PSW_MASK ( ~( 0x000000FFUL ) )
\r
62 #define portSYSCALL_TRAP ( 6 )
\r
64 /* Each CSA contains 16 words of data. */
\r
65 #define portNUM_WORDS_IN_CSA ( 16 )
\r
67 /* The interrupt enable bit in the PCP_SRC register. */
\r
68 #define portENABLE_CPU_INTERRUPT ( 1U << 12U )
\r
69 /*-----------------------------------------------------------*/
\r
72 * Perform any hardware configuration necessary to generate the tick interrupt.
\r
74 static void prvSystemTickHandler( int ) __attribute__((longcall));
\r
75 static void prvSetupTimerInterrupt( void );
\r
78 * Trap handler for yields.
\r
80 static void prvTrapYield( int iTrapIdentification );
\r
83 * Priority 1 interrupt handler for yields pended from an interrupt.
\r
85 static void prvInterruptYield( int iTrapIdentification );
\r
87 /*-----------------------------------------------------------*/
\r
89 /* This reference is required by the save/restore context macros. */
\r
90 extern volatile uint32_t *pxCurrentTCB;
\r
92 /* Precalculate the compare match value at compile time. */
\r
93 static const uint32_t ulCompareMatchValue = ( configPERIPHERAL_CLOCK_HZ / configTICK_RATE_HZ );
\r
95 /*-----------------------------------------------------------*/
\r
97 StackType_t *pxPortInitialiseStack( StackType_t * pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\r
99 uint32_t *pulUpperCSA = NULL;
\r
100 uint32_t *pulLowerCSA = NULL;
\r
102 /* 16 Address Registers (4 Address registers are global), 16 Data
\r
103 Registers, and 3 System Registers.
\r
105 There are 3 registers that track the CSAs.
\r
106 FCX points to the head of globally free set of CSAs.
\r
107 PCX for the task needs to point to Lower->Upper->NULL arrangement.
\r
108 LCX points to the last free CSA so that corrective action can be taken.
\r
110 Need two CSAs to store the context of a task.
\r
111 The upper context contains D8-D15, A10-A15, PSW and PCXI->NULL.
\r
112 The lower context contains D0-D7, A2-A7, A11 and PCXI->UpperContext.
\r
113 The pxCurrentTCB->pxTopOfStack points to the Lower Context RSLCX matching the initial BISR.
\r
114 The Lower Context points to the Upper Context ready for the return from the interrupt handler.
\r
116 The Real stack pointer for the task is stored in the A10 which is restored
\r
117 with the upper context. */
\r
119 /* Have to disable interrupts here because the CSAs are going to be
\r
121 portENTER_CRITICAL();
\r
123 /* DSync to ensure that buffering is not a problem. */
\r
126 /* Consume two free CSAs. */
\r
127 pulLowerCSA = portCSA_TO_ADDRESS( __MFCR( $FCX ) );
\r
128 if( NULL != pulLowerCSA )
\r
130 /* The Lower Links to the Upper. */
\r
131 pulUpperCSA = portCSA_TO_ADDRESS( pulLowerCSA[ 0 ] );
\r
134 /* Check that we have successfully reserved two CSAs. */
\r
135 if( ( NULL != pulLowerCSA ) && ( NULL != pulUpperCSA ) )
\r
137 /* Remove the two consumed CSAs from the free CSA list. */
\r
140 _mtcr( $FCX, pulUpperCSA[ 0 ] );
\r
146 /* Simply trigger a context list depletion trap. */
\r
150 portEXIT_CRITICAL();
\r
152 /* Clear the upper CSA. */
\r
153 memset( pulUpperCSA, 0, portNUM_WORDS_IN_CSA * sizeof( uint32_t ) );
\r
155 /* Upper Context. */
\r
156 pulUpperCSA[ 2 ] = ( uint32_t )pxTopOfStack; /* A10; Stack Return aka Stack Pointer */
\r
157 pulUpperCSA[ 1 ] = portSYSTEM_PROGRAM_STATUS_WORD; /* PSW */
\r
159 /* Clear the lower CSA. */
\r
160 memset( pulLowerCSA, 0, portNUM_WORDS_IN_CSA * sizeof( uint32_t ) );
\r
162 /* Lower Context. */
\r
163 pulLowerCSA[ 8 ] = ( uint32_t ) pvParameters; /* A4; Address Type Parameter Register */
\r
164 pulLowerCSA[ 1 ] = ( uint32_t ) pxCode; /* A11; Return Address aka RA */
\r
166 /* PCXI pointing to the Upper context. */
\r
167 pulLowerCSA[ 0 ] = ( portINITIAL_PCXI_UPPER_CONTEXT_WORD | ( uint32_t ) portADDRESS_TO_CSA( pulUpperCSA ) );
\r
169 /* Save the link to the CSA in the top of stack. */
\r
170 pxTopOfStack = (uint32_t * ) portADDRESS_TO_CSA( pulLowerCSA );
\r
172 /* DSync to ensure that buffering is not a problem. */
\r
175 return pxTopOfStack;
\r
177 /*-----------------------------------------------------------*/
\r
179 int32_t xPortStartScheduler( void )
\r
181 extern void vTrapInstallHandlers( void );
\r
182 uint32_t ulMFCR = 0UL;
\r
183 uint32_t *pulUpperCSA = NULL;
\r
184 uint32_t *pulLowerCSA = NULL;
\r
186 /* Interrupts at or below configMAX_SYSCALL_INTERRUPT_PRIORITY are disable
\r
187 when this function is called. */
\r
189 /* Set-up the timer interrupt. */
\r
190 prvSetupTimerInterrupt();
\r
192 /* Install the Trap Handlers. */
\r
193 vTrapInstallHandlers();
\r
195 /* Install the Syscall Handler for yield calls. */
\r
196 if( 0 == _install_trap_handler( portSYSCALL_TRAP, prvTrapYield ) )
\r
198 /* Failed to install the yield handler, force an assert. */
\r
199 configASSERT( ( ( volatile void * ) NULL ) );
\r
202 /* Enable then install the priority 1 interrupt for pending context
\r
203 switches from an ISR. See mod_SRC in the TriCore manual. */
\r
204 CPU_SRC0.reg = ( portENABLE_CPU_INTERRUPT ) | ( configKERNEL_YIELD_PRIORITY );
\r
205 if( 0 == _install_int_handler( configKERNEL_YIELD_PRIORITY, prvInterruptYield, 0 ) )
\r
207 /* Failed to install the yield handler, force an assert. */
\r
208 configASSERT( ( ( volatile void * ) NULL ) );
\r
213 /* Load the initial SYSCON. */
\r
214 _mtcr( $SYSCON, portINITIAL_SYSCON );
\r
217 /* ENDINIT has already been applied in the 'cstart.c' code. */
\r
219 /* Clear the PSW.CDC to enable the use of an RFE without it generating an
\r
220 exception because this code is not genuinely in an exception. */
\r
221 ulMFCR = __MFCR( $PSW );
\r
222 ulMFCR &= portRESTORE_PSW_MASK;
\r
224 _mtcr( $PSW, ulMFCR );
\r
227 /* Finally, perform the equivalent of a portRESTORE_CONTEXT() */
\r
228 pulLowerCSA = portCSA_TO_ADDRESS( ( *pxCurrentTCB ) );
\r
229 pulUpperCSA = portCSA_TO_ADDRESS( pulLowerCSA[0] );
\r
231 _mtcr( $PCXI, *pxCurrentTCB );
\r
237 /* Return to the first task selected to execute. */
\r
238 __asm volatile( "rfe" );
\r
240 /* Will not get here. */
\r
243 /*-----------------------------------------------------------*/
\r
245 static void prvSetupTimerInterrupt( void )
\r
247 /* Set-up the clock divider. */
\r
250 /* Wait until access to Endint protected register is enabled. */
\r
251 while( 0 != ( WDT_CON0.reg & 0x1UL ) );
\r
253 /* RMC == 1 so STM Clock == FPI */
\r
254 STM_CLC.reg = ( 1UL << 8 );
\r
258 /* Determine how many bits are used without changing other bits in the CMCON register. */
\r
259 STM_CMCON.reg &= ~( 0x1fUL );
\r
260 STM_CMCON.reg |= ( 0x1fUL - __CLZ( configPERIPHERAL_CLOCK_HZ / configTICK_RATE_HZ ) );
\r
262 /* Take into account the current time so a tick doesn't happen immediately. */
\r
263 STM_CMP0.reg = ulCompareMatchValue + STM_TIM0.reg;
\r
265 if( 0 != _install_int_handler( configKERNEL_INTERRUPT_PRIORITY, prvSystemTickHandler, 0 ) )
\r
267 /* Set-up the interrupt. */
\r
268 STM_SRC0.reg = ( configKERNEL_INTERRUPT_PRIORITY | 0x00005000UL );
\r
270 /* Enable the Interrupt. */
\r
271 STM_ISRR.reg &= ~( 0x03UL );
\r
272 STM_ISRR.reg |= 0x1UL;
\r
273 STM_ISRR.reg &= ~( 0x07UL );
\r
274 STM_ICR.reg |= 0x1UL;
\r
278 /* Failed to install the Tick Interrupt. */
\r
279 configASSERT( ( ( volatile void * ) NULL ) );
\r
282 /*-----------------------------------------------------------*/
\r
284 static void prvSystemTickHandler( int iArg )
\r
286 uint32_t ulSavedInterruptMask;
\r
287 uint32_t *pxUpperCSA = NULL;
\r
288 uint32_t xUpperCSA = 0UL;
\r
289 extern volatile uint32_t *pxCurrentTCB;
\r
290 int32_t lYieldRequired;
\r
292 /* Just to avoid compiler warnings about unused parameters. */
\r
295 /* Clear the interrupt source. */
\r
296 STM_ISRR.reg = 1UL;
\r
298 /* Reload the Compare Match register for X ticks into the future.
\r
300 If critical section or interrupt nesting budgets are exceeded, then
\r
301 it is possible that the calculated next compare match value is in the
\r
302 past. If this occurs (unlikely), it is possible that the resulting
\r
303 time slippage will exceed a single tick period. Any adverse effect of
\r
304 this is time bounded by the fact that only the first n bits of the 56 bit
\r
305 STM timer are being used for a compare match, so another compare match
\r
306 will occur after an overflow in just those n bits (not the entire 56 bits).
\r
307 As an example, if the peripheral clock is 75MHz, and the tick rate is 1KHz,
\r
308 a missed tick could result in the next tick interrupt occurring within a
\r
309 time that is 1.7 times the desired period. The fact that this is greater
\r
310 than a single tick period is an effect of using a timer that cannot be
\r
311 automatically reset, in hardware, by the occurrence of a tick interrupt.
\r
312 Changing the tick source to a timer that has an automatic reset on compare
\r
313 match (such as a GPTA timer) will reduce the maximum possible additional
\r
314 period to exactly 1 times the desired period. */
\r
315 STM_CMP0.reg += ulCompareMatchValue;
\r
317 /* Kernel API calls require Critical Sections. */
\r
318 ulSavedInterruptMask = portSET_INTERRUPT_MASK_FROM_ISR();
\r
320 /* Increment the Tick. */
\r
321 lYieldRequired = xTaskIncrementTick();
\r
323 portCLEAR_INTERRUPT_MASK_FROM_ISR( ulSavedInterruptMask );
\r
325 if( lYieldRequired != pdFALSE )
\r
327 /* Save the context of a task.
\r
328 The upper context is automatically saved when entering a trap or interrupt.
\r
329 Need to save the lower context as well and copy the PCXI CSA ID into
\r
330 pxCurrentTCB->pxTopOfStack. Only Lower Context CSA IDs may be saved to the
\r
333 Call vTaskSwitchContext to select the next task, note that this changes the
\r
334 value of pxCurrentTCB so that it needs to be reloaded.
\r
336 Call vPortSetMPURegisterSetOne to change the MPU mapping for the task
\r
337 that has just been switched in.
\r
339 Load the context of the task.
\r
340 Need to restore the lower context by loading the CSA from
\r
341 pxCurrentTCB->pxTopOfStack into PCXI (effectively changing the call stack).
\r
342 In the Interrupt handler post-amble, RSLCX will restore the lower context
\r
343 of the task. RFE will restore the upper context of the task, jump to the
\r
344 return address and restore the previous state of interrupts being
\r
345 enabled/disabled. */
\r
348 xUpperCSA = __MFCR( $PCXI );
\r
349 pxUpperCSA = portCSA_TO_ADDRESS( xUpperCSA );
\r
350 *pxCurrentTCB = pxUpperCSA[ 0 ];
\r
351 vTaskSwitchContext();
\r
352 pxUpperCSA[ 0 ] = *pxCurrentTCB;
\r
353 CPU_SRC0.bits.SETR = 0;
\r
357 /*-----------------------------------------------------------*/
\r
360 * When a task is deleted, it is yielded permanently until the IDLE task
\r
361 * has an opportunity to reclaim the memory that that task was using.
\r
362 * Typically, the memory used by a task is the TCB and Stack but in the
\r
363 * TriCore this includes the CSAs that were consumed as part of the Call
\r
364 * Stack. These CSAs can only be returned to the Globally Free Pool when
\r
365 * they are not part of the current Call Stack, hence, delaying the
\r
366 * reclamation until the IDLE task is freeing the task's other resources.
\r
367 * This function uses the head of the linked list of CSAs (from when the
\r
368 * task yielded for the last time) and finds the tail (the very bottom of
\r
369 * the call stack) and inserts this list at the head of the Free list,
\r
370 * attaching the existing Free List to the tail of the reclaimed call stack.
\r
372 * NOTE: the IDLE task needs processing time to complete this function
\r
373 * and in heavily loaded systems, the Free CSAs may be consumed faster
\r
374 * than they can be freed assuming that tasks are being spawned and
\r
375 * deleted frequently.
\r
377 void vPortReclaimCSA( uint32_t *pxTCB )
\r
379 uint32_t pxHeadCSA, pxTailCSA, pxFreeCSA;
\r
380 uint32_t *pulNextCSA;
\r
382 /* A pointer to the first CSA in the list of CSAs consumed by the task is
\r
383 stored in the first element of the tasks TCB structure (where the stack
\r
384 pointer would be on a traditional stack based architecture). */
\r
385 pxHeadCSA = ( *pxTCB ) & portCSA_FCX_MASK;
\r
387 /* Mask off everything in the CSA link field other than the address. If
\r
388 the address is NULL, then the CSA is not linking anywhere and there is
\r
390 pxTailCSA = pxHeadCSA;
\r
392 /* Convert the link value to contain just a raw address and store this
\r
393 in a local variable. */
\r
394 pulNextCSA = portCSA_TO_ADDRESS( pxTailCSA );
\r
396 /* Iterate over the CSAs that were consumed as part of the task. The
\r
397 first field in the CSA is the pointer to then next CSA. Mask off
\r
398 everything in the pointer to the next CSA, other than the link address.
\r
399 If this is NULL, then the CSA currently being pointed to is the last in
\r
401 while( 0UL != ( pulNextCSA[ 0 ] & portCSA_FCX_MASK ) )
\r
403 /* Clear all bits of the pointer to the next in the chain, other
\r
404 than the address bits themselves. */
\r
405 pulNextCSA[ 0 ] = pulNextCSA[ 0 ] & portCSA_FCX_MASK;
\r
407 /* Move the pointer to point to the next CSA in the list. */
\r
408 pxTailCSA = pulNextCSA[ 0 ];
\r
410 /* Update the local pointer to the CSA. */
\r
411 pulNextCSA = portCSA_TO_ADDRESS( pxTailCSA );
\r
416 /* Look up the current free CSA head. */
\r
418 pxFreeCSA = __MFCR( $FCX );
\r
420 /* Join the current Free onto the Tail of what is being reclaimed. */
\r
421 portCSA_TO_ADDRESS( pxTailCSA )[ 0 ] = pxFreeCSA;
\r
423 /* Move the head of the reclaimed into the Free. */
\r
425 _mtcr( $FCX, pxHeadCSA );
\r
430 /*-----------------------------------------------------------*/
\r
432 void vPortEndScheduler( void )
\r
434 /* Nothing to do. Unlikely to want to end. */
\r
436 /*-----------------------------------------------------------*/
\r
438 static void prvTrapYield( int iTrapIdentification )
\r
440 uint32_t *pxUpperCSA = NULL;
\r
441 uint32_t xUpperCSA = 0UL;
\r
442 extern volatile uint32_t *pxCurrentTCB;
\r
444 switch( iTrapIdentification )
\r
446 case portSYSCALL_TASK_YIELD:
\r
447 /* Save the context of a task.
\r
448 The upper context is automatically saved when entering a trap or interrupt.
\r
449 Need to save the lower context as well and copy the PCXI CSA ID into
\r
450 pxCurrentTCB->pxTopOfStack. Only Lower Context CSA IDs may be saved to the
\r
453 Call vTaskSwitchContext to select the next task, note that this changes the
\r
454 value of pxCurrentTCB so that it needs to be reloaded.
\r
456 Call vPortSetMPURegisterSetOne to change the MPU mapping for the task
\r
457 that has just been switched in.
\r
459 Load the context of the task.
\r
460 Need to restore the lower context by loading the CSA from
\r
461 pxCurrentTCB->pxTopOfStack into PCXI (effectively changing the call stack).
\r
462 In the Interrupt handler post-amble, RSLCX will restore the lower context
\r
463 of the task. RFE will restore the upper context of the task, jump to the
\r
464 return address and restore the previous state of interrupts being
\r
465 enabled/disabled. */
\r
468 xUpperCSA = __MFCR( $PCXI );
\r
469 pxUpperCSA = portCSA_TO_ADDRESS( xUpperCSA );
\r
470 *pxCurrentTCB = pxUpperCSA[ 0 ];
\r
471 vTaskSwitchContext();
\r
472 pxUpperCSA[ 0 ] = *pxCurrentTCB;
\r
473 CPU_SRC0.bits.SETR = 0;
\r
478 /* Unimplemented trap called. */
\r
479 configASSERT( ( ( volatile void * ) NULL ) );
\r
483 /*-----------------------------------------------------------*/
\r
485 static void prvInterruptYield( int iId )
\r
487 uint32_t *pxUpperCSA = NULL;
\r
488 uint32_t xUpperCSA = 0UL;
\r
489 extern volatile uint32_t *pxCurrentTCB;
\r
491 /* Just to remove compiler warnings. */
\r
494 /* Save the context of a task.
\r
495 The upper context is automatically saved when entering a trap or interrupt.
\r
496 Need to save the lower context as well and copy the PCXI CSA ID into
\r
497 pxCurrentTCB->pxTopOfStack. Only Lower Context CSA IDs may be saved to the
\r
500 Call vTaskSwitchContext to select the next task, note that this changes the
\r
501 value of pxCurrentTCB so that it needs to be reloaded.
\r
503 Call vPortSetMPURegisterSetOne to change the MPU mapping for the task
\r
504 that has just been switched in.
\r
506 Load the context of the task.
\r
507 Need to restore the lower context by loading the CSA from
\r
508 pxCurrentTCB->pxTopOfStack into PCXI (effectively changing the call stack).
\r
509 In the Interrupt handler post-amble, RSLCX will restore the lower context
\r
510 of the task. RFE will restore the upper context of the task, jump to the
\r
511 return address and restore the previous state of interrupts being
\r
512 enabled/disabled. */
\r
515 xUpperCSA = __MFCR( $PCXI );
\r
516 pxUpperCSA = portCSA_TO_ADDRESS( xUpperCSA );
\r
517 *pxCurrentTCB = pxUpperCSA[ 0 ];
\r
518 vTaskSwitchContext();
\r
519 pxUpperCSA[ 0 ] = *pxCurrentTCB;
\r
520 CPU_SRC0.bits.SETR = 0;
\r
523 /*-----------------------------------------------------------*/
\r
525 uint32_t uxPortSetInterruptMaskFromISR( void )
\r
527 uint32_t uxReturn = 0UL;
\r
530 uxReturn = __MFCR( $ICR );
\r
531 _mtcr( $ICR, ( ( uxReturn & ~portCCPN_MASK ) | configMAX_SYSCALL_INTERRUPT_PRIORITY ) );
\r
535 /* Return just the interrupt mask bits. */
\r
536 return ( uxReturn & portCCPN_MASK );
\r
538 /*-----------------------------------------------------------*/
\r