2 ******************************************************************************
3 * @file stm32f3xx_hal_i2c.c
4 * @author MCD Application Team
7 * @brief I2C HAL module driver.
9 * This file provides firmware functions to manage the following
10 * functionalities of the Inter Integrated Circuit (I2C) peripheral:
11 * + Initialization and de-initialization functions
12 * + IO operation functions
13 * + Peripheral State functions
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 the DMA Tx or Rx channel
41 (#) Configure the Communication Clock Timing, Own Address1, Master Adressing Mode, Dual Addressing mode,
42 Own Address2, Own Address2 Mask, General call and Nostretch mode in the hi2c Init structure.
44 (#) Initialize the I2C registers by calling the HAL_I2C_Init() API:
45 (+++) These API's configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
46 by calling the customed HAL_I2C_MspInit(&hi2c) API.
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 mode of operations 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 : Clears the specified I2C pending flag
145 (+) __HAL_I2C_ENABLE_IT: Enables the specified I2C interrupt
146 (+) __HAL_I2C_DISABLE_IT: Disables the specified I2C interrupt
149 (@) You can refer to the I2C HAL driver header file for more useful macros
152 ******************************************************************************
155 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
157 * Redistribution and use in source and binary forms, with or without modification,
158 * are permitted provided that the following conditions are met:
159 * 1. Redistributions of source code must retain the above copyright notice,
160 * this list of conditions and the following disclaimer.
161 * 2. Redistributions in binary form must reproduce the above copyright notice,
162 * this list of conditions and the following disclaimer in the documentation
163 * and/or other materials provided with the distribution.
164 * 3. Neither the name of STMicroelectronics nor the names of its contributors
165 * may be used to endorse or promote products derived from this software
166 * without specific prior written permission.
168 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
169 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
170 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
171 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
172 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
173 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
174 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
175 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
176 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
177 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
179 ******************************************************************************
182 /* Includes ------------------------------------------------------------------*/
183 #include "stm32f3xx_hal.h"
185 /** @addtogroup STM32F3xx_HAL_Driver
189 /** @defgroup I2C I2C HAL module driver
190 * @brief I2C HAL module driver
194 #ifdef HAL_I2C_MODULE_ENABLED
196 /* Private typedef -----------------------------------------------------------*/
197 /* Private define ------------------------------------------------------------*/
199 /** @defgroup I2C_Private_Define I2C Private Define
202 #define TIMING_CLEAR_MASK ((uint32_t)0xF0FFFFFF) /*<! I2C TIMING clear register Mask */
203 #define I2C_TIMEOUT_ADDR ((uint32_t)10000) /* 10 s */
204 #define I2C_TIMEOUT_BUSY ((uint32_t)25) /* 25 ms */
205 #define I2C_TIMEOUT_DIR ((uint32_t)25) /* 25 ms */
206 #define I2C_TIMEOUT_RXNE ((uint32_t)25) /* 25 ms */
207 #define I2C_TIMEOUT_STOPF ((uint32_t)25) /* 25 ms */
208 #define I2C_TIMEOUT_TC ((uint32_t)25) /* 25 ms */
209 #define I2C_TIMEOUT_TCR ((uint32_t)25) /* 25 ms */
210 #define I2C_TIMEOUT_TXIS ((uint32_t)25) /* 25 ms */
211 #define I2C_TIMEOUT_FLAG ((uint32_t)25) /* 25 ms */
216 /* Private macro -------------------------------------------------------------*/
217 /* Private variables ---------------------------------------------------------*/
218 /* Private function prototypes -----------------------------------------------*/
220 /** @defgroup I2C_Private_Functions I2C Private Functions
223 static void I2C_DMAMasterTransmitCplt(DMA_HandleTypeDef *hdma);
224 static void I2C_DMAMasterReceiveCplt(DMA_HandleTypeDef *hdma);
225 static void I2C_DMASlaveTransmitCplt(DMA_HandleTypeDef *hdma);
226 static void I2C_DMASlaveReceiveCplt(DMA_HandleTypeDef *hdma);
227 static void I2C_DMAMemTransmitCplt(DMA_HandleTypeDef *hdma);
228 static void I2C_DMAMemReceiveCplt(DMA_HandleTypeDef *hdma);
229 static void I2C_DMAError(DMA_HandleTypeDef *hdma);
231 static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout);
232 static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout);
233 static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
234 static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout);
235 static HAL_StatusTypeDef I2C_WaitOnTXISFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout);
236 static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout);
237 static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout);
238 static HAL_StatusTypeDef I2C_IsAcknowledgeFailed(I2C_HandleTypeDef *hi2c, uint32_t Timeout);
240 static HAL_StatusTypeDef I2C_MasterTransmit_ISR(I2C_HandleTypeDef *hi2c);
241 static HAL_StatusTypeDef I2C_MasterReceive_ISR(I2C_HandleTypeDef *hi2c);
243 static HAL_StatusTypeDef I2C_SlaveTransmit_ISR(I2C_HandleTypeDef *hi2c);
244 static HAL_StatusTypeDef I2C_SlaveReceive_ISR(I2C_HandleTypeDef *hi2c);
246 static void I2C_TransferConfig(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t Size, uint32_t Mode, uint32_t Request);
251 /* Exported functions ---------------------------------------------------------*/
253 /** @defgroup I2C_Exported_Functions I2C Exported Functions
257 /** @defgroup I2C_Exported_Functions_Group1 Initialization and de-initialization functions
258 * @brief Initialization and Configuration functions
261 ===============================================================================
262 ##### Initialization/de-initialization functions #####
263 ===============================================================================
264 [..] This subsection provides a set of functions allowing to initialize and
265 de-initialiaze the I2Cx peripheral:
267 (+) User must Implement HAL_I2C_MspInit() function in which he configures
268 all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
270 (+) Call the function HAL_I2C_Init() to configure the selected device with
271 the selected configuration:
274 (++) Addressing mode (Master, Slave)
275 (++) Dual Addressing mode
277 (++) Own Address 2 Mask
278 (++) General call mode
281 (+) Call the function HAL_I2C_DeInit() to restore the default configuration
282 of the selected I2Cx periperal.
289 * @brief Initializes the I2C according to the specified parameters
290 * in the I2C_InitTypeDef and create the associated handle.
291 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
292 * the configuration information for the specified I2C.
295 HAL_StatusTypeDef HAL_I2C_Init(I2C_HandleTypeDef *hi2c)
297 /* Check the I2C handle allocation */
303 /* Check the parameters */
304 assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
305 assert_param(IS_I2C_OWN_ADDRESS1(hi2c->Init.OwnAddress1));
306 assert_param(IS_I2C_ADDRESSING_MODE(hi2c->Init.AddressingMode));
307 assert_param(IS_I2C_DUAL_ADDRESS(hi2c->Init.DualAddressMode));
308 assert_param(IS_I2C_OWN_ADDRESS2(hi2c->Init.OwnAddress2));
309 assert_param(IS_I2C_OWN_ADDRESS2_MASK(hi2c->Init.OwnAddress2Masks));
310 assert_param(IS_I2C_GENERAL_CALL(hi2c->Init.GeneralCallMode));
311 assert_param(IS_I2C_NO_STRETCH(hi2c->Init.NoStretchMode));
313 if(hi2c->State == HAL_I2C_STATE_RESET)
315 /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
316 HAL_I2C_MspInit(hi2c);
319 hi2c->State = HAL_I2C_STATE_BUSY;
321 /* Disable the selected I2C peripheral */
322 __HAL_I2C_DISABLE(hi2c);
324 /*---------------------------- I2Cx TIMINGR Configuration ------------------*/
325 /* Configure I2Cx: Frequency range */
326 hi2c->Instance->TIMINGR = hi2c->Init.Timing & TIMING_CLEAR_MASK;
328 /*---------------------------- I2Cx OAR1 Configuration ---------------------*/
329 /* Configure I2Cx: Own Address1 and ack own address1 mode */
330 hi2c->Instance->OAR1 &= ~I2C_OAR1_OA1EN;
331 if(hi2c->Init.OwnAddress1 != 0)
333 if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_7BIT)
335 hi2c->Instance->OAR1 = (I2C_OAR1_OA1EN | hi2c->Init.OwnAddress1);
337 else /* I2C_ADDRESSINGMODE_10BIT */
339 hi2c->Instance->OAR1 = (I2C_OAR1_OA1EN | I2C_OAR1_OA1MODE | hi2c->Init.OwnAddress1);
343 /*---------------------------- I2Cx CR2 Configuration ----------------------*/
344 /* Configure I2Cx: Addressing Master mode */
345 if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT)
347 hi2c->Instance->CR2 = (I2C_CR2_ADD10);
349 /* Enable the AUTOEND by default, and enable NACK (should be disable only during Slave process */
350 hi2c->Instance->CR2 |= (I2C_CR2_AUTOEND | I2C_CR2_NACK);
352 /*---------------------------- I2Cx OAR2 Configuration ---------------------*/
353 /* Configure I2Cx: Dual mode and Own Address2 */
354 hi2c->Instance->OAR2 = (hi2c->Init.DualAddressMode | hi2c->Init.OwnAddress2 | (hi2c->Init.OwnAddress2Masks << 8));
356 /*---------------------------- I2Cx CR1 Configuration ----------------------*/
357 /* Configure I2Cx: Generalcall and NoStretch mode */
358 hi2c->Instance->CR1 = (hi2c->Init.GeneralCallMode | hi2c->Init.NoStretchMode);
360 /* Enable the selected I2C peripheral */
361 __HAL_I2C_ENABLE(hi2c);
363 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
364 hi2c->State = HAL_I2C_STATE_READY;
370 * @brief DeInitializes the I2C peripheral.
371 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
372 * the configuration information for the specified I2C.
375 HAL_StatusTypeDef HAL_I2C_DeInit(I2C_HandleTypeDef *hi2c)
377 /* Check the I2C handle allocation */
383 /* Check the parameters */
384 assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
386 hi2c->State = HAL_I2C_STATE_BUSY;
388 /* Disable the I2C Peripheral Clock */
389 __HAL_I2C_DISABLE(hi2c);
391 /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
392 HAL_I2C_MspDeInit(hi2c);
394 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
395 hi2c->State = HAL_I2C_STATE_RESET;
404 * @brief I2C MSP Init.
405 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
406 * the configuration information for the specified I2C.
409 __weak void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)
411 /* NOTE : This function Should not be modified, when the callback is needed,
412 the HAL_I2C_MspInit could be implemented in the user file
417 * @brief I2C MSP DeInit
418 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
419 * the configuration information for the specified I2C.
422 __weak void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c)
424 /* NOTE : This function Should not be modified, when the callback is needed,
425 the HAL_I2C_MspDeInit could be implemented in the user file
433 /** @defgroup I2C_Exported_Functions_Group2 Input and Output operation functions
434 * @brief Data transfers functions
437 ===============================================================================
438 ##### IO operation functions #####
439 ===============================================================================
441 This subsection provides a set of functions allowing to manage the I2C data
444 (#) There is two mode of transfer:
445 (++) Blocking mode : The communication is performed in the polling mode.
446 The status of all data processing is returned by the same function
447 after finishing transfer.
448 (++) No-Blocking mode : The communication is performed using Interrupts
449 or DMA. These functions return the status of the transfer startup.
450 The end of the data processing will be indicated through the
451 dedicated I2C IRQ when using Interrupt mode or the DMA IRQ when
454 (#) Blocking mode functions are :
455 (++) HAL_I2C_Master_Transmit()
456 (++) HAL_I2C_Master_Receive()
457 (++) HAL_I2C_Slave_Transmit()
458 (++) HAL_I2C_Slave_Receive()
459 (++) HAL_I2C_Mem_Write()
460 (++) HAL_I2C_Mem_Read()
461 (++) HAL_I2C_IsDeviceReady()
463 (#) No-Blocking mode functions with Interrupt are :
464 (++) HAL_I2C_Master_Transmit_IT()
465 (++) HAL_I2C_Master_Receive_IT()
466 (++) HAL_I2C_Slave_Transmit_IT()
467 (++) HAL_I2C_Slave_Receive_IT()
468 (++) HAL_I2C_Mem_Write_IT()
469 (++) HAL_I2C_Mem_Read_IT()
471 (#) No-Blocking mode functions with DMA are :
472 (++) HAL_I2C_Master_Transmit_DMA()
473 (++) HAL_I2C_Master_Receive_DMA()
474 (++) HAL_I2C_Slave_Transmit_DMA()
475 (++) HAL_I2C_Slave_Receive_DMA()
476 (++) HAL_I2C_Mem_Write_DMA()
477 (++) HAL_I2C_Mem_Read_DMA()
479 (#) A set of Transfer Complete Callbacks are provided in No_Blocking mode:
480 (++) HAL_I2C_MemTxCpltCallback()
481 (++) HAL_I2C_MemRxCpltCallback()
482 (++) HAL_I2C_MasterTxCpltCallback()
483 (++) HAL_I2C_MasterRxCpltCallback()
484 (++) HAL_I2C_SlaveTxCpltCallback()
485 (++) HAL_I2C_SlaveRxCpltCallback()
486 (++) HAL_I2C_ErrorCallback()
492 /** @defgroup Blocking_mode_Polling Blocking mode Polling
497 * @brief Transmits in master mode an amount of data in blocking mode.
498 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
499 * the configuration information for the specified I2C.
500 * @param DevAddress: Target device address
501 * @param pData: Pointer to data buffer
502 * @param Size: Amount of data to be sent
503 * @param Timeout: Timeout duration
506 HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
508 uint32_t sizetmp = 0;
510 if(hi2c->State == HAL_I2C_STATE_READY)
512 if((pData == HAL_NULL ) || (Size == 0))
517 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
525 hi2c->State = HAL_I2C_STATE_MASTER_BUSY_TX;
526 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
528 /* Send Slave Address */
529 /* Set NBYTES to write and reload if size > 255 and generate RESTART */
530 /* Size > 255, need to set RELOAD bit */
533 I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);
538 I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_GENERATE_START_WRITE);
544 /* Wait until TXIS flag is set */
545 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
547 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
556 /* Write data to TXDR */
557 hi2c->Instance->TXDR = (*pData++);
561 if((sizetmp == 0)&&(Size!=0))
563 /* Wait until TXE flag is set */
564 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout) != HAL_OK)
571 I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
576 I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
583 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
584 /* Wait until STOPF flag is set */
585 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
587 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
597 /* Clear STOP Flag */
598 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
600 /* Clear Configuration Register 2 */
601 __HAL_I2C_RESET_CR2(hi2c);
603 hi2c->State = HAL_I2C_STATE_READY;
605 /* Process Unlocked */
617 * @brief Receives in master mode an amount of data in blocking mode.
618 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
619 * the configuration information for the specified I2C.
620 * @param DevAddress: Target device address
621 * @param pData: Pointer to data buffer
622 * @param Size: Amount of data to be sent
623 * @param Timeout: Timeout duration
626 HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
628 uint32_t sizetmp = 0;
630 if(hi2c->State == HAL_I2C_STATE_READY)
632 if((pData == HAL_NULL ) || (Size == 0))
637 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
645 hi2c->State = HAL_I2C_STATE_MASTER_BUSY_RX;
646 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
648 /* Send Slave Address */
649 /* Set NBYTES to write and reload if size > 255 and generate RESTART */
650 /* Size > 255, need to set RELOAD bit */
653 I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
658 I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
664 /* Wait until RXNE flag is set */
665 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, Timeout) != HAL_OK)
670 /* Write data to RXDR */
671 (*pData++) =hi2c->Instance->RXDR;
675 if((sizetmp == 0)&&(Size!=0))
677 /* Wait until TCR flag is set */
678 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout) != HAL_OK)
685 I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
690 I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
697 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
698 /* Wait until STOPF flag is set */
699 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
701 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
711 /* Clear STOP Flag */
712 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
714 /* Clear Configuration Register 2 */
715 __HAL_I2C_RESET_CR2(hi2c);
717 hi2c->State = HAL_I2C_STATE_READY;
719 /* Process Unlocked */
731 * @brief Transmits in slave mode an amount of data in blocking mode.
732 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
733 * the configuration information for the specified I2C.
734 * @param pData: Pointer to data buffer
735 * @param Size: Amount of data to be sent
736 * @param Timeout: Timeout duration
739 HAL_StatusTypeDef HAL_I2C_Slave_Transmit(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
741 if(hi2c->State == HAL_I2C_STATE_READY)
743 if((pData == HAL_NULL ) || (Size == 0))
751 hi2c->State = HAL_I2C_STATE_SLAVE_BUSY_RX;
752 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
754 /* Enable Address Acknowledge */
755 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
757 /* Wait until ADDR flag is set */
758 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout) != HAL_OK)
760 /* Disable Address Acknowledge */
761 hi2c->Instance->CR2 |= I2C_CR2_NACK;
765 /* Clear ADDR flag */
766 __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
768 /* If 10bit addressing mode is selected */
769 if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT)
771 /* Wait until ADDR flag is set */
772 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout) != HAL_OK)
774 /* Disable Address Acknowledge */
775 hi2c->Instance->CR2 |= I2C_CR2_NACK;
779 /* Clear ADDR flag */
780 __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
783 /* Wait until DIR flag is set Transmitter mode */
784 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_DIR, RESET, Timeout) != HAL_OK)
786 /* Disable Address Acknowledge */
787 hi2c->Instance->CR2 |= I2C_CR2_NACK;
793 /* Wait until TXIS flag is set */
794 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
796 /* Disable Address Acknowledge */
797 hi2c->Instance->CR2 |= I2C_CR2_NACK;
799 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
809 /* Read data from TXDR */
810 hi2c->Instance->TXDR = (*pData++);
814 /* Wait until STOP flag is set */
815 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
817 /* Disable Address Acknowledge */
818 hi2c->Instance->CR2 |= I2C_CR2_NACK;
820 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
822 /* Normal use case for Transmitter mode */
823 /* A NACK is generated to confirm the end of transfer */
824 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
832 /* Clear STOP flag */
833 __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_STOPF);
835 /* Wait until BUSY flag is reset */
836 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout) != HAL_OK)
838 /* Disable Address Acknowledge */
839 hi2c->Instance->CR2 |= I2C_CR2_NACK;
843 /* Disable Address Acknowledge */
844 hi2c->Instance->CR2 |= I2C_CR2_NACK;
846 hi2c->State = HAL_I2C_STATE_READY;
848 /* Process Unlocked */
860 * @brief Receive in slave mode an amount of data in blocking mode
861 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
862 * the configuration information for the specified I2C.
863 * @param pData: Pointer to data buffer
864 * @param Size: Amount of data to be sent
865 * @param Timeout: Timeout duration
868 HAL_StatusTypeDef HAL_I2C_Slave_Receive(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
870 if(hi2c->State == HAL_I2C_STATE_READY)
872 if((pData == HAL_NULL ) || (Size == 0))
880 hi2c->State = HAL_I2C_STATE_SLAVE_BUSY_RX;
881 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
883 /* Enable Address Acknowledge */
884 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
886 /* Wait until ADDR flag is set */
887 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout) != HAL_OK)
889 /* Disable Address Acknowledge */
890 hi2c->Instance->CR2 |= I2C_CR2_NACK;
894 /* Clear ADDR flag */
895 __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
897 /* Wait until DIR flag is reset Receiver mode */
898 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_DIR, SET, Timeout) != HAL_OK)
900 /* Disable Address Acknowledge */
901 hi2c->Instance->CR2 |= I2C_CR2_NACK;
907 /* Wait until RXNE flag is set */
908 if(I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
910 /* Disable Address Acknowledge */
911 hi2c->Instance->CR2 |= I2C_CR2_NACK;
912 if(hi2c->ErrorCode == HAL_I2C_ERROR_TIMEOUT)
922 /* Read data from RXDR */
923 (*pData++) = hi2c->Instance->RXDR;
927 /* Wait until STOP flag is set */
928 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
930 /* Disable Address Acknowledge */
931 hi2c->Instance->CR2 |= I2C_CR2_NACK;
933 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
943 /* Clear STOP flag */
944 __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_STOPF);
946 /* Wait until BUSY flag is reset */
947 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout) != HAL_OK)
949 /* Disable Address Acknowledge */
950 hi2c->Instance->CR2 |= I2C_CR2_NACK;
955 /* Disable Address Acknowledge */
956 hi2c->Instance->CR2 |= I2C_CR2_NACK;
958 hi2c->State = HAL_I2C_STATE_READY;
960 /* Process Unlocked */
974 /** @addtogroup Non_Blocking_mode_Interrupt Non Blocking mode Interrupt
979 * @brief Transmit in master mode an amount of data in no-blocking mode with Interrupt
980 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
981 * the configuration information for the specified I2C.
982 * @param DevAddress: Target device address
983 * @param pData: Pointer to data buffer
984 * @param Size: Amount of data to be sent
987 HAL_StatusTypeDef HAL_I2C_Master_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
989 if(hi2c->State == HAL_I2C_STATE_READY)
991 if((pData == HAL_NULL) || (Size == 0))
996 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1001 /* Process Locked */
1004 hi2c->State = HAL_I2C_STATE_MASTER_BUSY_TX;
1005 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1007 hi2c->pBuffPtr = pData;
1008 hi2c->XferCount = Size;
1011 hi2c->XferSize = 255;
1015 hi2c->XferSize = Size;
1018 /* Send Slave Address */
1019 /* Set NBYTES to write and reload if size > 255 and generate RESTART */
1020 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
1022 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);
1026 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_WRITE);
1029 /* Process Unlocked */
1032 /* Note : The I2C interrupts must be enabled after unlocking current process
1033 to avoid the risk of I2C interrupt handle execution before current
1037 /* Enable ERR, TC, STOP, NACK, TXI interrupt */
1038 /* possible to enable all of these */
1039 /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
1040 __HAL_I2C_ENABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_TXI );
1051 * @brief Receive in master mode an amount of data in no-blocking mode with Interrupt
1052 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1053 * the configuration information for the specified I2C.
1054 * @param DevAddress: Target device address
1055 * @param pData: Pointer to data buffer
1056 * @param Size: Amount of data to be sent
1057 * @retval HAL status
1059 HAL_StatusTypeDef HAL_I2C_Master_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
1061 if(hi2c->State == HAL_I2C_STATE_READY)
1063 if((pData == HAL_NULL) || (Size == 0))
1068 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1073 /* Process Locked */
1076 hi2c->State = HAL_I2C_STATE_MASTER_BUSY_RX;
1077 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1079 hi2c->pBuffPtr = pData;
1080 hi2c->XferCount = Size;
1083 hi2c->XferSize = 255;
1087 hi2c->XferSize = Size;
1090 /* Send Slave Address */
1091 /* Set NBYTES to write and reload if size > 255 and generate RESTART */
1092 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
1094 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
1098 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
1101 /* Process Unlocked */
1104 /* Note : The I2C interrupts must be enabled after unlocking current process
1105 to avoid the risk of I2C interrupt handle execution before current
1108 /* Enable ERR, TC, STOP, NACK, RXI interrupt */
1109 /* possible to enable all of these */
1110 /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
1111 __HAL_I2C_ENABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_RXI );
1122 * @brief Transmit in slave mode an amount of data in no-blocking mode with Interrupt
1123 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1124 * the configuration information for the specified I2C.
1125 * @param pData: Pointer to data buffer
1126 * @param Size: Amount of data to be sent
1127 * @retval HAL status
1129 HAL_StatusTypeDef HAL_I2C_Slave_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
1131 if(hi2c->State == HAL_I2C_STATE_READY)
1133 if((pData == HAL_NULL) || (Size == 0))
1138 /* Process Locked */
1141 hi2c->State = HAL_I2C_STATE_SLAVE_BUSY_TX;
1142 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1144 /* Enable Address Acknowledge */
1145 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
1147 hi2c->pBuffPtr = pData;
1148 hi2c->XferSize = Size;
1149 hi2c->XferCount = Size;
1151 /* Process Unlocked */
1154 /* Note : The I2C interrupts must be enabled after unlocking current process
1155 to avoid the risk of I2C interrupt handle execution before current
1158 /* Enable ERR, TC, STOP, NACK, TXI interrupt */
1159 /* possible to enable all of these */
1160 /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
1161 __HAL_I2C_ENABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_TXI );
1172 * @brief Receive in slave mode an amount of data in no-blocking mode with Interrupt
1173 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1174 * the configuration information for the specified I2C.
1175 * @param pData: Pointer to data buffer
1176 * @param Size: Amount of data to be sent
1177 * @retval HAL status
1179 HAL_StatusTypeDef HAL_I2C_Slave_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
1181 if(hi2c->State == HAL_I2C_STATE_READY)
1183 if((pData == HAL_NULL) || (Size == 0))
1188 /* Process Locked */
1191 hi2c->State = HAL_I2C_STATE_SLAVE_BUSY_RX;
1192 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1194 /* Enable Address Acknowledge */
1195 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
1197 hi2c->pBuffPtr = pData;
1198 hi2c->XferSize = Size;
1199 hi2c->XferCount = Size;
1201 /* Process Unlocked */
1204 /* Note : The I2C interrupts must be enabled after unlocking current process
1205 to avoid the risk of I2C interrupt handle execution before current
1208 /* Enable ERR, TC, STOP, NACK, RXI interrupt */
1209 /* possible to enable all of these */
1210 /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
1211 __HAL_I2C_ENABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI);
1225 /** @addtogroup Non_Blocking_mode_DMA Non Blocking mode DMA
1230 * @brief Transmit in master mode an amount of data in no-blocking mode with DMA
1231 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1232 * the configuration information for the specified I2C.
1233 * @param DevAddress: Target device address
1234 * @param pData: Pointer to data buffer
1235 * @param Size: Amount of data to be sent
1236 * @retval HAL status
1238 HAL_StatusTypeDef HAL_I2C_Master_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
1240 if(hi2c->State == HAL_I2C_STATE_READY)
1242 if((pData == HAL_NULL) || (Size == 0))
1247 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1252 /* Process Locked */
1255 hi2c->State = HAL_I2C_STATE_MASTER_BUSY_TX;
1256 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1258 hi2c->pBuffPtr = pData;
1259 hi2c->XferCount = Size;
1262 hi2c->XferSize = 255;
1266 hi2c->XferSize = Size;
1269 /* Set the I2C DMA transfer complete callback */
1270 hi2c->hdmatx->XferCpltCallback = I2C_DMAMasterTransmitCplt;
1272 /* Set the DMA error callback */
1273 hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
1275 /* Enable the DMA channel */
1276 HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
1278 /* Send Slave Address */
1279 /* Set NBYTES to write and reload if size > 255 and generate RESTART */
1280 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
1282 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);
1286 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_WRITE);
1289 /* Wait until TXIS flag is set */
1290 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, I2C_TIMEOUT_TXIS) != HAL_OK)
1292 /* Disable Address Acknowledge */
1293 hi2c->Instance->CR2 |= I2C_CR2_NACK;
1295 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1306 /* Enable DMA Request */
1307 hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
1309 /* Process Unlocked */
1321 * @brief Receive in master mode an amount of data in no-blocking mode with DMA
1322 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1323 * the configuration information for the specified I2C.
1324 * @param DevAddress: Target device address
1325 * @param pData: Pointer to data buffer
1326 * @param Size: Amount of data to be sent
1327 * @retval HAL status
1329 HAL_StatusTypeDef HAL_I2C_Master_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
1331 if(hi2c->State == HAL_I2C_STATE_READY)
1333 if((pData == HAL_NULL) || (Size == 0))
1338 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1343 /* Process Locked */
1346 hi2c->State = HAL_I2C_STATE_MASTER_BUSY_RX;
1347 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1349 hi2c->pBuffPtr = pData;
1350 hi2c->XferCount = Size;
1353 hi2c->XferSize = 255;
1357 hi2c->XferSize = Size;
1360 /* Set the I2C DMA transfer complete callback */
1361 hi2c->hdmarx->XferCpltCallback = I2C_DMAMasterReceiveCplt;
1363 /* Set the DMA error callback */
1364 hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
1366 /* Enable the DMA channel */
1367 HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData, hi2c->XferSize);
1369 /* Send Slave Address */
1370 /* Set NBYTES to write and reload if size > 255 and generate RESTART */
1371 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
1373 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
1377 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
1380 /* Wait until RXNE flag is set */
1381 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, I2C_TIMEOUT_RXNE) != HAL_OK)
1387 /* Enable DMA Request */
1388 hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
1390 /* Process Unlocked */
1402 * @brief Transmit in slave mode an amount of data in no-blocking mode with DMA
1403 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1404 * the configuration information for the specified I2C.
1405 * @param pData: Pointer to data buffer
1406 * @param Size: Amount of data to be sent
1407 * @retval HAL status
1409 HAL_StatusTypeDef HAL_I2C_Slave_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
1411 if(hi2c->State == HAL_I2C_STATE_READY)
1413 if((pData == HAL_NULL) || (Size == 0))
1417 /* Process Locked */
1420 hi2c->State = HAL_I2C_STATE_SLAVE_BUSY_TX;
1421 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1423 hi2c->pBuffPtr = pData;
1424 hi2c->XferCount = Size;
1425 hi2c->XferSize = Size;
1427 /* Set the I2C DMA transfer complete callback */
1428 hi2c->hdmatx->XferCpltCallback = I2C_DMASlaveTransmitCplt;
1430 /* Set the DMA error callback */
1431 hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
1433 /* Enable the DMA channel */
1434 HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
1436 /* Enable Address Acknowledge */
1437 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
1439 /* Wait until ADDR flag is set */
1440 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, I2C_TIMEOUT_ADDR) != HAL_OK)
1442 /* Disable Address Acknowledge */
1443 hi2c->Instance->CR2 |= I2C_CR2_NACK;
1447 /* Clear ADDR flag */
1448 __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
1450 /* If 10bits addressing mode is selected */
1451 if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT)
1453 /* Wait until ADDR flag is set */
1454 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, I2C_TIMEOUT_ADDR) != HAL_OK)
1456 /* Disable Address Acknowledge */
1457 hi2c->Instance->CR2 |= I2C_CR2_NACK;
1461 /* Clear ADDR flag */
1462 __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
1465 /* Wait until DIR flag is set Transmitter mode */
1466 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_DIR, RESET, I2C_TIMEOUT_BUSY) != HAL_OK)
1468 /* Disable Address Acknowledge */
1469 hi2c->Instance->CR2 |= I2C_CR2_NACK;
1473 /* Enable DMA Request */
1474 hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
1476 /* Process Unlocked */
1488 * @brief Receive in slave mode an amount of data in no-blocking mode with DMA
1489 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1490 * the configuration information for the specified I2C.
1491 * @param pData: Pointer to data buffer
1492 * @param Size: Amount of data to be sent
1493 * @retval HAL status
1495 HAL_StatusTypeDef HAL_I2C_Slave_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
1497 if(hi2c->State == HAL_I2C_STATE_READY)
1499 if((pData == HAL_NULL) || (Size == 0))
1503 /* Process Locked */
1506 hi2c->State = HAL_I2C_STATE_SLAVE_BUSY_RX;
1507 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1509 hi2c->pBuffPtr = pData;
1510 hi2c->XferSize = Size;
1511 hi2c->XferCount = Size;
1513 /* Set the I2C DMA transfer complete callback */
1514 hi2c->hdmarx->XferCpltCallback = I2C_DMASlaveReceiveCplt;
1516 /* Set the DMA error callback */
1517 hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
1519 /* Enable the DMA channel */
1520 HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData, Size);
1522 /* Enable Address Acknowledge */
1523 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
1525 /* Wait until ADDR flag is set */
1526 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, I2C_TIMEOUT_ADDR) != HAL_OK)
1528 /* Disable Address Acknowledge */
1529 hi2c->Instance->CR2 |= I2C_CR2_NACK;
1533 /* Clear ADDR flag */
1534 __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
1536 /* Wait until DIR flag is set Receiver mode */
1537 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_DIR, SET, I2C_TIMEOUT_DIR) != HAL_OK)
1539 /* Disable Address Acknowledge */
1540 hi2c->Instance->CR2 |= I2C_CR2_NACK;
1544 /* Enable DMA Request */
1545 hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
1547 /* Process Unlocked */
1562 /** @addtogroup Blocking_mode_Polling Blocking mode Polling
1567 * @brief Write an amount of data in blocking mode to a specific memory address
1568 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1569 * the configuration information for the specified I2C.
1570 * @param DevAddress: Target device address
1571 * @param MemAddress: Internal memory address
1572 * @param MemAddSize: Size of internal memory address
1573 * @param pData: Pointer to data buffer
1574 * @param Size: Amount of data to be sent
1575 * @param Timeout: Timeout duration
1576 * @retval HAL status
1578 HAL_StatusTypeDef HAL_I2C_Mem_Write(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
1580 uint32_t Sizetmp = 0;
1582 /* Check the parameters */
1583 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
1585 if(hi2c->State == HAL_I2C_STATE_READY)
1587 if((pData == HAL_NULL) || (Size == 0))
1592 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1597 /* Process Locked */
1600 hi2c->State = HAL_I2C_STATE_MEM_BUSY_TX;
1601 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1603 /* Send Slave Address and Memory Address */
1604 if(I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, Timeout) != HAL_OK)
1606 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1608 /* Process Unlocked */
1614 /* Process Unlocked */
1620 /* Set NBYTES to write and reload if size > 255 */
1621 /* Size > 255, need to set RELOAD bit */
1624 I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
1629 I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
1635 /* Wait until TXIS flag is set */
1636 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
1638 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1648 /* Write data to DR */
1649 hi2c->Instance->TXDR = (*pData++);
1653 if((Sizetmp == 0)&&(Size!=0))
1655 /* Wait until TCR flag is set */
1656 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout) != HAL_OK)
1664 I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
1669 I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
1676 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
1677 /* Wait until STOPF flag is reset */
1678 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
1680 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1690 /* Clear STOP Flag */
1691 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
1693 /* Clear Configuration Register 2 */
1694 __HAL_I2C_RESET_CR2(hi2c);
1696 hi2c->State = HAL_I2C_STATE_READY;
1698 /* Process Unlocked */
1710 * @brief Read an amount of data in blocking mode from a specific memory address
1711 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1712 * the configuration information for the specified I2C.
1713 * @param DevAddress: Target device address
1714 * @param MemAddress: Internal memory address
1715 * @param MemAddSize: Size of internal memory address
1716 * @param pData: Pointer to data buffer
1717 * @param Size: Amount of data to be sent
1718 * @param Timeout: Timeout duration
1719 * @retval HAL status
1721 HAL_StatusTypeDef HAL_I2C_Mem_Read(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
1723 uint32_t Sizetmp = 0;
1725 /* Check the parameters */
1726 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
1728 if(hi2c->State == HAL_I2C_STATE_READY)
1730 if((pData == HAL_NULL) || (Size == 0))
1735 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1740 /* Process Locked */
1743 hi2c->State = HAL_I2C_STATE_MEM_BUSY_RX;
1744 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1746 /* Send Slave Address and Memory Address */
1747 if(I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, Timeout) != HAL_OK)
1749 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1751 /* Process Unlocked */
1757 /* Process Unlocked */
1763 /* Send Slave Address */
1764 /* Set NBYTES to write and reload if size > 255 and generate RESTART */
1765 /* Size > 255, need to set RELOAD bit */
1768 I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
1773 I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
1779 /* Wait until RXNE flag is set */
1780 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, Timeout) != HAL_OK)
1785 /* Read data from RXDR */
1786 (*pData++) = hi2c->Instance->RXDR;
1788 /* Decrement the Size counter */
1792 if((Sizetmp == 0)&&(Size!=0))
1794 /* Wait until TCR flag is set */
1795 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout) != HAL_OK)
1802 I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
1807 I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
1814 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
1815 /* Wait until STOPF flag is reset */
1816 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
1818 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1828 /* Clear STOP Flag */
1829 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
1831 /* Clear Configuration Register 2 */
1832 __HAL_I2C_RESET_CR2(hi2c);
1834 hi2c->State = HAL_I2C_STATE_READY;
1836 /* Process Unlocked */
1850 /** @addtogroup Non_Blocking_mode_Interrupt Non Blocking mode Interrupt
1855 * @brief Write an amount of data in no-blocking mode with Interrupt to a specific memory address
1856 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1857 * the configuration information for the specified I2C.
1858 * @param DevAddress: Target device address
1859 * @param MemAddress: Internal memory address
1860 * @param MemAddSize: Size of internal memory address
1861 * @param pData: Pointer to data buffer
1862 * @param Size: Amount of data to be sent
1863 * @retval HAL status
1865 HAL_StatusTypeDef HAL_I2C_Mem_Write_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
1867 /* Check the parameters */
1868 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
1870 if(hi2c->State == HAL_I2C_STATE_READY)
1872 if((pData == HAL_NULL) || (Size == 0))
1877 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1882 /* Process Locked */
1885 hi2c->State = HAL_I2C_STATE_MEM_BUSY_TX;
1886 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1888 hi2c->pBuffPtr = pData;
1889 hi2c->XferCount = Size;
1892 hi2c->XferSize = 255;
1896 hi2c->XferSize = Size;
1899 /* Send Slave Address and Memory Address */
1900 if(I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG) != HAL_OK)
1902 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1904 /* Process Unlocked */
1910 /* Process Unlocked */
1916 /* Set NBYTES to write and reload if size > 255 */
1917 /* Size > 255, need to set RELOAD bit */
1918 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
1920 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
1924 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
1927 /* Process Unlocked */
1930 /* Note : The I2C interrupts must be enabled after unlocking current process
1931 to avoid the risk of I2C interrupt handle execution before current
1934 /* Enable ERR, TC, STOP, NACK, TXI interrupt */
1935 /* possible to enable all of these */
1936 /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
1937 __HAL_I2C_ENABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_TXI );
1948 * @brief Read an amount of data in no-blocking mode with Interrupt from a specific memory address
1949 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1950 * the configuration information for the specified I2C.
1951 * @param DevAddress: Target device address
1952 * @param MemAddress: Internal memory address
1953 * @param MemAddSize: Size of internal memory address
1954 * @param pData: Pointer to data buffer
1955 * @param Size: Amount of data to be sent
1956 * @retval HAL status
1958 HAL_StatusTypeDef HAL_I2C_Mem_Read_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
1960 /* Check the parameters */
1961 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
1963 if(hi2c->State == HAL_I2C_STATE_READY)
1965 if((pData == HAL_NULL) || (Size == 0))
1970 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1975 /* Process Locked */
1978 hi2c->State = HAL_I2C_STATE_MEM_BUSY_RX;
1980 hi2c->pBuffPtr = pData;
1981 hi2c->XferCount = Size;
1984 hi2c->XferSize = 255;
1988 hi2c->XferSize = Size;
1991 /* Send Slave Address and Memory Address */
1992 if(I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG) != HAL_OK)
1994 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1996 /* Process Unlocked */
2002 /* Process Unlocked */
2008 /* Set NBYTES to write and reload if size > 255 and generate RESTART */
2009 /* Size > 255, need to set RELOAD bit */
2010 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
2012 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
2016 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
2019 /* Process Unlocked */
2022 /* Note : The I2C interrupts must be enabled after unlocking current process
2023 to avoid the risk of I2C interrupt handle execution before current
2026 /* Enable ERR, TC, STOP, NACK, RXI interrupt */
2027 /* possible to enable all of these */
2028 /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
2029 __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_RXI );
2043 /** @addtogroup Non_Blocking_mode_DMA Non Blocking mode DMA
2048 * @brief Write an amount of data in no-blocking mode with DMA to a specific memory address
2049 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2050 * the configuration information for the specified I2C.
2051 * @param DevAddress: Target device address
2052 * @param MemAddress: Internal memory address
2053 * @param MemAddSize: Size of internal memory address
2054 * @param pData: Pointer to data buffer
2055 * @param Size: Amount of data to be sent
2056 * @retval HAL status
2058 HAL_StatusTypeDef HAL_I2C_Mem_Write_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
2060 /* Check the parameters */
2061 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
2063 if(hi2c->State == HAL_I2C_STATE_READY)
2065 if((pData == HAL_NULL) || (Size == 0))
2070 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
2075 /* Process Locked */
2078 hi2c->State = HAL_I2C_STATE_MEM_BUSY_TX;
2079 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
2081 hi2c->pBuffPtr = pData;
2082 hi2c->XferCount = Size;
2085 hi2c->XferSize = 255;
2089 hi2c->XferSize = Size;
2092 /* Set the I2C DMA transfer complete callback */
2093 hi2c->hdmatx->XferCpltCallback = I2C_DMAMemTransmitCplt;
2095 /* Set the DMA error callback */
2096 hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
2098 /* Enable the DMA channel */
2099 HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
2101 /* Send Slave Address and Memory Address */
2102 if(I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG) != HAL_OK)
2104 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
2106 /* Process Unlocked */
2112 /* Process Unlocked */
2118 /* Send Slave Address */
2119 /* Set NBYTES to write and reload if size > 255 */
2120 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
2122 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
2126 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
2129 /* Wait until TXIS flag is set */
2130 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, I2C_TIMEOUT_TXIS) != HAL_OK)
2132 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
2142 /* Enable DMA Request */
2143 hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
2145 /* Process Unlocked */
2157 * @brief Reads an amount of data in no-blocking mode with DMA from a specific memory address.
2158 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2159 * the configuration information for the specified I2C.
2160 * @param DevAddress: Target device address
2161 * @param MemAddress: Internal memory address
2162 * @param MemAddSize: Size of internal memory address
2163 * @param pData: Pointer to data buffer
2164 * @param Size: Amount of data to be read
2165 * @retval HAL status
2167 HAL_StatusTypeDef HAL_I2C_Mem_Read_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
2169 /* Check the parameters */
2170 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
2172 if(hi2c->State == HAL_I2C_STATE_READY)
2174 if((pData == HAL_NULL) || (Size == 0))
2179 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
2184 /* Process Locked */
2187 hi2c->State = HAL_I2C_STATE_MEM_BUSY_RX;
2189 hi2c->pBuffPtr = pData;
2190 hi2c->XferCount = Size;
2193 hi2c->XferSize = 255;
2197 hi2c->XferSize = Size;
2200 /* Set the I2C DMA transfer complete callback */
2201 hi2c->hdmarx->XferCpltCallback = I2C_DMAMemReceiveCplt;
2203 /* Set the DMA error callback */
2204 hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
2206 /* Enable the DMA channel */
2207 HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData, hi2c->XferSize);
2209 /* Send Slave Address and Memory Address */
2210 if(I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG) != HAL_OK)
2212 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
2214 /* Process Unlocked */
2220 /* Process Unlocked */
2226 /* Set NBYTES to write and reload if size > 255 and generate RESTART */
2227 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
2229 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
2233 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
2236 /* Wait until RXNE flag is set */
2237 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, I2C_TIMEOUT_RXNE) != HAL_OK)
2242 /* Enable DMA Request */
2243 hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
2245 /* Process Unlocked */
2259 /** @addtogroup Blocking_mode_Polling Blocking mode Polling
2264 * @brief Checks if target device is ready for communication.
2265 * @note This function is used with Memory devices
2266 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2267 * the configuration information for the specified I2C.
2268 * @param DevAddress: Target device address
2269 * @param Trials: Number of trials
2270 * @param Timeout: Timeout duration
2271 * @retval HAL status
2273 HAL_StatusTypeDef HAL_I2C_IsDeviceReady(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout)
2275 uint32_t tickstart = 0;
2277 __IO uint32_t I2C_Trials = 0;
2279 if(hi2c->State == HAL_I2C_STATE_READY)
2281 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
2286 /* Process Locked */
2289 hi2c->State = HAL_I2C_STATE_BUSY;
2290 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
2294 /* Generate Start */
2295 hi2c->Instance->CR2 = __HAL_I2C_GENERATE_START(hi2c->Init.AddressingMode,DevAddress);
2297 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
2298 /* Wait until STOPF flag is set or a NACK flag is set*/
2299 tickstart = HAL_GetTick();
2300 while((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET) && (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == RESET) && (hi2c->State != HAL_I2C_STATE_TIMEOUT))
2302 if(Timeout != HAL_MAX_DELAY)
2304 if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
2306 /* Device is ready */
2307 hi2c->State = HAL_I2C_STATE_READY;
2308 /* Process Unlocked */
2315 /* Check if the NACKF flag has not been set */
2316 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == RESET)
2318 /* Wait until STOPF flag is reset */
2319 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout) != HAL_OK)
2324 /* Clear STOP Flag */
2325 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
2327 /* Device is ready */
2328 hi2c->State = HAL_I2C_STATE_READY;
2330 /* Process Unlocked */
2337 /* Wait until STOPF flag is reset */
2338 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout) != HAL_OK)
2343 /* Clear NACK Flag */
2344 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
2346 /* Clear STOP Flag, auto generated with autoend*/
2347 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
2350 /* Check if the maximum allowed number of trials has been reached */
2351 if (I2C_Trials++ == Trials)
2354 hi2c->Instance->CR2 |= I2C_CR2_STOP;
2356 /* Wait until STOPF flag is reset */
2357 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout) != HAL_OK)
2362 /* Clear STOP Flag */
2363 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
2365 }while(I2C_Trials < Trials);
2367 hi2c->State = HAL_I2C_STATE_READY;
2369 /* Process Unlocked */
2383 /** @defgroup IRQ_Handler_and_Callbacks IRQ Handler and Callbacks
2388 * @brief This function handles I2C event interrupt request.
2389 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2390 * the configuration information for the specified I2C.
2393 void HAL_I2C_EV_IRQHandler(I2C_HandleTypeDef *hi2c)
2395 /* I2C in mode Transmitter ---------------------------------------------------*/
2396 if (((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TCR) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TC) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == SET)) && (__HAL_I2C_GET_IT_SOURCE(hi2c, (I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_TXI | I2C_IT_ADDRI)) == SET))
2398 /* Slave mode selected */
2399 if (hi2c->State == HAL_I2C_STATE_SLAVE_BUSY_TX)
2401 I2C_SlaveTransmit_ISR(hi2c);
2405 if (((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TCR) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TC) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)) && (__HAL_I2C_GET_IT_SOURCE(hi2c, (I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_TXI)) == SET))
2407 /* Master mode selected */
2408 if ((hi2c->State == HAL_I2C_STATE_MASTER_BUSY_TX) || (hi2c->State == HAL_I2C_STATE_MEM_BUSY_TX))
2410 I2C_MasterTransmit_ISR(hi2c);
2414 /* I2C in mode Receiver ----------------------------------------------------*/
2415 if (((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TCR) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TC) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == SET)) && (__HAL_I2C_GET_IT_SOURCE(hi2c, (I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_RXI | I2C_IT_ADDRI)) == SET))
2417 /* Slave mode selected */
2418 if (hi2c->State == HAL_I2C_STATE_SLAVE_BUSY_RX)
2420 I2C_SlaveReceive_ISR(hi2c);
2423 if (((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TCR) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TC) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)) && (__HAL_I2C_GET_IT_SOURCE(hi2c, (I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_RXI)) == SET))
2425 /* Master mode selected */
2426 if ((hi2c->State == HAL_I2C_STATE_MASTER_BUSY_RX) || (hi2c->State == HAL_I2C_STATE_MEM_BUSY_RX))
2428 I2C_MasterReceive_ISR(hi2c);
2434 * @brief This function handles I2C error interrupt request.
2435 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2436 * the configuration information for the specified I2C.
2439 void HAL_I2C_ER_IRQHandler(I2C_HandleTypeDef *hi2c)
2441 /* I2C Bus error interrupt occurred ------------------------------------*/
2442 if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BERR) == SET) && (__HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_ERRI) == SET))
2444 hi2c->ErrorCode |= HAL_I2C_ERROR_BERR;
2446 /* Clear BERR flag */
2447 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_BERR);
2450 /* I2C Over-Run/Under-Run interrupt occurred ----------------------------------------*/
2451 if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_OVR) == SET) && (__HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_ERRI) == SET))
2453 hi2c->ErrorCode |= HAL_I2C_ERROR_OVR;
2455 /* Clear OVR flag */
2456 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_OVR);
2459 /* I2C Arbitration Loss error interrupt occurred -------------------------------------*/
2460 if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ARLO) == SET) && (__HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_ERRI) == SET))
2462 hi2c->ErrorCode |= HAL_I2C_ERROR_ARLO;
2464 /* Clear ARLO flag */
2465 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ARLO);
2468 /* Call the Error Callback in case of Error detected */
2469 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
2471 hi2c->State = HAL_I2C_STATE_READY;
2473 HAL_I2C_ErrorCallback(hi2c);
2478 * @brief Master Tx Transfer completed callbacks.
2479 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2480 * the configuration information for the specified I2C.
2483 __weak void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c)
2485 /* NOTE : This function Should not be modified, when the callback is needed,
2486 the HAL_I2C_TxCpltCallback could be implemented in the user file
2491 * @brief Master Rx Transfer completed callbacks.
2492 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2493 * the configuration information for the specified I2C.
2496 __weak void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c)
2498 /* NOTE : This function Should not be modified, when the callback is needed,
2499 the HAL_I2C_TxCpltCallback could be implemented in the user file
2503 /** @brief Slave Tx Transfer completed callbacks.
2504 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2505 * the configuration information for the specified I2C.
2508 __weak void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c)
2510 /* NOTE : This function Should not be modified, when the callback is needed,
2511 the HAL_I2C_TxCpltCallback could be implemented in the user file
2516 * @brief Slave Rx Transfer completed callbacks.
2517 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2518 * the configuration information for the specified I2C.
2521 __weak void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c)
2523 /* NOTE : This function Should not be modified, when the callback is needed,
2524 the HAL_I2C_TxCpltCallback could be implemented in the user file
2529 * @brief Memory Tx Transfer completed callbacks.
2530 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2531 * the configuration information for the specified I2C.
2534 __weak void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef *hi2c)
2536 /* NOTE : This function Should not be modified, when the callback is needed,
2537 the HAL_I2C_TxCpltCallback could be implemented in the user file
2542 * @brief Memory Rx Transfer completed callbacks.
2543 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2544 * the configuration information for the specified I2C.
2547 __weak void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c)
2549 /* NOTE : This function Should not be modified, when the callback is needed,
2550 the HAL_I2C_TxCpltCallback could be implemented in the user file
2555 * @brief I2C error callbacks.
2556 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2557 * the configuration information for the specified I2C.
2560 __weak void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
2562 /* NOTE : This function Should not be modified, when the callback is needed,
2563 the HAL_I2C_ErrorCallback could be implemented in the user file
2575 /** @defgroup I2C_Exported_Functions_Group3 Peripheral State and Errors functions
2576 * @brief Peripheral State and Errors functions
2579 ===============================================================================
2580 ##### Peripheral State and Errors functions #####
2581 ===============================================================================
2583 This subsection permit to get in run-time the status of the peripheral
2591 * @brief Returns the I2C state.
2592 * @param hi2c : I2C handle
2595 HAL_I2C_StateTypeDef HAL_I2C_GetState(I2C_HandleTypeDef *hi2c)
2601 * @brief Return the I2C error code
2602 * @param hi2c : pointer to a I2C_HandleTypeDef structure that contains
2603 * the configuration information for the specified I2C.
2604 * @retval I2C Error Code
2606 uint32_t HAL_I2C_GetError(I2C_HandleTypeDef *hi2c)
2608 return hi2c->ErrorCode;
2619 /** @addtogroup I2C_Private_Functions
2624 * @brief Handle Interrupt Flags Master Transmit Mode
2625 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2626 * the configuration information for the specified I2C.
2627 * @retval HAL status
2629 static HAL_StatusTypeDef I2C_MasterTransmit_ISR(I2C_HandleTypeDef *hi2c)
2631 uint16_t DevAddress;
2633 /* Process Locked */
2636 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == SET)
2638 /* Write data to TXDR */
2639 hi2c->Instance->TXDR = (*hi2c->pBuffPtr++);
2643 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TCR) == SET)
2645 if((hi2c->XferSize == 0)&&(hi2c->XferCount!=0))
2647 DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);
2649 if(hi2c->XferCount > 255)
2651 I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
2652 hi2c->XferSize = 255;
2656 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferCount, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
2657 hi2c->XferSize = hi2c->XferCount;
2662 /* Process Unlocked */
2665 /* Wrong size Status regarding TCR flag event */
2666 hi2c->ErrorCode |= HAL_I2C_ERROR_SIZE;
2667 HAL_I2C_ErrorCallback(hi2c);
2670 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TC) == SET)
2672 if(hi2c->XferCount == 0)
2675 hi2c->Instance->CR2 |= I2C_CR2_STOP;
2679 /* Process Unlocked */
2682 /* Wrong size Status regarding TCR flag event */
2683 hi2c->ErrorCode |= HAL_I2C_ERROR_SIZE;
2684 HAL_I2C_ErrorCallback(hi2c);
2687 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
2689 /* Disable ERR, TC, STOP, NACK, TXI interrupt */
2690 __HAL_I2C_DISABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_TXI );
2692 /* Clear STOP Flag */
2693 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
2695 /* Clear Configuration Register 2 */
2696 __HAL_I2C_RESET_CR2(hi2c);
2698 hi2c->State = HAL_I2C_STATE_READY;
2700 /* Process Unlocked */
2703 if(hi2c->State == HAL_I2C_STATE_MEM_BUSY_TX)
2705 HAL_I2C_MemTxCpltCallback(hi2c);
2709 HAL_I2C_MasterTxCpltCallback(hi2c);
2712 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
2714 /* Clear NACK Flag */
2715 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
2717 /* Process Unlocked */
2720 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
2721 HAL_I2C_ErrorCallback(hi2c);
2724 /* Process Unlocked */
2731 * @brief Handle Interrupt Flags Master Receive Mode
2732 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2733 * the configuration information for the specified I2C.
2734 * @retval HAL status
2736 static HAL_StatusTypeDef I2C_MasterReceive_ISR(I2C_HandleTypeDef *hi2c)
2738 uint16_t DevAddress;
2740 /* Process Locked */
2743 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
2745 /* Read data from RXDR */
2746 (*hi2c->pBuffPtr++) = hi2c->Instance->RXDR;
2750 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TCR) == SET)
2752 if((hi2c->XferSize == 0)&&(hi2c->XferCount!=0))
2754 DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);
2756 if(hi2c->XferCount > 255)
2758 I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
2759 hi2c->XferSize = 255;
2763 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferCount, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
2764 hi2c->XferSize = hi2c->XferCount;
2769 /* Process Unlocked */
2772 /* Wrong size Status regarding TCR flag event */
2773 hi2c->ErrorCode |= HAL_I2C_ERROR_SIZE;
2774 HAL_I2C_ErrorCallback(hi2c);
2777 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TC) == SET)
2779 if(hi2c->XferCount == 0)
2782 hi2c->Instance->CR2 |= I2C_CR2_STOP;
2786 /* Process Unlocked */
2789 /* Wrong size Status regarding TCR flag event */
2790 hi2c->ErrorCode |= HAL_I2C_ERROR_SIZE;
2791 HAL_I2C_ErrorCallback(hi2c);
2794 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
2796 /* Disable ERR, TC, STOP, NACK, TXI interrupt */
2797 __HAL_I2C_DISABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_RXI );
2799 /* Clear STOP Flag */
2800 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
2802 /* Clear Configuration Register 2 */
2803 __HAL_I2C_RESET_CR2(hi2c);
2805 hi2c->State = HAL_I2C_STATE_READY;
2807 /* Process Unlocked */
2810 if(hi2c->State == HAL_I2C_STATE_MEM_BUSY_RX)
2812 HAL_I2C_MemRxCpltCallback(hi2c);
2816 HAL_I2C_MasterRxCpltCallback(hi2c);
2819 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
2821 /* Clear NACK Flag */
2822 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
2824 /* Process Unlocked */
2827 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
2828 HAL_I2C_ErrorCallback(hi2c);
2831 /* Process Unlocked */
2839 * @brief Handle Interrupt Flags Slave Transmit Mode
2840 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2841 * the configuration information for the specified I2C.
2842 * @retval HAL status
2844 static HAL_StatusTypeDef I2C_SlaveTransmit_ISR(I2C_HandleTypeDef *hi2c)
2846 /* Process locked */
2849 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) != RESET)
2851 /* Check that I2C transfer finished */
2852 /* if yes, normal usecase, a NACK is sent by the MASTER when Transfer is finished */
2853 /* Mean XferCount == 0*/
2854 /* So clear Flag NACKF only */
2855 if(hi2c->XferCount == 0)
2857 /* Clear NACK Flag */
2858 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
2860 /* Process Unlocked */
2865 /* if no, error usecase, a Non-Acknowledge of last Data is generated by the MASTER*/
2866 /* Clear NACK Flag */
2867 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
2869 /* Set ErrorCode corresponding to a Non-Acknowledge */
2870 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
2872 /* Process Unlocked */
2875 /* Call the Error callback to prevent upper layer */
2876 HAL_I2C_ErrorCallback(hi2c);
2879 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == SET)
2881 /* Clear ADDR flag */
2882 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
2884 /* Check first if STOPF is set */
2885 /* to prevent a Write Data in TX buffer */
2886 /* which is stuck in TXDR until next */
2887 /* communication with Master */
2888 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
2890 /* Disable ERRI, TCI, STOPI, NACKI, ADDRI, RXI, TXI interrupt */
2891 __HAL_I2C_DISABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI );
2893 /* Disable Address Acknowledge */
2894 hi2c->Instance->CR2 |= I2C_CR2_NACK;
2896 /* Clear STOP Flag */
2897 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
2899 hi2c->State = HAL_I2C_STATE_READY;
2901 /* Process Unlocked */
2904 HAL_I2C_SlaveTxCpltCallback(hi2c);
2906 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == SET)
2908 /* Write data to TXDR only if XferCount not reach "0" */
2909 /* A TXIS flag can be set, during STOP treatment */
2910 if(hi2c->XferCount > 0)
2912 /* Write data to TXDR */
2913 hi2c->Instance->TXDR = (*hi2c->pBuffPtr++);
2918 /* Process Unlocked */
2925 * @brief Handle Interrupt Flags Slave Receive Mode
2926 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2927 * the configuration information for the specified I2C.
2928 * @retval HAL status
2930 static HAL_StatusTypeDef I2C_SlaveReceive_ISR(I2C_HandleTypeDef *hi2c)
2932 /* Process Locked */
2935 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) != RESET)
2937 /* Clear NACK Flag */
2938 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
2940 /* Process Unlocked */
2943 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
2944 HAL_I2C_ErrorCallback(hi2c);
2946 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == SET)
2948 /* Clear ADDR flag */
2949 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
2951 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
2953 /* Read data from RXDR */
2954 (*hi2c->pBuffPtr++) = hi2c->Instance->RXDR;
2958 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
2960 /* Disable ERRI, TCI, STOPI, NACKI, ADDRI, RXI, TXI interrupt */
2961 __HAL_I2C_DISABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_RXI );
2963 /* Disable Address Acknowledge */
2964 hi2c->Instance->CR2 |= I2C_CR2_NACK;
2966 /* Clear STOP Flag */
2967 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
2969 hi2c->State = HAL_I2C_STATE_READY;
2971 /* Process Unlocked */
2974 HAL_I2C_SlaveRxCpltCallback(hi2c);
2977 /* Process Unlocked */
2984 * @brief Master sends target device address followed by internal memory address for write request.
2985 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2986 * the configuration information for the specified I2C.
2987 * @param DevAddress: Target device address
2988 * @param MemAddress: Internal memory address
2989 * @param MemAddSize: Size of internal memory address
2990 * @param Timeout: Timeout duration
2991 * @retval HAL status
2993 static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout)
2995 I2C_TransferConfig(hi2c,DevAddress,MemAddSize, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);
2997 /* Wait until TXIS flag is set */
2998 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
3000 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3010 /* If Memory address size is 8Bit */
3011 if(MemAddSize == I2C_MEMADD_SIZE_8BIT)
3013 /* Send Memory Address */
3014 hi2c->Instance->TXDR = __HAL_I2C_MEM_ADD_LSB(MemAddress);
3016 /* If Memory address size is 16Bit */
3019 /* Send MSB of Memory Address */
3020 hi2c->Instance->TXDR = __HAL_I2C_MEM_ADD_MSB(MemAddress);
3022 /* Wait until TXIS flag is set */
3023 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
3025 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3035 /* Send LSB of Memory Address */
3036 hi2c->Instance->TXDR = __HAL_I2C_MEM_ADD_LSB(MemAddress);
3039 /* Wait until TCR flag is set */
3040 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout) != HAL_OK)
3049 * @brief Master sends target device address followed by internal memory address for read request.
3050 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
3051 * the configuration information for the specified I2C.
3052 * @param DevAddress: Target device address
3053 * @param MemAddress: Internal memory address
3054 * @param MemAddSize: Size of internal memory address
3055 * @param Timeout: Timeout duration
3056 * @retval HAL status
3058 static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout)
3060 I2C_TransferConfig(hi2c,DevAddress,MemAddSize, I2C_SOFTEND_MODE, I2C_GENERATE_START_WRITE);
3062 /* Wait until TXIS flag is set */
3063 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
3065 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3075 /* If Memory address size is 8Bit */
3076 if(MemAddSize == I2C_MEMADD_SIZE_8BIT)
3078 /* Send Memory Address */
3079 hi2c->Instance->TXDR = __HAL_I2C_MEM_ADD_LSB(MemAddress);
3081 /* If Mememory address size is 16Bit */
3084 /* Send MSB of Memory Address */
3085 hi2c->Instance->TXDR = __HAL_I2C_MEM_ADD_MSB(MemAddress);
3087 /* Wait until TXIS flag is set */
3088 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
3090 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3100 /* Send LSB of Memory Address */
3101 hi2c->Instance->TXDR = __HAL_I2C_MEM_ADD_LSB(MemAddress);
3104 /* Wait until TC flag is set */
3105 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TC, RESET, Timeout) != HAL_OK)
3114 * @brief DMA I2C master transmit process complete callback.
3115 * @param hdma: DMA handle
3118 static void I2C_DMAMasterTransmitCplt(DMA_HandleTypeDef *hdma)
3120 uint16_t DevAddress;
3121 I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3123 /* Check if last DMA request was done with RELOAD */
3124 /* Set NBYTES to write and reload if size > 255 */
3125 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
3127 /* Wait until TCR flag is set */
3128 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, I2C_TIMEOUT_TCR) != HAL_OK)
3130 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3133 /* Disable DMA Request */
3134 hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
3136 /* Check if Errors has been detected during transfer */
3137 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3139 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3140 /* Wait until STOPF flag is reset */
3141 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3143 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3145 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3149 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3153 /* Clear STOP Flag */
3154 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3156 /* Clear Configuration Register 2 */
3157 __HAL_I2C_RESET_CR2(hi2c);
3159 hi2c->XferCount = 0;
3161 hi2c->State = HAL_I2C_STATE_READY;
3162 HAL_I2C_ErrorCallback(hi2c);
3166 hi2c->pBuffPtr += hi2c->XferSize;
3167 hi2c->XferCount -= hi2c->XferSize;
3168 if(hi2c->XferCount > 255)
3170 hi2c->XferSize = 255;
3174 hi2c->XferSize = hi2c->XferCount;
3177 DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);
3179 /* Enable the DMA channel */
3180 HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
3182 /* Send Slave Address */
3183 /* Set NBYTES to write and reload if size > 255 */
3184 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
3186 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
3190 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
3193 /* Wait until TXIS flag is set */
3194 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, I2C_TIMEOUT_TXIS) != HAL_OK)
3196 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3197 /* Wait until STOPF flag is reset */
3198 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3200 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3202 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3206 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3210 /* Clear STOP Flag */
3211 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3213 /* Clear Configuration Register 2 */
3214 __HAL_I2C_RESET_CR2(hi2c);
3216 hi2c->XferCount = 0;
3218 hi2c->State = HAL_I2C_STATE_READY;
3219 HAL_I2C_ErrorCallback(hi2c);
3223 /* Enable DMA Request */
3224 hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
3230 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3231 /* Wait until STOPF flag is reset */
3232 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3234 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3236 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3240 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3244 /* Clear STOP Flag */
3245 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3247 /* Clear Configuration Register 2 */
3248 __HAL_I2C_RESET_CR2(hi2c);
3250 /* Disable DMA Request */
3251 hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
3253 hi2c->XferCount = 0;
3255 hi2c->State = HAL_I2C_STATE_READY;
3257 /* Check if Errors has been detected during transfer */
3258 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3260 HAL_I2C_ErrorCallback(hi2c);
3264 HAL_I2C_MasterTxCpltCallback(hi2c);
3270 * @brief DMA I2C slave transmit process complete callback.
3271 * @param hdma: DMA handle
3274 static void I2C_DMASlaveTransmitCplt(DMA_HandleTypeDef *hdma)
3276 I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3278 /* Wait until STOP flag is set */
3279 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3281 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3283 /* Normal Use case, a AF is generated by master */
3284 /* to inform slave the end of transfer */
3285 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
3289 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3293 /* Clear STOP flag */
3294 __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_STOPF);
3296 /* Wait until BUSY flag is reset */
3297 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY) != HAL_OK)
3299 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3302 /* Disable DMA Request */
3303 hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
3305 hi2c->XferCount = 0;
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;
3328 uint16_t DevAddress;
3330 /* Check if last DMA request was done with RELOAD */
3331 /* Set NBYTES to write and reload if size > 255 */
3332 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
3334 /* Wait until TCR flag is set */
3335 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, I2C_TIMEOUT_TCR) != HAL_OK)
3337 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3340 /* Disable DMA Request */
3341 hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
3343 /* Check if Errors has been detected during transfer */
3344 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3346 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3347 /* Wait until STOPF flag is reset */
3348 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3350 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3352 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3356 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3360 /* Clear STOP Flag */
3361 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3363 /* Clear Configuration Register 2 */
3364 __HAL_I2C_RESET_CR2(hi2c);
3366 hi2c->XferCount = 0;
3368 hi2c->State = HAL_I2C_STATE_READY;
3369 HAL_I2C_ErrorCallback(hi2c);
3373 hi2c->pBuffPtr += hi2c->XferSize;
3374 hi2c->XferCount -= hi2c->XferSize;
3375 if(hi2c->XferCount > 255)
3377 hi2c->XferSize = 255;
3381 hi2c->XferSize = hi2c->XferCount;
3384 DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);
3386 /* Enable the DMA channel */
3387 HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);
3389 /* Send Slave Address */
3390 /* Set NBYTES to write and reload if size > 255 */
3391 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
3393 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
3397 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
3400 /* Wait until RXNE flag is set */
3401 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, I2C_TIMEOUT_RXNE) != HAL_OK)
3403 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3406 /* Check if Errors has been detected during transfer */
3407 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3409 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3410 /* Wait until STOPF flag is reset */
3411 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3413 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3415 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3419 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3423 /* Clear STOP Flag */
3424 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3426 /* Clear Configuration Register 2 */
3427 __HAL_I2C_RESET_CR2(hi2c);
3429 hi2c->XferCount = 0;
3431 hi2c->State = HAL_I2C_STATE_READY;
3433 HAL_I2C_ErrorCallback(hi2c);
3437 /* Enable DMA Request */
3438 hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
3444 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3445 /* Wait until STOPF flag is reset */
3446 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3448 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3450 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3454 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3458 /* Clear STOP Flag */
3459 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3461 /* Clear Configuration Register 2 */
3462 __HAL_I2C_RESET_CR2(hi2c);
3464 /* Disable DMA Request */
3465 hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
3467 hi2c->XferCount = 0;
3469 hi2c->State = HAL_I2C_STATE_READY;
3471 /* Check if Errors has been detected during transfer */
3472 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3474 HAL_I2C_ErrorCallback(hi2c);
3478 HAL_I2C_MasterRxCpltCallback(hi2c);
3484 * @brief DMA I2C slave receive process complete callback.
3485 * @param hdma: DMA handle
3488 static void I2C_DMASlaveReceiveCplt(DMA_HandleTypeDef *hdma)
3490 I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3492 /* Wait until STOPF flag is reset */
3493 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3495 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3497 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3501 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3505 /* Clear STOPF flag */
3506 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3508 /* Wait until BUSY flag is reset */
3509 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY) != HAL_OK)
3511 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3514 /* Disable DMA Request */
3515 hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
3517 /* Disable Address Acknowledge */
3518 hi2c->Instance->CR2 |= I2C_CR2_NACK;
3520 hi2c->XferCount = 0;
3522 hi2c->State = HAL_I2C_STATE_READY;
3524 /* Check if Errors has been detected during transfer */
3525 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3527 HAL_I2C_ErrorCallback(hi2c);
3531 HAL_I2C_SlaveRxCpltCallback(hi2c);
3536 * @brief DMA I2C Memory Write process complete callback
3537 * @param hdma : DMA handle
3540 static void I2C_DMAMemTransmitCplt(DMA_HandleTypeDef *hdma)
3542 uint16_t DevAddress;
3543 I2C_HandleTypeDef* hi2c = ( I2C_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
3545 /* Check if last DMA request was done with RELOAD */
3546 /* Set NBYTES to write and reload if size > 255 */
3547 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
3549 /* Wait until TCR flag is set */
3550 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, I2C_TIMEOUT_TCR) != HAL_OK)
3552 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3555 /* Disable DMA Request */
3556 hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
3558 /* Check if Errors has been detected during transfer */
3559 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3561 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3562 /* Wait until STOPF flag is reset */
3563 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3565 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3567 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3571 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3575 /* Clear STOP Flag */
3576 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3578 /* Clear Configuration Register 2 */
3579 __HAL_I2C_RESET_CR2(hi2c);
3581 hi2c->XferCount = 0;
3583 hi2c->State = HAL_I2C_STATE_READY;
3584 HAL_I2C_ErrorCallback(hi2c);
3588 hi2c->pBuffPtr += hi2c->XferSize;
3589 hi2c->XferCount -= hi2c->XferSize;
3590 if(hi2c->XferCount > 255)
3592 hi2c->XferSize = 255;
3596 hi2c->XferSize = hi2c->XferCount;
3599 DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);
3601 /* Enable the DMA channel */
3602 HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
3604 /* Send Slave Address */
3605 /* Set NBYTES to write and reload if size > 255 */
3606 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
3608 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
3612 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
3615 /* Wait until TXIS flag is set */
3616 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, I2C_TIMEOUT_TXIS) != HAL_OK)
3618 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3619 /* Wait until STOPF flag is reset */
3620 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3622 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3624 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3628 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3632 /* Clear STOP Flag */
3633 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3635 /* Clear Configuration Register 2 */
3636 __HAL_I2C_RESET_CR2(hi2c);
3638 hi2c->XferCount = 0;
3640 hi2c->State = HAL_I2C_STATE_READY;
3641 HAL_I2C_ErrorCallback(hi2c);
3645 /* Enable DMA Request */
3646 hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
3652 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3653 /* Wait until STOPF flag is reset */
3654 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3656 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3658 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3662 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3666 /* Clear STOP Flag */
3667 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3669 /* Clear Configuration Register 2 */
3670 __HAL_I2C_RESET_CR2(hi2c);
3672 /* Disable DMA Request */
3673 hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
3675 hi2c->XferCount = 0;
3677 hi2c->State = HAL_I2C_STATE_READY;
3679 /* Check if Errors has been detected during transfer */
3680 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3682 HAL_I2C_ErrorCallback(hi2c);
3686 HAL_I2C_MemTxCpltCallback(hi2c);
3692 * @brief DMA I2C Memory Read process complete callback
3693 * @param hdma: DMA handle
3696 static void I2C_DMAMemReceiveCplt(DMA_HandleTypeDef *hdma)
3698 I2C_HandleTypeDef* hi2c = ( I2C_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
3699 uint16_t DevAddress;
3701 /* Check if last DMA request was done with RELOAD */
3702 /* Set NBYTES to write and reload if size > 255 */
3703 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
3705 /* Wait until TCR flag is set */
3706 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, I2C_TIMEOUT_TCR) != HAL_OK)
3708 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3711 /* Disable DMA Request */
3712 hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
3714 /* Check if Errors has been detected during transfer */
3715 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3717 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3718 /* Wait until STOPF flag is reset */
3719 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3721 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3723 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3727 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3731 /* Clear STOP Flag */
3732 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3734 /* Clear Configuration Register 2 */
3735 __HAL_I2C_RESET_CR2(hi2c);
3737 hi2c->XferCount = 0;
3739 hi2c->State = HAL_I2C_STATE_READY;
3740 HAL_I2C_ErrorCallback(hi2c);
3744 hi2c->pBuffPtr += hi2c->XferSize;
3745 hi2c->XferCount -= hi2c->XferSize;
3746 if(hi2c->XferCount > 255)
3748 hi2c->XferSize = 255;
3752 hi2c->XferSize = hi2c->XferCount;
3755 DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);
3757 /* Enable the DMA channel */
3758 HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);
3760 /* Send Slave Address */
3761 /* Set NBYTES to write and reload if size > 255 */
3762 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
3764 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
3768 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
3771 /* Wait until RXNE flag is set */
3772 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, I2C_TIMEOUT_RXNE) != HAL_OK)
3774 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3777 /* Check if Errors has been detected during transfer */
3778 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3780 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3781 /* Wait until STOPF flag is reset */
3782 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3784 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3786 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3790 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3794 /* Clear STOP Flag */
3795 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3797 /* Clear Configuration Register 2 */
3798 __HAL_I2C_RESET_CR2(hi2c);
3800 hi2c->XferCount = 0;
3802 hi2c->State = HAL_I2C_STATE_READY;
3803 HAL_I2C_ErrorCallback(hi2c);
3807 /* Enable DMA Request */
3808 hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
3814 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3815 /* Wait until STOPF flag is reset */
3816 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3818 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3820 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3824 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3828 /* Clear STOP Flag */
3829 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3831 /* Clear Configuration Register 2 */
3832 __HAL_I2C_RESET_CR2(hi2c);
3834 /* Disable DMA Request */
3835 hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
3837 hi2c->XferCount = 0;
3839 hi2c->State = HAL_I2C_STATE_READY;
3841 /* Check if Errors has been detected during transfer */
3842 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3844 HAL_I2C_ErrorCallback(hi2c);
3848 HAL_I2C_MemRxCpltCallback(hi2c);
3854 * @brief DMA I2C communication error callback.
3855 * @param hdma : DMA handle
3858 static void I2C_DMAError(DMA_HandleTypeDef *hdma)
3860 I2C_HandleTypeDef* hi2c = ( I2C_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
3862 /* Disable Acknowledge */
3863 hi2c->Instance->CR2 |= I2C_CR2_NACK;
3865 hi2c->XferCount = 0;
3867 hi2c->State = HAL_I2C_STATE_READY;
3869 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
3871 HAL_I2C_ErrorCallback(hi2c);
3875 * @brief This function handles I2C Communication Timeout.
3876 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
3877 * the configuration information for the specified I2C.
3878 * @param Flag: specifies the I2C flag to check.
3879 * @param Status: The new Flag status (SET or RESET).
3880 * @param Timeout: Timeout duration
3881 * @retval HAL status
3883 static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
3885 uint32_t tickstart = HAL_GetTick();
3887 /* Wait until flag is set */
3890 while(__HAL_I2C_GET_FLAG(hi2c, Flag) == RESET)
3892 /* Check for the Timeout */
3893 if(Timeout != HAL_MAX_DELAY)
3895 if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
3897 hi2c->State= HAL_I2C_STATE_READY;
3898 /* Process Unlocked */
3907 while(__HAL_I2C_GET_FLAG(hi2c, Flag) != RESET)
3909 /* Check for the Timeout */
3910 if(Timeout != HAL_MAX_DELAY)
3912 if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
3914 hi2c->State= HAL_I2C_STATE_READY;
3915 /* Process Unlocked */
3926 * @brief This function handles I2C Communication Timeout for specific usage of TXIS flag.
3927 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
3928 * the configuration information for the specified I2C.
3929 * @param Timeout: Timeout duration
3930 * @retval HAL status
3932 static HAL_StatusTypeDef I2C_WaitOnTXISFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout)
3934 uint32_t tickstart = HAL_GetTick();
3936 while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == RESET)
3938 /* Check if a NACK is detected */
3939 if(I2C_IsAcknowledgeFailed(hi2c, Timeout) != HAL_OK)
3944 /* Check for the Timeout */
3945 if(Timeout != HAL_MAX_DELAY)
3947 if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
3949 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3950 hi2c->State= HAL_I2C_STATE_READY;
3952 /* Process Unlocked */
3963 * @brief This function handles I2C Communication Timeout for specific usage of STOP flag.
3964 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
3965 * the configuration information for the specified I2C.
3966 * @param Timeout: Timeout duration
3967 * @retval HAL status
3969 static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout)
3971 uint32_t tickstart = 0x00;
3972 tickstart = HAL_GetTick();
3974 while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET)
3976 /* Check if a NACK is detected */
3977 if(I2C_IsAcknowledgeFailed(hi2c, Timeout) != HAL_OK)
3982 /* Check for the Timeout */
3983 if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
3985 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3986 hi2c->State= HAL_I2C_STATE_READY;
3988 /* Process Unlocked */
3998 * @brief This function handles I2C Communication Timeout for specific usage of RXNE flag.
3999 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
4000 * the configuration information for the specified I2C.
4001 * @param Timeout: Timeout duration
4002 * @retval HAL status
4004 static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout)
4006 uint32_t tickstart = 0x00;
4007 tickstart = HAL_GetTick();
4009 while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == RESET)
4011 /* Check if a STOPF is detected */
4012 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
4014 /* Clear STOP Flag */
4015 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
4017 /* Clear Configuration Register 2 */
4018 __HAL_I2C_RESET_CR2(hi2c);
4020 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
4021 hi2c->State= HAL_I2C_STATE_READY;
4023 /* Process Unlocked */
4029 /* Check for the Timeout */
4030 if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
4032 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
4033 hi2c->State= HAL_I2C_STATE_READY;
4035 /* Process Unlocked */
4045 * @brief This function handles Acknowledge failed detection during an I2C Communication.
4046 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
4047 * the configuration information for the specified I2C.
4048 * @param Timeout: Timeout duration
4049 * @retval HAL status
4051 static HAL_StatusTypeDef I2C_IsAcknowledgeFailed(I2C_HandleTypeDef *hi2c, uint32_t Timeout)
4053 uint32_t tickstart = 0x00;
4054 tickstart = HAL_GetTick();
4056 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
4058 /* Generate stop if necessary only in case of I2C peripheral in MASTER mode */
4059 if((hi2c->State == HAL_I2C_STATE_MASTER_BUSY_TX) || (hi2c->State == HAL_I2C_STATE_MEM_BUSY_TX)
4060 || (hi2c->State == HAL_I2C_STATE_MEM_BUSY_RX))
4062 /* No need to generate the STOP condition if AUTOEND mode is enabled */
4063 /* Generate the STOP condition only in case of SOFTEND mode is enabled */
4064 if((hi2c->Instance->CR2 & I2C_AUTOEND_MODE) != I2C_AUTOEND_MODE)
4067 hi2c->Instance->CR2 |= I2C_CR2_STOP;
4071 /* Wait until STOP Flag is reset */
4072 /* AutoEnd should be initiate after AF */
4073 while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET)
4075 /* Check for the Timeout */
4076 if(Timeout != HAL_MAX_DELAY)
4078 if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
4080 hi2c->State= HAL_I2C_STATE_READY;
4081 /* Process Unlocked */
4088 /* Clear NACKF Flag */
4089 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
4091 /* Clear STOP Flag */
4092 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
4094 /* Clear Configuration Register 2 */
4095 __HAL_I2C_RESET_CR2(hi2c);
4097 hi2c->ErrorCode = HAL_I2C_ERROR_AF;
4098 hi2c->State= HAL_I2C_STATE_READY;
4100 /* Process Unlocked */
4109 * @brief Handles I2Cx communication when starting transfer or during transfer (TC or TCR flag are set).
4110 * @param hi2c: I2C handle.
4111 * @param DevAddress: specifies the slave address to be programmed.
4112 * @param Size: specifies the number of bytes to be programmed.
4113 * This parameter must be a value between 0 and 255.
4114 * @param Mode: new state of the I2C START condition generation.
4115 * This parameter can be one of the following values:
4116 * @arg I2C_RELOAD_MODE: Enable Reload mode .
4117 * @arg I2C_AUTOEND_MODE: Enable Automatic end mode.
4118 * @arg I2C_SOFTEND_MODE: Enable Software end mode.
4119 * @param Request: new state of the I2C START condition generation.
4120 * This parameter can be one of the following values:
4121 * @arg I2C_NO_STARTSTOP: Don't Generate stop and start condition.
4122 * @arg I2C_GENERATE_STOP: Generate stop condition (Size should be set to 0).
4123 * @arg I2C_GENERATE_START_READ: Generate Restart for read request.
4124 * @arg I2C_GENERATE_START_WRITE: Generate Restart for write request.
4127 static void I2C_TransferConfig(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t Size, uint32_t Mode, uint32_t Request)
4129 uint32_t tmpreg = 0;
4131 /* Check the parameters */
4132 assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
4133 assert_param(IS_TRANSFER_MODE(Mode));
4134 assert_param(IS_TRANSFER_REQUEST(Request));
4136 /* Get the CR2 register value */
4137 tmpreg = hi2c->Instance->CR2;
4139 /* clear tmpreg specific bits */
4140 tmpreg &= (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP));
4143 tmpreg |= (uint32_t)(((uint32_t)DevAddress & I2C_CR2_SADD) | (((uint32_t)Size << 16 ) & I2C_CR2_NBYTES) | \
4144 (uint32_t)Mode | (uint32_t)Request);
4146 /* update CR2 register */
4147 hi2c->Instance->CR2 = tmpreg;
4154 #endif /* HAL_I2C_MODULE_ENABLED */
4163 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/