Hi,
what MCU do you use?
I have one idea...using API for all cases...it could be comon for all MCUs with API.
... not tested....written directly and immediately as answer.... :
1) Set API period, for example, for 52ms and let it disabled.
static void API_Init(unsigned int period, unsigned char clk)
{
CPMUAPICTL_APIFE = 0; // disable API
//---------------------------
CPMUAPICTL_APIF = 1; // clear interrupt flag
CPMUAPIR = period; // change period
CPMUAPICTL_APICLK = clk; // Autonomous Clock (ACLK)/BUSLK = 0/1
CPMUAPICTL_APIE = 1; // interrutp enable
//---------------------------
CPMUAPICTL_APIFE = 0; // let API disabled and prepared to go
}
2a) Everytime, when rising edge of the input signal (switch LED on) is received, start API period.
static inline void API_Go(void)
{
CPMUAPICTL_APIFE = 1; // enable API
}
2b) Everytime, when falling edge of the input signal (switch LED on) is received before API overflows, stop API period.
static inline void API_Stop(void)
{
CPMUAPICTL_APIFE = 0; // disable API
}
3) Results
Case a)
If the end of the input signal (switch LED on) is received within API period (without being affected by STOP mode ) then just switch LED on and disable API.
Case b)
If the STOP is generated sooner than input signal ((switch LED on) ) is is finished (falling edge) then you can go to STOP because API is still running on background.
In this case: MCU is in STOP, API is already running because rising of the edge was received before STOP, end of the input signal (switch LED on) is received within API period => API wakes the MCU up, we switch LEDs on, disable API and enter or leave STOP mode following application requirements.
Case c)
We are in the STOP mode as in the case b) but consider the input signal is longer than API period. In this case MCU is woken up by API, input signal is invalid (longer than 50ms) so we do nothing (LEDs are ignored), only disable API.
I hope it is clear and I have not made mistake...
Probably, there could be better, faster, easier,.....solution but this is what just came to my mind.
Best regards,
Ladislav
Only if necessaty:
//==============================================================================
static void API_ChangePeriod(unsigned int period)
{
API_Stop();
//---------------------------
CPMUAPICTL_APIF = 1; // clear interrupt flag
CPMUAPIR = period; // change period
//---------------------------
API_Go();
}