STM32F103xB HAL User Manual
stm32f1xx_hal_i2c.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f1xx_hal_i2c.c
00004   * @author  MCD Application Team
00005   * @brief   I2C HAL module driver.
00006   *          This file provides firmware functions to manage the following
00007   *          functionalities of the Inter Integrated Circuit (I2C) peripheral:
00008   *           + Initialization and de-initialization functions
00009   *           + IO operation functions
00010   *           + Peripheral State, Mode and Error functions
00011   *
00012   @verbatim
00013   ==============================================================================
00014                         ##### How to use this driver #####
00015   ==============================================================================
00016   [..]
00017     The I2C HAL driver can be used as follows:
00018 
00019     (#) Declare a I2C_HandleTypeDef handle structure, for example:
00020         I2C_HandleTypeDef  hi2c;
00021 
00022     (#)Initialize the I2C low level resources by implementing the @ref HAL_I2C_MspInit() API:
00023         (##) Enable the I2Cx interface clock
00024         (##) I2C pins configuration
00025             (+++) Enable the clock for the I2C GPIOs
00026             (+++) Configure I2C pins as alternate function open-drain
00027         (##) NVIC configuration if you need to use interrupt process
00028             (+++) Configure the I2Cx interrupt priority
00029             (+++) Enable the NVIC I2C IRQ Channel
00030         (##) DMA Configuration if you need to use DMA process
00031             (+++) Declare a DMA_HandleTypeDef handle structure for the transmit or receive channel
00032             (+++) Enable the DMAx interface clock using
00033             (+++) Configure the DMA handle parameters
00034             (+++) Configure the DMA Tx or Rx channel
00035             (+++) Associate the initialized DMA handle to the hi2c DMA Tx or Rx handle
00036             (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on
00037                   the DMA Tx or Rx channel
00038 
00039     (#) Configure the Communication Speed, Duty cycle, Addressing mode, Own Address1,
00040         Dual Addressing mode, Own Address2, General call and Nostretch mode in the hi2c Init structure.
00041 
00042     (#) Initialize the I2C registers by calling the @ref HAL_I2C_Init(), configures also the low level Hardware
00043         (GPIO, CLOCK, NVIC...etc) by calling the customized @ref HAL_I2C_MspInit() API.
00044 
00045     (#) To check if target device is ready for communication, use the function @ref HAL_I2C_IsDeviceReady()
00046 
00047     (#) For I2C IO and IO MEM operations, three operation modes are available within this driver :
00048 
00049     *** Polling mode IO operation ***
00050     =================================
00051     [..]
00052       (+) Transmit in master mode an amount of data in blocking mode using @ref HAL_I2C_Master_Transmit()
00053       (+) Receive in master mode an amount of data in blocking mode using @ref HAL_I2C_Master_Receive()
00054       (+) Transmit in slave mode an amount of data in blocking mode using @ref HAL_I2C_Slave_Transmit()
00055       (+) Receive in slave mode an amount of data in blocking mode using @ref HAL_I2C_Slave_Receive()
00056 
00057     *** Polling mode IO MEM operation ***
00058     =====================================
00059     [..]
00060       (+) Write an amount of data in blocking mode to a specific memory address using @ref HAL_I2C_Mem_Write()
00061       (+) Read an amount of data in blocking mode from a specific memory address using @ref HAL_I2C_Mem_Read()
00062 
00063 
00064     *** Interrupt mode IO operation ***
00065     ===================================
00066     [..]
00067       (+) Transmit in master mode an amount of data in non-blocking mode using @ref HAL_I2C_Master_Transmit_IT()
00068       (+) At transmission end of transfer, @ref HAL_I2C_MasterTxCpltCallback() is executed and user can
00069            add his own code by customization of function pointer @ref HAL_I2C_MasterTxCpltCallback()
00070       (+) Receive in master mode an amount of data in non-blocking mode using @ref HAL_I2C_Master_Receive_IT()
00071       (+) At reception end of transfer, @ref HAL_I2C_MasterRxCpltCallback() is executed and user can
00072            add his own code by customization of function pointer @ref HAL_I2C_MasterRxCpltCallback()
00073       (+) Transmit in slave mode an amount of data in non-blocking mode using @ref HAL_I2C_Slave_Transmit_IT()
00074       (+) At transmission end of transfer, @ref HAL_I2C_SlaveTxCpltCallback() is executed and user can
00075            add his own code by customization of function pointer @ref HAL_I2C_SlaveTxCpltCallback()
00076       (+) Receive in slave mode an amount of data in non-blocking mode using @ref HAL_I2C_Slave_Receive_IT()
00077       (+) At reception end of transfer, @ref HAL_I2C_SlaveRxCpltCallback() is executed and user can
00078            add his own code by customization of function pointer @ref HAL_I2C_SlaveRxCpltCallback()
00079       (+) In case of transfer Error, @ref HAL_I2C_ErrorCallback() function is executed and user can
00080            add his own code by customization of function pointer @ref HAL_I2C_ErrorCallback()
00081       (+) Abort a master I2C process communication with Interrupt using @ref HAL_I2C_Master_Abort_IT()
00082       (+) End of abort process, @ref HAL_I2C_AbortCpltCallback() is executed and user can
00083            add his own code by customization of function pointer @ref HAL_I2C_AbortCpltCallback()
00084 
00085     *** Interrupt mode or DMA mode IO sequential operation ***
00086     ==========================================================
00087     [..]
00088       (@) These interfaces allow to manage a sequential transfer with a repeated start condition
00089           when a direction change during transfer
00090     [..]
00091       (+) A specific option field manage the different steps of a sequential transfer
00092       (+) Option field values are defined through @ref I2C_XferOptions_definition and are listed below:
00093       (++) I2C_FIRST_AND_LAST_FRAME: No sequential usage, functional is same as associated interfaces in no sequential mode
00094       (++) I2C_FIRST_FRAME: Sequential usage, this option allow to manage a sequence with start condition, address
00095                             and data to transfer without a final stop condition
00096       (++) I2C_FIRST_AND_NEXT_FRAME: Sequential usage (Master only), this option allow to manage a sequence with start condition, address
00097                             and data to transfer without a final stop condition, an then permit a call the same master sequential interface
00098                             several times (like @ref HAL_I2C_Master_Seq_Transmit_IT() then @ref HAL_I2C_Master_Seq_Transmit_IT()
00099                             or @ref HAL_I2C_Master_Seq_Transmit_DMA() then @ref HAL_I2C_Master_Seq_Transmit_DMA())
00100       (++) I2C_NEXT_FRAME: Sequential usage, this option allow to manage a sequence with a restart condition, address
00101                             and with new data to transfer if the direction change or manage only the new data to transfer
00102                             if no direction change and without a final stop condition in both cases
00103       (++) I2C_LAST_FRAME: Sequential usage, this option allow to manage a sequance with a restart condition, address
00104                             and with new data to transfer if the direction change or manage only the new data to transfer
00105                             if no direction change and with a final stop condition in both cases
00106       (++) I2C_LAST_FRAME_NO_STOP: Sequential usage (Master only), this option allow to manage a restart condition after several call of the same master sequential
00107                             interface several times (link with option I2C_FIRST_AND_NEXT_FRAME).
00108                             Usage can, transfer several bytes one by one using HAL_I2C_Master_Seq_Transmit_IT(option I2C_FIRST_AND_NEXT_FRAME then I2C_NEXT_FRAME)
00109                               or HAL_I2C_Master_Seq_Receive_IT(option I2C_FIRST_AND_NEXT_FRAME then I2C_NEXT_FRAME)
00110                               or HAL_I2C_Master_Seq_Transmit_DMA(option I2C_FIRST_AND_NEXT_FRAME then I2C_NEXT_FRAME)
00111                               or HAL_I2C_Master_Seq_Receive_DMA(option I2C_FIRST_AND_NEXT_FRAME then I2C_NEXT_FRAME).
00112                             Then usage of this option I2C_LAST_FRAME_NO_STOP at the last Transmit or Receive sequence permit to call the opposite interface Receive or Transmit
00113                               without stopping the communication and so generate a restart condition.
00114       (++) I2C_OTHER_FRAME: Sequential usage (Master only), this option allow to manage a restart condition after each call of the same master sequential
00115                             interface.
00116                             Usage can, transfer several bytes one by one with a restart with slave address between each bytes using HAL_I2C_Master_Seq_Transmit_IT(option I2C_FIRST_FRAME then I2C_OTHER_FRAME)
00117                               or HAL_I2C_Master_Seq_Receive_IT(option I2C_FIRST_FRAME then I2C_OTHER_FRAME)
00118                               or HAL_I2C_Master_Seq_Transmit_DMA(option I2C_FIRST_FRAME then I2C_OTHER_FRAME)
00119                               or HAL_I2C_Master_Seq_Receive_DMA(option I2C_FIRST_FRAME then I2C_OTHER_FRAME).
00120                             Then usage of this option I2C_OTHER_AND_LAST_FRAME at the last frame to help automatic generation of STOP condition.
00121 
00122       (+) Different sequential I2C interfaces are listed below:
00123       (++) Sequential transmit in master I2C mode an amount of data in non-blocking mode using @ref HAL_I2C_Master_Seq_Transmit_IT()
00124             or using @ref HAL_I2C_Master_Seq_Transmit_DMA()
00125       (+++) At transmission end of current frame transfer, @ref HAL_I2C_MasterTxCpltCallback() is executed and user can
00126            add his own code by customization of function pointer @ref HAL_I2C_MasterTxCpltCallback()
00127       (++) Sequential receive in master I2C mode an amount of data in non-blocking mode using @ref HAL_I2C_Master_Seq_Receive_IT()
00128             or using @ref HAL_I2C_Master_Seq_Receive_DMA()
00129       (+++) At reception end of current frame transfer, @ref HAL_I2C_MasterRxCpltCallback() is executed and user can
00130            add his own code by customization of function pointer @ref HAL_I2C_MasterRxCpltCallback()
00131       (++) Abort a master IT or DMA I2C process communication with Interrupt using @ref HAL_I2C_Master_Abort_IT()
00132       (+++) End of abort process, @ref HAL_I2C_AbortCpltCallback() is executed and user can
00133            add his own code by customization of function pointer @ref HAL_I2C_AbortCpltCallback()
00134       (++) Enable/disable the Address listen mode in slave I2C mode using @ref HAL_I2C_EnableListen_IT() @ref HAL_I2C_DisableListen_IT()
00135       (+++) When address slave I2C match, @ref HAL_I2C_AddrCallback() is executed and user can
00136            add his own code to check the Address Match Code and the transmission direction request by master (Write/Read).
00137       (+++) At Listen mode end @ref HAL_I2C_ListenCpltCallback() is executed and user can
00138            add his own code by customization of function pointer @ref HAL_I2C_ListenCpltCallback()
00139       (++) Sequential transmit in slave I2C mode an amount of data in non-blocking mode using @ref HAL_I2C_Slave_Seq_Transmit_IT()
00140             or using @ref HAL_I2C_Slave_Seq_Transmit_DMA()
00141       (+++) At transmission end of current frame transfer, @ref HAL_I2C_SlaveTxCpltCallback() is executed and user can
00142            add his own code by customization of function pointer @ref HAL_I2C_SlaveTxCpltCallback()
00143       (++) Sequential receive in slave I2C mode an amount of data in non-blocking mode using @ref HAL_I2C_Slave_Seq_Receive_IT()
00144             or using @ref HAL_I2C_Slave_Seq_Receive_DMA()
00145       (+++) At reception end of current frame transfer, @ref HAL_I2C_SlaveRxCpltCallback() is executed and user can
00146            add his own code by customization of function pointer @ref HAL_I2C_SlaveRxCpltCallback()
00147       (++) In case of transfer Error, @ref HAL_I2C_ErrorCallback() function is executed and user can
00148            add his own code by customization of function pointer @ref HAL_I2C_ErrorCallback()
00149 
00150     *** Interrupt mode IO MEM operation ***
00151     =======================================
00152     [..]
00153       (+) Write an amount of data in non-blocking mode with Interrupt to a specific memory address using
00154           @ref HAL_I2C_Mem_Write_IT()
00155       (+) At Memory end of write transfer, @ref HAL_I2C_MemTxCpltCallback() is executed and user can
00156            add his own code by customization of function pointer @ref HAL_I2C_MemTxCpltCallback()
00157       (+) Read an amount of data in non-blocking mode with Interrupt from a specific memory address using
00158           @ref HAL_I2C_Mem_Read_IT()
00159       (+) At Memory end of read transfer, @ref HAL_I2C_MemRxCpltCallback() is executed and user can
00160            add his own code by customization of function pointer @ref HAL_I2C_MemRxCpltCallback()
00161       (+) In case of transfer Error, @ref HAL_I2C_ErrorCallback() function is executed and user can
00162            add his own code by customization of function pointer @ref HAL_I2C_ErrorCallback()
00163 
00164     *** DMA mode IO operation ***
00165     ==============================
00166     [..]
00167       (+) Transmit in master mode an amount of data in non-blocking mode (DMA) using
00168           @ref HAL_I2C_Master_Transmit_DMA()
00169       (+) At transmission end of transfer, @ref HAL_I2C_MasterTxCpltCallback() is executed and user can
00170            add his own code by customization of function pointer @ref HAL_I2C_MasterTxCpltCallback()
00171       (+) Receive in master mode an amount of data in non-blocking mode (DMA) using
00172           @ref HAL_I2C_Master_Receive_DMA()
00173       (+) At reception end of transfer, @ref HAL_I2C_MasterRxCpltCallback() is executed and user can
00174            add his own code by customization of function pointer @ref HAL_I2C_MasterRxCpltCallback()
00175       (+) Transmit in slave mode an amount of data in non-blocking mode (DMA) using
00176           @ref HAL_I2C_Slave_Transmit_DMA()
00177       (+) At transmission end of transfer, @ref HAL_I2C_SlaveTxCpltCallback() is executed and user can
00178            add his own code by customization of function pointer @ref HAL_I2C_SlaveTxCpltCallback()
00179       (+) Receive in slave mode an amount of data in non-blocking mode (DMA) using
00180           @ref HAL_I2C_Slave_Receive_DMA()
00181       (+) At reception end of transfer, @ref HAL_I2C_SlaveRxCpltCallback() is executed and user can
00182            add his own code by customization of function pointer @ref HAL_I2C_SlaveRxCpltCallback()
00183       (+) In case of transfer Error, @ref HAL_I2C_ErrorCallback() function is executed and user can
00184            add his own code by customization of function pointer @ref HAL_I2C_ErrorCallback()
00185       (+) Abort a master I2C process communication with Interrupt using @ref HAL_I2C_Master_Abort_IT()
00186       (+) End of abort process, @ref HAL_I2C_AbortCpltCallback() is executed and user can
00187            add his own code by customization of function pointer @ref HAL_I2C_AbortCpltCallback()
00188 
00189     *** DMA mode IO MEM operation ***
00190     =================================
00191     [..]
00192       (+) Write an amount of data in non-blocking mode with DMA to a specific memory address using
00193           @ref HAL_I2C_Mem_Write_DMA()
00194       (+) At Memory end of write transfer, @ref HAL_I2C_MemTxCpltCallback() is executed and user can
00195            add his own code by customization of function pointer @ref HAL_I2C_MemTxCpltCallback()
00196       (+) Read an amount of data in non-blocking mode with DMA from a specific memory address using
00197           @ref HAL_I2C_Mem_Read_DMA()
00198       (+) At Memory end of read transfer, @ref HAL_I2C_MemRxCpltCallback() is executed and user can
00199            add his own code by customization of function pointer @ref HAL_I2C_MemRxCpltCallback()
00200       (+) In case of transfer Error, @ref HAL_I2C_ErrorCallback() function is executed and user can
00201            add his own code by customization of function pointer @ref HAL_I2C_ErrorCallback()
00202 
00203 
00204      *** I2C HAL driver macros list ***
00205      ==================================
00206      [..]
00207        Below the list of most used macros in I2C HAL driver.
00208 
00209       (+) @ref __HAL_I2C_ENABLE:     Enable the I2C peripheral
00210       (+) @ref __HAL_I2C_DISABLE:    Disable the I2C peripheral
00211       (+) @ref __HAL_I2C_GET_FLAG:   Checks whether the specified I2C flag is set or not
00212       (+) @ref __HAL_I2C_CLEAR_FLAG: Clear the specified I2C pending flag
00213       (+) @ref __HAL_I2C_ENABLE_IT:  Enable the specified I2C interrupt
00214       (+) @ref __HAL_I2C_DISABLE_IT: Disable the specified I2C interrupt
00215 
00216      *** Callback registration ***
00217      =============================================
00218     [..]
00219      The compilation flag USE_HAL_I2C_REGISTER_CALLBACKS when set to 1
00220      allows the user to configure dynamically the driver callbacks.
00221      Use Functions @ref HAL_I2C_RegisterCallback() or @ref HAL_I2C_RegisterAddrCallback()
00222      to register an interrupt callback.
00223     [..]
00224      Function @ref HAL_I2C_RegisterCallback() allows to register following callbacks:
00225        (+) MasterTxCpltCallback : callback for Master transmission end of transfer.
00226        (+) MasterRxCpltCallback : callback for Master reception end of transfer.
00227        (+) SlaveTxCpltCallback  : callback for Slave transmission end of transfer.
00228        (+) SlaveRxCpltCallback  : callback for Slave reception end of transfer.
00229        (+) ListenCpltCallback   : callback for end of listen mode.
00230        (+) MemTxCpltCallback    : callback for Memory transmission end of transfer.
00231        (+) MemRxCpltCallback    : callback for Memory reception end of transfer.
00232        (+) ErrorCallback        : callback for error detection.
00233        (+) AbortCpltCallback    : callback for abort completion process.
00234        (+) MspInitCallback      : callback for Msp Init.
00235        (+) MspDeInitCallback    : callback for Msp DeInit.
00236      This function takes as parameters the HAL peripheral handle, the Callback ID
00237      and a pointer to the user callback function.
00238     [..]
00239      For specific callback AddrCallback use dedicated register callbacks : @ref HAL_I2C_RegisterAddrCallback().
00240     [..]
00241      Use function @ref HAL_I2C_UnRegisterCallback to reset a callback to the default
00242      weak function.
00243      @ref HAL_I2C_UnRegisterCallback takes as parameters the HAL peripheral handle,
00244      and the Callback ID.
00245      This function allows to reset following callbacks:
00246        (+) MasterTxCpltCallback : callback for Master transmission end of transfer.
00247        (+) MasterRxCpltCallback : callback for Master reception end of transfer.
00248        (+) SlaveTxCpltCallback  : callback for Slave transmission end of transfer.
00249        (+) SlaveRxCpltCallback  : callback for Slave reception end of transfer.
00250        (+) ListenCpltCallback   : callback for end of listen mode.
00251        (+) MemTxCpltCallback    : callback for Memory transmission end of transfer.
00252        (+) MemRxCpltCallback    : callback for Memory reception end of transfer.
00253        (+) ErrorCallback        : callback for error detection.
00254        (+) AbortCpltCallback    : callback for abort completion process.
00255        (+) MspInitCallback      : callback for Msp Init.
00256        (+) MspDeInitCallback    : callback for Msp DeInit.
00257     [..]
00258      For callback AddrCallback use dedicated register callbacks : @ref HAL_I2C_UnRegisterAddrCallback().
00259     [..]
00260      By default, after the @ref HAL_I2C_Init() and when the state is @ref HAL_I2C_STATE_RESET
00261      all callbacks are set to the corresponding weak functions:
00262      examples @ref HAL_I2C_MasterTxCpltCallback(), @ref HAL_I2C_MasterRxCpltCallback().
00263      Exception done for MspInit and MspDeInit functions that are
00264      reset to the legacy weak functions in the @ref HAL_I2C_Init()/ @ref HAL_I2C_DeInit() only when
00265      these callbacks are null (not registered beforehand).
00266      If MspInit or MspDeInit are not null, the @ref HAL_I2C_Init()/ @ref HAL_I2C_DeInit()
00267      keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
00268     [..]
00269      Callbacks can be registered/unregistered in @ref HAL_I2C_STATE_READY state only.
00270      Exception done MspInit/MspDeInit functions that can be registered/unregistered
00271      in @ref HAL_I2C_STATE_READY or @ref HAL_I2C_STATE_RESET state,
00272      thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
00273      Then, the user first registers the MspInit/MspDeInit user callbacks
00274      using @ref HAL_I2C_RegisterCallback() before calling @ref HAL_I2C_DeInit()
00275      or @ref HAL_I2C_Init() function.
00276     [..]
00277      When the compilation flag USE_HAL_I2C_REGISTER_CALLBACKS is set to 0 or
00278      not defined, the callback registration feature is not available and all callbacks
00279      are set to the corresponding weak functions.
00280 
00281 
00282      *** I2C Workarounds linked to Silicon Limitation ***
00283      ====================================================
00284      [..]
00285        Below the list of all silicon limitations implemented for HAL on STM32F1xx product.
00286        (@) See ErrataSheet to know full silicon limitation list of your product.
00287 
00288        (+) Workarounds Implemented inside I2C HAL Driver
00289           (++) Wrong data read into data register (Polling and Interrupt mode)
00290           (++) Start cannot be generated after a misplaced Stop
00291           (++) Some software events must be managed before the current byte is being transferred:
00292                Workaround: Use DMA in general, except when the Master is receiving a single byte.
00293                For Interupt mode, I2C should have the highest priority in the application.
00294           (++) Mismatch on the "Setup time for a repeated Start condition" timing parameter:
00295                Workaround: Reduce the frequency down to 88 kHz or use the I2C Fast-mode if
00296                supported by the slave.
00297           (++) Data valid time (tVD;DAT) violated without the OVR flag being set:
00298                Workaround: If the slave device allows it, use the clock stretching mechanism
00299                by programming NoStretchMode = I2C_NOSTRETCH_DISABLE in @ref HAL_I2C_Init.
00300 
00301      [..]
00302        (@) You can refer to the I2C HAL driver header file for more useful macros
00303 
00304   @endverbatim
00305   ******************************************************************************
00306   * @attention
00307   *
00308   * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
00309   * All rights reserved.</center></h2>
00310   *
00311   * This software component is licensed by ST under BSD 3-Clause license,
00312   * the "License"; You may not use this file except in compliance with the
00313   * License. You may obtain a copy of the License at:
00314   *                        opensource.org/licenses/BSD-3-Clause
00315   *
00316   ******************************************************************************
00317   */
00318 
00319 /* Includes ------------------------------------------------------------------*/
00320 #include "stm32f1xx_hal.h"
00321 
00322 /** @addtogroup STM32F1xx_HAL_Driver
00323   * @{
00324   */
00325 
00326 /** @defgroup I2C I2C
00327   * @brief I2C HAL module driver
00328   * @{
00329   */
00330 
00331 #ifdef HAL_I2C_MODULE_ENABLED
00332 
00333 /* Private typedef -----------------------------------------------------------*/
00334 /* Private define ------------------------------------------------------------*/
00335 /** @addtogroup I2C_Private_Define
00336   * @{
00337   */
00338 #define I2C_TIMEOUT_FLAG          35U         /*!< Timeout 35 ms             */
00339 #define I2C_TIMEOUT_BUSY_FLAG     25U         /*!< Timeout 25 ms             */
00340 #define I2C_TIMEOUT_STOP_FLAG     5U          /*!< Timeout 5 ms              */
00341 #define I2C_NO_OPTION_FRAME       0xFFFF0000U /*!< XferOptions default value */
00342 
00343 /* Private define for @ref PreviousState usage */
00344 #define I2C_STATE_MSK             ((uint32_t)((uint32_t)((uint32_t)HAL_I2C_STATE_BUSY_TX | (uint32_t)HAL_I2C_STATE_BUSY_RX) & (uint32_t)(~((uint32_t)HAL_I2C_STATE_READY)))) /*!< Mask State define, keep only RX and TX bits            */
00345 #define I2C_STATE_NONE            ((uint32_t)(HAL_I2C_MODE_NONE))                                                        /*!< Default Value                                          */
00346 #define I2C_STATE_MASTER_BUSY_TX  ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_TX & I2C_STATE_MSK) | (uint32_t)HAL_I2C_MODE_MASTER))            /*!< Master Busy TX, combinaison of State LSB and Mode enum */
00347 #define I2C_STATE_MASTER_BUSY_RX  ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_RX & I2C_STATE_MSK) | (uint32_t)HAL_I2C_MODE_MASTER))            /*!< Master Busy RX, combinaison of State LSB and Mode enum */
00348 #define I2C_STATE_SLAVE_BUSY_TX   ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_TX & I2C_STATE_MSK) | (uint32_t)HAL_I2C_MODE_SLAVE))             /*!< Slave Busy TX, combinaison of State LSB and Mode enum  */
00349 #define I2C_STATE_SLAVE_BUSY_RX   ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_RX & I2C_STATE_MSK) | (uint32_t)HAL_I2C_MODE_SLAVE))             /*!< Slave Busy RX, combinaison of State LSB and Mode enum  */
00350 
00351 /**
00352   * @}
00353   */
00354 
00355 /* Private macro -------------------------------------------------------------*/
00356 /* Private variables ---------------------------------------------------------*/
00357 /* Private function prototypes -----------------------------------------------*/
00358 
00359 /** @defgroup I2C_Private_Functions I2C Private Functions
00360   * @{
00361   */
00362 /* Private functions to handle DMA transfer */
00363 static void I2C_DMAXferCplt(DMA_HandleTypeDef *hdma);
00364 static void I2C_DMAError(DMA_HandleTypeDef *hdma);
00365 static void I2C_DMAAbort(DMA_HandleTypeDef *hdma);
00366 
00367 static void I2C_ITError(I2C_HandleTypeDef *hi2c);
00368 
00369 static HAL_StatusTypeDef I2C_MasterRequestWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Timeout, uint32_t Tickstart);
00370 static HAL_StatusTypeDef I2C_MasterRequestRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Timeout, uint32_t Tickstart);
00371 static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart);
00372 static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart);
00373 
00374 /* Private functions to handle flags during polling transfer */
00375 static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout, uint32_t Tickstart);
00376 static HAL_StatusTypeDef I2C_WaitOnMasterAddressFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, uint32_t Timeout, uint32_t Tickstart);
00377 static HAL_StatusTypeDef I2C_WaitOnTXEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart);
00378 static HAL_StatusTypeDef I2C_WaitOnBTFFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart);
00379 static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart);
00380 static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart);
00381 static HAL_StatusTypeDef I2C_WaitOnSTOPRequestThroughIT(I2C_HandleTypeDef *hi2c);
00382 static HAL_StatusTypeDef I2C_IsAcknowledgeFailed(I2C_HandleTypeDef *hi2c);
00383 
00384 /* Private functions for I2C transfer IRQ handler */
00385 static void I2C_MasterTransmit_TXE(I2C_HandleTypeDef *hi2c);
00386 static void I2C_MasterTransmit_BTF(I2C_HandleTypeDef *hi2c);
00387 static void I2C_MasterReceive_RXNE(I2C_HandleTypeDef *hi2c);
00388 static void I2C_MasterReceive_BTF(I2C_HandleTypeDef *hi2c);
00389 static void I2C_Master_SB(I2C_HandleTypeDef *hi2c);
00390 static void I2C_Master_ADD10(I2C_HandleTypeDef *hi2c);
00391 static void I2C_Master_ADDR(I2C_HandleTypeDef *hi2c);
00392 
00393 static void I2C_SlaveTransmit_TXE(I2C_HandleTypeDef *hi2c);
00394 static void I2C_SlaveTransmit_BTF(I2C_HandleTypeDef *hi2c);
00395 static void I2C_SlaveReceive_RXNE(I2C_HandleTypeDef *hi2c);
00396 static void I2C_SlaveReceive_BTF(I2C_HandleTypeDef *hi2c);
00397 static void I2C_Slave_ADDR(I2C_HandleTypeDef *hi2c, uint32_t IT2Flags);
00398 static void I2C_Slave_STOPF(I2C_HandleTypeDef *hi2c);
00399 static void I2C_Slave_AF(I2C_HandleTypeDef *hi2c);
00400 
00401 static void I2C_MemoryTransmit_TXE_BTF(I2C_HandleTypeDef *hi2c);
00402 
00403 /* Private function to Convert Specific options */
00404 static void I2C_ConvertOtherXferOptions(I2C_HandleTypeDef *hi2c);
00405 /**
00406   * @}
00407   */
00408 
00409 /* Exported functions --------------------------------------------------------*/
00410 
00411 /** @defgroup I2C_Exported_Functions I2C Exported Functions
00412   * @{
00413   */
00414 
00415 /** @defgroup I2C_Exported_Functions_Group1 Initialization and de-initialization functions
00416  *  @brief    Initialization and Configuration functions
00417  *
00418 @verbatim
00419  ===============================================================================
00420               ##### Initialization and de-initialization functions #####
00421  ===============================================================================
00422     [..]  This subsection provides a set of functions allowing to initialize and
00423           deinitialize the I2Cx peripheral:
00424 
00425       (+) User must Implement HAL_I2C_MspInit() function in which he configures
00426           all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC).
00427 
00428       (+) Call the function HAL_I2C_Init() to configure the selected device with
00429           the selected configuration:
00430         (++) Communication Speed
00431         (++) Duty cycle
00432         (++) Addressing mode
00433         (++) Own Address 1
00434         (++) Dual Addressing mode
00435         (++) Own Address 2
00436         (++) General call mode
00437         (++) Nostretch mode
00438 
00439       (+) Call the function HAL_I2C_DeInit() to restore the default configuration
00440           of the selected I2Cx peripheral.
00441 
00442 @endverbatim
00443   * @{
00444   */
00445 
00446 /**
00447   * @brief  Initializes the I2C according to the specified parameters
00448   *         in the I2C_InitTypeDef and initialize the associated handle.
00449   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
00450   *                the configuration information for the specified I2C.
00451   * @retval HAL status
00452   */
00453 HAL_StatusTypeDef HAL_I2C_Init(I2C_HandleTypeDef *hi2c)
00454 {
00455   uint32_t freqrange;
00456   uint32_t pclk1;
00457 
00458   /* Check the I2C handle allocation */
00459   if (hi2c == NULL)
00460   {
00461     return HAL_ERROR;
00462   }
00463 
00464   /* Check the parameters */
00465   assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
00466   assert_param(IS_I2C_CLOCK_SPEED(hi2c->Init.ClockSpeed));
00467   assert_param(IS_I2C_DUTY_CYCLE(hi2c->Init.DutyCycle));
00468   assert_param(IS_I2C_OWN_ADDRESS1(hi2c->Init.OwnAddress1));
00469   assert_param(IS_I2C_ADDRESSING_MODE(hi2c->Init.AddressingMode));
00470   assert_param(IS_I2C_DUAL_ADDRESS(hi2c->Init.DualAddressMode));
00471   assert_param(IS_I2C_OWN_ADDRESS2(hi2c->Init.OwnAddress2));
00472   assert_param(IS_I2C_GENERAL_CALL(hi2c->Init.GeneralCallMode));
00473   assert_param(IS_I2C_NO_STRETCH(hi2c->Init.NoStretchMode));
00474 
00475   if (hi2c->State == HAL_I2C_STATE_RESET)
00476   {
00477     /* Allocate lock resource and initialize it */
00478     hi2c->Lock = HAL_UNLOCKED;
00479 
00480 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
00481     /* Init the I2C Callback settings */
00482     hi2c->MasterTxCpltCallback = HAL_I2C_MasterTxCpltCallback; /* Legacy weak MasterTxCpltCallback */
00483     hi2c->MasterRxCpltCallback = HAL_I2C_MasterRxCpltCallback; /* Legacy weak MasterRxCpltCallback */
00484     hi2c->SlaveTxCpltCallback  = HAL_I2C_SlaveTxCpltCallback;  /* Legacy weak SlaveTxCpltCallback  */
00485     hi2c->SlaveRxCpltCallback  = HAL_I2C_SlaveRxCpltCallback;  /* Legacy weak SlaveRxCpltCallback  */
00486     hi2c->ListenCpltCallback   = HAL_I2C_ListenCpltCallback;   /* Legacy weak ListenCpltCallback   */
00487     hi2c->MemTxCpltCallback    = HAL_I2C_MemTxCpltCallback;    /* Legacy weak MemTxCpltCallback    */
00488     hi2c->MemRxCpltCallback    = HAL_I2C_MemRxCpltCallback;    /* Legacy weak MemRxCpltCallback    */
00489     hi2c->ErrorCallback        = HAL_I2C_ErrorCallback;        /* Legacy weak ErrorCallback        */
00490     hi2c->AbortCpltCallback    = HAL_I2C_AbortCpltCallback;    /* Legacy weak AbortCpltCallback    */
00491     hi2c->AddrCallback         = HAL_I2C_AddrCallback;         /* Legacy weak AddrCallback         */
00492 
00493     if (hi2c->MspInitCallback == NULL)
00494     {
00495       hi2c->MspInitCallback = HAL_I2C_MspInit; /* Legacy weak MspInit  */
00496     }
00497 
00498     /* Init the low level hardware : GPIO, CLOCK, NVIC */
00499     hi2c->MspInitCallback(hi2c);
00500 #else
00501     /* Init the low level hardware : GPIO, CLOCK, NVIC */
00502     HAL_I2C_MspInit(hi2c);
00503 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
00504   }
00505 
00506   hi2c->State = HAL_I2C_STATE_BUSY;
00507 
00508   /* Disable the selected I2C peripheral */
00509   __HAL_I2C_DISABLE(hi2c);
00510 
00511   /*Reset I2C*/
00512   hi2c->Instance->CR1 |= I2C_CR1_SWRST;
00513   hi2c->Instance->CR1 &= ~I2C_CR1_SWRST;
00514 
00515   /* Get PCLK1 frequency */
00516   pclk1 = HAL_RCC_GetPCLK1Freq();
00517 
00518   /* Check the minimum allowed PCLK1 frequency */
00519   if (I2C_MIN_PCLK_FREQ(pclk1, hi2c->Init.ClockSpeed) == 1U)
00520   {
00521     return HAL_ERROR;
00522   }
00523 
00524   /* Calculate frequency range */
00525   freqrange = I2C_FREQRANGE(pclk1);
00526 
00527   /*---------------------------- I2Cx CR2 Configuration ----------------------*/
00528   /* Configure I2Cx: Frequency range */
00529   MODIFY_REG(hi2c->Instance->CR2, I2C_CR2_FREQ, freqrange);
00530 
00531   /*---------------------------- I2Cx TRISE Configuration --------------------*/
00532   /* Configure I2Cx: Rise Time */
00533   MODIFY_REG(hi2c->Instance->TRISE, I2C_TRISE_TRISE, I2C_RISE_TIME(freqrange, hi2c->Init.ClockSpeed));
00534 
00535   /*---------------------------- I2Cx CCR Configuration ----------------------*/
00536   /* Configure I2Cx: Speed */
00537   MODIFY_REG(hi2c->Instance->CCR, (I2C_CCR_FS | I2C_CCR_DUTY | I2C_CCR_CCR), I2C_SPEED(pclk1, hi2c->Init.ClockSpeed, hi2c->Init.DutyCycle));
00538 
00539   /*---------------------------- I2Cx CR1 Configuration ----------------------*/
00540   /* Configure I2Cx: Generalcall and NoStretch mode */
00541   MODIFY_REG(hi2c->Instance->CR1, (I2C_CR1_ENGC | I2C_CR1_NOSTRETCH), (hi2c->Init.GeneralCallMode | hi2c->Init.NoStretchMode));
00542 
00543   /*---------------------------- I2Cx OAR1 Configuration ---------------------*/
00544   /* Configure I2Cx: Own Address1 and addressing mode */
00545   MODIFY_REG(hi2c->Instance->OAR1, (I2C_OAR1_ADDMODE | I2C_OAR1_ADD8_9 | I2C_OAR1_ADD1_7 | I2C_OAR1_ADD0), (hi2c->Init.AddressingMode | hi2c->Init.OwnAddress1));
00546 
00547   /*---------------------------- I2Cx OAR2 Configuration ---------------------*/
00548   /* Configure I2Cx: Dual mode and Own Address2 */
00549   MODIFY_REG(hi2c->Instance->OAR2, (I2C_OAR2_ENDUAL | I2C_OAR2_ADD2), (hi2c->Init.DualAddressMode | hi2c->Init.OwnAddress2));
00550 
00551   /* Enable the selected I2C peripheral */
00552   __HAL_I2C_ENABLE(hi2c);
00553 
00554   hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
00555   hi2c->State = HAL_I2C_STATE_READY;
00556   hi2c->PreviousState = I2C_STATE_NONE;
00557   hi2c->Mode = HAL_I2C_MODE_NONE;
00558 
00559   return HAL_OK;
00560 }
00561 
00562 /**
00563   * @brief  DeInitialize the I2C peripheral.
00564   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
00565   *         the configuration information for the specified I2C.
00566   * @retval HAL status
00567   */
00568 HAL_StatusTypeDef HAL_I2C_DeInit(I2C_HandleTypeDef *hi2c)
00569 {
00570   /* Check the I2C handle allocation */
00571   if (hi2c == NULL)
00572   {
00573     return HAL_ERROR;
00574   }
00575 
00576   /* Check the parameters */
00577   assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
00578 
00579   hi2c->State = HAL_I2C_STATE_BUSY;
00580 
00581   /* Disable the I2C Peripheral Clock */
00582   __HAL_I2C_DISABLE(hi2c);
00583 
00584 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
00585   if (hi2c->MspDeInitCallback == NULL)
00586   {
00587     hi2c->MspDeInitCallback = HAL_I2C_MspDeInit; /* Legacy weak MspDeInit  */
00588   }
00589 
00590   /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
00591   hi2c->MspDeInitCallback(hi2c);
00592 #else
00593   /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
00594   HAL_I2C_MspDeInit(hi2c);
00595 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
00596 
00597   hi2c->ErrorCode     = HAL_I2C_ERROR_NONE;
00598   hi2c->State         = HAL_I2C_STATE_RESET;
00599   hi2c->PreviousState = I2C_STATE_NONE;
00600   hi2c->Mode          = HAL_I2C_MODE_NONE;
00601 
00602   /* Release Lock */
00603   __HAL_UNLOCK(hi2c);
00604 
00605   return HAL_OK;
00606 }
00607 
00608 /**
00609   * @brief  Initialize the I2C MSP.
00610   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
00611   *         the configuration information for the specified I2C.
00612   * @retval None
00613   */
00614 __weak void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)
00615 {
00616   /* Prevent unused argument(s) compilation warning */
00617   UNUSED(hi2c);
00618 
00619   /* NOTE : This function should not be modified, when the callback is needed,
00620             the HAL_I2C_MspInit could be implemented in the user file
00621    */
00622 }
00623 
00624 /**
00625   * @brief  DeInitialize the I2C MSP.
00626   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
00627   *         the configuration information for the specified I2C.
00628   * @retval None
00629   */
00630 __weak void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c)
00631 {
00632   /* Prevent unused argument(s) compilation warning */
00633   UNUSED(hi2c);
00634 
00635   /* NOTE : This function should not be modified, when the callback is needed,
00636             the HAL_I2C_MspDeInit could be implemented in the user file
00637    */
00638 }
00639 
00640 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
00641 /**
00642   * @brief  Register a User I2C Callback
00643   *         To be used instead of the weak predefined callback
00644   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
00645   *                the configuration information for the specified I2C.
00646   * @param  CallbackID ID of the callback to be registered
00647   *         This parameter can be one of the following values:
00648   *          @arg @ref HAL_I2C_MASTER_TX_COMPLETE_CB_ID Master Tx Transfer completed callback ID
00649   *          @arg @ref HAL_I2C_MASTER_RX_COMPLETE_CB_ID Master Rx Transfer completed callback ID
00650   *          @arg @ref HAL_I2C_SLAVE_TX_COMPLETE_CB_ID Slave Tx Transfer completed callback ID
00651   *          @arg @ref HAL_I2C_SLAVE_RX_COMPLETE_CB_ID Slave Rx Transfer completed callback ID
00652   *          @arg @ref HAL_I2C_LISTEN_COMPLETE_CB_ID Listen Complete callback ID
00653   *          @arg @ref HAL_I2C_MEM_TX_COMPLETE_CB_ID Memory Tx Transfer callback ID
00654   *          @arg @ref HAL_I2C_MEM_RX_COMPLETE_CB_ID Memory Rx Transfer completed callback ID
00655   *          @arg @ref HAL_I2C_ERROR_CB_ID Error callback ID
00656   *          @arg @ref HAL_I2C_ABORT_CB_ID Abort callback ID
00657   *          @arg @ref HAL_I2C_MSPINIT_CB_ID MspInit callback ID
00658   *          @arg @ref HAL_I2C_MSPDEINIT_CB_ID MspDeInit callback ID
00659   * @param  pCallback pointer to the Callback function
00660   * @retval HAL status
00661   */
00662 HAL_StatusTypeDef HAL_I2C_RegisterCallback(I2C_HandleTypeDef *hi2c, HAL_I2C_CallbackIDTypeDef CallbackID, pI2C_CallbackTypeDef pCallback)
00663 {
00664   HAL_StatusTypeDef status = HAL_OK;
00665 
00666   if (pCallback == NULL)
00667   {
00668     /* Update the error code */
00669     hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
00670 
00671     return HAL_ERROR;
00672   }
00673   /* Process locked */
00674   __HAL_LOCK(hi2c);
00675 
00676   if (HAL_I2C_STATE_READY == hi2c->State)
00677   {
00678     switch (CallbackID)
00679     {
00680       case HAL_I2C_MASTER_TX_COMPLETE_CB_ID :
00681         hi2c->MasterTxCpltCallback = pCallback;
00682         break;
00683 
00684       case HAL_I2C_MASTER_RX_COMPLETE_CB_ID :
00685         hi2c->MasterRxCpltCallback = pCallback;
00686         break;
00687 
00688       case HAL_I2C_SLAVE_TX_COMPLETE_CB_ID :
00689         hi2c->SlaveTxCpltCallback = pCallback;
00690         break;
00691 
00692       case HAL_I2C_SLAVE_RX_COMPLETE_CB_ID :
00693         hi2c->SlaveRxCpltCallback = pCallback;
00694         break;
00695 
00696       case HAL_I2C_LISTEN_COMPLETE_CB_ID :
00697         hi2c->ListenCpltCallback = pCallback;
00698         break;
00699 
00700       case HAL_I2C_MEM_TX_COMPLETE_CB_ID :
00701         hi2c->MemTxCpltCallback = pCallback;
00702         break;
00703 
00704       case HAL_I2C_MEM_RX_COMPLETE_CB_ID :
00705         hi2c->MemRxCpltCallback = pCallback;
00706         break;
00707 
00708       case HAL_I2C_ERROR_CB_ID :
00709         hi2c->ErrorCallback = pCallback;
00710         break;
00711 
00712       case HAL_I2C_ABORT_CB_ID :
00713         hi2c->AbortCpltCallback = pCallback;
00714         break;
00715 
00716       case HAL_I2C_MSPINIT_CB_ID :
00717         hi2c->MspInitCallback = pCallback;
00718         break;
00719 
00720       case HAL_I2C_MSPDEINIT_CB_ID :
00721         hi2c->MspDeInitCallback = pCallback;
00722         break;
00723 
00724       default :
00725         /* Update the error code */
00726         hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
00727 
00728         /* Return error status */
00729         status =  HAL_ERROR;
00730         break;
00731     }
00732   }
00733   else if (HAL_I2C_STATE_RESET == hi2c->State)
00734   {
00735     switch (CallbackID)
00736     {
00737       case HAL_I2C_MSPINIT_CB_ID :
00738         hi2c->MspInitCallback = pCallback;
00739         break;
00740 
00741       case HAL_I2C_MSPDEINIT_CB_ID :
00742         hi2c->MspDeInitCallback = pCallback;
00743         break;
00744 
00745       default :
00746         /* Update the error code */
00747         hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
00748 
00749         /* Return error status */
00750         status =  HAL_ERROR;
00751         break;
00752     }
00753   }
00754   else
00755   {
00756     /* Update the error code */
00757     hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
00758 
00759     /* Return error status */
00760     status =  HAL_ERROR;
00761   }
00762 
00763   /* Release Lock */
00764   __HAL_UNLOCK(hi2c);
00765   return status;
00766 }
00767 
00768 /**
00769   * @brief  Unregister an I2C Callback
00770   *         I2C callback is redirected to the weak predefined callback
00771   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
00772   *                the configuration information for the specified I2C.
00773   * @param  CallbackID ID of the callback to be unregistered
00774   *         This parameter can be one of the following values:
00775   *         This parameter can be one of the following values:
00776   *          @arg @ref HAL_I2C_MASTER_TX_COMPLETE_CB_ID Master Tx Transfer completed callback ID
00777   *          @arg @ref HAL_I2C_MASTER_RX_COMPLETE_CB_ID Master Rx Transfer completed callback ID
00778   *          @arg @ref HAL_I2C_SLAVE_TX_COMPLETE_CB_ID Slave Tx Transfer completed callback ID
00779   *          @arg @ref HAL_I2C_SLAVE_RX_COMPLETE_CB_ID Slave Rx Transfer completed callback ID
00780   *          @arg @ref HAL_I2C_LISTEN_COMPLETE_CB_ID Listen Complete callback ID
00781   *          @arg @ref HAL_I2C_MEM_TX_COMPLETE_CB_ID Memory Tx Transfer callback ID
00782   *          @arg @ref HAL_I2C_MEM_RX_COMPLETE_CB_ID Memory Rx Transfer completed callback ID
00783   *          @arg @ref HAL_I2C_ERROR_CB_ID Error callback ID
00784   *          @arg @ref HAL_I2C_ABORT_CB_ID Abort callback ID
00785   *          @arg @ref HAL_I2C_MSPINIT_CB_ID MspInit callback ID
00786   *          @arg @ref HAL_I2C_MSPDEINIT_CB_ID MspDeInit callback ID
00787   * @retval HAL status
00788   */
00789 HAL_StatusTypeDef HAL_I2C_UnRegisterCallback(I2C_HandleTypeDef *hi2c, HAL_I2C_CallbackIDTypeDef CallbackID)
00790 {
00791   HAL_StatusTypeDef status = HAL_OK;
00792 
00793   /* Process locked */
00794   __HAL_LOCK(hi2c);
00795 
00796   if (HAL_I2C_STATE_READY == hi2c->State)
00797   {
00798     switch (CallbackID)
00799     {
00800       case HAL_I2C_MASTER_TX_COMPLETE_CB_ID :
00801         hi2c->MasterTxCpltCallback = HAL_I2C_MasterTxCpltCallback; /* Legacy weak MasterTxCpltCallback */
00802         break;
00803 
00804       case HAL_I2C_MASTER_RX_COMPLETE_CB_ID :
00805         hi2c->MasterRxCpltCallback = HAL_I2C_MasterRxCpltCallback; /* Legacy weak MasterRxCpltCallback */
00806         break;
00807 
00808       case HAL_I2C_SLAVE_TX_COMPLETE_CB_ID :
00809         hi2c->SlaveTxCpltCallback = HAL_I2C_SlaveTxCpltCallback;   /* Legacy weak SlaveTxCpltCallback  */
00810         break;
00811 
00812       case HAL_I2C_SLAVE_RX_COMPLETE_CB_ID :
00813         hi2c->SlaveRxCpltCallback = HAL_I2C_SlaveRxCpltCallback;   /* Legacy weak SlaveRxCpltCallback  */
00814         break;
00815 
00816       case HAL_I2C_LISTEN_COMPLETE_CB_ID :
00817         hi2c->ListenCpltCallback = HAL_I2C_ListenCpltCallback;     /* Legacy weak ListenCpltCallback   */
00818         break;
00819 
00820       case HAL_I2C_MEM_TX_COMPLETE_CB_ID :
00821         hi2c->MemTxCpltCallback = HAL_I2C_MemTxCpltCallback;       /* Legacy weak MemTxCpltCallback    */
00822         break;
00823 
00824       case HAL_I2C_MEM_RX_COMPLETE_CB_ID :
00825         hi2c->MemRxCpltCallback = HAL_I2C_MemRxCpltCallback;       /* Legacy weak MemRxCpltCallback    */
00826         break;
00827 
00828       case HAL_I2C_ERROR_CB_ID :
00829         hi2c->ErrorCallback = HAL_I2C_ErrorCallback;               /* Legacy weak ErrorCallback        */
00830         break;
00831 
00832       case HAL_I2C_ABORT_CB_ID :
00833         hi2c->AbortCpltCallback = HAL_I2C_AbortCpltCallback;       /* Legacy weak AbortCpltCallback    */
00834         break;
00835 
00836       case HAL_I2C_MSPINIT_CB_ID :
00837         hi2c->MspInitCallback = HAL_I2C_MspInit;                   /* Legacy weak MspInit              */
00838         break;
00839 
00840       case HAL_I2C_MSPDEINIT_CB_ID :
00841         hi2c->MspDeInitCallback = HAL_I2C_MspDeInit;               /* Legacy weak MspDeInit            */
00842         break;
00843 
00844       default :
00845         /* Update the error code */
00846         hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
00847 
00848         /* Return error status */
00849         status =  HAL_ERROR;
00850         break;
00851     }
00852   }
00853   else if (HAL_I2C_STATE_RESET == hi2c->State)
00854   {
00855     switch (CallbackID)
00856     {
00857       case HAL_I2C_MSPINIT_CB_ID :
00858         hi2c->MspInitCallback = HAL_I2C_MspInit;                   /* Legacy weak MspInit              */
00859         break;
00860 
00861       case HAL_I2C_MSPDEINIT_CB_ID :
00862         hi2c->MspDeInitCallback = HAL_I2C_MspDeInit;               /* Legacy weak MspDeInit            */
00863         break;
00864 
00865       default :
00866         /* Update the error code */
00867         hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
00868 
00869         /* Return error status */
00870         status =  HAL_ERROR;
00871         break;
00872     }
00873   }
00874   else
00875   {
00876     /* Update the error code */
00877     hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
00878 
00879     /* Return error status */
00880     status =  HAL_ERROR;
00881   }
00882 
00883   /* Release Lock */
00884   __HAL_UNLOCK(hi2c);
00885   return status;
00886 }
00887 
00888 /**
00889   * @brief  Register the Slave Address Match I2C Callback
00890   *         To be used instead of the weak HAL_I2C_AddrCallback() predefined callback
00891   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
00892   *                the configuration information for the specified I2C.
00893   * @param  pCallback pointer to the Address Match Callback function
00894   * @retval HAL status
00895   */
00896 HAL_StatusTypeDef HAL_I2C_RegisterAddrCallback(I2C_HandleTypeDef *hi2c, pI2C_AddrCallbackTypeDef pCallback)
00897 {
00898   HAL_StatusTypeDef status = HAL_OK;
00899 
00900   if (pCallback == NULL)
00901   {
00902     /* Update the error code */
00903     hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
00904 
00905     return HAL_ERROR;
00906   }
00907   /* Process locked */
00908   __HAL_LOCK(hi2c);
00909 
00910   if (HAL_I2C_STATE_READY == hi2c->State)
00911   {
00912     hi2c->AddrCallback = pCallback;
00913   }
00914   else
00915   {
00916     /* Update the error code */
00917     hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
00918 
00919     /* Return error status */
00920     status =  HAL_ERROR;
00921   }
00922 
00923   /* Release Lock */
00924   __HAL_UNLOCK(hi2c);
00925   return status;
00926 }
00927 
00928 /**
00929   * @brief  UnRegister the Slave Address Match I2C Callback
00930   *         Info Ready I2C Callback is redirected to the weak HAL_I2C_AddrCallback() predefined callback
00931   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
00932   *                the configuration information for the specified I2C.
00933   * @retval HAL status
00934   */
00935 HAL_StatusTypeDef HAL_I2C_UnRegisterAddrCallback(I2C_HandleTypeDef *hi2c)
00936 {
00937   HAL_StatusTypeDef status = HAL_OK;
00938 
00939   /* Process locked */
00940   __HAL_LOCK(hi2c);
00941 
00942   if (HAL_I2C_STATE_READY == hi2c->State)
00943   {
00944     hi2c->AddrCallback = HAL_I2C_AddrCallback; /* Legacy weak AddrCallback  */
00945   }
00946   else
00947   {
00948     /* Update the error code */
00949     hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
00950 
00951     /* Return error status */
00952     status =  HAL_ERROR;
00953   }
00954 
00955   /* Release Lock */
00956   __HAL_UNLOCK(hi2c);
00957   return status;
00958 }
00959 
00960 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
00961 
00962 /**
00963   * @}
00964   */
00965 
00966 /** @defgroup I2C_Exported_Functions_Group2 Input and Output operation functions
00967  *  @brief   Data transfers functions
00968  *
00969 @verbatim
00970  ===============================================================================
00971                       ##### IO operation functions #####
00972  ===============================================================================
00973     [..]
00974     This subsection provides a set of functions allowing to manage the I2C data
00975     transfers.
00976 
00977     (#) There are two modes of transfer:
00978        (++) Blocking mode : The communication is performed in the polling mode.
00979             The status of all data processing is returned by the same function
00980             after finishing transfer.
00981        (++) No-Blocking mode : The communication is performed using Interrupts
00982             or DMA. These functions return the status of the transfer startup.
00983             The end of the data processing will be indicated through the
00984             dedicated I2C IRQ when using Interrupt mode or the DMA IRQ when
00985             using DMA mode.
00986 
00987     (#) Blocking mode functions are :
00988         (++) HAL_I2C_Master_Transmit()
00989         (++) HAL_I2C_Master_Receive()
00990         (++) HAL_I2C_Slave_Transmit()
00991         (++) HAL_I2C_Slave_Receive()
00992         (++) HAL_I2C_Mem_Write()
00993         (++) HAL_I2C_Mem_Read()
00994         (++) HAL_I2C_IsDeviceReady()
00995 
00996     (#) No-Blocking mode functions with Interrupt are :
00997         (++) HAL_I2C_Master_Transmit_IT()
00998         (++) HAL_I2C_Master_Receive_IT()
00999         (++) HAL_I2C_Slave_Transmit_IT()
01000         (++) HAL_I2C_Slave_Receive_IT()
01001         (++) HAL_I2C_Mem_Write_IT()
01002         (++) HAL_I2C_Mem_Read_IT()
01003         (++) HAL_I2C_Master_Seq_Transmit_IT()
01004         (++) HAL_I2C_Master_Seq_Receive_IT()
01005         (++) HAL_I2C_Slave_Seq_Transmit_IT()
01006         (++) HAL_I2C_Slave_Seq_Receive_IT()
01007         (++) HAL_I2C_EnableListen_IT()
01008         (++) HAL_I2C_DisableListen_IT()
01009         (++) HAL_I2C_Master_Abort_IT()
01010 
01011     (#) No-Blocking mode functions with DMA are :
01012         (++) HAL_I2C_Master_Transmit_DMA()
01013         (++) HAL_I2C_Master_Receive_DMA()
01014         (++) HAL_I2C_Slave_Transmit_DMA()
01015         (++) HAL_I2C_Slave_Receive_DMA()
01016         (++) HAL_I2C_Mem_Write_DMA()
01017         (++) HAL_I2C_Mem_Read_DMA()
01018         (++) HAL_I2C_Master_Seq_Transmit_DMA()
01019         (++) HAL_I2C_Master_Seq_Receive_DMA()
01020         (++) HAL_I2C_Slave_Seq_Transmit_DMA()
01021         (++) HAL_I2C_Slave_Seq_Receive_DMA()
01022 
01023     (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
01024         (++) HAL_I2C_MasterTxCpltCallback()
01025         (++) HAL_I2C_MasterRxCpltCallback()
01026         (++) HAL_I2C_SlaveTxCpltCallback()
01027         (++) HAL_I2C_SlaveRxCpltCallback()
01028         (++) HAL_I2C_MemTxCpltCallback()
01029         (++) HAL_I2C_MemRxCpltCallback()
01030         (++) HAL_I2C_AddrCallback()
01031         (++) HAL_I2C_ListenCpltCallback()
01032         (++) HAL_I2C_ErrorCallback()
01033         (++) HAL_I2C_AbortCpltCallback()
01034 
01035 @endverbatim
01036   * @{
01037   */
01038 
01039 /**
01040   * @brief  Transmits in master mode an amount of data in blocking mode.
01041   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
01042   *                the configuration information for the specified I2C.
01043   * @param  DevAddress Target device address: The device 7 bits address value
01044   *         in datasheet must be shifted to the left before calling the interface
01045   * @param  pData Pointer to data buffer
01046   * @param  Size Amount of data to be sent
01047   * @param  Timeout Timeout duration
01048   * @retval HAL status
01049   */
01050 HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
01051 {
01052   /* Init tickstart for timeout management*/
01053   uint32_t tickstart = HAL_GetTick();
01054 
01055   if (hi2c->State == HAL_I2C_STATE_READY)
01056   {
01057     /* Wait until BUSY flag is reset */
01058     if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
01059     {
01060       return HAL_BUSY;
01061     }
01062 
01063     /* Process Locked */
01064     __HAL_LOCK(hi2c);
01065 
01066     /* Check if the I2C is already enabled */
01067     if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
01068     {
01069       /* Enable I2C peripheral */
01070       __HAL_I2C_ENABLE(hi2c);
01071     }
01072 
01073     /* Disable Pos */
01074     CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
01075 
01076     hi2c->State       = HAL_I2C_STATE_BUSY_TX;
01077     hi2c->Mode        = HAL_I2C_MODE_MASTER;
01078     hi2c->ErrorCode   = HAL_I2C_ERROR_NONE;
01079 
01080     /* Prepare transfer parameters */
01081     hi2c->pBuffPtr    = pData;
01082     hi2c->XferCount   = Size;
01083     hi2c->XferSize    = hi2c->XferCount;
01084     hi2c->XferOptions = I2C_NO_OPTION_FRAME;
01085 
01086     /* Send Slave Address */
01087     if (I2C_MasterRequestWrite(hi2c, DevAddress, Timeout, tickstart) != HAL_OK)
01088     {
01089       return HAL_ERROR;
01090     }
01091 
01092     /* Clear ADDR flag */
01093     __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
01094 
01095     while (hi2c->XferSize > 0U)
01096     {
01097       /* Wait until TXE flag is set */
01098       if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
01099       {
01100         if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
01101         {
01102           /* Generate Stop */
01103           SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
01104         }
01105         return HAL_ERROR;
01106       }
01107 
01108       /* Write data to DR */
01109       hi2c->Instance->DR = *hi2c->pBuffPtr;
01110 
01111       /* Increment Buffer pointer */
01112       hi2c->pBuffPtr++;
01113 
01114       /* Update counter */
01115       hi2c->XferCount--;
01116       hi2c->XferSize--;
01117 
01118       if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (hi2c->XferSize != 0U))
01119       {
01120         /* Write data to DR */
01121         hi2c->Instance->DR = *hi2c->pBuffPtr;
01122 
01123         /* Increment Buffer pointer */
01124         hi2c->pBuffPtr++;
01125 
01126         /* Update counter */
01127         hi2c->XferCount--;
01128         hi2c->XferSize--;
01129       }
01130 
01131       /* Wait until BTF flag is set */
01132       if (I2C_WaitOnBTFFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
01133       {
01134         if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
01135         {
01136           /* Generate Stop */
01137           SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
01138         }
01139         return HAL_ERROR;
01140       }
01141     }
01142 
01143     /* Generate Stop */
01144     SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
01145 
01146     hi2c->State = HAL_I2C_STATE_READY;
01147     hi2c->Mode = HAL_I2C_MODE_NONE;
01148 
01149     /* Process Unlocked */
01150     __HAL_UNLOCK(hi2c);
01151 
01152     return HAL_OK;
01153   }
01154   else
01155   {
01156     return HAL_BUSY;
01157   }
01158 }
01159 
01160 /**
01161   * @brief  Receives in master mode an amount of data in blocking mode.
01162   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
01163   *                the configuration information for the specified I2C.
01164   * @param  DevAddress Target device address: The device 7 bits address value
01165   *         in datasheet must be shifted to the left before calling the interface
01166   * @param  pData Pointer to data buffer
01167   * @param  Size Amount of data to be sent
01168   * @param  Timeout Timeout duration
01169   * @retval HAL status
01170   */
01171 HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
01172 {
01173   __IO uint32_t count = 0U;
01174 
01175   /* Init tickstart for timeout management*/
01176   uint32_t tickstart = HAL_GetTick();
01177 
01178   if (hi2c->State == HAL_I2C_STATE_READY)
01179   {
01180     /* Wait until BUSY flag is reset */
01181     if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
01182     {
01183       return HAL_BUSY;
01184     }
01185 
01186     /* Process Locked */
01187     __HAL_LOCK(hi2c);
01188 
01189     /* Check if the I2C is already enabled */
01190     if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
01191     {
01192       /* Enable I2C peripheral */
01193       __HAL_I2C_ENABLE(hi2c);
01194     }
01195 
01196     /* Disable Pos */
01197     CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
01198 
01199     hi2c->State       = HAL_I2C_STATE_BUSY_RX;
01200     hi2c->Mode        = HAL_I2C_MODE_MASTER;
01201     hi2c->ErrorCode   = HAL_I2C_ERROR_NONE;
01202 
01203     /* Prepare transfer parameters */
01204     hi2c->pBuffPtr    = pData;
01205     hi2c->XferCount   = Size;
01206     hi2c->XferSize    = hi2c->XferCount;
01207     hi2c->XferOptions = I2C_NO_OPTION_FRAME;
01208 
01209     /* Send Slave Address */
01210     if (I2C_MasterRequestRead(hi2c, DevAddress, Timeout, tickstart) != HAL_OK)
01211     {
01212       return HAL_ERROR;
01213     }
01214 
01215     if (hi2c->XferSize == 0U)
01216     {
01217       /* Clear ADDR flag */
01218       __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
01219 
01220       /* Generate Stop */
01221       SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
01222     }
01223     else if (hi2c->XferSize == 1U)
01224     {
01225       /* Disable Acknowledge */
01226       CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
01227 
01228       /* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3
01229       software sequence must complete before the current byte end of transfer */
01230       __disable_irq();
01231 
01232       /* Clear ADDR flag */
01233       __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
01234 
01235       /* Generate Stop */
01236       SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
01237 
01238       /* Re-enable IRQs */
01239       __enable_irq();
01240     }
01241     else if (hi2c->XferSize == 2U)
01242     {
01243       /* Enable Pos */
01244       SET_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
01245 
01246       /* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3
01247       software sequence must complete before the current byte end of transfer */
01248       __disable_irq();
01249 
01250       /* Clear ADDR flag */
01251       __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
01252 
01253       /* Disable Acknowledge */
01254       CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
01255 
01256       /* Re-enable IRQs */
01257       __enable_irq();
01258     }
01259     else
01260     {
01261       /* Enable Acknowledge */
01262       SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
01263 
01264       /* Clear ADDR flag */
01265       __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
01266     }
01267 
01268     while (hi2c->XferSize > 0U)
01269     {
01270       if (hi2c->XferSize <= 3U)
01271       {
01272         /* One byte */
01273         if (hi2c->XferSize == 1U)
01274         {
01275           /* Wait until RXNE flag is set */
01276           if (I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
01277           {
01278             return HAL_ERROR;
01279           }
01280 
01281           /* Read data from DR */
01282           *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
01283 
01284           /* Increment Buffer pointer */
01285           hi2c->pBuffPtr++;
01286 
01287           /* Update counter */
01288           hi2c->XferSize--;
01289           hi2c->XferCount--;
01290         }
01291         /* Two bytes */
01292         else if (hi2c->XferSize == 2U)
01293         {
01294           /* Wait until BTF flag is set */
01295           if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK)
01296           {
01297             return HAL_ERROR;
01298           }
01299 
01300           /* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3
01301              software sequence must complete before the current byte end of transfer */
01302           __disable_irq();
01303 
01304           /* Generate Stop */
01305           SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
01306 
01307           /* Read data from DR */
01308           *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
01309 
01310           /* Increment Buffer pointer */
01311           hi2c->pBuffPtr++;
01312 
01313           /* Update counter */
01314           hi2c->XferSize--;
01315           hi2c->XferCount--;
01316 
01317           /* Re-enable IRQs */
01318           __enable_irq();
01319 
01320           /* Read data from DR */
01321           *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
01322 
01323           /* Increment Buffer pointer */
01324           hi2c->pBuffPtr++;
01325 
01326           /* Update counter */
01327           hi2c->XferSize--;
01328           hi2c->XferCount--;
01329         }
01330         /* 3 Last bytes */
01331         else
01332         {
01333           /* Wait until BTF flag is set */
01334           if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK)
01335           {
01336             return HAL_ERROR;
01337           }
01338 
01339           /* Disable Acknowledge */
01340           CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
01341 
01342           /* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3
01343              software sequence must complete before the current byte end of transfer */
01344           __disable_irq();
01345 
01346           /* Read data from DR */
01347           *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
01348 
01349           /* Increment Buffer pointer */
01350           hi2c->pBuffPtr++;
01351 
01352           /* Update counter */
01353           hi2c->XferSize--;
01354           hi2c->XferCount--;
01355 
01356           /* Wait until BTF flag is set */
01357           count = I2C_TIMEOUT_FLAG * (SystemCoreClock / 25U / 1000U);
01358           do
01359           {
01360             count--;
01361             if (count == 0U)
01362             {
01363               hi2c->PreviousState       = I2C_STATE_NONE;
01364               hi2c->State               = HAL_I2C_STATE_READY;
01365               hi2c->Mode                = HAL_I2C_MODE_NONE;
01366               hi2c->ErrorCode           |= HAL_I2C_ERROR_TIMEOUT;
01367 
01368               /* Re-enable IRQs */
01369               __enable_irq();
01370 
01371               /* Process Unlocked */
01372               __HAL_UNLOCK(hi2c);
01373 
01374               return HAL_ERROR;
01375             }
01376           }
01377           while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == RESET);
01378 
01379           /* Generate Stop */
01380           SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
01381 
01382           /* Read data from DR */
01383           *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
01384 
01385           /* Increment Buffer pointer */
01386           hi2c->pBuffPtr++;
01387 
01388           /* Update counter */
01389           hi2c->XferSize--;
01390           hi2c->XferCount--;
01391 
01392           /* Re-enable IRQs */
01393           __enable_irq();
01394 
01395           /* Read data from DR */
01396           *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
01397 
01398           /* Increment Buffer pointer */
01399           hi2c->pBuffPtr++;
01400 
01401           /* Update counter */
01402           hi2c->XferSize--;
01403           hi2c->XferCount--;
01404         }
01405       }
01406       else
01407       {
01408         /* Wait until RXNE flag is set */
01409         if (I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
01410         {
01411           return HAL_ERROR;
01412         }
01413 
01414         /* Read data from DR */
01415         *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
01416 
01417         /* Increment Buffer pointer */
01418         hi2c->pBuffPtr++;
01419 
01420         /* Update counter */
01421         hi2c->XferSize--;
01422         hi2c->XferCount--;
01423 
01424         if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET)
01425         {
01426           /* Read data from DR */
01427           *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
01428 
01429           /* Increment Buffer pointer */
01430           hi2c->pBuffPtr++;
01431 
01432           /* Update counter */
01433           hi2c->XferSize--;
01434           hi2c->XferCount--;
01435         }
01436       }
01437     }
01438 
01439     hi2c->State = HAL_I2C_STATE_READY;
01440     hi2c->Mode = HAL_I2C_MODE_NONE;
01441 
01442     /* Process Unlocked */
01443     __HAL_UNLOCK(hi2c);
01444 
01445     return HAL_OK;
01446   }
01447   else
01448   {
01449     return HAL_BUSY;
01450   }
01451 }
01452 
01453 /**
01454   * @brief  Transmits in slave mode an amount of data in blocking mode.
01455   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
01456   *                the configuration information for the specified I2C.
01457   * @param  pData Pointer to data buffer
01458   * @param  Size Amount of data to be sent
01459   * @param  Timeout Timeout duration
01460   * @retval HAL status
01461   */
01462 HAL_StatusTypeDef HAL_I2C_Slave_Transmit(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
01463 {
01464   /* Init tickstart for timeout management*/
01465   uint32_t tickstart = HAL_GetTick();
01466 
01467   if (hi2c->State == HAL_I2C_STATE_READY)
01468   {
01469     if ((pData == NULL) || (Size == 0U))
01470     {
01471       return  HAL_ERROR;
01472     }
01473 
01474     /* Process Locked */
01475     __HAL_LOCK(hi2c);
01476 
01477     /* Check if the I2C is already enabled */
01478     if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
01479     {
01480       /* Enable I2C peripheral */
01481       __HAL_I2C_ENABLE(hi2c);
01482     }
01483 
01484     /* Disable Pos */
01485     CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
01486 
01487     hi2c->State       = HAL_I2C_STATE_BUSY_TX;
01488     hi2c->Mode        = HAL_I2C_MODE_SLAVE;
01489     hi2c->ErrorCode   = HAL_I2C_ERROR_NONE;
01490 
01491     /* Prepare transfer parameters */
01492     hi2c->pBuffPtr    = pData;
01493     hi2c->XferCount   = Size;
01494     hi2c->XferSize    = hi2c->XferCount;
01495     hi2c->XferOptions = I2C_NO_OPTION_FRAME;
01496 
01497     /* Enable Address Acknowledge */
01498     SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
01499 
01500     /* Wait until ADDR flag is set */
01501     if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK)
01502     {
01503       return HAL_ERROR;
01504     }
01505 
01506     /* Clear ADDR flag */
01507     __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
01508 
01509     /* If 10bit addressing mode is selected */
01510     if (hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT)
01511     {
01512       /* Wait until ADDR flag is set */
01513       if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK)
01514       {
01515         return HAL_ERROR;
01516       }
01517 
01518       /* Clear ADDR flag */
01519       __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
01520     }
01521 
01522     while (hi2c->XferSize > 0U)
01523     {
01524       /* Wait until TXE flag is set */
01525       if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
01526       {
01527         /* Disable Address Acknowledge */
01528         CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
01529 
01530         return HAL_ERROR;
01531       }
01532 
01533       /* Write data to DR */
01534       hi2c->Instance->DR = *hi2c->pBuffPtr;
01535 
01536       /* Increment Buffer pointer */
01537       hi2c->pBuffPtr++;
01538 
01539       /* Update counter */
01540       hi2c->XferCount--;
01541       hi2c->XferSize--;
01542 
01543       if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (hi2c->XferSize != 0U))
01544       {
01545         /* Write data to DR */
01546         hi2c->Instance->DR = *hi2c->pBuffPtr;
01547 
01548         /* Increment Buffer pointer */
01549         hi2c->pBuffPtr++;
01550 
01551         /* Update counter */
01552         hi2c->XferCount--;
01553         hi2c->XferSize--;
01554       }
01555     }
01556 
01557     /* Wait until AF flag is set */
01558     if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_AF, RESET, Timeout, tickstart) != HAL_OK)
01559     {
01560       return HAL_ERROR;
01561     }
01562 
01563     /* Clear AF flag */
01564     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
01565 
01566     /* Disable Address Acknowledge */
01567     CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
01568 
01569     hi2c->State = HAL_I2C_STATE_READY;
01570     hi2c->Mode = HAL_I2C_MODE_NONE;
01571 
01572     /* Process Unlocked */
01573     __HAL_UNLOCK(hi2c);
01574 
01575     return HAL_OK;
01576   }
01577   else
01578   {
01579     return HAL_BUSY;
01580   }
01581 }
01582 
01583 /**
01584   * @brief  Receive in slave mode an amount of data in blocking mode
01585   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
01586   *         the configuration information for the specified I2C.
01587   * @param  pData Pointer to data buffer
01588   * @param  Size Amount of data to be sent
01589   * @param  Timeout Timeout duration
01590   * @retval HAL status
01591   */
01592 HAL_StatusTypeDef HAL_I2C_Slave_Receive(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
01593 {
01594   /* Init tickstart for timeout management*/
01595   uint32_t tickstart = HAL_GetTick();
01596 
01597   if (hi2c->State == HAL_I2C_STATE_READY)
01598   {
01599     if ((pData == NULL) || (Size == (uint16_t)0))
01600     {
01601       return HAL_ERROR;
01602     }
01603 
01604     /* Process Locked */
01605     __HAL_LOCK(hi2c);
01606 
01607     /* Check if the I2C is already enabled */
01608     if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
01609     {
01610       /* Enable I2C peripheral */
01611       __HAL_I2C_ENABLE(hi2c);
01612     }
01613 
01614     /* Disable Pos */
01615     CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
01616 
01617     hi2c->State       = HAL_I2C_STATE_BUSY_RX;
01618     hi2c->Mode        = HAL_I2C_MODE_SLAVE;
01619     hi2c->ErrorCode   = HAL_I2C_ERROR_NONE;
01620 
01621     /* Prepare transfer parameters */
01622     hi2c->pBuffPtr    = pData;
01623     hi2c->XferCount   = Size;
01624     hi2c->XferSize    = hi2c->XferCount;
01625     hi2c->XferOptions = I2C_NO_OPTION_FRAME;
01626 
01627     /* Enable Address Acknowledge */
01628     SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
01629 
01630     /* Wait until ADDR flag is set */
01631     if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK)
01632     {
01633       return HAL_ERROR;
01634     }
01635 
01636     /* Clear ADDR flag */
01637     __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
01638 
01639     while (hi2c->XferSize > 0U)
01640     {
01641       /* Wait until RXNE flag is set */
01642       if (I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
01643       {
01644         /* Disable Address Acknowledge */
01645         CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
01646 
01647         return HAL_ERROR;
01648       }
01649 
01650       /* Read data from DR */
01651       *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
01652 
01653       /* Increment Buffer pointer */
01654       hi2c->pBuffPtr++;
01655 
01656       /* Update counter */
01657       hi2c->XferSize--;
01658       hi2c->XferCount--;
01659 
01660       if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (hi2c->XferSize != 0U))
01661       {
01662         /* Read data from DR */
01663         *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
01664 
01665         /* Increment Buffer pointer */
01666         hi2c->pBuffPtr++;
01667 
01668         /* Update counter */
01669         hi2c->XferSize--;
01670         hi2c->XferCount--;
01671       }
01672     }
01673 
01674     /* Wait until STOP flag is set */
01675     if (I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
01676     {
01677       /* Disable Address Acknowledge */
01678       CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
01679 
01680       return HAL_ERROR;
01681     }
01682 
01683     /* Clear STOP flag */
01684     __HAL_I2C_CLEAR_STOPFLAG(hi2c);
01685 
01686     /* Disable Address Acknowledge */
01687     CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
01688 
01689     hi2c->State = HAL_I2C_STATE_READY;
01690     hi2c->Mode = HAL_I2C_MODE_NONE;
01691 
01692     /* Process Unlocked */
01693     __HAL_UNLOCK(hi2c);
01694 
01695     return HAL_OK;
01696   }
01697   else
01698   {
01699     return HAL_BUSY;
01700   }
01701 }
01702 
01703 /**
01704   * @brief  Transmit in master mode an amount of data in non-blocking mode with Interrupt
01705   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
01706   *                the configuration information for the specified I2C.
01707   * @param  DevAddress Target device address: The device 7 bits address value
01708   *         in datasheet must be shifted to the left before calling the interface
01709   * @param  pData Pointer to data buffer
01710   * @param  Size Amount of data to be sent
01711   * @retval HAL status
01712   */
01713 HAL_StatusTypeDef HAL_I2C_Master_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
01714 {
01715   __IO uint32_t count = 0U;
01716 
01717   if (hi2c->State == HAL_I2C_STATE_READY)
01718   {
01719     /* Wait until BUSY flag is reset */
01720     count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
01721     do
01722     {
01723       count--;
01724       if (count == 0U)
01725       {
01726         hi2c->PreviousState       = I2C_STATE_NONE;
01727         hi2c->State               = HAL_I2C_STATE_READY;
01728         hi2c->Mode                = HAL_I2C_MODE_NONE;
01729         hi2c->ErrorCode           |= HAL_I2C_ERROR_TIMEOUT;
01730 
01731         /* Process Unlocked */
01732         __HAL_UNLOCK(hi2c);
01733 
01734         return HAL_ERROR;
01735       }
01736     }
01737     while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
01738 
01739     /* Process Locked */
01740     __HAL_LOCK(hi2c);
01741 
01742     /* Check if the I2C is already enabled */
01743     if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
01744     {
01745       /* Enable I2C peripheral */
01746       __HAL_I2C_ENABLE(hi2c);
01747     }
01748 
01749     /* Disable Pos */
01750     CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
01751 
01752     hi2c->State     = HAL_I2C_STATE_BUSY_TX;
01753     hi2c->Mode      = HAL_I2C_MODE_MASTER;
01754     hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
01755 
01756     /* Prepare transfer parameters */
01757     hi2c->pBuffPtr    = pData;
01758     hi2c->XferCount   = Size;
01759     hi2c->XferSize    = hi2c->XferCount;
01760     hi2c->XferOptions = I2C_NO_OPTION_FRAME;
01761     hi2c->Devaddress  = DevAddress;
01762 
01763     /* Process Unlocked */
01764     __HAL_UNLOCK(hi2c);
01765 
01766     /* Note : The I2C interrupts must be enabled after unlocking current process
01767               to avoid the risk of I2C interrupt handle execution before current
01768               process unlock */
01769     /* Enable EVT, BUF and ERR interrupt */
01770     __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
01771 
01772     /* Generate Start */
01773     SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
01774 
01775     return HAL_OK;
01776   }
01777   else
01778   {
01779     return HAL_BUSY;
01780   }
01781 }
01782 
01783 /**
01784   * @brief  Receive in master mode an amount of data in non-blocking mode with Interrupt
01785   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
01786   *                the configuration information for the specified I2C.
01787   * @param  DevAddress Target device address: The device 7 bits address value
01788   *         in datasheet must be shifted to the left before calling the interface
01789   * @param  pData Pointer to data buffer
01790   * @param  Size Amount of data to be sent
01791   * @retval HAL status
01792   */
01793 HAL_StatusTypeDef HAL_I2C_Master_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
01794 {
01795   __IO uint32_t count = 0U;
01796 
01797   if (hi2c->State == HAL_I2C_STATE_READY)
01798   {
01799     /* Wait until BUSY flag is reset */
01800     count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
01801     do
01802     {
01803       count--;
01804       if (count == 0U)
01805       {
01806         hi2c->PreviousState       = I2C_STATE_NONE;
01807         hi2c->State               = HAL_I2C_STATE_READY;
01808         hi2c->Mode                = HAL_I2C_MODE_NONE;
01809         hi2c->ErrorCode           |= HAL_I2C_ERROR_TIMEOUT;
01810 
01811         /* Process Unlocked */
01812         __HAL_UNLOCK(hi2c);
01813 
01814         return HAL_ERROR;
01815       }
01816     }
01817     while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
01818 
01819     /* Process Locked */
01820     __HAL_LOCK(hi2c);
01821 
01822     /* Check if the I2C is already enabled */
01823     if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
01824     {
01825       /* Enable I2C peripheral */
01826       __HAL_I2C_ENABLE(hi2c);
01827     }
01828 
01829     /* Disable Pos */
01830     CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
01831 
01832     hi2c->State     = HAL_I2C_STATE_BUSY_RX;
01833     hi2c->Mode      = HAL_I2C_MODE_MASTER;
01834     hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
01835 
01836     /* Prepare transfer parameters */
01837     hi2c->pBuffPtr    = pData;
01838     hi2c->XferCount   = Size;
01839     hi2c->XferSize    = hi2c->XferCount;
01840     hi2c->XferOptions = I2C_NO_OPTION_FRAME;
01841     hi2c->Devaddress  = DevAddress;
01842 
01843 
01844     /* Process Unlocked */
01845     __HAL_UNLOCK(hi2c);
01846 
01847     /* Note : The I2C interrupts must be enabled after unlocking current process
01848     to avoid the risk of I2C interrupt handle execution before current
01849     process unlock */
01850 
01851     /* Enable EVT, BUF and ERR interrupt */
01852     __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
01853 
01854     /* Enable Acknowledge */
01855     SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
01856 
01857     /* Generate Start */
01858     SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
01859 
01860     return HAL_OK;
01861   }
01862   else
01863   {
01864     return HAL_BUSY;
01865   }
01866 }
01867 
01868 /**
01869   * @brief  Transmit in slave mode an amount of data in non-blocking mode with Interrupt
01870   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
01871   *         the configuration information for the specified I2C.
01872   * @param  pData Pointer to data buffer
01873   * @param  Size Amount of data to be sent
01874   * @retval HAL status
01875   */
01876 HAL_StatusTypeDef HAL_I2C_Slave_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
01877 {
01878 
01879   if (hi2c->State == HAL_I2C_STATE_READY)
01880   {
01881     if ((pData == NULL) || (Size == 0U))
01882     {
01883       return  HAL_ERROR;
01884     }
01885 
01886     /* Process Locked */
01887     __HAL_LOCK(hi2c);
01888 
01889     /* Check if the I2C is already enabled */
01890     if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
01891     {
01892       /* Enable I2C peripheral */
01893       __HAL_I2C_ENABLE(hi2c);
01894     }
01895 
01896     /* Disable Pos */
01897     CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
01898 
01899     hi2c->State     = HAL_I2C_STATE_BUSY_TX;
01900     hi2c->Mode      = HAL_I2C_MODE_SLAVE;
01901     hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
01902 
01903     /* Prepare transfer parameters */
01904     hi2c->pBuffPtr    = pData;
01905     hi2c->XferCount   = Size;
01906     hi2c->XferSize    = hi2c->XferCount;
01907     hi2c->XferOptions = I2C_NO_OPTION_FRAME;
01908 
01909     /* Enable Address Acknowledge */
01910     SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
01911 
01912     /* Process Unlocked */
01913     __HAL_UNLOCK(hi2c);
01914 
01915     /* Note : The I2C interrupts must be enabled after unlocking current process
01916               to avoid the risk of I2C interrupt handle execution before current
01917               process unlock */
01918 
01919     /* Enable EVT, BUF and ERR interrupt */
01920     __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
01921 
01922     return HAL_OK;
01923   }
01924   else
01925   {
01926     return HAL_BUSY;
01927   }
01928 }
01929 
01930 /**
01931   * @brief  Receive in slave mode an amount of data in non-blocking mode with Interrupt
01932   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
01933   *                the configuration information for the specified I2C.
01934   * @param  pData Pointer to data buffer
01935   * @param  Size Amount of data to be sent
01936   * @retval HAL status
01937   */
01938 HAL_StatusTypeDef HAL_I2C_Slave_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
01939 {
01940 
01941   if (hi2c->State == HAL_I2C_STATE_READY)
01942   {
01943     if ((pData == NULL) || (Size == 0U))
01944     {
01945       return  HAL_ERROR;
01946     }
01947 
01948     /* Process Locked */
01949     __HAL_LOCK(hi2c);
01950 
01951     /* Check if the I2C is already enabled */
01952     if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
01953     {
01954       /* Enable I2C peripheral */
01955       __HAL_I2C_ENABLE(hi2c);
01956     }
01957 
01958     /* Disable Pos */
01959     CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
01960 
01961     hi2c->State     = HAL_I2C_STATE_BUSY_RX;
01962     hi2c->Mode      = HAL_I2C_MODE_SLAVE;
01963     hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
01964 
01965     /* Prepare transfer parameters */
01966     hi2c->pBuffPtr    = pData;
01967     hi2c->XferCount   = Size;
01968     hi2c->XferSize    = hi2c->XferCount;
01969     hi2c->XferOptions = I2C_NO_OPTION_FRAME;
01970 
01971     /* Enable Address Acknowledge */
01972     SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
01973 
01974     /* Process Unlocked */
01975     __HAL_UNLOCK(hi2c);
01976 
01977     /* Note : The I2C interrupts must be enabled after unlocking current process
01978               to avoid the risk of I2C interrupt handle execution before current
01979               process unlock */
01980 
01981     /* Enable EVT, BUF and ERR interrupt */
01982     __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
01983 
01984     return HAL_OK;
01985   }
01986   else
01987   {
01988     return HAL_BUSY;
01989   }
01990 }
01991 
01992 /**
01993   * @brief  Transmit in master mode an amount of data in non-blocking mode with DMA
01994   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
01995   *                the configuration information for the specified I2C.
01996   * @param  DevAddress Target device address: The device 7 bits address value
01997   *         in datasheet must be shifted to the left before calling the interface
01998   * @param  pData Pointer to data buffer
01999   * @param  Size Amount of data to be sent
02000   * @retval HAL status
02001   */
02002 HAL_StatusTypeDef HAL_I2C_Master_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
02003 {
02004   __IO uint32_t count = 0U;
02005   HAL_StatusTypeDef dmaxferstatus;
02006 
02007   if (hi2c->State == HAL_I2C_STATE_READY)
02008   {
02009     /* Wait until BUSY flag is reset */
02010     count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
02011     do
02012     {
02013       count--;
02014       if (count == 0U)
02015       {
02016         hi2c->PreviousState       = I2C_STATE_NONE;
02017         hi2c->State               = HAL_I2C_STATE_READY;
02018         hi2c->Mode                = HAL_I2C_MODE_NONE;
02019         hi2c->ErrorCode           |= HAL_I2C_ERROR_TIMEOUT;
02020 
02021         /* Process Unlocked */
02022         __HAL_UNLOCK(hi2c);
02023 
02024         return HAL_ERROR;
02025       }
02026     }
02027     while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
02028 
02029     /* Process Locked */
02030     __HAL_LOCK(hi2c);
02031 
02032     /* Check if the I2C is already enabled */
02033     if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
02034     {
02035       /* Enable I2C peripheral */
02036       __HAL_I2C_ENABLE(hi2c);
02037     }
02038 
02039     /* Disable Pos */
02040     CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
02041 
02042     hi2c->State     = HAL_I2C_STATE_BUSY_TX;
02043     hi2c->Mode      = HAL_I2C_MODE_MASTER;
02044     hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
02045 
02046     /* Prepare transfer parameters */
02047     hi2c->pBuffPtr    = pData;
02048     hi2c->XferCount   = Size;
02049     hi2c->XferSize    = hi2c->XferCount;
02050     hi2c->XferOptions = I2C_NO_OPTION_FRAME;
02051     hi2c->Devaddress  = DevAddress;
02052 
02053     if (hi2c->XferSize > 0U)
02054     {
02055       if (hi2c->hdmatx != NULL)
02056       {
02057         /* Set the I2C DMA transfer complete callback */
02058         hi2c->hdmatx->XferCpltCallback = I2C_DMAXferCplt;
02059 
02060         /* Set the DMA error callback */
02061         hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
02062 
02063         /* Set the unused DMA callbacks to NULL */
02064         hi2c->hdmatx->XferHalfCpltCallback = NULL;
02065         hi2c->hdmatx->XferAbortCallback = NULL;
02066 
02067         /* Enable the DMA channel */
02068         dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->DR, hi2c->XferSize);
02069       }
02070       else
02071       {
02072         /* Update I2C state */
02073         hi2c->State     = HAL_I2C_STATE_READY;
02074         hi2c->Mode      = HAL_I2C_MODE_NONE;
02075 
02076         /* Update I2C error code */
02077         hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
02078 
02079         /* Process Unlocked */
02080         __HAL_UNLOCK(hi2c);
02081 
02082         return HAL_ERROR;
02083       }
02084 
02085       if (dmaxferstatus == HAL_OK)
02086       {
02087         /* Process Unlocked */
02088         __HAL_UNLOCK(hi2c);
02089 
02090         /* Note : The I2C interrupts must be enabled after unlocking current process
02091         to avoid the risk of I2C interrupt handle execution before current
02092         process unlock */
02093 
02094         /* Enable EVT and ERR interrupt */
02095         __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
02096 
02097         /* Enable DMA Request */
02098         SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
02099 
02100         /* Enable Acknowledge */
02101         SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
02102 
02103         /* Generate Start */
02104         SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
02105       }
02106       else
02107       {
02108         /* Update I2C state */
02109         hi2c->State     = HAL_I2C_STATE_READY;
02110         hi2c->Mode      = HAL_I2C_MODE_NONE;
02111 
02112         /* Update I2C error code */
02113         hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
02114 
02115         /* Process Unlocked */
02116         __HAL_UNLOCK(hi2c);
02117 
02118         return HAL_ERROR;
02119       }
02120     }
02121     else
02122     {
02123       /* Enable Acknowledge */
02124       SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
02125 
02126       /* Generate Start */
02127       SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
02128 
02129       /* Process Unlocked */
02130       __HAL_UNLOCK(hi2c);
02131 
02132       /* Note : The I2C interrupts must be enabled after unlocking current process
02133       to avoid the risk of I2C interrupt handle execution before current
02134       process unlock */
02135 
02136       /* Enable EVT, BUF and ERR interrupt */
02137       __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
02138     }
02139 
02140     return HAL_OK;
02141   }
02142   else
02143   {
02144     return HAL_BUSY;
02145   }
02146 }
02147 
02148 /**
02149   * @brief  Receive in master mode an amount of data in non-blocking mode with DMA
02150   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
02151   *                the configuration information for the specified I2C.
02152   * @param  DevAddress Target device address: The device 7 bits address value
02153   *         in datasheet must be shifted to the left before calling the interface
02154   * @param  pData Pointer to data buffer
02155   * @param  Size Amount of data to be sent
02156   * @retval HAL status
02157   */
02158 HAL_StatusTypeDef HAL_I2C_Master_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
02159 {
02160   __IO uint32_t count = 0U;
02161   HAL_StatusTypeDef dmaxferstatus;
02162 
02163   if (hi2c->State == HAL_I2C_STATE_READY)
02164   {
02165     /* Wait until BUSY flag is reset */
02166     count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
02167     do
02168     {
02169       count--;
02170       if (count == 0U)
02171       {
02172         hi2c->PreviousState       = I2C_STATE_NONE;
02173         hi2c->State               = HAL_I2C_STATE_READY;
02174         hi2c->Mode                = HAL_I2C_MODE_NONE;
02175         hi2c->ErrorCode           |= HAL_I2C_ERROR_TIMEOUT;
02176 
02177         /* Process Unlocked */
02178         __HAL_UNLOCK(hi2c);
02179 
02180         return HAL_ERROR;
02181       }
02182     }
02183     while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
02184 
02185     /* Process Locked */
02186     __HAL_LOCK(hi2c);
02187 
02188     /* Check if the I2C is already enabled */
02189     if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
02190     {
02191       /* Enable I2C peripheral */
02192       __HAL_I2C_ENABLE(hi2c);
02193     }
02194 
02195     /* Disable Pos */
02196     CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
02197 
02198     hi2c->State     = HAL_I2C_STATE_BUSY_RX;
02199     hi2c->Mode      = HAL_I2C_MODE_MASTER;
02200     hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
02201 
02202     /* Prepare transfer parameters */
02203     hi2c->pBuffPtr    = pData;
02204     hi2c->XferCount   = Size;
02205     hi2c->XferSize    = hi2c->XferCount;
02206     hi2c->XferOptions = I2C_NO_OPTION_FRAME;
02207     hi2c->Devaddress  = DevAddress;
02208 
02209     if (hi2c->XferSize > 0U)
02210     {
02211       if (hi2c->hdmarx != NULL)
02212       {
02213         /* Set the I2C DMA transfer complete callback */
02214         hi2c->hdmarx->XferCpltCallback = I2C_DMAXferCplt;
02215 
02216         /* Set the DMA error callback */
02217         hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
02218 
02219         /* Set the unused DMA callbacks to NULL */
02220         hi2c->hdmarx->XferHalfCpltCallback = NULL;
02221         hi2c->hdmarx->XferAbortCallback = NULL;
02222 
02223         /* Enable the DMA channel */
02224         dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);
02225       }
02226       else
02227       {
02228         /* Update I2C state */
02229         hi2c->State     = HAL_I2C_STATE_READY;
02230         hi2c->Mode      = HAL_I2C_MODE_NONE;
02231 
02232         /* Update I2C error code */
02233         hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
02234 
02235         /* Process Unlocked */
02236         __HAL_UNLOCK(hi2c);
02237 
02238         return HAL_ERROR;
02239       }
02240 
02241       if (dmaxferstatus == HAL_OK)
02242       {
02243         /* Enable Acknowledge */
02244         SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
02245 
02246         /* Generate Start */
02247         SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
02248 
02249         /* Process Unlocked */
02250         __HAL_UNLOCK(hi2c);
02251 
02252         /* Note : The I2C interrupts must be enabled after unlocking current process
02253         to avoid the risk of I2C interrupt handle execution before current
02254         process unlock */
02255 
02256         /* Enable EVT and ERR interrupt */
02257         __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
02258 
02259         /* Enable DMA Request */
02260         SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
02261       }
02262       else
02263       {
02264         /* Update I2C state */
02265         hi2c->State     = HAL_I2C_STATE_READY;
02266         hi2c->Mode      = HAL_I2C_MODE_NONE;
02267 
02268         /* Update I2C error code */
02269         hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
02270 
02271         /* Process Unlocked */
02272         __HAL_UNLOCK(hi2c);
02273 
02274         return HAL_ERROR;
02275       }
02276     }
02277     else
02278     {
02279       /* Process Unlocked */
02280       __HAL_UNLOCK(hi2c);
02281 
02282       /* Note : The I2C interrupts must be enabled after unlocking current process
02283       to avoid the risk of I2C interrupt handle execution before current
02284       process unlock */
02285 
02286       /* Enable EVT, BUF and ERR interrupt */
02287       __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
02288 
02289       /* Enable Acknowledge */
02290       SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
02291 
02292       /* Generate Start */
02293       SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
02294     }
02295 
02296     return HAL_OK;
02297   }
02298   else
02299   {
02300     return HAL_BUSY;
02301   }
02302 }
02303 
02304 /**
02305   * @brief  Transmit in slave mode an amount of data in non-blocking mode with DMA
02306   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
02307   *                the configuration information for the specified I2C.
02308   * @param  pData Pointer to data buffer
02309   * @param  Size Amount of data to be sent
02310   * @retval HAL status
02311   */
02312 HAL_StatusTypeDef HAL_I2C_Slave_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
02313 {
02314   HAL_StatusTypeDef dmaxferstatus;
02315 
02316   if (hi2c->State == HAL_I2C_STATE_READY)
02317   {
02318     if ((pData == NULL) || (Size == 0U))
02319     {
02320       return  HAL_ERROR;
02321     }
02322 
02323     /* Process Locked */
02324     __HAL_LOCK(hi2c);
02325 
02326     /* Check if the I2C is already enabled */
02327     if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
02328     {
02329       /* Enable I2C peripheral */
02330       __HAL_I2C_ENABLE(hi2c);
02331     }
02332 
02333     /* Disable Pos */
02334     CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
02335 
02336     hi2c->State     = HAL_I2C_STATE_BUSY_TX;
02337     hi2c->Mode      = HAL_I2C_MODE_SLAVE;
02338     hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
02339 
02340     /* Prepare transfer parameters */
02341     hi2c->pBuffPtr    = pData;
02342     hi2c->XferCount   = Size;
02343     hi2c->XferSize    = hi2c->XferCount;
02344     hi2c->XferOptions = I2C_NO_OPTION_FRAME;
02345 
02346     if (hi2c->hdmatx != NULL)
02347     {
02348       /* Set the I2C DMA transfer complete callback */
02349       hi2c->hdmatx->XferCpltCallback = I2C_DMAXferCplt;
02350 
02351       /* Set the DMA error callback */
02352       hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
02353 
02354       /* Set the unused DMA callbacks to NULL */
02355       hi2c->hdmatx->XferHalfCpltCallback = NULL;
02356       hi2c->hdmatx->XferAbortCallback = NULL;
02357 
02358       /* Enable the DMA channel */
02359       dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->DR, hi2c->XferSize);
02360     }
02361     else
02362     {
02363       /* Update I2C state */
02364       hi2c->State     = HAL_I2C_STATE_LISTEN;
02365       hi2c->Mode      = HAL_I2C_MODE_NONE;
02366 
02367       /* Update I2C error code */
02368       hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
02369 
02370       /* Process Unlocked */
02371       __HAL_UNLOCK(hi2c);
02372 
02373       return HAL_ERROR;
02374     }
02375 
02376     if (dmaxferstatus == HAL_OK)
02377     {
02378       /* Enable Address Acknowledge */
02379       SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
02380 
02381       /* Process Unlocked */
02382       __HAL_UNLOCK(hi2c);
02383 
02384       /* Note : The I2C interrupts must be enabled after unlocking current process
02385       to avoid the risk of I2C interrupt handle execution before current
02386       process unlock */
02387       /* Enable EVT and ERR interrupt */
02388       __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
02389 
02390       /* Enable DMA Request */
02391       hi2c->Instance->CR2 |= I2C_CR2_DMAEN;
02392 
02393       return HAL_OK;
02394     }
02395     else
02396     {
02397       /* Update I2C state */
02398       hi2c->State     = HAL_I2C_STATE_READY;
02399       hi2c->Mode      = HAL_I2C_MODE_NONE;
02400 
02401       /* Update I2C error code */
02402       hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
02403 
02404       /* Process Unlocked */
02405       __HAL_UNLOCK(hi2c);
02406 
02407       return HAL_ERROR;
02408     }
02409   }
02410   else
02411   {
02412     return HAL_BUSY;
02413   }
02414 }
02415 
02416 /**
02417   * @brief  Receive in slave mode an amount of data in non-blocking mode with DMA
02418   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
02419   *                the configuration information for the specified I2C.
02420   * @param  pData Pointer to data buffer
02421   * @param  Size Amount of data to be sent
02422   * @retval HAL status
02423   */
02424 HAL_StatusTypeDef HAL_I2C_Slave_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
02425 {
02426   HAL_StatusTypeDef dmaxferstatus;
02427 
02428   if (hi2c->State == HAL_I2C_STATE_READY)
02429   {
02430     if ((pData == NULL) || (Size == 0U))
02431     {
02432       return  HAL_ERROR;
02433     }
02434 
02435     /* Process Locked */
02436     __HAL_LOCK(hi2c);
02437 
02438     /* Check if the I2C is already enabled */
02439     if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
02440     {
02441       /* Enable I2C peripheral */
02442       __HAL_I2C_ENABLE(hi2c);
02443     }
02444 
02445     /* Disable Pos */
02446     CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
02447 
02448     hi2c->State     = HAL_I2C_STATE_BUSY_RX;
02449     hi2c->Mode      = HAL_I2C_MODE_SLAVE;
02450     hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
02451 
02452     /* Prepare transfer parameters */
02453     hi2c->pBuffPtr    = pData;
02454     hi2c->XferCount   = Size;
02455     hi2c->XferSize    = hi2c->XferCount;
02456     hi2c->XferOptions = I2C_NO_OPTION_FRAME;
02457 
02458     if (hi2c->hdmarx != NULL)
02459     {
02460       /* Set the I2C DMA transfer complete callback */
02461       hi2c->hdmarx->XferCpltCallback = I2C_DMAXferCplt;
02462 
02463       /* Set the DMA error callback */
02464       hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
02465 
02466       /* Set the unused DMA callbacks to NULL */
02467       hi2c->hdmarx->XferHalfCpltCallback = NULL;
02468       hi2c->hdmarx->XferAbortCallback = NULL;
02469 
02470       /* Enable the DMA channel */
02471       dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);
02472     }
02473     else
02474     {
02475       /* Update I2C state */
02476       hi2c->State     = HAL_I2C_STATE_LISTEN;
02477       hi2c->Mode      = HAL_I2C_MODE_NONE;
02478 
02479       /* Update I2C error code */
02480       hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
02481 
02482       /* Process Unlocked */
02483       __HAL_UNLOCK(hi2c);
02484 
02485       return HAL_ERROR;
02486     }
02487 
02488     if (dmaxferstatus == HAL_OK)
02489     {
02490       /* Enable Address Acknowledge */
02491       SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
02492 
02493       /* Process Unlocked */
02494       __HAL_UNLOCK(hi2c);
02495 
02496       /* Note : The I2C interrupts must be enabled after unlocking current process
02497       to avoid the risk of I2C interrupt handle execution before current
02498       process unlock */
02499       /* Enable EVT and ERR interrupt */
02500       __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
02501 
02502       /* Enable DMA Request */
02503       SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
02504 
02505       return HAL_OK;
02506     }
02507     else
02508     {
02509       /* Update I2C state */
02510       hi2c->State     = HAL_I2C_STATE_READY;
02511       hi2c->Mode      = HAL_I2C_MODE_NONE;
02512 
02513       /* Update I2C error code */
02514       hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
02515 
02516       /* Process Unlocked */
02517       __HAL_UNLOCK(hi2c);
02518 
02519       return HAL_ERROR;
02520     }
02521   }
02522   else
02523   {
02524     return HAL_BUSY;
02525   }
02526 }
02527 
02528 /**
02529   * @brief  Write an amount of data in blocking mode to a specific memory address
02530   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
02531   *                the configuration information for the specified I2C.
02532   * @param  DevAddress Target device address: The device 7 bits address value
02533   *         in datasheet must be shifted to the left before calling the interface
02534   * @param  MemAddress Internal memory address
02535   * @param  MemAddSize Size of internal memory address
02536   * @param  pData Pointer to data buffer
02537   * @param  Size Amount of data to be sent
02538   * @param  Timeout Timeout duration
02539   * @retval HAL status
02540   */
02541 HAL_StatusTypeDef HAL_I2C_Mem_Write(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
02542 {
02543   /* Init tickstart for timeout management*/
02544   uint32_t tickstart = HAL_GetTick();
02545 
02546   /* Check the parameters */
02547   assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
02548 
02549   if (hi2c->State == HAL_I2C_STATE_READY)
02550   {
02551     /* Wait until BUSY flag is reset */
02552     if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
02553     {
02554       return HAL_BUSY;
02555     }
02556 
02557     /* Process Locked */
02558     __HAL_LOCK(hi2c);
02559 
02560     /* Check if the I2C is already enabled */
02561     if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
02562     {
02563       /* Enable I2C peripheral */
02564       __HAL_I2C_ENABLE(hi2c);
02565     }
02566 
02567     /* Disable Pos */
02568     CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
02569 
02570     hi2c->State     = HAL_I2C_STATE_BUSY_TX;
02571     hi2c->Mode      = HAL_I2C_MODE_MEM;
02572     hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
02573 
02574     /* Prepare transfer parameters */
02575     hi2c->pBuffPtr    = pData;
02576     hi2c->XferCount   = Size;
02577     hi2c->XferSize    = hi2c->XferCount;
02578     hi2c->XferOptions = I2C_NO_OPTION_FRAME;
02579 
02580     /* Send Slave Address and Memory Address */
02581     if (I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, Timeout, tickstart) != HAL_OK)
02582     {
02583       return HAL_ERROR;
02584     }
02585 
02586     while (hi2c->XferSize > 0U)
02587     {
02588       /* Wait until TXE flag is set */
02589       if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
02590       {
02591         if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
02592         {
02593           /* Generate Stop */
02594           SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
02595         }
02596         return HAL_ERROR;
02597       }
02598 
02599       /* Write data to DR */
02600       hi2c->Instance->DR = *hi2c->pBuffPtr;
02601 
02602       /* Increment Buffer pointer */
02603       hi2c->pBuffPtr++;
02604 
02605       /* Update counter */
02606       hi2c->XferSize--;
02607       hi2c->XferCount--;
02608 
02609       if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (hi2c->XferSize != 0U))
02610       {
02611         /* Write data to DR */
02612         hi2c->Instance->DR = *hi2c->pBuffPtr;
02613 
02614         /* Increment Buffer pointer */
02615         hi2c->pBuffPtr++;
02616 
02617         /* Update counter */
02618         hi2c->XferSize--;
02619         hi2c->XferCount--;
02620       }
02621     }
02622 
02623     /* Wait until BTF flag is set */
02624     if (I2C_WaitOnBTFFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
02625     {
02626       if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
02627       {
02628         /* Generate Stop */
02629         SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
02630       }
02631       return HAL_ERROR;
02632     }
02633 
02634     /* Generate Stop */
02635     SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
02636 
02637     hi2c->State = HAL_I2C_STATE_READY;
02638     hi2c->Mode = HAL_I2C_MODE_NONE;
02639 
02640     /* Process Unlocked */
02641     __HAL_UNLOCK(hi2c);
02642 
02643     return HAL_OK;
02644   }
02645   else
02646   {
02647     return HAL_BUSY;
02648   }
02649 }
02650 
02651 /**
02652   * @brief  Read an amount of data in blocking mode from a specific memory address
02653   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
02654   *                the configuration information for the specified I2C.
02655   * @param  DevAddress Target device address: The device 7 bits address value
02656   *         in datasheet must be shifted to the left before calling the interface
02657   * @param  MemAddress Internal memory address
02658   * @param  MemAddSize Size of internal memory address
02659   * @param  pData Pointer to data buffer
02660   * @param  Size Amount of data to be sent
02661   * @param  Timeout Timeout duration
02662   * @retval HAL status
02663   */
02664 HAL_StatusTypeDef HAL_I2C_Mem_Read(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
02665 {
02666   __IO uint32_t count = 0U;
02667 
02668   /* Init tickstart for timeout management*/
02669   uint32_t tickstart = HAL_GetTick();
02670 
02671   /* Check the parameters */
02672   assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
02673 
02674   if (hi2c->State == HAL_I2C_STATE_READY)
02675   {
02676     /* Wait until BUSY flag is reset */
02677     if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
02678     {
02679       return HAL_BUSY;
02680     }
02681 
02682     /* Process Locked */
02683     __HAL_LOCK(hi2c);
02684 
02685     /* Check if the I2C is already enabled */
02686     if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
02687     {
02688       /* Enable I2C peripheral */
02689       __HAL_I2C_ENABLE(hi2c);
02690     }
02691 
02692     /* Disable Pos */
02693     CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
02694 
02695     hi2c->State     = HAL_I2C_STATE_BUSY_RX;
02696     hi2c->Mode      = HAL_I2C_MODE_MEM;
02697     hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
02698 
02699     /* Prepare transfer parameters */
02700     hi2c->pBuffPtr    = pData;
02701     hi2c->XferCount   = Size;
02702     hi2c->XferSize    = hi2c->XferCount;
02703     hi2c->XferOptions = I2C_NO_OPTION_FRAME;
02704 
02705     /* Send Slave Address and Memory Address */
02706     if (I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, Timeout, tickstart) != HAL_OK)
02707     {
02708       return HAL_ERROR;
02709     }
02710 
02711     if (hi2c->XferSize == 0U)
02712     {
02713       /* Clear ADDR flag */
02714       __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
02715 
02716       /* Generate Stop */
02717       SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
02718     }
02719     else if (hi2c->XferSize == 1U)
02720     {
02721       /* Disable Acknowledge */
02722       CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
02723 
02724       /* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3
02725          software sequence must complete before the current byte end of transfer */
02726       __disable_irq();
02727 
02728       /* Clear ADDR flag */
02729       __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
02730 
02731       /* Generate Stop */
02732       SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
02733 
02734       /* Re-enable IRQs */
02735       __enable_irq();
02736     }
02737     else if (hi2c->XferSize == 2U)
02738     {
02739       /* Enable Pos */
02740       SET_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
02741 
02742       /* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3
02743          software sequence must complete before the current byte end of transfer */
02744       __disable_irq();
02745 
02746       /* Clear ADDR flag */
02747       __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
02748 
02749       /* Disable Acknowledge */
02750       CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
02751 
02752       /* Re-enable IRQs */
02753       __enable_irq();
02754     }
02755     else
02756     {
02757       /* Enable Acknowledge */
02758       SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
02759       /* Clear ADDR flag */
02760       __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
02761     }
02762 
02763     while (hi2c->XferSize > 0U)
02764     {
02765       if (hi2c->XferSize <= 3U)
02766       {
02767         /* One byte */
02768         if (hi2c->XferSize == 1U)
02769         {
02770           /* Wait until RXNE flag is set */
02771           if (I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
02772           {
02773             return HAL_ERROR;
02774           }
02775 
02776           /* Read data from DR */
02777           *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
02778 
02779           /* Increment Buffer pointer */
02780           hi2c->pBuffPtr++;
02781 
02782           /* Update counter */
02783           hi2c->XferSize--;
02784           hi2c->XferCount--;
02785         }
02786         /* Two bytes */
02787         else if (hi2c->XferSize == 2U)
02788         {
02789           /* Wait until BTF flag is set */
02790           if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK)
02791           {
02792             return HAL_ERROR;
02793           }
02794 
02795           /* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3
02796              software sequence must complete before the current byte end of transfer */
02797           __disable_irq();
02798 
02799           /* Generate Stop */
02800           SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
02801 
02802           /* Read data from DR */
02803           *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
02804 
02805           /* Increment Buffer pointer */
02806           hi2c->pBuffPtr++;
02807 
02808           /* Update counter */
02809           hi2c->XferSize--;
02810           hi2c->XferCount--;
02811 
02812           /* Re-enable IRQs */
02813           __enable_irq();
02814 
02815           /* Read data from DR */
02816           *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
02817 
02818           /* Increment Buffer pointer */
02819           hi2c->pBuffPtr++;
02820 
02821           /* Update counter */
02822           hi2c->XferSize--;
02823           hi2c->XferCount--;
02824         }
02825         /* 3 Last bytes */
02826         else
02827         {
02828           /* Wait until BTF flag is set */
02829           if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK)
02830           {
02831             return HAL_ERROR;
02832           }
02833 
02834           /* Disable Acknowledge */
02835           CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
02836 
02837           /* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3
02838              software sequence must complete before the current byte end of transfer */
02839           __disable_irq();
02840 
02841           /* Read data from DR */
02842           *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
02843 
02844           /* Increment Buffer pointer */
02845           hi2c->pBuffPtr++;
02846 
02847           /* Update counter */
02848           hi2c->XferSize--;
02849           hi2c->XferCount--;
02850 
02851           /* Wait until BTF flag is set */
02852           count = I2C_TIMEOUT_FLAG * (SystemCoreClock / 25U / 1000U);
02853           do
02854           {
02855             count--;
02856             if (count == 0U)
02857             {
02858               hi2c->PreviousState       = I2C_STATE_NONE;
02859               hi2c->State               = HAL_I2C_STATE_READY;
02860               hi2c->Mode                = HAL_I2C_MODE_NONE;
02861               hi2c->ErrorCode           |= HAL_I2C_ERROR_TIMEOUT;
02862 
02863               /* Re-enable IRQs */
02864               __enable_irq();
02865 
02866               /* Process Unlocked */
02867               __HAL_UNLOCK(hi2c);
02868 
02869               return HAL_ERROR;
02870             }
02871           }
02872           while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == RESET);
02873 
02874           /* Generate Stop */
02875           SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
02876 
02877           /* Read data from DR */
02878           *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
02879 
02880           /* Increment Buffer pointer */
02881           hi2c->pBuffPtr++;
02882 
02883           /* Update counter */
02884           hi2c->XferSize--;
02885           hi2c->XferCount--;
02886 
02887           /* Re-enable IRQs */
02888           __enable_irq();
02889 
02890           /* Read data from DR */
02891           *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
02892 
02893           /* Increment Buffer pointer */
02894           hi2c->pBuffPtr++;
02895 
02896           /* Update counter */
02897           hi2c->XferSize--;
02898           hi2c->XferCount--;
02899         }
02900       }
02901       else
02902       {
02903         /* Wait until RXNE flag is set */
02904         if (I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
02905         {
02906           return HAL_ERROR;
02907         }
02908 
02909         /* Read data from DR */
02910         *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
02911 
02912         /* Increment Buffer pointer */
02913         hi2c->pBuffPtr++;
02914 
02915         /* Update counter */
02916         hi2c->XferSize--;
02917         hi2c->XferCount--;
02918 
02919         if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET)
02920         {
02921           /* Read data from DR */
02922           *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
02923 
02924           /* Increment Buffer pointer */
02925           hi2c->pBuffPtr++;
02926 
02927           /* Update counter */
02928           hi2c->XferSize--;
02929           hi2c->XferCount--;
02930         }
02931       }
02932     }
02933 
02934     hi2c->State = HAL_I2C_STATE_READY;
02935     hi2c->Mode = HAL_I2C_MODE_NONE;
02936 
02937     /* Process Unlocked */
02938     __HAL_UNLOCK(hi2c);
02939 
02940     return HAL_OK;
02941   }
02942   else
02943   {
02944     return HAL_BUSY;
02945   }
02946 }
02947 
02948 /**
02949   * @brief  Write an amount of data in non-blocking mode with Interrupt to a specific memory address
02950   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
02951   *                the configuration information for the specified I2C.
02952   * @param  DevAddress Target device address: The device 7 bits address value
02953   *         in datasheet must be shifted to the left before calling the interface
02954   * @param  MemAddress Internal memory address
02955   * @param  MemAddSize Size of internal memory address
02956   * @param  pData Pointer to data buffer
02957   * @param  Size Amount of data to be sent
02958   * @retval HAL status
02959   */
02960 HAL_StatusTypeDef HAL_I2C_Mem_Write_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
02961 {
02962   __IO uint32_t count = 0U;
02963 
02964   /* Check the parameters */
02965   assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
02966 
02967   if (hi2c->State == HAL_I2C_STATE_READY)
02968   {
02969     /* Wait until BUSY flag is reset */
02970     count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
02971     do
02972     {
02973       count--;
02974       if (count == 0U)
02975       {
02976         hi2c->PreviousState       = I2C_STATE_NONE;
02977         hi2c->State               = HAL_I2C_STATE_READY;
02978         hi2c->Mode                = HAL_I2C_MODE_NONE;
02979         hi2c->ErrorCode           |= HAL_I2C_ERROR_TIMEOUT;
02980 
02981         /* Process Unlocked */
02982         __HAL_UNLOCK(hi2c);
02983 
02984         return HAL_ERROR;
02985       }
02986     }
02987     while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
02988 
02989     /* Process Locked */
02990     __HAL_LOCK(hi2c);
02991 
02992     /* Check if the I2C is already enabled */
02993     if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
02994     {
02995       /* Enable I2C peripheral */
02996       __HAL_I2C_ENABLE(hi2c);
02997     }
02998 
02999     /* Disable Pos */
03000     CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
03001 
03002     hi2c->State     = HAL_I2C_STATE_BUSY_TX;
03003     hi2c->Mode      = HAL_I2C_MODE_MEM;
03004     hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
03005 
03006     /* Prepare transfer parameters */
03007     hi2c->pBuffPtr    = pData;
03008     hi2c->XferCount   = Size;
03009     hi2c->XferSize    = hi2c->XferCount;
03010     hi2c->XferOptions = I2C_NO_OPTION_FRAME;
03011     hi2c->Devaddress  = DevAddress;
03012     hi2c->Memaddress  = MemAddress;
03013     hi2c->MemaddSize  = MemAddSize;
03014     hi2c->EventCount  = 0U;
03015 
03016     /* Generate Start */
03017     SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
03018 
03019     /* Process Unlocked */
03020     __HAL_UNLOCK(hi2c);
03021 
03022     /* Note : The I2C interrupts must be enabled after unlocking current process
03023     to avoid the risk of I2C interrupt handle execution before current
03024     process unlock */
03025 
03026     /* Enable EVT, BUF and ERR interrupt */
03027     __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
03028 
03029     return HAL_OK;
03030   }
03031   else
03032   {
03033     return HAL_BUSY;
03034   }
03035 }
03036 
03037 /**
03038   * @brief  Read an amount of data in non-blocking mode with Interrupt from a specific memory address
03039   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
03040   *                the configuration information for the specified I2C.
03041   * @param  DevAddress Target device address
03042   * @param  MemAddress Internal memory address
03043   * @param  MemAddSize Size of internal memory address
03044   * @param  pData Pointer to data buffer
03045   * @param  Size Amount of data to be sent
03046   * @retval HAL status
03047   */
03048 HAL_StatusTypeDef HAL_I2C_Mem_Read_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
03049 {
03050   __IO uint32_t count = 0U;
03051 
03052   /* Check the parameters */
03053   assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
03054 
03055   if (hi2c->State == HAL_I2C_STATE_READY)
03056   {
03057     /* Wait until BUSY flag is reset */
03058     count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
03059     do
03060     {
03061       count--;
03062       if (count == 0U)
03063       {
03064         hi2c->PreviousState       = I2C_STATE_NONE;
03065         hi2c->State               = HAL_I2C_STATE_READY;
03066         hi2c->Mode                = HAL_I2C_MODE_NONE;
03067         hi2c->ErrorCode           |= HAL_I2C_ERROR_TIMEOUT;
03068 
03069         /* Process Unlocked */
03070         __HAL_UNLOCK(hi2c);
03071 
03072         return HAL_ERROR;
03073       }
03074     }
03075     while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
03076 
03077     /* Process Locked */
03078     __HAL_LOCK(hi2c);
03079 
03080     /* Check if the I2C is already enabled */
03081     if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
03082     {
03083       /* Enable I2C peripheral */
03084       __HAL_I2C_ENABLE(hi2c);
03085     }
03086 
03087     /* Disable Pos */
03088     CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
03089 
03090     hi2c->State     = HAL_I2C_STATE_BUSY_RX;
03091     hi2c->Mode      = HAL_I2C_MODE_MEM;
03092     hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
03093 
03094     /* Prepare transfer parameters */
03095     hi2c->pBuffPtr    = pData;
03096     hi2c->XferCount   = Size;
03097     hi2c->XferSize    = hi2c->XferCount;
03098     hi2c->XferOptions = I2C_NO_OPTION_FRAME;
03099     hi2c->Devaddress  = DevAddress;
03100     hi2c->Memaddress  = MemAddress;
03101     hi2c->MemaddSize  = MemAddSize;
03102     hi2c->EventCount  = 0U;
03103 
03104     /* Enable Acknowledge */
03105     SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
03106 
03107     /* Generate Start */
03108     SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
03109 
03110     /* Process Unlocked */
03111     __HAL_UNLOCK(hi2c);
03112 
03113     if (hi2c->XferSize > 0U)
03114     {
03115       /* Note : The I2C interrupts must be enabled after unlocking current process
03116       to avoid the risk of I2C interrupt handle execution before current
03117       process unlock */
03118 
03119       /* Enable EVT, BUF and ERR interrupt */
03120       __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
03121     }
03122     return HAL_OK;
03123   }
03124   else
03125   {
03126     return HAL_BUSY;
03127   }
03128 }
03129 
03130 /**
03131   * @brief  Write an amount of data in non-blocking mode with DMA to a specific memory address
03132   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
03133   *                the configuration information for the specified I2C.
03134   * @param  DevAddress Target device address: The device 7 bits address value
03135   *         in datasheet must be shifted to the left before calling the interface
03136   * @param  MemAddress Internal memory address
03137   * @param  MemAddSize Size of internal memory address
03138   * @param  pData Pointer to data buffer
03139   * @param  Size Amount of data to be sent
03140   * @retval HAL status
03141   */
03142 HAL_StatusTypeDef HAL_I2C_Mem_Write_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
03143 {
03144   __IO uint32_t count = 0U;
03145   HAL_StatusTypeDef dmaxferstatus;
03146 
03147   /* Init tickstart for timeout management*/
03148   uint32_t tickstart = HAL_GetTick();
03149 
03150   /* Check the parameters */
03151   assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
03152 
03153   if (hi2c->State == HAL_I2C_STATE_READY)
03154   {
03155     /* Wait until BUSY flag is reset */
03156     count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
03157     do
03158     {
03159       count--;
03160       if (count == 0U)
03161       {
03162         hi2c->PreviousState       = I2C_STATE_NONE;
03163         hi2c->State               = HAL_I2C_STATE_READY;
03164         hi2c->Mode                = HAL_I2C_MODE_NONE;
03165         hi2c->ErrorCode           |= HAL_I2C_ERROR_TIMEOUT;
03166 
03167         /* Process Unlocked */
03168         __HAL_UNLOCK(hi2c);
03169 
03170         return HAL_ERROR;
03171       }
03172     }
03173     while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
03174 
03175     /* Process Locked */
03176     __HAL_LOCK(hi2c);
03177 
03178     /* Check if the I2C is already enabled */
03179     if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
03180     {
03181       /* Enable I2C peripheral */
03182       __HAL_I2C_ENABLE(hi2c);
03183     }
03184 
03185     /* Disable Pos */
03186     CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
03187 
03188     hi2c->State     = HAL_I2C_STATE_BUSY_TX;
03189     hi2c->Mode      = HAL_I2C_MODE_MEM;
03190     hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
03191 
03192     /* Prepare transfer parameters */
03193     hi2c->pBuffPtr    = pData;
03194     hi2c->XferCount   = Size;
03195     hi2c->XferSize    = hi2c->XferCount;
03196     hi2c->XferOptions = I2C_NO_OPTION_FRAME;
03197 
03198     if (hi2c->XferSize > 0U)
03199     {
03200       if (hi2c->hdmatx != NULL)
03201       {
03202         /* Set the I2C DMA transfer complete callback */
03203         hi2c->hdmatx->XferCpltCallback = I2C_DMAXferCplt;
03204 
03205         /* Set the DMA error callback */
03206         hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
03207 
03208         /* Set the unused DMA callbacks to NULL */
03209         hi2c->hdmatx->XferHalfCpltCallback = NULL;
03210         hi2c->hdmatx->XferAbortCallback = NULL;
03211 
03212         /* Enable the DMA channel */
03213         dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->DR, hi2c->XferSize);
03214       }
03215       else
03216       {
03217         /* Update I2C state */
03218         hi2c->State     = HAL_I2C_STATE_READY;
03219         hi2c->Mode      = HAL_I2C_MODE_NONE;
03220 
03221         /* Update I2C error code */
03222         hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
03223 
03224         /* Process Unlocked */
03225         __HAL_UNLOCK(hi2c);
03226 
03227         return HAL_ERROR;
03228       }
03229 
03230       if (dmaxferstatus == HAL_OK)
03231       {
03232         /* Send Slave Address and Memory Address */
03233         if (I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
03234         {
03235           /* Abort the ongoing DMA */
03236           dmaxferstatus = HAL_DMA_Abort_IT(hi2c->hdmatx);
03237 
03238           /* Prevent unused argument(s) compilation and MISRA warning */
03239           UNUSED(dmaxferstatus);
03240 
03241           /* Set the unused I2C DMA transfer complete callback to NULL */
03242           hi2c->hdmatx->XferCpltCallback = NULL;
03243 
03244           /* Disable Acknowledge */
03245           CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
03246 
03247           hi2c->XferSize = 0U;
03248           hi2c->XferCount = 0U;
03249 
03250           /* Disable I2C peripheral to prevent dummy data in buffer */
03251           __HAL_I2C_DISABLE(hi2c);
03252 
03253           return HAL_ERROR;
03254         }
03255 
03256         /* Clear ADDR flag */
03257         __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
03258 
03259         /* Process Unlocked */
03260         __HAL_UNLOCK(hi2c);
03261 
03262         /* Note : The I2C interrupts must be enabled after unlocking current process
03263         to avoid the risk of I2C interrupt handle execution before current
03264         process unlock */
03265         /* Enable ERR interrupt */
03266         __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_ERR);
03267 
03268         /* Enable DMA Request */
03269         SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
03270 
03271         return HAL_OK;
03272       }
03273       else
03274       {
03275         /* Update I2C state */
03276         hi2c->State     = HAL_I2C_STATE_READY;
03277         hi2c->Mode      = HAL_I2C_MODE_NONE;
03278 
03279         /* Update I2C error code */
03280         hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
03281 
03282         /* Process Unlocked */
03283         __HAL_UNLOCK(hi2c);
03284 
03285         return HAL_ERROR;
03286       }
03287     }
03288     else
03289     {
03290       /* Update I2C state */
03291       hi2c->State     = HAL_I2C_STATE_READY;
03292       hi2c->Mode      = HAL_I2C_MODE_NONE;
03293 
03294       /* Update I2C error code */
03295       hi2c->ErrorCode |= HAL_I2C_ERROR_SIZE;
03296 
03297       /* Process Unlocked */
03298       __HAL_UNLOCK(hi2c);
03299 
03300       return HAL_ERROR;
03301     }
03302   }
03303   else
03304   {
03305     return HAL_BUSY;
03306   }
03307 }
03308 
03309 /**
03310   * @brief  Reads an amount of data in non-blocking mode with DMA from a specific memory address.
03311   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
03312   *                the configuration information for the specified I2C.
03313   * @param  DevAddress Target device address: The device 7 bits address value
03314   *         in datasheet must be shifted to the left before calling the interface
03315   * @param  MemAddress Internal memory address
03316   * @param  MemAddSize Size of internal memory address
03317   * @param  pData Pointer to data buffer
03318   * @param  Size Amount of data to be read
03319   * @retval HAL status
03320   */
03321 HAL_StatusTypeDef HAL_I2C_Mem_Read_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
03322 {
03323   /* Init tickstart for timeout management*/
03324   uint32_t tickstart = HAL_GetTick();
03325   __IO uint32_t count = 0U;
03326   HAL_StatusTypeDef dmaxferstatus;
03327 
03328   /* Check the parameters */
03329   assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
03330 
03331   if (hi2c->State == HAL_I2C_STATE_READY)
03332   {
03333     /* Wait until BUSY flag is reset */
03334     count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
03335     do
03336     {
03337       count--;
03338       if (count == 0U)
03339       {
03340         hi2c->PreviousState       = I2C_STATE_NONE;
03341         hi2c->State               = HAL_I2C_STATE_READY;
03342         hi2c->Mode                = HAL_I2C_MODE_NONE;
03343         hi2c->ErrorCode           |= HAL_I2C_ERROR_TIMEOUT;
03344 
03345         /* Process Unlocked */
03346         __HAL_UNLOCK(hi2c);
03347 
03348         return HAL_ERROR;
03349       }
03350     }
03351     while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
03352 
03353     /* Process Locked */
03354     __HAL_LOCK(hi2c);
03355 
03356     /* Check if the I2C is already enabled */
03357     if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
03358     {
03359       /* Enable I2C peripheral */
03360       __HAL_I2C_ENABLE(hi2c);
03361     }
03362 
03363     /* Disable Pos */
03364     CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
03365 
03366     hi2c->State     = HAL_I2C_STATE_BUSY_RX;
03367     hi2c->Mode      = HAL_I2C_MODE_MEM;
03368     hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
03369 
03370     /* Prepare transfer parameters */
03371     hi2c->pBuffPtr    = pData;
03372     hi2c->XferCount   = Size;
03373     hi2c->XferSize    = hi2c->XferCount;
03374     hi2c->XferOptions = I2C_NO_OPTION_FRAME;
03375 
03376     if (hi2c->XferSize > 0U)
03377     {
03378       if (hi2c->hdmarx != NULL)
03379       {
03380         /* Set the I2C DMA transfer complete callback */
03381         hi2c->hdmarx->XferCpltCallback = I2C_DMAXferCplt;
03382 
03383         /* Set the DMA error callback */
03384         hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
03385 
03386         /* Set the unused DMA callbacks to NULL */
03387         hi2c->hdmarx->XferHalfCpltCallback = NULL;
03388         hi2c->hdmarx->XferAbortCallback = NULL;
03389 
03390         /* Enable the DMA channel */
03391         dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);
03392       }
03393       else
03394       {
03395         /* Update I2C state */
03396         hi2c->State     = HAL_I2C_STATE_READY;
03397         hi2c->Mode      = HAL_I2C_MODE_NONE;
03398 
03399         /* Update I2C error code */
03400         hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
03401 
03402         /* Process Unlocked */
03403         __HAL_UNLOCK(hi2c);
03404 
03405         return HAL_ERROR;
03406       }
03407 
03408       if (dmaxferstatus == HAL_OK)
03409       {
03410         /* Send Slave Address and Memory Address */
03411         if (I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
03412         {
03413           /* Abort the ongoing DMA */
03414           dmaxferstatus = HAL_DMA_Abort_IT(hi2c->hdmarx);
03415 
03416           /* Prevent unused argument(s) compilation and MISRA warning */
03417           UNUSED(dmaxferstatus);
03418 
03419           /* Set the unused I2C DMA transfer complete callback to NULL */
03420           hi2c->hdmarx->XferCpltCallback = NULL;
03421 
03422           /* Disable Acknowledge */
03423           CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
03424 
03425           hi2c->XferSize = 0U;
03426           hi2c->XferCount = 0U;
03427 
03428           /* Disable I2C peripheral to prevent dummy data in buffer */
03429           __HAL_I2C_DISABLE(hi2c);
03430 
03431           return HAL_ERROR;
03432         }
03433 
03434         if (hi2c->XferSize == 1U)
03435         {
03436           /* Disable Acknowledge */
03437           CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
03438         }
03439         else
03440         {
03441           /* Enable Last DMA bit */
03442           SET_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
03443         }
03444 
03445         /* Clear ADDR flag */
03446         __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
03447 
03448         /* Process Unlocked */
03449         __HAL_UNLOCK(hi2c);
03450 
03451         /* Note : The I2C interrupts must be enabled after unlocking current process
03452         to avoid the risk of I2C interrupt handle execution before current
03453         process unlock */
03454         /* Enable ERR interrupt */
03455         __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_ERR);
03456 
03457         /* Enable DMA Request */
03458         hi2c->Instance->CR2 |= I2C_CR2_DMAEN;
03459       }
03460       else
03461       {
03462         /* Update I2C state */
03463         hi2c->State     = HAL_I2C_STATE_READY;
03464         hi2c->Mode      = HAL_I2C_MODE_NONE;
03465 
03466         /* Update I2C error code */
03467         hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
03468 
03469         /* Process Unlocked */
03470         __HAL_UNLOCK(hi2c);
03471 
03472         return HAL_ERROR;
03473       }
03474     }
03475     else
03476     {
03477       /* Send Slave Address and Memory Address */
03478       if (I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
03479       {
03480         return HAL_ERROR;
03481       }
03482 
03483       /* Clear ADDR flag */
03484       __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
03485 
03486       /* Generate Stop */
03487       SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
03488 
03489       hi2c->State = HAL_I2C_STATE_READY;
03490 
03491       /* Process Unlocked */
03492       __HAL_UNLOCK(hi2c);
03493     }
03494 
03495     return HAL_OK;
03496   }
03497   else
03498   {
03499     return HAL_BUSY;
03500   }
03501 }
03502 
03503 /**
03504   * @brief  Checks if target device is ready for communication.
03505   * @note   This function is used with Memory devices
03506   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
03507   *                the configuration information for the specified I2C.
03508   * @param  DevAddress Target device address: The device 7 bits address value
03509   *         in datasheet must be shifted to the left before calling the interface
03510   * @param  Trials Number of trials
03511   * @param  Timeout Timeout duration
03512   * @retval HAL status
03513   */
03514 HAL_StatusTypeDef HAL_I2C_IsDeviceReady(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout)
03515 {
03516   /* Get tick */
03517   uint32_t tickstart = HAL_GetTick();
03518   uint32_t I2C_Trials = 1U;
03519   FlagStatus tmp1;
03520   FlagStatus tmp2;
03521 
03522   if (hi2c->State == HAL_I2C_STATE_READY)
03523   {
03524     /* Wait until BUSY flag is reset */
03525     if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
03526     {
03527       return HAL_BUSY;
03528     }
03529 
03530     /* Process Locked */
03531     __HAL_LOCK(hi2c);
03532 
03533     /* Check if the I2C is already enabled */
03534     if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
03535     {
03536       /* Enable I2C peripheral */
03537       __HAL_I2C_ENABLE(hi2c);
03538     }
03539 
03540     /* Disable Pos */
03541     CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
03542 
03543     hi2c->State = HAL_I2C_STATE_BUSY;
03544     hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
03545     hi2c->XferOptions = I2C_NO_OPTION_FRAME;
03546 
03547     do
03548     {
03549       /* Generate Start */
03550       SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
03551 
03552       /* Wait until SB flag is set */
03553       if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, tickstart) != HAL_OK)
03554       {
03555         if (READ_BIT(hi2c->Instance->CR1, I2C_CR1_START) == I2C_CR1_START)
03556         {
03557           hi2c->ErrorCode = HAL_I2C_WRONG_START;
03558         }
03559         return HAL_TIMEOUT;
03560       }
03561 
03562       /* Send slave address */
03563       hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress);
03564 
03565       /* Wait until ADDR or AF flag are set */
03566       /* Get tick */
03567       tickstart = HAL_GetTick();
03568 
03569       tmp1 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR);
03570       tmp2 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF);
03571       while ((hi2c->State != HAL_I2C_STATE_TIMEOUT) && (tmp1 == RESET) && (tmp2 == RESET))
03572       {
03573         if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
03574         {
03575           hi2c->State = HAL_I2C_STATE_TIMEOUT;
03576         }
03577         tmp1 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR);
03578         tmp2 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF);
03579       }
03580 
03581       hi2c->State = HAL_I2C_STATE_READY;
03582 
03583       /* Check if the ADDR flag has been set */
03584       if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == SET)
03585       {
03586         /* Generate Stop */
03587         SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
03588 
03589         /* Clear ADDR Flag */
03590         __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
03591 
03592         /* Wait until BUSY flag is reset */
03593         if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
03594         {
03595           return HAL_ERROR;
03596         }
03597 
03598         hi2c->State = HAL_I2C_STATE_READY;
03599 
03600         /* Process Unlocked */
03601         __HAL_UNLOCK(hi2c);
03602 
03603         return HAL_OK;
03604       }
03605       else
03606       {
03607         /* Generate Stop */
03608         SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
03609 
03610         /* Clear AF Flag */
03611         __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
03612 
03613         /* Wait until BUSY flag is reset */
03614         if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
03615         {
03616           return HAL_ERROR;
03617         }
03618       }
03619 
03620       /* Increment Trials */
03621       I2C_Trials++;
03622     }
03623     while (I2C_Trials < Trials);
03624 
03625     hi2c->State = HAL_I2C_STATE_READY;
03626 
03627     /* Process Unlocked */
03628     __HAL_UNLOCK(hi2c);
03629 
03630     return HAL_ERROR;
03631   }
03632   else
03633   {
03634     return HAL_BUSY;
03635   }
03636 }
03637 
03638 /**
03639   * @brief  Sequential transmit in master I2C mode an amount of data in non-blocking mode with Interrupt.
03640   * @note   This interface allow to manage repeated start condition when a direction change during transfer
03641   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
03642   *         the configuration information for the specified I2C.
03643   * @param  DevAddress Target device address: The device 7 bits address value
03644   *         in datasheet must be shifted to the left before calling the interface
03645   * @param  pData Pointer to data buffer
03646   * @param  Size Amount of data to be sent
03647   * @param  XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition
03648   * @retval HAL status
03649   */
03650 HAL_StatusTypeDef HAL_I2C_Master_Seq_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
03651 {
03652   __IO uint32_t Prev_State = 0x00U;
03653   __IO uint32_t count      = 0x00U;
03654 
03655   /* Check the parameters */
03656   assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
03657 
03658   if (hi2c->State == HAL_I2C_STATE_READY)
03659   {
03660     /* Check Busy Flag only if FIRST call of Master interface */
03661     if ((READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP) || (XferOptions == I2C_FIRST_AND_LAST_FRAME) || (XferOptions == I2C_FIRST_FRAME))
03662     {
03663       /* Wait until BUSY flag is reset */
03664       count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
03665       do
03666       {
03667         count--;
03668         if (count == 0U)
03669         {
03670           hi2c->PreviousState       = I2C_STATE_NONE;
03671           hi2c->State               = HAL_I2C_STATE_READY;
03672           hi2c->Mode                = HAL_I2C_MODE_NONE;
03673           hi2c->ErrorCode           |= HAL_I2C_ERROR_TIMEOUT;
03674 
03675           /* Process Unlocked */
03676           __HAL_UNLOCK(hi2c);
03677 
03678           return HAL_ERROR;
03679         }
03680       }
03681       while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
03682     }
03683 
03684     /* Process Locked */
03685     __HAL_LOCK(hi2c);
03686 
03687     /* Check if the I2C is already enabled */
03688     if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
03689     {
03690       /* Enable I2C peripheral */
03691       __HAL_I2C_ENABLE(hi2c);
03692     }
03693 
03694     /* Disable Pos */
03695     CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
03696 
03697     hi2c->State     = HAL_I2C_STATE_BUSY_TX;
03698     hi2c->Mode      = HAL_I2C_MODE_MASTER;
03699     hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
03700 
03701     /* Prepare transfer parameters */
03702     hi2c->pBuffPtr    = pData;
03703     hi2c->XferCount   = Size;
03704     hi2c->XferSize    = hi2c->XferCount;
03705     hi2c->XferOptions = XferOptions;
03706     hi2c->Devaddress  = DevAddress;
03707 
03708     Prev_State = hi2c->PreviousState;
03709 
03710     /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */
03711     /* Mean Previous state is same as current state */
03712     if ((Prev_State != I2C_STATE_MASTER_BUSY_TX) || (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 1))
03713     {
03714       /* Generate Start */
03715       SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
03716     }
03717 
03718     /* Process Unlocked */
03719     __HAL_UNLOCK(hi2c);
03720 
03721     /* Note : The I2C interrupts must be enabled after unlocking current process
03722     to avoid the risk of I2C interrupt handle execution before current
03723     process unlock */
03724 
03725     /* Enable EVT, BUF and ERR interrupt */
03726     __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
03727 
03728     return HAL_OK;
03729   }
03730   else
03731   {
03732     return HAL_BUSY;
03733   }
03734 }
03735 
03736 /**
03737   * @brief  Sequential transmit in master I2C mode an amount of data in non-blocking mode with DMA.
03738   * @note   This interface allow to manage repeated start condition when a direction change during transfer
03739   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
03740   *         the configuration information for the specified I2C.
03741   * @param  DevAddress Target device address: The device 7 bits address value
03742   *         in datasheet must be shifted to the left before calling the interface
03743   * @param  pData Pointer to data buffer
03744   * @param  Size Amount of data to be sent
03745   * @param  XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition
03746   * @retval HAL status
03747   */
03748 HAL_StatusTypeDef HAL_I2C_Master_Seq_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
03749 {
03750   __IO uint32_t Prev_State = 0x00U;
03751   __IO uint32_t count      = 0x00U;
03752   HAL_StatusTypeDef dmaxferstatus;
03753 
03754   /* Check the parameters */
03755   assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
03756 
03757   if (hi2c->State == HAL_I2C_STATE_READY)
03758   {
03759     /* Check Busy Flag only if FIRST call of Master interface */
03760     if ((READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP) || (XferOptions == I2C_FIRST_AND_LAST_FRAME) || (XferOptions == I2C_FIRST_FRAME))
03761     {
03762       /* Wait until BUSY flag is reset */
03763       count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
03764       do
03765       {
03766         count--;
03767         if (count == 0U)
03768         {
03769           hi2c->PreviousState       = I2C_STATE_NONE;
03770           hi2c->State               = HAL_I2C_STATE_READY;
03771           hi2c->Mode                = HAL_I2C_MODE_NONE;
03772           hi2c->ErrorCode           |= HAL_I2C_ERROR_TIMEOUT;
03773 
03774           /* Process Unlocked */
03775           __HAL_UNLOCK(hi2c);
03776 
03777           return HAL_ERROR;
03778         }
03779       }
03780       while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
03781     }
03782 
03783     /* Process Locked */
03784     __HAL_LOCK(hi2c);
03785 
03786     /* Check if the I2C is already enabled */
03787     if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
03788     {
03789       /* Enable I2C peripheral */
03790       __HAL_I2C_ENABLE(hi2c);
03791     }
03792 
03793     /* Disable Pos */
03794     CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
03795 
03796     hi2c->State     = HAL_I2C_STATE_BUSY_TX;
03797     hi2c->Mode      = HAL_I2C_MODE_MASTER;
03798     hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
03799 
03800     /* Prepare transfer parameters */
03801     hi2c->pBuffPtr    = pData;
03802     hi2c->XferCount   = Size;
03803     hi2c->XferSize    = hi2c->XferCount;
03804     hi2c->XferOptions = XferOptions;
03805     hi2c->Devaddress  = DevAddress;
03806 
03807     Prev_State = hi2c->PreviousState;
03808 
03809     if (hi2c->XferSize > 0U)
03810     {
03811       if (hi2c->hdmatx != NULL)
03812       {
03813         /* Set the I2C DMA transfer complete callback */
03814         hi2c->hdmatx->XferCpltCallback = I2C_DMAXferCplt;
03815 
03816         /* Set the DMA error callback */
03817         hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
03818 
03819         /* Set the unused DMA callbacks to NULL */
03820         hi2c->hdmatx->XferHalfCpltCallback = NULL;
03821         hi2c->hdmatx->XferAbortCallback = NULL;
03822 
03823         /* Enable the DMA channel */
03824         dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->DR, hi2c->XferSize);
03825       }
03826       else
03827       {
03828         /* Update I2C state */
03829         hi2c->State     = HAL_I2C_STATE_READY;
03830         hi2c->Mode      = HAL_I2C_MODE_NONE;
03831 
03832         /* Update I2C error code */
03833         hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
03834 
03835         /* Process Unlocked */
03836         __HAL_UNLOCK(hi2c);
03837 
03838         return HAL_ERROR;
03839       }
03840 
03841       if (dmaxferstatus == HAL_OK)
03842       {
03843         /* Enable Acknowledge */
03844         SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
03845 
03846         /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */
03847         /* Mean Previous state is same as current state */
03848         if ((Prev_State != I2C_STATE_MASTER_BUSY_TX) || (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 1))
03849         {
03850           /* Generate Start */
03851           SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
03852         }
03853 
03854         /* Process Unlocked */
03855         __HAL_UNLOCK(hi2c);
03856 
03857         /* Note : The I2C interrupts must be enabled after unlocking current process
03858         to avoid the risk of I2C interrupt handle execution before current
03859         process unlock */
03860 
03861         /* If XferOptions is not associated to a new frame, mean no start bit is request, enable directly the DMA request */
03862         /* In other cases, DMA request is enabled after Slave address treatment in IRQHandler */
03863         if ((XferOptions == I2C_NEXT_FRAME) || (XferOptions == I2C_LAST_FRAME) || (XferOptions == I2C_LAST_FRAME_NO_STOP))
03864         {
03865           /* Enable DMA Request */
03866           SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
03867         }
03868 
03869         /* Enable EVT and ERR interrupt */
03870         __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
03871       }
03872       else
03873       {
03874         /* Update I2C state */
03875         hi2c->State     = HAL_I2C_STATE_READY;
03876         hi2c->Mode      = HAL_I2C_MODE_NONE;
03877 
03878         /* Update I2C error code */
03879         hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
03880 
03881         /* Process Unlocked */
03882         __HAL_UNLOCK(hi2c);
03883 
03884         return HAL_ERROR;
03885       }
03886     }
03887     else
03888     {
03889       /* Enable Acknowledge */
03890       SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
03891 
03892       /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */
03893       /* Mean Previous state is same as current state */
03894       if ((Prev_State != I2C_STATE_MASTER_BUSY_TX) || (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 1))
03895       {
03896         /* Generate Start */
03897         SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
03898       }
03899 
03900       /* Process Unlocked */
03901       __HAL_UNLOCK(hi2c);
03902 
03903       /* Note : The I2C interrupts must be enabled after unlocking current process
03904       to avoid the risk of I2C interrupt handle execution before current
03905       process unlock */
03906 
03907       /* Enable EVT, BUF and ERR interrupt */
03908       __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
03909     }
03910 
03911     return HAL_OK;
03912   }
03913   else
03914   {
03915     return HAL_BUSY;
03916   }
03917 }
03918 
03919 /**
03920   * @brief  Sequential receive in master I2C mode an amount of data in non-blocking mode with Interrupt
03921   * @note   This interface allow to manage repeated start condition when a direction change during transfer
03922   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
03923   *         the configuration information for the specified I2C.
03924   * @param  DevAddress Target device address: The device 7 bits address value
03925   *         in datasheet must be shifted to the left before calling the interface
03926   * @param  pData Pointer to data buffer
03927   * @param  Size Amount of data to be sent
03928   * @param  XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition
03929   * @retval HAL status
03930   */
03931 HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
03932 {
03933   __IO uint32_t Prev_State = 0x00U;
03934   __IO uint32_t count = 0U;
03935   uint32_t enableIT = (I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
03936 
03937   /* Check the parameters */
03938   assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
03939 
03940   if (hi2c->State == HAL_I2C_STATE_READY)
03941   {
03942     /* Check Busy Flag only if FIRST call of Master interface */
03943     if ((READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP) || (XferOptions == I2C_FIRST_AND_LAST_FRAME) || (XferOptions == I2C_FIRST_FRAME))
03944     {
03945       /* Wait until BUSY flag is reset */
03946       count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
03947       do
03948       {
03949         count--;
03950         if (count == 0U)
03951         {
03952           hi2c->PreviousState       = I2C_STATE_NONE;
03953           hi2c->State               = HAL_I2C_STATE_READY;
03954           hi2c->Mode                = HAL_I2C_MODE_NONE;
03955           hi2c->ErrorCode           |= HAL_I2C_ERROR_TIMEOUT;
03956 
03957           /* Process Unlocked */
03958           __HAL_UNLOCK(hi2c);
03959 
03960           return HAL_ERROR;
03961         }
03962       }
03963       while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
03964     }
03965 
03966     /* Process Locked */
03967     __HAL_LOCK(hi2c);
03968 
03969     /* Check if the I2C is already enabled */
03970     if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
03971     {
03972       /* Enable I2C peripheral */
03973       __HAL_I2C_ENABLE(hi2c);
03974     }
03975 
03976     /* Disable Pos */
03977     CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
03978 
03979     hi2c->State     = HAL_I2C_STATE_BUSY_RX;
03980     hi2c->Mode      = HAL_I2C_MODE_MASTER;
03981     hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
03982 
03983     /* Prepare transfer parameters */
03984     hi2c->pBuffPtr    = pData;
03985     hi2c->XferCount   = Size;
03986     hi2c->XferSize    = hi2c->XferCount;
03987     hi2c->XferOptions = XferOptions;
03988     hi2c->Devaddress  = DevAddress;
03989 
03990     Prev_State = hi2c->PreviousState;
03991 
03992     if ((hi2c->XferCount == 2U) && ((XferOptions == I2C_LAST_FRAME) || (XferOptions == I2C_LAST_FRAME_NO_STOP)))
03993     {
03994       if (Prev_State == I2C_STATE_MASTER_BUSY_RX)
03995       {
03996         /* Disable Acknowledge */
03997         CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
03998 
03999         /* Enable Pos */
04000         SET_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
04001 
04002         /* Remove Enabling of IT_BUF, mean RXNE treatment, treat the 2 bytes through BTF */
04003         enableIT &= ~I2C_IT_BUF;
04004       }
04005       else
04006       {
04007         /* Enable Acknowledge */
04008         SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
04009       }
04010     }
04011     else
04012     {
04013       /* Enable Acknowledge */
04014       SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
04015     }
04016 
04017     /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */
04018     /* Mean Previous state is same as current state */
04019     if ((Prev_State != I2C_STATE_MASTER_BUSY_RX) || (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 1))
04020     {
04021       /* Generate Start */
04022       SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
04023     }
04024 
04025     /* Process Unlocked */
04026     __HAL_UNLOCK(hi2c);
04027 
04028     /* Note : The I2C interrupts must be enabled after unlocking current process
04029     to avoid the risk of I2C interrupt handle execution before current
04030     process unlock */
04031 
04032     /* Enable interrupts */
04033     __HAL_I2C_ENABLE_IT(hi2c, enableIT);
04034 
04035     return HAL_OK;
04036   }
04037   else
04038   {
04039     return HAL_BUSY;
04040   }
04041 }
04042 
04043 /**
04044   * @brief  Sequential receive in master mode an amount of data in non-blocking mode with DMA
04045   * @note   This interface allow to manage repeated start condition when a direction change during transfer
04046   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
04047   *         the configuration information for the specified I2C.
04048   * @param  DevAddress Target device address: The device 7 bits address value
04049   *         in datasheet must be shifted to the left before calling the interface
04050   * @param  pData Pointer to data buffer
04051   * @param  Size Amount of data to be sent
04052   * @param  XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition
04053   * @retval HAL status
04054   */
04055 HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
04056 {
04057   __IO uint32_t Prev_State = 0x00U;
04058   __IO uint32_t count = 0U;
04059   uint32_t enableIT = (I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
04060   HAL_StatusTypeDef dmaxferstatus;
04061 
04062   /* Check the parameters */
04063   assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
04064 
04065   if (hi2c->State == HAL_I2C_STATE_READY)
04066   {
04067     /* Check Busy Flag only if FIRST call of Master interface */
04068     if ((READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP) || (XferOptions == I2C_FIRST_AND_LAST_FRAME) || (XferOptions == I2C_FIRST_FRAME))
04069     {
04070       /* Wait until BUSY flag is reset */
04071       count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
04072       do
04073       {
04074         count--;
04075         if (count == 0U)
04076         {
04077           hi2c->PreviousState       = I2C_STATE_NONE;
04078           hi2c->State               = HAL_I2C_STATE_READY;
04079           hi2c->Mode                = HAL_I2C_MODE_NONE;
04080           hi2c->ErrorCode           |= HAL_I2C_ERROR_TIMEOUT;
04081 
04082           /* Process Unlocked */
04083           __HAL_UNLOCK(hi2c);
04084 
04085           return HAL_ERROR;
04086         }
04087       }
04088       while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
04089     }
04090 
04091     /* Process Locked */
04092     __HAL_LOCK(hi2c);
04093 
04094     /* Check if the I2C is already enabled */
04095     if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
04096     {
04097       /* Enable I2C peripheral */
04098       __HAL_I2C_ENABLE(hi2c);
04099     }
04100 
04101     /* Disable Pos */
04102     CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
04103 
04104     /* Clear Last DMA bit */
04105     CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
04106 
04107     hi2c->State     = HAL_I2C_STATE_BUSY_RX;
04108     hi2c->Mode      = HAL_I2C_MODE_MASTER;
04109     hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
04110 
04111     /* Prepare transfer parameters */
04112     hi2c->pBuffPtr    = pData;
04113     hi2c->XferCount   = Size;
04114     hi2c->XferSize    = hi2c->XferCount;
04115     hi2c->XferOptions = XferOptions;
04116     hi2c->Devaddress  = DevAddress;
04117 
04118     Prev_State = hi2c->PreviousState;
04119 
04120     if (hi2c->XferSize > 0U)
04121     {
04122       if ((hi2c->XferCount == 2U) && ((XferOptions == I2C_LAST_FRAME) || (XferOptions == I2C_LAST_FRAME_NO_STOP)))
04123       {
04124         if (Prev_State == I2C_STATE_MASTER_BUSY_RX)
04125         {
04126           /* Disable Acknowledge */
04127           CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
04128 
04129           /* Enable Pos */
04130           SET_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
04131 
04132           /* Enable Last DMA bit */
04133           SET_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
04134         }
04135         else
04136         {
04137           /* Enable Acknowledge */
04138           SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
04139         }
04140       }
04141       else
04142       {
04143         /* Enable Acknowledge */
04144         SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
04145 
04146         if ((XferOptions == I2C_LAST_FRAME) || (XferOptions == I2C_OTHER_AND_LAST_FRAME) || (XferOptions == I2C_LAST_FRAME_NO_STOP))
04147         {
04148           /* Enable Last DMA bit */
04149           SET_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
04150         }
04151       }
04152       if (hi2c->hdmarx != NULL)
04153       {
04154         /* Set the I2C DMA transfer complete callback */
04155         hi2c->hdmarx->XferCpltCallback = I2C_DMAXferCplt;
04156 
04157         /* Set the DMA error callback */
04158         hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
04159 
04160         /* Set the unused DMA callbacks to NULL */
04161         hi2c->hdmarx->XferHalfCpltCallback = NULL;
04162         hi2c->hdmarx->XferAbortCallback = NULL;
04163 
04164         /* Enable the DMA channel */
04165         dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);
04166       }
04167       else
04168       {
04169         /* Update I2C state */
04170         hi2c->State     = HAL_I2C_STATE_READY;
04171         hi2c->Mode      = HAL_I2C_MODE_NONE;
04172 
04173         /* Update I2C error code */
04174         hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
04175 
04176         /* Process Unlocked */
04177         __HAL_UNLOCK(hi2c);
04178 
04179         return HAL_ERROR;
04180       }
04181       if (dmaxferstatus == HAL_OK)
04182       {
04183         /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */
04184         /* Mean Previous state is same as current state */
04185         if ((Prev_State != I2C_STATE_MASTER_BUSY_RX) || (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 1))
04186         {
04187           /* Generate Start */
04188           SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
04189 
04190           /* Update interrupt for only EVT and ERR */
04191           enableIT = (I2C_IT_EVT | I2C_IT_ERR);
04192         }
04193         else
04194         {
04195           /* Update interrupt for only ERR */
04196           enableIT = I2C_IT_ERR;
04197         }
04198 
04199         /* Process Unlocked */
04200         __HAL_UNLOCK(hi2c);
04201 
04202         /* Note : The I2C interrupts must be enabled after unlocking current process
04203         to avoid the risk of I2C interrupt handle execution before current
04204         process unlock */
04205 
04206         /* If XferOptions is not associated to a new frame, mean no start bit is request, enable directly the DMA request */
04207         /* In other cases, DMA request is enabled after Slave address treatment in IRQHandler */
04208         if ((XferOptions == I2C_NEXT_FRAME) || (XferOptions == I2C_LAST_FRAME) || (XferOptions == I2C_LAST_FRAME_NO_STOP))
04209         {
04210           /* Enable DMA Request */
04211           SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
04212         }
04213 
04214         /* Enable EVT and ERR interrupt */
04215         __HAL_I2C_ENABLE_IT(hi2c, enableIT);
04216       }
04217       else
04218       {
04219         /* Update I2C state */
04220         hi2c->State     = HAL_I2C_STATE_READY;
04221         hi2c->Mode      = HAL_I2C_MODE_NONE;
04222 
04223         /* Update I2C error code */
04224         hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
04225 
04226         /* Process Unlocked */
04227         __HAL_UNLOCK(hi2c);
04228 
04229         return HAL_ERROR;
04230       }
04231     }
04232     else
04233     {
04234       /* Enable Acknowledge */
04235       SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
04236 
04237       /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */
04238       /* Mean Previous state is same as current state */
04239       if ((Prev_State != I2C_STATE_MASTER_BUSY_RX) || (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 1))
04240       {
04241         /* Generate Start */
04242         SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
04243       }
04244 
04245       /* Process Unlocked */
04246       __HAL_UNLOCK(hi2c);
04247 
04248       /* Note : The I2C interrupts must be enabled after unlocking current process
04249       to avoid the risk of I2C interrupt handle execution before current
04250       process unlock */
04251 
04252       /* Enable interrupts */
04253       __HAL_I2C_ENABLE_IT(hi2c, enableIT);
04254     }
04255     return HAL_OK;
04256   }
04257   else
04258   {
04259     return HAL_BUSY;
04260   }
04261 }
04262 
04263 /**
04264   * @brief  Sequential transmit in slave mode an amount of data in non-blocking mode with Interrupt
04265   * @note   This interface allow to manage repeated start condition when a direction change during transfer
04266   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
04267   *         the configuration information for the specified I2C.
04268   * @param  pData Pointer to data buffer
04269   * @param  Size Amount of data to be sent
04270   * @param  XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition
04271   * @retval HAL status
04272   */
04273 HAL_StatusTypeDef HAL_I2C_Slave_Seq_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
04274 {
04275   /* Check the parameters */
04276   assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
04277 
04278   if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
04279   {
04280     if ((pData == NULL) || (Size == 0U))
04281     {
04282       return  HAL_ERROR;
04283     }
04284 
04285     /* Process Locked */
04286     __HAL_LOCK(hi2c);
04287 
04288     /* Check if the I2C is already enabled */
04289     if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
04290     {
04291       /* Enable I2C peripheral */
04292       __HAL_I2C_ENABLE(hi2c);
04293     }
04294 
04295     /* Disable Pos */
04296     CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
04297 
04298     hi2c->State     = HAL_I2C_STATE_BUSY_TX_LISTEN;
04299     hi2c->Mode      = HAL_I2C_MODE_SLAVE;
04300     hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
04301 
04302     /* Prepare transfer parameters */
04303     hi2c->pBuffPtr    = pData;
04304     hi2c->XferCount   = Size;
04305     hi2c->XferSize    = hi2c->XferCount;
04306     hi2c->XferOptions = XferOptions;
04307 
04308     /* Clear ADDR flag */
04309     __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
04310 
04311     /* Process Unlocked */
04312     __HAL_UNLOCK(hi2c);
04313 
04314     /* Note : The I2C interrupts must be enabled after unlocking current process
04315               to avoid the risk of I2C interrupt handle execution before current
04316               process unlock */
04317 
04318     /* Enable EVT, BUF and ERR interrupt */
04319     __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
04320 
04321     return HAL_OK;
04322   }
04323   else
04324   {
04325     return HAL_BUSY;
04326   }
04327 }
04328 
04329 /**
04330   * @brief  Sequential transmit in slave mode an amount of data in non-blocking mode with DMA
04331   * @note   This interface allow to manage repeated start condition when a direction change during transfer
04332   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
04333   *         the configuration information for the specified I2C.
04334   * @param  pData Pointer to data buffer
04335   * @param  Size Amount of data to be sent
04336   * @param  XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition
04337   * @retval HAL status
04338   */
04339 HAL_StatusTypeDef HAL_I2C_Slave_Seq_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
04340 {
04341   HAL_StatusTypeDef dmaxferstatus;
04342 
04343   /* Check the parameters */
04344   assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
04345 
04346   if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
04347   {
04348     if ((pData == NULL) || (Size == 0U))
04349     {
04350       return  HAL_ERROR;
04351     }
04352 
04353     /* Process Locked */
04354     __HAL_LOCK(hi2c);
04355 
04356     /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
04357     __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
04358 
04359     /* I2C cannot manage full duplex exchange so disable previous IT enabled if any */
04360     /* and then toggle the HAL slave RX state to TX state */
04361     if (hi2c->State == HAL_I2C_STATE_BUSY_RX_LISTEN)
04362     {
04363       if ((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN)
04364       {
04365         /* Abort DMA Xfer if any */
04366         if (hi2c->hdmarx != NULL)
04367         {
04368           CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
04369 
04370           /* Set the I2C DMA Abort callback :
04371            will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
04372           hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort;
04373 
04374           /* Abort DMA RX */
04375           if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK)
04376           {
04377             /* Call Directly XferAbortCallback function in case of error */
04378             hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx);
04379           }
04380         }
04381       }
04382     }
04383     else if (hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN)
04384     {
04385       if ((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN)
04386       {
04387         CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
04388 
04389         /* Abort DMA Xfer if any */
04390         if (hi2c->hdmatx != NULL)
04391         {
04392           /* Set the I2C DMA Abort callback :
04393            will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
04394           hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort;
04395 
04396           /* Abort DMA TX */
04397           if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK)
04398           {
04399             /* Call Directly XferAbortCallback function in case of error */
04400             hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx);
04401           }
04402         }
04403       }
04404     }
04405     else
04406     {
04407       /* Nothing to do */
04408     }
04409 
04410     /* Check if the I2C is already enabled */
04411     if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
04412     {
04413       /* Enable I2C peripheral */
04414       __HAL_I2C_ENABLE(hi2c);
04415     }
04416 
04417     /* Disable Pos */
04418     CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
04419 
04420     hi2c->State     = HAL_I2C_STATE_BUSY_TX_LISTEN;
04421     hi2c->Mode      = HAL_I2C_MODE_SLAVE;
04422     hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
04423 
04424     /* Prepare transfer parameters */
04425     hi2c->pBuffPtr    = pData;
04426     hi2c->XferCount   = Size;
04427     hi2c->XferSize    = hi2c->XferCount;
04428     hi2c->XferOptions = XferOptions;
04429 
04430     if (hi2c->hdmatx != NULL)
04431     {
04432       /* Set the I2C DMA transfer complete callback */
04433       hi2c->hdmatx->XferCpltCallback = I2C_DMAXferCplt;
04434 
04435       /* Set the DMA error callback */
04436       hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
04437 
04438       /* Set the unused DMA callbacks to NULL */
04439       hi2c->hdmatx->XferHalfCpltCallback = NULL;
04440       hi2c->hdmatx->XferAbortCallback = NULL;
04441 
04442       /* Enable the DMA channel */
04443       dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->DR, hi2c->XferSize);
04444     }
04445     else
04446     {
04447       /* Update I2C state */
04448       hi2c->State     = HAL_I2C_STATE_LISTEN;
04449       hi2c->Mode      = HAL_I2C_MODE_NONE;
04450 
04451       /* Update I2C error code */
04452       hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
04453 
04454       /* Process Unlocked */
04455       __HAL_UNLOCK(hi2c);
04456 
04457       return HAL_ERROR;
04458     }
04459 
04460     if (dmaxferstatus == HAL_OK)
04461     {
04462       /* Enable Address Acknowledge */
04463       SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
04464 
04465       /* Clear ADDR flag */
04466       __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
04467 
04468       /* Process Unlocked */
04469       __HAL_UNLOCK(hi2c);
04470 
04471       /* Note : The I2C interrupts must be enabled after unlocking current process
04472       to avoid the risk of I2C interrupt handle execution before current
04473       process unlock */
04474       /* Enable EVT and ERR interrupt */
04475       __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
04476 
04477       /* Enable DMA Request */
04478       hi2c->Instance->CR2 |= I2C_CR2_DMAEN;
04479 
04480       return HAL_OK;
04481     }
04482     else
04483     {
04484       /* Update I2C state */
04485       hi2c->State     = HAL_I2C_STATE_READY;
04486       hi2c->Mode      = HAL_I2C_MODE_NONE;
04487 
04488       /* Update I2C error code */
04489       hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
04490 
04491       /* Process Unlocked */
04492       __HAL_UNLOCK(hi2c);
04493 
04494       return HAL_ERROR;
04495     }
04496   }
04497   else
04498   {
04499     return HAL_BUSY;
04500   }
04501 }
04502 
04503 /**
04504   * @brief  Sequential receive in slave mode an amount of data in non-blocking mode with Interrupt
04505   * @note   This interface allow to manage repeated start condition when a direction change during transfer
04506   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
04507   *         the configuration information for the specified I2C.
04508   * @param  pData Pointer to data buffer
04509   * @param  Size Amount of data to be sent
04510   * @param  XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition
04511   * @retval HAL status
04512   */
04513 HAL_StatusTypeDef HAL_I2C_Slave_Seq_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
04514 {
04515   /* Check the parameters */
04516   assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
04517 
04518   if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
04519   {
04520     if ((pData == NULL) || (Size == 0U))
04521     {
04522       return  HAL_ERROR;
04523     }
04524 
04525     /* Process Locked */
04526     __HAL_LOCK(hi2c);
04527 
04528     /* Check if the I2C is already enabled */
04529     if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
04530     {
04531       /* Enable I2C peripheral */
04532       __HAL_I2C_ENABLE(hi2c);
04533     }
04534 
04535     /* Disable Pos */
04536     CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
04537 
04538     hi2c->State     = HAL_I2C_STATE_BUSY_RX_LISTEN;
04539     hi2c->Mode      = HAL_I2C_MODE_SLAVE;
04540     hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
04541 
04542     /* Prepare transfer parameters */
04543     hi2c->pBuffPtr    = pData;
04544     hi2c->XferCount   = Size;
04545     hi2c->XferSize    = hi2c->XferCount;
04546     hi2c->XferOptions = XferOptions;
04547 
04548     /* Clear ADDR flag */
04549     __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
04550 
04551     /* Process Unlocked */
04552     __HAL_UNLOCK(hi2c);
04553 
04554     /* Note : The I2C interrupts must be enabled after unlocking current process
04555               to avoid the risk of I2C interrupt handle execution before current
04556               process unlock */
04557 
04558     /* Enable EVT, BUF and ERR interrupt */
04559     __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
04560 
04561     return HAL_OK;
04562   }
04563   else
04564   {
04565     return HAL_BUSY;
04566   }
04567 }
04568 
04569 /**
04570   * @brief  Sequential receive in slave mode an amount of data in non-blocking mode with DMA
04571   * @note   This interface allow to manage repeated start condition when a direction change during transfer
04572   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
04573   *         the configuration information for the specified I2C.
04574   * @param  pData Pointer to data buffer
04575   * @param  Size Amount of data to be sent
04576   * @param  XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition
04577   * @retval HAL status
04578   */
04579 HAL_StatusTypeDef HAL_I2C_Slave_Seq_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
04580 {
04581   HAL_StatusTypeDef dmaxferstatus;
04582 
04583   /* Check the parameters */
04584   assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
04585 
04586   if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
04587   {
04588     if ((pData == NULL) || (Size == 0U))
04589     {
04590       return  HAL_ERROR;
04591     }
04592 
04593     /* Process Locked */
04594     __HAL_LOCK(hi2c);
04595 
04596     /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
04597     __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
04598 
04599     /* I2C cannot manage full duplex exchange so disable previous IT enabled if any */
04600     /* and then toggle the HAL slave RX state to TX state */
04601     if (hi2c->State == HAL_I2C_STATE_BUSY_RX_LISTEN)
04602     {
04603       if ((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN)
04604       {
04605         /* Abort DMA Xfer if any */
04606         if (hi2c->hdmarx != NULL)
04607         {
04608           CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
04609 
04610           /* Set the I2C DMA Abort callback :
04611            will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
04612           hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort;
04613 
04614           /* Abort DMA RX */
04615           if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK)
04616           {
04617             /* Call Directly XferAbortCallback function in case of error */
04618             hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx);
04619           }
04620         }
04621       }
04622     }
04623     else if (hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN)
04624     {
04625       if ((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN)
04626       {
04627         CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
04628 
04629         /* Abort DMA Xfer if any */
04630         if (hi2c->hdmatx != NULL)
04631         {
04632           /* Set the I2C DMA Abort callback :
04633            will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
04634           hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort;
04635 
04636           /* Abort DMA TX */
04637           if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK)
04638           {
04639             /* Call Directly XferAbortCallback function in case of error */
04640             hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx);
04641           }
04642         }
04643       }
04644     }
04645     else
04646     {
04647       /* Nothing to do */
04648     }
04649 
04650     /* Check if the I2C is already enabled */
04651     if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
04652     {
04653       /* Enable I2C peripheral */
04654       __HAL_I2C_ENABLE(hi2c);
04655     }
04656 
04657     /* Disable Pos */
04658     CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
04659 
04660     hi2c->State     = HAL_I2C_STATE_BUSY_RX_LISTEN;
04661     hi2c->Mode      = HAL_I2C_MODE_SLAVE;
04662     hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
04663 
04664     /* Prepare transfer parameters */
04665     hi2c->pBuffPtr    = pData;
04666     hi2c->XferCount   = Size;
04667     hi2c->XferSize    = hi2c->XferCount;
04668     hi2c->XferOptions = XferOptions;
04669 
04670     if (hi2c->hdmarx != NULL)
04671     {
04672       /* Set the I2C DMA transfer complete callback */
04673       hi2c->hdmarx->XferCpltCallback = I2C_DMAXferCplt;
04674 
04675       /* Set the DMA error callback */
04676       hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
04677 
04678       /* Set the unused DMA callbacks to NULL */
04679       hi2c->hdmarx->XferHalfCpltCallback = NULL;
04680       hi2c->hdmarx->XferAbortCallback = NULL;
04681 
04682       /* Enable the DMA channel */
04683       dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);
04684     }
04685     else
04686     {
04687       /* Update I2C state */
04688       hi2c->State     = HAL_I2C_STATE_LISTEN;
04689       hi2c->Mode      = HAL_I2C_MODE_NONE;
04690 
04691       /* Update I2C error code */
04692       hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
04693 
04694       /* Process Unlocked */
04695       __HAL_UNLOCK(hi2c);
04696 
04697       return HAL_ERROR;
04698     }
04699 
04700     if (dmaxferstatus == HAL_OK)
04701     {
04702       /* Enable Address Acknowledge */
04703       SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
04704 
04705       /* Clear ADDR flag */
04706       __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
04707 
04708       /* Process Unlocked */
04709       __HAL_UNLOCK(hi2c);
04710 
04711       /* Enable DMA Request */
04712       SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
04713 
04714       /* Note : The I2C interrupts must be enabled after unlocking current process
04715       to avoid the risk of I2C interrupt handle execution before current
04716       process unlock */
04717       /* Enable EVT and ERR interrupt */
04718       __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
04719 
04720       return HAL_OK;
04721     }
04722     else
04723     {
04724       /* Update I2C state */
04725       hi2c->State     = HAL_I2C_STATE_READY;
04726       hi2c->Mode      = HAL_I2C_MODE_NONE;
04727 
04728       /* Update I2C error code */
04729       hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
04730 
04731       /* Process Unlocked */
04732       __HAL_UNLOCK(hi2c);
04733 
04734       return HAL_ERROR;
04735     }
04736   }
04737   else
04738   {
04739     return HAL_BUSY;
04740   }
04741 }
04742 
04743 /**
04744   * @brief  Enable the Address listen mode with Interrupt.
04745   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
04746   *                the configuration information for the specified I2C.
04747   * @retval HAL status
04748   */
04749 HAL_StatusTypeDef HAL_I2C_EnableListen_IT(I2C_HandleTypeDef *hi2c)
04750 {
04751   if (hi2c->State == HAL_I2C_STATE_READY)
04752   {
04753     hi2c->State = HAL_I2C_STATE_LISTEN;
04754 
04755     /* Check if the I2C is already enabled */
04756     if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
04757     {
04758       /* Enable I2C peripheral */
04759       __HAL_I2C_ENABLE(hi2c);
04760     }
04761 
04762     /* Enable Address Acknowledge */
04763     SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
04764 
04765     /* Enable EVT and ERR interrupt */
04766     __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
04767 
04768     return HAL_OK;
04769   }
04770   else
04771   {
04772     return HAL_BUSY;
04773   }
04774 }
04775 
04776 /**
04777   * @brief  Disable the Address listen mode with Interrupt.
04778   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
04779   *                the configuration information for the specified I2C.
04780   * @retval HAL status
04781   */
04782 HAL_StatusTypeDef HAL_I2C_DisableListen_IT(I2C_HandleTypeDef *hi2c)
04783 {
04784   /* Declaration of tmp to prevent undefined behavior of volatile usage */
04785   uint32_t tmp;
04786 
04787   /* Disable Address listen mode only if a transfer is not ongoing */
04788   if (hi2c->State == HAL_I2C_STATE_LISTEN)
04789   {
04790     tmp = (uint32_t)(hi2c->State) & I2C_STATE_MSK;
04791     hi2c->PreviousState = tmp | (uint32_t)(hi2c->Mode);
04792     hi2c->State = HAL_I2C_STATE_READY;
04793     hi2c->Mode = HAL_I2C_MODE_NONE;
04794 
04795     /* Disable Address Acknowledge */
04796     CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
04797 
04798     /* Disable EVT and ERR interrupt */
04799     __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
04800 
04801     return HAL_OK;
04802   }
04803   else
04804   {
04805     return HAL_BUSY;
04806   }
04807 }
04808 
04809 /**
04810   * @brief  Abort a master I2C IT or DMA process communication with Interrupt.
04811   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
04812   *         the configuration information for the specified I2C.
04813   * @param  DevAddress Target device address: The device 7 bits address value
04814   *         in datasheet must be shifted to the left before calling the interface
04815   * @retval HAL status
04816   */
04817 HAL_StatusTypeDef HAL_I2C_Master_Abort_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress)
04818 {
04819   /* Declaration of temporary variables to prevent undefined behavior of volatile usage */
04820   HAL_I2C_ModeTypeDef CurrentMode   = hi2c->Mode;
04821 
04822   /* Prevent unused argument(s) compilation warning */
04823   UNUSED(DevAddress);
04824 
04825   /* Abort Master transfer during Receive or Transmit process    */
04826   if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET) && (CurrentMode == HAL_I2C_MODE_MASTER))
04827   {
04828     /* Process Locked */
04829     __HAL_LOCK(hi2c);
04830 
04831     hi2c->PreviousState = I2C_STATE_NONE;
04832     hi2c->State = HAL_I2C_STATE_ABORT;
04833 
04834     /* Disable Acknowledge */
04835     CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
04836 
04837     /* Generate Stop */
04838     SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
04839 
04840     hi2c->XferCount = 0U;
04841 
04842     /* Disable EVT, BUF and ERR interrupt */
04843     __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
04844 
04845     /* Process Unlocked */
04846     __HAL_UNLOCK(hi2c);
04847 
04848     /* Call the corresponding callback to inform upper layer of End of Transfer */
04849     I2C_ITError(hi2c);
04850 
04851     return HAL_OK;
04852   }
04853   else
04854   {
04855     /* Wrong usage of abort function */
04856     /* This function should be used only in case of abort monitored by master device */
04857     /* Or periphal is not in busy state, mean there is no active sequence to be abort */
04858     return HAL_ERROR;
04859   }
04860 }
04861 
04862 /**
04863   * @}
04864   */
04865 
04866 /** @defgroup I2C_IRQ_Handler_and_Callbacks IRQ Handler and Callbacks
04867  * @{
04868  */
04869 
04870 /**
04871   * @brief  This function handles I2C event interrupt request.
04872   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
04873   *                the configuration information for the specified I2C.
04874   * @retval None
04875   */
04876 void HAL_I2C_EV_IRQHandler(I2C_HandleTypeDef *hi2c)
04877 {
04878   uint32_t sr1itflags;
04879   uint32_t sr2itflags               = 0U;
04880   uint32_t itsources                = READ_REG(hi2c->Instance->CR2);
04881   uint32_t CurrentXferOptions       = hi2c->XferOptions;
04882   HAL_I2C_ModeTypeDef CurrentMode   = hi2c->Mode;
04883   HAL_I2C_StateTypeDef CurrentState = hi2c->State;
04884 
04885   /* Master or Memory mode selected */
04886   if ((CurrentMode == HAL_I2C_MODE_MASTER) || (CurrentMode == HAL_I2C_MODE_MEM))
04887   {
04888     sr2itflags   = READ_REG(hi2c->Instance->SR2);
04889     sr1itflags   = READ_REG(hi2c->Instance->SR1);
04890 
04891     /* Exit IRQ event until Start Bit detected in case of Other frame requested */
04892     if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_SB) == RESET) && (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(CurrentXferOptions) == 1U))
04893     {
04894       return;
04895     }
04896 
04897     /* SB Set ----------------------------------------------------------------*/
04898     if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_SB) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET))
04899     {
04900       /* Convert OTHER_xxx XferOptions if any */
04901       I2C_ConvertOtherXferOptions(hi2c);
04902 
04903       I2C_Master_SB(hi2c);
04904     }
04905     /* ADD10 Set -------------------------------------------------------------*/
04906     else if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_ADD10) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET))
04907     {
04908       I2C_Master_ADD10(hi2c);
04909     }
04910     /* ADDR Set --------------------------------------------------------------*/
04911     else if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_ADDR) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET))
04912     {
04913       I2C_Master_ADDR(hi2c);
04914     }
04915     /* I2C in mode Transmitter -----------------------------------------------*/
04916     else if (I2C_CHECK_FLAG(sr2itflags, I2C_FLAG_TRA) != RESET)
04917     {
04918       /* Do not check buffer and BTF flag if a Xfer DMA is on going */
04919       if (READ_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN) != I2C_CR2_DMAEN)
04920       {
04921         /* TXE set and BTF reset -----------------------------------------------*/
04922         if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_TXE) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_BUF) != RESET) && (I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BTF) == RESET))
04923         {
04924           I2C_MasterTransmit_TXE(hi2c);
04925         }
04926         /* BTF set -------------------------------------------------------------*/
04927         else if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BTF) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET))
04928         {
04929           if (CurrentMode == HAL_I2C_MODE_MASTER)
04930           {
04931             I2C_MasterTransmit_BTF(hi2c);
04932           }
04933           else /* HAL_I2C_MODE_MEM */
04934           {
04935             I2C_MemoryTransmit_TXE_BTF(hi2c);
04936           }
04937         }
04938         else
04939         {
04940           /* Do nothing */
04941         }
04942       }
04943     }
04944     /* I2C in mode Receiver --------------------------------------------------*/
04945     else
04946     {
04947       /* Do not check buffer and BTF flag if a Xfer DMA is on going */
04948       if (READ_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN) != I2C_CR2_DMAEN)
04949       {
04950         /* RXNE set and BTF reset -----------------------------------------------*/
04951         if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_RXNE) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_BUF) != RESET) && (I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BTF) == RESET))
04952         {
04953           I2C_MasterReceive_RXNE(hi2c);
04954         }
04955         /* BTF set -------------------------------------------------------------*/
04956         else if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BTF) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET))
04957         {
04958           I2C_MasterReceive_BTF(hi2c);
04959         }
04960         else
04961         {
04962           /* Do nothing */
04963         }
04964       }
04965     }
04966   }
04967   /* Slave mode selected */
04968   else
04969   {
04970     /* If an error is detected, read only SR1 register to prevent */
04971     /* a clear of ADDR flags by reading SR2 after reading SR1 in Error treatment */
04972     if (hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
04973     {
04974       sr1itflags   = READ_REG(hi2c->Instance->SR1);
04975     }
04976     else
04977     {
04978       sr2itflags   = READ_REG(hi2c->Instance->SR2);
04979       sr1itflags   = READ_REG(hi2c->Instance->SR1);
04980     }
04981 
04982     /* ADDR set --------------------------------------------------------------*/
04983     if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_ADDR) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET))
04984     {
04985       /* Now time to read SR2, this will clear ADDR flag automatically */
04986       if (hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
04987       {
04988         sr2itflags   = READ_REG(hi2c->Instance->SR2);
04989       }
04990       I2C_Slave_ADDR(hi2c, sr2itflags);
04991     }
04992     /* STOPF set --------------------------------------------------------------*/
04993     else if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_STOPF) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET))
04994     {
04995       I2C_Slave_STOPF(hi2c);
04996     }
04997     /* I2C in mode Transmitter -----------------------------------------------*/
04998     else if ((CurrentState == HAL_I2C_STATE_BUSY_TX) || (CurrentState == HAL_I2C_STATE_BUSY_TX_LISTEN))
04999     {
05000       /* TXE set and BTF reset -----------------------------------------------*/
05001       if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_TXE) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_BUF) != RESET) && (I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BTF) == RESET))
05002       {
05003         I2C_SlaveTransmit_TXE(hi2c);
05004       }
05005       /* BTF set -------------------------------------------------------------*/
05006       else if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BTF) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET))
05007       {
05008         I2C_SlaveTransmit_BTF(hi2c);
05009       }
05010       else
05011       {
05012         /* Do nothing */
05013       }
05014     }
05015     /* I2C in mode Receiver --------------------------------------------------*/
05016     else
05017     {
05018       /* RXNE set and BTF reset ----------------------------------------------*/
05019       if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_RXNE) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_BUF) != RESET) && (I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BTF) == RESET))
05020       {
05021         I2C_SlaveReceive_RXNE(hi2c);
05022       }
05023       /* BTF set -------------------------------------------------------------*/
05024       else if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BTF) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET))
05025       {
05026         I2C_SlaveReceive_BTF(hi2c);
05027       }
05028       else
05029       {
05030         /* Do nothing */
05031       }
05032     }
05033   }
05034 }
05035 
05036 /**
05037   * @brief  This function handles I2C error interrupt request.
05038   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
05039   *                the configuration information for the specified I2C.
05040   * @retval None
05041   */
05042 void HAL_I2C_ER_IRQHandler(I2C_HandleTypeDef *hi2c)
05043 {
05044   HAL_I2C_ModeTypeDef tmp1;
05045   uint32_t tmp2;
05046   HAL_I2C_StateTypeDef tmp3;
05047   uint32_t tmp4;
05048   uint32_t sr1itflags = READ_REG(hi2c->Instance->SR1);
05049   uint32_t itsources  = READ_REG(hi2c->Instance->CR2);
05050   uint32_t error      = HAL_I2C_ERROR_NONE;
05051   HAL_I2C_ModeTypeDef CurrentMode   = hi2c->Mode;
05052 
05053   /* I2C Bus error interrupt occurred ----------------------------------------*/
05054   if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BERR) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_ERR) != RESET))
05055   {
05056     error |= HAL_I2C_ERROR_BERR;
05057 
05058     /* Clear BERR flag */
05059     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_BERR);
05060 
05061     /* Workaround: Start cannot be generated after a misplaced Stop */
05062     SET_BIT(hi2c->Instance->CR1, I2C_CR1_SWRST);
05063   }
05064 
05065   /* I2C Arbitration Lost error interrupt occurred ---------------------------*/
05066   if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_ARLO) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_ERR) != RESET))
05067   {
05068     error |= HAL_I2C_ERROR_ARLO;
05069 
05070     /* Clear ARLO flag */
05071     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ARLO);
05072   }
05073 
05074   /* I2C Acknowledge failure error interrupt occurred ------------------------*/
05075   if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_AF) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_ERR) != RESET))
05076   {
05077     tmp1 = CurrentMode;
05078     tmp2 = hi2c->XferCount;
05079     tmp3 = hi2c->State;
05080     tmp4 = hi2c->PreviousState;
05081     if ((tmp1 == HAL_I2C_MODE_SLAVE) && (tmp2 == 0U) && \
05082         ((tmp3 == HAL_I2C_STATE_BUSY_TX) || (tmp3 == HAL_I2C_STATE_BUSY_TX_LISTEN) || \
05083          ((tmp3 == HAL_I2C_STATE_LISTEN) && (tmp4 == I2C_STATE_SLAVE_BUSY_TX))))
05084     {
05085       I2C_Slave_AF(hi2c);
05086     }
05087     else
05088     {
05089       /* Clear AF flag */
05090       __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
05091 
05092       error |= HAL_I2C_ERROR_AF;
05093 
05094       /* Do not generate a STOP in case of Slave receive non acknowledge during transfer (mean not at the end of transfer) */
05095       if ((CurrentMode == HAL_I2C_MODE_MASTER) || (CurrentMode == HAL_I2C_MODE_MEM))
05096       {
05097         /* Generate Stop */
05098         SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
05099       }
05100     }
05101   }
05102 
05103   /* I2C Over-Run/Under-Run interrupt occurred -------------------------------*/
05104   if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_OVR) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_ERR) != RESET))
05105   {
05106     error |= HAL_I2C_ERROR_OVR;
05107     /* Clear OVR flag */
05108     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_OVR);
05109   }
05110 
05111   /* Call the Error Callback in case of Error detected -----------------------*/
05112   if (error != HAL_I2C_ERROR_NONE)
05113   {
05114     hi2c->ErrorCode |= error;
05115     I2C_ITError(hi2c);
05116   }
05117 }
05118 
05119 /**
05120   * @brief  Master Tx Transfer completed callback.
05121   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
05122   *                the configuration information for the specified I2C.
05123   * @retval None
05124   */
05125 __weak void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c)
05126 {
05127   /* Prevent unused argument(s) compilation warning */
05128   UNUSED(hi2c);
05129 
05130   /* NOTE : This function should not be modified, when the callback is needed,
05131             the HAL_I2C_MasterTxCpltCallback could be implemented in the user file
05132    */
05133 }
05134 
05135 /**
05136   * @brief  Master Rx Transfer completed callback.
05137   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
05138   *                the configuration information for the specified I2C.
05139   * @retval None
05140   */
05141 __weak void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c)
05142 {
05143   /* Prevent unused argument(s) compilation warning */
05144   UNUSED(hi2c);
05145 
05146   /* NOTE : This function should not be modified, when the callback is needed,
05147             the HAL_I2C_MasterRxCpltCallback could be implemented in the user file
05148    */
05149 }
05150 
05151 /** @brief  Slave Tx Transfer completed callback.
05152   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
05153   *                the configuration information for the specified I2C.
05154   * @retval None
05155   */
05156 __weak void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c)
05157 {
05158   /* Prevent unused argument(s) compilation warning */
05159   UNUSED(hi2c);
05160 
05161   /* NOTE : This function should not be modified, when the callback is needed,
05162             the HAL_I2C_SlaveTxCpltCallback could be implemented in the user file
05163    */
05164 }
05165 
05166 /**
05167   * @brief  Slave Rx Transfer completed callback.
05168   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
05169   *                the configuration information for the specified I2C.
05170   * @retval None
05171   */
05172 __weak void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c)
05173 {
05174   /* Prevent unused argument(s) compilation warning */
05175   UNUSED(hi2c);
05176 
05177   /* NOTE : This function should not be modified, when the callback is needed,
05178             the HAL_I2C_SlaveRxCpltCallback could be implemented in the user file
05179    */
05180 }
05181 
05182 /**
05183   * @brief  Slave Address Match callback.
05184   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
05185   *                the configuration information for the specified I2C.
05186   * @param  TransferDirection Master request Transfer Direction (Write/Read), value of @ref I2C_XferDirection_definition
05187   * @param  AddrMatchCode Address Match Code
05188   * @retval None
05189   */
05190 __weak void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode)
05191 {
05192   /* Prevent unused argument(s) compilation warning */
05193   UNUSED(hi2c);
05194   UNUSED(TransferDirection);
05195   UNUSED(AddrMatchCode);
05196 
05197   /* NOTE : This function should not be modified, when the callback is needed,
05198             the HAL_I2C_AddrCallback() could be implemented in the user file
05199    */
05200 }
05201 
05202 /**
05203   * @brief  Listen Complete callback.
05204   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
05205   *                the configuration information for the specified I2C.
05206   * @retval None
05207   */
05208 __weak void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c)
05209 {
05210   /* Prevent unused argument(s) compilation warning */
05211   UNUSED(hi2c);
05212 
05213   /* NOTE : This function should not be modified, when the callback is needed,
05214             the HAL_I2C_ListenCpltCallback() could be implemented in the user file
05215   */
05216 }
05217 
05218 /**
05219   * @brief  Memory Tx Transfer completed callback.
05220   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
05221   *                the configuration information for the specified I2C.
05222   * @retval None
05223   */
05224 __weak void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef *hi2c)
05225 {
05226   /* Prevent unused argument(s) compilation warning */
05227   UNUSED(hi2c);
05228 
05229   /* NOTE : This function should not be modified, when the callback is needed,
05230             the HAL_I2C_MemTxCpltCallback could be implemented in the user file
05231    */
05232 }
05233 
05234 /**
05235   * @brief  Memory Rx Transfer completed callback.
05236   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
05237   *                the configuration information for the specified I2C.
05238   * @retval None
05239   */
05240 __weak void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c)
05241 {
05242   /* Prevent unused argument(s) compilation warning */
05243   UNUSED(hi2c);
05244 
05245   /* NOTE : This function should not be modified, when the callback is needed,
05246             the HAL_I2C_MemRxCpltCallback could be implemented in the user file
05247    */
05248 }
05249 
05250 /**
05251   * @brief  I2C error callback.
05252   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
05253   *                the configuration information for the specified I2C.
05254   * @retval None
05255   */
05256 __weak void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
05257 {
05258   /* Prevent unused argument(s) compilation warning */
05259   UNUSED(hi2c);
05260 
05261   /* NOTE : This function should not be modified, when the callback is needed,
05262             the HAL_I2C_ErrorCallback could be implemented in the user file
05263    */
05264 }
05265 
05266 /**
05267   * @brief  I2C abort callback.
05268   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
05269   *                the configuration information for the specified I2C.
05270   * @retval None
05271   */
05272 __weak void HAL_I2C_AbortCpltCallback(I2C_HandleTypeDef *hi2c)
05273 {
05274   /* Prevent unused argument(s) compilation warning */
05275   UNUSED(hi2c);
05276 
05277   /* NOTE : This function should not be modified, when the callback is needed,
05278             the HAL_I2C_AbortCpltCallback could be implemented in the user file
05279    */
05280 }
05281 
05282 /**
05283   * @}
05284   */
05285 
05286 /** @defgroup I2C_Exported_Functions_Group3 Peripheral State, Mode and Error functions
05287  *  @brief   Peripheral State, Mode and Error functions
05288   *
05289 @verbatim
05290  ===============================================================================
05291             ##### Peripheral State, Mode and Error functions #####
05292  ===============================================================================
05293     [..]
05294     This subsection permit to get in run-time the status of the peripheral
05295     and the data flow.
05296 
05297 @endverbatim
05298   * @{
05299   */
05300 
05301 /**
05302   * @brief  Return the I2C handle state.
05303   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
05304   *                the configuration information for the specified I2C.
05305   * @retval HAL state
05306   */
05307 HAL_I2C_StateTypeDef HAL_I2C_GetState(I2C_HandleTypeDef *hi2c)
05308 {
05309   /* Return I2C handle state */
05310   return hi2c->State;
05311 }
05312 
05313 /**
05314   * @brief  Returns the I2C Master, Slave, Memory or no mode.
05315   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
05316   *         the configuration information for I2C module
05317   * @retval HAL mode
05318   */
05319 HAL_I2C_ModeTypeDef HAL_I2C_GetMode(I2C_HandleTypeDef *hi2c)
05320 {
05321   return hi2c->Mode;
05322 }
05323 
05324 /**
05325   * @brief  Return the I2C error code.
05326   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
05327   *              the configuration information for the specified I2C.
05328   * @retval I2C Error Code
05329   */
05330 uint32_t HAL_I2C_GetError(I2C_HandleTypeDef *hi2c)
05331 {
05332   return hi2c->ErrorCode;
05333 }
05334 
05335 /**
05336   * @}
05337   */
05338 
05339 /**
05340   * @}
05341   */
05342 
05343 /** @addtogroup I2C_Private_Functions
05344   * @{
05345   */
05346 
05347 /**
05348   * @brief  Handle TXE flag for Master
05349   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
05350   *         the configuration information for I2C module
05351   * @retval None
05352   */
05353 static void I2C_MasterTransmit_TXE(I2C_HandleTypeDef *hi2c)
05354 {
05355   /* Declaration of temporary variables to prevent undefined behavior of volatile usage */
05356   HAL_I2C_StateTypeDef CurrentState = hi2c->State;
05357   HAL_I2C_ModeTypeDef CurrentMode   = hi2c->Mode;
05358   uint32_t CurrentXferOptions       = hi2c->XferOptions;
05359 
05360   if ((hi2c->XferSize == 0U) && (CurrentState == HAL_I2C_STATE_BUSY_TX))
05361   {
05362     /* Call TxCpltCallback() directly if no stop mode is set */
05363     if ((CurrentXferOptions != I2C_FIRST_AND_LAST_FRAME) && (CurrentXferOptions != I2C_LAST_FRAME) && (CurrentXferOptions != I2C_NO_OPTION_FRAME))
05364     {
05365       __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
05366 
05367       hi2c->PreviousState = I2C_STATE_MASTER_BUSY_TX;
05368       hi2c->Mode = HAL_I2C_MODE_NONE;
05369       hi2c->State = HAL_I2C_STATE_READY;
05370 
05371 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
05372       hi2c->MasterTxCpltCallback(hi2c);
05373 #else
05374       HAL_I2C_MasterTxCpltCallback(hi2c);
05375 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
05376     }
05377     else /* Generate Stop condition then Call TxCpltCallback() */
05378     {
05379       /* Disable EVT, BUF and ERR interrupt */
05380       __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
05381 
05382       /* Generate Stop */
05383       SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
05384 
05385       hi2c->PreviousState = I2C_STATE_NONE;
05386       hi2c->State = HAL_I2C_STATE_READY;
05387 
05388       if (hi2c->Mode == HAL_I2C_MODE_MEM)
05389       {
05390         hi2c->Mode = HAL_I2C_MODE_NONE;
05391 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
05392         hi2c->MemTxCpltCallback(hi2c);
05393 #else
05394         HAL_I2C_MemTxCpltCallback(hi2c);
05395 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
05396       }
05397       else
05398       {
05399         hi2c->Mode = HAL_I2C_MODE_NONE;
05400 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
05401         hi2c->MasterTxCpltCallback(hi2c);
05402 #else
05403         HAL_I2C_MasterTxCpltCallback(hi2c);
05404 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
05405       }
05406     }
05407   }
05408   else if ((CurrentState == HAL_I2C_STATE_BUSY_TX) || \
05409            ((CurrentMode == HAL_I2C_MODE_MEM) && (CurrentState == HAL_I2C_STATE_BUSY_RX)))
05410   {
05411     if (hi2c->XferCount == 0U)
05412     {
05413       /* Disable BUF interrupt */
05414       __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF);
05415     }
05416     else
05417     {
05418       if (hi2c->Mode == HAL_I2C_MODE_MEM)
05419       {
05420         I2C_MemoryTransmit_TXE_BTF(hi2c);
05421       }
05422       else
05423       {
05424         /* Write data to DR */
05425         hi2c->Instance->DR = *hi2c->pBuffPtr;
05426 
05427         /* Increment Buffer pointer */
05428         hi2c->pBuffPtr++;
05429 
05430         /* Update counter */
05431         hi2c->XferCount--;
05432       }
05433     }
05434   }
05435   else
05436   {
05437     /* Do nothing */
05438   }
05439 }
05440 
05441 /**
05442   * @brief  Handle BTF flag for Master transmitter
05443   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
05444   *         the configuration information for I2C module
05445   * @retval None
05446   */
05447 static void I2C_MasterTransmit_BTF(I2C_HandleTypeDef *hi2c)
05448 {
05449   /* Declaration of temporary variables to prevent undefined behavior of volatile usage */
05450   uint32_t CurrentXferOptions = hi2c->XferOptions;
05451 
05452   if (hi2c->State == HAL_I2C_STATE_BUSY_TX)
05453   {
05454     if (hi2c->XferCount != 0U)
05455     {
05456       /* Write data to DR */
05457       hi2c->Instance->DR = *hi2c->pBuffPtr;
05458 
05459       /* Increment Buffer pointer */
05460       hi2c->pBuffPtr++;
05461 
05462       /* Update counter */
05463       hi2c->XferCount--;
05464     }
05465     else
05466     {
05467       /* Call TxCpltCallback() directly if no stop mode is set */
05468       if ((CurrentXferOptions != I2C_FIRST_AND_LAST_FRAME) && (CurrentXferOptions != I2C_LAST_FRAME) && (CurrentXferOptions != I2C_NO_OPTION_FRAME))
05469       {
05470         __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
05471 
05472         hi2c->PreviousState = I2C_STATE_MASTER_BUSY_TX;
05473         hi2c->Mode = HAL_I2C_MODE_NONE;
05474         hi2c->State = HAL_I2C_STATE_READY;
05475 
05476 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
05477         hi2c->MasterTxCpltCallback(hi2c);
05478 #else
05479         HAL_I2C_MasterTxCpltCallback(hi2c);
05480 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
05481       }
05482       else /* Generate Stop condition then Call TxCpltCallback() */
05483       {
05484         /* Disable EVT, BUF and ERR interrupt */
05485         __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
05486 
05487         /* Generate Stop */
05488         SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
05489 
05490         hi2c->PreviousState = I2C_STATE_NONE;
05491         hi2c->State = HAL_I2C_STATE_READY;
05492         hi2c->Mode = HAL_I2C_MODE_NONE;
05493 
05494 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
05495         hi2c->MasterTxCpltCallback(hi2c);
05496 #else
05497         HAL_I2C_MasterTxCpltCallback(hi2c);
05498 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
05499       }
05500     }
05501   }
05502   else
05503   {
05504     /* Do nothing */
05505   }
05506 }
05507 
05508 /**
05509   * @brief  Handle TXE and BTF flag for Memory transmitter
05510   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
05511   *         the configuration information for I2C module
05512   * @retval None
05513   */
05514 static void I2C_MemoryTransmit_TXE_BTF(I2C_HandleTypeDef *hi2c)
05515 {
05516   /* Declaration of temporary variables to prevent undefined behavior of volatile usage */
05517   HAL_I2C_StateTypeDef CurrentState = hi2c->State;
05518 
05519   if (hi2c->EventCount == 0U)
05520   {
05521     /* If Memory address size is 8Bit */
05522     if (hi2c->MemaddSize == I2C_MEMADD_SIZE_8BIT)
05523     {
05524       /* Send Memory Address */
05525       hi2c->Instance->DR = I2C_MEM_ADD_LSB(hi2c->Memaddress);
05526 
05527       hi2c->EventCount += 2U;
05528     }
05529     /* If Memory address size is 16Bit */
05530     else
05531     {
05532       /* Send MSB of Memory Address */
05533       hi2c->Instance->DR = I2C_MEM_ADD_MSB(hi2c->Memaddress);
05534 
05535       hi2c->EventCount++;
05536     }
05537   }
05538   else if (hi2c->EventCount == 1U)
05539   {
05540     /* Send LSB of Memory Address */
05541     hi2c->Instance->DR = I2C_MEM_ADD_LSB(hi2c->Memaddress);
05542 
05543     hi2c->EventCount++;
05544   }
05545   else if (hi2c->EventCount == 2U)
05546   {
05547     if (CurrentState == HAL_I2C_STATE_BUSY_RX)
05548     {
05549       /* Generate Restart */
05550       hi2c->Instance->CR1 |= I2C_CR1_START;
05551 
05552       hi2c->EventCount++;
05553     }
05554     else if ((hi2c->XferCount > 0U) && (CurrentState == HAL_I2C_STATE_BUSY_TX))
05555     {
05556       /* Write data to DR */
05557       hi2c->Instance->DR = *hi2c->pBuffPtr;
05558 
05559       /* Increment Buffer pointer */
05560       hi2c->pBuffPtr++;
05561 
05562       /* Update counter */
05563       hi2c->XferCount--;
05564     }
05565     else if ((hi2c->XferCount == 0U) && (CurrentState == HAL_I2C_STATE_BUSY_TX))
05566     {
05567       /* Generate Stop condition then Call TxCpltCallback() */
05568       /* Disable EVT, BUF and ERR interrupt */
05569       __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
05570 
05571       /* Generate Stop */
05572       SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
05573 
05574       hi2c->PreviousState = I2C_STATE_NONE;
05575       hi2c->State = HAL_I2C_STATE_READY;
05576       hi2c->Mode = HAL_I2C_MODE_NONE;
05577 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
05578       hi2c->MemTxCpltCallback(hi2c);
05579 #else
05580       HAL_I2C_MemTxCpltCallback(hi2c);
05581 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
05582     }
05583     else
05584     {
05585       /* Do nothing */
05586     }
05587   }
05588   else
05589   {
05590     /* Do nothing */
05591   }
05592 }
05593 
05594 /**
05595   * @brief  Handle RXNE flag for Master
05596   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
05597   *         the configuration information for I2C module
05598   * @retval None
05599   */
05600 static void I2C_MasterReceive_RXNE(I2C_HandleTypeDef *hi2c)
05601 {
05602   if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
05603   {
05604     uint32_t tmp;
05605 
05606     tmp = hi2c->XferCount;
05607     if (tmp > 3U)
05608     {
05609       /* Read data from DR */
05610       *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
05611 
05612       /* Increment Buffer pointer */
05613       hi2c->pBuffPtr++;
05614 
05615       /* Update counter */
05616       hi2c->XferCount--;
05617 
05618       if (hi2c->XferCount == (uint16_t)3)
05619       {
05620         /* Disable BUF interrupt, this help to treat correctly the last 4 bytes
05621         on BTF subroutine */
05622         /* Disable BUF interrupt */
05623         __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF);
05624       }
05625     }
05626     else if ((hi2c->XferOptions != I2C_FIRST_AND_NEXT_FRAME) && ((tmp == 1U) || (tmp == 0U)))
05627     {
05628       if (I2C_WaitOnSTOPRequestThroughIT(hi2c) == HAL_OK)
05629       {
05630         /* Disable Acknowledge */
05631         CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
05632 
05633         /* Disable EVT, BUF and ERR interrupt */
05634         __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
05635 
05636         /* Read data from DR */
05637         *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
05638 
05639         /* Increment Buffer pointer */
05640         hi2c->pBuffPtr++;
05641 
05642         /* Update counter */
05643         hi2c->XferCount--;
05644 
05645         hi2c->State = HAL_I2C_STATE_READY;
05646 
05647         if (hi2c->Mode == HAL_I2C_MODE_MEM)
05648         {
05649           hi2c->Mode = HAL_I2C_MODE_NONE;
05650           hi2c->PreviousState = I2C_STATE_NONE;
05651 
05652 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
05653           hi2c->MemRxCpltCallback(hi2c);
05654 #else
05655           HAL_I2C_MemRxCpltCallback(hi2c);
05656 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
05657         }
05658         else
05659         {
05660           hi2c->Mode = HAL_I2C_MODE_NONE;
05661           hi2c->PreviousState = I2C_STATE_MASTER_BUSY_RX;
05662 
05663 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
05664           hi2c->MasterRxCpltCallback(hi2c);
05665 #else
05666           HAL_I2C_MasterRxCpltCallback(hi2c);
05667 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
05668         }
05669       }
05670       else
05671       {
05672         /* Disable EVT, BUF and ERR interrupt */
05673         __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
05674 
05675         /* Read data from DR */
05676         *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
05677 
05678         /* Increment Buffer pointer */
05679         hi2c->pBuffPtr++;
05680 
05681         /* Update counter */
05682         hi2c->XferCount--;
05683 
05684         hi2c->State = HAL_I2C_STATE_READY;
05685         hi2c->Mode = HAL_I2C_MODE_NONE;
05686 
05687         /* Call user error callback */
05688 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
05689         hi2c->ErrorCallback(hi2c);
05690 #else
05691         HAL_I2C_ErrorCallback(hi2c);
05692 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
05693       }
05694     }
05695     else
05696     {
05697       /* Do nothing */
05698     }
05699   }
05700 }
05701 
05702 /**
05703   * @brief  Handle BTF flag for Master receiver
05704   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
05705   *         the configuration information for I2C module
05706   * @retval None
05707   */
05708 static void I2C_MasterReceive_BTF(I2C_HandleTypeDef *hi2c)
05709 {
05710   /* Declaration of temporary variables to prevent undefined behavior of volatile usage */
05711   uint32_t CurrentXferOptions = hi2c->XferOptions;
05712 
05713   if (hi2c->XferCount == 4U)
05714   {
05715     /* Disable BUF interrupt, this help to treat correctly the last 2 bytes
05716        on BTF subroutine if there is a reception delay between N-1 and N byte */
05717     __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF);
05718 
05719     /* Read data from DR */
05720     *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
05721 
05722     /* Increment Buffer pointer */
05723     hi2c->pBuffPtr++;
05724 
05725     /* Update counter */
05726     hi2c->XferCount--;
05727   }
05728   else if (hi2c->XferCount == 3U)
05729   {
05730     /* Disable BUF interrupt, this help to treat correctly the last 2 bytes
05731        on BTF subroutine if there is a reception delay between N-1 and N byte */
05732     __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF);
05733 
05734     if ((CurrentXferOptions != I2C_NEXT_FRAME) && (CurrentXferOptions != I2C_FIRST_AND_NEXT_FRAME))
05735     {
05736       /* Disable Acknowledge */
05737       CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
05738     }
05739 
05740     /* Read data from DR */
05741     *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
05742 
05743     /* Increment Buffer pointer */
05744     hi2c->pBuffPtr++;
05745 
05746     /* Update counter */
05747     hi2c->XferCount--;
05748   }
05749   else if (hi2c->XferCount == 2U)
05750   {
05751     /* Prepare next transfer or stop current transfer */
05752     if ((CurrentXferOptions == I2C_FIRST_FRAME) || (CurrentXferOptions == I2C_LAST_FRAME_NO_STOP))
05753     {
05754       /* Disable Acknowledge */
05755       CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
05756     }
05757     else if ((CurrentXferOptions == I2C_NEXT_FRAME) || (CurrentXferOptions == I2C_FIRST_AND_NEXT_FRAME))
05758     {
05759       /* Enable Acknowledge */
05760       SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
05761     }
05762     else if (CurrentXferOptions != I2C_LAST_FRAME_NO_STOP)
05763     {
05764       /* Generate Stop */
05765       SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
05766     }
05767     else
05768     {
05769       /* Do nothing */
05770     }
05771 
05772     /* Read data from DR */
05773     *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
05774 
05775     /* Increment Buffer pointer */
05776     hi2c->pBuffPtr++;
05777 
05778     /* Update counter */
05779     hi2c->XferCount--;
05780 
05781     /* Read data from DR */
05782     *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
05783 
05784     /* Increment Buffer pointer */
05785     hi2c->pBuffPtr++;
05786 
05787     /* Update counter */
05788     hi2c->XferCount--;
05789 
05790     /* Disable EVT and ERR interrupt */
05791     __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
05792 
05793     hi2c->State = HAL_I2C_STATE_READY;
05794     if (hi2c->Mode == HAL_I2C_MODE_MEM)
05795     {
05796       hi2c->Mode = HAL_I2C_MODE_NONE;
05797       hi2c->PreviousState = I2C_STATE_NONE;
05798 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
05799       hi2c->MemRxCpltCallback(hi2c);
05800 #else
05801       HAL_I2C_MemRxCpltCallback(hi2c);
05802 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
05803     }
05804     else
05805     {
05806       hi2c->Mode = HAL_I2C_MODE_NONE;
05807       hi2c->PreviousState = I2C_STATE_MASTER_BUSY_RX;
05808 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
05809       hi2c->MasterRxCpltCallback(hi2c);
05810 #else
05811       HAL_I2C_MasterRxCpltCallback(hi2c);
05812 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
05813     }
05814   }
05815   else
05816   {
05817     /* Read data from DR */
05818     *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
05819 
05820     /* Increment Buffer pointer */
05821     hi2c->pBuffPtr++;
05822 
05823     /* Update counter */
05824     hi2c->XferCount--;
05825   }
05826 }
05827 
05828 /**
05829   * @brief  Handle SB flag for Master
05830   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
05831   *         the configuration information for I2C module
05832   * @retval None
05833   */
05834 static void I2C_Master_SB(I2C_HandleTypeDef *hi2c)
05835 {
05836   if (hi2c->Mode == HAL_I2C_MODE_MEM)
05837   {
05838     if (hi2c->EventCount == 0U)
05839     {
05840       /* Send slave address */
05841       hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(hi2c->Devaddress);
05842     }
05843     else
05844     {
05845       hi2c->Instance->DR = I2C_7BIT_ADD_READ(hi2c->Devaddress);
05846     }
05847   }
05848   else
05849   {
05850     if (hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_7BIT)
05851     {
05852       /* Send slave 7 Bits address */
05853       if (hi2c->State == HAL_I2C_STATE_BUSY_TX)
05854       {
05855         hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(hi2c->Devaddress);
05856       }
05857       else
05858       {
05859         hi2c->Instance->DR = I2C_7BIT_ADD_READ(hi2c->Devaddress);
05860       }
05861 
05862       if (((hi2c->hdmatx != NULL) && (hi2c->hdmatx->XferCpltCallback != NULL))
05863           || ((hi2c->hdmarx != NULL) && (hi2c->hdmarx->XferCpltCallback != NULL)))
05864       {
05865         /* Enable DMA Request */
05866         SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
05867       }
05868     }
05869     else
05870     {
05871       if (hi2c->EventCount == 0U)
05872       {
05873         /* Send header of slave address */
05874         hi2c->Instance->DR = I2C_10BIT_HEADER_WRITE(hi2c->Devaddress);
05875       }
05876       else if (hi2c->EventCount == 1U)
05877       {
05878         /* Send header of slave address */
05879         hi2c->Instance->DR = I2C_10BIT_HEADER_READ(hi2c->Devaddress);
05880       }
05881       else
05882       {
05883         /* Do nothing */
05884       }
05885     }
05886   }
05887 }
05888 
05889 /**
05890   * @brief  Handle ADD10 flag for Master
05891   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
05892   *         the configuration information for I2C module
05893   * @retval None
05894   */
05895 static void I2C_Master_ADD10(I2C_HandleTypeDef *hi2c)
05896 {
05897   /* Send slave address */
05898   hi2c->Instance->DR = I2C_10BIT_ADDRESS(hi2c->Devaddress);
05899 
05900   if (((hi2c->hdmatx != NULL) && (hi2c->hdmatx->XferCpltCallback != NULL))
05901       || ((hi2c->hdmarx != NULL) && (hi2c->hdmarx->XferCpltCallback != NULL)))
05902   {
05903     /* Enable DMA Request */
05904     SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
05905   }
05906 }
05907 
05908 /**
05909   * @brief  Handle ADDR flag for Master
05910   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
05911   *         the configuration information for I2C module
05912   * @retval None
05913   */
05914 static void I2C_Master_ADDR(I2C_HandleTypeDef *hi2c)
05915 {
05916   /* Declaration of temporary variable to prevent undefined behavior of volatile usage */
05917   HAL_I2C_ModeTypeDef CurrentMode       = hi2c->Mode;
05918   uint32_t CurrentXferOptions           = hi2c->XferOptions;
05919   uint32_t Prev_State                   = hi2c->PreviousState;
05920 
05921   if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
05922   {
05923     if ((hi2c->EventCount == 0U) && (CurrentMode == HAL_I2C_MODE_MEM))
05924     {
05925       /* Clear ADDR flag */
05926       __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
05927     }
05928     else if ((hi2c->EventCount == 0U) && (hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT))
05929     {
05930       /* Clear ADDR flag */
05931       __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
05932 
05933       /* Generate Restart */
05934       SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
05935 
05936       hi2c->EventCount++;
05937     }
05938     else
05939     {
05940       if (hi2c->XferCount == 0U)
05941       {
05942         /* Clear ADDR flag */
05943         __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
05944 
05945         /* Generate Stop */
05946         SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
05947       }
05948       else if (hi2c->XferCount == 1U)
05949       {
05950         if (CurrentXferOptions == I2C_NO_OPTION_FRAME)
05951         {
05952           /* Disable Acknowledge */
05953           CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
05954 
05955           if ((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN)
05956           {
05957             /* Disable Acknowledge */
05958             CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
05959 
05960             /* Clear ADDR flag */
05961             __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
05962           }
05963           else
05964           {
05965             /* Clear ADDR flag */
05966             __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
05967 
05968             /* Generate Stop */
05969             SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
05970           }
05971         }
05972         /* Prepare next transfer or stop current transfer */
05973         else if ((CurrentXferOptions != I2C_FIRST_AND_LAST_FRAME) && (CurrentXferOptions != I2C_LAST_FRAME) \
05974                  && ((Prev_State != I2C_STATE_MASTER_BUSY_RX) || (CurrentXferOptions == I2C_FIRST_FRAME)))
05975         {
05976           if ((CurrentXferOptions != I2C_NEXT_FRAME) && (CurrentXferOptions != I2C_FIRST_AND_NEXT_FRAME) && (CurrentXferOptions != I2C_LAST_FRAME_NO_STOP))
05977           {
05978             /* Disable Acknowledge */
05979             CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
05980           }
05981           else
05982           {
05983             /* Enable Acknowledge */
05984             SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
05985           }
05986 
05987           /* Clear ADDR flag */
05988           __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
05989         }
05990         else
05991         {
05992           /* Disable Acknowledge */
05993           CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
05994 
05995           /* Clear ADDR flag */
05996           __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
05997 
05998           /* Generate Stop */
05999           SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
06000         }
06001       }
06002       else if (hi2c->XferCount == 2U)
06003       {
06004         if ((CurrentXferOptions != I2C_NEXT_FRAME) && (CurrentXferOptions != I2C_FIRST_AND_NEXT_FRAME) && (CurrentXferOptions != I2C_LAST_FRAME_NO_STOP))
06005         {
06006           /* Enable Pos */
06007           SET_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
06008 
06009           /* Clear ADDR flag */
06010           __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
06011 
06012           /* Disable Acknowledge */
06013           CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
06014         }
06015         else
06016         {
06017           /* Enable Acknowledge */
06018           SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
06019 
06020           /* Clear ADDR flag */
06021           __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
06022         }
06023 
06024         if (((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN) && ((CurrentXferOptions == I2C_NO_OPTION_FRAME) || (CurrentXferOptions == I2C_FIRST_FRAME) || (CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_LAST_FRAME_NO_STOP) || (CurrentXferOptions == I2C_LAST_FRAME)))
06025         {
06026           /* Enable Last DMA bit */
06027           SET_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
06028         }
06029       }
06030       else
06031       {
06032         /* Enable Acknowledge */
06033         SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
06034 
06035         if (((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN) && ((CurrentXferOptions == I2C_NO_OPTION_FRAME) || (CurrentXferOptions == I2C_FIRST_FRAME) || (CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_LAST_FRAME_NO_STOP) || (CurrentXferOptions == I2C_LAST_FRAME)))
06036         {
06037           /* Enable Last DMA bit */
06038           SET_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
06039         }
06040 
06041         /* Clear ADDR flag */
06042         __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
06043       }
06044 
06045       /* Reset Event counter  */
06046       hi2c->EventCount = 0U;
06047     }
06048   }
06049   else
06050   {
06051     /* Clear ADDR flag */
06052     __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
06053   }
06054 }
06055 
06056 /**
06057   * @brief  Handle TXE flag for Slave
06058   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
06059   *         the configuration information for I2C module
06060   * @retval None
06061   */
06062 static void I2C_SlaveTransmit_TXE(I2C_HandleTypeDef *hi2c)
06063 {
06064   /* Declaration of temporary variables to prevent undefined behavior of volatile usage */
06065   HAL_I2C_StateTypeDef CurrentState = hi2c->State;
06066 
06067   if (hi2c->XferCount != 0U)
06068   {
06069     /* Write data to DR */
06070     hi2c->Instance->DR = *hi2c->pBuffPtr;
06071 
06072     /* Increment Buffer pointer */
06073     hi2c->pBuffPtr++;
06074 
06075     /* Update counter */
06076     hi2c->XferCount--;
06077 
06078     if ((hi2c->XferCount == 0U) && (CurrentState == HAL_I2C_STATE_BUSY_TX_LISTEN))
06079     {
06080       /* Last Byte is received, disable Interrupt */
06081       __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF);
06082 
06083       /* Set state at HAL_I2C_STATE_LISTEN */
06084       hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_TX;
06085       hi2c->State = HAL_I2C_STATE_LISTEN;
06086 
06087       /* Call the corresponding callback to inform upper layer of End of Transfer */
06088 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
06089       hi2c->SlaveTxCpltCallback(hi2c);
06090 #else
06091       HAL_I2C_SlaveTxCpltCallback(hi2c);
06092 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
06093     }
06094   }
06095 }
06096 
06097 /**
06098   * @brief  Handle BTF flag for Slave transmitter
06099   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
06100   *         the configuration information for I2C module
06101   * @retval None
06102   */
06103 static void I2C_SlaveTransmit_BTF(I2C_HandleTypeDef *hi2c)
06104 {
06105   if (hi2c->XferCount != 0U)
06106   {
06107     /* Write data to DR */
06108     hi2c->Instance->DR = *hi2c->pBuffPtr;
06109 
06110     /* Increment Buffer pointer */
06111     hi2c->pBuffPtr++;
06112 
06113     /* Update counter */
06114     hi2c->XferCount--;
06115   }
06116 }
06117 
06118 /**
06119   * @brief  Handle RXNE flag for Slave
06120   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
06121   *         the configuration information for I2C module
06122   * @retval None
06123   */
06124 static void I2C_SlaveReceive_RXNE(I2C_HandleTypeDef *hi2c)
06125 {
06126   /* Declaration of temporary variables to prevent undefined behavior of volatile usage */
06127   HAL_I2C_StateTypeDef CurrentState = hi2c->State;
06128 
06129   if (hi2c->XferCount != 0U)
06130   {
06131     /* Read data from DR */
06132     *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
06133 
06134     /* Increment Buffer pointer */
06135     hi2c->pBuffPtr++;
06136 
06137     /* Update counter */
06138     hi2c->XferCount--;
06139 
06140     if ((hi2c->XferCount == 0U) && (CurrentState == HAL_I2C_STATE_BUSY_RX_LISTEN))
06141     {
06142       /* Last Byte is received, disable Interrupt */
06143       __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF);
06144 
06145       /* Set state at HAL_I2C_STATE_LISTEN */
06146       hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_RX;
06147       hi2c->State = HAL_I2C_STATE_LISTEN;
06148 
06149       /* Call the corresponding callback to inform upper layer of End of Transfer */
06150 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
06151       hi2c->SlaveRxCpltCallback(hi2c);
06152 #else
06153       HAL_I2C_SlaveRxCpltCallback(hi2c);
06154 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
06155     }
06156   }
06157 }
06158 
06159 /**
06160   * @brief  Handle BTF flag for Slave receiver
06161   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
06162   *         the configuration information for I2C module
06163   * @retval None
06164   */
06165 static void I2C_SlaveReceive_BTF(I2C_HandleTypeDef *hi2c)
06166 {
06167   if (hi2c->XferCount != 0U)
06168   {
06169     /* Read data from DR */
06170     *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
06171 
06172     /* Increment Buffer pointer */
06173     hi2c->pBuffPtr++;
06174 
06175     /* Update counter */
06176     hi2c->XferCount--;
06177   }
06178 }
06179 
06180 /**
06181   * @brief  Handle ADD flag for Slave
06182   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
06183   *         the configuration information for I2C module
06184   * @param  IT2Flags Interrupt2 flags to handle.
06185   * @retval None
06186   */
06187 static void I2C_Slave_ADDR(I2C_HandleTypeDef *hi2c, uint32_t IT2Flags)
06188 {
06189   uint8_t TransferDirection = I2C_DIRECTION_RECEIVE;
06190   uint16_t SlaveAddrCode;
06191 
06192   if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
06193   {
06194     /* Disable BUF interrupt, BUF enabling is manage through slave specific interface */
06195     __HAL_I2C_DISABLE_IT(hi2c, (I2C_IT_BUF));
06196 
06197     /* Transfer Direction requested by Master */
06198     if (I2C_CHECK_FLAG(IT2Flags, I2C_FLAG_TRA) == RESET)
06199     {
06200       TransferDirection = I2C_DIRECTION_TRANSMIT;
06201     }
06202 
06203     if (I2C_CHECK_FLAG(IT2Flags, I2C_FLAG_DUALF) == RESET)
06204     {
06205       SlaveAddrCode = (uint16_t)hi2c->Init.OwnAddress1;
06206     }
06207     else
06208     {
06209       SlaveAddrCode = (uint16_t)hi2c->Init.OwnAddress2;
06210     }
06211 
06212     /* Process Unlocked */
06213     __HAL_UNLOCK(hi2c);
06214 
06215     /* Call Slave Addr callback */
06216 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
06217     hi2c->AddrCallback(hi2c, TransferDirection, SlaveAddrCode);
06218 #else
06219     HAL_I2C_AddrCallback(hi2c, TransferDirection, SlaveAddrCode);
06220 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
06221   }
06222   else
06223   {
06224     /* Clear ADDR flag */
06225     __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
06226 
06227     /* Process Unlocked */
06228     __HAL_UNLOCK(hi2c);
06229   }
06230 }
06231 
06232 /**
06233   * @brief  Handle STOPF flag for Slave
06234   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
06235   *         the configuration information for I2C module
06236   * @retval None
06237   */
06238 static void I2C_Slave_STOPF(I2C_HandleTypeDef *hi2c)
06239 {
06240   /* Declaration of temporary variable to prevent undefined behavior of volatile usage */
06241   HAL_I2C_StateTypeDef CurrentState = hi2c->State;
06242 
06243   /* Disable EVT, BUF and ERR interrupt */
06244   __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
06245 
06246   /* Clear STOPF flag */
06247   __HAL_I2C_CLEAR_STOPFLAG(hi2c);
06248 
06249   /* Disable Acknowledge */
06250   CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
06251 
06252   /* If a DMA is ongoing, Update handle size context */
06253   if ((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN)
06254   {
06255     if ((CurrentState == HAL_I2C_STATE_BUSY_RX) || (CurrentState == HAL_I2C_STATE_BUSY_RX_LISTEN))
06256     {
06257       hi2c->XferCount = (uint16_t)(__HAL_DMA_GET_COUNTER(hi2c->hdmarx));
06258 
06259       if (hi2c->XferCount != 0U)
06260       {
06261         /* Set ErrorCode corresponding to a Non-Acknowledge */
06262         hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
06263       }
06264 
06265       /* Disable, stop the current DMA */
06266       CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
06267 
06268       /* Abort DMA Xfer if any */
06269       if (HAL_DMA_GetState(hi2c->hdmarx) != HAL_DMA_STATE_READY)
06270       {
06271         /* Set the I2C DMA Abort callback :
06272         will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
06273         hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort;
06274 
06275         /* Abort DMA RX */
06276         if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK)
06277         {
06278           /* Call Directly XferAbortCallback function in case of error */
06279           hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx);
06280         }
06281       }
06282     }
06283     else
06284     {
06285       hi2c->XferCount = (uint16_t)(__HAL_DMA_GET_COUNTER(hi2c->hdmatx));
06286 
06287       if (hi2c->XferCount != 0U)
06288       {
06289         /* Set ErrorCode corresponding to a Non-Acknowledge */
06290         hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
06291       }
06292 
06293       /* Disable, stop the current DMA */
06294       CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
06295 
06296       /* Abort DMA Xfer if any */
06297       if (HAL_DMA_GetState(hi2c->hdmatx) != HAL_DMA_STATE_READY)
06298       {
06299         /* Set the I2C DMA Abort callback :
06300         will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
06301         hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort;
06302 
06303         /* Abort DMA TX */
06304         if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK)
06305         {
06306           /* Call Directly XferAbortCallback function in case of error */
06307           hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx);
06308         }
06309       }
06310     }
06311   }
06312 
06313   /* All data are not transferred, so set error code accordingly */
06314   if (hi2c->XferCount != 0U)
06315   {
06316     /* Store Last receive data if any */
06317     if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET)
06318     {
06319       /* Read data from DR */
06320       *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
06321 
06322       /* Increment Buffer pointer */
06323       hi2c->pBuffPtr++;
06324 
06325       /* Update counter */
06326       hi2c->XferCount--;
06327     }
06328 
06329     /* Store Last receive data if any */
06330     if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
06331     {
06332       /* Read data from DR */
06333       *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
06334 
06335       /* Increment Buffer pointer */
06336       hi2c->pBuffPtr++;
06337 
06338       /* Update counter */
06339       hi2c->XferCount--;
06340     }
06341 
06342     if (hi2c->XferCount != 0U)
06343     {
06344       /* Set ErrorCode corresponding to a Non-Acknowledge */
06345       hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
06346     }
06347   }
06348 
06349   if (hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
06350   {
06351     /* Call the corresponding callback to inform upper layer of End of Transfer */
06352     I2C_ITError(hi2c);
06353   }
06354   else
06355   {
06356     if (CurrentState == HAL_I2C_STATE_BUSY_RX_LISTEN)
06357     {
06358       /* Set state at HAL_I2C_STATE_LISTEN */
06359       hi2c->PreviousState = I2C_STATE_NONE;
06360       hi2c->State = HAL_I2C_STATE_LISTEN;
06361 
06362       /* Call the corresponding callback to inform upper layer of End of Transfer */
06363 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
06364       hi2c->SlaveRxCpltCallback(hi2c);
06365 #else
06366       HAL_I2C_SlaveRxCpltCallback(hi2c);
06367 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
06368     }
06369 
06370     if (hi2c->State == HAL_I2C_STATE_LISTEN)
06371     {
06372       hi2c->XferOptions = I2C_NO_OPTION_FRAME;
06373       hi2c->PreviousState = I2C_STATE_NONE;
06374       hi2c->State = HAL_I2C_STATE_READY;
06375       hi2c->Mode = HAL_I2C_MODE_NONE;
06376 
06377       /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
06378 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
06379       hi2c->ListenCpltCallback(hi2c);
06380 #else
06381       HAL_I2C_ListenCpltCallback(hi2c);
06382 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
06383     }
06384     else
06385     {
06386       if ((hi2c->PreviousState  == I2C_STATE_SLAVE_BUSY_RX) || (CurrentState == HAL_I2C_STATE_BUSY_RX))
06387       {
06388         hi2c->PreviousState = I2C_STATE_NONE;
06389         hi2c->State = HAL_I2C_STATE_READY;
06390         hi2c->Mode = HAL_I2C_MODE_NONE;
06391 
06392 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
06393         hi2c->SlaveRxCpltCallback(hi2c);
06394 #else
06395         HAL_I2C_SlaveRxCpltCallback(hi2c);
06396 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
06397       }
06398     }
06399   }
06400 }
06401 
06402 /**
06403   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
06404   *         the configuration information for I2C module
06405   * @retval None
06406   */
06407 static void I2C_Slave_AF(I2C_HandleTypeDef *hi2c)
06408 {
06409   /* Declaration of temporary variables to prevent undefined behavior of volatile usage */
06410   HAL_I2C_StateTypeDef CurrentState = hi2c->State;
06411   uint32_t CurrentXferOptions       = hi2c->XferOptions;
06412 
06413   if (((CurrentXferOptions ==  I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_LAST_FRAME)) && \
06414       (CurrentState == HAL_I2C_STATE_LISTEN))
06415   {
06416     hi2c->XferOptions = I2C_NO_OPTION_FRAME;
06417 
06418     /* Disable EVT, BUF and ERR interrupt */
06419     __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
06420 
06421     /* Clear AF flag */
06422     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
06423 
06424     /* Disable Acknowledge */
06425     CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
06426 
06427     hi2c->PreviousState = I2C_STATE_NONE;
06428     hi2c->State         = HAL_I2C_STATE_READY;
06429     hi2c->Mode          = HAL_I2C_MODE_NONE;
06430 
06431     /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
06432 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
06433     hi2c->ListenCpltCallback(hi2c);
06434 #else
06435     HAL_I2C_ListenCpltCallback(hi2c);
06436 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
06437   }
06438   else if (CurrentState == HAL_I2C_STATE_BUSY_TX)
06439   {
06440     hi2c->XferOptions   = I2C_NO_OPTION_FRAME;
06441     hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_TX;
06442     hi2c->State         = HAL_I2C_STATE_READY;
06443     hi2c->Mode          = HAL_I2C_MODE_NONE;
06444 
06445     /* Disable EVT, BUF and ERR interrupt */
06446     __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
06447 
06448     /* Clear AF flag */
06449     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
06450 
06451     /* Disable Acknowledge */
06452     CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
06453 
06454 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
06455     hi2c->SlaveTxCpltCallback(hi2c);
06456 #else
06457     HAL_I2C_SlaveTxCpltCallback(hi2c);
06458 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
06459   }
06460   else
06461   {
06462     /* Clear AF flag only */
06463     /* State Listen, but XferOptions == FIRST or NEXT */
06464     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
06465   }
06466 }
06467 
06468 /**
06469   * @brief  I2C interrupts error process
06470   * @param  hi2c I2C handle.
06471   * @retval None
06472   */
06473 static void I2C_ITError(I2C_HandleTypeDef *hi2c)
06474 {
06475   /* Declaration of temporary variable to prevent undefined behavior of volatile usage */
06476   HAL_I2C_StateTypeDef CurrentState = hi2c->State;
06477   HAL_I2C_ModeTypeDef CurrentMode = hi2c->Mode;
06478   uint32_t CurrentError;
06479 
06480   if (((CurrentMode == HAL_I2C_MODE_MASTER) || (CurrentMode == HAL_I2C_MODE_MEM)) && (CurrentState == HAL_I2C_STATE_BUSY_RX))
06481   {
06482     /* Disable Pos bit in I2C CR1 when error occurred in Master/Mem Receive IT Process */
06483     hi2c->Instance->CR1 &= ~I2C_CR1_POS;
06484   }
06485 
06486   if (((uint32_t)CurrentState & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
06487   {
06488     /* keep HAL_I2C_STATE_LISTEN */
06489     hi2c->PreviousState = I2C_STATE_NONE;
06490     hi2c->State = HAL_I2C_STATE_LISTEN;
06491   }
06492   else
06493   {
06494     /* If state is an abort treatment on going, don't change state */
06495     /* This change will be do later */
06496     if ((READ_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN) != I2C_CR2_DMAEN) && (CurrentState != HAL_I2C_STATE_ABORT))
06497     {
06498       hi2c->State = HAL_I2C_STATE_READY;
06499       hi2c->Mode = HAL_I2C_MODE_NONE;
06500     }
06501     hi2c->PreviousState = I2C_STATE_NONE;
06502   }
06503 
06504   /* Abort DMA transfer */
06505   if (READ_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN) == I2C_CR2_DMAEN)
06506   {
06507     hi2c->Instance->CR2 &= ~I2C_CR2_DMAEN;
06508 
06509     if (hi2c->hdmatx->State != HAL_DMA_STATE_READY)
06510     {
06511       /* Set the DMA Abort callback :
06512       will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
06513       hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort;
06514 
06515       if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK)
06516       {
06517         /* Disable I2C peripheral to prevent dummy data in buffer */
06518         __HAL_I2C_DISABLE(hi2c);
06519 
06520         hi2c->State = HAL_I2C_STATE_READY;
06521 
06522         /* Call Directly XferAbortCallback function in case of error */
06523         hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx);
06524       }
06525     }
06526     else
06527     {
06528       /* Set the DMA Abort callback :
06529       will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
06530       hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort;
06531 
06532       if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK)
06533       {
06534         /* Store Last receive data if any */
06535         if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
06536         {
06537           /* Read data from DR */
06538           *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
06539 
06540           /* Increment Buffer pointer */
06541           hi2c->pBuffPtr++;
06542         }
06543 
06544         /* Disable I2C peripheral to prevent dummy data in buffer */
06545         __HAL_I2C_DISABLE(hi2c);
06546 
06547         hi2c->State = HAL_I2C_STATE_READY;
06548 
06549         /* Call Directly hi2c->hdmarx->XferAbortCallback function in case of error */
06550         hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx);
06551       }
06552     }
06553   }
06554   else if (hi2c->State == HAL_I2C_STATE_ABORT)
06555   {
06556     hi2c->State = HAL_I2C_STATE_READY;
06557     hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
06558 
06559     /* Store Last receive data if any */
06560     if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
06561     {
06562       /* Read data from DR */
06563       *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
06564 
06565       /* Increment Buffer pointer */
06566       hi2c->pBuffPtr++;
06567     }
06568 
06569     /* Disable I2C peripheral to prevent dummy data in buffer */
06570     __HAL_I2C_DISABLE(hi2c);
06571 
06572     /* Call the corresponding callback to inform upper layer of End of Transfer */
06573 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
06574     hi2c->AbortCpltCallback(hi2c);
06575 #else
06576     HAL_I2C_AbortCpltCallback(hi2c);
06577 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
06578   }
06579   else
06580   {
06581     /* Store Last receive data if any */
06582     if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
06583     {
06584       /* Read data from DR */
06585       *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
06586 
06587       /* Increment Buffer pointer */
06588       hi2c->pBuffPtr++;
06589     }
06590 
06591     /* Call user error callback */
06592 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
06593     hi2c->ErrorCallback(hi2c);
06594 #else
06595     HAL_I2C_ErrorCallback(hi2c);
06596 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
06597   }
06598 
06599   /* STOP Flag is not set after a NACK reception, BusError, ArbitrationLost, OverRun */
06600   CurrentError = hi2c->ErrorCode;
06601 
06602   if (((CurrentError & HAL_I2C_ERROR_BERR) == HAL_I2C_ERROR_BERR) || \
06603       ((CurrentError & HAL_I2C_ERROR_ARLO) == HAL_I2C_ERROR_ARLO) || \
06604       ((CurrentError & HAL_I2C_ERROR_AF) == HAL_I2C_ERROR_AF)     || \
06605       ((CurrentError & HAL_I2C_ERROR_OVR) == HAL_I2C_ERROR_OVR))
06606   {
06607     /* Disable EVT, BUF and ERR interrupt */
06608     __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
06609   }
06610 
06611   /* So may inform upper layer that listen phase is stopped */
06612   /* during NACK error treatment */
06613   CurrentState = hi2c->State;
06614   if (((hi2c->ErrorCode & HAL_I2C_ERROR_AF) == HAL_I2C_ERROR_AF) && (CurrentState == HAL_I2C_STATE_LISTEN))
06615   {
06616     hi2c->XferOptions   = I2C_NO_OPTION_FRAME;
06617     hi2c->PreviousState = I2C_STATE_NONE;
06618     hi2c->State         = HAL_I2C_STATE_READY;
06619     hi2c->Mode          = HAL_I2C_MODE_NONE;
06620 
06621     /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
06622 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
06623     hi2c->ListenCpltCallback(hi2c);
06624 #else
06625     HAL_I2C_ListenCpltCallback(hi2c);
06626 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
06627   }
06628 }
06629 
06630 /**
06631   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
06632   *         the configuration information for I2C module
06633   * @param  DevAddress Target device address: The device 7 bits address value
06634   *         in datasheet must be shifted to the left before calling the interface
06635   * @param  Timeout Timeout duration
06636   * @param  Tickstart Tick start value
06637   * @retval HAL status
06638   */
06639 static HAL_StatusTypeDef I2C_MasterRequestWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Timeout, uint32_t Tickstart)
06640 {
06641   /* Declaration of temporary variable to prevent undefined behavior of volatile usage */
06642   uint32_t CurrentXferOptions = hi2c->XferOptions;
06643 
06644   /* Generate Start condition if first transfer */
06645   if ((CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_FIRST_FRAME) || (CurrentXferOptions == I2C_NO_OPTION_FRAME))
06646   {
06647     /* Generate Start */
06648     SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
06649   }
06650   else if (hi2c->PreviousState == I2C_STATE_MASTER_BUSY_RX)
06651   {
06652     /* Generate ReStart */
06653     SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
06654   }
06655   else
06656   {
06657     /* Do nothing */
06658   }
06659 
06660   /* Wait until SB flag is set */
06661   if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK)
06662   {
06663     if (READ_BIT(hi2c->Instance->CR1, I2C_CR1_START) == I2C_CR1_START)
06664     {
06665       hi2c->ErrorCode = HAL_I2C_WRONG_START;
06666     }
06667     return HAL_TIMEOUT;
06668   }
06669 
06670   if (hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_7BIT)
06671   {
06672     /* Send slave address */
06673     hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress);
06674   }
06675   else
06676   {
06677     /* Send header of slave address */
06678     hi2c->Instance->DR = I2C_10BIT_HEADER_WRITE(DevAddress);
06679 
06680     /* Wait until ADD10 flag is set */
06681     if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADD10, Timeout, Tickstart) != HAL_OK)
06682     {
06683       return HAL_ERROR;
06684     }
06685 
06686     /* Send slave address */
06687     hi2c->Instance->DR = I2C_10BIT_ADDRESS(DevAddress);
06688   }
06689 
06690   /* Wait until ADDR flag is set */
06691   if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK)
06692   {
06693     return HAL_ERROR;
06694   }
06695 
06696   return HAL_OK;
06697 }
06698 
06699 /**
06700   * @brief  Master sends target device address for read request.
06701   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
06702   *         the configuration information for I2C module
06703   * @param  DevAddress Target device address: The device 7 bits address value
06704   *         in datasheet must be shifted to the left before calling the interface
06705   * @param  Timeout Timeout duration
06706   * @param  Tickstart Tick start value
06707   * @retval HAL status
06708   */
06709 static HAL_StatusTypeDef I2C_MasterRequestRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Timeout, uint32_t Tickstart)
06710 {
06711   /* Declaration of temporary variable to prevent undefined behavior of volatile usage */
06712   uint32_t CurrentXferOptions = hi2c->XferOptions;
06713 
06714   /* Enable Acknowledge */
06715   SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
06716 
06717   /* Generate Start condition if first transfer */
06718   if ((CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_FIRST_FRAME)  || (CurrentXferOptions == I2C_NO_OPTION_FRAME))
06719   {
06720     /* Generate Start */
06721     SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
06722   }
06723   else if (hi2c->PreviousState == I2C_STATE_MASTER_BUSY_TX)
06724   {
06725     /* Generate ReStart */
06726     SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
06727   }
06728   else
06729   {
06730     /* Do nothing */
06731   }
06732 
06733   /* Wait until SB flag is set */
06734   if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK)
06735   {
06736     if (READ_BIT(hi2c->Instance->CR1, I2C_CR1_START) == I2C_CR1_START)
06737     {
06738       hi2c->ErrorCode = HAL_I2C_WRONG_START;
06739     }
06740     return HAL_TIMEOUT;
06741   }
06742 
06743   if (hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_7BIT)
06744   {
06745     /* Send slave address */
06746     hi2c->Instance->DR = I2C_7BIT_ADD_READ(DevAddress);
06747   }
06748   else
06749   {
06750     /* Send header of slave address */
06751     hi2c->Instance->DR = I2C_10BIT_HEADER_WRITE(DevAddress);
06752 
06753     /* Wait until ADD10 flag is set */
06754     if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADD10, Timeout, Tickstart) != HAL_OK)
06755     {
06756       return HAL_ERROR;
06757     }
06758 
06759     /* Send slave address */
06760     hi2c->Instance->DR = I2C_10BIT_ADDRESS(DevAddress);
06761 
06762     /* Wait until ADDR flag is set */
06763     if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK)
06764     {
06765       return HAL_ERROR;
06766     }
06767 
06768     /* Clear ADDR flag */
06769     __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
06770 
06771     /* Generate Restart */
06772     SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
06773 
06774     /* Wait until SB flag is set */
06775     if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK)
06776     {
06777       if (READ_BIT(hi2c->Instance->CR1, I2C_CR1_START) == I2C_CR1_START)
06778       {
06779         hi2c->ErrorCode = HAL_I2C_WRONG_START;
06780       }
06781       return HAL_TIMEOUT;
06782     }
06783 
06784     /* Send header of slave address */
06785     hi2c->Instance->DR = I2C_10BIT_HEADER_READ(DevAddress);
06786   }
06787 
06788   /* Wait until ADDR flag is set */
06789   if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK)
06790   {
06791     return HAL_ERROR;
06792   }
06793 
06794   return HAL_OK;
06795 }
06796 
06797 /**
06798   * @brief  Master sends target device address followed by internal memory address for write request.
06799   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
06800   *         the configuration information for I2C module
06801   * @param  DevAddress Target device address: The device 7 bits address value
06802   *         in datasheet must be shifted to the left before calling the interface
06803   * @param  MemAddress Internal memory address
06804   * @param  MemAddSize Size of internal memory address
06805   * @param  Timeout Timeout duration
06806   * @param  Tickstart Tick start value
06807   * @retval HAL status
06808   */
06809 static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart)
06810 {
06811   /* Generate Start */
06812   SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
06813 
06814   /* Wait until SB flag is set */
06815   if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK)
06816   {
06817     if (READ_BIT(hi2c->Instance->CR1, I2C_CR1_START) == I2C_CR1_START)
06818     {
06819       hi2c->ErrorCode = HAL_I2C_WRONG_START;
06820     }
06821     return HAL_TIMEOUT;
06822   }
06823 
06824   /* Send slave address */
06825   hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress);
06826 
06827   /* Wait until ADDR flag is set */
06828   if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK)
06829   {
06830     return HAL_ERROR;
06831   }
06832 
06833   /* Clear ADDR flag */
06834   __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
06835 
06836   /* Wait until TXE flag is set */
06837   if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
06838   {
06839     if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
06840     {
06841       /* Generate Stop */
06842       SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
06843     }
06844     return HAL_ERROR;
06845   }
06846 
06847   /* If Memory address size is 8Bit */
06848   if (MemAddSize == I2C_MEMADD_SIZE_8BIT)
06849   {
06850     /* Send Memory Address */
06851     hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
06852   }
06853   /* If Memory address size is 16Bit */
06854   else
06855   {
06856     /* Send MSB of Memory Address */
06857     hi2c->Instance->DR = I2C_MEM_ADD_MSB(MemAddress);
06858 
06859     /* Wait until TXE flag is set */
06860     if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
06861     {
06862       if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
06863       {
06864         /* Generate Stop */
06865         SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
06866       }
06867       return HAL_ERROR;
06868     }
06869 
06870     /* Send LSB of Memory Address */
06871     hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
06872   }
06873 
06874   return HAL_OK;
06875 }
06876 
06877 /**
06878   * @brief  Master sends target device address followed by internal memory address for read request.
06879   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
06880   *         the configuration information for I2C module
06881   * @param  DevAddress Target device address: The device 7 bits address value
06882   *         in datasheet must be shifted to the left before calling the interface
06883   * @param  MemAddress Internal memory address
06884   * @param  MemAddSize Size of internal memory address
06885   * @param  Timeout Timeout duration
06886   * @param  Tickstart Tick start value
06887   * @retval HAL status
06888   */
06889 static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart)
06890 {
06891   /* Enable Acknowledge */
06892   SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
06893 
06894   /* Generate Start */
06895   SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
06896 
06897   /* Wait until SB flag is set */
06898   if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK)
06899   {
06900     if (READ_BIT(hi2c->Instance->CR1, I2C_CR1_START) == I2C_CR1_START)
06901     {
06902       hi2c->ErrorCode = HAL_I2C_WRONG_START;
06903     }
06904     return HAL_TIMEOUT;
06905   }
06906 
06907   /* Send slave address */
06908   hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress);
06909 
06910   /* Wait until ADDR flag is set */
06911   if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK)
06912   {
06913     return HAL_ERROR;
06914   }
06915 
06916   /* Clear ADDR flag */
06917   __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
06918 
06919   /* Wait until TXE flag is set */
06920   if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
06921   {
06922     if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
06923     {
06924       /* Generate Stop */
06925       SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
06926     }
06927     return HAL_ERROR;
06928   }
06929 
06930   /* If Memory address size is 8Bit */
06931   if (MemAddSize == I2C_MEMADD_SIZE_8BIT)
06932   {
06933     /* Send Memory Address */
06934     hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
06935   }
06936   /* If Memory address size is 16Bit */
06937   else
06938   {
06939     /* Send MSB of Memory Address */
06940     hi2c->Instance->DR = I2C_MEM_ADD_MSB(MemAddress);
06941 
06942     /* Wait until TXE flag is set */
06943     if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
06944     {
06945       if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
06946       {
06947         /* Generate Stop */
06948         SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
06949       }
06950       return HAL_ERROR;
06951     }
06952 
06953     /* Send LSB of Memory Address */
06954     hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
06955   }
06956 
06957   /* Wait until TXE flag is set */
06958   if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
06959   {
06960     if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
06961     {
06962       /* Generate Stop */
06963       SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
06964     }
06965     return HAL_ERROR;
06966   }
06967 
06968   /* Generate Restart */
06969   SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
06970 
06971   /* Wait until SB flag is set */
06972   if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK)
06973   {
06974     if (READ_BIT(hi2c->Instance->CR1, I2C_CR1_START) == I2C_CR1_START)
06975     {
06976       hi2c->ErrorCode = HAL_I2C_WRONG_START;
06977     }
06978     return HAL_TIMEOUT;
06979   }
06980 
06981   /* Send slave address */
06982   hi2c->Instance->DR = I2C_7BIT_ADD_READ(DevAddress);
06983 
06984   /* Wait until ADDR flag is set */
06985   if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK)
06986   {
06987     return HAL_ERROR;
06988   }
06989 
06990   return HAL_OK;
06991 }
06992 
06993 /**
06994   * @brief  DMA I2C process complete callback.
06995   * @param  hdma DMA handle
06996   * @retval None
06997   */
06998 static void I2C_DMAXferCplt(DMA_HandleTypeDef *hdma)
06999 {
07000   I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; /* Derogation MISRAC2012-Rule-11.5 */
07001 
07002   /* Declaration of temporary variable to prevent undefined behavior of volatile usage */
07003   HAL_I2C_StateTypeDef CurrentState = hi2c->State;
07004   HAL_I2C_ModeTypeDef CurrentMode   = hi2c->Mode;
07005   uint32_t CurrentXferOptions       = hi2c->XferOptions;
07006 
07007   /* Disable EVT and ERR interrupt */
07008   __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
07009 
07010   /* Clear Complete callback */
07011   if (hi2c->hdmatx != NULL)
07012   {
07013     hi2c->hdmatx->XferCpltCallback = NULL;
07014   }
07015   if (hi2c->hdmarx != NULL)
07016   {
07017     hi2c->hdmarx->XferCpltCallback = NULL;
07018   }
07019 
07020   if ((((uint32_t)CurrentState & (uint32_t)HAL_I2C_STATE_BUSY_TX) == (uint32_t)HAL_I2C_STATE_BUSY_TX) || ((((uint32_t)CurrentState & (uint32_t)HAL_I2C_STATE_BUSY_RX) == (uint32_t)HAL_I2C_STATE_BUSY_RX) && (CurrentMode == HAL_I2C_MODE_SLAVE)))
07021   {
07022     /* Disable DMA Request */
07023     CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
07024 
07025     hi2c->XferCount = 0U;
07026 
07027     if (CurrentState == HAL_I2C_STATE_BUSY_TX_LISTEN)
07028     {
07029       /* Set state at HAL_I2C_STATE_LISTEN */
07030       hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_TX;
07031       hi2c->State = HAL_I2C_STATE_LISTEN;
07032 
07033       /* Call the corresponding callback to inform upper layer of End of Transfer */
07034 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
07035       hi2c->SlaveTxCpltCallback(hi2c);
07036 #else
07037       HAL_I2C_SlaveTxCpltCallback(hi2c);
07038 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
07039     }
07040     else if (CurrentState == HAL_I2C_STATE_BUSY_RX_LISTEN)
07041     {
07042       /* Set state at HAL_I2C_STATE_LISTEN */
07043       hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_RX;
07044       hi2c->State = HAL_I2C_STATE_LISTEN;
07045 
07046       /* Call the corresponding callback to inform upper layer of End of Transfer */
07047 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
07048       hi2c->SlaveRxCpltCallback(hi2c);
07049 #else
07050       HAL_I2C_SlaveRxCpltCallback(hi2c);
07051 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
07052     }
07053     else
07054     {
07055       /* Do nothing */
07056     }
07057 
07058     /* Enable EVT and ERR interrupt to treat end of transfer in IRQ handler */
07059     __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
07060   }
07061   /* Check current Mode, in case of treatment DMA handler have been preempted by a prior interrupt */
07062   else if (hi2c->Mode != HAL_I2C_MODE_NONE)
07063   {
07064     if (hi2c->XferCount == (uint16_t)1)
07065     {
07066       /* Disable Acknowledge */
07067       CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
07068     }
07069 
07070     /* Disable EVT and ERR interrupt */
07071     __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
07072 
07073     /* Prepare next transfer or stop current transfer */
07074     if ((CurrentXferOptions == I2C_NO_OPTION_FRAME) || (CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_OTHER_AND_LAST_FRAME) || (CurrentXferOptions == I2C_LAST_FRAME))
07075     {
07076       /* Generate Stop */
07077       SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
07078     }
07079 
07080     /* Disable Last DMA */
07081     CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
07082 
07083     /* Disable DMA Request */
07084     CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
07085 
07086     hi2c->XferCount = 0U;
07087 
07088     /* Check if Errors has been detected during transfer */
07089     if (hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
07090     {
07091 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
07092       hi2c->ErrorCallback(hi2c);
07093 #else
07094       HAL_I2C_ErrorCallback(hi2c);
07095 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
07096     }
07097     else
07098     {
07099       hi2c->State = HAL_I2C_STATE_READY;
07100 
07101       if (hi2c->Mode == HAL_I2C_MODE_MEM)
07102       {
07103         hi2c->Mode = HAL_I2C_MODE_NONE;
07104         hi2c->PreviousState = I2C_STATE_NONE;
07105 
07106 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
07107         hi2c->MemRxCpltCallback(hi2c);
07108 #else
07109         HAL_I2C_MemRxCpltCallback(hi2c);
07110 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
07111       }
07112       else
07113       {
07114         hi2c->Mode = HAL_I2C_MODE_NONE;
07115         hi2c->PreviousState = I2C_STATE_MASTER_BUSY_RX;
07116 
07117 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
07118         hi2c->MasterRxCpltCallback(hi2c);
07119 #else
07120         HAL_I2C_MasterRxCpltCallback(hi2c);
07121 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
07122       }
07123     }
07124   }
07125   else
07126   {
07127     /* Do nothing */
07128   }
07129 }
07130 
07131 /**
07132   * @brief  DMA I2C communication error callback.
07133   * @param  hdma DMA handle
07134   * @retval None
07135   */
07136 static void I2C_DMAError(DMA_HandleTypeDef *hdma)
07137 {
07138   I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; /* Derogation MISRAC2012-Rule-11.5 */
07139 
07140   /* Clear Complete callback */
07141   if (hi2c->hdmatx != NULL)
07142   {
07143     hi2c->hdmatx->XferCpltCallback = NULL;
07144   }
07145   if (hi2c->hdmarx != NULL)
07146   {
07147     hi2c->hdmarx->XferCpltCallback = NULL;
07148   }
07149 
07150   /* Disable Acknowledge */
07151   CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
07152 
07153   hi2c->XferCount       = 0U;
07154   hi2c->State           = HAL_I2C_STATE_READY;
07155   hi2c->Mode            = HAL_I2C_MODE_NONE;
07156   hi2c->ErrorCode       |= HAL_I2C_ERROR_DMA;
07157 
07158 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
07159   hi2c->ErrorCallback(hi2c);
07160 #else
07161   HAL_I2C_ErrorCallback(hi2c);
07162 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
07163 }
07164 
07165 /**
07166   * @brief DMA I2C communication abort callback
07167   *        (To be called at end of DMA Abort procedure).
07168   * @param hdma DMA handle.
07169   * @retval None
07170   */
07171 static void I2C_DMAAbort(DMA_HandleTypeDef *hdma)
07172 {
07173   __IO uint32_t count = 0U;
07174   I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; /* Derogation MISRAC2012-Rule-11.5 */
07175 
07176   /* Declaration of temporary variable to prevent undefined behavior of volatile usage */
07177   HAL_I2C_StateTypeDef CurrentState = hi2c->State;
07178 
07179   /* During abort treatment, check that there is no pending STOP request */
07180   /* Wait until STOP flag is reset */
07181   count = I2C_TIMEOUT_FLAG * (SystemCoreClock / 25U / 1000U);
07182   do
07183   {
07184     if (count == 0U)
07185     {
07186       hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
07187       break;
07188     }
07189     count--;
07190   }
07191   while (READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP);
07192 
07193   /* Clear Complete callback */
07194   if (hi2c->hdmatx != NULL)
07195   {
07196     hi2c->hdmatx->XferCpltCallback = NULL;
07197   }
07198   if (hi2c->hdmarx != NULL)
07199   {
07200     hi2c->hdmarx->XferCpltCallback = NULL;
07201   }
07202 
07203   /* Disable Acknowledge */
07204   CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
07205 
07206   hi2c->XferCount = 0U;
07207 
07208   /* Reset XferAbortCallback */
07209   if (hi2c->hdmatx != NULL)
07210   {
07211     hi2c->hdmatx->XferAbortCallback = NULL;
07212   }
07213   if (hi2c->hdmarx != NULL)
07214   {
07215     hi2c->hdmarx->XferAbortCallback = NULL;
07216   }
07217 
07218   /* Disable I2C peripheral to prevent dummy data in buffer */
07219   __HAL_I2C_DISABLE(hi2c);
07220 
07221   /* Check if come from abort from user */
07222   if (hi2c->State == HAL_I2C_STATE_ABORT)
07223   {
07224     hi2c->State         = HAL_I2C_STATE_READY;
07225     hi2c->Mode          = HAL_I2C_MODE_NONE;
07226     hi2c->ErrorCode     = HAL_I2C_ERROR_NONE;
07227 
07228     /* Call the corresponding callback to inform upper layer of End of Transfer */
07229 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
07230     hi2c->AbortCpltCallback(hi2c);
07231 #else
07232     HAL_I2C_AbortCpltCallback(hi2c);
07233 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
07234   }
07235   else
07236   {
07237     if (((uint32_t)CurrentState & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
07238     {
07239       /* Renable I2C peripheral */
07240       __HAL_I2C_ENABLE(hi2c);
07241 
07242       /* Enable Acknowledge */
07243       SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
07244 
07245       /* keep HAL_I2C_STATE_LISTEN */
07246       hi2c->PreviousState = I2C_STATE_NONE;
07247       hi2c->State = HAL_I2C_STATE_LISTEN;
07248     }
07249     else
07250     {
07251       hi2c->State = HAL_I2C_STATE_READY;
07252       hi2c->Mode = HAL_I2C_MODE_NONE;
07253     }
07254 
07255     /* Call the corresponding callback to inform upper layer of End of Transfer */
07256 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
07257     hi2c->ErrorCallback(hi2c);
07258 #else
07259     HAL_I2C_ErrorCallback(hi2c);
07260 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
07261   }
07262 }
07263 
07264 /**
07265   * @brief  This function handles I2C Communication Timeout.
07266   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
07267   *         the configuration information for I2C module
07268   * @param  Flag specifies the I2C flag to check.
07269   * @param  Status The new Flag status (SET or RESET).
07270   * @param  Timeout Timeout duration
07271   * @param  Tickstart Tick start value
07272   * @retval HAL status
07273   */
07274 static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout, uint32_t Tickstart)
07275 {
07276   /* Wait until flag is set */
07277   while (__HAL_I2C_GET_FLAG(hi2c, Flag) == Status)
07278   {
07279     /* Check for the Timeout */
07280     if (Timeout != HAL_MAX_DELAY)
07281     {
07282       if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
07283       {
07284         hi2c->PreviousState     = I2C_STATE_NONE;
07285         hi2c->State             = HAL_I2C_STATE_READY;
07286         hi2c->Mode              = HAL_I2C_MODE_NONE;
07287         hi2c->ErrorCode         |= HAL_I2C_ERROR_TIMEOUT;
07288 
07289         /* Process Unlocked */
07290         __HAL_UNLOCK(hi2c);
07291 
07292         return HAL_ERROR;
07293       }
07294     }
07295   }
07296   return HAL_OK;
07297 }
07298 
07299 /**
07300   * @brief  This function handles I2C Communication Timeout for Master addressing phase.
07301   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
07302   *         the configuration information for I2C module
07303   * @param  Flag specifies the I2C flag to check.
07304   * @param  Timeout Timeout duration
07305   * @param  Tickstart Tick start value
07306   * @retval HAL status
07307   */
07308 static HAL_StatusTypeDef I2C_WaitOnMasterAddressFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, uint32_t Timeout, uint32_t Tickstart)
07309 {
07310   while (__HAL_I2C_GET_FLAG(hi2c, Flag) == RESET)
07311   {
07312     if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
07313     {
07314       /* Generate Stop */
07315       SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
07316 
07317       /* Clear AF Flag */
07318       __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
07319 
07320       hi2c->PreviousState       = I2C_STATE_NONE;
07321       hi2c->State               = HAL_I2C_STATE_READY;
07322       hi2c->Mode                = HAL_I2C_MODE_NONE;
07323       hi2c->ErrorCode           |= HAL_I2C_ERROR_AF;
07324 
07325       /* Process Unlocked */
07326       __HAL_UNLOCK(hi2c);
07327 
07328       return HAL_ERROR;
07329     }
07330 
07331     /* Check for the Timeout */
07332     if (Timeout != HAL_MAX_DELAY)
07333     {
07334       if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
07335       {
07336         hi2c->PreviousState       = I2C_STATE_NONE;
07337         hi2c->State               = HAL_I2C_STATE_READY;
07338         hi2c->Mode                = HAL_I2C_MODE_NONE;
07339         hi2c->ErrorCode           |= HAL_I2C_ERROR_TIMEOUT;
07340 
07341         /* Process Unlocked */
07342         __HAL_UNLOCK(hi2c);
07343 
07344         return HAL_ERROR;
07345       }
07346     }
07347   }
07348   return HAL_OK;
07349 }
07350 
07351 /**
07352   * @brief  This function handles I2C Communication Timeout for specific usage of TXE flag.
07353   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
07354   *                the configuration information for the specified I2C.
07355   * @param  Timeout Timeout duration
07356   * @param  Tickstart Tick start value
07357   * @retval HAL status
07358   */
07359 static HAL_StatusTypeDef I2C_WaitOnTXEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart)
07360 {
07361   while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXE) == RESET)
07362   {
07363     /* Check if a NACK is detected */
07364     if (I2C_IsAcknowledgeFailed(hi2c) != HAL_OK)
07365     {
07366       return HAL_ERROR;
07367     }
07368 
07369     /* Check for the Timeout */
07370     if (Timeout != HAL_MAX_DELAY)
07371     {
07372       if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
07373       {
07374         hi2c->PreviousState       = I2C_STATE_NONE;
07375         hi2c->State               = HAL_I2C_STATE_READY;
07376         hi2c->Mode                = HAL_I2C_MODE_NONE;
07377         hi2c->ErrorCode           |= HAL_I2C_ERROR_TIMEOUT;
07378 
07379         /* Process Unlocked */
07380         __HAL_UNLOCK(hi2c);
07381 
07382         return HAL_ERROR;
07383       }
07384     }
07385   }
07386   return HAL_OK;
07387 }
07388 
07389 /**
07390   * @brief  This function handles I2C Communication Timeout for specific usage of BTF flag.
07391   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
07392   *                the configuration information for the specified I2C.
07393   * @param  Timeout Timeout duration
07394   * @param  Tickstart Tick start value
07395   * @retval HAL status
07396   */
07397 static HAL_StatusTypeDef I2C_WaitOnBTFFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart)
07398 {
07399   while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == RESET)
07400   {
07401     /* Check if a NACK is detected */
07402     if (I2C_IsAcknowledgeFailed(hi2c) != HAL_OK)
07403     {
07404       return HAL_ERROR;
07405     }
07406 
07407     /* Check for the Timeout */
07408     if (Timeout != HAL_MAX_DELAY)
07409     {
07410       if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
07411       {
07412         hi2c->PreviousState       = I2C_STATE_NONE;
07413         hi2c->State               = HAL_I2C_STATE_READY;
07414         hi2c->Mode                = HAL_I2C_MODE_NONE;
07415         hi2c->ErrorCode           |= HAL_I2C_ERROR_TIMEOUT;
07416 
07417         /* Process Unlocked */
07418         __HAL_UNLOCK(hi2c);
07419 
07420         return HAL_ERROR;
07421       }
07422     }
07423   }
07424   return HAL_OK;
07425 }
07426 
07427 /**
07428   * @brief  This function handles I2C Communication Timeout for specific usage of STOP flag.
07429   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
07430   *                the configuration information for the specified I2C.
07431   * @param  Timeout Timeout duration
07432   * @param  Tickstart Tick start value
07433   * @retval HAL status
07434   */
07435 static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart)
07436 {
07437   while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET)
07438   {
07439     /* Check if a NACK is detected */
07440     if (I2C_IsAcknowledgeFailed(hi2c) != HAL_OK)
07441     {
07442       return HAL_ERROR;
07443     }
07444 
07445     /* Check for the Timeout */
07446     if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
07447     {
07448       hi2c->PreviousState       = I2C_STATE_NONE;
07449       hi2c->State               = HAL_I2C_STATE_READY;
07450       hi2c->Mode                = HAL_I2C_MODE_NONE;
07451       hi2c->ErrorCode           |= HAL_I2C_ERROR_TIMEOUT;
07452 
07453       /* Process Unlocked */
07454       __HAL_UNLOCK(hi2c);
07455 
07456       return HAL_ERROR;
07457     }
07458   }
07459   return HAL_OK;
07460 }
07461 
07462 /**
07463   * @brief  This function handles I2C Communication Timeout for specific usage of STOP request through Interrupt.
07464   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
07465   *                the configuration information for the specified I2C.
07466   * @retval HAL status
07467   */
07468 static HAL_StatusTypeDef I2C_WaitOnSTOPRequestThroughIT(I2C_HandleTypeDef *hi2c)
07469 {
07470   __IO uint32_t count = 0U;
07471 
07472   /* Wait until STOP flag is reset */
07473   count = I2C_TIMEOUT_STOP_FLAG * (SystemCoreClock / 25U / 1000U);
07474   do
07475   {
07476     count--;
07477     if (count == 0U)
07478     {
07479       hi2c->ErrorCode           |= HAL_I2C_ERROR_TIMEOUT;
07480 
07481       return HAL_ERROR;
07482     }
07483   }
07484   while (READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP);
07485 
07486   return HAL_OK;
07487 }
07488 
07489 /**
07490   * @brief  This function handles I2C Communication Timeout for specific usage of RXNE flag.
07491   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
07492   *                the configuration information for the specified I2C.
07493   * @param  Timeout Timeout duration
07494   * @param  Tickstart Tick start value
07495   * @retval HAL status
07496   */
07497 static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart)
07498 {
07499 
07500   while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == RESET)
07501   {
07502     /* Check if a STOPF is detected */
07503     if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
07504     {
07505       /* Clear STOP Flag */
07506       __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
07507 
07508       hi2c->PreviousState       = I2C_STATE_NONE;
07509       hi2c->State               = HAL_I2C_STATE_READY;
07510       hi2c->Mode                = HAL_I2C_MODE_NONE;
07511       hi2c->ErrorCode           |= HAL_I2C_ERROR_NONE;
07512 
07513       /* Process Unlocked */
07514       __HAL_UNLOCK(hi2c);
07515 
07516       return HAL_ERROR;
07517     }
07518 
07519     /* Check for the Timeout */
07520     if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
07521     {
07522       hi2c->PreviousState       = I2C_STATE_NONE;
07523       hi2c->State               = HAL_I2C_STATE_READY;
07524       hi2c->Mode                = HAL_I2C_MODE_NONE;
07525       hi2c->ErrorCode           |= HAL_I2C_ERROR_TIMEOUT;
07526 
07527       /* Process Unlocked */
07528       __HAL_UNLOCK(hi2c);
07529 
07530       return HAL_ERROR;
07531     }
07532   }
07533   return HAL_OK;
07534 }
07535 
07536 /**
07537   * @brief  This function handles Acknowledge failed detection during an I2C Communication.
07538   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
07539   *                the configuration information for the specified I2C.
07540   * @retval HAL status
07541   */
07542 static HAL_StatusTypeDef I2C_IsAcknowledgeFailed(I2C_HandleTypeDef *hi2c)
07543 {
07544   if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
07545   {
07546     /* Clear NACKF Flag */
07547     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
07548 
07549     hi2c->PreviousState       = I2C_STATE_NONE;
07550     hi2c->State               = HAL_I2C_STATE_READY;
07551     hi2c->Mode                = HAL_I2C_MODE_NONE;
07552     hi2c->ErrorCode           |= HAL_I2C_ERROR_AF;
07553 
07554     /* Process Unlocked */
07555     __HAL_UNLOCK(hi2c);
07556 
07557     return HAL_ERROR;
07558   }
07559   return HAL_OK;
07560 }
07561 
07562 /**
07563   * @brief  Convert I2Cx OTHER_xxx XferOptions to functional XferOptions.
07564   * @param  hi2c I2C handle.
07565   * @retval None
07566   */
07567 static void I2C_ConvertOtherXferOptions(I2C_HandleTypeDef *hi2c)
07568 {
07569   /* if user set XferOptions to I2C_OTHER_FRAME            */
07570   /* it request implicitly to generate a restart condition */
07571   /* set XferOptions to I2C_FIRST_FRAME                    */
07572   if (hi2c->XferOptions == I2C_OTHER_FRAME)
07573   {
07574     hi2c->XferOptions = I2C_FIRST_FRAME;
07575   }
07576   /* else if user set XferOptions to I2C_OTHER_AND_LAST_FRAME */
07577   /* it request implicitly to generate a restart condition    */
07578   /* then generate a stop condition at the end of transfer    */
07579   /* set XferOptions to I2C_FIRST_AND_LAST_FRAME              */
07580   else if (hi2c->XferOptions == I2C_OTHER_AND_LAST_FRAME)
07581   {
07582     hi2c->XferOptions = I2C_FIRST_AND_LAST_FRAME;
07583   }
07584   else
07585   {
07586     /* Nothing to do */
07587   }
07588 }
07589 
07590 /**
07591   * @}
07592   */
07593 
07594 #endif /* HAL_I2C_MODULE_ENABLED */
07595 /**
07596   * @}
07597   */
07598 
07599 /**
07600   * @}
07601   */
07602 
07603 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/