SOLVED MY OWN IDIOCY ... But including post anyway incase anybody else is as stupid as me ...
Issue - I2C Bus - protocol analyser not matching array content after i2cMasterTransmitTimeout/i2cMasterReceiveTimeout
Solution - Turn up sample rate on analyser
... I was accidentally using 16Mhz on the analyser and seeing differences. Just as I wrote that fact, I thought I would up to 24Mhz and my data corruption went away. Given I didn't know the bus rate as I've included the magic 0x00902025 config value I didn't think
Background
I have been working on a bit of code that simply reads accelerometer information across the I2C bus. I had a play with a mbed board remotely connected to the ic2 bus pins from the stm32f3discovery and had it up and running in 30 seconds. After working out the chibios way, I spent the next 1/2 day trying to work out why it didn't work properly.
It appears that if I execute i2cMasterTransmitTimeout twice in close succession, I get corrupted data in my array.
By corrupted data, I mean that the logic analyser trace i'm looking at doesn't match the bytes recorded in the array.
If I add a 20ms sleep between the first and second call, then it works and I have a matching output from the analyser and my array contents.
Can anyone shed any light on what i'm doing wrong and why a sleep between calls makes the array contents valid.
Examples of errors I've seen - 64 comes out as 0 sometimes, 192 is often corrupted and sometimes seeing values of 49 instead of 50 etc - so not sure if its analyser being off by a byte for example or the actual device not working.
The sample below shows:
Analyser: 0 - 253 - 64 - 255 - 128 - 187
chPrintf : 64 - 253 - 0 - 255 - 0 - 188
Note that the other analyser traffic for the initial config statements is correct and both match every time with a 20ms delay set.
Alanyser is sampling at 16Mhz
Example code snippet below of all the important bits and the commented sleep:
Code: Select all
static const I2CConfig i2cAccelConfig = {
0x00902025, //voodoo magic
0,
0
};
static unsigned char _txbuf[16];
static unsigned char _rxbuf[16];
_txbuf[0] = 0x20;
_txbuf[1] = 0x57;
i2cStart (&I2CD1, &i2cAccelConfig);
i2cMasterTransmitTimeout(&I2CD1, 0x19, _txbuf, 2, _rxbuf, 0, 1000 );
_txbuf[0] = 0x28 | 0x80;
int retcode = i2cMasterTransmitTimeout(&I2CD1, 0x19, _txbuf, 1, _rxbuf, 0, 1000 );
// chThdSleepMilliseconds(20);
i2cMasterReceiveTimeout(&I2CD1, 0x19, _rxbuf, 6, 1000 );
chThdSleepMilliseconds(1000);
chprintf((BaseSequentialStream*) &SD1, "RC %d - LSM303DLHC Reg:0x28+ = %u,%u,%u,%u,%u,%u\n\r", retcode,
_rxbuf[0], _rxbuf[1], _rxbuf[2], _rxbuf[3], _rxbuf[4], _rxbuf[5]);Sample Analyser Capture:
Sample from terminal in eclipse (chprintf output):