You are on page 1of 44

Chapter 3

Embedded OS for WSNs


MSc. Nguyen Khanh Loi
nkloi@hcmut.edu.vn
2/2021
MSc Nguyen Khanh Loi
Content

Chapter 3: Embedded OS for end-devices


❖ Intro to Contiki-OS
❖ Programming using Contiki
❖ Cooja Emulator

Wireless Sensor Networks 2 Embedded OS for WSNs


MSc Nguyen Khanh Loi
Computer Systems

❖ Traditional systems: separate chips


❖ Microcontroller: integrate on single chip

Mote
MCU
CPU Timer
Network Peripherals

Memory Storage

Wireless Sensor Networks 3 Embedded OS for WSNs


MSc Nguyen Khanh Loi
Mote Characteristics

❖ Limited resources
✓ RAM, ROM, Computation, Energy
• Wakeup, do work as quickly as possible, sleep

❖ Hardware modules operate concurrently


✓ No parallel execution of code (not Core 2 Duos!)
• Asynchronous operation is first class

❖ Diverse application requirements


✓ Efficient modularity

❖ Robust operation
✓ Numerous, unattended, critical
• Predictable operation
Wireless Sensor Networks 4 Embedded OS for WSNs
MSc Nguyen Khanh Loi
What is an Operating System (OS)?

❖ OS is a software component in computer systems


❖ OS controls resources of the computer system
✓ Allocation of memory for processes
✓ Power management
❖ OS coordinates activities in the computer system
✓ Which process uses the CPU
✓ When I/O takes place
❖ Application programs run on top of OS Services
❖ Many criteria to classify OS
✓ Design goal: general purpose vs OS for specific purposes
✓ Target platforms: limitation of resources, reliability
guarantees
Wireless Sensor Networks 5 Embedded OS for WSNs
MSc Nguyen Khanh Loi
General purpose OS

❖ Design principles and Concepts


❖ Execution model
❖ Memory management
❖ Multitasking and concurrency
❖ Support for File systems
❖ Safety and Security Features
❖ GUI, I/O System, Portability
❖ Device Drivers and OS Networking

Wireless Sensor Networks 6 Embedded OS for WSNs


MSc Nguyen Khanh Loi
Why OS for WSNs?

❖ Nodes are designed to operate with limited resources


✓ Power: WSN use batteries as a power supply
✓ Memory and operational capabilities: sensing is less
resource demanding than computation in conventional OS
❖ Power management in WSN OS is very important
❖ Variety of solutions for WSN
✓ Several WSN OS: TinyOS, Contiki OS, RTOS, etc
✓ Several Simulator tools: Cooja, NS-2, TOSSIM, etc.
✓ Several programming languages: C, nesC, etc.
❖ TinyOS and Contiki: the most popular WSN OS

Wireless Sensor Networks 7 Embedded OS for WSNs


MSc Nguyen Khanh Loi
Traditional Systems

❖ Well established layers of Application


abstractions Application
User
❖ Strict boundaries
❖ Ample resources
System
❖ Independent Applications at Network Stack
Threads Transport
endpoints communicate P2P
Network
through routers Address Space
Data Link
Files
Physical Layer
Drivers

Hardware

Wireless Sensor Networks 8 Embedded OS for WSNs


MSc Nguyen Khanh Loi
by comparison, WSNs ...

❖ Highly Constrained resources


✓ Processing, storage, bandwidth, power
❖ Applications spread over many small nodes
✓ Self-organizing Collectives
✓ Highly integrated with changing environment and network
❖ Robust
✓ inaccessible, critical operation
❖ Unclear where the boundaries belong

Wireless Sensor Networks 9 Embedded OS for WSNs


MSc Nguyen Khanh Loi
Soft or hard real-time applications

❖ Classified as non-real-time, hard or soft real-time. However, is somewhat fuzzy.


❖ Non-real-time: no important deadlines (all deadlines can be missed)
✓ Computer programs

Usefulness
❖ Soft real-time: deadline may not be fulfilled
✓ Multimedia transmission and reception
✓ Networking, telecom (Mobile) networks
✓ Websites and services Soft
❖ Hard real-time: no deadlines can be missed. Hard
✓ Air traffic control
✓ Nuclear power plant control Time

Wireless Sensor Networks 10 Embedded OS for WSNs


MSc Nguyen Khanh Loi
What is RTOS?

❖ Real-time operating system


✓ Used in applications that require fast, accurate
response times.
✓ Limited resources. RTOS focuses on only a
handful of features.
▪ Maximize the number of threads
▪ Scheduler
▪ Tasks/Multi-task
▪ Managed CPU/hardware resources and
providing services to other programs
✓ Processing must be done or the system will fail

Wireless Sensor Networks 11 Embedded OS for WSNs


MSc Nguyen Khanh Loi
What is Kernel?

✓ Part of the OS that offers core services to application software


✓ Offered abstraction layer that hides processor hardware details from the
application software
✓ 6 main basic service

Wireless Sensor Networks 12 Embedded OS for WSNs


MSc Nguyen Khanh Loi
Task state

▪ RUNNING: executing on the CPU


▪ READY: ready to be executed
▪ WAITING: waiting for an event
▪ INACTIVE: not active

Wireless Sensor Networks 13 Embedded OS for WSNs


MSc Nguyen Khanh Loi
Scheduler

❖ Part of the kernel that determines which tasks to execute.


❖ Some commonly used RTOS scheduling algorithms are:
✓ Cooperative scheduling
✓ Round-robin scheduling
✓ Preemptive scheduling

Wireless Sensor Networks 14 Embedded OS for WSNs


MSc Nguyen Khanh Loi
Cooperative scheduling

❖ Traditional programing.
❖ Each task can only be executed when another task has completed.
❖ Weakness: one task can use up all CPU resources.

Wireless Sensor Networks 15 Embedded OS for WSNs


MSc Nguyen Khanh Loi
Round-robin scheduling

❖ Simple, easy to implement


❖ Scheduler generally employs time-sharing
❖ Each task is executed at a predetermined time (time slice) and has no priority

Wireless Sensor Networks 16 Embedded OS for WSNs


MSc Nguyen Khanh Loi
Preemptive scheduling

❖ Preemption is the act of temporarily interrupting an executing task, with the


intention of resuming it at a later time.

Wireless Sensor Networks 17 Embedded OS for WSNs


MSc Nguyen Khanh Loi
Non-preemptive scheduling

Wireless Sensor Networks 18 Embedded OS for WSNs


MSc Nguyen Khanh Loi
Inter-task communication and resource sharing

❖ Tasks need to communicate with each other or access shared


resources together.
❖ Inter-Thread Communication
✓ Signal Events
✓ Message Queue
✓ Mail Queue
❖ Resource Sharing
✓ Mutexes
✓ Semaphores

Wireless Sensor Networks 19 Embedded OS for WSNs


MSc Nguyen Khanh Loi
Inter-Thread Communication: Signal Events

❖ Signals are used to trigger execution states between tasks.


❖ RTOS allow you to control or wait for signal flags

Wireless Sensor Networks 20 Embedded OS for WSNs


MSc Nguyen Khanh Loi
Inter-Thread Communication: Message Queue

❖ Message passing is another basic communication model between tasks.


❖ The data is passed from one thread to another in a FIFO-like operation.
❖ Using message queue functions, you can control, send, receive, or wait
for messages.

Wireless Sensor Networks 21 Embedded OS for WSNs


MSc Nguyen Khanh Loi
Inter-Thread Communication: Mail Queue

❖ A mail queue resembles a Message Queue.


❖ Data that is being transferred consists of memory blocks

Wireless Sensor Networks 22 Embedded OS for WSNs


MSc Nguyen Khanh Loi
Resource Sharing: Mutexes

❖ Mutual exclusion (widely known as Mutex) is used in various operating


systems for resource managemen.
❖ Many resources in a microcontroller device can be used repeatedly, but
only by one task at a time.
❖ Mutexes are used to protect access to a shared resource.

Wireless Sensor Networks 23 Embedded OS for WSNs


MSc Nguyen Khanh Loi
Resource Sharing: Semaphores

❖ Semaphores are used to manage and protect access to shared resources.


❖ Semaphores are very similar to Mutexes.
❖ Semaphore can be used to permit a fixed number of threads to access a
pool of shared resources.

Task Task

Task Task

Wireless Sensor Networks 24 Embedded OS for WSNs


MSc Nguyen Khanh Loi
Contiki OS

❖ Contiki is designed to address four essential


demand ROM
✓ Need for a lightweight OS
✓ Dynamic loading/ re-programming in a
resource constrained environment
✓ Event-driven kernel model desired
✓ Optional Protothread/ Preemptive
multithreading
❖ A Contiki system is partitioned into core and loaded
programs
✓ Processes
▪ Application programs
▪ Services
✓ Core
▪ Kernel
▪ Program loader
▪ Libraries
Wireless Sensor Networks 25 Embedded OS for WSNs
MSc Nguyen Khanh Loi
Cooja GUI

Wireless Sensor Networks 26 Embedded OS for WSNs


MSc Nguyen Khanh Loi
Event-driven and multi-threaded

Event-driven Multi-threaded
- No wait() statements + wait() statements
- No preemption + Preemption possible
- State machines + Sequential code flow
+ Compact code - Larger code overhead
+ Locking less of a problem - Locking problematic
+ Memory efficient - Langer memory requirements

Multi threaded

Event-driven

Wireless Sensor Networks 27 Embedded OS for WSNs


MSc Nguyen Khanh Loi
Contiki model

❖ Event-driven systems treat processes as event handlers that


monopolize the CPU till completion  CPU is unable to
respond to external stimuli
❖ A multi-threaded model consumes a lot of memory.
❖ Contiki solves this problem by using a hybrid model:
✓ Kernel is event-based
✓ Multi-threading implemented as a library

Wireless Sensor Networks 28 Embedded OS for WSNs


MSc Nguyen Khanh Loi
Contiki process

❖ The process structure comprises of :


✓ Process Control Block:
▪ Contains the information
about the process name,
process state, pointer to
process thread.
▪ Light weighted
✓ Process Thread:
▪ Contains the code of
process

Wireless Sensor Networks 29 Embedded OS for WSNs


MSc Nguyen Khanh Loi
Process Scheduling

❖ Cooperative: In Cooperative the process are executed in a continuous


manner and it cannot be preempted until the process is completely executed.
After that the next process is scheduled.
❖ Preemptive: In Preemptive it stops the execution of cooperative process
whenever there is an interruption due to the I/O or timers.

Wireless Sensor Networks 30 Embedded OS for WSNs


MSc Nguyen Khanh Loi
The Contiki code

Header files

#include "contiki.h" Defines the name of


#include <stdio.h> /* For printf() */ the process

PROCESS(hello_world_process, "Hello world process");


Defines the process
AUTOSTART_PROCESSES(&hello_world_process); will be started every
time module is
PROCESS_THREAD(hello_world_process, ev, data) { loaded
PROCESS_BEGIN();
printf("Hello world\n");
PROCESS_END();
}

Threads must have Event parameter; process can receive data


an end statement process can respond to during an event
events

Wireless Sensor Networks 31 Embedded OS for WSNs


MSc Nguyen Khanh Loi
The Contiki processes

Wireless Sensor Networks 33 Embedded OS for WSNs


MSc Nguyen Khanh Loi
Events in Contiki

❖Timer events:
❖a process may set a timer
❖generate an event when timer expires
❖External events:
❖I/O Interrupt.
❖UART, push-button, a radio chip…
❖Internal events:
❖Any process has the possibility to address events to any other
process, or itself.
❖Inter-process communication

Wireless Sensor Networks 34 Embedded OS for WSNs


MSc Nguyen Khanh Loi
Events in Contiki

Event ID Description
PROCESS_EVENT_NONE 0x80 No event.
PROCESS_EVENT_INIT 0x81 Delivered to a process when it is started.

PROCESS_EVENT_POLL 0x82 Delivered to a process being polled.

PROCESS_EVENT_EXIT 0x83 Delivered to an exiting a process.

PROCESS_EVENT_SERVICE_REMOVED 0x84 Unused.

PROCESS_EVENT_CONTINUE 0x85 Delivered to a paused process when resuming execution.

PROCESS_EVENT_MSG 0x86 Delivered to a process upon a sensor event.

PROCESS_EVENT_EXITED 0x87 Delivered to all processes about an exited process.

PROCESS_EVENT_TIMER 0x88 Delivered to a process when one of its timers expired.

PROCESS_EVENT_COM 0x89 Unused.

PROCESS_EVENT_MAX 0x8a The maximum number of the system-defined events.

Wireless Sensor Networks 35 Embedded OS for WSNs


MSc Nguyen Khanh Loi
Events in Contiki

Wireless Sensor Networks 36 Embedded OS for WSNs


MSc Nguyen Khanh Loi
Events in Contiki

Process 1 Process 2 Process 3

Wireless Sensor Networks 37 Embedded OS for WSNs


MSc Nguyen Khanh Loi
Events in Contiki

#include "contiki.h"
#include "process.h" This message from process 1
#include <stdio.h> This message from process 3
PROCESS(myprocess1, "process 1"); Process 2 is called by my process1
PROCESS(myprocess2, "process 2");
PROCESS(myprocess3, "process 3");
AUTOSTART_PROCESSES(&myprocess1,&myprocess2,&myprocess3);
PROCESS_THREAD(myprocess1, ev, data){
PROCESS_BEGIN();
process_post(&myprocess2, 0x100, 12);
printf("This message from process 1\n");
PROCESS_END();
}
PROCESS_THREAD(myprocess2, ev, data) {
PROCESS_BEGIN();
while(1){
PROCESS_WAIT_EVENT();
printf("Process 2 is called by my process1");
break;
} PROCESS_END();
}
PROCESS_THREAD(myprocess3, ev, data) {
PROCESS_BEGIN();
printf("This message from process 3\n");
PROCESS_END();
}

Wireless Sensor Networks 38 Embedded OS for WSNs


MSc Nguyen Khanh Loi
Contiki timers

❖ Contiki has two main timer types: etimer and rtimer


❖ Etimer: generates timed events
✓ Declarations:
static struct etimer et;
✓ In main process:
while(1) {
etimer_set(&et, CLOCK_SECOND);
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
etimer_reset(&et);
}
❖ Rtimer: uses callback function
✓ Callback executed after specified time
rtimer_set(&rt, time, 0 , &callback_function, void *argument);

Wireless Sensor Networks 39 Embedded OS for WSNs


MSc Nguyen Khanh Loi
Contiki timers

Wireless Sensor Networks 40 Embedded OS for WSNs


MSc Nguyen Khanh Loi
Contiki Threading

Wireless Sensor Networks 41 Embedded OS for WSNs


MSc Nguyen Khanh Loi
Contiki Threading

Wireless Sensor Networks 42 Embedded OS for WSNs


MSc Nguyen Khanh Loi
Contiki Threading
#include "contiki.h" PROCESS_THREAD(mythread, ev, data){
#include "sys/mt.h" static int toggle = 0;
#include <stdio.h> static struct etimer timer;
PROCESS(mythread, "demo thread"); static struct mt_thread thread1, thread2;
AUTOSTART_PROCESSES(&mythread); int th1 = 1, th2 = 2;
static int count=0; PROCESS_BEGIN();
void solve(int data) mt_init();
{ mt_start(&thread1, solve, th1);
while(1){ mt_start(&thread2, solve, th2);
printf("[Thread %d] Step 1: etimer_set(&timer, CLOCK_SECOND * 2);
%d\n", data, count); while(1){
mt_yield(); PROCESS_WAIT_EVENT();
printf("[Thread %d] Step 2: if(ev == PROCESS_EVENT_TIMER) {
%d\n", data, count); if(toggle) {
mt_yield(); mt_exec(&thread1);
count ++; toggle--;
} } else {
mt_exit(); mt_exec(&thread2);
} toggle++;
}
etimer_reset(&timer);
}
}
mt_stop(&thread1);
mt_stop(&thread2);
mt_remove();
PROCESS_END();
}

Wireless Sensor Networks 43 Embedded OS for WSNs


MSc Nguyen Khanh Loi
LED Blinking

#include "contiki.h"
#include "dev/leds.h"
#include <stdio.h>
static struct etimer et;
static uint8_t i;
PROCESS(hello_world_process, "Hello world process");
AUTOSTART_PROCESSES(&hello_world_process);
PROCESS_THREAD(hello_world_process, ev, data) {
PROCESS_BEGIN();
etimer_set(&et, CLOCK_SECOND*1);
i =0;
while(1) {
PROCESS_YIELD();
if (ev==PROCESS_EVENT_TIMER) {
leds_toggle(LEDS_BLUE); //LEDS_ORANGE or LEDS_GREEN
printf("Timer fired: i=%d \n",i);
i++;
etimer_restart(&et);
}
}
PROCESS_END();
}

Wireless Sensor Networks 44 Embedded OS for WSNs


MSc Nguyen Khanh Loi
Running Contiki on a Hardware

❖ Write your code


❖ Compile Contiki and the application
make TARGET=sky
Make file

❖ If you plan to compile your code on the chosen platform more than
once;
make TARGE=sky savetarget
❖ Upload your code
make simple_process.upload
❖ Login to the device
make login

Wireless Sensor Networks 45 Embedded OS for WSNs


MSc Nguyen Khanh Loi

You might also like