2 ******************************************************************************
3 * @file stm32f1xx_hal_i2c.c
4 * @author MCD Application Team
6 * @date 15-December-2014
7 * @brief I2C HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the Inter Integrated Circuit (I2C) peripheral:
10 * + Initialization and de-initialization functions
11 * + IO operation functions
12 * + Peripheral Control functions
13 * + Peripheral State functions
16 ==============================================================================
17 ##### How to use this driver #####
18 ==============================================================================
20 The I2C HAL driver can be used as follows:
22 (#) Declare a I2C_HandleTypeDef handle structure, for example:
23 I2C_HandleTypeDef hi2c;
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
40 the DMA Tx or Rx channel
42 (#) Configure the Communication Speed, Duty cycle, Addressing mode, Own Address1,
43 Dual Addressing mode, Own Address2, General call and Nostretch mode in the hi2c Init structure.
45 (#) Initialize the I2C registers by calling the HAL_I2C_Init(), configures also the low level Hardware
46 (GPIO, CLOCK, NVIC...etc) by calling the customed HAL_I2C_MspInit(&hi2c) API.
48 (#) To check if target device is ready for communication, use the function HAL_I2C_IsDeviceReady()
50 (#) For I2C IO and IO MEM operations, three operation modes are available within this driver :
52 *** Polling mode IO operation ***
53 =================================
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()
60 *** Polling mode IO MEM operation ***
61 =====================================
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()
67 *** Interrupt mode IO operation ***
68 ===================================
70 (+) The I2C interrupts should have the highest priority in the application in order
71 to make them uninterruptible.
72 (+) Transmit in master mode an amount of data in non blocking mode using HAL_I2C_Master_Transmit_IT()
73 (+) At transmission end of transfer HAL_I2C_MasterTxCpltCallback is executed and user can
74 add his own code by customization of function pointer HAL_I2C_MasterTxCpltCallback
75 (+) Receive in master mode an amount of data in non blocking mode using HAL_I2C_Master_Receive_IT()
76 (+) At reception end of transfer HAL_I2C_MasterRxCpltCallback is executed and user can
77 add his own code by customization of function pointer HAL_I2C_MasterRxCpltCallback
78 (+) Transmit in slave mode an amount of data in non blocking mode using HAL_I2C_Slave_Transmit_IT()
79 (+) At transmission end of transfer HAL_I2C_SlaveTxCpltCallback is executed and user can
80 add his own code by customization of function pointer HAL_I2C_SlaveTxCpltCallback
81 (+) Receive in slave mode an amount of data in non blocking mode using HAL_I2C_Slave_Receive_IT()
82 (+) At reception end of transfer HAL_I2C_SlaveRxCpltCallback is executed and user can
83 add his own code by customization of function pointer HAL_I2C_SlaveRxCpltCallback
84 (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
85 add his own code by customization of function pointer HAL_I2C_ErrorCallback
87 *** Interrupt mode IO MEM operation ***
88 =======================================
90 (+) The I2C interrupts should have the highest priority in the application in order
91 to make them uninterruptible.
92 (+) Write an amount of data in no-blocking mode with Interrupt to a specific memory address using
93 HAL_I2C_Mem_Write_IT()
94 (+) At MEM end of write transfer HAL_I2C_MemTxCpltCallback is executed and user can
95 add his own code by customization of function pointer HAL_I2C_MemTxCpltCallback
96 (+) Read an amount of data in no-blocking mode with Interrupt from a specific memory address using
98 (+) At MEM end of read transfer HAL_I2C_MemRxCpltCallback is executed and user can
99 add his own code by customization of function pointer HAL_I2C_MemRxCpltCallback
100 (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
101 add his own code by customization of function pointer HAL_I2C_ErrorCallback
103 *** DMA mode IO operation ***
104 ==============================
106 (+) Transmit in master mode an amount of data in non blocking mode (DMA) using
107 HAL_I2C_Master_Transmit_DMA()
108 (+) At transmission end of transfer HAL_I2C_MasterTxCpltCallback is executed and user can
109 add his own code by customization of function pointer HAL_I2C_MasterTxCpltCallback
110 (+) Receive in master mode an amount of data in non blocking mode (DMA) using
111 HAL_I2C_Master_Receive_DMA()
112 (+) At reception end of transfer HAL_I2C_MasterRxCpltCallback is executed and user can
113 add his own code by customization of function pointer HAL_I2C_MasterRxCpltCallback
114 (+) Transmit in slave mode an amount of data in non blocking mode (DMA) using
115 HAL_I2C_Slave_Transmit_DMA()
116 (+) At transmission end of transfer HAL_I2C_SlaveTxCpltCallback is executed and user can
117 add his own code by customization of function pointer HAL_I2C_SlaveTxCpltCallback
118 (+) Receive in slave mode an amount of data in non blocking mode (DMA) using
119 HAL_I2C_Slave_Receive_DMA()
120 (+) At reception end of transfer HAL_I2C_SlaveRxCpltCallback is executed and user can
121 add his own code by customization of function pointer HAL_I2C_SlaveRxCpltCallback
122 (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
123 add his own code by customization of function pointer HAL_I2C_ErrorCallback
125 *** DMA mode IO MEM operation ***
126 =================================
128 (+) Write an amount of data in no-blocking mode with DMA to a specific memory address using
129 HAL_I2C_Mem_Write_DMA()
130 (+) At MEM end of write transfer HAL_I2C_MemTxCpltCallback is executed and user can
131 add his own code by customization of function pointer HAL_I2C_MemTxCpltCallback
132 (+) Read an amount of data in no-blocking mode with DMA from a specific memory address using
133 HAL_I2C_Mem_Read_DMA()
134 (+) At MEM end of read transfer HAL_I2C_MemRxCpltCallback is executed and user can
135 add his own code by customization of function pointer HAL_I2C_MemRxCpltCallback
136 (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
137 add his own code by customization of function pointer HAL_I2C_ErrorCallback
140 *** I2C HAL driver macros list ***
141 ==================================
143 Below the list of most used macros in I2C HAL driver.
145 (+) __HAL_I2C_ENABLE: Enable the I2C peripheral
146 (+) __HAL_I2C_DISABLE: Disable the I2C peripheral
147 (+) __HAL_I2C_GET_FLAG : Checks whether the specified I2C flag is set or not
148 (+) __HAL_I2C_CLEAR_FLAG : Clear the specified I2C pending flag
149 (+) __HAL_I2C_ENABLE_IT: Enable the specified I2C interrupt
150 (+) __HAL_I2C_DISABLE_IT: Disable the specified I2C interrupt
151 (@) You can refer to the I2C HAL driver header file for more useful macros
154 *** I2C Workarounds linked to Silicon Limitation ***
155 ====================================================
157 Below the list of all silicon limitations implemented for HAL on STM32F1xx product.
158 (@) See ErrataSheet to know full silicon limitation list of your product.
160 (#) Workarounds Implemented inside I2C HAL Driver
161 (##) Wrong data read into data register (Polling and Interrupt mode)
162 (##) Start cannot be generated after a misplaced Stop
163 (##) Some software events must be managed before the current byte is being transferred:
164 Workaround: Use DMA in general, except when the Master is receiving a single byte.
165 For Interupt mode, I2C should have the highest priority in the application.
166 (##) Mismatch on the "Setup time for a repeated Start condition" timing parameter:
167 Workaround: Reduce the frequency down to 88 kHz or use the I2C Fast-mode if
168 supported by the slave.
169 (##) Data valid time (tVD;DAT) violated without the OVR flag being set:
170 Workaround: If the slave device allows it, use the clock stretching mechanism
171 by programming NoStretchMode = I2C_NOSTRETCH_DISABLE in HAL_I2C_Init.
174 ******************************************************************************
177 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
179 * Redistribution and use in source and binary forms, with or without modification,
180 * are permitted provided that the following conditions are met:
181 * 1. Redistributions of source code must retain the above copyright notice,
182 * this list of conditions and the following disclaimer.
183 * 2. Redistributions in binary form must reproduce the above copyright notice,
184 * this list of conditions and the following disclaimer in the documentation
185 * and/or other materials provided with the distribution.
186 * 3. Neither the name of STMicroelectronics nor the names of its contributors
187 * may be used to endorse or promote products derived from this software
188 * without specific prior written permission.
190 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
191 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
192 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
193 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
194 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
195 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
196 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
197 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
198 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
199 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
201 ******************************************************************************
204 /* Includes ------------------------------------------------------------------*/
205 #include "stm32f1xx_hal.h"
207 /** @addtogroup STM32F1xx_HAL_Driver
211 /** @defgroup I2C I2C
212 * @brief I2C HAL module driver
216 #ifdef HAL_I2C_MODULE_ENABLED
218 /* Private typedef -----------------------------------------------------------*/
219 /* Private constants ---------------------------------------------------------*/
220 /** @addtogroup I2C_Private_Constants I2C Private Constants
223 #define I2C_TIMEOUT_FLAG ((uint32_t)35) /* 35 ms */
224 #define I2C_TIMEOUT_ADDR_SLAVE ((uint32_t)10000) /* 10 s */
229 /* Private macro -------------------------------------------------------------*/
230 /* Private variables ---------------------------------------------------------*/
231 /* Private function prototypes -----------------------------------------------*/
232 /** @addtogroup I2C_Private_Functions I2C Private Functions
235 static void I2C_DMAMasterTransmitCplt(DMA_HandleTypeDef *hdma);
236 static void I2C_DMAMasterReceiveCplt(DMA_HandleTypeDef *hdma);
237 static void I2C_DMASlaveTransmitCplt(DMA_HandleTypeDef *hdma);
238 static void I2C_DMASlaveReceiveCplt(DMA_HandleTypeDef *hdma);
239 static void I2C_DMAMemTransmitCplt(DMA_HandleTypeDef *hdma);
240 static void I2C_DMAMemReceiveCplt(DMA_HandleTypeDef *hdma);
241 static void I2C_DMAError(DMA_HandleTypeDef *hdma);
243 static HAL_StatusTypeDef I2C_MasterRequestWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Timeout);
244 static HAL_StatusTypeDef I2C_MasterRequestRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Timeout);
245 static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout);
246 static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout);
247 static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
248 static HAL_StatusTypeDef I2C_WaitOnMasterAddressFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, uint32_t Timeout);
250 static HAL_StatusTypeDef I2C_MasterTransmit_TXE(I2C_HandleTypeDef *hi2c);
251 static HAL_StatusTypeDef I2C_MasterTransmit_BTF(I2C_HandleTypeDef *hi2c);
252 static HAL_StatusTypeDef I2C_MasterReceive_RXNE(I2C_HandleTypeDef *hi2c);
253 static HAL_StatusTypeDef I2C_MasterReceive_BTF(I2C_HandleTypeDef *hi2c);
255 static HAL_StatusTypeDef I2C_SlaveTransmit_TXE(I2C_HandleTypeDef *hi2c);
256 static HAL_StatusTypeDef I2C_SlaveTransmit_BTF(I2C_HandleTypeDef *hi2c);
257 static HAL_StatusTypeDef I2C_SlaveReceive_RXNE(I2C_HandleTypeDef *hi2c);
258 static HAL_StatusTypeDef I2C_SlaveReceive_BTF(I2C_HandleTypeDef *hi2c);
259 static HAL_StatusTypeDef I2C_Slave_ADDR(I2C_HandleTypeDef *hi2c);
260 static HAL_StatusTypeDef I2C_Slave_STOPF(I2C_HandleTypeDef *hi2c);
261 static HAL_StatusTypeDef I2C_Slave_AF(I2C_HandleTypeDef *hi2c);
263 static uint32_t I2C_Configure_Speed(I2C_HandleTypeDef *hi2c, uint32_t I2CClkSrcFreq);
268 /* Exported functions ---------------------------------------------------------*/
270 /** @defgroup I2C_Exported_Functions I2C Exported Functions
274 /** @defgroup I2C_Exported_Functions_Group1 Initialization and de-initialization functions
275 * @brief Initialization and Configuration functions
278 ===============================================================================
279 ##### Initialization and de-initialization functions #####
280 ===============================================================================
281 [..] This subsection provides a set of functions allowing to initialize and
282 de-initialiaze the I2Cx peripheral:
284 (+) User must Implement HAL_I2C_MspInit() function in which he configures
285 all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC).
287 (+) Call the function HAL_I2C_Init() to configure the selected device with
288 the selected configuration:
289 (++) Communication Speed
293 (++) Dual Addressing mode
295 (++) General call mode
298 (+) Call the function HAL_I2C_DeInit() to restore the default configuration
299 of the selected I2Cx periperal.
306 * @brief Initializes the I2C according to the specified parameters
307 * in the I2C_InitTypeDef and create the associated handle.
308 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
309 * the configuration information for the specified I2C.
312 HAL_StatusTypeDef HAL_I2C_Init(I2C_HandleTypeDef *hi2c)
314 uint32_t freqrange = 0;
317 /* Check the I2C handle allocation */
323 /* Check the parameters */
324 assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
325 assert_param(IS_I2C_CLOCK_SPEED(hi2c->Init.ClockSpeed));
326 assert_param(IS_I2C_DUTY_CYCLE(hi2c->Init.DutyCycle));
327 assert_param(IS_I2C_OWN_ADDRESS1(hi2c->Init.OwnAddress1));
328 assert_param(IS_I2C_ADDRESSING_MODE(hi2c->Init.AddressingMode));
329 assert_param(IS_I2C_DUAL_ADDRESS(hi2c->Init.DualAddressMode));
330 assert_param(IS_I2C_OWN_ADDRESS2(hi2c->Init.OwnAddress2));
331 assert_param(IS_I2C_GENERAL_CALL(hi2c->Init.GeneralCallMode));
332 assert_param(IS_I2C_NO_STRETCH(hi2c->Init.NoStretchMode));
334 if(hi2c->State == HAL_I2C_STATE_RESET)
336 /* Allocate lock resource and initialize it */
337 hi2c-> Lock = HAL_UNLOCKED;
339 /* Init the low level hardware : GPIO, CLOCK, NVIC */
340 HAL_I2C_MspInit(hi2c);
343 hi2c->State = HAL_I2C_STATE_BUSY;
345 /* Disable the selected I2C peripheral */
346 __HAL_I2C_DISABLE(hi2c);
348 /* Get PCLK1 frequency */
349 pclk1 = HAL_RCC_GetPCLK1Freq();
351 /* Calculate frequency range */
352 freqrange = I2C_FREQ_RANGE(pclk1);
354 /*---------------------------- I2Cx CR2 Configuration ----------------------*/
355 /* Configure I2Cx: Frequency range */
356 hi2c->Instance->CR2 = freqrange;
358 /*---------------------------- I2Cx TRISE Configuration --------------------*/
359 /* Configure I2Cx: Rise Time */
360 hi2c->Instance->TRISE = I2C_RISE_TIME(freqrange, hi2c->Init.ClockSpeed);
362 /*---------------------------- I2Cx CCR Configuration ----------------------*/
363 /* Configure I2Cx: Speed */
364 hi2c->Instance->CCR = I2C_Configure_Speed(hi2c, pclk1);
366 /*---------------------------- I2Cx CR1 Configuration ----------------------*/
367 /* Configure I2Cx: Generalcall and NoStretch mode */
368 hi2c->Instance->CR1 = (hi2c->Init.GeneralCallMode | hi2c->Init.NoStretchMode);
370 /*---------------------------- I2Cx OAR1 Configuration ---------------------*/
371 /* Configure I2Cx: Own Address1 and addressing mode */
372 hi2c->Instance->OAR1 = (hi2c->Init.AddressingMode | hi2c->Init.OwnAddress1);
374 /*---------------------------- I2Cx OAR2 Configuration ---------------------*/
375 /* Configure I2Cx: Dual mode and Own Address2 */
376 hi2c->Instance->OAR2 = (hi2c->Init.DualAddressMode | hi2c->Init.OwnAddress2);
378 /* Enable the selected I2C peripheral */
379 __HAL_I2C_ENABLE(hi2c);
381 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
382 hi2c->State = HAL_I2C_STATE_READY;
388 * @brief DeInitializes the I2C peripheral.
389 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
390 * the configuration information for the specified I2C.
393 HAL_StatusTypeDef HAL_I2C_DeInit(I2C_HandleTypeDef *hi2c)
395 /* Check the I2C handle allocation */
401 /* Check the parameters */
402 assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
404 hi2c->State = HAL_I2C_STATE_BUSY;
406 /* Disable the I2C Peripheral Clock */
407 __HAL_I2C_DISABLE(hi2c);
409 /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
410 HAL_I2C_MspDeInit(hi2c);
412 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
414 hi2c->State = HAL_I2C_STATE_RESET;
423 * @brief I2C MSP Init.
424 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
425 * the configuration information for the specified I2C.
428 __weak void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)
430 /* NOTE : This function Should not be modified, when the callback is needed,
431 the HAL_I2C_MspInit could be implemented in the user file
436 * @brief I2C MSP DeInit
437 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
438 * the configuration information for the specified I2C.
441 __weak void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c)
443 /* NOTE : This function Should not be modified, when the callback is needed,
444 the HAL_I2C_MspDeInit could be implemented in the user file
452 /** @defgroup I2C_Exported_Functions_Group2 Input and Output operation functions
453 * @brief Data transfers functions
456 ===============================================================================
457 ##### IO operation functions #####
458 ===============================================================================
460 This subsection provides a set of functions allowing to manage the I2C data
463 (#) There are two modes of transfer:
464 (++) Blocking mode : The communication is performed in the polling mode.
465 The status of all data processing is returned by the same function
466 after finishing transfer.
467 (++) No-Blocking mode : The communication is performed using Interrupts
468 or DMA. These functions return the status of the transfer startup.
469 The end of the data processing will be indicated through the
470 dedicated I2C IRQ when using Interrupt mode or the DMA IRQ when
473 (#) Blocking mode functions are :
474 (++) HAL_I2C_Master_Transmit()
475 (++) HAL_I2C_Master_Receive()
476 (++) HAL_I2C_Slave_Transmit()
477 (++) HAL_I2C_Slave_Receive()
478 (++) HAL_I2C_Mem_Write()
479 (++) HAL_I2C_Mem_Read()
480 (++) HAL_I2C_IsDeviceReady()
482 (#) No-Blocking mode functions with Interrupt are :
483 (++) HAL_I2C_Master_Transmit_IT()
484 (++) HAL_I2C_Master_Receive_IT()
485 (++) HAL_I2C_Slave_Transmit_IT()
486 (++) HAL_I2C_Slave_Receive_IT()
487 (++) HAL_I2C_Mem_Write_IT()
488 (++) HAL_I2C_Mem_Read_IT()
490 (#) No-Blocking mode functions with DMA are :
491 (++) HAL_I2C_Master_Transmit_DMA()
492 (++) HAL_I2C_Master_Receive_DMA()
493 (++) HAL_I2C_Slave_Transmit_DMA()
494 (++) HAL_I2C_Slave_Receive_DMA()
495 (++) HAL_I2C_Mem_Write_DMA()
496 (++) HAL_I2C_Mem_Read_DMA()
498 (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
499 (++) HAL_I2C_MemTxCpltCallback()
500 (++) HAL_I2C_MemRxCpltCallback()
501 (++) HAL_I2C_MasterTxCpltCallback()
502 (++) HAL_I2C_MasterRxCpltCallback()
503 (++) HAL_I2C_SlaveTxCpltCallback()
504 (++) HAL_I2C_SlaveRxCpltCallback()
505 (++) HAL_I2C_ErrorCallback()
512 * @brief Transmits in master mode an amount of data in blocking mode.
513 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
514 * the configuration information for the specified I2C.
515 * @param DevAddress: Target device address
516 * @param pData: Pointer to data buffer
517 * @param Size: Amount of data to be sent
518 * @param Timeout: Timeout duration
521 HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
523 if(hi2c->State == HAL_I2C_STATE_READY)
525 if((pData == NULL) || (Size == 0))
530 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
538 hi2c->State = HAL_I2C_STATE_BUSY_TX;
539 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
541 /* Send Slave Address */
542 if(I2C_MasterRequestWrite(hi2c, DevAddress, Timeout) != HAL_OK)
544 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
546 /* Process Unlocked */
552 /* Process Unlocked */
558 /* Clear ADDR flag */
559 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
563 /* Wait until TXE flag is set */
564 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TXE, RESET, Timeout) != HAL_OK)
569 /* Write data to DR */
570 hi2c->Instance->DR = (*pData++);
573 if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (Size != 0))
575 /* Write data to DR */
576 hi2c->Instance->DR = (*pData++);
581 /* Wait until TXE flag is set */
582 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout) != HAL_OK)
588 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
590 /* Wait until BUSY flag is reset */
591 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout) != HAL_OK)
596 hi2c->State = HAL_I2C_STATE_READY;
598 /* Process Unlocked */
610 * @brief Receives in master mode an amount of data in blocking mode.
611 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
612 * the configuration information for the specified I2C.
613 * @param DevAddress: Target device address
614 * @param pData: Pointer to data buffer
615 * @param Size: Amount of data to be sent
616 * @param Timeout: Timeout duration
619 HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
621 if(hi2c->State == HAL_I2C_STATE_READY)
623 if((pData == NULL) || (Size == 0))
628 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
636 hi2c->State = HAL_I2C_STATE_BUSY_RX;
637 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
639 /* Send Slave Address */
640 if(I2C_MasterRequestRead(hi2c, DevAddress, Timeout) != HAL_OK)
642 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
644 /* Process Unlocked */
650 /* Process Unlocked */
658 /* Disable Acknowledge */
659 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
661 /* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3
662 software sequence must complete before the current byte end of transfer */
665 /* Clear ADDR flag */
666 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
669 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
677 SET_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
679 /* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3
680 software sequence must complete before the current byte end of transfer */
683 /* Clear ADDR flag */
684 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
686 /* Disable Acknowledge */
687 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
694 /* Enable Acknowledge */
695 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
697 /* Clear ADDR flag */
698 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
708 /* Wait until RXNE flag is set */
709 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, Timeout) != HAL_OK)
714 /* Read data from DR */
715 (*pData++) = hi2c->Instance->DR;
721 /* Wait until BTF flag is set */
722 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout) != HAL_OK)
727 /* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3
728 software sequence must complete before the current byte end of transfer */
732 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
734 /* Read data from DR */
735 (*pData++) = hi2c->Instance->DR;
741 /* Read data from DR */
742 (*pData++) = hi2c->Instance->DR;
748 /* Wait until BTF flag is set */
749 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout) != HAL_OK)
754 /* Disable Acknowledge */
755 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
757 /* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3
758 software sequence must complete before the current byte end of transfer */
761 /* Read data from DR */
762 (*pData++) = hi2c->Instance->DR;
765 /* Wait until BTF flag is set */
766 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout) != HAL_OK)
772 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
774 /* Read data from DR */
775 (*pData++) = hi2c->Instance->DR;
781 /* Wait until RXNE flag is set */
782 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, Timeout) != HAL_OK)
787 /* Read data from DR */
788 (*pData++) = hi2c->Instance->DR;
794 /* Wait until RXNE flag is set */
795 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, Timeout) != HAL_OK)
800 /* Read data from DR */
801 (*pData++) = hi2c->Instance->DR;
804 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET)
806 /* Read data from DR */
807 (*pData++) = hi2c->Instance->DR;
814 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
816 /* Wait until BUSY flag is reset */
817 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout) != HAL_OK)
822 hi2c->State = HAL_I2C_STATE_READY;
824 /* Process Unlocked */
836 * @brief Transmits in slave mode an amount of data in blocking mode.
837 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
838 * the configuration information for the specified I2C.
839 * @param pData: Pointer to data buffer
840 * @param Size: Amount of data to be sent
841 * @param Timeout: Timeout duration
844 HAL_StatusTypeDef HAL_I2C_Slave_Transmit(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
846 if(hi2c->State == HAL_I2C_STATE_READY)
848 if((pData == NULL) || (Size == 0))
853 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
861 hi2c->State = HAL_I2C_STATE_BUSY_TX;
862 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
864 /* Enable Address Acknowledge */
865 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
867 /* Wait until ADDR flag is set */
868 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout) != HAL_OK)
873 /* Clear ADDR flag */
874 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
876 /* If 10bit addressing mode is selected */
877 if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT)
879 /* Wait until ADDR flag is set */
880 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout) != HAL_OK)
885 /* Clear ADDR flag */
886 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
891 /* Wait until TXE flag is set */
892 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TXE, RESET, Timeout) != HAL_OK)
897 /* Write data to DR */
898 hi2c->Instance->DR = (*pData++);
901 if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (Size != 0))
903 /* Write data to DR */
904 hi2c->Instance->DR = (*pData++);
909 /* Wait until AF flag is set */
910 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_AF, RESET, Timeout) != HAL_OK)
916 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
918 /* Disable Address Acknowledge */
919 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
921 /* Wait until BUSY flag is reset */
922 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout) != HAL_OK)
927 hi2c->State = HAL_I2C_STATE_READY;
929 /* Process Unlocked */
941 * @brief Receive in slave mode an amount of data in blocking mode
942 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
943 * the configuration information for the specified I2C.
944 * @param pData: Pointer to data buffer
945 * @param Size: Amount of data to be sent
946 * @param Timeout: Timeout duration
949 HAL_StatusTypeDef HAL_I2C_Slave_Receive(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
951 if(hi2c->State == HAL_I2C_STATE_READY)
953 if((pData == NULL) || (Size == 0))
958 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
966 hi2c->State = HAL_I2C_STATE_BUSY_RX;
967 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
969 /* Enable Address Acknowledge */
970 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
972 /* Wait until ADDR flag is set */
973 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout) != HAL_OK)
978 /* Clear ADDR flag */
979 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
983 /* Wait until RXNE flag is set */
984 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, Timeout) != HAL_OK)
989 /* Read data from DR */
990 (*pData++) = hi2c->Instance->DR;
993 if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (Size != 0))
995 /* Read data from DR */
996 (*pData++) = hi2c->Instance->DR;
1001 /* Wait until STOP flag is set */
1002 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout) != HAL_OK)
1007 /* Clear STOP flag */
1008 __HAL_I2C_CLEAR_STOPFLAG(hi2c);
1010 /* Disable Address Acknowledge */
1011 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1013 /* Wait until BUSY flag is reset */
1014 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout) != HAL_OK)
1019 hi2c->State = HAL_I2C_STATE_READY;
1021 /* Process Unlocked */
1034 * @brief Transmit in master mode an amount of data in no-blocking mode with Interrupt
1035 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1036 * the configuration information for the specified I2C.
1037 * @param DevAddress: Target device address
1038 * @param pData: Pointer to data buffer
1039 * @param Size: Amount of data to be sent
1040 * @retval HAL status
1042 HAL_StatusTypeDef HAL_I2C_Master_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
1044 if(hi2c->State == HAL_I2C_STATE_READY)
1046 if((pData == NULL) || (Size == 0))
1051 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1056 /* Process Locked */
1059 hi2c->State = HAL_I2C_STATE_BUSY_TX;
1060 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1062 hi2c->pBuffPtr = pData;
1063 hi2c->XferSize = Size;
1064 hi2c->XferCount = Size;
1066 /* Send Slave Address */
1067 if(I2C_MasterRequestWrite(hi2c, DevAddress, I2C_TIMEOUT_FLAG) != HAL_OK)
1069 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1071 /* Process Unlocked */
1077 /* Process Unlocked */
1083 /* Clear ADDR flag */
1084 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
1086 /* Process Unlocked */
1089 /* Note : The I2C interrupts must be enabled after unlocking current process
1090 to avoid the risk of I2C interrupt handle execution before current
1093 /* Enable EVT, BUF and ERR interrupt */
1094 __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
1105 * @brief Receive in master mode an amount of data in no-blocking mode with Interrupt
1106 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1107 * the configuration information for the specified I2C.
1108 * @param DevAddress: Target device address
1109 * @param pData: Pointer to data buffer
1110 * @param Size: Amount of data to be sent
1111 * @retval HAL status
1113 HAL_StatusTypeDef HAL_I2C_Master_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
1115 if(hi2c->State == HAL_I2C_STATE_READY)
1117 if((pData == NULL) || (Size == 0))
1122 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1127 /* Process Locked */
1130 hi2c->State = HAL_I2C_STATE_BUSY_RX;
1131 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1133 hi2c->pBuffPtr = pData;
1134 hi2c->XferSize = Size;
1135 hi2c->XferCount = Size;
1137 /* Send Slave Address */
1138 if(I2C_MasterRequestRead(hi2c, DevAddress, I2C_TIMEOUT_FLAG) != HAL_OK)
1140 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1142 /* Process Unlocked */
1148 /* Process Unlocked */
1154 if(hi2c->XferCount == 1)
1156 /* Disable Acknowledge */
1157 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1159 /* Clear ADDR flag */
1160 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
1163 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
1165 else if(hi2c->XferCount == 2)
1167 /* Disable Acknowledge */
1168 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1171 SET_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
1173 /* Clear ADDR flag */
1174 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
1178 /* Enable Acknowledge */
1179 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1181 /* Clear ADDR flag */
1182 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
1185 /* Process Unlocked */
1188 /* Note : The I2C interrupts must be enabled after unlocking current process
1189 to avoid the risk of I2C interrupt handle execution before current
1192 /* Enable EVT, BUF and ERR interrupt */
1193 __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
1204 * @brief Transmit in slave mode an amount of data in no-blocking mode with Interrupt
1205 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1206 * the configuration information for the specified I2C.
1207 * @param pData: Pointer to data buffer
1208 * @param Size: Amount of data to be sent
1209 * @retval HAL status
1211 HAL_StatusTypeDef HAL_I2C_Slave_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
1213 if(hi2c->State == HAL_I2C_STATE_READY)
1215 if((pData == NULL) || (Size == 0))
1220 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1225 /* Process Locked */
1228 hi2c->State = HAL_I2C_STATE_BUSY_TX;
1229 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1231 hi2c->pBuffPtr = pData;
1232 hi2c->XferSize = Size;
1233 hi2c->XferCount = Size;
1235 /* Enable Address Acknowledge */
1236 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1238 /* Process Unlocked */
1241 /* Note : The I2C interrupts must be enabled after unlocking current process
1242 to avoid the risk of I2C interrupt handle execution before current
1245 /* Enable EVT, BUF and ERR interrupt */
1246 __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
1257 * @brief Receive in slave mode an amount of data in no-blocking mode with Interrupt
1258 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1259 * the configuration information for the specified I2C.
1260 * @param pData: Pointer to data buffer
1261 * @param Size: Amount of data to be sent
1262 * @retval HAL status
1264 HAL_StatusTypeDef HAL_I2C_Slave_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
1266 if(hi2c->State == HAL_I2C_STATE_READY)
1268 if((pData == NULL) || (Size == 0))
1273 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1278 /* Process Locked */
1281 hi2c->State = HAL_I2C_STATE_BUSY_RX;
1282 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1284 hi2c->pBuffPtr = pData;
1285 hi2c->XferSize = Size;
1286 hi2c->XferCount = Size;
1288 /* Enable Address Acknowledge */
1289 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1291 /* Process Unlocked */
1294 /* Note : The I2C interrupts must be enabled after unlocking current process
1295 to avoid the risk of I2C interrupt handle execution before current
1298 /* Enable EVT, BUF and ERR interrupt */
1299 __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
1311 * @brief Transmit in master mode an amount of data in no-blocking mode with DMA
1312 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1313 * the configuration information for the specified I2C.
1314 * @param DevAddress: Target device address
1315 * @param pData: Pointer to data buffer
1316 * @param Size: Amount of data to be sent
1317 * @retval HAL status
1319 HAL_StatusTypeDef HAL_I2C_Master_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
1321 if(hi2c->State == HAL_I2C_STATE_READY)
1323 if((pData == NULL) || (Size == 0))
1328 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1333 /* Process Locked */
1336 hi2c->State = HAL_I2C_STATE_BUSY_TX;
1337 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1339 hi2c->pBuffPtr = pData;
1340 hi2c->XferSize = Size;
1341 hi2c->XferCount = Size;
1343 /* Set the I2C DMA transfert complete callback */
1344 hi2c->hdmatx->XferCpltCallback = I2C_DMAMasterTransmitCplt;
1346 /* Set the DMA error callback */
1347 hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
1349 /* Enable the DMA channel */
1350 HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->DR, Size);
1352 /* Send Slave Address */
1353 if(I2C_MasterRequestWrite(hi2c, DevAddress, I2C_TIMEOUT_FLAG) != HAL_OK)
1355 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1357 /* Process Unlocked */
1363 /* Process Unlocked */
1369 /* Enable DMA Request */
1370 SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
1372 /* Clear ADDR flag */
1373 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
1375 /* Process Unlocked */
1387 * @brief Receive in master mode an amount of data in no-blocking mode with DMA
1388 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1389 * the configuration information for the specified I2C.
1390 * @param DevAddress: Target device address
1391 * @param pData: Pointer to data buffer
1392 * @param Size: Amount of data to be sent
1393 * @retval HAL status
1395 HAL_StatusTypeDef HAL_I2C_Master_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
1397 if(hi2c->State == HAL_I2C_STATE_READY)
1399 if((pData == NULL) || (Size == 0))
1404 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1409 /* Process Locked */
1412 hi2c->State = HAL_I2C_STATE_BUSY_RX;
1413 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1415 hi2c->pBuffPtr = pData;
1416 hi2c->XferSize = Size;
1417 hi2c->XferCount = Size;
1419 /* Set the I2C DMA transfert complete callback */
1420 hi2c->hdmarx->XferCpltCallback = I2C_DMAMasterReceiveCplt;
1422 /* Set the DMA error callback */
1423 hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
1425 /* Enable the DMA channel */
1426 HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)pData, Size);
1428 /* Send Slave Address */
1429 if(I2C_MasterRequestRead(hi2c, DevAddress, I2C_TIMEOUT_FLAG) != HAL_OK)
1431 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1433 /* Process Unlocked */
1439 /* Process Unlocked */
1447 /* Disable Acknowledge */
1448 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1452 /* Enable Last DMA bit */
1453 SET_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
1456 /* Enable DMA Request */
1457 SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
1459 /* Clear ADDR flag */
1460 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
1462 /* Process Unlocked */
1474 * @brief Transmit in slave mode an amount of data in no-blocking mode with DMA
1475 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1476 * the configuration information for the specified I2C.
1477 * @param pData: Pointer to data buffer
1478 * @param Size: Amount of data to be sent
1479 * @retval HAL status
1481 HAL_StatusTypeDef HAL_I2C_Slave_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
1483 if(hi2c->State == HAL_I2C_STATE_READY)
1485 if((pData == NULL) || (Size == 0))
1490 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1495 /* Process Locked */
1498 hi2c->State = HAL_I2C_STATE_BUSY_TX;
1499 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1501 hi2c->pBuffPtr = pData;
1502 hi2c->XferSize = Size;
1503 hi2c->XferCount = Size;
1505 /* Set the I2C DMA transfert complete callback */
1506 hi2c->hdmatx->XferCpltCallback = I2C_DMASlaveTransmitCplt;
1508 /* Set the DMA error callback */
1509 hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
1511 /* Enable the DMA channel */
1512 HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->DR, Size);
1514 /* Enable DMA Request */
1515 SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
1517 /* Enable Address Acknowledge */
1518 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1520 /* Wait until ADDR flag is set */
1521 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, I2C_TIMEOUT_ADDR_SLAVE) != HAL_OK)
1526 /* If 7bit addressing mode is selected */
1527 if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_7BIT)
1529 /* Clear ADDR flag */
1530 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
1534 /* Clear ADDR flag */
1535 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
1537 /* Wait until ADDR flag is set */
1538 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, I2C_TIMEOUT_ADDR_SLAVE) != HAL_OK)
1543 /* Clear ADDR flag */
1544 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
1547 /* Process Unlocked */
1559 * @brief Receive in slave mode an amount of data in no-blocking mode with DMA
1560 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1561 * the configuration information for the specified I2C.
1562 * @param pData: Pointer to data buffer
1563 * @param Size: Amount of data to be sent
1564 * @retval HAL status
1566 HAL_StatusTypeDef HAL_I2C_Slave_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
1568 if(hi2c->State == HAL_I2C_STATE_READY)
1570 if((pData == NULL) || (Size == 0))
1575 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1580 /* Process Locked */
1583 hi2c->State = HAL_I2C_STATE_BUSY_RX;
1584 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1586 hi2c->pBuffPtr = pData;
1587 hi2c->XferSize = Size;
1588 hi2c->XferCount = Size;
1590 /* Set the I2C DMA transfert complete callback */
1591 hi2c->hdmarx->XferCpltCallback = I2C_DMASlaveReceiveCplt;
1593 /* Set the DMA error callback */
1594 hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
1596 /* Enable the DMA channel */
1597 HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)pData, Size);
1599 /* Enable DMA Request */
1600 SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
1602 /* Enable Address Acknowledge */
1603 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1605 /* Wait until ADDR flag is set */
1606 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, I2C_TIMEOUT_ADDR_SLAVE) != HAL_OK)
1611 /* Clear ADDR flag */
1612 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
1614 /* Process Unlocked */
1627 * @brief Write an amount of data in blocking mode to a specific memory address
1628 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1629 * the configuration information for the specified I2C.
1630 * @param DevAddress: Target device address
1631 * @param MemAddress: Internal memory address
1632 * @param MemAddSize: Size of internal memory address
1633 * @param pData: Pointer to data buffer
1634 * @param Size: Amount of data to be sent
1635 * @param Timeout: Timeout duration
1636 * @retval HAL status
1638 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)
1640 /* Check the parameters */
1641 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
1643 if(hi2c->State == HAL_I2C_STATE_READY)
1645 if((pData == NULL) || (Size == 0))
1650 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1655 /* Process Locked */
1658 hi2c->State = HAL_I2C_STATE_MEM_BUSY_TX;
1659 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1661 /* Send Slave Address and Memory Address */
1662 if(I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, Timeout) != HAL_OK)
1664 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1666 /* Process Unlocked */
1672 /* Process Unlocked */
1680 /* Wait until TXE flag is set */
1681 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TXE, RESET, Timeout) != HAL_OK)
1686 /* Write data to DR */
1687 hi2c->Instance->DR = (*pData++);
1690 if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (Size != 0))
1692 /* Write data to DR */
1693 hi2c->Instance->DR = (*pData++);
1698 /* Wait until TXE flag is set */
1699 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TXE, RESET, Timeout) != HAL_OK)
1705 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
1707 /* Wait until BUSY flag is reset */
1708 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout) != HAL_OK)
1713 hi2c->State = HAL_I2C_STATE_READY;
1715 /* Process Unlocked */
1727 * @brief Read an amount of data in blocking mode from a specific memory address
1728 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1729 * the configuration information for the specified I2C.
1730 * @param DevAddress: Target device address
1731 * @param MemAddress: Internal memory address
1732 * @param MemAddSize: Size of internal memory address
1733 * @param pData: Pointer to data buffer
1734 * @param Size: Amount of data to be sent
1735 * @param Timeout: Timeout duration
1736 * @retval HAL status
1738 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)
1740 /* Check the parameters */
1741 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
1743 if(hi2c->State == HAL_I2C_STATE_READY)
1745 if((pData == NULL) || (Size == 0))
1750 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1755 /* Process Locked */
1758 hi2c->State = HAL_I2C_STATE_MEM_BUSY_RX;
1759 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1761 /* Send Slave Address and Memory Address */
1762 if(I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, Timeout) != HAL_OK)
1764 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1766 /* Process Unlocked */
1772 /* Process Unlocked */
1780 /* Disable Acknowledge */
1781 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1783 /* Clear ADDR flag */
1784 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
1787 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
1791 /* Disable Acknowledge */
1792 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1795 SET_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
1797 /* Clear ADDR flag */
1798 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
1802 /* Clear ADDR flag */
1803 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
1813 /* Wait until RXNE flag is set */
1814 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, Timeout) != HAL_OK)
1819 /* Read data from DR */
1820 (*pData++) = hi2c->Instance->DR;
1826 /* Wait until BTF flag is set */
1827 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout) != HAL_OK)
1833 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
1835 /* Read data from DR */
1836 (*pData++) = hi2c->Instance->DR;
1839 /* Read data from DR */
1840 (*pData++) = hi2c->Instance->DR;
1846 /* Wait until BTF flag is set */
1847 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout) != HAL_OK)
1852 /* Disable Acknowledge */
1853 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1855 /* Read data from DR */
1856 (*pData++) = hi2c->Instance->DR;
1859 /* Wait until BTF flag is set */
1860 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout) != HAL_OK)
1866 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
1868 /* Read data from DR */
1869 (*pData++) = hi2c->Instance->DR;
1872 /* Read data from DR */
1873 (*pData++) = hi2c->Instance->DR;
1879 /* Wait until RXNE flag is set */
1880 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, Timeout) != HAL_OK)
1885 /* Read data from DR */
1886 (*pData++) = hi2c->Instance->DR;
1889 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET)
1891 /* Read data from DR */
1892 (*pData++) = hi2c->Instance->DR;
1899 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
1901 /* Wait until BUSY flag is reset */
1902 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout) != HAL_OK)
1907 hi2c->State = HAL_I2C_STATE_READY;
1909 /* Process Unlocked */
1922 * @brief Write an amount of data in no-blocking mode with Interrupt to a specific memory address
1923 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1924 * the configuration information for the specified I2C.
1925 * @param DevAddress: Target device address
1926 * @param MemAddress: Internal memory address
1927 * @param MemAddSize: Size of internal memory address
1928 * @param pData: Pointer to data buffer
1929 * @param Size: Amount of data to be sent
1930 * @retval HAL status
1932 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)
1934 /* Check the parameters */
1935 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
1937 if(hi2c->State == HAL_I2C_STATE_READY)
1939 if((pData == NULL) || (Size == 0))
1944 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1949 /* Process Locked */
1952 hi2c->State = HAL_I2C_STATE_MEM_BUSY_TX;
1953 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1955 hi2c->pBuffPtr = pData;
1956 hi2c->XferSize = Size;
1957 hi2c->XferCount = Size;
1959 /* Send Slave Address and Memory Address */
1960 if(I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG) != HAL_OK)
1962 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1964 /* Process Unlocked */
1970 /* Process Unlocked */
1976 /* Process Unlocked */
1979 /* Note : The I2C interrupts must be enabled after unlocking current process
1980 to avoid the risk of I2C interrupt handle execution before current
1983 /* Enable EVT, BUF and ERR interrupt */
1984 __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
1995 * @brief Read an amount of data in no-blocking mode with Interrupt from a specific memory address
1996 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1997 * the configuration information for the specified I2C.
1998 * @param DevAddress: Target device address
1999 * @param MemAddress: Internal memory address
2000 * @param MemAddSize: Size of internal memory address
2001 * @param pData: Pointer to data buffer
2002 * @param Size: Amount of data to be sent
2003 * @retval HAL status
2005 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)
2007 /* Check the parameters */
2008 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
2010 if(hi2c->State == HAL_I2C_STATE_READY)
2012 if((pData == NULL) || (Size == 0))
2017 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
2022 /* Process Locked */
2025 hi2c->State = HAL_I2C_STATE_MEM_BUSY_RX;
2026 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
2028 hi2c->pBuffPtr = pData;
2029 hi2c->XferSize = Size;
2030 hi2c->XferCount = Size;
2032 /* Send Slave Address and Memory Address */
2033 if(I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG) != HAL_OK)
2035 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
2037 /* Process Unlocked */
2043 /* Process Unlocked */
2049 if(hi2c->XferCount == 1)
2051 /* Disable Acknowledge */
2052 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
2054 /* Clear ADDR flag */
2055 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
2058 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
2060 else if(hi2c->XferCount == 2)
2062 /* Disable Acknowledge */
2063 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
2066 SET_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
2068 /* Clear ADDR flag */
2069 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
2073 /* Enable Acknowledge */
2074 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
2076 /* Clear ADDR flag */
2077 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
2080 /* Process Unlocked */
2083 /* Note : The I2C interrupts must be enabled after unlocking current process
2084 to avoid the risk of I2C interrupt handle execution before current
2087 /* Enable EVT, BUF and ERR interrupt */
2088 __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
2100 * @brief Write an amount of data in no-blocking mode with DMA to a specific memory address
2101 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2102 * the configuration information for the specified I2C.
2103 * @param DevAddress: Target device address
2104 * @param MemAddress: Internal memory address
2105 * @param MemAddSize: Size of internal memory address
2106 * @param pData: Pointer to data buffer
2107 * @param Size: Amount of data to be sent
2108 * @retval HAL status
2110 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)
2112 /* Check the parameters */
2113 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
2115 if(hi2c->State == HAL_I2C_STATE_READY)
2117 if((pData == NULL) || (Size == 0))
2122 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
2127 /* Process Locked */
2130 hi2c->State = HAL_I2C_STATE_MEM_BUSY_TX;
2131 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
2133 hi2c->pBuffPtr = pData;
2134 hi2c->XferSize = Size;
2135 hi2c->XferCount = Size;
2137 /* Set the I2C DMA transfert complete callback */
2138 hi2c->hdmatx->XferCpltCallback = I2C_DMAMemTransmitCplt;
2140 /* Set the DMA error callback */
2141 hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
2143 /* Enable the DMA channel */
2144 HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->DR, Size);
2146 /* Send Slave Address and Memory Address */
2147 if(I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG) != HAL_OK)
2149 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
2151 /* Process Unlocked */
2157 /* Process Unlocked */
2163 /* Enable DMA Request */
2164 SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
2166 /* Process Unlocked */
2178 * @brief Reads an amount of data in no-blocking mode with DMA from a specific memory address.
2179 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2180 * the configuration information for the specified I2C.
2181 * @param DevAddress: Target device address
2182 * @param MemAddress: Internal memory address
2183 * @param MemAddSize: Size of internal memory address
2184 * @param pData: Pointer to data buffer
2185 * @param Size: Amount of data to be read
2186 * @retval HAL status
2188 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)
2190 /* Check the parameters */
2191 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
2193 if(hi2c->State == HAL_I2C_STATE_READY)
2195 if((pData == NULL) || (Size == 0))
2200 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
2205 /* Process Locked */
2208 hi2c->State = HAL_I2C_STATE_MEM_BUSY_RX;
2209 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
2211 hi2c->pBuffPtr = pData;
2212 hi2c->XferSize = Size;
2213 hi2c->XferCount = Size;
2215 /* Set the I2C DMA transfert complete callback */
2216 hi2c->hdmarx->XferCpltCallback = I2C_DMAMemReceiveCplt;
2218 /* Set the DMA error callback */
2219 hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
2221 /* Enable the DMA channel */
2222 HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)pData, Size);
2224 /* Send Slave Address and Memory Address */
2225 if(I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG) != HAL_OK)
2227 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
2229 /* Process Unlocked */
2235 /* Process Unlocked */
2243 /* Disable Acknowledge */
2244 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
2248 /* Enable Last DMA bit */
2249 SET_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
2252 /* Enable DMA Request */
2253 SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
2255 /* Clear ADDR flag */
2256 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
2258 /* Process Unlocked */
2271 * @brief Checks if target device is ready for communication.
2272 * @note This function is used with Memory devices
2273 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2274 * the configuration information for the specified I2C.
2275 * @param DevAddress: Target device address
2276 * @param Trials: Number of trials
2277 * @param Timeout: Timeout duration
2278 * @retval HAL status
2280 HAL_StatusTypeDef HAL_I2C_IsDeviceReady(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout)
2282 uint32_t tickstart = 0, tmp1 = 0, tmp2 = 0, tmp3 = 0, I2C_Trials = 1;
2284 if(hi2c->State == HAL_I2C_STATE_READY)
2286 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
2291 /* Process Locked */
2294 hi2c->State = HAL_I2C_STATE_BUSY;
2295 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
2299 /* Generate Start */
2300 SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
2302 /* Wait until SB flag is set */
2303 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout) != HAL_OK)
2308 /* Send slave address */
2309 hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress);
2311 /* Wait until ADDR or AF flag are set */
2313 tickstart = HAL_GetTick();
2315 tmp1 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR);
2316 tmp2 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF);
2318 while((tmp1 == RESET) && (tmp2 == RESET) && (tmp3 != HAL_I2C_STATE_TIMEOUT))
2320 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
2322 hi2c->State = HAL_I2C_STATE_TIMEOUT;
2324 tmp1 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR);
2325 tmp2 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF);
2329 hi2c->State = HAL_I2C_STATE_READY;
2331 /* Check if the ADDR flag has been set */
2332 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == SET)
2335 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
2337 /* Clear ADDR Flag */
2338 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
2340 /* Wait until BUSY flag is reset */
2341 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout) != HAL_OK)
2346 hi2c->State = HAL_I2C_STATE_READY;
2348 /* Process Unlocked */
2356 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
2359 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
2361 /* Wait until BUSY flag is reset */
2362 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout) != HAL_OK)
2367 }while(I2C_Trials++ < Trials);
2369 hi2c->State = HAL_I2C_STATE_READY;
2371 /* Process Unlocked */
2385 /** @defgroup IRQ_Handler_and_Callbacks IRQ Handler and Callbacks
2390 * @brief This function handles I2C event interrupt request.
2391 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2392 * the configuration information for the specified I2C.
2395 void HAL_I2C_EV_IRQHandler(I2C_HandleTypeDef *hi2c)
2397 uint32_t tmp1 = 0, tmp2 = 0, tmp3 = 0, tmp4 = 0;
2398 /* Master mode selected */
2399 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_MSL) == SET)
2401 /* I2C in mode Transmitter -----------------------------------------------*/
2402 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TRA) == SET)
2404 tmp1 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXE);
2405 tmp2 = __HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_BUF);
2406 tmp3 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF);
2407 tmp4 = __HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_EVT);
2408 /* TXE set and BTF reset -----------------------------------------------*/
2409 if((tmp1 == SET) && (tmp2 == SET) && (tmp3 == RESET))
2411 I2C_MasterTransmit_TXE(hi2c);
2413 /* BTF set -------------------------------------------------------------*/
2414 else if((tmp3 == SET) && (tmp4 == SET))
2416 I2C_MasterTransmit_BTF(hi2c);
2419 /* I2C in mode Receiver --------------------------------------------------*/
2422 tmp1 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE);
2423 tmp2 = __HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_BUF);
2424 tmp3 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF);
2425 tmp4 = __HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_EVT);
2426 /* RXNE set and BTF reset -----------------------------------------------*/
2427 if((tmp1 == SET) && (tmp2 == SET) && (tmp3 == RESET))
2429 I2C_MasterReceive_RXNE(hi2c);
2431 /* BTF set -------------------------------------------------------------*/
2432 else if((tmp3 == SET) && (tmp4 == SET))
2434 I2C_MasterReceive_BTF(hi2c);
2438 /* Slave mode selected */
2441 tmp1 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR);
2442 tmp2 = __HAL_I2C_GET_IT_SOURCE(hi2c, (I2C_IT_EVT));
2443 tmp3 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF);
2444 tmp4 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TRA);
2445 /* ADDR set --------------------------------------------------------------*/
2446 if((tmp1 == SET) && (tmp2 == SET))
2448 I2C_Slave_ADDR(hi2c);
2450 /* STOPF set --------------------------------------------------------------*/
2451 else if((tmp3 == SET) && (tmp2 == SET))
2453 I2C_Slave_STOPF(hi2c);
2455 /* I2C in mode Transmitter -----------------------------------------------*/
2456 else if(tmp4 == SET)
2458 tmp1 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXE);
2459 tmp2 = __HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_BUF);
2460 tmp3 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF);
2461 tmp4 = __HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_EVT);
2462 /* TXE set and BTF reset -----------------------------------------------*/
2463 if((tmp1 == SET) && (tmp2 == SET) && (tmp3 == RESET))
2465 I2C_SlaveTransmit_TXE(hi2c);
2467 /* BTF set -------------------------------------------------------------*/
2468 else if((tmp3 == SET) && (tmp4 == SET))
2470 I2C_SlaveTransmit_BTF(hi2c);
2473 /* I2C in mode Receiver --------------------------------------------------*/
2476 tmp1 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE);
2477 tmp2 = __HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_BUF);
2478 tmp3 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF);
2479 tmp4 = __HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_EVT);
2480 /* RXNE set and BTF reset ----------------------------------------------*/
2481 if((tmp1 == SET) && (tmp2 == SET) && (tmp3 == RESET))
2483 I2C_SlaveReceive_RXNE(hi2c);
2485 /* BTF set -------------------------------------------------------------*/
2486 else if((tmp3 == SET) && (tmp4 == SET))
2488 I2C_SlaveReceive_BTF(hi2c);
2495 * @brief This function handles I2C error interrupt request.
2496 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
2497 * the configuration information for I2C module
2498 * @retval HAL status
2500 void HAL_I2C_ER_IRQHandler(I2C_HandleTypeDef *hi2c)
2502 uint32_t tmp1 = 0, tmp2 = 0, tmp3 = 0;
2504 tmp1 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BERR);
2505 tmp2 = __HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_ERR);
2506 /* I2C Bus error interrupt occurred ----------------------------------------*/
2507 if((tmp1 == SET) && (tmp2 == SET))
2509 hi2c->ErrorCode |= HAL_I2C_ERROR_BERR;
2511 /* Clear BERR flag */
2512 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_BERR);
2514 /* Workaround: Start cannot be generated after a misplaced Stop */
2515 SET_BIT(hi2c->Instance->CR1, I2C_CR1_SWRST);
2518 tmp1 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ARLO);
2519 tmp2 = __HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_ERR);
2520 /* I2C Arbitration Loss error interrupt occurred ---------------------------*/
2521 if((tmp1 == SET) && (tmp2 == SET))
2523 hi2c->ErrorCode |= HAL_I2C_ERROR_ARLO;
2525 /* Clear ARLO flag */
2526 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ARLO);
2529 tmp1 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF);
2530 tmp2 = __HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_ERR);
2531 /* I2C Acknowledge failure error interrupt occurred ------------------------*/
2532 if((tmp1 == SET) && (tmp2 == SET))
2534 tmp1 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_MSL);
2535 tmp2 = hi2c->XferCount;
2537 if((tmp1 == RESET) && (tmp2 == 0) && (tmp3 == HAL_I2C_STATE_BUSY_TX))
2543 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
2545 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
2549 tmp1 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_OVR);
2550 tmp2 = __HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_ERR);
2551 /* I2C Over-Run/Under-Run interrupt occurred -------------------------------*/
2552 if((tmp1 == SET) && (tmp2 == SET))
2554 hi2c->ErrorCode |= HAL_I2C_ERROR_OVR;
2555 /* Clear OVR flag */
2556 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_OVR);
2559 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
2561 hi2c->State = HAL_I2C_STATE_READY;
2563 /* Disable Pos bit in I2C CR1 when error occured in Master/Mem Receive IT Process */
2564 hi2c->Instance->CR1 &= ~I2C_CR1_POS;
2566 HAL_I2C_ErrorCallback(hi2c);
2571 * @brief Master Tx Transfer completed callbacks.
2572 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2573 * the configuration information for the specified I2C.
2576 __weak void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c)
2578 /* NOTE : This function Should not be modified, when the callback is needed,
2579 the HAL_I2C_TxCpltCallback could be implemented in the user file
2584 * @brief Master Rx Transfer completed callbacks.
2585 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2586 * the configuration information for the specified I2C.
2589 __weak void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c)
2591 /* NOTE : This function Should not be modified, when the callback is needed,
2592 the HAL_I2C_TxCpltCallback could be implemented in the user file
2596 /** @brief Slave Tx Transfer completed callbacks.
2597 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2598 * the configuration information for the specified I2C.
2601 __weak void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c)
2603 /* NOTE : This function Should not be modified, when the callback is needed,
2604 the HAL_I2C_TxCpltCallback could be implemented in the user file
2609 * @brief Slave Rx Transfer completed callbacks.
2610 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2611 * the configuration information for the specified I2C.
2614 __weak void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c)
2616 /* NOTE : This function Should not be modified, when the callback is needed,
2617 the HAL_I2C_TxCpltCallback could be implemented in the user file
2622 * @brief Memory Tx Transfer completed callbacks.
2623 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2624 * the configuration information for the specified I2C.
2627 __weak void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef *hi2c)
2629 /* NOTE : This function Should not be modified, when the callback is needed,
2630 the HAL_I2C_TxCpltCallback could be implemented in the user file
2635 * @brief Memory Rx Transfer completed callbacks.
2636 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2637 * the configuration information for the specified I2C.
2640 __weak void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c)
2642 /* NOTE : This function Should not be modified, when the callback is needed,
2643 the HAL_I2C_TxCpltCallback could be implemented in the user file
2648 * @brief I2C error callbacks.
2649 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2650 * the configuration information for the specified I2C.
2653 __weak void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
2655 /* NOTE : This function Should not be modified, when the callback is needed,
2656 the HAL_I2C_ErrorCallback could be implemented in the user file
2665 /** @defgroup I2C_Exported_Functions_Group3 Peripheral State and Errors functions
2666 * @brief Peripheral State and Errors functions
2669 ===============================================================================
2670 ##### Peripheral State and Errors functions #####
2671 ===============================================================================
2673 This subsection permits to get in run-time the status of the peripheral
2681 * @brief Returns the I2C state.
2682 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2683 * the configuration information for the specified I2C.
2686 HAL_I2C_StateTypeDef HAL_I2C_GetState(I2C_HandleTypeDef *hi2c)
2692 * @brief Return the I2C error code
2693 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2694 * the configuration information for the specified I2C.
2695 * @retval I2C Error Code
2697 uint32_t HAL_I2C_GetError(I2C_HandleTypeDef *hi2c)
2699 return hi2c->ErrorCode;
2710 /** @addtogroup I2C_Private_Functions
2715 * @brief Handle TXE flag for Master Transmit Mode
2716 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2717 * the configuration information for the specified I2C.
2718 * @retval HAL status
2720 static HAL_StatusTypeDef I2C_MasterTransmit_TXE(I2C_HandleTypeDef *hi2c)
2722 /* Write data to DR */
2723 hi2c->Instance->DR = (*hi2c->pBuffPtr++);
2726 if(hi2c->XferCount == 0)
2728 /* Disable BUF interrupt */
2729 __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF);
2736 * @brief Handle BTF flag for Master Transmit Mode
2737 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2738 * the configuration information for the specified I2C.
2739 * @retval HAL status
2741 static HAL_StatusTypeDef I2C_MasterTransmit_BTF(I2C_HandleTypeDef *hi2c)
2743 if(hi2c->XferCount != 0)
2745 /* Write data to DR */
2746 hi2c->Instance->DR = (*hi2c->pBuffPtr++);
2751 /* Disable EVT, BUF and ERR interrupt */
2752 __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
2755 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
2757 /* Wait until BUSY flag is reset */
2758 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_FLAG) != HAL_OK)
2763 if(hi2c->State == HAL_I2C_STATE_MEM_BUSY_TX)
2765 hi2c->State = HAL_I2C_STATE_READY;
2767 HAL_I2C_MemTxCpltCallback(hi2c);
2771 hi2c->State = HAL_I2C_STATE_READY;
2773 HAL_I2C_MasterTxCpltCallback(hi2c);
2780 * @brief Handle RXNE flag for Master Receive Mode
2781 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2782 * the configuration information for the specified I2C.
2783 * @retval HAL status
2785 static HAL_StatusTypeDef I2C_MasterReceive_RXNE(I2C_HandleTypeDef *hi2c)
2789 tmp = hi2c->XferCount;
2792 /* Read data from DR */
2793 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
2796 else if((tmp == 2) || (tmp == 3))
2798 /* Disable BUF interrupt */
2799 __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF);
2803 /* Disable EVT, BUF and ERR interrupt */
2804 __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
2806 /* Read data from DR */
2807 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
2810 /* Wait until BUSY flag is reset */
2811 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_FLAG) != HAL_OK)
2817 hi2c->Instance->CR1 &= ~I2C_CR1_POS;
2819 if(hi2c->State == HAL_I2C_STATE_MEM_BUSY_RX)
2821 hi2c->State = HAL_I2C_STATE_READY;
2823 HAL_I2C_MemRxCpltCallback(hi2c);
2827 hi2c->State = HAL_I2C_STATE_READY;
2829 HAL_I2C_MasterRxCpltCallback(hi2c);
2836 * @brief Handle BTF flag for Master Receive Mode
2837 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2838 * the configuration information for the specified I2C.
2839 * @retval HAL status
2841 static HAL_StatusTypeDef I2C_MasterReceive_BTF(I2C_HandleTypeDef *hi2c)
2843 if(hi2c->XferCount == 3)
2845 /* Disable Acknowledge */
2846 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
2848 /* Read data from DR */
2849 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
2852 else if(hi2c->XferCount == 2)
2855 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
2857 /* Disable EVT and ERR interrupt */
2858 /* Workaround - Wong data read into data register */
2859 __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
2861 /* Read data from DR */
2862 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
2865 /* Read data from DR */
2866 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
2869 /* Wait until BUSY flag is reset */
2870 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_FLAG) != HAL_OK)
2876 hi2c->Instance->CR1 &= ~I2C_CR1_POS;
2878 if(hi2c->State == HAL_I2C_STATE_MEM_BUSY_RX)
2880 hi2c->State = HAL_I2C_STATE_READY;
2882 HAL_I2C_MemRxCpltCallback(hi2c);
2886 hi2c->State = HAL_I2C_STATE_READY;
2888 HAL_I2C_MasterRxCpltCallback(hi2c);
2893 /* Read data from DR */
2894 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
2901 * @brief Handle TXE flag for Slave Transmit Mode
2902 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2903 * the configuration information for the specified I2C.
2904 * @retval HAL status
2906 static HAL_StatusTypeDef I2C_SlaveTransmit_TXE(I2C_HandleTypeDef *hi2c)
2908 if(hi2c->XferCount != 0)
2910 /* Write data to DR */
2911 hi2c->Instance->DR = (*hi2c->pBuffPtr++);
2918 * @brief Handle BTF flag for Slave Transmit Mode
2919 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2920 * the configuration information for the specified I2C.
2921 * @retval HAL status
2923 static HAL_StatusTypeDef I2C_SlaveTransmit_BTF(I2C_HandleTypeDef *hi2c)
2925 if(hi2c->XferCount != 0)
2927 /* Write data to DR */
2928 hi2c->Instance->DR = (*hi2c->pBuffPtr++);
2935 * @brief Handle RXNE flag for Slave Receive Mode
2936 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2937 * the configuration information for the specified I2C.
2938 * @retval HAL status
2940 static HAL_StatusTypeDef I2C_SlaveReceive_RXNE(I2C_HandleTypeDef *hi2c)
2942 if(hi2c->XferCount != 0)
2944 /* Read data from DR */
2945 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
2952 * @brief Handle BTF flag for Slave Receive Mode
2953 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2954 * the configuration information for the specified I2C.
2955 * @retval HAL status
2957 static HAL_StatusTypeDef I2C_SlaveReceive_BTF(I2C_HandleTypeDef *hi2c)
2959 if(hi2c->XferCount != 0)
2961 /* Read data from DR */
2962 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
2969 * @brief Handle ADD flag for Slave
2970 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2971 * the configuration information for the specified I2C.
2972 * @retval HAL status
2974 static HAL_StatusTypeDef I2C_Slave_ADDR(I2C_HandleTypeDef *hi2c)
2976 /* Clear ADDR flag */
2977 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
2983 * @brief Handle STOPF flag for Slave Mode
2984 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2985 * the configuration information for the specified I2C.
2986 * @retval HAL status
2988 static HAL_StatusTypeDef I2C_Slave_STOPF(I2C_HandleTypeDef *hi2c)
2990 /* Disable EVT, BUF and ERR interrupt */
2991 __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
2993 /* Clear STOPF flag */
2994 __HAL_I2C_CLEAR_STOPFLAG(hi2c);
2996 /* Disable Acknowledge */
2997 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
2999 /* Wait until BUSY flag is reset */
3000 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_FLAG) != HAL_OK)
3005 hi2c->State = HAL_I2C_STATE_READY;
3007 HAL_I2C_SlaveRxCpltCallback(hi2c);
3013 * @brief Handle Acknowledge Failed for Slave Mode
3014 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
3015 * the configuration information for the specified I2C.
3016 * @retval HAL status
3018 static HAL_StatusTypeDef I2C_Slave_AF(I2C_HandleTypeDef *hi2c)
3020 /* Disable EVT, BUF and ERR interrupt */
3021 __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
3024 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
3026 /* Disable Acknowledge */
3027 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
3029 /* Wait until BUSY flag is reset */
3030 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_FLAG) != HAL_OK)
3035 hi2c->State = HAL_I2C_STATE_READY;
3037 HAL_I2C_SlaveTxCpltCallback(hi2c);
3043 * @brief Master sends target device address followed by internal memory address for write request.
3044 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
3045 * the configuration information for the specified I2C.
3046 * @param DevAddress: Target device address
3047 * @param Timeout: Timeout duration
3048 * @retval HAL status
3050 static HAL_StatusTypeDef I2C_MasterRequestWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Timeout)
3052 /* Generate Start */
3053 SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
3055 /* Wait until SB flag is set */
3056 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout) != HAL_OK)
3061 if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_7BIT)
3063 /* Send slave address */
3064 hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress);
3068 /* Send header of slave address */
3069 hi2c->Instance->DR = I2C_10BIT_HEADER_WRITE(DevAddress);
3071 /* Wait until ADD10 flag is set */
3072 if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADD10, Timeout) != HAL_OK)
3074 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3084 /* Send slave address */
3085 hi2c->Instance->DR = I2C_10BIT_ADDRESS(DevAddress);
3088 /* Wait until ADDR flag is set */
3089 if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout) != HAL_OK)
3091 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3105 * @brief Master sends target device address followed by internal memory address for read request.
3106 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
3107 * the configuration information for the specified I2C.
3108 * @param DevAddress: Target device address
3109 * @param Timeout: Timeout duration
3110 * @retval HAL status
3112 static HAL_StatusTypeDef I2C_MasterRequestRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Timeout)
3114 /* Enable Acknowledge */
3115 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
3117 /* Generate Start */
3118 SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
3120 /* Wait until SB flag is set */
3121 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout) != HAL_OK)
3126 if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_7BIT)
3128 /* Send slave address */
3129 hi2c->Instance->DR = I2C_7BIT_ADD_READ(DevAddress);
3133 /* Send header of slave address */
3134 hi2c->Instance->DR = I2C_10BIT_HEADER_WRITE(DevAddress);
3136 /* Wait until ADD10 flag is set */
3137 if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADD10, Timeout) != HAL_OK)
3139 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3149 /* Send slave address */
3150 hi2c->Instance->DR = I2C_10BIT_ADDRESS(DevAddress);
3152 /* Wait until ADDR flag is set */
3153 if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout) != HAL_OK)
3155 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3165 /* Clear ADDR flag */
3166 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
3168 /* Generate Restart */
3169 SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
3171 /* Wait until SB flag is set */
3172 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout) != HAL_OK)
3177 /* Send header of slave address */
3178 hi2c->Instance->DR = I2C_10BIT_HEADER_READ(DevAddress);
3181 /* Wait until ADDR flag is set */
3182 if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout) != HAL_OK)
3184 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3198 * @brief Master sends target device address followed by internal memory address for write request.
3199 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
3200 * the configuration information for the specified I2C.
3201 * @param DevAddress: Target device address
3202 * @param MemAddress: Internal memory address
3203 * @param MemAddSize: Size of internal memory address
3204 * @param Timeout: Timeout duration
3205 * @retval HAL status
3207 static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout)
3209 /* Generate Start */
3210 SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
3212 /* Wait until SB flag is set */
3213 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout) != HAL_OK)
3218 /* Send slave address */
3219 hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress);
3221 /* Wait until ADDR flag is set */
3222 if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout) != HAL_OK)
3224 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3234 /* Clear ADDR flag */
3235 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
3237 /* Wait until TXE flag is set */
3238 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TXE, RESET, Timeout) != HAL_OK)
3243 /* If Memory address size is 8Bit */
3244 if(MemAddSize == I2C_MEMADD_SIZE_8BIT)
3246 /* Send Memory Address */
3247 hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
3249 /* If Memory address size is 16Bit */
3252 /* Send MSB of Memory Address */
3253 hi2c->Instance->DR = I2C_MEM_ADD_MSB(MemAddress);
3255 /* Wait until TXE flag is set */
3256 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TXE, RESET, Timeout) != HAL_OK)
3261 /* Send LSB of Memory Address */
3262 hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
3269 * @brief Master sends target device address followed by internal memory address for read request.
3270 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
3271 * the configuration information for the specified I2C.
3272 * @param DevAddress: Target device address
3273 * @param MemAddress: Internal memory address
3274 * @param MemAddSize: Size of internal memory address
3275 * @param Timeout: Timeout duration
3276 * @retval HAL status
3278 static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout)
3280 /* Enable Acknowledge */
3281 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
3283 /* Generate Start */
3284 SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
3286 /* Wait until SB flag is set */
3287 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout) != HAL_OK)
3292 /* Send slave address */
3293 hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress);
3295 /* Wait until ADDR flag is set */
3296 if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout) != HAL_OK)
3298 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3308 /* Clear ADDR flag */
3309 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
3311 /* Wait until TXE flag is set */
3312 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TXE, RESET, Timeout) != HAL_OK)
3317 /* If Memory address size is 8Bit */
3318 if(MemAddSize == I2C_MEMADD_SIZE_8BIT)
3320 /* Send Memory Address */
3321 hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
3323 /* If Memory address size is 16Bit */
3326 /* Send MSB of Memory Address */
3327 hi2c->Instance->DR = I2C_MEM_ADD_MSB(MemAddress);
3329 /* Wait until TXE flag is set */
3330 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TXE, RESET, Timeout) != HAL_OK)
3335 /* Send LSB of Memory Address */
3336 hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
3339 /* Wait until TXE flag is set */
3340 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TXE, RESET, Timeout) != HAL_OK)
3345 /* Generate Restart */
3346 SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
3348 /* Wait until SB flag is set */
3349 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout) != HAL_OK)
3354 /* Send slave address */
3355 hi2c->Instance->DR = I2C_7BIT_ADD_READ(DevAddress);
3357 /* Wait until ADDR flag is set */
3358 if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout) != HAL_OK)
3360 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3374 * @brief DMA I2C master transmit process complete callback.
3375 * @param hdma: DMA handle
3378 static void I2C_DMAMasterTransmitCplt(DMA_HandleTypeDef *hdma)
3380 I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3382 /* Wait until BTF flag is reset */
3383 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, I2C_TIMEOUT_FLAG) != HAL_OK)
3385 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3389 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
3391 /* Disable DMA Request */
3392 CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
3394 hi2c->XferCount = 0;
3396 /* Wait until BUSY flag is reset */
3397 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_FLAG) != HAL_OK)
3399 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3402 hi2c->State = HAL_I2C_STATE_READY;
3404 /* Check if Errors has been detected during transfer */
3405 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3407 HAL_I2C_ErrorCallback(hi2c);
3411 HAL_I2C_MasterTxCpltCallback(hi2c);
3416 * @brief DMA I2C slave transmit process complete callback.
3417 * @param hdma: DMA handle
3420 static void I2C_DMASlaveTransmitCplt(DMA_HandleTypeDef *hdma)
3422 I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3424 /* Wait until AF flag is reset */
3425 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_AF, RESET, I2C_TIMEOUT_FLAG) != HAL_OK)
3427 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3431 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
3433 /* Disable Address Acknowledge */
3434 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
3436 /* Disable DMA Request */
3437 CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
3439 hi2c->XferCount = 0;
3441 /* Wait until BUSY flag is reset */
3442 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_FLAG) != HAL_OK)
3444 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3447 hi2c->State = HAL_I2C_STATE_READY;
3449 /* Check if Errors has been detected during transfer */
3450 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3452 HAL_I2C_ErrorCallback(hi2c);
3456 HAL_I2C_SlaveTxCpltCallback(hi2c);
3461 * @brief DMA I2C master receive process complete callback
3462 * @param hdma: DMA handle
3465 static void I2C_DMAMasterReceiveCplt(DMA_HandleTypeDef *hdma)
3467 I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3470 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
3472 /* Disable Last DMA */
3473 CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
3475 /* Disable Acknowledge */
3476 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
3478 /* Disable DMA Request */
3479 CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
3481 hi2c->XferCount = 0;
3483 /* Wait until BUSY flag is reset */
3484 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_FLAG) != HAL_OK)
3486 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3489 hi2c->State = HAL_I2C_STATE_READY;
3491 /* Check if Errors has been detected during transfer */
3492 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3494 HAL_I2C_ErrorCallback(hi2c);
3498 HAL_I2C_MasterRxCpltCallback(hi2c);
3503 * @brief DMA I2C slave receive process complete callback.
3504 * @param hdma: DMA handle
3507 static void I2C_DMASlaveReceiveCplt(DMA_HandleTypeDef *hdma)
3509 I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3511 /* Wait until STOPF flag is reset */
3512 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, I2C_TIMEOUT_FLAG) != HAL_OK)
3514 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3517 /* Clear STOPF flag */
3518 __HAL_I2C_CLEAR_STOPFLAG(hi2c);
3520 /* Disable Address Acknowledge */
3521 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
3523 /* Disable DMA Request */
3524 CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
3526 hi2c->XferCount = 0;
3528 /* Wait until BUSY flag is reset */
3529 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_FLAG) != HAL_OK)
3531 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3534 hi2c->State = HAL_I2C_STATE_READY;
3536 /* Check if Errors has been detected during transfer */
3537 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3539 HAL_I2C_ErrorCallback(hi2c);
3543 HAL_I2C_SlaveRxCpltCallback(hi2c);
3548 * @brief DMA I2C Memory Write process complete callback
3549 * @param hdma: DMA handle
3552 static void I2C_DMAMemTransmitCplt(DMA_HandleTypeDef *hdma)
3554 I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3556 /* Wait until BTF flag is reset */
3557 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, I2C_TIMEOUT_FLAG) != HAL_OK)
3559 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3563 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
3565 /* Disable DMA Request */
3566 CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
3568 hi2c->XferCount = 0;
3570 /* Wait until BUSY flag is reset */
3571 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_FLAG) != HAL_OK)
3573 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3576 hi2c->State = HAL_I2C_STATE_READY;
3578 /* Check if Errors has been detected during transfer */
3579 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3581 HAL_I2C_ErrorCallback(hi2c);
3585 HAL_I2C_MemTxCpltCallback(hi2c);
3590 * @brief DMA I2C Memory Read process complete callback
3591 * @param hdma: DMA handle
3594 static void I2C_DMAMemReceiveCplt(DMA_HandleTypeDef *hdma)
3596 I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3599 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
3601 /* Disable Last DMA */
3602 CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
3604 /* Disable Acknowledge */
3605 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
3607 /* Disable DMA Request */
3608 CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
3610 hi2c->XferCount = 0;
3612 /* Wait until BUSY flag is reset */
3613 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_FLAG) != HAL_OK)
3615 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3618 hi2c->State = HAL_I2C_STATE_READY;
3620 /* Check if Errors has been detected during transfer */
3621 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3623 HAL_I2C_ErrorCallback(hi2c);
3627 HAL_I2C_MemRxCpltCallback(hi2c);
3632 * @brief I2C Configuration Speed function
3633 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
3634 * the configuration information for the specified I2C.
3635 * @param I2CClkSrcFreq: PCLK frequency from RCC.
3636 * @retval CCR Speed: Speed to set in I2C CCR Register
3638 static uint32_t I2C_Configure_Speed(I2C_HandleTypeDef *hi2c, uint32_t I2CClkSrcFreq)
3642 /* Clock Standard Mode */
3643 if(hi2c->Init.ClockSpeed <= I2C_STANDARD_MODE_MAX_CLK)
3645 /* Calculate Value to be set in CCR register */
3646 tmp1 = (I2CClkSrcFreq/(hi2c->Init.ClockSpeed << 1));
3648 /* The minimum allowed value set in CCR register is 0x04 for Standard Mode */
3649 if( (tmp1 & I2C_CCR_CCR) < 4 )
3660 /* Clock Fast Mode */
3663 /* Duty Cylce tLow/tHigh = 2 */
3664 if(hi2c->Init.DutyCycle == I2C_DUTYCYCLE_2)
3666 tmp1 |= (I2CClkSrcFreq/(hi2c->Init.ClockSpeed * 3)) | I2C_DUTYCYCLE_2;
3668 else /* Duty Cylce tLow/tHigh = 16/9 */
3670 tmp1 |= (I2CClkSrcFreq/(hi2c->Init.ClockSpeed * 25)) | I2C_DUTYCYCLE_16_9;
3673 /* The minimum allowed value set in CCR register is 0x01 for Fast Mode */
3674 if( (tmp1 & I2C_CCR_CCR) < 1 )
3686 * @brief DMA I2C communication error callback.
3687 * @param hdma: DMA handle
3690 static void I2C_DMAError(DMA_HandleTypeDef *hdma)
3692 I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3694 /* Disable Acknowledge */
3695 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
3697 hi2c->XferCount = 0;
3699 hi2c->State = HAL_I2C_STATE_READY;
3701 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
3703 HAL_I2C_ErrorCallback(hi2c);
3707 * @brief This function handles I2C Communication Timeout.
3708 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
3709 * the configuration information for the specified I2C.
3710 * @param Flag: specifies the I2C flag to check.
3711 * @param Status: The new Flag status (SET or RESET).
3712 * @param Timeout: Timeout duration
3713 * @retval HAL status
3715 static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
3717 uint32_t tickstart = 0;
3720 tickstart = HAL_GetTick();
3722 /* Wait until flag is set */
3725 while(__HAL_I2C_GET_FLAG(hi2c, Flag) == RESET)
3727 /* Check for the Timeout */
3728 if(Timeout != HAL_MAX_DELAY)
3730 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
3732 hi2c->State= HAL_I2C_STATE_READY;
3734 /* Process Unlocked */
3744 while(__HAL_I2C_GET_FLAG(hi2c, Flag) != RESET)
3746 /* Check for the Timeout */
3747 if(Timeout != HAL_MAX_DELAY)
3749 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
3751 hi2c->State= HAL_I2C_STATE_READY;
3753 /* Process Unlocked */
3765 * @brief This function handles I2C Communication Timeout for Master addressing phase.
3766 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
3767 * the configuration information for the specified I2C.
3768 * @param Flag: specifies the I2C flag to check.
3769 * @param Timeout: Timeout duration
3770 * @retval HAL status
3772 static HAL_StatusTypeDef I2C_WaitOnMasterAddressFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, uint32_t Timeout)
3774 uint32_t tickstart = 0;
3777 tickstart = HAL_GetTick();
3779 while(__HAL_I2C_GET_FLAG(hi2c, Flag) == RESET)
3781 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
3784 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
3787 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
3789 hi2c->ErrorCode = HAL_I2C_ERROR_AF;
3790 hi2c->State= HAL_I2C_STATE_READY;
3792 /* Process Unlocked */
3798 /* Check for the Timeout */
3799 if(Timeout != HAL_MAX_DELAY)
3801 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
3803 hi2c->State= HAL_I2C_STATE_READY;
3805 /* Process Unlocked */
3819 #endif /* HAL_I2C_MODULE_ENABLED */
3829 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/