cancel
Showing results for 
Search instead for 
Did you mean: 

How to establish SMBus communication with a smart battery by using STM32Cube FW-F4 V1.25.2 on STM32F429 MCU?

DFeng.2
Associate II

Hi All,

The following is wha I met.

I have a development board with STM32F429 MCU. I'd like to establish SMBus communication to a smart battery with this development board. PH4 and PH5 are used as SCL and SDA output of smbus2.

I used the following APIs from stm32 hal lib.

HAL_SMBUS_Init, HAL_SMBUS_ConfigDigitalFilter, HAL_SMBUS_Master_Transmit_IT, HAL_SMBUS_IsDeviceReady, and HAL_SMBUS_EV_IRQHandler.

Here's the main part of my code:

void SMBus_Init(tI2C_INDEX index)

{

  assert(index < eI2C_MAX);

  gSMBus_Handle[index].Instance = gI2C_Base[index];

  gSMBus_Handle[index].Init.ClockSpeed = 100000; // 100kHz

  gSMBus_Handle[index].Init.AnalogFilter = SMBUS_ANALOGFILTER_ENABLE;

  gSMBus_Handle[index].Init.OwnAddress1 = 2;

  gSMBus_Handle[index].Init.AddressingMode = SMBUS_ADDRESSINGMODE_7BIT;

  gSMBus_Handle[index].Init.DualAddressMode = SMBUS_DUALADDRESS_DISABLE;

  gSMBus_Handle[index].Init.OwnAddress2 = 0;

  gSMBus_Handle[index].Init.GeneralCallMode = SMBUS_GENERALCALL_DISABLE;

  gSMBus_Handle[index].Init.NoStretchMode = SMBUS_NOSTRETCH_DISABLE;

  gSMBus_Handle[index].Init.PacketErrorCheckMode = SMBUS_PEC_ENABLE;

  gSMBus_Handle[index].Init.PeripheralMode = SMBUS_PERIPHERAL_MODE_SMBUS_HOST;

  if (HAL_SMBUS_Init(&gSMBus_Handle[index]) != HAL_OK)

  {

    while(1);//Error_Handler();

  }

  /** Configure Digital filter 

  */

  if (HAL_SMBUS_ConfigDigitalFilter(&gSMBus_Handle[index], 0) != HAL_OK)

  {

   while(1);//Error_Handler();

  }

}

void I2C2_EV_IRQHandler(void)

{

  HAL_SMBUS_EV_IRQHandler(&gSMBus_Handle[eI2C2]);

}

When I called HAL_SMBUS_Master_Transmit_IT or HAL_SMBUS_IsDeviceReady, no clock signal was output from PH4 (I used scope to check SCL and SDA signals).

The following is the register values after executing SMBus_Init.

I2C CR1 = 0x003B

I2C CR2 = 0x002D

I2C DR = 0x0000

I2C SR1 = 0x0000

I2C SR2 = 0x0000

I2C CCR = 0x00E1

I2C TRISE = 0x002E

I2C FLTR = 0x0000

Could you tell me what I missed or which part I didn't do correctly, please?

4 REPLIES 4
Bubbles
ST Employee

Hi @DFeng.2​ ,

have you configured the GPIO to the correct AF?

Anyway, I recommend to take a look at the AN4502 - even though the F4 integrates an older I2C interface which is not compatible with the X-CUBE-SMBUS demo and is limited to 100kHz max SMBus speed.

BR,

J

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Thank you for you reply!

Here's my code to configure GPIO:

GPIO_InitTypeDef gpio_init_struct;

  gpio_init_struct.Pin = SMBUS2_SCL | SMBUS2_SDA;

  gpio_init_struct.Mode = GPIO_MODE_AF_OD;

  gpio_init_struct.Pull = GPIO_NOPULL;

  gpio_init_struct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

  gpio_init_struct.Alternate = GPIO_AF4_I2C2;

  HAL_GPIO_Init(SMBUS2_PORT, &gpio_init_struct);

In addition, I will also try the stm32 smbus stack provided in AN4502 package with the SMBus interface in the STM32F4 HAL lib.

Another typical error users do is not to enable the peripheral clock in the RCC registers. You need to have clock enabled for the GPIO too.

I have to warn you, I do not think it would be easy to make the stack from AN4502 work with the F4. It's not intended to support F4. But you can take it as an inspiration of course.

If you continue to have trouble with interface setup, try generating the initialization code using the STM32CubeMX.

J

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

I double checked my code, so I think I have already done all that I need to do.