2021-11-02 11:45 PM
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?
2021-11-03 09:04 AM
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.
2021-11-04 09:14 PM
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.
2021-11-05 01:56 AM
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.
2021-11-08 01:34 AM
I double checked my code, so I think I have already done all that I need to do.