Unexpected I2C behaviour - STM32F4 Discovery

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

Moderator: RoccoMarco

Post Reply
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Unexpected I2C behaviour - STM32F4 Discovery

Post by Tectu »

Hello folks,

I'm trying to communicate with an I²C chip which is connected to the I2CD1 of the STM32F4-Discovery board (PB8 and PB9). I use the following code:

Code: Select all

#include "ch.h"
#include "hal.h"
#include "chprintf.h"

static const I2CConfig i2ccfg = {
   OPMODE_I2C,
   400000,
   FAST_DUTY_CYCLE_2,
};

uint8_t txbuf[2];
uint8_t rxbuf[2];

int main(void) {
   halInit();
   chSysInit();

   palSetPadMode(GPIOB, 8, PAL_MODE_ALTERNATE(4));   /* SCL */
   palSetPadMode(GPIOB, 9, PAL_MODE_ALTERNATE(4));   /* SDA */

   txbuf[0] = 0x00;
   txbuf[0] = 0x00;

   i2cStart(&I2CD1, &i2ccfg);

   while(1) {
      i2cAcquireBus(&I2CD1);
      i2cMasterTransmit(&I2CD1, 0x82 >> 1, txbuf, 1, rxbuf, 2);
      i2cReleaseBus(&I2CD1);

      chprintf(shell, "Received: 0x%02X 0x%02X\r\n", rxbuf[0], rxbuf[1]);

      chThdSleepMilliseconds(500);
   }

   return 0;
}



I get 0x00 0x00 all the time (I'm expected to get 0x0811). So here's what's my scope says to that:

Image

I'm not sure from where this 'half' state comes from. There's no other hardware on these two pins which would make any problems with the I²C interface. But the main issue I'm seing here is that the STM32 does not generate the clock for the phase where the slave is supposed to return two bytes. The chip I'm trying to interface is an STMPE811.

Any ideas? :D


~ Tectu
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Unexpected I2C behaviour - STM32F4 Discovery

Post by Giovanni »

Hi,

Try asking Barthess, I can't assist with that.

Giovanni
E-B Felix
Posts: 9
Joined: Thu May 10, 2012 10:30 am

Re: Unexpected I2C behaviour - STM32F4 Discovery

Post by E-B Felix »

-> from where this 'half' state comes from

Looks like the SDA PIN from the Master is not Open-Drain...The slave is trying to pull down the ACK bit to Gnd while the master drives a hig level at the same time.
But it should be Open-Drain high impedance.

Check the pin settings for the master.

Cheers
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: Unexpected I2C behaviour - STM32F4 Discovery

Post by Tectu »

As E-B Felix suggested corretly, the master wasn't in Open Drain mode. The following two lines fixed this issue:

Code: Select all

palSetPadMode(GPIOB, 8, PAL_MODE_ALTERNATE(4) | PAL_STM32_OTYPE_OPENDRAIN);   /* SCL */
palSetPadMode(GPIOB, 9, PAL_MODE_ALTERNATE(4) | PAL_STM32_OTYPE_OPENDRAIN);   /* SDA */


However, The result didn't change much. I now got some very triangular waveform which didn't even go up to 3.3V completely. After contacting barthess in IRC (#chibios on irc.freenode.net:6667), He told me that my hardware might be missing pull-ups. I then decided to use the internal pull-ups of the STM32F4. Sadly, they didn't work that well and barthess suggested me to use external pull-ups and so I soldered two 10k resistors, as I know it from the AVR world, onto my board. However, this still didn't solve the problem. A replacement by 4k7 pullups as barthess suggested again did finally do the trick and everything is working now.

Thank you very much, guys! :)


~ Tectu
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Unexpected I2C behaviour - STM32F4 Discovery

Post by Giovanni »

Barthess is right, you need external pullups, the internal ones are VERY weak.

Giovanni
Post Reply