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.
An embedded system with interrupts is presented here:
The main module contains S2.1 processor with memory. The board has four interrupts (int0, int1, int2, int3) and four input ports (10, 11, 12, 13). I made a set of tools for practicing with this kind of programming. The tool consists of the compiler, the assembler and the simulator of the processor (S2.1 processor). The language I used can be found here (Rz language). The interrupt program looks like this:
// simple interrupt
// a simple loop run for 10000 inst.
// when interrupt occurs, it prints out cnt
cnt // global counter
int0()
print(cnt," ")
cnt = cnt + 1
main()
cnt = 0
while( 1 ) // wait for int
doze() // sleep and wait for interrupt
This program has an empty main loop. It waits for interrupt. The interrupt service routine increments a counter and print it out. The simulation runs for 1 second, or 1,000,000 time unit at the speed of the processor 1MHz. The sequence of commands to run the experiment is, compile the program (simple-int.txt), assemble the assembly file, run the simulator. The output of compiler will be an assembly language file (simple-int-s.txt). This file is assembled into an executable object file (simple-int-s.obj) suitable for the processor simulator.
C:\iot-rz\test>rz simple-int.txt > simple-int-s.txt
C:\iot-rz\test>asm simple-int-s.txt
C:\iot-rz\test>sim simple-int-s.obj
load program, last address 37
int 201
<0> int 402
<1> int 603
<2> int 804
. . .
<94> int 19296
<95> int 19497
<96> int 19698
<97> int 19899
<98> stop, clock 20000, execute 1897 instructions
C:\iot-rz\test>
ei(n) // n
0,1,2,3 equivalent to mov r1 #n, trap
r1 #16
di(n) // n 0,1,2,3
equivalent to mov r1 #n, trap r1 #15
settimer0(k) // equivalent
to mov r1 #k, trap r1 #13
settimer1(k) // equivalent
to mov r1 #k, trap r1 #14
x = readport(m) // m 10,11,12,13
int0()
x = readport(10)
print(x)
main()
while(1)
doze() // sleep and wait
for interrupt
int0()
x = readport(12)
y = x - ((x / 256) * 256) // modulo 256
print(y)
main()
settimer0(200)
// set timer0 to 200
while(1)
doze()
+-50
. The default
digital wave is a square wave of amplitude (0,5).turn-on-pump
t = 0
while( t < 10 )
delay(1 sec)
t = t + 1
turn-off-pump
t // global
int()
t = t + 1
if( t == 10 )
turn-off-pump
disableint()
main()
settimer(1 sec)
turn-on-pump
t = 0
while(1)
doze()
i = 0
while( i < 2 )
x = readwave()
if x > 0 then
while(1)
x = readwave()
if x < 0
then // zero cross from + to -
t0 =
readclock()
break
else if x < 0 then
while(1)
x = readwave()
if x > 0 then
t1 =
readclock()
break
i = i + 1
// we now have two zero cross
if t1 > t0 then
dt = t1 - 10
else
dt = t0 - t1
f = 1 / (2*dt) // compute the
frequency
state input next-state action
1 +
2
2 +
2 it is + + ... stay
on 2
2 -
3 zero cross + to -
3 -
3 it is - - ... stay
on 3
3 +
4 zero cross - to +
1 -
5 start -
5 -
5 it is - - ... stay
on 5
5 +
2 goto start of +
state // global
t0, t1 // global
int()
x = readwave()
if state == 1
if( x > 0 ) then state = 2
else state =
5
else if state == 2
if( x > 0 ) then state = 2
else
state = 3
t0 =
readclock() // record time of zero cross + to -
else if state == 3
if( x < 0 ) then state = 3
else
state = 4
t1 =
readclock() // record time of zero cross - to +
else if state == 5
if( x < 0 ) then state = 5
else state = 2
main()
state = 1
while(1)
doze()
if state == 4 then
dt = t1 - t0
f = 1 /
(2*dt) // compute the
frequency
state =
1
// reset for next cycle
settimer0(d).
It
determines how often the interrupt occurs. The number d
is the number to unit time of interrupt interval.