Skip to main content
Visitor II
January 5, 2022
Question

SM BUS COMMUNICATION WITH STM32F103RBT6

  • January 5, 2022
  • 6 replies
  • 4339 views

Hello Experts,

Here am using one slave which has SMBUS interface for data transmitting.

I am using STM32F103RBT6 for SM BUS and generating the code with STM CUBE MX ,but am unable generate the working code with it and its generating error code.

I updated the STM32F1 Patch and Cube Mx also still am unable to generate the code.

Does STM32F1 has SMBUS with HAL libraries?

    This topic has been closed for replies.

    6 replies

    Technical Moderator
    January 5, 2022

    Hello @HRedd.1​ ,

    There is no Driver SMBus for STM32F1.

    Can you please share the exact error with your project, this will help me to check the issue.

    Imen

    HRedd.1Author
    Visitor II
    January 6, 2022

    thanks for the reply.....

    this the code am getting after generation.

    static void MX_I2C1_SMBUS_Init(void)

    {

     /* USER CODE BEGIN I2C1_Init 0 */

     /* USER CODE END I2C1_Init 0 */

     /* USER CODE BEGIN I2C1_Init 1 */

     /* USER CODE END I2C1_Init 1 */

     /* USER CODE BEGIN I2C1_Init 2 */

     /* USER CODE END I2C1_Init 2 */

    }

    its not generating the full code..

    at present am using cube mx 6.1.1...

    Please update me the the version where i can generate full code for SMBUS with STM32F1..

    HRedd.1Author
    Visitor II
    January 6, 2022

    When can I expect ST to solve this issue?

    Technical Moderator
    January 6, 2022

    Hi @HRedd.1​ ,

    It seems the I2C configuration code under main.c is missed.

    Could you please confirm if this issue is reproduced with the latest version of CubeMX (v6.4.0) ?

    I'm adding CubeMx expert ​to the loop to check this issue.

    Hi @Khouloud ZEMMELI​ ,

    Can you please review this issue related to CubeMx tool - depends on the ticket ID 100162 -

    (PS: ticket ID 100162, this is an internal tracking number and is not accessible or usable by customers)

    Thanks

    Imen

    ST Employee
    January 6, 2022

    Hello @Imen DAHMEN​ ,

    This issue is not related to CubeMX, it is actually a FW issue, the SMBus driver is not yet available for F1 series.

    Could you please report this issue to the FW team ?

    Thanks,

    Sara.

    Technical Moderator
    January 6, 2022

    Thanks @Sara BEN HADJ YAHYA​,

    The issue is already escalated to FW team.

    Imen

    Graduate II
    June 13, 2024

    hey can you please confirm if the its 2024 if the support for stm32f103 is been added or not?

    Visitor II
    June 13, 2024

    Yes, the STM32F1 series microcontrollers, including the STM32F103RBT6, do support the SMBus (System Management Bus) interface. However, there are a few things to keep in mind:

    1. HAL Libraries Support: The HAL (Hardware Abstraction Layer) libraries provided by STMicroelectronics include support for SMBus communication protocols. This means you can use HAL functions to configure and communicate over the SMBus interface.

    2. CubeMX Configuration: When using STM32CubeMX to generate code, you need to ensure that you configure the correct peripheral (I2C in this case, since SMBus is based on I2C) and set the appropriate parameters such as clock speed, addressing mode, and other parameters specific to your SMBus communication requirements.

    3. Error Generation: If STM32CubeMX is generating errors when you try to configure the SMBus/I2C peripheral, there could be several reasons:

      • Make sure you have selected the correct STM32F1 series device (STM32F103RBT6) in STM32CubeMX.
      • Ensure that the firmware package and STM32CubeMX version you are using are compatible with the STM32F1 series and the features you intend to use.
    4. Update and Compatibility: You mentioned updating STM32F1 patch and CubeMX, which is good practice. Ensure that the updates are correctly applied and that there are no compatibility issues between the selected device and the tool versions.

    To troubleshoot the issue further:

    • Double-check your STM32CubeMX configuration for the I2C (SMBus) peripheral.
    • Look at the error messages specifically to understand what might be causing the code generation to fail.
    • Verify that the clock configuration, GPIO pin assignments, and other settings are correctly configured in STM32CubeMX.

    If you provide specific error messages or details about the issues you're encountering during code generation, I can help you troubleshoot further.

    Graduate II
    June 13, 2024

    Sadly yes im getting error in the code generation of SMBUS peripheral, can you please help me on this & is there a documentation where we have steps to implement this.

    Visitor II
    June 14, 2024

    Certainly! If you’re facing issues with code generation for SMBus using STM32CubeMX and the STM32F1 series, here's a step-by-step guide to help you through the process, including addressing common errors. I'll also include references to documentation and potential resources for further help.

     

    Step-by-Step Guide to Implement SMBus on STM32F103RBT6

    1. Update and Prepare Tools

    • STM32CubeMX: Ensure you have the latest version. Download from STMicroelectronics.
    • STM32CubeF1 Package: Download and update the STM32CubeF1 firmware package for the F1 series from STMicroelectronics.

    2. Configure SMBus in STM32CubeMX

    1. Create a New Project:

      • Open STM32CubeMX.
      • Select STM32F103RBT6 as your target MCU.
    2. Enable I2C Peripheral:

      • Go to Pinout & Configuration tab.
      • Click on the I2C1 (or another I2C peripheral) to enable it.
      • Assign SCL and SDA to appropriate GPIO pins.
    3. Configure I2C for SMBus:

      • In the Configuration tab for the I2C peripheral, look for SMBus settings. Usually, you will:
        • Enable SMBus mode.
        • Set Clock Speed (typically 100 kHz or 400 kHz).
        • Configure Addressing Mode (usually 7-bit).
        • Enable PEC if needed.
    4. Set Project and Generate Code:

      • Go to the Project tab.
      • Enter the project name, location, and select your IDE (e.g., STM32CubeIDE).
      • Click Generate Code.

    3. Common Errors and Solutions

    1. “Undefined Reference to HAL_SMBUS_Init”

      Cause: Missing HAL SMBus source file in your project or incorrect HAL module enabling.

      Solution:

      • Ensure stm32f1xx_hal_smbus.c is included in your project source files.
      • In stm32f1xx_hal_conf.h, ensure #define HAL_SMBUS_MODULE_ENABLED is defined.
    2. “HAL Error” During Initialization

      Cause: Incorrect peripheral configuration.

      Solution:

      • Verify the I2C clock settings in CubeMX.
      • Check GPIO pin configuration for SCL and SDA.
      • Ensure other peripheral settings (e.g., duty cycle, addressing mode) are correct.
    3. Compilation Errors Related to CubeMX Code

      Cause: Potential bugs in the generated code or conflicts in configuration.

      Solution:

      • Update to the latest version of STM32CubeMX and firmware.
      • Compare the generated code against working examples or reference projects.

     

     

    4. Manually Check and Edit Generated Code

    1. Initialization Code (main.c):

      • Ensure MX_I2C1_Init() is correctly initializing the I2C for SMBus.
      c
      Copy code

      /* I2C1 init function */
      void MX_I2C1_Init(void)
      {
      hi2c1.Instance = I2C1;
      hi2c1.Init.ClockSpeed = 100000; // Adjust as needed
      hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
      hi2c1.Init.OwnAddress1 = 0x08; // Example own address
      hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
      hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
      hi2c1.Init.OwnAddress2 = 0;
      hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
      hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;

      if (HAL_I2C_Init(&hi2c1) != HAL_OK)
      {
      Error_Handler();
      }

      // Additional SMBus-specific initialization
      hi2c1.Instance->CR1 |= I2C_CR1_SMBUS;
      // Enable PEC if needed
      // hi2c1.Instance->CR1 |= I2C_CR1_ENPEC;
      }

    2. Use SMBus Functions:

      • Use HAL SMBus functions for communication:
      c
      Copy code
      HAL_SMBUS_Master_Transmit(&hi2c1, SMBUS_ADDRESS, data, size, timeout);
      HAL_SMBUS_Master_Receive(&hi2c1, SMBUS_ADDRESS, data, size, timeout);

    5. Verify Peripheral Configuration

    • Clock Configuration: Ensure I2C clock settings are accurate. Mismatched settings can cause initialization failures.
    • GPIO Settings: Confirm SCL and SDA pins are correctly set for alternate function I2C.

    6. Documentation and Resources

    1. STM32F103 Reference Manual:

    2. HAL Documentation:

    3. Example Projects:

    4. Forums and Community Help:

     

     

    7. Example Code for SMBus Communication

    Here’s a simple example to perform SMBus communication:

     

    c
     

    #include "main.h"

    // Define SMBus address
    #define SMBUS_ADDRESS 0x1A

    I2C_HandleTypeDef hi2c1; // I2C handler

    void SystemClock_Config(void);
    static void MX_GPIO_Init(void);
    static void MX_I2C1_Init(void);

    int main(void)
    {
    HAL_Init();
    SystemClock_Config();
    MX_GPIO_Init();
    MX_I2C1_Init();

    uint8_t data[] = {0x00, 0xFF}; // Example data
    if (HAL_SMBUS_Master_Transmit(&hi2c1, (uint16_t)(SMBUS_ADDRESS << 1), data, sizeof(data), HAL_MAX_DELAY) != HAL_OK)
    {
    // Transmission Error
    Error_Handler();
    }

    // Additional application code

    while (1)
    {
    // Main loop
    }
    }

    static void MX_I2C1_Init(void)
    {
    hi2c1.Instance = I2C1;
    hi2c1.Init.ClockSpeed = 100000;
    hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
    hi2c1.Init.OwnAddress1 = 0x08;
    hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
    hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
    hi2c1.Init.OwnAddress2 = 0;
    hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
    hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;

    if (HAL_I2C_Init(&hi2c1) != HAL_OK)
    {
    Error_Handler();
    }
    }

     

     

    Conclusion

    Implementing SMBus on the STM32F103RBT6 involves careful configuration in STM32CubeMX and proper initialization and handling in the generated code. Addressing errors during code generation typically involves checking your configuration, ensuring the correct inclusion of HAL modules, and updating tools. Use the provided resources and example code to verify your setup and resolve any issues. If problems persist, consulting the community or STMicroelectronics support can provide additional assistance.