1 / 44

First Steps in TinyOS and nesC Programming

First Steps in TinyOS and nesC Programming. Topics Timer Application: MyApp Application directory contents Do and Dissect Configuration Wiring Module StdControl Timer Leds. First Steps in TinyOS Programming. Objectives What files needed in an application directory

zuwena
Download Presentation

First Steps in TinyOS and nesC Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. First Steps in TinyOS and nesC Programming Topics Timer Application: MyApp Application directory contents Do and Dissect Configuration Wiring Module StdControl Timer Leds WSN Training: First Steps in nesC Programming

  2. First Steps in TinyOS Programming • Objectives • What files needed in an application directory • How to use the Timer and LED components • How to compile and download an application to a Mote • Application: MyApp in /MoteWorks/apps/tutorials/lesson_1/ WSN Training: First Steps in nesC Programming

  3. About MyApp • This timer application uses one of the timers on the ATmega128L Mote. • The timer will be set to fire continuously every second and the Mote red LED will toggle on and off to show this visually. • So why go through the trouble of this program? • This is TinyOS’ version of “Hello World” • To help the learn unfamiliar TinyOS and nesC programming methods WSN Training: First Steps in nesC Programming

  4. The MoteWorks/apps/tutorials/lesson_1/MyApp • MyApp.ncconfiguration MyApp_Timer Specifying the INTERFACE /** * This configuration shows how to use the Timer and LED components **/ configuration MyApp { } implementation { components Main, MyAppM, TimerC, LedsC; Main.StdControl -> TimerC.StdControl; Main.StdControl -> MyAppM.StdControl; MyAppM.Timer -> TimerC.Timer[unique("Timer")]; MyAppM.Leds -> LedsC.Leds; } Main StdControl StdControl MyApp Timer Leds Timer Leds StdControl Specifying the IMPLEMENTATION TimerC LedsC WSN Training: First Steps in nesC Programming

  5. Lab Outline • Enter/review in all necessary code and auxiliary files • Build (compile) and download the application • Take a closer look at the code and auxiliary files • You should already have these • One MICA Mote: standard editions of MICA2 (MPR4x0) or MICAz (MPR2400) or OEM editions of MICA2 or MICAz • One gateway / programming board: MIB510, MIB520, or MIB600 and the associated hardware (cables, power supply) for each • A Windows PC with MoteWorks installed WSN Training: First Steps in nesC Programming

  6. MyApp Application Review • The application folder (directory) is where all your top-level application code and associated files will be stored. • Navigate to the directory /MoteWorks/apps/tutorials/ lesson_1 WSN Training: First Steps in nesC Programming

  7. Seven Common Files in an Application Directory • Makefile (section 5.2.1) • Makefile.component (section 5.2.2) • Top-level application configuration written in nesC • Top-level application module written in nesC • sensorboardsApp.h file (not seen here, but typical for applications using sensors and GPIO) • appFeatures.h (optional) • README (optional, but highly recommended) WSN Training: First Steps in nesC Programming

  8. MyApp Application: Makefile include Makefile.component include $(TOSROOT)/apps/MakeXbowlocal include $(MAKERULES) WSN Training: First Steps in nesC Programming

  9. MyApp – Makefile.component • This file describes the top level application component, MyApp, and the name of the sensorboard we are going to use. • The sensorboard reference tells the compiler we want to use the nesC components for accessing the sensor devices on that board. • The collection of those components is also known as a “driver” COMPONENT=MyApp SENSORBOARD=mts310 WSN Training: First Steps in nesC Programming

  10. First Steps in TinyOS and nesC Programming Topics Application: MyApp Application directory contents Do and Dissect Configuration Wiring Module StdControl interface Timer interface WSN Training: First Steps in nesC Programming

  11. MyApp – Top Level Configuration • What might we have in • The comments? • Application name? • The list of components? • Wiring? /** * <Some comments about the top-level application> **/ configuration <Application_Name> { } implementation { components <comma separated listed of components>; <User.Interface -> Provider.Interface>; } WSN Training: First Steps in nesC Programming

  12. MyApp – Top Level Configuration • What is Main? /** * This configuration shows how to use the Timer and LED components **/ configuration MyApp { } implementation { components Main, MyAppM, TimerC, LedsC; Main.StdControl -> TimerC.StdControl; Main.StdControl -> MyAppM.StdControl; MyAppM.Timer -> TimerC.Timer[unique("Timer")]; MyAppM.Leds -> LedsC.Leds; } WSN Training: First Steps in nesC Programming

  13. Main – the scheduler for TinyOS • A TinyOS application = configuration (wiring) of new and existing components + the component Main. • Main, the scheduler, runs the tasks created by those components. • A TinyOS top-level application must have the Main component in its configuration. WSN Training: First Steps in nesC Programming

  14. Main cont’d configuration Main { uses interface StdControl; } implementation { components RealMain, PotC, HPLInit; StdControl = RealMain.StdControl; RealMain.hardwareInit -> HPLInit; RealMain.Pot -> PotC; } • Main is a component that is executed first in a TinyOS application. • The command Main.StdControl.init()is the first command executed in TinyOS followed by Main.StdControl.start(). • StdControl is a common interface used to initialize, start, and stop TinyOS components. WSN Training: First Steps in nesC Programming

  15. MyApp – Top Level Configuration • Why does the component TimerC provide a unique Timer? /** * This configuration shows how to use the Timer and LED components **/ configuration MyApp { } implementation { components Main, MyAppM, TimerC, LedsC; Main.StdControl -> TimerC.StdControl; Main.StdControl -> MyAppM.StdControl; MyAppM.Timer -> TimerC.Timer[unique("Timer")]; MyAppM.Leds -> LedsC.Leds; } WSN Training: First Steps in nesC Programming

  16. Parameterized Interfaces (1 of 2) •  There may be multiple users of a component • One Timer component but many Users with different event time requirements • When a Timer “fires” which component should be signaled? • How does TinyOS handle this ? • Multiple instantiations of a Component’s Interface • Parameter specifies the specific instance WSN Training: First Steps in nesC Programming

  17. Parameterized Interfaces (2 of 2) • A parameterized interface allows a component to provide multiple instancesofan interface • Parameterization (or indexing) is set by a compile-time value • provides interface Timer[uint8_t id]; • Total number of instances permitted may be limited by implementation WSN Training: First Steps in nesC Programming

  18. nesC Concepts – Unique Instances • To make sure your instance parameter is not used by some one else, use unique(“astring”) • Generates an 8-bit identifier from the string given as an argument. • Multiple components using timer[unique(“Timer”)] are each guaranteed to get a signal associated with their specific timer settings. • All components must use the same “astring” • In our app Timer is used for the astring WSN Training: First Steps in nesC Programming

  19. First Steps in TinyOS and nesC Programming Topics Application Example: Do and Dissect the MyApp_Timer application Configuration Wiring Module StdControl interface Timer interface WSN Training: First Steps in nesC Programming

  20. MyApp – Top Level Module • The application’s module is located in the MoteWorks/apps/tutorial/lesson_1/MyAppM.nc file. • The function of this code is to start a timer and toggle the red LED on the Mote. WSN Training: First Steps in nesC Programming

  21. /** * This module shows how to use the Timer and LED components **/ module MyAppM { provides { interface StdControl; } uses { interface Timer; interface Leds; } } The MyAppM module provides theinterface StdControl.  To provide an interface means that MyAppM must implement the that interface.  As explained above, this is necessary to get the MyApp component initialized and started. MyApp – Top Level Module 1 of 3 The first part of the code states that this is a module called MyAppM and declares the interfaces which are prefaced by the keywords provides and uses.  WSN Training: First Steps in nesC Programming

  22. /** * This module shows how to use the Timer and LED components **/ module MyAppM { provides { interface StdControl; } uses { interface Timer; interface Leds; } } In many nesC applications, it is common to call a function periodically. The realization of that function is done by means of a timer. We also want to active one of the LEDs so we use an interface to the LED MyApp – Top Level Module 1 of 3 The name for the interface for a timer is, conveniently enough Timer. The name for the interface for an LED is Leds. WSN Training: First Steps in nesC Programming

  23. nesC Interface – The StdControl Interface (review) • The StdControl interface (like the component Main) must always be implemented at the top-level application. • The StdControl interface provides the basic functionality for the TinyOS application to be initialized, started and stopped. • StdControl is like a power switch to turn on and off components • Also does boot time initialization • StdControl should not be used for continuous processing • Use a timer instead WSN Training: First Steps in nesC Programming

  24. In /MoteWorks/tos/interfaces/StdControl.nc 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. StdControl Interface Details interface StdControl {  command result_t init();  command result_t start();  command result_t stop();} WSN Training: First Steps in nesC Programming

  25. interface Timer { command result_t start(char type, uint32_t interval); command result_t stop(); event result_t fired(); Here we see that Timer interface defines two commands start() and stop()commands and one event fired() event What is “result_t”? It is the data type for the status value returned by a command or event. This status value is either SUCCESS or FAIL. Mote About the Timer Interface WSN Training: First Steps in nesC Programming

  26. interface Timer { command result_t start(char type, uint32_t interval); command result_t stop(); event result_t fired(); The start() command is used to specify the type of the timer and the interval at which the timer will expire. The unit of the interval argument is millisecond. The valid types of timers TIMER_ONE_SHOT Ends after the specified interval TIMER_REPEAT Goes on and on until it is stopped by the stop() command. Mote About the Timer Interface (cont’d) WSN Training: First Steps in nesC Programming

  27. interface Timer { command result_t start(char type, uint32_t interval); command result_t stop(); event result_t fired(); How does an application know that its timer has expired? When it receives an event. What is an event? A signal from the implementation of an interface that something has occurred. In this case, the fired() event is signaled when the time interval has passed. This is an example of a bi-directional interface An interface can both provide commands that can be called by users of the interface, and signal events of call handlers implemented by the user. Remember: A module that uses an interface must implement the events that this interface uses. MyApp – Timer interface (cont’d) WSN Training: First Steps in nesC Programming

  28. MyApp – Top Level Module 1 of 6 • implementation { • /** • * Initialize the components. • * • * @return Always returns <code>SUCCESS</code> • **/ • command result_t StdControl.init() { • call Leds.init(); • return SUCCESS; • } • /** • * Start things up. This just sets the rate for the clock • * component. • * • * @return Always returns <code>SUCCESS</code> • **/ • command result_t StdControl.start() { • // Start a repeating timer that fires every 1000ms • return call Timer.start(TIMER_REPEAT, 1000); • } The MyAppM module implements the StdControl.init(), StdControl.start(), and StdControl.stop() commands, since it provides the StdControl interface. WSN Training: First Steps in nesC Programming

  29. MyApp – Top Level Module 2 of 6 • implementation { • /** • * Initialize the components. • * • * @return Always returns <code>SUCCESS</code> • **/ • command result_t StdControl.init() { • call Leds.init(); • return SUCCESS; • } • /** • * Start things up. This just sets the rate for the clock • * component. • * • * @return Always returns <code>SUCCESS</code> • **/ • command result_t StdControl.start() { • // Start a repeating timer that fires every 1000ms • return call Timer.start(TIMER_REPEAT, 1000); • } The init() command in the implemented StdControl interface simply initializes the Leds subcomponent with the call to Leds.init(). WSN Training: First Steps in nesC Programming

  30. MyApp – Top Level Module 3 of 6 • implementation { • /** • * Initialize the components. • * • * @return Always returns <code>SUCCESS</code> • **/ • command result_t StdControl.init() { • call Leds.init(); • return SUCCESS; • } • /** • * Start things up. This just sets the rate for the clock • * component. • * • * @return Always returns <code>SUCCESS</code> • **/ • command result_t StdControl.start() { • // Start a repeating timer that fires every 1000ms • return call Timer.start(TIMER_REPEAT, 1000); • } The start() command invokes Timer.start() to create a repeat timer (“TIMER_REPEAT”) that expires every 1000 msec. WSN Training: First Steps in nesC Programming

  31. MyApp – Top Level Module 4 of 6 • /** • * Halt execution of the application. • * This just disables the clock component. • * • * @return Always returns <code>SUCCESS</code> • **/ • command result_t StdControl.stop() { • return call Timer.stop(); • } • /** • * Toggle the red LED in response to the <code>Timer.fired</code> • * event. • * • * @return Always returns <code>SUCCESS</code> • **/ • event result_t Timer.fired() • { • call Leds.redToggle(); • return SUCCESS; • } • } stop() terminates the timer. WSN Training: First Steps in nesC Programming

  32. MyApp – Top Level Module 5 of 6 • /** • * Halt execution of the application. • * This just disables the clock component. • * • * @return Always returns <code>SUCCESS</code> • **/ • command result_t StdControl.stop() { • return call Timer.stop(); • } • /** • * Toggle the red LED in response to the <code>Timer.fired</code> • * event. • * • * @return Always returns <code>SUCCESS</code> • **/ • event result_t Timer.fired() • { • call Leds.redToggle(); • return SUCCESS; • } • } The Timer.fired() event is implemented. This is necessary since MyAppM must implement any event from an interface it uses.. WSN Training: First Steps in nesC Programming

  33. MyApp_Timer – Top Level Module 6 of 6 • /** • * Halt execution of the application. • * This just disables the clock component. • * • * @return Always returns <code>SUCCESS</code> • **/ • command result_t StdControl.stop() { • return call Timer.stop(); • } • /** • * Toggle the red LED in response to the <code>Timer.fired</code> • * event. • * • * @return Always returns <code>SUCCESS</code> • **/ • event result_t Timer.fired() • { • call Leds.redToggle(); • return SUCCESS; • } • } Each time Timer.fired() event is triggered, the Leds.redToggle() toggles the red LED. WSN Training: First Steps in nesC Programming

  34. MyAppM.nc – Key Lessons • The heart of most TinyOS applications is Timer.fired() • Compare to C where main() contains a while loop • A while loop in StdControl.init() would freeze the system and block all tasks • The proper way to centralize processing in TinyOS is to start a repeat timer (with REPEAT_TIMER) and implement logic in Timer.fired(). WSN Training: First Steps in nesC Programming

  35. First Steps in TinyOS and nesC Programming Topics Application: MyApp Application directory contents Do and Dissect Configuration Wiring Module StdControl Timer Programming with Programmer’s Notepad WSN Training: First Steps in nesC Programming

  36. MyApp_Timer – Compiling and Flashing the Mote • Now that you have reviewed all the application files, we can proceed with the compilation and flashing of the Mote • You need to be in the .nc of the app file you want to compile and program before you can execute shell commands from Programmer’s Notepad. • Attach one Mote to a Programmer/Gateway Board • One of MIB510, MIB520, MIB600 • One of MICAz, MICA2 • Compile your application: • Select Tools > make mica2 (or make micaz ormake mica2dot) • The “Output” section of the Programmers Notepad will print the compiling results to the screen: WSN Training: First Steps in nesC Programming

  37. WSN Training: First Steps in nesC Programming

  38. MyApp – Compiling and Flashing the Mote • Flash your Mote: • Select Tools > shell • In the command line type • make <platform>install,<n> mica2, or micaz, or mica2dot WSN Training: First Steps in nesC Programming

  39. Lab 1 • Change the application so that the yellow or green LED blinks • Challenge exercise: Setup two timers so that the yellow or green LED flashes at an some multiple of the red LED • See “Challenge_Apps” directory in WSN training CD for one solution Challenge exercises do not have “answers in the back of the book.” WSN Training: First Steps in nesC Programming

  40. Lab: Change Line 86 of BlinkM.nc Open Programmers Notepad window and navigate to the Blink directory: /MoteWorks/apps/general/Blink • Change from call Leds.redToggle(); to call Leds.yellowToggle(); WSN Training: First Steps in nesC Programming

  41. Lab: Step 2 -- Compile Compile an application in Programmer’s Notepad • Method 1: Click on Tools > Shell Type make <platform>install and enter into the User Input Parameter dialog box • Method 2: See next slide • Exchange <platform> with one of the following • mica2 • micaz • mica2dot WSN Training: First Steps in nesC Programming

  42. 2. View compiler output Lab: Step 2 — Compiling 1. Type in make <platform> command then hit “Enter” WSN Training: First Steps in nesC Programming

  43. Q & A: First Steps in nesC Programming Timer Application: MyApp Application directory contents Do and Dissect Configuration Wiring Module StdControl Timer Leds Programming with Programmer’s Notepad WSN Training: First Steps in nesC Programming

  44. Application Component Module Configuration Interface Makefile Makefile.component nesC TinyOS Provides Uses implementation Command Event Call Return – SUCCESS, FAIL Make StdControl – init(), start(), stop() TimerM Leds interface LedsC Timer interface -- TIMER_REPEAT, TIMER_ONE_SHOT docs Vocabulary WSN Training: First Steps in nesC Programming

More Related