From 2b44d5842b2810c916bba054e6aaba9bd79dfbf7 Mon Sep 17 00:00:00 2001 From: prabal Date: Fri, 6 Apr 2007 01:13:59 +0000 Subject: [PATCH] New tutorial --- apps/tutorials/BlinkConfig/BlinkConfigAppC.nc | 23 +++-- apps/tutorials/BlinkConfig/BlinkConfigC.nc | 96 ++++++++++--------- apps/tutorials/BlinkConfig/README.txt | 28 +++++- 3 files changed, 86 insertions(+), 61 deletions(-) diff --git a/apps/tutorials/BlinkConfig/BlinkConfigAppC.nc b/apps/tutorials/BlinkConfig/BlinkConfigAppC.nc index f990b6cc..ee271cda 100644 --- a/apps/tutorials/BlinkConfig/BlinkConfigAppC.nc +++ b/apps/tutorials/BlinkConfig/BlinkConfigAppC.nc @@ -24,14 +24,14 @@ * UPDATES, ENHANCEMENTS, OR MODIFICATIONS." */ #include "StorageVolumes.h" +#include "Timer.h" /** - * Application to demonstrate the ConfigStorageC abstraction. A value - * is written to, and read from, the flash storage. A successful test - * will turn on both the green and blue (yellow) LEDs. A failed test - * is any other LED configuration. + * Application to demonstrate the ConfigStorageC abstraction. A timer + * period is read from flash, divided by two, and written back to + * flash. An LED is toggled each time the timer fires. * - * @author Prabal Dutta + * @author Prabal Dutta */ configuration BlinkConfigAppC { } @@ -39,12 +39,11 @@ implementation { components BlinkConfigC as App; components new ConfigStorageC(VOLUME_CONFIGTEST); components MainC, LedsC, PlatformC, SerialActiveMessageC; + components new TimerMilliC() as Timer0; - App.Boot -> MainC.Boot; - - App.AMControl -> SerialActiveMessageC; - App.AMSend -> SerialActiveMessageC.AMSend[1]; - App.Config -> ConfigStorageC.ConfigStorage; - App.Mount -> ConfigStorageC.Mount; - App.Leds -> LedsC; + App.Boot -> MainC.Boot; + App.Config -> ConfigStorageC.ConfigStorage; + App.Mount -> ConfigStorageC.Mount; + App.Leds -> LedsC; + App.Timer0 -> Timer0; } diff --git a/apps/tutorials/BlinkConfig/BlinkConfigC.nc b/apps/tutorials/BlinkConfig/BlinkConfigC.nc index 91853c7d..13cb987f 100644 --- a/apps/tutorials/BlinkConfig/BlinkConfigC.nc +++ b/apps/tutorials/BlinkConfig/BlinkConfigC.nc @@ -25,39 +25,40 @@ */ /** - * Application to demonstrate the ConfigStorageC abstraction. A value - * is written to, and read from, the flash storage. A successful test - * will turn on both the green and blue (yellow) LEDs. A failed test - * is any other LED combination.. + * Application to demonstrate the ConfigStorageC abstraction. A timer + * period is read from flash, divided by two, and written back to + * flash. An LED is toggled each time the timer fires. * - * @author Prabal Dutta + * @author Prabal Dutta */ +#include + module BlinkConfigC { uses { interface Boot; interface Leds; interface ConfigStorage as Config; - interface AMSend; - interface SplitControl as AMControl; interface Mount as Mount; + interface Timer as Timer0; } } implementation { - uint16_t period = 2048; - uint16_t period2 = 1024; + + typedef struct config_t { + uint16_t version; + uint16_t period; + } config_t; enum { CONFIG_ADDR = 0, + CONFIG_VERSION = 1, + DEFAULT_PERIOD = 1024 }; - event void Boot.booted() { - call AMControl.start(); - } + uint8_t state; + config_t conf; - event void AMControl.startDone(error_t error) { - if (error != SUCCESS) { - call AMControl.start(); - } + event void Boot.booted() { if (call Mount.mount() != SUCCESS) { // Handle failure } @@ -68,50 +69,57 @@ implementation { // Handle failure } else{ - call Config.write(CONFIG_ADDR, &period, sizeof(period)); + if (call Config.read(CONFIG_ADDR, &conf, sizeof(conf)) != SUCCESS) { + // Handle failure + } } } - event void Config.writeDone(storage_addr_t addr, void *buf, - storage_len_t len, error_t result) { - // Verify addr and len + event void Config.readDone(storage_addr_t addr, void* buf, + storage_len_t len, error_t err) __attribute__((noinline)) { - if (result == SUCCESS) { - // Note success + if (err == SUCCESS) { + memcpy(&conf, buf, len); + if (conf.version == CONFIG_VERSION) { + conf.period = conf.period > 128 ? conf.period/2 : DEFAULT_PERIOD; + } + else { + // Version mismatch. Restore default. + call Leds.led1On(); + conf.version = CONFIG_VERSION; + conf.period = DEFAULT_PERIOD; + } + call Leds.led0On(); + call Config.write(CONFIG_ADDR, &conf, sizeof(conf)); } else { - // Handle failure - } - if (call Config.commit() != SUCCESS) { - // Handle failure + // Handle failure. } } - event void Config.commitDone(error_t error) { - if (call Config.read(CONFIG_ADDR, &period2, sizeof(period2)) != SUCCESS) { - // Handle failure - } - } - - event void Config.readDone(storage_addr_t addr, void* buf, - storage_len_t len, error_t result) __attribute__((noinline)) { - memcpy(&period2, buf, len); + event void Config.writeDone(storage_addr_t addr, void *buf, + storage_len_t len, error_t err) { + // Verify addr and len - if (period == period2) { - call Leds.led2On(); + if (err == SUCCESS) { + if (call Config.commit() != SUCCESS) { + // Handle failure + } } - - if (len == 2 && addr == CONFIG_ADDR) { - call Leds.led1On(); + else { + // Handle failure } } - event void AMSend.sendDone(message_t* msg, error_t error) { - if (error != SUCCESS) { - call Leds.led0On(); + event void Config.commitDone(error_t err) { + call Leds.led0Off(); + call Timer0.startPeriodic(conf.period); + if (err == SUCCESS) { + // Handle failure } } - event void AMControl.stopDone(error_t error) { + event void Timer0.fired() { + call Leds.led2Toggle(); } } diff --git a/apps/tutorials/BlinkConfig/README.txt b/apps/tutorials/BlinkConfig/README.txt index cd4301f1..07de8285 100644 --- a/apps/tutorials/BlinkConfig/README.txt +++ b/apps/tutorials/BlinkConfig/README.txt @@ -1,15 +1,33 @@ -$Id$ +$Id$: README.txt,v 1.5 2006/12/12 18:22:52 vlahan Exp $ README for Config Author/Contact: tinyos-help@millennium.berkeley.edu Description: -Application to demonstrate the ConfigStorageC abstraction. A value is -written to, and read from, the flash storage. + Application to demonstrate the ConfigStorageC abstraction. A timer + period is read from flash, divided by two, and written back to + flash. An LED is toggled each time the timer fires. -A successful test will turn on both the green and blue (yellow) -LEDs. A failed test is any other LED combination. + To use this application: + + (i) Program a mote with this application (e.g. make telos install) + (ii) Wait until the red LED turns off (writing to flash is done) + (iii) Power cycle the mote and wait until the red LED turns off. + (iv) Repeat step (iii) and notice that the blink rate of the blue + (yellow) LED doubles each time the mote is power cycled. The + blink rate cycles through the following values: 1Hz, 2Hz, 4Hz, + and 8Hz. + + The first time this application is installed, the green LED will + light up and remain on (indicating that the configuration storage + volume did not have the expected version number). + + The red LED will remain lit during the flash write/commit operation. + + The blue (yellow) LED blinks at the period stored and read from flash. + + See Lesson 7 for details. Tools: -- 2.39.2