Documente Academic
Documente Profesional
Documente Cultură
Topics:
TinyOS Structures
Commands, Events, & Tasks
Configurations, Components, & Modules
Implementation
Application Example
How the pieces fit together
TOS Advanced Flows
TOS Messaging
TinyOS/nesC Programming (Parts 1 and 2)
TinyOS Layers
Application
Configuration
Legend
User
Application Interfaces
TOS
Application Specific
Components
Library
Components
Commands
HW
Events
System Components
Hardware Presentation/Abstraction Layer
Mote Devices
TinyOS/nesC Programming (Parts 1 and 2)
Sensor Devices
2
Mote CPU
Processor
4 to 8 MIPs
8-bit RISC
Memory
4 KB SRAM
-- Variables and data
128 KB Flash -- Code space
Peripherals
Storage
TOS
Component
Description
ADCM.nc
AMStandard.nc
CRCPacket.nc
ClockC.nc
Timing component
GenericComm.nc
FramerM.nc
I2CPacketC.nc
LedsC.nc
LED interface
LoggerM.nc
TimerM.nc
UART.nc
VoltageM.nc
TinyOS Structure
Configuration
Component
CntToLedsAndRfm Example
Study an Application to see Components, Configurations,
Commands, Events, and Tasks in action
LAB: Auto-generate Documentation
cd tinyos-1.x/contrib/xbow/CntToLedsAndRfm
make mica2 docs
Open with Document file Explorer
C:\tinyos\cygwin\opt\tinyos-1.x\
doc\nesdoc\mica2\apps.cnttoledsandrfm.CntToLedsAndRfm.n
c.app.html
CntToLedsandRfm Wiring
TinyOS Configuration
Connects Components together
Every TOS Application has a single top-level
CONFIGURATION file
A configuration wire connects Components
together through Interfaces
Configuration Template
Tinyos-1.x/tos/lib/IntToRfm.nc
includes IntMsg;
Interface to Components
configuration IntToRfm
Implementation
{ provides {
interface IntOutput;
interface StdControl;
}}
implementation
{ components IntToRfmM, GenericComm as Comm;
IntOutput = IntToRfmM;
as alias
StdControl = IntToRfmM;
IntToRfmM.Send -> Comm.SendMsg[AM_INTMSG];
IntToRfmM.SubControl -> Comm;
}
Wiring to other components
TOS
Interface
Description
ADC.nc
I2C.nc
Leds.nc
LogData.nc
Radio.nc
Receive.nc
Send.nc
StdControl.nc
Timer.nc
10
Configuration Syntax
Binds the User to the Providers component via an
Interface:
or
11
Component
Specification
provides
Interface_A
uses
Interface_B
Implementation
Frame
Implementation
Functions
12
Interface Syntax
Tinyos-1.x/tos/interfaces/timer.nc
includes Timer; // make TIMER_x constants available
interface Timer {
/**
* Start the timer.
* @param type The type of timer to start.
*/
command result_t start(char type, uint32_t interval);
command result_t stop();
event result_t fired();
}
13
Implementation
Defines how the component works.
Events
Multiple interfaces
Interface_A
Component Implementation
Functions
Interface_B
Frame
Interface_C
Commands
TinyOS/nesC Programming (Parts 1 and 2)
14
Events
Implementation Requirements
All Commands & Events declared as provided in
the INTERFACE section must be implemented.
Two Sections
15
Module Syntax
Tinyos-1.x/tos/lib/counter.nc
module Counter {
provides {
interface StdControl; }
uses {
interface Timer;
interface IntOutput; }}
implementation {
Frame
Frame memory
memory variable
variable
int state;
command result_t StdControl.start() {
return call Timer.start(TIMER_REPEAT, 250); }
event result_t IntOutput.outputComplete(result_t success){
if(success == 0) state --;
return SUCCESS; }
}//implementation
16
Interfaces
myComponent.nc
Implementation
Modules - myComponentM.nc
Configurations myComponentC.nc
17
Time Critical
Interrupts cause Events (Timer, ADC, Sensors)
Small / short duration
Suspend Tasks
Tasks
Time Flexible
Run sequentially by TOS Scheduler
Run to completion wrt other Tasks
Interruptible
18
Preempt
POST
events
main {
while(1) {
while(more_tasks)
schedule_task;
sleep;
}
}
Tasks
FIFO
commands
commands
Interrupts
Time
Hardware
19
Tiny OS Kernel
INTERRUPT
TASK#1
TASK#2
TASK#3
IF
TASK QUE
EMPTY
then
SLEEP
INTERRUPT
VECTORs
ADC
ISR
TIMER1
EVENT
Handler
UART0
SPI
if Que !Empty:
execute TASK
TASK#n
INT0
Call Command(B)
Return
Event Return
Task Return
No TASKS pending
Command(s)
TASK(m)
Call Command(A)
Return
Command(s)
20
INTERRUPT
SLEEP
(Interrupts Enabled)
Event
Command
Functions
signal event1
call command1
Component2
Interface
21
Interface
Component3
Commands
Implemented under the keyword command
Invoked/called by call syntax
call InterfaceName.CommandName(params)
Tinyos-1.x/tos/lib/counter.nc
command result_t StdControl.start() {
return call Timer.start(TIMER_REPEAT, 250);
}
22
Events
Initiated by a signal.
Handled with an Event Handler
Tinyos-1.x/tos/lib/counter.nc
event result_t Send.sendDone(TOS_MsgPtr msg,result_t success) {
if (pending && msg == &data){
pending = FALSE;
signal IntOutput.outputComplete(success);}
return SUCCESS;
}
23
Tasks
Declared before they are invoked
No parameters (use Frame variables)
Tinyos-1.x/tos/system/TimerM.nc
task void HandleFire() {
if (mState) {
for (i=0;i<NUM_TIMERS;i++) {
post signalOneTimer();
}//HandleFire
async event result_t Clock.fire() {
post HandleFire();
return SUCCESS;
}
24
TinyOS Tasks
Time flexible
Run sequentially by TOS Scheduler
25
TinyOS Scheduler
Scheduler is a simple first in, first out scheme
TinyOS STATE
TinyOS Task First-in First-Out Queue & Scheduler
Sleep
Sleep
TASK3
TASK1
TASK2
TASK3
POST T3
POST T2
TASK1
EVENT 2
POST Task1
TASK 1
TASK2
T3
TASK2
TASK3
T3
COMP 2
E3
EVENT 1
COMP 1
TOSH_SIGNAL ISR
INTERRUPT
Time
TinyOS/nesC Programming (Parts 1 and 2)
26
Atomic block
atomic {
//code is atomic can not be interrupted
ApplicationState = NewState;
27
atomic Syntax
Tinyos-1.x/tos/system/TimerM.nc
command result_t Timer.stop[uint8_t id]() {
if (id>=NUM_TIMERS) return FAIL;
if (mState&(0x1L<<id)) { // if the timer is running
atomic mState &= ~(0x1L<<id);
if (!mState) {
setIntervalFlag = 1;
}
return SUCCESS;
}
return FAIL; //timer not running
}
28
sync Code
29
async Syntax
async attribute used to indicate command or event
tinyos-1.x/tos/system/TimerM.nc
async event result_t Clock.fire() {
post HandleFire();
return SUCCESS;
}
30
The
The post
post task
task decouples
decouples
the
the async
async event
event
TOS Rules
Hardware Event handler Commands and Events
must be declared with the async keyword
31
Component1
Component2
Interface
Implementation
Interface
Functions
signal event1
Modules
Provides, uses
call command1
Interface
Component3
32
TOS Messaging
Groups, Addresses
Active Messaging
Wireless Communication
33
Component1
commandA {
call Comp2.goCmdX;
//continue
return SUCCESS}
Event handler
event cmdXDone{
//process result
return SUCCESS}
Commands initiate an
operation
Continue/idle
Event indicates
completion
Component2
goCmdX{
post task1();
return SUCCESS}
Task1{
//do stuff
signal cmdXDone();
return SUCCESS}
TOS Scheduler
TinyOS/nesC Programming (Parts 1 and 2)
34
Slow devices
35
Phase
Phase 11 Start
Start the
the output
output
operation
operation
36
37
Parameterized Interfaces
Multiple Users of a Component
38
Parameterized Interfaces
A parameterized interface allows a component to
provide multiple instances of an interface
Parameterization (index) is set by a compile-time
value
provides interface Timer[uint8_t id];
39
Unique Instances
How to make sure your instance Parameter is not
used by some one else?
unique(astring)
generates a unique 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.
40
Tinyos-1.x/tos/system/TimerM.nc
configuration CntToLedsAndRfm {
}
implementation {
components Main, Counter, IntToLeds,
IntToRfm, TimerC;
module TimerM {
provides interface Timer[uint8_t id];
provides interface StdControl;
uses {
interface Leds;
interface Clock;
interface PowerManagement;
}
}
implementation {
uint32_t mState;
uint8_t queue_size;
uint8_t queue[NUM_TIMERS];
Instantiates
Instantiates the
the Timer
Timer number
number uniquely
uniquely
41
Fan-Out
Component is wired to multiple Destinations
Commands and Events will flow between all
connected components
Order is not guaranteed
Tinyos-1.x/apps/CntToLedsAndRfm.nc
configuration CntToLedsAndRfm {
}
implementation {
components Main, Counter, IntToLeds, IntToRfm, TimerC;
Counter.IntOutput -> IntToLeds;
Counter.IntOutput -> IntToRfm;
42
43
0xFF
0x7E
44
45
TOSmsg (tinyos-1.x/tos/types/am.h)
#define TOSH_DATA_LENGTH 29
typedef struct TOS_Msg{
uint16_t addr;
uint8_t type;
uint8_t group;
uint8_t length;
int8_t data[TOSH_DATA_LENGTH];
uint16_t crc;
TOS
TOS Message
Message 36
36 Bytes
Bytes
//Extra
uint16_t strength;
uint8_t ack;
uint16_t time;
uint8_t sendSecurityMode;
uint8_t receiveSecurityMode;
} TOS_Msg;
46
Extension
Extension passed
passed from
from MAC
MAC layer
layer
77 bytes
bytes
RSSI on Receive
LowPowerRadioStack: Determines PowerMode / Preamble
size on Transmit
47
Application
AM
RADIO
STACK
TX
RX
RADIO
STACK
Generic
Comm
AM
signal[m](TOSMsg) signal[n]
AM Handler
#m
TinyOS/nesC Programming (Parts 1 and 2)
48
Generic
Comm
AM Handler
#n
signal[p]
AM Handler
#m
San Jose / February 9-10, 2005
Active Message ID
defined in IntMsg.h
49
TX Component
.send(&TXBuffer)
.sendDone(&TXBuffer)
RX Side
Event Handler gets RXBuffer.
Returns a buffer for next RX
message.
Signal.Rcv(&RXBuffer)
RX
Event Handler
50
AM Component
AM Component
return (&RXBuffer)
Receive Event
51
Sending a Message
IntToRfmM.nc
The
bool pending;
The buffer
buffer is
is now
now busy
busy
struct TOS_Msg data;
/* ... */
command result_t IntOutput.output(uint16_t value) {
IntMsg *message = (IntMsg *)data.data;
if (!pending) {
pending = TRUE;
message->val = value;
TXBuffer
TXBuffer pointer
pointer
atomic {
message->src = TOS_LOCAL_ADDRESS;
}
if(call Send.send(TOS_BCAST_ADDR, sizeof(IntMsg),&data))
return SUCCESS;
pending = FALSE; //request failed /
return FAIL;
Destination
Destination address
address
}
TinyOS/nesC Programming (Parts 1 and 2)
52
Use
Use the
the returned
returned buffer
buffer for
for next
next Msg
Msg
TinyOS/nesC Programming (Parts 1 and 2)
53
Return
Return the
the Buffer
Buffer to
to GenericComm
GenericComm
54
TinyOS Review
Split phase processes
Indeterminate duration
Parallelism
Parameterized interfaces
Active messages
Routing
Message structure
Buffers
55
56