Architectures and Applications for Wireless Sensor Networks (01204525) Node Programming Models Chaiporn Jaikaeo Department of Computer.

Slides:



Advertisements
Similar presentations
EMS1EP Lecture 4 Intro to Programming Dr. Robert Ross.
Advertisements

Slide 2-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Using the Operating System 2.
Chung-Ta King National Tsing Hua University
1 Lab 3 Objectives  Case study: “Hello world” program on motes  Write you first program on mote.
Chapter 6: Process Synchronization
MICRO-CONTROLLER: A microcontroller is the brain of the robot: These are: 1. Integrated Circuits (ICs) 2. Programmable.
Chapter 6 Limited Direct Execution
Computer Systems/Operating Systems - Class 8
CMPT 300: Operating Systems I Dr. Mohamed Hefeeda
Contiki A Lightweight and Flexible Operating System for Tiny Networked Sensors Presented by: Jeremy Schiff.
OS Fall ’ 02 Introduction Operating Systems Fall 2002.
7/13/2007AIIT Summer Course - D#1 Wireless Embedded Systems and Networking Lab Day 5: Part 1: TinyOS Programming on Open Source Distribution Jaein Jeong.
OS Spring’03 Introduction Operating Systems Spring 2003.
Mehmet Can Vuran, Instructor University of Nebraska-Lincoln Acknowledgement: Overheads adapted from those provided by the authors of the textbook.
Threads CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
OS Spring’04 Introduction Operating Systems Spring 2004.
Threading Abstractions for Embedded Operating Systems on Memory Constrained Devices Andrew Barton-Sweeney September 21, 2006.
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
Copyright Arshi Khan1 System Programming Instructor Arshi Khan.
What do operating systems do? manage processes manage memory and computer resources provide security features execute user programs make solving user.
Department of Electronic & Electrical Engineering Embedded system Aims: Introduction to: Hardware. Software Ideas for projects ? Robotics/Control/Sensors.
328eForth for Arduino Uno C. H. Ting February 16, 2012 SVFIG.
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
COMP201 Computer Systems Exceptions and Interrupts.
Protection and the Kernel: Mode, Space, and Context.
Architectures and Applications for Wireless Sensor Networks ( ) Sensor Node Programming II (UART and Radio) Chaiporn Jaikaeo
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
FINAL MPX DELIVERABLE Due when you schedule your interview and presentation.
Architecture Support for OS CSCI 444/544 Operating Systems Fall 2008.
A Revolutionary Programming Pattern that Will Clean up your Code : Coroutines in C++ David Sackstein ACCU 2015.
Concurrency, Mutual Exclusion and Synchronization.
The Contiki Operating System
Dhanshree Nimje Smita Khartad
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
Background: Operating Systems Brad Karp UCL Computer Science CS GZ03 / M th November, 2008.
ECS642U Embedded Systems Cyclic Execution and Polling William Marsh.
Lab 13 Department of Computer Science and Information Engineering National Taiwan University Lab13 – Interrupt + Timer 2014/12/23 1 /16.
Architectures and Applications for Wireless Sensor Networks (204525) Sensor Node Programming Chaiporn Jaikaeo Department of Computer.
Chapter 2 Processes and Threads Introduction 2.2 Processes A Process is the execution of a Program More specifically… – A process is a program.
Operating Systems 1 K. Salah Module 1.2: Fundamental Concepts Interrupts System Calls.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
Xiong Junjie Node-level debugging based on finite state machine in wireless sensor networks.
Department of Computer Science and Software Engineering
Architectures and Applications for Wireless Sensor Networks ( ) Sensor Network Programming and MoteLib Simulator Chaiporn Jaikaeo
CIS250 OPERATING SYSTEMS Chapter One Introduction.
Network Kernel Architectures and Implementation ( ) Node Programming Chaiporn Jaikaeo Department of Computer Engineering Kasetsart.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
1 Run-to-Completion Non-Preemptive Scheduler. 2 In These Notes... What is Scheduling? What is non-preemptive scheduling? Examples Run to completion (cooperative)
Slides created by: Professor Ian G. Harris Operating Systems  Allow the processor to perform several tasks at virtually the same time Ex. Web Controlled.
Assembly Language Co-Routines
Embedded C- Language Lets Learn fundamentals !!. An Embedded system is combination of computer hardware and software, and perhaps additional mechanical.
CSCI/CMPE 4334 Operating Systems Review: Exam 1 1.
Chapter 6 Limited Direct Execution Chien-Chung Shen CIS/UD
Contiki OS Sharvil Patel, Michael Ray, Emily Rowland 1.
Software Architecture of Sensors. Hardware - Sensor Nodes Sensing: sensor --a transducer that converts a physical, chemical, or biological parameter into.
Advanced Operating Systems CS6025 Spring 2016 Processes and Threads (Chapter 2)
1.1 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 1: Introduction What Operating Systems Do √ Computer-System Organization.
Introduction to Operating Systems Concepts
INTRODUCTION TO WIRELESS SENSOR NETWORKS
68HC11 Interrupts & Resets.
CS 6560: Operating Systems Design
OPERATING SYSTEMS CS3502 Fall 2017
Mechanism: Limited Direct Execution
Lecture Topics: 11/1 General Operating System Concepts Processes
Threads Chapter 4.
Node Programming Models
CSE 451: Operating Systems Autumn 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 596 Allen Center 1.
Still Chapter 2 (Based on Silberchatz’s text and Nachos Roadmap.)
CSE 451: Operating Systems Winter 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 412 Sieg Hall 1.
Presentation transcript:

Architectures and Applications for Wireless Sensor Networks ( ) Node Programming Models Chaiporn Jaikaeo Department of Computer Engineering Kasetsart University

2 Outline Microcontroller programming Microcontroller programming  Software development cycle Hardware abstraction Hardware abstraction Event-driven programming model Event-driven programming model  TinyOS Multithreaded programming model Multithreaded programming model  Contiki

3 IWING-MRF Mote Radio transceiver 8-bit AVR Microcontroller USB Connector (for reprogramming and power) Analog/Digital sensor connectors External battery connector UART connectors Morakot Saravanee, Chaiporn Jaikaeo, Intelligent Wireless Network Group (IWING), KU

4 Microcontroller flash memory BSL Typical Development Process For microcontrollers with bootstrap loader (BSL) installed For microcontrollers with bootstrap loader (BSL) installed Source code (C/Asm) Cross Compiler/Assembler Machine code Serial/USB

5 Build Simple App Let's build a simple application Let's build a simple application How to know whether our program is running? How to know whether our program is running?  Make mote output something  What can be used as output?

6 IWING-MRF Schematic Available on course's homepage Available on course's homepage

7 IWING-MRF – Blinking LED Task: turn a LED on and off repeatedly Task: turn a LED on and off repeatedly Idea Idea  Configure Port D's Pin 5 (PD5) for output  Repeatedly set the pin logic level to 0 and 1  Add some delay before toggling pin level

8 IWING-MRF C Code – blink.c How to add delay? How to add delay? Can the code be made shorter? Can the code be made shorter? #include main() { DDRD |= (1 << 5); // Make PD5 output while (1) { // Set pin logic to low PORTD &= ~(1 << 5); // Add some delay // Set pin logic to high PORTD |= (1 << 5); }

9 Compiling Make an ELF binary by running cross compiler Make an ELF binary by running cross compiler Note: blink.elf is not a Windows or Linux executable! Translate the object file into ihex format Translate the object file into ihex format $ avr-gcc -mmcu=atmega328p –o blink.elf blink.c $ avr-objcopy -j.text -j.data –O ihex blink.elf blink.hex

10 Flashing Code Into Mote Plug mote into a USB port Plug mote into a USB port Activate boot-loader Activate boot-loader  Press and release RESET while holding USER/B.L. Make sure it is recognized by your PC Make sure it is recognized by your PC Invoke chip programmer Invoke chip programmer $ avrdude -p atmega328p -c usbasp -U flash:w:blink.hex $ lsusb Bus 003 Device 049: ID 16c0:05dc VOTI Bus 001 Device 003: ID 046d:c03d Logitech, Inc.ls

11 IWING-MRF's Boot Loader

12 IWING's MoteLib Software Hardware Morakot Saravanee, Patra Poome, Chaiporn Jaikaeo, Intelligent Wireless Network Group (IWING), KU

13 Hardware Abstraction IWING-MRF Hardware IWING-MRF API Implementation

14 Mote and Network Emulator Virtual Mote

15 Event-Driven Programming Model MoteLib and TinyOS provides event-based programming environment MoteLib and TinyOS provides event-based programming environment Idle loop Radio event handlerSensor event handlerTimer event handler Boot event handler Handled by Kernel Handled by developer

16 Example Turn red LED on and off repeatedly every 500 ms Turn red LED on and off repeatedly every 500 ms #include Timer t; void fired() { ledToggle(0); } void boot() { timerCreate(&t); timerStart(&t, TIMER_PERIODIC, 500, fired); }

PORTB PORTC PORTD Practicum Board CPE, KU ResetPower Concurrency Example: press and release SW1 to toggle LED1 Example: press and release SW1 to toggle LED1  USB must still be operational by calling usbPoll() at least every 50 ms 17 SW1LED1

Failed Attempt 18 void checkSwitch() { while (!IS_PUSHED(SW1)) while (!IS_PUSHED(SW1)) ; while (IS_PUSHED(SW1)) while (IS_PUSHED(SW1)) ; toggle(LED1); toggle(LED1);} int main() { for (;;) for (;;) { usbPoll(); usbPoll(); checkSwitch(); checkSwitch(); }} Blocking Blocking

Finite State Machines (FSM) To allow concurrent tasks, each task is typically implemented using a Finite State Machine (FSM) To allow concurrent tasks, each task is typically implemented using a Finite State Machine (FSM) 19 Released Pushed SW1 pushed SW1 released / Toggle LED int state; int main() { state = RELEASED; state = RELEASED; for (;;) { for (;;) { usbPoll(); usbPoll(); checkSwitch(); checkSwitch(); }} void checkSwitch() { if (state == RELEASED) { if (state == RELEASED) { if (IS_PRESSED(SW1)) { if (IS_PRESSED(SW1)) { state = PUSHED; state = PUSHED; } } if (state == PUSHED) { if (state == PUSHED) { if (!IS_PRESSED(SW1)) { if (!IS_PRESSED(SW1)) { state = RELEASED; state = RELEASED; toggle(LED1); toggle(LED1); } }}

20 Creating a Reading Task Event-based code can be difficult to read and maintain Event-based code can be difficult to read and maintain Idea Idea  Make a reading task that runs forever  Other tasks can also be added to run concurrently Start timer Wait until timer expired Create timer Turn on sensors Request reading Wait until data ready Complete 4 samples? Compute and display average Turn off sensors

Problem with Event-based Model Threads: sequential code flowEvents: unstructured code flow Very much like programming with GOTOs 21

Events Require One Stack Four event handlers, one stack Four event handlers, one stack Eventhandler 1Eventhandler 2Eventhandler 3 Stack is reused for every event handler Eventhandler 4 22

Problem with Multithreading Four threads, each with its own stack Four threads, each with its own stack Thread 1Thread 2Thread 3Thread 4 23

24 Emulating Concurrency Previous example wouldn't work because of the blocking while-loop Previous example wouldn't work because of the blocking while-loop  Other parts of the system will be unresponsive Must return to kernel inside of the while- loops Must return to kernel inside of the while- loops During kernel's idle loop, keep jumping into the while-loops During kernel's idle loop, keep jumping into the while-loops

Coroutines Generalized subroutines Generalized subroutines  Allow multiple entry points for suspending and resuming execution at certain locations Can be used to implement: Can be used to implement:  Cooperative multitasking  Actor model of concurrency 25

Routine 2 Subroutines vs. Coroutines Routine 1 Subroutines Routine 2 Routine 1 Coroutines call call return return yieldyield yield yield 26 “Subroutines are a special case of coroutines.” --Donald Knuth Fundamental Algorithms. The Art of Computer Programming Fundamental Algorithms. The Art of Computer Programming

27 Programming Model Kernel's Idle loop Task 2 Event handler2 Task 1 Event handler1 Handled by Kernel Handled by developer call return call return continue yield yield continue

Implementing Continuation Each coroutine must be able to continue from where it last yielded Each coroutine must be able to continue from where it last yielded Routine 1 28 Main Loop continue yield continue yield

29 Duff's Device Invented to optimize data transfer by means of loop unwinding Invented to optimize data transfer by means of loop unwinding Switch cases are used like GOTO labels Switch cases are used like GOTO labels do { *to = *from++; } while(--count > 0); register n = (count + 7) / 8; switch(count % 8) { case 0: do { *to = *from++; case 7: *to = *from++; case 6: *to = *from++; case 5: *to = *from++; case 4: *to = *from++; case 3: *to = *from++; case 2: *to = *from++; case 1: *to = *from++; } while(--n > 0); }

30 Protothreads Invented by Adam Dunkels and Oliver Schmidt Invented by Adam Dunkels and Oliver Schmidt  Used in the Contiki OS Provides light-weight mechanism for concurrent programming using standard C macros and switch-case statements Provides light-weight mechanism for concurrent programming using standard C macros and switch-case statements Heavily inspired by Duff's Device and Simon Tatham's Coroutines in C Heavily inspired by Duff's Device and Simon Tatham's Coroutines in CCoroutines in CCoroutines in C See See 

Protothreads Protothreads require only one stack Protothreads require only one stack E.g, four protothreads, each with its own stack E.g, four protothreads, each with its own stack Events require one stack Protothread 1 Protothread 2Protothread 3Protothread 4 Just like events 31

Six-line implementation Protothreads implemented using the C switch statement Protothreads implemented using the C switch statement Heavily inspired by Duff's Device and Simon Tatham's Coroutines in C Heavily inspired by Duff's Device and Simon Tatham's Coroutines in CCoroutines in CCoroutines in C struct pt { unsigned short lc; }; #define PT_INIT(pt) pt->lc = 0 #define PT_BEGIN(pt) switch(pt->lc) { case 0: #define PT_EXIT(pt) pt->lc = 0; return 2 #define PT_WAIT_UNTIL(pt, c) pt->lc = __LINE__; case __LINE__: \ if(!(c)) return 0 if(!(c)) return 0 #define PT_END(pt) } pt->lc = 0; return 1 32

33 Protothreads Limitations Local variables must be manually preserved Local variables must be manually preserved  Local variables are created on stack  They are destroyed when function returns  So they should be stored in an explicit state object  Or declared static, if reentrancy is not required switch-case statements are not allowed switch-case statements are not allowed Cannot take advantage of multi-processing Cannot take advantage of multi-processing

Contiki OS Scheduler 34