Embedded OS Case Study: TinyOS Open-source development environment Simple (and tiny) operating system TinyOS Programming language and model nesc Set of services Principal elements Scheduler/event model of concurrency Software components for efficient modularity Software encapsulation for resources of sensor networks CSE 466 - Winter 2007 Case Study: TinyOS 1
TinyOS History www.tinyos.net Motivation create Unix analog (circa 1969) Uniform programming language: C Uniform device abstractions Open source: grow with different developers/needs Support creation of many tools Created at UC Berkeley 1st version written by Jason Hill in 2000 Large part of development moved to Intel Research Berkeley in 2001 www.intel-research.net/berkeley Smart Dust, Inc. founded in 2002 Large deployments Great Duck Island (GDI) http://www.greatduckisland.net/ Center for Embedded Network Sensing (CENS) http://www.cens.ucla.edu/ CSE 466 - Winter 2007 Case Study: TinyOS 2
TinyOS Design Goals Support networked embedded systems Asleep most of the time, but remain vigilant to stimuli Bursts of events and operations Support UCB mote hardware Power, sensing, computation, communication Easy to port to evolving platforms Support technological advances Keep scaling down Smaller, cheaper, lower power CSE 466 - Winter 2007 Case Study: TinyOS 3
TinyOS Design Options Can t use existing RTOS s Microkernel architecture VxWorks, PocketPC, PalmOS Execution similar to desktop systems PDA s, cell phones, embedded PC s More than a order of magnitude too heavyweight and slow Energy hogs CSE 466 - Winter 2007 Case Study: TinyOS 4
TinyOS Design Conclusion Similar to building networking interfaces Data driven execution Manage large # of concurrent data flows Manage large # of outstanding events Add: managing application data processing Conclusion: need a multi-threading engine Extremely efficient Extremely simple CSE 466 - Winter 2007 Case Study: TinyOS 5
TinyOS Kernel Design Two-level scheduling structure Events Small amount of processing to be done in a timely manner E.g. timer, ADC interrupts Can interrupt longer running tasks Tasks Not time critical Larger amount of processing E.g. computing the average of a set of readings in an array Run to completion with respect to other tasks Only need a single stack CSE 466 - Winter 2007 Case Study: TinyOS 6
TinyOS Concurrency Model Tasks FIFO queue Interrupts Two-level of concurrency: tasks and interrupts CSE 466 - Winter 2007 Case Study: TinyOS 7
TinyOS Concurrency Model (cont d) Tasks FIFO queue Placed on queue by: Application Other tasks Self-queued Interrupt service routine Run-to-completion No other tasks can run until completed Interruptable, but any new tasks go to end of queue Interrupts Stop running task Post new tasks to queue CSE 466 - Winter 2007 Case Study: TinyOS 8
TinyOS Concurrency Model (cont d) Two-levels of concurrency Possible conflicts between interrupts and tasks Atomic statements atomic Asynchronous service routines (as opposed to synchronous tasks) Race conditions detected by compiler Can generated false positives CSE 466 - Winter 2007 Case Study: TinyOS 9
TinyOS Programming Model Separation of construction and composition Programs are built out of components Specification of component behavior in terms of a set of interfaces Components specify interfaces they use and provide Components are statically wired to each other via their interfaces This increases runtime efficiency by enabling compiler optimizations Finite-state-machine-like specifications Thread of control passes into a component through its interfaces to another component CSE 466 - Winter 2007 Case Study: TinyOS 10
TinyOS Basic Constructs Commands Events Cause action to be initiated Notify action has occurred Generated by external interrupts Call back to provide results from previous command Tasks Background computation Not time critical Application event command Component event command Hardware Interface task task task CSE 466 - Winter 2007 Case Study: TinyOS 11
Flow of Events and Commands Fountain of events leading to commands and tasks (which in turn issue may issue other commands that may cause other events, ) task to get out of async tasks events commands interrupts Software Hardware CSE 466 - Winter 2007 Case Study: TinyOS 12
TinyOS File Types Interfaces (xxx.nc) Specifies functionality to outside world what commands can be called what events need handling Module (xxxm.nc) Code implementation Code for Interface functions Configuration (xxxc.nc) Wiring of components When top level app, drop C from filename xxx.nc interfacea.nc comp1c.nc (wires) interfaceb.nc main.nc interfacem.nc interfacem.nc app.nc (wires) interfacea.nc interfacea.nc comp2m.nc (code) interfaceb.nc comp3m.nc (code) CSE 466 - Winter 2007 Case Study: TinyOS 13
The nesc Language nesc: networks of embedded sensors C Compiler for applications that run on UCB motes Built on top of avg-gcc nesc uses the filename extension ".nc Static Language No dynamic memory (no malloc) No function pointers No heap Influenced by Java Includes task FIFO scheduler Designed to foster code reuse Modules per application range from 8 to 67, mean of 24*** Average lines of code in a module only 120*** Advantages of eliminating monolithic programs Code can be reused more easily Number of errors should decrease TinyOS kernel (C) TinyOS libs (nesc) Application (nesc) nesc Compiler Application & TinyOS (C) C Compiler Application Executable ***The NesC Language: A Holistic Approach to Network of Embedded Systems. David Gay, Phil Levis, Rob von Behren, Matt Welsh, Eric Brewer, and David Culler. Proceedings of Programming Language Design and Implementation (PLDI) 2003, June 2003. CSE 466 - Winter 2007 Case Study: TinyOS 14
Commands Commands are issued with call call Timer.start(TIMER_REPEAT, 1000); Cause action to be initiated Bounded amount of work Does not block Act similarly to a function call Execution of a command is immediate CSE 466 - Winter 2007 Case Study: TinyOS 15
Events Events are called with signal signal ByteComm.txByteReady(SUCCESS); Used to notify a component an action has occurred Lowest-level events triggered by hardware interrupts Bounded amount of work Do not block Act similarly to a function call Execution of a event is immediate CSE 466 - Winter 2007 Case Study: TinyOS 16
Tasks Tasks are queued with post post radioencodethread(); Used for longer running operations Pre-empted by events Initiated by interrupts Tasks run to completion Not pre-empted by other tasks Example tasks High level calculate aggregate of sensor readings Low level encode radio packet for transmission, calculate CRC CSE 466 - Winter 2007 Case Study: TinyOS 17
Components Two types of components in nesc: Module Configuration A component provides and uses Interfaces CSE 466 - Winter 2007 Case Study: TinyOS 18
Module Provides application code Contains C-like code Must implement the provides interfaces Implement the commands it provides Make sure to actually signal Must implement the uses interfaces Implement the events that need to be handled call commands as needed CSE 466 - Winter 2007 Case Study: TinyOS 19
Configuration A configuration is a component that "wires" other components together. Configurations are used to assemble other components together Connects interfaces used by components to interfaces provided by others. CSE 466 - Winter 2007 Case Study: TinyOS 20
Interfaces Bi-directional multi-function interaction channel between two components Allows a single interface to represent a complex event E.g., a registration of some event, followed by a callback Critical for non-blocking operation provides interfaces Represent the functionality that the component provides to its user Service commands implemented command functions Issue events signal to user for passing data or signalling done uses interfaces Represent the functionality that the component needs from a provider Service events implement event handling Issue commands ask provider to do something CSE 466 - Winter 2007 Case Study: TinyOS 21
Application Consists of one or more components, wired together to form a runnable program Single top-level configuration that specifies the set of components in the application and how they connect to one another Connection (wire) to main component to start execution Must implement init, start, and stop commands CSE 466 - Winter 2007 Case Study: TinyOS 22
Components/Wiring Directed wire (an arrow: -> ) connects components Only 2 components at a time point-to-point Connection is across compatible interfaces A <- B is equivalent to B -> A [component using interface] -> [component providing interface] [interface] -> [implementation] = can be used to wire a component directly to the top-level object s interfaces Typically used in a configuration file to use a sub-component directly Unused system components excluded from compilation CSE 466 - Winter 2007 Case Study: TinyOS 23
Blink Application What the executable does: 1. Main initializes and starts the application 2. BlinkM initializes ClockC s rate at 1Hz 3. ClockC continuously signals BlinkM at a rate of 1 Hz 4. BlinkM commands LedsC red led to toggle each time it receives a signal from ClockC tos/system/main.nc tos/interfaces/stdcontrol.nc tos/interfaces/stdcontrol.nc BlinkM.nc Note: The StdControl interface is similar to state machines (init, start, stop); used extensively throughout TinyOS apps & libs tos/interfaces/timer.nc tos/interfaces/leds.nc tos/interfaces/singletimer.nc tos/interfaces/timer.nc tos/interfaces/leds.nc tos/system/timerc.nc tos/system/ledsc.nc CSE 466 - Winter 2007 Case Study: TinyOS 24
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.Leds; CSE 466 - Winter 2007 Case Study: TinyOS 25
StdControl.nc interface StdControl command result_t init(); command result_t start(); command result_t stop(); CSE 466 - Winter 2007 Case Study: TinyOS 26
BlinkM.nc BlinkM.nc module BlinkM provides interface StdControl; uses interface Timer; interface Leds; implementation command result_t StdControl.init() call Leds.init(); command result_t StdControl.start() return call Timer.start(TIMER_REPEAT, 1000); command result_t StdControl.stop() return call Timer.stop(); event result_t Timer.fired() call Leds.redToggle(); CSE 466 - Winter 2007 Case Study: TinyOS 27
SingleTimer.nc (should have been SingleTimerC.nc) Parameterized interfaces allows a component to provide multiple instances of an interface that are parameterized by a value Timer implements one level of indirection to actual timer functions Timer module supports many interfaces This module simply creates one unique timer interface and wires it up By wiring Timer to a separate instance of the Timer interface provided by TimerC, each component can effectively get its own "private" timer Uses a compile-time constant function unique() to ensure index is unique configuration SingleTimer provides interface Timer; provides interface StdControl; implementation components TimerC; Timer = TimerC.Timer[unique("Timer")]; StdControl = TimerC.StdControl; CSE 466 - Winter 2007 Case Study: TinyOS 28
Blink.nc without SingleTimer configuration Blink implementation components Main, BlinkM, TimerC, LedsC; Main.StdControl -> TimerC.StdControl; Main.StdControl -> BlinkM.StdControl; BlinkM.Timer -> TimerC.Timer[unique("Timer")]; BlinkM.Leds -> LedsC.Leds; CSE 466 - Winter 2007 Case Study: TinyOS 29
Timer.nc interface Timer command result_t start(char type, uint32_t interval); command result_t stop(); event result_t fired(); CSE 466 - Winter 2007 Case Study: TinyOS 30
TimerC.nc Implementation of multiple timer interfaces to a single shared timer Each interface is named Each interface connects to one other module CSE 466 - Winter 2007 Case Study: TinyOS 31
Leds.nc (partial) interface Leds /** * Initialize the LEDs; among other things, initialization turns them all off. */ async command result_t init(); /** * Turn the red LED on. */ async command result_t redon(); /** * Turn the red LED off. */ async command result_t redoff(); /** * Toggle the red LED. If it was on, turn it off. If it was off, * turn it on. */ async command result_t redtoggle();... CSE 466 - Winter 2007 Case Study: TinyOS 32
LedsC.nc (partial) module LedsC provides interface Leds; implementation uint8_t ledson; enum RED_BIT = 1, GREEN_BIT = 2, YELLOW_BIT = 4 ; async command result_t Leds.init() atomic ledson = 0; dbg(dbg_boot, "LEDS: initialized.\n"); TOSH_MAKE_RED_LED_OUTPUT(); TOSH_MAKE_YELLOW_LED_OUTPUT(); TOSH_MAKE_GREEN_LED_OUTPUT(); TOSH_SET_RED_LED_PIN(); TOSH_SET_YELLOW_LED_PIN(); TOSH_SET_GREEN_LED_PIN(); async command result_t Leds.redOn() dbg(dbg_led, "LEDS: Red on.\n"); atomic TOSH_CLR_RED_LED_PIN(); ledson = RED_BIT; async command result_t Leds.redOff() dbg(dbg_led, "LEDS: Red off.\n"); atomic TOSH_SET_RED_LED_PIN(); ledson &= ~RED_BIT; async command result_t Leds.redToggle() result_t rval; atomic if (ledson & RED_BIT) rval = call Leds.redOff(); else rval = call Leds.redOn(); return rval;... CSE 466 - Winter 2007 Case Study: TinyOS 33
static inline void TOSH_SET_PIN_DIRECTIONS(void ) static inline void HPLPowerManagementM$doAdjustment(void) Blink Compiled TOSH_MAKE_RED_LED_OUTPUT(); inline static result_t TimerM$Clock$setRate(char arg_0xa33adc0, char uint8_t foo; arg_0xa33af00) TOSH_MAKE_YELLOW_LED_OUTPUT(); uint8_t mcu; unsigned char result; TOSH_MAKE_GREEN_LED_OUTPUT(); foo = HPLPowerManagementM$getPowerLevel(); result = HPLClock$Clock$setRate(arg_0xa33adc0, arg_0xa33af00); TOSH_MAKE_CC_CHP_OUT_INPUT(); mcu = * (volatile unsigned char *)(unsigned int )& * (volatile unsigned char return result; inline static result_t BlinkM$Leds$redToggle(void) *)(0x35 + 0x20); TOSH_MAKE_PW7_OUTPUT(); unsigned char result; mcu &= 0xe3; TOSH_MAKE_PW6_OUTPUT(); static inline result_t TimerM$StdControl$init(void) if (foo == HPLPowerManagementM$EXT_STANDBY foo == result = LedsC$Leds$redToggle(); TOSH_MAKE_PW5_OUTPUT(); HPLPowerManagementM$POWER_SAVE) return result; TOSH_MAKE_PW4_OUTPUT(); TimerM$mState = 0; mcu = HPLPowerManagementM$IDLE; TOSH_MAKE_PW3_OUTPUT(); TimerM$setIntervalFlag = 0; while ((* (volatile unsigned char *)(unsigned int )& * (volatile static inline result_t BlinkM$Timer$fired(void) TOSH_MAKE_PW2_OUTPUT(); unsigned char *)(0x30 + 0x20) & 0x7)!= 0) TimerM$queue_head = TimerM$queue_tail = -1; TOSH_MAKE_PW1_OUTPUT(); asm volatile ("nop"); TimerM$queue_size = 0; BlinkM$Leds$redToggle(); TOSH_MAKE_PW0_OUTPUT(); TimerM$mScale = 3; int main(void); TOSH_MAKE_CC_PALE_OUTPUT(); mcu &= 0xe3; TimerM$mInterval = TimerM$maxTimerInterval; static result_t PotM$HPLPot$finalise(void); TOSH_MAKE_CC_PDATA_OUTPUT(); return TimerM$Clock$setRate(TimerM$mInterval, TimerM$mScale); static inline result_t TimerM$Timer$default$fired(uint8_t id) static result_t PotM$HPLPot$decrease(void); TOSH_MAKE_CC_PCLK_OUTPUT(); mcu = foo; static result_t PotM$HPLPot$increase(void); TOSH_MAKE_MISO_INPUT(); * (volatile unsigned char *)(unsigned int )& * (volatile unsigned char inline static result_t RealMain$StdControl$init(void) static inline void TOSH_SET_GREEN_LED_PIN(void) *)(0x35 + 0x20) = mcu; uint8_t PotM$potSetting; TOSH_MAKE_SPI_OC1C_INPUT(); unsigned char result; * (volatile unsigned char *)(unsigned int )& * (volatile unsigned char static inline void PotM$setPot(uint8_t value); TOSH_MAKE_SERIAL_ID_INPUT(); #define dbg(mode, format,...) ((void)0) result = TimerM$StdControl$init(); *)(0x35 + 0x20) = 1 << 5; inline static result_t TimerM$Timer$fired(uint8_t arg_0xa31d660) * (volatile unsigned char *)(unsigned int )& * (volatile unsigned static inline result_t PotM$Pot$init(uint8_t initialsetting); TOSH_CLR_SERIAL_ID_PIN(); char *)(0x1B + 0x20) #define dbg_clear(mode, format,...) ((void)0) = 1 << 1; result = rcombine(result, BlinkM$StdControl$init()); unsigned char result; static inline result_t HPLPotC$Pot$decrease(void); TOSH_MAKE_FLASH_SELECT_OUTPUT(); #define dbg_active(mode) 0 return result; static inline void nesc_enable_interrupt(void) switch (arg_0xa31d660) static inline result_t HPLPotC$Pot$increase(void); TOSH_MAKE_FLASH_OUT_OUTPUT(); typedef signed char int8_t; static inline void TOSH_SET_YELLOW_LED_PIN(void) case 0: typedef long long TOS_dbg_mode; static inline result_t HPLPotC$Pot$finalise(void); TOSH_MAKE_FLASH_CLK_OUTPUT(); typedef unsigned char uint8_t; inline static uint8_t TimerM$PowerManagement$adjustPower(void) asm volatile ("sei"); result = BlinkM$Timer$fired(); enum nesc_unnamed4251 static inline result_t HPLInit$init(void); TOSH_SET_FLASH_SELECT_PIN(); typedef int int16_t; * (volatile unsigned char *)(unsigned int )& * (volatile unsigned char *)(0x1B + 0x20) unsigned char result; static inline void TOSH_wait(void) break; DBG_ALL = ~0ULL, static result_t BlinkM$Leds$init(void); = 1 << 0; TOSH_SET_RED_LED_PIN(); typedef unsigned int uint16_t; result = HPLPowerManagementM$PowerManagement$adjustPower(); default: DBG_BOOT = 1ULL << 0, static result_t BlinkM$Leds$redToggle(void); TOSH_SET_YELLOW_LED_PIN(); typedef long int32_t; return result; asm volatile ("nop"); result = TimerM$Timer$default$fired(arg_0xa31d660); DBG_CLOCK = 1ULL << 1, static result_t BlinkM$Timer$start(char arg_0xa2fd578, static uint32_t inline arg_0xa2fd6d0); void TOSH_SET_RED_LED_PIN(void) TOSH_SET_GREEN_LED_PIN(); typedef unsigned long uint32_t; asm volatile ("nop"); DBG_TASK = 1ULL << 2, static inline result_t BlinkM$StdControl$init(void); typedef long long int64_t; static inline void HPLClock$Clock$setInterval(uint8_t value) static inline void TOSH_sleep(void) return result; DBG_SCHED = 1ULL << 3, static inline result_t BlinkM$StdControl$start(void); * (volatile unsigned char *)(unsigned int )& * (volatile unsigned static char inline *)(0x1B result_t + 0x20) HPLInit$init(void) typedef unsigned long long uint64_t; = 1 << 2; DBG_SENSOR = 1ULL << 4, static inline result_t BlinkM$Timer$fired(void); typedef int16_t intptr_t; * (volatile unsigned char *)(unsigned int )& * (volatile unsigned char * (volatile unsigned char *)(unsigned int )& * (volatile unsigned static char inline uint8_t TimerM$dequeue(void) DBG_LED = 1ULL << 5, static uint8_t TimerM$PowerManagement$adjustPower(void); TOSH_SET_PIN_DIRECTIONS(); *)(0x31 + 0x20) = value; *)(0x35 + 0x20) = 1 << 5; typedef uint16_t uintptr_t; static inline void TOSH_SET_FLASH_SELECT_PIN(void) DBG_CRYPTO = 1ULL << 6, static void TimerM$Clock$setInterval(uint8_t arg_0xa33b8c0); asm volatile ("sleep"); typedef unsigned int size_t; if (TimerM$queue_size == 0) DBG_ROUTE = 1ULL << 7, static uint8_t TimerM$Clock$readCounter(void); inline static void TimerM$Clock$setInterval(uint8_t arg_0xa33b8c0) inline void nesc_atomic_end( nesc_atomic_t oldsreg) typedef int wchar_t; return NUM_TIMERS; * (volatile unsigned char *)(unsigned int )& * (volatile unsigned char *)(0x1B + 0x20) DBG_AM = 1ULL << 8, static result_t TimerM$Clock$setRate(char arg_0xa33adc0, char arg_0xa33af00); typedef struct nesc_unnamed4242 = 1 << 3; inline static result_t RealMain$hardwareInit(void) HPLClock$Clock$setInterval(arg_0xa33b8c0); DBG_CRC = 1ULL << 9, static result_t TimerM$Timer$fired(uint8_t arg_0xa31d660); unsigned char result; int quot; * (volatile unsigned char *)(unsigned int )& * (volatile unsigned char *)(0x3F + 0x20) = oldsreg; if (TimerM$queue_head == NUM_TIMERS - 1) DBG_PACKET = 1ULL << 10, uint32_t TimerM$mState; result = HPLInit$init(); int rem; static inline void TOSH_MAKE_FLASH_CLK_OUTPUT(void) static inline uint8_t HPLClock$Clock$readCounter(void) TimerM$queue_head = -1; DBG_ENCODE = 1ULL << 11, uint8_t TimerM$setIntervalFlag; return result; div_t; inline nesc_atomic_t nesc_atomic_start(void ) DBG_RADIO = 1ULL << 12, uint8_t TimerM$mScale; typedef struct nesc_unnamed4243 * (volatile unsigned char *)(unsigned int )& * (volatile unsigned char *)(0x11 + 0x20) return * (volatile unsigned char *)(unsigned int )& * (volatile unsigned DBG_LOG = 1ULL << 13, uint8_t TimerM$mInterval; = 1 << 5; char *)(0x32 + 0x20); TimerM$queue_head++; static inline result_t HPLPotC$Pot$finalise(void) long quot; DBG_ADC = 1ULL << 14, nesc_atomic_t result = * (volatile unsigned char *)(unsigned int )& TimerM$queue_size--; * int8_t TimerM$queue_head; (volatile unsigned char *)(0x3F + 0x20); long rem; return TimerM$queue[(uint8_t )TimerM$queue_head]; DBG_I2C = 1ULL << 15, static inline void TOSH_MAKE_FLASH_OUT_OUTPUT(void) inline static uint8_t TimerM$Clock$readCounter(void) int8_t TimerM$queue_tail; asm volatile ("cli"); ldiv_t; DBG_UART = 1ULL << 16, uint8_t TimerM$queue_size; unsigned char result; return result; typedef int (* compar_fn_t)(const void *, const void *); static inline void TimerM$signalOneTimer(void) DBG_PROG = 1ULL << 17, uint8_t TimerM$queue[NUM_TIMERS]; * (volatile unsigned char *)(unsigned int )& * (volatile unsigned char *)(0x11 + 0x20) result = HPLClock$Clock$readCounter(); inline static result_t PotM$HPLPot$finalise(void) typedef int ptrdiff_t; = 1 << 3; return result; DBG_SOUNDER = 1ULL << 18, struct TimerM$timer_s unsigned char result; static inline bool TOSH_run_next_task(void) typedef unsigned char bool; uint8_t itimer = TimerM$dequeue(); DBG_TIME = 1ULL << 19, uint8_t type; result = HPLPotC$Pot$finalise(); enum nesc_unnamed4244 static inline void TOSH_MAKE_FLASH_SELECT_OUTPUT(void) if (itimer < NUM_TIMERS) static inline result_t TimerM$Timer$start(uint8_t id, char type, uint32_t DBG_SIM = 1ULL << 21, int32_t ticks; return result; FALSE = 0, interval) nesc_atomic_t finterruptflags; TimerM$Timer$fired(itimer); DBG_QUEUE = 1ULL << 22, int32_t ticksleft; TRUE = 1 * (volatile unsigned char *)(unsigned int )& * (volatile unsigned char *)(0x1A + 0x20) uint8_t old_full; DBG_SIMRADIO = 1ULL << 23, TimerM$mTimerList[NUM_TIMERS]; = 1 << 3; static inline result_t HPLPotC$Pot$increase(void) ; uint8_t diff; void (*func)(void ); DBG_HARD = 1ULL << 24, enum TimerM$ nesc_unnamed4257 enum nesc_unnamed4245 if (id >= NUM_TIMERS) if (TOSH_sched_full == TOSH_sched_free) static inline void TimerM$enqueue(uint8_t value) DBG_MEM = 1ULL << 25, TimerM$maxTimerInterval = 230 static inline void TOSH_CLR_SERIAL_ID_PIN(void) FAIL = 0, return FAIL; return 0; DBG_USR1 = 1ULL << 27, ; SUCCESS = 1 else if (TimerM$queue_tail == NUM_TIMERS - 1) DBG_USR2 = 1ULL << 28, static inline result_t TimerM$StdControl$init(void); * (volatile unsigned char *)(unsigned int )& * (volatile unsigned inline char static *)(0x1B result_t + 0x20) PotM$HPLPot$increase(void) ; if (type > 1) finterruptflags = nesc_atomic_start(); TimerM$queue_tail = -1; &= ~(1 << 4); DBG_USR3 = 1ULL << 29, static inline result_t TimerM$StdControl$start(void); unsigned char result; static inline uint8_t rcombine(uint8_t r1, uint8_t r2); return FAIL; old_full = TOSH_sched_full; DBG_TEMP = 1ULL << 30, static inline result_t TimerM$Timer$start(uint8_t id, char type, result = HPLPotC$Pot$increase(); typedef uint8_t result_t; TOSH_sched_full++; TimerM$queue_tail++; DBG_ERROR = 1ULL << 31, static inline void TOSH_MAKE_SERIAL_ID_INPUT(void) uint32_t interval); return result; static inline result_t rcombine(result_t r1, result_t r2); TimerM$mTimerList[id].ticks = interval; TOSH_sched_full &= TOSH_TASK_BITMASK; TimerM$queue_size++; DBG_NONE = 0, static void TimerM$adjustInterval(void); enum nesc_unnamed4246 TimerM$mTimerList[id].type = type; func = TOSH_queue[(int )old_full].tp; TimerM$queue[(uint8_t )TimerM$queue_tail] = value; * (volatile unsigned char *)(unsigned int )& * (volatile unsigned DBG_DEFAULT = DBG_ALL static inline result_t TimerM$Timer$default$fired(uint8_t id); static char inline *)(0x1A result_t + 0x20) HPLPotC$Pot$decrease(void) NULL = 0x0 &= ~(1 << 4); nesc_atomic_t nesc_atomic = nesc_atomic_start(); TOSH_queue[(int )old_full].tp = 0; ; static inline void TimerM$enqueue(uint8_t value); ; nesc_atomic_end(finterruptflags); static inline void TimerM$HandleFire(void) typedef struct nesc_unnamed4252 static inline uint8_t TimerM$dequeue(void); typedef void attribute(( progmem )) prog_void; static inline void TOSH_MAKE_SPI_OC1C_INPUT(void) diff = TimerM$Clock$readCounter(); func(); void (*tp)(void); static inline void TimerM$signalOneTimer(void); typedef char attribute(( progmem )) prog_char; interval += diff; return 1; uint8_t i; TOSH_sched_entry_T; static inline void TimerM$HandleFire(void); inline static result_t PotM$HPLPot$decrease(void) typedef unsigned char attribute(( progmem )) prog_uchar; * (volatile unsigned char *)(unsigned int )& * (volatile unsigned char *)(0x17 + 0x20) TimerM$mTimerList[id].ticksLeft = interval; TimerM$setIntervalFlag = 1; enum nesc_unnamed4253 static inline result_t TimerM$Clock$fire(void); &= ~(1 << 7); unsigned char result; typedef int attribute(( progmem )) prog_int; TimerM$mState = 0x1 << id; if (TimerM$mState) TOSH_MAX_TASKS = 8, static result_t HPLClock$Clock$fire(void); result = HPLPotC$Pot$decrease(); typedef long attribute(( progmem )) prog_long; if (interval < TimerM$mInterval) static inline void TOSH_run_task(void) for (i = 0; i < NUM_TIMERS; i++) TOSH_TASK_BITMASK = TOSH_MAX_TASKS - 1 uint8_t HPLClock$set_flag; static inline void TOSH_MAKE_MISO_INPUT(void) return result; typedef long long attribute(( progmem )) prog_long_long; TimerM$mInterval = interval; if (TimerM$mState & (0x1 << i)) ; uint8_t HPLClock$mscale; enum nesc_unnamed4247 TimerM$Clock$setInterval(TimerM$mInterval); while (TOSH_run_next_task()) ; TimerM$mTimerList[i].ticksLeft -= TimerM$mInterval + 1; TOSH_sched_entry_T TOSH_queue[TOSH_MAX_TASKS]; uint8_t HPLClock$nextScale; * (volatile unsigned char *)(unsigned int )& * (volatile unsigned static char inline *)(0x17 void + PotM$setPot(uint8_t 0x20) value) TOSH_period16 = 0x00, TimerM$setIntervalFlag = 0; TOSH_sleep(); if (TimerM$mTimerList[i].ticksLeft <= 2) &= ~(1 << 3); volatile uint8_t TOSH_sched_full; uint8_t HPLClock$minterval; TOSH_period32 = 0x01, TimerM$PowerManagement$adjustPower(); TOSH_wait(); if (TimerM$mTimerList[i].type == TIMER_REPEAT) volatile uint8_t TOSH_sched_free; static inline void HPLClock$Clock$setInterval(uint8_t value); uint8_t i; TOSH_period64 = 0x02, TimerM$mTimerList[i].ticksLeft += TimerM$mTimerList[i].ticks; static inline void TOSH_MAKE_CC_PCLK_OUTPUT(void) static inline void TOSH_wait(void ); static inline uint8_t HPLClock$Clock$readCounter(void); for (i = 0; i < 151; i++) TOSH_period128 = 0x03, static void TimerM$adjustInterval(void) static inline void TOSH_sleep(void ); static inline result_t HPLClock$Clock$setRate(char interval, char scale); PotM$HPLPot$decrease(); TOSH_period256 = 0x04, nesc_atomic_end( nesc_atomic); else * (volatile unsigned char *)(unsigned int )& * (volatile unsigned char *)(0x11 + 0x20) static inline void TOSH_sched_init(void ); void attribute((interrupt)) vector_15(void); for (i = 0; i < value; i++) TOSH_period512 = 0x05, = 1 << 6; uint8_t i; bool TOS_post(void (*tp)(void)); bool HPLPowerManagementM$disabled = TRUE; PotM$HPLPot$increase(); TOSH_period1024 = 0x06, uint8_t val = TimerM$maxTimerInterval; TimerM$mState &= ~(0x1 << i); static inline bool TOSH_run_next_task(void); enum HPLPowerManagementM$ nesc_unnamed4258 PotM$HPLPot$finalise(); TOSH_period2048 = 0x07 static inline void TOSH_MAKE_CC_PDATA_OUTPUT(void) inline static result_t BlinkM$Timer$start(char arg_0xa2fd578, uint32_t if (TimerM$mState) static inline void TOSH_run_task(void); HPLPowerManagementM$IDLE = 0, PotM$potSetting = value; arg_0xa2fd6d0) ; for (i = 0; i < NUM_TIMERS; i++) TimerM$enqueue(i); enum nesc_unnamed4254 HPLPowerManagementM$ADC_NR = 1 << 3, unsigned char result; if (TimerM$mState & (0x1 << i) && TimerM$mTimerList[i].ticksLeft < static inline void TOSH_wait(void); * (volatile unsigned char *)(unsigned int )& * (volatile unsigned char *)(0x11 + 0x20) TOS_post(TimerM$signalOneTimer); TIMER_REPEAT = 0, HPLPowerManagementM$POWER_DOWN = 1 << 4, = 1 << 7; static inline result_t PotM$Pot$init(uint8_t initialsetting) result = TimerM$Timer$start(0, arg_0xa2fd578, arg_0xa2fd6d0); val) static inline void TOSH_sleep(void); TIMER_ONE_SHOT = 1, HPLPowerManagementM$POWER_SAVE = (1 << 3) + (1 << 4), return result; val = TimerM$mTimerList[i].ticksLeft; typedef uint8_t nesc_atomic_t; NUM_TIMERS = 1 HPLPowerManagementM$STANDBY = (1 << 2) + (1 << 4), static inline void TOSH_MAKE_CC_PALE_OUTPUT(void) PotM$setPot(initialSetting); inline nesc_atomic_t nesc_atomic_start(void ); ; HPLPowerManagementM$EXT_STANDBY = (1 << 3) + (1 << 4) + (1 << 2) static inline result_t BlinkM$StdControl$start(void) inline void nesc_atomic_end( nesc_atomic_t oldsreg); enum nesc_unnamed4255 ; * (volatile unsigned char *)(unsigned int )& * (volatile unsigned char *)(0x11 + 0x20) nesc_atomic_t nesc_atomic = nesc_atomic_start(); static inline void nesc_enable_interrupt(void); = 1 << 4; TimerM$adjustInterval(); TOS_I1000PS = 32, TOS_S1000PS = 1, static inline uint8_t HPLPowerManagementM$getPowerLevel(void); inline static result_t RealMain$Pot$init(uint8_t arg_0xa2ce970) return BlinkM$Timer$start(TIMER_REPEAT, 1000); static inline void TOSH_SET_RED_LED_PIN(void); TOS_I100PS = 40, TOS_S100PS = 2, static inline void HPLPowerManagementM$doAdjustment(void); unsigned char result; TimerM$mInterval = val; static inline void TOSH_CLR_RED_LED_PIN(void); static inline void TOSH_MAKE_PW0_OUTPUT(void) static inline result_t TimerM$Clock$fire(void) TOS_I10PS = 101, TOS_S10PS = 3, static uint8_t HPLPowerManagementM$PowerManagement$adjustPower(void); result = PotM$Pot$init(arg_0xa2ce970); static inline result_t TimerM$StdControl$start(void) TimerM$Clock$setInterval(TimerM$mInterval); static inline void TOSH_MAKE_RED_LED_OUTPUT(void); TOS_I1024PS = 0, TOS_S1024PS = 3, uint8_t LedsC$ledsOn; return result; TimerM$setIntervalFlag = 0; static inline void TOSH_SET_GREEN_LED_PIN(void); * (volatile unsigned char *)(unsigned int )& * (volatile unsigned char *)(0x14 + 0x20) TOS_post(TimerM$HandleFire); TOS_I512PS = 1, TOS_S512PS = 3, enum LedsC$ nesc_unnamed4259 = 1 << 0; static inline void TOSH_MAKE_GREEN_LED_OUTPUT(void); TOS_I256PS = 3, TOS_S256PS = 3, LedsC$RED_BIT = 1, static inline void TOSH_sched_init(void ) nesc_atomic_end( nesc_atomic); static inline void TOSH_SET_YELLOW_LED_PIN(void); TOS_I128PS = 7, TOS_S128PS = 3, LedsC$GREEN_BIT = 2, static inline void TOSH_MAKE_PW1_OUTPUT(void) inline static result_t RealMain$StdControl$start(void) static inline void TOSH_MAKE_YELLOW_LED_OUTPUT(void); inline static result_t HPLClock$Clock$fire(void) TOS_I64PS = 15, TOS_S64PS = 3, LedsC$YELLOW_BIT = 4 TOSH_sched_free = 0; unsigned char result; else static inline void TOSH_CLR_SERIAL_ID_PIN(void); unsigned char result; TOS_I32PS = 31, TOS_S32PS = 3, ; * (volatile unsigned char *)(unsigned int )& * (volatile unsigned TOSH_sched_full char *)(0x14 = + 0; result = TimerM$StdControl$start(); nesc_atomic_t nesc_atomic = nesc_atomic_start(); 0x20) static inline void TOSH_MAKE_SERIAL_ID_INPUT(void); result = TimerM$Clock$fire(); TOS_I16PS = 63, TOS_S16PS = 3, = 1 << 1; static inline result_t LedsC$Leds$init(void); result = rcombine(result, BlinkM$StdControl$start()); static inline void TOSH_MAKE_CC_CHP_OUT_INPUT(void); return result; TOS_I8PS = 127, TOS_S8PS = 3, static inline result_t LedsC$Leds$redOn(void); static inline result_t rcombine(result_t r1, result_t r2) return result; TimerM$mInterval = TimerM$maxTimerInterval; static inline void TOSH_MAKE_CC_PDATA_OUTPUT(void); TOS_I4PS = 255, TOS_S4PS = 3, static inline void TOSH_MAKE_PW2_OUTPUT(void) static inline result_t LedsC$Leds$redOff(void); TimerM$Clock$setInterval(TimerM$mInterval); static inline void TOSH_MAKE_CC_PCLK_OUTPUT(void); bool TOS_post(void (*tp)(void)) TOS_I2PS = 15, TOS_S2PS = 7, static inline result_t LedsC$Leds$redToggle(void); return r1 == FAIL? FAIL : r2; static inline uint8_t HPLPowerManagementM$getPowerLevel(void) TimerM$setIntervalFlag = 0; static inline void TOSH_MAKE_CC_PALE_OUTPUT(void); TOS_I1PS = 31, TOS_S1PS = 7, * (volatile unsigned char *)(unsigned int )& * (volatile unsigned char *)(0x14 + 0x20) static inline void TOSH_SET_FLASH_SELECT_PIN(void); = 1 << 2; nesc_atomic_t finterruptflags; TOS_I0PS = 0, TOS_S0PS = 0 static inline uint8_t diff; nesc_atomic_end( nesc_atomic); static inline void TOSH_MAKE_FLASH_SELECT_OUTPUT(void); uint8_t tmp; ; result_t LedsC$Leds$init(void) if (* (volatile unsigned char *)(unsigned int )& * (volatile unsigned char static inline void TOSH_MAKE_FLASH_CLK_OUTPUT(void); static inline void TOSH_MAKE_PW3_OUTPUT(void) *)(0x37 + 0x20) & ~((1 << 1) (1 << 0))) finterruptflags = nesc_atomic_start(); TimerM$PowerManagement$adjustPower(); enum nesc_unnamed4256 static inline void TOSH_MAKE_FLASH_OUT_OUTPUT(void); return HPLPowerManagementM$IDLE; tmp = TOSH_sched_free; DEFAULT_SCALE = 3, DEFAULT_INTERVAL = 127 nesc_atomic_t nesc_atomic = nesc_atomic_start(); static inline void TOSH_MAKE_MISO_INPUT(void); * (volatile unsigned char *)(unsigned int )& * (volatile unsigned char *)(0x14 + 0x20) TOSH_sched_free++; static inline void TOSH_CLR_RED_LED_PIN(void) ; = 1 << 3; static inline void TOSH_MAKE_SPI_OC1C_INPUT(void); else TOSH_sched_free &= TOSH_TASK_BITMASK; static result_t PotM$Pot$init(uint8_t arg_0xa2ce970); LedsC$ledsOn = 0; static inline void TOSH_MAKE_PW0_OUTPUT(void); if (* (volatile unsigned char *)(unsigned int )& * (volatile unsigned char if (TOSH_sched_free!= TOSH_sched_full) * (volatile unsigned char *)(unsigned int )& * (volatile unsigned char static result_t HPLPotC$Pot$finalise(void); static inline void TOSH_MAKE_PW4_OUTPUT(void) TOSH_SET_RED_LED_PIN(); *)(0x0D + 0x20) & (1 << 7)) static inline void TOSH_MAKE_PW1_OUTPUT(void); *)(0x1B + 0x20) &= ~(1 << 2); nesc_atomic_end(finterruptflags); static result_t HPLPotC$Pot$decrease(void); TOSH_SET_YELLOW_LED_PIN(); return HPLPowerManagementM$IDLE; static inline void TOSH_MAKE_PW2_OUTPUT(void); TOSH_queue[tmp].tp = tp; static result_t HPLPotC$Pot$increase(void); * (volatile unsigned char *)(unsigned int )& * (volatile unsigned char TOSH_SET_GREEN_LED_PIN(); *)(0x14 + 0x20) static inline void TOSH_MAKE_PW3_OUTPUT(void); static inline result_t LedsC$Leds$redOn(void) return TRUE; static uint8_t HPLPowerManagementM$PowerManagement$adjustPower(void) = 1 << 4; static result_t HPLInit$init(void); else static inline void TOSH_MAKE_PW4_OUTPUT(void); static result_t BlinkM$StdControl$init(void); nesc_atomic_end( nesc_atomic); if (* (volatile unsigned char *)(unsigned int )& * (volatile unsigned static inline void TOSH_MAKE_PW5_OUTPUT(void); nesc_atomic_t nesc_atomic = nesc_atomic_start(); else uint8_t mcu; static inline void TOSH_MAKE_PW5_OUTPUT(void) static result_t BlinkM$StdControl$start(void); char *)(0x06 + 0x20) & (1 << 7)) static inline void TOSH_MAKE_PW6_OUTPUT(void); TOSH_sched_free = tmp; if (!HPLPowerManagementM$disabled) static result_t BlinkM$Timer$fired(void); return HPLPowerManagementM$ADC_NR; static inline void TOSH_MAKE_PW7_OUTPUT(void); TOSH_CLR_RED_LED_PIN(); nesc_atomic_end(finterruptflags); TOS_post(HPLPowerManagementM$doAdjustment); * (volatile unsigned char *)(unsigned int )& * (volatile unsigned char *)(0x14 + 0x20) static result_t TimerM$Clock$fire(void); inline static result_t BlinkM$Leds$init(void) static inline void TOSH_SET_PIN_DIRECTIONS(void ); = 1 << 5; LedsC$ledsOn = LedsC$RED_BIT; return FALSE; static result_t TimerM$StdControl$init(void); unsigned char result; else enum nesc_unnamed4248 else static result_t TimerM$StdControl$start(void); result = LedsC$Leds$init(); if (* (volatile unsigned char *)(unsigned int )& * (volatile unsigned TOSH_ADC_PORTMAPSIZE = 12 static inline void TOSH_MAKE_PW6_OUTPUT(void) char *)(0x37 + 0x20) & ((1 << 1) (1 << 0))) nesc_atomic_end( nesc_atomic); static result_t TimerM$Timer$default$fired(uint8_t arg_0xa31d660); return result; ; diff = * (volatile unsigned char *)(unsigned int )& * (volatile int main(void) mcu = * (volatile unsigned char *)(unsigned int )& * (volatile unsigned char static result_t TimerM$Timer$start(uint8_t arg_0xa31d660, char arg_0xa2fd578, *)(0x35 + 0x20); unsigned char *)(0x31 + 0x20) - * (volatile unsigned char enum nesc_unnamed4249 * (volatile unsigned char *)(unsigned int )& * (volatile unsigned char *)(0x14 + 0x20) uint32_t arg_0xa2fd6d0); = 1 << 6; static inline result_t BlinkM$StdControl$init(void) *)(unsigned int )& * (volatile unsigned char *)(0x32 + mcu &= 0xe3; TOSH_ACTUAL_CC_RSSI_PORT = 0, 0x20); static inline result_t LedsC$Leds$redOff(void) RealMain$hardwareInit(); static void HPLClock$Clock$setInterval(uint8_t arg_0xa33b8c0); mcu = HPLPowerManagementM$IDLE; TOSH_ACTUAL_VOLTAGE_PORT = 7, if (diff < 16) RealMain$Pot$init(10); static uint8_t HPLClock$Clock$readCounter(void); static inline void TOSH_MAKE_PW7_OUTPUT(void) BlinkM$Leds$init(); * (volatile unsigned char *)(unsigned int )& * (volatile unsigned char TOSH_ACTUAL_BANDGAP_PORT = 30, return HPLPowerManagementM$EXT_STANDBY; nesc_atomic_t nesc_atomic = nesc_atomic_start(); TOSH_sched_init(); *)(0x35 + 0x20) = mcu; static result_t HPLClock$Clock$setRate(char arg_0xa33adc0, char arg_0xa33af00); TOSH_ACTUAL_GND_PORT = 31 RealMain$StdControl$init(); * (volatile unsigned char *)(unsigned int )& * (volatile unsigned char static uint8_t HPLPowerManagementM$PowerManagement$adjustPower(void); * (volatile unsigned char *)(unsigned int )& * (volatile unsigned char *)(0x14 + 0x20) *)(0x35 + 0x20) = 1 << 5; ; return HPLPowerManagementM$POWER_SAVE; TOSH_SET_RED_LED_PIN(); RealMain$StdControl$start(); static result_t LedsC$Leds$init(void); = 1 << 7; static inline result_t HPLClock$Clock$setRate(char interval, char scale) enum nesc_unnamed4250 LedsC$ledsOn &= ~LedsC$RED_BIT; nesc_enable_interrupt(); static result_t LedsC$Leds$redOff(void); return 0; TOS_ADC_CC_RSSI_PORT = 0, static result_t LedsC$Leds$redToggle(void); static inline void TOSH_MAKE_CC_CHP_OUT_INPUT(void) else while (1) scale &= 0x7; TOS_ADC_VOLTAGE_PORT = 7, nesc_atomic_end( nesc_atomic); TOSH_run_task(); static result_t LedsC$Leds$redOn(void); scale = 0x8; void attribute((interrupt)) vector_15(void) TOS_ADC_BANDGAP_PORT = 10, return HPLPowerManagementM$POWER_DOWN; static result_t RealMain$hardwareInit(void); * (volatile unsigned char *)(unsigned int )& * (volatile unsigned char *)(0x1A + 0x20) &= ~(1 << 6); nesc_atomic_t nesc_atomic = nesc_atomic_start(); TOS_ADC_GND_PORT = 11 static result_t RealMain$Pot$init(uint8_t arg_0xa2ce970); ; nesc_atomic_t nesc_atomic = nesc_atomic_start(); static inline result_t LedsC$Leds$redToggle(void) static result_t RealMain$StdControl$init(void); static inline void TOSH_MAKE_GREEN_LED_OUTPUT(void) * (volatile unsigned char *)(unsigned int )& * (volatile unsigned char static result_t RealMain$StdControl$start(void); *)(0x37 + 0x20) &= ~(1 << 0); if (HPLClock$set_flag) result_t rval; * (volatile unsigned char *)(unsigned int )& * (volatile unsigned char * (volatile unsigned char *)(unsigned int )& * (volatile unsigned char *)(0x1A + 0x20) *)(0x37 + 0x20) &= ~(1 << 1); nesc_atomic_t nesc_atomic = nesc_atomic_start(); HPLClock$mscale = HPLClock$nextScale; = 1 << 1; * (volatile unsigned char *)(unsigned int )& * (volatile unsigned char HPLClock$nextScale = 0x8; *)(0x30 + 0x20) = 1 << 3; * (volatile unsigned char *)(unsigned int )& * (volatile unsigned char if (LedsC$ledsOn & LedsC$RED_BIT) static inline void TOSH_MAKE_YELLOW_LED_OUTPUT(void) * (volatile unsigned char *)(unsigned int )& * (volatile unsigned char *)(0x33 + 0x20) = HPLClock$nextScale; *)(0x33 + 0x20) = scale; rval = LedsC$Leds$redOff(); * (volatile unsigned char *)(unsigned int )& * (volatile unsigned char else * (volatile unsigned char *)(unsigned int )& * (volatile unsigned char *)(0x1A + 0x20) * (volatile unsigned char *)(unsigned int )& * (volatile unsigned char *)(0x32 + 0x20) = 0; = 1 << 0; rval = LedsC$Leds$redOn(); *)(0x31 + 0x20) = HPLClock$minterval; * (volatile unsigned char *)(unsigned int )& * (volatile unsigned char HPLClock$set_flag = 0; *)(0x31 + 0x20) = interval; static inline void TOSH_MAKE_RED_LED_OUTPUT(void) * (volatile unsigned char *)(unsigned int )& * (volatile unsigned char *)(0x37 + 0x20) = 1 << 1; nesc_atomic_end( nesc_atomic); * (volatile unsigned char *)(unsigned int )& * (volatile unsigned char *)(0x1A + 0x20) return rval; nesc_atomic_end( nesc_atomic); = 1 << 2; nesc_atomic_end( nesc_atomic); HPLClock$Clock$fire(); 1K lines of C (another 1K lines of comments) = ~1.5K bytes of assembly code CSE 466 - Winter 2007 Case Study: TinyOS 34
Blink Compiled a small piece static inline result_t LedsC$Leds$redToggle(void) result_t rval; nesc_atomic_t nesc_atomic = nesc_atomic_start(); if (LedsC$ledsOn & LedsC$RED_BIT) rval = LedsC$Leds$redOff(); else rval = LedsC$Leds$redOn(); nesc_atomic_end( nesc_atomic); return rval; inline static result_t BlinkM$Leds$redToggle(void) unsigned char result; result = LedsC$Leds$redToggle(); return result; static inline result_t BlinkM$Timer$fired(void) BlinkM$Leds$redToggle(); static inline result_t LedsC$Leds$redOn(void) nesc_atomic_t nesc_atomic = nesc_atomic_start(); TOSH_CLR_RED_LED_PIN(); LedsC$ledsOn = LedsC$RED_BIT; nesc_atomic_end( nesc_atomic); static inline result_t LedsC$Leds$redOff(void) nesc_atomic_t nesc_atomic = nesc_atomic_start(); TOSH_SET_RED_LED_PIN(); LedsC$ledsOn &= ~LedsC$RED_BIT; nesc_atomic_end( nesc_atomic); CSE 466 - Winter 2007 Case Study: TinyOS 35
Concurrency Model Asynchronous Code (AC) Any code that is reachable from an interrupt handler Synchronous Code (SC) Any code that is ONLY reachable from a task Boot sequence Potential race conditions Asynchronous Code and Synchronous Code Asynchronous Code and Asynchronous Code Non-preemption eliminates data races among tasks nesc reports potential data races to the programmer at compile time (new with version 1.1) Use atomic statement when needed async keyword is used to declare asynchronous code to compiler CSE 466 - Winter 2007 Case Study: TinyOS 36
Commands, Events, and Tasks... status = call CmdName(args)... command CmdName(args)... return status; event EvtName (args)... return status;... post TskName();...... status = signal EvtName(args)... task void TskName... CSE 466 - Winter 2007 Case Study: TinyOS 37
Split Phase Operations Component1 Event or task call command, try again if not OK Event handler Check success flag (OK, failed, etc.) Component2 Command post task and return OK, or return busy Task task executes and signals completion with event Phase I call command with parameters command either posts task to do real work or signals busy and to try again later Phase II task completes and uses event (with return parameters) to signal completion event handler checks for success (may cause re-issue of command if failed) CSE 466 - Winter 2007 Case Study: TinyOS 38
Example configuration CntToLeds implementation components Main, Counter, IntToLeds, TimerC; Main.StdControl -> IntToLeds.StdControl; Main.StdControl -> Counter.StdControl; Main.StdControl -> TimerC.StdControl; Counter.Timer -> TimerC.Timer[unique("Timer")]; Counter.IntOutput -> IntToLeds.IntOutput; M ain.nc StdControl.nc StdControl.nc Counter.nc Timer.nc IntOutput.nc StdControl.nc TimerC.nc Timer.nc IntOutput.nc IntToLeds.nc StdControl.nc CSE 466 - Winter 2007 Case Study: TinyOS 39
Exercise Which of the following goes inside the module you are implementing if we assume you are the user of the interface? NOTE: Not all of these choices are exposed through an interface. Assume those that are not exposed are implemented in your module. post taska( ); call commandb(args); signal eventc(args); taska implementation commandb implementation eventc implementation CSE 466 - Winter 2007 Case Study: TinyOS 40
Sense Application SenseM.nc module SenseM provides interface StdControl; uses interface Timer; Sense.nc interface ADC; interface StdControl as ADCControl; configuration Sense interface Leds; implementation cont d components Main, SenseM, LedsC, TimerC, DemoSensorC as Sensor; Main.StdControl -> Sensor.StdControl; Main.StdControl -> TimerC.StdControl; Main.StdControl -> SenseM.StdControl; SenseM.ADC -> Sensor.ADC; SenseM.ADCControl -> Sensor.StdControl; SenseM.Leds -> LedsC.Leds; SenseM.Timer -> TimerC.Timer[unique("Timer")]; DemoSensorC.nc configuration DemoSensorC provides interface ADC; provides interface StdControl; implementation components Photo as Sensor; StdControl = Sensor; ADC = Sensor; CSE 466 - Winter 2007 Case Study: TinyOS 41
SenseM.nc cont d implementation /* Module scoped method. Displays the lowest 3 bits to the LEDs, with RED being the most signficant and YELLOW being the least significant */ result_t display(uint16_t value) if (value &1) call Leds.yellowOn(); else call Leds.yellowOff(); if (value &2) call Leds.greenOn(); else call Leds.greenOff(); if (value &4) call Leds.redOn(); else call Leds.redOff(); command result_t StdControl.init() return call Leds.init(); command result_t StdControl.start() return call Timer.start(TIMER_REPEAT, 500); command result_t StdControl.stop() return call Timer.stop(); event result_t Timer.fired() return call ADC.getData(); async event result_t ADC.dataReady(uint16_t data) display(7-((data>>7) &0x7)); CSE 466 - Winter 2007 Case Study: TinyOS 42
Sense Application Using Task module SenseTaskM provides interface StdControl; uses interface Timer; SenseTask.nc interface ADC; interface Leds; configuration SenseTask implementation cont d components Main, SenseTaskM, LedsC, TimerC, DemoSensorC as Sensor; Main.StdControl -> TimerC; Main.StdControl -> Sensor; Main.StdControl -> SenseTaskM; SenseM.nc SenseTaskM.Timer -> TimerC.Timer[unique("Timer")]; SenseTaskM.ADC -> Sensor; SenseTaskM.Leds -> LedsC; CSE 466 - Winter 2007 Case Study: TinyOS 43
SenseTaskM.nc implementation enum log2size = 3, // log2 of buffer size size=1 << log2size, // circular buffer size sizemask=size - 1, // bit mask ; int8_t head; // head index int16_t rdata[size]; // circular buffer inline void putdata(int16_t val) int16_t p; atomic p = head; head = (p+1) & sizemask; rdata[p] = val; result_t display(uint16_t value) if (value &1) call Leds.yellowOn(); else call Leds.yellowOff(); if (value &2) call Leds.greenOn(); else call Leds.greenOff(); if (value &4) call Leds.redOn(); else call Leds.redOff(); task void processdata() int16_t i, sum=0; atomic for (i=0; i<size; i++) sum += (rdata[i] >> 7); display(sum >> log2size); command result_t StdControl.init() atomic head = 0; return call Leds.init(); command result_t StdControl.start() return call Timer.start(TIMER_REPEAT, 500); command result_t StdControl.stop() return call Timer.stop(); event result_t Timer.fired() return call ADC.getData(); async event result_t ADC.dataReady(uint16_t data) putdata(data); post processdata(); CSE 466 - Winter 2007 Case Study: TinyOS 44
A More Extensive Application application Route map Router Sensor Application Active Messages bit byte packet Radio Packet Radio Byte RFM Serial Packet UART Temp ADC Photo Clocks SW HW CSE 466 - Winter 2007 Case Study: TinyOS 45