TinyOS
Ankit Gupta Karan Mangla Lakshya Goel Nitin Jain
TinyOS Overview of TinyOS Industrial motivations behind TinyOS - - PDF document
Karan Mangla Lakshya Goel Ankit Gupta Nitin Jain TinyOS Overview of TinyOS Industrial motivations behind TinyOS What is TinyOS? TinyOS vs. traditional OS TinyOS design models TinyOS structure Industrial Motivations behind
Ankit Gupta Karan Mangla Lakshya Goel Nitin Jain
Industrial motivations behind TinyOS What is TinyOS? TinyOS vs. traditional OS TinyOS design models TinyOS structure
Low power wireless communication devices
Particularly wireless networked sensors;
Physical limitations
Computation ability Memory Power supply High-level concurrency
An event-based operating system designed
Designed to support concurrency-intensive
Developed by the EECS Department of U.C.
C and Assembly languages Source code size: 500KB, 16KB commented lines
Special purpose (not general purpose) Resource constraint
4MHz ATMEL 8535 8bit MCU 512 byte RAM and 8K Flash
No dedicated I/O controller (missed deadline
One program at one time (no multi-programming) Thin-threads (tasks)
Component-based model (modularity)
Simple functions are incorporated in components with
clean interfaces;
Complex functions can be implemented by composing
components.
Event-based model
Interact with outside by events (no command shell) There are two kinds of events for TinyOS: External events : Clock events and message events; Internal events triggered by external events.
Communication Actuating Sensing Communication Application Main (scheduler) Hardware Abstractions (ADC, CLOCK, I2C, LEDS, PHOTO, UART, RFM)
Consists of a scheduler and a graph of components.
Component Structure Concurrency Model TinyOS schedulers Memory Model
A TinyOS Component:
Frame (storage) Tasks (computation & concurrency)
Time consuming computations; Have no hard real-time requirements;
Commands and events (interfaces)
Can use other interfaces Provide events
To be coded by the interface user
Provides commands
Coded by the interface provider
Perform longer operations Can be preempted by hardware event
Declaration: task void <name>() { ... } Posting a task: post taskname();
Function call across component boundary
cause action to be initiated bounded amount of work
cannot block
always return status (0 => error)
component can refuse to perform command
share call stack and execution context access to local frame commands may post tasks or call commands
Upcall to notify action has occured
bounded (and small) amount of work access local frame, shares stack
Lowest-level events triggered by hardware
hardware abstraction components perform critical
May signal events or call commands Events may call commands
Two threads of execution
Tasks Hardware Event Handler – handles context switch
Both preemptable by asynchronous code Possibility of race condition
NesC warns while compiling Method of making a block atomic allowed
Atomicity
Disable hardware interrupt handling thread while
Task scheduler
Implement FIFO scheduler in sched.c
Data structure
TOS_sched_entry_T TOS_queue[MAX_THREADS];
Functions
TOS_queue Any Component (e.g. SHELL) MAIN Component
Schedule next task in queue (TOS_schedule_task)
Post task into queue (TOS_post)
STATIC memory(except msg buffers)
No heap(malloc) No function pointers
Global variables
Available on a per-frame basis
Local variables
Saved on the stack Declared within a method
Msg buffers allocated statically by
components,nut shared dynamically by
One task at a time – non preemptive kernel No local variables in events Buffers for active messaging Stack copy not with each task – reduce
Active Messages (AM) is a simple, extensible
Used in parallel and distributed computing
Centered on the concept of integrating
Low level networking protocols of legacy stacks
Kilobytes of memory at a minimum And performance is dependent on a fast processing
component.
Routing protocols implemented above these are :
Complex Place bandwidth demands that become unatenable for
links with kilobits per second throughput.
Sockets - Not well suited to the constrained
Interfaces centered on “stop and wait”
Fundamental fit between the event based nature of network
sensor applications and the event based primitives of the Active Messages communication.
The lightweight architecture of Active Messages can be
leveraged to balance the need for an extensible communication framework while maintaining efficiency and agility.
Event based handler invocation model allows application
developers to avoid busy-waiting for data to arrive and allows the system to overlap communication with other activities such as interacting with sensors or executing other applications.
Event centric nature of Active Messages makes it a natural fit for
these devices.
tos/system/AMStandard.nc Layers Involved
Specifying message data to send Specifying receiver’s address Determining when message’s memory can be
Buffering Incoming message Processing Message
Handler ID required to be specified
Communication
BLINK Main (scheduler) Hardware Abstractions (CLOCK and LEDS)
TimerM HPLclock The above application is used to toggle a Led on the mote at fixed intervals.
application
components to those provided by other components that are being used.
Blink.nc configuration Blink { } implementation { components Main, BlinkM, SingleTimer, LedsC; Main.StdControl -> SingleTimer.StdControl; Main.StdControl -> BlinkM.StdControl; BlinkM.Timer -> SingleTimer.Timer; BlinkM.Leds -> LedsC; }
RealMain.nc
module RealMain { uses { command result_t hardwareInit(); interface StdControl; interface Pot; } } implementation { int main() __attribute__ ((C, spontaneous)) { call hardwareInit(); call Pot.init(10); TOSH_sched_init(); call StdControl.init(); call StdControl.start(); __nesc_enable_interrupt(); while(1) { TOSH_run_task(); } } }
implementations of components.
hence when it utilizes this interface it communicates with all components that are connected to the Main component. Most components have a Std Control interface as this is what initializes every component.
involves one call to Std Contol init followed by a call to Std Control start to start the application. This is then followed by starting the task scheduler.
power settings of the mote. These calls are wired to lower level components which directly interface with the device controllers for the system.
Implementation of Main Component
BlinkM.nc
module BlinkM { provides { interface StdControl; } uses { interface Timer; interface Leds; } } implementation { command result_t StdControl.init() { call Leds.init(); return SUCCESS; } command result_t StdControl.start() { // Start a repeating timer that fires every 1000ms return call Timer.start(TIMER_REPEAT, 1000); } command result_t StdControl.stop() { return call Timer.stop(); } event result_t Timer.fired() { call Leds.redToggle(); return SUCCESS; } }
the Blink application.
all applications which must be initialized as startup.
interface for this application.
time period after which it toggles its Leds.
the register controlling the Leds to toggle the Led when the command Leds.redToggle is called.
TimerM.nc
task void signalOneTimer() { uint8_t itimer = dequeue(); if (itimer < NUM_TIMERS) signal Timer.fired[itimer](); } task void HandleFire() { uint8_t i; setIntervalFlag = 1; if (mState) { for (i=0;i<NUM_TIMERS;i++) { if (mState&(0x1<<i)) { mTimerList[i].ticksLeft -= (mInterval+1) ; if (mTimerList[i].ticksLeft<=2) { if (mTimerList[i].type==TIMER_REPEAT) { mTimerList[i].ticksLeft += mTimerList[i].ticks; } else {// one shot timer mState &=~(0x1<<i); } enqueue(i); post signalOneTimer(); }}}} adjustInterval(); } async event result_t Clock.fire() { post HandleFire(); return SUCCESS; }
we use in this application.
toggle its Leds.
multiple timers.
timer has finished and post a task to signal a timer fired event for that timer.
timer.fired within Clock.fire which would allow a more real time implementation of the system.
means that the computation cannot be done inside an event handler and must be posted as a task.
Implementation of the SingleTimer component
HPLclock.nc TOSH_INTERRUPT(SIG_OUTPUT_COMPARE0) { atomic { if (set_flag) { mscale = nextScale; nextScale|=0x8;
set_flag=0; } } signal Clock.fire(); } Hardware.h #define TOSH_INTERRUPT(signame) \ void signame() __attribute__ ((interrupt, spontaneous, C))
interface to the actual hardware.
interfacing with the clock device driver on the mica2 platform.
defined in C which can be called by other higher level components. This is the file that varies from implementation to implementation. Let us look at how nesC allows us to define interrupt handlers.
interrupt occurs
function with __attribute__((interrupt))
be used to define interrupt handlers.
Hardware Interface of the application
Application level Components Functionality AM_BEACON.c Application that periodically broadcasts out identity
predetermined packet of information AM_BOUNCER.c Equivalent to a ping handler. Accepts a packet coming in from the network and responds with a packet sent back to the
AM_ECHO.c A component that forwards data through a node. Essentially, this is a router for a source based routing algorithm.
Main level object Functionality MAIN.c Required component that initializes the system and starts the
sched.c System Scheduler, must be included in all applications. This is a simple FIFO scheduler that is used to schedule the tasks. Active messaging layer AM.c When packet arrives, check address and dispatch message to appropriate message handler. AM_STANDARD.c Base station with UART forwarding. This component behaves same as AM.c component EXCEPT that if a packet is sent to the address 0x7e, it is directed to UART instead of radio.
Packet Transport Layer Functionality PACKETOBJ.c This component takes a pointer to a byte array to be sent down to the byte level component below it. It hands the bytes down to byte level component one at a time. CRCPACKETOBJ.c Checks for CRC validity. This component performs same function as basic PACKETOBJ.c component except that it transmits additional two bytes. UART_PACKET.c This packet level component is almost identical to PACKETOBJ.c component except it sends to UART instead of radio.
Radio Byte Engine (use one) Functionality RADIO_BYTE.c A byte level component used with RFM component. This component accepts bytes from packet layer component, encodes them for transmission, and passes the bits down to the RFM component one at a time. As bits arrive from the radio, it collects the bits together into a byte worth, decodes byte and then sends the decoded byte up to the packet layer. SEC_DED_RADIO_BY TE.c Provides Single error correction/Double error detection. Also detects preamble and start symbol detection. Also uses RFM for sending and receiving bits.
UART Transport Layer Functionality RADIO.c This component performs bit level control over the radio. Addtionally, it controls the amount of time per bit. UART.c Takes bytes 1 at a time and transmits them over UART. When the component is ready to handle another byte, it fires TX_DONE
BYTE_ARRIVAL event. Lower level device Components CLOCK.c Signals periodic events. It initialized with the interval with which to fire event and then periodically fires CLOCK_EVENT at that rate. LEDS.c Controls the outputs of the LED's
Lower level device components Functionality ADC.c Provides A/D support for analog sensors (Asynchronous interface) MIC.c, PHOTO.c, SOUNDER.c, VOLTAGE.c Interface to Microphone Interface to Photosensor Interface to Sound Device Interface to Voltage sensors
www.tinyos.net
Source code Tutorials Bits about NesC