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