Designing low-power embedded applications
In this section, a few design patterns are proposed to achieve a better energy profile on the target device, by evaluating the power demand of all the components and the states of the system we are about to design. Once we know how to measure the values in the target, and the details about the low-power mode in the selected architecture and microprocessor family, the application can be programmed keeping other parameters in mind, such as the energy efficiency of the software we are writing.
Replacing busy loops with sleep mode
The reason busy loops are very popular among hobbyists is that they are so easy to implement. Suppose that the system needs to wait for a digital input to switch to a low-logic state, and this input is mapped to a certain GPIO. This can be easily done with the following one-liner:
while((GPIOX_IDR & (1 << INPUT_PINX)) != 0) ;
While this is perfectly working as expected, it will force the CPU in a loop of fetch-decode...