I2C No ack with 80% devices on STM32F4

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
[email protected]
Posts: 33
Joined: Sat Aug 25, 2012 9:53 pm
Been thanked: 1 time

I2C No ack with 80% devices on STM32F4

Post by [email protected] »

Hi all.

1st of all I want to say thank for doing great job.
I am using chibios for few weeks on STM32F4 Disvovery board.

Unfortunately I am having much problems with I2C devices- 80% of them wont work reporting no ack failure after sending address.
I have even written the i2c devices scanner to prevent wrong address problem.

There are only few devices working like L3G4200D gyro which is ST device.
But for example i could not communicate with such simple devices like PCF8574 sift registry, HMC5883L, ADXL345 etc.

My i2c config is:
static I2CConfig i2cfg1 = {
OPMODE_I2C,
100000,
STD_DUTY_CYCLE,
};

I have chacked the signal on 2 wires with osciloscope and everything seems to be fine- data sent is correct, but no ack received.
screenshot.jpg
screenshot.jpg (104.09 KiB) Viewed 8318 times


My devices are also fine as they are working well with arduino.
I have also tried to add resistors between VCC and SDA, SCL w/o success.

It should not be a hardware problem as all devices works.
So is this a configuration problem?
I could not get it work for few weeks.

Please note that i am testing addresses from 0b0000000 to 0b1111111, so this is why there is a 0x00 address as it is 1st one from testing loop.

Thanks in advance!
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: I2C No ack with 80% devices on STM32F4

Post by Giovanni »

HI,

Hard to give advice being unable to test, do you have external pull ups? could it be some kind of electrical problem? try to reduce clock speed and see if it makes any difference.

Giovanni
[email protected]
Posts: 33
Joined: Sat Aug 25, 2012 9:53 pm
Been thanked: 1 time

Re: I2C No ack with 80% devices on STM32F4

Post by [email protected] »

Hi Giovanni!

Thanks for replay.
I have tried almost all frequencies, duty cycles and nothing changed. Some devices works, some not.
Have also tried different voltages (3- 5) from different sources.
All my devices are connected directly to MCU.

I am configuring pads with palSetPadMode(i2c_gpio->sda_gpio_port, i2c_gpio->sda_gpio, PAL_MODE_ALTERNATE(4));
I am also using config files taken from examples and test projects.
Should I add additional flags to mode?
What else should i check in config?

One more thing: using latest trunk.

Not sure should I use external pull ups.

Am I the only one with such problems?

Thanks in advance!
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: I2C No ack with 80% devices on STM32F4

Post by Giovanni »

I think you should use external pull-ups, internal ones are weak. The boards I know have 4.7Kohm to VDD.

Giovanni
User avatar
barthess
Posts: 861
Joined: Wed Dec 08, 2010 7:55 pm
Been thanked: 7 times

Re: I2C No ack with 80% devices on STM32F4

Post by barthess »

I2C requires 10k resistors or less. Internal pullups have 30-50k nominal. I am pretty sure you must use external pullups.
Not sure about
[email protected] wrote: palSetPadMode(i2c_gpio->sda_gpio_port, i2c_gpio->sda_gpio, PAL_MODE_ALTERNATE(4));

you must configure them like "Alternate function open-drain"
[email protected]
Posts: 33
Joined: Sat Aug 25, 2012 9:53 pm
Been thanked: 1 time

Re: I2C No ack with 80% devices on STM32F4

Post by [email protected] »

OMG IT DID THE TRICK!

To solve the problem 3things together are needed:
1. palSetPadMode(i2c_gpio->sda_gpio_port, i2c_gpio->sda_gpio, PAL_MODE_ALTERNATE(4) | PAL_STM32_OTYPE_OPENDRAIN); //NOT ONLY PAL_MODE_ALTERNATE
2. 100R added between MCU SDA/SCL and slave's SDA/SCL (as described in STM32F4 Datasheet)
2. 4K7 added between VDD and SDA/SCL lines (as described in STM32F4 Datasheet)

You are my hero Giovanni!

BTW: I thought that PAL_MODE_ALTERNATE(4) should do all the trick, as AF4 is dedicated for I2C, so why to need specify anything additional? Maybe PAL_MODE_ALTERNATE(4) should also add PAL_STM32_OTYPE_OPENDRAIN mask?

Greetings,
Piotr Klimczak
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: I2C No ack with 80% devices on STM32F4

Post by Giovanni »

Hi,

[email protected] wrote:BTW: I thought that PAL_MODE_ALTERNATE(4) should do all the trick, as AF4 is dedicated for I2C, so why to need specify anything additional? Maybe PAL_MODE_ALTERNATE(4) should also add PAL_STM32_OTYPE_OPENDRAIN mask?


No because not all alternate functions require an open drain output, probably just I2C.

Giovanni
Pilt
Posts: 6
Joined: Sat Sep 15, 2012 2:08 pm

Re: I2C No ack with 80% devices on STM32F4

Post by Pilt »

Hi,

I also had the same problem interfacing to an I2C device. However I solved the issue by changing the relevant outputs to open drain by editing the board.h file. Therefore palSetPadMode(i2c_gpio->sda_gpio_port, i2c_gpio->sda_gpio, PAL_MODE_ALTERNATE(4)); is still correct if your board.h file configurse these pins as opendrain.

However, I set my pins to alternate mode in board.h, which is processed before the call to i2cStart(). This works but is there any negative effect from connecting to a alternative peripheral before activating it?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: I2C No ack with 80% devices on STM32F4

Post by Giovanni »

Hi,

Setting the alternate function before activating the peripheral can create problems.

For example, the USART TX line is set to zero until the clock is activated and this creates a spurious start bit if the alternate is set in board.h (before the driver is activated).

The safest way to initialize peripherals like USART and I2C is the following (I2C case).

1) In board.h set the pins as *normal* outputs with open drain and an acceptable idle logic state (idle is one for I2C if I remember well).
2) Start the peripheral clock using i2cStart(), the pins are still normal outputs and keep their idle state.
3) Now switch to alternate(4) with open drain and the pins state will go from idle to idle, no transients.

Giovanni
Post Reply