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++;
}