cancel
Showing results for 
Search instead for 
Did you mean: 

SM BUS COMMUNICATION WITH STM32F103RBT6

HRedd.1
Associate II

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?

12 REPLIES 12

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.

Rama; this looks great, is there a web address for these guidelines, or should i just copy/paste into a 'local' notebook?

Danke, merçi, shukran...and thanks!

en.stm32cubef1-v1-8-6 There is no stm32f1xx_hal_smbus.c file