Hello all,

I've been having some trouble using the msp430 usart module, on the i2c
mode. I made a simple program to communicate with the tmote invent's digital
potentiometer, an analog devices' AD5242, but it is influenced by other
components that uses the USART. For example, depending on the moment I start
the radio, I can't send packages on the i2c bus. I get success on all
command calls and events, but the packet isn't transmitted. I even checked
the pin on the oscilloscope and nothing is sent. My test application is
attached.

When compiling, I got the following warnings about non-atomic access to
varibles on the msp430 usart module:

/opt/msp430/msp430/include/msp430/usart.h:85: warning: non-atomic accesses
to shared variable `U0CTL':
/home/d1ma5/tinyos-2.x-cvs/tos/chips/msp430/usart/HplMsp430Usart0P.nc:153:
warning:   non-atomic write
/home/d1ma5/tinyos-2.x-cvs/tos/chips/msp430/usart/HplMsp430Usart0P.nc:156:
warning:   non-atomic r/w

since the U0CTL register was not marked as MSP430REG_NORACE, this could mean
that a race condition can be happening. As stated in the file
msp430hardware.h, "..., CPU registers access must be treated seriously and
carefully". I also get weird results when I use the invent's i2c based leds,
depending on the delay I put in a timer the expected result does not occur.

On my program, if I uncomment the 2nd line of the Boot.booted event, the
expected result does not happen.

Thanks for the attention,
~Dimas~


============ I2CTestC.nc =============
/**
 * Test app to help discover the root of the mysterious bug of the AD5241
logic
 * pins.
 * @author Dimas Abreu Dutra
 */
#include "Msp430Adc12.h"
#include <I2C.h>

module I2CTestC {
  uses interface Boot;
  uses interface SplitControl as RadioControl;
  uses interface Timer<TMilli>;

  uses interface I2CPacket<TI2CBasicAddr>;
  uses interface Resource; //I2C bus resource

  uses interface Leds; //For debug, not needed in normal operation
  //uses interface EasyDebug;//Radio-based debugging

  uses interface Read<uint16_t> as AccelXRead;

  provides {
    interface AdcConfigure<const msp430adc12_channel_config_t*> as
AccelXConfig;
    interface AdcConfigure<const msp430adc12_channel_config_t*> as
AccelYConfig;
  }
} implementation {

  uint8_t i2c_data[2];

  enum{
    TIMER_PERIOD_MS = 500,
    MAX7315_ADDR = 0x20,
    U92_ADDR = 0x2F,
  };

  typedef enum{
    STATE_CONFIG_PORTS,
    STATE_SET_LEDS,
    STATE_TURN_ACCEL_ON,
    STATE_TURN_ACCEL_OFF,
  } module_state_t;

  module_state_t module_state = STATE_CONFIG_PORTS;

  task void nextState();
  task void configMax7315Ports();
  task void setLeds();
  task void turnOnAccel();
  task void turnOffAccel();
  task void i2cWriteDone();

  event void Boot.booted(){
    //IF I UNCOMMENT THE LINE BELLOW, THE EXPECTED RESULT DOES NOT OCCUR!!!
    //call RadioControl.start();
    call Timer.startPeriodic(TIMER_PERIOD_MS);
  }

  event void RadioControl.startDone(error_t result){
    //call Timer.startPeriodic(TIMER_PERIOD_MS);
  }

  event void RadioControl.stopDone(error_t result){}

  event void Timer.fired(){
    call Leds.led0Toggle();
    //call EasyDebug.debug(0x01, 0);
    call Resource.request();
  }

  event void Resource.granted(){
    //call EasyDebug.debug(0x02, 0);

    post nextState();
  }

  task void nextState(){
    switch(module_state){
    case STATE_CONFIG_PORTS:
      post configMax7315Ports();
      module_state = STATE_SET_LEDS;
      break;
    case STATE_SET_LEDS:
      post setLeds();
      module_state = STATE_TURN_ACCEL_ON;
      break;
    case STATE_TURN_ACCEL_ON:
      post turnOnAccel();
      module_state = STATE_TURN_ACCEL_OFF;
      break;
    case STATE_TURN_ACCEL_OFF:
      post turnOffAccel();
      module_state = STATE_CONFIG_PORTS;
      break;
    }
  }

  error_t i2c_write_result;
  async event void I2CPacket.writeDone(error_t error, uint16_t addr,
                       uint8_t length, uint8_t *data) {
    atomic i2c_write_result = error;
    post i2cWriteDone();
    call Resource.release();
  }

  task void i2cWriteDone(){
    error_t _i2c_write_result;
    atomic _i2c_write_result = i2c_write_result;
    call AccelXRead.read();
    //call EasyDebug.debug(0x03, _i2c_write_result);
  }

  async event void I2CPacket.readDone(error_t error, uint16_t addr,
                      uint8_t length, uint8_t *data) {}

  uint8_t led_val = 0xFA;
  task void setLeds(){
    error_t result;
    i2c_data[0] = 0x01;
    i2c_data[1] = led_val;

    led_val ^= 0x07;

    result  = call I2CPacket.write(I2C_START|I2C_STOP, MAX7315_ADDR,
                   2, i2c_data);
    //call EasyDebug.debug(0x05, result);
  }

  task void configMax7315Ports(){
    error_t result;
    i2c_data[0] = 0x03;
    i2c_data[1] = 0xF8;
    result  = call I2CPacket.write(I2C_START|I2C_STOP, MAX7315_ADDR,
                   2, i2c_data);
    //call EasyDebug.debug(0x06, result);
  }

  task void turnOnAccel(){
    error_t result;
    i2c_data[0] = 0x18;//0b00011000 in hex
    result  = call I2CPacket.write(I2C_START|I2C_STOP, U92_ADDR,
                   1, i2c_data);
    //call EasyDebug.debug(0x07, result);
  }

  task void turnOffAccel(){
    error_t result;
    i2c_data[0] = 0x00;
    result  = call I2CPacket.write(I2C_START|I2C_STOP, U92_ADDR,
                   1, i2c_data);
    //call EasyDebug.debug(0x08, result);
  }

  event void AccelXRead.readDone(error_t result, uint16_t val){
    //call EasyDebug.debug(0x09, val);
    if (val > 0x0700) call Leds.led1Toggle();
    if (val < 0x090) call Leds.led2Toggle();
  }

  const msp430adc12_channel_config_t accelXConfig = {
  inch: INPUT_CHANNEL_A0,
  sref: REFERENCE_VREFplus_AVss,
  ref2_5v: REFVOLT_LEVEL_2_5,
  adc12ssel: SHT_SOURCE_ACLK,
  adc12div: SHT_CLOCK_DIV_1,
  sht: SAMPLE_HOLD_4_CYCLES,
  sampcon_ssel: SAMPCON_SOURCE_SMCLK,
  sampcon_id: SAMPCON_CLOCK_DIV_1
  };

  const msp430adc12_channel_config_t accelYConfig = {
  inch: INPUT_CHANNEL_A1,
  sref: REFERENCE_VREFplus_AVss,
  ref2_5v: REFVOLT_LEVEL_2_5,
  adc12ssel: SHT_SOURCE_ACLK,
  adc12div: SHT_CLOCK_DIV_1,
  sht: SAMPLE_HOLD_4_CYCLES,
  sampcon_ssel: SAMPCON_SOURCE_SMCLK,
  sampcon_id: SAMPCON_CLOCK_DIV_1
  };

  async command const msp430adc12_channel_config_t*
  AccelXConfig.getConfiguration() {
    return &accelXConfig;
  }

  async command const msp430adc12_channel_config_t*
  AccelYConfig.getConfiguration() {
    return &accelYConfig;
  }

  //default command void EasyDebug.debug(uint16_t id, uint16_t data){}
}

============ I2CTestAppC.nc =============
/**
 * Test app to help discover the root of the mysterious bug of the AD5241
logic
 * pins.
 * @author Dimas Abreu Dutra
 */

configuration I2CTestAppC{}
implementation {
  components MainC, I2CTestC as App;
  App -> MainC.Boot;

  components new Msp430I2CC() as I2CBus;
  App.I2CPacket -> I2CBus;
  App.Resource -> I2CBus;

  components ActiveMessageC;
  App.RadioControl -> ActiveMessageC;

  //components EasyDebugC;
  //App.EasyDebug -> EasyDebugC;

  components LedsC;
  App.Leds -> LedsC;

  components new TimerMilliC();
  App.Timer -> TimerMilliC;

  components new AdcReadClientC() as AccelXRead;
  AccelXRead -> App.AccelXConfig;
  App.AccelXRead -> AccelXRead;
}
_______________________________________________
Tinyos-help mailing list
Tinyos-help@millennium.berkeley.edu
https://www.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help

Reply via email to