Title: TinyOS Programming
1TinyOS Programming
- by Radu Stoleru
- CS 851 Presentation, Fall 2002
2Outline
- Review
- Programming model
- TinyOS 101 the BLINK example
- Environment/Tools
- nesC Programming Language
- Demo
- Issues/Comments
- References
3Review
- TinyOS (TOS) application/binary image,
executable on an ATmega processor - event-driven architecture
- single-shared stack
- no kernel, no process management, no memory
management, no virtual memory - 2-level scheduling
- simple FIFO scheduler, part of the main
4Review
- TOS application graph of components
scheduler
- main
- // component initialization
- while(1)
- while(more_tasks) schedule_task
- sleep
- // while
- // main
5Review
- Scheduling
- 2-level scheduling (events and tasks)
- single shared stack, used by events and function
calls - Tasks
- are preemptable by events
- may call commands
- may signal events
- not preempted by tasks
- Events - lowest level events support by hardware
interrupt - INTERRUPT(_output_compare2_)() // Hardware
Timer Event Handler -
- TOS_SIGNAL_EVENT(CLOCK_FIRE_EVENT)() //
Software event -
6Review
- Hardware Abstraction
- LED (pin numbering/HW wiring)
- CLOCK (counter interrupt)
- UART (baud rate control, transfer)
- ADC (ADC interrupt handling)
- RFM (abstracts bit level timing, RFM specific
control logic)
7Review
- the Sensor stack
- photo, and temperature sensing components
- sits on top of ADC component
- typical request data, wait for data event
8Review
- the Communication stack
- building up from the RFM bit level
- bit level abstracts away radio specifics
- byte level radio component collects individual
bits into bytes - packet level constructs packets from bytes
- messaging layer interprets packets as messages
9Review
- Component interface
- commands accepts (implemented)
- commands uses
- events accepts (implemented)
- events uses
- Component implementation
- functions that implement interface
- frame internal state
- tasks concurrency control
10Programming Model
- Components
- .comp specification
- .C behaviour
- .desc select and wire
- specification
- accepts commands
- uses commands
- signals events
- handles events
comp2 .desc
application .desc
11Programming Model
- 4 kinds of components
- .desc only (ex cnt_to_leds)
- .C and .comp (ex CLOCK)
- .desc and .comp (ex GENERIC_COMM)
- .C, .comp and .desc (ex INT_TO_RFM)
12Programming Model
- (4th type) component interface (.comp)
implementation (.c) wiring (.desc) - ltCompNamegt.comp
- TOS_MODULE ltCompNamegt
- ACCEPTS
- // command_signatures
-
- HANDLES
- // event_signatures
-
- USES
- // command_signatures
-
- SIGNALS
- // event_signatures
-
13Programming Model
- ltCompNamegt.c
- include "tos.h"
- include ltCompNamegt.h"
- define TOS_FRAME_TYPE
- TOS_FRAME_BEGIN(lt CompName gt_frame)
- // state declaration
-
- TOS_FRAME_END(lt CompName gt_frame)
- char TOS_COMMAND(ltcommand_name)()
- // command implementation
-
- char TOS_EVENT(ltevent_namegt)()
- // event implementation
-
14Programming Model
- ltCompNamegt.desc
- // Component Selection
- INCLUDE
- MAIN
- ltCompNamegt
- ltComp_Igt
- ltComp_Jgt
-
-
-
- // Wiring
- ltCompNamegt.ltcommandgt ltComp_Igt.ltcommandgt
-
- ltCompNamegt.lteventgt ltComp_Jgt.lteventgt
-
15TOS 101 the Blink example
- example app that handles the clock events to
update LEDs like a counter
16TOS 101 the Blink example
- blink.desc
- include modules
- MAIN
- BLINK
- CLOCK
- LEDS
-
- BLINKBLINK_INIT
MAINMAIN_SUB_INIT - BLINKBLINK_START
MAINMAIN_SUB_START - BLINKBLINK_LEDy_on
LEDSYELLOW_LED_ON - BLINKBLINK_LEDy_off
LEDSYELLOW_LED_OFF - BLINKBLINK_LEDr_on
LEDSRED_LED_ON - BLINKBLINK_LEDr_off
LEDSRED_LED_OFF - BLINKBLINK_LEDg_on
LEDSGREEN_LED_ON - BLINKBLINK_LEDg_off
LEDSGREEN_LED_OFF - BLINKBLINK_SUB_INIT
CLOCKCLOCK_INIT - BLINKBLINK_CLOCK_EVENT CLOCKCLOCK_FIRE_EVENT
17TOS 101 the Blink example
- blink.comp
- TOS_MODULE BLINK
- ACCEPTS //commands
- char BLINK_INIT(void)
- char BLINK_START(void)
-
- HANDLES // events
- void BLINK_CLOCK_EVENT(void)
-
- USES // commands
- char BLINK_SUB_INIT(char interval, char scale)
- char BLINK_LEDy_on() char
BLINK_LEDy_off() - char BLINK_LEDr_on() char
BLINK_LEDr_off() - char BLINK_LEDg_on() char
BLINK_LEDg_off() -
- SIGNALS //events
-
18TOS 101 the Blink example
- blink.c
- include "tos.h"
- include "BLINK.h"
- //Frame Declaration
- define TOS_FRAME_TYPE BLINK_frame
- TOS_FRAME_BEGIN(BLINK_frame)
- char state
-
- TOS_FRAME_END(BLINK_frame)
- / BLINK_INIT Clear all the LEDs and initialize
state / - char TOS_COMMAND(BLINK_INIT)()
- TOS_CALL_COMMAND(BLINK_LEDr_off)()
TOS_CALL_COMMAND(BLINK_LEDy_off)() - TOS_CALL_COMMAND(BLINK_LEDg_off)()
- VAR(state)0
- TOS_CALL_COMMAND(BLINK_SUB_INIT)(tick1ps)
- return 1
-
19TOS 101 the Blink example
- blink.c (cont.)
- / BLINK_START initialize clock component to
generate periodic events. / - char TOS_COMMAND(BLINK_START)()
- return 1
-
- / Clock Event Handler Toggle the Red LED on
each tick. / - void TOS_EVENT(BLINK_CLOCK_EVENT)()
- char state VAR(state)
- if (state 0)
- VAR(state) 1
- TOS_CALL_COMMAND(BLINK_LEDr_on)()
- else
- VAR(state) 0 TOS_CALL_COMMAND(BLINK_LEDr_off)
() -
-
20Environment/Tools
- OS cygwin/Win2000 or gcc/Linux
- Software atmel tools, java, perl
mote
Code download
mote-PC comms
programming board
21Environment/Tools
22Environment/Tools
- download TOS distribution and Java JDK from
- http//webs.cs.berkeley.edu/tos/dist-0.6.1/setup.e
xe - http//java.sun.com/j2se/1.3/download.html
- directory structure, after installation
- c\avrgcc
- \cygwin
- \nest\apps cnt_to_leds, cnt_to_rfm, sense,
- \docs connector.pdf, tossim.pdf,
- \tools toscheck, inject, verify,
- \tos shared/system components,
23Environment/Tools
- verify the software installation
- c\nest\tools\toscheck.exe
- verify the hardware is working
- c\nest\apps\mica_hardware_verify\make mica
- install the mote into the board. Red LED on.
- c\nest\apps\mica_hardware_verify\make mica
install.1 - c\nest\apps\mica_hardware_verify\java
hardware_check COM1
24Environment/Tools
- build your application
- code your components (.comp .c files)
- describe your aplication (.desc file)
- change/run makefile (mote execution)
- - set GROUP_ID
- - make clean make mica make mica install.ltgt
- change/run makefilePC (PC simulation)
- - set GROUP_ID
- - make clean make f makefilePC
25Environment/Tools
- simulate your application (on PC)
- c\nest\apps\blink\binpc\main opts lt nodesgt
- inject packets into the network
- c\nest\tools\net\tinyos\tossim\java
NetworkInjector - visualize network traffic
- c\nest\tools\net\tinyos\tossim\java TossimGUI
- use static routes
- edit c\nest\apps\router\router\cells.txt
- c\nest\tools\tos\pc\binpc\main r static lt
nodesgt
26Environment/Tools
- SerialForwarder multiple clients communicate
with a base station mote - c\nest\tools\net\tools\SerialForwarder\java
SerialForward -no-gui -port 9001 source serial
-comm COM2 -packetsize 15
27nesC Programming Language
- Goals
- pragmatic low-level language for programming
motes - intermediate language for future high-level
languages
28nesC Programming Language
- Components
- implementation
- - module C behaviour
- configurationselect and wire
- interfaces
- provides interface
- requires interface
comp3
comp1 module
comp4
comp2 configuration
application configuration
29nesC Programming Language
- 2 kinds of components
- configuration was .desc and .comp
- module was .C and .comp
30nesC Programming Language
- Connect configurations
- configuration app
- implementation
- uses c1, c2, c3
- c1 -gt c2 // implicit interface sel.
- c2.out -gt c3.triangle
- c3 lt- c2.side
-
- Partial configurations
- component c2c3
- provides interface triangle t1
-
- implementation
- uses c2, c3
- t1 -gt c2.in
- c2.out -gt c3.triangle
- c3 lt- c2.side
31nesC Programming Language
- modules
- module C1
- requires interface triangle
- implementation ...
- module C2
- provides interface triangle in
- requires
- interface triangle out
- interface rectangle side
- implementation ...
- module C3
- provides interface triangle
- provides interface rectangle
- implementation ...
32nesC Blink example
- blink.td (configuration)
- configuration Blink
-
- implementation
- uses Main, BlinkM, Clock, Leds
- Main.SimpleInit -gt BlinkM.SimpleInit
- BlinkM.Clock -gt Clock
- BlinkM.Leds -gt Leds
-
33nesC Blink example
- blinkM.td (module)
- module BlinkM
- provides
- interface SimpleInit
-
- requires
- interface Clock
- interface Leds
-
-
- implementation
- bool state
- command result_t SimpleInit.init()
- stateFALSE return SUCCESS
-
- ...
34nesC Blink example
- blinkM.td (module)
- ...
- command result_t SimpleInit.start()
- return call Clock.setRate(128, 6)
-
- event result_t Clock.fire()
- state !state
- if(state) call Leds.redOn()
- else call Leds.redOff()
-
35nesC Programming Language
- nesc1 takes a component and
- checks nesC errors
- checks (most) C errors
- generates one C-file for the application
- avr-gcc compiles nesc1 output
- generated code has line directives, so clean
error messages
36nesC Programming Language
37nesC Programming Language
- nesC much nicer to use than TinyOS
- no more obscure C compiler warnings/errors
- no more obscure bugs due to typos
- produces smaller code than TinyOS
- get rid of the macros for frames, commands,
events - eliminate wiring error through type checking
- simplify wiring by using interfaces
- increase modularity by separation of interfaces
and modules
38Demo
- one mote running
- cnt_to_led
- two motes running
- cnt_to_rfm_and_led
- rfm_to_led
39Issues/Comments
- Programming perspective (TOS)
- no memory protection -gt easy to corrupt/crash the
system - not much compile time error type checking.
- heavy use of macros
- a lot of shaky perl code
- not much code optimization
- no support for common idioms FSM-like programs,
atomicity
40Issues/Comments
- System perspective
- simplistic FIFO scheduling -gt no real-time
guarantees - bounded number of pending tasks
- no process management -gt resource allocation
problematic, e.g. shared resources - software level bit manipulation. HW
implementation can provide speed-up and power
saving
41References
- http//today.cs.berkeley.edu/tos/
- http//webs.cs.berkeley.edu/retreat-6-02
- C. Lu Berkeley Motes and TinyOS, CS851
Presentation, 2001 - T. He Get Started with TinyOS, CS851
Presentation, 2001