Merge pull request #11494 from haslinghuis/dshot_gpio
[betaflight.git] / lib / main / STM32F1 / Drivers / STM32F1xx_HAL_Driver / Src / stm32f1xx_hal_i2c.c
blob3c957e2b69e205b890e2b5cf1c6ef974f94107a1
1 /**
2 ******************************************************************************
3 * @file stm32f1xx_hal_i2c.c
4 * @author MCD Application Team
5 * @version V1.1.1
6 * @date 12-May-2017
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, Mode and Error functions
14 @verbatim
15 ==============================================================================
16 ##### How to use this driver #####
17 ==============================================================================
18 [..]
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 implementing 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 initialized 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 Speed, Duty cycle, Addressing mode, Own Address1,
42 Dual Addressing mode, Own Address2, 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 customized 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 =================================
53 [..]
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 =====================================
61 [..]
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 ===================================
68 [..]
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
83 (+) Abort a master I2C process communication with Interrupt using HAL_I2C_Master_Abort_IT()
84 (+) End of abort process, HAL_I2C_AbortCpltCallback() is executed and user can
85 add his own code by customization of function pointer HAL_I2C_AbortCpltCallback()
87 *** Interrupt mode IO sequential operation ***
88 ==============================================
89 [..]
90 (@) These interfaces allow to manage a sequential transfer with a repeated start condition
91 when a direction change during transfer
92 [..]
93 (+) A specific option field manage the different steps of a sequential transfer
94 (+) Option field values are defined through @ref I2C_XFEROPTIONS and are listed below:
95 (++) I2C_FIRST_AND_LAST_FRAME: No sequential usage, functionnal is same as associated interfaces in no sequential mode
96 (++) I2C_FIRST_FRAME: Sequential usage, this option allow to manage a sequence with start condition, address
97 and data to transfer without a final stop condition
98 (++) I2C_NEXT_FRAME: Sequential usage, this option allow to manage a sequence with a restart condition, address
99 and with new data to transfer if the direction change or manage only the new data to transfer
100 if no direction change and without a final stop condition in both cases
101 (++) I2C_LAST_FRAME: Sequential usage, this option allow to manage a sequance with a restart condition, address
102 and with new data to transfer if the direction change or manage only the new data to transfer
103 if no direction change and with a final stop condition in both cases
105 (+) Differents sequential I2C interfaces are listed below:
106 (++) Sequential transmit in master I2C mode an amount of data in non-blocking mode using HAL_I2C_Master_Sequential_Transmit_IT()
107 (+++) At transmission end of current frame transfer, HAL_I2C_MasterTxCpltCallback() is executed and user can
108 add his own code by customization of function pointer HAL_I2C_MasterTxCpltCallback()
109 (++) Sequential receive in master I2C mode an amount of data in non-blocking mode using HAL_I2C_Master_Sequential_Receive_IT()
110 (+++) At reception end of current frame transfer, HAL_I2C_MasterRxCpltCallback() is executed and user can
111 add his own code by customization of function pointer HAL_I2C_MasterRxCpltCallback()
112 (++) Abort a master I2C process communication with Interrupt using HAL_I2C_Master_Abort_IT()
113 (+++) End of abort process, HAL_I2C_AbortCpltCallback() is executed and user can
114 add his own code by customization of function pointer HAL_I2C_AbortCpltCallback()
115 (++) Enable/disable the Address listen mode in slave I2C mode using HAL_I2C_EnableListen_IT() HAL_I2C_DisableListen_IT()
116 (+++) When address slave I2C match, HAL_I2C_AddrCallback() is executed and user can
117 add his own code to check the Address Match Code and the transmission direction request by master (Write/Read).
118 (+++) At Listen mode end HAL_I2C_ListenCpltCallback() is executed and user can
119 add his own code by customization of function pointer HAL_I2C_ListenCpltCallback()
120 (++) Sequential transmit in slave I2C mode an amount of data in non-blocking mode using HAL_I2C_Slave_Sequential_Transmit_IT()
121 (+++) At transmission end of current frame transfer, HAL_I2C_SlaveTxCpltCallback() is executed and user can
122 add his own code by customization of function pointer HAL_I2C_SlaveTxCpltCallback()
123 (++) Sequential receive in slave I2C mode an amount of data in non-blocking mode using HAL_I2C_Slave_Sequential_Receive_IT()
124 (+++) At reception end of current frame transfer, HAL_I2C_SlaveRxCpltCallback() is executed and user can
125 add his own code by customization of function pointer HAL_I2C_SlaveRxCpltCallback()
126 (++) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
127 add his own code by customization of function pointer HAL_I2C_ErrorCallback()
129 *** Interrupt mode IO MEM operation ***
130 =======================================
131 [..]
132 (+) Write an amount of data in no-blocking mode with Interrupt to a specific memory address using
133 HAL_I2C_Mem_Write_IT()
134 (+) At MEM end of write transfer HAL_I2C_MemTxCpltCallback is executed and user can
135 add his own code by customization of function pointer HAL_I2C_MemTxCpltCallback
136 (+) Read an amount of data in no-blocking mode with Interrupt from a specific memory address using
137 HAL_I2C_Mem_Read_IT()
138 (+) At MEM end of read transfer HAL_I2C_MemRxCpltCallback is executed and user can
139 add his own code by customization of function pointer HAL_I2C_MemRxCpltCallback
140 (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
141 add his own code by customization of function pointer HAL_I2C_ErrorCallback
143 *** DMA mode IO operation ***
144 ==============================
145 [..]
146 (+) Transmit in master mode an amount of data in non blocking mode (DMA) using
147 HAL_I2C_Master_Transmit_DMA()
148 (+) At transmission end of transfer HAL_I2C_MasterTxCpltCallback is executed and user can
149 add his own code by customization of function pointer HAL_I2C_MasterTxCpltCallback
150 (+) Receive in master mode an amount of data in non blocking mode (DMA) using
151 HAL_I2C_Master_Receive_DMA()
152 (+) At reception end of transfer HAL_I2C_MasterRxCpltCallback is executed and user can
153 add his own code by customization of function pointer HAL_I2C_MasterRxCpltCallback
154 (+) Transmit in slave mode an amount of data in non blocking mode (DMA) using
155 HAL_I2C_Slave_Transmit_DMA()
156 (+) At transmission end of transfer HAL_I2C_SlaveTxCpltCallback is executed and user can
157 add his own code by customization of function pointer HAL_I2C_SlaveTxCpltCallback
158 (+) Receive in slave mode an amount of data in non blocking mode (DMA) using
159 HAL_I2C_Slave_Receive_DMA()
160 (+) At reception end of transfer HAL_I2C_SlaveRxCpltCallback is executed and user can
161 add his own code by customization of function pointer HAL_I2C_SlaveRxCpltCallback
162 (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
163 add his own code by customization of function pointer HAL_I2C_ErrorCallback
164 (+) Abort a master I2C process communication with Interrupt using HAL_I2C_Master_Abort_IT()
165 (+) End of abort process, HAL_I2C_AbortCpltCallback() is executed and user can
166 add his own code by customization of function pointer HAL_I2C_AbortCpltCallback()
168 *** DMA mode IO MEM operation ***
169 =================================
170 [..]
171 (+) Write an amount of data in no-blocking mode with DMA to a specific memory address using
172 HAL_I2C_Mem_Write_DMA()
173 (+) At MEM end of write transfer HAL_I2C_MemTxCpltCallback is executed and user can
174 add his own code by customization of function pointer HAL_I2C_MemTxCpltCallback
175 (+) Read an amount of data in no-blocking mode with DMA from a specific memory address using
176 HAL_I2C_Mem_Read_DMA()
177 (+) At MEM end of read transfer HAL_I2C_MemRxCpltCallback is executed and user can
178 add his own code by customization of function pointer HAL_I2C_MemRxCpltCallback
179 (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
180 add his own code by customization of function pointer HAL_I2C_ErrorCallback
183 *** I2C HAL driver macros list ***
184 ==================================
185 [..]
186 Below the list of most used macros in I2C HAL driver.
188 (+) __HAL_I2C_ENABLE: Enable the I2C peripheral
189 (+) __HAL_I2C_DISABLE: Disable the I2C peripheral
190 (+) __HAL_I2C_GET_FLAG : Checks whether the specified I2C flag is set or not
191 (+) __HAL_I2C_CLEAR_FLAG : Clear the specified I2C pending flag
192 (+) __HAL_I2C_ENABLE_IT: Enable the specified I2C interrupt
193 (+) __HAL_I2C_DISABLE_IT: Disable the specified I2C interrupt
195 [..]
196 (@) You can refer to the I2C HAL driver header file for more useful macros
198 *** I2C Workarounds linked to Silicon Limitation ***
199 ====================================================
200 [..]
201 Below the list of all silicon limitations implemented for HAL on STM32F1xx product.
202 (@) See ErrataSheet to know full silicon limitation list of your product.
204 (#) Workarounds Implemented inside I2C HAL Driver
205 (##) Wrong data read into data register (Polling and Interrupt mode)
206 (##) Start cannot be generated after a misplaced Stop
207 (##) Some software events must be managed before the current byte is being transferred:
208 Workaround: Use DMA in general, except when the Master is receiving a single byte.
209 For Interupt mode, I2C should have the highest priority in the application.
210 (##) Mismatch on the "Setup time for a repeated Start condition" timing parameter:
211 Workaround: Reduce the frequency down to 88 kHz or use the I2C Fast-mode if
212 supported by the slave.
213 (##) Data valid time (tVD;DAT) violated without the OVR flag being set:
214 Workaround: If the slave device allows it, use the clock stretching mechanism
215 by programming NoStretchMode = I2C_NOSTRETCH_DISABLE in HAL_I2C_Init.
217 @endverbatim
218 ******************************************************************************
219 * @attention
221 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
223 * Redistribution and use in source and binary forms, with or without modification,
224 * are permitted provided that the following conditions are met:
225 * 1. Redistributions of source code must retain the above copyright notice,
226 * this list of conditions and the following disclaimer.
227 * 2. Redistributions in binary form must reproduce the above copyright notice,
228 * this list of conditions and the following disclaimer in the documentation
229 * and/or other materials provided with the distribution.
230 * 3. Neither the name of STMicroelectronics nor the names of its contributors
231 * may be used to endorse or promote products derived from this software
232 * without specific prior written permission.
234 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
235 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
236 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
237 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
238 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
239 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
240 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
241 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
242 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
243 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
245 ******************************************************************************
248 /* Includes ------------------------------------------------------------------*/
249 #include "stm32f1xx_hal.h"
251 /** @addtogroup STM32F1xx_HAL_Driver
252 * @{
255 /** @defgroup I2C I2C
256 * @brief I2C HAL module driver
257 * @{
260 #ifdef HAL_I2C_MODULE_ENABLED
262 /* Private typedef -----------------------------------------------------------*/
263 /* Private define ------------------------------------------------------------*/
264 /** @addtogroup I2C_Private_Define
265 * @{
267 #define I2C_TIMEOUT_FLAG 35U /*!< Timeout 35 ms */
268 #define I2C_TIMEOUT_BUSY_FLAG 25U /*!< Timeout 25 ms */
269 #define I2C_NO_OPTION_FRAME 0xFFFF0000U /*!< XferOptions default value */
271 /* Private define for @ref PreviousState usage */
272 #define I2C_STATE_MSK ((uint32_t)((HAL_I2C_STATE_BUSY_TX | HAL_I2C_STATE_BUSY_RX) & (~(uint32_t)HAL_I2C_STATE_READY))) /*!< Mask State define, keep only RX and TX bits */
273 #define I2C_STATE_NONE ((uint32_t)(HAL_I2C_MODE_NONE)) /*!< Default Value */
274 #define I2C_STATE_MASTER_BUSY_TX ((uint32_t)((HAL_I2C_STATE_BUSY_TX & I2C_STATE_MSK) | HAL_I2C_MODE_MASTER)) /*!< Master Busy TX, combinaison of State LSB and Mode enum */
275 #define I2C_STATE_MASTER_BUSY_RX ((uint32_t)((HAL_I2C_STATE_BUSY_RX & I2C_STATE_MSK) | HAL_I2C_MODE_MASTER)) /*!< Master Busy RX, combinaison of State LSB and Mode enum */
276 #define I2C_STATE_SLAVE_BUSY_TX ((uint32_t)((HAL_I2C_STATE_BUSY_TX & I2C_STATE_MSK) | HAL_I2C_MODE_SLAVE)) /*!< Slave Busy TX, combinaison of State LSB and Mode enum */
277 #define I2C_STATE_SLAVE_BUSY_RX ((uint32_t)((HAL_I2C_STATE_BUSY_RX & I2C_STATE_MSK) | HAL_I2C_MODE_SLAVE)) /*!< Slave Busy RX, combinaison of State LSB and Mode enum */
280 * @}
283 /* Private macro -------------------------------------------------------------*/
284 /* Private variables ---------------------------------------------------------*/
285 /* Private function prototypes -----------------------------------------------*/
286 /** @addtogroup I2C_Private_Functions
287 * @{
289 /* Private functions to handle DMA transfer */
290 static void I2C_DMAXferCplt(DMA_HandleTypeDef *hdma);
291 static void I2C_DMAError(DMA_HandleTypeDef *hdma);
292 static void I2C_DMAAbort(DMA_HandleTypeDef *hdma);
294 static void I2C_ITError(I2C_HandleTypeDef *hi2c);
296 static HAL_StatusTypeDef I2C_MasterRequestWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Timeout, uint32_t Tickstart);
297 static HAL_StatusTypeDef I2C_MasterRequestRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Timeout, uint32_t Tickstart);
298 static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart);
299 static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart);
300 static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout, uint32_t Tickstart);
301 static HAL_StatusTypeDef I2C_WaitOnMasterAddressFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, uint32_t Timeout, uint32_t Tickstart);
302 static HAL_StatusTypeDef I2C_WaitOnTXEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart);
303 static HAL_StatusTypeDef I2C_WaitOnBTFFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart);
304 static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart);
305 static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart);
306 static HAL_StatusTypeDef I2C_IsAcknowledgeFailed(I2C_HandleTypeDef *hi2c);
308 /* Private functions for I2C transfer IRQ handler */
309 static HAL_StatusTypeDef I2C_MasterTransmit_TXE(I2C_HandleTypeDef *hi2c);
310 static HAL_StatusTypeDef I2C_MasterTransmit_BTF(I2C_HandleTypeDef *hi2c);
311 static HAL_StatusTypeDef I2C_MasterReceive_RXNE(I2C_HandleTypeDef *hi2c);
312 static HAL_StatusTypeDef I2C_MasterReceive_BTF(I2C_HandleTypeDef *hi2c);
313 static HAL_StatusTypeDef I2C_Master_SB(I2C_HandleTypeDef *hi2c);
314 static HAL_StatusTypeDef I2C_Master_ADD10(I2C_HandleTypeDef *hi2c);
315 static HAL_StatusTypeDef I2C_Master_ADDR(I2C_HandleTypeDef *hi2c);
317 static HAL_StatusTypeDef I2C_SlaveTransmit_TXE(I2C_HandleTypeDef *hi2c);
318 static HAL_StatusTypeDef I2C_SlaveTransmit_BTF(I2C_HandleTypeDef *hi2c);
319 static HAL_StatusTypeDef I2C_SlaveReceive_RXNE(I2C_HandleTypeDef *hi2c);
320 static HAL_StatusTypeDef I2C_SlaveReceive_BTF(I2C_HandleTypeDef *hi2c);
321 static HAL_StatusTypeDef I2C_Slave_ADDR(I2C_HandleTypeDef *hi2c);
322 static HAL_StatusTypeDef I2C_Slave_STOPF(I2C_HandleTypeDef *hi2c);
323 static HAL_StatusTypeDef I2C_Slave_AF(I2C_HandleTypeDef *hi2c);
325 * @}
328 /* Exported functions --------------------------------------------------------*/
329 /** @defgroup I2C_Exported_Functions I2C Exported Functions
330 * @{
333 /** @defgroup I2C_Exported_Functions_Group1 Initialization and de-initialization functions
334 * @brief Initialization and Configuration functions
336 @verbatim
337 ===============================================================================
338 ##### Initialization and de-initialization functions #####
339 ===============================================================================
340 [..] This subsection provides a set of functions allowing to initialize and
341 de-initialize the I2Cx peripheral:
343 (+) User must Implement HAL_I2C_MspInit() function in which he configures
344 all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC).
346 (+) Call the function HAL_I2C_Init() to configure the selected device with
347 the selected configuration:
348 (++) Communication Speed
349 (++) Duty cycle
350 (++) Addressing mode
351 (++) Own Address 1
352 (++) Dual Addressing mode
353 (++) Own Address 2
354 (++) General call mode
355 (++) Nostretch mode
357 (+) Call the function HAL_I2C_DeInit() to restore the default configuration
358 of the selected I2Cx peripheral.
360 @endverbatim
361 * @{
365 * @brief Initializes the I2C according to the specified parameters
366 * in the I2C_InitTypeDef and create the associated handle.
367 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
368 * the configuration information for I2C module
369 * @retval HAL status
371 HAL_StatusTypeDef HAL_I2C_Init(I2C_HandleTypeDef *hi2c)
373 uint32_t freqrange = 0U;
374 uint32_t pclk1 = 0U;
376 /* Check the I2C handle allocation */
377 if(hi2c == NULL)
379 return HAL_ERROR;
382 /* Check the parameters */
383 assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
384 assert_param(IS_I2C_CLOCK_SPEED(hi2c->Init.ClockSpeed));
385 assert_param(IS_I2C_DUTY_CYCLE(hi2c->Init.DutyCycle));
386 assert_param(IS_I2C_OWN_ADDRESS1(hi2c->Init.OwnAddress1));
387 assert_param(IS_I2C_ADDRESSING_MODE(hi2c->Init.AddressingMode));
388 assert_param(IS_I2C_DUAL_ADDRESS(hi2c->Init.DualAddressMode));
389 assert_param(IS_I2C_OWN_ADDRESS2(hi2c->Init.OwnAddress2));
390 assert_param(IS_I2C_GENERAL_CALL(hi2c->Init.GeneralCallMode));
391 assert_param(IS_I2C_NO_STRETCH(hi2c->Init.NoStretchMode));
393 if(hi2c->State == HAL_I2C_STATE_RESET)
395 /* Allocate lock resource and initialize it */
396 hi2c->Lock = HAL_UNLOCKED;
397 /* Init the low level hardware : GPIO, CLOCK, NVIC */
398 HAL_I2C_MspInit(hi2c);
401 hi2c->State = HAL_I2C_STATE_BUSY;
403 /* Disable the selected I2C peripheral */
404 __HAL_I2C_DISABLE(hi2c);
406 /* Get PCLK1 frequency */
407 pclk1 = HAL_RCC_GetPCLK1Freq();
409 /* Calculate frequency range */
410 freqrange = I2C_FREQRANGE(pclk1);
412 /*---------------------------- I2Cx CR2 Configuration ----------------------*/
413 /* Configure I2Cx: Frequency range */
414 hi2c->Instance->CR2 = freqrange;
416 /*---------------------------- I2Cx TRISE Configuration --------------------*/
417 /* Configure I2Cx: Rise Time */
418 hi2c->Instance->TRISE = I2C_RISE_TIME(freqrange, hi2c->Init.ClockSpeed);
420 /*---------------------------- I2Cx CCR Configuration ----------------------*/
421 /* Configure I2Cx: Speed */
422 hi2c->Instance->CCR = I2C_SPEED(pclk1, hi2c->Init.ClockSpeed, hi2c->Init.DutyCycle);
424 /*---------------------------- I2Cx CR1 Configuration ----------------------*/
425 /* Configure I2Cx: Generalcall and NoStretch mode */
426 hi2c->Instance->CR1 = (hi2c->Init.GeneralCallMode | hi2c->Init.NoStretchMode);
428 /*---------------------------- I2Cx OAR1 Configuration ---------------------*/
429 /* Configure I2Cx: Own Address1 and addressing mode */
430 hi2c->Instance->OAR1 = (hi2c->Init.AddressingMode | hi2c->Init.OwnAddress1);
432 /*---------------------------- I2Cx OAR2 Configuration ---------------------*/
433 /* Configure I2Cx: Dual mode and Own Address2 */
434 hi2c->Instance->OAR2 = (hi2c->Init.DualAddressMode | hi2c->Init.OwnAddress2);
436 /* Enable the selected I2C peripheral */
437 __HAL_I2C_ENABLE(hi2c);
439 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
440 hi2c->State = HAL_I2C_STATE_READY;
441 hi2c->PreviousState = I2C_STATE_NONE;
442 hi2c->Mode = HAL_I2C_MODE_NONE;
444 return HAL_OK;
448 * @brief DeInitializes the I2C peripheral.
449 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
450 * the configuration information for I2C module
451 * @retval HAL status
453 HAL_StatusTypeDef HAL_I2C_DeInit(I2C_HandleTypeDef *hi2c)
455 /* Check the I2C handle allocation */
456 if(hi2c == NULL)
458 return HAL_ERROR;
461 /* Check the parameters */
462 assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
464 hi2c->State = HAL_I2C_STATE_BUSY;
466 /* Disable the I2C Peripheral Clock */
467 __HAL_I2C_DISABLE(hi2c);
469 /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
470 HAL_I2C_MspDeInit(hi2c);
472 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
473 hi2c->State = HAL_I2C_STATE_RESET;
474 hi2c->PreviousState = I2C_STATE_NONE;
475 hi2c->Mode = HAL_I2C_MODE_NONE;
477 /* Release Lock */
478 __HAL_UNLOCK(hi2c);
480 return HAL_OK;
484 * @brief I2C MSP Init.
485 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
486 * the configuration information for I2C module
487 * @retval None
489 __weak void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)
491 /* Prevent unused argument(s) compilation warning */
492 UNUSED(hi2c);
493 /* NOTE : This function Should not be modified, when the callback is needed,
494 the HAL_I2C_MspInit could be implemented in the user file
499 * @brief I2C MSP DeInit
500 * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains
501 * the configuration information for I2C module
502 * @retval None
504 __weak void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c)
506 /* Prevent unused argument(s) compilation warning */
507 UNUSED(hi2c);
508 /* NOTE : This function Should not be modified, when the callback is needed,
509 the HAL_I2C_MspDeInit could be implemented in the user file
514 * @}
517 /** @defgroup I2C_Exported_Functions_Group2 IO operation functions
518 * @brief Data transfers functions
520 @verbatim
521 ===============================================================================
522 ##### IO operation functions #####
523 ===============================================================================
524 [..]
525 This subsection provides a set of functions allowing to manage the I2C data
526 transfers.
528 (#) There are two modes of transfer:
529 (++) Blocking mode : The communication is performed in the polling mode.
530 The status of all data processing is returned by the same function
531 after finishing transfer.
532 (++) No-Blocking mode : The communication is performed using Interrupts
533 or DMA. These functions return the status of the transfer startup.
534 The end of the data processing will be indicated through the
535 dedicated I2C IRQ when using Interrupt mode or the DMA IRQ when
536 using DMA mode.
538 (#) Blocking mode functions are :
539 (++) HAL_I2C_Master_Transmit()
540 (++) HAL_I2C_Master_Receive()
541 (++) HAL_I2C_Slave_Transmit()
542 (++) HAL_I2C_Slave_Receive()
543 (++) HAL_I2C_Mem_Write()
544 (++) HAL_I2C_Mem_Read()
545 (++) HAL_I2C_IsDeviceReady()
547 (#) No-Blocking mode functions with Interrupt are :
548 (++) HAL_I2C_Master_Transmit_IT()
549 (++) HAL_I2C_Master_Receive_IT()
550 (++) HAL_I2C_Slave_Transmit_IT()
551 (++) HAL_I2C_Slave_Receive_IT()
552 (++) HAL_I2C_Master_Sequential_Transmit_IT()
553 (++) HAL_I2C_Master_Sequential_Receive_IT()
554 (++) HAL_I2C_Slave_Sequential_Transmit_IT()
555 (++) HAL_I2C_Slave_Sequential_Receive_IT()
556 (++) HAL_I2C_Mem_Write_IT()
557 (++) HAL_I2C_Mem_Read_IT()
559 (#) No-Blocking mode functions with DMA are :
560 (++) HAL_I2C_Master_Transmit_DMA()
561 (++) HAL_I2C_Master_Receive_DMA()
562 (++) HAL_I2C_Slave_Transmit_DMA()
563 (++) HAL_I2C_Slave_Receive_DMA()
564 (++) HAL_I2C_Mem_Write_DMA()
565 (++) HAL_I2C_Mem_Read_DMA()
567 (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
568 (++) HAL_I2C_MemTxCpltCallback()
569 (++) HAL_I2C_MemRxCpltCallback()
570 (++) HAL_I2C_MasterTxCpltCallback()
571 (++) HAL_I2C_MasterRxCpltCallback()
572 (++) HAL_I2C_SlaveTxCpltCallback()
573 (++) HAL_I2C_SlaveRxCpltCallback()
574 (++) HAL_I2C_ErrorCallback()
575 (++) HAL_I2C_AbortCpltCallback()
577 @endverbatim
578 * @{
582 * @brief Transmits in master mode an amount of data in blocking mode.
583 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
584 * the configuration information for the specified I2C.
585 * @param DevAddress Target device address: The device 7 bits address value
586 * in datasheet must be shift at right before call interface
587 * @param pData Pointer to data buffer
588 * @param Size Amount of data to be sent
589 * @param Timeout Timeout duration
590 * @retval HAL status
592 HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
594 uint32_t tickstart = 0x00U;
596 /* Init tickstart for timeout management*/
597 tickstart = HAL_GetTick();
599 if(hi2c->State == HAL_I2C_STATE_READY)
601 /* Wait until BUSY flag is reset */
602 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
604 return HAL_BUSY;
607 /* Process Locked */
608 __HAL_LOCK(hi2c);
610 /* Check if the I2C is already enabled */
611 if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
613 /* Enable I2C peripheral */
614 __HAL_I2C_ENABLE(hi2c);
617 /* Disable Pos */
618 hi2c->Instance->CR1 &= ~I2C_CR1_POS;
620 hi2c->State = HAL_I2C_STATE_BUSY_TX;
621 hi2c->Mode = HAL_I2C_MODE_MASTER;
622 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
624 /* Prepare transfer parameters */
625 hi2c->pBuffPtr = pData;
626 hi2c->XferCount = Size;
627 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
628 hi2c->XferSize = hi2c->XferCount;
630 /* Send Slave Address */
631 if(I2C_MasterRequestWrite(hi2c, DevAddress, Timeout, tickstart) != HAL_OK)
633 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
635 /* Process Unlocked */
636 __HAL_UNLOCK(hi2c);
637 return HAL_ERROR;
639 else
641 /* Process Unlocked */
642 __HAL_UNLOCK(hi2c);
643 return HAL_TIMEOUT;
647 /* Clear ADDR flag */
648 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
650 while(hi2c->XferSize > 0U)
652 /* Wait until TXE flag is set */
653 if(I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
655 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
657 /* Generate Stop */
658 hi2c->Instance->CR1 |= I2C_CR1_STOP;
659 return HAL_ERROR;
661 else
663 return HAL_TIMEOUT;
667 /* Write data to DR */
668 hi2c->Instance->DR = (*hi2c->pBuffPtr++);
669 hi2c->XferCount--;
670 hi2c->XferSize--;
672 if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (hi2c->XferSize != 0U))
674 /* Write data to DR */
675 hi2c->Instance->DR = (*hi2c->pBuffPtr++);
676 hi2c->XferCount--;
677 hi2c->XferSize--;
680 /* Wait until BTF flag is set */
681 if(I2C_WaitOnBTFFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
683 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
685 /* Generate Stop */
686 hi2c->Instance->CR1 |= I2C_CR1_STOP;
687 return HAL_ERROR;
689 else
691 return HAL_TIMEOUT;
696 /* Generate Stop */
697 hi2c->Instance->CR1 |= I2C_CR1_STOP;
699 hi2c->State = HAL_I2C_STATE_READY;
700 hi2c->Mode = HAL_I2C_MODE_NONE;
702 /* Process Unlocked */
703 __HAL_UNLOCK(hi2c);
705 return HAL_OK;
707 else
709 return HAL_BUSY;
714 * @brief Receives in master mode an amount of data in blocking mode.
715 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
716 * the configuration information for the specified I2C.
717 * @param DevAddress Target device address: The device 7 bits address value
718 * in datasheet must be shift at right before call interface
719 * @param pData Pointer to data buffer
720 * @param Size Amount of data to be sent
721 * @param Timeout Timeout duration
722 * @retval HAL status
724 HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
726 uint32_t tickstart = 0x00U;
728 /* Init tickstart for timeout management*/
729 tickstart = HAL_GetTick();
731 if(hi2c->State == HAL_I2C_STATE_READY)
733 /* Wait until BUSY flag is reset */
734 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
736 return HAL_BUSY;
739 /* Process Locked */
740 __HAL_LOCK(hi2c);
742 /* Check if the I2C is already enabled */
743 if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
745 /* Enable I2C peripheral */
746 __HAL_I2C_ENABLE(hi2c);
749 /* Disable Pos */
750 hi2c->Instance->CR1 &= ~I2C_CR1_POS;
752 hi2c->State = HAL_I2C_STATE_BUSY_RX;
753 hi2c->Mode = HAL_I2C_MODE_MASTER;
754 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
756 /* Prepare transfer parameters */
757 hi2c->pBuffPtr = pData;
758 hi2c->XferCount = Size;
759 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
760 hi2c->XferSize = hi2c->XferCount;
762 /* Send Slave Address */
763 if(I2C_MasterRequestRead(hi2c, DevAddress, Timeout, tickstart) != HAL_OK)
765 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
767 /* Process Unlocked */
768 __HAL_UNLOCK(hi2c);
769 return HAL_ERROR;
771 else
773 /* Process Unlocked */
774 __HAL_UNLOCK(hi2c);
775 return HAL_TIMEOUT;
779 if(hi2c->XferSize == 0U)
781 /* Clear ADDR flag */
782 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
784 /* Generate Stop */
785 hi2c->Instance->CR1 |= I2C_CR1_STOP;
787 else if(hi2c->XferSize == 1U)
789 /* Disable Acknowledge */
790 hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
792 /* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3
793 software sequence must complete before the current byte end of transfer */
794 __disable_irq();
796 /* Clear ADDR flag */
797 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
799 /* Generate Stop */
800 hi2c->Instance->CR1 |= I2C_CR1_STOP;
802 /* Re-enable IRQs */
803 __enable_irq();
805 else if(hi2c->XferSize == 2U)
807 /* Enable Pos */
808 hi2c->Instance->CR1 |= I2C_CR1_POS;
810 /* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3
811 software sequence must complete before the current byte end of transfer */
812 __disable_irq();
814 /* Clear ADDR flag */
815 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
817 /* Disable Acknowledge */
818 hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
820 /* Re-enable IRQs */
821 __enable_irq();
823 else
825 /* Enable Acknowledge */
826 hi2c->Instance->CR1 |= I2C_CR1_ACK;
828 /* Clear ADDR flag */
829 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
832 while(hi2c->XferSize > 0U)
834 if(hi2c->XferSize <= 3U)
836 /* One byte */
837 if(hi2c->XferSize == 1U)
839 /* Wait until RXNE flag is set */
840 if(I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
842 if(hi2c->ErrorCode == HAL_I2C_ERROR_TIMEOUT)
844 return HAL_TIMEOUT;
846 else
848 return HAL_ERROR;
852 /* Read data from DR */
853 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
854 hi2c->XferSize--;
855 hi2c->XferCount--;
857 /* Two bytes */
858 else if(hi2c->XferSize == 2U)
860 /* Wait until BTF flag is set */
861 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK)
863 return HAL_TIMEOUT;
866 /* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3
867 software sequence must complete before the current byte end of transfer */
868 __disable_irq();
870 /* Generate Stop */
871 hi2c->Instance->CR1 |= I2C_CR1_STOP;
873 /* Read data from DR */
874 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
875 hi2c->XferSize--;
876 hi2c->XferCount--;
878 /* Re-enable IRQs */
879 __enable_irq();
881 /* Read data from DR */
882 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
883 hi2c->XferSize--;
884 hi2c->XferCount--;
886 /* 3 Last bytes */
887 else
889 /* Wait until BTF flag is set */
890 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK)
892 return HAL_TIMEOUT;
895 /* Disable Acknowledge */
896 hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
898 /* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3
899 software sequence must complete before the current byte end of transfer */
900 __disable_irq();
902 /* Read data from DR */
903 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
904 hi2c->XferSize--;
905 hi2c->XferCount--;
907 /* Wait until BTF flag is set */
908 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK)
910 return HAL_TIMEOUT;
913 /* Generate Stop */
914 hi2c->Instance->CR1 |= I2C_CR1_STOP;
916 /* Read data from DR */
917 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
918 hi2c->XferSize--;
919 hi2c->XferCount--;
921 /* Re-enable IRQs */
922 __enable_irq();
924 /* Read data from DR */
925 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
926 hi2c->XferSize--;
927 hi2c->XferCount--;
930 else
932 /* Wait until RXNE flag is set */
933 if(I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
935 if(hi2c->ErrorCode == HAL_I2C_ERROR_TIMEOUT)
937 return HAL_TIMEOUT;
939 else
941 return HAL_ERROR;
945 /* Read data from DR */
946 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
947 hi2c->XferSize--;
948 hi2c->XferCount--;
950 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET)
952 /* Read data from DR */
953 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
954 hi2c->XferSize--;
955 hi2c->XferCount--;
960 hi2c->State = HAL_I2C_STATE_READY;
961 hi2c->Mode = HAL_I2C_MODE_NONE;
963 /* Process Unlocked */
964 __HAL_UNLOCK(hi2c);
966 return HAL_OK;
968 else
970 return HAL_BUSY;
975 * @brief Transmits in slave mode an amount of data in blocking mode.
976 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
977 * the configuration information for the specified I2C.
978 * @param pData Pointer to data buffer
979 * @param Size Amount of data to be sent
980 * @param Timeout Timeout duration
981 * @retval HAL status
983 HAL_StatusTypeDef HAL_I2C_Slave_Transmit(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
985 uint32_t tickstart = 0x00U;
987 /* Init tickstart for timeout management*/
988 tickstart = HAL_GetTick();
990 if(hi2c->State == HAL_I2C_STATE_READY)
992 if((pData == NULL) || (Size == 0U))
994 return HAL_ERROR;
997 /* Process Locked */
998 __HAL_LOCK(hi2c);
1000 /* Check if the I2C is already enabled */
1001 if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
1003 /* Enable I2C peripheral */
1004 __HAL_I2C_ENABLE(hi2c);
1007 /* Disable Pos */
1008 hi2c->Instance->CR1 &= ~I2C_CR1_POS;
1010 hi2c->State = HAL_I2C_STATE_BUSY_TX;
1011 hi2c->Mode = HAL_I2C_MODE_SLAVE;
1012 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1014 /* Prepare transfer parameters */
1015 hi2c->pBuffPtr = pData;
1016 hi2c->XferCount = Size;
1017 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
1018 hi2c->XferSize = hi2c->XferCount;
1020 /* Enable Address Acknowledge */
1021 hi2c->Instance->CR1 |= I2C_CR1_ACK;
1023 /* Wait until ADDR flag is set */
1024 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK)
1026 return HAL_TIMEOUT;
1029 /* Clear ADDR flag */
1030 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
1032 /* If 10bit addressing mode is selected */
1033 if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT)
1035 /* Wait until ADDR flag is set */
1036 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK)
1038 return HAL_TIMEOUT;
1041 /* Clear ADDR flag */
1042 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
1045 while(hi2c->XferSize > 0U)
1047 /* Wait until TXE flag is set */
1048 if(I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
1050 /* Disable Address Acknowledge */
1051 hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
1053 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1055 return HAL_ERROR;
1057 else
1059 return HAL_TIMEOUT;
1063 /* Write data to DR */
1064 hi2c->Instance->DR = (*hi2c->pBuffPtr++);
1065 hi2c->XferCount--;
1066 hi2c->XferSize--;
1068 if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (hi2c->XferSize != 0U))
1070 /* Write data to DR */
1071 hi2c->Instance->DR = (*hi2c->pBuffPtr++);
1072 hi2c->XferCount--;
1073 hi2c->XferSize--;
1077 /* Wait until AF flag is set */
1078 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_AF, RESET, Timeout, tickstart) != HAL_OK)
1080 return HAL_TIMEOUT;
1083 /* Clear AF flag */
1084 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
1086 /* Disable Address Acknowledge */
1087 hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
1089 hi2c->State = HAL_I2C_STATE_READY;
1090 hi2c->Mode = HAL_I2C_MODE_NONE;
1092 /* Process Unlocked */
1093 __HAL_UNLOCK(hi2c);
1095 return HAL_OK;
1097 else
1099 return HAL_BUSY;
1104 * @brief Receive in slave mode an amount of data in blocking mode
1105 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
1106 * the configuration information for the specified I2C.
1107 * @param pData Pointer to data buffer
1108 * @param Size Amount of data to be sent
1109 * @param Timeout Timeout duration
1110 * @retval HAL status
1112 HAL_StatusTypeDef HAL_I2C_Slave_Receive(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
1114 uint32_t tickstart = 0x00U;
1116 /* Init tickstart for timeout management*/
1117 tickstart = HAL_GetTick();
1119 if(hi2c->State == HAL_I2C_STATE_READY)
1121 if((pData == NULL) || (Size == 0U))
1123 return HAL_ERROR;
1126 /* Process Locked */
1127 __HAL_LOCK(hi2c);
1129 /* Check if the I2C is already enabled */
1130 if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
1132 /* Enable I2C peripheral */
1133 __HAL_I2C_ENABLE(hi2c);
1136 /* Disable Pos */
1137 hi2c->Instance->CR1 &= ~I2C_CR1_POS;
1139 hi2c->State = HAL_I2C_STATE_BUSY_RX;
1140 hi2c->Mode = HAL_I2C_MODE_SLAVE;
1141 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1143 /* Prepare transfer parameters */
1144 hi2c->pBuffPtr = pData;
1145 hi2c->XferCount = Size;
1146 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
1147 hi2c->XferSize = hi2c->XferCount;
1149 /* Enable Address Acknowledge */
1150 hi2c->Instance->CR1 |= I2C_CR1_ACK;
1152 /* Wait until ADDR flag is set */
1153 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK)
1155 return HAL_TIMEOUT;
1158 /* Clear ADDR flag */
1159 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
1161 while(hi2c->XferSize > 0U)
1163 /* Wait until RXNE flag is set */
1164 if(I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
1166 /* Disable Address Acknowledge */
1167 hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
1169 if(hi2c->ErrorCode == HAL_I2C_ERROR_TIMEOUT)
1171 return HAL_TIMEOUT;
1173 else
1175 return HAL_ERROR;
1179 /* Read data from DR */
1180 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
1181 hi2c->XferSize--;
1182 hi2c->XferCount--;
1184 if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (Size != 0U))
1186 /* Read data from DR */
1187 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
1188 hi2c->XferSize--;
1189 hi2c->XferCount--;
1193 /* Wait until STOP flag is set */
1194 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
1196 /* Disable Address Acknowledge */
1197 hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
1199 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1201 return HAL_ERROR;
1203 else
1205 return HAL_TIMEOUT;
1209 /* Clear STOP flag */
1210 __HAL_I2C_CLEAR_STOPFLAG(hi2c);
1212 /* Disable Address Acknowledge */
1213 hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
1215 hi2c->State = HAL_I2C_STATE_READY;
1216 hi2c->Mode = HAL_I2C_MODE_NONE;
1218 /* Process Unlocked */
1219 __HAL_UNLOCK(hi2c);
1221 return HAL_OK;
1223 else
1225 return HAL_BUSY;
1230 * @brief Transmit in master mode an amount of data in non-blocking mode with Interrupt
1231 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
1232 * the configuration information for the specified I2C.
1233 * @param DevAddress Target device address: The device 7 bits address value
1234 * in datasheet must be shift at right before call interface
1235 * @param pData Pointer to data buffer
1236 * @param Size Amount of data to be sent
1237 * @retval HAL status
1239 HAL_StatusTypeDef HAL_I2C_Master_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
1241 __IO uint32_t count = 0U;
1243 if(hi2c->State == HAL_I2C_STATE_READY)
1245 /* Wait until BUSY flag is reset */
1246 count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock /25U /1000U);
1249 if(count-- == 0U)
1251 hi2c->PreviousState = I2C_STATE_NONE;
1252 hi2c->State= HAL_I2C_STATE_READY;
1254 /* Process Unlocked */
1255 __HAL_UNLOCK(hi2c);
1257 return HAL_TIMEOUT;
1260 while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
1262 /* Process Locked */
1263 __HAL_LOCK(hi2c);
1265 /* Check if the I2C is already enabled */
1266 if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
1268 /* Enable I2C peripheral */
1269 __HAL_I2C_ENABLE(hi2c);
1272 /* Disable Pos */
1273 hi2c->Instance->CR1 &= ~I2C_CR1_POS;
1275 hi2c->State = HAL_I2C_STATE_BUSY_TX;
1276 hi2c->Mode = HAL_I2C_MODE_MASTER;
1277 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1279 /* Prepare transfer parameters */
1280 hi2c->pBuffPtr = pData;
1281 hi2c->XferCount = Size;
1282 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
1283 hi2c->XferSize = hi2c->XferCount;
1284 hi2c->Devaddress = DevAddress;
1286 /* Generate Start */
1287 hi2c->Instance->CR1 |= I2C_CR1_START;
1289 /* Process Unlocked */
1290 __HAL_UNLOCK(hi2c);
1292 /* Note : The I2C interrupts must be enabled after unlocking current process
1293 to avoid the risk of I2C interrupt handle execution before current
1294 process unlock */
1295 /* Enable EVT, BUF and ERR interrupt */
1296 __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
1298 return HAL_OK;
1300 else
1302 return HAL_BUSY;
1307 * @brief Receive in master mode an amount of data in non-blocking mode with Interrupt
1308 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
1309 * the configuration information for the specified I2C.
1310 * @param DevAddress Target device address: The device 7 bits address value
1311 * in datasheet must be shift at right before call interface
1312 * @param pData Pointer to data buffer
1313 * @param Size Amount of data to be sent
1314 * @retval HAL status
1316 HAL_StatusTypeDef HAL_I2C_Master_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
1318 __IO uint32_t count = 0U;
1320 if(hi2c->State == HAL_I2C_STATE_READY)
1322 /* Wait until BUSY flag is reset */
1323 count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock /25U /1000U);
1326 if(count-- == 0U)
1328 hi2c->PreviousState = I2C_STATE_NONE;
1329 hi2c->State= HAL_I2C_STATE_READY;
1331 /* Process Unlocked */
1332 __HAL_UNLOCK(hi2c);
1334 return HAL_TIMEOUT;
1337 while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
1339 /* Process Locked */
1340 __HAL_LOCK(hi2c);
1342 /* Check if the I2C is already enabled */
1343 if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
1345 /* Enable I2C peripheral */
1346 __HAL_I2C_ENABLE(hi2c);
1349 /* Disable Pos */
1350 hi2c->Instance->CR1 &= ~I2C_CR1_POS;
1352 hi2c->State = HAL_I2C_STATE_BUSY_RX;
1353 hi2c->Mode = HAL_I2C_MODE_MASTER;
1354 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1356 /* Prepare transfer parameters */
1357 hi2c->pBuffPtr = pData;
1358 hi2c->XferCount = Size;
1359 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
1360 hi2c->XferSize = hi2c->XferCount;
1361 hi2c->Devaddress = DevAddress;
1363 /* Enable Acknowledge */
1364 hi2c->Instance->CR1 |= I2C_CR1_ACK;
1366 /* Generate Start */
1367 hi2c->Instance->CR1 |= I2C_CR1_START;
1369 /* Process Unlocked */
1370 __HAL_UNLOCK(hi2c);
1372 /* Note : The I2C interrupts must be enabled after unlocking current process
1373 to avoid the risk of I2C interrupt handle execution before current
1374 process unlock */
1376 /* Enable EVT, BUF and ERR interrupt */
1377 __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
1379 return HAL_OK;
1381 else
1383 return HAL_BUSY;
1388 * @brief Sequential transmit in master mode an amount of data in non-blocking mode with Interrupt
1389 * @note This interface allow to manage repeated start condition when a direction change during transfer
1390 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
1391 * the configuration information for the specified I2C.
1392 * @param DevAddress Target device address: The device 7 bits address value
1393 * in datasheet must be shift at right before call interface
1394 * @param pData Pointer to data buffer
1395 * @param Size Amount of data to be sent
1396 * @param XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition
1397 * @retval HAL status
1399 HAL_StatusTypeDef HAL_I2C_Master_Sequential_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
1401 __IO uint32_t Prev_State = 0x00U;
1402 __IO uint32_t count = 0x00U;
1404 /* Check the parameters */
1405 assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
1407 if(hi2c->State == HAL_I2C_STATE_READY)
1409 /* Check Busy Flag only if FIRST call of Master interface */
1410 if((XferOptions == I2C_FIRST_AND_LAST_FRAME) || (XferOptions == I2C_FIRST_FRAME))
1412 /* Wait until BUSY flag is reset */
1413 count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock /25U /1000U);
1416 if(count-- == 0U)
1418 hi2c->PreviousState = I2C_STATE_NONE;
1419 hi2c->State= HAL_I2C_STATE_READY;
1421 /* Process Unlocked */
1422 __HAL_UNLOCK(hi2c);
1424 return HAL_TIMEOUT;
1427 while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
1430 /* Process Locked */
1431 __HAL_LOCK(hi2c);
1433 /* Check if the I2C is already enabled */
1434 if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
1436 /* Enable I2C peripheral */
1437 __HAL_I2C_ENABLE(hi2c);
1440 /* Disable Pos */
1441 hi2c->Instance->CR1 &= ~I2C_CR1_POS;
1443 hi2c->State = HAL_I2C_STATE_BUSY_TX;
1444 hi2c->Mode = HAL_I2C_MODE_MASTER;
1445 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1447 /* Prepare transfer parameters */
1448 hi2c->pBuffPtr = pData;
1449 hi2c->XferCount = Size;
1450 hi2c->XferOptions = XferOptions;
1451 hi2c->XferSize = hi2c->XferCount;
1452 hi2c->Devaddress = DevAddress;
1454 Prev_State = hi2c->PreviousState;
1456 /* Generate Start */
1457 if((Prev_State == I2C_STATE_MASTER_BUSY_RX) || (Prev_State == I2C_STATE_NONE))
1459 /* Generate Start condition if first transfer */
1460 if((XferOptions == I2C_FIRST_AND_LAST_FRAME) || (XferOptions == I2C_FIRST_FRAME))
1462 /* Generate Start */
1463 hi2c->Instance->CR1 |= I2C_CR1_START;
1465 else
1467 /* Generate ReStart */
1468 hi2c->Instance->CR1 |= I2C_CR1_START;
1472 /* Process Unlocked */
1473 __HAL_UNLOCK(hi2c);
1475 /* Note : The I2C interrupts must be enabled after unlocking current process
1476 to avoid the risk of I2C interrupt handle execution before current
1477 process unlock */
1479 /* Enable EVT, BUF and ERR interrupt */
1480 __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
1482 return HAL_OK;
1484 else
1486 return HAL_BUSY;
1491 * @brief Sequential receive in master mode an amount of data in non-blocking mode with Interrupt
1492 * @note This interface allow to manage repeated start condition when a direction change during transfer
1493 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
1494 * the configuration information for the specified I2C.
1495 * @param DevAddress Target device address: The device 7 bits address value
1496 * in datasheet must be shift at right before call interface
1497 * @param pData Pointer to data buffer
1498 * @param Size Amount of data to be sent
1499 * @param XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition
1500 * @retval HAL status
1502 HAL_StatusTypeDef HAL_I2C_Master_Sequential_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
1504 __IO uint32_t count = 0U;
1506 /* Check the parameters */
1507 assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
1509 if(hi2c->State == HAL_I2C_STATE_READY)
1511 /* Check Busy Flag only if FIRST call of Master interface */
1512 if((XferOptions == I2C_FIRST_AND_LAST_FRAME) || (XferOptions == I2C_FIRST_FRAME))
1514 /* Wait until BUSY flag is reset */
1515 count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock /25U /1000U);
1518 if(count-- == 0U)
1520 hi2c->PreviousState = I2C_STATE_NONE;
1521 hi2c->State= HAL_I2C_STATE_READY;
1523 /* Process Unlocked */
1524 __HAL_UNLOCK(hi2c);
1526 return HAL_TIMEOUT;
1529 while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
1532 /* Process Locked */
1533 __HAL_LOCK(hi2c);
1535 /* Check if the I2C is already enabled */
1536 if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
1538 /* Enable I2C peripheral */
1539 __HAL_I2C_ENABLE(hi2c);
1542 /* Disable Pos */
1543 hi2c->Instance->CR1 &= ~I2C_CR1_POS;
1545 hi2c->State = HAL_I2C_STATE_BUSY_RX;
1546 hi2c->Mode = HAL_I2C_MODE_MASTER;
1547 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1549 /* Prepare transfer parameters */
1550 hi2c->pBuffPtr = pData;
1551 hi2c->XferCount = Size;
1552 hi2c->XferOptions = XferOptions;
1553 hi2c->XferSize = hi2c->XferCount;
1554 hi2c->Devaddress = DevAddress;
1556 if((hi2c->PreviousState == I2C_STATE_MASTER_BUSY_TX) || (hi2c->PreviousState == I2C_STATE_NONE))
1558 /* Generate Start condition if first transfer */
1559 if((XferOptions == I2C_FIRST_AND_LAST_FRAME) || (XferOptions == I2C_FIRST_FRAME) || (XferOptions == I2C_NO_OPTION_FRAME))
1561 /* Enable Acknowledge */
1562 hi2c->Instance->CR1 |= I2C_CR1_ACK;
1564 /* Generate Start */
1565 hi2c->Instance->CR1 |= I2C_CR1_START;
1567 else
1569 /* Enable Acknowledge */
1570 hi2c->Instance->CR1 |= I2C_CR1_ACK;
1572 /* Generate ReStart */
1573 hi2c->Instance->CR1 |= I2C_CR1_START;
1577 /* Process Unlocked */
1578 __HAL_UNLOCK(hi2c);
1580 /* Note : The I2C interrupts must be enabled after unlocking current process
1581 to avoid the risk of I2C interrupt handle execution before current
1582 process unlock */
1584 /* Enable EVT, BUF and ERR interrupt */
1585 __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
1587 return HAL_OK;
1589 else
1591 return HAL_BUSY;
1596 * @brief Transmit in slave mode an amount of data in non-blocking mode with Interrupt
1597 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
1598 * the configuration information for the specified I2C.
1599 * @param pData Pointer to data buffer
1600 * @param Size Amount of data to be sent
1601 * @retval HAL status
1603 HAL_StatusTypeDef HAL_I2C_Slave_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
1605 __IO uint32_t count = 0U;
1607 if(hi2c->State == HAL_I2C_STATE_READY)
1609 if((pData == NULL) || (Size == 0U))
1611 return HAL_ERROR;
1614 /* Wait until BUSY flag is reset */
1615 count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock /25U /1000U);
1618 if(count-- == 0U)
1620 hi2c->PreviousState = I2C_STATE_NONE;
1621 hi2c->State= HAL_I2C_STATE_READY;
1623 /* Process Unlocked */
1624 __HAL_UNLOCK(hi2c);
1626 return HAL_TIMEOUT;
1629 while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
1631 /* Process Locked */
1632 __HAL_LOCK(hi2c);
1634 /* Check if the I2C is already enabled */
1635 if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
1637 /* Enable I2C peripheral */
1638 __HAL_I2C_ENABLE(hi2c);
1641 /* Disable Pos */
1642 hi2c->Instance->CR1 &= ~I2C_CR1_POS;
1644 hi2c->State = HAL_I2C_STATE_BUSY_TX;
1645 hi2c->Mode = HAL_I2C_MODE_SLAVE;
1646 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1648 /* Prepare transfer parameters */
1649 hi2c->pBuffPtr = pData;
1650 hi2c->XferCount = Size;
1651 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
1652 hi2c->XferSize = hi2c->XferCount;
1654 /* Enable Address Acknowledge */
1655 hi2c->Instance->CR1 |= I2C_CR1_ACK;
1657 /* Process Unlocked */
1658 __HAL_UNLOCK(hi2c);
1660 /* Note : The I2C interrupts must be enabled after unlocking current process
1661 to avoid the risk of I2C interrupt handle execution before current
1662 process unlock */
1664 /* Enable EVT, BUF and ERR interrupt */
1665 __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
1667 return HAL_OK;
1669 else
1671 return HAL_BUSY;
1676 * @brief Receive in slave mode an amount of data in non-blocking mode with Interrupt
1677 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
1678 * the configuration information for the specified I2C.
1679 * @param pData Pointer to data buffer
1680 * @param Size Amount of data to be sent
1681 * @retval HAL status
1683 HAL_StatusTypeDef HAL_I2C_Slave_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
1685 __IO uint32_t count = 0U;
1687 if(hi2c->State == HAL_I2C_STATE_READY)
1689 if((pData == NULL) || (Size == 0U))
1691 return HAL_ERROR;
1694 /* Wait until BUSY flag is reset */
1695 count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock /25U /1000U);
1698 if(count-- == 0U)
1700 hi2c->PreviousState = I2C_STATE_NONE;
1701 hi2c->State= HAL_I2C_STATE_READY;
1703 /* Process Unlocked */
1704 __HAL_UNLOCK(hi2c);
1706 return HAL_TIMEOUT;
1709 while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
1711 /* Process Locked */
1712 __HAL_LOCK(hi2c);
1714 /* Check if the I2C is already enabled */
1715 if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
1717 /* Enable I2C peripheral */
1718 __HAL_I2C_ENABLE(hi2c);
1721 /* Disable Pos */
1722 hi2c->Instance->CR1 &= ~I2C_CR1_POS;
1724 hi2c->State = HAL_I2C_STATE_BUSY_RX;
1725 hi2c->Mode = HAL_I2C_MODE_SLAVE;
1726 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1728 /* Prepare transfer parameters */
1729 hi2c->pBuffPtr = pData;
1730 hi2c->XferSize = Size;
1731 hi2c->XferCount = Size;
1732 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
1734 /* Enable Address Acknowledge */
1735 hi2c->Instance->CR1 |= I2C_CR1_ACK;
1737 /* Process Unlocked */
1738 __HAL_UNLOCK(hi2c);
1740 /* Note : The I2C interrupts must be enabled after unlocking current process
1741 to avoid the risk of I2C interrupt handle execution before current
1742 process unlock */
1744 /* Enable EVT, BUF and ERR interrupt */
1745 __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
1747 return HAL_OK;
1749 else
1751 return HAL_BUSY;
1756 * @brief Sequential transmit in slave mode an amount of data in no-blocking mode with Interrupt
1757 * @note This interface allow to manage repeated start condition when a direction change during transfer
1758 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
1759 * the configuration information for I2C module
1760 * @param pData Pointer to data buffer
1761 * @param Size Amount of data to be sent
1762 * @param XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition
1763 * @retval HAL status
1765 HAL_StatusTypeDef HAL_I2C_Slave_Sequential_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
1767 /* Check the parameters */
1768 assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
1770 if(hi2c->State == HAL_I2C_STATE_LISTEN)
1772 if((pData == NULL) || (Size == 0U))
1774 return HAL_ERROR;
1777 /* Process Locked */
1778 __HAL_LOCK(hi2c);
1780 /* Check if the I2C is already enabled */
1781 if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
1783 /* Enable I2C peripheral */
1784 __HAL_I2C_ENABLE(hi2c);
1787 /* Disable Pos */
1788 hi2c->Instance->CR1 &= ~I2C_CR1_POS;
1790 hi2c->State = HAL_I2C_STATE_BUSY_TX_LISTEN;
1791 hi2c->Mode = HAL_I2C_MODE_SLAVE;
1792 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1794 /* Prepare transfer parameters */
1795 hi2c->pBuffPtr = pData;
1796 hi2c->XferCount = Size;
1797 hi2c->XferOptions = XferOptions;
1798 hi2c->XferSize = hi2c->XferCount;
1800 /* Clear ADDR flag */
1801 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
1803 /* Process Unlocked */
1804 __HAL_UNLOCK(hi2c);
1806 /* Note : The I2C interrupts must be enabled after unlocking current process
1807 to avoid the risk of I2C interrupt handle execution before current
1808 process unlock */
1810 /* Enable EVT, BUF and ERR interrupt */
1811 __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
1813 return HAL_OK;
1815 else
1817 return HAL_BUSY;
1822 * @brief Sequential receive in slave mode an amount of data in non-blocking mode with Interrupt
1823 * @note This interface allow to manage repeated start condition when a direction change during transfer
1824 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
1825 * the configuration information for the specified I2C.
1826 * @param pData Pointer to data buffer
1827 * @param Size Amount of data to be sent
1828 * @param XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition
1829 * @retval HAL status
1831 HAL_StatusTypeDef HAL_I2C_Slave_Sequential_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
1833 /* Check the parameters */
1834 assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
1836 if(hi2c->State == HAL_I2C_STATE_LISTEN)
1838 if((pData == NULL) || (Size == 0U))
1840 return HAL_ERROR;
1843 /* Process Locked */
1844 __HAL_LOCK(hi2c);
1846 /* Check if the I2C is already enabled */
1847 if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
1849 /* Enable I2C peripheral */
1850 __HAL_I2C_ENABLE(hi2c);
1853 /* Disable Pos */
1854 hi2c->Instance->CR1 &= ~I2C_CR1_POS;
1856 hi2c->State = HAL_I2C_STATE_BUSY_RX_LISTEN;
1857 hi2c->Mode = HAL_I2C_MODE_SLAVE;
1858 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1860 /* Prepare transfer parameters */
1861 hi2c->pBuffPtr = pData;
1862 hi2c->XferCount = Size;
1863 hi2c->XferOptions = XferOptions;
1864 hi2c->XferSize = hi2c->XferCount;
1866 /* Clear ADDR flag */
1867 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
1869 /* Process Unlocked */
1870 __HAL_UNLOCK(hi2c);
1872 /* Note : The I2C interrupts must be enabled after unlocking current process
1873 to avoid the risk of I2C interrupt handle execution before current
1874 process unlock */
1876 /* Enable EVT, BUF and ERR interrupt */
1877 __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
1879 return HAL_OK;
1881 else
1883 return HAL_BUSY;
1888 * @brief Enable the Address listen mode with Interrupt.
1889 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
1890 * the configuration information for the specified I2C.
1891 * @retval HAL status
1893 HAL_StatusTypeDef HAL_I2C_EnableListen_IT(I2C_HandleTypeDef *hi2c)
1895 if(hi2c->State == HAL_I2C_STATE_READY)
1897 hi2c->State = HAL_I2C_STATE_LISTEN;
1899 /* Check if the I2C is already enabled */
1900 if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
1902 /* Enable I2C peripheral */
1903 __HAL_I2C_ENABLE(hi2c);
1906 /* Enable Address Acknowledge */
1907 hi2c->Instance->CR1 |= I2C_CR1_ACK;
1909 /* Enable EVT and ERR interrupt */
1910 __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
1912 return HAL_OK;
1914 else
1916 return HAL_BUSY;
1921 * @brief Disable the Address listen mode with Interrupt.
1922 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
1923 * the configuration information for the specified I2C.
1924 * @retval HAL status
1926 HAL_StatusTypeDef HAL_I2C_DisableListen_IT(I2C_HandleTypeDef *hi2c)
1928 /* Declaration of tmp to prevent undefined behavior of volatile usage */
1929 uint32_t tmp;
1931 /* Disable Address listen mode only if a transfer is not ongoing */
1932 if(hi2c->State == HAL_I2C_STATE_LISTEN)
1934 tmp = (uint32_t)(hi2c->State) & I2C_STATE_MSK;
1935 hi2c->PreviousState = tmp | (uint32_t)(hi2c->Mode);
1936 hi2c->State = HAL_I2C_STATE_READY;
1937 hi2c->Mode = HAL_I2C_MODE_NONE;
1939 /* Disable Address Acknowledge */
1940 hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
1942 /* Disable EVT and ERR interrupt */
1943 __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
1945 return HAL_OK;
1947 else
1949 return HAL_BUSY;
1954 * @brief Transmit in master mode an amount of data in non-blocking mode with DMA
1955 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
1956 * the configuration information for the specified I2C.
1957 * @param DevAddress Target device address: The device 7 bits address value
1958 * in datasheet must be shift at right before call interface
1959 * @param pData Pointer to data buffer
1960 * @param Size Amount of data to be sent
1961 * @retval HAL status
1963 HAL_StatusTypeDef HAL_I2C_Master_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
1965 __IO uint32_t count = 0U;
1967 if(hi2c->State == HAL_I2C_STATE_READY)
1969 /* Wait until BUSY flag is reset */
1970 count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock /25U /1000U);
1973 if(count-- == 0U)
1975 hi2c->PreviousState = I2C_STATE_NONE;
1976 hi2c->State= HAL_I2C_STATE_READY;
1978 /* Process Unlocked */
1979 __HAL_UNLOCK(hi2c);
1981 return HAL_TIMEOUT;
1984 while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
1986 /* Process Locked */
1987 __HAL_LOCK(hi2c);
1989 /* Check if the I2C is already enabled */
1990 if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
1992 /* Enable I2C peripheral */
1993 __HAL_I2C_ENABLE(hi2c);
1996 /* Disable Pos */
1997 hi2c->Instance->CR1 &= ~I2C_CR1_POS;
1999 hi2c->State = HAL_I2C_STATE_BUSY_TX;
2000 hi2c->Mode = HAL_I2C_MODE_MASTER;
2001 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
2003 /* Prepare transfer parameters */
2004 hi2c->pBuffPtr = pData;
2005 hi2c->XferCount = Size;
2006 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
2007 hi2c->XferSize = hi2c->XferCount;
2008 hi2c->Devaddress = DevAddress;
2010 if(hi2c->XferSize > 0U)
2012 /* Set the I2C DMA transfer complete callback */
2013 hi2c->hdmatx->XferCpltCallback = I2C_DMAXferCplt;
2015 /* Set the DMA error callback */
2016 hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
2018 /* Set the unused DMA callbacks to NULL */
2019 hi2c->hdmatx->XferHalfCpltCallback = NULL;
2020 hi2c->hdmatx->XferAbortCallback = NULL;
2022 /* Enable the DMA channel */
2023 HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->DR, hi2c->XferSize);
2025 /* Enable Acknowledge */
2026 hi2c->Instance->CR1 |= I2C_CR1_ACK;
2028 /* Generate Start */
2029 hi2c->Instance->CR1 |= I2C_CR1_START;
2031 /* Process Unlocked */
2032 __HAL_UNLOCK(hi2c);
2034 /* Note : The I2C interrupts must be enabled after unlocking current process
2035 to avoid the risk of I2C interrupt handle execution before current
2036 process unlock */
2038 /* Enable EVT and ERR interrupt */
2039 __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
2041 /* Enable DMA Request */
2042 hi2c->Instance->CR2 |= I2C_CR2_DMAEN;
2044 else
2046 /* Enable Acknowledge */
2047 hi2c->Instance->CR1 |= I2C_CR1_ACK;
2049 /* Generate Start */
2050 hi2c->Instance->CR1 |= I2C_CR1_START;
2052 /* Process Unlocked */
2053 __HAL_UNLOCK(hi2c);
2055 /* Note : The I2C interrupts must be enabled after unlocking current process
2056 to avoid the risk of I2C interrupt handle execution before current
2057 process unlock */
2059 /* Enable EVT, BUF and ERR interrupt */
2060 __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
2063 return HAL_OK;
2065 else
2067 return HAL_BUSY;
2072 * @brief Receive in master mode an amount of data in non-blocking mode with DMA
2073 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
2074 * the configuration information for the specified I2C.
2075 * @param DevAddress Target device address: The device 7 bits address value
2076 * in datasheet must be shift at right before call interface
2077 * @param pData Pointer to data buffer
2078 * @param Size Amount of data to be sent
2079 * @retval HAL status
2081 HAL_StatusTypeDef HAL_I2C_Master_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
2083 __IO uint32_t count = 0U;
2085 if(hi2c->State == HAL_I2C_STATE_READY)
2087 /* Wait until BUSY flag is reset */
2088 count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock /25U /1000U);
2091 if(count-- == 0U)
2093 hi2c->PreviousState = I2C_STATE_NONE;
2094 hi2c->State= HAL_I2C_STATE_READY;
2096 /* Process Unlocked */
2097 __HAL_UNLOCK(hi2c);
2099 return HAL_TIMEOUT;
2102 while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
2104 /* Process Locked */
2105 __HAL_LOCK(hi2c);
2107 /* Check if the I2C is already enabled */
2108 if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
2110 /* Enable I2C peripheral */
2111 __HAL_I2C_ENABLE(hi2c);
2114 /* Disable Pos */
2115 hi2c->Instance->CR1 &= ~I2C_CR1_POS;
2117 hi2c->State = HAL_I2C_STATE_BUSY_RX;
2118 hi2c->Mode = HAL_I2C_MODE_MASTER;
2119 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
2121 /* Prepare transfer parameters */
2122 hi2c->pBuffPtr = pData;
2123 hi2c->XferCount = Size;
2124 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
2125 hi2c->XferSize = hi2c->XferCount;
2126 hi2c->Devaddress = DevAddress;
2128 if(hi2c->XferSize > 0U)
2130 /* Set the I2C DMA transfer complete callback */
2131 hi2c->hdmarx->XferCpltCallback = I2C_DMAXferCplt;
2133 /* Set the DMA error callback */
2134 hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
2136 /* Set the unused DMA callbacks to NULL */
2137 hi2c->hdmarx->XferHalfCpltCallback = NULL;
2138 hi2c->hdmarx->XferAbortCallback = NULL;
2140 /* Enable the DMA channel */
2141 HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);
2143 /* Enable Acknowledge */
2144 hi2c->Instance->CR1 |= I2C_CR1_ACK;
2146 /* Generate Start */
2147 hi2c->Instance->CR1 |= I2C_CR1_START;
2149 /* Process Unlocked */
2150 __HAL_UNLOCK(hi2c);
2152 /* Note : The I2C interrupts must be enabled after unlocking current process
2153 to avoid the risk of I2C interrupt handle execution before current
2154 process unlock */
2156 /* Enable EVT and ERR interrupt */
2157 __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
2159 /* Enable DMA Request */
2160 hi2c->Instance->CR2 |= I2C_CR2_DMAEN;
2162 else
2164 /* Enable Acknowledge */
2165 hi2c->Instance->CR1 |= I2C_CR1_ACK;
2167 /* Generate Start */
2168 hi2c->Instance->CR1 |= I2C_CR1_START;
2170 /* Process Unlocked */
2171 __HAL_UNLOCK(hi2c);
2173 /* Note : The I2C interrupts must be enabled after unlocking current process
2174 to avoid the risk of I2C interrupt handle execution before current
2175 process unlock */
2177 /* Enable EVT, BUF and ERR interrupt */
2178 __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
2181 return HAL_OK;
2183 else
2185 return HAL_BUSY;
2190 * @brief Abort a master I2C process communication with Interrupt.
2191 * @note This abort can be called only if state is ready
2192 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
2193 * the configuration information for the specified I2C.
2194 * @param DevAddress Target device address: The device 7 bits address value
2195 * in datasheet must be shift at right before call interface
2196 * @retval HAL status
2198 HAL_StatusTypeDef HAL_I2C_Master_Abort_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress)
2200 /* Prevent unused argument(s) compilation warning */
2201 UNUSED(DevAddress);
2203 /* Abort Master transfer during Receive or Transmit process */
2204 if(hi2c->Mode == HAL_I2C_MODE_MASTER)
2206 /* Process Locked */
2207 __HAL_LOCK(hi2c);
2209 hi2c->PreviousState = I2C_STATE_NONE;
2210 hi2c->State = HAL_I2C_STATE_ABORT;
2212 /* Disable Acknowledge */
2213 hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
2215 /* Generate Stop */
2216 hi2c->Instance->CR1 |= I2C_CR1_STOP;
2218 hi2c->XferCount = 0U;
2220 /* Disable EVT, BUF and ERR interrupt */
2221 __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
2223 /* Process Unlocked */
2224 __HAL_UNLOCK(hi2c);
2226 /* Call the corresponding callback to inform upper layer of End of Transfer */
2227 I2C_ITError(hi2c);
2229 return HAL_OK;
2231 else
2233 /* Wrong usage of abort function */
2234 /* This function should be used only in case of abort monitored by master device */
2235 return HAL_ERROR;
2240 * @brief Transmit in slave mode an amount of data in non-blocking mode with DMA
2241 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
2242 * the configuration information for the specified I2C.
2243 * @param pData Pointer to data buffer
2244 * @param Size Amount of data to be sent
2245 * @retval HAL status
2247 HAL_StatusTypeDef HAL_I2C_Slave_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
2249 __IO uint32_t count = 0U;
2251 if(hi2c->State == HAL_I2C_STATE_READY)
2253 if((pData == NULL) || (Size == 0U))
2255 return HAL_ERROR;
2258 /* Wait until BUSY flag is reset */
2259 count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock /25U /1000U);
2262 if(count-- == 0U)
2264 hi2c->PreviousState = I2C_STATE_NONE;
2265 hi2c->State= HAL_I2C_STATE_READY;
2267 /* Process Unlocked */
2268 __HAL_UNLOCK(hi2c);
2270 return HAL_TIMEOUT;
2273 while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
2275 /* Process Locked */
2276 __HAL_LOCK(hi2c);
2278 /* Check if the I2C is already enabled */
2279 if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
2281 /* Enable I2C peripheral */
2282 __HAL_I2C_ENABLE(hi2c);
2285 /* Disable Pos */
2286 hi2c->Instance->CR1 &= ~I2C_CR1_POS;
2288 hi2c->State = HAL_I2C_STATE_BUSY_TX;
2289 hi2c->Mode = HAL_I2C_MODE_SLAVE;
2290 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
2292 /* Prepare transfer parameters */
2293 hi2c->pBuffPtr = pData;
2294 hi2c->XferCount = Size;
2295 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
2296 hi2c->XferSize = hi2c->XferCount;
2298 /* Set the I2C DMA transfer complete callback */
2299 hi2c->hdmatx->XferCpltCallback = I2C_DMAXferCplt;
2301 /* Set the DMA error callback */
2302 hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
2304 /* Set the unused DMA callbacks to NULL */
2305 hi2c->hdmatx->XferHalfCpltCallback = NULL;
2306 hi2c->hdmatx->XferAbortCallback = NULL;
2308 /* Enable the DMA channel */
2309 HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->DR, hi2c->XferSize);
2311 /* Enable Address Acknowledge */
2312 hi2c->Instance->CR1 |= I2C_CR1_ACK;
2314 /* Process Unlocked */
2315 __HAL_UNLOCK(hi2c);
2317 /* Note : The I2C interrupts must be enabled after unlocking current process
2318 to avoid the risk of I2C interrupt handle execution before current
2319 process unlock */
2320 /* Enable EVT and ERR interrupt */
2321 __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
2323 /* Enable DMA Request */
2324 hi2c->Instance->CR2 |= I2C_CR2_DMAEN;
2326 return HAL_OK;
2328 else
2330 return HAL_BUSY;
2335 * @brief Receive in slave mode an amount of data in non-blocking mode with DMA
2336 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
2337 * the configuration information for the specified I2C.
2338 * @param pData Pointer to data buffer
2339 * @param Size Amount of data to be sent
2340 * @retval HAL status
2342 HAL_StatusTypeDef HAL_I2C_Slave_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
2344 __IO uint32_t count = 0U;
2346 if(hi2c->State == HAL_I2C_STATE_READY)
2348 if((pData == NULL) || (Size == 0U))
2350 return HAL_ERROR;
2353 /* Wait until BUSY flag is reset */
2354 count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock /25U /1000U);
2357 if(count-- == 0U)
2359 hi2c->PreviousState = I2C_STATE_NONE;
2360 hi2c->State= HAL_I2C_STATE_READY;
2362 /* Process Unlocked */
2363 __HAL_UNLOCK(hi2c);
2365 return HAL_TIMEOUT;
2368 while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
2370 /* Process Locked */
2371 __HAL_LOCK(hi2c);
2373 /* Check if the I2C is already enabled */
2374 if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
2376 /* Enable I2C peripheral */
2377 __HAL_I2C_ENABLE(hi2c);
2380 /* Disable Pos */
2381 hi2c->Instance->CR1 &= ~I2C_CR1_POS;
2383 hi2c->State = HAL_I2C_STATE_BUSY_RX;
2384 hi2c->Mode = HAL_I2C_MODE_SLAVE;
2385 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
2387 /* Prepare transfer parameters */
2388 hi2c->pBuffPtr = pData;
2389 hi2c->XferCount = Size;
2390 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
2391 hi2c->XferSize = hi2c->XferCount;
2393 /* Set the I2C DMA transfer complete callback */
2394 hi2c->hdmarx->XferCpltCallback = I2C_DMAXferCplt;
2396 /* Set the DMA error callback */
2397 hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
2399 /* Set the unused DMA callbacks to NULL */
2400 hi2c->hdmarx->XferHalfCpltCallback = NULL;
2401 hi2c->hdmarx->XferAbortCallback = NULL;
2403 /* Enable the DMA channel */
2404 HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);
2406 /* Enable Address Acknowledge */
2407 hi2c->Instance->CR1 |= I2C_CR1_ACK;
2409 /* Process Unlocked */
2410 __HAL_UNLOCK(hi2c);
2412 /* Note : The I2C interrupts must be enabled after unlocking current process
2413 to avoid the risk of I2C interrupt handle execution before current
2414 process unlock */
2415 /* Enable EVT and ERR interrupt */
2416 __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
2418 /* Enable DMA Request */
2419 hi2c->Instance->CR2 |= I2C_CR2_DMAEN;
2421 return HAL_OK;
2423 else
2425 return HAL_BUSY;
2429 * @brief Write an amount of data in blocking mode to a specific memory address
2430 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
2431 * the configuration information for the specified I2C.
2432 * @param DevAddress Target device address
2433 * @param MemAddress Internal memory address
2434 * @param MemAddSize Size of internal memory address
2435 * @param pData Pointer to data buffer
2436 * @param Size Amount of data to be sent
2437 * @param Timeout Timeout duration
2438 * @retval HAL status
2440 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)
2442 uint32_t tickstart = 0x00U;
2444 /* Init tickstart for timeout management*/
2445 tickstart = HAL_GetTick();
2447 /* Check the parameters */
2448 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
2450 if(hi2c->State == HAL_I2C_STATE_READY)
2452 /* Wait until BUSY flag is reset */
2453 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
2455 return HAL_BUSY;
2458 /* Process Locked */
2459 __HAL_LOCK(hi2c);
2461 /* Check if the I2C is already enabled */
2462 if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
2464 /* Enable I2C peripheral */
2465 __HAL_I2C_ENABLE(hi2c);
2468 /* Disable Pos */
2469 hi2c->Instance->CR1 &= ~I2C_CR1_POS;
2471 hi2c->State = HAL_I2C_STATE_BUSY_TX;
2472 hi2c->Mode = HAL_I2C_MODE_MEM;
2473 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
2475 /* Prepare transfer parameters */
2476 hi2c->pBuffPtr = pData;
2477 hi2c->XferCount = Size;
2478 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
2479 hi2c->XferSize = hi2c->XferCount;
2481 /* Send Slave Address and Memory Address */
2482 if(I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, Timeout, tickstart) != HAL_OK)
2484 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
2486 /* Process Unlocked */
2487 __HAL_UNLOCK(hi2c);
2488 return HAL_ERROR;
2490 else
2492 /* Process Unlocked */
2493 __HAL_UNLOCK(hi2c);
2494 return HAL_TIMEOUT;
2498 while(hi2c->XferSize > 0U)
2500 /* Wait until TXE flag is set */
2501 if(I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
2503 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
2505 /* Generate Stop */
2506 hi2c->Instance->CR1 |= I2C_CR1_STOP;
2507 return HAL_ERROR;
2509 else
2511 return HAL_TIMEOUT;
2515 /* Write data to DR */
2516 hi2c->Instance->DR = (*hi2c->pBuffPtr++);
2517 hi2c->XferSize--;
2518 hi2c->XferCount--;
2520 if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (hi2c->XferSize != 0U))
2522 /* Write data to DR */
2523 hi2c->Instance->DR = (*hi2c->pBuffPtr++);
2524 hi2c->XferSize--;
2525 hi2c->XferCount--;
2529 /* Wait until BTF flag is set */
2530 if(I2C_WaitOnBTFFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
2532 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
2534 /* Generate Stop */
2535 hi2c->Instance->CR1 |= I2C_CR1_STOP;
2536 return HAL_ERROR;
2538 else
2540 return HAL_TIMEOUT;
2544 /* Generate Stop */
2545 hi2c->Instance->CR1 |= I2C_CR1_STOP;
2547 hi2c->State = HAL_I2C_STATE_READY;
2548 hi2c->Mode = HAL_I2C_MODE_NONE;
2550 /* Process Unlocked */
2551 __HAL_UNLOCK(hi2c);
2553 return HAL_OK;
2555 else
2557 return HAL_BUSY;
2562 * @brief Read an amount of data in blocking mode from a specific memory address
2563 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
2564 * the configuration information for the specified I2C.
2565 * @param DevAddress Target device address
2566 * @param MemAddress Internal memory address
2567 * @param MemAddSize Size of internal memory address
2568 * @param pData Pointer to data buffer
2569 * @param Size Amount of data to be sent
2570 * @param Timeout Timeout duration
2571 * @retval HAL status
2573 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)
2575 uint32_t tickstart = 0x00U;
2577 /* Init tickstart for timeout management*/
2578 tickstart = HAL_GetTick();
2580 /* Check the parameters */
2581 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
2583 if(hi2c->State == HAL_I2C_STATE_READY)
2585 /* Wait until BUSY flag is reset */
2586 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
2588 return HAL_BUSY;
2591 /* Process Locked */
2592 __HAL_LOCK(hi2c);
2594 /* Check if the I2C is already enabled */
2595 if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
2597 /* Enable I2C peripheral */
2598 __HAL_I2C_ENABLE(hi2c);
2601 /* Disable Pos */
2602 hi2c->Instance->CR1 &= ~I2C_CR1_POS;
2604 hi2c->State = HAL_I2C_STATE_BUSY_RX;
2605 hi2c->Mode = HAL_I2C_MODE_MEM;
2606 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
2608 /* Prepare transfer parameters */
2609 hi2c->pBuffPtr = pData;
2610 hi2c->XferCount = Size;
2611 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
2612 hi2c->XferSize = hi2c->XferCount;
2614 /* Send Slave Address and Memory Address */
2615 if(I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, Timeout, tickstart) != HAL_OK)
2617 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
2619 /* Process Unlocked */
2620 __HAL_UNLOCK(hi2c);
2621 return HAL_ERROR;
2623 else
2625 /* Process Unlocked */
2626 __HAL_UNLOCK(hi2c);
2627 return HAL_TIMEOUT;
2631 if(hi2c->XferSize == 0U)
2633 /* Clear ADDR flag */
2634 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
2636 /* Generate Stop */
2637 hi2c->Instance->CR1 |= I2C_CR1_STOP;
2639 else if(hi2c->XferSize == 1U)
2641 /* Disable Acknowledge */
2642 hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
2644 /* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3
2645 software sequence must complete before the current byte end of transfer */
2646 __disable_irq();
2648 /* Clear ADDR flag */
2649 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
2651 /* Generate Stop */
2652 hi2c->Instance->CR1 |= I2C_CR1_STOP;
2654 /* Re-enable IRQs */
2655 __enable_irq();
2657 else if(hi2c->XferSize == 2U)
2659 /* Enable Pos */
2660 hi2c->Instance->CR1 |= I2C_CR1_POS;
2662 /* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3
2663 software sequence must complete before the current byte end of transfer */
2664 __disable_irq();
2666 /* Clear ADDR flag */
2667 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
2669 /* Disable Acknowledge */
2670 hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
2672 /* Re-enable IRQs */
2673 __enable_irq();
2675 else
2677 /* Enable Acknowledge */
2678 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
2680 /* Clear ADDR flag */
2681 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
2684 while(hi2c->XferSize > 0U)
2686 if(hi2c->XferSize <= 3U)
2688 /* One byte */
2689 if(hi2c->XferSize== 1U)
2691 /* Wait until RXNE flag is set */
2692 if(I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
2694 if(hi2c->ErrorCode == HAL_I2C_ERROR_TIMEOUT)
2696 return HAL_TIMEOUT;
2698 else
2700 return HAL_ERROR;
2704 /* Read data from DR */
2705 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
2706 hi2c->XferSize--;
2707 hi2c->XferCount--;
2709 /* Two bytes */
2710 else if(hi2c->XferSize == 2U)
2712 /* Wait until BTF flag is set */
2713 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK)
2715 return HAL_TIMEOUT;
2718 /* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3
2719 software sequence must complete before the current byte end of transfer */
2720 __disable_irq();
2722 /* Generate Stop */
2723 hi2c->Instance->CR1 |= I2C_CR1_STOP;
2725 /* Read data from DR */
2726 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
2727 hi2c->XferSize--;
2728 hi2c->XferCount--;
2730 /* Re-enable IRQs */
2731 __enable_irq();
2733 /* Read data from DR */
2734 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
2735 hi2c->XferSize--;
2736 hi2c->XferCount--;
2738 /* 3 Last bytes */
2739 else
2741 /* Wait until BTF flag is set */
2742 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK)
2744 return HAL_TIMEOUT;
2747 /* Disable Acknowledge */
2748 hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
2750 /* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3
2751 software sequence must complete before the current byte end of transfer */
2752 __disable_irq();
2754 /* Read data from DR */
2755 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
2756 hi2c->XferSize--;
2757 hi2c->XferCount--;
2759 /* Wait until BTF flag is set */
2760 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK)
2762 return HAL_TIMEOUT;
2765 /* Generate Stop */
2766 hi2c->Instance->CR1 |= I2C_CR1_STOP;
2768 /* Read data from DR */
2769 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
2770 hi2c->XferSize--;
2771 hi2c->XferCount--;
2773 /* Re-enable IRQs */
2774 __enable_irq();
2776 /* Read data from DR */
2777 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
2778 hi2c->XferSize--;
2779 hi2c->XferCount--;
2782 else
2784 /* Wait until RXNE flag is set */
2785 if(I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
2787 if(hi2c->ErrorCode == HAL_I2C_ERROR_TIMEOUT)
2789 return HAL_TIMEOUT;
2791 else
2793 return HAL_ERROR;
2797 /* Read data from DR */
2798 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
2799 hi2c->XferSize--;
2800 hi2c->XferCount--;
2802 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET)
2804 /* Read data from DR */
2805 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
2806 hi2c->XferSize--;
2807 hi2c->XferCount--;
2812 hi2c->State = HAL_I2C_STATE_READY;
2813 hi2c->Mode = HAL_I2C_MODE_NONE;
2815 /* Process Unlocked */
2816 __HAL_UNLOCK(hi2c);
2818 return HAL_OK;
2820 else
2822 return HAL_BUSY;
2827 * @brief Write an amount of data in non-blocking mode with Interrupt to a specific memory address
2828 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
2829 * the configuration information for the specified I2C.
2830 * @param DevAddress Target device address
2831 * @param MemAddress Internal memory address
2832 * @param MemAddSize Size of internal memory address
2833 * @param pData Pointer to data buffer
2834 * @param Size Amount of data to be sent
2835 * @retval HAL status
2837 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)
2839 __IO uint32_t count = 0U;
2841 /* Check the parameters */
2842 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
2844 if(hi2c->State == HAL_I2C_STATE_READY)
2846 /* Wait until BUSY flag is reset */
2847 count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock /25U /1000U);
2850 if(count-- == 0U)
2852 hi2c->PreviousState = I2C_STATE_NONE;
2853 hi2c->State= HAL_I2C_STATE_READY;
2855 /* Process Unlocked */
2856 __HAL_UNLOCK(hi2c);
2858 return HAL_TIMEOUT;
2861 while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
2863 /* Process Locked */
2864 __HAL_LOCK(hi2c);
2866 /* Check if the I2C is already enabled */
2867 if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
2869 /* Enable I2C peripheral */
2870 __HAL_I2C_ENABLE(hi2c);
2873 /* Disable Pos */
2874 hi2c->Instance->CR1 &= ~I2C_CR1_POS;
2876 hi2c->State = HAL_I2C_STATE_BUSY_TX;
2877 hi2c->Mode = HAL_I2C_MODE_MEM;
2878 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
2880 /* Prepare transfer parameters */
2881 hi2c->pBuffPtr = pData;
2882 hi2c->XferSize = Size;
2883 hi2c->XferCount = Size;
2884 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
2885 hi2c->Devaddress = DevAddress;
2886 hi2c->Memaddress = MemAddress;
2887 hi2c->MemaddSize = MemAddSize;
2888 hi2c->EventCount = 0U;
2890 /* Generate Start */
2891 hi2c->Instance->CR1 |= I2C_CR1_START;
2893 /* Process Unlocked */
2894 __HAL_UNLOCK(hi2c);
2896 /* Note : The I2C interrupts must be enabled after unlocking current process
2897 to avoid the risk of I2C interrupt handle execution before current
2898 process unlock */
2900 /* Enable EVT, BUF and ERR interrupt */
2901 __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
2903 return HAL_OK;
2905 else
2907 return HAL_BUSY;
2912 * @brief Read an amount of data in non-blocking mode with Interrupt from a specific memory address
2913 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
2914 * the configuration information for the specified I2C.
2915 * @param DevAddress Target device address
2916 * @param MemAddress Internal memory address
2917 * @param MemAddSize Size of internal memory address
2918 * @param pData Pointer to data buffer
2919 * @param Size Amount of data to be sent
2920 * @retval HAL status
2922 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)
2924 __IO uint32_t count = 0U;
2926 /* Check the parameters */
2927 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
2929 if(hi2c->State == HAL_I2C_STATE_READY)
2931 /* Wait until BUSY flag is reset */
2932 count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock /25U /1000U);
2935 if(count-- == 0U)
2937 hi2c->PreviousState = I2C_STATE_NONE;
2938 hi2c->State= HAL_I2C_STATE_READY;
2940 /* Process Unlocked */
2941 __HAL_UNLOCK(hi2c);
2943 return HAL_TIMEOUT;
2946 while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
2948 /* Process Locked */
2949 __HAL_LOCK(hi2c);
2951 /* Check if the I2C is already enabled */
2952 if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
2954 /* Enable I2C peripheral */
2955 __HAL_I2C_ENABLE(hi2c);
2958 /* Disable Pos */
2959 hi2c->Instance->CR1 &= ~I2C_CR1_POS;
2961 hi2c->State = HAL_I2C_STATE_BUSY_RX;
2962 hi2c->Mode = HAL_I2C_MODE_MEM;
2963 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
2965 /* Prepare transfer parameters */
2966 hi2c->pBuffPtr = pData;
2967 hi2c->XferSize = Size;
2968 hi2c->XferCount = Size;
2969 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
2970 hi2c->Devaddress = DevAddress;
2971 hi2c->Memaddress = MemAddress;
2972 hi2c->MemaddSize = MemAddSize;
2973 hi2c->EventCount = 0U;
2975 /* Enable Acknowledge */
2976 hi2c->Instance->CR1 |= I2C_CR1_ACK;
2978 /* Generate Start */
2979 hi2c->Instance->CR1 |= I2C_CR1_START;
2981 /* Process Unlocked */
2982 __HAL_UNLOCK(hi2c);
2984 if(hi2c->XferSize > 0U)
2986 /* Note : The I2C interrupts must be enabled after unlocking current process
2987 to avoid the risk of I2C interrupt handle execution before current
2988 process unlock */
2990 /* Enable EVT, BUF and ERR interrupt */
2991 __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
2993 return HAL_OK;
2995 else
2997 return HAL_BUSY;
3002 * @brief Write an amount of data in non-blocking mode with DMA to a specific memory address
3003 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
3004 * the configuration information for the specified I2C.
3005 * @param DevAddress Target device address
3006 * @param MemAddress Internal memory address
3007 * @param MemAddSize Size of internal memory address
3008 * @param pData Pointer to data buffer
3009 * @param Size Amount of data to be sent
3010 * @retval HAL status
3012 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)
3014 __IO uint32_t count = 0U;
3016 uint32_t tickstart = 0x00U;
3018 /* Init tickstart for timeout management*/
3019 tickstart = HAL_GetTick();
3021 /* Check the parameters */
3022 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
3024 if(hi2c->State == HAL_I2C_STATE_READY)
3026 /* Wait until BUSY flag is reset */
3027 count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock /25U /1000U);
3030 if(count-- == 0U)
3032 hi2c->PreviousState = I2C_STATE_NONE;
3033 hi2c->State= HAL_I2C_STATE_READY;
3035 /* Process Unlocked */
3036 __HAL_UNLOCK(hi2c);
3038 return HAL_TIMEOUT;
3041 while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
3043 /* Process Locked */
3044 __HAL_LOCK(hi2c);
3046 /* Check if the I2C is already enabled */
3047 if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
3049 /* Enable I2C peripheral */
3050 __HAL_I2C_ENABLE(hi2c);
3053 /* Disable Pos */
3054 hi2c->Instance->CR1 &= ~I2C_CR1_POS;
3056 hi2c->State = HAL_I2C_STATE_BUSY_TX;
3057 hi2c->Mode = HAL_I2C_MODE_MEM;
3058 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
3060 /* Prepare transfer parameters */
3061 hi2c->pBuffPtr = pData;
3062 hi2c->XferSize = Size;
3063 hi2c->XferCount = Size;
3064 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
3066 if(hi2c->XferSize > 0U)
3068 /* Set the I2C DMA transfer complete callback */
3069 hi2c->hdmatx->XferCpltCallback = I2C_DMAXferCplt;
3071 /* Set the DMA error callback */
3072 hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
3074 /* Set the unused DMA callbacks to NULL */
3075 hi2c->hdmatx->XferHalfCpltCallback = NULL;
3076 hi2c->hdmatx->XferAbortCallback = NULL;
3078 /* Enable the DMA channel */
3079 HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->DR, hi2c->XferSize);
3081 /* Send Slave Address and Memory Address */
3082 if(I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
3084 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3086 /* Process Unlocked */
3087 __HAL_UNLOCK(hi2c);
3088 return HAL_ERROR;
3090 else
3092 /* Process Unlocked */
3093 __HAL_UNLOCK(hi2c);
3094 return HAL_TIMEOUT;
3098 /* Clear ADDR flag */
3099 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
3101 /* Process Unlocked */
3102 __HAL_UNLOCK(hi2c);
3104 /* Note : The I2C interrupts must be enabled after unlocking current process
3105 to avoid the risk of I2C interrupt handle execution before current
3106 process unlock */
3107 /* Enable ERR interrupt */
3108 __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_ERR);
3110 /* Enable DMA Request */
3111 hi2c->Instance->CR2 |= I2C_CR2_DMAEN;
3113 return HAL_OK;
3115 else
3117 return HAL_BUSY;
3122 * @brief Reads an amount of data in non-blocking mode with DMA from a specific memory address.
3123 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
3124 * the configuration information for the specified I2C.
3125 * @param DevAddress Target device address
3126 * @param MemAddress Internal memory address
3127 * @param MemAddSize Size of internal memory address
3128 * @param pData Pointer to data buffer
3129 * @param Size Amount of data to be read
3130 * @retval HAL status
3132 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)
3134 uint32_t tickstart = 0x00U;
3135 __IO uint32_t count = 0U;
3137 /* Init tickstart for timeout management*/
3138 tickstart = HAL_GetTick();
3140 /* Check the parameters */
3141 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
3143 if(hi2c->State == HAL_I2C_STATE_READY)
3145 /* Wait until BUSY flag is reset */
3146 count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock /25U /1000U);
3149 if(count-- == 0U)
3151 hi2c->PreviousState = I2C_STATE_NONE;
3152 hi2c->State= HAL_I2C_STATE_READY;
3154 /* Process Unlocked */
3155 __HAL_UNLOCK(hi2c);
3157 return HAL_TIMEOUT;
3160 while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
3162 /* Process Locked */
3163 __HAL_LOCK(hi2c);
3165 /* Check if the I2C is already enabled */
3166 if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
3168 /* Enable I2C peripheral */
3169 __HAL_I2C_ENABLE(hi2c);
3172 /* Disable Pos */
3173 hi2c->Instance->CR1 &= ~I2C_CR1_POS;
3175 hi2c->State = HAL_I2C_STATE_BUSY_RX;
3176 hi2c->Mode = HAL_I2C_MODE_MEM;
3177 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
3179 /* Prepare transfer parameters */
3180 hi2c->pBuffPtr = pData;
3181 hi2c->XferCount = Size;
3182 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
3183 hi2c->XferSize = hi2c->XferCount;
3185 if(hi2c->XferSize > 0U)
3187 /* Set the I2C DMA transfer complete callback */
3188 hi2c->hdmarx->XferCpltCallback = I2C_DMAXferCplt;
3190 /* Set the DMA error callback */
3191 hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
3193 /* Set the unused DMA callbacks to NULL */
3194 hi2c->hdmarx->XferAbortCallback = NULL;
3196 /* Enable the DMA channel */
3197 HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);
3199 /* Send Slave Address and Memory Address */
3200 if(I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
3202 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3204 /* Process Unlocked */
3205 __HAL_UNLOCK(hi2c);
3206 return HAL_ERROR;
3208 else
3210 /* Process Unlocked */
3211 __HAL_UNLOCK(hi2c);
3212 return HAL_TIMEOUT;
3216 if(Size == 1U)
3218 /* Disable Acknowledge */
3219 hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
3221 else
3223 /* Enable Last DMA bit */
3224 hi2c->Instance->CR2 |= I2C_CR2_LAST;
3227 /* Clear ADDR flag */
3228 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
3230 /* Process Unlocked */
3231 __HAL_UNLOCK(hi2c);
3233 /* Note : The I2C interrupts must be enabled after unlocking current process
3234 to avoid the risk of I2C interrupt handle execution before current
3235 process unlock */
3236 /* Enable ERR interrupt */
3237 __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_ERR);
3239 /* Enable DMA Request */
3240 hi2c->Instance->CR2 |= I2C_CR2_DMAEN;
3242 else
3244 /* Send Slave Address and Memory Address */
3245 if(I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
3247 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3249 /* Process Unlocked */
3250 __HAL_UNLOCK(hi2c);
3251 return HAL_ERROR;
3253 else
3255 /* Process Unlocked */
3256 __HAL_UNLOCK(hi2c);
3257 return HAL_TIMEOUT;
3261 /* Clear ADDR flag */
3262 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
3264 /* Generate Stop */
3265 hi2c->Instance->CR1 |= I2C_CR1_STOP;
3267 hi2c->State = HAL_I2C_STATE_READY;
3269 /* Process Unlocked */
3270 __HAL_UNLOCK(hi2c);
3273 return HAL_OK;
3275 else
3277 return HAL_BUSY;
3282 * @brief Checks if target device is ready for communication.
3283 * @note This function is used with Memory devices
3284 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
3285 * the configuration information for the specified I2C.
3286 * @param DevAddress Target device address
3287 * @param Trials Number of trials
3288 * @param Timeout Timeout duration
3289 * @retval HAL status
3291 HAL_StatusTypeDef HAL_I2C_IsDeviceReady(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout)
3293 uint32_t tickstart = 0U, tmp1 = 0U, tmp2 = 0U, tmp3 = 0U, I2C_Trials = 1U;
3295 /* Get tick */
3296 tickstart = HAL_GetTick();
3298 if(hi2c->State == HAL_I2C_STATE_READY)
3300 /* Wait until BUSY flag is reset */
3301 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
3303 return HAL_BUSY;
3306 /* Process Locked */
3307 __HAL_LOCK(hi2c);
3309 /* Check if the I2C is already enabled */
3310 if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
3312 /* Enable I2C peripheral */
3313 __HAL_I2C_ENABLE(hi2c);
3316 /* Disable Pos */
3317 hi2c->Instance->CR1 &= ~I2C_CR1_POS;
3319 hi2c->State = HAL_I2C_STATE_BUSY;
3320 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
3321 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
3325 /* Generate Start */
3326 hi2c->Instance->CR1 |= I2C_CR1_START;
3328 /* Wait until SB flag is set */
3329 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, tickstart) != HAL_OK)
3331 return HAL_TIMEOUT;
3334 /* Send slave address */
3335 hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress);
3337 /* Wait until ADDR or AF flag are set */
3338 /* Get tick */
3339 tickstart = HAL_GetTick();
3341 tmp1 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR);
3342 tmp2 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF);
3343 tmp3 = hi2c->State;
3344 while((tmp1 == RESET) && (tmp2 == RESET) && (tmp3 != HAL_I2C_STATE_TIMEOUT))
3346 if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
3348 hi2c->State = HAL_I2C_STATE_TIMEOUT;
3350 tmp1 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR);
3351 tmp2 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF);
3352 tmp3 = hi2c->State;
3355 hi2c->State = HAL_I2C_STATE_READY;
3357 /* Check if the ADDR flag has been set */
3358 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == SET)
3360 /* Generate Stop */
3361 hi2c->Instance->CR1 |= I2C_CR1_STOP;
3363 /* Clear ADDR Flag */
3364 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
3366 /* Wait until BUSY flag is reset */
3367 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
3369 return HAL_TIMEOUT;
3372 hi2c->State = HAL_I2C_STATE_READY;
3374 /* Process Unlocked */
3375 __HAL_UNLOCK(hi2c);
3377 return HAL_OK;
3379 else
3381 /* Generate Stop */
3382 hi2c->Instance->CR1 |= I2C_CR1_STOP;
3384 /* Clear AF Flag */
3385 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
3387 /* Wait until BUSY flag is reset */
3388 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
3390 return HAL_TIMEOUT;
3393 }while(I2C_Trials++ < Trials);
3395 hi2c->State = HAL_I2C_STATE_READY;
3397 /* Process Unlocked */
3398 __HAL_UNLOCK(hi2c);
3400 return HAL_ERROR;
3402 else
3404 return HAL_BUSY;
3409 * @brief This function handles I2C event interrupt request.
3410 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
3411 * the configuration information for the specified I2C.
3412 * @retval None
3414 void HAL_I2C_EV_IRQHandler(I2C_HandleTypeDef *hi2c)
3416 uint32_t sr2itflags = READ_REG(hi2c->Instance->SR2);
3417 uint32_t sr1itflags = READ_REG(hi2c->Instance->SR1);
3418 uint32_t itsources = READ_REG(hi2c->Instance->CR2);
3420 uint32_t CurrentMode = hi2c->Mode;
3422 /* Master or Memory mode selected */
3423 if((CurrentMode == HAL_I2C_MODE_MASTER) || (CurrentMode == HAL_I2C_MODE_MEM))
3425 /* SB Set ----------------------------------------------------------------*/
3426 if(((sr1itflags & I2C_FLAG_SB) != RESET) && ((itsources & I2C_IT_EVT) != RESET))
3428 I2C_Master_SB(hi2c);
3430 /* ADD10 Set -------------------------------------------------------------*/
3431 else if(((sr1itflags & I2C_FLAG_ADD10) != RESET) && ((itsources & I2C_IT_EVT) != RESET))
3433 I2C_Master_ADD10(hi2c);
3435 /* ADDR Set --------------------------------------------------------------*/
3436 else if(((sr1itflags & I2C_FLAG_ADDR) != RESET) && ((itsources & I2C_IT_EVT) != RESET))
3438 I2C_Master_ADDR(hi2c);
3441 /* I2C in mode Transmitter -----------------------------------------------*/
3442 if((sr2itflags & I2C_FLAG_TRA) != RESET)
3444 /* TXE set and BTF reset -----------------------------------------------*/
3445 if(((sr1itflags & I2C_FLAG_TXE) != RESET) && ((itsources & I2C_IT_BUF) != RESET) && ((sr1itflags & I2C_FLAG_BTF) == RESET))
3447 I2C_MasterTransmit_TXE(hi2c);
3449 /* BTF set -------------------------------------------------------------*/
3450 else if(((sr1itflags & I2C_FLAG_BTF) != RESET) && ((itsources & I2C_IT_EVT) != RESET))
3452 I2C_MasterTransmit_BTF(hi2c);
3455 /* I2C in mode Receiver --------------------------------------------------*/
3456 else
3458 /* RXNE set and BTF reset -----------------------------------------------*/
3459 if(((sr1itflags & I2C_FLAG_RXNE) != RESET) && ((itsources & I2C_IT_BUF) != RESET) && ((sr1itflags & I2C_FLAG_BTF) == RESET))
3461 I2C_MasterReceive_RXNE(hi2c);
3463 /* BTF set -------------------------------------------------------------*/
3464 else if(((sr1itflags & I2C_FLAG_BTF) != RESET) && ((itsources & I2C_IT_EVT) != RESET))
3466 I2C_MasterReceive_BTF(hi2c);
3470 /* Slave mode selected */
3471 else
3473 /* ADDR set --------------------------------------------------------------*/
3474 if(((sr1itflags & I2C_FLAG_ADDR) != RESET) && ((itsources & I2C_IT_EVT) != RESET))
3476 I2C_Slave_ADDR(hi2c);
3478 /* STOPF set --------------------------------------------------------------*/
3479 else if(((sr1itflags & I2C_FLAG_STOPF) != RESET) && ((itsources & I2C_IT_EVT) != RESET))
3481 I2C_Slave_STOPF(hi2c);
3483 /* I2C in mode Transmitter -----------------------------------------------*/
3484 else if((sr2itflags & I2C_FLAG_TRA) != RESET)
3486 /* TXE set and BTF reset -----------------------------------------------*/
3487 if(((sr1itflags & I2C_FLAG_TXE) != RESET) && ((itsources & I2C_IT_BUF) != RESET) && ((sr1itflags & I2C_FLAG_BTF) == RESET))
3489 I2C_SlaveTransmit_TXE(hi2c);
3491 /* BTF set -------------------------------------------------------------*/
3492 else if(((sr1itflags & I2C_FLAG_BTF) != RESET) && ((itsources & I2C_IT_EVT) != RESET))
3494 I2C_SlaveTransmit_BTF(hi2c);
3497 /* I2C in mode Receiver --------------------------------------------------*/
3498 else
3500 /* RXNE set and BTF reset ----------------------------------------------*/
3501 if(((sr1itflags & I2C_FLAG_RXNE) != RESET) && ((itsources & I2C_IT_BUF) != RESET) && ((sr1itflags & I2C_FLAG_BTF) == RESET))
3503 I2C_SlaveReceive_RXNE(hi2c);
3505 /* BTF set -------------------------------------------------------------*/
3506 else if(((sr1itflags & I2C_FLAG_BTF) != RESET) && ((itsources & I2C_IT_EVT) != RESET))
3508 I2C_SlaveReceive_BTF(hi2c);
3515 * @brief This function handles I2C error interrupt request.
3516 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
3517 * the configuration information for the specified I2C.
3518 * @retval None
3520 void HAL_I2C_ER_IRQHandler(I2C_HandleTypeDef *hi2c)
3522 uint32_t tmp1 = 0U, tmp2 = 0U, tmp3 = 0U, tmp4 = 0U;
3523 uint32_t sr1itflags = READ_REG(hi2c->Instance->SR1);
3524 uint32_t itsources = READ_REG(hi2c->Instance->CR2);
3526 /* I2C Bus error interrupt occurred ----------------------------------------*/
3527 if(((sr1itflags & I2C_FLAG_BERR) != RESET) && ((itsources & I2C_IT_ERR) != RESET))
3529 hi2c->ErrorCode |= HAL_I2C_ERROR_BERR;
3531 /* Clear BERR flag */
3532 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_BERR);
3534 /* Workaround: Start cannot be generated after a misplaced Stop */
3535 SET_BIT(hi2c->Instance->CR1, I2C_CR1_SWRST);
3538 /* I2C Arbitration Loss error interrupt occurred ---------------------------*/
3539 if(((sr1itflags & I2C_FLAG_ARLO) != RESET) && ((itsources & I2C_IT_ERR) != RESET))
3541 hi2c->ErrorCode |= HAL_I2C_ERROR_ARLO;
3543 /* Clear ARLO flag */
3544 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ARLO);
3547 /* I2C Acknowledge failure error interrupt occurred ------------------------*/
3548 if(((sr1itflags & I2C_FLAG_AF) != RESET) && ((itsources & I2C_IT_ERR) != RESET))
3550 tmp1 = hi2c->Mode;
3551 tmp2 = hi2c->XferCount;
3552 tmp3 = hi2c->State;
3553 tmp4 = hi2c->PreviousState;
3554 if((tmp1 == HAL_I2C_MODE_SLAVE) && (tmp2 == 0U) && \
3555 ((tmp3 == HAL_I2C_STATE_BUSY_TX) || (tmp3 == HAL_I2C_STATE_BUSY_TX_LISTEN) || \
3556 ((tmp3 == HAL_I2C_STATE_LISTEN) && (tmp4 == I2C_STATE_SLAVE_BUSY_TX))))
3558 I2C_Slave_AF(hi2c);
3560 else
3562 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3564 /* Do not generate a STOP in case of Slave receive non acknowledge during transfer (mean not at the end of transfer) */
3565 if(hi2c->Mode == HAL_I2C_MODE_MASTER)
3567 /* Generate Stop */
3568 SET_BIT(hi2c->Instance->CR1,I2C_CR1_STOP);
3571 /* Clear AF flag */
3572 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
3576 /* I2C Over-Run/Under-Run interrupt occurred -------------------------------*/
3577 if(((sr1itflags & I2C_FLAG_OVR) != RESET) && ((itsources & I2C_IT_ERR) != RESET))
3579 hi2c->ErrorCode |= HAL_I2C_ERROR_OVR;
3580 /* Clear OVR flag */
3581 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_OVR);
3584 /* Call the Error Callback in case of Error detected -----------------------*/
3585 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3587 I2C_ITError(hi2c);
3592 * @brief Master Tx Transfer completed callback.
3593 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
3594 * the configuration information for the specified I2C.
3595 * @retval None
3597 __weak void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c)
3599 /* Prevent unused argument(s) compilation warning */
3600 UNUSED(hi2c);
3602 /* NOTE : This function should not be modified, when the callback is needed,
3603 the HAL_I2C_MasterTxCpltCallback can be implemented in the user file
3608 * @brief Master Rx Transfer completed callback.
3609 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
3610 * the configuration information for the specified I2C.
3611 * @retval None
3613 __weak void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c)
3615 /* Prevent unused argument(s) compilation warning */
3616 UNUSED(hi2c);
3618 /* NOTE : This function should not be modified, when the callback is needed,
3619 the HAL_I2C_MasterRxCpltCallback can be implemented in the user file
3623 /** @brief Slave Tx Transfer completed callback.
3624 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
3625 * the configuration information for the specified I2C.
3626 * @retval None
3628 __weak void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c)
3630 /* Prevent unused argument(s) compilation warning */
3631 UNUSED(hi2c);
3633 /* NOTE : This function should not be modified, when the callback is needed,
3634 the HAL_I2C_SlaveTxCpltCallback can be implemented in the user file
3639 * @brief Slave Rx Transfer completed callback.
3640 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
3641 * the configuration information for the specified I2C.
3642 * @retval None
3644 __weak void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c)
3646 /* Prevent unused argument(s) compilation warning */
3647 UNUSED(hi2c);
3649 /* NOTE : This function should not be modified, when the callback is needed,
3650 the HAL_I2C_SlaveRxCpltCallback can be implemented in the user file
3655 * @brief Slave Address Match callback.
3656 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
3657 * the configuration information for the specified I2C.
3658 * @param TransferDirection Master request Transfer Direction (Write/Read), value of @ref I2C_XferOptions_definition
3659 * @param AddrMatchCode Address Match Code
3660 * @retval None
3662 __weak void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode)
3664 /* Prevent unused argument(s) compilation warning */
3665 UNUSED(hi2c);
3666 UNUSED(TransferDirection);
3667 UNUSED(AddrMatchCode);
3669 /* NOTE : This function should not be modified, when the callback is needed,
3670 the HAL_I2C_AddrCallback can be implemented in the user file
3675 * @brief Listen Complete callback.
3676 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
3677 * the configuration information for the specified I2C.
3678 * @retval None
3680 __weak void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c)
3682 /* Prevent unused argument(s) compilation warning */
3683 UNUSED(hi2c);
3685 /* NOTE : This function should not be modified, when the callback is needed,
3686 the HAL_I2C_ListenCpltCallback can be implemented in the user file
3691 * @brief Memory Tx Transfer completed callback.
3692 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
3693 * the configuration information for the specified I2C.
3694 * @retval None
3696 __weak void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef *hi2c)
3698 /* Prevent unused argument(s) compilation warning */
3699 UNUSED(hi2c);
3701 /* NOTE : This function should not be modified, when the callback is needed,
3702 the HAL_I2C_MemTxCpltCallback can be implemented in the user file
3707 * @brief Memory Rx Transfer completed callback.
3708 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
3709 * the configuration information for the specified I2C.
3710 * @retval None
3712 __weak void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c)
3714 /* Prevent unused argument(s) compilation warning */
3715 UNUSED(hi2c);
3717 /* NOTE : This function should not be modified, when the callback is needed,
3718 the HAL_I2C_MemRxCpltCallback can be implemented in the user file
3723 * @brief I2C error callback.
3724 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
3725 * the configuration information for the specified I2C.
3726 * @retval None
3728 __weak void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
3730 /* Prevent unused argument(s) compilation warning */
3731 UNUSED(hi2c);
3733 /* NOTE : This function should not be modified, when the callback is needed,
3734 the HAL_I2C_ErrorCallback can be implemented in the user file
3739 * @brief I2C abort callback.
3740 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
3741 * the configuration information for the specified I2C.
3742 * @retval None
3744 __weak void HAL_I2C_AbortCpltCallback(I2C_HandleTypeDef *hi2c)
3746 /* Prevent unused argument(s) compilation warning */
3747 UNUSED(hi2c);
3749 /* NOTE : This function should not be modified, when the callback is needed,
3750 the HAL_I2C_AbortCpltCallback could be implemented in the user file
3755 * @}
3758 /** @defgroup I2C_Exported_Functions_Group3 Peripheral State, Mode and Error functions
3759 * @brief Peripheral State and Errors functions
3761 @verbatim
3762 ===============================================================================
3763 ##### Peripheral State, Mode and Error functions #####
3764 ===============================================================================
3765 [..]
3766 This subsection permits to get in run-time the status of the peripheral
3767 and the data flow.
3769 @endverbatim
3770 * @{
3774 * @brief Return the I2C handle state.
3775 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
3776 * the configuration information for the specified I2C.
3777 * @retval HAL state
3779 HAL_I2C_StateTypeDef HAL_I2C_GetState(I2C_HandleTypeDef *hi2c)
3781 /* Return I2C handle state */
3782 return hi2c->State;
3786 * @brief Return the I2C Master, Slave, Memory or no mode.
3787 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
3788 * the configuration information for I2C module
3789 * @retval HAL mode
3791 HAL_I2C_ModeTypeDef HAL_I2C_GetMode(I2C_HandleTypeDef *hi2c)
3793 return hi2c->Mode;
3797 * @brief Return the I2C error code
3798 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
3799 * the configuration information for the specified I2C.
3800 * @retval I2C Error Code
3802 uint32_t HAL_I2C_GetError(I2C_HandleTypeDef *hi2c)
3804 return hi2c->ErrorCode;
3808 * @}
3812 * @brief Handle TXE flag for Master
3813 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
3814 * the configuration information for I2C module
3815 * @retval HAL status
3817 static HAL_StatusTypeDef I2C_MasterTransmit_TXE(I2C_HandleTypeDef *hi2c)
3819 /* Declaration of temporary variables to prevent undefined behavior of volatile usage */
3820 uint32_t CurrentState = hi2c->State;
3821 uint32_t CurrentMode = hi2c->Mode;
3822 uint32_t CurrentXferOptions = hi2c->XferOptions;
3824 if((hi2c->XferSize == 0U) && (CurrentState == HAL_I2C_STATE_BUSY_TX))
3826 /* Call TxCpltCallback() directly if no stop mode is set */
3827 if((CurrentXferOptions != I2C_FIRST_AND_LAST_FRAME) && (CurrentXferOptions != I2C_LAST_FRAME) && (CurrentXferOptions != I2C_NO_OPTION_FRAME))
3829 __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
3831 hi2c->PreviousState = I2C_STATE_MASTER_BUSY_TX;
3832 hi2c->Mode = HAL_I2C_MODE_NONE;
3833 hi2c->State = HAL_I2C_STATE_READY;
3835 HAL_I2C_MasterTxCpltCallback(hi2c);
3837 else /* Generate Stop condition then Call TxCpltCallback() */
3839 /* Disable EVT, BUF and ERR interrupt */
3840 __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
3842 /* Generate Stop */
3843 hi2c->Instance->CR1 |= I2C_CR1_STOP;
3845 hi2c->PreviousState = I2C_STATE_NONE;
3846 hi2c->State = HAL_I2C_STATE_READY;
3848 if(hi2c->Mode == HAL_I2C_MODE_MEM)
3850 hi2c->Mode = HAL_I2C_MODE_NONE;
3851 HAL_I2C_MemTxCpltCallback(hi2c);
3853 else
3855 hi2c->Mode = HAL_I2C_MODE_NONE;
3856 HAL_I2C_MasterTxCpltCallback(hi2c);
3860 else if((CurrentState == HAL_I2C_STATE_BUSY_TX) || \
3861 ((CurrentMode == HAL_I2C_MODE_MEM) && (CurrentState == HAL_I2C_STATE_BUSY_RX)))
3863 if(hi2c->XferCount == 0U)
3865 /* Disable BUF interrupt */
3866 __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF);
3868 else
3870 if(hi2c->Mode == HAL_I2C_MODE_MEM)
3872 if(hi2c->EventCount == 0)
3874 /* If Memory address size is 8Bit */
3875 if(hi2c->MemaddSize == I2C_MEMADD_SIZE_8BIT)
3877 /* Send Memory Address */
3878 hi2c->Instance->DR = I2C_MEM_ADD_LSB(hi2c->Memaddress);
3880 hi2c->EventCount += 2;
3882 /* If Memory address size is 16Bit */
3883 else
3885 /* Send MSB of Memory Address */
3886 hi2c->Instance->DR = I2C_MEM_ADD_MSB(hi2c->Memaddress);
3888 hi2c->EventCount++;
3891 else if(hi2c->EventCount == 1)
3893 /* Send LSB of Memory Address */
3894 hi2c->Instance->DR = I2C_MEM_ADD_LSB(hi2c->Memaddress);
3896 hi2c->EventCount++;
3898 else if(hi2c->EventCount == 2)
3900 if(hi2c->State == HAL_I2C_STATE_BUSY_RX)
3902 /* Generate Restart */
3903 hi2c->Instance->CR1 |= I2C_CR1_START;
3905 else if(hi2c->State == HAL_I2C_STATE_BUSY_TX)
3907 /* Write data to DR */
3908 hi2c->Instance->DR = (*hi2c->pBuffPtr++);
3909 hi2c->XferCount--;
3913 else
3915 /* Write data to DR */
3916 hi2c->Instance->DR = (*hi2c->pBuffPtr++);
3917 hi2c->XferCount--;
3921 return HAL_OK;
3925 * @brief Handle BTF flag for Master transmitter
3926 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
3927 * the configuration information for I2C module
3928 * @retval HAL status
3930 static HAL_StatusTypeDef I2C_MasterTransmit_BTF(I2C_HandleTypeDef *hi2c)
3932 /* Declaration of temporary variables to prevent undefined behavior of volatile usage */
3933 uint32_t CurrentXferOptions = hi2c->XferOptions;
3935 if(hi2c->State == HAL_I2C_STATE_BUSY_TX)
3937 if(hi2c->XferCount != 0U)
3939 /* Write data to DR */
3940 hi2c->Instance->DR = (*hi2c->pBuffPtr++);
3941 hi2c->XferCount--;
3943 else
3945 /* Call TxCpltCallback() directly if no stop mode is set */
3946 if((CurrentXferOptions != I2C_FIRST_AND_LAST_FRAME) && (CurrentXferOptions != I2C_LAST_FRAME) && (CurrentXferOptions != I2C_NO_OPTION_FRAME))
3948 __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
3950 hi2c->PreviousState = I2C_STATE_MASTER_BUSY_TX;
3951 hi2c->Mode = HAL_I2C_MODE_NONE;
3952 hi2c->State = HAL_I2C_STATE_READY;
3954 HAL_I2C_MasterTxCpltCallback(hi2c);
3956 else /* Generate Stop condition then Call TxCpltCallback() */
3958 /* Disable EVT, BUF and ERR interrupt */
3959 __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
3961 /* Generate Stop */
3962 hi2c->Instance->CR1 |= I2C_CR1_STOP;
3964 hi2c->PreviousState = I2C_STATE_NONE;
3965 hi2c->State = HAL_I2C_STATE_READY;
3967 if(hi2c->Mode == HAL_I2C_MODE_MEM)
3969 hi2c->Mode = HAL_I2C_MODE_NONE;
3971 HAL_I2C_MemTxCpltCallback(hi2c);
3973 else
3975 hi2c->Mode = HAL_I2C_MODE_NONE;
3977 HAL_I2C_MasterTxCpltCallback(hi2c);
3982 return HAL_OK;
3986 * @brief Handle RXNE flag for Master
3987 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
3988 * the configuration information for I2C module
3989 * @retval HAL status
3991 static HAL_StatusTypeDef I2C_MasterReceive_RXNE(I2C_HandleTypeDef *hi2c)
3993 if(hi2c->State == HAL_I2C_STATE_BUSY_RX)
3995 uint32_t tmp = 0U;
3997 tmp = hi2c->XferCount;
3998 if(tmp > 3U)
4000 /* Read data from DR */
4001 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
4002 hi2c->XferCount--;
4004 else if((tmp == 2U) || (tmp == 3U))
4006 if(hi2c->XferOptions != I2C_NEXT_FRAME)
4008 /* Disable Acknowledge */
4009 hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
4011 /* Enable Pos */
4012 hi2c->Instance->CR1 |= I2C_CR1_POS;
4014 else
4016 /* Enable Acknowledge */
4017 hi2c->Instance->CR1 |= I2C_CR1_ACK;
4020 /* Disable BUF interrupt */
4021 __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF);
4023 else
4025 if(hi2c->XferOptions != I2C_NEXT_FRAME)
4027 /* Disable Acknowledge */
4028 hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
4030 else
4032 /* Enable Acknowledge */
4033 hi2c->Instance->CR1 |= I2C_CR1_ACK;
4036 /* Disable EVT, BUF and ERR interrupt */
4037 __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
4039 /* Read data from DR */
4040 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
4041 hi2c->XferCount--;
4043 hi2c->State = HAL_I2C_STATE_READY;
4044 hi2c->PreviousState = I2C_STATE_NONE;
4046 if(hi2c->Mode == HAL_I2C_MODE_MEM)
4048 hi2c->Mode = HAL_I2C_MODE_NONE;
4049 HAL_I2C_MemRxCpltCallback(hi2c);
4051 else
4053 hi2c->Mode = HAL_I2C_MODE_NONE;
4054 HAL_I2C_MasterRxCpltCallback(hi2c);
4058 return HAL_OK;
4062 * @brief Handle BTF flag for Master receiver
4063 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
4064 * the configuration information for I2C module
4065 * @retval HAL status
4067 static HAL_StatusTypeDef I2C_MasterReceive_BTF(I2C_HandleTypeDef *hi2c)
4069 /* Declaration of temporary variables to prevent undefined behavior of volatile usage */
4070 uint32_t CurrentXferOptions = hi2c->XferOptions;
4072 if(hi2c->XferCount == 3U)
4074 if((CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_LAST_FRAME) || (CurrentXferOptions == I2C_NO_OPTION_FRAME))
4076 /* Disable Acknowledge */
4077 hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
4080 /* Read data from DR */
4081 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
4082 hi2c->XferCount--;
4084 else if(hi2c->XferCount == 2U)
4086 /* Prepare next transfer or stop current transfer */
4087 if((CurrentXferOptions != I2C_FIRST_AND_LAST_FRAME) && (CurrentXferOptions != I2C_LAST_FRAME) && (CurrentXferOptions != I2C_NO_OPTION_FRAME))
4089 if(CurrentXferOptions != I2C_NEXT_FRAME)
4091 /* Disable Acknowledge */
4092 hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
4094 else
4096 /* Enable Acknowledge */
4097 hi2c->Instance->CR1 |= I2C_CR1_ACK;
4100 /* Disable EVT and ERR interrupt */
4101 __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
4103 else
4105 /* Disable EVT and ERR interrupt */
4106 __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
4108 /* Generate Stop */
4109 hi2c->Instance->CR1 |= I2C_CR1_STOP;
4112 /* Read data from DR */
4113 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
4114 hi2c->XferCount--;
4116 /* Read data from DR */
4117 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
4118 hi2c->XferCount--;
4120 hi2c->State = HAL_I2C_STATE_READY;
4121 hi2c->PreviousState = I2C_STATE_NONE;
4123 if(hi2c->Mode == HAL_I2C_MODE_MEM)
4125 hi2c->Mode = HAL_I2C_MODE_NONE;
4127 HAL_I2C_MemRxCpltCallback(hi2c);
4129 else
4131 hi2c->Mode = HAL_I2C_MODE_NONE;
4133 HAL_I2C_MasterRxCpltCallback(hi2c);
4136 else
4138 /* Read data from DR */
4139 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
4140 hi2c->XferCount--;
4142 return HAL_OK;
4146 * @brief Handle SB flag for Master
4147 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
4148 * the configuration information for I2C module
4149 * @retval HAL status
4151 static HAL_StatusTypeDef I2C_Master_SB(I2C_HandleTypeDef *hi2c)
4153 if(hi2c->Mode == HAL_I2C_MODE_MEM)
4155 if(hi2c->EventCount == 0U)
4157 /* Send slave address */
4158 hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(hi2c->Devaddress);
4160 else
4162 hi2c->Instance->DR = I2C_7BIT_ADD_READ(hi2c->Devaddress);
4165 else
4167 if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_7BIT)
4169 /* Send slave 7 Bits address */
4170 if(hi2c->State == HAL_I2C_STATE_BUSY_TX)
4172 hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(hi2c->Devaddress);
4174 else
4176 hi2c->Instance->DR = I2C_7BIT_ADD_READ(hi2c->Devaddress);
4179 else
4181 if(hi2c->EventCount == 0U)
4183 /* Send header of slave address */
4184 hi2c->Instance->DR = I2C_10BIT_HEADER_WRITE(hi2c->Devaddress);
4186 else if(hi2c->EventCount == 1U)
4188 /* Send header of slave address */
4189 hi2c->Instance->DR = I2C_10BIT_HEADER_READ(hi2c->Devaddress);
4194 return HAL_OK;
4198 * @brief Handle ADD10 flag for Master
4199 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
4200 * the configuration information for I2C module
4201 * @retval HAL status
4203 static HAL_StatusTypeDef I2C_Master_ADD10(I2C_HandleTypeDef *hi2c)
4205 /* Send slave address */
4206 hi2c->Instance->DR = I2C_10BIT_ADDRESS(hi2c->Devaddress);
4208 return HAL_OK;
4212 * @brief Handle ADDR flag for Master
4213 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
4214 * the configuration information for I2C module
4215 * @retval HAL status
4217 static HAL_StatusTypeDef I2C_Master_ADDR(I2C_HandleTypeDef *hi2c)
4219 /* Declaration of temporary variable to prevent undefined behavior of volatile usage */
4220 uint32_t CurrentMode = hi2c->Mode;
4221 uint32_t CurrentXferOptions = hi2c->XferOptions;
4222 uint32_t Prev_State = hi2c->PreviousState;
4224 if(hi2c->State == HAL_I2C_STATE_BUSY_RX)
4226 if((hi2c->EventCount == 0U) && (CurrentMode == HAL_I2C_MODE_MEM))
4228 /* Clear ADDR flag */
4229 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
4231 else if((hi2c->EventCount == 0U) && (hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT))
4233 /* Clear ADDR flag */
4234 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
4236 /* Generate Restart */
4237 hi2c->Instance->CR1 |= I2C_CR1_START;
4239 hi2c->EventCount++;
4241 else
4243 if(hi2c->XferCount == 0U)
4245 /* Clear ADDR flag */
4246 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
4248 /* Generate Stop */
4249 hi2c->Instance->CR1 |= I2C_CR1_STOP;
4251 else if(hi2c->XferCount == 1U)
4253 if(CurrentXferOptions == I2C_NO_OPTION_FRAME)
4255 /* Disable Acknowledge */
4256 hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
4258 if((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN)
4260 /* Disable Acknowledge */
4261 hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
4263 /* Clear ADDR flag */
4264 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
4266 else
4268 /* Clear ADDR flag */
4269 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
4271 /* Generate Stop */
4272 hi2c->Instance->CR1 |= I2C_CR1_STOP;
4275 /* Prepare next transfer or stop current transfer */
4276 else if((CurrentXferOptions != I2C_FIRST_AND_LAST_FRAME) && (CurrentXferOptions != I2C_LAST_FRAME) \
4277 && (Prev_State != I2C_STATE_MASTER_BUSY_RX))
4279 if(hi2c->XferOptions != I2C_NEXT_FRAME)
4281 /* Disable Acknowledge */
4282 hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
4284 else
4286 /* Enable Acknowledge */
4287 hi2c->Instance->CR1 |= I2C_CR1_ACK;
4290 /* Clear ADDR flag */
4291 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
4293 else
4295 /* Disable Acknowledge */
4296 hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
4298 /* Clear ADDR flag */
4299 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
4301 /* Generate Stop */
4302 hi2c->Instance->CR1 |= I2C_CR1_STOP;
4305 else if(hi2c->XferCount == 2U)
4307 if(hi2c->XferOptions != I2C_NEXT_FRAME)
4309 /* Enable Pos */
4310 hi2c->Instance->CR1 |= I2C_CR1_POS;
4312 /* Clear ADDR flag */
4313 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
4315 /* Disable Acknowledge */
4316 hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
4318 else
4320 /* Enable Acknowledge */
4321 hi2c->Instance->CR1 |= I2C_CR1_ACK;
4323 /* Clear ADDR flag */
4324 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
4327 if((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN)
4329 /* Enable Last DMA bit */
4330 hi2c->Instance->CR2 |= I2C_CR2_LAST;
4333 else
4335 /* Enable Acknowledge */
4336 hi2c->Instance->CR1 |= I2C_CR1_ACK;
4338 if((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN)
4340 /* Enable Last DMA bit */
4341 hi2c->Instance->CR2 |= I2C_CR2_LAST;
4344 /* Clear ADDR flag */
4345 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
4348 /* Reset Event counter */
4349 hi2c->EventCount = 0U;
4352 else
4354 /* Clear ADDR flag */
4355 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
4358 return HAL_OK;
4362 * @brief Handle TXE flag for Slave
4363 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
4364 * the configuration information for I2C module
4365 * @retval HAL status
4367 static HAL_StatusTypeDef I2C_SlaveTransmit_TXE(I2C_HandleTypeDef *hi2c)
4369 /* Declaration of temporary variables to prevent undefined behavior of volatile usage */
4370 uint32_t CurrentState = hi2c->State;
4372 if(hi2c->XferCount != 0U)
4374 /* Write data to DR */
4375 hi2c->Instance->DR = (*hi2c->pBuffPtr++);
4376 hi2c->XferCount--;
4378 if((hi2c->XferCount == 0U) && (CurrentState == HAL_I2C_STATE_BUSY_TX_LISTEN))
4380 /* Last Byte is received, disable Interrupt */
4381 __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF);
4383 /* Set state at HAL_I2C_STATE_LISTEN */
4384 hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_TX;
4385 hi2c->State = HAL_I2C_STATE_LISTEN;
4387 /* Call the Tx complete callback to inform upper layer of the end of receive process */
4388 HAL_I2C_SlaveTxCpltCallback(hi2c);
4391 return HAL_OK;
4395 * @brief Handle BTF flag for Slave transmitter
4396 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
4397 * the configuration information for I2C module
4398 * @retval HAL status
4400 static HAL_StatusTypeDef I2C_SlaveTransmit_BTF(I2C_HandleTypeDef *hi2c)
4402 if(hi2c->XferCount != 0U)
4404 /* Write data to DR */
4405 hi2c->Instance->DR = (*hi2c->pBuffPtr++);
4406 hi2c->XferCount--;
4408 return HAL_OK;
4412 * @brief Handle RXNE flag for Slave
4413 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
4414 * the configuration information for I2C module
4415 * @retval HAL status
4417 static HAL_StatusTypeDef I2C_SlaveReceive_RXNE(I2C_HandleTypeDef *hi2c)
4419 /* Declaration of temporary variables to prevent undefined behavior of volatile usage */
4420 uint32_t CurrentState = hi2c->State;
4422 if(hi2c->XferCount != 0U)
4424 /* Read data from DR */
4425 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
4426 hi2c->XferCount--;
4428 if((hi2c->XferCount == 0U) && (CurrentState == HAL_I2C_STATE_BUSY_RX_LISTEN))
4430 /* Last Byte is received, disable Interrupt */
4431 __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF);
4433 /* Set state at HAL_I2C_STATE_LISTEN */
4434 hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_RX;
4435 hi2c->State = HAL_I2C_STATE_LISTEN;
4437 /* Call the Rx complete callback to inform upper layer of the end of receive process */
4438 HAL_I2C_SlaveRxCpltCallback(hi2c);
4441 return HAL_OK;
4445 * @brief Handle BTF flag for Slave receiver
4446 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
4447 * the configuration information for I2C module
4448 * @retval HAL status
4450 static HAL_StatusTypeDef I2C_SlaveReceive_BTF(I2C_HandleTypeDef *hi2c)
4452 if(hi2c->XferCount != 0U)
4454 /* Read data from DR */
4455 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
4456 hi2c->XferCount--;
4458 return HAL_OK;
4462 * @brief Handle ADD flag for Slave
4463 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
4464 * the configuration information for I2C module
4465 * @retval HAL status
4467 static HAL_StatusTypeDef I2C_Slave_ADDR(I2C_HandleTypeDef *hi2c)
4469 uint8_t TransferDirection = I2C_DIRECTION_RECEIVE;
4470 uint16_t SlaveAddrCode = 0U;
4472 /* Transfer Direction requested by Master */
4473 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TRA) == RESET)
4475 TransferDirection = I2C_DIRECTION_TRANSMIT;
4478 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_DUALF) == RESET)
4480 SlaveAddrCode = hi2c->Init.OwnAddress1;
4482 else
4484 SlaveAddrCode = hi2c->Init.OwnAddress2;
4487 /* Call Slave Addr callback */
4488 HAL_I2C_AddrCallback(hi2c, TransferDirection, SlaveAddrCode);
4490 return HAL_OK;
4494 * @brief Handle STOPF flag for Slave
4495 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
4496 * the configuration information for I2C module
4497 * @retval HAL status
4499 static HAL_StatusTypeDef I2C_Slave_STOPF(I2C_HandleTypeDef *hi2c)
4501 /* Declaration of temporary variable to prevent undefined behavior of volatile usage */
4502 uint32_t CurrentState = hi2c->State;
4504 /* Disable EVT, BUF and ERR interrupt */
4505 __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
4507 /* Clear STOPF flag */
4508 __HAL_I2C_CLEAR_STOPFLAG(hi2c);
4510 /* Disable Acknowledge */
4511 hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
4513 /* If a DMA is ongoing, Update handle size context */
4514 if((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN)
4516 if((hi2c->State == HAL_I2C_STATE_BUSY_RX) || (hi2c->State == HAL_I2C_STATE_BUSY_RX_LISTEN))
4518 hi2c->XferCount = __HAL_DMA_GET_COUNTER(hi2c->hdmarx);
4520 else
4522 hi2c->XferCount = __HAL_DMA_GET_COUNTER(hi2c->hdmatx);
4526 /* All data are not transferred, so set error code accordingly */
4527 if(hi2c->XferCount != 0U)
4529 /* Store Last receive data if any */
4530 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET)
4532 /* Read data from DR */
4533 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
4534 hi2c->XferCount--;
4537 /* Store Last receive data if any */
4538 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
4540 /* Read data from DR */
4541 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
4542 hi2c->XferCount--;
4545 /* Set ErrorCode corresponding to a Non-Acknowledge */
4546 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
4549 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
4551 /* Call the corresponding callback to inform upper layer of End of Transfer */
4552 I2C_ITError(hi2c);
4554 else
4556 if((CurrentState == HAL_I2C_STATE_LISTEN ) || (CurrentState == HAL_I2C_STATE_BUSY_RX_LISTEN) || \
4557 (CurrentState == HAL_I2C_STATE_BUSY_TX_LISTEN))
4559 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
4560 hi2c->PreviousState = I2C_STATE_NONE;
4561 hi2c->State = HAL_I2C_STATE_READY;
4562 hi2c->Mode = HAL_I2C_MODE_NONE;
4564 /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
4565 HAL_I2C_ListenCpltCallback(hi2c);
4567 else
4569 if((hi2c->PreviousState == I2C_STATE_SLAVE_BUSY_RX) || (CurrentState == HAL_I2C_STATE_BUSY_RX))
4571 hi2c->PreviousState = I2C_STATE_NONE;
4572 hi2c->State = HAL_I2C_STATE_READY;
4573 hi2c->Mode = HAL_I2C_MODE_NONE;
4575 HAL_I2C_SlaveRxCpltCallback(hi2c);
4579 return HAL_OK;
4583 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
4584 * the configuration information for I2C module
4585 * @retval HAL status
4587 static HAL_StatusTypeDef I2C_Slave_AF(I2C_HandleTypeDef *hi2c)
4589 /* Declaration of temporary variables to prevent undefined behavior of volatile usage */
4590 uint32_t CurrentState = hi2c->State;
4591 uint32_t CurrentXferOptions = hi2c->XferOptions;
4593 if(((CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_LAST_FRAME)) && \
4594 (CurrentState == HAL_I2C_STATE_LISTEN))
4596 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
4598 /* Disable EVT, BUF and ERR interrupt */
4599 __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
4601 /* Clear AF flag */
4602 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
4604 /* Disable Acknowledge */
4605 hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
4607 hi2c->PreviousState = I2C_STATE_NONE;
4608 hi2c->State = HAL_I2C_STATE_READY;
4609 hi2c->Mode = HAL_I2C_MODE_NONE;
4611 /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
4612 HAL_I2C_ListenCpltCallback(hi2c);
4614 else if(CurrentState == HAL_I2C_STATE_BUSY_TX)
4616 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
4617 hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_TX;
4618 hi2c->State = HAL_I2C_STATE_READY;
4619 hi2c->Mode = HAL_I2C_MODE_NONE;
4621 /* Disable EVT, BUF and ERR interrupt */
4622 __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
4624 /* Clear AF flag */
4625 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
4627 /* Disable Acknowledge */
4628 hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
4630 HAL_I2C_SlaveTxCpltCallback(hi2c);
4632 else
4634 /* Clear AF flag only */
4635 /* State Listen, but XferOptions == FIRST or NEXT */
4636 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
4639 return HAL_OK;
4643 * @brief I2C interrupts error process
4644 * @param hi2c I2C handle.
4645 * @retval None
4647 static void I2C_ITError(I2C_HandleTypeDef *hi2c)
4649 /* Declaration of temporary variable to prevent undefined behavior of volatile usage */
4650 uint32_t CurrentState = hi2c->State;
4652 if((CurrentState == HAL_I2C_STATE_BUSY_TX_LISTEN) || (CurrentState == HAL_I2C_STATE_BUSY_RX_LISTEN))
4654 /* keep HAL_I2C_STATE_LISTEN */
4655 hi2c->PreviousState = I2C_STATE_NONE;
4656 hi2c->State = HAL_I2C_STATE_LISTEN;
4658 else
4660 /* If state is an abort treatment on going, don't change state */
4661 /* This change will be do later */
4662 if((hi2c->State != HAL_I2C_STATE_ABORT) && ((hi2c->Instance->CR2 & I2C_CR2_DMAEN) != I2C_CR2_DMAEN))
4664 hi2c->State = HAL_I2C_STATE_READY;
4666 hi2c->PreviousState = I2C_STATE_NONE;
4667 hi2c->Mode = HAL_I2C_MODE_NONE;
4670 /* Disable Pos bit in I2C CR1 when error occurred in Master/Mem Receive IT Process */
4671 hi2c->Instance->CR1 &= ~I2C_CR1_POS;
4673 /* Abort DMA transfer */
4674 if((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN)
4676 hi2c->Instance->CR2 &= ~I2C_CR2_DMAEN;
4678 if(hi2c->hdmatx->State != HAL_DMA_STATE_READY)
4680 /* Set the DMA Abort callback :
4681 will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
4682 hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort;
4684 if(HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK)
4686 /* Disable I2C peripheral to prevent dummy data in buffer */
4687 __HAL_I2C_DISABLE(hi2c);
4689 hi2c->State = HAL_I2C_STATE_READY;
4691 /* Call Directly XferAbortCallback function in case of error */
4692 hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx);
4695 else
4697 /* Set the DMA Abort callback :
4698 will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
4699 hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort;
4701 if(HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK)
4703 /* Store Last receive data if any */
4704 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
4706 /* Read data from DR */
4707 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
4710 /* Disable I2C peripheral to prevent dummy data in buffer */
4711 __HAL_I2C_DISABLE(hi2c);
4713 hi2c->State = HAL_I2C_STATE_READY;
4715 /* Call Directly hi2c->hdmarx->XferAbortCallback function in case of error */
4716 hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx);
4720 else if(hi2c->State == HAL_I2C_STATE_ABORT)
4722 hi2c->State = HAL_I2C_STATE_READY;
4723 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
4725 /* Store Last receive data if any */
4726 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
4728 /* Read data from DR */
4729 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
4732 /* Disable I2C peripheral to prevent dummy data in buffer */
4733 __HAL_I2C_DISABLE(hi2c);
4735 /* Call the corresponding callback to inform upper layer of End of Transfer */
4736 HAL_I2C_AbortCpltCallback(hi2c);
4738 else
4740 /* Store Last receive data if any */
4741 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
4743 /* Read data from DR */
4744 (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
4747 /* Call user error callback */
4748 HAL_I2C_ErrorCallback(hi2c);
4750 /* STOP Flag is not set after a NACK reception */
4751 /* So may inform upper layer that listen phase is stopped */
4752 /* during NACK error treatment */
4753 if((hi2c->State == HAL_I2C_STATE_LISTEN) && ((hi2c->ErrorCode & HAL_I2C_ERROR_AF) == HAL_I2C_ERROR_AF))
4755 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
4756 hi2c->PreviousState = I2C_STATE_NONE;
4757 hi2c->State = HAL_I2C_STATE_READY;
4758 hi2c->Mode = HAL_I2C_MODE_NONE;
4760 /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
4761 HAL_I2C_ListenCpltCallback(hi2c);
4766 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
4767 * the configuration information for I2C module
4768 * @param DevAddress Target device address: The device 7 bits address value
4769 * in datasheet must be shift at right before call interface
4770 * @param Timeout Timeout duration
4771 * @param Tickstart Tick start value
4772 * @retval HAL status
4774 static HAL_StatusTypeDef I2C_MasterRequestWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Timeout, uint32_t Tickstart)
4776 /* Declaration of temporary variable to prevent undefined behavior of volatile usage */
4777 uint32_t CurrentXferOptions = hi2c->XferOptions;
4779 /* Generate Start condition if first transfer */
4780 if((CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_FIRST_FRAME) || (CurrentXferOptions == I2C_NO_OPTION_FRAME))
4782 /* Generate Start */
4783 hi2c->Instance->CR1 |= I2C_CR1_START;
4785 else if(hi2c->PreviousState == I2C_STATE_MASTER_BUSY_RX)
4787 /* Generate ReStart */
4788 hi2c->Instance->CR1 |= I2C_CR1_START;
4791 /* Wait until SB flag is set */
4792 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK)
4794 return HAL_TIMEOUT;
4797 if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_7BIT)
4799 /* Send slave address */
4800 hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress);
4802 else
4804 /* Send header of slave address */
4805 hi2c->Instance->DR = I2C_10BIT_HEADER_WRITE(DevAddress);
4807 /* Wait until ADD10 flag is set */
4808 if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADD10, Timeout, Tickstart) != HAL_OK)
4810 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
4812 return HAL_ERROR;
4814 else
4816 return HAL_TIMEOUT;
4820 /* Send slave address */
4821 hi2c->Instance->DR = I2C_10BIT_ADDRESS(DevAddress);
4824 /* Wait until ADDR flag is set */
4825 if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK)
4827 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
4829 return HAL_ERROR;
4831 else
4833 return HAL_TIMEOUT;
4837 return HAL_OK;
4841 * @brief Master sends target device address for read request.
4842 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
4843 * the configuration information for I2C module
4844 * @param DevAddress Target device address: The device 7 bits address value
4845 * in datasheet must be shift at right before call interface
4846 * @param Timeout Timeout duration
4847 * @param Tickstart Tick start value
4848 * @retval HAL status
4850 static HAL_StatusTypeDef I2C_MasterRequestRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Timeout, uint32_t Tickstart)
4852 /* Declaration of temporary variable to prevent undefined behavior of volatile usage */
4853 uint32_t CurrentXferOptions = hi2c->XferOptions;
4855 /* Enable Acknowledge */
4856 hi2c->Instance->CR1 |= I2C_CR1_ACK;
4858 /* Generate Start condition if first transfer */
4859 if((CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_FIRST_FRAME) || (CurrentXferOptions == I2C_NO_OPTION_FRAME))
4861 /* Generate Start */
4862 hi2c->Instance->CR1 |= I2C_CR1_START;
4864 else if(hi2c->PreviousState == I2C_STATE_MASTER_BUSY_TX)
4866 /* Generate ReStart */
4867 hi2c->Instance->CR1 |= I2C_CR1_START;
4870 /* Wait until SB flag is set */
4871 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK)
4873 return HAL_TIMEOUT;
4876 if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_7BIT)
4878 /* Send slave address */
4879 hi2c->Instance->DR = I2C_7BIT_ADD_READ(DevAddress);
4881 else
4883 /* Send header of slave address */
4884 hi2c->Instance->DR = I2C_10BIT_HEADER_WRITE(DevAddress);
4886 /* Wait until ADD10 flag is set */
4887 if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADD10, Timeout, Tickstart) != HAL_OK)
4889 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
4891 return HAL_ERROR;
4893 else
4895 return HAL_TIMEOUT;
4899 /* Send slave address */
4900 hi2c->Instance->DR = I2C_10BIT_ADDRESS(DevAddress);
4902 /* Wait until ADDR flag is set */
4903 if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK)
4905 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
4907 return HAL_ERROR;
4909 else
4911 return HAL_TIMEOUT;
4915 /* Clear ADDR flag */
4916 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
4918 /* Generate Restart */
4919 hi2c->Instance->CR1 |= I2C_CR1_START;
4921 /* Wait until SB flag is set */
4922 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK)
4924 return HAL_TIMEOUT;
4927 /* Send header of slave address */
4928 hi2c->Instance->DR = I2C_10BIT_HEADER_READ(DevAddress);
4931 /* Wait until ADDR flag is set */
4932 if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK)
4934 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
4936 return HAL_ERROR;
4938 else
4940 return HAL_TIMEOUT;
4944 return HAL_OK;
4948 * @brief Master sends target device address followed by internal memory address for write request.
4949 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
4950 * the configuration information for I2C module
4951 * @param DevAddress Target device address
4952 * @param MemAddress Internal memory address
4953 * @param MemAddSize Size of internal memory address
4954 * @param Timeout Timeout duration
4955 * @param Tickstart Tick start value
4956 * @retval HAL status
4958 static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart)
4960 /* Generate Start */
4961 hi2c->Instance->CR1 |= I2C_CR1_START;
4963 /* Wait until SB flag is set */
4964 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK)
4966 return HAL_TIMEOUT;
4969 /* Send slave address */
4970 hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress);
4972 /* Wait until ADDR flag is set */
4973 if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK)
4975 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
4977 return HAL_ERROR;
4979 else
4981 return HAL_TIMEOUT;
4985 /* Clear ADDR flag */
4986 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
4988 /* Wait until TXE flag is set */
4989 if(I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
4991 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
4993 /* Generate Stop */
4994 hi2c->Instance->CR1 |= I2C_CR1_STOP;
4995 return HAL_ERROR;
4997 else
4999 return HAL_TIMEOUT;
5003 /* If Memory address size is 8Bit */
5004 if(MemAddSize == I2C_MEMADD_SIZE_8BIT)
5006 /* Send Memory Address */
5007 hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
5009 /* If Memory address size is 16Bit */
5010 else
5012 /* Send MSB of Memory Address */
5013 hi2c->Instance->DR = I2C_MEM_ADD_MSB(MemAddress);
5015 /* Wait until TXE flag is set */
5016 if(I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
5018 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
5020 /* Generate Stop */
5021 hi2c->Instance->CR1 |= I2C_CR1_STOP;
5022 return HAL_ERROR;
5024 else
5026 return HAL_TIMEOUT;
5030 /* Send LSB of Memory Address */
5031 hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
5034 return HAL_OK;
5038 * @brief Master sends target device address followed by internal memory address for read request.
5039 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
5040 * the configuration information for I2C module
5041 * @param DevAddress Target device address
5042 * @param MemAddress Internal memory address
5043 * @param MemAddSize Size of internal memory address
5044 * @param Timeout Timeout duration
5045 * @param Tickstart Tick start value
5046 * @retval HAL status
5048 static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart)
5050 /* Enable Acknowledge */
5051 hi2c->Instance->CR1 |= I2C_CR1_ACK;
5053 /* Generate Start */
5054 hi2c->Instance->CR1 |= I2C_CR1_START;
5056 /* Wait until SB flag is set */
5057 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK)
5059 return HAL_TIMEOUT;
5062 /* Send slave address */
5063 hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress);
5065 /* Wait until ADDR flag is set */
5066 if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK)
5068 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
5070 return HAL_ERROR;
5072 else
5074 return HAL_TIMEOUT;
5078 /* Clear ADDR flag */
5079 __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
5081 /* Wait until TXE flag is set */
5082 if(I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
5084 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
5086 /* Generate Stop */
5087 hi2c->Instance->CR1 |= I2C_CR1_STOP;
5088 return HAL_ERROR;
5090 else
5092 return HAL_TIMEOUT;
5096 /* If Memory address size is 8Bit */
5097 if(MemAddSize == I2C_MEMADD_SIZE_8BIT)
5099 /* Send Memory Address */
5100 hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
5102 /* If Memory address size is 16Bit */
5103 else
5105 /* Send MSB of Memory Address */
5106 hi2c->Instance->DR = I2C_MEM_ADD_MSB(MemAddress);
5108 /* Wait until TXE flag is set */
5109 if(I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
5111 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
5113 /* Generate Stop */
5114 hi2c->Instance->CR1 |= I2C_CR1_STOP;
5115 return HAL_ERROR;
5117 else
5119 return HAL_TIMEOUT;
5123 /* Send LSB of Memory Address */
5124 hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
5127 /* Wait until TXE flag is set */
5128 if(I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
5130 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
5132 /* Generate Stop */
5133 hi2c->Instance->CR1 |= I2C_CR1_STOP;
5134 return HAL_ERROR;
5136 else
5138 return HAL_TIMEOUT;
5142 /* Generate Restart */
5143 hi2c->Instance->CR1 |= I2C_CR1_START;
5145 /* Wait until SB flag is set */
5146 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK)
5148 return HAL_TIMEOUT;
5151 /* Send slave address */
5152 hi2c->Instance->DR = I2C_7BIT_ADD_READ(DevAddress);
5154 /* Wait until ADDR flag is set */
5155 if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK)
5157 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
5159 return HAL_ERROR;
5161 else
5163 return HAL_TIMEOUT;
5167 return HAL_OK;
5171 * @brief DMA I2C process complete callback.
5172 * @param hdma DMA handle
5173 * @retval None
5175 static void I2C_DMAXferCplt(DMA_HandleTypeDef *hdma)
5177 I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
5179 /* Declaration of temporary variable to prevent undefined behavior of volatile usage */
5180 uint32_t CurrentState = hi2c->State;
5181 uint32_t CurrentMode = hi2c->Mode;
5183 if((CurrentState == HAL_I2C_STATE_BUSY_TX) || ((CurrentState == HAL_I2C_STATE_BUSY_RX) && (CurrentMode == HAL_I2C_MODE_SLAVE)))
5185 /* Disable DMA Request */
5186 hi2c->Instance->CR2 &= ~I2C_CR2_DMAEN;
5188 hi2c->XferCount = 0U;
5190 /* Enable EVT and ERR interrupt */
5191 __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
5193 else
5195 /* Disable Acknowledge */
5196 hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
5198 /* Generate Stop */
5199 hi2c->Instance->CR1 |= I2C_CR1_STOP;
5201 /* Disable Last DMA */
5202 hi2c->Instance->CR2 &= ~I2C_CR2_LAST;
5204 /* Disable DMA Request */
5205 hi2c->Instance->CR2 &= ~I2C_CR2_DMAEN;
5207 hi2c->XferCount = 0U;
5209 /* Check if Errors has been detected during transfer */
5210 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
5212 HAL_I2C_ErrorCallback(hi2c);
5214 else
5216 hi2c->State = HAL_I2C_STATE_READY;
5218 if(hi2c->Mode == HAL_I2C_MODE_MEM)
5220 hi2c->Mode = HAL_I2C_MODE_NONE;
5222 HAL_I2C_MemRxCpltCallback(hi2c);
5224 else
5226 hi2c->Mode = HAL_I2C_MODE_NONE;
5228 HAL_I2C_MasterRxCpltCallback(hi2c);
5235 * @brief DMA I2C communication error callback.
5236 * @param hdma DMA handle
5237 * @retval None
5239 static void I2C_DMAError(DMA_HandleTypeDef *hdma)
5241 I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
5243 /* Disable Acknowledge */
5244 hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
5246 hi2c->XferCount = 0U;
5248 hi2c->State = HAL_I2C_STATE_READY;
5249 hi2c->Mode = HAL_I2C_MODE_NONE;
5251 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
5253 HAL_I2C_ErrorCallback(hi2c);
5257 * @brief DMA I2C communication abort callback
5258 * (To be called at end of DMA Abort procedure).
5259 * @param hdma: DMA handle.
5260 * @retval None
5262 static void I2C_DMAAbort(DMA_HandleTypeDef *hdma)
5264 I2C_HandleTypeDef* hi2c = ( I2C_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
5266 /* Disable Acknowledge */
5267 hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
5269 hi2c->XferCount = 0U;
5271 /* Reset XferAbortCallback */
5272 hi2c->hdmatx->XferAbortCallback = NULL;
5273 hi2c->hdmarx->XferAbortCallback = NULL;
5275 /* Check if come from abort from user */
5276 if(hi2c->State == HAL_I2C_STATE_ABORT)
5278 hi2c->State = HAL_I2C_STATE_READY;
5279 hi2c->Mode = HAL_I2C_MODE_NONE;
5280 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
5282 /* Disable I2C peripheral to prevent dummy data in buffer */
5283 __HAL_I2C_DISABLE(hi2c);
5285 /* Call the corresponding callback to inform upper layer of End of Transfer */
5286 HAL_I2C_AbortCpltCallback(hi2c);
5288 else
5290 hi2c->State = HAL_I2C_STATE_READY;
5291 hi2c->Mode = HAL_I2C_MODE_NONE;
5293 /* Disable I2C peripheral to prevent dummy data in buffer */
5294 __HAL_I2C_DISABLE(hi2c);
5296 /* Call the corresponding callback to inform upper layer of End of Transfer */
5297 HAL_I2C_ErrorCallback(hi2c);
5302 * @brief This function handles I2C Communication Timeout.
5303 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
5304 * the configuration information for I2C module
5305 * @param Flag specifies the I2C flag to check.
5306 * @param Status The new Flag status (SET or RESET).
5307 * @param Timeout Timeout duration
5308 * @param Tickstart Tick start value
5309 * @retval HAL status
5311 static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout, uint32_t Tickstart)
5313 /* Wait until flag is set */
5314 while((__HAL_I2C_GET_FLAG(hi2c, Flag) ? SET : RESET) == Status)
5316 /* Check for the Timeout */
5317 if(Timeout != HAL_MAX_DELAY)
5319 if((Timeout == 0U)||((HAL_GetTick() - Tickstart ) > Timeout))
5321 hi2c->PreviousState = I2C_STATE_NONE;
5322 hi2c->State= HAL_I2C_STATE_READY;
5323 hi2c->Mode = HAL_I2C_MODE_NONE;
5325 /* Process Unlocked */
5326 __HAL_UNLOCK(hi2c);
5328 return HAL_TIMEOUT;
5333 return HAL_OK;
5337 * @brief This function handles I2C Communication Timeout for Master addressing phase.
5338 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
5339 * the configuration information for I2C module
5340 * @param Flag specifies the I2C flag to check.
5341 * @param Timeout Timeout duration
5342 * @param Tickstart Tick start value
5343 * @retval HAL status
5345 static HAL_StatusTypeDef I2C_WaitOnMasterAddressFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, uint32_t Timeout, uint32_t Tickstart)
5347 while(__HAL_I2C_GET_FLAG(hi2c, Flag) == RESET)
5349 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
5351 /* Generate Stop */
5352 hi2c->Instance->CR1 |= I2C_CR1_STOP;
5354 /* Clear AF Flag */
5355 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
5357 hi2c->ErrorCode = HAL_I2C_ERROR_AF;
5358 hi2c->PreviousState = I2C_STATE_NONE;
5359 hi2c->State= HAL_I2C_STATE_READY;
5361 /* Process Unlocked */
5362 __HAL_UNLOCK(hi2c);
5364 return HAL_ERROR;
5367 /* Check for the Timeout */
5368 if(Timeout != HAL_MAX_DELAY)
5370 if((Timeout == 0U)||((HAL_GetTick() - Tickstart ) > Timeout))
5372 hi2c->PreviousState = I2C_STATE_NONE;
5373 hi2c->State= HAL_I2C_STATE_READY;
5375 /* Process Unlocked */
5376 __HAL_UNLOCK(hi2c);
5378 return HAL_TIMEOUT;
5382 return HAL_OK;
5386 * @brief This function handles I2C Communication Timeout for specific usage of TXE flag.
5387 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
5388 * the configuration information for the specified I2C.
5389 * @param Timeout Timeout duration
5390 * @param Tickstart Tick start value
5391 * @retval HAL status
5393 static HAL_StatusTypeDef I2C_WaitOnTXEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart)
5395 while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXE) == RESET)
5397 /* Check if a NACK is detected */
5398 if(I2C_IsAcknowledgeFailed(hi2c) != HAL_OK)
5400 return HAL_ERROR;
5403 /* Check for the Timeout */
5404 if(Timeout != HAL_MAX_DELAY)
5406 if((Timeout == 0U) || ((HAL_GetTick()-Tickstart) > Timeout))
5408 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
5409 hi2c->PreviousState = I2C_STATE_NONE;
5410 hi2c->State= HAL_I2C_STATE_READY;
5412 /* Process Unlocked */
5413 __HAL_UNLOCK(hi2c);
5415 return HAL_TIMEOUT;
5419 return HAL_OK;
5423 * @brief This function handles I2C Communication Timeout for specific usage of BTF flag.
5424 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
5425 * the configuration information for the specified I2C.
5426 * @param Timeout Timeout duration
5427 * @param Tickstart Tick start value
5428 * @retval HAL status
5430 static HAL_StatusTypeDef I2C_WaitOnBTFFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart)
5432 while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == RESET)
5434 /* Check if a NACK is detected */
5435 if(I2C_IsAcknowledgeFailed(hi2c) != HAL_OK)
5437 return HAL_ERROR;
5440 /* Check for the Timeout */
5441 if(Timeout != HAL_MAX_DELAY)
5443 if((Timeout == 0U) || ((HAL_GetTick()-Tickstart) > Timeout))
5445 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
5446 hi2c->PreviousState = I2C_STATE_NONE;
5447 hi2c->State= HAL_I2C_STATE_READY;
5449 /* Process Unlocked */
5450 __HAL_UNLOCK(hi2c);
5452 return HAL_TIMEOUT;
5456 return HAL_OK;
5460 * @brief This function handles I2C Communication Timeout for specific usage of STOP flag.
5461 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
5462 * the configuration information for the specified I2C.
5463 * @param Timeout Timeout duration
5464 * @param Tickstart Tick start value
5465 * @retval HAL status
5467 static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart)
5469 while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET)
5471 /* Check if a NACK is detected */
5472 if(I2C_IsAcknowledgeFailed(hi2c) != HAL_OK)
5474 return HAL_ERROR;
5477 /* Check for the Timeout */
5478 if((Timeout == 0U) || ((HAL_GetTick()-Tickstart) > Timeout))
5480 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
5481 hi2c->PreviousState = I2C_STATE_NONE;
5482 hi2c->State= HAL_I2C_STATE_READY;
5484 /* Process Unlocked */
5485 __HAL_UNLOCK(hi2c);
5487 return HAL_TIMEOUT;
5490 return HAL_OK;
5494 * @brief This function handles I2C Communication Timeout for specific usage of RXNE flag.
5495 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
5496 * the configuration information for the specified I2C.
5497 * @param Timeout Timeout duration
5498 * @param Tickstart Tick start value
5499 * @retval HAL status
5501 static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart)
5504 while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == RESET)
5506 /* Check if a STOPF is detected */
5507 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
5509 /* Clear STOP Flag */
5510 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
5512 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
5513 hi2c->PreviousState = I2C_STATE_NONE;
5514 hi2c->State= HAL_I2C_STATE_READY;
5516 /* Process Unlocked */
5517 __HAL_UNLOCK(hi2c);
5519 return HAL_ERROR;
5522 /* Check for the Timeout */
5523 if((Timeout == 0U) || ((HAL_GetTick()-Tickstart) > Timeout))
5525 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
5526 hi2c->State= HAL_I2C_STATE_READY;
5528 /* Process Unlocked */
5529 __HAL_UNLOCK(hi2c);
5531 return HAL_TIMEOUT;
5534 return HAL_OK;
5538 * @brief This function handles Acknowledge failed detection during an I2C Communication.
5539 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
5540 * the configuration information for the specified I2C.
5541 * @retval HAL status
5543 static HAL_StatusTypeDef I2C_IsAcknowledgeFailed(I2C_HandleTypeDef *hi2c)
5545 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
5547 /* Clear NACKF Flag */
5548 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
5550 hi2c->ErrorCode = HAL_I2C_ERROR_AF;
5551 hi2c->PreviousState = I2C_STATE_NONE;
5552 hi2c->State= HAL_I2C_STATE_READY;
5554 /* Process Unlocked */
5555 __HAL_UNLOCK(hi2c);
5557 return HAL_ERROR;
5559 return HAL_OK;
5562 * @}
5565 #endif /* HAL_I2C_MODULE_ENABLED */
5568 * @}
5572 * @}
5575 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/