Sunday, October 13, 2013

Week 8: I2C comms are working!

The majority of this week was spent hammering out issues with the NXT and Arduino. However it all paid off, and now the two devices are finally communicating over I2C!

Previously I had encountered an issue with the SDA line not being driven. This directed me towards the NXT, and upon close inspection it appears I had configured a pragma statement to use the wrong port. For reference, the relevant (and now correct!) line of code is:

#pragma config(Sensor, S1,TIR,sensorI2CCustom)

After debugging this and fixing it, the SDA line now drove data correctly and everything seemed okay on the NXT side. The issue now, infuriatingly enough, was that the Arduino did not seem to be receiving the data correctly. The code uploaded to the ChipKit was simple enough, a simple polling loop waiting for I2C data to come in. Through my debugging, I found that the packet receipt was being recognized, however none of the data was being decoded properly. Recall from the Lego I2C spec that the first packet transmitted by the NXT to a device simply contains the address of the register for the sensor to read/write. The info I was decoding on the ChipKit side was malformed or corrupted somehow, and it was mostly garbage.

To cut to the chase, I'm entirely unsure why the 022 libraries didn't work, and I gave up them. After a few hours of debugging I tried my own reference Arduino Uno on a whim, using an up-to-date Wire library to see if it would work. Surprisingly it did, and my data was now coming through cleanly. To cut my losses and prevent any more time from being wasted, I'm going to recommend using reference Arduinos from now on, and moving forward I'm going to ditch the ChipKit. This will allow me to use the most recent Wire library available, albeit the tradeoff will be fewer pins and a weaker processor.