搜索
bottom↓
回复: 6

STM32F030 HAL库I2C Slave问题

[复制链接]

出0入0汤圆

发表于 2016-3-6 16:03:08 | 显示全部楼层 |阅读模式
想用一片STM32F030来模拟AT24C02
支持I2C Host读取256个字节
可以不用支持I2C host的写操作

使用中断来实现
主机一次读取16个字节,循环读取16次

现在的问题是主机读取第一个16字节没问题
读取第二个16字节就开始有问题了.

请高手帮忙支招,万分感谢









"stm32f0xx_it.c

  1. /**
  2. * @brief This function handles I2C1 global interrupt.
  3. */
  4. void I2C1_IRQHandler(void)
  5. {
  6.   /* USER CODE BEGIN I2C1_IRQn 0 */

  7.   /* USER CODE END I2C1_IRQn 0 */
  8.   if (hi2c1.Instance->ISR & (I2C_FLAG_BERR | I2C_FLAG_ARLO | I2C_FLAG_OVR))
  9.         {
  10.     HAL_I2C_ER_IRQHandler(&hi2c1);
  11.   }
  12.         else
  13.         {
  14.     HAL_I2C_EV_IRQHandler(&hi2c1);
  15.   }
  16.   /* USER CODE BEGIN I2C1_IRQn 1 */

  17.   /* USER CODE END I2C1_IRQn 1 */
  18. }
复制代码






  1. /**
  2.   ******************************************************************************
  3.   * @file    stm32f0xx_hal_i2c.c
  4.   * @author  MCD Application Team
  5.   * @version V1.3.1
  6.   * @date    29-January-2016
  7.   * @brief   I2C HAL module driver.
  8.   *          This file provides firmware functions to manage the following
  9.   *          functionalities of the Inter Integrated Circuit (I2C) peripheral:
  10.   *           + Initialization and de-initialization functions
  11.   *           + IO operation functions
  12.   *           + Peripheral State and Errors functions
  13.   *         
  14.   @verbatim
  15.   ==============================================================================
  16.                         ##### How to use this driver #####
  17.   ==============================================================================
  18.     [..]
  19.     The I2C HAL driver can be used as follows:
  20.    
  21.     (#) Declare a I2C_HandleTypeDef handle structure, for example:
  22.         I2C_HandleTypeDef  hi2c;

  23.     (#)Initialize the I2C low level resources by implementing the HAL_I2C_MspInit() API:
  24.         (##) Enable the I2Cx interface clock
  25.         (##) I2C pins configuration
  26.             (+++) Enable the clock for the I2C GPIOs
  27.             (+++) Configure I2C pins as alternate function open-drain
  28.         (##) NVIC configuration if you need to use interrupt process
  29.             (+++) Configure the I2Cx interrupt priority
  30.             (+++) Enable the NVIC I2C IRQ Channel
  31.         (##) DMA Configuration if you need to use DMA process
  32.             (+++) Declare a DMA_HandleTypeDef handle structure for the transmit or receive channel
  33.             (+++) Enable the DMAx interface clock using
  34.             (+++) Configure the DMA handle parameters
  35.             (+++) Configure the DMA Tx or Rx channel
  36.             (+++) Associate the initialized DMA handle to the hi2c DMA Tx or Rx handle
  37.             (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on
  38.                   the DMA Tx or Rx channel

  39.     (#) Configure the Communication Clock Timing, Own Address1, Master Addressing mode, Dual Addressing mode,
  40.         Own Address2, Own Address2 Mask, General call and Nostretch mode in the hi2c Init structure.

  41.     (#) Initialize the I2C registers by calling the HAL_I2C_Init(), configures also the low level Hardware
  42.         (GPIO, CLOCK, NVIC...etc) by calling the customized HAL_I2C_MspInit(&hi2c) API.

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

  44.     (#) For I2C IO and IO MEM operations, three operation modes are available within this driver :

  45.     *** Polling mode IO operation ***
  46.     =================================
  47.     [..]
  48.       (+) Transmit in master mode an amount of data in blocking mode using HAL_I2C_Master_Transmit()
  49.       (+) Receive in master mode an amount of data in blocking mode using HAL_I2C_Master_Receive()
  50.       (+) Transmit in slave mode an amount of data in blocking mode using HAL_I2C_Slave_Transmit()
  51.       (+) Receive in slave mode an amount of data in blocking mode using HAL_I2C_Slave_Receive()

  52.     *** Polling mode IO MEM operation ***
  53.     =====================================
  54.     [..]
  55.       (+) Write an amount of data in blocking mode to a specific memory address using HAL_I2C_Mem_Write()
  56.       (+) Read an amount of data in blocking mode from a specific memory address using HAL_I2C_Mem_Read()


  57.     *** Interrupt mode IO operation ***
  58.     ===================================
  59.     [..]
  60.       (+) Transmit in master mode an amount of data in non-blocking mode using HAL_I2C_Master_Transmit_IT()
  61.       (+) At transmission end of transfer, HAL_I2C_MasterTxCpltCallback() is executed and user can
  62.            add his own code by customization of function pointer HAL_I2C_MasterTxCpltCallback()
  63.       (+) Receive in master mode an amount of data in non-blocking mode using HAL_I2C_Master_Receive_IT()
  64.       (+) At reception end of transfer, HAL_I2C_MasterRxCpltCallback() is executed and user can
  65.            add his own code by customization of function pointer HAL_I2C_MasterRxCpltCallback()
  66.       (+) Transmit in slave mode an amount of data in non-blocking mode using HAL_I2C_Slave_Transmit_IT()
  67.       (+) At transmission end of transfer, HAL_I2C_SlaveTxCpltCallback() is executed and user can
  68.            add his own code by customization of function pointer HAL_I2C_SlaveTxCpltCallback()
  69.       (+) Receive in slave mode an amount of data in non-blocking mode using HAL_I2C_Slave_Receive_IT()
  70.       (+) At reception end of transfer, HAL_I2C_SlaveRxCpltCallback() is executed and user can
  71.            add his own code by customization of function pointer HAL_I2C_SlaveRxCpltCallback()
  72.       (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
  73.            add his own code by customization of function pointer HAL_I2C_ErrorCallback()

  74.     *** Interrupt mode IO MEM operation ***
  75.     =======================================
  76.     [..]
  77.       (+) Write an amount of data in non-blocking mode with Interrupt to a specific memory address using
  78.           HAL_I2C_Mem_Write_IT()
  79.       (+) At Memory end of write transfer, HAL_I2C_MemTxCpltCallback() is executed and user can
  80.            add his own code by customization of function pointer HAL_I2C_MemTxCpltCallback()
  81.       (+) Read an amount of data in non-blocking mode with Interrupt from a specific memory address using
  82.           HAL_I2C_Mem_Read_IT()
  83.       (+) At Memory end of read transfer, HAL_I2C_MemRxCpltCallback() is executed and user can
  84.            add his own code by customization of function pointer HAL_I2C_MemRxCpltCallback()
  85.       (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
  86.            add his own code by customization of function pointer HAL_I2C_ErrorCallback()

  87.     *** DMA mode IO operation ***
  88.     ==============================
  89.     [..]
  90.       (+) Transmit in master mode an amount of data in non-blocking mode (DMA) using
  91.           HAL_I2C_Master_Transmit_DMA()
  92.       (+) At transmission end of transfer, HAL_I2C_MasterTxCpltCallback() is executed and user can
  93.            add his own code by customization of function pointer HAL_I2C_MasterTxCpltCallback()
  94.       (+) Receive in master mode an amount of data in non-blocking mode (DMA) using
  95.           HAL_I2C_Master_Receive_DMA()
  96.       (+) At reception end of transfer, HAL_I2C_MasterRxCpltCallback() is executed and user can
  97.            add his own code by customization of function pointer HAL_I2C_MasterRxCpltCallback()
  98.       (+) Transmit in slave mode an amount of data in non-blocking mode (DMA) using
  99.           HAL_I2C_Slave_Transmit_DMA()
  100.       (+) At transmission end of transfer, HAL_I2C_SlaveTxCpltCallback() is executed and user can
  101.            add his own code by customization of function pointer HAL_I2C_SlaveTxCpltCallback()
  102.       (+) Receive in slave mode an amount of data in non-blocking mode (DMA) using
  103.           HAL_I2C_Slave_Receive_DMA()
  104.       (+) At reception end of transfer, HAL_I2C_SlaveRxCpltCallback() is executed and user can
  105.            add his own code by customization of function pointer HAL_I2C_SlaveRxCpltCallback()
  106.       (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
  107.            add his own code by customization of function pointer HAL_I2C_ErrorCallback()

  108.     *** DMA mode IO MEM operation ***
  109.     =================================
  110.     [..]
  111.       (+) Write an amount of data in non-blocking mode with DMA to a specific memory address using
  112.           HAL_I2C_Mem_Write_DMA()
  113.       (+) At Memory end of write transfer, HAL_I2C_MemTxCpltCallback() is executed and user can
  114.            add his own code by customization of function pointer HAL_I2C_MemTxCpltCallback()
  115.       (+) Read an amount of data in non-blocking mode with DMA from a specific memory address using
  116.           HAL_I2C_Mem_Read_DMA()
  117.       (+) At Memory end of read transfer, HAL_I2C_MemRxCpltCallback() is executed and user can
  118.            add his own code by customization of function pointer HAL_I2C_MemRxCpltCallback()
  119.       (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
  120.            add his own code by customization of function pointer HAL_I2C_ErrorCallback()


  121.      *** I2C HAL driver macros list ***
  122.      ==================================
  123.      [..]
  124.        Below the list of most used macros in I2C HAL driver.

  125.       (+) __HAL_I2C_ENABLE:      Enable the I2C peripheral
  126.       (+) __HAL_I2C_DISABLE:     Disable the I2C peripheral
  127.       (+) __HAL_I2C_GET_FLAG:    Check whether the specified I2C flag is set or not
  128.       (+) __HAL_I2C_CLEAR_FLAG:  Clear the specified I2C pending flag
  129.       (+) __HAL_I2C_ENABLE_IT:   Enable the specified I2C interrupt
  130.       (+) __HAL_I2C_DISABLE_IT:  Disable the specified I2C interrupt

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

  133.   @endverbatim
  134.   ******************************************************************************
  135.   * @attention
  136.   *
  137.   * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  138.   *
  139.   * Redistribution and use in source and binary forms, with or without modification,
  140.   * are permitted provided that the following conditions are met:
  141.   *   1. Redistributions of source code must retain the above copyright notice,
  142.   *      this list of conditions and the following disclaimer.
  143.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  144.   *      this list of conditions and the following disclaimer in the documentation
  145.   *      and/or other materials provided with the distribution.
  146.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  147.   *      may be used to endorse or promote products derived from this software
  148.   *      without specific prior written permission.
  149.   *
  150.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  151.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  152.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  153.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  154.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  155.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  156.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  157.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  158.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  159.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  160.   *
  161.   ******************************************************************************  
  162.   */

  163. /* Includes ------------------------------------------------------------------*/
  164. #include "stm32f0xx_hal.h"

  165. /** @addtogroup STM32F0xx_HAL_Driver
  166.   * @{
  167.   */

  168. /** @defgroup I2C I2C
  169.   * @brief I2C HAL module driver
  170.   * @{
  171.   */

  172. #ifdef HAL_I2C_MODULE_ENABLED

  173. /* Private typedef -----------------------------------------------------------*/
  174. /* Private define ------------------------------------------------------------*/

  175. /** @defgroup I2C_Private_Define I2C Private Define
  176.   * @{
  177.   */
  178. #define TIMING_CLEAR_MASK   ((uint32_t)0xF0FFFFFFU)  /*!< I2C TIMING clear register Mask */
  179. #define I2C_TIMEOUT_ADDR    ((uint32_t)10000)       /*!< 10 s  */
  180. #define I2C_TIMEOUT_BUSY    ((uint32_t)25)          /*!< 25 ms */
  181. #define I2C_TIMEOUT_DIR     ((uint32_t)25)          /*!< 25 ms */
  182. #define I2C_TIMEOUT_RXNE    ((uint32_t)25)          /*!< 25 ms */
  183. #define I2C_TIMEOUT_STOPF   ((uint32_t)25)          /*!< 25 ms */
  184. #define I2C_TIMEOUT_TC      ((uint32_t)25)          /*!< 25 ms */
  185. #define I2C_TIMEOUT_TCR     ((uint32_t)25)          /*!< 25 ms */
  186. #define I2C_TIMEOUT_TXIS    ((uint32_t)25)          /*!< 25 ms */
  187. #define I2C_TIMEOUT_FLAG    ((uint32_t)25)          /*!< 25 ms */

  188. #define MAX_NBYTE_SIZE      255
  189. /**
  190.   * @}
  191.   */
  192.   
  193. /* Private macro -------------------------------------------------------------*/
  194. /* Private variables ---------------------------------------------------------*/
  195. /* Private function prototypes -----------------------------------------------*/

  196. /** @defgroup I2C_Private_Functions I2C Private Functions
  197.   * @{
  198.   */
  199. static void I2C_DMAMasterTransmitCplt(DMA_HandleTypeDef *hdma);
  200. static void I2C_DMAMasterReceiveCplt(DMA_HandleTypeDef *hdma);
  201. static void I2C_DMASlaveTransmitCplt(DMA_HandleTypeDef *hdma);
  202. static void I2C_DMASlaveReceiveCplt(DMA_HandleTypeDef *hdma);
  203. static void I2C_DMAMemTransmitCplt(DMA_HandleTypeDef *hdma);
  204. static void I2C_DMAMemReceiveCplt(DMA_HandleTypeDef *hdma);
  205. static void I2C_DMAError(DMA_HandleTypeDef *hdma);

  206. static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout);
  207. static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout);
  208. static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
  209. static HAL_StatusTypeDef I2C_WaitOnTXISFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout);
  210. static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout);
  211. static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout);
  212. static HAL_StatusTypeDef I2C_IsAcknowledgeFailed(I2C_HandleTypeDef *hi2c, uint32_t Timeout);

  213. static HAL_StatusTypeDef I2C_MasterTransmit_ISR(I2C_HandleTypeDef *hi2c);
  214. static HAL_StatusTypeDef I2C_MasterReceive_ISR(I2C_HandleTypeDef *hi2c);

  215. static HAL_StatusTypeDef I2C_SlaveTransmit_ISR(I2C_HandleTypeDef *hi2c);
  216. static HAL_StatusTypeDef I2C_SlaveReceive_ISR(I2C_HandleTypeDef *hi2c);

  217. static void I2C_TransferConfig(I2C_HandleTypeDef *hi2c,  uint16_t DevAddress, uint8_t Size, uint32_t Mode, uint32_t Request);
  218. /**
  219.   * @}
  220.   */
  221.   
  222. /* Exported functions --------------------------------------------------------*/

  223. /** @defgroup I2C_Exported_Functions I2C Exported Functions
  224.   * @{
  225.   */

  226. /** @defgroup I2C_Exported_Functions_Group1 Initialization and de-initialization functions
  227. *  @brief    Initialization and Configuration functions
  228. *
  229. @verbatim   
  230. ===============================================================================
  231.               ##### Initialization and de-initialization functions #####
  232. ===============================================================================
  233.     [..]  This subsection provides a set of functions allowing to initialize and
  234.           deinitialize the I2Cx peripheral:

  235.       (+) User must Implement HAL_I2C_MspInit() function in which he configures
  236.           all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).

  237.       (+) Call the function HAL_I2C_Init() to configure the selected device with
  238.           the selected configuration:
  239.         (++) Clock Timing
  240.         (++) Own Address 1
  241.         (++) Addressing mode (Master, Slave)
  242.         (++) Dual Addressing mode
  243.         (++) Own Address 2
  244.         (++) Own Address 2 Mask
  245.         (++) General call mode
  246.         (++) Nostretch mode

  247.       (+) Call the function HAL_I2C_DeInit() to restore the default configuration
  248.           of the selected I2Cx peripheral.      

  249. @endverbatim
  250.   * @{
  251.   */

  252. /**
  253.   * @brief  Initializes the I2C according to the specified parameters
  254.   *         in the I2C_InitTypeDef and initialize the associated handle.
  255.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  256.   *                the configuration information for the specified I2C.
  257.   * @retval HAL status
  258.   */
  259. HAL_StatusTypeDef HAL_I2C_Init(I2C_HandleTypeDef *hi2c)
  260. {
  261.   /* Check the I2C handle allocation */
  262.   if(hi2c == NULL)
  263.   {
  264.     return HAL_ERROR;
  265.   }
  266.   
  267.   /* Check the parameters */
  268.   assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
  269.   assert_param(IS_I2C_OWN_ADDRESS1(hi2c->Init.OwnAddress1));
  270.   assert_param(IS_I2C_ADDRESSING_MODE(hi2c->Init.AddressingMode));
  271.   assert_param(IS_I2C_DUAL_ADDRESS(hi2c->Init.DualAddressMode));
  272.   assert_param(IS_I2C_OWN_ADDRESS2(hi2c->Init.OwnAddress2));
  273.   assert_param(IS_I2C_OWN_ADDRESS2_MASK(hi2c->Init.OwnAddress2Masks));
  274.   assert_param(IS_I2C_GENERAL_CALL(hi2c->Init.GeneralCallMode));
  275.   assert_param(IS_I2C_NO_STRETCH(hi2c->Init.NoStretchMode));

  276.   if(hi2c->State == HAL_I2C_STATE_RESET)
  277.   {
  278.     /* Allocate lock resource and initialize it */
  279.     hi2c->Lock = HAL_UNLOCKED;
  280.    
  281.     /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
  282.     HAL_I2C_MspInit(hi2c);
  283.   }

  284.   hi2c->State = HAL_I2C_STATE_BUSY;
  285.   
  286.   /* Disable the selected I2C peripheral */
  287.   __HAL_I2C_DISABLE(hi2c);
  288.   
  289.   /*---------------------------- I2Cx TIMINGR Configuration ------------------*/
  290.   /* Configure I2Cx: Frequency range */
  291.   hi2c->Instance->TIMINGR = hi2c->Init.Timing & TIMING_CLEAR_MASK;
  292.   
  293.   /*---------------------------- I2Cx OAR1 Configuration ---------------------*/
  294.   /* Configure I2Cx: Own Address1 and ack own address1 mode */
  295.   hi2c->Instance->OAR1 &= ~I2C_OAR1_OA1EN;
  296.   if(hi2c->Init.OwnAddress1 != 0)
  297.   {
  298.     if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_7BIT)
  299.     {
  300.       hi2c->Instance->OAR1 = (I2C_OAR1_OA1EN | hi2c->Init.OwnAddress1);
  301.     }
  302.     else /* I2C_ADDRESSINGMODE_10BIT */
  303.     {
  304.       hi2c->Instance->OAR1 = (I2C_OAR1_OA1EN | I2C_OAR1_OA1MODE | hi2c->Init.OwnAddress1);
  305.     }
  306.   }

  307.   /*---------------------------- I2Cx CR2 Configuration ----------------------*/
  308.   /* Configure I2Cx: Addressing Master mode */
  309.   if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT)
  310.   {
  311.     hi2c->Instance->CR2 = (I2C_CR2_ADD10);
  312.   }
  313.   /* Enable the AUTOEND by default, and enable NACK (should be disable only during Slave process */
  314.   hi2c->Instance->CR2 |= (I2C_CR2_AUTOEND | I2C_CR2_NACK);
  315.   
  316.   /*---------------------------- I2Cx OAR2 Configuration ---------------------*/
  317.   /* Configure I2Cx: Dual mode and Own Address2 */
  318.   hi2c->Instance->OAR2 = (hi2c->Init.DualAddressMode | hi2c->Init.OwnAddress2 | (hi2c->Init.OwnAddress2Masks << 8));

  319.   /*---------------------------- I2Cx CR1 Configuration ----------------------*/
  320.   /* Configure I2Cx: Generalcall and NoStretch mode */
  321.   hi2c->Instance->CR1 = (hi2c->Init.GeneralCallMode | hi2c->Init.NoStretchMode);
  322.   
  323.   /* Enable the selected I2C peripheral */
  324.   __HAL_I2C_ENABLE(hi2c);
  325.   
  326.   hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  327.   hi2c->State = HAL_I2C_STATE_READY;
  328.   hi2c->Mode = HAL_I2C_MODE_NONE;
  329.   
  330.   return HAL_OK;
  331. }

  332. /**
  333.   * @brief  DeInitialize the I2C peripheral.
  334.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  335.   *                the configuration information for the specified I2C.
  336.   * @retval HAL status
  337.   */
  338. HAL_StatusTypeDef HAL_I2C_DeInit(I2C_HandleTypeDef *hi2c)
  339. {
  340.   /* Check the I2C handle allocation */
  341.   if(hi2c == NULL)
  342.   {
  343.     return HAL_ERROR;
  344.   }
  345.   
  346.   /* Check the parameters */
  347.   assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
  348.   
  349.   hi2c->State = HAL_I2C_STATE_BUSY;
  350.   
  351.   /* Disable the I2C Peripheral Clock */
  352.   __HAL_I2C_DISABLE(hi2c);
  353.   
  354.   /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  355.   HAL_I2C_MspDeInit(hi2c);
  356.   
  357.   hi2c->ErrorCode = HAL_I2C_ERROR_NONE;

  358.   hi2c->State = HAL_I2C_STATE_RESET;
  359.   hi2c->Mode = HAL_I2C_MODE_NONE;
  360.   
  361.   /* Release Lock */
  362.   __HAL_UNLOCK(hi2c);

  363.   return HAL_OK;
  364. }

  365. /**
  366.   * @brief Initialize the I2C MSP.
  367.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  368.   *                the configuration information for the specified I2C.
  369.   * @retval None
  370.   */
  371. __weak void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)
  372. {
  373.   /* Prevent unused argument(s) compilation warning */
  374.   UNUSED(hi2c);

  375.   /* NOTE : This function should not be modified, when the callback is needed,
  376.             the HAL_I2C_MspInit could be implemented in the user file
  377.    */
  378. }

  379. /**
  380.   * @brief DeInitialize the I2C MSP.
  381.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  382.   *                the configuration information for the specified I2C.
  383.   * @retval None
  384.   */
  385. __weak void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c)
  386. {
  387.   /* Prevent unused argument(s) compilation warning */
  388.   UNUSED(hi2c);

  389.   /* NOTE : This function should not be modified, when the callback is needed,
  390.             the HAL_I2C_MspDeInit could be implemented in the user file
  391.    */
  392. }

  393. /**
  394.   * @}
  395.   */

  396. /** @defgroup I2C_Exported_Functions_Group2 Input and Output operation functions
  397. *  @brief   Data transfers functions
  398. *
  399. @verbatim   
  400. ===============================================================================
  401.                       ##### IO operation functions #####
  402. ===============================================================================  
  403.     [..]
  404.     This subsection provides a set of functions allowing to manage the I2C data
  405.     transfers.

  406.     (#) There are two modes of transfer:
  407.        (++) Blocking mode : The communication is performed in the polling mode.
  408.             The status of all data processing is returned by the same function
  409.             after finishing transfer.  
  410.        (++) No-Blocking mode : The communication is performed using Interrupts
  411.             or DMA. These functions return the status of the transfer startup.
  412.             The end of the data processing will be indicated through the
  413.             dedicated I2C IRQ when using Interrupt mode or the DMA IRQ when
  414.             using DMA mode.

  415.     (#) Blocking mode functions are :
  416.         (++) HAL_I2C_Master_Transmit()
  417.         (++) HAL_I2C_Master_Receive()
  418.         (++) HAL_I2C_Slave_Transmit()
  419.         (++) HAL_I2C_Slave_Receive()
  420.         (++) HAL_I2C_Mem_Write()
  421.         (++) HAL_I2C_Mem_Read()
  422.         (++) HAL_I2C_IsDeviceReady()
  423.         
  424.     (#) No-Blocking mode functions with Interrupt are :
  425.         (++) HAL_I2C_Master_Transmit_IT()
  426.         (++) HAL_I2C_Master_Receive_IT()
  427.         (++) HAL_I2C_Slave_Transmit_IT()
  428.         (++) HAL_I2C_Slave_Receive_IT()
  429.         (++) HAL_I2C_Mem_Write_IT()
  430.         (++) HAL_I2C_Mem_Read_IT()

  431.     (#) No-Blocking mode functions with DMA are :
  432.         (++) HAL_I2C_Master_Transmit_DMA()
  433.         (++) HAL_I2C_Master_Receive_DMA()
  434.         (++) HAL_I2C_Slave_Transmit_DMA()
  435.         (++) HAL_I2C_Slave_Receive_DMA()
  436.         (++) HAL_I2C_Mem_Write_DMA()
  437.         (++) HAL_I2C_Mem_Read_DMA()

  438.     (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
  439.         (++) HAL_I2C_MemTxCpltCallback()
  440.         (++) HAL_I2C_MemRxCpltCallback()
  441.         (++) HAL_I2C_MasterTxCpltCallback()
  442.         (++) HAL_I2C_MasterRxCpltCallback()
  443.         (++) HAL_I2C_SlaveTxCpltCallback()
  444.         (++) HAL_I2C_SlaveRxCpltCallback()
  445.         (++) HAL_I2C_ErrorCallback()

  446. @endverbatim
  447.   * @{
  448.   */

  449. /**
  450.   * @brief  Transmits in master mode an amount of data in blocking mode.
  451.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  452.   *                the configuration information for the specified I2C.
  453.   * @param  DevAddress Target device address
  454.   * @param  pData Pointer to data buffer
  455.   * @param  Size Amount of data to be sent
  456.   * @param  Timeout Timeout duration
  457.   * @retval HAL status
  458.   */
  459. HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  460. {
  461.   uint32_t sizetmp = 0;

  462.   if(hi2c->State == HAL_I2C_STATE_READY)
  463.   {   
  464.     if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  465.     {
  466.       return HAL_BUSY;
  467.     }

  468.     /* Process Locked */
  469.     __HAL_LOCK(hi2c);
  470.    
  471.     hi2c->State = HAL_I2C_STATE_BUSY_TX;
  472.     hi2c->Mode = HAL_I2C_MODE_MASTER;
  473.     hi2c->ErrorCode   = HAL_I2C_ERROR_NONE;
  474.    
  475.     /* Send Slave Address */
  476.     /* Set NBYTES to write and reload if size > MAX_NBYTE_SIZE and generate RESTART */
  477.     /* Size > MAX_NBYTE_SIZE, need to set RELOAD bit */
  478.     if(Size > MAX_NBYTE_SIZE)
  479.     {
  480.       I2C_TransferConfig(hi2c, DevAddress, MAX_NBYTE_SIZE, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);
  481.       sizetmp = MAX_NBYTE_SIZE;
  482.     }
  483.     else
  484.     {
  485.       I2C_TransferConfig(hi2c, DevAddress, Size, I2C_AUTOEND_MODE, I2C_GENERATE_START_WRITE);
  486.       sizetmp = Size;
  487.     }

  488.     while(Size > 0)
  489.     {
  490.       /* Wait until TXIS flag is set */
  491.       if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
  492.       {
  493.         if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  494.         {
  495.           return HAL_ERROR;
  496.         }
  497.         else
  498.         {
  499.           return HAL_TIMEOUT;
  500.         }
  501.       }
  502.       /* Write data to TXDR */
  503.       hi2c->Instance->TXDR = (*pData++);
  504.       sizetmp--;
  505.       Size--;

  506.       if((sizetmp == 0)&&(Size!=0))
  507.       {
  508.         /* Wait until TCR flag is set */
  509.         if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout) != HAL_OK)      
  510.         {
  511.           return HAL_TIMEOUT;
  512.         }
  513.         
  514.         if(Size > MAX_NBYTE_SIZE)
  515.         {
  516.           I2C_TransferConfig(hi2c,DevAddress,MAX_NBYTE_SIZE, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  517.           sizetmp = MAX_NBYTE_SIZE;
  518.         }
  519.         else
  520.         {
  521.           I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  522.           sizetmp = Size;
  523.         }
  524.       }
  525.     }
  526.   
  527.     /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  528.     /* Wait until STOPF flag is set */
  529.     if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
  530.     {
  531.       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  532.       {
  533.         return HAL_ERROR;
  534.       }
  535.       else
  536.       {
  537.         return HAL_TIMEOUT;
  538.       }
  539.     }
  540.    
  541.     /* Clear STOP Flag */
  542.     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  543.          
  544.     /* Clear Configuration Register 2 */
  545.     I2C_RESET_CR2(hi2c);

  546.     hi2c->State = HAL_I2C_STATE_READY;
  547.     hi2c->Mode = HAL_I2C_MODE_NONE;
  548.    
  549.     /* Process Unlocked */
  550.     __HAL_UNLOCK(hi2c);

  551.     return HAL_OK;
  552.   }
  553.   else
  554.   {
  555.     return HAL_BUSY;
  556.   }
  557. }

  558. /**
  559.   * @brief  Receives in master mode an amount of data in blocking mode.
  560.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  561.   *                the configuration information for the specified I2C.
  562.   * @param  DevAddress Target device address
  563.   * @param  pData Pointer to data buffer
  564.   * @param  Size Amount of data to be sent
  565.   * @param  Timeout Timeout duration
  566.   * @retval HAL status
  567.   */
  568. HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  569. {
  570.   uint32_t sizetmp = 0;

  571.   if(hi2c->State == HAL_I2C_STATE_READY)
  572.   {   
  573.     if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  574.     {
  575.       return HAL_BUSY;
  576.     }

  577.     /* Process Locked */
  578.     __HAL_LOCK(hi2c);
  579.    
  580.     hi2c->State = HAL_I2C_STATE_BUSY_RX;
  581.     hi2c->Mode = HAL_I2C_MODE_MASTER;
  582.     hi2c->ErrorCode   = HAL_I2C_ERROR_NONE;
  583.    
  584.     /* Send Slave Address */
  585.     /* Set NBYTES to write and reload if size > MAX_NBYTE_SIZE and generate RESTART */
  586.     /* Size > MAX_NBYTE_SIZE, need to set RELOAD bit */
  587.     if(Size > MAX_NBYTE_SIZE)
  588.     {
  589.       I2C_TransferConfig(hi2c, DevAddress, MAX_NBYTE_SIZE, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
  590.       sizetmp = MAX_NBYTE_SIZE;
  591.     }
  592.     else if(Size > 0)
  593.     {
  594.       I2C_TransferConfig(hi2c, DevAddress, Size, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
  595.       sizetmp = Size;
  596.     }
  597.     else
  598.     {
  599.       I2C_TransferConfig(hi2c, DevAddress, Size, I2C_SOFTEND_MODE, I2C_GENERATE_START_READ);
  600.       sizetmp = Size;
  601.       
  602.       /* Wait until TC flag is set */
  603.       if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TC, RESET, Timeout) != HAL_OK)      
  604.       {
  605.         return HAL_TIMEOUT;
  606.       }
  607.       else
  608.       {
  609.         /* Generate a Stop command */
  610.         hi2c->Instance->CR2 |= I2C_CR2_STOP;
  611.       }
  612.     }
  613.    
  614.     while(Size > 0)
  615.     {
  616.       /* Wait until RXNE flag is set */
  617.       if(I2C_WaitOnRXNEFlagUntilTimeout(hi2c, I2C_FLAG_RXNE) != HAL_OK)      
  618.       {
  619.         if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  620.         {
  621.           return HAL_ERROR;
  622.         }
  623.         else
  624.         {
  625.           return HAL_TIMEOUT;
  626.         }
  627.       }
  628.      
  629.       /* Write data to RXDR */
  630.       (*pData++) =hi2c->Instance->RXDR;
  631.       sizetmp--;
  632.       Size--;

  633.       if((sizetmp == 0)&&(Size!=0))
  634.       {
  635.         /* Wait until TCR flag is set */
  636.         if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout) != HAL_OK)      
  637.         {
  638.           return HAL_TIMEOUT;
  639.         }
  640.         
  641.         if(Size > MAX_NBYTE_SIZE)
  642.         {
  643.           I2C_TransferConfig(hi2c, DevAddress, MAX_NBYTE_SIZE, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  644.           sizetmp = MAX_NBYTE_SIZE;
  645.         }
  646.         else
  647.         {
  648.           I2C_TransferConfig(hi2c, DevAddress, Size, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  649.           sizetmp = Size;
  650.         }
  651.       }
  652.     }
  653.    
  654.     /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  655.     /* Wait until STOPF flag is set */
  656.     if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  657.     {
  658.       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  659.       {
  660.         return HAL_ERROR;
  661.       }
  662.       else
  663.       {
  664.         return HAL_TIMEOUT;
  665.       }
  666.     }
  667.    
  668.     /* Clear STOP Flag */
  669.     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  670.          
  671.     /* Clear Configuration Register 2 */
  672.     I2C_RESET_CR2(hi2c);
  673.    
  674.     hi2c->State = HAL_I2C_STATE_READY;
  675.     hi2c->Mode = HAL_I2C_MODE_NONE;
  676.    
  677.     /* Process Unlocked */
  678.     __HAL_UNLOCK(hi2c);
  679.    
  680.     return HAL_OK;
  681.   }
  682.   else
  683.   {
  684.     return HAL_BUSY;
  685.   }
  686. }

  687. /**
  688.   * @brief  Transmits in slave mode an amount of data in blocking mode.
  689.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  690.   *                the configuration information for the specified I2C.
  691.   * @param  pData Pointer to data buffer
  692.   * @param  Size Amount of data to be sent
  693.   * @param  Timeout Timeout duration
  694.   * @retval HAL status
  695.   */
  696. HAL_StatusTypeDef HAL_I2C_Slave_Transmit(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  697. {
  698.   if(hi2c->State == HAL_I2C_STATE_READY)
  699.   {   
  700.     if((pData == NULL) || (Size == 0))
  701.     {
  702.       return  HAL_ERROR;
  703.     }
  704.     /* Process Locked */
  705.     __HAL_LOCK(hi2c);
  706.    
  707.     hi2c->State = HAL_I2C_STATE_BUSY_TX;
  708.     hi2c->Mode = HAL_I2C_MODE_SLAVE;
  709.     hi2c->ErrorCode   = HAL_I2C_ERROR_NONE;
  710.    
  711.     /* Enable Address Acknowledge */
  712.     hi2c->Instance->CR2 &= ~I2C_CR2_NACK;

  713.     /* Wait until ADDR flag is set */
  714.     if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout) != HAL_OK)      
  715.     {
  716.       /* Disable Address Acknowledge */
  717.       hi2c->Instance->CR2 |= I2C_CR2_NACK;
  718.       return HAL_TIMEOUT;
  719.     }
  720.    
  721.     /* Clear ADDR flag */
  722.     __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);

  723.     /* If 10bit addressing mode is selected */
  724.     if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT)
  725.     {
  726.       /* Wait until ADDR flag is set */
  727.       if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout) != HAL_OK)      
  728.       {
  729.         /* Disable Address Acknowledge */
  730.         hi2c->Instance->CR2 |= I2C_CR2_NACK;
  731.         return HAL_TIMEOUT;
  732.       }
  733.    
  734.       /* Clear ADDR flag */
  735.       __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
  736.     }

  737.     /* Wait until DIR flag is set Transmitter mode */
  738.     if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_DIR, RESET, Timeout) != HAL_OK)      
  739.     {
  740.       /* Disable Address Acknowledge */
  741.       hi2c->Instance->CR2 |= I2C_CR2_NACK;
  742.       return HAL_TIMEOUT;
  743.     }

  744.     while(Size > 0)
  745.     {
  746.       /* Wait until TXIS flag is set */
  747.       if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
  748.       {
  749.         /* Disable Address Acknowledge */
  750.         hi2c->Instance->CR2 |= I2C_CR2_NACK;

  751.         if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  752.         {
  753.           return HAL_ERROR;
  754.         }
  755.         else
  756.         {
  757.           return HAL_TIMEOUT;
  758.         }
  759.       }
  760.       
  761.       /* Read data from TXDR */
  762.       hi2c->Instance->TXDR = (*pData++);
  763.       Size--;
  764.     }
  765.    
  766.     /* Wait until STOP flag is set */
  767.     if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  768.     {
  769.       /* Disable Address Acknowledge */
  770.       hi2c->Instance->CR2 |= I2C_CR2_NACK;

  771.       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  772.       {
  773.         /* Normal use case for Transmitter mode */
  774.         /* A NACK is generated to confirm the end of transfer */
  775.         hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  776.       }
  777.       else
  778.       {
  779.         return HAL_TIMEOUT;
  780.       }
  781.     }
  782.    
  783.     /* Clear STOP flag */
  784.     __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_STOPF);
  785.    
  786.     /* Wait until BUSY flag is reset */
  787.     if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout) != HAL_OK)      
  788.     {
  789.       /* Disable Address Acknowledge */
  790.       hi2c->Instance->CR2 |= I2C_CR2_NACK;
  791.       return HAL_TIMEOUT;
  792.     }
  793.    
  794.     /* Disable Address Acknowledge */
  795.     hi2c->Instance->CR2 |= I2C_CR2_NACK;

  796.     hi2c->State = HAL_I2C_STATE_READY;
  797.     hi2c->Mode = HAL_I2C_MODE_NONE;
  798.    
  799.     /* Process Unlocked */
  800.     __HAL_UNLOCK(hi2c);
  801.    
  802.     return HAL_OK;
  803.   }
  804.   else
  805.   {
  806.     return HAL_BUSY;
  807.   }
  808. }

  809. /**
  810.   * @brief  Receive in slave mode an amount of data in blocking mode
  811.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  812.   *                the configuration information for the specified I2C.
  813.   * @param  pData Pointer to data buffer
  814.   * @param  Size Amount of data to be sent
  815.   * @param  Timeout Timeout duration
  816.   * @retval HAL status
  817.   */
  818. HAL_StatusTypeDef HAL_I2C_Slave_Receive(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  819. {
  820.   if(hi2c->State == HAL_I2C_STATE_READY)
  821.   {  
  822.     if((pData == NULL) || (Size == 0))
  823.     {
  824.       return  HAL_ERROR;
  825.     }
  826.     /* Process Locked */
  827.     __HAL_LOCK(hi2c);
  828.    
  829.     hi2c->State = HAL_I2C_STATE_BUSY_RX;
  830.     hi2c->Mode = HAL_I2C_MODE_SLAVE;
  831.     hi2c->ErrorCode   = HAL_I2C_ERROR_NONE;
  832.    
  833.     /* Enable Address Acknowledge */
  834.     hi2c->Instance->CR2 &= ~I2C_CR2_NACK;

  835.     /* Wait until ADDR flag is set */
  836.     if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout) != HAL_OK)      
  837.     {
  838.       /* Disable Address Acknowledge */
  839.       hi2c->Instance->CR2 |= I2C_CR2_NACK;
  840.       return HAL_TIMEOUT;
  841.     }

  842.     /* Clear ADDR flag */
  843.     __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
  844.    
  845.     /* Wait until DIR flag is reset Receiver mode */
  846.     if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_DIR, SET, Timeout) != HAL_OK)      
  847.     {
  848.       /* Disable Address Acknowledge */
  849.       hi2c->Instance->CR2 |= I2C_CR2_NACK;
  850.       return HAL_TIMEOUT;
  851.     }

  852.     while(Size > 0)
  853.     {
  854.       /* Wait until RXNE flag is set */
  855.       if(I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout) != HAL_OK)      
  856.       {
  857.         /* Disable Address Acknowledge */
  858.         hi2c->Instance->CR2 |= I2C_CR2_NACK;
  859.         
  860.         /* Store Last receive data if any */
  861.         if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
  862.         {
  863.           /* Read data from RXDR */
  864.           (*pData++) = hi2c->Instance->RXDR;
  865.         }
  866.         
  867.         if(hi2c->ErrorCode == HAL_I2C_ERROR_TIMEOUT)
  868.         {
  869.           return HAL_TIMEOUT;
  870.         }
  871.         else
  872.         {
  873.           return HAL_ERROR;
  874.         }
  875.       }
  876.       
  877.       /* Read data from RXDR */
  878.       (*pData++) = hi2c->Instance->RXDR;
  879.       Size--;
  880.     }
  881.    
  882.     /* Wait until STOP flag is set */
  883.     if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  884.     {
  885.       /* Disable Address Acknowledge */
  886.       hi2c->Instance->CR2 |= I2C_CR2_NACK;

  887.       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  888.       {
  889.         return HAL_ERROR;
  890.       }
  891.       else
  892.       {
  893.         return HAL_TIMEOUT;
  894.       }
  895.     }

  896.     /* Clear STOP flag */
  897.     __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_STOPF);
  898.    
  899.     /* Wait until BUSY flag is reset */
  900.     if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout) != HAL_OK)      
  901.     {
  902.       /* Disable Address Acknowledge */
  903.       hi2c->Instance->CR2 |= I2C_CR2_NACK;
  904.       return HAL_TIMEOUT;
  905.     }

  906.     /* Disable Address Acknowledge */
  907.     hi2c->Instance->CR2 |= I2C_CR2_NACK;
  908.    
  909.     hi2c->State = HAL_I2C_STATE_READY;
  910.     hi2c->Mode = HAL_I2C_MODE_NONE;

  911.     /* Process Unlocked */
  912.     __HAL_UNLOCK(hi2c);
  913.    
  914.     return HAL_OK;
  915.   }
  916.   else
  917.   {
  918.     return HAL_BUSY;
  919.   }
  920. }

  921. /**
  922.   * @brief  Transmit in master mode an amount of data in non-blocking mode with Interrupt
  923.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  924.   *                the configuration information for the specified I2C.
  925.   * @param  DevAddress Target device address
  926.   * @param  pData Pointer to data buffer
  927.   * @param  Size Amount of data to be sent
  928.   * @retval HAL status
  929.   */
  930. HAL_StatusTypeDef HAL_I2C_Master_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
  931. {   
  932.   if(hi2c->State == HAL_I2C_STATE_READY)
  933.   {
  934.     if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  935.     {
  936.       return HAL_BUSY;
  937.     }

  938.     /* Process Locked */
  939.     __HAL_LOCK(hi2c);
  940.    
  941.     hi2c->State = HAL_I2C_STATE_BUSY_TX;
  942.     hi2c->Mode = HAL_I2C_MODE_MASTER;
  943.     hi2c->ErrorCode   = HAL_I2C_ERROR_NONE;
  944.    
  945.     hi2c->pBuffPtr = pData;
  946.     hi2c->XferCount = Size;
  947.     if(Size > MAX_NBYTE_SIZE)
  948.     {
  949.       hi2c->XferSize = MAX_NBYTE_SIZE;
  950.     }
  951.     else
  952.     {
  953.       hi2c->XferSize = Size;
  954.     }
  955.    
  956.     /* Send Slave Address */
  957.     /* Set NBYTES to write and reload if size > MAX_NBYTE_SIZE and generate RESTART */
  958.     if( (hi2c->XferSize == MAX_NBYTE_SIZE) && (hi2c->XferSize < hi2c->XferCount) )
  959.     {
  960.       I2C_TransferConfig(hi2c, DevAddress, hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);
  961.     }
  962.     else
  963.     {
  964.       I2C_TransferConfig(hi2c, DevAddress, hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_WRITE);
  965.     }
  966.    
  967.     /* Process Unlocked */
  968.     __HAL_UNLOCK(hi2c);

  969.     /* Note : The I2C interrupts must be enabled after unlocking current process
  970.               to avoid the risk of I2C interrupt handle execution before current
  971.               process unlock */


  972.     /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  973.     /* possible to enable all of these */
  974.     /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  975.     __HAL_I2C_ENABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_TXI );
  976.         
  977.     return HAL_OK;
  978.   }
  979.   else
  980.   {
  981.     return HAL_BUSY;
  982.   }
  983. }

  984. /**
  985.   * @brief  Receive in master mode an amount of data in non-blocking mode with Interrupt
  986.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  987.   *                the configuration information for the specified I2C.
  988.   * @param  DevAddress Target device address
  989.   * @param  pData Pointer to data buffer
  990.   * @param  Size Amount of data to be sent
  991.   * @retval HAL status
  992.   */
  993. HAL_StatusTypeDef HAL_I2C_Master_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
  994. {
  995.   if(hi2c->State == HAL_I2C_STATE_READY)
  996.   {
  997.     if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  998.     {
  999.       return HAL_BUSY;
  1000.     }

  1001.     /* Process Locked */
  1002.     __HAL_LOCK(hi2c);
  1003.    
  1004.     hi2c->State = HAL_I2C_STATE_BUSY_RX;
  1005.     hi2c->Mode = HAL_I2C_MODE_MASTER;
  1006.     hi2c->ErrorCode   = HAL_I2C_ERROR_NONE;
  1007.    
  1008.     hi2c->pBuffPtr = pData;
  1009.     hi2c->XferCount = Size;
  1010.     if(Size > MAX_NBYTE_SIZE)
  1011.     {
  1012.       hi2c->XferSize = MAX_NBYTE_SIZE;
  1013.     }
  1014.     else
  1015.     {
  1016.       hi2c->XferSize = Size;
  1017.     }
  1018.    
  1019.     /* Send Slave Address */
  1020.     /* Set NBYTES to write and reload if size > MAX_NBYTE_SIZE and generate RESTART */
  1021.     if( (hi2c->XferSize == MAX_NBYTE_SIZE) && (hi2c->XferSize < hi2c->XferCount) )
  1022.     {
  1023.       I2C_TransferConfig(hi2c, DevAddress, hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
  1024.     }
  1025.     else
  1026.     {
  1027.       I2C_TransferConfig(hi2c, DevAddress, hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
  1028.     }
  1029.    
  1030.     /* Process Unlocked */
  1031.     __HAL_UNLOCK(hi2c);

  1032.     /* Note : The I2C interrupts must be enabled after unlocking current process
  1033.               to avoid the risk of I2C interrupt handle execution before current
  1034.               process unlock */
  1035.    
  1036.     /* Enable ERR, TC, STOP, NACK, RXI interrupt */
  1037.     /* possible to enable all of these */
  1038.     /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  1039.     __HAL_I2C_ENABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_RXI );
  1040.    
  1041.     return HAL_OK;
  1042.   }
  1043.   else
  1044.   {
  1045.     return HAL_BUSY;
  1046.   }
  1047. }

  1048. /**
  1049.   * @brief  Transmit in slave mode an amount of data in non-blocking mode with Interrupt
  1050.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1051.   *                the configuration information for the specified I2C.
  1052.   * @param  pData Pointer to data buffer
  1053.   * @param  Size Amount of data to be sent
  1054.   * @retval HAL status
  1055.   */
  1056. HAL_StatusTypeDef HAL_I2C_Slave_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
  1057. {
  1058.   if(hi2c->State == HAL_I2C_STATE_READY)
  1059.   {
  1060.     if((pData == NULL) || (Size == 0))
  1061.     {
  1062.       return  HAL_ERROR;
  1063.     }
  1064.     /* Process Locked */
  1065.     __HAL_LOCK(hi2c);
  1066.    
  1067.     hi2c->State = HAL_I2C_STATE_BUSY_TX;
  1068.     hi2c->Mode = HAL_I2C_MODE_SLAVE;
  1069.     hi2c->ErrorCode   = HAL_I2C_ERROR_NONE;
  1070.    
  1071.     /* Enable Address Acknowledge */
  1072.     hi2c->Instance->CR2 &= ~I2C_CR2_NACK;

  1073.     hi2c->pBuffPtr = pData;
  1074.     hi2c->XferSize = Size;
  1075.     hi2c->XferCount = Size;
  1076.    
  1077.     /* Process Unlocked */
  1078.     __HAL_UNLOCK(hi2c);

  1079.     /* Note : The I2C interrupts must be enabled after unlocking current process
  1080.               to avoid the risk of I2C interrupt handle execution before current
  1081.               process unlock */
  1082.    
  1083.     /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  1084.     /* possible to enable all of these */
  1085.     /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  1086.     __HAL_I2C_ENABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_TXI );
  1087.    
  1088.     return HAL_OK;
  1089.   }
  1090.   else
  1091.   {
  1092.     return HAL_BUSY;
  1093.   }
  1094. }

  1095. /**
  1096.   * @brief  Receive in slave mode an amount of data in non-blocking mode with Interrupt
  1097.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1098.   *                the configuration information for the specified I2C.
  1099.   * @param  pData Pointer to data buffer
  1100.   * @param  Size Amount of data to be sent
  1101.   * @retval HAL status
  1102.   */
  1103. HAL_StatusTypeDef HAL_I2C_Slave_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
  1104. {
  1105.   if(hi2c->State == HAL_I2C_STATE_READY)
  1106.   {
  1107.     if((pData == NULL) || (Size == 0))
  1108.     {
  1109.       return  HAL_ERROR;
  1110.     }
  1111.     /* Process Locked */
  1112.     __HAL_LOCK(hi2c);
  1113.    
  1114.     hi2c->State = HAL_I2C_STATE_BUSY_RX;
  1115.     hi2c->Mode = HAL_I2C_MODE_SLAVE;
  1116.     hi2c->ErrorCode   = HAL_I2C_ERROR_NONE;
  1117.    
  1118.     /* Enable Address Acknowledge */
  1119.     hi2c->Instance->CR2 &= ~I2C_CR2_NACK;

  1120.     hi2c->pBuffPtr = pData;
  1121.     hi2c->XferSize = Size;
  1122.     hi2c->XferCount = Size;
  1123.    
  1124.     /* Process Unlocked */
  1125.     __HAL_UNLOCK(hi2c);

  1126.     /* Note : The I2C interrupts must be enabled after unlocking current process
  1127.               to avoid the risk of I2C interrupt handle execution before current
  1128.               process unlock */
  1129.    
  1130.     /* Enable ERR, TC, STOP, NACK, RXI interrupt */
  1131.     /* possible to enable all of these */
  1132.     /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  1133.     __HAL_I2C_ENABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI);
  1134.    
  1135.     return HAL_OK;
  1136.   }
  1137.   else
  1138.   {
  1139.     return HAL_BUSY;
  1140.   }
  1141. }

  1142. /**
  1143.   * @brief  Transmit in master mode an amount of data in non-blocking mode with DMA
  1144.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1145.   *                the configuration information for the specified I2C.
  1146.   * @param  DevAddress Target device address
  1147.   * @param  pData Pointer to data buffer
  1148.   * @param  Size Amount of data to be sent
  1149.   * @retval HAL status
  1150.   */
  1151. HAL_StatusTypeDef HAL_I2C_Master_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
  1152. {
  1153.   if(hi2c->State == HAL_I2C_STATE_READY)
  1154.   {
  1155.     if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  1156.     {
  1157.       return HAL_BUSY;
  1158.     }

  1159.     /* Process Locked */
  1160.     __HAL_LOCK(hi2c);
  1161.    
  1162.     hi2c->State = HAL_I2C_STATE_BUSY_TX;
  1163.     hi2c->Mode = HAL_I2C_MODE_MASTER;
  1164.     hi2c->ErrorCode   = HAL_I2C_ERROR_NONE;
  1165.    
  1166.     hi2c->pBuffPtr = pData;
  1167.     hi2c->XferCount = Size;
  1168.     if(Size > MAX_NBYTE_SIZE)
  1169.     {
  1170.       hi2c->XferSize = MAX_NBYTE_SIZE;
  1171.     }
  1172.     else
  1173.     {
  1174.       hi2c->XferSize = Size;
  1175.     }
  1176.    
  1177.     if(hi2c->XferSize > 0)
  1178.     {
  1179.       /* Set the I2C DMA transfer complete callback */
  1180.       hi2c->hdmatx->XferCpltCallback = I2C_DMAMasterTransmitCplt;
  1181.       
  1182.       /* Set the DMA error callback */
  1183.       hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
  1184.       
  1185.       /* Enable the DMA channel */
  1186.       HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);

  1187.       /* Send Slave Address */
  1188.       /* Set NBYTES to write and reload if size > MAX_NBYTE_SIZE and generate RESTART */
  1189.       if( (hi2c->XferSize == MAX_NBYTE_SIZE) && (hi2c->XferSize < hi2c->XferCount) )
  1190.       {
  1191.         I2C_TransferConfig(hi2c, DevAddress, hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);
  1192.       }
  1193.       else
  1194.       {
  1195.         I2C_TransferConfig(hi2c, DevAddress, hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_WRITE);
  1196.       }

  1197.       /* Wait until TXIS flag is set */
  1198.       if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, I2C_TIMEOUT_TXIS) != HAL_OK)
  1199.       {
  1200.         /* Disable Address Acknowledge */
  1201.         hi2c->Instance->CR2 |= I2C_CR2_NACK;

  1202.         /* Abort DMA */
  1203.         HAL_DMA_Abort(hi2c->hdmatx);

  1204.         if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  1205.         {
  1206.           return HAL_ERROR;
  1207.         }
  1208.         else
  1209.         {
  1210.           return HAL_TIMEOUT;
  1211.         }
  1212.       }

  1213.       /* Enable DMA Request */
  1214.       hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;   
  1215.     }
  1216.     else
  1217.     {      
  1218.       I2C_TransferConfig(hi2c, DevAddress, hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_WRITE);

  1219.       /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  1220.       /* Wait until STOPF flag is set */
  1221.       if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  1222.       {
  1223.         if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  1224.         {
  1225.           return HAL_ERROR;
  1226.         }
  1227.         else
  1228.         {
  1229.           return HAL_TIMEOUT;
  1230.         }
  1231.       }
  1232.       
  1233.       /* Clear STOP Flag */
  1234.       __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  1235.          
  1236.       /* Clear Configuration Register 2 */
  1237.       I2C_RESET_CR2(hi2c);

  1238.       hi2c->State = HAL_I2C_STATE_READY;
  1239.       hi2c->Mode = HAL_I2C_MODE_NONE;
  1240.     }
  1241.    
  1242.     /* Process Unlocked */
  1243.     __HAL_UNLOCK(hi2c);
  1244.    
  1245.     return HAL_OK;
  1246.   }
  1247.   else
  1248.   {
  1249.     return HAL_BUSY;
  1250.   }
  1251. }

  1252. /**
  1253.   * @brief  Receive in master mode an amount of data in non-blocking mode with DMA
  1254.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1255.   *                the configuration information for the specified I2C.
  1256.   * @param  DevAddress Target device address
  1257.   * @param  pData Pointer to data buffer
  1258.   * @param  Size Amount of data to be sent
  1259.   * @retval HAL status
  1260.   */
  1261. HAL_StatusTypeDef HAL_I2C_Master_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
  1262. {
  1263.   if(hi2c->State == HAL_I2C_STATE_READY)
  1264.   {
  1265.     if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  1266.     {
  1267.       return HAL_BUSY;
  1268.     }

  1269.     /* Process Locked */
  1270.     __HAL_LOCK(hi2c);
  1271.    
  1272.     hi2c->State = HAL_I2C_STATE_BUSY_RX;
  1273.     hi2c->Mode = HAL_I2C_MODE_MASTER;
  1274.     hi2c->ErrorCode   = HAL_I2C_ERROR_NONE;
  1275.    
  1276.     hi2c->pBuffPtr = pData;
  1277.     hi2c->XferCount = Size;
  1278.     if(Size > MAX_NBYTE_SIZE)
  1279.     {
  1280.       hi2c->XferSize = MAX_NBYTE_SIZE;
  1281.     }
  1282.     else
  1283.     {
  1284.       hi2c->XferSize = Size;
  1285.     }
  1286.    
  1287.     if(hi2c->XferSize > 0)
  1288.     {
  1289.       /* Set the I2C DMA transfer complete callback */
  1290.       hi2c->hdmarx->XferCpltCallback = I2C_DMAMasterReceiveCplt;
  1291.       
  1292.       /* Set the DMA error callback */
  1293.       hi2c->hdmarx->XferErrorCallback = I2C_DMAError;

  1294.       /* Enable the DMA channel */
  1295.       HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData, hi2c->XferSize);

  1296.       /* Send Slave Address */
  1297.       /* Set NBYTES to write and reload if size > MAX_NBYTE_SIZE and generate RESTART */
  1298.       if( (hi2c->XferSize == MAX_NBYTE_SIZE) && (hi2c->XferSize < hi2c->XferCount) )
  1299.       {
  1300.         I2C_TransferConfig(hi2c, DevAddress, hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
  1301.       }
  1302.       else
  1303.       {
  1304.         I2C_TransferConfig(hi2c, DevAddress, hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
  1305.       }

  1306.       /* Wait until RXNE flag is set */
  1307.       if(I2C_WaitOnRXNEFlagUntilTimeout(hi2c, I2C_FLAG_RXNE) != HAL_OK)
  1308.       {
  1309.         /* Abort DMA */
  1310.         HAL_DMA_Abort(hi2c->hdmarx);

  1311.         if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  1312.         {
  1313.           return HAL_ERROR;
  1314.         }
  1315.         else
  1316.         {
  1317.           return HAL_TIMEOUT;
  1318.         }
  1319.       }
  1320.       
  1321.       /* Enable DMA Request */
  1322.       hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;   
  1323.     }
  1324.     else
  1325.     {
  1326.       I2C_TransferConfig(hi2c, DevAddress, Size, I2C_SOFTEND_MODE, I2C_GENERATE_START_READ);
  1327.       
  1328.       /* Wait until TC flag is set */
  1329.       if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TC, RESET, I2C_TIMEOUT_TC) != HAL_OK)      
  1330.       {
  1331.         return HAL_TIMEOUT;
  1332.       }
  1333.       else
  1334.       {
  1335.         /* Generate a Stop command */
  1336.         hi2c->Instance->CR2 |= I2C_CR2_STOP;
  1337.       }

  1338.       hi2c->State = HAL_I2C_STATE_READY;
  1339.       hi2c->Mode = HAL_I2C_MODE_NONE;
  1340.     }

  1341.     /* Process Unlocked */
  1342.     __HAL_UNLOCK(hi2c);
  1343.    
  1344.     return HAL_OK;
  1345.   }
  1346.   else
  1347.   {
  1348.     return HAL_BUSY;
  1349.   }
  1350. }

  1351. /**
  1352.   * @brief  Transmit in slave mode an amount of data in non-blocking mode with DMA
  1353.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1354.   *                the configuration information for the specified I2C.
  1355.   * @param  pData Pointer to data buffer
  1356.   * @param  Size Amount of data to be sent
  1357.   * @retval HAL status
  1358.   */
  1359. HAL_StatusTypeDef HAL_I2C_Slave_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
  1360. {
  1361.   if(hi2c->State == HAL_I2C_STATE_READY)
  1362.   {
  1363.     if((pData == NULL) || (Size == 0))
  1364.     {
  1365.       return  HAL_ERROR;
  1366.     }
  1367.     /* Process Locked */
  1368.     __HAL_LOCK(hi2c);
  1369.    
  1370.     hi2c->State = HAL_I2C_STATE_BUSY_TX;
  1371.     hi2c->Mode = HAL_I2C_MODE_SLAVE;
  1372.     hi2c->ErrorCode   = HAL_I2C_ERROR_NONE;
  1373.    
  1374.     hi2c->pBuffPtr = pData;
  1375.     hi2c->XferCount = Size;
  1376.     hi2c->XferSize = Size;
  1377.    
  1378.     /* Set the I2C DMA transfer complete callback */
  1379.     hi2c->hdmatx->XferCpltCallback = I2C_DMASlaveTransmitCplt;
  1380.    
  1381.     /* Set the DMA error callback */
  1382.     hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
  1383.    
  1384.     /* Enable the DMA channel */
  1385.     HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
  1386.    
  1387.     /* Enable Address Acknowledge */
  1388.     hi2c->Instance->CR2 &= ~I2C_CR2_NACK;

  1389.     /* Wait until ADDR flag is set */
  1390.     if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, I2C_TIMEOUT_ADDR) != HAL_OK)      
  1391.     {
  1392.       /* Disable Address Acknowledge */
  1393.       hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1394.       return HAL_TIMEOUT;
  1395.     }

  1396.     /* Clear ADDR flag */
  1397.     __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
  1398.    
  1399.     /* If 10bits addressing mode is selected */
  1400.     if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT)
  1401.     {
  1402.       /* Wait until ADDR flag is set */
  1403.       if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, I2C_TIMEOUT_ADDR) != HAL_OK)      
  1404.       {
  1405.         /* Disable Address Acknowledge */
  1406.         hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1407.         return HAL_TIMEOUT;
  1408.       }

  1409.       /* Clear ADDR flag */
  1410.       __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
  1411.     }
  1412.    
  1413.     /* Wait until DIR flag is set Transmitter mode */
  1414.     if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_DIR, RESET, I2C_TIMEOUT_BUSY) != HAL_OK)      
  1415.     {
  1416.       /* Disable Address Acknowledge */
  1417.       hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1418.       return HAL_TIMEOUT;
  1419.     }
  1420.       
  1421.     /* Enable DMA Request */
  1422.     hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
  1423.    
  1424.     /* Process Unlocked */
  1425.     __HAL_UNLOCK(hi2c);
  1426.    
  1427.     return HAL_OK;
  1428.   }
  1429.   else
  1430.   {
  1431.     return HAL_BUSY;
  1432.   }
  1433. }

  1434. /**
  1435.   * @brief  Receive in slave mode an amount of data in non-blocking mode with DMA
  1436.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1437.   *                the configuration information for the specified I2C.
  1438.   * @param  pData Pointer to data buffer
  1439.   * @param  Size Amount of data to be sent
  1440.   * @retval HAL status
  1441.   */
  1442. HAL_StatusTypeDef HAL_I2C_Slave_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
  1443. {
  1444.   if(hi2c->State == HAL_I2C_STATE_READY)
  1445.   {
  1446.     if((pData == NULL) || (Size == 0))
  1447.     {
  1448.       return  HAL_ERROR;                                    
  1449.     }   
  1450.     /* Process Locked */
  1451.     __HAL_LOCK(hi2c);
  1452.    
  1453.     hi2c->State = HAL_I2C_STATE_BUSY_RX;
  1454.     hi2c->Mode = HAL_I2C_MODE_SLAVE;
  1455.     hi2c->ErrorCode   = HAL_I2C_ERROR_NONE;
  1456.    
  1457.     hi2c->pBuffPtr = pData;
  1458.     hi2c->XferSize = Size;
  1459.     hi2c->XferCount = Size;
  1460.    
  1461.     /* Set the I2C DMA transfer complete callback */
  1462.     hi2c->hdmarx->XferCpltCallback = I2C_DMASlaveReceiveCplt;
  1463.    
  1464.     /* Set the DMA error callback */
  1465.     hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
  1466.    
  1467.     /* Enable the DMA channel */
  1468.     HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData, Size);
  1469.    
  1470.     /* Enable Address Acknowledge */
  1471.     hi2c->Instance->CR2 &= ~I2C_CR2_NACK;

  1472.     /* Wait until ADDR flag is set */
  1473.     if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, I2C_TIMEOUT_ADDR) != HAL_OK)      
  1474.     {
  1475.       /* Disable Address Acknowledge */
  1476.       hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1477.       return HAL_TIMEOUT;
  1478.     }

  1479.     /* Clear ADDR flag */
  1480.     __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
  1481.    
  1482.     /* Wait until DIR flag is set Receiver mode */
  1483.     if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_DIR, SET, I2C_TIMEOUT_DIR) != HAL_OK)      
  1484.     {
  1485.       /* Disable Address Acknowledge */
  1486.       hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1487.       return HAL_TIMEOUT;
  1488.     }

  1489.     /* Enable DMA Request */
  1490.     hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;  
  1491.    
  1492.     /* Process Unlocked */
  1493.     __HAL_UNLOCK(hi2c);
  1494.    
  1495.     return HAL_OK;
  1496.   }
  1497.   else
  1498.   {
  1499.     return HAL_BUSY;
  1500.   }
  1501. }
  1502. /**
  1503.   * @brief  Write an amount of data in blocking mode to a specific memory address
  1504.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1505.   *                the configuration information for the specified I2C.
  1506.   * @param  DevAddress Target device address
  1507.   * @param  MemAddress Internal memory address
  1508.   * @param  MemAddSize Size of internal memory address
  1509.   * @param  pData Pointer to data buffer
  1510.   * @param  Size Amount of data to be sent
  1511.   * @param  Timeout Timeout duration
  1512.   * @retval HAL status
  1513.   */
  1514. 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)
  1515. {
  1516.   uint32_t Sizetmp = 0;

  1517.   /* Check the parameters */
  1518.   assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  1519.   
  1520.   if(hi2c->State == HAL_I2C_STATE_READY)
  1521.   {
  1522.     if((pData == NULL) || (Size == 0))
  1523.     {
  1524.       return  HAL_ERROR;                                    
  1525.     }

  1526.     if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  1527.     {
  1528.       return HAL_BUSY;
  1529.     }

  1530.     /* Process Locked */
  1531.     __HAL_LOCK(hi2c);
  1532.    
  1533.     hi2c->State = HAL_I2C_STATE_BUSY_TX;
  1534.     hi2c->Mode = HAL_I2C_MODE_MEM;
  1535.     hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1536.    
  1537.     /* Send Slave Address and Memory Address */
  1538.     if(I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, Timeout) != HAL_OK)
  1539.     {
  1540.       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  1541.       {
  1542.         /* Process Unlocked */
  1543.         __HAL_UNLOCK(hi2c);
  1544.         return HAL_ERROR;
  1545.       }
  1546.       else
  1547.       {
  1548.         /* Process Unlocked */
  1549.         __HAL_UNLOCK(hi2c);
  1550.         return HAL_TIMEOUT;
  1551.       }
  1552.     }

  1553.     /* Set NBYTES to write and reload if size > MAX_NBYTE_SIZE */
  1554.     /* Size > MAX_NBYTE_SIZE, need to set RELOAD bit */
  1555.     if(Size > MAX_NBYTE_SIZE)
  1556.     {
  1557.       I2C_TransferConfig(hi2c, DevAddress, MAX_NBYTE_SIZE, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  1558.       Sizetmp = MAX_NBYTE_SIZE;
  1559.     }
  1560.     else
  1561.     {
  1562.       I2C_TransferConfig(hi2c, DevAddress, Size, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  1563.       Sizetmp = Size;
  1564.     }
  1565.    
  1566.     do
  1567.     {
  1568.       /* Wait until TXIS flag is set */
  1569.       if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
  1570.       {
  1571.         if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  1572.         {
  1573.           return HAL_ERROR;
  1574.         }
  1575.         else
  1576.         {
  1577.           return HAL_TIMEOUT;
  1578.         }
  1579.       }
  1580.      
  1581.       /* Write data to DR */
  1582.       hi2c->Instance->TXDR = (*pData++);
  1583.       Sizetmp--;
  1584.       Size--;

  1585.       if((Sizetmp == 0)&&(Size!=0))
  1586.       {
  1587.         /* Wait until TCR flag is set */
  1588.         if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout) != HAL_OK)      
  1589.         {
  1590.           return HAL_TIMEOUT;
  1591.         }

  1592.         
  1593.         if(Size > MAX_NBYTE_SIZE)
  1594.         {
  1595.           I2C_TransferConfig(hi2c, DevAddress, MAX_NBYTE_SIZE, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  1596.           Sizetmp = MAX_NBYTE_SIZE;
  1597.         }
  1598.         else
  1599.         {
  1600.           I2C_TransferConfig(hi2c, DevAddress, Size, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  1601.           Sizetmp = Size;
  1602.         }
  1603.       }
  1604.       
  1605.     }while(Size > 0);
  1606.    
  1607.     /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  1608.     /* Wait until STOPF flag is reset */
  1609.     if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  1610.     {
  1611.       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  1612.       {
  1613.         return HAL_ERROR;
  1614.       }
  1615.       else
  1616.       {
  1617.         return HAL_TIMEOUT;
  1618.       }
  1619.     }
  1620.    
  1621.     /* Clear STOP Flag */
  1622.     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  1623.          
  1624.     /* Clear Configuration Register 2 */
  1625.     I2C_RESET_CR2(hi2c);

  1626.     hi2c->State = HAL_I2C_STATE_READY;           
  1627.     hi2c->Mode = HAL_I2C_MODE_NONE;
  1628.    
  1629.     /* Process Unlocked */
  1630.     __HAL_UNLOCK(hi2c);
  1631.    
  1632.     return HAL_OK;
  1633.   }
  1634.   else
  1635.   {
  1636.     return HAL_BUSY;
  1637.   }
  1638. }

  1639. /**
  1640.   * @brief  Read an amount of data in blocking mode from a specific memory address
  1641.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1642.   *                the configuration information for the specified I2C.
  1643.   * @param  DevAddress Target device address
  1644.   * @param  MemAddress Internal memory address
  1645.   * @param  MemAddSize Size of internal memory address
  1646.   * @param  pData Pointer to data buffer
  1647.   * @param  Size Amount of data to be sent
  1648.   * @param  Timeout Timeout duration
  1649.   * @retval HAL status
  1650.   */
  1651. 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)
  1652. {
  1653.   uint32_t Sizetmp = 0;

  1654.   /* Check the parameters */
  1655.   assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  1656.   
  1657.   if(hi2c->State == HAL_I2C_STATE_READY)
  1658.   {   
  1659.     if((pData == NULL) || (Size == 0))
  1660.     {
  1661.       return  HAL_ERROR;                                    
  1662.     }

  1663.     if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  1664.     {
  1665.       return HAL_BUSY;
  1666.     }

  1667.     /* Process Locked */
  1668.     __HAL_LOCK(hi2c);
  1669.    
  1670.     hi2c->State = HAL_I2C_STATE_BUSY_RX;
  1671.     hi2c->Mode = HAL_I2C_MODE_MEM;
  1672.     hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1673.    
  1674.     /* Send Slave Address and Memory Address */
  1675.     if(I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, Timeout) != HAL_OK)
  1676.     {
  1677.       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  1678.       {
  1679.         /* Process Unlocked */
  1680.         __HAL_UNLOCK(hi2c);
  1681.         return HAL_ERROR;
  1682.       }
  1683.       else
  1684.       {
  1685.         /* Process Unlocked */
  1686.         __HAL_UNLOCK(hi2c);
  1687.         return HAL_TIMEOUT;
  1688.       }
  1689.     }

  1690.     /* Send Slave Address */
  1691.     /* Set NBYTES to write and reload if size > MAX_NBYTE_SIZE and generate RESTART */
  1692.     /* Size > MAX_NBYTE_SIZE, need to set RELOAD bit */
  1693.     if(Size > MAX_NBYTE_SIZE)
  1694.     {
  1695.       I2C_TransferConfig(hi2c, DevAddress, MAX_NBYTE_SIZE, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
  1696.       Sizetmp = MAX_NBYTE_SIZE;
  1697.     }
  1698.     else
  1699.     {
  1700.       I2C_TransferConfig(hi2c, DevAddress, Size, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
  1701.       Sizetmp = Size;
  1702.     }
  1703.    
  1704.     do
  1705.     {  
  1706.       /* Wait until RXNE flag is set */
  1707.       if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, Timeout) != HAL_OK)      
  1708.       {
  1709.         return HAL_TIMEOUT;
  1710.       }
  1711.          
  1712.       /* Read data from RXDR */
  1713.       (*pData++) = hi2c->Instance->RXDR;

  1714.       /* Decrement the Size counter */
  1715.       Sizetmp--;
  1716.       Size--;   

  1717.       if((Sizetmp == 0)&&(Size!=0))
  1718.       {
  1719.         /* Wait until TCR flag is set */
  1720.         if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout) != HAL_OK)      
  1721.         {
  1722.           return HAL_TIMEOUT;
  1723.         }
  1724.         
  1725.         if(Size > MAX_NBYTE_SIZE)
  1726.         {
  1727.           I2C_TransferConfig(hi2c, DevAddress, MAX_NBYTE_SIZE, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  1728.           Sizetmp = MAX_NBYTE_SIZE;
  1729.         }
  1730.         else
  1731.         {
  1732.           I2C_TransferConfig(hi2c, DevAddress, Size, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  1733.           Sizetmp = Size;
  1734.         }
  1735.       }

  1736.     }while(Size > 0);

  1737.     /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  1738.     /* Wait until STOPF flag is reset */
  1739.     if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  1740.     {
  1741.       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  1742.       {
  1743.         return HAL_ERROR;
  1744.       }
  1745.       else
  1746.       {
  1747.         return HAL_TIMEOUT;
  1748.       }
  1749.     }

  1750.     /* Clear STOP Flag */
  1751.     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  1752.          
  1753.     /* Clear Configuration Register 2 */
  1754.     I2C_RESET_CR2(hi2c);
  1755.    
  1756.     hi2c->State = HAL_I2C_STATE_READY;
  1757.     hi2c->Mode = HAL_I2C_MODE_NONE;
  1758.    
  1759.     /* Process Unlocked */
  1760.     __HAL_UNLOCK(hi2c);
  1761.    
  1762.     return HAL_OK;
  1763.   }
  1764.   else
  1765.   {
  1766.     return HAL_BUSY;
  1767.   }
  1768. }
  1769. /**
  1770.   * @brief  Write an amount of data in non-blocking mode with Interrupt to a specific memory address
  1771.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1772.   *                the configuration information for the specified I2C.
  1773.   * @param  DevAddress Target device address
  1774.   * @param  MemAddress Internal memory address
  1775.   * @param  MemAddSize Size of internal memory address
  1776.   * @param  pData Pointer to data buffer
  1777.   * @param  Size Amount of data to be sent
  1778.   * @retval HAL status
  1779.   */
  1780. 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)
  1781. {
  1782.   /* Check the parameters */
  1783.   assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  1784.   
  1785.   if(hi2c->State == HAL_I2C_STATE_READY)
  1786.   {
  1787.     if((pData == NULL) || (Size == 0))
  1788.     {
  1789.       return  HAL_ERROR;                                    
  1790.     }
  1791.    
  1792.     if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  1793.     {
  1794.       return HAL_BUSY;
  1795.     }

  1796.     /* Process Locked */
  1797.     __HAL_LOCK(hi2c);
  1798.    
  1799.     hi2c->State = HAL_I2C_STATE_BUSY_TX;
  1800.     hi2c->Mode = HAL_I2C_MODE_MEM;
  1801.     hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1802.    
  1803.     hi2c->pBuffPtr = pData;
  1804.     hi2c->XferCount = Size;
  1805.     if(Size > MAX_NBYTE_SIZE)
  1806.     {
  1807.       hi2c->XferSize = MAX_NBYTE_SIZE;
  1808.     }
  1809.     else
  1810.     {
  1811.       hi2c->XferSize = Size;
  1812.     }
  1813.    
  1814.     /* Send Slave Address and Memory Address */
  1815.     if(I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG) != HAL_OK)
  1816.     {
  1817.       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  1818.       {
  1819.         /* Process Unlocked */
  1820.         __HAL_UNLOCK(hi2c);
  1821.         return HAL_ERROR;
  1822.       }
  1823.       else
  1824.       {
  1825.         /* Process Unlocked */
  1826.         __HAL_UNLOCK(hi2c);
  1827.         return HAL_TIMEOUT;
  1828.       }
  1829.     }

  1830.     /* Set NBYTES to write and reload if size > MAX_NBYTE_SIZE */
  1831.     /* Size > MAX_NBYTE_SIZE, need to set RELOAD bit */
  1832.     if( (hi2c->XferSize == MAX_NBYTE_SIZE) && (hi2c->XferSize < hi2c->XferCount) )
  1833.     {
  1834.       I2C_TransferConfig(hi2c, DevAddress, hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  1835.     }
  1836.     else
  1837.     {
  1838.       I2C_TransferConfig(hi2c, DevAddress, hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  1839.     }  

  1840.     /* Process Unlocked */
  1841.     __HAL_UNLOCK(hi2c);

  1842.     /* Note : The I2C interrupts must be enabled after unlocking current process
  1843.               to avoid the risk of I2C interrupt handle execution before current
  1844.               process unlock */
  1845.    
  1846.     /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  1847.     /* possible to enable all of these */
  1848.     /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  1849.     __HAL_I2C_ENABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_TXI );
  1850.    
  1851.     return HAL_OK;
  1852.   }
  1853.   else
  1854.   {
  1855.     return HAL_BUSY;
  1856.   }
  1857. }

  1858. /**
  1859.   * @brief  Read an amount of data in non-blocking mode with Interrupt from a specific memory address
  1860.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1861.   *                the configuration information for the specified I2C.
  1862.   * @param  DevAddress Target device address
  1863.   * @param  MemAddress Internal memory address
  1864.   * @param  MemAddSize Size of internal memory address
  1865.   * @param  pData Pointer to data buffer
  1866.   * @param  Size Amount of data to be sent
  1867.   * @retval HAL status
  1868.   */
  1869. 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)
  1870. {
  1871.   /* Check the parameters */
  1872.   assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  1873.   
  1874.   if(hi2c->State == HAL_I2C_STATE_READY)
  1875.   {
  1876.     if((pData == NULL) || (Size == 0))
  1877.     {
  1878.       return  HAL_ERROR;                                    
  1879.     }
  1880.    
  1881.     if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  1882.     {
  1883.       return HAL_BUSY;
  1884.     }

  1885.     /* Process Locked */
  1886.     __HAL_LOCK(hi2c);
  1887.    
  1888.     hi2c->State = HAL_I2C_STATE_BUSY_RX;
  1889.     hi2c->Mode = HAL_I2C_MODE_MEM;
  1890.    
  1891.     hi2c->pBuffPtr = pData;
  1892.     hi2c->XferCount = Size;
  1893.     if(Size > MAX_NBYTE_SIZE)
  1894.     {
  1895.       hi2c->XferSize = MAX_NBYTE_SIZE;
  1896.     }
  1897.     else
  1898.     {
  1899.       hi2c->XferSize = Size;
  1900.     }
  1901.    
  1902.     /* Send Slave Address and Memory Address */
  1903.     if(I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG) != HAL_OK)
  1904.     {
  1905.       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  1906.       {
  1907.         /* Process Unlocked */
  1908.         __HAL_UNLOCK(hi2c);
  1909.         return HAL_ERROR;
  1910.       }
  1911.       else
  1912.       {
  1913.         /* Process Unlocked */
  1914.         __HAL_UNLOCK(hi2c);
  1915.         return HAL_TIMEOUT;
  1916.       }
  1917.     }
  1918.       
  1919.     /* Set NBYTES to write and reload if size > MAX_NBYTE_SIZE and generate RESTART */
  1920.     /* Size > MAX_NBYTE_SIZE, need to set RELOAD bit */
  1921.     if( (hi2c->XferSize == MAX_NBYTE_SIZE) && (hi2c->XferSize < hi2c->XferCount) )
  1922.     {
  1923.       I2C_TransferConfig(hi2c, DevAddress, hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
  1924.     }
  1925.     else
  1926.     {
  1927.       I2C_TransferConfig(hi2c, DevAddress, hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
  1928.     }

  1929.     /* Process Unlocked */
  1930.     __HAL_UNLOCK(hi2c);

  1931.     /* Note : The I2C interrupts must be enabled after unlocking current process
  1932.               to avoid the risk of I2C interrupt handle execution before current
  1933.               process unlock */
  1934.    
  1935.     /* Enable ERR, TC, STOP, NACK, RXI interrupt */
  1936.     /* possible to enable all of these */
  1937.     /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  1938.     __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_RXI );
  1939.    
  1940.     return HAL_OK;
  1941.   }
  1942.   else
  1943.   {
  1944.     return HAL_BUSY;
  1945.   }   
  1946. }
  1947. /**
  1948.   * @brief  Write an amount of data in non-blocking mode with DMA to a specific memory address
  1949.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1950.   *                the configuration information for the specified I2C.
  1951.   * @param  DevAddress Target device address
  1952.   * @param  MemAddress Internal memory address
  1953.   * @param  MemAddSize Size of internal memory address
  1954.   * @param  pData Pointer to data buffer
  1955.   * @param  Size Amount of data to be sent
  1956.   * @retval HAL status
  1957.   */
  1958. 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)
  1959. {
  1960.   /* Check the parameters */
  1961.   assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  1962.   
  1963.   if(hi2c->State == HAL_I2C_STATE_READY)
  1964.   {
  1965.     if((pData == NULL) || (Size == 0))
  1966.     {
  1967.       return  HAL_ERROR;                                    
  1968.     }
  1969.    
  1970.     if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  1971.     {
  1972.       return HAL_BUSY;
  1973.     }

  1974.     /* Process Locked */
  1975.     __HAL_LOCK(hi2c);
  1976.    
  1977.     hi2c->State = HAL_I2C_STATE_BUSY_TX;
  1978.     hi2c->Mode = HAL_I2C_MODE_MEM;
  1979.     hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1980.    
  1981.     hi2c->pBuffPtr = pData;
  1982.     hi2c->XferCount = Size;
  1983.     if(Size > MAX_NBYTE_SIZE)
  1984.     {
  1985.       hi2c->XferSize = MAX_NBYTE_SIZE;
  1986.     }
  1987.     else
  1988.     {
  1989.       hi2c->XferSize = Size;
  1990.     }
  1991.    
  1992.     /* Set the I2C DMA transfer complete callback */
  1993.     hi2c->hdmatx->XferCpltCallback = I2C_DMAMemTransmitCplt;
  1994.    
  1995.     /* Set the DMA error callback */
  1996.     hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
  1997.    
  1998.     /* Enable the DMA channel */
  1999.     HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
  2000.    
  2001.     /* Send Slave Address and Memory Address */
  2002.     if(I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG) != HAL_OK)
  2003.     {
  2004.       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2005.       {
  2006.         /* Process Unlocked */
  2007.         __HAL_UNLOCK(hi2c);
  2008.         return HAL_ERROR;
  2009.       }
  2010.       else
  2011.       {
  2012.         /* Process Unlocked */
  2013.         __HAL_UNLOCK(hi2c);
  2014.         return HAL_TIMEOUT;
  2015.       }
  2016.     }
  2017.    
  2018.     /* Send Slave Address */
  2019.     /* Set NBYTES to write and reload if size > MAX_NBYTE_SIZE */
  2020.     if( (hi2c->XferSize == MAX_NBYTE_SIZE) && (hi2c->XferSize < hi2c->XferCount) )
  2021.     {
  2022.       I2C_TransferConfig(hi2c, DevAddress, hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  2023.     }
  2024.     else
  2025.     {
  2026.       I2C_TransferConfig(hi2c, DevAddress, hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  2027.     }
  2028.    
  2029.     /* Wait until TXIS flag is set */
  2030.     if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, I2C_TIMEOUT_TXIS) != HAL_OK)
  2031.     {
  2032.       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2033.       {
  2034.         return HAL_ERROR;
  2035.       }
  2036.       else
  2037.       {
  2038.         return HAL_TIMEOUT;
  2039.       }
  2040.     }

  2041.     /* Enable DMA Request */
  2042.     hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;  
  2043.    
  2044.     /* Process Unlocked */
  2045.     __HAL_UNLOCK(hi2c);
  2046.    
  2047.     return HAL_OK;
  2048.   }
  2049.   else
  2050.   {
  2051.     return HAL_BUSY;
  2052.   }
  2053. }

  2054. /**
  2055.   * @brief  Reads an amount of data in non-blocking mode with DMA from a specific memory address.
  2056.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2057.   *                the configuration information for the specified I2C.
  2058.   * @param  DevAddress Target device address
  2059.   * @param  MemAddress Internal memory address
  2060.   * @param  MemAddSize Size of internal memory address
  2061.   * @param  pData Pointer to data buffer
  2062.   * @param  Size Amount of data to be read
  2063.   * @retval HAL status
  2064.   */
  2065. 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)
  2066. {
  2067.   /* Check the parameters */
  2068.   assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  2069.   
  2070.   if(hi2c->State == HAL_I2C_STATE_READY)
  2071.   {
  2072.     if((pData == NULL) || (Size == 0))
  2073.     {
  2074.       return  HAL_ERROR;                                    
  2075.     }

  2076.     if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  2077.     {
  2078.       return HAL_BUSY;
  2079.     }

  2080.     /* Process Locked */
  2081.     __HAL_LOCK(hi2c);
  2082.    
  2083.     hi2c->State = HAL_I2C_STATE_BUSY_RX;
  2084.     hi2c->Mode = HAL_I2C_MODE_MEM;
  2085.    
  2086.     hi2c->pBuffPtr = pData;
  2087.     hi2c->XferCount = Size;
  2088.     if(Size > MAX_NBYTE_SIZE)
  2089.     {
  2090.       hi2c->XferSize = MAX_NBYTE_SIZE;
  2091.     }
  2092.     else
  2093.     {
  2094.       hi2c->XferSize = Size;
  2095.     }

  2096.     /* Set the I2C DMA transfer complete callback */
  2097.     hi2c->hdmarx->XferCpltCallback = I2C_DMAMemReceiveCplt;
  2098.    
  2099.     /* Set the DMA error callback */
  2100.     hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
  2101.    
  2102.     /* Enable the DMA channel */
  2103.     HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData, hi2c->XferSize);
  2104.    
  2105.     /* Send Slave Address and Memory Address */
  2106.     if(I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG) != HAL_OK)
  2107.     {
  2108.       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2109.       {
  2110.         /* Process Unlocked */
  2111.         __HAL_UNLOCK(hi2c);
  2112.         return HAL_ERROR;
  2113.       }
  2114.       else
  2115.       {
  2116.         /* Process Unlocked */
  2117.         __HAL_UNLOCK(hi2c);
  2118.         return HAL_TIMEOUT;
  2119.       }
  2120.     }
  2121.    
  2122.     /* Set NBYTES to write and reload if size > MAX_NBYTE_SIZE and generate RESTART */
  2123.     if( (hi2c->XferSize == MAX_NBYTE_SIZE) && (hi2c->XferSize < hi2c->XferCount) )
  2124.     {
  2125.       I2C_TransferConfig(hi2c, DevAddress, hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
  2126.     }
  2127.     else
  2128.     {
  2129.       I2C_TransferConfig(hi2c, DevAddress, hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
  2130.     }

  2131.     /* Wait until RXNE flag is set */
  2132.     if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, I2C_TIMEOUT_RXNE) != HAL_OK)      
  2133.     {
  2134.       return HAL_TIMEOUT;
  2135.     }
  2136.    
  2137.     /* Enable DMA Request */
  2138.     hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;  
  2139.    
  2140.     /* Process Unlocked */
  2141.     __HAL_UNLOCK(hi2c);
  2142.    
  2143.     return HAL_OK;
  2144.   }
  2145.   else
  2146.   {
  2147.     return HAL_BUSY;
  2148.   }
  2149. }

  2150. /**
  2151.   * @brief  Checks if target device is ready for communication.
  2152.   * @note   This function is used with Memory devices
  2153.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2154.   *                the configuration information for the specified I2C.
  2155.   * @param  DevAddress Target device address
  2156.   * @param  Trials Number of trials
  2157.   * @param  Timeout Timeout duration
  2158.   * @retval HAL status
  2159.   */
  2160. HAL_StatusTypeDef HAL_I2C_IsDeviceReady(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout)
  2161. {  
  2162.   uint32_t tickstart = 0;
  2163.   
  2164.   __IO uint32_t I2C_Trials = 0;

  2165.   if(hi2c->State == HAL_I2C_STATE_READY)
  2166.   {
  2167.     if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  2168.     {
  2169.       return HAL_BUSY;
  2170.     }

  2171.     /* Process Locked */
  2172.     __HAL_LOCK(hi2c);
  2173.    
  2174.     hi2c->State = HAL_I2C_STATE_BUSY;
  2175.     hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2176.    
  2177.     do
  2178.     {
  2179.       /* Generate Start */
  2180.       hi2c->Instance->CR2 = I2C_GENERATE_START(hi2c->Init.AddressingMode,DevAddress);
  2181.       
  2182.       /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  2183.       /* Wait until STOPF flag is set or a NACK flag is set*/
  2184.       tickstart = HAL_GetTick();
  2185.       while((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET) && (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == RESET) && (hi2c->State != HAL_I2C_STATE_TIMEOUT))
  2186.       {
  2187.         if(Timeout != HAL_MAX_DELAY)
  2188.         {
  2189.           if((Timeout == 0) || ((HAL_GetTick() - tickstart) > Timeout))
  2190.           {
  2191.             /* Device is ready */
  2192.             hi2c->State = HAL_I2C_STATE_READY;
  2193.             /* Process Unlocked */
  2194.             __HAL_UNLOCK(hi2c);
  2195.             return HAL_TIMEOUT;
  2196.           }
  2197.         }
  2198.       }
  2199.       
  2200.       /* Check if the NACKF flag has not been set */
  2201.       if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == RESET)
  2202.       {
  2203.         /* Wait until STOPF flag is reset */
  2204.         if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout) != HAL_OK)
  2205.         {
  2206.           return HAL_TIMEOUT;
  2207.         }
  2208.         
  2209.         /* Clear STOP Flag */
  2210.         __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);

  2211.         /* Device is ready */
  2212.         hi2c->State = HAL_I2C_STATE_READY;
  2213.         
  2214.         /* Process Unlocked */
  2215.         __HAL_UNLOCK(hi2c);
  2216.         
  2217.         return HAL_OK;
  2218.       }
  2219.       else
  2220.       {
  2221.         /* Wait until STOPF flag is reset */
  2222.         if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout) != HAL_OK)
  2223.         {
  2224.           return HAL_TIMEOUT;
  2225.         }

  2226.         /* Clear NACK Flag */
  2227.         __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);

  2228.         /* Clear STOP Flag, auto generated with autoend*/
  2229.         __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  2230.       }
  2231.       
  2232.       /* Check if the maximum allowed number of trials has been reached */
  2233.       if (I2C_Trials++ == Trials)
  2234.       {
  2235.         /* Generate Stop */
  2236.         hi2c->Instance->CR2 |= I2C_CR2_STOP;
  2237.         
  2238.         /* Wait until STOPF flag is reset */
  2239.         if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout) != HAL_OK)
  2240.         {
  2241.           return HAL_TIMEOUT;
  2242.         }
  2243.         
  2244.         /* Clear STOP Flag */
  2245.         __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  2246.       }      
  2247.     }while(I2C_Trials < Trials);

  2248.     hi2c->State = HAL_I2C_STATE_READY;

  2249.     /* Process Unlocked */
  2250.     __HAL_UNLOCK(hi2c);
  2251.         
  2252.     return HAL_TIMEOUT;
  2253.   }
  2254.   else
  2255.   {
  2256.     return HAL_BUSY;
  2257.   }
  2258. }
  2259. /**
  2260.   * @}
  2261.   */

  2262. /** @defgroup I2C_IRQ_Handler_and_Callbacks IRQ Handler and Callbacks
  2263. * @{
  2264. */   

  2265. /**
  2266.   * @brief  This function handles I2C event interrupt request.
  2267.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2268.   *                the configuration information for the specified I2C.
  2269.   * @retval None
  2270.   */
  2271. void HAL_I2C_EV_IRQHandler(I2C_HandleTypeDef *hi2c)
  2272. {
  2273.   /* I2C in mode Transmitter ---------------------------------------------------*/
  2274.   if (((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TCR) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TC) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == SET)) && (__HAL_I2C_GET_IT_SOURCE(hi2c, (I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_TXI | I2C_IT_ADDRI)) == SET))
  2275.   {     
  2276.     /* Slave mode selected */
  2277.     if (hi2c->Mode  == HAL_I2C_MODE_SLAVE)
  2278.     {
  2279.       I2C_SlaveTransmit_ISR(hi2c);
  2280.     }
  2281.   }
  2282.    
  2283.   if (((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TCR) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TC) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)) && (__HAL_I2C_GET_IT_SOURCE(hi2c, (I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_TXI)) == SET))
  2284.   {     
  2285.     /* Master or Memory mode selected */
  2286.     if ((hi2c->Mode == HAL_I2C_MODE_MASTER) || (hi2c->Mode == HAL_I2C_MODE_MEM))
  2287.     {
  2288.       I2C_MasterTransmit_ISR(hi2c);
  2289.     }
  2290.   }
  2291.    
  2292.   /* I2C in mode Receiver ----------------------------------------------------*/
  2293.   if (((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TCR) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TC) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == SET)) && (__HAL_I2C_GET_IT_SOURCE(hi2c, (I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_RXI | I2C_IT_ADDRI)) == SET))
  2294.   {
  2295.     /* Slave mode selected */
  2296.     if (hi2c->Mode  == HAL_I2C_MODE_SLAVE)
  2297.     {
  2298.       I2C_SlaveReceive_ISR(hi2c);
  2299.     }
  2300.   }
  2301.   if (((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TCR) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TC) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)) && (__HAL_I2C_GET_IT_SOURCE(hi2c, (I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_RXI)) == SET))
  2302.   {
  2303.     /* Master or Memory mode selected */
  2304.     if ((hi2c->Mode == HAL_I2C_MODE_MASTER) || (hi2c->Mode == HAL_I2C_MODE_MEM))
  2305.     {
  2306.       I2C_MasterReceive_ISR(hi2c);
  2307.     }
  2308.   }
  2309. }

  2310. /**
  2311.   * @brief  This function handles I2C error interrupt request.
  2312.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2313.   *                the configuration information for the specified I2C.
  2314.   * @retval None
  2315.   */
  2316. void HAL_I2C_ER_IRQHandler(I2C_HandleTypeDef *hi2c)
  2317. {
  2318.   /* I2C Bus error interrupt occurred ------------------------------------*/
  2319.   if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BERR) == SET) && (__HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_ERRI) == SET))
  2320.   {
  2321.     hi2c->ErrorCode |= HAL_I2C_ERROR_BERR;
  2322.    
  2323.     /* Clear BERR flag */
  2324.     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_BERR);
  2325.   }
  2326.   
  2327.   /* I2C Over-Run/Under-Run interrupt occurred ----------------------------------------*/
  2328.   if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_OVR) == SET) && (__HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_ERRI) == SET))
  2329.   {
  2330.     hi2c->ErrorCode |= HAL_I2C_ERROR_OVR;

  2331.     /* Clear OVR flag */
  2332.     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_OVR);
  2333.   }

  2334.   /* I2C Arbitration Loss error interrupt occurred -------------------------------------*/
  2335.   if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ARLO) == SET) && (__HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_ERRI) == SET))
  2336.   {
  2337.     hi2c->ErrorCode |= HAL_I2C_ERROR_ARLO;

  2338.     /* Clear ARLO flag */
  2339.     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ARLO);
  2340.   }

  2341.   /* Call the Error Callback in case of Error detected */
  2342.   if((hi2c->ErrorCode & (HAL_I2C_ERROR_BERR | HAL_I2C_ERROR_OVR | HAL_I2C_ERROR_ARLO)) !=  HAL_I2C_ERROR_NONE)
  2343.   {
  2344.     hi2c->State = HAL_I2C_STATE_READY;
  2345.    
  2346.     HAL_I2C_ErrorCallback(hi2c);
  2347.   }
  2348. }

  2349. /**
  2350.   * @brief  Master Tx Transfer completed callback.
  2351.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2352.   *                the configuration information for the specified I2C.
  2353.   * @retval None
  2354.   */
  2355. __weak void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c)
  2356. {
  2357.   /* Prevent unused argument(s) compilation warning */
  2358.   UNUSED(hi2c);

  2359.   /* NOTE : This function should not be modified, when the callback is needed,
  2360.             the HAL_I2C_TxCpltCallback could be implemented in the user file
  2361.    */
  2362. }

  2363. /**
  2364.   * @brief  Master Rx Transfer completed callback.
  2365.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2366.   *                the configuration information for the specified I2C.
  2367.   * @retval None
  2368.   */
  2369. __weak void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c)
  2370. {
  2371.   /* Prevent unused argument(s) compilation warning */
  2372.   UNUSED(hi2c);

  2373.   /* NOTE : This function should not be modified, when the callback is needed,
  2374.             the HAL_I2C_TxCpltCallback could be implemented in the user file
  2375.    */
  2376. }

  2377. /** @brief  Slave Tx Transfer completed callback.
  2378.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2379.   *                the configuration information for the specified I2C.
  2380.   * @retval None
  2381.   */
  2382. __weak void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c)
  2383. {
  2384.   /* Prevent unused argument(s) compilation warning */
  2385.   UNUSED(hi2c);

  2386.   /* NOTE : This function should not be modified, when the callback is needed,
  2387.             the HAL_I2C_TxCpltCallback could be implemented in the user file
  2388.    */
  2389. }

  2390. /**
  2391.   * @brief  Slave Rx Transfer completed callback.
  2392.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2393.   *                the configuration information for the specified I2C.
  2394.   * @retval None
  2395.   */
  2396. __weak void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c)
  2397. {
  2398.   /* Prevent unused argument(s) compilation warning */
  2399.   UNUSED(hi2c);

  2400.   /* NOTE : This function should not be modified, when the callback is needed,
  2401.             the HAL_I2C_TxCpltCallback could be implemented in the user file
  2402.    */
  2403. }

  2404. /**
  2405.   * @brief  Memory Tx Transfer completed callback.
  2406.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2407.   *                the configuration information for the specified I2C.
  2408.   * @retval None
  2409.   */
  2410. __weak void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef *hi2c)
  2411. {
  2412.   /* Prevent unused argument(s) compilation warning */
  2413.   UNUSED(hi2c);

  2414.   /* NOTE : This function should not be modified, when the callback is needed,
  2415.             the HAL_I2C_TxCpltCallback could be implemented in the user file
  2416.    */
  2417. }

  2418. /**
  2419.   * @brief  Memory Rx Transfer completed callback.
  2420.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2421.   *                the configuration information for the specified I2C.
  2422.   * @retval None
  2423.   */
  2424. __weak void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c)
  2425. {
  2426.   /* Prevent unused argument(s) compilation warning */
  2427.   UNUSED(hi2c);

  2428.   /* NOTE : This function should not be modified, when the callback is needed,
  2429.             the HAL_I2C_TxCpltCallback could be implemented in the user file
  2430.    */
  2431. }

  2432. /**
  2433.   * @brief  I2C error callback.
  2434.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2435.   *                the configuration information for the specified I2C.
  2436.   * @retval None
  2437.   */
  2438. __weak void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
  2439. {
  2440.   /* Prevent unused argument(s) compilation warning */
  2441.   UNUSED(hi2c);

  2442.   /* NOTE : This function should not be modified, when the callback is needed,
  2443.             the HAL_I2C_ErrorCallback could be implemented in the user file
  2444.    */
  2445. }

  2446. /**
  2447.   * @}
  2448.   */

  2449. /** @defgroup I2C_Exported_Functions_Group3 Peripheral State, Mode and Error functions
  2450. *  @brief   Peripheral State, Mode and Error functions
  2451. *
  2452. @verbatim   
  2453. ===============================================================================
  2454.             ##### Peripheral State, Mode and Error functions #####
  2455. ===============================================================================  
  2456.     [..]
  2457.     This subsection permit to get in run-time the status of the peripheral
  2458.     and the data flow.

  2459. @endverbatim
  2460.   * @{
  2461.   */

  2462. /**
  2463.   * @brief  Return the I2C handle state.
  2464.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2465.   *                the configuration information for the specified I2C.
  2466.   * @retval HAL state
  2467.   */
  2468. HAL_I2C_StateTypeDef HAL_I2C_GetState(I2C_HandleTypeDef *hi2c)
  2469. {
  2470.   /* Return I2C handle state */
  2471.   return hi2c->State;
  2472. }

  2473. /**
  2474.   * @brief  Returns the I2C Master, Slave, Memory or no mode.
  2475.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2476.   *         the configuration information for I2C module
  2477.   * @retval HAL mode
  2478.   */
  2479. HAL_I2C_ModeTypeDef HAL_I2C_GetMode(I2C_HandleTypeDef *hi2c)
  2480. {
  2481.   return hi2c->Mode;
  2482. }

  2483. /**
  2484. * @brief  Return the I2C error code.
  2485.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2486.   *              the configuration information for the specified I2C.
  2487.   * @retval I2C Error Code
  2488. */
  2489. uint32_t HAL_I2C_GetError(I2C_HandleTypeDef *hi2c)
  2490. {
  2491.   return hi2c->ErrorCode;
  2492. }

  2493. /**
  2494.   * @}
  2495.   */

  2496. /**
  2497.   * @}
  2498.   */   

  2499. /** @addtogroup I2C_Private_Functions
  2500.   * @{
  2501.   */
  2502.   
  2503. /**
  2504.   * @brief  Handle Interrupt Flags Master Transmit Mode
  2505.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2506.   *                the configuration information for the specified I2C.
  2507.   * @retval HAL status
  2508.   */
  2509. static HAL_StatusTypeDef I2C_MasterTransmit_ISR(I2C_HandleTypeDef *hi2c)
  2510. {
  2511.   uint16_t DevAddress;
  2512.   
  2513.   /* Process Locked */
  2514.   __HAL_LOCK(hi2c);
  2515.   
  2516.   if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == SET)
  2517.   {
  2518.     /* Write data to TXDR */
  2519.     hi2c->Instance->TXDR = (*hi2c->pBuffPtr++);
  2520.     hi2c->XferSize--;
  2521.     hi2c->XferCount--;       
  2522.   }
  2523.   else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TCR) == SET)
  2524.   {
  2525.     if((hi2c->XferSize == 0)&&(hi2c->XferCount!=0))
  2526.     {
  2527.       DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);
  2528.       
  2529.       if(hi2c->XferCount > MAX_NBYTE_SIZE)
  2530.       {   
  2531.         I2C_TransferConfig(hi2c, DevAddress, MAX_NBYTE_SIZE, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  2532.         hi2c->XferSize = MAX_NBYTE_SIZE;
  2533.       }
  2534.       else
  2535.       {
  2536.         I2C_TransferConfig(hi2c, DevAddress, hi2c->XferCount, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  2537.         hi2c->XferSize = hi2c->XferCount;
  2538.       }
  2539.     }
  2540.     else
  2541.     {
  2542.       /* Process Unlocked */
  2543.       __HAL_UNLOCK(hi2c);
  2544.       
  2545.       /* Wrong size Status regarding TCR flag event */
  2546.       hi2c->ErrorCode |= HAL_I2C_ERROR_SIZE;
  2547.       HAL_I2C_ErrorCallback(hi2c);
  2548.     }
  2549.   }
  2550.   else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TC) == SET)
  2551.   {
  2552.     if(hi2c->XferCount == 0)
  2553.     {
  2554.       /* Generate Stop */
  2555.       hi2c->Instance->CR2 |= I2C_CR2_STOP;
  2556.     }
  2557.     else
  2558.     {
  2559.       /* Process Unlocked */
  2560.       __HAL_UNLOCK(hi2c);
  2561.       
  2562.       /* Wrong size Status regarding TCR flag event */
  2563.       hi2c->ErrorCode |= HAL_I2C_ERROR_SIZE;
  2564.       HAL_I2C_ErrorCallback(hi2c);
  2565.     }
  2566.   }
  2567.   else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
  2568.   {
  2569.     if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
  2570.     {
  2571.       /* Clear NACK Flag */
  2572.       __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);

  2573.       hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  2574.     }

  2575.     /* Disable ERR, TC, STOP, NACK, TXI interrupts */
  2576.     __HAL_I2C_DISABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_TXI );

  2577.     /* Clear STOP Flag */
  2578.     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);

  2579.     /* Clear Configuration Register 2 */
  2580.     I2C_RESET_CR2(hi2c);

  2581.     /* Flush TX register if not empty */
  2582.     if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXE) == RESET)
  2583.     {
  2584.       __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_TXE);
  2585.     }

  2586.     /* Call the correct callback to inform upper layer */
  2587.     if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  2588.     {
  2589.       hi2c->State = HAL_I2C_STATE_READY;
  2590.       hi2c->Mode = HAL_I2C_MODE_NONE;

  2591.       /* Process Unlocked */
  2592.       __HAL_UNLOCK(hi2c);

  2593.       HAL_I2C_ErrorCallback(hi2c);
  2594.     }
  2595.     else
  2596.     {
  2597.       if (hi2c->Mode == HAL_I2C_MODE_MEM)
  2598.       {
  2599.         hi2c->State = HAL_I2C_STATE_READY;
  2600.         hi2c->Mode = HAL_I2C_MODE_NONE;

  2601.         /* Process Unlocked */
  2602.         __HAL_UNLOCK(hi2c);

  2603.         HAL_I2C_MemTxCpltCallback(hi2c);
  2604.       }
  2605.       else
  2606.       {
  2607.         hi2c->State = HAL_I2C_STATE_READY;
  2608.         hi2c->Mode = HAL_I2C_MODE_NONE;

  2609.         /* Process Unlocked */
  2610.         __HAL_UNLOCK(hi2c);

  2611.         HAL_I2C_MasterTxCpltCallback(hi2c);
  2612.       }
  2613.     }
  2614.   }
  2615.   else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
  2616.   {
  2617.     /* Clear NACK Flag */
  2618.     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);

  2619.     /* Process Unlocked */
  2620.     __HAL_UNLOCK(hi2c);
  2621.    
  2622.     hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  2623.     HAL_I2C_ErrorCallback(hi2c);
  2624.   }
  2625.   
  2626.   /* Process Unlocked */
  2627.   __HAL_UNLOCK(hi2c);
  2628.   
  2629.   return HAL_OK;   
  2630. }  

  2631. /**
  2632.   * @brief  Handle Interrupt Flags Master Receive Mode
  2633.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2634.   *                the configuration information for the specified I2C.
  2635.   * @retval HAL status
  2636.   */
  2637. static HAL_StatusTypeDef I2C_MasterReceive_ISR(I2C_HandleTypeDef *hi2c)
  2638. {
  2639.   uint16_t DevAddress;

  2640.   /* Process Locked */
  2641.   __HAL_LOCK(hi2c);
  2642.   
  2643.   if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
  2644.   {  
  2645.     /* Read data from RXDR */
  2646.     (*hi2c->pBuffPtr++) = hi2c->Instance->RXDR;
  2647.     hi2c->XferSize--;
  2648.     hi2c->XferCount--;
  2649.   }
  2650.   else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TCR) == SET)
  2651.   {
  2652.     if((hi2c->XferSize == 0)&&(hi2c->XferCount!=0))
  2653.     {                  
  2654.       DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);
  2655.       
  2656.       if(hi2c->XferCount > MAX_NBYTE_SIZE)
  2657.       {
  2658.         I2C_TransferConfig(hi2c, DevAddress, MAX_NBYTE_SIZE, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  2659.         hi2c->XferSize = MAX_NBYTE_SIZE;
  2660.       }      
  2661.       else
  2662.       {   
  2663.         I2C_TransferConfig(hi2c, DevAddress, hi2c->XferCount, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  2664.         hi2c->XferSize = hi2c->XferCount;
  2665.       }
  2666.     }
  2667.     else
  2668.     {
  2669.       /* Process Unlocked */
  2670.       __HAL_UNLOCK(hi2c);
  2671.       
  2672.       /* Wrong size Status regarding TCR flag event */
  2673.       hi2c->ErrorCode |= HAL_I2C_ERROR_SIZE;
  2674.       HAL_I2C_ErrorCallback(hi2c);
  2675.     }
  2676.   }
  2677.   else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TC) == SET)
  2678.   {
  2679.     if(hi2c->XferCount == 0)
  2680.     {
  2681.       /* Generate Stop */
  2682.       hi2c->Instance->CR2 |= I2C_CR2_STOP;
  2683.     }
  2684.     else
  2685.     {
  2686.       /* Process Unlocked */
  2687.       __HAL_UNLOCK(hi2c);
  2688.       
  2689.       /* Wrong size Status regarding TCR flag event */
  2690.       hi2c->ErrorCode |= HAL_I2C_ERROR_SIZE;
  2691.       HAL_I2C_ErrorCallback(hi2c);
  2692.     }
  2693.   }
  2694.   else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
  2695.   {
  2696.     if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
  2697.     {
  2698.       /* Clear NACK Flag */
  2699.       __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);

  2700.       hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  2701.     }

  2702.     /* Disable ERR, TC, STOP, NACK, RXI interrupts */
  2703.     __HAL_I2C_DISABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_RXI );
  2704.       
  2705.     /* Clear STOP Flag */
  2706.     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  2707.       
  2708.     /* Clear Configuration Register 2 */
  2709.     I2C_RESET_CR2(hi2c);
  2710.    
  2711.     /* Call the correct callback to inform upper layer */
  2712.     if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  2713.     {
  2714.       hi2c->State = HAL_I2C_STATE_READY;
  2715.       hi2c->Mode = HAL_I2C_MODE_NONE;

  2716.       /* Process Unlocked */
  2717.       __HAL_UNLOCK(hi2c);
  2718.    
  2719.       HAL_I2C_ErrorCallback(hi2c);
  2720.     }
  2721.     else
  2722.     {
  2723.       if (hi2c->Mode == HAL_I2C_MODE_MEM)
  2724.       {
  2725.         hi2c->State = HAL_I2C_STATE_READY;
  2726.         hi2c->Mode = HAL_I2C_MODE_NONE;

  2727.         /* Process Unlocked */
  2728.         __HAL_UNLOCK(hi2c);
  2729.       
  2730.         HAL_I2C_MemRxCpltCallback(hi2c);
  2731.       }
  2732.       else
  2733.       {
  2734.         hi2c->State = HAL_I2C_STATE_READY;
  2735.         hi2c->Mode = HAL_I2C_MODE_NONE;

  2736.         /* Process Unlocked */
  2737.         __HAL_UNLOCK(hi2c);
  2738.       
  2739.         HAL_I2C_MasterRxCpltCallback(hi2c);
  2740.       }
  2741.     }
  2742.   }
  2743.   else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
  2744.   {
  2745.     /* Clear NACK Flag */
  2746.     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);

  2747.     /* Process Unlocked */
  2748.     __HAL_UNLOCK(hi2c);
  2749.    
  2750.     hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  2751.     HAL_I2C_ErrorCallback(hi2c);
  2752.   }
  2753.    
  2754.   /* Process Unlocked */
  2755.   __HAL_UNLOCK(hi2c);
  2756.   
  2757.   return HAL_OK;

  2758. }  

  2759. /**
  2760.   * @brief  Handle Interrupt Flags Slave Transmit Mode
  2761.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2762.   *                the configuration information for the specified I2C.
  2763.   * @retval HAL status
  2764.   */
  2765. static HAL_StatusTypeDef I2C_SlaveTransmit_ISR(I2C_HandleTypeDef *hi2c)
  2766. {
  2767.   /* Process locked */
  2768.   __HAL_LOCK(hi2c);
  2769.   
  2770.   if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) != RESET)
  2771.   {
  2772.     /* Check that I2C transfer finished */
  2773.     /* if yes, normal usecase, a NACK is sent by the MASTER when Transfer is finished */
  2774.     /* Mean XferCount == 0*/
  2775.     /* So clear Flag NACKF only */
  2776.     if(hi2c->XferCount == 0)
  2777.     {
  2778.       /* Clear NACK Flag */
  2779.       __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);

  2780.       /* Process Unlocked */
  2781.       __HAL_UNLOCK(hi2c);
  2782.     }
  2783.     else
  2784.     {
  2785.       /* if no, error usecase, a Non-Acknowledge of last Data is generated by the MASTER*/
  2786.       /* Clear NACK Flag */
  2787.       __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);

  2788.       /* Set ErrorCode corresponding to a Non-Acknowledge */
  2789.       hi2c->ErrorCode |= HAL_I2C_ERROR_AF;

  2790.       /* Process Unlocked */
  2791.       __HAL_UNLOCK(hi2c);
  2792.    
  2793.       /* Call the Error callback to prevent upper layer */
  2794.       HAL_I2C_ErrorCallback(hi2c);
  2795.     }
  2796.   }
  2797.   else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == SET)
  2798.   {
  2799.     /* Clear ADDR flag */
  2800.     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
  2801.   }
  2802.   /* Check first if STOPF is set          */
  2803.   /* to prevent a Write Data in TX buffer */
  2804.   /* which is stuck in TXDR until next    */
  2805.   /* communication with Master            */
  2806.   else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
  2807.   {
  2808.     /* Disable ERRI, TCI, STOPI, NACKI, ADDRI, RXI, TXI interrupts */
  2809.     __HAL_I2C_DISABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI );
  2810.    
  2811.     /* Disable Address Acknowledge */
  2812.     hi2c->Instance->CR2 |= I2C_CR2_NACK;

  2813.     /* Clear STOP Flag */
  2814.     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);

  2815.     hi2c->State = HAL_I2C_STATE_READY;
  2816.     hi2c->Mode = HAL_I2C_MODE_NONE;
  2817.    
  2818.     /* Process Unlocked */
  2819.     __HAL_UNLOCK(hi2c);

  2820.     HAL_I2C_SlaveTxCpltCallback(hi2c);
  2821.   }
  2822.   else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == SET)
  2823.   {
  2824.     /* Write data to TXDR only if XferCount not reach "0" */
  2825.     /* A TXIS flag can be set, during STOP treatment      */
  2826.     if(hi2c->XferCount > 0)
  2827.     {
  2828.       /* Write data to TXDR */
  2829.       hi2c->Instance->TXDR = (*hi2c->pBuffPtr++);
  2830.       hi2c->XferCount--;
  2831.     }
  2832.   }

  2833.   /* Process Unlocked */
  2834.   __HAL_UNLOCK(hi2c);
  2835.   
  2836.   return HAL_OK;
  2837. }  

  2838. /**
  2839.   * @brief  Handle Interrupt Flags Slave Receive Mode
  2840.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2841.   *                the configuration information for the specified I2C.
  2842.   * @retval HAL status
  2843.   */
  2844. static HAL_StatusTypeDef I2C_SlaveReceive_ISR(I2C_HandleTypeDef *hi2c)
  2845. {
  2846.   /* Process Locked */
  2847.   __HAL_LOCK(hi2c);
  2848.   
  2849.   if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) != RESET)
  2850.   {
  2851.     /* Clear NACK Flag */
  2852.     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);

  2853.     /* Process Unlocked */
  2854.     __HAL_UNLOCK(hi2c);
  2855.    
  2856.     hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  2857.     HAL_I2C_ErrorCallback(hi2c);
  2858.   }
  2859.   else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == SET)
  2860.   {
  2861.     /* Clear ADDR flag */
  2862.     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
  2863.   }
  2864.   else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
  2865.   {
  2866.     /* Read data from RXDR */
  2867.     (*hi2c->pBuffPtr++) = hi2c->Instance->RXDR;
  2868.     hi2c->XferSize--;
  2869.     hi2c->XferCount--;
  2870.   }
  2871.   else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
  2872.   {
  2873.     /* Disable ERRI, TCI, STOPI, NACKI, ADDRI, RXI, TXI interrupts */
  2874.     __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI);

  2875.     /* Disable Address Acknowledge */
  2876.     hi2c->Instance->CR2 |= I2C_CR2_NACK;

  2877.     /* Clear STOP Flag */
  2878.     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);

  2879.     hi2c->State = HAL_I2C_STATE_READY;
  2880.     hi2c->Mode = HAL_I2C_MODE_NONE;
  2881.    
  2882.     /* Process Unlocked */
  2883.     __HAL_UNLOCK(hi2c);

  2884.     HAL_I2C_SlaveRxCpltCallback(hi2c);
  2885.   }

  2886.   /* Process Unlocked */
  2887.   __HAL_UNLOCK(hi2c);
  2888.   
  2889.   return HAL_OK;     
  2890. }  

  2891. /**
  2892.   * @brief  Master sends target device address followed by internal memory address for write request.
  2893.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2894.   *                the configuration information for the specified I2C.
  2895.   * @param  DevAddress Target device address
  2896.   * @param  MemAddress Internal memory address
  2897.   * @param  MemAddSize Size of internal memory address
  2898.   * @param  Timeout Timeout duration
  2899.   * @retval HAL status
  2900.   */
  2901. static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout)   
  2902. {
  2903.   I2C_TransferConfig(hi2c, DevAddress, MemAddSize, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);

  2904.   /* Wait until TXIS flag is set */
  2905.   if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
  2906.   {
  2907.     if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2908.     {
  2909.       return HAL_ERROR;
  2910.     }
  2911.     else
  2912.     {
  2913.       return HAL_TIMEOUT;
  2914.     }
  2915.   }

  2916.   /* If Memory address size is 8Bit */
  2917.   if(MemAddSize == I2C_MEMADD_SIZE_8BIT)
  2918.   {
  2919.     /* Send Memory Address */
  2920.     hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress);   
  2921.   }      
  2922.   /* If Memory address size is 16Bit */
  2923.   else
  2924.   {
  2925.     /* Send MSB of Memory Address */
  2926.     hi2c->Instance->TXDR = I2C_MEM_ADD_MSB(MemAddress);
  2927.    
  2928.     /* Wait until TXIS flag is set */
  2929.     if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
  2930.     {
  2931.       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2932.       {
  2933.         return HAL_ERROR;
  2934.       }
  2935.       else
  2936.       {
  2937.         return HAL_TIMEOUT;
  2938.       }
  2939.     }
  2940.    
  2941.     /* Send LSB of Memory Address */
  2942.     hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress);  
  2943.   }
  2944.   
  2945.   /* Wait until TCR flag is set */
  2946.   if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout) != HAL_OK)      
  2947.   {
  2948.     return HAL_TIMEOUT;
  2949.   }

  2950. return HAL_OK;
  2951. }

  2952. /**
  2953.   * @brief  Master sends target device address followed by internal memory address for read request.
  2954.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2955.   *                the configuration information for the specified I2C.
  2956.   * @param  DevAddress Target device address
  2957.   * @param  MemAddress Internal memory address
  2958.   * @param  MemAddSize Size of internal memory address
  2959.   * @param  Timeout Timeout duration
  2960.   * @retval HAL status
  2961.   */
  2962. static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout)
  2963. {
  2964.   I2C_TransferConfig(hi2c, DevAddress, MemAddSize, I2C_SOFTEND_MODE, I2C_GENERATE_START_WRITE);
  2965.   
  2966.   /* Wait until TXIS flag is set */
  2967.   if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
  2968.   {
  2969.     if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2970.     {
  2971.       return HAL_ERROR;
  2972.     }
  2973.     else
  2974.     {
  2975.       return HAL_TIMEOUT;
  2976.     }
  2977.   }
  2978.   
  2979.   /* If Memory address size is 8Bit */
  2980.   if(MemAddSize == I2C_MEMADD_SIZE_8BIT)
  2981.   {
  2982.     /* Send Memory Address */
  2983.     hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress);   
  2984.   }      
  2985.   /* If Memory address size is 16Bit */
  2986.   else
  2987.   {
  2988.     /* Send MSB of Memory Address */
  2989.     hi2c->Instance->TXDR = I2C_MEM_ADD_MSB(MemAddress);
  2990.    
  2991.     /* Wait until TXIS flag is set */
  2992.     if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
  2993.     {
  2994.       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2995.       {
  2996.         return HAL_ERROR;
  2997.       }
  2998.       else
  2999.       {
  3000.         return HAL_TIMEOUT;
  3001.       }
  3002.     }
  3003.    
  3004.     /* Send LSB of Memory Address */
  3005.     hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress);  
  3006.   }
  3007.   
  3008.   /* Wait until TC flag is set */
  3009.   if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TC, RESET, Timeout) != HAL_OK)      
  3010.   {
  3011.     return HAL_TIMEOUT;
  3012.   }
  3013.   
  3014.   return HAL_OK;
  3015. }

  3016. /**
  3017.   * @brief  DMA I2C master transmit process complete callback.
  3018.   * @param  hdma DMA handle
  3019.   * @retval None
  3020.   */
  3021. static void I2C_DMAMasterTransmitCplt(DMA_HandleTypeDef *hdma)
  3022. {
  3023.   uint16_t DevAddress;
  3024.   I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  3025.   
  3026.   /* Check if last DMA request was done with RELOAD */
  3027.   /* Set NBYTES to write and reload if size > MAX_NBYTE_SIZE */
  3028.   if( (hi2c->XferSize == MAX_NBYTE_SIZE) && (hi2c->XferSize < hi2c->XferCount) )
  3029.   {
  3030.     /* Wait until TCR flag is set */
  3031.     if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, I2C_TIMEOUT_TCR) != HAL_OK)      
  3032.     {
  3033.       hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3034.     }

  3035.     /* Disable DMA Request */
  3036.     hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;

  3037.     /* Check if Errors has been detected during transfer */
  3038.     if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  3039.     {
  3040.       /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  3041.       /* Wait until STOPF flag is reset */
  3042.       if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  3043.       {
  3044.         if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  3045.         {
  3046.           hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  3047.         }
  3048.         else
  3049.         {
  3050.           hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3051.         }
  3052.       }
  3053.    
  3054.       /* Clear STOP Flag */
  3055.       __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  3056.          
  3057.       /* Clear Configuration Register 2 */
  3058.       I2C_RESET_CR2(hi2c);

  3059.       hi2c->XferCount = 0;
  3060.    
  3061.       hi2c->State = HAL_I2C_STATE_READY;
  3062.       hi2c->Mode = HAL_I2C_MODE_NONE;

  3063.       HAL_I2C_ErrorCallback(hi2c);
  3064.     }
  3065.     else
  3066.     {
  3067.       hi2c->pBuffPtr += hi2c->XferSize;
  3068.       hi2c->XferCount -= hi2c->XferSize;
  3069.       if(hi2c->XferCount > MAX_NBYTE_SIZE)
  3070.       {
  3071.         hi2c->XferSize = MAX_NBYTE_SIZE;
  3072.       }
  3073.       else
  3074.       {
  3075.         hi2c->XferSize = hi2c->XferCount;
  3076.       }

  3077.       DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);

  3078.       /* Enable the DMA channel */
  3079.       HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);

  3080.       /* Send Slave Address */
  3081.       /* Set NBYTES to write and reload if size > MAX_NBYTE_SIZE */
  3082.       if( (hi2c->XferSize == MAX_NBYTE_SIZE) && (hi2c->XferSize < hi2c->XferCount) )
  3083.       {
  3084.         I2C_TransferConfig(hi2c, DevAddress, hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  3085.       }
  3086.       else
  3087.       {
  3088.         I2C_TransferConfig(hi2c, DevAddress, hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  3089.       }  

  3090.       /* Wait until TXIS flag is set */
  3091.       if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, I2C_TIMEOUT_TXIS) != HAL_OK)
  3092.       {
  3093.         /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  3094.         /* Wait until STOPF flag is reset */
  3095.         if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  3096.         {
  3097.           if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  3098.           {
  3099.             hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  3100.           }
  3101.           else
  3102.           {
  3103.             hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3104.           }
  3105.         }

  3106.         /* Clear STOP Flag */
  3107.         __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  3108.             
  3109.         /* Clear Configuration Register 2 */
  3110.         I2C_RESET_CR2(hi2c);

  3111.         hi2c->XferCount = 0;

  3112.         hi2c->State = HAL_I2C_STATE_READY;
  3113.         hi2c->Mode = HAL_I2C_MODE_NONE;

  3114.         HAL_I2C_ErrorCallback(hi2c);
  3115.       }
  3116.       else
  3117.       {
  3118.         /* Enable DMA Request */
  3119.         hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
  3120.       }
  3121.     }
  3122.   }
  3123.   else
  3124.   {
  3125.     /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  3126.     /* Wait until STOPF flag is reset */
  3127.     if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  3128.     {
  3129.       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  3130.       {
  3131.         hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  3132.       }
  3133.       else
  3134.       {
  3135.         hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3136.       }
  3137.     }
  3138.   
  3139.     /* Clear STOP Flag */
  3140.     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  3141.          
  3142.     /* Clear Configuration Register 2 */
  3143.     I2C_RESET_CR2(hi2c);

  3144.     /* Disable DMA Request */
  3145.     hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
  3146.   
  3147.     hi2c->XferCount = 0;
  3148.   
  3149.     hi2c->State = HAL_I2C_STATE_READY;
  3150.     hi2c->Mode = HAL_I2C_MODE_NONE;

  3151.    /* Check if Errors has been detected during transfer */
  3152.     if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  3153.     {
  3154.       HAL_I2C_ErrorCallback(hi2c);
  3155.     }
  3156.     else
  3157.     {
  3158.       HAL_I2C_MasterTxCpltCallback(hi2c);
  3159.     }
  3160.   }
  3161. }

  3162. /**
  3163.   * @brief  DMA I2C slave transmit process complete callback.
  3164.   * @param  hdma DMA handle
  3165.   * @retval None
  3166.   */
  3167. static void I2C_DMASlaveTransmitCplt(DMA_HandleTypeDef *hdma)
  3168. {
  3169.   I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  3170.   
  3171.   /* Wait until STOP flag is set */
  3172.   if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  3173.   {
  3174.     if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  3175.     {
  3176.       /* Normal Use case, a AF is generated by master */
  3177.       /* to inform slave the end of transfer */
  3178.       hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  3179.     }
  3180.     else
  3181.     {
  3182.       hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3183.     }
  3184.   }
  3185.   
  3186.   /* Clear STOP flag */
  3187.   __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_STOPF);
  3188.   
  3189.   /* Wait until BUSY flag is reset */
  3190.   if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY) != HAL_OK)      
  3191.   {
  3192.     hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3193.   }
  3194.   
  3195.   /* Disable DMA Request */
  3196.   hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
  3197.   
  3198.   hi2c->XferCount = 0;
  3199.   
  3200.   hi2c->State = HAL_I2C_STATE_READY;
  3201.   hi2c->Mode = HAL_I2C_MODE_NONE;

  3202.   /* Check if Errors has been detected during transfer */
  3203.   if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  3204.   {
  3205.     HAL_I2C_ErrorCallback(hi2c);
  3206.   }
  3207.   else
  3208.   {
  3209.     HAL_I2C_SlaveTxCpltCallback(hi2c);
  3210.   }
  3211. }

  3212. /**
  3213.   * @brief DMA I2C master receive process complete callback
  3214.   * @param  hdma DMA handle
  3215.   * @retval None
  3216.   */
  3217. static void I2C_DMAMasterReceiveCplt(DMA_HandleTypeDef *hdma)
  3218. {
  3219.   I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  3220.   uint16_t DevAddress;
  3221.   
  3222.   /* Check if last DMA request was done with RELOAD */
  3223.   /* Set NBYTES to write and reload if size > MAX_NBYTE_SIZE */
  3224.   if( (hi2c->XferSize == MAX_NBYTE_SIZE) && (hi2c->XferSize < hi2c->XferCount) )
  3225.   {
  3226.     /* Wait until TCR flag is set */
  3227.     if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, I2C_TIMEOUT_TCR) != HAL_OK)      
  3228.     {
  3229.       hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3230.     }

  3231.     /* Disable DMA Request */
  3232.     hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;

  3233.     /* Check if Errors has been detected during transfer */
  3234.     if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  3235.     {
  3236.       /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  3237.       /* Wait until STOPF flag is reset */
  3238.       if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  3239.       {
  3240.         if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  3241.         {
  3242.           hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  3243.         }
  3244.         else
  3245.         {
  3246.           hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3247.         }
  3248.       }
  3249.    
  3250.       /* Clear STOP Flag */
  3251.       __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  3252.          
  3253.       /* Clear Configuration Register 2 */
  3254.       I2C_RESET_CR2(hi2c);
  3255.    
  3256.       hi2c->XferCount = 0;
  3257.    
  3258.       hi2c->State = HAL_I2C_STATE_READY;
  3259.       hi2c->Mode = HAL_I2C_MODE_NONE;

  3260.       HAL_I2C_ErrorCallback(hi2c);
  3261.     }
  3262.     else
  3263.     {
  3264.       hi2c->pBuffPtr += hi2c->XferSize;
  3265.       hi2c->XferCount -= hi2c->XferSize;
  3266.       if(hi2c->XferCount > MAX_NBYTE_SIZE)
  3267.       {
  3268.         hi2c->XferSize = MAX_NBYTE_SIZE;
  3269.       }
  3270.       else
  3271.       {
  3272.         hi2c->XferSize = hi2c->XferCount;
  3273.       }

  3274.       DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);

  3275.       /* Enable the DMA channel */
  3276.       HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);

  3277.       /* Send Slave Address */
  3278.       /* Set NBYTES to write and reload if size > MAX_NBYTE_SIZE */
  3279.       if( (hi2c->XferSize == MAX_NBYTE_SIZE) && (hi2c->XferSize < hi2c->XferCount) )
  3280.       {
  3281.         I2C_TransferConfig(hi2c, DevAddress, hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  3282.       }
  3283.       else
  3284.       {
  3285.         I2C_TransferConfig(hi2c, DevAddress, hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  3286.       }  

  3287.       /* Wait until RXNE flag is set */
  3288.       if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, I2C_TIMEOUT_RXNE) != HAL_OK)      
  3289.       {
  3290.         hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3291.       }

  3292.       /* Check if Errors has been detected during transfer */
  3293.       if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  3294.       {
  3295.         /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  3296.         /* Wait until STOPF flag is reset */
  3297.         if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  3298.         {
  3299.           if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  3300.           {
  3301.             hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  3302.           }
  3303.           else
  3304.           {
  3305.             hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3306.           }
  3307.         }
  3308.       
  3309.         /* Clear STOP Flag */
  3310.         __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  3311.             
  3312.         /* Clear Configuration Register 2 */
  3313.         I2C_RESET_CR2(hi2c);
  3314.       
  3315.         hi2c->XferCount = 0;
  3316.       
  3317.         hi2c->State = HAL_I2C_STATE_READY;
  3318.         hi2c->Mode = HAL_I2C_MODE_NONE;

  3319.         HAL_I2C_ErrorCallback(hi2c);
  3320.       }
  3321.       else
  3322.       {
  3323.         /* Enable DMA Request */
  3324.         hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
  3325.       }
  3326.     }
  3327.   }
  3328.   else
  3329.   {
  3330.     /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  3331.     /* Wait until STOPF flag is reset */
  3332.     if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  3333.     {
  3334.       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  3335.       {
  3336.         hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  3337.       }
  3338.       else
  3339.       {
  3340.         hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3341.       }
  3342.     }
  3343.   
  3344.     /* Clear STOP Flag */
  3345.     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  3346.          
  3347.     /* Clear Configuration Register 2 */
  3348.     I2C_RESET_CR2(hi2c);
  3349.   
  3350.     /* Disable DMA Request */
  3351.     hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
  3352.   
  3353.     hi2c->XferCount = 0;
  3354.   
  3355.     hi2c->State = HAL_I2C_STATE_READY;
  3356.     hi2c->Mode = HAL_I2C_MODE_NONE;

  3357.     /* Check if Errors has been detected during transfer */
  3358.     if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  3359.     {
  3360.       HAL_I2C_ErrorCallback(hi2c);
  3361.     }
  3362.     else
  3363.     {
  3364.       HAL_I2C_MasterRxCpltCallback(hi2c);
  3365.     }
  3366.   }
  3367. }

  3368. /**
  3369.   * @brief  DMA I2C slave receive process complete callback.
  3370.   * @param  hdma DMA handle
  3371.   * @retval None
  3372.   */
  3373. static void I2C_DMASlaveReceiveCplt(DMA_HandleTypeDef *hdma)
  3374. {  
  3375.   I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  3376.   
  3377.   /* Wait until STOPF flag is reset */
  3378.   if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  3379.   {
  3380.     if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  3381.     {
  3382.       hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  3383.     }
  3384.     else
  3385.     {
  3386.       hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3387.     }
  3388.   }
  3389.   
  3390.   /* Clear STOPF flag */
  3391.   __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  3392.   
  3393.   /* Wait until BUSY flag is reset */
  3394.   if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY) != HAL_OK)      
  3395.   {
  3396.     hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3397.   }
  3398.   
  3399.   /* Disable DMA Request */
  3400.   hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
  3401.   
  3402.   /* Disable Address Acknowledge */
  3403.   hi2c->Instance->CR2 |= I2C_CR2_NACK;

  3404.   hi2c->XferCount = 0;
  3405.   
  3406.   hi2c->State = HAL_I2C_STATE_READY;
  3407.   hi2c->Mode = HAL_I2C_MODE_NONE;

  3408.   /* Check if Errors has been detected during transfer */
  3409.   if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  3410.   {
  3411.     HAL_I2C_ErrorCallback(hi2c);
  3412.   }
  3413.   else
  3414.   {
  3415.     HAL_I2C_SlaveRxCpltCallback(hi2c);
  3416.   }
  3417. }

  3418. /**
  3419.   * @brief DMA I2C Memory Write process complete callback
  3420.   * @param hdma DMA handle
  3421.   * @retval None
  3422.   */
  3423. static void I2C_DMAMemTransmitCplt(DMA_HandleTypeDef *hdma)   
  3424. {
  3425.   uint16_t DevAddress;
  3426.   I2C_HandleTypeDef* hi2c = ( I2C_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  3427.   
  3428.   /* Check if last DMA request was done with RELOAD */
  3429.   /* Set NBYTES to write and reload if size > MAX_NBYTE_SIZE */
  3430.   if( (hi2c->XferSize == MAX_NBYTE_SIZE) && (hi2c->XferSize < hi2c->XferCount) )
  3431.   {
  3432.     /* Wait until TCR flag is set */
  3433.     if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, I2C_TIMEOUT_TCR) != HAL_OK)      
  3434.     {
  3435.       hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3436.     }

  3437.     /* Disable DMA Request */
  3438.     hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;

  3439.     /* Check if Errors has been detected during transfer */
  3440.     if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  3441.     {
  3442.       /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  3443.       /* Wait until STOPF flag is reset */
  3444.       if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  3445.       {
  3446.         if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  3447.         {
  3448.           hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  3449.         }
  3450.         else
  3451.         {
  3452.           hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3453.         }
  3454.       }
  3455.    
  3456.       /* Clear STOP Flag */
  3457.       __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  3458.          
  3459.       /* Clear Configuration Register 2 */
  3460.       I2C_RESET_CR2(hi2c);

  3461.       hi2c->XferCount = 0;
  3462.    
  3463.       hi2c->State = HAL_I2C_STATE_READY;
  3464.       HAL_I2C_ErrorCallback(hi2c);
  3465.     }
  3466.     else
  3467.     {
  3468.       hi2c->pBuffPtr += hi2c->XferSize;
  3469.       hi2c->XferCount -= hi2c->XferSize;
  3470.       if(hi2c->XferCount > MAX_NBYTE_SIZE)
  3471.       {
  3472.         hi2c->XferSize = MAX_NBYTE_SIZE;
  3473.       }
  3474.       else
  3475.       {
  3476.         hi2c->XferSize = hi2c->XferCount;
  3477.       }

  3478.       DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);

  3479.       /* Enable the DMA channel */
  3480.       HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);

  3481.       /* Send Slave Address */
  3482.       /* Set NBYTES to write and reload if size > MAX_NBYTE_SIZE */
  3483.       if( (hi2c->XferSize == MAX_NBYTE_SIZE) && (hi2c->XferSize < hi2c->XferCount) )
  3484.       {
  3485.         I2C_TransferConfig(hi2c, DevAddress, hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  3486.       }
  3487.       else
  3488.       {
  3489.         I2C_TransferConfig(hi2c, DevAddress, hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  3490.       }  

  3491.       /* Wait until TXIS flag is set */
  3492.       if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, I2C_TIMEOUT_TXIS) != HAL_OK)
  3493.       {
  3494.         /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  3495.         /* Wait until STOPF flag is reset */
  3496.         if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  3497.         {
  3498.           if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  3499.           {
  3500.             hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  3501.           }
  3502.           else
  3503.           {
  3504.             hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3505.           }
  3506.         }

  3507.         /* Clear STOP Flag */
  3508.         __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  3509.             
  3510.         /* Clear Configuration Register 2 */
  3511.         I2C_RESET_CR2(hi2c);

  3512.         hi2c->XferCount = 0;

  3513.         hi2c->State = HAL_I2C_STATE_READY;
  3514.         hi2c->Mode = HAL_I2C_MODE_NONE;

  3515.         HAL_I2C_ErrorCallback(hi2c);
  3516.       }
  3517.       else
  3518.       {
  3519.         /* Enable DMA Request */
  3520.         hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
  3521.       }
  3522.     }
  3523.   }
  3524.   else
  3525.   {
  3526.     /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  3527.     /* Wait until STOPF flag is reset */
  3528.     if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  3529.     {
  3530.       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  3531.       {
  3532.         hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  3533.       }
  3534.       else
  3535.       {
  3536.         hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3537.       }
  3538.     }
  3539.   
  3540.     /* Clear STOP Flag */
  3541.     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  3542.          
  3543.     /* Clear Configuration Register 2 */
  3544.     I2C_RESET_CR2(hi2c);

  3545.     /* Disable DMA Request */
  3546.     hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
  3547.   
  3548.     hi2c->XferCount = 0;
  3549.   
  3550.     hi2c->State = HAL_I2C_STATE_READY;
  3551.     hi2c->Mode = HAL_I2C_MODE_NONE;

  3552.     /* Check if Errors has been detected during transfer */
  3553.     if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  3554.     {
  3555.       HAL_I2C_ErrorCallback(hi2c);
  3556.     }
  3557.     else
  3558.     {
  3559.       HAL_I2C_MemTxCpltCallback(hi2c);
  3560.     }
  3561.   }
  3562. }

  3563. /**
  3564.   * @brief  DMA I2C Memory Read process complete callback
  3565.   * @param  hdma DMA handle
  3566.   * @retval None
  3567.   */
  3568. static void I2C_DMAMemReceiveCplt(DMA_HandleTypeDef *hdma)   
  3569. {  
  3570.   I2C_HandleTypeDef* hi2c = ( I2C_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;  
  3571.   uint16_t DevAddress;
  3572.   
  3573.   /* Check if last DMA request was done with RELOAD */
  3574.   /* Set NBYTES to write and reload if size > MAX_NBYTE_SIZE */
  3575.   if( (hi2c->XferSize == MAX_NBYTE_SIZE) && (hi2c->XferSize < hi2c->XferCount) )
  3576.   {
  3577.     /* Wait until TCR flag is set */
  3578.     if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, I2C_TIMEOUT_TCR) != HAL_OK)      
  3579.     {
  3580.       hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3581.     }

  3582.     /* Disable DMA Request */
  3583.     hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;

  3584.     /* Check if Errors has been detected during transfer */
  3585.     if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  3586.     {
  3587.       /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  3588.       /* Wait until STOPF flag is reset */
  3589.       if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  3590.       {
  3591.         if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  3592.         {
  3593.           hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  3594.         }
  3595.         else
  3596.         {
  3597.           hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3598.         }
  3599.       }
  3600.    
  3601.       /* Clear STOP Flag */
  3602.       __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  3603.          
  3604.       /* Clear Configuration Register 2 */
  3605.       I2C_RESET_CR2(hi2c);
  3606.    
  3607.       hi2c->XferCount = 0;
  3608.    
  3609.       hi2c->State = HAL_I2C_STATE_READY;
  3610.       hi2c->Mode = HAL_I2C_MODE_NONE;

  3611.       HAL_I2C_ErrorCallback(hi2c);
  3612.     }
  3613.     else
  3614.     {
  3615.       hi2c->pBuffPtr += hi2c->XferSize;
  3616.       hi2c->XferCount -= hi2c->XferSize;
  3617.       if(hi2c->XferCount > MAX_NBYTE_SIZE)
  3618.       {
  3619.         hi2c->XferSize = MAX_NBYTE_SIZE;
  3620.       }
  3621.       else
  3622.       {
  3623.         hi2c->XferSize = hi2c->XferCount;
  3624.       }

  3625.       DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);

  3626.       /* Enable the DMA channel */
  3627.       HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);

  3628.       /* Send Slave Address */
  3629.       /* Set NBYTES to write and reload if size > MAX_NBYTE_SIZE */
  3630.       if( (hi2c->XferSize == MAX_NBYTE_SIZE) && (hi2c->XferSize < hi2c->XferCount) )
  3631.       {
  3632.         I2C_TransferConfig(hi2c, DevAddress, hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  3633.       }
  3634.       else
  3635.       {
  3636.         I2C_TransferConfig(hi2c, DevAddress, hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  3637.       }  

  3638.       /* Wait until RXNE flag is set */
  3639.       if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, I2C_TIMEOUT_RXNE) != HAL_OK)      
  3640.       {
  3641.         hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3642.       }

  3643.       /* Check if Errors has been detected during transfer */
  3644.       if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  3645.       {
  3646.         /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  3647.         /* Wait until STOPF flag is reset */
  3648.         if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  3649.         {
  3650.           if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  3651.           {
  3652.             hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  3653.           }
  3654.           else
  3655.           {
  3656.             hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3657.           }
  3658.         }
  3659.       
  3660.         /* Clear STOP Flag */
  3661.         __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  3662.             
  3663.         /* Clear Configuration Register 2 */
  3664.         I2C_RESET_CR2(hi2c);
  3665.       
  3666.         hi2c->XferCount = 0;

  3667.         hi2c->State = HAL_I2C_STATE_READY;
  3668.         hi2c->Mode = HAL_I2C_MODE_NONE;

  3669.         HAL_I2C_ErrorCallback(hi2c);
  3670.       }
  3671.       else
  3672.       {
  3673.         /* Enable DMA Request */
  3674.         hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
  3675.       }
  3676.     }
  3677.   }
  3678.   else
  3679.   {
  3680.     /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  3681.     /* Wait until STOPF flag is reset */
  3682.     if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  3683.     {
  3684.       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  3685.       {
  3686.         hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  3687.       }
  3688.       else
  3689.       {
  3690.         hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3691.       }
  3692.     }
  3693.   
  3694.     /* Clear STOP Flag */
  3695.     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  3696.          
  3697.     /* Clear Configuration Register 2 */
  3698.     I2C_RESET_CR2(hi2c);
  3699.   
  3700.     /* Disable DMA Request */
  3701.     hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
  3702.   
  3703.     hi2c->XferCount = 0;
  3704.   
  3705.     hi2c->State = HAL_I2C_STATE_READY;
  3706.     hi2c->Mode = HAL_I2C_MODE_NONE;

  3707.     /* Check if Errors has been detected during transfer */
  3708.     if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  3709.     {
  3710.       HAL_I2C_ErrorCallback(hi2c);
  3711.     }
  3712.     else
  3713.     {
  3714.       HAL_I2C_MemRxCpltCallback(hi2c);
  3715.     }
  3716.   }
  3717. }

  3718. /**
  3719.   * @brief  DMA I2C communication error callback.
  3720.   * @param hdma DMA handle
  3721.   * @retval None
  3722.   */
  3723. static void I2C_DMAError(DMA_HandleTypeDef *hdma)   
  3724. {
  3725.   I2C_HandleTypeDef* hi2c = ( I2C_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  3726.   
  3727.   /* Disable Acknowledge */
  3728.   hi2c->Instance->CR2 |= I2C_CR2_NACK;
  3729.   
  3730.   hi2c->XferCount = 0;
  3731.   
  3732.   hi2c->State = HAL_I2C_STATE_READY;
  3733.   hi2c->Mode = HAL_I2C_MODE_NONE;
  3734.   
  3735.   hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  3736.   
  3737.   HAL_I2C_ErrorCallback(hi2c);
  3738. }

  3739. /**
  3740.   * @brief  This function handles I2C Communication Timeout.
  3741.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3742.   *                the configuration information for the specified I2C.
  3743.   * @param  Flag Specifies the I2C flag to check.
  3744.   * @param  Status The new Flag status (SET or RESET).
  3745.   * @param  Timeout Timeout duration
  3746.   * @retval HAL status
  3747.   */
  3748. static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout)  
  3749. {  
  3750.   uint32_t tickstart = HAL_GetTick();

  3751.   /* Wait until flag is set */
  3752.   if(Status == RESET)
  3753.   {   
  3754.     while(__HAL_I2C_GET_FLAG(hi2c, Flag) == RESET)
  3755.     {
  3756.       /* Check for the Timeout */
  3757.       if(Timeout != HAL_MAX_DELAY)
  3758.       {
  3759.         if((Timeout == 0) || ((HAL_GetTick() - tickstart) > Timeout))
  3760.         {
  3761.           hi2c->State= HAL_I2C_STATE_READY;
  3762.           hi2c->Mode = HAL_I2C_MODE_NONE;

  3763.           /* Process Unlocked */
  3764.           __HAL_UNLOCK(hi2c);
  3765.           return HAL_TIMEOUT;
  3766.         }
  3767.       }
  3768.     }
  3769.   }
  3770.   else
  3771.   {
  3772.     while(__HAL_I2C_GET_FLAG(hi2c, Flag) != RESET)
  3773.     {
  3774.       /* Check for the Timeout */
  3775.       if(Timeout != HAL_MAX_DELAY)
  3776.       {
  3777.         if((Timeout == 0) || ((HAL_GetTick() - tickstart) > Timeout))
  3778.         {
  3779.           hi2c->State= HAL_I2C_STATE_READY;
  3780.           hi2c->Mode = HAL_I2C_MODE_NONE;

  3781.           /* Process Unlocked */
  3782.           __HAL_UNLOCK(hi2c);
  3783.           return HAL_TIMEOUT;
  3784.         }
  3785.       }
  3786.     }
  3787.   }
  3788.   return HAL_OK;      
  3789. }

  3790. /**
  3791.   * @brief  This function handles I2C Communication Timeout for specific usage of TXIS flag.
  3792.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3793.   *                the configuration information for the specified I2C.
  3794.   * @param  Timeout Timeout duration
  3795.   * @retval HAL status
  3796.   */
  3797. static HAL_StatusTypeDef I2C_WaitOnTXISFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout)  
  3798. {  
  3799.   uint32_t tickstart = HAL_GetTick();
  3800.   
  3801.   while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == RESET)
  3802.   {
  3803.     /* Check if a NACK is detected */
  3804.     if(I2C_IsAcknowledgeFailed(hi2c, Timeout) != HAL_OK)
  3805.     {
  3806.       return HAL_ERROR;
  3807.     }

  3808.     /* Check for the Timeout */
  3809.     if(Timeout != HAL_MAX_DELAY)
  3810.     {
  3811.       if((Timeout == 0) || ((HAL_GetTick() - tickstart) > Timeout))
  3812.       {
  3813.         hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3814.         hi2c->State= HAL_I2C_STATE_READY;
  3815.         hi2c->Mode = HAL_I2C_MODE_NONE;

  3816.         /* Process Unlocked */
  3817.         __HAL_UNLOCK(hi2c);

  3818.         return HAL_TIMEOUT;
  3819.       }
  3820.     }
  3821.   }
  3822.   return HAL_OK;      
  3823. }

  3824. /**
  3825.   * @brief  This function handles I2C Communication Timeout for specific usage of STOP flag.
  3826.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3827.   *                the configuration information for the specified I2C.
  3828.   * @param  Timeout Timeout duration
  3829.   * @retval HAL status
  3830.   */
  3831. static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout)
  3832. {  
  3833.   uint32_t tickstart = 0x00;
  3834.   tickstart = HAL_GetTick();
  3835.   
  3836.   while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET)
  3837.   {
  3838.     /* Check if a NACK is detected */
  3839.     if(I2C_IsAcknowledgeFailed(hi2c, Timeout) != HAL_OK)
  3840.     {
  3841.       return HAL_ERROR;
  3842.     }

  3843.     /* Check for the Timeout */
  3844.     if((Timeout == 0) || ((HAL_GetTick() - tickstart) > Timeout))
  3845.     {
  3846.       hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3847.       hi2c->State= HAL_I2C_STATE_READY;
  3848.       hi2c->Mode = HAL_I2C_MODE_NONE;

  3849.       /* Process Unlocked */
  3850.       __HAL_UNLOCK(hi2c);

  3851.       return HAL_TIMEOUT;
  3852.     }
  3853.   }
  3854.   return HAL_OK;
  3855. }

  3856. /**
  3857.   * @brief  This function handles I2C Communication Timeout for specific usage of RXNE flag.
  3858.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3859.   *                the configuration information for the specified I2C.
  3860.   * @param  Timeout Timeout duration
  3861.   * @retval HAL status
  3862.   */
  3863. static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout)
  3864. {  
  3865.   uint32_t tickstart = 0x00;
  3866.   tickstart = HAL_GetTick();
  3867.   
  3868.   while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == RESET)
  3869.   {
  3870.     /* Check if a NACK is detected */
  3871.     if(I2C_IsAcknowledgeFailed(hi2c, Timeout) != HAL_OK)
  3872.     {
  3873.       return HAL_ERROR;
  3874.     }

  3875.     /* Check if a STOPF is detected */
  3876.     if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
  3877.     {
  3878.       /* Clear STOP Flag */
  3879.       __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);

  3880.       /* Clear Configuration Register 2 */
  3881.       I2C_RESET_CR2(hi2c);

  3882.       hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  3883.       hi2c->State= HAL_I2C_STATE_READY;
  3884.       hi2c->Mode = HAL_I2C_MODE_NONE;

  3885.       /* Process Unlocked */
  3886.       __HAL_UNLOCK(hi2c);

  3887.       return HAL_ERROR;
  3888.     }
  3889.                
  3890.     /* Check for the Timeout */
  3891.     if((Timeout == 0) || ((HAL_GetTick() - tickstart) > Timeout))
  3892.     {
  3893.       hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3894.       hi2c->State= HAL_I2C_STATE_READY;

  3895.       /* Process Unlocked */
  3896.       __HAL_UNLOCK(hi2c);

  3897.       return HAL_TIMEOUT;
  3898.     }
  3899.   }
  3900.   return HAL_OK;
  3901. }

  3902. /**
  3903.   * @brief  This function handles Acknowledge failed detection during an I2C Communication.
  3904.   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3905.   *                the configuration information for the specified I2C.
  3906.   * @param  Timeout Timeout duration
  3907.   * @retval HAL status
  3908.   */
  3909. static HAL_StatusTypeDef I2C_IsAcknowledgeFailed(I2C_HandleTypeDef *hi2c, uint32_t Timeout)
  3910. {
  3911.   uint32_t tickstart = 0x00;
  3912.   tickstart = HAL_GetTick();

  3913.   if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
  3914.   {
  3915.     /* Wait until STOP Flag is reset */
  3916.     /* AutoEnd should be initiate after AF */
  3917.     while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET)
  3918.     {
  3919.       /* Check for the Timeout */
  3920.       if(Timeout != HAL_MAX_DELAY)
  3921.       {
  3922.         if((Timeout == 0) || ((HAL_GetTick() - tickstart) > Timeout))
  3923.         {
  3924.           hi2c->State= HAL_I2C_STATE_READY;
  3925.           hi2c->Mode = HAL_I2C_MODE_NONE;

  3926.           /* Process Unlocked */
  3927.           __HAL_UNLOCK(hi2c);
  3928.           return HAL_TIMEOUT;
  3929.         }
  3930.       }
  3931.     }

  3932.     /* Clear NACKF Flag */
  3933.     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);

  3934.     /* Clear STOP Flag */
  3935.     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);

  3936.     /* Flush TX register if not empty */
  3937.     if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXE) == RESET)
  3938.     {
  3939.       __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_TXE);
  3940.     }

  3941.     /* Clear Configuration Register 2 */
  3942.     I2C_RESET_CR2(hi2c);

  3943.     hi2c->ErrorCode = HAL_I2C_ERROR_AF;
  3944.     hi2c->State= HAL_I2C_STATE_READY;
  3945.     hi2c->Mode = HAL_I2C_MODE_NONE;

  3946.     /* Process Unlocked */
  3947.     __HAL_UNLOCK(hi2c);

  3948.     return HAL_ERROR;
  3949.   }
  3950.   return HAL_OK;      
  3951. }

  3952. /**
  3953.   * @brief  Handles I2Cx communication when starting transfer or during transfer (TC or TCR flag are set).
  3954.   * @param  hi2c I2C handle.
  3955.   * @param  DevAddress Specifies the slave address to be programmed.
  3956.   * @param  Size Specifies the number of bytes to be programmed.
  3957.   *   This parameter must be a value between 0 and MAX_NBYTE_SIZE.
  3958.   * @param  Mode New state of the I2C START condition generation.
  3959.   *   This parameter can be one of the following values:
  3960.   *     @arg @ref I2C_RELOAD_MODE Enable Reload mode .
  3961.   *     @arg @ref I2C_AUTOEND_MODE Enable Automatic end mode.
  3962.   *     @arg @ref I2C_SOFTEND_MODE Enable Software end mode.
  3963.   * @param  Request New state of the I2C START condition generation.
  3964.   *   This parameter can be one of the following values:
  3965.   *     @arg @ref I2C_NO_STARTSTOP Don't Generate stop and start condition.
  3966.   *     @arg @ref I2C_GENERATE_STOP Generate stop condition (Size should be set to 0).
  3967.   *     @arg @ref I2C_GENERATE_START_READ Generate Restart for read request.
  3968.   *     @arg @ref I2C_GENERATE_START_WRITE Generate Restart for write request.
  3969.   * @retval None
  3970.   */
  3971. static void I2C_TransferConfig(I2C_HandleTypeDef *hi2c,  uint16_t DevAddress, uint8_t Size, uint32_t Mode, uint32_t Request)
  3972. {
  3973.   uint32_t tmpreg = 0;
  3974.   
  3975.   /* Check the parameters */
  3976.   assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
  3977.   assert_param(IS_TRANSFER_MODE(Mode));
  3978.   assert_param(IS_TRANSFER_REQUEST(Request));
  3979.    
  3980.   /* Get the CR2 register value */
  3981.   tmpreg = hi2c->Instance->CR2;
  3982.   
  3983.   /* clear tmpreg specific bits */
  3984.   tmpreg &= (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP));
  3985.   
  3986.   /* update tmpreg */
  3987.   tmpreg |= (uint32_t)(((uint32_t)DevAddress & I2C_CR2_SADD) | (((uint32_t)Size << 16 ) & I2C_CR2_NBYTES) | \
  3988.             (uint32_t)Mode | (uint32_t)Request);
  3989.   
  3990.   /* update CR2 register */
  3991.   hi2c->Instance->CR2 = tmpreg;  
  3992. }  

  3993. /**
  3994.   * @}
  3995.   */  

  3996. #endif /* HAL_I2C_MODULE_ENABLED */
  3997. /**
  3998.   * @}
  3999.   */

  4000. /**
  4001.   * @}
  4002.   */

  4003. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码








  1. int main(void)
  2. {

  3.   /* USER CODE BEGIN 1 */

  4.   /* USER CODE END 1 */

  5.   /* MCU Configuration----------------------------------------------------------*/

  6.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  7.   HAL_Init();

  8.   /* Configure the system clock */
  9.   SystemClock_Config();

  10.   /* Initialize all configured peripherals */
  11.   MX_GPIO_Init();
  12.   MX_I2C1_Init();
  13.   MX_SPI1_Init();
  14.   //MX_USART1_UART_Init();
  15.                 i2c_status=HAL_I2C_Slave_Transmit_IT(&hi2c1,&EDID_Default[0],256);
  16.   /* USER CODE BEGIN 2 */

  17.   /* USER CODE END 2 */

  18.   /* Infinite loop */
  19.   /* USER CODE BEGIN WHILE */
  20.   while (1)
  21.   {
  22.   /* USER CODE END WHILE */
  23.        
  24.   /* USER CODE BEGIN 3 */
  25.        
  26.   }
  27.   /* USER CODE END 3 */

  28. }
复制代码











  1. /* Includes ------------------------------------------------------------------*/
  2. #include "i2c.h"

  3. #include "gpio.h"

  4. /* USER CODE BEGIN 0 */

  5. /* USER CODE END 0 */

  6. I2C_HandleTypeDef hi2c1;

  7. /* I2C1 init function */
  8. void MX_I2C1_Init(void)
  9. {

  10.   hi2c1.Instance = I2C1;
  11.   hi2c1.Init.Timing = 0x00201D2B;//0x2000090E;
  12.   hi2c1.Init.OwnAddress1 = 0xA0;
  13.   hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  14.   hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
  15.   hi2c1.Init.OwnAddress2 = 0;
  16.   hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  17.   hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
  18.   hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
  19.   HAL_I2C_Init(&hi2c1);

  20.     /**Configure Analogue filter
  21.     */
  22.   HAL_I2CEx_AnalogFilter_Config(&hi2c1, I2C_ANALOGFILTER_ENABLED);

  23. }

  24. void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
  25. {

  26.   GPIO_InitTypeDef GPIO_InitStruct;
  27.   if(hi2c->Instance==I2C1)
  28.   {
  29.   /* USER CODE BEGIN I2C1_MspInit 0 */

  30.   /* USER CODE END I2C1_MspInit 0 */
  31.   
  32.     /**I2C1 GPIO Configuration   
  33.     PA9     ------> I2C1_SCL
  34.     PA10     ------> I2C1_SDA
  35.     */
  36.     GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;
  37.     GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  38.     GPIO_InitStruct.Pull = GPIO_PULLUP;
  39.     GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  40.     GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
  41.     HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  42.     /* Peripheral clock enable */
  43.     __I2C1_CLK_ENABLE();

  44.     /* Peripheral interrupt init*/
  45.     HAL_NVIC_SetPriority(I2C1_IRQn, 1, 0);
  46.     HAL_NVIC_EnableIRQ(I2C1_IRQn);
  47.   /* USER CODE BEGIN I2C1_MspInit 1 */

  48.   /* USER CODE END I2C1_MspInit 1 */
  49.   }
  50. }

  51. void HAL_I2C_MspDeInit(I2C_HandleTypeDef* hi2c)
  52. {

  53.   if(hi2c->Instance==I2C1)
  54.   {
  55.   /* USER CODE BEGIN I2C1_MspDeInit 0 */

  56.   /* USER CODE END I2C1_MspDeInit 0 */
  57.     /* Peripheral clock disable */
  58.     __I2C1_CLK_DISABLE();
  59.   
  60.     /**I2C1 GPIO Configuration   
  61.     PA9     ------> I2C1_SCL
  62.     PA10     ------> I2C1_SDA
  63.     */
  64.     HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);

  65.     /* Peripheral interrupt Deinit*/
  66.     HAL_NVIC_DisableIRQ(I2C1_IRQn);

  67.   }
  68.   /* USER CODE BEGIN I2C1_MspDeInit 1 */

  69.   /* USER CODE END I2C1_MspDeInit 1 */
  70. }

  71. /* USER CODE BEGIN 1 */

  72. /* USER CODE END 1 */

  73. /**
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

阿莫论坛20周年了!感谢大家的支持与爱护!!

月入3000的是反美的。收入3万是亲美的。收入30万是移民美国的。收入300万是取得绿卡后回国,教唆那些3000来反美的!

出0入0汤圆

 楼主| 发表于 2016-3-7 22:12:24 | 显示全部楼层
自问自答

库貌似有问题

I2C 从机NOSTRETCH 模式必须在在使能I2C 外设之前配置。 参见: I2C 从机初始化。

void MX_I2C1_Init(void)
{

  hi2c1.Instance = I2C1;
  hi2c1.Init.Timing = 0x2000090E;//0x00201D2B;
  hi2c1.Init.OwnAddress1 = 0xA0;
  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
  hi2c1.Init.OwnAddress2 = 0;
  hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
  HAL_I2C_Init(&hi2c1);

    /**Configure Analogue filter
    */
  HAL_I2CEx_AnalogFilter_Config(&hi2c1, I2C_ANALOGFILTER_ENABLED);
        __I2C1_CLK_ENABLE();
}




将__I2C1_CLK_ENABLE();放在初始化最后,测试通过!

出0入0汤圆

发表于 2016-3-8 09:42:46 | 显示全部楼层
是不是你没有重写HAL_I2C_MspInit(hi2c);这个函数?所以你添加了CLK enable就好了,在I2C_MspInit中会有关于硬件匹配的设置,例如:

                /* Peripheral clock enable */
                __I2C2_CLK_ENABLE();
                __GPIOB_CLK_ENABLE();

                /**I2C2 GPIO Configuration   
                   PB3     ------> I2C2_SDA
                   PB4     ------> I2C2_SCL
                */
                GPIO_InitStruct.Pin = GPIO_PIN_3;
                GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
                GPIO_InitStruct.Pull = GPIO_NOPULL;
                GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
                GPIO_InitStruct.Alternate = GPIO_AF9_I2C2;
                HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

                GPIO_InitStruct.Pin = GPIO_PIN_4;
                GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
                GPIO_InitStruct.Pull = GPIO_NOPULL;
                GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
                GPIO_InitStruct.Alternate = GPIO_AF4_I2C2;
                HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
        }

这些是需要你自己添加的。

出0入0汤圆

 楼主| 发表于 2016-3-8 19:54:02 | 显示全部楼层
MegaHealth 发表于 2016-3-8 09:42
是不是你没有重写HAL_I2C_MspInit(hi2c);这个函数?所以你添加了CLK enable就好了,在I2C_MspInit中会有关 ...

这些是有的

库生成的c
是先enable clock再配置analog filter的

要把这两个顺序对调才行

可以通讯了   但发送大量数据会有误码
这还是一个问题

出0入0汤圆

发表于 2016-5-19 11:41:16 | 显示全部楼层
请问下楼主调通误码的问题了么。我在用stm32L081的做i2c从机的时候也遇到了同样的情况。。主机第一次读没问题,第二次读的时候发完地址,读了1个byte后时钟线就一直被拉低。

出0入0汤圆

发表于 2016-5-19 12:12:53 来自手机 | 显示全部楼层
这个长度啊!我是用手机看的。IIC每发送一个字节,必须要有一个ACK响应的。

出0入0汤圆

发表于 2019-7-1 06:15:33 | 显示全部楼层
楼主后面解决STM32硬件I2C的问题了吗?我新手,最近在尝试使用ST的I2C,处于崩溃边缘徘徊。想问一下HAL_I2C_SLAVE_Receive()和HAL_I2C_SLAVE_Receive_IT()以及HAL_I2C_SLAVE_Receive_DMA()在使用上的区别在哪里?现在使用HAL_I2C_SLAVE_Receive()放在主程序while(1)中可以接收数据,但很容易受到delay()的影响导致通讯错误,然后I2C锁死,把它放入中断函数中不知道会不会能避免这样的错误
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-4-23 17:19

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表