Professional Documents
Culture Documents
Embedded OS
MSc. Nguyen Khanh Loi
nkloi@hcmut.edu.vn
2
MSc Nguyen Khanh Loi
Computer Systems
Mote
MCU
CPU Timer
Network Peripherals
Memory Storage
3
MSc Nguyen Khanh Loi
Mote Characteristics
v Limited resources
ü RAM, ROM, Computation, Energy
• Wakeup, do work as quickly as possible, sleep
v Robust operation
ü Numerous, unattended, critical
• Predictable operation
4
MSc Nguyen Khanh Loi
What is an Operating System (OS)?
6
MSc Nguyen Khanh Loi
Why OS for WSNs?
7
MSc Nguyen Khanh Loi
Traditional Systems
Hardware
8
MSc Nguyen Khanh Loi
by comparison, WSNs ...
9
MSc Nguyen Khanh Loi
Soft or hard real-time applications
Usefulness
v Soft real-time: deadline may not be fulfilled
ü Multimedia transmission and reception
ü Networking, telecom (Mobile) networks
ü Websites and services Soft
v Hard real-time: no deadlines can be missed. Hard
ü Air traffic control
ü Nuclear power plant control Time
10
MSc Nguyen Khanh Loi
What is RTOS?
11
MSc Nguyen Khanh Loi
What is Kernel?
12
MSc Nguyen Khanh Loi
Task state
13
MSc Nguyen Khanh Loi
Scheduler
14
MSc Nguyen Khanh Loi
Cooperative scheduling
v Traditional programing.
v Each task can only be executed when another task has completed.
v Weakness: one task can use up all CPU resources.
15
MSc Nguyen Khanh Loi
Round-robin scheduling
16
MSc Nguyen Khanh Loi
Preemptive scheduling
17
MSc Nguyen Khanh Loi
Non-preemptive scheduling
18
MSc Nguyen Khanh Loi
Inter-task communication and resource sharing
19
MSc Nguyen Khanh Loi
Inter-Thread Communication: Signal Events
20
MSc Nguyen Khanh Loi
Inter-Thread Communication: Message Queue
21
MSc Nguyen Khanh Loi
Inter-Thread Communication: Mail Queue
22
MSc Nguyen Khanh Loi
Resource Sharing: Mutexes
23
MSc Nguyen Khanh Loi
Resource Sharing: Semaphores
Task Task
Task Task
24
MSc Nguyen Khanh Loi
Contiki OS
26
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
27
MSc Nguyen Khanh Loi
Contiki model
28
MSc Nguyen Khanh Loi
Contiki process
29
MSc Nguyen Khanh Loi
The Contiki code
Header files
30
MSc Nguyen Khanh Loi
Process Scheduling
31
MSc Nguyen Khanh Loi
The Contiki processes
33
MSc Nguyen Khanh Loi
Events in Contiki
vTimer events:
va process may set a timer
vgenerate an event when timer expires
vExternal events:
vI/O Interrupt.
vUART, push-button, a radio chip…
vInternal events:
vAny process has the possibility to address events to any other
process, or itself.
vInter-process communication
34
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.
35
MSc Nguyen Khanh Loi
Contiki timers
36
MSc Nguyen Khanh Loi
Contiki timers
37
MSc Nguyen Khanh Loi
Events in Contiki
38
MSc Nguyen Khanh Loi
Events in Contiki
p o st
s_
ces Process 3
Process 1 pro Process 2
39
MSc Nguyen Khanh Loi
Events in Contiki
#include "contiki.h"
#include "process.h"
#include <stdio.h> This message from process 1
PROCESS(myprocess1, "process 1"); This message from process 3
PROCESS(myprocess2, "process 2"); Process 2 is called by my process1
PROCESS(myprocess3, "process 3");
AUTOSTART_PROCESSES(&myprocess1,&myprocess2,&myprocess3);
PROCESS_THREAD(myprocess1, ev, data){
PROCESS_BEGIN();
static int data = 12;
process_post(&myprocess2, 0x100, &data);
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();
}
40
MSc Nguyen Khanh Loi
Contiki Threading
41
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();
}
42
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();
}
43
MSc Nguyen Khanh Loi
Running Contiki on a Hardware
v If you plan to compile your code on the chosen platform more than
once;
make TARGE=sky savetarget
v Upload your code
chown :user /dev/ttyUSB0
sudo make TARGET=sky simple_process.upload
v Login to the device
sudo make TARGET=sky login
44
MSc Nguyen Khanh Loi