[Tinyos-beta-commits] CVS: tinyos-1.x/beta/STM25P BlockRead.nc, NONE, 1.1 BlockStorage.h, NONE, 1.1 BlockStorageC.nc, NONE, 1.1 BlockStorageM.nc, NONE, 1.1 BlockWrite.nc, NONE, 1.1 FlashVolume.h, NONE, 1.1 FlashVolume.nc, NONE, 1.1 FlashVolumeC.nc, NONE, 1.1 FlashVolumeM.nc, NONE, 1.1 HPLSTM25P.nc, NONE, 1.1 STM25P.h, NONE, 1.1 STM25P.nc, NONE, 1.1 STM25PC.nc, NONE, 1.1 STM25PM.nc, NONE, 1.1

Jonathan Hui jwhui at users.sourceforge.net
Sat Nov 20 10:59:45 PST 2004


Update of /cvsroot/tinyos/tinyos-1.x/beta/STM25P
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv31093/STM25P

Added Files:
	BlockRead.nc BlockStorage.h BlockStorageC.nc BlockStorageM.nc 
	BlockWrite.nc FlashVolume.h FlashVolume.nc FlashVolumeC.nc 
	FlashVolumeM.nc HPLSTM25P.nc STM25P.h STM25P.nc STM25PC.nc 
	STM25PM.nc 
Log Message:
- Initial checkin of the storage stack for the STM25P family of flash
chips. 

- The new storage stack follows the HPL/HAL/HIL abstraction as
discussed in the TinyOS 1.2/2.x working group meetings. However, this
is not being checked into the 2.x branch since telosb uses the
STM25P80.

- Currently, only the BlockRead/Write interfaces of the HIL are
supported. LogRead/Write and ConfigRead/Write are in the works.

- The new storage interfaces are required for version 2 of Deluge.



--- NEW FILE: BlockRead.nc ---
// $Id: BlockRead.nc,v 1.1 2004/11/20 18:59:42 jwhui Exp $

/*									tab:4
 * "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>
 */

includes BlockStorage;

interface BlockRead {
  command result_t read(block_addr_t addr, uint8_t* buf, block_addr_t len);
  event   result_t readDone(result_t result);

  command result_t verify();
  event   result_t verifyDone(result_t result);
}

--- NEW FILE: BlockStorage.h ---
// $Id: BlockStorage.h,v 1.1 2004/11/20 18:59:42 jwhui Exp $

/*									tab:4
 * "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>
 */

#ifndef __BLOCK_STORAGE_H__
#define __BLOCK_STORAGE_H__

typedef uint16_t block_addr_t;

#endif

--- NEW FILE: BlockStorageC.nc ---
// $Id: BlockStorageC.nc,v 1.1 2004/11/20 18:59:42 jwhui Exp $

/*									tab:4
 * "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>
 */

includes BlockStorage;
includes STM25P;

configuration BlockStorageC {
  provides {
    interface StdControl;
    interface BlockWrite[uint8_t id];
    interface BlockRead[uint8_t id];
  }
}

implementation {

  components
    BlockStorageM,
    FlashVolumeC,
    STM25PC,
    LedsC as Leds;

  StdControl = BlockStorageM;
  StdControl = FlashVolumeC;
  StdControl = STM25PC;
  BlockWrite = BlockStorageM;
  BlockRead = BlockStorageM;

  BlockStorageM.ActualFV -> FlashVolumeC.FlashVolume;
  BlockStorageM.Leds -> Leds;
  BlockStorageM.STM25P -> STM25PC;

}

--- NEW FILE: BlockStorageM.nc ---
// $Id: BlockStorageM.nc,v 1.1 2004/11/20 18:59:42 jwhui Exp $

/*									tab:4
 * "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>
 */

module BlockStorageM {
  provides {
    interface StdControl;
    interface BlockWrite[uint8_t id];
    interface BlockRead[uint8_t id];
  }
  uses {
    interface FlashVolume as ActualFV[uint8_t id];
    interface Leds;
    interface STM25P;
  }
}

implementation {

  enum {
    S_IDLE,
    S_WRITE,
    S_ERASE,
    S_COMMIT,
    S_READ,
    S_VERIFY,
  };

  uint8_t baseAddr;

  uint8_t state;
  uint8_t client;

  uint8_t* bufPtr;
  block_addr_t curAddr;
  block_addr_t bytesRemaining;

  command result_t StdControl.init() {
    state = S_IDLE;
    return SUCCESS;
  }

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

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

  result_t actualSignal(result_t result) {

    uint8_t tmpState = state;

    state = S_IDLE;

    switch(tmpState) {
    case S_READ: return signal BlockRead.readDone[client](result);
    case S_WRITE: return signal BlockWrite.writeDone[client](result);
    case S_ERASE: return signal BlockWrite.eraseDone[client](result);
    }

    return SUCCESS;

  }

  task void signalSuccess() { actualSignal(SUCCESS); }
  
  task void signalFail() { actualSignal(FAIL); }

  void signalDone(result_t result) {
    if (result == SUCCESS)
      post signalSuccess();
    else
      post signalFail();
  }

  bool admitRequest(uint8_t id) {
    if (state != S_IDLE)
      return FALSE;
    client = id;
    return TRUE;
  }

  block_addr_t calcNumBytes() {

    block_addr_t pageOffset = curAddr % (block_addr_t)STM25P_PAGE_SIZE;
    block_addr_t numBytes;
    
    if (bytesRemaining < STM25P_PAGE_SIZE - pageOffset)
      numBytes = bytesRemaining;
    else
      numBytes = STM25P_PAGE_SIZE - pageOffset;

    return numBytes;

  }

  command result_t BlockWrite.write[uint8_t id](block_addr_t addr, uint8_t* buf, block_addr_t len) {

    stm25p_addr_t physicalAddr;

    if (admitRequest(id) == FAIL)
      return FAIL;

    curAddr = addr;
    bufPtr = buf;
    bytesRemaining = len;

    state = S_WRITE;

    physicalAddr = call ActualFV.physicalAddr[client](addr);

    if (call STM25P.pageProgram(physicalAddr, buf, calcNumBytes()) == FAIL) {
      state = S_IDLE;
      return FAIL;
    }
    
    return SUCCESS;

  }

  command result_t BlockWrite.erase[uint8_t id]() {

    stm25p_addr_t physicalAddr;

    if (admitRequest(id) == FAIL)
      return FAIL;

    state = S_ERASE;

    physicalAddr = call ActualFV.physicalAddr[client](0);

    if (call STM25P.sectorErase(physicalAddr) == FAIL) {
      state = S_IDLE;
      return FAIL;
    }

    return SUCCESS;

  }

  command result_t BlockWrite.commit[uint8_t id]() {

    if (admitRequest(id) == FAIL)
      return FAIL;

    state = S_COMMIT;

    state = S_IDLE;

    return SUCCESS;

  }

  command result_t BlockRead.read[uint8_t id](block_addr_t addr, uint8_t* buf, block_addr_t len) {

    stm25p_addr_t physicalAddr;

    if (admitRequest(id) == FAIL)
      return FAIL;

    state = S_READ;

    physicalAddr = call ActualFV.physicalAddr[client](addr);

    if (call STM25P.read(physicalAddr, buf, len) == FAIL) {
      state = S_IDLE;
      return FAIL;
    }

    return SUCCESS;

  }

  command result_t BlockRead.verify[uint8_t id]() {
    if (admitRequest(id) == FAIL)
      return FAIL;
    state = S_VERIFY;
    state = S_IDLE;
    return SUCCESS;
  }

  event result_t STM25P.readDone(result_t result) {
    signalDone(result);
    return SUCCESS;
  }

  event result_t STM25P.pageProgramDone(result_t result) {

    block_addr_t lastBytes = calcNumBytes();
    stm25p_addr_t physicalAddr;

    if (bytesRemaining <= lastBytes) {
      signalDone(SUCCESS);
      return SUCCESS;
    }
    else {
      curAddr += lastBytes;
      bufPtr += lastBytes;
      bytesRemaining -= lastBytes;
    }

    physicalAddr = call ActualFV.physicalAddr[client](curAddr);

    if (call STM25P.pageProgram(physicalAddr, bufPtr, calcNumBytes()) == FAIL)
      signalDone(FAIL);

    return SUCCESS;

  }

  event result_t STM25P.sectorEraseDone(result_t result) {
    signalDone(result);
    return SUCCESS;
  }

  event result_t STM25P.bulkEraseDone(result_t result) { 
    return SUCCESS; 
  }

  default event result_t BlockWrite.writeDone[uint8_t id](result_t result) { return SUCCESS; }
  default event result_t BlockWrite.eraseDone[uint8_t id](result_t result) { return SUCCESS; }
  default event result_t BlockWrite.commitDone[uint8_t id](result_t result) { return SUCCESS; }
  default event result_t BlockRead.readDone[uint8_t id](result_t result) { return SUCCESS; }
  default event result_t BlockRead.verifyDone[uint8_t id](result_t result) { return SUCCESS; }

}

--- NEW FILE: BlockWrite.nc ---
// $Id: BlockWrite.nc,v 1.1 2004/11/20 18:59:43 jwhui Exp $

/*									tab:4
 * "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>
 */

includes BlockStorage;

interface BlockWrite {
  command result_t write(block_addr_t addr, uint8_t* buf, block_addr_t len);
  event result_t writeDone(result_t result);

  command result_t erase();
  event result_t eraseDone(result_t result);

  command result_t commit();
  event result_t commitDone(result_t result);
}

--- NEW FILE: FlashVolume.h ---
// $Id: FlashVolume.h,v 1.1 2004/11/20 18:59:43 jwhui Exp $

/*									tab:4
 * "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>
 */

#ifndef __FLASH_VOLUME_H__
#define __FLASH_VOLUME_H__

#define FV_INVALID_VOLUME  0xff
#define FV_VOLUME_SIZE     ((stm25p_addr_t)0x10000)
#define FV_FACTORY_IMAGE   0xf
#define FV_MAX_VOLUMES     16
#define FV_INVALID_ADDR    ((stm25p_addr_t)0xffffffff)

#endif

--- NEW FILE: FlashVolume.nc ---
// $Id: FlashVolume.nc,v 1.1 2004/11/20 18:59:43 jwhui Exp $

/*									tab:4
 * "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>
 */

includes FlashVolume;
includes STM25P;

interface FlashVolume {
  command result_t mount(uint8_t uid);
  command stm25p_addr_t physicalAddr(uint32_t volumeAddr);
}

--- NEW FILE: FlashVolumeC.nc ---
// $Id: FlashVolumeC.nc,v 1.1 2004/11/20 18:59:43 jwhui Exp $

/*									tab:4
 * "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>
 */

configuration FlashVolumeC {
  provides {
    interface FlashVolume[uint8_t id];
    interface StdControl;
  }
}

implementation {

  components
    FlashVolumeM;

  FlashVolume = FlashVolumeM;
  StdControl = FlashVolumeM;

}

--- NEW FILE: FlashVolumeM.nc ---
// $Id: FlashVolumeM.nc,v 1.1 2004/11/20 18:59:43 jwhui Exp $

/*									tab:4
 * "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>
 */

module FlashVolumeM {
  provides {
    interface FlashVolume[uint8_t id];
    interface StdControl;
  }
}

implementation {

  enum {
    NUM_VOLUMES = uniqueCount("FlashVolume"),
  };

  uint8_t volumeMap[NUM_VOLUMES];

  command result_t StdControl.init() {
    uint8_t i;
    for ( i = 0; i < NUM_VOLUMES; i++ )
      volumeMap[i] = FV_INVALID_VOLUME;
    return SUCCESS; 
  }

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

  command result_t FlashVolume.mount[uint8_t id](uint8_t uid) {
    if (uid >= FV_MAX_VOLUMES)
      return FAIL;
    volumeMap[id] = uid;
    return SUCCESS;
  }
  
  command stm25p_addr_t FlashVolume.physicalAddr[uint8_t id](uint32_t volumeAddr) {

    stm25p_addr_t base = volumeMap[id]*FV_VOLUME_SIZE;

    if (volumeMap[id] == FV_INVALID_VOLUME
	|| volumeAddr < base
	|| (base+FV_VOLUME_SIZE) <= volumeAddr)
      return FV_INVALID_ADDR;
    
    return base + volumeAddr;
    
  }

}

--- NEW FILE: HPLSTM25P.nc ---
// $Id: HPLSTM25P.nc,v 1.1 2004/11/20 18:59:43 jwhui Exp $

/*									tab:4
 * "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 HPLSTM25P {
  async command result_t getBus();
  async command result_t releaseBus();
  async command result_t beginCmd(uint8_t cmd);
  async command result_t endCmd();
  async command result_t hold();
  async command result_t unhold();
  async command result_t txBuf(uint8_t* buf, stm25p_addr_t len);
  async command result_t rxBuf(uint8_t* buf, stm25p_addr_t len);
}

--- NEW FILE: STM25P.h ---
// $Id: STM25P.h,v 1.1 2004/11/20 18:59:43 jwhui Exp $

/*									tab:4
 * "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>
 */

#ifndef __STM25P_H__
#define __STM25P_H__

#define STM25P_PAGE_SIZE       256

#define STM25P_INVALID_SIG     0xFF
#define STM25P_ADDR_SIZE       3
#define STM25P_FR_DUMMY_BYTES  1
#define STM25P_RES_DUMMY_BYTES 3

                                // I, A, D, T, R
#define STM25P_WREN        0x06 // 1, 0, 0, 0, 0
#define STM25P_WRDI        0x04 // 1, 0, 0, 0, 0
#define STM25P_RDSR        0x05 // 1, 0, 0, 0, 1
#define STM25P_WRSR        0x01 // 1, 0, 0, 1, 0
#define STM25P_READ        0x03 // 1, 3, 0, 0, N
#define STM25P_FAST_READ   0x0b // 1, 3, 1, 0, N
#define STM25P_PP          0x02 // 1, 3, 0, N, 0
#define STM25P_SE          0xd8 // 1, 3, 0, 0, 0
#define STM25P_BE          0xc7 // 1, 0, 0, 0, 0
#define STM25P_DP          0xb9 // 1, 0, 0, 0, 0
#define STM25P_RES         0xab // 1, 0, 3, 0, 1

typedef uint8_t  stm25p_status_t;
typedef uint32_t stm25p_addr_t;
typedef uint8_t  stm25p_sig_t;

#endif

--- NEW FILE: STM25P.nc ---
// $Id: STM25P.nc,v 1.1 2004/11/20 18:59:43 jwhui Exp $

/*									tab:4
 * "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 STM25P {
  command result_t read(stm25p_addr_t addr, uint8_t* data, stm25p_addr_t len);
  event   result_t readDone(result_t result);

  command result_t pageProgram(stm25p_addr_t addr, uint8_t* data, stm25p_addr_t len);
  event   result_t pageProgramDone(result_t result);

  command result_t sectorErase(stm25p_addr_t addr);
  event   result_t sectorEraseDone(result_t result);

  command result_t bulkErase();
  event   result_t bulkEraseDone(result_t result);

  command stm25p_sig_t getSignature();
}

--- NEW FILE: STM25PC.nc ---
// $Id: STM25PC.nc,v 1.1 2004/11/20 18:59:43 jwhui Exp $

/*									tab:4
 * "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>
 */

includes STM25P;

configuration STM25PC {
  provides {
    interface StdControl;
    interface STM25P;
  }
}

implementation {
  components STM25PM, HPLSTM25PC, NoLeds as Leds;

  StdControl = STM25PM;
  StdControl = HPLSTM25PC;
  STM25P = STM25PM;

  STM25PM.HPLSTM25P -> HPLSTM25PC;
  STM25PM.Leds -> Leds;
}

--- NEW FILE: STM25PM.nc ---
// $Id: STM25PM.nc,v 1.1 2004/11/20 18:59:43 jwhui Exp $

/*									tab:4
 * "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>
 */

module STM25PM {
  provides {
    interface StdControl;
    interface STM25P;
  }
  uses {
    interface HPLSTM25P;
    interface Leds;
  }
}

implementation {
  
  enum {
    S_POWEROFF, // deep power-down state
    S_POWERON,  // awake state, no command in progress
  };

  stm25p_sig_t signature;
  uint8_t    curCmd;

  void sendCmd(uint8_t cmd, stm25p_addr_t addr, uint8_t* data, stm25p_addr_t len);

  command result_t StdControl.init() {
    curCmd = S_POWEROFF;
    signature = STM25P_INVALID_SIG;
    return SUCCESS;
  }

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

  result_t signalDone(result_t result) {
    uint8_t tmpCmd = curCmd;
    curCmd = S_POWERON;

    switch(tmpCmd) {
    case STM25P_READ: return signal STM25P.readDone(result);
    case STM25P_PP: return signal STM25P.pageProgramDone(result);
    case STM25P_SE: return signal STM25P.sectorEraseDone(result);
    case STM25P_BE: return signal STM25P.bulkEraseDone(result);
    }
    return SUCCESS;
  }

  task void signalSuccess() { signalDone(SUCCESS); }
  task void signalFail() { signalDone(FAIL); }

  bool isWriting() {
    uint8_t status;
    if (call HPLSTM25P.getBus() == FAIL)
      return TRUE;
    sendCmd(STM25P_RDSR, 0, &status, sizeof(status));
    call HPLSTM25P.releaseBus();
    return (status & 0x1) ? TRUE : FALSE;
  }

  // probably better to use a timer since write operations can take on
  // the order of seconds to complete
  task void checkWriteDone() {
    if (isWriting()) {
      post checkWriteDone();
      return;
    }
    signalDone(SUCCESS);
  }

  void sendCmd(uint8_t cmd, stm25p_addr_t addr, uint8_t* data, stm25p_addr_t len) {

    uint8_t addrBytes[STM25P_ADDR_SIZE];
    int8_t  i;

    // start command
    call HPLSTM25P.beginCmd(cmd);

    // address
    switch(cmd) {
    case STM25P_READ: case STM25P_FAST_READ: case STM25P_PP: case STM25P_SE:
      for ( i = 0; i < STM25P_ADDR_SIZE; i++ )
	addrBytes[i] = (addr >> ((STM25P_ADDR_SIZE-1-i)*8)) & 0xff;
      call HPLSTM25P.txBuf(addrBytes, STM25P_ADDR_SIZE);
      break;
    }

    // dummy bytes
    switch(cmd) {
    case STM25P_FAST_READ:
      call HPLSTM25P.txBuf(addrBytes, STM25P_FR_DUMMY_BYTES);
      break;
    case STM25P_RES:
      call HPLSTM25P.txBuf(addrBytes, STM25P_RES_DUMMY_BYTES);
      break;
    }

    // data
    switch(cmd) {
    case STM25P_RDSR: case STM25P_READ: case STM25P_FAST_READ: case STM25P_RES:
      call HPLSTM25P.rxBuf(data, len);
      break;
    case STM25P_WRSR: case STM25P_PP:
      call HPLSTM25P.txBuf(data, len);
      break;
    }

    // end command
    call HPLSTM25P.endCmd();

  }

  void powerOff() {
    sendCmd(STM25P_DP, 0, NULL, 0);
    curCmd = S_POWEROFF;
  }

  void powerOn() {
    sendCmd(STM25P_RES, 0, &signature, sizeof(signature));
    TOSH_uwait(2); // wait at least 1.8us to power on
    curCmd = S_POWERON;
  }

  result_t newRequest(uint8_t cmd, stm25p_addr_t addr, uint8_t* data, stm25p_addr_t len) {

    // make sure flash is powered on
    if (curCmd == S_POWEROFF)
      powerOn();
    // make sure nothing else is in progress
    else if (curCmd != S_POWERON)
      return FAIL;
    // make sure we can get the bus
    else if (call HPLSTM25P.getBus() == FAIL)
      return FAIL;
    
    curCmd = cmd;
    
    // enable writes if needed
    if (curCmd == STM25P_PP || curCmd == STM25P_SE || curCmd == STM25P_BE)
      sendCmd(STM25P_WREN, 0, NULL, 0);
    
    // send command
    sendCmd(curCmd, addr, data, len);

    call HPLSTM25P.releaseBus();

    // setup check for write done
    if (curCmd == STM25P_PP || curCmd == STM25P_SE || curCmd == STM25P_BE)
      post checkWriteDone();
    else
      post signalSuccess();

    return SUCCESS;

  }

  command result_t STM25P.read(stm25p_addr_t addr, uint8_t* data, stm25p_addr_t len) {
    return newRequest(STM25P_READ, addr, data, len);
  }

  command result_t STM25P.pageProgram(stm25p_addr_t addr, uint8_t* data, stm25p_addr_t len) {
    return newRequest(STM25P_PP, addr, data, len);
  }

  command result_t STM25P.sectorErase(stm25p_addr_t addr) {
    return newRequest(STM25P_SE, addr, NULL, 0);
  }

  command result_t STM25P.bulkErase() {
    return newRequest(STM25P_BE, 0, NULL, 0);
  }

  command stm25p_sig_t STM25P.getSignature() { 
    return signature; 
  }

}



More information about the Tinyos-beta-commits mailing list