[Tinyos-2-commits] CVS: tinyos-2.x/tos/chips/ds2745 DS2745.h, NONE, 1.1.2.1 HplDS2745.nc, NONE, 1.1.2.1 HplDS2745LogicP.nc, NONE, 1.1.2.1

Philip Buonadonna philipb at users.sourceforge.net
Thu Aug 10 17:06:33 PDT 2006


Update of /cvsroot/tinyos/tinyos-2.x/tos/chips/ds2745
In directory sc8-pr-cvs10.sourceforge.net:/tmp/cvs-serv24884

Added Files:
      Tag: tinyos-2_0_devel-BRANCH
	DS2745.h HplDS2745.nc HplDS2745LogicP.nc 
Log Message:
Initial add of DS2745 support (HPL Only)

--- NEW FILE: DS2745.h ---
/*
 * Copyright (c) 2005-2006 Arch Rock Corporation
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the
 *   distribution.
 * - Neither the name of the Arch Rock Corporation nor the names of
 *   its contributors may be used to endorse or promote products derived
 *   from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
 * ARCHED ROCK OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE
 */

/**
 * Types and definitions for the Dallas DS2745 I2C Battery Monitor
 *
 * @author Phil Buonadonna <pbuonadonna at archrock.com>
 * @version $Revision: 1.1.2.1 $ $Date: 2006/08/11 00:06:31 $
 */

#ifndef _DS2745_H
#define _DS2745_H

#define DS2745_PTR_SC		(0x01)
#define DS2745_PTR_TEMPMSB	(0x0A)
#define DS2745_PTR_TEMPLSB	(0x0B)
#define DS2745_PTR_VOLTMSB	(0x0C)
#define DS2745_PTR_VOLTLSB	(0x0D)
#define DS2745_PTR_CURRMSB	(0x0E)
#define DS2745_PTR_CURRLSB	(0x0F)
#define DS2745_PTR_ACCURMSB	(0x10)
#define DS2745_PTR_ACCURLSB	(0x11)
#define DS2745_PTR_OFFSETBIAS	(0x61)
#define DS2745_PTR_ACCBIAS	(0x62)

#define DS2745_SC_PORF		(1 << 6)
#define DS2745_SC_SMOD		(1 << 5)
#define DS2745_SC_NBEN		(1 << 4)
#define DS2745_SC_PIO		(1 << 3)
#define DS2745_SC_FQ(_x)	(((_x) & 0x3))

#endif /* _DS2745_H */

--- NEW FILE: HplDS2745.nc ---
/*
 * Copyright (c) 2005-2006 Arch Rock Corporation
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the
 *   distribution.
 * - Neither the name of the Arched Rock Corporation nor the names of
 *   its contributors may be used to endorse or promote products derived
 *   from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
 * ARCHED ROCK OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE
 */

/**
 * HplDS2745 is the HPL inteface to the Dallas DS2745 I2C Battery 
 * Monitor.
 *
 * @author Phil Buonadonna <pbuonadonna at archrock.com>
 * @version $Revision: 1.1.2.1 $ $Date: 2006/08/11 00:06:31 $
 */

interface HplDS2745 {

  /**
   * Sets a new value to the DS2745 configuration register.
   *
   * @param val the new value to be written
   *
   * @return SUCCESS if the set will be performed
   */
  command error_t setConfigReg( uint8_t val );

  /**
   * Signals the completion of the configuration register set.
   *
   * @param error SUCCESS if the set was successful
   */
  async event void setConfigRegDone( error_t error );


  /**
   * Starts a temperature measurement.
   *
   * @return SUCCESS if the measurement will be made
   */
  command error_t measureTemperature();

  /**
   * Presents the result of a temperature measurement.
   *
   * @param error SUCCESS if the measurement was successful
   * @param val the temperature reading
   */
  async event void measureTemperatureDone( error_t error, uint16_t val );


  /** 
   * Starts a voltage measurement.
   *
   * @return SUCCESS if the measurement will be made
   */
  command error_t measureVoltage();

  /**
   * Presents the result of a voltage measurement.
   *
   * @param error SUCCESS if the measurement was successful
   * @param val the voltage reading
   */
  async event void measureVoltageDone( error_t error, uint16_t val);


  /** 
   * Starts a current measurement.
   *
   * @return SUCCESS if the measurement will be made
   */
  command error_t measureCurrent();

  /**
   * Presents the result of a current measurement.
   *
   * @param error SUCCESS if the measurement was successful
   * @param val the current reading
   */
  async event void measureCurrentDone( error_t error, uint16_t val);


  /** 
   * Starts an accumulated current measurement.
   *
   * @return SUCCESS if the measurement will be made
   */
  command measureAccCurrent();

  /**
   * Presents the result of a accumulated current measurement.
   *
   * @param error SUCCESS if the measurement was successful
   * @param val the accumulated current reading
   */
  async event void measureAccCurrentDone( error_t error, uint16_t val);


  /** 
   * Initiates setting of the current offset bias value
   *
   * @param The signed two's complement bias value.
   *
   * @return SUCCESS if the setting will be made
   */
  command error_t setOffsetBias(int8_t val);

  /**
   * Signals completion and error, if any, in setting the current
   * offset bias value.
   *
   * @param error SUCCESS if the setting was successful
   */
  async event void setOffsetBiasDone( error_t error );


  /** 
   * Initiates setting of the accumulated current offset bias value
   *
   * @param The signed two's complement bias value.
   *
   * @return SUCCESS if the setting will be made
   */
  command error_t setAccOffsetBias(int8_t val);

  /**
   * Signals completion and error, if any, in setting the accumulated
   * current offset bias value.
   *
   * @param error SUCCESS if the setting was successful
   */
  async event void setAccOffsetBiasDone( error_t error );


}

--- NEW FILE: HplDS2745LogicP.nc ---
/*
 * Copyright (c) 2005-2006 Arch Rock Corporation
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the
 *   distribution.
 * - Neither the name of the Arched Rock Corporation nor the names of
 *   its contributors may be used to endorse or promote products derived
 *   from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
 * ARCHED ROCK OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE
 */

/**
 * HplDS2745LogicP is the driver for the Dallas DS2745. It requires an 
 * I2C packet interface and provides the HplTMP175 HPL interface.
 * 
 * @author Phil Buonadonna <pbuonadonna at archrock.com>
 * @version $Revision: 1.1.2.1 $ $Date: 2006/08/11 00:06:31 $
 */

#include "DS2745.h"
#include "I2C.h"

generic module HplDS2745LogicP(uint16_t devAddr)
{
  provides interface Init;
  provides interface SplitControl;
  provides interface HplDS2745;

  uses interface I2CPacket<TI2CBasicAddr>;

}

implementation {

  enum {
    STATE_IDLE,
    STATE_STARTING,
    STATE_STOPPING,
    STATE_STOPPED,
    STATE_SETCONFIG,
    STATE_READTEMP,
    STATE_READVOLTAGE
    STATE_READCURRENT,
    STATE_READACCCURRENT,
    STATE_SETBIAS,
    STATE_SETACCBIAS
  };

  uint8_t mI2CBuffer[4];
  uint8_t mState;
  norace error_t mSSError;

  static errot_t doReadReg(uint8_t nextSTate, uint8_t reg) {
    error_t error = SUCCESS;
	
    atomic {
      if (mState == STATE_IDLE) {
	mState = nextState;
      }
      else {
	error = EBUSY;
      }
    }
    if (error)
      return error;

    mI2CBuffer[0] = reg;

    error = call I2CPacket.write(I2C_START,devAddr,1,mI2CBuffer);
    
    if (error) 
      atomic mState = STATE_IDLE;

    return error;

  }

  static error_t doSetReg(uint8_t nextState, uint8_t reg, uint16_t val) {
    error_t error = SUCCESS;

    atomic {
      if (mState == STATE_IDLE) {
	mState = nextState;
      }
      else {
	error = EBUSY;
      }
    }
    if (error)
      return error;

    mI2CBuffer[0] = reg;
    mI2CBuffer[1] = val;

    error = call I2CPacket.write((I2C_START | I2C_STOP),devAddr,2,mI2CBuffer);
    
    if (error) 
      atomic mState = STATE_IDLE;

    return error;
  }

  task void StartDone() {
    atomic mState = STATE_IDLE;
    signal SplitControl.startDone();
    return;
  }

  task void StopDone() {
    atomic mState = STATE_STOPPED;
    signal SplitControl.stopDone(mSSError);
    return;
  }

  command error_t Init.init() {
    mState = STATE_STOPPING;
    return SUCCESS;
  }

  command error_t SplitControl.start() {
    error_t error = SUCCESS;
    atomic {
      if (mState == STATE_STOPPED) { 
	mState = STATE_STARTING; 
      }
      else {
	error = EBUSY;
      }
    }
    if (!error)
      post StartDone();

    return error;
  }

  command error_t SplitControl.stop() {
    error_t error = SUCCESS;
    atomic {
      if (mState == STATE_IDLE) {
	mState = STATE_STOPPING;
      }
      else {
	error = EBUSY;
      }
    }
    if (!error)
      post StopDone();

    return error;
  }
  
  command error_t HplDS2745.measureTemperature() { 
    return doReadReg(STATE_READTEMP,DS2745_PTR_TEMPMSB);
  }

  command error_t HplDS2745.measureVoltage() { 
    return doReadReg(STATE_READTEMP,DS2745_PTR_VOLTMSB);
  }

  command error_t HplDS2745.measureCurrent() { 
    return doReadReg(STATE_READTEMP,DS2745_PTR_CURRMSB);
  }

  command error_t HplDS2745.measureAccCurrent() { 
    return doReadReg(STATE_READTEMP,DS2745_PTR_ACCURMSB);
  }

  command error_t HplDS2745.setOffsetBias(uint16_t val) { 
    return doSetReg(STATE_SETBIAS,DS2745_PTR_OFFSETBIAS,val); 
  }

  command error_t HplDS2745.setAccOffsetBias(uint16_t val) {
    return doSetReg(STATE_SETACCBIAS,DS2745_PTR_ACCBIAS,val); 
  }

  async event void I2CPacket.readDone(error_t i2c_error, uint16_t chipAddr, uint8_t len, uint8_t *buf) {
    uint16_t tempVal;
    tempVal = buf[0];
    tempVal = ((tempVal << 8) | buf[1]);

    switch (mState) {
    case STATE_READTEMP:
      signal HplDS2745.measureTemperatureDone(i2c_error,tempVal);
      break;
    case STATE_READVOLTAGE:
      signal HplDS2745.measureVoltageDone(i2c_error,tempVal);
      break;
    case STATE_READCURRENT:
      signal HplDS2745.measureCurrentDone(i2c_error,tempVal);
      break;
    case STATE_READACCCURRENT:
      signal HplDS2745.measureAccCurrentDone(i2c_error,tempVal);
      break;
    default:
      break;
    }
    atomic mState = STATE_IDLE;
    return;
  }

  async event void I2CPacket.writeDone(error_t i2c_error, uint16_t chipAddr, uint8_t len, uint8_t *buf) {
    error_t error = i2c_error;

    switch (mState) {
    case STATE_READTEMP:
      if (error) 
	signal HplDS2745.measureTemperatureDone(error,0);
      else
	error = call I2CPacket.read((I2C_START | I2C_STOP),devAddr,2,mI2CBuffer);
      break;
    case STATE_READVOLTAGE:
      if (error) 
	signal HplDS2745.measureVoltageDone(error,0);
      else
	error = call I2CPacket.read((I2C_START | I2C_STOP),devAddr,2,mI2CBuffer);
      break;
    case STATE_READCURRENT:
      if (error) 
	signal HplDS2745.measureCurrentDone(error,0);
      else 
	error = call I2CPacket.read((I2C_START | I2C_STOP),devAddr,2,mI2CBuffer);
      break;
    case STATE_READACCCURRENT:
      if (error) 
	signal HplDS2745.measureAccCurrentDoneDone(error,0);
      else
	error = call I2CPacket.read((I2C_START | I2C_STOP),devAddr,2,mI2CBuffer);
      break;
    case STATE_SETBIAS:
      atomic mState = STATE_IDLE;
      signal HplDS2745.setConfigRegDone(error);
      break;
    case STATE_SETACCBIAS:
      atomic mState = STATE_IDLE;
      signal HplDS2745.setTHighRegDone(error);
      break;
    default:
      atomic mState = STATE_IDLE;
      break;
    }
    if (error)
      atomic mState = STATE_IDLE;
    return;
  }
  
  default event void SplitControl.startDone( error_t error ) { return; }
  default event void SplitControl.stopDone( error_t error ) { return; }
  default async event void HplDS2745.measureTemperatureDone( error_t error, uint16_t val ){ return; }
  default async event void HplDS2745.measureVoltageDone( error_t error, uint16_t val ){ return; }
  default async event void HplDS2745.measureCurrentDone( error_t error, uint16_t val ){ return; }
  default async event void HplDS2745.measureAccCurrentDone( error_t error, uint16_t val ){ return; }
  default async event void HplDS2745.setOffsetBiasDone( error_t error ){ return; }
  default async event void HplDS2745.setAccOffsetBiasDone(error_t error){ return; }
}



More information about the Tinyos-2-commits mailing list