 So let's talk now about low power modes and let's see what support for low power modes free Arto's has Normally, it's not a very friendly environment because it gives you a periodic interrupt every millisecond So even if you would sleep you would still wake up quite often which means Setting up the oscillator running at some frequency may be setting up another wake-up period and This all eats Especially from battery So free Arto's in its default configuration isn't really battery friendly However, it doesn't prevent you from configuring it into much nicer state What we can do we can enable a tick less idle mode What it does When we enter the idle we don't have any other task to do its job So we know that during this time We don't have anything to do and we can spare the energy You know that the microcontrollers can spare the energy by shutting down unused peripherals clocks flash memory and so on and Among that because the wake-up time can be significantly long Compared to the runtime We will benefit if we are able to disable the periodic wake-up So if we enable the tick less idle The kernel will call any time it gets into the idle mode. It will call the function suppress ticks and sleep so we have to implement this macro and It's our responsibility to set up the microcontroller in the mode appropriate to the required current consumption So it can be a sleep Or it can be even a stop mode If you are able to wake up the RTOS After a specific time based for example on a real-time clock or Based on a communication from some peripheral like a UART a Ternet SPI or something else we can disable the microcontrollers clocks and We can drop down from milliamps To just couple of microamps or even lower within this macro We can enter in the sleep or stop modes But we have to as well be careful about activating our wake-up sources Especially if you enter a stop mode You will need something as in her nose to wake you up because without internal clock no timer runs No communication peripherals if it doesn't have extra support for a stop mode So we must be able to set up the appropriate external interrupt or wake up We can for example take care about the wake up using things like a low-power timers LP timers or RTC This one helps us to Get down to the stop mode these still run from auxiliary oscillator that has a consumption of half of micro amp or less and The rest of the microcontroller can sleep then these two can give you a periodic or Once in a time a pulse and this will bring up the whole microcontroller How to achieve the tick less mode and how to achieve the peripheral? Functionality as we require it for low-power mode so we know that the idle task is created automatically and Whenever the idle task is executed it cleans up the deleted tasks it yields if we don't use preemption and If no other task is waiting it gets the yield it executes the application idle hook and It uses the macro That jumps to the low-power mode if we use the tick less mode So how to enter into the low-power mode? We first need to calculate the expected idle time. There is a macro for that So we calculate the expected idle time and then We can continue we suspend all tasks then again we check the expected idle time if Some other task has got updated it then we execute the macros to suppress ticks and sleep and We put the expected idle time in these macros If it's a bigger than the default one we should continue otherwise We enter the suppress tick and sleep function and we enter a low power mode So this way we have obtained for from the Friartos The time after which we need to wake up all the blocked tasks This is a very important thing because now if you have a task delay or If you have for example a timeout in the queue waiting This function the get expected idle time will calculate The minimum time for wake up of some of the tasks or objects so if for example use OS delay one second and OS message get from the queue with a timeout of four seconds What would be the expected idle time in this case? one second delay in task one and Getting message from the queue in task two with a timeout of four seconds What is my expected idle time one second the minimum one? Because you need to wake up earlier for the first task So the function will give you one second and then you can program this one second wake up within an RTC or low-power timer Then you enter low-power mode. You can completely stop the clocks of the micro So you drop the consumption to one two micro amps And then the RTC or low-power timer that you preprogrammed gives you a wake-up call this restores the clocks for the Friartos and Then when you wake up you can continue with the rescue When you resume you have to restore the functionality of the scheduler so you should resume all tasks and This will return the functionality of the scheduler and the scheduler Will wake up of course you have to as well Increment the time manually in the time base and then the scheduler will call the task because it elapsed already and It will manage proper timing for the rest So you can see that it's a little bit of job to enter into a low-power mode, but it's worth it and Especially for battery-operated applications. It's a must Do you know what is the difference between the interrupt and the event? First they have a different input to the core The interrupts come from the NVIC And they are typically registered from different peripherals When you activate the interrupt in the peripheral interrupts have as well associated interrupt vector and priority So when an interrupt is registered The core will stop a current function and It will jump to the interrupt handler Proceed the interrupt handler and return So interrupts have kind of private Handlers private functions to serve them However event is a different thing Event can be generated first by external Interrupt and event unit xt. It doesn't cause calling the interrupt handlers So when you wait for an event and you get an event The difference is that the microcontroller will wake up But it will continue linearly with the code from the moment where it stopped So it has a drawback and the benefit the drawback is you don't know what source of the event caused it And the benefit is that you don't need to care and you don't need to call the interrupt routine So the resume from low-power mode is faster Additionally, events can be generated if you have an interrupt that is registered but not enabled in NVIC So if you enable this in the system control block, you can as well generate events from disabled interrupts That's just a little off topic to tell you how you can activate the entry into low-power modes in the sleep mode Do you know what is the difference between sleep and stop mode in a sleep mode? Only the core is stopped and if you set up the Peripheral bus registers, you can as well stop some peripherals selected once But the clocks run the system bus runs DMAs run and All activated peripherals run as well. So the consumption can be for example, still 15 10 milliamps In stop mode the situation is different Because in stop mode you deactivate the oscillators Which means the crystal oscillator is off high speed oscillators are off PLL is off System bus is off core and flash are not clocked. So the consumption drops to the static consumption of the chip and If we don't optimize too much it can be around three five micro amps maximum So in stop mode most peripherals are frozen because they don't have any clock and If they are frozen they can't generate wake-up events So a big difference between sleep and stop is who can wake you up? Because in sleep mode any peripheral that's alive can give you an interrupt or even in stop mode only the ones that have some external connection or Some low-power oscillator still active Like a low-power timer or real-time clock you can as well get from the stop mode with the buttons Up to 16 pins connected on the package So if you have a power on button like on a mobile this can as well wake you up from a stop There is one another difference if you were running at a PLL clock and you resume from the stop mode The PLL was off and that the high frequency oscillator was off So you need to configure them again. So the startup time is a little bit longer But very often it's worth The current consumption use pair so consider which of the two modes is suitable for your low-power applications still from both of these low-power modes you can get back and And continue in the free RTOS and your application operations