Hi Martin, Fred and any others contributing here.
I'm the guy who wrote the app note AN4503 on low power. Fred, I'm not sure why the KQRUG says the wakeup sequence is not obvious. It is know and predictable once you work through the details.
Let me say first that I'm updating the app note with some more accurate notes about the use of the interrupt and the LLWU wakeup. It will be out soon, I'm told that this means March 2015.
Note, that although the MCU versions may be different, all of the mode entry and exit sequences have stayed pretty much the same. Some devices, like some of the KL series parts do not have a pin interrupt for each of the LLWU wakeup pins, so this discussion about using port pin interrupts with LLWU pins does not apply to all LLWU wakeup sources.
It seems this thread has some good questions about this low power exit, so I will let you know what I have experienced and what I have learned, speaking with the MCU low power designers and other apps engineers.
When waking from LLS from a LLWU enabled pin, only the LLWU interrupt is taken.
While in LLS mode, the port interrupt logic is not even powered up. So, when the edge that wakes the MCU from LLS occurs the only logic that sees the edge is in the LLWU module.The port interrupt flag is not set and therefore the port interrupt function will not be called. I hope that clears up some confusion that I might have caused. My statement in Rev. 1, 11/2012 of AN4503, that said that the ISF of the port pin is cleared with the clearing of the LLWU pin flag is incorrect. Sorry.
If there is a critical edge that you must catch, you need to use a pin that had both functions pin interrupt and LLWU wakeup. Enable the edge interrupt logic with the PORTx_PCRxx register and the LLWU edge detect logic in the LLEU_PEx registers. Then you will not ever miss an edge.
In RUN, VLPR, WAIT, VLPW, VLPS, or STOP the interrupt for the port pin will be taken. While the MCU is transitioning into LLS and even VLLSx modes, the edge input will cause an interrupt and abort the mode transition. The Stop Abort Flag will be set in this case will be cleared upon the next low power mode entry if no other interrupt occurs during mode entry. While in LLS the edge will wake the MCU and take the LLWU interrupt vector.
There is no reason that the port interrupt function and the LLWU interrupt function need to be two different functions. One function can handle the edge exception handling. If the LLWU flag is set clear it, if the PORTx_PCR_ISF flag is set, clear it, do your work and then exit the isr function.
Now things are just a little different when it is a module waking the MCU from LLS. If using the LPTMR to wake from LLS the LLWU interrupt is taken first. Clearing the LLWU module flag has no effect and is not needed. If you do nothing with the LPTMR in the LLWU interrupt function then when exiting the LLWU interrupt the LPTMR interrupt will be taken. (Exiting one interrupt and entering another is call Tail Chaining and takes fewer cycles that a full exit and entry into the service routine.) However if you clear the LPTMR flag, read the LPTRM flag back then clear the NVIC_CPIF bit in the NVIC, then the source of the interrupt is cleared and the LPTMR interrupt service routine is not called.
There is a subtle serialization that is mentioned in the sequence above. Please note that it can take nearly 12 cycles for a write to clear the LPTMR flag to complete. There are a few gaskets that the write passes through on its way to the peripheral. If you write to the flag and then immediately try to exit the interrupt, the flag write will not have had time to complete. The result will be that the interrupt handler the MCU just left, is re-entered since the flag causing the interrupt is not cleared yet. However, if you write the flag, then read it back, the core will stall the code execution to wait for the write to complete before it issues the read. This ordered process serializes the sequence of real-time events.
Another little tidbit of information it took some experimentation to figure out, is that when waking the MCU from VLLSx modes with the RTC_SECONDS interrupt, you need to enable the clock gate to the RTC before the the NVIC clear pending interrupt flag will be cleared. The reason for this is that the RTC has a handshake that is occuring with the NVIC and trying the clear the NVIC clear pending interrupt flag from the LLWU interrupt function of the RTC_SECONDS interrupt function will not complete if the RTC clock gate is not written.
I'll keep monitoring this thread to see if any of you have any further questions about various mode entry and exits.
Best Regards,
Philip Drake