Jump to content
43oh

Energia G2553 I2C library, can't reduce Wire.write(data, len) length


Recommended Posts

I am working on the MSP430G2553 using I2C and Serial libraries. I am playing right now to try to understand the usage of the libraries.

 

I have an I2C slave that I am trying to control how much data gets returned when a read is executed. Using the serial interface I can increase the size that is sent over the I2C but I can't reduce the size even though the variable passed to Wire.write shows it has reduced.

 

Slave code:



#include <Wire.h>
#define MY_ADDR 2
#define ERR_RQST_MSB     0xF0  ///< First byte sent when request hasn't been preceded by a value
#define ERR_RQST_LSB     0x00  ///< Second byte sent when request hasn't been preceded by a value
#define EXPECT_RQST_SIZE  2    ///< Expected size for any data request
#define RSPNS_SIZE        2    ///< How many to send back for any request
#define MONITOR_BAUD    38400  ///< Baud rate for monitor interface
#define MIN_CMD          0     ///< Minimum valid command 
#define MAX_CMD          4     ///< Maximum valid command 
#define RESET_CMD        128   ///< Command to "reset" processor

byte sendMSB = ERR_RQST_MSB;
byte sendLSB = ERR_RQST_LSB;

// Definition of the available one character commands
typedef enum CommandList{
  clEmpty    = '\0',
  clWrtLen   = 'w',
  clAdr      = 'a',
  clBaud     = 'b',  
}clist;
const byte Term1 = (byte)'\r';
const byte Term2 = (byte)'\n';

byte cmd = 0;
byte rx = 0;
byte tx = 0;
byte rxCmdBuf[16] = "";
byte *rxPtr = &rxCmdBuf[0];
bool processingCommand = false;
bool newCommand = false;

const uint8_t sendBuf[] = "0123456789012345678";
#define MAX_WRT_LEN 16

uint16_t curBaud = MONITOR_BAUD;
uint8_t  curAdrs = MY_ADDR;
volatile uint8_t  writeLen = 1;

void setup()
{
  Serial.begin(curBaud);   // start serial for output
  Wire.begin(curAdrs);          // join i2c bus with MY_ADDR
  Wire.onRequest(requestEvent); // register event
  Wire.onReceive(receiveEvent); // register event
}

void loop()
{
  delay(100);
  if(tx > 0){
    tx = 0;
    Serial.println("Tx");
  }
  if(rx > 0){
    rx = 0;
    Serial.println("Rx");
  }
  receiveCommand();
  processCommand();
}

// Bring one command at a time into the buffer
void receiveCommand(void)
{
  // If they are processing a command then let new chars sit
  if(!processingCommand){
    // Update the buffer if there is more data
    while(Serial.available() && !newCommand){
      *rxPtr = Serial.read();
      //Serial.write((char *)rxPtr);
      // If any terminating character came in then the command 
      // is complete, mark it as such, otherwise add it to the buffer
      if((*rxPtr == Term1)||(*rxPtr == Term2)){
        *rxPtr++ = 0;
        newCommand = true;
        
      }else{
        *rxPtr = (byte)tolower(*rxPtr);
        rxPtr++;
      }
    }
  }
}

// Change this to do any special processing of the command
void processCommand(void)
{
  static uint16_t lastParm = 0;
  static byte lastCmd;
  
  // If there is anything to do then do it
  if(newCommand || processingCommand){
    if(newCommand){
      newCommand = false;
      lastCmd = rxCmdBuf[0];
      lastParm = (uint16_t)strtoul((char *)&rxCmdBuf[1], NULL, 10);
    }
    // First character is always the command
    switch((clist)lastCmd){
      case clWrtLen:
        processingCommand = false;
        
        if(lastParm < MAX_WRT_LEN){
          writeLen = lastParm;
        }
        Serial.println("Write length");
        Serial.println(writeLen);
        break;
        
      case clAdr:
        processingCommand = false;
        break;
        
      case clBaud:
        processingCommand = false;
        break;
   
      case clEmpty:
        processingCommand = false;
        break;
      
      default:
        Serial.println((char *)rxCmdBuf);
        Serial.println("Please enter a valid command");
        processingCommand = false;
        break;
    }
    
    // Reset the buffer if we are done
    if(!processingCommand){
      rxCmdBuf[0] = 0;
      rxPtr = &rxCmdBuf[0];
    }
  }
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent()
{
  byte len = writeLen;
  if(len > 15) writeLen = 2;
  Wire.write(sendBuf, len);//"slave says send something"); // respond with message of 6 bytes
  tx++;                     // as expected by master
  writeLen = 1;
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
  while(1 < Wire.available()) // loop through all but the last
  {
    char c = Wire.read(); // receive byte as a character
    Serial.print(c);         // print the character
  }
  int x = Wire.read();    // receive byte as an integer
  Serial.println(x);         // print the integer
  rx++;
}
Link to post
Share on other sites

Once I send a length of say 4, I can't reduce the length below that. The master always receives the maximum length of what I set even though the length variable has been reduced again below that.

 

Example for a session:

Send w2

writeLen = 2, data master recieves is length 2

Send w4

writeLen = 4, data master recieves is length 4

Send w2

writeLen = 2, data master recieves is length 4 though writeLen is reported as 2 thru the serial port

Send w8

writeLen = 8, data master recieves is length 8

Send w2

writeLen = 2, data master recieves is length 8 though writeLen is reported as 2 thru the serial port

Link to post
Share on other sites
  • 1 month later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...