HAL SMBUS How to Use

group SMBUS_How_To_Use

How to use the SMBUS HAL module driver

  1. Declare a hal_smbus_handle_t handle structure and initialize the SMBUSx driver with an I2C HW instance by calling the HAL_SMBUS_Init(). The SMBUSx clock is enabled inside the HAL_SMBUS_Init() if USE_HAL_SMBUS_CLK_ENABLE_MODEL > HAL_CLK_ENABLE_NO.

  2. Configure the low level hardware (GPIO, CLOCK, NVIC…etc):

    • Enable the SMBUSx clock if USE_HAL_SMBUS_CLK_ENABLE_MODEL = HAL_CLK_ENABLE_NO

    • SMBUSx pins configuration :

      • Enable the clock for the SMBUSx GPIOs

        • Configure SMBUSx pins as alternate function open-drain

      • NVIC configuration if you need to use interrupt process

        • Configure the SMBUSx interrupt priority

        • Enable the NVIC SMBUSx IRQ Channel

  3. Configure the Communication Clock Timing (same calculation as I2C), Own Address1, Device mode by calling HAL_SMBUS_SetConfig.

  4. Configure and/or enable advanced features. For instance, HAL_SMBUS_EnableAnalogFilter(), HAL_SMBUS_SetDigitalFilter(), HAL_SMBUS_SetConfigOwnAddress2(), HAL_SMBUS_EnableOwnAddress2(), …APIs. All these advanced configurations are optional (not mandatory).

  5. For SMBUSx IO operations modes, only interrupt is available within this driver as the SMBUS protocol requires the application to perform exchange with a byte granularity within the slave device.

  6. Callbacks definition in Interrupt

    • When the compilation define USE_HAL_SMBUS_REGISTER_CALLBACKS is set to 1, the user can configure dynamically the driver callbacks, via its own method:

If one needs to unregister a callback, register the default callback via the registration function.

By default, after the HAL_SMBUS_Init() and when the state is HAL_SMBUS_STATE_INIT, all callbacks are set to the corresponding default weak functions.

Callbacks can be registered in handle global_state HAL_SMBUS_STATE_INIT and HAL_SMBUS_STATE_IDLE.

When the compilation define USE_HAL_SMBUS_REGISTER_CALLBACKS is set to 0 or not defined, the callback registration feature is not available and weak callbacks are used, represented by the default value in the table above.

  1. Acquire/Release the SMBUS bus

    • When the compilation flag USE_HAL_MUTEX is set to 1, it allows the user to acquire/reserve the whole I2C bus for executing process . The HAL Acquire/Release are based on the HAL OS abstraction layer (stm32_hal_os.c/.h osal) :

    • When the compilation flag USE_HAL_MUTEX is set to 0 or not defined, HAL_SMBUS_AcquireBus() and HAL_SMBUS_ReleaseBus() are not available.