STM32F4xx HAL Documentation
Hardware Abstraction Layer for STM32F4 familiy
Loading...
Searching...
No Matches
stm32f4xx_hal_smbus.c File Reference

SMBUS HAL module driver. This file provides firmware functions to manage the following functionalities of the System Management Bus (SMBus) peripheral, based on SMBUS principals of operation : More...

#include "stm32f4xx_hal.h"
Include dependency graph for stm32f4xx_hal_smbus.c:

Go to the source code of this file.

Macros

#define SMBUS_TIMEOUT_FLAG   35U
 
#define SMBUS_TIMEOUT_BUSY_FLAG   25U
 
#define SMBUS_NO_OPTION_FRAME   0xFFFF0000U
 
#define SMBUS_SENDPEC_MODE   I2C_CR1_PEC
 
#define SMBUS_GET_PEC(__HANDLE__)
 
#define SMBUS_STATE_MSK   ((uint32_t)((HAL_SMBUS_STATE_BUSY_TX | HAL_SMBUS_STATE_BUSY_RX) & (~(uint32_t)HAL_SMBUS_STATE_READY)))
 
#define SMBUS_STATE_NONE   ((uint32_t)(HAL_SMBUS_MODE_NONE))
 
#define SMBUS_STATE_MASTER_BUSY_TX   ((uint32_t)((HAL_SMBUS_STATE_BUSY_TX & SMBUS_STATE_MSK) | HAL_SMBUS_MODE_MASTER))
 
#define SMBUS_STATE_MASTER_BUSY_RX   ((uint32_t)((HAL_SMBUS_STATE_BUSY_RX & SMBUS_STATE_MSK) | HAL_SMBUS_MODE_MASTER))
 
#define SMBUS_STATE_SLAVE_BUSY_TX   ((uint32_t)((HAL_SMBUS_STATE_BUSY_TX & SMBUS_STATE_MSK) | HAL_SMBUS_MODE_SLAVE))
 
#define SMBUS_STATE_SLAVE_BUSY_RX   ((uint32_t)((HAL_SMBUS_STATE_BUSY_RX & SMBUS_STATE_MSK) | HAL_SMBUS_MODE_SLAVE))
 

Functions

static HAL_StatusTypeDef SMBUS_WaitOnFlagUntilTimeout (SMBUS_HandleTypeDef *hsmbus, uint32_t Flag, FlagStatus Status, uint32_t Timeout, uint32_t Tickstart)
 This function handles SMBUS Communication Timeout.
 
static void SMBUS_ITError (SMBUS_HandleTypeDef *hsmbus)
 SMBUS interrupts error process.
 
static void SMBUS_Flush_DR (SMBUS_HandleTypeDef *hsmbus)
 SMBUS data register flush process.
 
static HAL_StatusTypeDef SMBUS_MasterTransmit_TXE (SMBUS_HandleTypeDef *hsmbus)
 Handle TXE flag for Master.
 
static HAL_StatusTypeDef SMBUS_MasterTransmit_BTF (SMBUS_HandleTypeDef *hsmbus)
 Handle BTF flag for Master transmitter.
 
static HAL_StatusTypeDef SMBUS_MasterReceive_RXNE (SMBUS_HandleTypeDef *hsmbus)
 Handle RXNE flag for Master.
 
static HAL_StatusTypeDef SMBUS_MasterReceive_BTF (SMBUS_HandleTypeDef *hsmbus)
 Handle BTF flag for Master receiver.
 
static HAL_StatusTypeDef SMBUS_Master_SB (SMBUS_HandleTypeDef *hsmbus)
 Handle SB flag for Master.
 
static HAL_StatusTypeDef SMBUS_Master_ADD10 (SMBUS_HandleTypeDef *hsmbus)
 Handle ADD10 flag for Master.
 
static HAL_StatusTypeDef SMBUS_Master_ADDR (SMBUS_HandleTypeDef *hsmbus)
 Handle ADDR flag for Master.
 
static HAL_StatusTypeDef SMBUS_SlaveTransmit_TXE (SMBUS_HandleTypeDef *hsmbus)
 Handle TXE flag for Slave.
 
static HAL_StatusTypeDef SMBUS_SlaveTransmit_BTF (SMBUS_HandleTypeDef *hsmbus)
 Handle BTF flag for Slave transmitter.
 
static HAL_StatusTypeDef SMBUS_SlaveReceive_RXNE (SMBUS_HandleTypeDef *hsmbus)
 Handle RXNE flag for Slave.
 
static HAL_StatusTypeDef SMBUS_SlaveReceive_BTF (SMBUS_HandleTypeDef *hsmbus)
 Handle BTF flag for Slave receiver.
 
static HAL_StatusTypeDef SMBUS_Slave_ADDR (SMBUS_HandleTypeDef *hsmbus)
 Handle ADD flag for Slave.
 
static HAL_StatusTypeDef SMBUS_Slave_STOPF (SMBUS_HandleTypeDef *hsmbus)
 Handle STOPF flag for Slave.
 
static HAL_StatusTypeDef SMBUS_Slave_AF (SMBUS_HandleTypeDef *hsmbus)
 
HAL_StatusTypeDef HAL_SMBUS_Init (SMBUS_HandleTypeDef *hsmbus)
 Initializes the SMBUS according to the specified parameters in the SMBUS_InitTypeDef and initialize the associated handle.
 
HAL_StatusTypeDef HAL_SMBUS_DeInit (SMBUS_HandleTypeDef *hsmbus)
 DeInitializes the SMBUS peripheral.
 
void HAL_SMBUS_MspInit (SMBUS_HandleTypeDef *hsmbus)
 Initialize the SMBUS MSP.
 
void HAL_SMBUS_MspDeInit (SMBUS_HandleTypeDef *hsmbus)
 DeInitialize the SMBUS MSP.
 
HAL_StatusTypeDef HAL_SMBUS_Master_Transmit_IT (SMBUS_HandleTypeDef *hsmbus, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
 Transmits in master mode an amount of data in blocking mode.
 
HAL_StatusTypeDef HAL_SMBUS_Master_Receive_IT (SMBUS_HandleTypeDef *hsmbus, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
 Receive in master/host SMBUS mode an amount of data in non blocking mode with Interrupt.
 
HAL_StatusTypeDef HAL_SMBUS_Master_Abort_IT (SMBUS_HandleTypeDef *hsmbus, uint16_t DevAddress)
 Abort a master/host SMBUS process communication with Interrupt.
 
HAL_StatusTypeDef HAL_SMBUS_Slave_Transmit_IT (SMBUS_HandleTypeDef *hsmbus, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
 Transmit in slave/device SMBUS mode an amount of data in non blocking mode with Interrupt.
 
HAL_StatusTypeDef HAL_SMBUS_Slave_Receive_IT (SMBUS_HandleTypeDef *hsmbus, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
 Enable the Address listen mode with Interrupt.
 
HAL_StatusTypeDef HAL_SMBUS_EnableListen_IT (SMBUS_HandleTypeDef *hsmbus)
 Enable the Address listen mode with Interrupt.
 
HAL_StatusTypeDef HAL_SMBUS_DisableListen_IT (SMBUS_HandleTypeDef *hsmbus)
 Disable the Address listen mode with Interrupt.
 
HAL_StatusTypeDef HAL_SMBUS_EnableAlert_IT (SMBUS_HandleTypeDef *hsmbus)
 Enable the SMBUS alert mode with Interrupt.
 
HAL_StatusTypeDef HAL_SMBUS_DisableAlert_IT (SMBUS_HandleTypeDef *hsmbus)
 Disable the SMBUS alert mode with Interrupt.
 
HAL_StatusTypeDef HAL_SMBUS_IsDeviceReady (SMBUS_HandleTypeDef *hsmbus, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout)
 Check if target device is ready for communication.
 
void HAL_SMBUS_EV_IRQHandler (SMBUS_HandleTypeDef *hsmbus)
 This function handles SMBUS event interrupt request.
 
void HAL_SMBUS_ER_IRQHandler (SMBUS_HandleTypeDef *hsmbus)
 This function handles SMBUS error interrupt request.
 
__weak void HAL_SMBUS_MasterTxCpltCallback (SMBUS_HandleTypeDef *hsmbus)
 Master Tx Transfer completed callback.
 
__weak void HAL_SMBUS_MasterRxCpltCallback (SMBUS_HandleTypeDef *hsmbus)
 Master Rx Transfer completed callback.
 
__weak void HAL_SMBUS_SlaveTxCpltCallback (SMBUS_HandleTypeDef *hsmbus)
 Slave Tx Transfer completed callback.
 
__weak void HAL_SMBUS_SlaveRxCpltCallback (SMBUS_HandleTypeDef *hsmbus)
 Slave Rx Transfer completed callback.
 
__weak void HAL_SMBUS_AddrCallback (SMBUS_HandleTypeDef *hsmbus, uint8_t TransferDirection, uint16_t AddrMatchCode)
 Slave Address Match callback.
 
__weak void HAL_SMBUS_ListenCpltCallback (SMBUS_HandleTypeDef *hsmbus)
 Listen Complete callback.
 
__weak void HAL_SMBUS_ErrorCallback (SMBUS_HandleTypeDef *hsmbus)
 SMBUS error callback.
 
__weak void HAL_SMBUS_AbortCpltCallback (SMBUS_HandleTypeDef *hsmbus)
 SMBUS abort callback.
 
HAL_SMBUS_StateTypeDef HAL_SMBUS_GetState (SMBUS_HandleTypeDef *hsmbus)
 Return the SMBUS handle state.
 
HAL_SMBUS_ModeTypeDef HAL_SMBUS_GetMode (SMBUS_HandleTypeDef *hsmbus)
 Return the SMBUS Master, Slave or no mode.
 
uint32_t HAL_SMBUS_GetError (SMBUS_HandleTypeDef *hsmbus)
 Return the SMBUS error code.
 

Detailed Description

SMBUS HAL module driver. This file provides firmware functions to manage the following functionalities of the System Management Bus (SMBus) peripheral, based on SMBUS principals of operation :

Author
MCD Application Team
  • Initialization and de-initialization functions
  • IO operation functions
  • Peripheral State, Mode and Error functions
Attention

Copyright (c) 2016 STMicroelectronics. All rights reserved.

This software is licensed under terms that can be found in the LICENSE file in the root directory of this software component. If no LICENSE file comes with this software, it is provided AS-IS.

==============================================================================
                      ##### How to use this driver #####
==============================================================================
[..]
  The SMBUS HAL driver can be used as follows:

  (#) Declare a SMBUS_HandleTypeDef handle structure, for example:
      SMBUS_HandleTypeDef  hsmbus;

  (#)Initialize the SMBUS low level resources by implementing the HAL_SMBUS_MspInit() API:
      (##) Enable the SMBUSx interface clock
      (##) SMBUS pins configuration
          (+++) Enable the clock for the SMBUS GPIOs
          (+++) Configure SMBUS pins as alternate function open-drain
      (##) NVIC configuration if you need to use interrupt process
          (+++) Configure the SMBUSx interrupt priority
          (+++) Enable the NVIC SMBUS IRQ Channel

  (#) Configure the Communication Speed, Duty cycle, Addressing mode, Own Address1,
      Dual Addressing mode, Own Address2, General call and Nostretch mode in the hsmbus Init structure.

  (#) Initialize the SMBUS registers by calling the HAL_SMBUS_Init(), configures also the low level Hardware
      (GPIO, CLOCK, NVIC...etc) by calling the customized HAL_SMBUS_MspInit(&hsmbus) API.

  (#) To check if target device is ready for communication, use the function HAL_SMBUS_IsDeviceReady()

  (#) For SMBUS IO operations, only one mode of operations is available within this driver :


  *** Interrupt mode IO operation ***
  ===================================

[..]
    (+) Transmit in master/host SMBUS mode an amount of data in non blocking mode using HAL_SMBUS_Master_Transmit_IT()
    (++) At transmission end of transfer HAL_SMBUS_MasterTxCpltCallback() is executed and user can
         add his own code by customization of function pointer HAL_SMBUS_MasterTxCpltCallback()
    (+) Receive in master/host SMBUS mode an amount of data in non blocking mode using HAL_SMBUS_Master_Receive_IT()
    (++) At reception end of transfer HAL_SMBUS_MasterRxCpltCallback() is executed and user can
         add his own code by customization of function pointer HAL_SMBUS_MasterRxCpltCallback()
    (+) Abort a master/Host SMBUS process communication with Interrupt using HAL_SMBUS_Master_Abort_IT()
    (++) End of abort process, HAL_SMBUS_AbortCpltCallback() is executed and user can
         add his own code by customization of function pointer HAL_SMBUS_AbortCpltCallback()
    (+) Enable/disable the Address listen mode in slave/device or host/slave SMBUS mode
         using HAL_SMBUS_EnableListen_IT() HAL_SMBUS_DisableListen_IT()
    (++) When address slave/device SMBUS match, HAL_SMBUS_AddrCallback() is executed and user can
         add his own code to check the Address Match Code and the transmission direction request by master/host (Write/Read).
    (++) At Listen mode end HAL_SMBUS_ListenCpltCallback() is executed and user can
         add his own code by customization of function pointer HAL_SMBUS_ListenCpltCallback()
    (+) Transmit in slave/device SMBUS mode an amount of data in non blocking mode using HAL_SMBUS_Slave_Transmit_IT()
    (++) At transmission end of transfer HAL_SMBUS_SlaveTxCpltCallback() is executed and user can
         add his own code by customization of function pointer HAL_SMBUS_SlaveTxCpltCallback()
    (+) Receive in slave/device SMBUS mode an amount of data in non blocking mode using HAL_SMBUS_Slave_Receive_IT()
    (++) At reception end of transfer HAL_SMBUS_SlaveRxCpltCallback() is executed and user can
         add his own code by customization of function pointer HAL_SMBUS_SlaveRxCpltCallback()
    (+) Enable/Disable the SMBUS alert mode using HAL_SMBUS_EnableAlert_IT() and HAL_SMBUS_DisableAlert_IT()
    (++) When SMBUS Alert is generated HAL_SMBUS_ErrorCallback() is executed and user can
         add his own code by customization of function pointer HAL_SMBUS_ErrorCallback()
         to check the Alert Error Code using function HAL_SMBUS_GetError()
    (+) Get HAL state machine or error values using HAL_SMBUS_GetState() or HAL_SMBUS_GetError()
    (+) In case of transfer Error, HAL_SMBUS_ErrorCallback() function is executed and user can
         add his own code by customization of function pointer HAL_SMBUS_ErrorCallback()
         to check the Error Code using function HAL_SMBUS_GetError()


   *** SMBUS HAL driver macros list ***
   ==================================
   [..]
     Below the list of most used macros in SMBUS HAL driver.

    (+) __HAL_SMBUS_ENABLE    : Enable the SMBUS peripheral
    (+) __HAL_SMBUS_DISABLE   : Disable the SMBUS peripheral
    (+) __HAL_SMBUS_GET_FLAG  : Checks whether the specified SMBUS flag is set or not
    (+) __HAL_SMBUS_CLEAR_FLAG: Clear the specified SMBUS pending flag
    (+) __HAL_SMBUS_ENABLE_IT : Enable the specified SMBUS interrupt
    (+) __HAL_SMBUS_DISABLE_IT: Disable the specified SMBUS interrupt

   [..]
     (@) You can refer to the SMBUS HAL driver header file for more useful macros

   *** Callback registration ***
   =============================================
  [..]
   The compilation flag USE_HAL_SMBUS_REGISTER_CALLBACKS when set to 1
   allows the user to configure dynamically the driver callbacks.
   Use Functions HAL_SMBUS_RegisterCallback() or HAL_SMBUS_RegisterXXXCallback()
   to register an interrupt callback.

   Function HAL_SMBUS_RegisterCallback() allows to register following callbacks:
     (+) MasterTxCpltCallback : callback for Master transmission end of transfer.
     (+) MasterRxCpltCallback : callback for Master reception end of transfer.
     (+) SlaveTxCpltCallback  : callback for Slave transmission end of transfer.
     (+) SlaveRxCpltCallback  : callback for Slave reception end of transfer.
     (+) ListenCpltCallback   : callback for end of listen mode.
     (+) ErrorCallback        : callback for error detection.
     (+) AbortCpltCallback    : callback for abort completion process.
     (+) MspInitCallback      : callback for Msp Init.
     (+) MspDeInitCallback    : callback for Msp DeInit.
   This function takes as parameters the HAL peripheral handle, the Callback ID
   and a pointer to the user callback function.
  [..]
   For specific callback AddrCallback use dedicated register callbacks : HAL_SMBUS_RegisterAddrCallback().
  [..]
   Use function HAL_SMBUS_UnRegisterCallback to reset a callback to the default
   weak function.
   HAL_SMBUS_UnRegisterCallback takes as parameters the HAL peripheral handle,
   and the Callback ID.
   This function allows to reset following callbacks:
     (+) MasterTxCpltCallback : callback for Master transmission end of transfer.
     (+) MasterRxCpltCallback : callback for Master reception end of transfer.
     (+) SlaveTxCpltCallback  : callback for Slave transmission end of transfer.
     (+) SlaveRxCpltCallback  : callback for Slave reception end of transfer.
     (+) ListenCpltCallback   : callback for end of listen mode.
     (+) ErrorCallback        : callback for error detection.
     (+) AbortCpltCallback    : callback for abort completion process.
     (+) MspInitCallback      : callback for Msp Init.
     (+) MspDeInitCallback    : callback for Msp DeInit.
  [..]
   For callback AddrCallback use dedicated register callbacks : HAL_SMBUS_UnRegisterAddrCallback().
  [..]
   By default, after the HAL_SMBUS_Init() and when the state is HAL_SMBUS_STATE_RESET
   all callbacks are set to the corresponding weak functions:
   examples HAL_SMBUS_MasterTxCpltCallback(), HAL_SMBUS_MasterRxCpltCallback().
   Exception done for MspInit and MspDeInit functions that are
   reset to the legacy weak functions in the HAL_SMBUS_Init()/ HAL_SMBUS_DeInit() only when
   these callbacks are null (not registered beforehand).
   If MspInit or MspDeInit are not null, the HAL_SMBUS_Init()/ HAL_SMBUS_DeInit()
   keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
  [..]
   Callbacks can be registered/unregistered in HAL_SMBUS_STATE_READY state only.
   Exception done MspInit/MspDeInit functions that can be registered/unregistered
   in HAL_SMBUS_STATE_READY or HAL_SMBUS_STATE_RESET state,
   thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
   Then, the user first registers the MspInit/MspDeInit user callbacks
   using HAL_SMBUS_RegisterCallback() before calling HAL_SMBUS_DeInit()
   or HAL_SMBUS_Init() function.
  [..]
   When the compilation flag USE_HAL_SMBUS_REGISTER_CALLBACKS is set to 0 or
   not defined, the callback registration feature is not available and all callbacks
   are set to the corresponding weak functions.

Definition in file stm32f4xx_hal_smbus.c.