ljstrickler61 0 Posted June 29, 2014 Share Posted June 29, 2014 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++; } Quote Link to post Share on other sites
energia 485 Posted June 29, 2014 Share Posted June 29, 2014 What I understood from the question is that even though you do a Wire.write of say 2 bytes, the master receives more? Is this for any length or a problem in general? Would you mind explaining the Wire.write size problem in a bit more detail. Thanks! Robert Quote Link to post Share on other sites
ljstrickler61 0 Posted June 29, 2014 Author Share Posted June 29, 2014 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 Quote Link to post Share on other sites
energia 485 Posted June 30, 2014 Share Posted June 30, 2014 That definitely smells like a bug. Which LaunchPad are you using? I will look into this later this week. Quote Link to post Share on other sites
energia 485 Posted June 30, 2014 Share Posted June 30, 2014 p.s. would you please be so kind to file a bug on https://github.com/energia/Energia/issues Thanks, Robert Quote Link to post Share on other sites
ljstrickler61 0 Posted July 1, 2014 Author Share Posted July 1, 2014 MSP430G 1.5 and I should get to filing the bug later today Quote Link to post Share on other sites
ljstrickler61 0 Posted August 16, 2014 Author Share Posted August 16, 2014 Finally was able to create the bug report (job and family got in the way) #440 Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.