/*********************************************************************************************************************** * Function Name: R_BSP_InterruptsEnable * Description : Globally enable interrupts. * Arguments : none * Return Value : none ***********************************************************************************************************************/ void R_BSP_InterruptsEnable (void) { #if defined(__RENESAS__) /* Use the compiler intrinsic function to set the I flag. This function is available with for the Renesas RX compiler. This may need to be changed for other compilers. */ setpsw_i(); #endif }
void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime ) { uint32_t ulMatchValue, ulCompleteTickPeriods, ulCurrentCount; eSleepModeStatus eSleepAction; /* THIS FUNCTION IS CALLED WITH THE SCHEDULER SUSPENDED. */ /* Make sure the CMT reload value does not overflow the counter. */ if( xExpectedIdleTime > xMaximumPossibleSuppressedTicks ) { xExpectedIdleTime = xMaximumPossibleSuppressedTicks; } /* Calculate the reload value required to wait xExpectedIdleTime tick periods. */ ulMatchValue = ulMatchValueForOneTick * xExpectedIdleTime; if( ulMatchValue > ulStoppedTimerCompensation ) { /* Compensate for the fact that the CMT is going to be stopped momentarily. */ ulMatchValue -= ulStoppedTimerCompensation; } /* Stop the CMT momentarily. The time the CMT is stopped for is accounted for as best it can be, but using the tickless mode will inevitably result in some tiny drift of the time maintained by the kernel with respect to calendar time. */ CMT.CMSTR0.BIT.STR0 = 0; while( CMT.CMSTR0.BIT.STR0 == 1 ) { /* Nothing to do here. */ } /* Critical section using the global interrupt bit as the i bit is automatically reset by the WAIT instruction. */ clrpsw_i(); /* The tick flag is set to false before sleeping. If it is true when sleep mode is exited then sleep mode was probably exited because the tick was suppressed for the entire xExpectedIdleTime period. */ ulTickFlag = pdFALSE; /* If a context switch is pending then abandon the low power entry as the context switch might have been pended by an external interrupt that requires processing. */ eSleepAction = eTaskConfirmSleepModeStatus(); if( eSleepAction == eAbortSleep ) { /* Restart tick. */ CMT.CMSTR0.BIT.STR0 = 1; setpsw_i(); } else if( eSleepAction == eNoTasksWaitingTimeout ) { /* Protection off. */ SYSTEM.PRCR.WORD = portUNLOCK_KEY; /* Ready for software standby with all clocks stopped. */ SYSTEM.SBYCR.BIT.SSBY = 1; /* Protection on. */ SYSTEM.PRCR.WORD = portLOCK_KEY; /* Sleep until something happens. Calling prvSleep() will automatically reset the i bit in the PSW. */ prvSleep( xExpectedIdleTime ); /* Restart the CMT. */ CMT.CMSTR0.BIT.STR0 = 1; } else { /* Protection off. */ SYSTEM.PRCR.WORD = portUNLOCK_KEY; /* Ready for deep sleep mode. */ SYSTEM.MSTPCRC.BIT.DSLPE = 1; SYSTEM.MSTPCRA.BIT.MSTPA28 = 1; SYSTEM.SBYCR.BIT.SSBY = 0; /* Protection on. */ SYSTEM.PRCR.WORD = portLOCK_KEY; /* Adjust the match value to take into account that the current time slice is already partially complete. */ ulMatchValue -= ( uint32_t ) CMT0.CMCNT; CMT0.CMCOR = ( uint16_t ) ulMatchValue; /* Restart the CMT to count up to the new match value. */ CMT0.CMCNT = 0; CMT.CMSTR0.BIT.STR0 = 1; /* Sleep until something happens. Calling prvSleep() will automatically reset the i bit in the PSW. */ prvSleep( xExpectedIdleTime ); /* Stop CMT. Again, the time the SysTick is stopped for is accounted for as best it can be, but using the tickless mode will inevitably result in some tiny drift of the time maintained by the kernel with respect to calendar time. */ CMT.CMSTR0.BIT.STR0 = 0; while( CMT.CMSTR0.BIT.STR0 == 1 ) { /* Nothing to do here. */ } ulCurrentCount = ( uint32_t ) CMT0.CMCNT; if( ulTickFlag != pdFALSE ) { /* The tick interrupt has already executed, although because this function is called with the scheduler suspended the actual tick processing will not occur until after this function has exited. Reset the match value with whatever remains of this tick period. */ ulMatchValue = ulMatchValueForOneTick - ulCurrentCount; CMT0.CMCOR = ( uint16_t ) ulMatchValue; /* The tick interrupt handler will already have pended the tick processing in the kernel. As the pending tick will be processed as soon as this function exits, the tick value maintained by the tick is stepped forward by one less than the time spent sleeping. The actual stepping of the tick appears later in this function. */ ulCompleteTickPeriods = xExpectedIdleTime - 1UL; } else { /* Something other than the tick interrupt ended the sleep. How many complete tick periods passed while the processor was sleeping? */ ulCompleteTickPeriods = ulCurrentCount / ulMatchValueForOneTick; /* The match value is set to whatever fraction of a single tick period remains. */ ulMatchValue = ulCurrentCount - ( ulCompleteTickPeriods * ulMatchValueForOneTick ); CMT0.CMCOR = ( uint16_t ) ulMatchValue; } /* Restart the CMT so it runs up to the match value. The match value will get set to the value required to generate exactly one tick period the next time the CMT interrupt executes. */ CMT0.CMCNT = 0; CMT.CMSTR0.BIT.STR0 = 1; /* Wind the tick forward by the number of tick periods that the CPU remained in a low power state. */ vTaskStepTick( ulCompleteTickPeriods ); } }