Download presentation
Presentation is loading. Please wait.
Published byLinda Gaines Modified over 9 years ago
1
1 Lab 3 Objectives Case study: “Hello world” program on motes Write you first program on mote
2
2 “Hello world” program on motes MyApp in /MoteWorks/apps/tutorials/lesson_1/ Application directory contents C onfiguration: wiring modules StdControl Timer Leds Programming with Programmer’s Notepad
3
3 About MyApp Timer application timer will be set to fire continuously every second and the Mote red LED will toggle on and off to show this visually. Why go through the trouble of this program? This is TinyOS’ version of “Hello World” To help you learn TinyOS and nesC programming methods
4
4 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
5
5 Seven Common Files in an Application Directory 1.Makefile 2.Makefile.component 3.Top-level application configuration written in nesC 4.Top-level application module written in nesC 5.sensorboardsApp.h file (not seen here, but typical for applications using sensors and GPIO) 6.appFeatures.h (optional) 7.README (optional, but highly recommended)
6
6 MyApp Application: Makefile include Makefile.component include $(TOSROOT)/apps/MakeXbowlocal include $(MAKERULES)
7
7 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
8
8 “Hello world” program on motes MyApp in /MoteWorks/apps/tutorials/lesson_1/ Application directory contents C onfiguration: wiring modules StdControl Timer Leds Programming with Programmer’s Notepad
9
9 Top Level Configuration We might have comments Application name List of components Wiring /** * **/ configuration { } implementation { components ; Provider.Interface>; }
10
10 /** * 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; } MyApp – Top Level Configuration MyApp.nc Specifying the INTERFACE Specifying the IMPLEMENTATION TimerCLedsC Main MyApp_Timer MyApp StdControl TimerLeds Timer StdControl
11
11 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; }
12
12 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.
13
13 Main cont’d 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. configuration Main { uses interface StdControl; } implementation { components RealMain, PotC, HPLInit; StdControl = RealMain.StdControl; RealMain.hardwareInit -> HPLInit; RealMain.Pot -> PotC; }
14
14 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; }
15
15 Parameterized Interfaces 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? TinyOS handle this using parameterized interfaces Parameterization set by a compile-time value provides interface Timer[uint8_t id]; Total number of instances permitted may be limited by implementation
16
16 nesC Concepts – Unique Instances To make sure your instance parameter is not used by someone 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 as the astring
17
17 “Hello world” program on motes MyApp in /MoteWorks/apps/tutorials/lesson_1/ Application directory contents C onfiguration: wiring modules StdControl Timer Leds Programming with Programmer’s Notepad
18
18 MyApp – Top Level Module Located in MoteWorks/apps/tutorial/lesson_1/MyAppM.nc The function of this code is to start a timer and toggle the red LED on the Mote.
19
19 MyApp – Top Level Module /** * This module shows how to use the Timer and LED components **/ module MyAppM { provides { interface StdControl; } uses { interface Timer; interface Leds; } MyAppM provides the interface StdControl To provide an interface means that MyAppM must implement that interface As explained earlier, this is necessary to get the MyApp component initialized and started 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. 1 of 3
20
20 MyApp – Top Level Module /** * This module shows how to use the Timer and LED components **/ module MyAppM { provides { interface StdControl; } uses { interface Timer; interface Leds; } Many nesC applications need to call a function periodically Done by means of a timer We also want to use one of the LEDs, so we use an interface to the LED The name for the interface for a timer is Timer. The name for the interface for an LED is Leds. 1 of 3
21
21 The StdControl Interface StdControl interface (like the component Main ) must always be implemented at the top-level application 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
22
22 StdControl Interface Details 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() is called. interface StdControl { command result_t init(); command result_t start(); command result_t stop(); }
23
23 MyApp Timer Interface interface Timer { command result_t start(char type, uint32_t interval); command result_t stop(); event result_t fired(); Timer interface defines two commands and one event Commands: start() and stop() event: fired() What is “ result_t ”? data type for the status value returned by a command or event either SUCCESS or FAIL.
24
24 MyApp Timer Interface (cont’d) interface Timer { command result_t start(char type, uint32_t interval); command result_t stop(); event result_t fired(); start() command specify the type of the timer TIMER_ONE_SHOT –Ends after the specified interval TIMER_REPEAT –Goes on and on until stopped by the stop() command Specify the interval at which the timer will expire unit of the interval argument: millisecond
25
25 MyApp Timer interface (cont’d) 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 This is an example of a bi-directional interface 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.
26
26 MyApp – Top Level Module implementation { /** * Initialize the components. * * @return Always returns SUCCESS **/ 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 SUCCESS **/ 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. 1 of 6 The init() command in the implemented StdControl interface simply initializes the Leds subcomponent (by calling Leds.init()). The start() command invokes Timer.start() to create a repeat timer (“ TIMER_REPEAT ”) that expires every 1000 msec.
27
27 MyApp – Top Level Module /** * Halt execution of the application. * This just disables the clock component. * * @return Always returns SUCCESS **/ command result_t StdControl.stop() { return call Timer.stop(); } /** * Toggle the red LED in response to the Timer.fired * event. * * @return Always returns SUCCESS **/ event result_t Timer.fired() { call Leds.redToggle(); return SUCCESS; } stop() terminates the timer. 4 of 6 The Timer.fired() event is implemented. This is necessary since MyAppM must implement any event from an interface that it uses.. Each time Timer.fired() event is triggered, the Leds.redToggle() toggles the red LED.
28
28 MyAppM.nc – Key Lessons The heart of most TinyOS applications is Timer.fired() The proper way to centralize processing in TinyOS is to start a repeat timer (with REPEAT_TIMER ) and implement logic in Timer.fired().
29
29 “Hello world” program on motes MyApp in /MoteWorks/apps/tutorials/lesson_1/ Application directory contents C onfiguration: wiring modules StdControl Timer Leds Programming with Programmer’s Notepad
30
30 Compiling and Flashing the Mote 1.Now that you have reviewed all the application files, you 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. 2.Attach one Mote to a Programmer/Gateway Board One of MIB510, MIB520, MIB600 One of MICAz, MICA2 3.Compile your application: Select Tools > make mica2 (or make micaz or make mica2dot) The “Output” section of the Programmers Notepad will print the compiling results.
31
31
32
32 MyApp – Compiling and Flashing the Mote 4.Flash your Mote: Select Tools > shell In the command line type make install, mica2, or micaz, or mica2dot
33
33 Vocabulary 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.