[Tinyos-2-commits] CVS: tinyos-2.x/tos/lib/TOSBoot Deluge.h, NONE, 1.1 Exec.nc, NONE, 1.1 ExtFlash.nc, NONE, 1.1 Hardware.nc, NONE, 1.1 Leds.nc, NONE, 1.1 Makefile, NONE, 1.1 ProgFlash.nc, NONE, 1.1 TOSBoot.h, NONE, 1.1 TOSBoot.nc, NONE, 1.1 TOSBootM.nc, NONE, 1.1 Voltage.nc, NONE, 1.1 crc.h, NONE, 1.1

Razvan Musaloiu-E. razvanm at users.sourceforge.net
Tue May 22 13:34:23 PDT 2007


Update of /cvsroot/tinyos/tinyos-2.x/tos/lib/TOSBoot
In directory sc8-pr-cvs10.sourceforge.net:/tmp/cvs-serv24715/tos/lib/TOSBoot

Added Files:
	Deluge.h Exec.nc ExtFlash.nc Hardware.nc Leds.nc Makefile 
	ProgFlash.nc TOSBoot.h TOSBoot.nc TOSBootM.nc Voltage.nc crc.h 
Log Message:
Initial commit of the Deluge T2. Some notes:
- TOSBoot includes some code to allow the MicaZ version to compile but it's not support for it it's not done yet.
- tools/tinyos/misc/Makefile.am is not updated yet so the new tools will not be installed by default.


--- NEW FILE: Deluge.h ---
// $Id: Deluge.h,v 1.1 2007/05/22 20:34:20 razvanm Exp $

/*									tab:2
 *
 *
 * "Copyright (c) 2000-2005 The Regents of the University  of California.  
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice, the following
 * two paragraphs and the author appear in all copies of this software.
 * 
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
 *
 */

/**
 * @author Jonathan Hui <jwhui at cs.berkeley.edu>
 */

#ifndef __DELUGE_H__
#define __DELUGE_H__

#include "DelugeMetadata.h"
#include "TOSBoot.h"

#ifndef DELUGE_NUM_IMAGES
#define DELUGE_NUM_IMAGES 3
#endif

enum {
  DELUGE_VERSION                    = 2,
  DELUGE_MAX_ADV_PERIOD_LOG2        = 22,
  DELUGE_NUM_NEWDATA_ADVS_REQUIRED  = 2,
  DELUGE_NUM_MIN_ADV_PERIODS        = 2,
  DELUGE_MAX_NUM_REQ_TRIES          = 1,
  DELUGE_REBOOT_DELAY               = 4,
  DELUGE_FAILED_SEND_DELAY          = 16,
  DELUGE_MIN_DELAY                  = 16,
  DELUGE_PKTS_PER_PAGE              = 48,
  DELUGE_PKT_PAYLOAD_SIZE           = 23,
  DELUGE_DATA_OFFSET                = 128,
  DELUGE_IDENT_SIZE                 = 128,
  DELUGE_INVALID_ADDR               = (0x7fffffffL),
  DELUGE_MAX_REQ_DELAY              = (0x1L << (DELUGE_MIN_ADV_PERIOD_LOG2-1)),
  DELUGE_NACK_TIMEOUT               = (DELUGE_MAX_REQ_DELAY >> 0x1),
  DELUGE_BYTES_PER_PAGE             = (DELUGE_PKTS_PER_PAGE*DELUGE_PKT_PAYLOAD_SIZE),
  DELUGE_PKT_BITVEC_SIZE            = (((DELUGE_PKTS_PER_PAGE-1) / 8) + 1),
  DELUGE_MAX_IMAGE_SIZE             = (128L*1024L),
  DELUGE_MAX_PAGES                  = 128,
  DELUGE_CRC_SIZE                   = sizeof(uint16_t),
  DELUGE_CRC_BLOCK_SIZE             = DELUGE_MAX_PAGES*DELUGE_CRC_SIZE,
  DELUGE_GOLDEN_IMAGE_NUM           = 0x0,
  DELUGE_INVALID_VNUM               = -1,
  DELUGE_INVALID_IMGNUM             = 0xff,
  DELUGE_INVALID_PKTNUM             = 0xff,
  DELUGE_INVALID_PGNUM              = 0xff,
};

#include "Storage.h"

struct deluge_image_t {
  imgnum_t imageNum;
  volume_id_t volumeId;
};

typedef struct DelugeAdvTimer {
  uint32_t timer      : 32;
  uint8_t  periodLog2 : 8;
  bool     overheard  : 1;
  uint8_t  newAdvs    : 7;
} DelugeAdvTimer;

typedef struct DelugeNodeDesc {
  imgvnum_t vNum;
  uint32_t  uid;
  imgnum_t  imgNum;
  uint8_t   reserved;
  uint16_t  crc;
} DelugeNodeDesc;

enum {
  DELUGE_VOLUME_ID_0 = unique("StorageManager"),
#if DELUGE_NUM_IMAGES >= 2
  DELUGE_VOLUME_ID_1 = unique("StorageManager"),
#if DELUGE_NUM_IMAGES >= 3
  DELUGE_VOLUME_ID_2 = unique("StorageManager"),
#if DELUGE_NUM_IMAGES >= 4
  DELUGE_VOLUME_ID_3 = unique("StorageManager"),
#if DELUGE_NUM_IMAGES >= 5
  DELUGE_VOLUME_ID_4 = unique("StorageManager"),
#if DELUGE_NUM_IMAGES >= 6
  DELUGE_VOLUME_ID_5 = unique("StorageManager"),
#if DELUGE_NUM_IMAGES >= 7
  DELUGE_VOLUME_ID_6 = unique("StorageManager"),
#if DELUGE_NUM_IMAGES >= 8
  DELUGE_VOLUME_ID_7 = unique("StorageManager"),
#endif
#endif
#endif
#endif
#endif
#endif
#endif
};

static const struct deluge_image_t DELUGE_IMAGES[DELUGE_NUM_IMAGES] = {
  { DELUGE_VOLUME_ID_0, 0xDF },
#if DELUGE_NUM_IMAGES >= 2
  { DELUGE_VOLUME_ID_1, 0xD0 },
#if DELUGE_NUM_IMAGES >= 3
  { DELUGE_VOLUME_ID_2, 0xD1 },
#if DELUGE_NUM_IMAGES >= 4
  { DELUGE_VOLUME_ID_3, 0xD2 },
#if DELUGE_NUM_IMAGES >= 5
  { DELUGE_VOLUME_ID_4, 0xD3 },
#if DELUGE_NUM_IMAGES >= 6
  { DELUGE_VOLUME_ID_5, 0xD4 },
#if DELUGE_NUM_IMAGES >= 7
  { DELUGE_VOLUME_ID_6, 0xD5 },
#if DELUGE_NUM_IMAGES >= 8
  { DELUGE_VOLUME_ID_7, 0xD6 },
#endif
#endif
#endif
#endif
#endif
#endif
#endif
};

#endif

--- NEW FILE: Exec.nc ---

interface Exec {
  command void exec();
}

--- NEW FILE: ExtFlash.nc ---
// $Id: ExtFlash.nc,v 1.1 2007/05/22 20:34:20 razvanm Exp $

/*									tab:2
 *
 *
 * "Copyright (c) 2000-2005 The Regents of the University  of California.  
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice, the following
 * two paragraphs and the author appear in all copies of this software.
 * 
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
 *
 */

/**
 * @author Jonathan Hui <jwhui at cs.berkeley.edu>
 */

interface ExtFlash {
  command void startRead(uint32_t addr);
  command uint8_t  readByte();
  command void stopRead();
}

--- NEW FILE: Hardware.nc ---
// $Id: Hardware.nc,v 1.1 2007/05/22 20:34:20 razvanm Exp $

/*									tab:2
 *
 *
 * "Copyright (c) 2000-2005 The Regents of the University  of California.  
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice, the following
 * two paragraphs and the author appear in all copies of this software.
 * 
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
 *
 */

/**
 * @author Jonathan Hui <jwhui at cs.berkeley.edu>
 */

interface Hardware {
  command void init();
  command void reboot();
}

--- NEW FILE: Leds.nc ---
// $Id: Leds.nc,v 1.1 2007/05/22 20:34:20 razvanm Exp $

/*									tab:2
 *
 *
 * "Copyright (c) 2000-2004 The Regents of the University  of California.  
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice, the following
 * two paragraphs and the author appear in all copies of this software.
 * 
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
 *
 */

/**
 * @author Jonathan Hui <jwhui at cs.berkeley.edu>
 */

interface Leds {
  command void set(uint8_t ledsOn);
  command void flash(uint8_t a);
  command void glow(uint8_t a, uint8_t b);
}

--- NEW FILE: Makefile ---
# $Id: Makefile,v 1.1 2007/05/22 20:34:20 razvanm Exp $

#									tab:2
#
#
# "Copyright (c) 2000-2005 The Regents of the University  of California.  
# All rights reserved.
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose, without fee, and without written agreement is
# hereby granted, provided that the above copyright notice, the following
# two paragraphs and the author appear in all copies of this software.
# 
# IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
# DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
# OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
# CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# 
# THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
# ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
# PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
#
#

#
# @author Jonathan Hui <jwhui at cs.berkeley.edu>
#

COMPONENT=TOSBoot
TINYOS_NP=

PFLAGS += -DNESC_BUILD_BINARY

# ifeq ($(MAKECMDGOALS),mica2)
#   CFLAGS += -DTOSBOOT_START=0x1f000 -DTOSBOOT_END=0x20000
#   CFLAGS += -Wl,--section-start=.text=0x1f000
#   CFLAGS += -Iat45db -Iavr -Imica2 -Ilib
# endif

# ifeq ($(MAKECMDGOALS),mica2dot)
#   CFLAGS += -DTOSBOOT_START=0x1f000 -DTOSBOOT_END=0x20000
#   CFLAGS += -Wl,--section-start=.text=0x1f000
#   CFLAGS += -Iat45db -Iavr -Imica2dot -Ilib
# endif

ifeq ($(MAKECMDGOALS),micaz)
  CFLAGS += -DTOSBOOT_START=0x1f000 -DTOSBOOT_END=0x20000
  CFLAGS += -Wl,--section-start=.text=0x1f000
  CFLAGS += -Iat45db -Iavr -Imicaz -Ilib
  CFLAGS += -I../net/Deluge
endif

# ifeq ($(MAKECMDGOALS),telosa)
#   CFLAGS += -DTOSBOOT_START=0x1100 -DTOSBOOT_END=0x1a00
#   CFLAGS += -Iat45db -Imsp430 -Itelos -Ilib
#   POST_BUILD_EXTRA_DEPS += strip_iv
# endif

ifeq ($(MAKECMDGOALS),telosb)
  CFLAGS += -DTOSBOOT_START=0x4000 -DTOSBOOT_END=0x4a00
  CFLAGS += -Imsp430 -Istm25p -Itelosb -Ilib
  CFLAGS += -I../net/Deluge
  POST_BUILD_EXTRA_DEPS += strip_iv
endif

strip_iv: FORCE
	@echo "    removing interrupt vector from binary"
	msp430-objcopy -j .text -j .data -O ihex $(BUILDDIR)/main.exe $(BUILDDIR)/main.ihex

include $(MAKERULES)

--- NEW FILE: ProgFlash.nc ---
// $Id: ProgFlash.nc,v 1.1 2007/05/22 20:34:20 razvanm Exp $

/*									tab:2
 *
 *
 * "Copyright (c) 2000-2005 The Regents of the University  of California.  
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice, the following
 * two paragraphs and the author appear in all copies of this software.
 * 
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
 *
 */

/**
 * @author Jonathan Hui <jwhui at cs.berkeley.edu>
 */

interface ProgFlash {
  command error_t write(in_flash_addr_t addr, uint8_t* buf, in_flash_addr_t len);
}

--- NEW FILE: TOSBoot.h ---
// $Id: TOSBoot.h,v 1.1 2007/05/22 20:34:20 razvanm Exp $

/*									tab:2
 * "Copyright (c) 2000-2005 The Regents of the University  of California.  
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice, the following
 * two paragraphs and the author appear in all copies of this software.
 * 
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
 */

/**
 * @author  Jonathan Hui <jwhui at cs.berkeley.edu>
 */

#ifndef __TOSBOOT_H__
#define __TOSBOOT_H__

#include "TOSBoot_platform.h"

typedef struct tosboot_args_t {
  uint32_t imageAddr;
  uint8_t  gestureCount;
  bool     noReprogram;
} tosboot_args_t;

#endif

--- NEW FILE: TOSBoot.nc ---
// $Id: TOSBoot.nc,v 1.1 2007/05/22 20:34:21 razvanm Exp $

/*									tab:2
 *
 *
 * "Copyright (c) 2000-2005 The Regents of the University  of California.  
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice, the following
 * two paragraphs and the author appear in all copies of this software.
 * 
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
 *
 */

/**
 * @author Jonathan Hui <jwhui at cs.berkeley.edu>
 */

includes Deluge;
includes TOSBoot;

configuration TOSBoot {
}
implementation {

  components
    TOSBootM,
    ExecC,
    ExtFlashC,
    HardwareC,
    InternalFlashC as IntFlash,
    LedsC,
    PluginC,
    ProgFlashM as ProgFlash,
    VoltageC;

  TOSBootM.SubInit -> ExtFlashC;
  TOSBootM.SubControl -> ExtFlashC.StdControl;
  TOSBootM.SubControl -> PluginC;

  TOSBootM.Exec -> ExecC;
  TOSBootM.ExtFlash -> ExtFlashC;
  TOSBootM.Hardware -> HardwareC;
  TOSBootM.IntFlash -> IntFlash;
  TOSBootM.Leds -> LedsC;
  TOSBootM.ProgFlash -> ProgFlash;
  TOSBootM.Voltage -> VoltageC;

}

--- NEW FILE: TOSBootM.nc ---
/*
 * "Copyright (c) 2000-2005 The Regents of the University  of California.  
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice, the following
 * two paragraphs and the author appear in all copies of this software.
 * 
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
 *
 * Copyright (c) 2007 Johns Hopkins University.
 * All rights reserved.
 *
 */

/**
 * @author Jonathan Hui <jwhui at cs.berkeley.edu>
 * @author Chieh-Jan Mike Liang <cliang4 at cs.jhu.edu>
 * @author Razvan Musaloiu-E. <razvanm at cs.jhu.edu>
 */

includes crc;
includes hardware;

module TOSBootM {
  uses {
    interface Exec;
    interface ExtFlash;
    interface Hardware;
    interface InternalFlash as IntFlash;
    interface Leds;
    interface ProgFlash;
    interface StdControl as SubControl;
    interface Init as SubInit;
    interface Voltage;
  }
}
implementation {

  enum {
    LEDS_LOWBATT = 1,
    LEDS_GESTURE = 7,
  };

  enum {
    R_SUCCESS,
    R_INVALID_IMAGE_ERROR,
    R_PROGRAMMING_ERROR,
  };

  void startupLeds() {

    uint8_t  output = 0x7;
    uint8_t  i;

    for (i = 3; i; i--, output >>= 1 )
      call Leds.glow(output, output >> 1);

  }

  in_flash_addr_t extFlashReadAddr() {
    in_flash_addr_t result = 0;
    uint8_t  i;
    for ( i = 0; i < 4; i++ )
      result |= ((in_flash_addr_t)call ExtFlash.readByte() & 0xff) << (i*8);    
    return result;
  }

  bool verifyImage(ex_flash_addr_t startAddr) {

    uint16_t crcTarget = 0, crcTmp = 0;
    uint16_t addr, len;
    pgnum_t  numPgs;
    uint8_t  i;

    // read size of image
    call ExtFlash.startRead(startAddr + offsetof(DelugeImgDesc,numPgs));
    numPgs = call ExtFlash.readByte();
    call ExtFlash.stopRead();

    if (numPgs == 0 || numPgs == 0xff)
      return FALSE;

    startAddr += DELUGE_METADATA_SIZE;

    addr = DELUGE_CRC_BLOCK_SIZE;
    len = DELUGE_BYTES_PER_PAGE-DELUGE_CRC_BLOCK_SIZE;

    for ( i = 0; i < numPgs && crcTarget == crcTmp; i++ ) {

      // read crc
      call ExtFlash.startRead(startAddr + i*sizeof(uint16_t));
      crcTarget = (uint16_t)(call ExtFlash.readByte() & 0xff);
      crcTarget |= (uint16_t)(call ExtFlash.readByte() & 0xff) << 8;
      call ExtFlash.stopRead();

      // compute crc
      call ExtFlash.startRead(startAddr + addr);
      for ( crcTmp = 0; len; len-- )
	crcTmp = crcByte(crcTmp, call ExtFlash.readByte());
      call ExtFlash.stopRead();

      addr = (uint16_t)(i+1)*DELUGE_BYTES_PER_PAGE;
      len = DELUGE_BYTES_PER_PAGE;
    }

    return (i == numPgs) && (crcTarget == crcTmp);
  }

  error_t programImage(ex_flash_addr_t startAddr) {

    uint8_t  buf[TOSBOOT_INT_PAGE_SIZE];
    uint16_t pageAddr, newPageAddr;
    in_flash_addr_t intAddr;
    in_flash_addr_t secLength;
    ex_flash_addr_t curAddr;

    if (!verifyImage(startAddr))
      return R_INVALID_IMAGE_ERROR;

    curAddr = startAddr + DELUGE_METADATA_SIZE + DELUGE_CRC_BLOCK_SIZE + DELUGE_IDENT_SIZE;

    call ExtFlash.startRead(curAddr);

    intAddr = extFlashReadAddr();
    secLength = extFlashReadAddr();
    curAddr = curAddr + 8;

    // check that the image starts on the correct boundary
    if (intAddr != TOSBOOT_END) {
      call ExtFlash.stopRead();
      return R_INVALID_IMAGE_ERROR;
    }

    while ( secLength ) {
      
      pageAddr = newPageAddr = intAddr / TOSBOOT_INT_PAGE_SIZE;

      call ExtFlash.startRead(curAddr);
      // fill in ram buffer for internal program flash sector
      do {

	// check if secLength is all ones
	if ( secLength == 0xffffffff )
	  return FAIL;

	buf[(uint16_t)intAddr % TOSBOOT_INT_PAGE_SIZE] = call ExtFlash.readByte();
	intAddr++; curAddr++;
	
	if ( --secLength == 0 ) {
	  intAddr = extFlashReadAddr();
	  secLength = extFlashReadAddr();
	  curAddr = curAddr + 8;
	}

	newPageAddr = intAddr / TOSBOOT_INT_PAGE_SIZE;

      } while ( pageAddr == newPageAddr && secLength );
      call ExtFlash.stopRead();

      call Leds.set(pageAddr);


      // write out page
      if (call ProgFlash.write(pageAddr*TOSBOOT_INT_PAGE_SIZE, buf,
			       TOSBOOT_INT_PAGE_SIZE) == FAIL)
	return R_PROGRAMMING_ERROR;

    }

    return R_SUCCESS;

  }

  void runApp() {
    call SubControl.stop();
    call Exec.exec();
  }

  void startupSequence() {

    tosboot_args_t args;

    // check voltage and make sure flash can be programmed
    //   if not, just run the app, can't check for gestures
    //   if we can't write to the internal flash anyway
    if ( !call Voltage.okToProgram() ) {
      // give user some time and count down LEDs
      call Leds.flash(LEDS_LOWBATT);
      startupLeds();
      runApp();
    }

    // get current value of counter
    call IntFlash.read((uint8_t*)TOSBOOT_ARGS_ADDR, &args, sizeof(args));

    // increment gesture counter, see if it exceeds threshold
    if ( ++args.gestureCount >= TOSBOOT_GESTURE_MAX_COUNT - 1 ) {
      // gesture has been detected, display receipt of gesture on LEDs
      call Leds.flash(LEDS_GESTURE);

      // load golden image from flash
      // if the golden image is invalid, forget about reprogramming
      // if an error happened during reprogramming, reboot and try again
      //   not much else we can do :-/
      if (programImage(TOSBOOT_GOLDEN_IMG_ADDR) == R_PROGRAMMING_ERROR)
	call Hardware.reboot();
    }
    else {
      // update gesture counter
      call IntFlash.write((uint8_t*)TOSBOOT_ARGS_ADDR, &args, sizeof(args));
      if ( !args.noReprogram ) {
	// if an error happened during reprogramming, reboot and try again
	//   after two tries, try programming the golden image
	if (programImage(args.imageAddr) == R_PROGRAMMING_ERROR) {
	  call Hardware.reboot();
	}
      }
    }

    // give user some time and count down LEDs
    startupLeds();

    // reset counter and reprogramming flag
    args.gestureCount = 0xff;
    args.noReprogram = TRUE;
    call IntFlash.write((uint8_t*)TOSBOOT_ARGS_ADDR, &args, sizeof(args));
    
    runApp();

  }

  int main() __attribute__ ((C, spontaneous)) {

    __nesc_disable_interrupt();

    TOSH_SET_PIN_DIRECTIONS();
    call Hardware.init();

    call SubInit.init();
    call SubControl.start();

    startupSequence();

    return 0;

  }

}

--- NEW FILE: Voltage.nc ---
// $Id: Voltage.nc,v 1.1 2007/05/22 20:34:21 razvanm Exp $

/*									tab:2
 *
 *
 * "Copyright (c) 2000-2004 The Regents of the University  of California.  
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice, the following
 * two paragraphs and the author appear in all copies of this software.
 * 
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
 *
 */

/**
 * @author Jonathan Hui <jwhui at cs.berkeley.edu>
 */

interface Voltage {
  command bool okToProgram();
}

--- NEW FILE: crc.h ---
// $Id: crc.h,v 1.1 2007/05/22 20:34:21 razvanm Exp $

/*									tab:4
 * "Copyright (c) 2000-2003 The Regents of the University  of California.  
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice, the following
 * two paragraphs and the author appear in all copies of this software.
 * 
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
 *
 * Copyright (c) 2002-2003 Intel Corporation
 * All rights reserved.
 *
 * This file is distributed under the terms in the attached INTEL-LICENSE     
 * file. If you do not find these files, copies can be found by writing to
 * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, Berkeley, CA, 
 * 94704.  Attention:  Intel License Inquiry.
 */

/**
 * Default CRC function. Note that avrmote has a much more efficient one. 
 *
 * This CRC-16 function produces a 16-bit running CRC that adheres to the
 * ITU-T CRC standard.
 *
 * The ITU-T polynomial is: G_16(x) = x^16 + x^12 + x^5 + 1
 *
 */

uint16_t crcByte(uint16_t crc, uint8_t b)
{
  uint8_t i;
  
  crc = crc ^ b << 8;
  i = 8;
  do
    if (crc & 0x8000)
      crc = crc << 1 ^ 0x1021;
    else
      crc = crc << 1;
  while (--i);

  return crc;
}



More information about the Tinyos-2-commits mailing list