2 * FreeRTOS Kernel V10.4.3 LTS Patch 3
\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 * https://www.FreeRTOS.org
\r
23 * https://github.com/FreeRTOS
\r
25 * 1 tab == 4 spaces!
\r
28 /*-----------------------------------------------------------
\r
29 * Implementation of functions defined in portable.h for the SH2A port.
\r
30 *----------------------------------------------------------*/
\r
32 /* Standard C includes. */
\r
35 /* Scheduler includes. */
\r
36 #include "FreeRTOS.h"
\r
39 /* Library includes. */
\r
42 /* Hardware specifics. */
\r
43 #if ( configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H == 1 )
\r
45 #include "platform.h"
\r
47 #else /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
\r
49 #include "iodefine.h"
\r
51 #endif /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
\r
52 /*-----------------------------------------------------------*/
\r
54 /* Tasks should start with interrupts enabled and in Supervisor mode, therefore
\r
55 PSW is set with U and I set, and PM and IPL clear. */
\r
56 #define portINITIAL_PSW ( ( StackType_t ) 0x00030000 )
\r
58 /* The peripheral clock is divided by this value before being supplying the
\r
60 #if ( configUSE_TICKLESS_IDLE == 0 )
\r
61 /* If tickless idle is not used then the divisor can be fixed. */
\r
62 #define portCLOCK_DIVISOR 8UL
\r
63 #elif ( configPERIPHERAL_CLOCK_HZ >= 12000000 )
\r
64 #define portCLOCK_DIVISOR 512UL
\r
65 #elif ( configPERIPHERAL_CLOCK_HZ >= 6000000 )
\r
66 #define portCLOCK_DIVISOR 128UL
\r
67 #elif ( configPERIPHERAL_CLOCK_HZ >= 1000000 )
\r
68 #define portCLOCK_DIVISOR 32UL
\r
70 #define portCLOCK_DIVISOR 8UL
\r
73 /* These macros allow a critical section to be added around the call to
\r
74 xTaskIncrementTick(), which is only ever called from interrupts at the kernel
\r
75 priority - ie a known priority. Therefore these local macros are a slight
\r
76 optimisation compared to calling the global SET/CLEAR_INTERRUPT_MASK macros,
\r
77 which would require the old IPL to be read first and stored in a local variable. */
\r
78 #define portDISABLE_INTERRUPTS_FROM_KERNEL_ISR() __asm volatile ( "MVTIPL %0" ::"i"(configMAX_SYSCALL_INTERRUPT_PRIORITY) )
\r
79 #define portENABLE_INTERRUPTS_FROM_KERNEL_ISR() __asm volatile ( "MVTIPL %0" ::"i"(configKERNEL_INTERRUPT_PRIORITY) )
\r
81 /* Keys required to lock and unlock access to certain system registers
\r
83 #define portUNLOCK_KEY 0xA50B
\r
84 #define portLOCK_KEY 0xA500
\r
86 /*-----------------------------------------------------------*/
\r
89 * Function to start the first task executing - written in asm code as direct
\r
90 * access to registers is required.
\r
92 static void prvStartFirstTask( void ) __attribute__((naked));
\r
95 * Software interrupt handler. Performs the actual context switch (saving and
\r
96 * restoring of registers). Written in asm code as direct register access is
\r
99 #if ( configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H == 1 )
\r
101 R_BSP_PRAGMA_INTERRUPT( vSoftwareInterruptISR, VECT( ICU, SWINT ) )
\r
102 R_BSP_ATTRIB_INTERRUPT void vSoftwareInterruptISR( void ) __attribute__( ( naked ) );
\r
104 #else /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
\r
106 void vSoftwareInterruptISR( void ) __attribute__( ( naked ) );
\r
108 #endif /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
\r
111 * The tick ISR handler. The peripheral used is configured by the application
\r
112 * via a hook/callback function.
\r
114 #if ( configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H == 1 )
\r
116 R_BSP_PRAGMA_INTERRUPT( vTickISR, _VECT( configTICK_VECTOR ) )
\r
117 R_BSP_ATTRIB_INTERRUPT void vTickISR( void ); /* Do not add __attribute__( ( interrupt ) ). */
\r
119 #else /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
\r
121 void vTickISR( void ) __attribute__( ( interrupt ) );
\r
123 #endif /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
\r
126 * Sets up the periodic ISR used for the RTOS tick using the CMT.
\r
127 * The application writer can define configSETUP_TICK_INTERRUPT() (in
\r
128 * FreeRTOSConfig.h) such that their own tick interrupt configuration is used
\r
129 * in place of prvSetupTimerInterrupt().
\r
131 static void prvSetupTimerInterrupt( void );
\r
132 #ifndef configSETUP_TICK_INTERRUPT
\r
133 /* The user has not provided their own tick interrupt configuration so use
\r
134 the definition in this file (which uses the interval timer). */
\r
135 #define configSETUP_TICK_INTERRUPT() prvSetupTimerInterrupt()
\r
136 #endif /* configSETUP_TICK_INTERRUPT */
\r
139 * Called after the sleep mode registers have been configured, prvSleep()
\r
140 * executes the pre and post sleep macros, and actually calls the wait
\r
143 #if configUSE_TICKLESS_IDLE == 1
\r
144 static void prvSleep( TickType_t xExpectedIdleTime );
\r
145 #endif /* configUSE_TICKLESS_IDLE */
\r
147 /*-----------------------------------------------------------*/
\r
149 /* Used in the context save and restore code. */
\r
150 extern void *pxCurrentTCB;
\r
152 /* Calculate how many clock increments make up a single tick period. */
\r
153 static const uint32_t ulMatchValueForOneTick = ( ( configPERIPHERAL_CLOCK_HZ / portCLOCK_DIVISOR ) / configTICK_RATE_HZ );
\r
155 #if configUSE_TICKLESS_IDLE == 1
\r
157 /* Holds the maximum number of ticks that can be suppressed - which is
\r
158 basically how far into the future an interrupt can be generated. Set
\r
159 during initialisation. This is the maximum possible value that the
\r
160 compare match register can hold divided by ulMatchValueForOneTick. */
\r
161 static const TickType_t xMaximumPossibleSuppressedTicks = USHRT_MAX / ( ( configPERIPHERAL_CLOCK_HZ / portCLOCK_DIVISOR ) / configTICK_RATE_HZ );
\r
163 /* Flag set from the tick interrupt to allow the sleep processing to know if
\r
164 sleep mode was exited because of a tick interrupt, or an interrupt
\r
165 generated by something else. */
\r
166 static volatile uint32_t ulTickFlag = pdFALSE;
\r
168 /* The CMT counter is stopped temporarily each time it is re-programmed.
\r
169 The following constant offsets the CMT counter match value by the number of
\r
170 CMT counts that would typically be missed while the counter was stopped to
\r
171 compensate for the lost time. The large difference between the divided CMT
\r
172 clock and the CPU clock means it is likely ulStoppedTimerCompensation will
\r
173 equal zero - and be optimised away. */
\r
174 static const uint32_t ulStoppedTimerCompensation = 100UL / ( configCPU_CLOCK_HZ / ( configPERIPHERAL_CLOCK_HZ / portCLOCK_DIVISOR ) );
\r
178 /*-----------------------------------------------------------*/
\r
181 * See header file for description.
\r
183 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\r
185 /* Offset to end up on 8 byte boundary. */
\r
188 /* R0 is not included as it is the stack pointer. */
\r
189 *pxTopOfStack = 0x00;
\r
191 *pxTopOfStack = 0x00;
\r
193 *pxTopOfStack = portINITIAL_PSW;
\r
195 *pxTopOfStack = ( StackType_t ) pxCode;
\r
197 /* When debugging it can be useful if every register is set to a known
\r
198 value. Otherwise code space can be saved by just setting the registers
\r
199 that need to be set. */
\r
200 #ifdef USE_FULL_REGISTER_INITIALISATION
\r
203 *pxTopOfStack = 0x12345678; /* r15. */
\r
205 *pxTopOfStack = 0xaaaabbbb;
\r
207 *pxTopOfStack = 0xdddddddd;
\r
209 *pxTopOfStack = 0xcccccccc;
\r
211 *pxTopOfStack = 0xbbbbbbbb;
\r
213 *pxTopOfStack = 0xaaaaaaaa;
\r
215 *pxTopOfStack = 0x99999999;
\r
217 *pxTopOfStack = 0x88888888;
\r
219 *pxTopOfStack = 0x77777777;
\r
221 *pxTopOfStack = 0x66666666;
\r
223 *pxTopOfStack = 0x55555555;
\r
225 *pxTopOfStack = 0x44444444;
\r
227 *pxTopOfStack = 0x33333333;
\r
229 *pxTopOfStack = 0x22222222;
\r
234 /* Leave space for the registers that will get popped from the stack
\r
235 when the task first starts executing. */
\r
236 pxTopOfStack -= 15;
\r
240 *pxTopOfStack = ( StackType_t ) pvParameters; /* R1 */
\r
242 *pxTopOfStack = 0x12345678; /* Accumulator. */
\r
244 *pxTopOfStack = 0x87654321; /* Accumulator. */
\r
246 return pxTopOfStack;
\r
248 /*-----------------------------------------------------------*/
\r
250 BaseType_t xPortStartScheduler( void )
\r
252 /* Use pxCurrentTCB just so it does not get optimised away. */
\r
253 if( pxCurrentTCB != NULL )
\r
255 /* Call an application function to set up the timer that will generate
\r
256 the tick interrupt. This way the application can decide which
\r
257 peripheral to use. If tickless mode is used then the default
\r
258 implementation defined in this file (which uses CMT0) should not be
\r
260 configSETUP_TICK_INTERRUPT();
\r
262 /* Enable the software interrupt. */
\r
263 _IEN( _ICU_SWINT ) = 1;
\r
265 /* Ensure the software interrupt is clear. */
\r
266 _IR( _ICU_SWINT ) = 0;
\r
268 /* Ensure the software interrupt is set to the kernel priority. */
\r
269 _IPR( _ICU_SWINT ) = configKERNEL_INTERRUPT_PRIORITY;
\r
271 /* Start the first task. */
\r
272 prvStartFirstTask();
\r
275 /* Execution should not reach here as the tasks are now running!
\r
276 prvSetupTimerInterrupt() is called here to prevent the compiler outputting
\r
277 a warning about a statically declared function not being referenced in the
\r
278 case that the application writer has provided their own tick interrupt
\r
279 configuration routine (and defined configSETUP_TICK_INTERRUPT() such that
\r
280 their own routine will be called in place of prvSetupTimerInterrupt()). */
\r
281 prvSetupTimerInterrupt();
\r
283 /* Should not get here. */
\r
286 /*-----------------------------------------------------------*/
\r
288 void vPortEndScheduler( void )
\r
290 /* Not implemented in ports where there is nothing to return to.
\r
291 Artificially force an assert. */
\r
292 configASSERT( pxCurrentTCB == NULL );
\r
294 /*-----------------------------------------------------------*/
\r
296 static void prvStartFirstTask( void )
\r
300 /* When starting the scheduler there is nothing that needs moving to the
\r
301 interrupt stack because the function is not called from an interrupt.
\r
302 Just ensure the current stack is the user stack. */
\r
305 /* Obtain the location of the stack associated with which ever task
\r
306 pxCurrentTCB is currently pointing to. */
\r
307 "MOV.L #_pxCurrentTCB, R15 \n" \
\r
308 "MOV.L [R15], R15 \n" \
\r
309 "MOV.L [R15], R0 \n" \
\r
311 /* Restore the registers from the stack of the task pointed to by
\r
315 /* Accumulator low 32 bits. */
\r
319 /* Accumulator high 32 bits. */
\r
322 /* R1 to R15 - R0 is not included as it is the SP. */
\r
325 /* This pops the remaining registers. */
\r
331 /*-----------------------------------------------------------*/
\r
333 void vPortSoftwareInterruptISR( void )
\r
337 /* Re-enable interrupts. */
\r
340 /* Move the data that was automatically pushed onto the interrupt stack when
\r
341 the interrupt occurred from the interrupt stack to the user stack.
\r
343 R15 is saved before it is clobbered. */
\r
346 /* Read the user stack pointer. */
\r
347 "MVFC USP, R15 \n" \
\r
349 /* Move the address down to the data being moved. */
\r
350 "SUB #12, R15 \n" \
\r
351 "MVTC R15, USP \n" \
\r
353 /* Copy the data across, R15, then PC, then PSW. */
\r
354 "MOV.L [ R0 ], [ R15 ] \n" \
\r
355 "MOV.L 4[ R0 ], 4[ R15 ] \n" \
\r
356 "MOV.L 8[ R0 ], 8[ R15 ] \n" \
\r
358 /* Move the interrupt stack pointer to its new correct position. */
\r
361 /* All the rest of the registers are saved directly to the user stack. */
\r
364 /* Save the rest of the general registers (R15 has been saved already). */
\r
365 "PUSHM R1-R14 \n" \
\r
367 /* Save the accumulator. */
\r
374 /* Shifted left as it is restored to the low order word. */
\r
375 "SHLL #16, R15 \n" \
\r
378 /* Save the stack pointer to the TCB. */
\r
379 "MOV.L #_pxCurrentTCB, R15 \n" \
\r
380 "MOV.L [ R15 ], R15 \n" \
\r
381 "MOV.L R0, [ R15 ] \n" \
\r
383 /* Ensure the interrupt mask is set to the syscall priority while the kernel
\r
384 structures are being accessed. */
\r
387 /* Select the next task to run. */
\r
388 "BSR.A _vTaskSwitchContext \n" \
\r
390 /* Reset the interrupt mask as no more data structure access is required. */
\r
393 /* Load the stack pointer of the task that is now selected as the Running
\r
394 state task from its TCB. */
\r
395 "MOV.L #_pxCurrentTCB,R15 \n" \
\r
396 "MOV.L [ R15 ], R15 \n" \
\r
397 "MOV.L [ R15 ], R0 \n" \
\r
399 /* Restore the context of the new task. The PSW (Program Status Word) and
\r
400 PC will be popped by the RTE instruction. */
\r
409 :: "i"(configMAX_SYSCALL_INTERRUPT_PRIORITY), "i"(configKERNEL_INTERRUPT_PRIORITY)
\r
412 /*-----------------------------------------------------------*/
\r
414 void vPortTickISR( void )
\r
416 /* Re-enabled interrupts. */
\r
417 __asm volatile( "SETPSW I" );
\r
419 /* Increment the tick, and perform any processing the new tick value
\r
420 necessitates. Ensure IPL is at the max syscall value first. */
\r
421 portDISABLE_INTERRUPTS_FROM_KERNEL_ISR();
\r
423 if( xTaskIncrementTick() != pdFALSE )
\r
428 portENABLE_INTERRUPTS_FROM_KERNEL_ISR();
\r
430 #if configUSE_TICKLESS_IDLE == 1
\r
432 /* The CPU woke because of a tick. */
\r
433 ulTickFlag = pdTRUE;
\r
435 /* If this is the first tick since exiting tickless mode then the CMT
\r
436 compare match value needs resetting. */
\r
437 CMT0.CMCOR = ( uint16_t ) ulMatchValueForOneTick;
\r
441 /*-----------------------------------------------------------*/
\r
443 uint32_t ulPortGetIPL( void )
\r
447 "MVFC PSW, R1 \n" \
\r
448 "SHLR #24, R1 \n" \
\r
452 /* This will never get executed, but keeps the compiler from complaining. */
\r
455 /*-----------------------------------------------------------*/
\r
457 void vPortSetIPL( uint32_t ulNewIPL )
\r
462 "MVFC PSW, R5 \n" \
\r
463 "SHLL #24, R1 \n" \
\r
464 "AND #-0F000001H, R5 \n" \
\r
466 "MVTC R5, PSW \n" \
\r
471 /*-----------------------------------------------------------*/
\r
473 static void prvSetupTimerInterrupt( void )
\r
476 SYSTEM.PRCR.WORD = portUNLOCK_KEY;
\r
482 SYSTEM.PRCR.WORD = portLOCK_KEY;
\r
484 /* Interrupt on compare match. */
\r
485 CMT0.CMCR.BIT.CMIE = 1;
\r
487 /* Set the compare match value. */
\r
488 CMT0.CMCOR = ( uint16_t ) ulMatchValueForOneTick;
\r
490 /* Divide the PCLK. */
\r
491 #if portCLOCK_DIVISOR == 512
\r
493 CMT0.CMCR.BIT.CKS = 3;
\r
495 #elif portCLOCK_DIVISOR == 128
\r
497 CMT0.CMCR.BIT.CKS = 2;
\r
499 #elif portCLOCK_DIVISOR == 32
\r
501 CMT0.CMCR.BIT.CKS = 1;
\r
503 #elif portCLOCK_DIVISOR == 8
\r
505 CMT0.CMCR.BIT.CKS = 0;
\r
509 #error Invalid portCLOCK_DIVISOR setting
\r
513 /* Enable the interrupt... */
\r
514 _IEN( _CMT0_CMI0 ) = 1;
\r
516 /* ...and set its priority to the application defined kernel priority. */
\r
517 _IPR( _CMT0_CMI0 ) = configKERNEL_INTERRUPT_PRIORITY;
\r
519 /* Start the timer. */
\r
520 CMT.CMSTR0.BIT.STR0 = 1;
\r
522 /*-----------------------------------------------------------*/
\r
524 #if configUSE_TICKLESS_IDLE == 1
\r
526 static void prvSleep( TickType_t xExpectedIdleTime )
\r
528 /* Allow the application to define some pre-sleep processing. */
\r
529 configPRE_SLEEP_PROCESSING( xExpectedIdleTime );
\r
531 /* xExpectedIdleTime being set to 0 by configPRE_SLEEP_PROCESSING()
\r
532 means the application defined code has already executed the WAIT
\r
534 if( xExpectedIdleTime > 0 )
\r
536 __asm volatile( "WAIT" );
\r
539 /* Allow the application to define some post sleep processing. */
\r
540 configPOST_SLEEP_PROCESSING( xExpectedIdleTime );
\r
543 #endif /* configUSE_TICKLESS_IDLE */
\r
544 /*-----------------------------------------------------------*/
\r
546 #if configUSE_TICKLESS_IDLE == 1
\r
548 void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )
\r
550 uint32_t ulMatchValue, ulCompleteTickPeriods, ulCurrentCount;
\r
551 eSleepModeStatus eSleepAction;
\r
553 /* THIS FUNCTION IS CALLED WITH THE SCHEDULER SUSPENDED. */
\r
555 /* Make sure the CMT reload value does not overflow the counter. */
\r
556 if( xExpectedIdleTime > xMaximumPossibleSuppressedTicks )
\r
558 xExpectedIdleTime = xMaximumPossibleSuppressedTicks;
\r
561 /* Calculate the reload value required to wait xExpectedIdleTime tick
\r
563 ulMatchValue = ulMatchValueForOneTick * xExpectedIdleTime;
\r
564 if( ulMatchValue > ulStoppedTimerCompensation )
\r
566 /* Compensate for the fact that the CMT is going to be stopped
\r
568 ulMatchValue -= ulStoppedTimerCompensation;
\r
571 /* Stop the CMT momentarily. The time the CMT is stopped for is
\r
572 accounted for as best it can be, but using the tickless mode will
\r
573 inevitably result in some tiny drift of the time maintained by the
\r
574 kernel with respect to calendar time. */
\r
575 CMT.CMSTR0.BIT.STR0 = 0;
\r
576 while( CMT.CMSTR0.BIT.STR0 == 1 )
\r
578 /* Nothing to do here. */
\r
581 /* Critical section using the global interrupt bit as the i bit is
\r
582 automatically reset by the WAIT instruction. */
\r
583 __asm volatile( "CLRPSW i" );
\r
585 /* The tick flag is set to false before sleeping. If it is true when
\r
586 sleep mode is exited then sleep mode was probably exited because the
\r
587 tick was suppressed for the entire xExpectedIdleTime period. */
\r
588 ulTickFlag = pdFALSE;
\r
590 /* If a context switch is pending then abandon the low power entry as
\r
591 the context switch might have been pended by an external interrupt that
\r
592 requires processing. */
\r
593 eSleepAction = eTaskConfirmSleepModeStatus();
\r
594 if( eSleepAction == eAbortSleep )
\r
596 /* Restart tick. */
\r
597 CMT.CMSTR0.BIT.STR0 = 1;
\r
598 __asm volatile( "SETPSW i" );
\r
600 else if( eSleepAction == eNoTasksWaitingTimeout )
\r
602 /* Protection off. */
\r
603 SYSTEM.PRCR.WORD = portUNLOCK_KEY;
\r
605 /* Ready for software standby with all clocks stopped. */
\r
606 SYSTEM.SBYCR.BIT.SSBY = 1;
\r
608 /* Protection on. */
\r
609 SYSTEM.PRCR.WORD = portLOCK_KEY;
\r
611 /* Sleep until something happens. Calling prvSleep() will
\r
612 automatically reset the i bit in the PSW. */
\r
613 prvSleep( xExpectedIdleTime );
\r
615 /* Restart the CMT. */
\r
616 CMT.CMSTR0.BIT.STR0 = 1;
\r
620 /* Protection off. */
\r
621 SYSTEM.PRCR.WORD = portUNLOCK_KEY;
\r
623 /* Ready for deep sleep mode. */
\r
624 SYSTEM.MSTPCRC.BIT.DSLPE = 1;
\r
625 SYSTEM.MSTPCRA.BIT.MSTPA28 = 1;
\r
626 SYSTEM.SBYCR.BIT.SSBY = 0;
\r
628 /* Protection on. */
\r
629 SYSTEM.PRCR.WORD = portLOCK_KEY;
\r
631 /* Adjust the match value to take into account that the current
\r
632 time slice is already partially complete. */
\r
633 ulMatchValue -= ( uint32_t ) CMT0.CMCNT;
\r
634 CMT0.CMCOR = ( uint16_t ) ulMatchValue;
\r
636 /* Restart the CMT to count up to the new match value. */
\r
638 CMT.CMSTR0.BIT.STR0 = 1;
\r
640 /* Sleep until something happens. Calling prvSleep() will
\r
641 automatically reset the i bit in the PSW. */
\r
642 prvSleep( xExpectedIdleTime );
\r
644 /* Stop CMT. Again, the time the SysTick is stopped for is
\r
645 accounted for as best it can be, but using the tickless mode will
\r
646 inevitably result in some tiny drift of the time maintained by the
\r
647 kernel with respect to calendar time. */
\r
648 CMT.CMSTR0.BIT.STR0 = 0;
\r
649 while( CMT.CMSTR0.BIT.STR0 == 1 )
\r
651 /* Nothing to do here. */
\r
654 ulCurrentCount = ( uint32_t ) CMT0.CMCNT;
\r
656 if( ulTickFlag != pdFALSE )
\r
658 /* The tick interrupt has already executed, although because
\r
659 this function is called with the scheduler suspended the actual
\r
660 tick processing will not occur until after this function has
\r
661 exited. Reset the match value with whatever remains of this
\r
663 ulMatchValue = ulMatchValueForOneTick - ulCurrentCount;
\r
664 CMT0.CMCOR = ( uint16_t ) ulMatchValue;
\r
666 /* The tick interrupt handler will already have pended the tick
\r
667 processing in the kernel. As the pending tick will be
\r
668 processed as soon as this function exits, the tick value
\r
669 maintained by the tick is stepped forward by one less than the
\r
670 time spent sleeping. The actual stepping of the tick appears
\r
671 later in this function. */
\r
672 ulCompleteTickPeriods = xExpectedIdleTime - 1UL;
\r
676 /* Something other than the tick interrupt ended the sleep.
\r
677 How many complete tick periods passed while the processor was
\r
679 ulCompleteTickPeriods = ulCurrentCount / ulMatchValueForOneTick;
\r
681 /* The match value is set to whatever fraction of a single tick
\r
683 ulMatchValue = ulCurrentCount - ( ulCompleteTickPeriods * ulMatchValueForOneTick );
\r
684 CMT0.CMCOR = ( uint16_t ) ulMatchValue;
\r
687 /* Restart the CMT so it runs up to the match value. The match value
\r
688 will get set to the value required to generate exactly one tick period
\r
689 the next time the CMT interrupt executes. */
\r
691 CMT.CMSTR0.BIT.STR0 = 1;
\r
693 /* Wind the tick forward by the number of tick periods that the CPU
\r
694 remained in a low power state. */
\r
695 vTaskStepTick( ulCompleteTickPeriods );
\r
699 #endif /* configUSE_TICKLESS_IDLE */
\r