2 ******************************************************************************
3 * @file stm32l0xx_hal_i2c.c
4 * @author MCD Application Team
6 * @date 06-February-2015
7 * @brief I2C HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the Inter Integrated Circuit (I2C) peripheral:
10 * + Initialization and de-initialization functions
11 * + IO operation functions
12 * + Peripheral State and Errors functions
15 ==============================================================================
16 ##### How to use this driver #####
17 ==============================================================================
19 The I2C HAL driver can be used as follows:
21 (#) Declare a I2C_HandleTypeDef handle structure, for example:
22 I2C_HandleTypeDef hi2c;
24 (#)Initialize the I2C low level resources by implement the HAL_I2C_MspInit ()API:
25 (##) Enable the I2Cx interface clock
26 (##) I2C pins configuration
27 (+++) Enable the clock for the I2C GPIOs
28 (+++) Configure I2C pins as alternate function open-drain
29 (##) NVIC configuration if you need to use interrupt process
30 (+++) Configure the I2Cx interrupt priority
31 (+++) Enable the NVIC I2C IRQ Channel
32 (##) DMA Configuration if you need to use DMA process
33 (+++) Declare a DMA_HandleTypeDef handle structure for the transmit or receive channel
34 (+++) Enable the DMAx interface clock using
35 (+++) Configure the DMA handle parameters
36 (+++) Configure the DMA Tx or Rx channel
37 (+++) Associate the initilalized DMA handle to the hi2c DMA Tx or Rx handle
38 (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on
39 the DMA Tx or Rx channel
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(), configures also the low level Hardware
45 (GPIO, CLOCK, NVIC...etc) by calling the customed HAL_I2C_MspInit(&hi2c) API.
47 (#) To check if target device is ready for communication, use the function HAL_I2C_IsDeviceReady()
49 (#) For I2C IO and IO MEM operations, three operation modes are available within this driver :
51 *** Polling mode IO operation ***
52 =================================
54 (+) Transmit in master mode an amount of data in blocking mode using HAL_I2C_Master_Transmit()
55 (+) Receive in master mode an amount of data in blocking mode using HAL_I2C_Master_Receive()
56 (+) Transmit in slave mode an amount of data in blocking mode using HAL_I2C_Slave_Transmit()
57 (+) Receive in slave mode an amount of data in blocking mode using HAL_I2C_Slave_Receive()
59 *** Polling mode IO MEM operation ***
60 =====================================
62 (+) Write an amount of data in blocking mode to a specific memory address using HAL_I2C_Mem_Write()
63 (+) Read an amount of data in blocking mode from a specific memory address using HAL_I2C_Mem_Read()
66 *** Interrupt mode IO operation ***
67 ===================================
69 (+) Transmit in master mode an amount of data in non blocking mode using HAL_I2C_Master_Transmit_IT()
70 (+) At transmission end of transfer HAL_I2C_MasterTxCpltCallback is executed and user can
71 add his own code by customization of function pointer HAL_I2C_MasterTxCpltCallback
72 (+) Receive in master mode an amount of data in non blocking mode using HAL_I2C_Master_Receive_IT()
73 (+) At reception end of transfer HAL_I2C_MasterRxCpltCallback is executed and user can
74 add his own code by customization of function pointer HAL_I2C_MasterRxCpltCallback
75 (+) Transmit in slave mode an amount of data in non blocking mode using HAL_I2C_Slave_Transmit_IT()
76 (+) At transmission end of transfer HAL_I2C_SlaveTxCpltCallback is executed and user can
77 add his own code by customization of function pointer HAL_I2C_SlaveTxCpltCallback
78 (+) Receive in slave mode an amount of data in non blocking mode using HAL_I2C_Slave_Receive_IT()
79 (+) At reception end of transfer HAL_I2C_SlaveRxCpltCallback is executed and user can
80 add his own code by customization of function pointer HAL_I2C_SlaveRxCpltCallback
81 (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
82 add his own code by customization of function pointer HAL_I2C_ErrorCallback
84 *** Interrupt mode IO MEM operation ***
85 =======================================
87 (+) Write an amount of data in no-blocking mode with Interrupt to a specific memory address using
88 HAL_I2C_Mem_Write_IT()
89 (+) At MEM end of write transfer HAL_I2C_MemTxCpltCallback is executed and user can
90 add his own code by customization of function pointer HAL_I2C_MemTxCpltCallback
91 (+) Read an amount of data in no-blocking mode with Interrupt from a specific memory address using
93 (+) At MEM end of read transfer HAL_I2C_MemRxCpltCallback is executed and user can
94 add his own code by customization of function pointer HAL_I2C_MemRxCpltCallback
95 (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
96 add his own code by customization of function pointer HAL_I2C_ErrorCallback
98 *** DMA mode IO operation ***
99 ==============================
101 (+) Transmit in master mode an amount of data in non blocking mode (DMA) using
102 HAL_I2C_Master_Transmit_DMA()
103 (+) At transmission end of transfer HAL_I2C_MasterTxCpltCallback is executed and user can
104 add his own code by customization of function pointer HAL_I2C_MasterTxCpltCallback
105 (+) Receive in master mode an amount of data in non blocking mode (DMA) using
106 HAL_I2C_Master_Receive_DMA()
107 (+) At reception end of transfer HAL_I2C_MasterRxCpltCallback is executed and user can
108 add his own code by customization of function pointer HAL_I2C_MasterRxCpltCallback
109 (+) Transmit in slave mode an amount of data in non blocking mode (DMA) using
110 HAL_I2C_Slave_Transmit_DMA()
111 (+) At transmission end of transfer HAL_I2C_SlaveTxCpltCallback is executed and user can
112 add his own code by customization of function pointer HAL_I2C_SlaveTxCpltCallback
113 (+) Receive in slave mode an amount of data in non blocking mode (DMA) using
114 HAL_I2C_Slave_Receive_DMA()
115 (+) At reception end of transfer HAL_I2C_SlaveRxCpltCallback is executed and user can
116 add his own code by customization of function pointer HAL_I2C_SlaveRxCpltCallback
117 (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
118 add his own code by customization of function pointer HAL_I2C_ErrorCallback
120 *** DMA mode IO MEM operation ***
121 =================================
123 (+) Write an amount of data in no-blocking mode with DMA to a specific memory address using
124 HAL_I2C_Mem_Write_DMA()
125 (+) At MEM end of write transfer HAL_I2C_MemTxCpltCallback is executed and user can
126 add his own code by customization of function pointer HAL_I2C_MemTxCpltCallback
127 (+) Read an amount of data in no-blocking mode with DMA from a specific memory address using
128 HAL_I2C_Mem_Read_DMA()
129 (+) At MEM end of read transfer HAL_I2C_MemRxCpltCallback is executed and user can
130 add his own code by customization of function pointer HAL_I2C_MemRxCpltCallback
131 (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
132 add his own code by customization of function pointer HAL_I2C_ErrorCallback
135 *** I2C HAL driver macros list ***
136 ==================================
138 Below the list of most used macros in I2C HAL driver.
140 (+) __HAL_I2C_ENABLE: Enable the I2C peripheral
141 (+) __HAL_I2C_DISABLE: Disable the I2C peripheral
142 (+) __HAL_I2C_GET_FLAG : Check whether the specified I2C flag is set or not
143 (+) __HAL_I2C_CLEAR_FLAG : Clear the specified I2C pending flag
144 (+) __HAL_I2C_ENABLE_IT: Enable the specified I2C interrupt
145 (+) __HAL_I2C_DISABLE_IT: Disable the specified I2C interrupt
148 (@) You can refer to the I2C HAL driver header file for more useful macros
151 ******************************************************************************
154 * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
156 * Redistribution and use in source and binary forms, with or without modification,
157 * are permitted provided that the following conditions are met:
158 * 1. Redistributions of source code must retain the above copyright notice,
159 * this list of conditions and the following disclaimer.
160 * 2. Redistributions in binary form must reproduce the above copyright notice,
161 * this list of conditions and the following disclaimer in the documentation
162 * and/or other materials provided with the distribution.
163 * 3. Neither the name of STMicroelectronics nor the names of its contributors
164 * may be used to endorse or promote products derived from this software
165 * without specific prior written permission.
167 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
168 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
169 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
170 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
171 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
172 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
173 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
174 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
175 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
176 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
178 ******************************************************************************
181 /* Includes ------------------------------------------------------------------*/
182 #include "stm32l0xx_hal.h"
184 /** @addtogroup STM32L0xx_HAL_Driver
188 /** @defgroup I2C I2C
189 * @brief I2C HAL module driver
193 #ifdef HAL_I2C_MODULE_ENABLED
195 /* Private typedef -----------------------------------------------------------*/
196 /* Private constants ---------------------------------------------------------*/
197 /** @addtogroup I2C_Private_Constants I2C Private Constants
200 #define TIMING_CLEAR_MASK ((uint32_t)0xF0FFFFFF) /*<! I2C TIMING clear register Mask */
201 #define I2C_TIMEOUT_ADDR ((uint32_t)10000) /* 10 s */
202 #define I2C_TIMEOUT_BUSY ((uint32_t)25) /* 25 ms */
203 #define I2C_TIMEOUT_DIR ((uint32_t)25) /* 25 ms */
204 #define I2C_TIMEOUT_RXNE ((uint32_t)25) /* 25 ms */
205 #define I2C_TIMEOUT_STOPF ((uint32_t)25) /* 25 ms */
206 #define I2C_TIMEOUT_TC ((uint32_t)25) /* 25 ms */
207 #define I2C_TIMEOUT_TCR ((uint32_t)25) /* 25 ms */
208 #define I2C_TIMEOUT_TXIS ((uint32_t)25) /* 25 ms */
209 #define I2C_TIMEOUT_FLAG ((uint32_t)25) /* 25 ms */
214 /* Private macro -------------------------------------------------------------*/
215 /* Private variables ---------------------------------------------------------*/
216 /* Private function prototypes -----------------------------------------------*/
217 /** @addtogroup I2C_Private_Functions I2C Private Functions
220 static void I2C_DMAMasterTransmitCplt(DMA_HandleTypeDef *hdma);
221 static void I2C_DMAMasterReceiveCplt(DMA_HandleTypeDef *hdma);
222 static void I2C_DMASlaveTransmitCplt(DMA_HandleTypeDef *hdma);
223 static void I2C_DMASlaveReceiveCplt(DMA_HandleTypeDef *hdma);
224 static void I2C_DMAMemTransmitCplt(DMA_HandleTypeDef *hdma);
225 static void I2C_DMAMemReceiveCplt(DMA_HandleTypeDef *hdma);
226 static void I2C_DMAError(DMA_HandleTypeDef *hdma);
228 static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout);
229 static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout);
230 static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
231 static HAL_StatusTypeDef I2C_WaitOnTXISFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout);
232 static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout);
233 static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout);
234 static HAL_StatusTypeDef I2C_IsAcknowledgeFailed(I2C_HandleTypeDef *hi2c, uint32_t Timeout);
236 static HAL_StatusTypeDef I2C_MasterTransmit_ISR(I2C_HandleTypeDef *hi2c);
237 static HAL_StatusTypeDef I2C_MasterReceive_ISR(I2C_HandleTypeDef *hi2c);
239 static HAL_StatusTypeDef I2C_SlaveTransmit_ISR(I2C_HandleTypeDef *hi2c);
240 static HAL_StatusTypeDef I2C_SlaveReceive_ISR(I2C_HandleTypeDef *hi2c);
242 static void I2C_TransferConfig(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t Size, uint32_t Mode, uint32_t Request);
247 /* Exported functions --------------------------------------------------------*/
249 /** @defgroup I2C_Exported_Functions I2C Exported Functions
253 /** @defgroup I2C_Exported_Functions_Group1 Initialization and de-initialization functions
254 * @brief Initialization and Configuration functions
257 ===============================================================================
258 ##### Initialization and de-initialization functions #####
259 ===============================================================================
260 [..] This subsection provides a set of functions allowing to initialize and
261 de-initialiaze the I2Cx peripheral:
263 (+) User must Implement HAL_I2C_MspInit() function in which he configures
264 all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
266 (+) Call the function HAL_I2C_Init() to configure the selected device with
267 the selected configuration:
270 (++) Addressing mode (Master, Slave)
271 (++) Dual Addressing mode
273 (++) Own Address 2 Mask
274 (++) General call mode
277 (+) Call the function HAL_I2C_DeInit() to restore the default configuration
278 of the selected I2Cx periperal.
285 * @brief Initializes the I2C according to the specified parameters
286 * in the I2C_InitTypeDef and create the associated handle.
287 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
288 * the configuration information for the specified I2C.
291 HAL_StatusTypeDef HAL_I2C_Init(I2C_HandleTypeDef *hi2c)
293 /* Check the I2C handle allocation */
299 /* Check the parameters */
300 assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
301 assert_param(IS_I2C_OWN_ADDRESS1(hi2c->Init.OwnAddress1));
302 assert_param(IS_I2C_ADDRESSING_MODE(hi2c->Init.AddressingMode));
303 assert_param(IS_I2C_DUAL_ADDRESS(hi2c->Init.DualAddressMode));
304 assert_param(IS_I2C_OWN_ADDRESS2(hi2c->Init.OwnAddress2));
305 assert_param(IS_I2C_OWN_ADDRESS2_MASK(hi2c->Init.OwnAddress2Masks));
306 assert_param(IS_I2C_GENERAL_CALL(hi2c->Init.GeneralCallMode));
307 assert_param(IS_I2C_NO_STRETCH(hi2c->Init.NoStretchMode));
309 if(hi2c->State == HAL_I2C_STATE_RESET)
311 /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
312 HAL_I2C_MspInit(hi2c);
315 hi2c->State = HAL_I2C_STATE_BUSY;
317 /* Disable the selected I2C peripheral */
318 __HAL_I2C_DISABLE(hi2c);
320 /*---------------------------- I2Cx TIMINGR Configuration ------------------*/
321 /* Configure I2Cx: Frequency range */
322 hi2c->Instance->TIMINGR = hi2c->Init.Timing & TIMING_CLEAR_MASK;
324 /*---------------------------- I2Cx OAR1 Configuration ---------------------*/
325 /* Configure I2Cx: Own Address1 and ack own address1 mode */
326 hi2c->Instance->OAR1 &= ~I2C_OAR1_OA1EN;
327 if(hi2c->Init.OwnAddress1 != 0)
329 if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_7BIT)
331 hi2c->Instance->OAR1 = (I2C_OAR1_OA1EN | hi2c->Init.OwnAddress1);
333 else /* I2C_ADDRESSINGMODE_10BIT */
335 hi2c->Instance->OAR1 = (I2C_OAR1_OA1EN | I2C_OAR1_OA1MODE | hi2c->Init.OwnAddress1);
339 /*---------------------------- I2Cx CR2 Configuration ----------------------*/
340 /* Configure I2Cx: Addressing Master mode */
341 if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT)
343 hi2c->Instance->CR2 = (I2C_CR2_ADD10);
345 /* Enable the AUTOEND by default, and enable NACK (should be disable only during Slave process */
346 hi2c->Instance->CR2 |= (I2C_CR2_AUTOEND | I2C_CR2_NACK);
348 /*---------------------------- I2Cx OAR2 Configuration ---------------------*/
349 /* Configure I2Cx: Dual mode and Own Address2 */
350 hi2c->Instance->OAR2 = (hi2c->Init.DualAddressMode | hi2c->Init.OwnAddress2 | (hi2c->Init.OwnAddress2Masks << 8));
352 /*---------------------------- I2Cx CR1 Configuration ----------------------*/
353 /* Configure I2Cx: Generalcall and NoStretch mode */
354 hi2c->Instance->CR1 = (hi2c->Init.GeneralCallMode | hi2c->Init.NoStretchMode);
356 /* Enable the selected I2C peripheral */
357 __HAL_I2C_ENABLE(hi2c);
359 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
360 hi2c->State = HAL_I2C_STATE_READY;
366 * @brief DeInitializes the I2C peripheral.
367 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
368 * the configuration information for the specified I2C.
371 HAL_StatusTypeDef HAL_I2C_DeInit(I2C_HandleTypeDef *hi2c)
373 /* Check the I2C handle allocation */
379 /* Check the parameters */
380 assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
382 hi2c->State = HAL_I2C_STATE_BUSY;
384 /* Disable the I2C Peripheral Clock */
385 __HAL_I2C_DISABLE(hi2c);
387 /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
388 HAL_I2C_MspDeInit(hi2c);
390 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
392 hi2c->State = HAL_I2C_STATE_RESET;
401 * @brief I2C MSP Init.
402 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
403 * the configuration information for the specified I2C.
406 __weak void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)
408 /* NOTE : This function Should not be modified, when the callback is needed,
409 the HAL_I2C_MspInit could be implemented in the user file
414 * @brief I2C MSP DeInit
415 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
416 * the configuration information for the specified I2C.
419 __weak void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c)
421 /* NOTE : This function Should not be modified, when the callback is needed,
422 the HAL_I2C_MspDeInit could be implemented in the user file
430 /** @defgroup I2C_Exported_Functions_Group2 Input and Output operation functions
431 * @brief Data transfers functions
434 ===============================================================================
435 ##### IO operation functions #####
436 ===============================================================================
438 This subsection provides a set of functions allowing to manage the I2C data
441 (#) There are two modes of transfer:
442 (++) Blocking mode : The communication is performed in the polling mode.
443 The status of all data processing is returned by the same function
444 after finishing transfer.
445 (++) No-Blocking mode : The communication is performed using Interrupts
446 or DMA. These functions return the status of the transfer startup.
447 The end of the data processing will be indicated through the
448 dedicated I2C IRQ when using Interrupt mode or the DMA IRQ when
451 (#) Blocking mode functions are :
452 (++) HAL_I2C_Master_Transmit()
453 (++) HAL_I2C_Master_Receive()
454 (++) HAL_I2C_Slave_Transmit()
455 (++) HAL_I2C_Slave_Receive()
456 (++) HAL_I2C_Mem_Write()
457 (++) HAL_I2C_Mem_Read()
458 (++) HAL_I2C_IsDeviceReady()
460 (#) No-Blocking mode functions with Interrupt are :
461 (++) HAL_I2C_Master_Transmit_IT()
462 (++) HAL_I2C_Master_Receive_IT()
463 (++) HAL_I2C_Slave_Transmit_IT()
464 (++) HAL_I2C_Slave_Receive_IT()
465 (++) HAL_I2C_Mem_Write_IT()
466 (++) HAL_I2C_Mem_Read_IT()
468 (#) No-Blocking mode functions with DMA are :
469 (++) HAL_I2C_Master_Transmit_DMA()
470 (++) HAL_I2C_Master_Receive_DMA()
471 (++) HAL_I2C_Slave_Transmit_DMA()
472 (++) HAL_I2C_Slave_Receive_DMA()
473 (++) HAL_I2C_Mem_Write_DMA()
474 (++) HAL_I2C_Mem_Read_DMA()
476 (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
477 (++) HAL_I2C_MemTxCpltCallback()
478 (++) HAL_I2C_MemRxCpltCallback()
479 (++) HAL_I2C_MasterTxCpltCallback()
480 (++) HAL_I2C_MasterRxCpltCallback()
481 (++) HAL_I2C_SlaveTxCpltCallback()
482 (++) HAL_I2C_SlaveRxCpltCallback()
483 (++) HAL_I2C_ErrorCallback()
490 * @brief Transmits in master mode an amount of data in blocking mode.
491 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
492 * the configuration information for the specified I2C.
493 * @param DevAddress: Target device address
494 * @param pData: Pointer to data buffer
495 * @param Size: Amount of data to be sent
496 * @param Timeout: Timeout duration
499 HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
501 uint32_t sizetmp = 0;
503 if(hi2c->State == HAL_I2C_STATE_READY)
505 if((pData == NULL ) || (Size == 0))
510 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
518 hi2c->State = HAL_I2C_STATE_MASTER_BUSY_TX;
519 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
521 /* Send Slave Address */
522 /* Set NBYTES to write and reload if size > 255 and generate RESTART */
523 /* Size > 255, need to set RELOAD bit */
526 I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);
531 I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_GENERATE_START_WRITE);
537 /* Wait until TXIS flag is set */
538 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
540 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
549 /* Write data to TXDR */
550 hi2c->Instance->TXDR = (*pData++);
554 if((sizetmp == 0)&&(Size!=0))
556 /* Wait until TXE flag is set */
557 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout) != HAL_OK)
564 I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
569 I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
576 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
577 /* Wait until STOPF flag is set */
578 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
580 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
590 /* Clear STOP Flag */
591 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
593 /* Clear Configuration Register 2 */
594 __I2C_RESET_CR2(hi2c);
596 hi2c->State = HAL_I2C_STATE_READY;
598 /* Process Unlocked */
610 * @brief Receives in master mode an amount of data in blocking mode.
611 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
612 * the configuration information for the specified I2C.
613 * @param DevAddress: Target device address
614 * @param pData: Pointer to data buffer
615 * @param Size: Amount of data to be sent
616 * @param Timeout: Timeout duration
619 HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
621 uint32_t sizetmp = 0;
623 if(hi2c->State == HAL_I2C_STATE_READY)
625 if((pData == NULL ) || (Size == 0))
630 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
638 hi2c->State = HAL_I2C_STATE_MASTER_BUSY_RX;
639 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
641 /* Send Slave Address */
642 /* Set NBYTES to write and reload if size > 255 and generate RESTART */
643 /* Size > 255, need to set RELOAD bit */
646 I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
651 I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
657 /* Wait until RXNE flag is set */
658 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, Timeout) != HAL_OK)
663 /* Write data to RXDR */
664 (*pData++) =hi2c->Instance->RXDR;
668 if((sizetmp == 0)&&(Size!=0))
670 /* Wait until TCR flag is set */
671 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout) != HAL_OK)
678 I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
683 I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
690 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
691 /* Wait until STOPF flag is set */
692 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
694 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
704 /* Clear STOP Flag */
705 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
707 /* Clear Configuration Register 2 */
708 __I2C_RESET_CR2(hi2c);
710 hi2c->State = HAL_I2C_STATE_READY;
712 /* Process Unlocked */
724 * @brief Transmits in slave mode an amount of data in blocking mode.
725 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
726 * the configuration information for the specified I2C.
727 * @param pData: Pointer to data buffer
728 * @param Size: Amount of data to be sent
729 * @param Timeout: Timeout duration
732 HAL_StatusTypeDef HAL_I2C_Slave_Transmit(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
734 if(hi2c->State == HAL_I2C_STATE_READY)
736 if((pData == NULL ) || (Size == 0))
744 hi2c->State = HAL_I2C_STATE_SLAVE_BUSY_RX;
745 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
747 /* Enable Address Acknowledge */
748 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
750 /* Wait until ADDR flag is set */
751 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout) != HAL_OK)
753 /* Disable Address Acknowledge */
754 hi2c->Instance->CR2 |= I2C_CR2_NACK;
758 /* Clear ADDR flag */
759 __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
761 /* If 10bit addressing mode is selected */
762 if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT)
764 /* Wait until ADDR flag is set */
765 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout) != HAL_OK)
767 /* Disable Address Acknowledge */
768 hi2c->Instance->CR2 |= I2C_CR2_NACK;
772 /* Clear ADDR flag */
773 __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
776 /* Wait until DIR flag is set Transmitter mode */
777 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_DIR, RESET, Timeout) != HAL_OK)
779 /* Disable Address Acknowledge */
780 hi2c->Instance->CR2 |= I2C_CR2_NACK;
786 /* Wait until TXIS flag is set */
787 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
789 /* Disable Address Acknowledge */
790 hi2c->Instance->CR2 |= I2C_CR2_NACK;
792 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
802 /* Read data from TXDR */
803 hi2c->Instance->TXDR = (*pData++);
807 /* Wait until STOP flag is set */
808 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
810 /* Disable Address Acknowledge */
811 hi2c->Instance->CR2 |= I2C_CR2_NACK;
813 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
815 /* Normal use case for Transmitter mode */
816 /* A NACK is generated to confirm the end of transfer */
817 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
825 /* Clear STOP flag */
826 __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_STOPF);
828 /* Wait until BUSY flag is reset */
829 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout) != HAL_OK)
831 /* Disable Address Acknowledge */
832 hi2c->Instance->CR2 |= I2C_CR2_NACK;
836 /* Disable Address Acknowledge */
837 hi2c->Instance->CR2 |= I2C_CR2_NACK;
839 hi2c->State = HAL_I2C_STATE_READY;
841 /* Process Unlocked */
853 * @brief Receive in slave mode an amount of data in blocking mode
854 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
855 * the configuration information for the specified I2C.
856 * @param pData: Pointer to data buffer
857 * @param Size: Amount of data to be sent
858 * @param Timeout: Timeout duration
861 HAL_StatusTypeDef HAL_I2C_Slave_Receive(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
863 if(hi2c->State == HAL_I2C_STATE_READY)
865 if((pData == NULL ) || (Size == 0))
873 hi2c->State = HAL_I2C_STATE_SLAVE_BUSY_RX;
874 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
876 /* Enable Address Acknowledge */
877 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
879 /* Wait until ADDR flag is set */
880 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout) != HAL_OK)
882 /* Disable Address Acknowledge */
883 hi2c->Instance->CR2 |= I2C_CR2_NACK;
887 /* Clear ADDR flag */
888 __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
890 /* Wait until DIR flag is reset Receiver mode */
891 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_DIR, SET, Timeout) != HAL_OK)
893 /* Disable Address Acknowledge */
894 hi2c->Instance->CR2 |= I2C_CR2_NACK;
900 /* Wait until RXNE flag is set */
901 if(I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
903 /* Disable Address Acknowledge */
904 hi2c->Instance->CR2 |= I2C_CR2_NACK;
905 if(hi2c->ErrorCode == HAL_I2C_ERROR_TIMEOUT)
915 /* Read data from RXDR */
916 (*pData++) = hi2c->Instance->RXDR;
920 /* Wait until STOP flag is set */
921 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
923 /* Disable Address Acknowledge */
924 hi2c->Instance->CR2 |= I2C_CR2_NACK;
926 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
936 /* Clear STOP flag */
937 __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_STOPF);
939 /* Wait until BUSY flag is reset */
940 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout) != HAL_OK)
942 /* Disable Address Acknowledge */
943 hi2c->Instance->CR2 |= I2C_CR2_NACK;
948 /* Disable Address Acknowledge */
949 hi2c->Instance->CR2 |= I2C_CR2_NACK;
951 hi2c->State = HAL_I2C_STATE_READY;
953 /* Process Unlocked */
965 * @brief Transmit in master mode an amount of data in no-blocking mode with Interrupt
966 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
967 * the configuration information for the specified I2C.
968 * @param DevAddress: Target device address
969 * @param pData: Pointer to data buffer
970 * @param Size: Amount of data to be sent
973 HAL_StatusTypeDef HAL_I2C_Master_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
975 if(hi2c->State == HAL_I2C_STATE_READY)
977 if((pData == NULL) || (Size == 0))
982 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
990 hi2c->State = HAL_I2C_STATE_MASTER_BUSY_TX;
991 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
993 hi2c->pBuffPtr = pData;
994 hi2c->XferCount = Size;
997 hi2c->XferSize = 255;
1001 hi2c->XferSize = Size;
1004 /* Send Slave Address */
1005 /* Set NBYTES to write and reload if size > 255 and generate RESTART */
1006 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
1008 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);
1012 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_WRITE);
1015 /* Process Unlocked */
1018 /* Note : The I2C interrupts must be enabled after unlocking current process
1019 to avoid the risk of I2C interrupt handle execution before current
1023 /* Enable ERR, TC, STOP, NACK, TXI interrupt */
1024 /* possible to enable all of these */
1025 /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
1026 __HAL_I2C_ENABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_TXI );
1037 * @brief Receive in master mode an amount of data in no-blocking mode with Interrupt
1038 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1039 * the configuration information for the specified I2C.
1040 * @param DevAddress: Target device address
1041 * @param pData: Pointer to data buffer
1042 * @param Size: Amount of data to be sent
1043 * @retval HAL status
1045 HAL_StatusTypeDef HAL_I2C_Master_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
1047 if(hi2c->State == HAL_I2C_STATE_READY)
1049 if((pData == NULL) || (Size == 0))
1054 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1059 /* Process Locked */
1062 hi2c->State = HAL_I2C_STATE_MASTER_BUSY_RX;
1063 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1065 hi2c->pBuffPtr = pData;
1066 hi2c->XferCount = Size;
1069 hi2c->XferSize = 255;
1073 hi2c->XferSize = Size;
1076 /* Send Slave Address */
1077 /* Set NBYTES to write and reload if size > 255 and generate RESTART */
1078 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
1080 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
1084 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
1087 /* Process Unlocked */
1090 /* Note : The I2C interrupts must be enabled after unlocking current process
1091 to avoid the risk of I2C interrupt handle execution before current
1094 /* Enable ERR, TC, STOP, NACK, RXI interrupt */
1095 /* possible to enable all of these */
1096 /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
1097 __HAL_I2C_ENABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_RXI );
1108 * @brief Transmit in slave mode an amount of data in no-blocking mode with Interrupt
1109 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1110 * the configuration information for the specified I2C.
1111 * @param pData: Pointer to data buffer
1112 * @param Size: Amount of data to be sent
1113 * @retval HAL status
1115 HAL_StatusTypeDef HAL_I2C_Slave_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
1117 if(hi2c->State == HAL_I2C_STATE_READY)
1119 if((pData == NULL) || (Size == 0))
1124 /* Process Locked */
1127 hi2c->State = HAL_I2C_STATE_SLAVE_BUSY_TX;
1128 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1130 /* Enable Address Acknowledge */
1131 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
1133 hi2c->pBuffPtr = pData;
1134 hi2c->XferSize = Size;
1135 hi2c->XferCount = Size;
1137 /* Process Unlocked */
1140 /* Note : The I2C interrupts must be enabled after unlocking current process
1141 to avoid the risk of I2C interrupt handle execution before current
1144 /* Enable ERR, TC, STOP, NACK, TXI interrupt */
1145 /* possible to enable all of these */
1146 /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
1147 __HAL_I2C_ENABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_TXI );
1158 * @brief Receive in slave mode an amount of data in no-blocking mode with Interrupt
1159 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1160 * the configuration information for the specified I2C.
1161 * @param pData: Pointer to data buffer
1162 * @param Size: Amount of data to be sent
1163 * @retval HAL status
1165 HAL_StatusTypeDef HAL_I2C_Slave_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
1167 if(hi2c->State == HAL_I2C_STATE_READY)
1169 if((pData == NULL) || (Size == 0))
1174 /* Process Locked */
1177 hi2c->State = HAL_I2C_STATE_SLAVE_BUSY_RX;
1178 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1180 /* Enable Address Acknowledge */
1181 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
1183 hi2c->pBuffPtr = pData;
1184 hi2c->XferSize = Size;
1185 hi2c->XferCount = Size;
1187 /* Process Unlocked */
1190 /* Note : The I2C interrupts must be enabled after unlocking current process
1191 to avoid the risk of I2C interrupt handle execution before current
1194 /* Enable ERR, TC, STOP, NACK, RXI interrupt */
1195 /* possible to enable all of these */
1196 /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
1197 __HAL_I2C_ENABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI);
1208 * @brief Transmit in master mode an amount of data in no-blocking mode with DMA
1209 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1210 * the configuration information for the specified I2C.
1211 * @param DevAddress: Target device address
1212 * @param pData: Pointer to data buffer
1213 * @param Size: Amount of data to be sent
1214 * @retval HAL status
1216 HAL_StatusTypeDef HAL_I2C_Master_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
1218 if(hi2c->State == HAL_I2C_STATE_READY)
1220 if((pData == NULL) || (Size == 0))
1225 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1230 /* Process Locked */
1233 hi2c->State = HAL_I2C_STATE_MASTER_BUSY_TX;
1234 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1236 hi2c->pBuffPtr = pData;
1237 hi2c->XferCount = Size;
1240 hi2c->XferSize = 255;
1244 hi2c->XferSize = Size;
1247 /* Set the I2C DMA transfer complete callback */
1248 hi2c->hdmatx->XferCpltCallback = I2C_DMAMasterTransmitCplt;
1250 /* Set the DMA error callback */
1251 hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
1253 /* Enable the DMA channel */
1254 HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
1256 /* Send Slave Address */
1257 /* Set NBYTES to write and reload if size > 255 and generate RESTART */
1258 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
1260 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);
1264 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_WRITE);
1267 /* Wait until TXIS flag is set */
1268 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, I2C_TIMEOUT_TXIS) != HAL_OK)
1270 /* Disable Address Acknowledge */
1271 hi2c->Instance->CR2 |= I2C_CR2_NACK;
1273 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1284 /* Enable DMA Request */
1285 hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
1287 /* Process Unlocked */
1299 * @brief Receive in master mode an amount of data in no-blocking mode with DMA
1300 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1301 * the configuration information for the specified I2C.
1302 * @param DevAddress: Target device address
1303 * @param pData: Pointer to data buffer
1304 * @param Size: Amount of data to be sent
1305 * @retval HAL status
1307 HAL_StatusTypeDef HAL_I2C_Master_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
1309 if(hi2c->State == HAL_I2C_STATE_READY)
1311 if((pData == NULL) || (Size == 0))
1316 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1321 /* Process Locked */
1324 hi2c->State = HAL_I2C_STATE_MASTER_BUSY_RX;
1325 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1327 hi2c->pBuffPtr = pData;
1328 hi2c->XferCount = Size;
1331 hi2c->XferSize = 255;
1335 hi2c->XferSize = Size;
1338 /* Set the I2C DMA transfer complete callback */
1339 hi2c->hdmarx->XferCpltCallback = I2C_DMAMasterReceiveCplt;
1341 /* Set the DMA error callback */
1342 hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
1344 /* Enable the DMA channel */
1345 HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData, hi2c->XferSize);
1347 /* Send Slave Address */
1348 /* Set NBYTES to write and reload if size > 255 and generate RESTART */
1349 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
1351 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
1355 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
1358 /* Wait until RXNE flag is set */
1359 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, I2C_TIMEOUT_RXNE) != HAL_OK)
1365 /* Enable DMA Request */
1366 hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
1368 /* Process Unlocked */
1380 * @brief Transmit in slave mode an amount of data in no-blocking mode with DMA
1381 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1382 * the configuration information for the specified I2C.
1383 * @param pData: Pointer to data buffer
1384 * @param Size: Amount of data to be sent
1385 * @retval HAL status
1387 HAL_StatusTypeDef HAL_I2C_Slave_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
1389 if(hi2c->State == HAL_I2C_STATE_READY)
1391 if((pData == NULL) || (Size == 0))
1395 /* Process Locked */
1398 hi2c->State = HAL_I2C_STATE_SLAVE_BUSY_TX;
1399 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1401 hi2c->pBuffPtr = pData;
1402 hi2c->XferCount = Size;
1403 hi2c->XferSize = Size;
1405 /* Set the I2C DMA transfer complete callback */
1406 hi2c->hdmatx->XferCpltCallback = I2C_DMASlaveTransmitCplt;
1408 /* Set the DMA error callback */
1409 hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
1411 /* Enable the DMA channel */
1412 HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
1414 /* Enable Address Acknowledge */
1415 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
1417 /* Wait until ADDR flag is set */
1418 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, I2C_TIMEOUT_ADDR) != HAL_OK)
1420 /* Disable Address Acknowledge */
1421 hi2c->Instance->CR2 |= I2C_CR2_NACK;
1425 /* Clear ADDR flag */
1426 __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
1428 /* If 10bits addressing mode is selected */
1429 if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT)
1431 /* Wait until ADDR flag is set */
1432 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, I2C_TIMEOUT_ADDR) != HAL_OK)
1434 /* Disable Address Acknowledge */
1435 hi2c->Instance->CR2 |= I2C_CR2_NACK;
1439 /* Clear ADDR flag */
1440 __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
1443 /* Wait until DIR flag is set Transmitter mode */
1444 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_DIR, RESET, I2C_TIMEOUT_BUSY) != HAL_OK)
1446 /* Disable Address Acknowledge */
1447 hi2c->Instance->CR2 |= I2C_CR2_NACK;
1451 /* Enable DMA Request */
1452 hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
1454 /* Process Unlocked */
1466 * @brief Receive in slave mode an amount of data in no-blocking mode with DMA
1467 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1468 * the configuration information for the specified I2C.
1469 * @param pData: Pointer to data buffer
1470 * @param Size: Amount of data to be sent
1471 * @retval HAL status
1473 HAL_StatusTypeDef HAL_I2C_Slave_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
1475 if(hi2c->State == HAL_I2C_STATE_READY)
1477 if((pData == NULL) || (Size == 0))
1481 /* Process Locked */
1484 hi2c->State = HAL_I2C_STATE_SLAVE_BUSY_RX;
1485 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1487 hi2c->pBuffPtr = pData;
1488 hi2c->XferSize = Size;
1489 hi2c->XferCount = Size;
1491 /* Set the I2C DMA transfer complete callback */
1492 hi2c->hdmarx->XferCpltCallback = I2C_DMASlaveReceiveCplt;
1494 /* Set the DMA error callback */
1495 hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
1497 /* Enable the DMA channel */
1498 HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData, Size);
1500 /* Enable Address Acknowledge */
1501 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
1503 /* Wait until ADDR flag is set */
1504 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, I2C_TIMEOUT_ADDR) != HAL_OK)
1506 /* Disable Address Acknowledge */
1507 hi2c->Instance->CR2 |= I2C_CR2_NACK;
1511 /* Clear ADDR flag */
1512 __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
1514 /* Wait until DIR flag is set Receiver mode */
1515 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_DIR, SET, I2C_TIMEOUT_DIR) != HAL_OK)
1517 /* Disable Address Acknowledge */
1518 hi2c->Instance->CR2 |= I2C_CR2_NACK;
1522 /* Enable DMA Request */
1523 hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
1525 /* Process Unlocked */
1537 * @brief Write an amount of data in blocking mode to a specific memory address
1538 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1539 * the configuration information for the specified I2C.
1540 * @param DevAddress: Target device address
1541 * @param MemAddress: Internal memory address
1542 * @param MemAddSize: Size of internal memory address
1543 * @param pData: Pointer to data buffer
1544 * @param Size: Amount of data to be sent
1545 * @param Timeout: Timeout duration
1546 * @retval HAL status
1548 HAL_StatusTypeDef HAL_I2C_Mem_Write(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
1550 uint32_t Sizetmp = 0;
1552 /* Check the parameters */
1553 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
1555 if(hi2c->State == HAL_I2C_STATE_READY)
1557 if((pData == NULL) || (Size == 0))
1562 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1567 /* Process Locked */
1570 hi2c->State = HAL_I2C_STATE_MEM_BUSY_TX;
1571 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1573 /* Send Slave Address and Memory Address */
1574 if(I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, Timeout) != HAL_OK)
1576 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1578 /* Process Unlocked */
1584 /* Process Unlocked */
1590 /* Set NBYTES to write and reload if size > 255 */
1591 /* Size > 255, need to set RELOAD bit */
1594 I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
1599 I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
1605 /* Wait until TXIS flag is set */
1606 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
1608 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1618 /* Write data to DR */
1619 hi2c->Instance->TXDR = (*pData++);
1623 if((Sizetmp == 0)&&(Size!=0))
1625 /* Wait until TCR flag is set */
1626 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout) != HAL_OK)
1634 I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
1639 I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
1646 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
1647 /* Wait until STOPF flag is reset */
1648 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
1650 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1660 /* Clear STOP Flag */
1661 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
1663 /* Clear Configuration Register 2 */
1664 __I2C_RESET_CR2(hi2c);
1666 hi2c->State = HAL_I2C_STATE_READY;
1668 /* Process Unlocked */
1680 * @brief Read an amount of data in blocking mode from a specific memory address
1681 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1682 * the configuration information for the specified I2C.
1683 * @param DevAddress: Target device address
1684 * @param MemAddress: Internal memory address
1685 * @param MemAddSize: Size of internal memory address
1686 * @param pData: Pointer to data buffer
1687 * @param Size: Amount of data to be sent
1688 * @param Timeout: Timeout duration
1689 * @retval HAL status
1691 HAL_StatusTypeDef HAL_I2C_Mem_Read(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
1693 uint32_t Sizetmp = 0;
1695 /* Check the parameters */
1696 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
1698 if(hi2c->State == HAL_I2C_STATE_READY)
1700 if((pData == NULL) || (Size == 0))
1705 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1710 /* Process Locked */
1713 hi2c->State = HAL_I2C_STATE_MEM_BUSY_RX;
1714 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1716 /* Send Slave Address and Memory Address */
1717 if(I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, Timeout) != HAL_OK)
1719 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1721 /* Process Unlocked */
1727 /* Process Unlocked */
1733 /* Send Slave Address */
1734 /* Set NBYTES to write and reload if size > 255 and generate RESTART */
1735 /* Size > 255, need to set RELOAD bit */
1738 I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
1743 I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
1749 /* Wait until RXNE flag is set */
1750 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, Timeout) != HAL_OK)
1755 /* Read data from RXDR */
1756 (*pData++) = hi2c->Instance->RXDR;
1758 /* Decrement the Size counter */
1762 if((Sizetmp == 0)&&(Size!=0))
1764 /* Wait until TCR flag is set */
1765 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout) != HAL_OK)
1772 I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
1777 I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
1784 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
1785 /* Wait until STOPF flag is reset */
1786 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
1788 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1798 /* Clear STOP Flag */
1799 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
1801 /* Clear Configuration Register 2 */
1802 __I2C_RESET_CR2(hi2c);
1804 hi2c->State = HAL_I2C_STATE_READY;
1806 /* Process Unlocked */
1818 * @brief Write an amount of data in no-blocking mode with Interrupt to a specific memory address
1819 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1820 * the configuration information for the specified I2C.
1821 * @param DevAddress: Target device address
1822 * @param MemAddress: Internal memory address
1823 * @param MemAddSize: Size of internal memory address
1824 * @param pData: Pointer to data buffer
1825 * @param Size: Amount of data to be sent
1826 * @retval HAL status
1828 HAL_StatusTypeDef HAL_I2C_Mem_Write_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
1830 /* Check the parameters */
1831 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
1833 if(hi2c->State == HAL_I2C_STATE_READY)
1835 if((pData == NULL) || (Size == 0))
1840 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1845 /* Process Locked */
1848 hi2c->State = HAL_I2C_STATE_MEM_BUSY_TX;
1849 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1851 hi2c->pBuffPtr = pData;
1852 hi2c->XferCount = Size;
1855 hi2c->XferSize = 255;
1859 hi2c->XferSize = Size;
1862 /* Send Slave Address and Memory Address */
1863 if(I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG) != HAL_OK)
1865 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1867 /* Process Unlocked */
1873 /* Process Unlocked */
1879 /* Set NBYTES to write and reload if size > 255 */
1880 /* Size > 255, need to set RELOAD bit */
1881 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
1883 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
1887 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
1890 /* Process Unlocked */
1893 /* Note : The I2C interrupts must be enabled after unlocking current process
1894 to avoid the risk of I2C interrupt handle execution before current
1897 /* Enable ERR, TC, STOP, NACK, TXI interrupt */
1898 /* possible to enable all of these */
1899 /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
1900 __HAL_I2C_ENABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_TXI );
1911 * @brief Read an amount of data in no-blocking mode with Interrupt from a specific memory address
1912 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1913 * the configuration information for the specified I2C.
1914 * @param DevAddress: Target device address
1915 * @param MemAddress: Internal memory address
1916 * @param MemAddSize: Size of internal memory address
1917 * @param pData: Pointer to data buffer
1918 * @param Size: Amount of data to be sent
1919 * @retval HAL status
1921 HAL_StatusTypeDef HAL_I2C_Mem_Read_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
1923 /* Check the parameters */
1924 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
1926 if(hi2c->State == HAL_I2C_STATE_READY)
1928 if((pData == NULL) || (Size == 0))
1933 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1938 /* Process Locked */
1941 hi2c->State = HAL_I2C_STATE_MEM_BUSY_RX;
1943 hi2c->pBuffPtr = pData;
1944 hi2c->XferCount = Size;
1947 hi2c->XferSize = 255;
1951 hi2c->XferSize = Size;
1954 /* Send Slave Address and Memory Address */
1955 if(I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG) != HAL_OK)
1957 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1959 /* Process Unlocked */
1965 /* Process Unlocked */
1971 /* Set NBYTES to write and reload if size > 255 and generate RESTART */
1972 /* Size > 255, need to set RELOAD bit */
1973 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
1975 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
1979 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
1982 /* Process Unlocked */
1985 /* Note : The I2C interrupts must be enabled after unlocking current process
1986 to avoid the risk of I2C interrupt handle execution before current
1989 /* Enable ERR, TC, STOP, NACK, RXI interrupt */
1990 /* possible to enable all of these */
1991 /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
1992 __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_RXI );
2003 * @brief Write an amount of data in no-blocking mode with DMA to a specific memory address
2004 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2005 * the configuration information for the specified I2C.
2006 * @param DevAddress: Target device address
2007 * @param MemAddress: Internal memory address
2008 * @param MemAddSize: Size of internal memory address
2009 * @param pData: Pointer to data buffer
2010 * @param Size: Amount of data to be sent
2011 * @retval HAL status
2013 HAL_StatusTypeDef HAL_I2C_Mem_Write_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
2015 /* Check the parameters */
2016 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
2018 if(hi2c->State == HAL_I2C_STATE_READY)
2020 if((pData == NULL) || (Size == 0))
2025 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
2030 /* Process Locked */
2033 hi2c->State = HAL_I2C_STATE_MEM_BUSY_TX;
2034 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
2036 hi2c->pBuffPtr = pData;
2037 hi2c->XferCount = Size;
2040 hi2c->XferSize = 255;
2044 hi2c->XferSize = Size;
2047 /* Set the I2C DMA transfer complete callback */
2048 hi2c->hdmatx->XferCpltCallback = I2C_DMAMemTransmitCplt;
2050 /* Set the DMA error callback */
2051 hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
2053 /* Enable the DMA channel */
2054 HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
2056 /* Send Slave Address and Memory Address */
2057 if(I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG) != HAL_OK)
2059 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
2061 /* Process Unlocked */
2067 /* Process Unlocked */
2073 /* Send Slave Address */
2074 /* Set NBYTES to write and reload if size > 255 */
2075 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
2077 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
2081 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
2084 /* Wait until TXIS flag is set */
2085 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, I2C_TIMEOUT_TXIS) != HAL_OK)
2087 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
2097 /* Enable DMA Request */
2098 hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
2100 /* Process Unlocked */
2112 * @brief Reads an amount of data in no-blocking mode with DMA from a specific memory address.
2113 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2114 * the configuration information for the specified I2C.
2115 * @param DevAddress: Target device address
2116 * @param MemAddress: Internal memory address
2117 * @param MemAddSize: Size of internal memory address
2118 * @param pData: Pointer to data buffer
2119 * @param Size: Amount of data to be read
2120 * @retval HAL status
2122 HAL_StatusTypeDef HAL_I2C_Mem_Read_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
2124 /* Check the parameters */
2125 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
2127 if(hi2c->State == HAL_I2C_STATE_READY)
2129 if((pData == NULL) || (Size == 0))
2134 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
2139 /* Process Locked */
2142 hi2c->State = HAL_I2C_STATE_MEM_BUSY_RX;
2144 hi2c->pBuffPtr = pData;
2145 hi2c->XferCount = Size;
2148 hi2c->XferSize = 255;
2152 hi2c->XferSize = Size;
2155 /* Set the I2C DMA transfer complete callback */
2156 hi2c->hdmarx->XferCpltCallback = I2C_DMAMemReceiveCplt;
2158 /* Set the DMA error callback */
2159 hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
2161 /* Enable the DMA channel */
2162 HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData, hi2c->XferSize);
2164 /* Send Slave Address and Memory Address */
2165 if(I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG) != HAL_OK)
2167 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
2169 /* Process Unlocked */
2175 /* Process Unlocked */
2181 /* Set NBYTES to write and reload if size > 255 and generate RESTART */
2182 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
2184 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
2188 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
2191 /* Wait until RXNE flag is set */
2192 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, I2C_TIMEOUT_RXNE) != HAL_OK)
2197 /* Enable DMA Request */
2198 hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
2200 /* Process Unlocked */
2212 * @brief Checks if target device is ready for communication.
2213 * @note This function is used with Memory devices
2214 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2215 * the configuration information for the specified I2C.
2216 * @param DevAddress: Target device address
2217 * @param Trials: Number of trials
2218 * @param Timeout: Timeout duration
2219 * @retval HAL status
2221 HAL_StatusTypeDef HAL_I2C_IsDeviceReady(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout)
2223 uint32_t tickstart = 0;
2225 __IO uint32_t I2C_Trials = 0;
2227 if(hi2c->State == HAL_I2C_STATE_READY)
2229 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
2234 /* Process Locked */
2237 hi2c->State = HAL_I2C_STATE_BUSY;
2238 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
2242 /* Generate Start */
2243 hi2c->Instance->CR2 = __I2C_GENERATE_START(hi2c->Init.AddressingMode,DevAddress);
2245 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
2246 /* Wait until STOPF flag is set or a NACK flag is set*/
2247 tickstart = HAL_GetTick();
2248 while((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET) && (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == RESET) && (hi2c->State != HAL_I2C_STATE_TIMEOUT))
2250 if(Timeout != HAL_MAX_DELAY)
2252 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
2254 /* Device is ready */
2255 hi2c->State = HAL_I2C_STATE_READY;
2256 /* Process Unlocked */
2263 /* Check if the NACKF flag has not been set */
2264 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == RESET)
2266 /* Wait until STOPF flag is reset */
2267 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout) != HAL_OK)
2272 /* Clear STOP Flag */
2273 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
2275 /* Device is ready */
2276 hi2c->State = HAL_I2C_STATE_READY;
2278 /* Process Unlocked */
2285 /* Wait until STOPF flag is reset */
2286 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout) != HAL_OK)
2291 /* Clear NACK Flag */
2292 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
2294 /* Clear STOP Flag, auto generated with autoend*/
2295 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
2298 /* Check if the maximum allowed number of trials has been reached */
2299 if (I2C_Trials++ == Trials)
2302 hi2c->Instance->CR2 |= I2C_CR2_STOP;
2304 /* Wait until STOPF flag is reset */
2305 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout) != HAL_OK)
2310 /* Clear STOP Flag */
2311 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
2313 }while(I2C_Trials < Trials);
2315 hi2c->State = HAL_I2C_STATE_READY;
2317 /* Process Unlocked */
2331 /** @defgroup IRQ_Handler_and_Callbacks IRQ Handler and Callbacks
2336 * @brief This function handles I2C event interrupt request.
2337 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2338 * the configuration information for the specified I2C.
2341 void HAL_I2C_EV_IRQHandler(I2C_HandleTypeDef *hi2c)
2343 /* I2C in mode Transmitter ---------------------------------------------------*/
2344 if (((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TCR) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TC) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == SET)) && (__HAL_I2C_GET_IT_SOURCE(hi2c, (I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_TXI | I2C_IT_ADDRI)) == SET))
2346 /* Slave mode selected */
2347 if (hi2c->State == HAL_I2C_STATE_SLAVE_BUSY_TX)
2349 I2C_SlaveTransmit_ISR(hi2c);
2353 if (((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TCR) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TC) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)) && (__HAL_I2C_GET_IT_SOURCE(hi2c, (I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_TXI)) == SET))
2355 /* Master mode selected */
2356 if ((hi2c->State == HAL_I2C_STATE_MASTER_BUSY_TX) || (hi2c->State == HAL_I2C_STATE_MEM_BUSY_TX))
2358 I2C_MasterTransmit_ISR(hi2c);
2362 /* I2C in mode Receiver ----------------------------------------------------*/
2363 if (((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TCR) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TC) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == SET)) && (__HAL_I2C_GET_IT_SOURCE(hi2c, (I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_RXI | I2C_IT_ADDRI)) == SET))
2365 /* Slave mode selected */
2366 if (hi2c->State == HAL_I2C_STATE_SLAVE_BUSY_RX)
2368 I2C_SlaveReceive_ISR(hi2c);
2371 if (((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TCR) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TC) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)) && (__HAL_I2C_GET_IT_SOURCE(hi2c, (I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_RXI)) == SET))
2373 /* Master mode selected */
2374 if ((hi2c->State == HAL_I2C_STATE_MASTER_BUSY_RX) || (hi2c->State == HAL_I2C_STATE_MEM_BUSY_RX))
2376 I2C_MasterReceive_ISR(hi2c);
2382 * @brief This function handles I2C error interrupt request.
2383 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2384 * the configuration information for the specified I2C.
2387 void HAL_I2C_ER_IRQHandler(I2C_HandleTypeDef *hi2c)
2389 /* I2C Bus error interrupt occurred ------------------------------------*/
2390 if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BERR) == SET) && (__HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_ERRI) == SET))
2392 hi2c->ErrorCode |= HAL_I2C_ERROR_BERR;
2394 /* Clear BERR flag */
2395 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_BERR);
2398 /* I2C Over-Run/Under-Run interrupt occurred ----------------------------------------*/
2399 if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_OVR) == SET) && (__HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_ERRI) == SET))
2401 hi2c->ErrorCode |= HAL_I2C_ERROR_OVR;
2403 /* Clear OVR flag */
2404 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_OVR);
2407 /* I2C Arbitration Loss error interrupt occurred -------------------------------------*/
2408 if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ARLO) == SET) && (__HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_ERRI) == SET))
2410 hi2c->ErrorCode |= HAL_I2C_ERROR_ARLO;
2412 /* Clear ARLO flag */
2413 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ARLO);
2416 /* Call the Error Callback in case of Error detected */
2417 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
2419 hi2c->State = HAL_I2C_STATE_READY;
2421 HAL_I2C_ErrorCallback(hi2c);
2426 * @brief Master Tx Transfer completed callbacks.
2427 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2428 * the configuration information for the specified I2C.
2431 __weak void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c)
2433 /* NOTE : This function Should not be modified, when the callback is needed,
2434 the HAL_I2C_TxCpltCallback could be implemented in the user file
2439 * @brief Master Rx Transfer completed callbacks.
2440 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2441 * the configuration information for the specified I2C.
2444 __weak void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c)
2446 /* NOTE : This function Should not be modified, when the callback is needed,
2447 the HAL_I2C_TxCpltCallback could be implemented in the user file
2451 /** @brief Slave Tx Transfer completed callbacks.
2452 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2453 * the configuration information for the specified I2C.
2456 __weak void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c)
2458 /* NOTE : This function Should not be modified, when the callback is needed,
2459 the HAL_I2C_TxCpltCallback could be implemented in the user file
2464 * @brief Slave Rx Transfer completed callbacks.
2465 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2466 * the configuration information for the specified I2C.
2469 __weak void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c)
2471 /* NOTE : This function Should not be modified, when the callback is needed,
2472 the HAL_I2C_TxCpltCallback could be implemented in the user file
2477 * @brief Memory Tx Transfer completed callbacks.
2478 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2479 * the configuration information for the specified I2C.
2482 __weak void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef *hi2c)
2484 /* NOTE : This function Should not be modified, when the callback is needed,
2485 the HAL_I2C_TxCpltCallback could be implemented in the user file
2490 * @brief Memory Rx Transfer completed callbacks.
2491 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2492 * the configuration information for the specified I2C.
2495 __weak void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c)
2497 /* NOTE : This function Should not be modified, when the callback is needed,
2498 the HAL_I2C_TxCpltCallback could be implemented in the user file
2503 * @brief I2C error 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_ErrorCallback(I2C_HandleTypeDef *hi2c)
2510 /* NOTE : This function Should not be modified, when the callback is needed,
2511 the HAL_I2C_ErrorCallback could be implemented in the user file
2520 /** @defgroup I2C_Exported_Functions_Group3 Peripheral State and Errors functions
2521 * @brief Peripheral State and Errors functions
2524 ===============================================================================
2525 ##### Peripheral State and Errors functions #####
2526 ===============================================================================
2528 This subsection permit to get in run-time the status of the peripheral
2536 * @brief Returns the I2C state.
2537 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2538 * the configuration information for the specified I2C.
2541 HAL_I2C_StateTypeDef HAL_I2C_GetState(I2C_HandleTypeDef *hi2c)
2547 * @brief Return the I2C error code
2548 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2549 * the configuration information for the specified I2C.
2550 * @retval I2C Error Code
2552 uint32_t HAL_I2C_GetError(I2C_HandleTypeDef *hi2c)
2554 return hi2c->ErrorCode;
2565 /** @addtogroup I2C_Private_Functions
2570 * @brief Handle Interrupt Flags Master Transmit Mode
2571 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2572 * the configuration information for the specified I2C.
2573 * @retval HAL status
2575 static HAL_StatusTypeDef I2C_MasterTransmit_ISR(I2C_HandleTypeDef *hi2c)
2577 uint16_t DevAddress;
2579 /* Process Locked */
2582 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == SET)
2584 /* Write data to TXDR */
2585 hi2c->Instance->TXDR = (*hi2c->pBuffPtr++);
2589 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TCR) == SET)
2591 if((hi2c->XferSize == 0)&&(hi2c->XferCount!=0))
2593 DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);
2595 if(hi2c->XferCount > 255)
2597 I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
2598 hi2c->XferSize = 255;
2602 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferCount, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
2603 hi2c->XferSize = hi2c->XferCount;
2608 /* Process Unlocked */
2611 /* Wrong size Status regarding TCR flag event */
2612 hi2c->ErrorCode |= HAL_I2C_ERROR_SIZE;
2613 HAL_I2C_ErrorCallback(hi2c);
2616 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TC) == SET)
2618 if(hi2c->XferCount == 0)
2621 hi2c->Instance->CR2 |= I2C_CR2_STOP;
2625 /* Process Unlocked */
2628 /* Wrong size Status regarding TCR flag event */
2629 hi2c->ErrorCode |= HAL_I2C_ERROR_SIZE;
2630 HAL_I2C_ErrorCallback(hi2c);
2633 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
2635 /* Disable ERR, TC, STOP, NACK, TXI interrupt */
2636 __HAL_I2C_DISABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_TXI );
2638 /* Clear STOP Flag */
2639 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
2641 /* Clear Configuration Register 2 */
2642 __I2C_RESET_CR2(hi2c);
2644 hi2c->State = HAL_I2C_STATE_READY;
2646 /* Process Unlocked */
2649 if(hi2c->State == HAL_I2C_STATE_MEM_BUSY_TX)
2651 HAL_I2C_MemTxCpltCallback(hi2c);
2655 HAL_I2C_MasterTxCpltCallback(hi2c);
2658 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
2660 /* Clear NACK Flag */
2661 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
2663 /* Process Unlocked */
2666 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
2667 HAL_I2C_ErrorCallback(hi2c);
2670 /* Process Unlocked */
2677 * @brief Handle Interrupt Flags Master Receive Mode
2678 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2679 * the configuration information for the specified I2C.
2680 * @retval HAL status
2682 static HAL_StatusTypeDef I2C_MasterReceive_ISR(I2C_HandleTypeDef *hi2c)
2684 uint16_t DevAddress;
2686 /* Process Locked */
2689 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
2691 /* Read data from RXDR */
2692 (*hi2c->pBuffPtr++) = hi2c->Instance->RXDR;
2696 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TCR) == SET)
2698 if((hi2c->XferSize == 0)&&(hi2c->XferCount!=0))
2700 DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);
2702 if(hi2c->XferCount > 255)
2704 I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
2705 hi2c->XferSize = 255;
2709 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferCount, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
2710 hi2c->XferSize = hi2c->XferCount;
2715 /* Process Unlocked */
2718 /* Wrong size Status regarding TCR flag event */
2719 hi2c->ErrorCode |= HAL_I2C_ERROR_SIZE;
2720 HAL_I2C_ErrorCallback(hi2c);
2723 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TC) == SET)
2725 if(hi2c->XferCount == 0)
2728 hi2c->Instance->CR2 |= I2C_CR2_STOP;
2732 /* Process Unlocked */
2735 /* Wrong size Status regarding TCR flag event */
2736 hi2c->ErrorCode |= HAL_I2C_ERROR_SIZE;
2737 HAL_I2C_ErrorCallback(hi2c);
2740 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
2742 /* Disable ERR, TC, STOP, NACK, TXI interrupt */
2743 __HAL_I2C_DISABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_RXI );
2745 /* Clear STOP Flag */
2746 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
2748 /* Clear Configuration Register 2 */
2749 __I2C_RESET_CR2(hi2c);
2751 hi2c->State = HAL_I2C_STATE_READY;
2753 /* Process Unlocked */
2756 if(hi2c->State == HAL_I2C_STATE_MEM_BUSY_RX)
2758 HAL_I2C_MemRxCpltCallback(hi2c);
2762 HAL_I2C_MasterRxCpltCallback(hi2c);
2765 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
2767 /* Clear NACK Flag */
2768 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
2770 /* Process Unlocked */
2773 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
2774 HAL_I2C_ErrorCallback(hi2c);
2777 /* Process Unlocked */
2785 * @brief Handle Interrupt Flags Slave Transmit Mode
2786 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2787 * the configuration information for the specified I2C.
2788 * @retval HAL status
2790 static HAL_StatusTypeDef I2C_SlaveTransmit_ISR(I2C_HandleTypeDef *hi2c)
2792 /* Process locked */
2795 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) != RESET)
2797 /* Check that I2C transfer finished */
2798 /* if yes, normal usecase, a NACK is sent by the MASTER when Transfer is finished */
2799 /* Mean XferCount == 0*/
2800 /* So clear Flag NACKF only */
2801 if(hi2c->XferCount == 0)
2803 /* Clear NACK Flag */
2804 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
2806 /* Process Unlocked */
2811 /* if no, error usecase, a Non-Acknowledge of last Data is generated by the MASTER*/
2812 /* Clear NACK Flag */
2813 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
2815 /* Set ErrorCode corresponding to a Non-Acknowledge */
2816 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
2818 /* Process Unlocked */
2821 /* Call the Error callback to prevent upper layer */
2822 HAL_I2C_ErrorCallback(hi2c);
2825 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == SET)
2827 /* Clear ADDR flag */
2828 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
2830 /* Check first if STOPF is set */
2831 /* to prevent a Write Data in TX buffer */
2832 /* which is stuck in TXDR until next */
2833 /* communication with Master */
2834 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
2836 /* Disable ERRI, TCI, STOPI, NACKI, ADDRI, RXI, TXI interrupt */
2837 __HAL_I2C_DISABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI );
2839 /* Disable Address Acknowledge */
2840 hi2c->Instance->CR2 |= I2C_CR2_NACK;
2842 /* Clear STOP Flag */
2843 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
2845 hi2c->State = HAL_I2C_STATE_READY;
2847 /* Process Unlocked */
2850 HAL_I2C_SlaveTxCpltCallback(hi2c);
2852 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == SET)
2854 /* Write data to TXDR only if XferCount not reach "0" */
2855 /* A TXIS flag can be set, during STOP treatment */
2856 if(hi2c->XferCount > 0)
2858 /* Write data to TXDR */
2859 hi2c->Instance->TXDR = (*hi2c->pBuffPtr++);
2864 /* Process Unlocked */
2871 * @brief Handle Interrupt Flags Slave Receive Mode
2872 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2873 * the configuration information for the specified I2C.
2874 * @retval HAL status
2876 static HAL_StatusTypeDef I2C_SlaveReceive_ISR(I2C_HandleTypeDef *hi2c)
2878 /* Process Locked */
2881 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) != RESET)
2883 /* Clear NACK Flag */
2884 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
2886 /* Process Unlocked */
2889 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
2890 HAL_I2C_ErrorCallback(hi2c);
2892 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == SET)
2894 /* Clear ADDR flag */
2895 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
2897 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
2899 /* Read data from RXDR */
2900 (*hi2c->pBuffPtr++) = hi2c->Instance->RXDR;
2904 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
2906 /* Disable ERRI, TCI, STOPI, NACKI, ADDRI, RXI, TXI interrupt */
2907 __HAL_I2C_DISABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_RXI );
2909 /* Disable Address Acknowledge */
2910 hi2c->Instance->CR2 |= I2C_CR2_NACK;
2912 /* Clear STOP Flag */
2913 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
2915 hi2c->State = HAL_I2C_STATE_READY;
2917 /* Process Unlocked */
2920 HAL_I2C_SlaveRxCpltCallback(hi2c);
2923 /* Process Unlocked */
2930 * @brief Master sends target device address followed by internal memory address for write request.
2931 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2932 * the configuration information for the specified I2C.
2933 * @param DevAddress: Target device address
2934 * @param MemAddress: Internal memory address
2935 * @param MemAddSize: Size of internal memory address
2936 * @param Timeout: Timeout duration
2937 * @retval HAL status
2939 static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout)
2941 I2C_TransferConfig(hi2c,DevAddress,MemAddSize, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);
2943 /* Wait until TXIS flag is set */
2944 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
2946 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
2956 /* If Memory address size is 8Bit */
2957 if(MemAddSize == I2C_MEMADD_SIZE_8BIT)
2959 /* Send Memory Address */
2960 hi2c->Instance->TXDR = __I2C_MEM_ADD_LSB(MemAddress);
2962 /* If Memory address size is 16Bit */
2965 /* Send MSB of Memory Address */
2966 hi2c->Instance->TXDR = __I2C_MEM_ADD_MSB(MemAddress);
2968 /* Wait until TXIS flag is set */
2969 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
2971 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
2981 /* Send LSB of Memory Address */
2982 hi2c->Instance->TXDR = __I2C_MEM_ADD_LSB(MemAddress);
2985 /* Wait until TCR flag is set */
2986 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout) != HAL_OK)
2995 * @brief Master sends target device address followed by internal memory address for read request.
2996 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2997 * the configuration information for the specified I2C.
2998 * @param DevAddress: Target device address
2999 * @param MemAddress: Internal memory address
3000 * @param MemAddSize: Size of internal memory address
3001 * @param Timeout: Timeout duration
3002 * @retval HAL status
3004 static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout)
3006 I2C_TransferConfig(hi2c,DevAddress,MemAddSize, I2C_SOFTEND_MODE, I2C_GENERATE_START_WRITE);
3008 /* Wait until TXIS flag is set */
3009 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
3011 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3021 /* If Memory address size is 8Bit */
3022 if(MemAddSize == I2C_MEMADD_SIZE_8BIT)
3024 /* Send Memory Address */
3025 hi2c->Instance->TXDR = __I2C_MEM_ADD_LSB(MemAddress);
3027 /* If Mememory address size is 16Bit */
3030 /* Send MSB of Memory Address */
3031 hi2c->Instance->TXDR = __I2C_MEM_ADD_MSB(MemAddress);
3033 /* Wait until TXIS flag is set */
3034 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
3036 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3046 /* Send LSB of Memory Address */
3047 hi2c->Instance->TXDR = __I2C_MEM_ADD_LSB(MemAddress);
3050 /* Wait until TC flag is set */
3051 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TC, RESET, Timeout) != HAL_OK)
3060 * @brief DMA I2C master transmit process complete callback.
3061 * @param hdma: DMA handle
3064 static void I2C_DMAMasterTransmitCplt(DMA_HandleTypeDef *hdma)
3066 uint16_t DevAddress;
3067 I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3069 /* Check if last DMA request was done with RELOAD */
3070 /* Set NBYTES to write and reload if size > 255 */
3071 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
3073 /* Wait until TCR flag is set */
3074 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, I2C_TIMEOUT_TCR) != HAL_OK)
3076 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3079 /* Disable DMA Request */
3080 hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
3082 /* Check if Errors has been detected during transfer */
3083 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3085 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3086 /* Wait until STOPF flag is reset */
3087 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3089 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3091 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3095 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3099 /* Clear STOP Flag */
3100 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3102 /* Clear Configuration Register 2 */
3103 __I2C_RESET_CR2(hi2c);
3105 hi2c->XferCount = 0;
3107 hi2c->State = HAL_I2C_STATE_READY;
3108 HAL_I2C_ErrorCallback(hi2c);
3112 hi2c->pBuffPtr += hi2c->XferSize;
3113 hi2c->XferCount -= hi2c->XferSize;
3114 if(hi2c->XferCount > 255)
3116 hi2c->XferSize = 255;
3120 hi2c->XferSize = hi2c->XferCount;
3123 DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);
3125 /* Enable the DMA channel */
3126 HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
3128 /* Send Slave Address */
3129 /* Set NBYTES to write and reload if size > 255 */
3130 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
3132 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
3136 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
3139 /* Wait until TXIS flag is set */
3140 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, I2C_TIMEOUT_TXIS) != HAL_OK)
3142 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3143 /* Wait until STOPF flag is reset */
3144 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3146 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3148 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3152 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3156 /* Clear STOP Flag */
3157 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3159 /* Clear Configuration Register 2 */
3160 __I2C_RESET_CR2(hi2c);
3162 hi2c->XferCount = 0;
3164 hi2c->State = HAL_I2C_STATE_READY;
3165 HAL_I2C_ErrorCallback(hi2c);
3169 /* Enable DMA Request */
3170 hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
3176 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3177 /* Wait until STOPF flag is reset */
3178 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3180 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3182 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3186 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3190 /* Clear STOP Flag */
3191 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3193 /* Clear Configuration Register 2 */
3194 __I2C_RESET_CR2(hi2c);
3196 /* Disable DMA Request */
3197 hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
3199 hi2c->XferCount = 0;
3201 hi2c->State = HAL_I2C_STATE_READY;
3203 /* Check if Errors has been detected during transfer */
3204 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3206 HAL_I2C_ErrorCallback(hi2c);
3210 HAL_I2C_MasterTxCpltCallback(hi2c);
3216 * @brief DMA I2C slave transmit process complete callback.
3217 * @param hdma: DMA handle
3220 static void I2C_DMASlaveTransmitCplt(DMA_HandleTypeDef *hdma)
3222 I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3224 /* Wait until STOP flag is set */
3225 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3227 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3229 /* Normal Use case, a AF is generated by master */
3230 /* to inform slave the end of transfer */
3231 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
3235 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3239 /* Clear STOP flag */
3240 __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_STOPF);
3242 /* Wait until BUSY flag is reset */
3243 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY) != HAL_OK)
3245 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3248 /* Disable DMA Request */
3249 hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
3251 hi2c->XferCount = 0;
3253 hi2c->State = HAL_I2C_STATE_READY;
3255 /* Check if Errors has been detected during transfer */
3256 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3258 HAL_I2C_ErrorCallback(hi2c);
3262 HAL_I2C_SlaveTxCpltCallback(hi2c);
3267 * @brief DMA I2C master receive process complete callback
3268 * @param hdma: DMA handle
3271 static void I2C_DMAMasterReceiveCplt(DMA_HandleTypeDef *hdma)
3273 I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3274 uint16_t DevAddress;
3276 /* Check if last DMA request was done with RELOAD */
3277 /* Set NBYTES to write and reload if size > 255 */
3278 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
3280 /* Wait until TCR flag is set */
3281 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, I2C_TIMEOUT_TCR) != HAL_OK)
3283 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3286 /* Disable DMA Request */
3287 hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
3289 /* Check if Errors has been detected during transfer */
3290 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3292 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3293 /* Wait until STOPF flag is reset */
3294 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3296 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3298 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3302 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3306 /* Clear STOP Flag */
3307 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3309 /* Clear Configuration Register 2 */
3310 __I2C_RESET_CR2(hi2c);
3312 hi2c->XferCount = 0;
3314 hi2c->State = HAL_I2C_STATE_READY;
3315 HAL_I2C_ErrorCallback(hi2c);
3319 hi2c->pBuffPtr += hi2c->XferSize;
3320 hi2c->XferCount -= hi2c->XferSize;
3321 if(hi2c->XferCount > 255)
3323 hi2c->XferSize = 255;
3327 hi2c->XferSize = hi2c->XferCount;
3330 DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);
3332 /* Enable the DMA channel */
3333 HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);
3335 /* Send Slave Address */
3336 /* Set NBYTES to write and reload if size > 255 */
3337 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
3339 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
3343 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
3346 /* Wait until RXNE flag is set */
3347 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, I2C_TIMEOUT_RXNE) != HAL_OK)
3349 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3352 /* Check if Errors has been detected during transfer */
3353 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3355 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3356 /* Wait until STOPF flag is reset */
3357 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3359 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3361 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3365 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3369 /* Clear STOP Flag */
3370 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3372 /* Clear Configuration Register 2 */
3373 __I2C_RESET_CR2(hi2c);
3375 hi2c->XferCount = 0;
3377 hi2c->State = HAL_I2C_STATE_READY;
3379 HAL_I2C_ErrorCallback(hi2c);
3383 /* Enable DMA Request */
3384 hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
3390 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3391 /* Wait until STOPF flag is reset */
3392 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3394 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3396 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3400 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3404 /* Clear STOP Flag */
3405 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3407 /* Clear Configuration Register 2 */
3408 __I2C_RESET_CR2(hi2c);
3410 /* Disable DMA Request */
3411 hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
3413 hi2c->XferCount = 0;
3415 hi2c->State = HAL_I2C_STATE_READY;
3417 /* Check if Errors has been detected during transfer */
3418 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3420 HAL_I2C_ErrorCallback(hi2c);
3424 HAL_I2C_MasterRxCpltCallback(hi2c);
3430 * @brief DMA I2C slave receive process complete callback.
3431 * @param hdma: DMA handle
3434 static void I2C_DMASlaveReceiveCplt(DMA_HandleTypeDef *hdma)
3436 I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3438 /* Wait until STOPF flag is reset */
3439 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3441 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3443 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3447 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3451 /* Clear STOPF flag */
3452 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3454 /* Wait until BUSY flag is reset */
3455 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY) != HAL_OK)
3457 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3460 /* Disable DMA Request */
3461 hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
3463 /* Disable Address Acknowledge */
3464 hi2c->Instance->CR2 |= I2C_CR2_NACK;
3466 hi2c->XferCount = 0;
3468 hi2c->State = HAL_I2C_STATE_READY;
3470 /* Check if Errors has been detected during transfer */
3471 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3473 HAL_I2C_ErrorCallback(hi2c);
3477 HAL_I2C_SlaveRxCpltCallback(hi2c);
3482 * @brief DMA I2C Memory Write process complete callback
3483 * @param hdma : DMA handle
3486 static void I2C_DMAMemTransmitCplt(DMA_HandleTypeDef *hdma)
3488 uint16_t DevAddress;
3489 I2C_HandleTypeDef* hi2c = ( I2C_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
3491 /* Check if last DMA request was done with RELOAD */
3492 /* Set NBYTES to write and reload if size > 255 */
3493 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
3495 /* Wait until TCR flag is set */
3496 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, I2C_TIMEOUT_TCR) != HAL_OK)
3498 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3501 /* Disable DMA Request */
3502 hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
3504 /* Check if Errors has been detected during transfer */
3505 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3507 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3508 /* Wait until STOPF flag is reset */
3509 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3511 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3513 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3517 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3521 /* Clear STOP Flag */
3522 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3524 /* Clear Configuration Register 2 */
3525 __I2C_RESET_CR2(hi2c);
3527 hi2c->XferCount = 0;
3529 hi2c->State = HAL_I2C_STATE_READY;
3530 HAL_I2C_ErrorCallback(hi2c);
3534 hi2c->pBuffPtr += hi2c->XferSize;
3535 hi2c->XferCount -= hi2c->XferSize;
3536 if(hi2c->XferCount > 255)
3538 hi2c->XferSize = 255;
3542 hi2c->XferSize = hi2c->XferCount;
3545 DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);
3547 /* Enable the DMA channel */
3548 HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
3550 /* Send Slave Address */
3551 /* Set NBYTES to write and reload if size > 255 */
3552 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
3554 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
3558 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
3561 /* Wait until TXIS flag is set */
3562 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, I2C_TIMEOUT_TXIS) != HAL_OK)
3564 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3565 /* Wait until STOPF flag is reset */
3566 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3568 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3570 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3574 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3578 /* Clear STOP Flag */
3579 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3581 /* Clear Configuration Register 2 */
3582 __I2C_RESET_CR2(hi2c);
3584 hi2c->XferCount = 0;
3586 hi2c->State = HAL_I2C_STATE_READY;
3587 HAL_I2C_ErrorCallback(hi2c);
3591 /* Enable DMA Request */
3592 hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
3598 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3599 /* Wait until STOPF flag is reset */
3600 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3602 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3604 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3608 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3612 /* Clear STOP Flag */
3613 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3615 /* Clear Configuration Register 2 */
3616 __I2C_RESET_CR2(hi2c);
3618 /* Disable DMA Request */
3619 hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
3621 hi2c->XferCount = 0;
3623 hi2c->State = HAL_I2C_STATE_READY;
3625 /* Check if Errors has been detected during transfer */
3626 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3628 HAL_I2C_ErrorCallback(hi2c);
3632 HAL_I2C_MemTxCpltCallback(hi2c);
3638 * @brief DMA I2C Memory Read process complete callback
3639 * @param hdma: DMA handle
3642 static void I2C_DMAMemReceiveCplt(DMA_HandleTypeDef *hdma)
3644 I2C_HandleTypeDef* hi2c = ( I2C_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
3645 uint16_t DevAddress;
3647 /* Check if last DMA request was done with RELOAD */
3648 /* Set NBYTES to write and reload if size > 255 */
3649 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
3651 /* Wait until TCR flag is set */
3652 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, I2C_TIMEOUT_TCR) != HAL_OK)
3654 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3657 /* Disable DMA Request */
3658 hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
3660 /* Check if Errors has been detected during transfer */
3661 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3663 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3664 /* Wait until STOPF flag is reset */
3665 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3667 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3669 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3673 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3677 /* Clear STOP Flag */
3678 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3680 /* Clear Configuration Register 2 */
3681 __I2C_RESET_CR2(hi2c);
3683 hi2c->XferCount = 0;
3685 hi2c->State = HAL_I2C_STATE_READY;
3686 HAL_I2C_ErrorCallback(hi2c);
3690 hi2c->pBuffPtr += hi2c->XferSize;
3691 hi2c->XferCount -= hi2c->XferSize;
3692 if(hi2c->XferCount > 255)
3694 hi2c->XferSize = 255;
3698 hi2c->XferSize = hi2c->XferCount;
3701 DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);
3703 /* Enable the DMA channel */
3704 HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);
3706 /* Send Slave Address */
3707 /* Set NBYTES to write and reload if size > 255 */
3708 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
3710 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
3714 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
3717 /* Wait until RXNE flag is set */
3718 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, I2C_TIMEOUT_RXNE) != HAL_OK)
3720 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3723 /* Check if Errors has been detected during transfer */
3724 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3726 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3727 /* Wait until STOPF flag is reset */
3728 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3730 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3732 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3736 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3740 /* Clear STOP Flag */
3741 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3743 /* Clear Configuration Register 2 */
3744 __I2C_RESET_CR2(hi2c);
3746 hi2c->XferCount = 0;
3748 hi2c->State = HAL_I2C_STATE_READY;
3749 HAL_I2C_ErrorCallback(hi2c);
3753 /* Enable DMA Request */
3754 hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
3760 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3761 /* Wait until STOPF flag is reset */
3762 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3764 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3766 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3770 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3774 /* Clear STOP Flag */
3775 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3777 /* Clear Configuration Register 2 */
3778 __I2C_RESET_CR2(hi2c);
3780 /* Disable DMA Request */
3781 hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
3783 hi2c->XferCount = 0;
3785 hi2c->State = HAL_I2C_STATE_READY;
3787 /* Check if Errors has been detected during transfer */
3788 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3790 HAL_I2C_ErrorCallback(hi2c);
3794 HAL_I2C_MemRxCpltCallback(hi2c);
3800 * @brief DMA I2C communication error callback.
3801 * @param hdma : DMA handle
3804 static void I2C_DMAError(DMA_HandleTypeDef *hdma)
3806 I2C_HandleTypeDef* hi2c = ( I2C_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
3808 /* Disable Acknowledge */
3809 hi2c->Instance->CR2 |= I2C_CR2_NACK;
3811 hi2c->XferCount = 0;
3813 hi2c->State = HAL_I2C_STATE_READY;
3815 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
3817 HAL_I2C_ErrorCallback(hi2c);
3821 * @brief This function handles I2C Communication Timeout.
3822 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
3823 * the configuration information for the specified I2C.
3824 * @param Flag: specifies the I2C flag to check.
3825 * @param Status: The new Flag status (SET or RESET).
3826 * @param Timeout: Timeout duration
3827 * @retval HAL status
3829 static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
3831 uint32_t tickstart = HAL_GetTick();
3833 /* Wait until flag is set */
3836 while(__HAL_I2C_GET_FLAG(hi2c, Flag) == RESET)
3838 /* Check for the Timeout */
3839 if(Timeout != HAL_MAX_DELAY)
3841 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
3843 hi2c->State= HAL_I2C_STATE_READY;
3844 /* Process Unlocked */
3853 while(__HAL_I2C_GET_FLAG(hi2c, Flag) != RESET)
3855 /* Check for the Timeout */
3856 if(Timeout != HAL_MAX_DELAY)
3858 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
3860 hi2c->State= HAL_I2C_STATE_READY;
3861 /* Process Unlocked */
3872 * @brief This function handles I2C Communication Timeout for specific usage of TXIS flag.
3873 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
3874 * the configuration information for the specified I2C.
3875 * @param Timeout: Timeout duration
3876 * @retval HAL status
3878 static HAL_StatusTypeDef I2C_WaitOnTXISFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout)
3880 uint32_t tickstart = HAL_GetTick();
3882 while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == RESET)
3884 /* Check if a NACK is detected */
3885 if(I2C_IsAcknowledgeFailed(hi2c, Timeout) != HAL_OK)
3890 /* Check for the Timeout */
3891 if(Timeout != HAL_MAX_DELAY)
3893 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
3895 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3896 hi2c->State= HAL_I2C_STATE_READY;
3898 /* Process Unlocked */
3909 * @brief This function handles I2C Communication Timeout for specific usage of STOP flag.
3910 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
3911 * the configuration information for the specified I2C.
3912 * @param Timeout: Timeout duration
3913 * @retval HAL status
3915 static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout)
3917 uint32_t tickstart = 0x00;
3918 tickstart = HAL_GetTick();
3920 while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET)
3922 /* Check if a NACK is detected */
3923 if(I2C_IsAcknowledgeFailed(hi2c, Timeout) != HAL_OK)
3928 /* Check for the Timeout */
3929 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
3931 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3932 hi2c->State= HAL_I2C_STATE_READY;
3934 /* Process Unlocked */
3944 * @brief This function handles I2C Communication Timeout for specific usage of RXNE flag.
3945 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
3946 * the configuration information for the specified I2C.
3947 * @param Timeout: Timeout duration
3948 * @retval HAL status
3950 static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout)
3952 uint32_t tickstart = 0x00;
3953 tickstart = HAL_GetTick();
3955 while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == RESET)
3957 /* Check if a STOPF is detected */
3958 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
3960 /* Clear STOP Flag */
3961 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3963 /* Clear Configuration Register 2 */
3964 __I2C_RESET_CR2(hi2c);
3966 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
3967 hi2c->State= HAL_I2C_STATE_READY;
3969 /* Process Unlocked */
3975 /* Check for the Timeout */
3976 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
3978 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3979 hi2c->State= HAL_I2C_STATE_READY;
3981 /* Process Unlocked */
3991 * @brief This function handles Acknowledge failed detection during an I2C Communication.
3992 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
3993 * the configuration information for the specified I2C.
3994 * @param Timeout: Timeout duration
3995 * @retval HAL status
3997 static HAL_StatusTypeDef I2C_IsAcknowledgeFailed(I2C_HandleTypeDef *hi2c, uint32_t Timeout)
3999 uint32_t tickstart = 0x00;
4000 tickstart = HAL_GetTick();
4002 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
4004 /* Generate stop if necessary only in case of I2C peripheral in MASTER mode */
4005 if((hi2c->State == HAL_I2C_STATE_MASTER_BUSY_TX) || (hi2c->State == HAL_I2C_STATE_MEM_BUSY_TX)
4006 || (hi2c->State == HAL_I2C_STATE_MEM_BUSY_RX))
4008 /* No need to generate the STOP condition if AUTOEND mode is enabled */
4009 /* Generate the STOP condition only in case of SOFTEND mode is enabled */
4010 if((hi2c->Instance->CR2 & I2C_AUTOEND_MODE) != I2C_AUTOEND_MODE)
4013 hi2c->Instance->CR2 |= I2C_CR2_STOP;
4017 /* Wait until STOP Flag is reset */
4018 /* AutoEnd should be initiate after AF */
4019 while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET)
4021 /* Check for the Timeout */
4022 if(Timeout != HAL_MAX_DELAY)
4024 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
4026 hi2c->State= HAL_I2C_STATE_READY;
4027 /* Process Unlocked */
4034 /* Clear NACKF Flag */
4035 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
4037 /* Clear STOP Flag */
4038 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
4040 /* Clear Configuration Register 2 */
4041 __I2C_RESET_CR2(hi2c);
4043 hi2c->ErrorCode = HAL_I2C_ERROR_AF;
4044 hi2c->State= HAL_I2C_STATE_READY;
4046 /* Process Unlocked */
4055 * @brief Handles I2Cx communication when starting transfer or during transfer (TC or TCR flag are set).
4056 * @param hi2c: I2C handle.
4057 * @param DevAddress: specifies the slave address to be programmed.
4058 * @param Size: specifies the number of bytes to be programmed.
4059 * This parameter must be a value between 0 and 255.
4060 * @param Mode: new state of the I2C START condition generation.
4061 * This parameter can be one of the following values:
4062 * @arg I2C_RELOAD_MODE: Enable Reload mode .
4063 * @arg I2C_AUTOEND_MODE: Enable Automatic end mode.
4064 * @arg I2C_SOFTEND_MODE: Enable Software end mode.
4065 * @param Request: new state of the I2C START condition generation.
4066 * This parameter can be one of the following values:
4067 * @arg I2C_NO_STARTSTOP: Don't Generate stop and start condition.
4068 * @arg I2C_GENERATE_STOP: Generate stop condition (Size should be set to 0).
4069 * @arg I2C_GENERATE_START_READ: Generate Restart for read request.
4070 * @arg I2C_GENERATE_START_WRITE: Generate Restart for write request.
4073 static void I2C_TransferConfig(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t Size, uint32_t Mode, uint32_t Request)
4075 uint32_t tmpreg = 0;
4077 /* Check the parameters */
4078 assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
4079 assert_param(IS_TRANSFER_MODE(Mode));
4080 assert_param(IS_TRANSFER_REQUEST(Request));
4082 /* Get the CR2 register value */
4083 tmpreg = hi2c->Instance->CR2;
4085 /* clear tmpreg specific bits */
4086 tmpreg &= (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP));
4089 tmpreg |= (uint32_t)(((uint32_t)DevAddress & I2C_CR2_SADD) | (((uint32_t)Size << 16 ) & I2C_CR2_NBYTES) | \
4090 (uint32_t)Mode | (uint32_t)Request);
4092 /* update CR2 register */
4093 hi2c->Instance->CR2 = tmpreg;
4100 #endif /* HAL_I2C_MODULE_ENABLED */
4109 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/