cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F415 weird I2C behaviour

Dustin Lehmann
Associate II
Posted on March 14, 2018 at 09:24

Hello,

I am using a STM32F415RGT6 embedded in the 1Bitsy Board. I want to set up the I2C Peripheral in order to read some data from a sensor.

My example code:

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,

ENABLE

);

GPIO_InitTypeDef

gpioInit;

GPIO_StructInit(&gpioInit);

gpioInit.

GPIO_Mode

=

GPIO_Mode_AF

;

gpioInit.

GPIO_Pin

= GPIO_Pin_6 | GPIO_Pin_7;

gpioInit.

GPIO_PuPd

=

GPIO_PuPd_UP

;

gpioInit.

GPIO_Speed

= GPIO_Speed_25MHz;

GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1);

GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_I2C1);

GPIO_Init(GPIOB, &gpioInit);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,

ENABLE

);

I2C_DeInit(I2C1);

I2C_InitTypeDef

I2C_InitStructure;

I2C_StructInit(&I2C_InitStructure);

/* I2C configuration */

I2C_InitStructure.

I2C_Mode

= I2C_Mode_I2C;

I2C_InitStructure.

I2C_ClockSpeed

= 100000;

I2C_InitStructure.

I2C_DutyCycle

= I2C_DutyCycle_2;

I2C_InitStructure.

I2C_OwnAddress1

= 0x01;

I2C_InitStructure.

I2C_Ack

= I2C_Ack_Enable;

I2C_InitStructure.

I2C_AcknowledgedAddress

= I2C_AcknowledgedAddress_7bit;

I2C_Init(I2C1, &I2C_InitStructure);

I2C_Cmd(I2C1,

ENABLE

);

while

(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY))

;

/* Generate Start, send the address and wait for ACK */

I2C_GenerateSTART(I2C1,

ENABLE

);

while

(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT))

;

I2C_Send7bitAddress(I2C1, 0xE0, I2C_Direction_Transmitter);

while

(!I2C_CheckEvent(I2C1,

I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))

;

After that I want to write a 0x00, but the code always hangs in the last line, apparently the Master never reads the acknowledge. The I2C status registers always read:

I2C1 -> SR1 = 1024

I2C1 -> SR2 = 3

which means that the Acknowledge Failure bit is always set. If I analyze it using my Saleae I get the following:

0690X0000060A7aQAE.png

The Slave sends the ACK, but the STM32F415 cannot read it.

The weird thing: If I try the same code on my F407 - Disco (only with clock set to 400khz, but it's the same behaviour on both MCUs regardless of Speed), it works flawlessly:

0690X0000060A7VQAU.png

All other peripherals work fine. I already tried several workarounds, but the AF bit is always set, regardless of method. I hope you can help me.

Best Regards

#i2c #acknowledge-byte #stm32f4 #i2c-setup #stm32f415 #i2c-bug
12 REPLIES 12
Posted on March 14, 2018 at 15:00

What could be different between the F415 on the 1Bitsy board and the F407 on the discovery board in terms of I2C?

Assuming the same (logical) peripheral and similar, proper initialization :

  • external pull-up resistors
  • the I2C slave device
  • bus length
  • other external components

Assuming bus length is not yet an issue, you can check for other components connected to the SDA/SCL pins, and to the wiring of the slave itself. For instance, some devices have a dual-mode interfaces (SPI / I2C), with a pin to select the bus mode.

And quite often boards have already peripherals connected to some pins, precluding other uses. ST's discovery boards with LCD display are a good example of the latter.

Dustin Lehmann
Associate II
Posted on March 14, 2018 at 21:55

Well, I found the problem. It just don't work with the F415 with the logic analyzer measuring SCL and SDA. If I remove the analyzer it works. With the F407 it does not matter if it is plugged in or not. Well, that were 8 wasted hours of debugging. Thanks to all who tried to help me!

Posted on March 14, 2018 at 23:16

That sounds like marginal timing settings.

In the 'F4xx, the I2C has quite a couple of timing settings. Can you please post those read-out values?

JW