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.

    • Interrupt mode IO sequential operation. These interfaces allow to manage a sequential transfer with a repeated start condition when a direction change during transfer. A specific option field manages the different steps of a sequential transfer through hal_smbus_xfer_opt_t and are listed below

      • XXXXXXXX_WITH_PEC suffix: Those options are activated by enabling Packet Error Check with HAL_SMBUS_EnablePacketErrorCheck and allows for the Hardware PEC comparison to happen

      • XXXXXXXX_NO_PEC suffix: Those options are activated by default or by disabling Packet Error Check using HAL_SMBUS_DisablePacketErrorCheck and avoid the Hardware PEC comparison to happen

      • HAL_SMBUS_XFER_FIRST_AND_LAST_FRAME: No sequential, means that a stop condition is automatically generated at the end of the single sequence.

      • HAL_SMBUS_XFER_FIRST_FRAME: Sequential, this option allows to manage a sequence with start condition, address and data to transfer without a final stop condition.

      • HAL_SMBUS_XFER_FIRST_AND_NEXT_FRAME: Sequential (Master only), this option allows to manage a sequence with start condition, address and data to transfer without a final stop condition. This allows a call to the same master sequential interface several times ( HAL_SMBUS_MASTER_SEQ_Transmit_IT() followed by another call to HAL_SMBUS_MASTER_SEQ_Transmit_IT() )

      • HAL_SMBUS_XFER_NEXT_FRAME: Sequential, this option allows to manage a sequence with a restart condition, address and with new data to transfer if the direction change or manage only the new data to transfer if no direction change and without a final stop condition in both cases.

      • HAL_SMBUS_XFER_LAST_FRAME: Sequential, same as HAL_SMBUS_XFER_NEXT_FRAME but with a final stop condition in both cases.

      • HAL_SMBUS_XFER_OTHER_FRAME: Sequential (Master only), this option allows to manage a restart condition after each call of the same master sequential interface. User can transfer several bytes one by one with a restart with slave address between each byte using

        • HAL_SMBUS_MASTER_SEQ_Transmit_IT

        • HAL_SMBUS_MASTER_SEQ_Receive_IT Then usage of this option HAL_SMBUS_XFER_OTHER_AND_LAST_FRAME at the last frame to help automatic generation of STOP condition.

    • Different sequential SMBUS interfaces are listed below:

  6. Callbacks definition in Interrupt

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

Callback name

Default value

Callback registration function

MASTER_TxCpltCallback

HAL_SMBUS_MASTER_TxCpltCallback()

HAL_SMBUS_MASTER_RegisterTxCpltCallback()

MASTER_RxCpltCallback

HAL_SMBUS_MASTER_RxCpltCallback()

HAL_SMBUS_MASTER_RegisterRxCpltCallback()

SLAVE_TxCpltCallback

HAL_SMBUS_SLAVE_TxCpltCallback()

HAL_SMBUS_SLAVE_RegisterTxCpltCallback()

SLAVE_RxCpltCallback

HAL_SMBUS_SLAVE_RxCpltCallback()

HAL_SMBUS_SLAVE_RegisterRxCpltCallback()

AddrMatchCallback

HAL_SMBUS_SLAVE_AddrCallback()

HAL_SMBUS_SLAVE_RegisterAddrMatchCallback()

ListenCpltCallback

HAL_SMBUS_SLAVE_ListenCpltCallback()

HAL_SMBUS_SLAVE_RegisterListenCpltCallback()

AbortCpltCallback

HAL_SMBUS_AbortCpltCallback()

HAL_SMBUS_RegisterAbortCpltCallback()

ErrorCallback

HAL_SMBUS_ErrorCallback()

HAL_SMBUS_RegisterErrorCallback()

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 0U 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) :

      • HAL_SMBUS_AcquireBus for acquire the bus or wait for it.

      • HAL_SMBUS_ReleaseBus for releasing the bus.

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