]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32L0/stm32l0xx_hal_i2c.c
Merge commit '4d116a04e94cf0d19317d5b44e4fa9f34a3e5594'
[qmk_firmware.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32L0 / stm32l0xx_hal_i2c.c
1 /**
2   ******************************************************************************
3   * @file    stm32l0xx_hal_i2c.c
4   * @author  MCD Application Team
5   * @version V1.2.0
6   * @date    06-February-2015
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
24     (#)Initialize the I2C low level resources by implement the HAL_I2C_MspInit ()API:
25         (##) Enable the I2Cx interface clock
26         (##) I2C pins configuration
27             (+++) Enable the clock for the I2C GPIOs
28             (+++) Configure I2C pins as alternate function open-drain
29         (##) NVIC configuration if you need to use interrupt process
30             (+++) Configure the I2Cx interrupt priority
31             (+++) Enable the NVIC I2C IRQ Channel
32         (##) DMA Configuration if you need to use DMA process
33             (+++) Declare a DMA_HandleTypeDef handle structure for the transmit or receive channel
34             (+++) Enable the DMAx interface clock using
35             (+++) Configure the DMA handle parameters
36             (+++) Configure the DMA Tx or Rx channel
37             (+++) Associate the initilalized DMA handle to the hi2c DMA Tx or Rx handle
38             (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on 
39                   the DMA Tx or Rx channel
40
41     (#) Configure the Communication Clock Timing, Own Address1, Master Adressing Mode, Dual Addressing mode,
42         Own Address2, Own Address2 Mask, General call and Nostretch mode in the hi2c Init structure.
43
44     (#) Initialize the I2C registers by calling the HAL_I2C_Init(), configures also the low level Hardware 
45         (GPIO, CLOCK, NVIC...etc) by calling the customed HAL_I2C_MspInit(&hi2c) API.
46
47     (#) To check if target device is ready for communication, use the function HAL_I2C_IsDeviceReady()
48
49     (#) For I2C IO and IO MEM operations, three operation modes are available within this driver :
50
51     *** Polling mode IO operation ***
52     =================================
53     [..]
54       (+) Transmit in master mode an amount of data in blocking mode using HAL_I2C_Master_Transmit()
55       (+) Receive in master mode an amount of data in blocking mode using HAL_I2C_Master_Receive()
56       (+) Transmit in slave mode an amount of data in blocking mode using HAL_I2C_Slave_Transmit()
57       (+) Receive in slave mode an amount of data in blocking mode using HAL_I2C_Slave_Receive()
58
59     *** Polling mode IO MEM operation ***
60     =====================================
61     [..]
62       (+) Write an amount of data in blocking mode to a specific memory address using HAL_I2C_Mem_Write()
63       (+) Read an amount of data in blocking mode from a specific memory address using HAL_I2C_Mem_Read()
64
65
66     *** Interrupt mode IO operation ***
67     ===================================
68     [..]
69       (+) Transmit in master mode an amount of data in non blocking mode using HAL_I2C_Master_Transmit_IT()
70       (+) At transmission end of transfer HAL_I2C_MasterTxCpltCallback is executed and user can
71            add his own code by customization of function pointer HAL_I2C_MasterTxCpltCallback
72       (+) Receive in master mode an amount of data in non blocking mode using HAL_I2C_Master_Receive_IT()
73       (+) At reception end of transfer HAL_I2C_MasterRxCpltCallback is executed and user can
74            add his own code by customization of function pointer HAL_I2C_MasterRxCpltCallback
75       (+) Transmit in slave mode an amount of data in non blocking mode using HAL_I2C_Slave_Transmit_IT()
76       (+) At transmission end of transfer HAL_I2C_SlaveTxCpltCallback is executed and user can
77            add his own code by customization of function pointer HAL_I2C_SlaveTxCpltCallback
78       (+) Receive in slave mode an amount of data in non blocking mode using HAL_I2C_Slave_Receive_IT()
79       (+) At reception end of transfer HAL_I2C_SlaveRxCpltCallback is executed and user can
80            add his own code by customization of function pointer HAL_I2C_SlaveRxCpltCallback
81       (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
82            add his own code by customization of function pointer HAL_I2C_ErrorCallback
83
84     *** Interrupt mode IO MEM operation ***
85     =======================================
86     [..]
87       (+) Write an amount of data in no-blocking mode with Interrupt to a specific memory address using
88           HAL_I2C_Mem_Write_IT()
89       (+) At MEM end of write transfer HAL_I2C_MemTxCpltCallback is executed and user can
90            add his own code by customization of function pointer HAL_I2C_MemTxCpltCallback
91       (+) Read an amount of data in no-blocking mode with Interrupt from a specific memory address using
92           HAL_I2C_Mem_Read_IT()
93       (+) At MEM end of read transfer HAL_I2C_MemRxCpltCallback is executed and user can
94            add his own code by customization of function pointer HAL_I2C_MemRxCpltCallback
95       (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
96            add his own code by customization of function pointer HAL_I2C_ErrorCallback
97
98     *** DMA mode IO operation ***
99     ==============================
100     [..]
101       (+) Transmit in master mode an amount of data in non blocking mode (DMA) using
102           HAL_I2C_Master_Transmit_DMA()
103       (+) At transmission end of transfer HAL_I2C_MasterTxCpltCallback is executed and user can
104            add his own code by customization of function pointer HAL_I2C_MasterTxCpltCallback
105       (+) Receive in master mode an amount of data in non blocking mode (DMA) using
106           HAL_I2C_Master_Receive_DMA()
107       (+) At reception end of transfer HAL_I2C_MasterRxCpltCallback is executed and user can
108            add his own code by customization of function pointer HAL_I2C_MasterRxCpltCallback
109       (+) Transmit in slave mode an amount of data in non blocking mode (DMA) using
110           HAL_I2C_Slave_Transmit_DMA()
111       (+) At transmission end of transfer HAL_I2C_SlaveTxCpltCallback is executed and user can
112            add his own code by customization of function pointer HAL_I2C_SlaveTxCpltCallback
113       (+) Receive in slave mode an amount of data in non blocking mode (DMA) using
114           HAL_I2C_Slave_Receive_DMA()
115       (+) At reception end of transfer HAL_I2C_SlaveRxCpltCallback is executed and user can
116            add his own code by customization of function pointer HAL_I2C_SlaveRxCpltCallback
117       (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
118            add his own code by customization of function pointer HAL_I2C_ErrorCallback
119
120     *** DMA mode IO MEM operation ***
121     =================================
122     [..]
123       (+) Write an amount of data in no-blocking mode with DMA to a specific memory address using
124           HAL_I2C_Mem_Write_DMA()
125       (+) At MEM end of write transfer HAL_I2C_MemTxCpltCallback is executed and user can
126            add his own code by customization of function pointer HAL_I2C_MemTxCpltCallback
127       (+) Read an amount of data in no-blocking mode with DMA from a specific memory address using
128           HAL_I2C_Mem_Read_DMA()
129       (+) At MEM end of read transfer HAL_I2C_MemRxCpltCallback is executed and user can
130            add his own code by customization of function pointer HAL_I2C_MemRxCpltCallback
131       (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
132            add his own code by customization of function pointer HAL_I2C_ErrorCallback
133
134
135      *** I2C HAL driver macros list ***
136      ==================================
137      [..]
138        Below the list of most used macros in I2C HAL driver.
139
140       (+) __HAL_I2C_ENABLE: Enable the I2C peripheral
141       (+) __HAL_I2C_DISABLE: Disable the I2C peripheral
142       (+) __HAL_I2C_GET_FLAG : Check whether the specified I2C flag is set or not
143       (+) __HAL_I2C_CLEAR_FLAG : Clear the specified I2C pending flag
144       (+) __HAL_I2C_ENABLE_IT: Enable the specified I2C interrupt
145       (+) __HAL_I2C_DISABLE_IT: Disable the specified I2C interrupt
146
147      [..]
148        (@) You can refer to the I2C HAL driver header file for more useful macros
149
150   @endverbatim
151   ******************************************************************************
152   * @attention
153   *
154   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
155   *
156   * Redistribution and use in source and binary forms, with or without modification,
157   * are permitted provided that the following conditions are met:
158   *   1. Redistributions of source code must retain the above copyright notice,
159   *      this list of conditions and the following disclaimer.
160   *   2. Redistributions in binary form must reproduce the above copyright notice,
161   *      this list of conditions and the following disclaimer in the documentation
162   *      and/or other materials provided with the distribution.
163   *   3. Neither the name of STMicroelectronics nor the names of its contributors
164   *      may be used to endorse or promote products derived from this software
165   *      without specific prior written permission.
166   *
167   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
168   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
169   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
170   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
171   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
172   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
173   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
174   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
175   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
176   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
177   *
178   ******************************************************************************  
179   */ 
180
181 /* Includes ------------------------------------------------------------------*/
182 #include "stm32l0xx_hal.h"
183
184 /** @addtogroup STM32L0xx_HAL_Driver
185   * @{
186   */
187
188 /** @defgroup I2C I2C
189   * @brief I2C HAL module driver
190   * @{
191   */
192
193 #ifdef HAL_I2C_MODULE_ENABLED
194
195 /* Private typedef -----------------------------------------------------------*/
196 /* Private constants ---------------------------------------------------------*/
197 /** @addtogroup I2C_Private_Constants I2C Private Constants
198   * @{
199   */
200 #define TIMING_CLEAR_MASK       ((uint32_t)0xF0FFFFFF)  /*<! I2C TIMING clear register Mask */
201 #define I2C_TIMEOUT_ADDR    ((uint32_t)10000)  /* 10 s  */
202 #define I2C_TIMEOUT_BUSY    ((uint32_t)25)     /* 25 ms */
203 #define I2C_TIMEOUT_DIR     ((uint32_t)25)     /* 25 ms */
204 #define I2C_TIMEOUT_RXNE    ((uint32_t)25)     /* 25 ms */
205 #define I2C_TIMEOUT_STOPF   ((uint32_t)25)     /* 25 ms */
206 #define I2C_TIMEOUT_TC      ((uint32_t)25)     /* 25 ms */
207 #define I2C_TIMEOUT_TCR     ((uint32_t)25)     /* 25 ms */
208 #define I2C_TIMEOUT_TXIS    ((uint32_t)25)     /* 25 ms */
209 #define I2C_TIMEOUT_FLAG    ((uint32_t)25)     /* 25 ms */
210 /**
211   * @}
212   */ 
213
214 /* Private macro -------------------------------------------------------------*/
215 /* Private variables ---------------------------------------------------------*/
216 /* Private function prototypes -----------------------------------------------*/
217 /** @addtogroup I2C_Private_Functions I2C Private Functions
218   * @{
219   */
220 static void I2C_DMAMasterTransmitCplt(DMA_HandleTypeDef *hdma);
221 static void I2C_DMAMasterReceiveCplt(DMA_HandleTypeDef *hdma);
222 static void I2C_DMASlaveTransmitCplt(DMA_HandleTypeDef *hdma);
223 static void I2C_DMASlaveReceiveCplt(DMA_HandleTypeDef *hdma);
224 static void I2C_DMAMemTransmitCplt(DMA_HandleTypeDef *hdma);
225 static void I2C_DMAMemReceiveCplt(DMA_HandleTypeDef *hdma);
226 static void I2C_DMAError(DMA_HandleTypeDef *hdma);
227
228 static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout);
229 static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout);
230 static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
231 static HAL_StatusTypeDef I2C_WaitOnTXISFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout);
232 static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout);
233 static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout);
234 static HAL_StatusTypeDef I2C_IsAcknowledgeFailed(I2C_HandleTypeDef *hi2c, uint32_t Timeout);
235
236 static HAL_StatusTypeDef I2C_MasterTransmit_ISR(I2C_HandleTypeDef *hi2c);
237 static HAL_StatusTypeDef I2C_MasterReceive_ISR(I2C_HandleTypeDef *hi2c);
238
239 static HAL_StatusTypeDef I2C_SlaveTransmit_ISR(I2C_HandleTypeDef *hi2c);
240 static HAL_StatusTypeDef I2C_SlaveReceive_ISR(I2C_HandleTypeDef *hi2c);
241
242 static void I2C_TransferConfig(I2C_HandleTypeDef *hi2c,  uint16_t DevAddress, uint8_t Size, uint32_t Mode, uint32_t Request);
243 /**
244   * @}
245   */ 
246
247 /* Exported functions --------------------------------------------------------*/
248
249 /** @defgroup I2C_Exported_Functions I2C Exported Functions
250   * @{
251   */
252
253 /** @defgroup I2C_Exported_Functions_Group1 Initialization and de-initialization functions
254  *  @brief    Initialization and Configuration functions 
255  *
256 @verbatim    
257  ===============================================================================
258               ##### Initialization and de-initialization functions #####
259  ===============================================================================
260     [..]  This subsection provides a set of functions allowing to initialize and 
261           de-initialiaze the I2Cx peripheral:
262
263       (+) User must Implement HAL_I2C_MspInit() function in which he configures 
264           all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
265
266       (+) Call the function HAL_I2C_Init() to configure the selected device with 
267           the selected configuration:
268         (++) Clock Timing
269         (++) Own Address 1
270         (++) Addressing mode (Master, Slave)
271         (++) Dual Addressing mode
272         (++) Own Address 2
273         (++) Own Address 2 Mask
274         (++) General call mode
275         (++) Nostretch mode
276
277       (+) Call the function HAL_I2C_DeInit() to restore the default configuration 
278           of the selected I2Cx periperal.       
279
280 @endverbatim
281   * @{
282   */
283
284 /**
285   * @brief  Initializes the I2C according to the specified parameters 
286   *         in the I2C_InitTypeDef and create the associated handle.
287   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
288   *                the configuration information for the specified I2C.
289   * @retval HAL status
290   */
291 HAL_StatusTypeDef HAL_I2C_Init(I2C_HandleTypeDef *hi2c)
292
293   /* Check the I2C handle allocation */
294   if(hi2c == NULL)
295   {
296     return HAL_ERROR;
297   }
298   
299   /* Check the parameters */
300   assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
301   assert_param(IS_I2C_OWN_ADDRESS1(hi2c->Init.OwnAddress1));
302   assert_param(IS_I2C_ADDRESSING_MODE(hi2c->Init.AddressingMode));
303   assert_param(IS_I2C_DUAL_ADDRESS(hi2c->Init.DualAddressMode));
304   assert_param(IS_I2C_OWN_ADDRESS2(hi2c->Init.OwnAddress2));
305   assert_param(IS_I2C_OWN_ADDRESS2_MASK(hi2c->Init.OwnAddress2Masks));
306   assert_param(IS_I2C_GENERAL_CALL(hi2c->Init.GeneralCallMode));
307   assert_param(IS_I2C_NO_STRETCH(hi2c->Init.NoStretchMode));
308
309   if(hi2c->State == HAL_I2C_STATE_RESET)
310   {
311     /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
312     HAL_I2C_MspInit(hi2c);
313   }
314
315   hi2c->State = HAL_I2C_STATE_BUSY;
316   
317   /* Disable the selected I2C peripheral */
318   __HAL_I2C_DISABLE(hi2c);
319   
320   /*---------------------------- I2Cx TIMINGR Configuration ------------------*/
321   /* Configure I2Cx: Frequency range */
322   hi2c->Instance->TIMINGR = hi2c->Init.Timing & TIMING_CLEAR_MASK;
323   
324   /*---------------------------- I2Cx OAR1 Configuration ---------------------*/
325   /* Configure I2Cx: Own Address1 and ack own address1 mode */
326   hi2c->Instance->OAR1 &= ~I2C_OAR1_OA1EN;
327   if(hi2c->Init.OwnAddress1 != 0)
328   {
329     if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_7BIT)
330     {
331       hi2c->Instance->OAR1 = (I2C_OAR1_OA1EN | hi2c->Init.OwnAddress1);
332     }
333     else /* I2C_ADDRESSINGMODE_10BIT */
334     {
335       hi2c->Instance->OAR1 = (I2C_OAR1_OA1EN | I2C_OAR1_OA1MODE | hi2c->Init.OwnAddress1);
336     }
337   }
338   
339   /*---------------------------- I2Cx CR2 Configuration ----------------------*/
340   /* Configure I2Cx: Addressing Master mode */
341   if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT)
342   {
343     hi2c->Instance->CR2 = (I2C_CR2_ADD10);
344   }
345   /* Enable the AUTOEND by default, and enable NACK (should be disable only during Slave process */
346   hi2c->Instance->CR2 |= (I2C_CR2_AUTOEND | I2C_CR2_NACK);
347   
348   /*---------------------------- I2Cx OAR2 Configuration ---------------------*/
349   /* Configure I2Cx: Dual mode and Own Address2 */
350   hi2c->Instance->OAR2 = (hi2c->Init.DualAddressMode | hi2c->Init.OwnAddress2 | (hi2c->Init.OwnAddress2Masks << 8));
351
352   /*---------------------------- I2Cx CR1 Configuration ----------------------*/
353   /* Configure I2Cx: Generalcall and NoStretch mode */
354   hi2c->Instance->CR1 = (hi2c->Init.GeneralCallMode | hi2c->Init.NoStretchMode);
355   
356   /* Enable the selected I2C peripheral */
357   __HAL_I2C_ENABLE(hi2c);
358   
359   hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
360   hi2c->State = HAL_I2C_STATE_READY;
361   
362   return HAL_OK;
363 }
364
365 /**
366   * @brief  DeInitializes the I2C peripheral. 
367   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
368   *                the configuration information for the specified I2C.
369   * @retval HAL status
370   */
371 HAL_StatusTypeDef HAL_I2C_DeInit(I2C_HandleTypeDef *hi2c)
372 {
373   /* Check the I2C handle allocation */
374   if(hi2c == NULL)
375   {
376     return HAL_ERROR;
377   }
378   
379   /* Check the parameters */
380   assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
381   
382   hi2c->State = HAL_I2C_STATE_BUSY;
383   
384   /* Disable the I2C Peripheral Clock */
385   __HAL_I2C_DISABLE(hi2c);
386   
387   /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
388   HAL_I2C_MspDeInit(hi2c);
389   
390   hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
391
392   hi2c->State = HAL_I2C_STATE_RESET;
393   
394   /* Release Lock */
395   __HAL_UNLOCK(hi2c);
396
397   return HAL_OK;
398 }
399
400 /**
401   * @brief I2C MSP Init.
402   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
403   *                the configuration information for the specified I2C.
404   * @retval None
405   */
406  __weak void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)
407 {
408   /* NOTE : This function Should not be modified, when the callback is needed,
409             the HAL_I2C_MspInit could be implemented in the user file
410    */ 
411 }
412
413 /**
414   * @brief I2C MSP DeInit
415   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
416   *                the configuration information for the specified I2C.
417   * @retval None
418   */
419  __weak void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c)
420 {
421   /* NOTE : This function Should not be modified, when the callback is needed,
422             the HAL_I2C_MspDeInit could be implemented in the user file
423    */ 
424 }
425
426 /**
427   * @}
428   */
429
430 /** @defgroup I2C_Exported_Functions_Group2 Input and Output operation functions 
431  *  @brief   Data transfers functions 
432  *
433 @verbatim   
434  ===============================================================================
435                       ##### IO operation functions #####
436  ===============================================================================  
437     [..]
438     This subsection provides a set of functions allowing to manage the I2C data 
439     transfers.
440
441     (#) There are two modes of transfer:
442        (++) Blocking mode : The communication is performed in the polling mode. 
443             The status of all data processing is returned by the same function 
444             after finishing transfer.  
445        (++) No-Blocking mode : The communication is performed using Interrupts 
446             or DMA. These functions return the status of the transfer startup.
447             The end of the data processing will be indicated through the 
448             dedicated I2C IRQ when using Interrupt mode or the DMA IRQ when 
449             using DMA mode.
450
451     (#) Blocking mode functions are :
452         (++) HAL_I2C_Master_Transmit()
453         (++) HAL_I2C_Master_Receive()
454         (++) HAL_I2C_Slave_Transmit()
455         (++) HAL_I2C_Slave_Receive()
456         (++) HAL_I2C_Mem_Write()
457         (++) HAL_I2C_Mem_Read()
458         (++) HAL_I2C_IsDeviceReady()
459         
460     (#) No-Blocking mode functions with Interrupt are :
461         (++) HAL_I2C_Master_Transmit_IT()
462         (++) HAL_I2C_Master_Receive_IT()
463         (++) HAL_I2C_Slave_Transmit_IT()
464         (++) HAL_I2C_Slave_Receive_IT()
465         (++) HAL_I2C_Mem_Write_IT()
466         (++) HAL_I2C_Mem_Read_IT()
467
468     (#) No-Blocking mode functions with DMA are :
469         (++) HAL_I2C_Master_Transmit_DMA()
470         (++) HAL_I2C_Master_Receive_DMA()
471         (++) HAL_I2C_Slave_Transmit_DMA()
472         (++) HAL_I2C_Slave_Receive_DMA()
473         (++) HAL_I2C_Mem_Write_DMA()
474         (++) HAL_I2C_Mem_Read_DMA()
475
476     (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
477         (++) HAL_I2C_MemTxCpltCallback()
478         (++) HAL_I2C_MemRxCpltCallback()
479         (++) HAL_I2C_MasterTxCpltCallback()
480         (++) HAL_I2C_MasterRxCpltCallback()
481         (++) HAL_I2C_SlaveTxCpltCallback()
482         (++) HAL_I2C_SlaveRxCpltCallback()
483         (++) HAL_I2C_ErrorCallback()
484
485 @endverbatim
486   * @{
487   */
488
489 /**
490   * @brief  Transmits in master mode an amount of data in blocking mode.
491   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
492   *                the configuration information for the specified I2C.
493   * @param  DevAddress: Target device address
494   * @param  pData: Pointer to data buffer
495   * @param  Size: Amount of data to be sent
496   * @param  Timeout: Timeout duration
497   * @retval HAL status
498   */
499 HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
500 {
501   uint32_t sizetmp = 0;
502
503   if(hi2c->State == HAL_I2C_STATE_READY)
504   {    
505     if((pData == NULL ) || (Size == 0)) 
506     {
507       return  HAL_ERROR;                                    
508     }
509     
510     if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
511     {
512       return HAL_BUSY;
513     }
514
515     /* Process Locked */
516     __HAL_LOCK(hi2c);
517     
518     hi2c->State = HAL_I2C_STATE_MASTER_BUSY_TX;
519     hi2c->ErrorCode   = HAL_I2C_ERROR_NONE;
520     
521     /* Send Slave Address */
522     /* Set NBYTES to write and reload if size > 255 and generate RESTART */
523     /* Size > 255, need to set RELOAD bit */
524     if(Size > 255)
525     {
526       I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);
527       sizetmp = 255;
528     }
529     else
530     {
531       I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_GENERATE_START_WRITE);
532       sizetmp = Size;
533     }
534       
535     do
536     {
537       /* Wait until TXIS flag is set */
538       if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
539       {
540         if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
541         {
542           return HAL_ERROR;
543         }
544         else
545         {
546           return HAL_TIMEOUT;
547         }
548       }
549       /* Write data to TXDR */
550       hi2c->Instance->TXDR = (*pData++);
551       sizetmp--;
552       Size--;
553
554       if((sizetmp == 0)&&(Size!=0))
555       {
556         /* Wait until TXE flag is set */
557         if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout) != HAL_OK)      
558         {
559           return HAL_TIMEOUT;
560         }
561         
562         if(Size > 255)
563         {
564           I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
565           sizetmp = 255;
566         }
567         else
568         {
569           I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
570           sizetmp = Size;
571         }
572       }
573
574     }while(Size > 0);
575     
576     /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
577     /* Wait until STOPF flag is set */
578     if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
579     {
580       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
581       {
582         return HAL_ERROR;
583       }
584       else
585       {
586         return HAL_TIMEOUT;
587       }
588     }
589     
590     /* Clear STOP Flag */
591     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
592         
593     /* Clear Configuration Register 2 */
594     __I2C_RESET_CR2(hi2c);
595
596     hi2c->State = HAL_I2C_STATE_READY;    
597     
598     /* Process Unlocked */
599     __HAL_UNLOCK(hi2c);
600
601     return HAL_OK;
602   }
603   else
604   {
605     return HAL_BUSY; 
606   }
607 }
608
609 /**
610   * @brief  Receives in master mode an amount of data in blocking mode. 
611   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
612   *                the configuration information for the specified I2C.
613   * @param  DevAddress: Target device address
614   * @param  pData: Pointer to data buffer
615   * @param  Size: Amount of data to be sent
616   * @param  Timeout: Timeout duration
617   * @retval HAL status
618   */
619 HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
620 {
621   uint32_t sizetmp = 0;
622
623   if(hi2c->State == HAL_I2C_STATE_READY)
624   {    
625     if((pData == NULL ) || (Size == 0)) 
626     {
627       return  HAL_ERROR;                                    
628     }
629     
630     if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
631     {
632       return HAL_BUSY;
633     }
634
635     /* Process Locked */
636     __HAL_LOCK(hi2c);
637     
638     hi2c->State = HAL_I2C_STATE_MASTER_BUSY_RX;
639     hi2c->ErrorCode   = HAL_I2C_ERROR_NONE;
640     
641     /* Send Slave Address */
642     /* Set NBYTES to write and reload if size > 255 and generate RESTART */
643     /* Size > 255, need to set RELOAD bit */
644     if(Size > 255)
645     {
646       I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
647       sizetmp = 255;
648     }
649     else
650     {
651       I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
652       sizetmp = Size;
653     }
654     
655     do
656     {
657       /* Wait until RXNE flag is set */
658       if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, Timeout) != HAL_OK)      
659       {
660         return HAL_TIMEOUT;
661       }
662      
663       /* Write data to RXDR */
664       (*pData++) =hi2c->Instance->RXDR;
665       sizetmp--;
666       Size--;
667
668       if((sizetmp == 0)&&(Size!=0))
669       {
670         /* Wait until TCR flag is set */
671         if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout) != HAL_OK)      
672         {
673           return HAL_TIMEOUT;
674         }
675         
676         if(Size > 255)
677         {
678           I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
679           sizetmp = 255;
680         }
681         else
682         {
683           I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
684           sizetmp = Size;
685         }
686       }
687
688     }while(Size > 0);
689     
690     /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
691     /* Wait until STOPF flag is set */
692     if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
693     {
694       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
695       {
696         return HAL_ERROR;
697       }
698       else
699       {
700         return HAL_TIMEOUT;
701       }
702     }
703     
704     /* Clear STOP Flag */
705     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
706         
707     /* Clear Configuration Register 2 */
708     __I2C_RESET_CR2(hi2c);
709     
710     hi2c->State = HAL_I2C_STATE_READY;    
711     
712     /* Process Unlocked */
713     __HAL_UNLOCK(hi2c);
714     
715     return HAL_OK;
716   }
717   else
718   {
719     return HAL_BUSY; 
720   }
721 }
722
723 /**
724   * @brief  Transmits in slave mode an amount of data in blocking mode. 
725   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
726   *                the configuration information for the specified I2C.
727   * @param  pData: Pointer to data buffer
728   * @param  Size: Amount of data to be sent
729   * @param  Timeout: Timeout duration
730   * @retval HAL status
731   */
732 HAL_StatusTypeDef HAL_I2C_Slave_Transmit(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
733 {
734   if(hi2c->State == HAL_I2C_STATE_READY)
735   {    
736     if((pData == NULL ) || (Size == 0)) 
737     {
738       return  HAL_ERROR;                                    
739     }
740     
741     /* Process Locked */
742     __HAL_LOCK(hi2c);
743     
744     hi2c->State = HAL_I2C_STATE_SLAVE_BUSY_RX;
745     hi2c->ErrorCode   = HAL_I2C_ERROR_NONE;
746     
747     /* Enable Address Acknowledge */
748     hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
749
750     /* Wait until ADDR flag is set */
751     if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout) != HAL_OK)      
752     {
753       /* Disable Address Acknowledge */
754       hi2c->Instance->CR2 |= I2C_CR2_NACK;
755       return HAL_TIMEOUT;
756     }
757     
758     /* Clear ADDR flag */
759     __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
760
761     /* If 10bit addressing mode is selected */
762     if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT)
763     {
764       /* Wait until ADDR flag is set */
765       if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout) != HAL_OK)      
766       {
767         /* Disable Address Acknowledge */
768         hi2c->Instance->CR2 |= I2C_CR2_NACK;
769         return HAL_TIMEOUT;
770       }
771     
772       /* Clear ADDR flag */
773       __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
774     }
775
776     /* Wait until DIR flag is set Transmitter mode */
777     if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_DIR, RESET, Timeout) != HAL_OK)      
778     {
779       /* Disable Address Acknowledge */
780       hi2c->Instance->CR2 |= I2C_CR2_NACK;
781       return HAL_TIMEOUT;
782     }
783
784     do
785     {
786       /* Wait until TXIS flag is set */
787       if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
788       {
789         /* Disable Address Acknowledge */
790         hi2c->Instance->CR2 |= I2C_CR2_NACK;
791
792         if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
793         {
794           return HAL_ERROR;
795         }
796         else
797         {
798           return HAL_TIMEOUT;
799         }
800       }
801       
802       /* Read data from TXDR */
803       hi2c->Instance->TXDR = (*pData++);
804       Size--;
805     }while(Size > 0);
806     
807     /* Wait until STOP flag is set */
808     if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
809     {
810       /* Disable Address Acknowledge */
811       hi2c->Instance->CR2 |= I2C_CR2_NACK;
812
813       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
814       {
815         /* Normal use case for Transmitter mode */
816         /* A NACK is generated to confirm the end of transfer */
817         hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
818       }
819       else
820       {
821         return HAL_TIMEOUT;
822       }
823     }
824     
825     /* Clear STOP flag */
826     __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_STOPF);
827     
828     /* Wait until BUSY flag is reset */ 
829     if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout) != HAL_OK)      
830     {
831       /* Disable Address Acknowledge */
832       hi2c->Instance->CR2 |= I2C_CR2_NACK;
833       return HAL_TIMEOUT;
834     }
835     
836     /* Disable Address Acknowledge */
837     hi2c->Instance->CR2 |= I2C_CR2_NACK;
838
839     hi2c->State = HAL_I2C_STATE_READY;
840     
841     /* Process Unlocked */
842     __HAL_UNLOCK(hi2c);
843     
844     return HAL_OK;
845   }
846   else
847   {
848     return HAL_BUSY; 
849   }
850 }
851
852 /**
853   * @brief  Receive in slave mode an amount of data in blocking mode 
854   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
855   *                the configuration information for the specified I2C.
856   * @param  pData: Pointer to data buffer
857   * @param  Size: Amount of data to be sent
858   * @param  Timeout: Timeout duration
859   * @retval HAL status
860   */
861 HAL_StatusTypeDef HAL_I2C_Slave_Receive(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
862 {
863   if(hi2c->State == HAL_I2C_STATE_READY)
864   {  
865     if((pData == NULL ) || (Size == 0)) 
866     {
867       return  HAL_ERROR;                                    
868     }
869     
870     /* Process Locked */
871     __HAL_LOCK(hi2c);
872     
873     hi2c->State = HAL_I2C_STATE_SLAVE_BUSY_RX;
874     hi2c->ErrorCode   = HAL_I2C_ERROR_NONE;
875     
876     /* Enable Address Acknowledge */
877     hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
878
879     /* Wait until ADDR flag is set */
880     if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout) != HAL_OK)      
881     {
882       /* Disable Address Acknowledge */
883       hi2c->Instance->CR2 |= I2C_CR2_NACK;
884       return HAL_TIMEOUT;
885     }
886
887     /* Clear ADDR flag */
888     __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
889     
890     /* Wait until DIR flag is reset Receiver mode */
891     if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_DIR, SET, Timeout) != HAL_OK)      
892     {
893       /* Disable Address Acknowledge */
894       hi2c->Instance->CR2 |= I2C_CR2_NACK;
895       return HAL_TIMEOUT;
896     }
897
898     while(Size > 0)
899     {
900       /* Wait until RXNE flag is set */
901       if(I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout) != HAL_OK)      
902       {
903         /* Disable Address Acknowledge */
904         hi2c->Instance->CR2 |= I2C_CR2_NACK;
905         if(hi2c->ErrorCode == HAL_I2C_ERROR_TIMEOUT)
906         {
907           return HAL_TIMEOUT;
908         }
909         else
910         {
911           return HAL_ERROR;
912         }
913       }
914       
915       /* Read data from RXDR */
916       (*pData++) = hi2c->Instance->RXDR;
917       Size--;
918     }
919     
920     /* Wait until STOP flag is set */
921     if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
922     {
923       /* Disable Address Acknowledge */
924       hi2c->Instance->CR2 |= I2C_CR2_NACK;
925
926       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
927       {
928         return HAL_ERROR;
929       }
930       else
931       {
932         return HAL_TIMEOUT;
933       }
934     }
935
936     /* Clear STOP flag */
937     __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_STOPF);
938     
939     /* Wait until BUSY flag is reset */ 
940     if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout) != HAL_OK)      
941     {
942       /* Disable Address Acknowledge */
943       hi2c->Instance->CR2 |= I2C_CR2_NACK;
944       return HAL_TIMEOUT;
945     }
946
947     
948     /* Disable Address Acknowledge */
949     hi2c->Instance->CR2 |= I2C_CR2_NACK;
950     
951     hi2c->State = HAL_I2C_STATE_READY;
952
953     /* Process Unlocked */
954     __HAL_UNLOCK(hi2c);
955     
956     return HAL_OK;
957   }
958   else
959   {
960     return HAL_BUSY; 
961   } 
962 }
963
964 /**
965   * @brief  Transmit in master mode an amount of data in no-blocking mode with Interrupt
966   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
967   *                the configuration information for the specified I2C.
968   * @param  DevAddress: Target device address
969   * @param  pData: Pointer to data buffer
970   * @param  Size: Amount of data to be sent
971   * @retval HAL status
972   */
973 HAL_StatusTypeDef HAL_I2C_Master_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
974 {   
975   if(hi2c->State == HAL_I2C_STATE_READY)
976   {
977     if((pData == NULL) || (Size == 0)) 
978     {
979       return  HAL_ERROR;                                    
980     }
981     
982     if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
983     {
984       return HAL_BUSY;
985     }
986
987     /* Process Locked */
988     __HAL_LOCK(hi2c);
989     
990     hi2c->State = HAL_I2C_STATE_MASTER_BUSY_TX;
991     hi2c->ErrorCode   = HAL_I2C_ERROR_NONE;
992     
993     hi2c->pBuffPtr = pData;
994     hi2c->XferCount = Size;
995     if(Size > 255)
996     {
997       hi2c->XferSize = 255;
998     }
999     else
1000     {
1001       hi2c->XferSize = Size;
1002     }
1003     
1004     /* Send Slave Address */
1005     /* Set NBYTES to write and reload if size > 255 and generate RESTART */
1006     if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
1007     {
1008       I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);
1009     }
1010     else
1011     {
1012       I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_WRITE);
1013     }
1014     
1015     /* Process Unlocked */
1016     __HAL_UNLOCK(hi2c); 
1017
1018     /* Note : The I2C interrupts must be enabled after unlocking current process 
1019               to avoid the risk of I2C interrupt handle execution before current
1020               process unlock */
1021
1022
1023     /* Enable ERR, TC, STOP, NACK, TXI interrupt */
1024     /* possible to enable all of these */
1025     /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
1026     __HAL_I2C_ENABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_TXI );
1027         
1028     return HAL_OK;
1029   }
1030   else
1031   {
1032     return HAL_BUSY;
1033   } 
1034 }
1035
1036 /**
1037   * @brief  Receive in master mode an amount of data in no-blocking mode with Interrupt
1038   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1039   *                the configuration information for the specified I2C.
1040   * @param  DevAddress: Target device address
1041   * @param  pData: Pointer to data buffer
1042   * @param  Size: Amount of data to be sent
1043   * @retval HAL status
1044   */
1045 HAL_StatusTypeDef HAL_I2C_Master_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
1046 {
1047   if(hi2c->State == HAL_I2C_STATE_READY)
1048   {
1049     if((pData == NULL) || (Size == 0)) 
1050     {
1051       return  HAL_ERROR;                                    
1052     }
1053     
1054     if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1055     {
1056       return HAL_BUSY;
1057     }
1058
1059     /* Process Locked */
1060     __HAL_LOCK(hi2c);
1061     
1062     hi2c->State = HAL_I2C_STATE_MASTER_BUSY_RX;
1063     hi2c->ErrorCode   = HAL_I2C_ERROR_NONE;
1064     
1065     hi2c->pBuffPtr = pData;
1066     hi2c->XferCount = Size;
1067     if(Size > 255)
1068     {
1069       hi2c->XferSize = 255;
1070     }
1071     else
1072     {
1073       hi2c->XferSize = Size;
1074     }
1075     
1076     /* Send Slave Address */
1077     /* Set NBYTES to write and reload if size > 255 and generate RESTART */
1078     if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
1079     {
1080       I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
1081     }
1082     else
1083     {
1084       I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
1085     }
1086     
1087     /* Process Unlocked */
1088     __HAL_UNLOCK(hi2c); 
1089
1090     /* Note : The I2C interrupts must be enabled after unlocking current process 
1091               to avoid the risk of I2C interrupt handle execution before current
1092               process unlock */
1093     
1094     /* Enable ERR, TC, STOP, NACK, RXI interrupt */
1095     /* possible to enable all of these */
1096     /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
1097     __HAL_I2C_ENABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_RXI );
1098     
1099     return HAL_OK;
1100   }
1101   else
1102   {
1103     return HAL_BUSY; 
1104   } 
1105 }
1106
1107 /**
1108   * @brief  Transmit in slave mode an amount of data in no-blocking mode with Interrupt 
1109   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1110   *                the configuration information for the specified I2C.
1111   * @param  pData: Pointer to data buffer
1112   * @param  Size: Amount of data to be sent
1113   * @retval HAL status
1114   */
1115 HAL_StatusTypeDef HAL_I2C_Slave_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
1116 {
1117   if(hi2c->State == HAL_I2C_STATE_READY)
1118   {
1119     if((pData == NULL) || (Size == 0)) 
1120     {
1121       return  HAL_ERROR;                                    
1122     }
1123     
1124     /* Process Locked */
1125     __HAL_LOCK(hi2c);
1126     
1127     hi2c->State = HAL_I2C_STATE_SLAVE_BUSY_TX;
1128     hi2c->ErrorCode   = HAL_I2C_ERROR_NONE;
1129     
1130     /* Enable Address Acknowledge */
1131     hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
1132
1133     hi2c->pBuffPtr = pData;
1134     hi2c->XferSize = Size;
1135     hi2c->XferCount = Size;
1136     
1137     /* Process Unlocked */
1138     __HAL_UNLOCK(hi2c); 
1139
1140     /* Note : The I2C interrupts must be enabled after unlocking current process 
1141               to avoid the risk of I2C interrupt handle execution before current
1142               process unlock */
1143     
1144     /* Enable ERR, TC, STOP, NACK, TXI interrupt */
1145     /* possible to enable all of these */
1146     /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
1147     __HAL_I2C_ENABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_TXI );
1148     
1149     return HAL_OK;
1150   }
1151   else
1152   {
1153     return HAL_BUSY; 
1154   } 
1155 }
1156
1157 /**
1158   * @brief  Receive in slave mode an amount of data in no-blocking mode with Interrupt 
1159   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1160   *                the configuration information for the specified I2C.
1161   * @param  pData: Pointer to data buffer
1162   * @param  Size: Amount of data to be sent
1163   * @retval HAL status
1164   */
1165 HAL_StatusTypeDef HAL_I2C_Slave_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
1166 {
1167   if(hi2c->State == HAL_I2C_STATE_READY)
1168   {
1169     if((pData == NULL) || (Size == 0)) 
1170     {
1171       return  HAL_ERROR;                                    
1172     }
1173     
1174     /* Process Locked */
1175     __HAL_LOCK(hi2c);
1176     
1177     hi2c->State = HAL_I2C_STATE_SLAVE_BUSY_RX;
1178     hi2c->ErrorCode   = HAL_I2C_ERROR_NONE;
1179     
1180     /* Enable Address Acknowledge */
1181     hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
1182
1183     hi2c->pBuffPtr = pData;
1184     hi2c->XferSize = Size;
1185     hi2c->XferCount = Size;
1186     
1187     /* Process Unlocked */
1188     __HAL_UNLOCK(hi2c); 
1189
1190     /* Note : The I2C interrupts must be enabled after unlocking current process 
1191               to avoid the risk of I2C interrupt handle execution before current
1192               process unlock */
1193     
1194     /* Enable ERR, TC, STOP, NACK, RXI interrupt */
1195     /* possible to enable all of these */
1196     /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
1197     __HAL_I2C_ENABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI);
1198     
1199     return HAL_OK;
1200   }
1201   else
1202   {
1203     return HAL_BUSY; 
1204   }
1205 }
1206
1207 /**
1208   * @brief  Transmit in master mode an amount of data in no-blocking mode with DMA
1209   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1210   *                the configuration information for the specified I2C.
1211   * @param  DevAddress: Target device address
1212   * @param  pData: Pointer to data buffer
1213   * @param  Size: Amount of data to be sent
1214   * @retval HAL status
1215   */
1216 HAL_StatusTypeDef HAL_I2C_Master_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
1217 {
1218   if(hi2c->State == HAL_I2C_STATE_READY)
1219   {
1220     if((pData == NULL) || (Size == 0)) 
1221     {
1222       return  HAL_ERROR;                                    
1223     }     
1224
1225     if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1226     {
1227       return HAL_BUSY;
1228     }
1229
1230     /* Process Locked */
1231     __HAL_LOCK(hi2c);
1232     
1233     hi2c->State = HAL_I2C_STATE_MASTER_BUSY_TX;
1234     hi2c->ErrorCode   = HAL_I2C_ERROR_NONE;
1235     
1236     hi2c->pBuffPtr = pData;
1237     hi2c->XferCount = Size;
1238     if(Size > 255)
1239     {
1240       hi2c->XferSize = 255;
1241     }
1242     else
1243     {
1244       hi2c->XferSize = Size;
1245     }
1246     
1247     /* Set the I2C DMA transfer complete callback */
1248     hi2c->hdmatx->XferCpltCallback = I2C_DMAMasterTransmitCplt;
1249     
1250     /* Set the DMA error callback */
1251     hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
1252     
1253     /* Enable the DMA channel */
1254     HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
1255     
1256     /* Send Slave Address */
1257     /* Set NBYTES to write and reload if size > 255 and generate RESTART */
1258     if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
1259     {
1260       I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);
1261     }
1262     else
1263     {
1264       I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_WRITE);
1265     }  
1266
1267     /* Wait until TXIS flag is set */
1268     if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, I2C_TIMEOUT_TXIS) != HAL_OK)
1269     {
1270       /* Disable Address Acknowledge */
1271       hi2c->Instance->CR2 |= I2C_CR2_NACK;
1272
1273       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1274       {
1275         return HAL_ERROR;
1276       }
1277       else
1278       {
1279         return HAL_TIMEOUT;
1280       }
1281     }
1282
1283     
1284     /* Enable DMA Request */
1285     hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;   
1286     
1287     /* Process Unlocked */
1288     __HAL_UNLOCK(hi2c);
1289     
1290     return HAL_OK;
1291   }
1292   else
1293   {
1294     return HAL_BUSY;
1295   }
1296 }
1297
1298 /**
1299   * @brief  Receive in master mode an amount of data in no-blocking mode with DMA 
1300   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1301   *                the configuration information for the specified I2C.
1302   * @param  DevAddress: Target device address
1303   * @param  pData: Pointer to data buffer
1304   * @param  Size: Amount of data to be sent
1305   * @retval HAL status
1306   */
1307 HAL_StatusTypeDef HAL_I2C_Master_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
1308 {
1309   if(hi2c->State == HAL_I2C_STATE_READY)
1310   {
1311     if((pData == NULL) || (Size == 0)) 
1312     {
1313       return  HAL_ERROR;                                    
1314     }  
1315
1316     if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1317     {
1318       return HAL_BUSY;
1319     }
1320
1321     /* Process Locked */
1322     __HAL_LOCK(hi2c);
1323     
1324     hi2c->State = HAL_I2C_STATE_MASTER_BUSY_RX;
1325     hi2c->ErrorCode   = HAL_I2C_ERROR_NONE;
1326     
1327     hi2c->pBuffPtr = pData;
1328     hi2c->XferCount = Size;
1329     if(Size > 255)
1330     {
1331       hi2c->XferSize = 255;
1332     }
1333     else
1334     {
1335       hi2c->XferSize = Size;
1336     }
1337     
1338     /* Set the I2C DMA transfer complete callback */
1339     hi2c->hdmarx->XferCpltCallback = I2C_DMAMasterReceiveCplt;
1340     
1341     /* Set the DMA error callback */
1342     hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
1343     
1344     /* Enable the DMA channel */
1345     HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData, hi2c->XferSize);
1346     
1347     /* Send Slave Address */
1348     /* Set NBYTES to write and reload if size > 255 and generate RESTART */
1349     if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
1350     {
1351       I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
1352     }
1353     else
1354     {
1355       I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
1356     }
1357
1358     /* Wait until RXNE flag is set */
1359     if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, I2C_TIMEOUT_RXNE) != HAL_OK)      
1360     {
1361       return HAL_TIMEOUT;
1362     }
1363
1364     
1365     /* Enable DMA Request */
1366     hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;   
1367     
1368     /* Process Unlocked */
1369     __HAL_UNLOCK(hi2c);
1370     
1371     return HAL_OK;
1372   }
1373   else
1374   {
1375     return HAL_BUSY;
1376   }
1377 }
1378
1379 /**
1380   * @brief  Transmit in slave mode an amount of data in no-blocking mode with DMA 
1381   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1382   *                the configuration information for the specified I2C.
1383   * @param  pData: Pointer to data buffer
1384   * @param  Size: Amount of data to be sent
1385   * @retval HAL status
1386   */
1387 HAL_StatusTypeDef HAL_I2C_Slave_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
1388 {
1389   if(hi2c->State == HAL_I2C_STATE_READY)
1390   {
1391     if((pData == NULL) || (Size == 0)) 
1392     {
1393       return  HAL_ERROR;                                    
1394     }   
1395     /* Process Locked */
1396     __HAL_LOCK(hi2c); 
1397     
1398     hi2c->State = HAL_I2C_STATE_SLAVE_BUSY_TX;
1399     hi2c->ErrorCode   = HAL_I2C_ERROR_NONE;
1400     
1401     hi2c->pBuffPtr = pData;
1402     hi2c->XferCount = Size;
1403     hi2c->XferSize = Size;
1404     
1405     /* Set the I2C DMA transfer complete callback */
1406     hi2c->hdmatx->XferCpltCallback = I2C_DMASlaveTransmitCplt;
1407     
1408     /* Set the DMA error callback */
1409     hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
1410     
1411     /* Enable the DMA channel */
1412     HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
1413     
1414     /* Enable Address Acknowledge */
1415     hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
1416
1417     /* Wait until ADDR flag is set */
1418     if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, I2C_TIMEOUT_ADDR) != HAL_OK)      
1419     {
1420       /* Disable Address Acknowledge */
1421       hi2c->Instance->CR2 |= I2C_CR2_NACK;
1422       return HAL_TIMEOUT;
1423     }
1424
1425     /* Clear ADDR flag */
1426     __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
1427     
1428     /* If 10bits addressing mode is selected */
1429     if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT)
1430     {
1431       /* Wait until ADDR flag is set */
1432       if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, I2C_TIMEOUT_ADDR) != HAL_OK)      
1433       {
1434         /* Disable Address Acknowledge */
1435         hi2c->Instance->CR2 |= I2C_CR2_NACK;
1436         return HAL_TIMEOUT;
1437       }
1438
1439       /* Clear ADDR flag */
1440       __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
1441     }
1442     
1443     /* Wait until DIR flag is set Transmitter mode */
1444     if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_DIR, RESET, I2C_TIMEOUT_BUSY) != HAL_OK)      
1445     {
1446       /* Disable Address Acknowledge */
1447       hi2c->Instance->CR2 |= I2C_CR2_NACK;
1448       return HAL_TIMEOUT;
1449     }
1450       
1451     /* Enable DMA Request */
1452     hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN; 
1453     
1454     /* Process Unlocked */
1455     __HAL_UNLOCK(hi2c);
1456     
1457     return HAL_OK;
1458   }
1459   else
1460   {
1461     return HAL_BUSY;
1462   }
1463 }
1464
1465 /**
1466   * @brief  Receive in slave mode an amount of data in no-blocking mode with DMA 
1467   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1468   *                the configuration information for the specified I2C.
1469   * @param  pData: Pointer to data buffer
1470   * @param  Size: Amount of data to be sent
1471   * @retval HAL status
1472   */
1473 HAL_StatusTypeDef HAL_I2C_Slave_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
1474 {
1475   if(hi2c->State == HAL_I2C_STATE_READY)
1476   {
1477     if((pData == NULL) || (Size == 0)) 
1478     {
1479       return  HAL_ERROR;                                    
1480     }   
1481     /* Process Locked */
1482     __HAL_LOCK(hi2c);
1483     
1484     hi2c->State = HAL_I2C_STATE_SLAVE_BUSY_RX;
1485     hi2c->ErrorCode   = HAL_I2C_ERROR_NONE;
1486     
1487     hi2c->pBuffPtr = pData;
1488     hi2c->XferSize = Size;
1489     hi2c->XferCount = Size;
1490     
1491     /* Set the I2C DMA transfer complete callback */
1492     hi2c->hdmarx->XferCpltCallback = I2C_DMASlaveReceiveCplt;
1493     
1494     /* Set the DMA error callback */
1495     hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
1496     
1497     /* Enable the DMA channel */
1498     HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData, Size);
1499     
1500     /* Enable Address Acknowledge */
1501     hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
1502
1503     /* Wait until ADDR flag is set */
1504     if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, I2C_TIMEOUT_ADDR) != HAL_OK)      
1505     {
1506       /* Disable Address Acknowledge */
1507       hi2c->Instance->CR2 |= I2C_CR2_NACK;
1508       return HAL_TIMEOUT;
1509     }
1510
1511     /* Clear ADDR flag */
1512     __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
1513     
1514     /* Wait until DIR flag is set Receiver mode */
1515     if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_DIR, SET, I2C_TIMEOUT_DIR) != HAL_OK)      
1516     {
1517       /* Disable Address Acknowledge */
1518       hi2c->Instance->CR2 |= I2C_CR2_NACK;
1519       return HAL_TIMEOUT;
1520     }
1521  
1522     /* Enable DMA Request */
1523     hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;  
1524     
1525     /* Process Unlocked */
1526     __HAL_UNLOCK(hi2c);
1527     
1528     return HAL_OK;
1529   }
1530   else
1531   {
1532     return HAL_BUSY;
1533   }
1534 }
1535
1536 /**
1537   * @brief  Write an amount of data in blocking mode to a specific memory address
1538   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1539   *                the configuration information for the specified I2C.
1540   * @param  DevAddress: Target device address
1541   * @param  MemAddress: Internal memory address
1542   * @param  MemAddSize: Size of internal memory address
1543   * @param  pData: Pointer to data buffer
1544   * @param  Size: Amount of data to be sent
1545   * @param  Timeout: Timeout duration
1546   * @retval HAL status
1547   */
1548 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)
1549 {
1550   uint32_t Sizetmp = 0;
1551
1552   /* Check the parameters */
1553   assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
1554   
1555   if(hi2c->State == HAL_I2C_STATE_READY)
1556   { 
1557     if((pData == NULL) || (Size == 0)) 
1558     {
1559       return  HAL_ERROR;                                    
1560     }
1561
1562     if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1563     {
1564       return HAL_BUSY;
1565     }
1566
1567     /* Process Locked */
1568     __HAL_LOCK(hi2c);
1569     
1570     hi2c->State = HAL_I2C_STATE_MEM_BUSY_TX;
1571     hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1572     
1573     /* Send Slave Address and Memory Address */
1574     if(I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, Timeout) != HAL_OK)
1575     {
1576       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1577       {
1578         /* Process Unlocked */
1579         __HAL_UNLOCK(hi2c);
1580         return HAL_ERROR;
1581       }
1582       else
1583       {
1584         /* Process Unlocked */
1585         __HAL_UNLOCK(hi2c);
1586         return HAL_TIMEOUT;
1587       }
1588     }
1589
1590     /* Set NBYTES to write and reload if size > 255 */
1591     /* Size > 255, need to set RELOAD bit */
1592     if(Size > 255)
1593     {
1594       I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
1595       Sizetmp = 255;
1596     }
1597     else
1598     {
1599       I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
1600       Sizetmp = Size;
1601     }
1602     
1603     do
1604     {
1605       /* Wait until TXIS flag is set */
1606       if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
1607       {
1608         if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1609         {
1610           return HAL_ERROR;
1611         }
1612         else
1613         {
1614           return HAL_TIMEOUT;
1615         }
1616       }
1617      
1618       /* Write data to DR */
1619       hi2c->Instance->TXDR = (*pData++);
1620       Sizetmp--;
1621       Size--;
1622
1623       if((Sizetmp == 0)&&(Size!=0))
1624       {
1625         /* Wait until TCR flag is set */
1626         if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout) != HAL_OK)      
1627         {
1628           return HAL_TIMEOUT;
1629         }
1630
1631         
1632         if(Size > 255)
1633         {
1634           I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
1635           Sizetmp = 255;
1636         }
1637         else
1638         {
1639           I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
1640           Sizetmp = Size;
1641         }
1642       }
1643       
1644     }while(Size > 0);
1645     
1646     /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
1647     /* Wait until STOPF flag is reset */ 
1648     if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
1649     {
1650       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1651       {
1652         return HAL_ERROR;
1653       }
1654       else
1655       {
1656         return HAL_TIMEOUT;
1657       }
1658     }
1659     
1660     /* Clear STOP Flag */
1661     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
1662         
1663     /* Clear Configuration Register 2 */
1664     __I2C_RESET_CR2(hi2c);
1665
1666     hi2c->State = HAL_I2C_STATE_READY;    
1667     
1668     /* Process Unlocked */
1669     __HAL_UNLOCK(hi2c);
1670     
1671     return HAL_OK;
1672   }
1673   else
1674   {
1675     return HAL_BUSY;
1676   }
1677 }
1678
1679 /**
1680   * @brief  Read an amount of data in blocking mode from a specific memory address
1681   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1682   *                the configuration information for the specified I2C.
1683   * @param  DevAddress: Target device address
1684   * @param  MemAddress: Internal memory address
1685   * @param  MemAddSize: Size of internal memory address
1686   * @param  pData: Pointer to data buffer
1687   * @param  Size: Amount of data to be sent
1688   * @param  Timeout: Timeout duration
1689   * @retval HAL status
1690   */
1691 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)
1692 {
1693   uint32_t Sizetmp = 0;
1694
1695   /* Check the parameters */
1696   assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
1697   
1698   if(hi2c->State == HAL_I2C_STATE_READY)
1699   {    
1700     if((pData == NULL) || (Size == 0)) 
1701     {
1702       return  HAL_ERROR;                                    
1703     }
1704
1705     if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1706     {
1707       return HAL_BUSY;
1708     }
1709
1710     /* Process Locked */
1711     __HAL_LOCK(hi2c);
1712     
1713     hi2c->State = HAL_I2C_STATE_MEM_BUSY_RX;
1714     hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1715     
1716     /* Send Slave Address and Memory Address */
1717     if(I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, Timeout) != HAL_OK)
1718     {
1719       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1720       {
1721         /* Process Unlocked */
1722         __HAL_UNLOCK(hi2c);
1723         return HAL_ERROR;
1724       }
1725       else
1726       {
1727         /* Process Unlocked */
1728         __HAL_UNLOCK(hi2c);
1729         return HAL_TIMEOUT;
1730       }
1731     }
1732
1733     /* Send Slave Address */
1734     /* Set NBYTES to write and reload if size > 255 and generate RESTART */
1735     /* Size > 255, need to set RELOAD bit */
1736     if(Size > 255)
1737     {
1738       I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
1739       Sizetmp = 255;
1740     }
1741     else
1742     {
1743       I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
1744       Sizetmp = Size;
1745     }
1746     
1747     do
1748     {  
1749       /* Wait until RXNE flag is set */
1750       if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, Timeout) != HAL_OK)      
1751       {
1752         return HAL_TIMEOUT;
1753       }
1754           
1755       /* Read data from RXDR */
1756       (*pData++) = hi2c->Instance->RXDR;
1757
1758       /* Decrement the Size counter */
1759       Sizetmp--;
1760       Size--;   
1761
1762       if((Sizetmp == 0)&&(Size!=0))
1763       {
1764         /* Wait until TCR flag is set */
1765         if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout) != HAL_OK)      
1766         {
1767           return HAL_TIMEOUT;
1768         }
1769         
1770         if(Size > 255)
1771         {
1772           I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
1773           Sizetmp = 255;
1774         }
1775         else
1776         {
1777           I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
1778           Sizetmp = Size;
1779         }
1780       }
1781
1782     }while(Size > 0);
1783
1784     /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
1785     /* Wait until STOPF flag is reset */ 
1786     if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
1787     {
1788       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1789       {
1790         return HAL_ERROR;
1791       }
1792       else
1793       {
1794         return HAL_TIMEOUT;
1795       }
1796     }
1797
1798     /* Clear STOP Flag */
1799     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
1800         
1801     /* Clear Configuration Register 2 */
1802     __I2C_RESET_CR2(hi2c);
1803     
1804     hi2c->State = HAL_I2C_STATE_READY;
1805     
1806     /* Process Unlocked */
1807     __HAL_UNLOCK(hi2c);
1808     
1809     return HAL_OK;
1810   }
1811   else
1812   {
1813     return HAL_BUSY;
1814   }
1815 }
1816
1817 /**
1818   * @brief  Write an amount of data in no-blocking mode with Interrupt to a specific memory address
1819   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1820   *                the configuration information for the specified I2C.
1821   * @param  DevAddress: Target device address
1822   * @param  MemAddress: Internal memory address
1823   * @param  MemAddSize: Size of internal memory address
1824   * @param  pData: Pointer to data buffer
1825   * @param  Size: Amount of data to be sent
1826   * @retval HAL status
1827   */
1828 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)
1829 {
1830   /* Check the parameters */
1831   assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
1832   
1833   if(hi2c->State == HAL_I2C_STATE_READY)
1834   {
1835     if((pData == NULL) || (Size == 0)) 
1836     {
1837       return  HAL_ERROR;                                    
1838     }
1839     
1840     if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1841     {
1842       return HAL_BUSY;
1843     }
1844
1845     /* Process Locked */
1846     __HAL_LOCK(hi2c);
1847     
1848     hi2c->State = HAL_I2C_STATE_MEM_BUSY_TX;
1849     hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1850     
1851     hi2c->pBuffPtr = pData;
1852     hi2c->XferCount = Size;
1853     if(Size > 255)
1854     {
1855       hi2c->XferSize = 255;
1856     }
1857     else
1858     {
1859       hi2c->XferSize = Size;
1860     }
1861     
1862     /* Send Slave Address and Memory Address */
1863     if(I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG) != HAL_OK)
1864     {
1865       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1866       {
1867         /* Process Unlocked */
1868         __HAL_UNLOCK(hi2c);
1869         return HAL_ERROR;
1870       }
1871       else
1872       {
1873         /* Process Unlocked */
1874         __HAL_UNLOCK(hi2c);
1875         return HAL_TIMEOUT;
1876       }
1877     }
1878
1879     /* Set NBYTES to write and reload if size > 255 */
1880     /* Size > 255, need to set RELOAD bit */
1881     if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
1882     {
1883       I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
1884     }
1885     else
1886     {
1887       I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
1888     }  
1889
1890     /* Process Unlocked */
1891     __HAL_UNLOCK(hi2c); 
1892
1893     /* Note : The I2C interrupts must be enabled after unlocking current process 
1894               to avoid the risk of I2C interrupt handle execution before current
1895               process unlock */
1896     
1897     /* Enable ERR, TC, STOP, NACK, TXI interrupt */
1898     /* possible to enable all of these */
1899     /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
1900     __HAL_I2C_ENABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_TXI );
1901     
1902     return HAL_OK;
1903   }
1904   else
1905   {
1906     return HAL_BUSY;
1907   }
1908 }
1909
1910 /**
1911   * @brief  Read an amount of data in no-blocking mode with Interrupt from a specific memory address
1912   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1913   *                the configuration information for the specified I2C.
1914   * @param  DevAddress: Target device address
1915   * @param  MemAddress: Internal memory address
1916   * @param  MemAddSize: Size of internal memory address
1917   * @param  pData: Pointer to data buffer
1918   * @param  Size: Amount of data to be sent
1919   * @retval HAL status
1920   */
1921 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)
1922 {
1923   /* Check the parameters */
1924   assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
1925   
1926   if(hi2c->State == HAL_I2C_STATE_READY)
1927   {
1928     if((pData == NULL) || (Size == 0)) 
1929     {
1930       return  HAL_ERROR;                                    
1931     }
1932     
1933     if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1934     {
1935       return HAL_BUSY;
1936     }
1937
1938     /* Process Locked */
1939     __HAL_LOCK(hi2c);
1940     
1941     hi2c->State = HAL_I2C_STATE_MEM_BUSY_RX;
1942     
1943     hi2c->pBuffPtr = pData;
1944     hi2c->XferCount = Size;
1945     if(Size > 255)
1946     {
1947       hi2c->XferSize = 255;
1948     }
1949     else
1950     {
1951       hi2c->XferSize = Size;
1952     }
1953     
1954     /* Send Slave Address and Memory Address */
1955     if(I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG) != HAL_OK)
1956     {
1957       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1958       {
1959         /* Process Unlocked */
1960         __HAL_UNLOCK(hi2c);
1961         return HAL_ERROR;
1962       }
1963       else
1964       {
1965         /* Process Unlocked */
1966         __HAL_UNLOCK(hi2c);
1967         return HAL_TIMEOUT;
1968       }
1969     }
1970       
1971     /* Set NBYTES to write and reload if size > 255 and generate RESTART */
1972     /* Size > 255, need to set RELOAD bit */
1973     if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
1974     {
1975       I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
1976     }
1977     else
1978     {
1979       I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
1980     }
1981
1982     /* Process Unlocked */
1983     __HAL_UNLOCK(hi2c); 
1984
1985     /* Note : The I2C interrupts must be enabled after unlocking current process 
1986               to avoid the risk of I2C interrupt handle execution before current
1987               process unlock */
1988     
1989     /* Enable ERR, TC, STOP, NACK, RXI interrupt */
1990     /* possible to enable all of these */
1991     /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
1992     __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_RXI );
1993     
1994     return HAL_OK;
1995   }
1996   else
1997   {
1998     return HAL_BUSY; 
1999   }   
2000 }
2001
2002 /**
2003   * @brief  Write an amount of data in no-blocking mode with DMA to a specific memory address
2004   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2005   *                the configuration information for the specified I2C.
2006   * @param  DevAddress: Target device address
2007   * @param  MemAddress: Internal memory address
2008   * @param  MemAddSize: Size of internal memory address
2009   * @param  pData: Pointer to data buffer
2010   * @param  Size: Amount of data to be sent
2011   * @retval HAL status
2012   */
2013 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)
2014 {
2015   /* Check the parameters */
2016   assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
2017   
2018   if(hi2c->State == HAL_I2C_STATE_READY)
2019   {
2020     if((pData == NULL) || (Size == 0)) 
2021     {
2022       return  HAL_ERROR;                                    
2023     }
2024     
2025     if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
2026     {
2027       return HAL_BUSY;
2028     }
2029
2030     /* Process Locked */
2031     __HAL_LOCK(hi2c);
2032     
2033     hi2c->State = HAL_I2C_STATE_MEM_BUSY_TX;
2034     hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
2035     
2036     hi2c->pBuffPtr = pData;
2037     hi2c->XferCount = Size;
2038     if(Size > 255)
2039     {
2040       hi2c->XferSize = 255;
2041     }
2042     else
2043     {
2044       hi2c->XferSize = Size;
2045     }
2046     
2047     /* Set the I2C DMA transfer complete callback */
2048     hi2c->hdmatx->XferCpltCallback = I2C_DMAMemTransmitCplt;
2049     
2050     /* Set the DMA error callback */
2051     hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
2052     
2053     /* Enable the DMA channel */
2054     HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
2055     
2056     /* Send Slave Address and Memory Address */
2057     if(I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG) != HAL_OK)
2058     {
2059       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
2060       {
2061         /* Process Unlocked */
2062         __HAL_UNLOCK(hi2c);
2063         return HAL_ERROR;
2064       }
2065       else
2066       {
2067         /* Process Unlocked */
2068         __HAL_UNLOCK(hi2c);
2069         return HAL_TIMEOUT;
2070       }
2071     }
2072     
2073     /* Send Slave Address */
2074     /* Set NBYTES to write and reload if size > 255 */
2075     if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
2076     {
2077       I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
2078     }
2079     else
2080     {
2081       I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
2082     }
2083     
2084     /* Wait until TXIS flag is set */
2085     if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, I2C_TIMEOUT_TXIS) != HAL_OK)
2086     {
2087       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
2088       {
2089         return HAL_ERROR;
2090       }
2091       else
2092       {
2093         return HAL_TIMEOUT;
2094       }
2095     }
2096
2097     /* Enable DMA Request */
2098     hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;  
2099     
2100     /* Process Unlocked */
2101     __HAL_UNLOCK(hi2c);
2102     
2103     return HAL_OK;
2104   }
2105   else
2106   {
2107     return HAL_BUSY;
2108   }
2109 }
2110
2111 /**
2112   * @brief  Reads an amount of data in no-blocking mode with DMA from a specific memory address.
2113   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2114   *                the configuration information for the specified I2C.
2115   * @param  DevAddress: Target device address
2116   * @param  MemAddress: Internal memory address
2117   * @param  MemAddSize: Size of internal memory address
2118   * @param  pData: Pointer to data buffer
2119   * @param  Size: Amount of data to be read
2120   * @retval HAL status
2121   */
2122 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)
2123 {
2124   /* Check the parameters */
2125   assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
2126   
2127   if(hi2c->State == HAL_I2C_STATE_READY)
2128   {
2129     if((pData == NULL) || (Size == 0)) 
2130     {
2131       return  HAL_ERROR;                                    
2132     }
2133
2134     if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
2135     {
2136       return HAL_BUSY;
2137     }
2138
2139     /* Process Locked */
2140     __HAL_LOCK(hi2c);
2141     
2142     hi2c->State = HAL_I2C_STATE_MEM_BUSY_RX;
2143     
2144     hi2c->pBuffPtr = pData;
2145     hi2c->XferCount = Size;
2146     if(Size > 255)
2147     {
2148       hi2c->XferSize = 255;
2149     }
2150     else
2151     {
2152       hi2c->XferSize = Size;
2153     }
2154
2155     /* Set the I2C DMA transfer complete callback */
2156     hi2c->hdmarx->XferCpltCallback = I2C_DMAMemReceiveCplt;
2157     
2158     /* Set the DMA error callback */
2159     hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
2160     
2161     /* Enable the DMA channel */
2162     HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData, hi2c->XferSize);
2163     
2164     /* Send Slave Address and Memory Address */
2165     if(I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG) != HAL_OK)
2166     {
2167       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
2168       {
2169         /* Process Unlocked */
2170         __HAL_UNLOCK(hi2c);
2171         return HAL_ERROR;
2172       }
2173       else
2174       {
2175         /* Process Unlocked */
2176         __HAL_UNLOCK(hi2c);
2177         return HAL_TIMEOUT;
2178       }
2179     }
2180     
2181     /* Set NBYTES to write and reload if size > 255 and generate RESTART */
2182     if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
2183     {
2184       I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
2185     }
2186     else
2187     {
2188       I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
2189     }
2190
2191     /* Wait until RXNE flag is set */
2192     if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, I2C_TIMEOUT_RXNE) != HAL_OK)      
2193     {
2194       return HAL_TIMEOUT;
2195     }
2196     
2197     /* Enable DMA Request */
2198     hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;  
2199     
2200     /* Process Unlocked */
2201     __HAL_UNLOCK(hi2c);
2202     
2203     return HAL_OK;
2204   }
2205   else
2206   {
2207     return HAL_BUSY;
2208   }
2209 }
2210
2211 /**
2212   * @brief  Checks if target device is ready for communication. 
2213   * @note   This function is used with Memory devices
2214   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2215   *                the configuration information for the specified I2C.
2216   * @param  DevAddress: Target device address
2217   * @param  Trials: Number of trials
2218   * @param  Timeout: Timeout duration
2219   * @retval HAL status
2220   */
2221 HAL_StatusTypeDef HAL_I2C_IsDeviceReady(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout)
2222 {  
2223   uint32_t tickstart = 0;
2224   
2225   __IO uint32_t I2C_Trials = 0;
2226  
2227   if(hi2c->State == HAL_I2C_STATE_READY)
2228   {
2229     if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
2230     {
2231       return HAL_BUSY;
2232     }
2233
2234     /* Process Locked */
2235     __HAL_LOCK(hi2c);
2236     
2237     hi2c->State = HAL_I2C_STATE_BUSY;
2238     hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
2239     
2240     do
2241     {
2242       /* Generate Start */
2243       hi2c->Instance->CR2 = __I2C_GENERATE_START(hi2c->Init.AddressingMode,DevAddress);
2244       
2245       /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
2246       /* Wait until STOPF flag is set or a NACK flag is set*/
2247       tickstart = HAL_GetTick();
2248       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))
2249       {
2250         if(Timeout != HAL_MAX_DELAY)
2251         {
2252           if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
2253           {
2254             /* Device is ready */
2255             hi2c->State = HAL_I2C_STATE_READY;
2256             /* Process Unlocked */
2257             __HAL_UNLOCK(hi2c);         
2258             return HAL_TIMEOUT;
2259           }
2260         } 
2261       }
2262       
2263       /* Check if the NACKF flag has not been set */
2264       if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == RESET)
2265       {
2266         /* Wait until STOPF flag is reset */ 
2267         if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout) != HAL_OK)
2268         {
2269           return HAL_TIMEOUT;
2270         }
2271         
2272         /* Clear STOP Flag */
2273         __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
2274
2275         /* Device is ready */
2276         hi2c->State = HAL_I2C_STATE_READY;
2277         
2278         /* Process Unlocked */
2279         __HAL_UNLOCK(hi2c);
2280         
2281         return HAL_OK;
2282       }
2283       else
2284       {
2285         /* Wait until STOPF flag is reset */ 
2286         if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout) != HAL_OK)
2287         {
2288           return HAL_TIMEOUT;
2289         }
2290
2291         /* Clear NACK Flag */
2292         __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
2293
2294         /* Clear STOP Flag, auto generated with autoend*/
2295         __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
2296       }
2297       
2298       /* Check if the maximum allowed number of trials has been reached */
2299       if (I2C_Trials++ == Trials)
2300       {
2301         /* Generate Stop */
2302         hi2c->Instance->CR2 |= I2C_CR2_STOP;
2303         
2304         /* Wait until STOPF flag is reset */ 
2305         if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout) != HAL_OK)
2306         {
2307           return HAL_TIMEOUT;
2308         }
2309         
2310         /* Clear STOP Flag */
2311         __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
2312       }      
2313     }while(I2C_Trials < Trials);
2314
2315     hi2c->State = HAL_I2C_STATE_READY;
2316
2317     /* Process Unlocked */
2318     __HAL_UNLOCK(hi2c);
2319         
2320     return HAL_TIMEOUT;
2321   }
2322   else
2323   {
2324     return HAL_BUSY;
2325   }
2326 }
2327 /**
2328   * @}
2329   */
2330
2331 /** @defgroup IRQ_Handler_and_Callbacks IRQ Handler and Callbacks
2332  * @{
2333  */   
2334
2335 /**
2336   * @brief  This function handles I2C event interrupt request.
2337   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2338   *                the configuration information for the specified I2C.
2339   * @retval None
2340   */
2341 void HAL_I2C_EV_IRQHandler(I2C_HandleTypeDef *hi2c)
2342 {
2343   /* I2C in mode Transmitter ---------------------------------------------------*/
2344   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))
2345   {     
2346     /* Slave mode selected */
2347     if (hi2c->State == HAL_I2C_STATE_SLAVE_BUSY_TX)
2348     {
2349       I2C_SlaveTransmit_ISR(hi2c);
2350     }
2351   }
2352     
2353   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))
2354   {     
2355     /* Master mode selected */
2356     if ((hi2c->State == HAL_I2C_STATE_MASTER_BUSY_TX) || (hi2c->State == HAL_I2C_STATE_MEM_BUSY_TX))
2357     {
2358       I2C_MasterTransmit_ISR(hi2c);
2359     }
2360   }
2361
2362   /* I2C in mode Receiver ----------------------------------------------------*/
2363   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))
2364   {
2365     /* Slave mode selected */
2366     if (hi2c->State == HAL_I2C_STATE_SLAVE_BUSY_RX)
2367     {
2368       I2C_SlaveReceive_ISR(hi2c);
2369     }
2370   } 
2371   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))
2372   {
2373     /* Master mode selected */
2374     if ((hi2c->State == HAL_I2C_STATE_MASTER_BUSY_RX) || (hi2c->State == HAL_I2C_STATE_MEM_BUSY_RX))
2375     {
2376       I2C_MasterReceive_ISR(hi2c);
2377     }
2378   } 
2379 }
2380
2381 /**
2382   * @brief  This function handles I2C error interrupt request.
2383   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2384   *                the configuration information for the specified I2C.
2385   * @retval None
2386   */
2387 void HAL_I2C_ER_IRQHandler(I2C_HandleTypeDef *hi2c)
2388 {
2389   /* I2C Bus error interrupt occurred ------------------------------------*/
2390   if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BERR) == SET) && (__HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_ERRI) == SET))
2391   { 
2392     hi2c->ErrorCode |= HAL_I2C_ERROR_BERR;
2393    
2394     /* Clear BERR flag */
2395     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_BERR);
2396   }
2397   
2398   /* I2C Over-Run/Under-Run interrupt occurred ----------------------------------------*/
2399   if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_OVR) == SET) && (__HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_ERRI) == SET))
2400   { 
2401     hi2c->ErrorCode |= HAL_I2C_ERROR_OVR;
2402
2403     /* Clear OVR flag */
2404     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_OVR);
2405   }
2406
2407   /* I2C Arbitration Loss error interrupt occurred -------------------------------------*/
2408   if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ARLO) == SET) && (__HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_ERRI) == SET))
2409   { 
2410     hi2c->ErrorCode |= HAL_I2C_ERROR_ARLO;
2411
2412     /* Clear ARLO flag */
2413     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ARLO);
2414   }
2415
2416   /* Call the Error Callback in case of Error detected */
2417   if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
2418   {
2419     hi2c->State = HAL_I2C_STATE_READY;
2420     
2421     HAL_I2C_ErrorCallback(hi2c);
2422   }
2423 }
2424
2425 /**
2426   * @brief  Master Tx Transfer completed callbacks.
2427   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2428   *                the configuration information for the specified I2C.
2429   * @retval None
2430   */
2431  __weak void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c)
2432 {
2433   /* NOTE : This function Should not be modified, when the callback is needed,
2434             the HAL_I2C_TxCpltCallback could be implemented in the user file
2435    */ 
2436 }
2437
2438 /**
2439   * @brief  Master Rx Transfer completed callbacks.
2440   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2441   *                the configuration information for the specified I2C.
2442   * @retval None
2443   */
2444 __weak void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c)
2445 {
2446   /* NOTE : This function Should not be modified, when the callback is needed,
2447             the HAL_I2C_TxCpltCallback could be implemented in the user file
2448    */
2449 }
2450
2451 /** @brief  Slave Tx Transfer completed callbacks.
2452   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2453   *                the configuration information for the specified I2C.
2454   * @retval None
2455   */
2456  __weak void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c)
2457 {
2458   /* NOTE : This function Should not be modified, when the callback is needed,
2459             the HAL_I2C_TxCpltCallback could be implemented in the user file
2460    */ 
2461 }
2462
2463 /**
2464   * @brief  Slave Rx Transfer completed callbacks.
2465   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2466   *                the configuration information for the specified I2C.
2467   * @retval None
2468   */
2469 __weak void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c)
2470 {
2471   /* NOTE : This function Should not be modified, when the callback is needed,
2472             the HAL_I2C_TxCpltCallback could be implemented in the user file
2473    */
2474 }
2475
2476 /**
2477   * @brief  Memory Tx Transfer completed callbacks.
2478   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2479   *                the configuration information for the specified I2C.
2480   * @retval None
2481   */
2482  __weak void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef *hi2c)
2483 {
2484   /* NOTE : This function Should not be modified, when the callback is needed,
2485             the HAL_I2C_TxCpltCallback could be implemented in the user file
2486    */ 
2487 }
2488
2489 /**
2490   * @brief  Memory Rx Transfer completed callbacks.
2491   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2492   *                the configuration information for the specified I2C.
2493   * @retval None
2494   */
2495 __weak void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c)
2496 {
2497   /* NOTE : This function Should not be modified, when the callback is needed,
2498             the HAL_I2C_TxCpltCallback could be implemented in the user file
2499    */
2500 }
2501
2502 /**
2503   * @brief  I2C error callbacks.
2504   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2505   *                the configuration information for the specified I2C.
2506   * @retval None
2507   */
2508  __weak void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
2509 {
2510   /* NOTE : This function Should not be modified, when the callback is needed,
2511             the HAL_I2C_ErrorCallback could be implemented in the user file
2512    */ 
2513 }
2514
2515 /**
2516   * @}
2517   */
2518
2519
2520 /** @defgroup I2C_Exported_Functions_Group3 Peripheral State and Errors functions
2521  *  @brief   Peripheral State and Errors functions
2522  *
2523 @verbatim   
2524  ===============================================================================
2525             ##### Peripheral State and Errors functions #####
2526  ===============================================================================  
2527     [..]
2528     This subsection permit to get in run-time the status of the peripheral 
2529     and the data flow.
2530
2531 @endverbatim
2532   * @{
2533   */
2534
2535 /**
2536   * @brief  Returns the I2C state.
2537   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2538   *                the configuration information for the specified I2C.
2539   * @retval HAL state
2540   */
2541 HAL_I2C_StateTypeDef HAL_I2C_GetState(I2C_HandleTypeDef *hi2c)
2542 {
2543   return hi2c->State;
2544 }
2545
2546 /**
2547 * @brief  Return the I2C error code
2548   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2549   *              the configuration information for the specified I2C.
2550 * @retval I2C Error Code
2551 */
2552 uint32_t HAL_I2C_GetError(I2C_HandleTypeDef *hi2c)
2553 {
2554   return hi2c->ErrorCode;
2555 }
2556
2557 /**
2558   * @}
2559   */  
2560
2561 /**
2562   * @}
2563   */   
2564
2565 /** @addtogroup I2C_Private_Functions
2566   * @{
2567   */
2568   
2569 /**
2570   * @brief  Handle Interrupt Flags Master Transmit Mode
2571   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2572   *                the configuration information for the specified I2C.
2573   * @retval HAL status
2574   */
2575 static HAL_StatusTypeDef I2C_MasterTransmit_ISR(I2C_HandleTypeDef *hi2c) 
2576 {
2577   uint16_t DevAddress;
2578   
2579   /* Process Locked */
2580   __HAL_LOCK(hi2c); 
2581   
2582   if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == SET)
2583   {
2584     /* Write data to TXDR */
2585     hi2c->Instance->TXDR = (*hi2c->pBuffPtr++);
2586     hi2c->XferSize--;
2587     hi2c->XferCount--;  
2588   }
2589   else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TCR) == SET)
2590   {
2591     if((hi2c->XferSize == 0)&&(hi2c->XferCount!=0))
2592     {
2593       DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);
2594       
2595       if(hi2c->XferCount > 255)
2596       {    
2597         I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
2598         hi2c->XferSize = 255;
2599       }
2600       else
2601       {
2602         I2C_TransferConfig(hi2c,DevAddress,hi2c->XferCount, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
2603         hi2c->XferSize = hi2c->XferCount;
2604       }
2605     }
2606     else
2607     {
2608       /* Process Unlocked */
2609       __HAL_UNLOCK(hi2c);
2610       
2611       /* Wrong size Status regarding TCR flag event */
2612       hi2c->ErrorCode |= HAL_I2C_ERROR_SIZE;
2613       HAL_I2C_ErrorCallback(hi2c);
2614     }
2615   }
2616   else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TC) == SET)
2617   {
2618     if(hi2c->XferCount == 0)
2619     {
2620       /* Generate Stop */
2621       hi2c->Instance->CR2 |= I2C_CR2_STOP;
2622     }
2623     else
2624     {
2625       /* Process Unlocked */
2626       __HAL_UNLOCK(hi2c);
2627       
2628       /* Wrong size Status regarding TCR flag event */
2629       hi2c->ErrorCode |= HAL_I2C_ERROR_SIZE;
2630       HAL_I2C_ErrorCallback(hi2c);
2631     }
2632   }
2633   else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
2634   {
2635     /* Disable ERR, TC, STOP, NACK, TXI interrupt */
2636     __HAL_I2C_DISABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_TXI );
2637
2638     /* Clear STOP Flag */
2639     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
2640
2641     /* Clear Configuration Register 2 */
2642     __I2C_RESET_CR2(hi2c);
2643
2644     hi2c->State = HAL_I2C_STATE_READY;
2645
2646     /* Process Unlocked */
2647     __HAL_UNLOCK(hi2c);
2648
2649     if(hi2c->State == HAL_I2C_STATE_MEM_BUSY_TX)
2650     {
2651       HAL_I2C_MemTxCpltCallback(hi2c);
2652     }
2653     else
2654     {
2655       HAL_I2C_MasterTxCpltCallback(hi2c);
2656     }
2657   }
2658   else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
2659   {
2660     /* Clear NACK Flag */
2661     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
2662
2663     /* Process Unlocked */
2664     __HAL_UNLOCK(hi2c);
2665     
2666     hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
2667     HAL_I2C_ErrorCallback(hi2c);
2668   }
2669   
2670   /* Process Unlocked */
2671   __HAL_UNLOCK(hi2c);
2672   
2673   return HAL_OK;    
2674 }  
2675
2676 /**
2677   * @brief  Handle Interrupt Flags Master Receive Mode
2678   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2679   *                the configuration information for the specified I2C.
2680   * @retval HAL status
2681   */
2682 static HAL_StatusTypeDef I2C_MasterReceive_ISR(I2C_HandleTypeDef *hi2c) 
2683 {
2684   uint16_t DevAddress;
2685
2686   /* Process Locked */
2687   __HAL_LOCK(hi2c);
2688   
2689   if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
2690   {  
2691     /* Read data from RXDR */
2692     (*hi2c->pBuffPtr++) = hi2c->Instance->RXDR;
2693     hi2c->XferSize--;
2694     hi2c->XferCount--;
2695   }
2696   else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TCR) == SET)
2697   {
2698     if((hi2c->XferSize == 0)&&(hi2c->XferCount!=0))
2699     {                  
2700       DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);
2701       
2702       if(hi2c->XferCount > 255)
2703       {
2704         I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
2705         hi2c->XferSize = 255;
2706       }      
2707       else
2708       {    
2709         I2C_TransferConfig(hi2c,DevAddress,hi2c->XferCount, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
2710         hi2c->XferSize = hi2c->XferCount;
2711       } 
2712     } 
2713     else
2714     {
2715       /* Process Unlocked */
2716       __HAL_UNLOCK(hi2c);
2717       
2718       /* Wrong size Status regarding TCR flag event */
2719       hi2c->ErrorCode |= HAL_I2C_ERROR_SIZE;
2720       HAL_I2C_ErrorCallback(hi2c);
2721     }
2722   }
2723   else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TC) == SET)
2724   {
2725     if(hi2c->XferCount == 0)
2726     {
2727       /* Generate Stop */
2728       hi2c->Instance->CR2 |= I2C_CR2_STOP;
2729     }
2730     else
2731     {
2732       /* Process Unlocked */
2733       __HAL_UNLOCK(hi2c);
2734       
2735       /* Wrong size Status regarding TCR flag event */
2736       hi2c->ErrorCode |= HAL_I2C_ERROR_SIZE;
2737       HAL_I2C_ErrorCallback(hi2c);
2738     }
2739   }
2740   else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
2741   {
2742     /* Disable ERR, TC, STOP, NACK, TXI interrupt */
2743     __HAL_I2C_DISABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_RXI );
2744       
2745     /* Clear STOP Flag */
2746     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
2747       
2748     /* Clear Configuration Register 2 */
2749     __I2C_RESET_CR2(hi2c);
2750     
2751     hi2c->State = HAL_I2C_STATE_READY;
2752
2753     /* Process Unlocked */
2754     __HAL_UNLOCK(hi2c);
2755     
2756     if(hi2c->State == HAL_I2C_STATE_MEM_BUSY_RX)
2757     {
2758       HAL_I2C_MemRxCpltCallback(hi2c);
2759     }
2760     else
2761     {
2762       HAL_I2C_MasterRxCpltCallback(hi2c);
2763     }
2764   }
2765   else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
2766   {
2767     /* Clear NACK Flag */
2768     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
2769
2770     /* Process Unlocked */
2771     __HAL_UNLOCK(hi2c);
2772     
2773     hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
2774     HAL_I2C_ErrorCallback(hi2c);
2775   }
2776     
2777   /* Process Unlocked */
2778   __HAL_UNLOCK(hi2c); 
2779   
2780   return HAL_OK; 
2781
2782 }  
2783
2784 /**
2785   * @brief  Handle Interrupt Flags Slave Transmit Mode
2786   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2787   *                the configuration information for the specified I2C.
2788   * @retval HAL status
2789   */
2790 static HAL_StatusTypeDef I2C_SlaveTransmit_ISR(I2C_HandleTypeDef *hi2c) 
2791 {
2792   /* Process locked */
2793   __HAL_LOCK(hi2c);
2794   
2795   if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) != RESET)
2796   {
2797     /* Check that I2C transfer finished */
2798     /* if yes, normal usecase, a NACK is sent by the MASTER when Transfer is finished */
2799     /* Mean XferCount == 0*/
2800     /* So clear Flag NACKF only */
2801     if(hi2c->XferCount == 0)
2802     {
2803       /* Clear NACK Flag */
2804       __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
2805
2806       /* Process Unlocked */
2807       __HAL_UNLOCK(hi2c);
2808     }
2809     else
2810     {
2811       /* if no, error usecase, a Non-Acknowledge of last Data is generated by the MASTER*/
2812       /* Clear NACK Flag */
2813       __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
2814
2815       /* Set ErrorCode corresponding to a Non-Acknowledge */
2816       hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
2817
2818       /* Process Unlocked */
2819       __HAL_UNLOCK(hi2c);
2820     
2821       /* Call the Error callback to prevent upper layer */
2822       HAL_I2C_ErrorCallback(hi2c);
2823     }
2824   }
2825   else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == SET)
2826   {
2827     /* Clear ADDR flag */
2828     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
2829   }
2830   /* Check first if STOPF is set          */
2831   /* to prevent a Write Data in TX buffer */
2832   /* which is stuck in TXDR until next    */
2833   /* communication with Master            */
2834   else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
2835   {
2836     /* Disable ERRI, TCI, STOPI, NACKI, ADDRI, RXI, TXI interrupt */
2837     __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 );
2838     
2839     /* Disable Address Acknowledge */
2840     hi2c->Instance->CR2 |= I2C_CR2_NACK;
2841
2842     /* Clear STOP Flag */
2843     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
2844
2845     hi2c->State = HAL_I2C_STATE_READY;
2846     
2847     /* Process Unlocked */
2848     __HAL_UNLOCK(hi2c);
2849
2850     HAL_I2C_SlaveTxCpltCallback(hi2c);
2851   }
2852   else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == SET)
2853   {
2854     /* Write data to TXDR only if XferCount not reach "0" */
2855     /* A TXIS flag can be set, during STOP treatment      */
2856     if(hi2c->XferCount > 0)
2857     {
2858       /* Write data to TXDR */
2859       hi2c->Instance->TXDR = (*hi2c->pBuffPtr++);
2860       hi2c->XferCount--;
2861     }
2862   }
2863
2864   /* Process Unlocked */
2865   __HAL_UNLOCK(hi2c);
2866   
2867   return HAL_OK;
2868 }  
2869
2870 /**
2871   * @brief  Handle Interrupt Flags Slave Receive Mode
2872   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2873   *                the configuration information for the specified I2C.
2874   * @retval HAL status
2875   */
2876 static HAL_StatusTypeDef I2C_SlaveReceive_ISR(I2C_HandleTypeDef *hi2c) 
2877 {
2878   /* Process Locked */
2879   __HAL_LOCK(hi2c);
2880   
2881   if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) != RESET)
2882   {
2883     /* Clear NACK Flag */
2884     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
2885
2886     /* Process Unlocked */
2887     __HAL_UNLOCK(hi2c);
2888     
2889     hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
2890     HAL_I2C_ErrorCallback(hi2c);
2891   }
2892   else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == SET)
2893   {
2894     /* Clear ADDR flag */
2895     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
2896   }
2897   else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
2898   {
2899     /* Read data from RXDR */
2900     (*hi2c->pBuffPtr++) = hi2c->Instance->RXDR;
2901     hi2c->XferSize--;
2902     hi2c->XferCount--;
2903   }
2904   else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
2905   {
2906     /* Disable ERRI, TCI, STOPI, NACKI, ADDRI, RXI, TXI interrupt */
2907     __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_RXI );
2908     
2909     /* Disable Address Acknowledge */
2910     hi2c->Instance->CR2 |= I2C_CR2_NACK;
2911
2912     /* Clear STOP Flag */
2913     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
2914
2915     hi2c->State = HAL_I2C_STATE_READY;
2916     
2917     /* Process Unlocked */
2918     __HAL_UNLOCK(hi2c);
2919
2920     HAL_I2C_SlaveRxCpltCallback(hi2c);
2921   }
2922
2923   /* Process Unlocked */
2924   __HAL_UNLOCK(hi2c);
2925   
2926   return HAL_OK;     
2927 }  
2928
2929 /**
2930   * @brief  Master sends target device address followed by internal memory address for write request.
2931   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2932   *                the configuration information for the specified I2C.
2933   * @param  DevAddress: Target device address
2934   * @param  MemAddress: Internal memory address
2935   * @param  MemAddSize: Size of internal memory address
2936   * @param  Timeout: Timeout duration
2937   * @retval HAL status
2938   */
2939 static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout)   
2940 {
2941   I2C_TransferConfig(hi2c,DevAddress,MemAddSize, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);
2942
2943   /* Wait until TXIS flag is set */
2944   if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
2945   {
2946     if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
2947     {
2948       return HAL_ERROR;
2949     }
2950     else
2951     {
2952       return HAL_TIMEOUT;
2953     }
2954   }
2955
2956   /* If Memory address size is 8Bit */
2957   if(MemAddSize == I2C_MEMADD_SIZE_8BIT)
2958   {
2959     /* Send Memory Address */
2960     hi2c->Instance->TXDR = __I2C_MEM_ADD_LSB(MemAddress);    
2961   }      
2962   /* If Memory address size is 16Bit */
2963   else
2964   {
2965     /* Send MSB of Memory Address */
2966     hi2c->Instance->TXDR = __I2C_MEM_ADD_MSB(MemAddress); 
2967     
2968     /* Wait until TXIS flag is set */
2969     if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
2970     {
2971       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
2972       {
2973         return HAL_ERROR;
2974       }
2975       else
2976       {
2977         return HAL_TIMEOUT;
2978       }
2979     }
2980     
2981     /* Send LSB of Memory Address */
2982     hi2c->Instance->TXDR = __I2C_MEM_ADD_LSB(MemAddress);  
2983   }
2984   
2985   /* Wait until TCR flag is set */
2986   if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout) != HAL_OK)      
2987   {
2988     return HAL_TIMEOUT;
2989   }
2990
2991 return HAL_OK;
2992 }
2993
2994 /**
2995   * @brief  Master sends target device address followed by internal memory address for read request.
2996   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2997   *                the configuration information for the specified I2C.
2998   * @param  DevAddress: Target device address
2999   * @param  MemAddress: Internal memory address
3000   * @param  MemAddSize: Size of internal memory address
3001   * @param  Timeout: Timeout duration
3002   * @retval HAL status
3003   */
3004 static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout)
3005 {
3006   I2C_TransferConfig(hi2c,DevAddress,MemAddSize, I2C_SOFTEND_MODE, I2C_GENERATE_START_WRITE);
3007   
3008   /* Wait until TXIS flag is set */
3009   if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
3010   {
3011     if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3012     {
3013       return HAL_ERROR;
3014     }
3015     else
3016     {
3017       return HAL_TIMEOUT;
3018     }
3019   }
3020   
3021   /* If Memory address size is 8Bit */
3022   if(MemAddSize == I2C_MEMADD_SIZE_8BIT)
3023   {
3024     /* Send Memory Address */
3025     hi2c->Instance->TXDR = __I2C_MEM_ADD_LSB(MemAddress);    
3026   }      
3027   /* If Mememory address size is 16Bit */
3028   else
3029   {
3030     /* Send MSB of Memory Address */
3031     hi2c->Instance->TXDR = __I2C_MEM_ADD_MSB(MemAddress); 
3032     
3033     /* Wait until TXIS flag is set */
3034     if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
3035     {
3036       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3037       {
3038         return HAL_ERROR;
3039       }
3040       else
3041       {
3042         return HAL_TIMEOUT;
3043       }
3044     }
3045     
3046     /* Send LSB of Memory Address */
3047     hi2c->Instance->TXDR = __I2C_MEM_ADD_LSB(MemAddress);  
3048   }
3049   
3050   /* Wait until TC flag is set */
3051   if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TC, RESET, Timeout) != HAL_OK)      
3052   {
3053     return HAL_TIMEOUT;
3054   }
3055   
3056   return HAL_OK;
3057 }
3058
3059 /**
3060   * @brief  DMA I2C master transmit process complete callback.
3061   * @param  hdma: DMA handle
3062   * @retval None
3063   */
3064 static void I2C_DMAMasterTransmitCplt(DMA_HandleTypeDef *hdma) 
3065 {
3066   uint16_t DevAddress;
3067   I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3068   
3069   /* Check if last DMA request was done with RELOAD */
3070   /* Set NBYTES to write and reload if size > 255 */
3071   if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
3072   {
3073     /* Wait until TCR flag is set */
3074     if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, I2C_TIMEOUT_TCR) != HAL_OK)      
3075     {
3076       hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3077     }
3078
3079     /* Disable DMA Request */
3080     hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN; 
3081     
3082     /* Check if Errors has been detected during transfer */
3083     if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3084     {
3085       /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3086       /* Wait until STOPF flag is reset */ 
3087       if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3088       {
3089         if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3090         {
3091           hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3092         }
3093         else
3094         {
3095           hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3096         }
3097       }
3098     
3099       /* Clear STOP Flag */
3100       __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3101           
3102       /* Clear Configuration Register 2 */
3103       __I2C_RESET_CR2(hi2c);
3104
3105       hi2c->XferCount = 0;
3106     
3107       hi2c->State = HAL_I2C_STATE_READY;
3108       HAL_I2C_ErrorCallback(hi2c);
3109     }
3110     else
3111     {
3112       hi2c->pBuffPtr += hi2c->XferSize;
3113       hi2c->XferCount -= hi2c->XferSize;
3114       if(hi2c->XferCount > 255)
3115       {
3116         hi2c->XferSize = 255;
3117       }
3118       else
3119       {
3120         hi2c->XferSize = hi2c->XferCount;
3121       }
3122
3123       DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);
3124               
3125       /* Enable the DMA channel */
3126       HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
3127       
3128       /* Send Slave Address */
3129       /* Set NBYTES to write and reload if size > 255 */
3130       if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
3131       {
3132         I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
3133       }
3134       else
3135       {
3136         I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
3137       }  
3138
3139       /* Wait until TXIS flag is set */
3140       if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, I2C_TIMEOUT_TXIS) != HAL_OK)
3141       {
3142         /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3143         /* Wait until STOPF flag is reset */ 
3144         if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3145         {
3146           if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3147           {
3148             hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3149           }
3150           else
3151           {
3152             hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3153           }
3154         }
3155       
3156         /* Clear STOP Flag */
3157         __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3158             
3159         /* Clear Configuration Register 2 */
3160         __I2C_RESET_CR2(hi2c);
3161
3162         hi2c->XferCount = 0;
3163       
3164         hi2c->State = HAL_I2C_STATE_READY;
3165         HAL_I2C_ErrorCallback(hi2c);
3166       }
3167       else
3168       {
3169         /* Enable DMA Request */
3170         hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
3171       }
3172     }
3173   }
3174   else
3175   {
3176     /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3177     /* Wait until STOPF flag is reset */ 
3178     if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3179     {
3180       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3181       {
3182         hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3183       }
3184       else
3185       {
3186         hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3187       }
3188     }
3189   
3190     /* Clear STOP Flag */
3191     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3192         
3193     /* Clear Configuration Register 2 */
3194     __I2C_RESET_CR2(hi2c);
3195
3196     /* Disable DMA Request */
3197     hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN; 
3198   
3199     hi2c->XferCount = 0;
3200   
3201     hi2c->State = HAL_I2C_STATE_READY;
3202
3203    /* Check if Errors has been detected during transfer */
3204     if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3205     {
3206       HAL_I2C_ErrorCallback(hi2c);
3207     }
3208     else
3209     {
3210       HAL_I2C_MasterTxCpltCallback(hi2c);
3211     }
3212   }
3213 }
3214
3215 /**
3216   * @brief  DMA I2C slave transmit process complete callback. 
3217   * @param  hdma: DMA handle
3218   * @retval None
3219   */
3220 static void I2C_DMASlaveTransmitCplt(DMA_HandleTypeDef *hdma) 
3221 {
3222   I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3223   
3224   /* Wait until STOP flag is set */
3225   if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3226   {
3227     if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3228     {
3229       /* Normal Use case, a AF is generated by master */
3230       /* to inform slave the end of transfer */
3231       hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
3232     }
3233     else
3234     {
3235       hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3236     }
3237   }
3238   
3239   /* Clear STOP flag */
3240   __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_STOPF);
3241   
3242   /* Wait until BUSY flag is reset */ 
3243   if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY) != HAL_OK)      
3244   {
3245     hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3246   }
3247   
3248   /* Disable DMA Request */
3249   hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN; 
3250   
3251   hi2c->XferCount = 0;
3252   
3253   hi2c->State = HAL_I2C_STATE_READY;
3254
3255   /* Check if Errors has been detected during transfer */
3256   if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3257   {
3258     HAL_I2C_ErrorCallback(hi2c);
3259   }
3260   else
3261   {
3262     HAL_I2C_SlaveTxCpltCallback(hi2c);
3263   }
3264 }
3265
3266 /**
3267   * @brief DMA I2C master receive process complete callback 
3268   * @param  hdma: DMA handle
3269   * @retval None
3270   */
3271 static void I2C_DMAMasterReceiveCplt(DMA_HandleTypeDef *hdma) 
3272 {
3273   I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3274   uint16_t DevAddress;
3275   
3276   /* Check if last DMA request was done with RELOAD */
3277   /* Set NBYTES to write and reload if size > 255 */
3278   if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
3279   {
3280     /* Wait until TCR flag is set */
3281     if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, I2C_TIMEOUT_TCR) != HAL_OK)      
3282     {
3283       hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3284     }
3285
3286     /* Disable DMA Request */
3287     hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN; 
3288
3289     /* Check if Errors has been detected during transfer */
3290     if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3291     {
3292       /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3293       /* Wait until STOPF flag is reset */ 
3294       if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3295       {
3296         if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3297         {
3298           hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3299         }
3300         else
3301         {
3302           hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3303         }
3304       }
3305     
3306       /* Clear STOP Flag */
3307       __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3308           
3309       /* Clear Configuration Register 2 */
3310       __I2C_RESET_CR2(hi2c);
3311     
3312       hi2c->XferCount = 0;
3313     
3314       hi2c->State = HAL_I2C_STATE_READY;
3315       HAL_I2C_ErrorCallback(hi2c);
3316     }
3317     else
3318     {
3319       hi2c->pBuffPtr += hi2c->XferSize;
3320       hi2c->XferCount -= hi2c->XferSize;
3321       if(hi2c->XferCount > 255)
3322       {
3323         hi2c->XferSize = 255;
3324       }
3325       else
3326       {
3327         hi2c->XferSize = hi2c->XferCount;
3328       }
3329
3330       DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);
3331               
3332       /* Enable the DMA channel */
3333       HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);
3334       
3335       /* Send Slave Address */
3336       /* Set NBYTES to write and reload if size > 255 */
3337       if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
3338       {
3339         I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
3340       }
3341       else
3342       {
3343         I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
3344       }  
3345
3346       /* Wait until RXNE flag is set */
3347       if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, I2C_TIMEOUT_RXNE) != HAL_OK)      
3348       {
3349         hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3350       }
3351       
3352       /* Check if Errors has been detected during transfer */
3353       if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3354       {
3355         /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3356         /* Wait until STOPF flag is reset */ 
3357         if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3358         {
3359           if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3360           {
3361             hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3362           }
3363           else
3364           {
3365             hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3366           }
3367         }
3368       
3369         /* Clear STOP Flag */
3370         __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3371             
3372         /* Clear Configuration Register 2 */
3373         __I2C_RESET_CR2(hi2c);
3374       
3375         hi2c->XferCount = 0;
3376       
3377         hi2c->State = HAL_I2C_STATE_READY;
3378       
3379         HAL_I2C_ErrorCallback(hi2c);
3380       }
3381       else
3382       {
3383         /* Enable DMA Request */
3384         hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
3385       }
3386     }
3387   }
3388   else
3389   {
3390     /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3391     /* Wait until STOPF flag is reset */ 
3392     if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3393     {
3394       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3395       {
3396         hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3397       }
3398       else
3399       {
3400         hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3401       }
3402     }
3403   
3404     /* Clear STOP Flag */
3405     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3406         
3407     /* Clear Configuration Register 2 */
3408     __I2C_RESET_CR2(hi2c);
3409   
3410     /* Disable DMA Request */
3411     hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN; 
3412   
3413     hi2c->XferCount = 0;
3414   
3415     hi2c->State = HAL_I2C_STATE_READY;
3416
3417     /* Check if Errors has been detected during transfer */
3418     if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3419     {
3420       HAL_I2C_ErrorCallback(hi2c);
3421     }
3422     else
3423     {
3424       HAL_I2C_MasterRxCpltCallback(hi2c);
3425     }
3426   }
3427 }
3428
3429 /**
3430   * @brief  DMA I2C slave receive process complete callback.
3431   * @param  hdma: DMA handle
3432   * @retval None
3433   */
3434 static void I2C_DMASlaveReceiveCplt(DMA_HandleTypeDef *hdma) 
3435 {  
3436   I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3437   
3438   /* Wait until STOPF flag is reset */ 
3439   if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3440   {
3441     if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3442     {
3443       hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3444     }
3445     else
3446     {
3447       hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3448     }
3449   }
3450   
3451   /* Clear STOPF flag */
3452   __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3453   
3454   /* Wait until BUSY flag is reset */ 
3455   if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY) != HAL_OK)      
3456   {
3457     hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3458   }
3459   
3460   /* Disable DMA Request */
3461   hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN; 
3462   
3463   /* Disable Address Acknowledge */
3464   hi2c->Instance->CR2 |= I2C_CR2_NACK;
3465
3466   hi2c->XferCount = 0;
3467   
3468   hi2c->State = HAL_I2C_STATE_READY;
3469
3470   /* Check if Errors has been detected during transfer */
3471   if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3472   {
3473     HAL_I2C_ErrorCallback(hi2c);
3474   }
3475   else
3476   {
3477     HAL_I2C_SlaveRxCpltCallback(hi2c);
3478   }
3479 }
3480
3481 /**
3482   * @brief DMA I2C Memory Write process complete callback 
3483   * @param hdma : DMA handle
3484   * @retval None
3485   */
3486 static void I2C_DMAMemTransmitCplt(DMA_HandleTypeDef *hdma)   
3487 {
3488   uint16_t DevAddress;
3489   I2C_HandleTypeDef* hi2c = ( I2C_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
3490   
3491   /* Check if last DMA request was done with RELOAD */
3492   /* Set NBYTES to write and reload if size > 255 */
3493   if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
3494   {
3495     /* Wait until TCR flag is set */
3496     if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, I2C_TIMEOUT_TCR) != HAL_OK)      
3497     {
3498       hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3499     }
3500
3501     /* Disable DMA Request */
3502     hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN; 
3503     
3504     /* Check if Errors has been detected during transfer */
3505     if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3506     {
3507       /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3508       /* Wait until STOPF flag is reset */ 
3509       if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3510       {
3511         if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3512         {
3513           hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3514         }
3515         else
3516         {
3517           hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3518         }
3519       }
3520     
3521       /* Clear STOP Flag */
3522       __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3523           
3524       /* Clear Configuration Register 2 */
3525       __I2C_RESET_CR2(hi2c);
3526
3527       hi2c->XferCount = 0;
3528     
3529       hi2c->State = HAL_I2C_STATE_READY;
3530       HAL_I2C_ErrorCallback(hi2c);
3531     }
3532     else
3533     {
3534       hi2c->pBuffPtr += hi2c->XferSize;
3535       hi2c->XferCount -= hi2c->XferSize;
3536       if(hi2c->XferCount > 255)
3537       {
3538         hi2c->XferSize = 255;
3539       }
3540       else
3541       {
3542         hi2c->XferSize = hi2c->XferCount;
3543       }
3544
3545       DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);
3546               
3547       /* Enable the DMA channel */
3548       HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
3549       
3550       /* Send Slave Address */
3551       /* Set NBYTES to write and reload if size > 255 */
3552       if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
3553       {
3554         I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
3555       }
3556       else
3557       {
3558         I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
3559       }  
3560
3561       /* Wait until TXIS flag is set */
3562       if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, I2C_TIMEOUT_TXIS) != HAL_OK)
3563       {
3564         /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3565         /* Wait until STOPF flag is reset */ 
3566         if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3567         {
3568           if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3569           {
3570             hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3571           }
3572           else
3573           {
3574             hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3575           }
3576         }
3577       
3578         /* Clear STOP Flag */
3579         __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3580             
3581         /* Clear Configuration Register 2 */
3582         __I2C_RESET_CR2(hi2c);
3583
3584         hi2c->XferCount = 0;
3585       
3586         hi2c->State = HAL_I2C_STATE_READY;
3587         HAL_I2C_ErrorCallback(hi2c);
3588       }
3589       else
3590       {
3591         /* Enable DMA Request */
3592         hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
3593       }
3594     }
3595   }
3596   else
3597   {
3598     /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3599     /* Wait until STOPF flag is reset */ 
3600     if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3601     {
3602       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3603       {
3604         hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3605       }
3606       else
3607       {
3608         hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3609       }
3610     }
3611   
3612     /* Clear STOP Flag */
3613     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3614         
3615     /* Clear Configuration Register 2 */
3616     __I2C_RESET_CR2(hi2c);
3617
3618     /* Disable DMA Request */
3619     hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN; 
3620   
3621     hi2c->XferCount = 0;
3622   
3623     hi2c->State = HAL_I2C_STATE_READY;
3624
3625     /* Check if Errors has been detected during transfer */
3626     if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3627     {
3628       HAL_I2C_ErrorCallback(hi2c);
3629     }
3630     else
3631     {
3632       HAL_I2C_MemTxCpltCallback(hi2c);
3633     }
3634   }
3635 }
3636
3637 /**
3638   * @brief  DMA I2C Memory Read process complete callback
3639   * @param  hdma: DMA handle
3640   * @retval None
3641   */
3642 static void I2C_DMAMemReceiveCplt(DMA_HandleTypeDef *hdma)   
3643 {  
3644   I2C_HandleTypeDef* hi2c = ( I2C_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;  
3645   uint16_t DevAddress;
3646   
3647   /* Check if last DMA request was done with RELOAD */
3648   /* Set NBYTES to write and reload if size > 255 */
3649   if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
3650   {
3651     /* Wait until TCR flag is set */
3652     if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, I2C_TIMEOUT_TCR) != HAL_OK)      
3653     {
3654       hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3655     }
3656
3657     /* Disable DMA Request */
3658     hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN; 
3659
3660     /* Check if Errors has been detected during transfer */
3661     if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3662     {
3663       /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3664       /* Wait until STOPF flag is reset */ 
3665       if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3666       {
3667         if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3668         {
3669           hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3670         }
3671         else
3672         {
3673           hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3674         }
3675       }
3676     
3677       /* Clear STOP Flag */
3678       __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3679           
3680       /* Clear Configuration Register 2 */
3681       __I2C_RESET_CR2(hi2c);
3682     
3683       hi2c->XferCount = 0;
3684     
3685       hi2c->State = HAL_I2C_STATE_READY;
3686       HAL_I2C_ErrorCallback(hi2c);
3687     }
3688     else
3689     {
3690       hi2c->pBuffPtr += hi2c->XferSize;
3691       hi2c->XferCount -= hi2c->XferSize;
3692       if(hi2c->XferCount > 255)
3693       {
3694         hi2c->XferSize = 255;
3695       }
3696       else
3697       {
3698         hi2c->XferSize = hi2c->XferCount;
3699       }
3700
3701       DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);
3702               
3703       /* Enable the DMA channel */
3704       HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);
3705       
3706       /* Send Slave Address */
3707       /* Set NBYTES to write and reload if size > 255 */
3708       if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
3709       {
3710         I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
3711       }
3712       else
3713       {
3714         I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
3715       }  
3716
3717       /* Wait until RXNE flag is set */
3718       if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, I2C_TIMEOUT_RXNE) != HAL_OK)      
3719       {
3720         hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3721       }
3722       
3723       /* Check if Errors has been detected during transfer */
3724       if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3725       {
3726         /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3727         /* Wait until STOPF flag is reset */ 
3728         if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3729         {
3730           if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3731           {
3732             hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3733           }
3734           else
3735           {
3736             hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3737           }
3738         }
3739       
3740         /* Clear STOP Flag */
3741         __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3742             
3743         /* Clear Configuration Register 2 */
3744         __I2C_RESET_CR2(hi2c);
3745       
3746         hi2c->XferCount = 0;
3747       
3748         hi2c->State = HAL_I2C_STATE_READY;
3749         HAL_I2C_ErrorCallback(hi2c);
3750       }
3751       else
3752       {
3753         /* Enable DMA Request */
3754         hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
3755       }
3756     }
3757   }
3758   else
3759   {
3760     /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3761     /* Wait until STOPF flag is reset */ 
3762     if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3763     {
3764       if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3765       {
3766         hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3767       }
3768       else
3769       {
3770         hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3771       }
3772     }
3773   
3774     /* Clear STOP Flag */
3775     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3776         
3777     /* Clear Configuration Register 2 */
3778     __I2C_RESET_CR2(hi2c);
3779   
3780     /* Disable DMA Request */
3781     hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN; 
3782   
3783     hi2c->XferCount = 0;
3784   
3785     hi2c->State = HAL_I2C_STATE_READY;
3786
3787     /* Check if Errors has been detected during transfer */
3788     if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3789     {
3790       HAL_I2C_ErrorCallback(hi2c);
3791     }
3792     else
3793     {
3794       HAL_I2C_MemRxCpltCallback(hi2c);
3795     }
3796   }
3797 }
3798
3799 /**
3800   * @brief  DMA I2C communication error callback. 
3801   * @param hdma : DMA handle
3802   * @retval None
3803   */
3804 static void I2C_DMAError(DMA_HandleTypeDef *hdma)   
3805 {
3806   I2C_HandleTypeDef* hi2c = ( I2C_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
3807   
3808   /* Disable Acknowledge */
3809   hi2c->Instance->CR2 |= I2C_CR2_NACK;
3810   
3811   hi2c->XferCount = 0;
3812   
3813   hi2c->State = HAL_I2C_STATE_READY;
3814   
3815   hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
3816   
3817   HAL_I2C_ErrorCallback(hi2c);
3818 }
3819
3820 /**
3821   * @brief  This function handles I2C Communication Timeout.
3822   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
3823   *                the configuration information for the specified I2C.
3824   * @param  Flag: specifies the I2C flag to check.
3825   * @param  Status: The new Flag status (SET or RESET).
3826   * @param  Timeout: Timeout duration
3827   * @retval HAL status
3828   */
3829 static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout)  
3830 {  
3831   uint32_t tickstart = HAL_GetTick();
3832      
3833   /* Wait until flag is set */
3834   if(Status == RESET)
3835   {    
3836     while(__HAL_I2C_GET_FLAG(hi2c, Flag) == RESET)
3837     {
3838       /* Check for the Timeout */
3839       if(Timeout != HAL_MAX_DELAY)
3840       {
3841         if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
3842         {
3843           hi2c->State= HAL_I2C_STATE_READY;
3844           /* Process Unlocked */
3845           __HAL_UNLOCK(hi2c);
3846           return HAL_TIMEOUT;
3847         }
3848       }
3849     }
3850   }
3851   else
3852   {
3853     while(__HAL_I2C_GET_FLAG(hi2c, Flag) != RESET)
3854     {
3855       /* Check for the Timeout */
3856       if(Timeout != HAL_MAX_DELAY)
3857       {
3858         if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
3859         {
3860           hi2c->State= HAL_I2C_STATE_READY;
3861           /* Process Unlocked */
3862           __HAL_UNLOCK(hi2c);
3863           return HAL_TIMEOUT;
3864         }
3865       }
3866     }
3867   }
3868   return HAL_OK;
3869 }
3870
3871 /**
3872   * @brief  This function handles I2C Communication Timeout for specific usage of TXIS flag.
3873   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
3874   *                the configuration information for the specified I2C.
3875   * @param  Timeout: Timeout duration
3876   * @retval HAL status
3877   */
3878 static HAL_StatusTypeDef I2C_WaitOnTXISFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout)  
3879 {  
3880   uint32_t tickstart = HAL_GetTick();
3881   
3882   while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == RESET)
3883   {
3884     /* Check if a NACK is detected */
3885     if(I2C_IsAcknowledgeFailed(hi2c, Timeout) != HAL_OK)
3886     {
3887       return HAL_ERROR;
3888     }
3889                 
3890     /* Check for the Timeout */
3891     if(Timeout != HAL_MAX_DELAY)
3892     {
3893       if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
3894       {
3895         hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3896         hi2c->State= HAL_I2C_STATE_READY;
3897
3898         /* Process Unlocked */
3899         __HAL_UNLOCK(hi2c);
3900
3901         return HAL_TIMEOUT;
3902       }
3903     }
3904   }
3905   return HAL_OK;      
3906 }
3907
3908 /**
3909   * @brief  This function handles I2C Communication Timeout for specific usage of STOP flag.
3910   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
3911   *                the configuration information for the specified I2C.
3912   * @param  Timeout: Timeout duration
3913   * @retval HAL status
3914   */
3915 static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout)
3916 {  
3917   uint32_t tickstart = 0x00;
3918   tickstart = HAL_GetTick();
3919   
3920   while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET)
3921   {
3922     /* Check if a NACK is detected */
3923     if(I2C_IsAcknowledgeFailed(hi2c, Timeout) != HAL_OK)
3924     {
3925       return HAL_ERROR;
3926     }
3927                 
3928     /* Check for the Timeout */
3929     if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
3930     {
3931       hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3932       hi2c->State= HAL_I2C_STATE_READY;
3933
3934       /* Process Unlocked */
3935       __HAL_UNLOCK(hi2c);
3936
3937       return HAL_TIMEOUT;
3938     }
3939   }
3940   return HAL_OK;
3941 }
3942
3943 /**
3944   * @brief  This function handles I2C Communication Timeout for specific usage of RXNE flag.
3945   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
3946   *                the configuration information for the specified I2C.
3947   * @param  Timeout: Timeout duration
3948   * @retval HAL status
3949   */
3950 static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout)
3951 {  
3952   uint32_t tickstart = 0x00;
3953   tickstart = HAL_GetTick();
3954   
3955   while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == RESET)
3956   {
3957     /* Check if a STOPF is detected */
3958     if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
3959     {
3960       /* Clear STOP Flag */
3961       __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3962
3963       /* Clear Configuration Register 2 */
3964       __I2C_RESET_CR2(hi2c);
3965
3966       hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
3967       hi2c->State= HAL_I2C_STATE_READY;
3968
3969       /* Process Unlocked */
3970       __HAL_UNLOCK(hi2c);
3971
3972       return HAL_ERROR;
3973     }
3974                 
3975     /* Check for the Timeout */
3976     if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
3977     {
3978       hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3979       hi2c->State= HAL_I2C_STATE_READY;
3980
3981       /* Process Unlocked */
3982       __HAL_UNLOCK(hi2c);
3983
3984       return HAL_TIMEOUT;
3985     }
3986   }
3987   return HAL_OK;
3988 }
3989
3990 /**
3991   * @brief  This function handles Acknowledge failed detection during an I2C Communication.
3992   * @param  hi2c : Pointer to a I2C_HandleTypeDef structure that contains
3993   *                the configuration information for the specified I2C.
3994   * @param  Timeout: Timeout duration
3995   * @retval HAL status
3996   */
3997 static HAL_StatusTypeDef I2C_IsAcknowledgeFailed(I2C_HandleTypeDef *hi2c, uint32_t Timeout)
3998 {
3999   uint32_t tickstart = 0x00;
4000   tickstart = HAL_GetTick();
4001
4002   if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
4003   {
4004     /* Generate stop if necessary only in case of I2C peripheral in MASTER mode */
4005     if((hi2c->State == HAL_I2C_STATE_MASTER_BUSY_TX) || (hi2c->State == HAL_I2C_STATE_MEM_BUSY_TX)
4006        || (hi2c->State == HAL_I2C_STATE_MEM_BUSY_RX))
4007     {
4008       /* No need to generate the STOP condition if AUTOEND mode is enabled */
4009       /* Generate the STOP condition only in case of SOFTEND mode is enabled */
4010       if((hi2c->Instance->CR2 & I2C_AUTOEND_MODE) != I2C_AUTOEND_MODE)
4011       {
4012         /* Generate Stop */
4013         hi2c->Instance->CR2 |= I2C_CR2_STOP;
4014       }
4015     }
4016                 
4017     /* Wait until STOP Flag is reset */
4018     /* AutoEnd should be initiate after AF */
4019     while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET)
4020     {
4021       /* Check for the Timeout */
4022       if(Timeout != HAL_MAX_DELAY)
4023       {
4024       if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
4025         {
4026           hi2c->State= HAL_I2C_STATE_READY;
4027           /* Process Unlocked */
4028           __HAL_UNLOCK(hi2c);
4029           return HAL_TIMEOUT;
4030         }
4031       }
4032     }
4033
4034     /* Clear NACKF Flag */
4035     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
4036
4037     /* Clear STOP Flag */
4038     __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
4039
4040     /* Clear Configuration Register 2 */
4041     __I2C_RESET_CR2(hi2c);
4042
4043     hi2c->ErrorCode = HAL_I2C_ERROR_AF;
4044     hi2c->State= HAL_I2C_STATE_READY;
4045
4046     /* Process Unlocked */
4047     __HAL_UNLOCK(hi2c);
4048
4049     return HAL_ERROR;
4050   }
4051   return HAL_OK;
4052 }
4053
4054 /**
4055   * @brief  Handles I2Cx communication when starting transfer or during transfer (TC or TCR flag are set).
4056   * @param  hi2c: I2C handle.
4057   * @param  DevAddress: specifies the slave address to be programmed.
4058   * @param  Size: specifies the number of bytes to be programmed.
4059   *   This parameter must be a value between 0 and 255.
4060   * @param  Mode: new state of the I2C START condition generation.
4061   *   This parameter can be one of the following values:
4062   *     @arg I2C_RELOAD_MODE: Enable Reload mode .
4063   *     @arg I2C_AUTOEND_MODE: Enable Automatic end mode.
4064   *     @arg I2C_SOFTEND_MODE: Enable Software end mode.
4065   * @param  Request: new state of the I2C START condition generation.
4066   *   This parameter can be one of the following values:
4067   *     @arg I2C_NO_STARTSTOP: Don't Generate stop and start condition.
4068   *     @arg I2C_GENERATE_STOP: Generate stop condition (Size should be set to 0).
4069   *     @arg I2C_GENERATE_START_READ: Generate Restart for read request.
4070   *     @arg I2C_GENERATE_START_WRITE: Generate Restart for write request.
4071   * @retval None
4072   */
4073 static void I2C_TransferConfig(I2C_HandleTypeDef *hi2c,  uint16_t DevAddress, uint8_t Size, uint32_t Mode, uint32_t Request)
4074 {
4075   uint32_t tmpreg = 0;
4076   
4077   /* Check the parameters */
4078   assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
4079   assert_param(IS_TRANSFER_MODE(Mode));
4080   assert_param(IS_TRANSFER_REQUEST(Request));
4081     
4082   /* Get the CR2 register value */
4083   tmpreg = hi2c->Instance->CR2;
4084   
4085   /* clear tmpreg specific bits */
4086   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));
4087   
4088   /* update tmpreg */
4089   tmpreg |= (uint32_t)(((uint32_t)DevAddress & I2C_CR2_SADD) | (((uint32_t)Size << 16 ) & I2C_CR2_NBYTES) | \
4090             (uint32_t)Mode | (uint32_t)Request);
4091   
4092   /* update CR2 register */
4093   hi2c->Instance->CR2 = tmpreg;  
4094 }  
4095
4096 /**
4097   * @}
4098   */
4099
4100 #endif /* HAL_I2C_MODULE_ENABLED */
4101 /**
4102   * @}
4103   */
4104
4105 /**
4106   * @}
4107   */
4108
4109 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
4110