2 ******************************************************************************
3 * @file stm32l1xx_hal_i2c.c
4 * @author MCD Application Team
6 * @date 5-September-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 (+) Transmit in master mode an amount of data in non blocking mode using HAL_I2C_Master_Transmit_IT()
71 (+) At transmission end of transfer HAL_I2C_MasterTxCpltCallback is executed and user can
72 add his own code by customization of function pointer HAL_I2C_MasterTxCpltCallback
73 (+) Receive in master mode an amount of data in non blocking mode using HAL_I2C_Master_Receive_IT()
74 (+) At reception end of transfer HAL_I2C_MasterRxCpltCallback is executed and user can
75 add his own code by customization of function pointer HAL_I2C_MasterRxCpltCallback
76 (+) Transmit in slave mode an amount of data in non blocking mode using HAL_I2C_Slave_Transmit_IT()
77 (+) At transmission end of transfer HAL_I2C_SlaveTxCpltCallback is executed and user can
78 add his own code by customization of function pointer HAL_I2C_SlaveTxCpltCallback
79 (+) Receive in slave mode an amount of data in non blocking mode using HAL_I2C_Slave_Receive_IT()
80 (+) At reception end of transfer HAL_I2C_SlaveRxCpltCallback is executed and user can
81 add his own code by customization of function pointer HAL_I2C_SlaveRxCpltCallback
82 (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
83 add his own code by customization of function pointer HAL_I2C_ErrorCallback
85 *** Interrupt mode IO MEM operation ***
86 =======================================
88 (+) Write an amount of data in no-blocking mode with Interrupt to a specific memory address using
89 HAL_I2C_Mem_Write_IT()
90 (+) At MEM end of write transfer HAL_I2C_MemTxCpltCallback is executed and user can
91 add his own code by customization of function pointer HAL_I2C_MemTxCpltCallback
92 (+) Read an amount of data in no-blocking mode with Interrupt from a specific memory address using
94 (+) At MEM end of read transfer HAL_I2C_MemRxCpltCallback is executed and user can
95 add his own code by customization of function pointer HAL_I2C_MemRxCpltCallback
96 (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
97 add his own code by customization of function pointer HAL_I2C_ErrorCallback
99 *** DMA mode IO operation ***
100 ==============================
102 (+) Transmit in master mode an amount of data in non blocking mode (DMA) using
103 HAL_I2C_Master_Transmit_DMA()
104 (+) At transmission end of transfer HAL_I2C_MasterTxCpltCallback is executed and user can
105 add his own code by customization of function pointer HAL_I2C_MasterTxCpltCallback
106 (+) Receive in master mode an amount of data in non blocking mode (DMA) using
107 HAL_I2C_Master_Receive_DMA()
108 (+) At reception end of transfer HAL_I2C_MasterRxCpltCallback is executed and user can
109 add his own code by customization of function pointer HAL_I2C_MasterRxCpltCallback
110 (+) Transmit in slave mode an amount of data in non blocking mode (DMA) using
111 HAL_I2C_Slave_Transmit_DMA()
112 (+) At transmission end of transfer HAL_I2C_SlaveTxCpltCallback is executed and user can
113 add his own code by customization of function pointer HAL_I2C_SlaveTxCpltCallback
114 (+) Receive in slave mode an amount of data in non blocking mode (DMA) using
115 HAL_I2C_Slave_Receive_DMA()
116 (+) At reception end of transfer HAL_I2C_SlaveRxCpltCallback is executed and user can
117 add his own code by customization of function pointer HAL_I2C_SlaveRxCpltCallback
118 (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
119 add his own code by customization of function pointer HAL_I2C_ErrorCallback
121 *** DMA mode IO MEM operation ***
122 =================================
124 (+) Write an amount of data in no-blocking mode with DMA to a specific memory address using
125 HAL_I2C_Mem_Write_DMA()
126 (+) At MEM end of write transfer HAL_I2C_MemTxCpltCallback is executed and user can
127 add his own code by customization of function pointer HAL_I2C_MemTxCpltCallback
128 (+) Read an amount of data in no-blocking mode with DMA from a specific memory address using
129 HAL_I2C_Mem_Read_DMA()
130 (+) At MEM end of read transfer HAL_I2C_MemRxCpltCallback is executed and user can
131 add his own code by customization of function pointer HAL_I2C_MemRxCpltCallback
132 (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
133 add his own code by customization of function pointer HAL_I2C_ErrorCallback
136 *** I2C HAL driver macros list ***
137 ==================================
139 Below the list of most used macros in I2C HAL driver.
141 (+) __HAL_I2C_ENABLE: Enable the I2C peripheral
142 (+) __HAL_I2C_DISABLE: Disable the I2C peripheral
143 (+) __HAL_I2C_GET_FLAG : Checks whether the specified I2C flag is set or not
144 (+) __HAL_I2C_CLEAR_FLAG : Clear the specified I2C pending flag
145 (+) __HAL_I2C_ENABLE_IT: Enable the specified I2C interrupt
146 (+) __HAL_I2C_DISABLE_IT: Disable the specified I2C interrupt
149 (@) You can refer to the I2C HAL driver header file for more useful macros
153 ******************************************************************************
156 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
158 * Redistribution and use in source and binary forms, with or without modification,
159 * are permitted provided that the following conditions are met:
160 * 1. Redistributions of source code must retain the above copyright notice,
161 * this list of conditions and the following disclaimer.
162 * 2. Redistributions in binary form must reproduce the above copyright notice,
163 * this list of conditions and the following disclaimer in the documentation
164 * and/or other materials provided with the distribution.
165 * 3. Neither the name of STMicroelectronics nor the names of its contributors
166 * may be used to endorse or promote products derived from this software
167 * without specific prior written permission.
169 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
170 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
171 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
172 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
173 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
174 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
175 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
176 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
177 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
178 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
180 ******************************************************************************
183 /* Includes ------------------------------------------------------------------*/
184 #include "stm32l1xx_hal.h"
186 /** @addtogroup STM32L1xx_HAL_Driver
190 /** @defgroup I2C I2C
191 * @brief I2C HAL module driver
195 #ifdef HAL_I2C_MODULE_ENABLED
197 /* Private typedef -----------------------------------------------------------*/
198 /* Private define ------------------------------------------------------------*/
199 /** @defgroup I2C_Private_Constants I2C Private Constants
202 #define I2C_TIMEOUT_FLAG ((uint32_t)35) /* 35 ms */
203 #define I2C_TIMEOUT_ADDR_SLAVE ((uint32_t)10000) /* 10 s */
205 #define I2C_MIN_PCLK_FREQ ((uint32_t)2000000) /* 2 MHz*/
211 /* Private macro -------------------------------------------------------------*/
212 /* Private variables ---------------------------------------------------------*/
213 /* Private function prototypes -----------------------------------------------*/
214 /** @defgroup I2C_Private_Functions I2C Private Functions
218 static void I2C_DMAMasterTransmitCplt(DMA_HandleTypeDef *hdma);
219 static void I2C_DMAMasterReceiveCplt(DMA_HandleTypeDef *hdma);
220 static void I2C_DMASlaveTransmitCplt(DMA_HandleTypeDef *hdma);
221 static void I2C_DMASlaveReceiveCplt(DMA_HandleTypeDef *hdma);
222 static void I2C_DMAMemTransmitCplt(DMA_HandleTypeDef *hdma);
223 static void I2C_DMAMemReceiveCplt(DMA_HandleTypeDef *hdma);
224 static void I2C_DMAError(DMA_HandleTypeDef *hdma);
226 static HAL_StatusTypeDef I2C_MasterRequestWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Timeout);
227 static HAL_StatusTypeDef I2C_MasterRequestRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Timeout);
228 static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout);
229 static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout);
230 static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
231 static HAL_StatusTypeDef I2C_WaitOnMasterAddressFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, uint32_t Timeout);
233 static HAL_StatusTypeDef I2C_MasterTransmit_TXE(I2C_HandleTypeDef *hi2c);
234 static HAL_StatusTypeDef I2C_MasterTransmit_BTF(I2C_HandleTypeDef *hi2c);
235 static HAL_StatusTypeDef I2C_MasterReceive_RXNE(I2C_HandleTypeDef *hi2c);
236 static HAL_StatusTypeDef I2C_MasterReceive_BTF(I2C_HandleTypeDef *hi2c);
238 static HAL_StatusTypeDef I2C_SlaveTransmit_TXE(I2C_HandleTypeDef *hi2c);
239 static HAL_StatusTypeDef I2C_SlaveTransmit_BTF(I2C_HandleTypeDef *hi2c);
240 static HAL_StatusTypeDef I2C_SlaveReceive_RXNE(I2C_HandleTypeDef *hi2c);
241 static HAL_StatusTypeDef I2C_SlaveReceive_BTF(I2C_HandleTypeDef *hi2c);
242 static HAL_StatusTypeDef I2C_Slave_ADDR(I2C_HandleTypeDef *hi2c);
243 static HAL_StatusTypeDef I2C_Slave_STOPF(I2C_HandleTypeDef *hi2c);
244 static HAL_StatusTypeDef I2C_Slave_AF(I2C_HandleTypeDef *hi2c);
250 /* Exported functions ---------------------------------------------------------*/
252 /** @defgroup I2C_Exported_Functions I2C Exported Functions
256 /** @defgroup I2C_Exported_Functions_Group1 Initialization and de-initialization functions
257 * @brief Initialization and Configuration functions
260 ===============================================================================
261 ##### Initialization and de-initialization functions #####
262 ===============================================================================
263 [..] This subsection provides a set of functions allowing to initialize and
264 de-initialiaze the I2Cx peripheral:
266 (+) User must Implement HAL_I2C_MspInit() function in which he configures
267 all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC).
269 (+) Call the function HAL_I2C_Init() to configure the selected device with
270 the selected configuration:
271 (++) Communication Speed
275 (++) Dual Addressing mode
277 (++) General call mode
280 (+) Call the function HAL_I2C_DeInit() to restore the default configuration
281 of the selected I2Cx periperal.
288 * @brief Initializes the I2C according to the specified parameters
289 * in the I2C_InitTypeDef and create the associated handle.
290 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
291 * the configuration information for I2C module
294 HAL_StatusTypeDef HAL_I2C_Init(I2C_HandleTypeDef *hi2c)
296 uint32_t freqrange = 0;
299 /* Check the I2C handle allocation */
305 /* Check the parameters */
306 assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
307 assert_param(IS_I2C_CLOCK_SPEED(hi2c->Init.ClockSpeed));
308 assert_param(IS_I2C_DUTY_CYCLE(hi2c->Init.DutyCycle));
309 assert_param(IS_I2C_OWN_ADDRESS1(hi2c->Init.OwnAddress1));
310 assert_param(IS_I2C_ADDRESSING_MODE(hi2c->Init.AddressingMode));
311 assert_param(IS_I2C_DUAL_ADDRESS(hi2c->Init.DualAddressMode));
312 assert_param(IS_I2C_OWN_ADDRESS2(hi2c->Init.OwnAddress2));
313 assert_param(IS_I2C_GENERAL_CALL(hi2c->Init.GeneralCallMode));
314 assert_param(IS_I2C_NO_STRETCH(hi2c->Init.NoStretchMode));
316 if(hi2c->State == HAL_I2C_STATE_RESET)
318 /* Init the low level hardware : GPIO, CLOCK, NVIC */
319 HAL_I2C_MspInit(hi2c);
322 /* Get PCLK1 frequency */
323 pclk1 = HAL_RCC_GetPCLK1Freq();
325 /* The minimum allowed frequency is 2 MHz */
326 if(pclk1 < I2C_MIN_PCLK_FREQ)
331 hi2c->State = HAL_I2C_STATE_BUSY;
333 /* Disble the selected I2C peripheral */
334 __HAL_I2C_DISABLE(hi2c);
336 /* Calculate frequency range */
337 freqrange = I2C_FREQRANGE(pclk1);
339 /*---------------------------- I2Cx CR2 Configuration ----------------------*/
340 /* Configure I2Cx: Frequency range */
341 MODIFY_REG(hi2c->Instance->CR2, I2C_CR2_FREQ, freqrange);
343 /*---------------------------- I2Cx TRISE Configuration --------------------*/
344 /* Configure I2Cx: Rise Time */
345 MODIFY_REG(hi2c->Instance->TRISE, I2C_TRISE_TRISE, I2C_RISE_TIME(freqrange, hi2c->Init.ClockSpeed));
347 /*---------------------------- I2Cx CCR Configuration ----------------------*/
348 /* Configure I2Cx: Speed */
349 MODIFY_REG(hi2c->Instance->CCR, (I2C_CCR_FS | I2C_CCR_DUTY | I2C_CCR_CCR), I2C_SPEED(pclk1, hi2c->Init.ClockSpeed, hi2c->Init.DutyCycle));
351 /*---------------------------- I2Cx CR1 Configuration ----------------------*/
352 /* Configure I2Cx: Generalcall and NoStretch mode */
353 MODIFY_REG(hi2c->Instance->CR1, (I2C_CR1_ENGC | I2C_CR1_NOSTRETCH), (hi2c->Init.GeneralCallMode | hi2c->Init.NoStretchMode));
355 /*---------------------------- I2Cx OAR1 Configuration ---------------------*/
356 /* Configure I2Cx: Own Address1 and addressing mode */
357 MODIFY_REG(hi2c->Instance->OAR1, (I2C_OAR1_ADDMODE | I2C_OAR1_ADD8_9 | I2C_OAR1_ADD1_7 | I2C_OAR1_ADD0), (hi2c->Init.AddressingMode | hi2c->Init.OwnAddress1));
359 /*---------------------------- I2Cx OAR2 Configuration ---------------------*/
360 /* Configure I2Cx: Dual mode and Own Address2 */
361 MODIFY_REG(hi2c->Instance->OAR2, (I2C_OAR2_ENDUAL | I2C_OAR2_ADD2), (hi2c->Init.DualAddressMode | hi2c->Init.OwnAddress2));
363 /* Enable the selected I2C peripheral */
364 __HAL_I2C_ENABLE(hi2c);
366 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
367 hi2c->State = HAL_I2C_STATE_READY;
373 * @brief DeInitializes the I2C peripheral.
374 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
375 * the configuration information for I2C module
378 HAL_StatusTypeDef HAL_I2C_DeInit(I2C_HandleTypeDef *hi2c)
380 /* Check the I2C handle allocation */
386 /* Check the parameters */
387 assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
389 hi2c->State = HAL_I2C_STATE_BUSY;
391 /* Disable the I2C Peripheral Clock */
392 __HAL_I2C_DISABLE(hi2c);
394 /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
395 HAL_I2C_MspDeInit(hi2c);
397 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
399 hi2c->State = HAL_I2C_STATE_RESET;
408 * @brief I2C MSP Init.
409 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
410 * the configuration information for I2C module
413 __weak void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)
415 /* NOTE : This function Should not be modified, when the callback is needed,
416 the HAL_I2C_MspInit could be implemented in the user file
421 * @brief I2C MSP DeInit
422 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
423 * the configuration information for I2C module
426 __weak void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c)
428 /* NOTE : This function Should not be modified, when the callback is needed,
429 the HAL_I2C_MspDeInit could be implemented in the user file
437 /** @defgroup I2C_Exported_Functions_Group2 IO operation functions
438 * @brief Data transfers functions
441 ===============================================================================
442 ##### IO operation functions #####
443 ===============================================================================
445 This subsection provides a set of functions allowing to manage the I2C data
448 (#) There are two modes of transfer:
449 (++) Blocking mode : The communication is performed in the polling mode.
450 The status of all data processing is returned by the same function
451 after finishing transfer.
452 (++) No-Blocking mode : The communication is performed using Interrupts
453 or DMA. These functions return the status of the transfer startup.
454 The end of the data processing will be indicated through the
455 dedicated I2C IRQ when using Interrupt mode or the DMA IRQ when
458 (#) Blocking mode functions are :
459 (++) HAL_I2C_Master_Transmit()
460 (++) HAL_I2C_Master_Receive()
461 (++) HAL_I2C_Slave_Transmit()
462 (++) HAL_I2C_Slave_Receive()
463 (++) HAL_I2C_Mem_Write()
464 (++) HAL_I2C_Mem_Read()
465 (++) HAL_I2C_IsDeviceReady()
467 (#) No-Blocking mode functions with Interrupt are :
468 (++) HAL_I2C_Master_Transmit_IT()
469 (++) HAL_I2C_Master_Receive_IT()
470 (++) HAL_I2C_Slave_Transmit_IT()
471 (++) HAL_I2C_Slave_Receive_IT()
472 (++) HAL_I2C_Mem_Write_IT()
473 (++) HAL_I2C_Mem_Read_IT()
475 (#) No-Blocking mode functions with DMA are :
476 (++) HAL_I2C_Master_Transmit_DMA()
477 (++) HAL_I2C_Master_Receive_DMA()
478 (++) HAL_I2C_Slave_Transmit_DMA()
479 (++) HAL_I2C_Slave_Receive_DMA()
480 (++) HAL_I2C_Mem_Write_DMA()
481 (++) HAL_I2C_Mem_Read_DMA()
483 (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
484 (++) HAL_I2C_MemTxCpltCallback()
485 (++) HAL_I2C_MemRxCpltCallback()
486 (++) HAL_I2C_MasterTxCpltCallback()
487 (++) HAL_I2C_MasterRxCpltCallback()
488 (++) HAL_I2C_SlaveTxCpltCallback()
489 (++) HAL_I2C_SlaveRxCpltCallback()
490 (++) HAL_I2C_ErrorCallback()
497 * @brief Transmits in master mode an amount of data in blocking mode.
498 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
499 * the configuration information for I2C module
500 * @param DevAddress: Target device address
501 * @param pData: Pointer to data buffer
502 * @param Size: Amount of data to be sent
503 * @param Timeout: Timeout duration
506 HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
508 if(hi2c->State == HAL_I2C_STATE_READY)
510 if((pData == HAL_NULL) || (Size == 0))
515 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
523 hi2c->State = HAL_I2C_STATE_BUSY_TX;
524 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
526 /* Send Slave Address */
527 if(I2C_MasterRequestWrite(hi2c, DevAddress, Timeout) != HAL_OK)
529 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
539 /* Clear ADDR flag */
540 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
544 /* Wait until TXE flag is set */
545 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TXE, RESET, Timeout) != HAL_OK)
550 /* Write data to DR */
551 hi2c->Instance->DR = (*pData++);
554 if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (Size != 0))
556 /* Write data to DR */
557 hi2c->Instance->DR = (*pData++);
562 /* Wait until TXE flag is set */
563 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TXE, RESET, Timeout) != HAL_OK)
569 SET_BIT(hi2c->Instance->CR1,I2C_CR1_STOP);
571 /* Wait until BUSY flag is reset */
572 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout) != HAL_OK)
577 hi2c->State = HAL_I2C_STATE_READY;
579 /* Process Unlocked */
591 * @brief Receives in master mode an amount of data in blocking mode.
592 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
593 * the configuration information for I2C module
594 * @param DevAddress: Target device address
595 * @param pData: Pointer to data buffer
596 * @param Size: Amount of data to be sent
597 * @param Timeout: Timeout duration
600 HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
602 if(hi2c->State == HAL_I2C_STATE_READY)
604 if((pData == HAL_NULL) || (Size == 0))
609 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
617 hi2c->State = HAL_I2C_STATE_BUSY_RX;
618 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
620 /* Send Slave Address */
621 if(I2C_MasterRequestRead(hi2c, DevAddress, Timeout) != HAL_OK)
623 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
635 /* Disable Acknowledge */
636 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
638 /* Clear ADDR flag */
639 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
642 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
646 /* Disable Acknowledge */
647 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
650 SET_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
652 /* Clear ADDR flag */
653 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
657 /* Enable Acknowledge */
658 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
660 /* Clear ADDR flag */
661 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
671 /* Wait until RXNE flag is set */
672 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, Timeout) != HAL_OK)
677 /* Read data from DR */
678 (*pData++) = hi2c->Instance->DR;
684 /* Wait until BTF flag is set */
685 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout) != HAL_OK)
691 SET_BIT(hi2c->Instance->CR1,I2C_CR1_STOP);
693 /* Read data from DR */
694 (*pData++) = hi2c->Instance->DR;
697 /* Read data from DR */
698 (*pData++) = hi2c->Instance->DR;
704 /* Wait until BTF flag is set */
705 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout) != HAL_OK)
710 /* Disable Acknowledge */
711 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
713 /* Read data from DR */
714 (*pData++) = hi2c->Instance->DR;
717 /* Wait until BTF flag is set */
718 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout) != HAL_OK)
724 SET_BIT(hi2c->Instance->CR1,I2C_CR1_STOP);
726 /* Read data from DR */
727 (*pData++) = hi2c->Instance->DR;
730 /* Read data from DR */
731 (*pData++) = hi2c->Instance->DR;
737 /* Wait until RXNE flag is set */
738 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, Timeout) != HAL_OK)
743 /* Read data from DR */
744 (*pData++) = hi2c->Instance->DR;
747 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET)
749 /* Read data from DR */
750 (*pData++) = hi2c->Instance->DR;
757 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
759 /* Wait until BUSY flag is reset */
760 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout) != HAL_OK)
765 hi2c->State = HAL_I2C_STATE_READY;
767 /* Process Unlocked */
779 * @brief Transmits in slave mode an amount of data in blocking mode.
780 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
781 * the configuration information for I2C module
782 * @param pData: Pointer to data buffer
783 * @param Size: Amount of data to be sent
784 * @param Timeout: Timeout duration
787 HAL_StatusTypeDef HAL_I2C_Slave_Transmit(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
789 if(hi2c->State == HAL_I2C_STATE_READY)
791 if((pData == HAL_NULL) || (Size == 0))
796 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
804 hi2c->State = HAL_I2C_STATE_BUSY_TX;
805 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
807 /* Enable Address Acknowledge */
808 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
810 /* Wait until ADDR flag is set */
811 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout) != HAL_OK)
816 /* Clear ADDR flag */
817 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
819 /* If 10bit addressing mode is selected */
820 if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT)
822 /* Wait until ADDR flag is set */
823 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout) != HAL_OK)
828 /* Clear ADDR flag */
829 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
834 /* Wait until TXE flag is set */
835 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TXE, RESET, Timeout) != HAL_OK)
840 /* Write data to DR */
841 hi2c->Instance->DR = (*pData++);
844 if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (Size != 0))
846 /* Write data to DR */
847 hi2c->Instance->DR = (*pData++);
852 /* Wait until AF flag is set */
853 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_AF, RESET, Timeout) != HAL_OK)
859 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
861 /* Disable Address Acknowledge */
862 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
864 /* Wait until BUSY flag is reset */
865 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout) != HAL_OK)
870 hi2c->State = HAL_I2C_STATE_READY;
872 /* Process Unlocked */
884 * @brief Receive in slave mode an amount of data in blocking mode
885 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
886 * the configuration information for I2C module
887 * @param pData: Pointer to data buffer
888 * @param Size: Amount of data to be sent
889 * @param Timeout: Timeout duration
892 HAL_StatusTypeDef HAL_I2C_Slave_Receive(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
894 if(hi2c->State == HAL_I2C_STATE_READY)
896 if((pData == HAL_NULL) || (Size == 0))
901 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
909 hi2c->State = HAL_I2C_STATE_BUSY_RX;
910 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
912 /* Enable Address Acknowledge */
913 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
915 /* Wait until ADDR flag is set */
916 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout) != HAL_OK)
921 /* Clear ADDR flag */
922 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
926 /* Wait until RXNE flag is set */
927 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, Timeout) != HAL_OK)
932 /* Read data from DR */
933 (*pData++) = hi2c->Instance->DR;
936 if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (Size != 0))
938 /* Read data from DR */
939 (*pData++) = hi2c->Instance->DR;
944 /* Wait until STOP flag is set */
945 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout) != HAL_OK)
950 /* Clear STOP flag */
951 __HAL_I2C_CLEAR_STOPFLAG(hi2c);
953 /* Disable Address Acknowledge */
954 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
956 /* Wait until BUSY flag is reset */
957 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout) != HAL_OK)
962 hi2c->State = HAL_I2C_STATE_READY;
964 /* Process Unlocked */
976 * @brief Transmit in master mode an amount of data in no-blocking mode with Interrupt
977 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
978 * the configuration information for I2C module
979 * @param DevAddress: Target device address
980 * @param pData: Pointer to data buffer
981 * @param Size: Amount of data to be sent
984 HAL_StatusTypeDef HAL_I2C_Master_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
986 if(hi2c->State == HAL_I2C_STATE_READY)
988 if((pData == HAL_NULL) || (Size == 0))
993 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1001 hi2c->State = HAL_I2C_STATE_BUSY_TX;
1002 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1004 hi2c->pBuffPtr = pData;
1005 hi2c->XferSize = Size;
1006 hi2c->XferCount = Size;
1008 /* Send Slave Address */
1009 if(I2C_MasterRequestWrite(hi2c, DevAddress, I2C_TIMEOUT_FLAG) != HAL_OK)
1011 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1021 /* Clear ADDR flag */
1022 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
1024 /* Process Unlocked */
1027 /* Note : The I2C interrupts must be enabled after unlocking current process
1028 to avoid the risk of I2C interrupt handle execution before current
1031 /* Enable EVT, BUF and ERR interrupt */
1032 __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
1043 * @brief Receive in master mode an amount of data in no-blocking mode with Interrupt
1044 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
1045 * the configuration information for I2C module
1046 * @param DevAddress: Target device address
1047 * @param pData: Pointer to data buffer
1048 * @param Size: Amount of data to be sent
1049 * @retval HAL status
1051 HAL_StatusTypeDef HAL_I2C_Master_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
1053 if(hi2c->State == HAL_I2C_STATE_READY)
1055 if((pData == HAL_NULL) || (Size == 0))
1060 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1065 /* Process Locked */
1068 hi2c->State = HAL_I2C_STATE_BUSY_RX;
1069 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1071 hi2c->pBuffPtr = pData;
1072 hi2c->XferSize = Size;
1073 hi2c->XferCount = Size;
1075 /* Send Slave Address */
1076 if(I2C_MasterRequestRead(hi2c, DevAddress, I2C_TIMEOUT_FLAG) != HAL_OK)
1078 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1088 if(hi2c->XferCount == 1)
1090 /* Disable Acknowledge */
1091 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1093 /* Clear ADDR flag */
1094 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
1097 SET_BIT(hi2c->Instance->CR1,I2C_CR1_STOP);
1099 else if(hi2c->XferCount == 2)
1101 /* Disable Acknowledge */
1102 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1105 SET_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
1107 /* Clear ADDR flag */
1108 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
1112 /* Enable Acknowledge */
1113 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1115 /* Clear ADDR flag */
1116 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
1119 /* Process Unlocked */
1122 /* Note : The I2C interrupts must be enabled after unlocking current process
1123 to avoid the risk of I2C interrupt handle execution before current
1126 /* Enable EVT, BUF and ERR interrupt */
1127 __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
1138 * @brief Transmit in slave mode an amount of data in no-blocking mode with Interrupt
1139 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
1140 * the configuration information for I2C module
1141 * @param pData: Pointer to data buffer
1142 * @param Size: Amount of data to be sent
1143 * @retval HAL status
1145 HAL_StatusTypeDef HAL_I2C_Slave_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
1147 if(hi2c->State == HAL_I2C_STATE_READY)
1149 if((pData == HAL_NULL) || (Size == 0))
1154 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1159 /* Process Locked */
1162 hi2c->State = HAL_I2C_STATE_BUSY_TX;
1163 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1165 hi2c->pBuffPtr = pData;
1166 hi2c->XferSize = Size;
1167 hi2c->XferCount = Size;
1169 /* Enable Address Acknowledge */
1170 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1172 /* Process Unlocked */
1175 /* Note : The I2C interrupts must be enabled after unlocking current process
1176 to avoid the risk of I2C interrupt handle execution before current
1179 /* Enable EVT, BUF and ERR interrupt */
1180 __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
1191 * @brief Receive in slave mode an amount of data in no-blocking mode with Interrupt
1192 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
1193 * the configuration information for I2C module
1194 * @param pData: Pointer to data buffer
1195 * @param Size: Amount of data to be sent
1196 * @retval HAL status
1198 HAL_StatusTypeDef HAL_I2C_Slave_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
1200 if(hi2c->State == HAL_I2C_STATE_READY)
1202 if((pData == HAL_NULL) || (Size == 0))
1207 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1212 /* Process Locked */
1215 hi2c->State = HAL_I2C_STATE_BUSY_RX;
1216 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1218 hi2c->pBuffPtr = pData;
1219 hi2c->XferSize = Size;
1220 hi2c->XferCount = Size;
1222 /* Enable Address Acknowledge */
1223 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1225 /* Process Unlocked */
1228 /* Note : The I2C interrupts must be enabled after unlocking current process
1229 to avoid the risk of I2C interrupt handle execution before current
1232 /* Enable EVT, BUF and ERR interrupt */
1233 __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
1244 * @brief Transmit in master mode an amount of data in no-blocking mode with DMA
1245 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
1246 * the configuration information for I2C module
1247 * @param DevAddress: Target device address
1248 * @param pData: Pointer to data buffer
1249 * @param Size: Amount of data to be sent
1250 * @retval HAL status
1252 HAL_StatusTypeDef HAL_I2C_Master_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
1254 if(hi2c->State == HAL_I2C_STATE_READY)
1256 if((pData == HAL_NULL) || (Size == 0))
1261 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1266 /* Process Locked */
1269 hi2c->State = HAL_I2C_STATE_BUSY_TX;
1270 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1272 hi2c->pBuffPtr = pData;
1273 hi2c->XferSize = Size;
1274 hi2c->XferCount = Size;
1276 /* Set the I2C DMA transfert complete callback */
1277 hi2c->hdmatx->XferCpltCallback = I2C_DMAMasterTransmitCplt;
1279 /* Set the DMA error callback */
1280 hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
1282 /* Enable the DMA Channel */
1283 HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->DR, Size);
1285 /* Send Slave Address */
1286 if(I2C_MasterRequestWrite(hi2c, DevAddress, I2C_TIMEOUT_FLAG) != HAL_OK)
1288 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1298 /* Enable DMA Request */
1299 SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
1301 /* Clear ADDR flag */
1302 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
1304 /* Process Unlocked */
1316 * @brief Receive in master mode an amount of data in no-blocking mode with DMA
1317 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
1318 * the configuration information for I2C module
1319 * @param DevAddress: Target device address
1320 * @param pData: Pointer to data buffer
1321 * @param Size: Amount of data to be sent
1322 * @retval HAL status
1324 HAL_StatusTypeDef HAL_I2C_Master_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
1326 if(hi2c->State == HAL_I2C_STATE_READY)
1328 if((pData == HAL_NULL) || (Size == 0))
1333 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1338 /* Process Locked */
1341 hi2c->State = HAL_I2C_STATE_BUSY_RX;
1342 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1344 hi2c->pBuffPtr = pData;
1345 hi2c->XferSize = Size;
1346 hi2c->XferCount = Size;
1348 /* Set the I2C DMA transfert complete callback */
1349 hi2c->hdmarx->XferCpltCallback = I2C_DMAMasterReceiveCplt;
1351 /* Set the DMA error callback */
1352 hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
1354 /* Enable the DMA Channel */
1355 HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)pData, Size);
1357 /* Send Slave Address */
1358 if(I2C_MasterRequestRead(hi2c, DevAddress, I2C_TIMEOUT_FLAG) != HAL_OK)
1360 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1372 /* Disable Acknowledge */
1373 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1377 /* Enable Last DMA bit */
1378 SET_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
1381 /* Enable DMA Request */
1382 SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
1384 /* Clear ADDR flag */
1385 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
1387 /* Process Unlocked */
1399 * @brief Transmit in slave mode an amount of data in no-blocking mode with DMA
1400 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
1401 * the configuration information for I2C module
1402 * @param pData: Pointer to data buffer
1403 * @param Size: Amount of data to be sent
1404 * @retval HAL status
1406 HAL_StatusTypeDef HAL_I2C_Slave_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
1408 if(hi2c->State == HAL_I2C_STATE_READY)
1410 if((pData == HAL_NULL) || (Size == 0))
1415 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1420 /* Process Locked */
1423 hi2c->State = HAL_I2C_STATE_BUSY_TX;
1424 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1426 hi2c->pBuffPtr = pData;
1427 hi2c->XferSize = Size;
1428 hi2c->XferCount = Size;
1430 /* Set the I2C DMA transfert complete callback */
1431 hi2c->hdmatx->XferCpltCallback = I2C_DMASlaveTransmitCplt;
1433 /* Set the DMA error callback */
1434 hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
1436 /* Enable the DMA Channel */
1437 HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->DR, Size);
1439 /* Enable DMA Request */
1440 SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
1442 /* Enable Address Acknowledge */
1443 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1445 /* Wait until ADDR flag is set */
1446 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, I2C_TIMEOUT_ADDR_SLAVE) != HAL_OK)
1451 /* If 7bit addressing mode is selected */
1452 if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_7BIT)
1454 /* Clear ADDR flag */
1455 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
1459 /* Clear ADDR flag */
1460 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
1462 /* Wait until ADDR flag is set */
1463 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, I2C_TIMEOUT_ADDR_SLAVE) != HAL_OK)
1468 /* Clear ADDR flag */
1469 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
1472 /* Process Unlocked */
1484 * @brief Receive in slave mode an amount of data in no-blocking mode with DMA
1485 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
1486 * the configuration information for I2C module
1487 * @param pData: Pointer to data buffer
1488 * @param Size: Amount of data to be sent
1489 * @retval HAL status
1491 HAL_StatusTypeDef HAL_I2C_Slave_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
1493 if(hi2c->State == HAL_I2C_STATE_READY)
1495 if((pData == HAL_NULL) || (Size == 0))
1500 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1505 /* Process Locked */
1508 hi2c->State = HAL_I2C_STATE_BUSY_RX;
1509 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1511 hi2c->pBuffPtr = pData;
1512 hi2c->XferSize = Size;
1513 hi2c->XferCount = Size;
1515 /* Set the I2C DMA transfert complete callback */
1516 hi2c->hdmarx->XferCpltCallback = I2C_DMASlaveReceiveCplt;
1518 /* Set the DMA error callback */
1519 hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
1521 /* Enable the DMA Channel */
1522 HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)pData, Size);
1524 /* Enable DMA Request */
1525 SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
1527 /* Enable Address Acknowledge */
1528 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1530 /* Wait until ADDR flag is set */
1531 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, I2C_TIMEOUT_ADDR_SLAVE) != HAL_OK)
1536 /* Clear ADDR flag */
1537 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
1539 /* Process Unlocked */
1550 * @brief Write an amount of data in blocking mode to a specific memory address
1551 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
1552 * the configuration information for I2C module
1553 * @param DevAddress: Target device address
1554 * @param MemAddress: Internal memory address
1555 * @param MemAddSize: Size of internal memory address
1556 * @param pData: Pointer to data buffer
1557 * @param Size: Amount of data to be sent
1558 * @param Timeout: Timeout duration
1559 * @retval HAL status
1561 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)
1563 /* Check the parameters */
1564 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
1566 if(hi2c->State == HAL_I2C_STATE_READY)
1568 if((pData == HAL_NULL) || (Size == 0))
1573 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1578 /* Process Locked */
1581 hi2c->State = HAL_I2C_STATE_MEM_BUSY_TX;
1582 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1584 /* Send Slave Address and Memory Address */
1585 if(I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, Timeout) != HAL_OK)
1587 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1599 /* Wait until TXE flag is set */
1600 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TXE, RESET, Timeout) != HAL_OK)
1605 /* Write data to DR */
1606 hi2c->Instance->DR = (*pData++);
1609 if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (Size != 0))
1611 /* Write data to DR */
1612 hi2c->Instance->DR = (*pData++);
1617 /* Wait until TXE flag is set */
1618 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TXE, RESET, Timeout) != HAL_OK)
1624 SET_BIT(hi2c->Instance->CR1,I2C_CR1_STOP);
1626 /* Wait until BUSY flag is reset */
1627 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout) != HAL_OK)
1632 hi2c->State = HAL_I2C_STATE_READY;
1634 /* Process Unlocked */
1646 * @brief Read an amount of data in blocking mode from a specific memory address
1647 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
1648 * the configuration information for I2C module
1649 * @param DevAddress: Target device address
1650 * @param MemAddress: Internal memory address
1651 * @param MemAddSize: Size of internal memory address
1652 * @param pData: Pointer to data buffer
1653 * @param Size: Amount of data to be sent
1654 * @param Timeout: Timeout duration
1655 * @retval HAL status
1657 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)
1659 /* Check the parameters */
1660 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
1662 if(hi2c->State == HAL_I2C_STATE_READY)
1664 if((pData == HAL_NULL) || (Size == 0))
1669 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1674 /* Process Locked */
1677 hi2c->State = HAL_I2C_STATE_MEM_BUSY_RX;
1678 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1680 /* Send Slave Address and Memory Address */
1681 if(I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, Timeout) != HAL_OK)
1683 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1695 /* Disable Acknowledge */
1696 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1698 /* Clear ADDR flag */
1699 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
1702 SET_BIT(hi2c->Instance->CR1,I2C_CR1_STOP);
1706 /* Disable Acknowledge */
1707 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1710 SET_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
1712 /* Clear ADDR flag */
1713 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
1717 /* Clear ADDR flag */
1718 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
1728 /* Wait until RXNE flag is set */
1729 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, Timeout) != HAL_OK)
1734 /* Read data from DR */
1735 (*pData++) = hi2c->Instance->DR;
1741 /* Wait until BTF flag is set */
1742 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout) != HAL_OK)
1748 SET_BIT(hi2c->Instance->CR1,I2C_CR1_STOP);
1750 /* Read data from DR */
1751 (*pData++) = hi2c->Instance->DR;
1754 /* Read data from DR */
1755 (*pData++) = hi2c->Instance->DR;
1761 /* Wait until BTF flag is set */
1762 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout) != HAL_OK)
1767 /* Disable Acknowledge */
1768 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1770 /* Read data from DR */
1771 (*pData++) = hi2c->Instance->DR;
1774 /* Wait until BTF flag is set */
1775 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout) != HAL_OK)
1781 SET_BIT(hi2c->Instance->CR1,I2C_CR1_STOP);
1783 /* Read data from DR */
1784 (*pData++) = hi2c->Instance->DR;
1787 /* Read data from DR */
1788 (*pData++) = hi2c->Instance->DR;
1794 /* Wait until RXNE flag is set */
1795 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, Timeout) != HAL_OK)
1800 /* Read data from DR */
1801 (*pData++) = hi2c->Instance->DR;
1804 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET)
1806 /* Read data from DR */
1807 (*pData++) = hi2c->Instance->DR;
1814 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
1816 /* Wait until BUSY flag is reset */
1817 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout) != HAL_OK)
1822 hi2c->State = HAL_I2C_STATE_READY;
1824 /* Process Unlocked */
1835 * @brief Write an amount of data in no-blocking mode with Interrupt to a specific memory address
1836 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
1837 * the configuration information for I2C module
1838 * @param DevAddress: Target device address
1839 * @param MemAddress: Internal memory address
1840 * @param MemAddSize: Size of internal memory address
1841 * @param pData: Pointer to data buffer
1842 * @param Size: Amount of data to be sent
1843 * @retval HAL status
1845 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)
1847 /* Check the parameters */
1848 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
1850 if(hi2c->State == HAL_I2C_STATE_READY)
1852 if((pData == HAL_NULL) || (Size == 0))
1857 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1862 /* Process Locked */
1865 hi2c->State = HAL_I2C_STATE_MEM_BUSY_TX;
1866 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1868 hi2c->pBuffPtr = pData;
1869 hi2c->XferSize = Size;
1870 hi2c->XferCount = Size;
1872 /* Send Slave Address and Memory Address */
1873 if(I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG) != HAL_OK)
1875 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1885 /* Process Unlocked */
1888 /* Note : The I2C interrupts must be enabled after unlocking current process
1889 to avoid the risk of I2C interrupt handle execution before current
1892 /* Enable EVT, BUF and ERR interrupt */
1893 __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
1904 * @brief Read an amount of data in no-blocking mode with Interrupt from a specific memory address
1905 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
1906 * the configuration information for I2C module
1907 * @param DevAddress: Target device address
1908 * @param MemAddress: Internal memory address
1909 * @param MemAddSize: Size of internal memory address
1910 * @param pData: Pointer to data buffer
1911 * @param Size: Amount of data to be sent
1912 * @retval HAL status
1914 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)
1916 /* Check the parameters */
1917 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
1919 if(hi2c->State == HAL_I2C_STATE_READY)
1921 if((pData == HAL_NULL) || (Size == 0))
1926 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1931 /* Process Locked */
1934 hi2c->State = HAL_I2C_STATE_MEM_BUSY_RX;
1935 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1937 hi2c->pBuffPtr = pData;
1938 hi2c->XferSize = Size;
1939 hi2c->XferCount = Size;
1941 /* Send Slave Address and Memory Address */
1942 if(I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG) != HAL_OK)
1944 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1954 if(hi2c->XferCount == 1)
1956 /* Disable Acknowledge */
1957 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1959 /* Clear ADDR flag */
1960 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
1963 SET_BIT(hi2c->Instance->CR1,I2C_CR1_STOP);
1965 else if(hi2c->XferCount == 2)
1967 /* Disable Acknowledge */
1968 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1971 SET_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
1973 /* Clear ADDR flag */
1974 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
1978 /* Enable Acknowledge */
1979 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1981 /* Clear ADDR flag */
1982 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
1985 /* Process Unlocked */
1988 /* Note : The I2C interrupts must be enabled after unlocking current process
1989 to avoid the risk of I2C interrupt handle execution before current
1992 /* Enable EVT, BUF and ERR interrupt */
1993 __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
2003 * @brief Write an amount of data in no-blocking mode with DMA to a specific memory address
2004 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
2005 * the configuration information for I2C module
2006 * @param DevAddress: Target device address
2007 * @param MemAddress: Internal memory address
2008 * @param MemAddSize: Size of internal memory address
2009 * @param pData: Pointer to data buffer
2010 * @param Size: Amount of data to be sent
2011 * @retval HAL status
2013 HAL_StatusTypeDef HAL_I2C_Mem_Write_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
2015 /* Check the parameters */
2016 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
2018 if(hi2c->State == HAL_I2C_STATE_READY)
2020 if((pData == HAL_NULL) || (Size == 0))
2025 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
2030 /* Process Locked */
2033 hi2c->State = HAL_I2C_STATE_MEM_BUSY_TX;
2034 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
2036 hi2c->pBuffPtr = pData;
2037 hi2c->XferSize = Size;
2038 hi2c->XferCount = Size;
2040 /* Set the I2C DMA transfert complete callback */
2041 hi2c->hdmatx->XferCpltCallback = I2C_DMAMemTransmitCplt;
2043 /* Set the DMA error callback */
2044 hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
2046 /* Enable the DMA Channel */
2047 HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->DR, Size);
2049 /* Send Slave Address and Memory Address */
2050 if(I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG) != HAL_OK)
2052 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
2062 /* Enable DMA Request */
2063 SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
2065 /* Process Unlocked */
2077 * @brief Reads an amount of data in no-blocking mode with DMA from a specific memory address.
2078 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
2079 * the configuration information for I2C module
2080 * @param DevAddress: Target device address
2081 * @param MemAddress: Internal memory address
2082 * @param MemAddSize: Size of internal memory address
2083 * @param pData: Pointer to data buffer
2084 * @param Size: Amount of data to be read
2085 * @retval HAL status
2087 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)
2089 /* Check the parameters */
2090 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
2092 if(hi2c->State == HAL_I2C_STATE_READY)
2094 if((pData == HAL_NULL) || (Size == 0))
2099 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
2104 /* Process Locked */
2107 hi2c->State = HAL_I2C_STATE_MEM_BUSY_RX;
2108 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
2110 hi2c->pBuffPtr = pData;
2111 hi2c->XferSize = Size;
2112 hi2c->XferCount = Size;
2114 /* Set the I2C DMA transfert complete callback */
2115 hi2c->hdmarx->XferCpltCallback = I2C_DMAMemReceiveCplt;
2117 /* Set the DMA error callback */
2118 hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
2120 /* Enable the DMA Channel */
2121 HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)pData, Size);
2123 /* Send Slave Address and Memory Address */
2124 if(I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG) != HAL_OK)
2126 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
2138 /* Disable Acknowledge */
2139 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
2143 /* Enable Last DMA bit */
2144 SET_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
2147 /* Enable DMA Request */
2148 SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
2150 /* Clear ADDR flag */
2151 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
2153 /* Process Unlocked */
2165 * @brief Checks if target device is ready for communication.
2166 * @note This function is used with Memory devices
2167 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
2168 * the configuration information for I2C module
2169 * @param DevAddress: Target device address
2170 * @param Trials: Number of trials
2171 * @param Timeout: Timeout duration
2172 * @retval HAL status
2174 HAL_StatusTypeDef HAL_I2C_IsDeviceReady(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout)
2176 uint32_t tickstart = 0, I2C_Trials = 1;
2178 if(hi2c->State == HAL_I2C_STATE_READY)
2180 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
2185 /* Process Locked */
2188 hi2c->State = HAL_I2C_STATE_BUSY;
2189 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
2193 /* Generate Start */
2194 SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
2196 /* Wait until SB flag is set */
2197 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout) != HAL_OK)
2202 /* Send slave address */
2203 hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress);
2205 /* Wait until ADDR or AF flag are set */
2207 tickstart = HAL_GetTick();
2209 while((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == RESET) && \
2210 (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == RESET) && \
2211 (hi2c->State != HAL_I2C_STATE_TIMEOUT))
2213 if(Timeout != HAL_MAX_DELAY)
2215 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
2217 hi2c->State = HAL_I2C_STATE_TIMEOUT;
2222 hi2c->State = HAL_I2C_STATE_READY;
2224 /* Check if the ADDR flag has been set */
2225 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == SET)
2228 SET_BIT(hi2c->Instance->CR1,I2C_CR1_STOP);
2230 /* Clear ADDR Flag */
2231 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
2233 /* Wait until BUSY flag is reset */
2234 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout) != HAL_OK)
2239 hi2c->State = HAL_I2C_STATE_READY;
2241 /* Process Unlocked */
2249 SET_BIT(hi2c->Instance->CR1,I2C_CR1_STOP);
2252 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
2254 /* Wait until BUSY flag is reset */
2255 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout) != HAL_OK)
2260 }while(I2C_Trials++ < Trials);
2262 hi2c->State = HAL_I2C_STATE_READY;
2264 /* Process Unlocked */
2276 * @brief This function handles I2C event interrupt request.
2277 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
2278 * the configuration information for I2C module
2279 * @retval HAL status
2281 void HAL_I2C_EV_IRQHandler(I2C_HandleTypeDef *hi2c)
2283 /* Master mode selected */
2284 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_MSL) == SET)
2286 /* I2C in mode Transmitter -----------------------------------------------*/
2287 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TRA) == SET)
2289 /* TXE set and BTF reset -----------------------------------------------*/
2290 if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXE) == SET) && \
2291 (__HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_BUF) == SET) && \
2292 (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == RESET))
2294 I2C_MasterTransmit_TXE(hi2c);
2296 /* BTF set -------------------------------------------------------------*/
2297 else if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && \
2298 (__HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_EVT) == SET))
2300 I2C_MasterTransmit_BTF(hi2c);
2303 /* I2C in mode Receiver --------------------------------------------------*/
2306 /* RXNE set and BTF reset -----------------------------------------------*/
2307 if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET) && \
2308 (__HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_BUF) == SET) && \
2309 (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == RESET))
2311 I2C_MasterReceive_RXNE(hi2c);
2313 /* BTF set -------------------------------------------------------------*/
2314 else if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (__HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_EVT) == SET))
2316 I2C_MasterReceive_BTF(hi2c);
2320 /* Slave mode selected */
2323 /* ADDR set --------------------------------------------------------------*/
2324 if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == SET) && \
2325 (__HAL_I2C_GET_IT_SOURCE(hi2c, (I2C_IT_EVT)) == SET))
2327 I2C_Slave_ADDR(hi2c);
2329 /* STOPF set --------------------------------------------------------------*/
2330 else if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET) && \
2331 (__HAL_I2C_GET_IT_SOURCE(hi2c, (I2C_IT_EVT)) == SET))
2333 I2C_Slave_STOPF(hi2c);
2335 /* I2C in mode Transmitter -----------------------------------------------*/
2336 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TRA) == SET)
2338 /* TXE set and BTF reset -----------------------------------------------*/
2339 if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXE) == SET) && \
2340 (__HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_BUF) == SET) && \
2341 (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == RESET))
2343 I2C_SlaveTransmit_TXE(hi2c);
2345 /* BTF set -------------------------------------------------------------*/
2346 else if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && \
2347 (__HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_EVT) == SET))
2349 I2C_SlaveTransmit_BTF(hi2c);
2352 /* I2C in mode Receiver --------------------------------------------------*/
2355 /* RXNE set and BTF reset ----------------------------------------------*/
2356 if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET) && \
2357 (__HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_BUF) == SET) && \
2358 (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == RESET))
2360 I2C_SlaveReceive_RXNE(hi2c);
2362 /* BTF set -------------------------------------------------------------*/
2363 else if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (__HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_EVT) == SET))
2365 I2C_SlaveReceive_BTF(hi2c);
2372 * @brief This function handles I2C error interrupt request.
2373 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
2374 * the configuration information for I2C module
2375 * @retval HAL status
2377 void HAL_I2C_ER_IRQHandler(I2C_HandleTypeDef *hi2c)
2380 /* I2C Bus error interrupt occurred ----------------------------------------*/
2381 if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BERR) == SET) && \
2382 (__HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_ERR) == SET))
2384 hi2c->ErrorCode |= HAL_I2C_ERROR_BERR;
2386 /* Clear BERR flag */
2387 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_BERR);
2390 /* I2C Arbitration Loss error interrupt occurred ---------------------------*/
2391 if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ARLO) == SET) && \
2392 (__HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_ERR) == SET))
2394 hi2c->ErrorCode |= HAL_I2C_ERROR_ARLO;
2396 /* Clear ARLO flag */
2397 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ARLO);
2400 /* I2C Acknowledge failure error interrupt occurred ------------------------*/
2401 if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET) && \
2402 (__HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_ERR) == SET))
2404 if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_MSL) == RESET) && \
2405 (hi2c->XferCount == 0) && \
2406 (hi2c->State == HAL_I2C_STATE_BUSY_TX))
2412 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
2414 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
2418 /* I2C Over-Run/Under-Run interrupt occurred -------------------------------*/
2419 if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_OVR) == SET) && \
2420 (__HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_ERR) == SET))
2422 hi2c->ErrorCode |= HAL_I2C_ERROR_OVR;
2423 /* Clear OVR flag */
2424 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_OVR);
2427 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
2429 hi2c->State = HAL_I2C_STATE_READY;
2431 /* Disable EVT, BUF and ERR interrupts */
2432 __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
2434 HAL_I2C_ErrorCallback(hi2c);
2439 * @brief Master Tx Transfer completed callbacks.
2440 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
2441 * the configuration information for I2C module
2444 __weak void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c)
2446 /* NOTE : This function should not be modified, when the callback is needed,
2447 the HAL_I2C_MasterTxCpltCallback can be implemented in the user file
2452 * @brief Master Rx Transfer completed callbacks.
2453 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
2454 * the configuration information for I2C module
2457 __weak void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c)
2459 /* NOTE : This function should not be modified, when the callback is needed,
2460 the HAL_I2C_MasterRxCpltCallback can be implemented in the user file
2464 /** @brief Slave Tx Transfer completed callbacks.
2465 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
2466 * the configuration information for I2C module
2469 __weak void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c)
2471 /* NOTE : This function should not be modified, when the callback is needed,
2472 the HAL_I2C_SlaveTxCpltCallback can be implemented in the user file
2477 * @brief Slave Rx Transfer completed callbacks.
2478 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
2479 * the configuration information for I2C module
2482 __weak void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c)
2484 /* NOTE : This function should not be modified, when the callback is needed,
2485 the HAL_I2C_SlaveRxCpltCallback can be implemented in the user file
2490 * @brief Memory Tx Transfer completed callbacks.
2491 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
2492 * the configuration information for I2C module
2495 __weak void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef *hi2c)
2497 /* NOTE : This function should not be modified, when the callback is needed,
2498 the HAL_I2C_MemTxCpltCallback can be implemented in the user file
2503 * @brief Memory Rx Transfer completed callbacks.
2504 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
2505 * the configuration information for I2C module
2508 __weak void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c)
2510 /* NOTE : This function should not be modified, when the callback is needed,
2511 the HAL_I2C_MemRxCpltCallback can be implemented in the user file
2516 * @brief I2C error callbacks.
2517 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
2518 * the configuration information for I2C module
2521 __weak void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
2523 /* NOTE : This function should not be modified, when the callback is needed,
2524 the HAL_I2C_ErrorCallback can be implemented in the user file
2532 /** @defgroup I2C_Exported_Functions_Group3 Peripheral State and Errors functions
2533 * @brief Peripheral State and Errors functions
2536 ===============================================================================
2537 ##### Peripheral State and Errors functions #####
2538 ===============================================================================
2540 This subsection permits to get in run-time the status of the peripheral
2548 * @brief Returns the I2C state.
2549 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
2550 * the configuration information for I2C module
2553 HAL_I2C_StateTypeDef HAL_I2C_GetState(I2C_HandleTypeDef *hi2c)
2559 * @brief Return the I2C error code
2560 * @param hi2c : pointer to a I2C_HandleTypeDef structure that contains
2561 * the configuration information for the specified I2C.
2562 * @retval I2C Error Code
2564 uint32_t HAL_I2C_GetError(I2C_HandleTypeDef *hi2c)
2566 return hi2c->ErrorCode;
2578 /** @addtogroup I2C_Private_Functions
2584 * @brief Handle TXE flag for Master
2585 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
2586 * the configuration information for I2C module
2587 * @retval HAL status
2589 static HAL_StatusTypeDef I2C_MasterTransmit_TXE(I2C_HandleTypeDef *hi2c)
2591 /* Write data to DR */
2592 hi2c->Instance->DR = (*hi2c->pBuffPtr++);
2595 if(hi2c->XferCount == 0)
2597 /* Disable BUF interrupt */
2598 __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF);
2605 * @brief Handle BTF flag for Master transmitter
2606 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
2607 * the configuration information for I2C module
2608 * @retval HAL status
2610 static HAL_StatusTypeDef I2C_MasterTransmit_BTF(I2C_HandleTypeDef *hi2c)
2612 if(hi2c->XferCount != 0)
2614 /* Write data to DR */
2615 hi2c->Instance->DR = (*hi2c->pBuffPtr++);
2620 /* Disable EVT, BUF and ERR interrupt */
2621 __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
2624 SET_BIT(hi2c->Instance->CR1,I2C_CR1_STOP);
2626 /* Wait until BUSY flag is reset */
2627 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_FLAG) != HAL_OK)
2632 if(hi2c->State == HAL_I2C_STATE_MEM_BUSY_TX)
2634 hi2c->State = HAL_I2C_STATE_READY;
2636 HAL_I2C_MemTxCpltCallback(hi2c);
2640 hi2c->State = HAL_I2C_STATE_READY;
2642 HAL_I2C_MasterTxCpltCallback(hi2c);
2649 * @brief Handle RXNE flag for Master
2650 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
2651 * the configuration information for I2C module
2652 * @retval HAL status
2654 static HAL_StatusTypeDef I2C_MasterReceive_RXNE(I2C_HandleTypeDef *hi2c)
2658 tmp = hi2c->XferCount;
2661 /* Read data from DR */
2662 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
2665 else if((tmp == 2) || (tmp == 3))
2667 /* Disable BUF interrupt */
2668 __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF);
2672 /* Disable EVT, BUF and ERR interrupt */
2673 __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
2675 /* Read data from DR */
2676 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
2679 /* Wait until BUSY flag is reset */
2680 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_FLAG) != HAL_OK)
2685 if(hi2c->State == HAL_I2C_STATE_MEM_BUSY_RX)
2687 hi2c->State = HAL_I2C_STATE_READY;
2689 HAL_I2C_MemRxCpltCallback(hi2c);
2693 hi2c->State = HAL_I2C_STATE_READY;
2695 HAL_I2C_MasterRxCpltCallback(hi2c);
2702 * @brief Handle BTF flag for Master receiver
2703 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
2704 * the configuration information for I2C module
2705 * @retval HAL status
2707 static HAL_StatusTypeDef I2C_MasterReceive_BTF(I2C_HandleTypeDef *hi2c)
2709 if(hi2c->XferCount == 3)
2711 /* Disable Acknowledge */
2712 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
2714 /* Read data from DR */
2715 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
2718 else if(hi2c->XferCount == 2)
2721 SET_BIT(hi2c->Instance->CR1,I2C_CR1_STOP);
2723 /* Read data from DR */
2724 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
2727 /* Read data from DR */
2728 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
2731 /* Disable EVT and ERR interrupt */
2732 __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
2734 /* Wait until BUSY flag is reset */
2735 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_FLAG) != HAL_OK)
2740 if(hi2c->State == HAL_I2C_STATE_MEM_BUSY_RX)
2742 hi2c->State = HAL_I2C_STATE_READY;
2744 HAL_I2C_MemRxCpltCallback(hi2c);
2748 hi2c->State = HAL_I2C_STATE_READY;
2750 HAL_I2C_MasterRxCpltCallback(hi2c);
2755 /* Read data from DR */
2756 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
2763 * @brief Handle TXE flag for Slave
2764 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
2765 * the configuration information for I2C module
2766 * @retval HAL status
2768 static HAL_StatusTypeDef I2C_SlaveTransmit_TXE(I2C_HandleTypeDef *hi2c)
2770 if(hi2c->XferCount != 0)
2772 /* Write data to DR */
2773 hi2c->Instance->DR = (*hi2c->pBuffPtr++);
2780 * @brief Handle BTF flag for Slave transmitter
2781 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
2782 * the configuration information for I2C module
2783 * @retval HAL status
2785 static HAL_StatusTypeDef I2C_SlaveTransmit_BTF(I2C_HandleTypeDef *hi2c)
2787 if(hi2c->XferCount != 0)
2789 /* Write data to DR */
2790 hi2c->Instance->DR = (*hi2c->pBuffPtr++);
2797 * @brief Handle RXNE flag for Slave
2798 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
2799 * the configuration information for I2C module
2800 * @retval HAL status
2802 static HAL_StatusTypeDef I2C_SlaveReceive_RXNE(I2C_HandleTypeDef *hi2c)
2804 if(hi2c->XferCount != 0)
2806 /* Read data from DR */
2807 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
2814 * @brief Handle BTF flag for Slave receiver
2815 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
2816 * the configuration information for I2C module
2817 * @retval HAL status
2819 static HAL_StatusTypeDef I2C_SlaveReceive_BTF(I2C_HandleTypeDef *hi2c)
2821 if(hi2c->XferCount != 0)
2823 /* Read data from DR */
2824 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
2831 * @brief Handle ADD flag for Slave
2832 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
2833 * the configuration information for I2C module
2834 * @retval HAL status
2836 static HAL_StatusTypeDef I2C_Slave_ADDR(I2C_HandleTypeDef *hi2c)
2838 /* Clear ADDR flag */
2839 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
2845 * @brief Handle STOPF flag for Slave
2846 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
2847 * the configuration information for I2C module
2848 * @retval HAL status
2850 static HAL_StatusTypeDef I2C_Slave_STOPF(I2C_HandleTypeDef *hi2c)
2852 /* Disable EVT, BUF and ERR interrupt */
2853 __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
2855 /* Clear STOPF flag */
2856 __HAL_I2C_CLEAR_STOPFLAG(hi2c);
2858 /* Disable Acknowledge */
2859 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
2861 /* Wait until BUSY flag is reset */
2862 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_FLAG) != HAL_OK)
2867 hi2c->State = HAL_I2C_STATE_READY;
2869 HAL_I2C_SlaveRxCpltCallback(hi2c);
2875 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
2876 * the configuration information for I2C module
2877 * @retval HAL status
2879 static HAL_StatusTypeDef I2C_Slave_AF(I2C_HandleTypeDef *hi2c)
2881 /* Disable EVT, BUF and ERR interrupt */
2882 __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
2885 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
2887 /* Disable Acknowledge */
2888 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
2890 /* Wait until BUSY flag is reset */
2891 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_FLAG) != HAL_OK)
2896 hi2c->State = HAL_I2C_STATE_READY;
2898 HAL_I2C_SlaveTxCpltCallback(hi2c);
2904 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
2905 * the configuration information for I2C module
2906 * @param DevAddress: Target device address
2907 * @param Timeout: Timeout duration
2908 * @retval HAL status
2910 static HAL_StatusTypeDef I2C_MasterRequestWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Timeout)
2912 /* Generate Start */
2913 SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
2915 /* Wait until SB flag is set */
2916 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout) != HAL_OK)
2921 if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_7BIT)
2923 /* Send slave address */
2924 hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress);
2928 /* Send header of slave address */
2929 hi2c->Instance->DR = I2C_10BIT_HEADER_WRITE(DevAddress);
2931 /* Wait until ADD10 flag is set */
2932 if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADD10, Timeout) != HAL_OK)
2934 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
2944 /* Send slave address */
2945 hi2c->Instance->DR = I2C_10BIT_ADDRESS(DevAddress);
2948 /* Wait until ADDR flag is set */
2949 if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout) != HAL_OK)
2951 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
2965 * @brief Master sends target device address for read request.
2966 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
2967 * the configuration information for I2C module
2968 * @param DevAddress: Target device address
2969 * @param Timeout: Timeout duration
2970 * @retval HAL status
2972 static HAL_StatusTypeDef I2C_MasterRequestRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Timeout)
2974 /* Enable Acknowledge */
2975 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
2977 /* Generate Start */
2978 SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
2980 /* Wait until SB flag is set */
2981 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout) != HAL_OK)
2986 if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_7BIT)
2988 /* Send slave address */
2989 hi2c->Instance->DR = I2C_7BIT_ADD_READ(DevAddress);
2993 /* Send header of slave address */
2994 hi2c->Instance->DR = I2C_10BIT_HEADER_WRITE(DevAddress);
2996 /* Wait until ADD10 flag is set */
2997 if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADD10, Timeout) != HAL_OK)
2999 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3009 /* Send slave address */
3010 hi2c->Instance->DR = I2C_10BIT_ADDRESS(DevAddress);
3012 /* Wait until ADDR flag is set */
3013 if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout) != HAL_OK)
3015 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3025 /* Clear ADDR flag */
3026 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
3028 /* Generate Restart */
3029 SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
3031 /* Wait until SB flag is set */
3032 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout) != HAL_OK)
3037 /* Send header of slave address */
3038 hi2c->Instance->DR = I2C_10BIT_HEADER_READ(DevAddress);
3041 /* Wait until ADDR flag is set */
3042 if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout) != HAL_OK)
3044 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3058 * @brief Master sends target device address followed by internal memory address for write request.
3059 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
3060 * the configuration information for I2C module
3061 * @param DevAddress: Target device address
3062 * @param MemAddress: Internal memory address
3063 * @param MemAddSize: Size of internal memory address
3064 * @param Timeout: Timeout duration
3065 * @retval HAL status
3067 static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout)
3069 /* Generate Start */
3070 SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
3072 /* Wait until SB flag is set */
3073 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout) != HAL_OK)
3078 /* Send slave address */
3079 hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress);
3081 /* Wait until ADDR flag is set */
3082 if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout) != HAL_OK)
3084 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3094 /* Clear ADDR flag */
3095 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
3097 /* Wait until TXE flag is set */
3098 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TXE, RESET, Timeout) != HAL_OK)
3103 /* If Memory address size is 8Bit */
3104 if(MemAddSize == I2C_MEMADD_SIZE_8BIT)
3106 /* Send Memory Address */
3107 hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
3109 /* If Memory address size is 16Bit */
3112 /* Send MSB of Memory Address */
3113 hi2c->Instance->DR = I2C_MEM_ADD_MSB(MemAddress);
3115 /* Wait until TXE flag is set */
3116 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TXE, RESET, Timeout) != HAL_OK)
3121 /* Send LSB of Memory Address */
3122 hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
3129 * @brief Master sends target device address followed by internal memory address for read request.
3130 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
3131 * the configuration information for I2C module
3132 * @param DevAddress: Target device address
3133 * @param MemAddress: Internal memory address
3134 * @param MemAddSize: Size of internal memory address
3135 * @param Timeout: Timeout duration
3136 * @retval HAL status
3138 static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout)
3140 /* Enable Acknowledge */
3141 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
3143 /* Generate Start */
3144 SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
3146 /* Wait until SB flag is set */
3147 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout) != HAL_OK)
3152 /* Send slave address */
3153 hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress);
3155 /* Wait until ADDR flag is set */
3156 if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout) != HAL_OK)
3158 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3168 /* Clear ADDR flag */
3169 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
3171 /* Wait until TXE flag is set */
3172 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TXE, RESET, Timeout) != HAL_OK)
3177 /* If Memory address size is 8Bit */
3178 if(MemAddSize == I2C_MEMADD_SIZE_8BIT)
3180 /* Send Memory Address */
3181 hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
3183 /* If Memory address size is 16Bit */
3186 /* Send MSB of Memory Address */
3187 hi2c->Instance->DR = I2C_MEM_ADD_MSB(MemAddress);
3189 /* Wait until TXE flag is set */
3190 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TXE, RESET, Timeout) != HAL_OK)
3195 /* Send LSB of Memory Address */
3196 hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
3199 /* Wait until TXE flag is set */
3200 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TXE, RESET, Timeout) != HAL_OK)
3205 /* Generate Restart */
3206 SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
3208 /* Wait until SB flag is set */
3209 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout) != HAL_OK)
3214 /* Send slave address */
3215 hi2c->Instance->DR = I2C_7BIT_ADD_READ(DevAddress);
3217 /* Wait until ADDR flag is set */
3218 if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout) != HAL_OK)
3220 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3234 * @brief DMA I2C master transmit process complete callback.
3235 * @param hdma: DMA handle
3238 static void I2C_DMAMasterTransmitCplt(DMA_HandleTypeDef *hdma)
3240 I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3242 /* Wait until BTF flag is reset */
3243 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, I2C_TIMEOUT_FLAG) != HAL_OK)
3245 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3249 SET_BIT(hi2c->Instance->CR1,I2C_CR1_STOP);
3251 /* Disable DMA Request */
3252 CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
3254 hi2c->XferCount = 0;
3256 /* Wait until BUSY flag is reset */
3257 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_FLAG) != HAL_OK)
3259 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3262 hi2c->State = HAL_I2C_STATE_READY;
3264 /* Check if Errors has been detected during transfer */
3265 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3267 HAL_I2C_ErrorCallback(hi2c);
3271 HAL_I2C_MasterTxCpltCallback(hi2c);
3276 * @brief DMA I2C slave transmit process complete callback.
3277 * @param hdma: DMA handle
3280 static void I2C_DMASlaveTransmitCplt(DMA_HandleTypeDef *hdma)
3282 I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3284 /* Wait until AF flag is reset */
3285 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_AF, RESET, I2C_TIMEOUT_FLAG) != HAL_OK)
3287 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3291 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
3293 /* Disable Address Acknowledge */
3294 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
3296 /* Disable DMA Request */
3297 CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
3299 hi2c->XferCount = 0;
3301 /* Wait until BUSY flag is reset */
3302 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_FLAG) != HAL_OK)
3304 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3307 hi2c->State = HAL_I2C_STATE_READY;
3309 /* Check if Errors has been detected during transfer */
3310 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3312 HAL_I2C_ErrorCallback(hi2c);
3316 HAL_I2C_SlaveTxCpltCallback(hi2c);
3321 * @brief DMA I2C master receive process complete callback
3322 * @param hdma: DMA handle
3325 static void I2C_DMAMasterReceiveCplt(DMA_HandleTypeDef *hdma)
3327 I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3330 SET_BIT(hi2c->Instance->CR1,I2C_CR1_STOP);
3332 /* Disable Last DMA */
3333 CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
3335 /* Disable Acknowledge */
3336 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
3338 /* Disable DMA Request */
3339 CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
3341 hi2c->XferCount = 0;
3343 /* Wait until BUSY flag is reset */
3344 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_FLAG) != HAL_OK)
3346 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3349 hi2c->State = HAL_I2C_STATE_READY;
3351 /* Check if Errors has been detected during transfer */
3352 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3354 HAL_I2C_ErrorCallback(hi2c);
3358 HAL_I2C_MasterRxCpltCallback(hi2c);
3363 * @brief DMA I2C slave receive process complete callback.
3364 * @param hdma: DMA handle
3367 static void I2C_DMASlaveReceiveCplt(DMA_HandleTypeDef *hdma)
3369 I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3371 /* Wait until STOPF flag is reset */
3372 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, I2C_TIMEOUT_FLAG) != HAL_OK)
3374 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3377 /* Clear STOPF flag */
3378 __HAL_I2C_CLEAR_STOPFLAG(hi2c);
3380 /* Disable Address Acknowledge */
3381 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
3383 /* Disable DMA Request */
3384 CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
3386 hi2c->XferCount = 0;
3388 /* Wait until BUSY flag is reset */
3389 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_FLAG) != HAL_OK)
3391 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3394 hi2c->State = HAL_I2C_STATE_READY;
3396 /* Check if Errors has been detected during transfer */
3397 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3399 HAL_I2C_ErrorCallback(hi2c);
3403 HAL_I2C_SlaveRxCpltCallback(hi2c);
3408 * @brief DMA I2C Memory Write process complete callback
3409 * @param hdma: DMA handle
3412 static void I2C_DMAMemTransmitCplt(DMA_HandleTypeDef *hdma)
3414 I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3416 /* Wait until BTF flag is reset */
3417 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, I2C_TIMEOUT_FLAG) != HAL_OK)
3419 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3423 SET_BIT(hi2c->Instance->CR1,I2C_CR1_STOP);
3425 /* Disable DMA Request */
3426 CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
3428 hi2c->XferCount = 0;
3430 /* Wait until BUSY flag is reset */
3431 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_FLAG) != HAL_OK)
3433 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3436 hi2c->State = HAL_I2C_STATE_READY;
3438 /* Check if Errors has been detected during transfer */
3439 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3441 HAL_I2C_ErrorCallback(hi2c);
3445 HAL_I2C_MemTxCpltCallback(hi2c);
3450 * @brief DMA I2C Memory Read process complete callback
3451 * @param hdma: DMA handle
3454 static void I2C_DMAMemReceiveCplt(DMA_HandleTypeDef *hdma)
3456 I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3459 SET_BIT(hi2c->Instance->CR1,I2C_CR1_STOP);
3461 /* Disable Last DMA */
3462 CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
3464 /* Disable Acknowledge */
3465 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
3467 /* Disable DMA Request */
3468 CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
3470 hi2c->XferCount = 0;
3472 /* Wait until BUSY flag is reset */
3473 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_FLAG) != HAL_OK)
3475 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3478 hi2c->State = HAL_I2C_STATE_READY;
3480 /* Check if Errors has been detected during transfer */
3481 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3483 HAL_I2C_ErrorCallback(hi2c);
3487 HAL_I2C_MemRxCpltCallback(hi2c);
3492 * @brief DMA I2C communication error callback.
3493 * @param hdma: DMA handle
3496 static void I2C_DMAError(DMA_HandleTypeDef *hdma)
3498 I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3500 /* Disable Acknowledge */
3501 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
3503 hi2c->XferCount = 0;
3505 hi2c->State = HAL_I2C_STATE_READY;
3507 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
3509 HAL_I2C_ErrorCallback(hi2c);
3513 * @brief This function handles I2C Communication Timeout.
3514 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
3515 * the configuration information for I2C module
3516 * @param Flag: specifies the I2C flag to check.
3517 * @param Status: The new Flag status (SET or RESET).
3518 * @param Timeout: Timeout duration
3519 * @retval HAL status
3521 static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
3523 uint32_t tickstart = 0;
3526 tickstart = HAL_GetTick();
3528 /* Wait until flag is set */
3531 while(__HAL_I2C_GET_FLAG(hi2c, Flag) == RESET)
3533 /* Check for the Timeout */
3534 if(Timeout != HAL_MAX_DELAY)
3536 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
3538 hi2c->State= HAL_I2C_STATE_READY;
3540 /* Process Unlocked */
3550 while(__HAL_I2C_GET_FLAG(hi2c, Flag) != RESET)
3552 /* Check for the Timeout */
3553 if(Timeout != HAL_MAX_DELAY)
3555 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
3557 hi2c->State= HAL_I2C_STATE_READY;
3559 /* Process Unlocked */
3571 * @brief This function handles I2C Communication Timeout for Master addressing phase.
3572 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
3573 * the configuration information for I2C module
3574 * @param Flag: specifies the I2C flag to check.
3575 * @param Timeout: Timeout duration
3576 * @retval HAL status
3578 static HAL_StatusTypeDef I2C_WaitOnMasterAddressFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, uint32_t Timeout)
3580 uint32_t tickstart = 0;
3583 tickstart = HAL_GetTick();
3585 while(__HAL_I2C_GET_FLAG(hi2c, Flag) == RESET)
3587 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
3590 SET_BIT(hi2c->Instance->CR1,I2C_CR1_STOP);
3593 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
3595 hi2c->ErrorCode = HAL_I2C_ERROR_AF;
3596 hi2c->State= HAL_I2C_STATE_READY;
3598 /* Process Unlocked */
3604 /* Check for the Timeout */
3605 if(Timeout != HAL_MAX_DELAY)
3607 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
3609 hi2c->State= HAL_I2C_STATE_READY;
3611 /* Process Unlocked */
3625 #endif /* HAL_I2C_MODULE_ENABLED */
3635 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/