Feb 2007TinyOS/nesC Basic Concepts1 Introduction to TinyOS Programming Topics  TinyOS execution model  Async and atomic code  Configuration and wiring.

Slides:



Advertisements
Similar presentations
How to use TinyOS Jason Hill Rob Szewczyk Alec Woo David Culler An event based execution environment for Networked Sensors.
Advertisements

NesC Prepared for the Multimedia Networks Group University of Virginia.
Feb 2007WSN Training: First Steps in nesC Programming1 First Steps in TinyOS and nesC Programming Topics  Timer Application: MyApp  Application directory.
1 Lab 3 Objectives  Case study: “Hello world” program on motes  Write you first program on mote.
Sensor Network Platforms and Tools
Chapter 13 Embedded Systems
TinyOS Introduction Advanced Computer Networks. TinyOS Outline  Introduction to the Architecture of TinyOS and nesC  Component Model –Components, interfaces,
Architectural Support for OS March 29, 2000 Instructor: Gary Kimura Slides courtesy of Hank Levy.
Contiki A Lightweight and Flexible Operating System for Tiny Networked Sensors Presented by: Jeremy Schiff.
Systems Wireless EmBedded nesC Update Eric Brewer with help from David Culler, David Gay, Phil Levis, Rob von Behren, and Matt Welsh.
How to Code on TinyOS Xufei Mao Advisor: Dr. Xiang-yang Li CS Dept. IIT.
Sample Project Ideas KD Kang. Project Idea 1: Real-time task scheduling in TinyOS EDF in TinyOS 2.x –Description is available at
I/O Hardware n Incredible variety of I/O devices n Common concepts: – Port – connection point to the computer – Bus (daisy chain or shared direct access)
Home: Phones OFF Please Unix Kernel Parminder Singh Kang Home:
OS Spring’03 Introduction Operating Systems Spring 2003.
TinyOS Software Engineering Sensor Networks for the Masses.
NesC: 1.1 Bumps and Future Directions David Gay, Intel Research, Berkeley (and the nesC and TinyOS teams)
1 Lab 3 Objectives  Case study: “Hello world” program on motes  Write you first program on mote.
Chapter 13: I/O Systems I/O Hardware Application I/O Interface
1 Software Development Infrastructure for Sensor Networks  Operating systems ( TinyOS )  Resource (device) management  Basic primitives  Protocols.
System Calls 1.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
By: R Jayampathi Sampath
April 15, 2005TinyOS: A Component Based OSPage 1 of 27 TinyOS A Component-Based Operating System for Networked Embedded Systems Tom Bush Graduate College.
1 Lab2 Objectives  Basics of TinyOS  Basics of nesC programming language.
1 Lab2 Objectives  Basics of TinyOS  Basics of nesC programming language.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
Operating Systems Lecture 2 Processes and Threads Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Dhanshree Nimje Smita Khartad
Simulation of Distributed Application and Protocols using TOSSIM Valliappan Annamalai.
HANBACK ELECTRONICS CO., LTD. 저자권 보호됨 TinyOS & NesC.
Part 2 TinyOS and nesC Programming Selected slides from:
CE Operating Systems Lecture 3 Overview of OS functions and structure.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Processes Introduction to Operating Systems: Module 3.
1 CSE451 Architectural Supports for Operating Systems Autumn 2002 Gary Kimura Lecture #2 October 2, 2002.
RTX - 51 Objectives  Resources needed  Architecture  Components of RTX-51 - Task - Memory pools - Mail box - Signals.
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
Operating Systems 1 K. Salah Module 1.2: Fundamental Concepts Interrupts System Calls.
Overview of Operating Systems Introduction to Operating Systems: Module 0.
Xiong Junjie Node-level debugging based on finite state machine in wireless sensor networks.
System Components ● There are three main protected modules of the System  The Hardware Abstraction Layer ● A virtual machine to configure all devices.
WSN Software Platforms - concepts Vinod Kulathumani Lecture uses some slides from tutorials prepared by authors of these platforms.
Chapter 13 – I/O Systems (Pgs ). Devices  Two conflicting properties A. Growing uniformity in interfaces (both h/w and s/w): e.g., USB, TWAIN.
1 Lab2 Objectives  Basics of TinyOS  Basics of nesC programming language.
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Lab 3, Part 2 Selected slides from: Wireless Sensor Networks Hardware/Software Tiny OS & NesC Programming borrowed from Turgay Korkmaz.
TinyOS Sandeep Gupta. Operating System (OS) What is an OS? Main functions  Process management  Memory management  Resource management Traditional OSs.
1 Chapter 2: Operating-System Structures Services Interface provided to users & programmers –System calls (programmer access) –User level access to system.
Why does it need? [USN] ( 주 ) 한백전자 Background Wireless Sensor Network (WSN)  Relationship between Sensor and WSN Individual sensors are very limited.
Lecture 1 Page 1 CS 111 Summer 2013 Important OS Properties For real operating systems built and used by real people Differs depending on who you are talking.
TinyOS Sandeep Gupta. TinyOS basics TinyOS is  Single tasking OS  Interrupt driven Written using a Component based language A set of components put.
TinyOS and nesC. Outline ● Wireless sensor networks and TinyOS ● Networked embedded system C (nesC) – Components – Interfaces – Concurrency model – Tool.
Introduction to Operating Systems Concepts
Simulation of Distributed Application and Protocols using TOSSIM
Chapter 2: System Structures
Real-time Software Design
An Introduction to nesC
WSN Training: TinyOS/nesC Basic Concepts TinyOS and nesC
Chapter 2: Operating-System Structures
CSE 451: Operating Systems Autumn 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 596 Allen Center 1.
CSE 451: Operating Systems Autumn 2001 Lecture 2 Architectural Support for Operating Systems Brian Bershad 310 Sieg Hall 1.
CSE 451: Operating Systems Winter 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 412 Sieg Hall 1.
Architectural Support for OS
Chapter 2: Operating-System Structures
In Today’s Class.. General Kernel Responsibilities Kernel Organization
Chapter 13: I/O Systems “The two main jobs of a computer are I/O and [CPU] processing. In many cases, the main job is I/O, and the [CPU] processing is.
Presentation transcript:

Feb 2007TinyOS/nesC Basic Concepts1 Introduction to TinyOS Programming Topics  TinyOS execution model  Async and atomic code  Configuration and wiring Appendix  TinyOS Programming Tips (courtesy of Prof. Phil Levis – Stanford Univ.)

TinyOS/nesC Basic Concepts2Feb 2007 TinyOS Process Categories 1.Events 2.Commands 3.Tasks Application Code X EVENTSEVENTS COMMADSCOMMADS App_Y App_Z TASK Events Commands

TinyOS/nesC Basic Concepts3Feb 2007 TinyOS Process Categories 1.Events  Time critical  Interrupts cause Events (Timer, ADC, Sensors)  Short duration 2.Commands  Control services  Sensor actuation  Synchronous/asynchronous calls (split phase with callback event) 3.Tasks  Time flexible (delayed processing)  Run sequentially by TOS Scheduler  Run to completion with respect to other tasks

TinyOS/nesC Basic Concepts4Feb 2007 TinyOS Execution Model

TinyOS/nesC Basic Concepts5Feb 2007 Concurrency Tasks and interrupts (foreground and background operations)  Tasks cannot preempt other tasks  Interrupts can preempt tasks  Interrupts can preempt other interrupts, but not important for this course  TOSH_INTERRUPT() – interrupt allowed  TOSH_SIGNAL() – interrupt forbidden Scheduler  Two level scheduling - interrupts (vector) and tasks (queue)  Queue of tasks is FIFO  No local state associated with tasks  Programmer must manage own internal state when tasks need it  Danger: task queue overflows (because no dynamic memory)  Scheduler puts processor to sleep when no event/command is running and task queue is empty

TinyOS/nesC Basic Concepts6Feb 2007 TinyOS Execution Model (revisited)

TinyOS/nesC Basic Concepts7Feb 2007 TinyOS Theory of Execution: Events & Tasks Consequences of an event  Runs to completion  Preempt tasks and other events What can an event do?  signal events  call commands  post tasks Consequences of a task  No preemption mechanism  Keep code as small execution pieces to not block other task too long  To run a long operations, create a separate task for each operation, rather than using on big task What can initiate (post) tasks?  Within a command  Within an event, or  Within another task

TinyOS/nesC Basic Concepts8Feb 2007 nesC Model Two key concepts 1.Interfaces  uses  provides 2.Components  modules  configurations An application is a particular system of components What makes this good for Mote- based wireless sensors? Component A Component B Component D Component C Application configuration Component E Component F

TinyOS/nesC Basic Concepts9Feb 2007 Each Mote Runs a Single Application Properties  All memory resources are known statically  Rather than a general-purpose OS, applications are built from a subset of system components coupled with application-specific code  The hardware/software boundary varies depending on the application, wireless platform, and I/O devices Challenges  Driven by interaction with the environment (interrupts)  Limited computational and power resources  Soft real-time behavior (radio management and sensor polling)

TinyOS/nesC Basic Concepts10Feb 2007 Applications are Written by Assembling Components CC1000 Radio Packet UART Serial Packet ADC Tempphoto Active Messages clocks byte packet Route map routersensor application application HW SW Consequence: No dynamic memory allocation Reason: No way to know if system will run out of memory unless static memory checking is used.

TinyOS/nesC Basic Concepts11Feb 2007 Applications are Written by Assembling Components Anatomy of an application  Main component for initialization, start, stop  Connected graph of components Anatomy of a component  Interfaces  Implementation  Default handlers

TinyOS/nesC Basic Concepts12Feb 2007 How Mote Apps Start int main() { call hardwareInit();//init hardware pins TOSH_sched_init();//init scheduler call StdControl.init();//init user app call StdControl.start();//start user app while(1) { TOSH_run_task();//infinite spin loop for task }

TinyOS/nesC Basic Concepts13Feb 2007 nesC Component Types Components Configurations Modules  A collection of other components put together by “wiring” syntax.  Does not use C-like code  Can also provide and use interfaces  Provides, uses, and implements interface(s)  Contains C-like code

TinyOS/nesC Basic Concepts14Feb 2007 Why modules and configurations ? The reason for the distinction between modules and configurations is to allow a developer to “snap together” applications using pre-build components without additional programming. For example, a developer could provide a configuration that simply wires together one or more pre-existing modules. The idea is for developers to provide a set of components, a “library,” that can be re-used in a wide range of applications.

TinyOS/nesC Basic Concepts15Feb 2007 nesC Component Structure – General Specification section  Identified by the keyword configuration or module  List of interfaces that component  uses, provides  Alias interfaces as new name Implementation section  Identified by the keyword implementation  Defines internal workings  May include other components and associated “wiring” Generalized component Frame Functions implementation provides uses Interface_A Interface_B NOTE: This model applies to both modules and configurations configuration|module

TinyOS/nesC Basic Concepts16Feb 2007 nesC Interface Defines interaction boundary between modules and configurations Define “public” methods that a component can use  Provides = Exposes functionality to others  Uses = Requires another component An interface a collection of one or more commands (  ) and/or events (  ) Interface_A Interface_B Interface_C Frame Functions Uses configuration|module Commands Event Events Command Provides Event Cmd

TinyOS/nesC Basic Concepts17Feb 2007 nesC Interface usage Interface user must implement events (  ) Interface provider must implement commands (  ) Used for grouping functionality, like  Standard control interface (e.g., init, start, stop )  Split-phase operation (e.g., send, sendDone ) interface Send { command void send(TOS_Msg *m); event void sendDone(); } interface Send MultiHopEngineM RadioCRCPacket Send.nc provides uses

TinyOS/nesC Basic Concepts18Feb 2007 nesC Commands and Events command (  )  Passed parameters pushed onto stack  Non-blocking  Return status  Can call lower level commands event (  )  Callback interface (Server signals client)  Can call commands, post tasks, signal other events  Hardware interrupt triggers are lowest level events  Passed parameters pushed onto stack  Can pre-empt tasks Frame Functions Component Events Commands

TinyOS/nesC Basic Concepts19Feb 2007 Using Commands and Events with Call and Signal call  Executes a command immediately  Flow down  the configuration layers  Issued to lower level – uses library code  Control returns to calling component  Standard C function -- parameters on stack signal  Executes an event immediately  Flows up  the configuration layers  Issued to higher level -- provides callback  Control returns to signaling component  Standard C function – parameters on stack Component1 Component3 Interface Functions signal event1 call command1 Component Interface

TinyOS/nesC Basic Concepts20Feb 2007 nesC Interface Examples module MyAppM { provides interface StdControl; uses interface ADC; uses interface Timer; uses interface Send; } implementation { … } interface StdControl { command void init(); command void start(); command void stop(); } interface ADC { command void getData(); event void dataReady(int data); } interface Timer { command void start(int interval); command void stop(); event void fired(); } interface Send { command void send(TOS_Msg *m); event void sendDone(); }

TinyOS/nesC Basic Concepts21Feb 2007 in /MoteWorks/tos/interfaces/

TinyOS/nesC Basic Concepts22Feb 2007 nesC Module Components Modules implements interfaces A Module has:  Frame (internal state)  Functions (computation)  Interface (events, commands) Frame  Global variables and data  One per component  Statically allocated  Fixed size Functions  Implementation of:  Commands, events, tasks  Commands and Events are simply C function calls Interface_A Interface_B Interface_C Frame Functions Uses Provides Component Commands Events Commands

TinyOS/nesC Basic Concepts23Feb 2007 nesC Module – A Bare Minimum Module An implementation of an interface  Written in C code, no wiring module ModuleName { provides interface StdControl; } implementation { // ========== FRAME ========= // ========== FUNCTIONS ===== command result_t Std.init() { return SUCCESS; } command result_t Std.start() { return SUCCESS; } command result_t Std.stop() { return SUCCESS; } Component Frame Functions provides module implementation uses Interface_A Interface_B

TinyOS/nesC Basic Concepts24Feb 2007 Frame State Modules maintain local and persistent state.  Module states or variables are statically allocated  Each Module has a separate variable name space  Variables declared in Command and Event are local and allocated on the stack module fooM { provides interface I1; uses interface I2; } implementation { uint8_t count=0; command void I1.increment() { count++; } event uint8_t I2.get() { return count; } Call I1.increment(); //first call Call I1.increment(); //second call signal I2.get(); //return 2

TinyOS/nesC Basic Concepts25Feb 2007 nesC Configuration Components Components that are configurations are made by wiring together other components  No code, just wiring GenericConfiugration.nc provides configuration uses Interface_A Interface_B configuration D { provides interface_A; uses interface_B; } implementation { components C1, C2; X = C1.X; X = C2.X; } implementation C1 D C2 GenericConfiguration.nc

TinyOS/nesC Basic Concepts26Feb 2007 nesC Wiring Syntax Binds (connects) the User to an Provider’s implementation User.interface -> Provider.interface Example MyApp_Timer.Timer -> TimerC.Timer[unique("Timer")]; Connected elements must be compatible  Interface to interface, command to command, event to event  Example: you can only wire interface Send to Send, but cannot connect Send to Receive

TinyOS/nesC Basic Concepts27Feb Alias or pass through linkage endpoint 1 = endpoint 2 2.Direct linkage, style 1 endpoint 1 -> endpoint 2 3.Direct linkage, style 2 endpoint 1 <- endpoint 2 which is equivalent to: endpoint 2 -> endpoint 1 Three nesC Wiring Statements configuration C { provides interface X as Xprovider; uses interface X as Xuser; } implementation { Xuser = Xprovider; } C.nc C1 D C2 D1 D2 C

TinyOS/nesC Basic Concepts28Feb 2007 nesC Filename Convention nesC file suffix:.nc C is for configuration ( Clock, ClockC )  “ C ” distinguishes between an interface and the component that provides it M is for module ( Timer, TimerC, TimerM )  “ M ” when a single component has both: a configuration and a module I is for interface?  Very often, interfaces have no suffix.

TinyOS/nesC Basic Concepts29Feb 2007 Concurrency (Review) Tasks and interrupts (foreground and background operations)  Tasks cannot preempt other tasks  Interrupts can preempt tasks  Interrupts can preempt other interrupts, but not important for this course  TOSH_INTERRUPT() – interrupt allowed  TOSH_SIGNAL() – interrupt forbidden Scheduler  Two level scheduling - interrupts (vector) and tasks (queue)  Queue of tasks is FIFO  No local state associated with tasks  Programmer must manage own internal state when tasks need it  Danger: task queue overflows (because no dynamic memory)  Scheduler puts processor to sleep when no event/command is running and task queue is empty

TinyOS/nesC Basic Concepts30Feb 2007 nesC Features for Concurrency post  Puts a function on a task queue  Must be void foo(void) atomic  Interrupts can preempt execution  Turn off interrupts with atomic{ } e.g., to implement a semaphore async  Use async to tell compiler that this code can be called from an interrupt context – used to detect potential race conditions norace  Use norac e to tell compiler it was wrong about a race condition existing (the compiler usually suggests several false positives) task void do-work() { //declares a task that does work} post do-work();

TinyOS/nesC Basic Concepts31Feb 2007 atomic Keyword atomic keyword is used to denote a block of code that runs uninterrupted (interrupts disabled)  Prevents race conditions When should it be used?  Required to update global variables that are referenced in async event handlers  Must use atomic block in all other functions and tasks where variable is referenced nesC compiler with generate warning messages for global variables that need atomic blocks (see example below) SensorAppM.nc:44: warning: non-atomic accesses to shared variable ‘voltage’

TinyOS/nesC Basic Concepts32Feb 2007 Shared States and atomic atomic keyword  Disables all interrupts, therefore use with caution and intention  Affects code block within {…} that follows it  Useful for locking a resource with a semaphore Example Compiles to: cli(); // disable interrupts lda r1, busy// load busy to register jnz r1, inUse// check busy str busy, 1// set busy to true inUse: sbi(); // enable interrupts No interrupts will disrupt code flow atomic { if (!busy) busy = TRUE; }

TinyOS/nesC Basic Concepts33Feb 2007 atomic Syntax Example From MoteWorks/tos/system/TimerM.nc

TinyOS/nesC Basic Concepts34Feb 2007 async versus sync async Code  Code that is reachable from at least one interrupt handler sync Code  Commands, events, or tasks only reachable from tasks Any update to shared state (memory) that is reachable from asynchronous code is a potential data race.  nesC reports an error for any command or event that is asynchronous code and that was not declared with the async keyword.  This ensures that code that was not written to execute safely in an interrupt handler is not called inadvertently. Asynchronous events must use split-phase processing and return quickly to re-enable interrupts as soon as possible  Post a task for delayed processing

TinyOS/nesC Basic Concepts35Feb 2007 async Syntax async event result_t Clock.fire() { post HandleFire(); return SUCCESS; } tinyos-1.x/tos/system/TimerM.nc Post task “decouples” the async event async attribute used to indicate command or event is part of an asynchronous flow async processes are “decoupled” by posting a task  A task is always synchronous

TinyOS/nesC Basic Concepts36Feb 2007 AVR C Libraries Atmel platforms implicitly include AVR C Libraries  MoteWorks 2.0 includes avr-libc Included library files  Register Names, I/O read/write macros  Interrupt handlers  Watchdog timer  Reading from program space  Internal EEPROM Read/Write

TinyOS/nesC Basic Concepts37Feb 2007 QUIZ 1.In what power state does TinyOS spend most of its time? 2.What type of memory management strategy is used by TinyOS? 3.What is the purpose of the TinyOS hardware abstraction layer? 4.What are the two types of software components in TinyOS applications? 5.What is the difference between uses and provides? 6.What is the difference between commands and events?

Feb 2007TinyOS/nesC Basic Concepts38 Q & A: Basic TinyOS and nesC Concepts Topics  TinyOS Architecture, Organization, and Components  nesC Programming Language  TinyOS Execution Model Appendix  TinyOS 2.0 Programming Tips (courtesy of Prof. Phil Levis – Stanford Univ.)

TinyOS/nesC Basic Concepts39Feb 2007 nesC Interface – The StdControl Interface The StdControl interface (in /MoteWorks/tos/interfaces/StdControl.nc) is a very common interface in nesC programming. StdControl defines three commands : init(), start(), and stop().  init() is called when a component is first initialized  start() is called to execute a component  stop() is called when a components is stopped init() can be called multiple times but will never be called after either start() or stop() are called. interface StdControl { command result_t init(); command result_t start(); command result_t stop(); }

TinyOS/nesC Basic Concepts40Feb 2007 nesC Keywords -- Specification KeywordDescription component Building blocks of a nesC application. Can be a module or a configuration module A basic component implemented in nesC configuration A component made from wiring other components interface A collection of event and command definitions implementation Contains code & variables for module or configuration provides Defines interfaces provided by a component uses Defines interfaces used by a module or configuration as Alias an interface to another name command Direct function call exposed by an interface event Callback message exposed by an interface

Feb 2007TinyOS/nesC Basic Concepts41 Appendix: TinyOS Programming Tips  By Phil Levis guide to TinyOS 2.0 Programming Guide  Note: Not all TinyOS 2.0 concepts apply to MoteWorks  Last update: June 28, 2006

TinyOS/nesC Basic Concepts42Feb 2007 TinyOS Programming Hints – Condensed (1 of 4) Programming Hint 1: It’s dangerous to signal events from commands, as you might cause a very long call loop, corrupt memory and crash your program. Programming Hint 2: Keep tasks short. Programming Hint 3: Keep code synchronous when you can. Code should be async only if its timing is very important or if it might be used by something whose timing is important. Programming Hint 4: Keep atomic sections short, and have as few of them as possible. Be careful about calling out to other components from within an atomic section. Programming Hint 5: Only one component should be able to modify a pointer’s data at any time. In the best case, only one component should be storing the pointer at any time.

TinyOS/nesC Basic Concepts43Feb 2007 TinyOS Programming Hints – Condensed (2 of 3) Programming Hint 6: Allocate all state in components. If your application requirements necessitate a dynamic memory pool, encapsulate it in a component and try to limit the set of users. Programming Hint 7: Conserve memory by using enums rather than const variables for integer constants, and don’t declare variables with an enum type. * Programming Hint 8: In the top-level configuration of a software abstraction, auto-wire init to MainC. This removes the burden of wiring init from the programmer, which removes unnecessary work from the boot sequence and removes the possibility of bugs from forgetting to wire. * Programming Hint 9: If a component is a usable abstraction by itself, its name should end with C. If it is intended to be an internal and private part of a larger abstraction, its name should end with P. Never wire to P components from outside your package (directory). *TinyOS 2.0 specific

TinyOS/nesC Basic Concepts44Feb 2007 TinyOS Programming Hints – Condensed (3 of 4) Programming Hint 10: Use the as keyword liberally. Programming Hint 11: Never ignore combine warnings. Programming Hint 12: If a function has an argument which is one of a small number of constants, consider defining it as a few separate functions to prevent bugs. If the functions of an interface all have an argument that’s almost always a constant within a large range, consider using a parameterized interface to save code space. If the functions of an interface all have an argument that’s a constant within a large range but only certain valid values, implement it as a parameterized interface but expose it as individual interfaces, to both minimize code size and prevent bugs. Programming Hint 13: If a component depends on unique, then #define a string to use in a header file, to prevent bugs from string typos. *TinyOS 2.0 specific

TinyOS/nesC Basic Concepts45Feb 2007 TinyOS Programming Hints – Condensed (4 of 4) * Programming Hint 14: Never, ever use the “packed” attribute. * Programming Hint 15: Always use platform independent types when defining message formats. * Programming Hint 16: If you have to perform significant computation on a platform independent type or access it many (hundreds or more) times, then temporarily copying it to a native type can be a good idea. *TinyOS 2.0 specific