2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
29 /*-----------------------------------------------------------
30 * Implementation of functions defined in portable.h for the SH2A port.
31 *----------------------------------------------------------*/
33 /* Standard C includes. */
36 /* Scheduler includes. */
40 /* Library includes. */
43 /* Hardware specifics. */
44 #if ( configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H == 1 )
48 #else /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
52 #endif /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
53 /*-----------------------------------------------------------*/
55 /* Tasks should start with interrupts enabled and in Supervisor mode, therefore
56 * PSW is set with U and I set, and PM and IPL clear. */
57 #define portINITIAL_PSW ( ( StackType_t ) 0x00030000 )
59 /* The peripheral clock is divided by this value before being supplying the
61 #if ( configUSE_TICKLESS_IDLE == 0 )
62 /* If tickless idle is not used then the divisor can be fixed. */
63 #define portCLOCK_DIVISOR 8UL
64 #elif ( configPERIPHERAL_CLOCK_HZ >= 12000000 )
65 #define portCLOCK_DIVISOR 512UL
66 #elif ( configPERIPHERAL_CLOCK_HZ >= 6000000 )
67 #define portCLOCK_DIVISOR 128UL
68 #elif ( configPERIPHERAL_CLOCK_HZ >= 1000000 )
69 #define portCLOCK_DIVISOR 32UL
71 #define portCLOCK_DIVISOR 8UL
74 /* These macros allow a critical section to be added around the call to
75 * xTaskIncrementTick(), which is only ever called from interrupts at the kernel
76 * priority - ie a known priority. Therefore these local macros are a slight
77 * optimisation compared to calling the global SET/CLEAR_INTERRUPT_MASK macros,
78 * which would require the old IPL to be read first and stored in a local variable. */
79 #define portDISABLE_INTERRUPTS_FROM_KERNEL_ISR() __asm volatile ( "MVTIPL %0" ::"i" ( configMAX_SYSCALL_INTERRUPT_PRIORITY ) )
80 #define portENABLE_INTERRUPTS_FROM_KERNEL_ISR() __asm volatile ( "MVTIPL %0" ::"i" ( configKERNEL_INTERRUPT_PRIORITY ) )
82 /* Keys required to lock and unlock access to certain system registers
84 #define portUNLOCK_KEY 0xA50B
85 #define portLOCK_KEY 0xA500
87 /*-----------------------------------------------------------*/
90 * Function to start the first task executing - written in asm code as direct
91 * access to registers is required.
93 static void prvStartFirstTask( void ) __attribute__( ( naked ) );
96 * Software interrupt handler. Performs the actual context switch (saving and
97 * restoring of registers). Written in asm code as direct register access is
100 #if ( configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H == 1 )
102 R_BSP_PRAGMA_INTERRUPT( vPortSoftwareInterruptISR, VECT( ICU, SWINT ) )
103 R_BSP_ATTRIB_INTERRUPT void vPortSoftwareInterruptISR( void ) __attribute__( ( naked ) );
105 #else /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
107 void vPortSoftwareInterruptISR( void ) __attribute__( ( naked ) );
109 #endif /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
112 * The tick ISR handler. The peripheral used is configured by the application
113 * via a hook/callback function.
115 #if ( configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H == 1 )
117 R_BSP_PRAGMA_INTERRUPT( vPortTickISR, _VECT( configTICK_VECTOR ) )
118 R_BSP_ATTRIB_INTERRUPT void vPortTickISR( void ); /* Do not add __attribute__( ( interrupt ) ). */
120 #else /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
122 void vPortTickISR( void ) __attribute__( ( interrupt ) );
124 #endif /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
127 * Sets up the periodic ISR used for the RTOS tick using the CMT.
128 * The application writer can define configSETUP_TICK_INTERRUPT() (in
129 * FreeRTOSConfig.h) such that their own tick interrupt configuration is used
130 * in place of prvSetupTimerInterrupt().
132 static void prvSetupTimerInterrupt( void );
133 #ifndef configSETUP_TICK_INTERRUPT
135 /* The user has not provided their own tick interrupt configuration so use
136 * the definition in this file (which uses the interval timer). */
137 #define configSETUP_TICK_INTERRUPT() prvSetupTimerInterrupt()
138 #endif /* configSETUP_TICK_INTERRUPT */
141 * Called after the sleep mode registers have been configured, prvSleep()
142 * executes the pre and post sleep macros, and actually calls the wait
145 #if configUSE_TICKLESS_IDLE == 1
146 static void prvSleep( TickType_t xExpectedIdleTime );
147 #endif /* configUSE_TICKLESS_IDLE */
149 /*-----------------------------------------------------------*/
151 /* Used in the context save and restore code. */
152 extern void * pxCurrentTCB;
154 /* Calculate how many clock increments make up a single tick period. */
155 static const uint32_t ulMatchValueForOneTick = ( ( configPERIPHERAL_CLOCK_HZ / portCLOCK_DIVISOR ) / configTICK_RATE_HZ );
157 #if configUSE_TICKLESS_IDLE == 1
159 /* Holds the maximum number of ticks that can be suppressed - which is
160 * basically how far into the future an interrupt can be generated. Set
161 * during initialisation. This is the maximum possible value that the
162 * compare match register can hold divided by ulMatchValueForOneTick. */
163 static const TickType_t xMaximumPossibleSuppressedTicks = USHRT_MAX / ( ( configPERIPHERAL_CLOCK_HZ / portCLOCK_DIVISOR ) / configTICK_RATE_HZ );
165 /* Flag set from the tick interrupt to allow the sleep processing to know if
166 * sleep mode was exited because of a tick interrupt, or an interrupt
167 * generated by something else. */
168 static volatile uint32_t ulTickFlag = pdFALSE;
170 /* The CMT counter is stopped temporarily each time it is re-programmed.
171 * The following constant offsets the CMT counter match value by the number of
172 * CMT counts that would typically be missed while the counter was stopped to
173 * compensate for the lost time. The large difference between the divided CMT
174 * clock and the CPU clock means it is likely ulStoppedTimerCompensation will
175 * equal zero - and be optimised away. */
176 static const uint32_t ulStoppedTimerCompensation = 100UL / ( configCPU_CLOCK_HZ / ( configPERIPHERAL_CLOCK_HZ / portCLOCK_DIVISOR ) );
178 #endif /* if configUSE_TICKLESS_IDLE == 1 */
180 /*-----------------------------------------------------------*/
183 * See header file for description.
185 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
186 TaskFunction_t pxCode,
187 void * pvParameters )
189 /* Offset to end up on 8 byte boundary. */
192 /* R0 is not included as it is the stack pointer. */
193 *pxTopOfStack = 0x00;
195 *pxTopOfStack = 0x00;
197 *pxTopOfStack = portINITIAL_PSW;
199 *pxTopOfStack = ( StackType_t ) pxCode;
201 /* When debugging it can be useful if every register is set to a known
202 * value. Otherwise code space can be saved by just setting the registers
203 * that need to be set. */
204 #ifdef USE_FULL_REGISTER_INITIALISATION
207 *pxTopOfStack = 0x12345678; /* r15. */
209 *pxTopOfStack = 0xaaaabbbb;
211 *pxTopOfStack = 0xdddddddd;
213 *pxTopOfStack = 0xcccccccc;
215 *pxTopOfStack = 0xbbbbbbbb;
217 *pxTopOfStack = 0xaaaaaaaa;
219 *pxTopOfStack = 0x99999999;
221 *pxTopOfStack = 0x88888888;
223 *pxTopOfStack = 0x77777777;
225 *pxTopOfStack = 0x66666666;
227 *pxTopOfStack = 0x55555555;
229 *pxTopOfStack = 0x44444444;
231 *pxTopOfStack = 0x33333333;
233 *pxTopOfStack = 0x22222222;
236 #else /* ifdef USE_FULL_REGISTER_INITIALISATION */
238 /* Leave space for the registers that will get popped from the stack
239 * when the task first starts executing. */
242 #endif /* ifdef USE_FULL_REGISTER_INITIALISATION */
244 *pxTopOfStack = ( StackType_t ) pvParameters; /* R1 */
246 *pxTopOfStack = 0x12345678; /* Accumulator. */
248 *pxTopOfStack = 0x87654321; /* Accumulator. */
252 /*-----------------------------------------------------------*/
254 BaseType_t xPortStartScheduler( void )
256 /* Use pxCurrentTCB just so it does not get optimised away. */
257 if( pxCurrentTCB != NULL )
259 /* Call an application function to set up the timer that will generate
260 * the tick interrupt. This way the application can decide which
261 * peripheral to use. If tickless mode is used then the default
262 * implementation defined in this file (which uses CMT0) should not be
264 configSETUP_TICK_INTERRUPT();
266 /* Enable the software interrupt. */
267 _IEN( _ICU_SWINT ) = 1;
269 /* Ensure the software interrupt is clear. */
270 _IR( _ICU_SWINT ) = 0;
272 /* Ensure the software interrupt is set to the kernel priority. */
273 _IPR( _ICU_SWINT ) = configKERNEL_INTERRUPT_PRIORITY;
275 /* Start the first task. */
279 /* Execution should not reach here as the tasks are now running!
280 * prvSetupTimerInterrupt() is called here to prevent the compiler outputting
281 * a warning about a statically declared function not being referenced in the
282 * case that the application writer has provided their own tick interrupt
283 * configuration routine (and defined configSETUP_TICK_INTERRUPT() such that
284 * their own routine will be called in place of prvSetupTimerInterrupt()). */
285 prvSetupTimerInterrupt();
287 /* Should not get here. */
290 /*-----------------------------------------------------------*/
292 void vPortEndScheduler( void )
294 /* Not implemented in ports where there is nothing to return to.
295 * Artificially force an assert. */
296 configASSERT( pxCurrentTCB == NULL );
298 /*-----------------------------------------------------------*/
300 static void prvStartFirstTask( void )
305 /* When starting the scheduler there is nothing that needs moving to the
306 * interrupt stack because the function is not called from an interrupt.
307 * Just ensure the current stack is the user stack. */
311 /* Obtain the location of the stack associated with which ever task
312 * pxCurrentTCB is currently pointing to. */
313 "MOV.L #_pxCurrentTCB, R15 \n" \
314 "MOV.L [R15], R15 \n" \
315 "MOV.L [R15], R0 \n" \
318 /* Restore the registers from the stack of the task pointed to by
322 /* Accumulator low 32 bits. */
326 /* Accumulator high 32 bits. */
329 /* R1 to R15 - R0 is not included as it is the SP. */
332 /* This pops the remaining registers. */
338 /*-----------------------------------------------------------*/
340 void vPortSoftwareInterruptISR( void )
344 /* Re-enable interrupts. */
348 /* Move the data that was automatically pushed onto the interrupt stack when
349 * the interrupt occurred from the interrupt stack to the user stack.
351 * R15 is saved before it is clobbered. */
354 /* Read the user stack pointer. */
357 /* Move the address down to the data being moved. */
361 /* Copy the data across, R15, then PC, then PSW. */
362 "MOV.L [ R0 ], [ R15 ] \n" \
363 "MOV.L 4[ R0 ], 4[ R15 ] \n" \
364 "MOV.L 8[ R0 ], 8[ R15 ] \n" \
366 /* Move the interrupt stack pointer to its new correct position. */
369 /* All the rest of the registers are saved directly to the user stack. */
372 /* Save the rest of the general registers (R15 has been saved already). */
375 /* Save the accumulator. */
382 /* Shifted left as it is restored to the low order word. */
386 /* Save the stack pointer to the TCB. */
387 "MOV.L #_pxCurrentTCB, R15 \n" \
388 "MOV.L [ R15 ], R15 \n" \
389 "MOV.L R0, [ R15 ] \n" \
392 /* Ensure the interrupt mask is set to the syscall priority while the kernel
393 * structures are being accessed. */
396 /* Select the next task to run. */
397 "BSR.A _vTaskSwitchContext \n" \
399 /* Reset the interrupt mask as no more data structure access is required. */
403 /* Load the stack pointer of the task that is now selected as the Running
404 * state task from its TCB. */
405 "MOV.L #_pxCurrentTCB,R15 \n" \
406 "MOV.L [ R15 ], R15 \n" \
407 "MOV.L [ R15 ], R0 \n" \
410 /* Restore the context of the new task. The PSW (Program Status Word) and
411 * PC will be popped by the RTE instruction. */
420 ::"i" ( configMAX_SYSCALL_INTERRUPT_PRIORITY ), "i" ( configKERNEL_INTERRUPT_PRIORITY )
423 /*-----------------------------------------------------------*/
425 void vPortTickISR( void )
427 /* Re-enabled interrupts. */
428 __asm volatile ( "SETPSW I" );
430 /* Increment the tick, and perform any processing the new tick value
431 * necessitates. Ensure IPL is at the max syscall value first. */
432 portDISABLE_INTERRUPTS_FROM_KERNEL_ISR();
434 if( xTaskIncrementTick() != pdFALSE )
439 portENABLE_INTERRUPTS_FROM_KERNEL_ISR();
441 #if configUSE_TICKLESS_IDLE == 1
443 /* The CPU woke because of a tick. */
446 /* If this is the first tick since exiting tickless mode then the CMT
447 * compare match value needs resetting. */
448 CMT0.CMCOR = ( uint16_t ) ulMatchValueForOneTick;
452 /*-----------------------------------------------------------*/
454 uint32_t ulPortGetIPL( void )
463 /* This will never get executed, but keeps the compiler from complaining. */
466 /*-----------------------------------------------------------*/
468 void vPortSetIPL( uint32_t ulNewIPL )
475 "AND #-0F000001H, R5 \n" \
482 /*-----------------------------------------------------------*/
484 static void prvSetupTimerInterrupt( void )
487 SYSTEM.PRCR.WORD = portUNLOCK_KEY;
493 SYSTEM.PRCR.WORD = portLOCK_KEY;
495 /* Interrupt on compare match. */
496 CMT0.CMCR.BIT.CMIE = 1;
498 /* Set the compare match value. */
499 CMT0.CMCOR = ( uint16_t ) ulMatchValueForOneTick;
501 /* Divide the PCLK. */
502 #if portCLOCK_DIVISOR == 512
504 CMT0.CMCR.BIT.CKS = 3;
506 #elif portCLOCK_DIVISOR == 128
508 CMT0.CMCR.BIT.CKS = 2;
510 #elif portCLOCK_DIVISOR == 32
512 CMT0.CMCR.BIT.CKS = 1;
514 #elif portCLOCK_DIVISOR == 8
516 CMT0.CMCR.BIT.CKS = 0;
518 #else /* if portCLOCK_DIVISOR == 512 */
520 #error Invalid portCLOCK_DIVISOR setting
522 #endif /* if portCLOCK_DIVISOR == 512 */
524 /* Enable the interrupt... */
525 _IEN( _CMT0_CMI0 ) = 1;
527 /* ...and set its priority to the application defined kernel priority. */
528 _IPR( _CMT0_CMI0 ) = configKERNEL_INTERRUPT_PRIORITY;
530 /* Start the timer. */
531 CMT.CMSTR0.BIT.STR0 = 1;
533 /*-----------------------------------------------------------*/
535 #if configUSE_TICKLESS_IDLE == 1
537 static void prvSleep( TickType_t xExpectedIdleTime )
539 /* Allow the application to define some pre-sleep processing. */
540 configPRE_SLEEP_PROCESSING( xExpectedIdleTime );
542 /* xExpectedIdleTime being set to 0 by configPRE_SLEEP_PROCESSING()
543 * means the application defined code has already executed the WAIT
545 if( xExpectedIdleTime > 0 )
547 __asm volatile ( "WAIT" );
550 /* Allow the application to define some post sleep processing. */
551 configPOST_SLEEP_PROCESSING( xExpectedIdleTime );
554 #endif /* configUSE_TICKLESS_IDLE */
555 /*-----------------------------------------------------------*/
557 #if configUSE_TICKLESS_IDLE == 1
559 void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )
561 uint32_t ulMatchValue, ulCompleteTickPeriods, ulCurrentCount;
562 eSleepModeStatus eSleepAction;
564 /* THIS FUNCTION IS CALLED WITH THE SCHEDULER SUSPENDED. */
566 /* Make sure the CMT reload value does not overflow the counter. */
567 if( xExpectedIdleTime > xMaximumPossibleSuppressedTicks )
569 xExpectedIdleTime = xMaximumPossibleSuppressedTicks;
572 /* Calculate the reload value required to wait xExpectedIdleTime tick
574 ulMatchValue = ulMatchValueForOneTick * xExpectedIdleTime;
576 if( ulMatchValue > ulStoppedTimerCompensation )
578 /* Compensate for the fact that the CMT is going to be stopped
580 ulMatchValue -= ulStoppedTimerCompensation;
583 /* Stop the CMT momentarily. The time the CMT is stopped for is
584 * accounted for as best it can be, but using the tickless mode will
585 * inevitably result in some tiny drift of the time maintained by the
586 * kernel with respect to calendar time. */
587 CMT.CMSTR0.BIT.STR0 = 0;
589 while( CMT.CMSTR0.BIT.STR0 == 1 )
591 /* Nothing to do here. */
594 /* Critical section using the global interrupt bit as the i bit is
595 * automatically reset by the WAIT instruction. */
596 __asm volatile ( "CLRPSW i" );
598 /* The tick flag is set to false before sleeping. If it is true when
599 * sleep mode is exited then sleep mode was probably exited because the
600 * tick was suppressed for the entire xExpectedIdleTime period. */
601 ulTickFlag = pdFALSE;
603 /* If a context switch is pending then abandon the low power entry as
604 * the context switch might have been pended by an external interrupt that
605 * requires processing. */
606 eSleepAction = eTaskConfirmSleepModeStatus();
608 if( eSleepAction == eAbortSleep )
611 CMT.CMSTR0.BIT.STR0 = 1;
612 __asm volatile ( "SETPSW i" );
614 else if( eSleepAction == eNoTasksWaitingTimeout )
616 /* Protection off. */
617 SYSTEM.PRCR.WORD = portUNLOCK_KEY;
619 /* Ready for software standby with all clocks stopped. */
620 SYSTEM.SBYCR.BIT.SSBY = 1;
623 SYSTEM.PRCR.WORD = portLOCK_KEY;
625 /* Sleep until something happens. Calling prvSleep() will
626 * automatically reset the i bit in the PSW. */
627 prvSleep( xExpectedIdleTime );
629 /* Restart the CMT. */
630 CMT.CMSTR0.BIT.STR0 = 1;
634 /* Protection off. */
635 SYSTEM.PRCR.WORD = portUNLOCK_KEY;
637 /* Ready for deep sleep mode. */
638 SYSTEM.MSTPCRC.BIT.DSLPE = 1;
639 SYSTEM.MSTPCRA.BIT.MSTPA28 = 1;
640 SYSTEM.SBYCR.BIT.SSBY = 0;
643 SYSTEM.PRCR.WORD = portLOCK_KEY;
645 /* Adjust the match value to take into account that the current
646 * time slice is already partially complete. */
647 ulMatchValue -= ( uint32_t ) CMT0.CMCNT;
648 CMT0.CMCOR = ( uint16_t ) ulMatchValue;
650 /* Restart the CMT to count up to the new match value. */
652 CMT.CMSTR0.BIT.STR0 = 1;
654 /* Sleep until something happens. Calling prvSleep() will
655 * automatically reset the i bit in the PSW. */
656 prvSleep( xExpectedIdleTime );
658 /* Stop CMT. Again, the time the SysTick is stopped for is
659 * accounted for as best it can be, but using the tickless mode will
660 * inevitably result in some tiny drift of the time maintained by the
661 * kernel with respect to calendar time. */
662 CMT.CMSTR0.BIT.STR0 = 0;
664 while( CMT.CMSTR0.BIT.STR0 == 1 )
666 /* Nothing to do here. */
669 ulCurrentCount = ( uint32_t ) CMT0.CMCNT;
671 if( ulTickFlag != pdFALSE )
673 /* The tick interrupt has already executed, although because
674 * this function is called with the scheduler suspended the actual
675 * tick processing will not occur until after this function has
676 * exited. Reset the match value with whatever remains of this
678 ulMatchValue = ulMatchValueForOneTick - ulCurrentCount;
679 CMT0.CMCOR = ( uint16_t ) ulMatchValue;
681 /* The tick interrupt handler will already have pended the tick
682 * processing in the kernel. As the pending tick will be
683 * processed as soon as this function exits, the tick value
684 * maintained by the tick is stepped forward by one less than the
685 * time spent sleeping. The actual stepping of the tick appears
686 * later in this function. */
687 ulCompleteTickPeriods = xExpectedIdleTime - 1UL;
691 /* Something other than the tick interrupt ended the sleep.
692 * How many complete tick periods passed while the processor was
694 ulCompleteTickPeriods = ulCurrentCount / ulMatchValueForOneTick;
696 /* The match value is set to whatever fraction of a single tick
698 ulMatchValue = ulCurrentCount - ( ulCompleteTickPeriods * ulMatchValueForOneTick );
699 CMT0.CMCOR = ( uint16_t ) ulMatchValue;
702 /* Restart the CMT so it runs up to the match value. The match value
703 * will get set to the value required to generate exactly one tick period
704 * the next time the CMT interrupt executes. */
706 CMT.CMSTR0.BIT.STR0 = 1;
708 /* Wind the tick forward by the number of tick periods that the CPU
709 * remained in a low power state. */
710 vTaskStepTick( ulCompleteTickPeriods );
714 #endif /* configUSE_TICKLESS_IDLE */