Log in

View Full Version : receiving data from Arduino over i2c?


fovea1959
22-02-2014, 17:49
this is an experiment, not needed to get bot ready for competition, but mentor curiousity:

I think I have writing to the Arduino down: if I do an i2c.write, the Arduino gets a receiveEvent, and the first byte that the Arduino receives is the register number I specified on the I2C.write() call.

Getting data back is hurting my brain. If I issue an i2c.read, I would expect for the Arduino to get a requestEvent. It gets a receiveEvent, and the data on that event is the register number. The problem is that even if I write data on the Arduino from the receiveEvent handler, it doesn't seem to get back to the cRIO; cRIO just seems to hang.

Does anyone have a good example of a cRIO receiving data from an Arduino over I2C that they can share?

Mike Bortfeldt
23-02-2014, 10:49
Doug,

The following two threads may help point you in the right direction. My first thought is that you are running into an error I saw last year regarding a bug in the wire library where when you issue a read command, the Arduino outputs a STOP in between the write (register address) and the read portion of the transaction. More detail can be found in the first thread along with a work around. However, from what I recall, this problem did not hang the cRIO, it just returned a transaction failure, so your issue may be different. Let me know if this helps. Also, if you can post your code, I could take a closer look.

Mike

Link1
Link2

fovea1959
23-02-2014, 11:29
Mike: thanks. I think I was getting bit by the Wire STOP bug. Not sure about the hang; it may have been something else...

I have your code running as expected; I'll look at finding the Wire patch to see if that simplifies things (don't like the 2-transaction code, but at least I can get data back from the Arduino.

fovea1959
23-02-2014, 12:59
Mike, is this the correct bug/patch for the Arduino wire library?

https://github.com/arduino/Arduino/issues/848


This is because there is a bug in the Arduino wire library that sends a "STOP" after the first part of the transaction even though it is setup as a SLAVE. There is a fix for this, but I have not had the chance to test it out.

Mike Bortfeldt
23-02-2014, 13:48
Doug,

That is the correct problem and I believe the "accepted" fix. The line removed is in the TW_SR_STOP case. There is no reason to call twi_stop(); as the i2c hardware will hold the clock line low until the line twi_releaseBus(); is called a few statements later.

Mike

BTW - let me know if it actually works.

fovea1959
23-02-2014, 14:58
I will. Found the source code; need to figure out how to rebuild the library after commenting out the offending twi_stop().

fovea1959
23-02-2014, 15:54
...and giving up. Doesn't matter what I do to the code in ARDUINO\library\Wire\, nothing seems to change. I even put deliberate syntax errors in there so that the compile would blow up (to verify that my changes are being compiled), nothing happens, so it appears to me that simply fixing ARDUINO\library\Wire\utility\twi.c doesn't make the changed source get recompiled and linked into the final program.

Not familiar enough with the Arduino build process to understand how to make the patch happen. Everything I Google indicates it should just be change the library source code and reload....

Will use your workaround...

Mike Bortfeldt
23-02-2014, 16:16
Doug,

Have you tried completely closing the IDE and then reopening the sketch? I've found that many times it doesn't recognize a new source version except at startup.

Mike

fovea1959
23-02-2014, 21:18
that didn't seem to help, but I moved it from the Arduino install directory to user's Arduino directory, and changes started taking effect.

It's hanging: I'll run some more tests and see if I can figure out what's going on....