Here's an example I whooped up for you, with comments throughout on the
behavior of BlockStorage. Hopefully everyone will find this useful.

And I also hope it works without too many corrections, or at least it may
get some concepts across.  I haven't tried to compile it.

Make sure you format the flash using BlockStorage's format application (or
some custom variant of it) first before
you try using BlockStorage.  Also, search this mailing list for previous
discussions on BlockStorage.

-David



/** WriteToFlash.h header file - set some stuff up */
/////////////////////////////////////////////////////

enum {
  // Keep each interface connected to your module using the same
paramaterized interface number!
  BLOCKSTORAGE_WRITETOFLASH = unique("BlockStorage"),
};

//////////////////////////////////////////////////eof





/** Configuration file */
/////////////////////////////////////////////////////

includes WriteToFlash;

configuration WriteToFlashC {
}

implementation {
  components Main, WriteToFlashM, BlockStorageC;

  Main.StdControl -> WriteToFlashM;

  WriteToFlashM.Mount -> BlockStorageC.Mount[BLOCKSTORAGE_WRITETOFLASH];
  WriteToFlashM.BlockWrite ->
BlockStorageC.BlockWrite[BLOCKSTORAGE_WRITETOFLASH];
  WriteToFlashM.BlockRead ->
BlockStorageC.BlockRead[BLOCKSTORAGE_WRITETOFLASH];
}


//////////////////////////////////////////////////eof




/** This is the actual module you should be interested in */
/////////////////////////////////////////////////////

module WriteToFlashM {
  provides {
    interface StdControl;
  }
  uses {
    interface Mount;
    interface BlockWrite;
    interface BlockRead;
  }
}

implementation {

  /** String to write to flash */
  char myString[] = "Hello BlockStorage World!";

  /** Something to read into as an example */
  char readBuffer[256];


  /***************** Prototypes *****************/
  result_t checkStorageResult(storage_result_t result);


  /***************** StdControl Commands ****************/
  command result_t StdControl.init() {
    return SUCCESS; 
  }

  command result_t StdControl.start() {
                          // Only mount to a volume after start().
    call Mount.mount(0);  // mount to volume 0 set by the standalone format
app run earlier.
    return SUCCESS;
  }

  command result_t StdControl.stop() {
    return SUCCESS;
  }


  /***************** Mount Events ****************/
  event void Mount.mountDone(storage_result_t result, volume_id_t id) {
    // Only now you can write and read flash!
    // Address 0x0 is the base address of the volume you're mounted to.
    // There are no limitations to the addresses you can read or write, even
outside
    // of the current volume.
    // When issuing a BlockStorage command, always wait for the event
    // before trying to issue another command.
    // Let's write to flash...

    if(!checkStorageResult(result)) {
      // woops, did you format the flash correctly? with volume 0?
      // Maybe flip on your red led here and find out.
      return;
    }

    if(!call BlockWrite.write(0x0, &myString, sizeof(myString)) {
      // Boy, you must have really screwed something up
      // We mounted ok, so this error would be extremely unlikely 
      // unless some other component tried to use the BlockStorage
interface.
    }
  }


  /***************** BlockWrite Events ****************/
  event void BlockWrite.writeDone(storage_result_t result, block_addr_t
addr, void* buf, block_addr_t len) {
    if(checkStorageResult(result)) {
      // hooray, we wrote to flash. Let's read it now.
      if(!call BlockRead.read(0x0, &readBuffer, sizeof(myString))) {
        // unlikely error.
      }
    }
  }

  event void BlockWrite.eraseDone(storage_result_t result) {
    // Full sectors are erased on the M25P80 telos flash type.
    // That's 64kB to erase at one time!
    // Takes about 1 solid second to erase a sector.
    // AT45DB flash type only erases the first page of flash
    // no matter where you're mounted (I need to verify this)
    // The sector you are erasing on the M25P80 flash is the
    // volume you're mounted to.
  } 

  event void BlockWrite.commitDone(storage_result_t result) {
    // Stubbed out for the STM25P80 flash type on telos motes, but
    // totally in use for the AT45DB041B flash type on the mica2, mica2dot,
micaz, 
    // etc. motes.  For cross platform compatibility, commit your data
    // when that data really needs to exist on flash.
  }

  /***************** BlockRead Events ****************/
  event void BlockRead.readDone(storage_result_t result, block_addr_t addr,
void* buf, block_addr_t len) {
    if(checkStorageResult(result)) {
      // Hooray! we read from flash.
      // your readBuffer should contain the same value as your myString
array.
      // green led's on, you're good to go.
    }
  }

  event void BlockRead.verifyDone(storage_result_t result) {
    // never used it...
  }

  event void BlockRead.computeCrcDone(storage_result_t result, uint16_t crc,
block_addr_t addr, block_addr_t len) {
    // Reads the given range of flash addresses and computes the 16-bit crc.
  }


  /***************** Functions ****************/ 
  // this function will convert the storage_result_t results into
  // simple SUCCESS or FAILs for fast and careless error checking:
  result_t checkStorageResult(storage_result_t result) {
    if(result == STORAGE_OK) {
      return SUCCESS;
    } else {
      return FAIL;
    }
  }
}
//////////////////////////////////////////////////eof






-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of L Tony
Sent: Monday, March 06, 2006 3:20 PM
To: [email protected]
Subject: [Tinyos-help] write/read from flash on telosb mote 


Hi,

I use telosb mote. In my project, I use a t-mote to receive a sequence of 
packets. For every received packet, the mote writes some value (like RSSI) 
to the mote's flash. Then I connect the mote to laptop and read all the 
bytes from flash. I searched the archive and found I should use the updated 
BlockStorage instead of ByteEEPROM. But I don't know how the BlockStorage 
works. Are there any example application using BlockStorage? Thanks.

Best regards,
Tony

_________________________________________________________________
享用世界上最大的电子邮件系统― MSN Hotmail。  http://www.hotmail.com  

_______________________________________________
Tinyos-help mailing list
[email protected]
https://mail.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help


_______________________________________________
Tinyos-help mailing list
[email protected]
https://mail.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help

Reply via email to