Wire onReceive(handler) issue

dc101
Posts: 16
Joined: Mon Aug 22, 2011 12:46 am

Wire onReceive(handler) issue

Post by dc101 » Fri Mar 30, 2012 6:17 pm

Using the examples code for the Wire library, I'm simply trying to transmit the word Hello from an Arduino Uno to a Chipkit Uno32.

I'm having a problem with the onReceive event handler on the Chipkit, not reporting the correct number of bytes received. On the Chipkit, onReceive seems to always report 1 byte received. However, if I switch the code and make the Arduino the slave, and the Chipkit the master, the Arduino correctly reports 5 bytes received.

I've used the same code on both, making the mandatory send/write received/read changes. At first I thought it was because I was sending a string, but I changed it to a byte array, inserting the correct number of bytes, and the Chipkit still reported 1 byte received. I've also verified that both I2C buses are set to 100KHz in the Wire libs.

Any ideas?

Sender:

Code: Select all

#include <Wire.h>

void setup()
{
  Wire.begin(); // join i2c bus (address optional for master)
}

void loop()
{
  Wire.beginTransmission(55); // transmit to device #55
  Wire.send("Hello");        // sends five bytes
  Wire.endTransmission();    // stop transmitting

  delay(500);
}
Receiver:

Code: Select all

#include <Wire.h>
char c = 0;

void setup()
{
  Wire.begin(55);                // join i2c bus with address #4
  Wire.onReceive(receiveEvent);  // register event
  Serial.begin(57600);           // start serial for output
}

void loop()
{
  delay(100);
}

void receiveEvent(int howMany)
{
 Serial.println(howMany);
 while(Wire.available()) // loop through all but the last
 {
   c = Wire.read(); // receive byte as a character
   Serial.print(c);         // print the character
 }
}

photomankc
Posts: 23
Joined: Sat Nov 12, 2011 4:34 am

Re: Wire onReceive(handler) issue

Post by photomankc » Sat Mar 31, 2012 6:43 am

Yes, I have that issue too. The handler will return after each byte received so you will never get a number greater than 1. In my case I had a system where I always knew how many bytes should follow the first so I set up a global counter that incremented with each byte and didnt act until all bytes were collected but I would prefer it worked the way it does on the Arduino.

Ryan K
Posts: 79
Joined: Fri Feb 10, 2012 12:36 am

Re: Wire onReceive(handler) issue

Post by Ryan K » Sat Mar 31, 2012 10:30 pm

Hello,

It is due to this section of code in the twi.c file in the wire library utilities folder:

Code: Select all

case TW_SR_DATA:
	twi_rxBuffer[0] = ptwi->ixRcv.reg;
	twi_onSlaveReceive(twi_rxBuffer, 1);
	// Release clock line
	ptwi->ixCon.set = (1 << _I2CCON_SCLREL);
	ptwi->ixStat.clr = (1 << _I2CSTAT_I2COV) | (1 << _I2CSTAT_RBF);
	break;
The twi_onSlaveReceive(twi_rxBuffer, 1); always has a 1 as the parameter and will be called every time data is received. Hopefully I'll have time soon to adjust that section to count and wait until a transmission is done.

Best Regards,
Ryan K
Digilent

dc101
Posts: 16
Joined: Mon Aug 22, 2011 12:46 am

Re: Wire onReceive(handler) issue

Post by dc101 » Sun Apr 01, 2012 1:08 am

Ryan,

Thanks for pointing me in the right direction.

I thought it might be somewhere around that section of code, but wasn't sure.

At first glance, it doesn't seem like a difficult fix. I tried taking the TW_MR_DATA code, modifying it slightly, adding some TW_SR_ACK and NACK_SENT code, but I just ended up breaking it. Now I get nothing :) Ohwell, I'll try again tomorrow.

It seems like this should work though, as it's essentially what the standard Arduino code does. I have a feeling that it's missing some I2C processing though, be it a stop/start/ack or nack.

-Tim

photomankc
Posts: 23
Joined: Sat Nov 12, 2011 4:34 am

Re: Wire onReceive(handler) issue

Post by photomankc » Mon Apr 23, 2012 4:51 am

Ryan K wrote:Hello,

It is due to this section of code in the twi.c file in the wire library utilities folder:

Code: Select all

case TW_SR_DATA:
	twi_rxBuffer[0] = ptwi->ixRcv.reg;
	twi_onSlaveReceive(twi_rxBuffer, 1);
	// Release clock line
	ptwi->ixCon.set = (1 << _I2CCON_SCLREL);
	ptwi->ixStat.clr = (1 << _I2CSTAT_I2COV) | (1 << _I2CSTAT_RBF);
	break;
The twi_onSlaveReceive(twi_rxBuffer, 1); always has a 1 as the parameter and will be called every time data is received. Hopefully I'll have time soon to adjust that section to count and wait until a transmission is done.

Best Regards,
Ryan K
Digilent

Any ideas when we might expect this? It would completely break my current code but would be well worth it not to have to count bytes for every command.

Ryan K
Posts: 79
Joined: Fri Feb 10, 2012 12:36 am

Re: Wire onReceive(handler) issue

Post by Ryan K » Fri Apr 27, 2012 1:41 am

Hello,

I haven't had a chance to take a good look at it, I've been quite busy here. I'll see what I can do in the upcoming weeks. I hope to have a fix for it by mid May.

Best Regards,
Ryan K
Digilent

TheInterestingObject
Posts: 1
Joined: Tue Jan 15, 2013 12:42 am

Re: Wire onReceive(handler) issue

Post by TheInterestingObject » Thu Jan 17, 2013 11:01 pm

This problem still persists. Is there anyone out there that has a fix for this problem? Is there a new time frame for when this library might get an official fix?

avenue33
Site Admin
Posts: 298
Joined: Sat Jul 16, 2011 10:19 am
Contact:

Re: Wire onReceive(handler) issue

Post by avenue33 » Tue Jan 22, 2013 8:05 pm

I faced the very same problem and found the issue was referenced in the forum.
The twi_onSlaveReceive(twi_rxBuffer, 1); always has a 1 as the parameter and will be called every time data is received. Hopefully I'll have time soon to adjust that section to count and wait until a transmission is done.
Ryan, could you please give a look at the twi.c file? Thanks!

avenue33
Site Admin
Posts: 298
Joined: Sat Jul 16, 2011 10:19 am
Contact:

Re: Wire onReceive(handler) issue

Post by avenue33 » Sun Jan 27, 2013 12:41 am

I opened an issue at the GitHub repository.

See :arrow: https://github.com/chipKIT32/chipKIT32-MAX/issues/310

mkirby
Posts: 13
Joined: Thu Oct 18, 2012 11:30 pm

Re: Wire onReceive(handler) issue

Post by mkirby » Thu Feb 07, 2013 8:06 pm

Just a heads up to everybody on this thread.....I'm an employee for Digilent, and I am currently looking into fixing this issue.

Post Reply