STM32F3Discovery - I2C and Anlyser (SOLVED)

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
enly1
Posts: 38
Joined: Wed Oct 09, 2013 10:21 pm

STM32F3Discovery - I2C and Anlyser (SOLVED)

Post by enly1 »

Hi All,

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:
Analyser Sample
Analyser Sample
Chibios-I2c.JPG (91 KiB) Viewed 6878 times


Sample from terminal in eclipse (chprintf output):
USART Printout Sample
USART Printout Sample
Chibios-usart1-out.JPG (14.64 KiB) Viewed 6878 times
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: STM32F3Discovery - I2C and Anlyser (SOLVED)

Post by Giovanni »

You are welcome anyway :)

Giovanni
enly1
Posts: 38
Joined: Wed Oct 09, 2013 10:21 pm

Re: STM32F3Discovery - I2C and Anlyser (SOLVED)

Post by enly1 »

Another update ...

Well I thought I had worked out what the issue was, but realised after some additional testing that it wasn't the cause after all ...

I managed to create timing values from the I2C config spreadsheet provided by STM and did some testing of a slower rate ( the default value I had generated a 680Khz bus rate according to the analyser)

At 100Khz, I was still reading errors with a 24Mhz sample rate, but only following on a soft reset after programming. Hard resets generated correct samples all times.
Assumed it was some instability caused from the way it was being reset, so added a 1 second sleep before starting the I2C bus and the problem went away again.

The problem ONLY manifests itself after a soft reset via the STLinkv2 onboard after programming and MCU Reset (via STlink) and not the reset button on the board.

So no idea what the actual cause of the issue is at a hardware level, but am working around it somewhat - and at least know it is there.

Just thought it worth sharing.

Suspect subsequent readings of the i2C bus would work themselves out after a period, but don't yet have a reader setup to read frequent samples to know - will update if I remember when I do.
enly1
Posts: 38
Joined: Wed Oct 09, 2013 10:21 pm

Re: STM32F3Discovery - I2C and Anlyser (SOLVED)

Post by enly1 »

Next update - without the delay and issuing several requests for the same data (that hasn't changed between reads) the readings appears to show consistently wrong on the analyser in comparison with the output from Chibios code.

So not sure what creates the issue, but now I know its there, will work around it with a sleep for now and keep an eye out for the issue on subsequent uses.

Lost enough time trying to work this one out ... and have very limited time available for this stuff - so back to it.
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: STM32F3Discovery - I2C and Anlyser (SOLVED)

Post by Giovanni »

Hi,

Is it possible that the soft reset blocks an I2C operation in the middle leaving the I2C peripheral in a abnormal state? if this is the case then there is not much you can do on the master side.

Giovanni
enly1
Posts: 38
Joined: Wed Oct 09, 2013 10:21 pm

Re: STM32F3Discovery - I2C and Anlyser (SOLVED)

Post by enly1 »

Hard to say - the initial write operation works as expected with or without the delay.

Will look at options for the stlink to see if I can be more forceful with the reset etc - but given I know it does it and know that there is a workaround, I'm not investigating too hard any more.
Legath
Posts: 21
Joined: Thu Oct 17, 2013 3:24 pm

Re: STM32F3Discovery - I2C and Anlyser (SOLVED)

Post by Legath »

enly1 wrote:Hard to say - the initial write operation works as expected with or without the delay.

Will look at options for the stlink to see if I can be more forceful with the reset etc - but given I know it does it and know that there is a workaround, I'm not investigating too hard any more.



Hi,

i have found the same issue on mac os port of saleae software.
But on linux all works fine.

One more - your voodo magic set i2c in 660KHz state, is abnormal.
My config next

Code: Select all

 static const I2CConfig i2c1cfg = {
  (STM32_TIMINGR_PRESC(9U)|STM32_TIMINGR_SCLDEL(3U)|STM32_TIMINGR_SDADEL(3U)|STM32_TIMINGR_SCLH(3U)|STM32_TIMINGR_SCLL(9U)),
  0,
  0
  };


And one question for Giovanni:

sometimes i have 405KHz instead 400,
is it normal?
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: STM32F3Discovery - I2C and Anlyser (SOLVED)

Post by Giovanni »

Hi,

It is not normal, probably not even possible because the dividers would not allow that frequency (I have to verify this assertion). You should look at the I2C registers when you see that frequency and verify if the content looks any different from the normal case.

I would also consider a problem with measurement.

Giovanni
Post Reply