The oppostive function to attach_idle_hook()

Hello,

I’d like to use attach_idle_hook() to set a function in order to deep power-down my flash to save energy. But I also like to reinstate function as soon as my MCU wakes up (STM32L4). Is there an “opposite” function to attach_idle_hook() which is called when the MCU wakes up?

Thank you,
Andy

Hello Andy,

The Power management (sleep) - API references and tutorials | Mbed OS 6 Documentation says that

You can wake up the processor by any internal peripheral interrupt or external pin interrupt.

This implies that when the MCU wakes up the interrupt service routine of the triggered interrupt is called first rather than a common built-in function. Then the program execution continues with the function which the MCU determined by evaluating the system’s actual status (could differ from case to case) before falling into sleep. So it seems there is no such common single function provided by the OS.

However, you can design one by yourself, if you really need such, and call it from each interrupt service routine by testing an “on_wakeup” flag set in the rtos_attach_idle_hook function before falling into sleep (don’t forget to clear the flag for next use). But in that case you have to keep this function very short (not-blocking) otherwise you risk a system break down. So my recommendation is “don’t do it”!

Hello Zoltan,

thank you for your advice.

Basically, what I want to achieve is that an external component such as NOR flash is automatically powered down/powered up together with the MCU and as such handled as “MCU-integrated component” which goes into sleep when the MCU does.

Another thing: If I attach the idle hook function it is called over and over again. How do I implement that it is always called once when the systems enters idle? In the example the Idle hook is reset to null but then it won’t be called next time the device enters deep-sleep.

It was only my tip. I have never tried something like that. But in your case seems to be worth of an attempt.

If I attach the idle hook function it is called over and over again. How do I implement that it is always called once when the systems enters idle? In the example the Idle hook is reset to null but then it won’t be called next time the device enters deep-sleep.

  • Copy & paste the default_idle_hook to your new_idle_loop and complete it with your code to power down and up the NOR flash. For example:
...
void new_idle_loop()
{
#ifdef MBED_TICKLESS

    // insert here your code to power down the NOR flash   
    rtos::Kernel::Clock::duration_u32 ticks_to_sleep{osKernelSuspend()};
    // osKernelSuspend will call OS_Tick_Disable, cancelling the tick, which frees
    // up the os timer for the timed sleep
    rtos::Kernel::Clock::duration_u32 ticks_slept = mbed::internal::do_timed_sleep_relative_to_acknowledged_ticks(ticks_to_sleep, rtos_event_pending);
    MBED_ASSERT(ticks_slept < rtos::Kernel::wait_for_u32_max);
    osKernelResume(ticks_slept.count());
    // insert here your code to power up the NOR flash

#else

    // insert here your code to power down the NOR flash
    // critical section to complete sleep with locked deepsleep
    core_util_critical_section_enter();
    sleep_manager_lock_deep_sleep();
    sleep();
    sleep_manager_unlock_deep_sleep();
    core_util_critical_section_exit();
    // insert here your code to power up the NOR flash
}

#endif
...