cancel
Showing results for 
Search instead for 
Did you mean: 

Implementation of SMBus communication in STM32F4 Series

PSR1
Associate II

Hi ST,

I'm trying to build SMBus Communication in STM32F407 with a battery IC (bq25720), but I can't read the device ID response.
Could you assist me with this?

 

Thanks,

PSR

19 REPLIES 19

Have you checked with an oscilloscope to see if you see data activity?

Do you have pull-ups on the SDA/SCL pins?

Show your schematic of your connections to that IC.

If smoke escapes your device, put the smoke back in. It'll still work as a conversation piece. If you find my answers useful, click the Accept as Solution button so that way others can see the solution.
Karl Yamashita
Lead III

Instead of screenshots of your code, copy and paste using the </> 

 

If smoke escapes your device, put the smoke back in. It'll still work as a conversation piece. If you find my answers useful, click the Accept as Solution button so that way others can see the solution.

Hello @Karl Yamashita,

        Please see the attached bq25720_evm.pdf. I connected L4 series SDA and SCL lines, and everything I did for your information was implemented; please review the logic analyzer output of SMBus lines.

PSR1_1-1726822211072.png

PSR1_2-1726822583485.png

 

Thanks,

PSR.

 

 

 

(uint8_t*)CMD_ID

Oops, not a  good idea. Consider:

uint8_t cmd_id = CMD_ID;
&cmd_id

 

Hello @PSR1 

 

The value of ret is 2 which mean that the peripheral is busy when calling HAL_SMBUS_Master_Receive_IT(). 

The first transaction is not already finished. 

Hier you are calling interrupt process, so your log should be placed in the complete call back function.

// Define the BQ25720 device address and register address
#define BQ25720_ADDR 0x09
#define CMD_ID 0xFF
uint8_t device_id[2];
SMBUS_HandleTypeDef   hsmbus1;
HAL_StatusTypeDef ret;

void TxComplete_Callback(SMBUS_HandleTypeDef *hsmbus);
void RxComplete_Callback(SMBUS_HandleTypeDef *hsmbus);
void Error_Callback(SMBUS_HandleTypeDef *hsmbus);

void Init_SMBUS_Callbacks(SMBUS_HandleTypeDef *hsmbus)
{
    HAL_SMBUS_RegisterCallback(hsmbus, HAL_SMBUS_TX_COMPLETE_CB_ID, TxComplete_Callback);
    HAL_SMBUS_RegisterCallback(hsmbus, HAL_SMBUS_RX_COMPLETE_CB_ID, RxComplete_Callback);
    HAL_SMBUS_RegisterCallback(hsmbus, HAL_SMBUS_ERROR_CB_ID, Error_Callback);
}
main 
{


Init_SMBUS_Callbacks();
// Send the register address
ret =HAL_SMBUS_Master_Transmit_IT(&hsmbus1, (BQ25720_ADDR << 1), (uint8_t*)CMD_ID , 1, SMBUS_FIRST_FRAME);
if (ret != HAL_OK) {
    // Transmission Error
    Error_Handler();
}

while(1);
}

void TxComplete_Callback(SMBUS_HandleTypeDef *hsmbus)
{
    LOG_INFO("SMBUS Mater Transmitted \r\n" );
    if (HAL_SMBUS_Master_Receive_IT(&hsmbus1, DEVICE_ADDRESS<<1, &device_id[0], 2, SMBUS_LAST_FRAME_NO_PEC); != HAL_OK)
    {
        LOG_INFO("Failed to initiate receive in TX Complete Callback\r\n");
    }
}

void RxComplete_Callback(SMBUS_HandleTypeDef *hsmbus)
{
LOG_INFO("Charger Status : %.2x %.2x\r\n",device_id[0],device_id[1]);
}

void Error_Callback(SMBUS_HandleTypeDef *hsmbus)
{
    uint32_t errorCode = HAL_SMBUS_GetError(&hsmbus1);
    printf("SMBus Error: %lu\n", errorCode);
}

 

Please try with this implementation.

If your question is answered, please close this topic by clicking "Accept as Solution".

Thanks
Omar

Hello @Saket_Om,

Thank you for your response. I attempted to implement the above code; but,

HAL_SMBUS_RegisterCallback

is not enabled. How do I enable?

 

Thanks,

PSR

You can enable it from the stm32f4xx_hal_conf.h 

#define USE_HAL_SMBUS_REGISTER_CALLBACKS 1U

 

If your question is answered, please close this topic by clicking "Accept as Solution".

Thanks
Omar

Hello @Saket_Om ,

I’m utilizing

HAL_SMBUS_RegisterCallback(hsmbus, HAL_SMBUS_MASTER_TX_COMPLETE_CB_ID, TxComplete_Callback);

HAL_SMBUS_RegisterCallback(hsmbus, HAL_SMBUS_MASTER_RX_COMPLETE_CB_ID, RxComplete_Callback);

HAL_SMBUS_RegisterCallback(hsmbus, HAL_SMBUS_ERROR_CB_ID, Error_Callback);

instead of this

    HAL_SMBUS_RegisterCallback(hsmbus, HAL_SMBUS_TX_COMPLETE_CB_ID, TxComplete_Callback);

 

SMBus_master_receive_is not acknowledged, with log status FF FF.

PSR1_0-1726836340049.pngPSR1_1-1726836368866.png

 

Thanks,

PSR.

 

 

 

Hello @PSR1 

It is the normal sequence according to the reference manual RM0090. A NA bit is sent after the last byte received. 

Saket_Om_0-1726838093733.png

 

Your SMBUS setup work fine, however, you need to check if you are receiving the good data. If not, you should investigate more.

 

If your question is answered, please close this topic by clicking "Accept as Solution".

Thanks
Omar

Hi @Saket_Om,

Thank you for your solution.

Which function can I use to read the registers of BQ25720's battery IC?

 

Thanks,

PSR.