Internet of Things are a kind of embedded systems that connected through internet. Hence, it is advisable to study how to program embedded systems in order to program IoT devices. Most of IoT devices will be mobile and therefore low-power consumption is a requirement. In this lecture I will present approach to lower the power consumption of embedded systems through programming.
An embedded system connected to a physical world. This connection can be implemented as a port where the signal can be read from the external world into the processor. One simple way to do it is called "polling" where the processor keeps reading the port:
while(1)
a = readport()
process(a)
The frequency of reading the external signal is as fast as the time to execute one time around the loop. Now let us consider how much the power is consumed by running this program for one second. Let us assume that the processor will consume the power one unit when it executes one instruction, let us call this unit e. Let the processor has the ability to run one million instructions per second (1 MIPS). This entailed that the clock speed of this processor is probably 1 MHz if the processor can execute one instruction per clock (processor cycle time 1 micro-second, us). This processor is far cry from the modern processor running in a typical laptop machine (2 GHz processor clock, 2016 Intel or ARM core). So, running through the above program for one second will consume 1 Me. If one time around the loop takes 100 instructions to do processing and other overhead for looping, then the port will be read every 10 milli-second, ms., in other words with the rate 10 KHz. If we need to process the external signal with that rate, we run full speed of the processor, it will consume 1 Me.
Instead of do "polling", there is another mechanism to allow processor to read the port periodically, the interrupt. An interrupt is an external event that causes a processor to suspend its current program and jumps to a special program called "interrupt service routine" (ISR). Once the interrupt service routine is complete, it jumps back to continue the suspended program. We can issue this external interrupt periodically, usually using the peripheral called "counter and timer devices" and use ISR to perform reading and processing the signal from the port.
// interrupt service routine
interrupt()
a = readport()
process(a)
main()
while(1)
// do nothing
The main loop now do nothing, the processor runs an empty loop and waits for interrupt to occur. All the hard work is done in the ISR. This style of programming is called "interrupting". Let us see how much energy this program takes. Let the interrupt occurs every 10 ms (rate 10KHz), so that the rate of reading the port of this program is the same as the "polling" program. Because it performs the same amount of work as "polling" program, it consumes similar amount of energy, 1 Me. If we reduce the rate, for example 10 times, to 1KHz, or interrupt every 100 ms. The amount of energy consume will not reduce because the processor will run the same amount of number of instructions but it is shifted more to the main "empty loop". To reduce the energy, we need to avoid wasting the processor cycle.
The processor has one special instruction, "sleep and wait for interrupt" (doze), once executed, the processor will be put in the sleep state and not consuming much energy (assume zero energy). When an interrupt occurs, the processor will wake up, jumps to ISR, when ISR is completed, it will continue from where it wakes up. Now we insert this instruction into the main loop:
// interrupt service routine
interrupt()
a = readport()
process(a)
main()
while(1)
doze() // sleep and wait for
interrupt
How much energy this new program consume? Assume interrupt every 100ms (rate 1KHz), the ISR runs 1,000 times in one second, each time executed 100 instructions, therefore total instructions executed is 100,000. The main loop now runs until doze() then stop, so it may takes 2-3 instructions to go around the loop. This main loop will be repeated the same amount of time as the interrupt occurs. So, this amount of instruction overhead in the main loop is negligible. The total energy consumed is therefore 100 Ke. So, this program consumes 10 times less than the polling program. It has not wasting any processor cycle.