]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F0/stm32f0xx_hal_i2c.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32F0 / stm32f0xx_hal_i2c.c
1 /**
2 ******************************************************************************
3 * @file stm32f0xx_hal_i2c.c
4 * @author MCD Application Team
5 * @version V1.2.0
6 * @date 11-December-2014
7 * @brief I2C HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the Inter Integrated Circuit (I2C) peripheral:
10 * + Initialization and de-initialization functions
11 * + IO operation functions
12 * + Peripheral State functions
13 *
14 @verbatim
15 ==============================================================================
16 ##### How to use this driver #####
17 ==============================================================================
18 [..]
19 The I2C HAL driver can be used as follows:
20
21 (#) Declare a I2C_HandleTypeDef handle structure, for example:
22 I2C_HandleTypeDef hi2c;
23
24 (#)Initialize the I2C low level resources by implement the HAL_I2C_MspInit ()API:
25 (++) Enable the I2Cx interface clock
26 (++) I2C pins configuration
27 (+++) Enable the clock for the I2C GPIOs
28 (+++) Configure I2C pins as alternate function open-drain
29 (++) NVIC configuration if you need to use interrupt process
30 (+++) Configure the I2Cx interrupt priority
31 (+++) Enable the NVIC I2C IRQ Channel
32 (++) DMA Configuration if you need to use DMA process
33 (+++) Declare a DMA_HandleTypeDef handle structure for the transmit or receive channel
34 (+++) Enable the DMAx interface clock using
35 (+++) Configure the DMA handle parameters
36 (+++) Configure the DMA Tx or Rx channel
37 (+++) Associate the initilalized DMA handle to the hi2c DMA Tx or Rx handle
38 (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx or Rx channel
39
40 (#) Configure the Communication Clock Timing, Own Address1, Master Adressing Mode, Dual Addressing mode,
41 Own Address2, Own Address2 Mask, General call and Nostretch mode in the hi2c Init structure.
42
43 (#) Initialize the I2C registers by calling the HAL_I2C_Init() API:
44 (++) These API s configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
45 by calling the customed HAL_I2C_MspInit(&hi2c) API.
46
47 (#) To check if target device is ready for communication, use the function HAL_I2C_IsDeviceReady()
48
49 (#) For I2C IO and IO MEM operations, three modes of operations are available within this driver :
50
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()
58
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()
64
65
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
84 *** Interrupt mode IO MEM operation ***
85 =======================================
86 [..]
87 (+) Write an amount of data in no-blocking mode with Interrupt to a specific memory address using
88 HAL_I2C_Mem_Write_IT()
89 (+) At MEM end of write transfer HAL_I2C_MemTxCpltCallback is executed and user can
90 add his own code by customization of function pointer HAL_I2C_MemTxCpltCallback
91 (+) Read an amount of data in no-blocking mode with Interrupt from a specific memory address using
92 HAL_I2C_Mem_Read_IT()
93 (+) At MEM end of read transfer HAL_I2C_MemRxCpltCallback is executed and user can
94 add his own code by customization of function pointer HAL_I2C_MemRxCpltCallback
95 (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
96 add his own code by customization of function pointer HAL_I2C_ErrorCallback
97
98 *** DMA mode IO operation ***
99 ==============================
100 [..]
101 (+) Transmit in master mode an amount of data in non blocking mode (DMA) using
102 HAL_I2C_Master_Transmit_DMA()
103 (+) At transmission end of transfer HAL_I2C_MasterTxCpltCallback is executed and user can
104 add his own code by customization of function pointer HAL_I2C_MasterTxCpltCallback
105 (+) Receive in master mode an amount of data in non blocking mode (DMA) using
106 HAL_I2C_Master_Receive_DMA()
107 (+) At reception end of transfer HAL_I2C_MasterRxCpltCallback is executed and user can
108 add his own code by customization of function pointer HAL_I2C_MasterRxCpltCallback
109 (+) Transmit in slave mode an amount of data in non blocking mode (DMA) using
110 HAL_I2C_Slave_Transmit_DMA()
111 (+) At transmission end of transfer HAL_I2C_SlaveTxCpltCallback is executed and user can
112 add his own code by customization of function pointer HAL_I2C_SlaveTxCpltCallback
113 (+) Receive in slave mode an amount of data in non blocking mode (DMA) using
114 HAL_I2C_Slave_Receive_DMA()
115 (+) At reception end of transfer HAL_I2C_SlaveRxCpltCallback is executed and user can
116 add his own code by customization of function pointer HAL_I2C_SlaveRxCpltCallback
117 (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
118 add his own code by customization of function pointer HAL_I2C_ErrorCallback
119
120 *** DMA mode IO MEM operation ***
121 =================================
122 [..]
123 (+) Write an amount of data in no-blocking mode with DMA to a specific memory address using
124 HAL_I2C_Mem_Write_DMA()
125 (+) At MEM end of write transfer HAL_I2C_MemTxCpltCallback is executed and user can
126 add his own code by customization of function pointer HAL_I2C_MemTxCpltCallback
127 (+) Read an amount of data in no-blocking mode with DMA from a specific memory address using
128 HAL_I2C_Mem_Read_DMA()
129 (+) At MEM end of read transfer HAL_I2C_MemRxCpltCallback is executed and user can
130 add his own code by customization of function pointer HAL_I2C_MemRxCpltCallback
131 (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
132 add his own code by customization of function pointer HAL_I2C_ErrorCallback
133
134
135 *** I2C HAL driver macros list ***
136 ==================================
137 [..]
138 Below the list of most used macros in I2C HAL driver.
139
140 (+) __HAL_I2C_ENABLE: Enable the I2C peripheral
141 (+) __HAL_I2C_DISABLE: Disable the I2C peripheral
142 (+) __HAL_I2C_GET_FLAG : Checks whether the specified I2C flag is set or not
143 (+) __HAL_I2C_CLEAR_FLAG : Clears the specified I2C pending flag
144 (+) __HAL_I2C_ENABLE_IT: Enables the specified I2C interrupt
145 (+) __HAL_I2C_DISABLE_IT: Disables the specified I2C interrupt
146
147 [..]
148 (@) You can refer to the I2C HAL driver header file for more useful macros
149
150 @endverbatim
151 ******************************************************************************
152 * @attention
153 *
154 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
155 *
156 * Redistribution and use in source and binary forms, with or without modification,
157 * are permitted provided that the following conditions are met:
158 * 1. Redistributions of source code must retain the above copyright notice,
159 * this list of conditions and the following disclaimer.
160 * 2. Redistributions in binary form must reproduce the above copyright notice,
161 * this list of conditions and the following disclaimer in the documentation
162 * and/or other materials provided with the distribution.
163 * 3. Neither the name of STMicroelectronics nor the names of its contributors
164 * may be used to endorse or promote products derived from this software
165 * without specific prior written permission.
166 *
167 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
168 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
169 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
170 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
171 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
172 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
173 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
174 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
175 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
176 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
177 *
178 ******************************************************************************
179 */
180
181 /* Includes ------------------------------------------------------------------*/
182 #include "stm32f0xx_hal.h"
183
184 /** @addtogroup STM32F0xx_HAL_Driver
185 * @{
186 */
187
188 /** @defgroup I2C I2C HAL module driver
189 * @brief I2C HAL module driver
190 * @{
191 */
192
193 #ifdef HAL_I2C_MODULE_ENABLED
194
195 /* Private typedef -----------------------------------------------------------*/
196 /* Private define ------------------------------------------------------------*/
197
198 /** @defgroup I2C_Private_Define I2C Private Define
199 * @{
200 */
201 #define TIMING_CLEAR_MASK ((uint32_t)0xF0FFFFFF) /*<! I2C TIMING clear register Mask */
202 #define I2C_TIMEOUT_ADDR ((uint32_t)10000) /* 10 s */
203 #define I2C_TIMEOUT_BUSY ((uint32_t)25) /* 25 ms */
204 #define I2C_TIMEOUT_DIR ((uint32_t)25) /* 25 ms */
205 #define I2C_TIMEOUT_RXNE ((uint32_t)25) /* 25 ms */
206 #define I2C_TIMEOUT_STOPF ((uint32_t)25) /* 25 ms */
207 #define I2C_TIMEOUT_TC ((uint32_t)25) /* 25 ms */
208 #define I2C_TIMEOUT_TCR ((uint32_t)25) /* 25 ms */
209 #define I2C_TIMEOUT_TXIS ((uint32_t)25) /* 25 ms */
210 #define I2C_TIMEOUT_FLAG ((uint32_t)25) /* 25 ms */
211 /**
212 * @}
213 */
214
215 /* Private macro -------------------------------------------------------------*/
216 /* Private variables ---------------------------------------------------------*/
217 /* Private function prototypes -----------------------------------------------*/
218
219 /** @defgroup I2C_Private_Functions I2C Private Functions
220 * @{
221 */
222 static void I2C_DMAMasterTransmitCplt(DMA_HandleTypeDef *hdma);
223 static void I2C_DMAMasterReceiveCplt(DMA_HandleTypeDef *hdma);
224 static void I2C_DMASlaveTransmitCplt(DMA_HandleTypeDef *hdma);
225 static void I2C_DMASlaveReceiveCplt(DMA_HandleTypeDef *hdma);
226 static void I2C_DMAMemTransmitCplt(DMA_HandleTypeDef *hdma);
227 static void I2C_DMAMemReceiveCplt(DMA_HandleTypeDef *hdma);
228 static void I2C_DMAError(DMA_HandleTypeDef *hdma);
229
230 static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout);
231 static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout);
232 static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
233 static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout);
234 static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout);
235 static HAL_StatusTypeDef I2C_WaitOnTXISFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout);
236 static HAL_StatusTypeDef I2C_IsAcknowledgeFailed(I2C_HandleTypeDef *hi2c, uint32_t Timeout);
237
238 static HAL_StatusTypeDef I2C_MasterTransmit_ISR(I2C_HandleTypeDef *hi2c);
239 static HAL_StatusTypeDef I2C_MasterReceive_ISR(I2C_HandleTypeDef *hi2c);
240
241 static HAL_StatusTypeDef I2C_SlaveTransmit_ISR(I2C_HandleTypeDef *hi2c);
242 static HAL_StatusTypeDef I2C_SlaveReceive_ISR(I2C_HandleTypeDef *hi2c);
243
244 static void I2C_TransferConfig(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t Size, uint32_t Mode, uint32_t Request);
245 /**
246 * @}
247 */
248
249 /* Exported functions ---------------------------------------------------------*/
250
251 /** @defgroup I2C_Exported_Functions I2C Exported Functions
252 * @{
253 */
254
255 /** @defgroup I2C_Exported_Functions_Group1 Initialization and de-initialization functions
256 * @brief Initialization and Configuration functions
257 *
258 @verbatim
259 ===============================================================================
260 ##### Initialization and Configuration functions #####
261 ===============================================================================
262 [..] This subsection provides a set of functions allowing to initialize and
263 de-initialiaze the I2Cx peripheral:
264
265 (+) User must Implement HAL_I2C_MspInit() function in which he configures
266 all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
267
268 (+) Call the function HAL_I2C_Init() to configure the selected device with
269 the selected configuration:
270 (++) Clock Timing
271 (++) Own Address 1
272 (++) Addressing mode (Master, Slave)
273 (++) Dual Addressing mode
274 (++) Own Address 2
275 (++) Own Address 2 Mask
276 (++) General call mode
277 (++) Nostretch mode
278
279 (+) Call the function HAL_I2C_DeInit() to restore the default configuration
280 of the selected I2Cx periperal.
281
282 @endverbatim
283 * @{
284 */
285
286 /**
287 * @brief Initializes the I2C according to the specified parameters
288 * in the I2C_InitTypeDef and create the associated handle.
289 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
290 * the configuration information for the specified I2C.
291 * @retval HAL status
292 */
293 HAL_StatusTypeDef HAL_I2C_Init(I2C_HandleTypeDef *hi2c)
294 {
295 /* Check the I2C handle allocation */
296 if(hi2c == NULL)
297 {
298 return HAL_ERROR;
299 }
300
301 /* Check the parameters */
302 assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
303 assert_param(IS_I2C_OWN_ADDRESS1(hi2c->Init.OwnAddress1));
304 assert_param(IS_I2C_ADDRESSING_MODE(hi2c->Init.AddressingMode));
305 assert_param(IS_I2C_DUAL_ADDRESS(hi2c->Init.DualAddressMode));
306 assert_param(IS_I2C_OWN_ADDRESS2(hi2c->Init.OwnAddress2));
307 assert_param(IS_I2C_OWN_ADDRESS2_MASK(hi2c->Init.OwnAddress2Masks));
308 assert_param(IS_I2C_GENERAL_CALL(hi2c->Init.GeneralCallMode));
309 assert_param(IS_I2C_NO_STRETCH(hi2c->Init.NoStretchMode));
310
311 if(hi2c->State == HAL_I2C_STATE_RESET)
312 {
313 /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
314 HAL_I2C_MspInit(hi2c);
315 }
316
317 hi2c->State = HAL_I2C_STATE_BUSY;
318
319 /* Disable the selected I2C peripheral */
320 __HAL_I2C_DISABLE(hi2c);
321
322 /*---------------------------- I2Cx TIMINGR Configuration ------------------*/
323 /* Configure I2Cx: Frequency range */
324 hi2c->Instance->TIMINGR = hi2c->Init.Timing & TIMING_CLEAR_MASK;
325
326 /*---------------------------- I2Cx OAR1 Configuration ---------------------*/
327 /* Configure I2Cx: Own Address1 and ack own address1 mode */
328 hi2c->Instance->OAR1 &= ~I2C_OAR1_OA1EN;
329 if(hi2c->Init.OwnAddress1 != 0)
330 {
331 if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_7BIT)
332 {
333 hi2c->Instance->OAR1 = (I2C_OAR1_OA1EN | hi2c->Init.OwnAddress1);
334 }
335 else /* I2C_ADDRESSINGMODE_10BIT */
336 {
337 hi2c->Instance->OAR1 = (I2C_OAR1_OA1EN | I2C_OAR1_OA1MODE | hi2c->Init.OwnAddress1);
338 }
339 }
340
341 /*---------------------------- I2Cx CR2 Configuration ----------------------*/
342 /* Configure I2Cx: Addressing Master mode */
343 if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT)
344 {
345 hi2c->Instance->CR2 = (I2C_CR2_ADD10);
346 }
347 /* Enable the AUTOEND by default, and enable NACK (should be disable only during Slave process */
348 hi2c->Instance->CR2 |= (I2C_CR2_AUTOEND | I2C_CR2_NACK);
349
350 /*---------------------------- I2Cx OAR2 Configuration ---------------------*/
351 /* Configure I2Cx: Dual mode and Own Address2 */
352 hi2c->Instance->OAR2 = (hi2c->Init.DualAddressMode | hi2c->Init.OwnAddress2 | (hi2c->Init.OwnAddress2Masks << 8));
353
354 /*---------------------------- I2Cx CR1 Configuration ----------------------*/
355 /* Configure I2Cx: Generalcall and NoStretch mode */
356 hi2c->Instance->CR1 = (hi2c->Init.GeneralCallMode | hi2c->Init.NoStretchMode);
357
358 /* Enable the selected I2C peripheral */
359 __HAL_I2C_ENABLE(hi2c);
360
361 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
362 hi2c->State = HAL_I2C_STATE_READY;
363
364 return HAL_OK;
365 }
366
367 /**
368 * @brief DeInitializes the I2C peripheral.
369 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
370 * the configuration information for the specified I2C.
371 * @retval HAL status
372 */
373 HAL_StatusTypeDef HAL_I2C_DeInit(I2C_HandleTypeDef *hi2c)
374 {
375 /* Check the I2C handle allocation */
376 if(hi2c == NULL)
377 {
378 return HAL_ERROR;
379 }
380
381 /* Check the parameters */
382 assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
383
384 hi2c->State = HAL_I2C_STATE_BUSY;
385
386 /* Disable the I2C Peripheral Clock */
387 __HAL_I2C_DISABLE(hi2c);
388
389 /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
390 HAL_I2C_MspDeInit(hi2c);
391
392 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
393 hi2c->State = HAL_I2C_STATE_RESET;
394
395 /* Release Lock */
396 __HAL_UNLOCK(hi2c);
397
398 return HAL_OK;
399 }
400
401 /**
402 * @brief I2C MSP Init.
403 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
404 * the configuration information for the specified I2C.
405 * @retval None
406 */
407 __weak void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)
408 {
409 /* NOTE : This function Should not be modified, when the callback is needed,
410 the HAL_I2C_MspInit could be implemented in the user file
411 */
412 }
413
414 /**
415 * @brief I2C MSP DeInit
416 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
417 * the configuration information for the specified I2C.
418 * @retval None
419 */
420 __weak void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c)
421 {
422 /* NOTE : This function Should not be modified, when the callback is needed,
423 the HAL_I2C_MspDeInit could be implemented in the user file
424 */
425 }
426
427 /**
428 * @}
429 */
430
431 /** @defgroup I2C_Exported_Functions_Group2 Input and Output operation functions
432 * @brief Data transfers functions
433 *
434 @verbatim
435 ===============================================================================
436 ##### IO operation functions #####
437 ===============================================================================
438 [..]
439 This subsection provides a set of functions allowing to manage the I2C data
440 transfers.
441
442 (#) There is two mode of transfer:
443 (++) Blocking mode : The communication is performed in the polling mode.
444 The status of all data processing is returned by the same function
445 after finishing transfer.
446 (++) No-Blocking mode : The communication is performed using Interrupts
447 or DMA. These functions return the status of the transfer startup.
448 The end of the data processing will be indicated through the
449 dedicated I2C IRQ when using Interrupt mode or the DMA IRQ when
450 using DMA mode.
451
452 (#) Blocking mode functions are :
453 (++) HAL_I2C_Master_Transmit()
454 (++) HAL_I2C_Master_Receive()
455 (++) HAL_I2C_Slave_Transmit()
456 (++) HAL_I2C_Slave_Receive()
457 (++) HAL_I2C_Mem_Write()
458 (++) HAL_I2C_Mem_Read()
459 (++) HAL_I2C_IsDeviceReady()
460
461 (#) No-Blocking mode functions with Interrupt are :
462 (++) HAL_I2C_Master_Transmit_IT()
463 (++) HAL_I2C_Master_Receive_IT()
464 (++) HAL_I2C_Slave_Transmit_IT()
465 (++) HAL_I2C_Slave_Receive_IT()
466 (++) HAL_I2C_Mem_Write_IT()
467 (++) HAL_I2C_Mem_Read_IT()
468
469 (#) No-Blocking mode functions with DMA are :
470 (++) HAL_I2C_Master_Transmit_DMA()
471 (++) HAL_I2C_Master_Receive_DMA()
472 (++) HAL_I2C_Slave_Transmit_DMA()
473 (++) HAL_I2C_Slave_Receive_DMA()
474 (++) HAL_I2C_Mem_Write_DMA()
475 (++) HAL_I2C_Mem_Read_DMA()
476
477 (#) A set of Transfer Complete Callbacks are provided in No_Blocking mode:
478 (++) HAL_I2C_MemTxCpltCallback()
479 (++) HAL_I2C_MemRxCpltCallback()
480 (++) HAL_I2C_MasterTxCpltCallback()
481 (++) HAL_I2C_MasterRxCpltCallback()
482 (++) HAL_I2C_SlaveTxCpltCallback()
483 (++) HAL_I2C_SlaveRxCpltCallback()
484 (++) HAL_I2C_ErrorCallback()
485
486 @endverbatim
487 * @{
488 */
489
490 /**
491 * @brief Transmits in master mode an amount of data in blocking mode.
492 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
493 * the configuration information for the specified I2C.
494 * @param DevAddress: Target device address
495 * @param pData: Pointer to data buffer
496 * @param Size: Amount of data to be sent
497 * @param Timeout: Timeout duration
498 * @retval HAL status
499 */
500 HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
501 {
502 uint32_t sizetmp = 0;
503
504 if(hi2c->State == HAL_I2C_STATE_READY)
505 {
506 if((pData == NULL ) || (Size == 0))
507 {
508 return HAL_ERROR;
509 }
510
511 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
512 {
513 return HAL_BUSY;
514 }
515
516 /* Process Locked */
517 __HAL_LOCK(hi2c);
518
519 hi2c->State = HAL_I2C_STATE_MASTER_BUSY_TX;
520 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
521
522 /* Send Slave Address */
523 /* Set NBYTES to write and reload if size > 255 and generate RESTART */
524 /* Size > 255, need to set RELOAD bit */
525 if(Size > 255)
526 {
527 I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);
528 sizetmp = 255;
529 }
530 else
531 {
532 I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_GENERATE_START_WRITE);
533 sizetmp = Size;
534 }
535
536 do
537 {
538 /* Wait until TXIS flag is set */
539 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
540 {
541 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
542 {
543 return HAL_ERROR;
544 }
545 else
546 {
547 return HAL_TIMEOUT;
548 }
549 }
550 /* Write data to TXDR */
551 hi2c->Instance->TXDR = (*pData++);
552 sizetmp--;
553 Size--;
554
555 if((sizetmp == 0)&&(Size!=0))
556 {
557 /* Wait until TXE flag is set */
558 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout) != HAL_OK)
559 {
560 return HAL_TIMEOUT;
561 }
562
563 if(Size > 255)
564 {
565 I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
566 sizetmp = 255;
567 }
568 else
569 {
570 I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
571 sizetmp = Size;
572 }
573 }
574
575 }while(Size > 0);
576
577 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
578 /* Wait until STOPF flag is set */
579 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
580 {
581 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
582 {
583 return HAL_ERROR;
584 }
585 else
586 {
587 return HAL_TIMEOUT;
588 }
589 }
590
591 /* Clear STOP Flag */
592 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
593
594 /* Clear Configuration Register 2 */
595 __HAL_I2C_RESET_CR2(hi2c);
596
597 hi2c->State = HAL_I2C_STATE_READY;
598
599 /* Process Unlocked */
600 __HAL_UNLOCK(hi2c);
601
602 return HAL_OK;
603 }
604 else
605 {
606 return HAL_BUSY;
607 }
608 }
609
610 /**
611 * @brief Receives in master mode an amount of data in blocking mode.
612 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
613 * the configuration information for the specified I2C.
614 * @param DevAddress: Target device address
615 * @param pData: Pointer to data buffer
616 * @param Size: Amount of data to be sent
617 * @param Timeout: Timeout duration
618 * @retval HAL status
619 */
620 HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
621 {
622 uint32_t sizetmp = 0;
623
624 if(hi2c->State == HAL_I2C_STATE_READY)
625 {
626 if((pData == NULL ) || (Size == 0))
627 {
628 return HAL_ERROR;
629 }
630
631 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
632 {
633 return HAL_BUSY;
634 }
635
636 /* Process Locked */
637 __HAL_LOCK(hi2c);
638
639 hi2c->State = HAL_I2C_STATE_MASTER_BUSY_RX;
640 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
641
642 /* Send Slave Address */
643 /* Set NBYTES to write and reload if size > 255 and generate RESTART */
644 /* Size > 255, need to set RELOAD bit */
645 if(Size > 255)
646 {
647 I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
648 sizetmp = 255;
649 }
650 else
651 {
652 I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
653 sizetmp = Size;
654 }
655
656 do
657 {
658 /* Wait until RXNE flag is set */
659 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, Timeout) != HAL_OK)
660 {
661 return HAL_TIMEOUT;
662 }
663
664 /* Write data to RXDR */
665 (*pData++) =hi2c->Instance->RXDR;
666 sizetmp--;
667 Size--;
668
669 if((sizetmp == 0)&&(Size!=0))
670 {
671 /* Wait until TCR flag is set */
672 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout) != HAL_OK)
673 {
674 return HAL_TIMEOUT;
675 }
676
677 if(Size > 255)
678 {
679 I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
680 sizetmp = 255;
681 }
682 else
683 {
684 I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
685 sizetmp = Size;
686 }
687 }
688
689 }while(Size > 0);
690
691 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
692 /* Wait until STOPF flag is set */
693 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
694 {
695 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
696 {
697 return HAL_ERROR;
698 }
699 else
700 {
701 return HAL_TIMEOUT;
702 }
703 }
704
705 /* Clear STOP Flag */
706 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
707
708 /* Clear Configuration Register 2 */
709 __HAL_I2C_RESET_CR2(hi2c);
710
711 hi2c->State = HAL_I2C_STATE_READY;
712
713 /* Process Unlocked */
714 __HAL_UNLOCK(hi2c);
715
716 return HAL_OK;
717 }
718 else
719 {
720 return HAL_BUSY;
721 }
722 }
723
724 /**
725 * @brief Transmits in slave mode an amount of data in blocking mode.
726 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
727 * the configuration information for the specified I2C.
728 * @param pData: Pointer to data buffer
729 * @param Size: Amount of data to be sent
730 * @param Timeout: Timeout duration
731 * @retval HAL status
732 */
733 HAL_StatusTypeDef HAL_I2C_Slave_Transmit(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
734 {
735 if(hi2c->State == HAL_I2C_STATE_READY)
736 {
737 if((pData == NULL ) || (Size == 0))
738 {
739 return HAL_ERROR;
740 }
741
742 /* Process Locked */
743 __HAL_LOCK(hi2c);
744
745 hi2c->State = HAL_I2C_STATE_SLAVE_BUSY_RX;
746 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
747
748 /* Enable Address Acknowledge */
749 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
750
751 /* Wait until ADDR flag is set */
752 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout) != HAL_OK)
753 {
754 /* Disable Address Acknowledge */
755 hi2c->Instance->CR2 |= I2C_CR2_NACK;
756 return HAL_TIMEOUT;
757 }
758
759 /* Clear ADDR flag */
760 __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
761
762 /* If 10bit addressing mode is selected */
763 if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT)
764 {
765 /* Wait until ADDR flag is set */
766 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout) != HAL_OK)
767 {
768 /* Disable Address Acknowledge */
769 hi2c->Instance->CR2 |= I2C_CR2_NACK;
770 return HAL_TIMEOUT;
771 }
772
773 /* Clear ADDR flag */
774 __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
775 }
776
777 /* Wait until DIR flag is set Transmitter mode */
778 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_DIR, RESET, Timeout) != HAL_OK)
779 {
780 /* Disable Address Acknowledge */
781 hi2c->Instance->CR2 |= I2C_CR2_NACK;
782 return HAL_TIMEOUT;
783 }
784
785 do
786 {
787 /* Wait until TXIS flag is set */
788 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
789 {
790 /* Disable Address Acknowledge */
791 hi2c->Instance->CR2 |= I2C_CR2_NACK;
792
793 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
794 {
795 return HAL_ERROR;
796 }
797 else
798 {
799 return HAL_TIMEOUT;
800 }
801 }
802
803 /* Read data from TXDR */
804 hi2c->Instance->TXDR = (*pData++);
805 Size--;
806 }while(Size > 0);
807
808 /* Wait until STOP flag is set */
809 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
810 {
811 /* Disable Address Acknowledge */
812 hi2c->Instance->CR2 |= I2C_CR2_NACK;
813
814 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
815 {
816 /* Normal use case for Transmitter mode */
817 /* A NACK is generated to confirm the end of transfer */
818 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
819 }
820 else
821 {
822 return HAL_TIMEOUT;
823 }
824 }
825
826 /* Clear STOP flag */
827 __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_STOPF);
828
829 /* Wait until BUSY flag is reset */
830 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout) != HAL_OK)
831 {
832 /* Disable Address Acknowledge */
833 hi2c->Instance->CR2 |= I2C_CR2_NACK;
834 return HAL_TIMEOUT;
835 }
836
837 /* Disable Address Acknowledge */
838 hi2c->Instance->CR2 |= I2C_CR2_NACK;
839
840 hi2c->State = HAL_I2C_STATE_READY;
841
842 /* Process Unlocked */
843 __HAL_UNLOCK(hi2c);
844
845 return HAL_OK;
846 }
847 else
848 {
849 return HAL_BUSY;
850 }
851 }
852
853 /**
854 * @brief Receive in slave mode an amount of data in blocking mode
855 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
856 * the configuration information for the specified I2C.
857 * @param pData: Pointer to data buffer
858 * @param Size: Amount of data to be sent
859 * @param Timeout: Timeout duration
860 * @retval HAL status
861 */
862 HAL_StatusTypeDef HAL_I2C_Slave_Receive(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
863 {
864 if(hi2c->State == HAL_I2C_STATE_READY)
865 {
866 if((pData == NULL ) || (Size == 0))
867 {
868 return HAL_ERROR;
869 }
870
871 /* Process Locked */
872 __HAL_LOCK(hi2c);
873
874 hi2c->State = HAL_I2C_STATE_SLAVE_BUSY_RX;
875 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
876
877 /* Enable Address Acknowledge */
878 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
879
880 /* Wait until ADDR flag is set */
881 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout) != HAL_OK)
882 {
883 /* Disable Address Acknowledge */
884 hi2c->Instance->CR2 |= I2C_CR2_NACK;
885 return HAL_TIMEOUT;
886 }
887
888 /* Clear ADDR flag */
889 __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
890
891 /* Wait until DIR flag is reset Receiver mode */
892 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_DIR, SET, Timeout) != HAL_OK)
893 {
894 /* Disable Address Acknowledge */
895 hi2c->Instance->CR2 |= I2C_CR2_NACK;
896 return HAL_TIMEOUT;
897 }
898
899 while(Size > 0)
900 {
901 /* Wait until RXNE flag is set */
902 if(I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
903 {
904 /* Disable Address Acknowledge */
905 hi2c->Instance->CR2 |= I2C_CR2_NACK;
906 if(hi2c->ErrorCode == HAL_I2C_ERROR_TIMEOUT)
907 {
908 return HAL_TIMEOUT;
909 }
910 else
911 {
912 return HAL_ERROR;
913 }
914 }
915
916 /* Read data from RXDR */
917 (*pData++) = hi2c->Instance->RXDR;
918 Size--;
919 }
920
921 /* Wait until STOP flag is set */
922 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
923 {
924 /* Disable Address Acknowledge */
925 hi2c->Instance->CR2 |= I2C_CR2_NACK;
926
927 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
928 {
929 return HAL_ERROR;
930 }
931 else
932 {
933 return HAL_TIMEOUT;
934 }
935 }
936
937 /* Clear STOP flag */
938 __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_STOPF);
939
940 /* Wait until BUSY flag is reset */
941 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout) != HAL_OK)
942 {
943 /* Disable Address Acknowledge */
944 hi2c->Instance->CR2 |= I2C_CR2_NACK;
945 return HAL_TIMEOUT;
946 }
947
948
949 /* Disable Address Acknowledge */
950 hi2c->Instance->CR2 |= I2C_CR2_NACK;
951
952 hi2c->State = HAL_I2C_STATE_READY;
953
954 /* Process Unlocked */
955 __HAL_UNLOCK(hi2c);
956
957 return HAL_OK;
958 }
959 else
960 {
961 return HAL_BUSY;
962 }
963 }
964
965 /**
966 * @brief Transmit in master mode an amount of data in no-blocking mode with Interrupt
967 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
968 * the configuration information for the specified I2C.
969 * @param DevAddress: Target device address
970 * @param pData: Pointer to data buffer
971 * @param Size: Amount of data to be sent
972 * @retval HAL status
973 */
974 HAL_StatusTypeDef HAL_I2C_Master_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
975 {
976 if(hi2c->State == HAL_I2C_STATE_READY)
977 {
978 if((pData == NULL) || (Size == 0))
979 {
980 return HAL_ERROR;
981 }
982
983 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
984 {
985 return HAL_BUSY;
986 }
987
988 /* Process Locked */
989 __HAL_LOCK(hi2c);
990
991 hi2c->State = HAL_I2C_STATE_MASTER_BUSY_TX;
992 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
993
994 hi2c->pBuffPtr = pData;
995 hi2c->XferCount = Size;
996 if(Size > 255)
997 {
998 hi2c->XferSize = 255;
999 }
1000 else
1001 {
1002 hi2c->XferSize = Size;
1003 }
1004
1005 /* Send Slave Address */
1006 /* Set NBYTES to write and reload if size > 255 and generate RESTART */
1007 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
1008 {
1009 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);
1010 }
1011 else
1012 {
1013 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_WRITE);
1014 }
1015
1016 /* Process Unlocked */
1017 __HAL_UNLOCK(hi2c);
1018
1019 /* Note : The I2C interrupts must be enabled after unlocking current process
1020 to avoid the risk of I2C interrupt handle execution before current
1021 process unlock */
1022
1023
1024 /* Enable ERR, TC, STOP, NACK, TXI interrupt */
1025 /* possible to enable all of these */
1026 /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
1027 __HAL_I2C_ENABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_TXI );
1028
1029 return HAL_OK;
1030 }
1031 else
1032 {
1033 return HAL_BUSY;
1034 }
1035 }
1036
1037 /**
1038 * @brief Receive in master mode an amount of data in no-blocking mode with Interrupt
1039 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1040 * the configuration information for the specified I2C.
1041 * @param DevAddress: Target device address
1042 * @param pData: Pointer to data buffer
1043 * @param Size: Amount of data to be sent
1044 * @retval HAL status
1045 */
1046 HAL_StatusTypeDef HAL_I2C_Master_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
1047 {
1048 if(hi2c->State == HAL_I2C_STATE_READY)
1049 {
1050 if((pData == NULL) || (Size == 0))
1051 {
1052 return HAL_ERROR;
1053 }
1054
1055 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1056 {
1057 return HAL_BUSY;
1058 }
1059
1060 /* Process Locked */
1061 __HAL_LOCK(hi2c);
1062
1063 hi2c->State = HAL_I2C_STATE_MASTER_BUSY_RX;
1064 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1065
1066 hi2c->pBuffPtr = pData;
1067 hi2c->XferCount = Size;
1068 if(Size > 255)
1069 {
1070 hi2c->XferSize = 255;
1071 }
1072 else
1073 {
1074 hi2c->XferSize = Size;
1075 }
1076
1077 /* Send Slave Address */
1078 /* Set NBYTES to write and reload if size > 255 and generate RESTART */
1079 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
1080 {
1081 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
1082 }
1083 else
1084 {
1085 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
1086 }
1087
1088 /* Process Unlocked */
1089 __HAL_UNLOCK(hi2c);
1090
1091 /* Note : The I2C interrupts must be enabled after unlocking current process
1092 to avoid the risk of I2C interrupt handle execution before current
1093 process unlock */
1094
1095 /* Enable ERR, TC, STOP, NACK, RXI interrupt */
1096 /* possible to enable all of these */
1097 /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
1098 __HAL_I2C_ENABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_RXI );
1099
1100 return HAL_OK;
1101 }
1102 else
1103 {
1104 return HAL_BUSY;
1105 }
1106 }
1107
1108 /**
1109 * @brief Transmit in slave mode an amount of data in no-blocking mode with Interrupt
1110 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1111 * the configuration information for the specified I2C.
1112 * @param pData: Pointer to data buffer
1113 * @param Size: Amount of data to be sent
1114 * @retval HAL status
1115 */
1116 HAL_StatusTypeDef HAL_I2C_Slave_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
1117 {
1118 if(hi2c->State == HAL_I2C_STATE_READY)
1119 {
1120 if((pData == NULL) || (Size == 0))
1121 {
1122 return HAL_ERROR;
1123 }
1124
1125 /* Process Locked */
1126 __HAL_LOCK(hi2c);
1127
1128 hi2c->State = HAL_I2C_STATE_SLAVE_BUSY_TX;
1129 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1130
1131 /* Enable Address Acknowledge */
1132 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
1133
1134 hi2c->pBuffPtr = pData;
1135 hi2c->XferSize = Size;
1136 hi2c->XferCount = Size;
1137
1138 /* Process Unlocked */
1139 __HAL_UNLOCK(hi2c);
1140
1141 /* Note : The I2C interrupts must be enabled after unlocking current process
1142 to avoid the risk of I2C interrupt handle execution before current
1143 process unlock */
1144
1145 /* Enable ERR, TC, STOP, NACK, TXI interrupt */
1146 /* possible to enable all of these */
1147 /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
1148 __HAL_I2C_ENABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_TXI );
1149
1150 return HAL_OK;
1151 }
1152 else
1153 {
1154 return HAL_BUSY;
1155 }
1156 }
1157
1158 /**
1159 * @brief Receive in slave mode an amount of data in no-blocking mode with Interrupt
1160 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1161 * the configuration information for the specified I2C.
1162 * @param pData: Pointer to data buffer
1163 * @param Size: Amount of data to be sent
1164 * @retval HAL status
1165 */
1166 HAL_StatusTypeDef HAL_I2C_Slave_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
1167 {
1168 if(hi2c->State == HAL_I2C_STATE_READY)
1169 {
1170 if((pData == NULL) || (Size == 0))
1171 {
1172 return HAL_ERROR;
1173 }
1174
1175 /* Process Locked */
1176 __HAL_LOCK(hi2c);
1177
1178 hi2c->State = HAL_I2C_STATE_SLAVE_BUSY_RX;
1179 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1180
1181 /* Enable Address Acknowledge */
1182 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
1183
1184 hi2c->pBuffPtr = pData;
1185 hi2c->XferSize = Size;
1186 hi2c->XferCount = Size;
1187
1188 /* Process Unlocked */
1189 __HAL_UNLOCK(hi2c);
1190
1191 /* Note : The I2C interrupts must be enabled after unlocking current process
1192 to avoid the risk of I2C interrupt handle execution before current
1193 process unlock */
1194
1195 /* Enable ERR, TC, STOP, NACK, RXI interrupt */
1196 /* possible to enable all of these */
1197 /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
1198 __HAL_I2C_ENABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI);
1199
1200 return HAL_OK;
1201 }
1202 else
1203 {
1204 return HAL_BUSY;
1205 }
1206 }
1207
1208 /**
1209 * @brief Transmit in master mode an amount of data in no-blocking mode with DMA
1210 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1211 * the configuration information for the specified I2C.
1212 * @param DevAddress: Target device address
1213 * @param pData: Pointer to data buffer
1214 * @param Size: Amount of data to be sent
1215 * @retval HAL status
1216 */
1217 HAL_StatusTypeDef HAL_I2C_Master_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
1218 {
1219 if(hi2c->State == HAL_I2C_STATE_READY)
1220 {
1221 if((pData == NULL) || (Size == 0))
1222 {
1223 return HAL_ERROR;
1224 }
1225
1226 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1227 {
1228 return HAL_BUSY;
1229 }
1230
1231 /* Process Locked */
1232 __HAL_LOCK(hi2c);
1233
1234 hi2c->State = HAL_I2C_STATE_MASTER_BUSY_TX;
1235 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1236
1237 hi2c->pBuffPtr = pData;
1238 hi2c->XferCount = Size;
1239 if(Size > 255)
1240 {
1241 hi2c->XferSize = 255;
1242 }
1243 else
1244 {
1245 hi2c->XferSize = Size;
1246 }
1247
1248 /* Set the I2C DMA transfer complete callback */
1249 hi2c->hdmatx->XferCpltCallback = I2C_DMAMasterTransmitCplt;
1250
1251 /* Set the DMA error callback */
1252 hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
1253
1254 /* Enable the DMA channel */
1255 HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
1256
1257 /* Send Slave Address */
1258 /* Set NBYTES to write and reload if size > 255 and generate RESTART */
1259 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
1260 {
1261 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);
1262 }
1263 else
1264 {
1265 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_WRITE);
1266 }
1267
1268 /* Wait until TXIS flag is set */
1269 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, I2C_TIMEOUT_TXIS) != HAL_OK)
1270 {
1271 /* Disable Address Acknowledge */
1272 hi2c->Instance->CR2 |= I2C_CR2_NACK;
1273
1274 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1275 {
1276 return HAL_ERROR;
1277 }
1278 else
1279 {
1280 return HAL_TIMEOUT;
1281 }
1282 }
1283
1284
1285 /* Enable DMA Request */
1286 hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
1287
1288 /* Process Unlocked */
1289 __HAL_UNLOCK(hi2c);
1290
1291 return HAL_OK;
1292 }
1293 else
1294 {
1295 return HAL_BUSY;
1296 }
1297 }
1298
1299 /**
1300 * @brief Receive in master mode an amount of data in no-blocking mode with DMA
1301 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1302 * the configuration information for the specified I2C.
1303 * @param DevAddress: Target device address
1304 * @param pData: Pointer to data buffer
1305 * @param Size: Amount of data to be sent
1306 * @retval HAL status
1307 */
1308 HAL_StatusTypeDef HAL_I2C_Master_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
1309 {
1310 if(hi2c->State == HAL_I2C_STATE_READY)
1311 {
1312 if((pData == NULL) || (Size == 0))
1313 {
1314 return HAL_ERROR;
1315 }
1316
1317 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1318 {
1319 return HAL_BUSY;
1320 }
1321
1322 /* Process Locked */
1323 __HAL_LOCK(hi2c);
1324
1325 hi2c->State = HAL_I2C_STATE_MASTER_BUSY_RX;
1326 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1327
1328 hi2c->pBuffPtr = pData;
1329 hi2c->XferCount = Size;
1330 if(Size > 255)
1331 {
1332 hi2c->XferSize = 255;
1333 }
1334 else
1335 {
1336 hi2c->XferSize = Size;
1337 }
1338
1339 /* Set the I2C DMA transfer complete callback */
1340 hi2c->hdmarx->XferCpltCallback = I2C_DMAMasterReceiveCplt;
1341
1342 /* Set the DMA error callback */
1343 hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
1344
1345 /* Enable the DMA channel */
1346 HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData, hi2c->XferSize);
1347
1348 /* Send Slave Address */
1349 /* Set NBYTES to write and reload if size > 255 and generate RESTART */
1350 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
1351 {
1352 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
1353 }
1354 else
1355 {
1356 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
1357 }
1358
1359 /* Wait until RXNE flag is set */
1360 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, I2C_TIMEOUT_RXNE) != HAL_OK)
1361 {
1362 return HAL_TIMEOUT;
1363 }
1364
1365
1366 /* Enable DMA Request */
1367 hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
1368
1369 /* Process Unlocked */
1370 __HAL_UNLOCK(hi2c);
1371
1372 return HAL_OK;
1373 }
1374 else
1375 {
1376 return HAL_BUSY;
1377 }
1378 }
1379
1380 /**
1381 * @brief Transmit in slave mode an amount of data in no-blocking mode with DMA
1382 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1383 * the configuration information for the specified I2C.
1384 * @param pData: Pointer to data buffer
1385 * @param Size: Amount of data to be sent
1386 * @retval HAL status
1387 */
1388 HAL_StatusTypeDef HAL_I2C_Slave_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
1389 {
1390 if(hi2c->State == HAL_I2C_STATE_READY)
1391 {
1392 if((pData == NULL) || (Size == 0))
1393 {
1394 return HAL_ERROR;
1395 }
1396 /* Process Locked */
1397 __HAL_LOCK(hi2c);
1398
1399 hi2c->State = HAL_I2C_STATE_SLAVE_BUSY_TX;
1400 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1401
1402 hi2c->pBuffPtr = pData;
1403 hi2c->XferCount = Size;
1404 hi2c->XferSize = Size;
1405
1406 /* Set the I2C DMA transfer complete callback */
1407 hi2c->hdmatx->XferCpltCallback = I2C_DMASlaveTransmitCplt;
1408
1409 /* Set the DMA error callback */
1410 hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
1411
1412 /* Enable the DMA channel */
1413 HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
1414
1415 /* Enable Address Acknowledge */
1416 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
1417
1418 /* Wait until ADDR flag is set */
1419 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, I2C_TIMEOUT_ADDR) != HAL_OK)
1420 {
1421 /* Disable Address Acknowledge */
1422 hi2c->Instance->CR2 |= I2C_CR2_NACK;
1423 return HAL_TIMEOUT;
1424 }
1425
1426 /* Clear ADDR flag */
1427 __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
1428
1429 /* If 10bits addressing mode is selected */
1430 if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT)
1431 {
1432 /* Wait until ADDR flag is set */
1433 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, I2C_TIMEOUT_ADDR) != HAL_OK)
1434 {
1435 /* Disable Address Acknowledge */
1436 hi2c->Instance->CR2 |= I2C_CR2_NACK;
1437 return HAL_TIMEOUT;
1438 }
1439
1440 /* Clear ADDR flag */
1441 __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
1442 }
1443
1444 /* Wait until DIR flag is set Transmitter mode */
1445 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_DIR, RESET, I2C_TIMEOUT_BUSY) != HAL_OK)
1446 {
1447 /* Disable Address Acknowledge */
1448 hi2c->Instance->CR2 |= I2C_CR2_NACK;
1449 return HAL_TIMEOUT;
1450 }
1451
1452 /* Enable DMA Request */
1453 hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
1454
1455 /* Process Unlocked */
1456 __HAL_UNLOCK(hi2c);
1457
1458 return HAL_OK;
1459 }
1460 else
1461 {
1462 return HAL_BUSY;
1463 }
1464 }
1465
1466 /**
1467 * @brief Receive in slave mode an amount of data in no-blocking mode with DMA
1468 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1469 * the configuration information for the specified I2C.
1470 * @param pData: Pointer to data buffer
1471 * @param Size: Amount of data to be sent
1472 * @retval HAL status
1473 */
1474 HAL_StatusTypeDef HAL_I2C_Slave_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
1475 {
1476 if(hi2c->State == HAL_I2C_STATE_READY)
1477 {
1478 if((pData == NULL) || (Size == 0))
1479 {
1480 return HAL_ERROR;
1481 }
1482 /* Process Locked */
1483 __HAL_LOCK(hi2c);
1484
1485 hi2c->State = HAL_I2C_STATE_SLAVE_BUSY_RX;
1486 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1487
1488 hi2c->pBuffPtr = pData;
1489 hi2c->XferSize = Size;
1490 hi2c->XferCount = Size;
1491
1492 /* Set the I2C DMA transfer complete callback */
1493 hi2c->hdmarx->XferCpltCallback = I2C_DMASlaveReceiveCplt;
1494
1495 /* Set the DMA error callback */
1496 hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
1497
1498 /* Enable the DMA channel */
1499 HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData, Size);
1500
1501 /* Enable Address Acknowledge */
1502 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
1503
1504 /* Wait until ADDR flag is set */
1505 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, I2C_TIMEOUT_ADDR) != HAL_OK)
1506 {
1507 /* Disable Address Acknowledge */
1508 hi2c->Instance->CR2 |= I2C_CR2_NACK;
1509 return HAL_TIMEOUT;
1510 }
1511
1512 /* Clear ADDR flag */
1513 __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
1514
1515 /* Wait until DIR flag is set Receiver mode */
1516 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_DIR, SET, I2C_TIMEOUT_DIR) != HAL_OK)
1517 {
1518 /* Disable Address Acknowledge */
1519 hi2c->Instance->CR2 |= I2C_CR2_NACK;
1520 return HAL_TIMEOUT;
1521 }
1522
1523 /* Enable DMA Request */
1524 hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
1525
1526 /* Process Unlocked */
1527 __HAL_UNLOCK(hi2c);
1528
1529 return HAL_OK;
1530 }
1531 else
1532 {
1533 return HAL_BUSY;
1534 }
1535 }
1536
1537 /**
1538 * @brief Write an amount of data in blocking mode to a specific memory address
1539 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1540 * the configuration information for the specified I2C.
1541 * @param DevAddress: Target device address
1542 * @param MemAddress: Internal memory address
1543 * @param MemAddSize: Size of internal memory address
1544 * @param pData: Pointer to data buffer
1545 * @param Size: Amount of data to be sent
1546 * @param Timeout: Timeout duration
1547 * @retval HAL status
1548 */
1549 HAL_StatusTypeDef HAL_I2C_Mem_Write(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
1550 {
1551 uint32_t Sizetmp = 0;
1552
1553 /* Check the parameters */
1554 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
1555
1556 if(hi2c->State == HAL_I2C_STATE_READY)
1557 {
1558 if((pData == NULL) || (Size == 0))
1559 {
1560 return HAL_ERROR;
1561 }
1562
1563 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1564 {
1565 return HAL_BUSY;
1566 }
1567
1568 /* Process Locked */
1569 __HAL_LOCK(hi2c);
1570
1571 hi2c->State = HAL_I2C_STATE_MEM_BUSY_TX;
1572 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1573
1574 /* Send Slave Address and Memory Address */
1575 if(I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, Timeout) != HAL_OK)
1576 {
1577 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1578 {
1579 /* Process Unlocked */
1580 __HAL_UNLOCK(hi2c);
1581 return HAL_ERROR;
1582 }
1583 else
1584 {
1585 /* Process Unlocked */
1586 __HAL_UNLOCK(hi2c);
1587 return HAL_TIMEOUT;
1588 }
1589 }
1590
1591 /* Set NBYTES to write and reload if size > 255 */
1592 /* Size > 255, need to set RELOAD bit */
1593 if(Size > 255)
1594 {
1595 I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
1596 Sizetmp = 255;
1597 }
1598 else
1599 {
1600 I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
1601 Sizetmp = Size;
1602 }
1603
1604 do
1605 {
1606 /* Wait until TXIS flag is set */
1607 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
1608 {
1609 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1610 {
1611 return HAL_ERROR;
1612 }
1613 else
1614 {
1615 return HAL_TIMEOUT;
1616 }
1617 }
1618
1619 /* Write data to DR */
1620 hi2c->Instance->TXDR = (*pData++);
1621 Sizetmp--;
1622 Size--;
1623
1624 if((Sizetmp == 0)&&(Size!=0))
1625 {
1626 /* Wait until TCR flag is set */
1627 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout) != HAL_OK)
1628 {
1629 return HAL_TIMEOUT;
1630 }
1631
1632
1633 if(Size > 255)
1634 {
1635 I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
1636 Sizetmp = 255;
1637 }
1638 else
1639 {
1640 I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
1641 Sizetmp = Size;
1642 }
1643 }
1644
1645 }while(Size > 0);
1646
1647 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
1648 /* Wait until STOPF flag is reset */
1649 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
1650 {
1651 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1652 {
1653 return HAL_ERROR;
1654 }
1655 else
1656 {
1657 return HAL_TIMEOUT;
1658 }
1659 }
1660
1661 /* Clear STOP Flag */
1662 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
1663
1664 /* Clear Configuration Register 2 */
1665 __HAL_I2C_RESET_CR2(hi2c);
1666
1667 hi2c->State = HAL_I2C_STATE_READY;
1668
1669 /* Process Unlocked */
1670 __HAL_UNLOCK(hi2c);
1671
1672 return HAL_OK;
1673 }
1674 else
1675 {
1676 return HAL_BUSY;
1677 }
1678 }
1679
1680 /**
1681 * @brief Read an amount of data in blocking mode from a specific memory address
1682 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1683 * the configuration information for the specified I2C.
1684 * @param DevAddress: Target device address
1685 * @param MemAddress: Internal memory address
1686 * @param MemAddSize: Size of internal memory address
1687 * @param pData: Pointer to data buffer
1688 * @param Size: Amount of data to be sent
1689 * @param Timeout: Timeout duration
1690 * @retval HAL status
1691 */
1692 HAL_StatusTypeDef HAL_I2C_Mem_Read(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
1693 {
1694 uint32_t Sizetmp = 0;
1695
1696 /* Check the parameters */
1697 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
1698
1699 if(hi2c->State == HAL_I2C_STATE_READY)
1700 {
1701 if((pData == NULL) || (Size == 0))
1702 {
1703 return HAL_ERROR;
1704 }
1705
1706 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1707 {
1708 return HAL_BUSY;
1709 }
1710
1711 /* Process Locked */
1712 __HAL_LOCK(hi2c);
1713
1714 hi2c->State = HAL_I2C_STATE_MEM_BUSY_RX;
1715 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1716
1717 /* Send Slave Address and Memory Address */
1718 if(I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, Timeout) != HAL_OK)
1719 {
1720 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1721 {
1722 /* Process Unlocked */
1723 __HAL_UNLOCK(hi2c);
1724 return HAL_ERROR;
1725 }
1726 else
1727 {
1728 /* Process Unlocked */
1729 __HAL_UNLOCK(hi2c);
1730 return HAL_TIMEOUT;
1731 }
1732 }
1733
1734 /* Send Slave Address */
1735 /* Set NBYTES to write and reload if size > 255 and generate RESTART */
1736 /* Size > 255, need to set RELOAD bit */
1737 if(Size > 255)
1738 {
1739 I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
1740 Sizetmp = 255;
1741 }
1742 else
1743 {
1744 I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
1745 Sizetmp = Size;
1746 }
1747
1748 do
1749 {
1750 /* Wait until RXNE flag is set */
1751 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, Timeout) != HAL_OK)
1752 {
1753 return HAL_TIMEOUT;
1754 }
1755
1756 /* Read data from RXDR */
1757 (*pData++) = hi2c->Instance->RXDR;
1758
1759 /* Decrement the Size counter */
1760 Sizetmp--;
1761 Size--;
1762
1763 if((Sizetmp == 0)&&(Size!=0))
1764 {
1765 /* Wait until TCR flag is set */
1766 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout) != HAL_OK)
1767 {
1768 return HAL_TIMEOUT;
1769 }
1770
1771 if(Size > 255)
1772 {
1773 I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
1774 Sizetmp = 255;
1775 }
1776 else
1777 {
1778 I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
1779 Sizetmp = Size;
1780 }
1781 }
1782
1783 }while(Size > 0);
1784
1785 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
1786 /* Wait until STOPF flag is reset */
1787 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
1788 {
1789 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1790 {
1791 return HAL_ERROR;
1792 }
1793 else
1794 {
1795 return HAL_TIMEOUT;
1796 }
1797 }
1798
1799 /* Clear STOP Flag */
1800 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
1801
1802 /* Clear Configuration Register 2 */
1803 __HAL_I2C_RESET_CR2(hi2c);
1804
1805 hi2c->State = HAL_I2C_STATE_READY;
1806
1807 /* Process Unlocked */
1808 __HAL_UNLOCK(hi2c);
1809
1810 return HAL_OK;
1811 }
1812 else
1813 {
1814 return HAL_BUSY;
1815 }
1816 }
1817
1818 /**
1819 * @brief Write an amount of data in no-blocking mode with Interrupt to a specific memory address
1820 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1821 * the configuration information for the specified I2C.
1822 * @param DevAddress: Target device address
1823 * @param MemAddress: Internal memory address
1824 * @param MemAddSize: Size of internal memory address
1825 * @param pData: Pointer to data buffer
1826 * @param Size: Amount of data to be sent
1827 * @retval HAL status
1828 */
1829 HAL_StatusTypeDef HAL_I2C_Mem_Write_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
1830 {
1831 /* Check the parameters */
1832 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
1833
1834 if(hi2c->State == HAL_I2C_STATE_READY)
1835 {
1836 if((pData == NULL) || (Size == 0))
1837 {
1838 return HAL_ERROR;
1839 }
1840
1841 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1842 {
1843 return HAL_BUSY;
1844 }
1845
1846 /* Process Locked */
1847 __HAL_LOCK(hi2c);
1848
1849 hi2c->State = HAL_I2C_STATE_MEM_BUSY_TX;
1850 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
1851
1852 hi2c->pBuffPtr = pData;
1853 hi2c->XferCount = Size;
1854 if(Size > 255)
1855 {
1856 hi2c->XferSize = 255;
1857 }
1858 else
1859 {
1860 hi2c->XferSize = Size;
1861 }
1862
1863 /* Send Slave Address and Memory Address */
1864 if(I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG) != HAL_OK)
1865 {
1866 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1867 {
1868 /* Process Unlocked */
1869 __HAL_UNLOCK(hi2c);
1870 return HAL_ERROR;
1871 }
1872 else
1873 {
1874 /* Process Unlocked */
1875 __HAL_UNLOCK(hi2c);
1876 return HAL_TIMEOUT;
1877 }
1878 }
1879
1880 /* Set NBYTES to write and reload if size > 255 */
1881 /* Size > 255, need to set RELOAD bit */
1882 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
1883 {
1884 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
1885 }
1886 else
1887 {
1888 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
1889 }
1890
1891 /* Process Unlocked */
1892 __HAL_UNLOCK(hi2c);
1893
1894 /* Note : The I2C interrupts must be enabled after unlocking current process
1895 to avoid the risk of I2C interrupt handle execution before current
1896 process unlock */
1897
1898 /* Enable ERR, TC, STOP, NACK, TXI interrupt */
1899 /* possible to enable all of these */
1900 /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
1901 __HAL_I2C_ENABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_TXI );
1902
1903 return HAL_OK;
1904 }
1905 else
1906 {
1907 return HAL_BUSY;
1908 }
1909 }
1910
1911 /**
1912 * @brief Read an amount of data in no-blocking mode with Interrupt from a specific memory address
1913 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
1914 * the configuration information for the specified I2C.
1915 * @param DevAddress: Target device address
1916 * @param MemAddress: Internal memory address
1917 * @param MemAddSize: Size of internal memory address
1918 * @param pData: Pointer to data buffer
1919 * @param Size: Amount of data to be sent
1920 * @retval HAL status
1921 */
1922 HAL_StatusTypeDef HAL_I2C_Mem_Read_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
1923 {
1924 /* Check the parameters */
1925 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
1926
1927 if(hi2c->State == HAL_I2C_STATE_READY)
1928 {
1929 if((pData == NULL) || (Size == 0))
1930 {
1931 return HAL_ERROR;
1932 }
1933
1934 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1935 {
1936 return HAL_BUSY;
1937 }
1938
1939 /* Process Locked */
1940 __HAL_LOCK(hi2c);
1941
1942 hi2c->State = HAL_I2C_STATE_MEM_BUSY_RX;
1943
1944 hi2c->pBuffPtr = pData;
1945 hi2c->XferCount = Size;
1946 if(Size > 255)
1947 {
1948 hi2c->XferSize = 255;
1949 }
1950 else
1951 {
1952 hi2c->XferSize = Size;
1953 }
1954
1955 /* Send Slave Address and Memory Address */
1956 if(I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG) != HAL_OK)
1957 {
1958 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1959 {
1960 /* Process Unlocked */
1961 __HAL_UNLOCK(hi2c);
1962 return HAL_ERROR;
1963 }
1964 else
1965 {
1966 /* Process Unlocked */
1967 __HAL_UNLOCK(hi2c);
1968 return HAL_TIMEOUT;
1969 }
1970 }
1971
1972 /* Set NBYTES to write and reload if size > 255 and generate RESTART */
1973 /* Size > 255, need to set RELOAD bit */
1974 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
1975 {
1976 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
1977 }
1978 else
1979 {
1980 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
1981 }
1982
1983 /* Process Unlocked */
1984 __HAL_UNLOCK(hi2c);
1985
1986 /* Note : The I2C interrupts must be enabled after unlocking current process
1987 to avoid the risk of I2C interrupt handle execution before current
1988 process unlock */
1989
1990 /* Enable ERR, TC, STOP, NACK, RXI interrupt */
1991 /* possible to enable all of these */
1992 /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
1993 __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_RXI );
1994
1995 return HAL_OK;
1996 }
1997 else
1998 {
1999 return HAL_BUSY;
2000 }
2001 }
2002
2003 /**
2004 * @brief Write an amount of data in no-blocking mode with DMA to a specific memory address
2005 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2006 * the configuration information for the specified I2C.
2007 * @param DevAddress: Target device address
2008 * @param MemAddress: Internal memory address
2009 * @param MemAddSize: Size of internal memory address
2010 * @param pData: Pointer to data buffer
2011 * @param Size: Amount of data to be sent
2012 * @retval HAL status
2013 */
2014 HAL_StatusTypeDef HAL_I2C_Mem_Write_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
2015 {
2016 /* Check the parameters */
2017 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
2018
2019 if(hi2c->State == HAL_I2C_STATE_READY)
2020 {
2021 if((pData == NULL) || (Size == 0))
2022 {
2023 return HAL_ERROR;
2024 }
2025
2026 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
2027 {
2028 return HAL_BUSY;
2029 }
2030
2031 /* Process Locked */
2032 __HAL_LOCK(hi2c);
2033
2034 hi2c->State = HAL_I2C_STATE_MEM_BUSY_TX;
2035 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
2036
2037 hi2c->pBuffPtr = pData;
2038 hi2c->XferCount = Size;
2039 if(Size > 255)
2040 {
2041 hi2c->XferSize = 255;
2042 }
2043 else
2044 {
2045 hi2c->XferSize = Size;
2046 }
2047
2048 /* Set the I2C DMA transfer complete callback */
2049 hi2c->hdmatx->XferCpltCallback = I2C_DMAMemTransmitCplt;
2050
2051 /* Set the DMA error callback */
2052 hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
2053
2054 /* Enable the DMA channel */
2055 HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
2056
2057 /* Send Slave Address and Memory Address */
2058 if(I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG) != HAL_OK)
2059 {
2060 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
2061 {
2062 /* Process Unlocked */
2063 __HAL_UNLOCK(hi2c);
2064 return HAL_ERROR;
2065 }
2066 else
2067 {
2068 /* Process Unlocked */
2069 __HAL_UNLOCK(hi2c);
2070 return HAL_TIMEOUT;
2071 }
2072 }
2073
2074 /* Send Slave Address */
2075 /* Set NBYTES to write and reload if size > 255 */
2076 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
2077 {
2078 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
2079 }
2080 else
2081 {
2082 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
2083 }
2084
2085 /* Wait until TXIS flag is set */
2086 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, I2C_TIMEOUT_TXIS) != HAL_OK)
2087 {
2088 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
2089 {
2090 return HAL_ERROR;
2091 }
2092 else
2093 {
2094 return HAL_TIMEOUT;
2095 }
2096 }
2097
2098 /* Enable DMA Request */
2099 hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
2100
2101 /* Process Unlocked */
2102 __HAL_UNLOCK(hi2c);
2103
2104 return HAL_OK;
2105 }
2106 else
2107 {
2108 return HAL_BUSY;
2109 }
2110 }
2111
2112 /**
2113 * @brief Reads an amount of data in no-blocking mode with DMA from a specific memory address.
2114 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2115 * the configuration information for the specified I2C.
2116 * @param DevAddress: Target device address
2117 * @param MemAddress: Internal memory address
2118 * @param MemAddSize: Size of internal memory address
2119 * @param pData: Pointer to data buffer
2120 * @param Size: Amount of data to be read
2121 * @retval HAL status
2122 */
2123 HAL_StatusTypeDef HAL_I2C_Mem_Read_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
2124 {
2125 /* Check the parameters */
2126 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
2127
2128 if(hi2c->State == HAL_I2C_STATE_READY)
2129 {
2130 if((pData == NULL) || (Size == 0))
2131 {
2132 return HAL_ERROR;
2133 }
2134
2135 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
2136 {
2137 return HAL_BUSY;
2138 }
2139
2140 /* Process Locked */
2141 __HAL_LOCK(hi2c);
2142
2143 hi2c->State = HAL_I2C_STATE_MEM_BUSY_RX;
2144
2145 hi2c->pBuffPtr = pData;
2146 hi2c->XferCount = Size;
2147 if(Size > 255)
2148 {
2149 hi2c->XferSize = 255;
2150 }
2151 else
2152 {
2153 hi2c->XferSize = Size;
2154 }
2155
2156 /* Set the I2C DMA transfer complete callback */
2157 hi2c->hdmarx->XferCpltCallback = I2C_DMAMemReceiveCplt;
2158
2159 /* Set the DMA error callback */
2160 hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
2161
2162 /* Enable the DMA channel */
2163 HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData, hi2c->XferSize);
2164
2165 /* Send Slave Address and Memory Address */
2166 if(I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG) != HAL_OK)
2167 {
2168 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
2169 {
2170 /* Process Unlocked */
2171 __HAL_UNLOCK(hi2c);
2172 return HAL_ERROR;
2173 }
2174 else
2175 {
2176 /* Process Unlocked */
2177 __HAL_UNLOCK(hi2c);
2178 return HAL_TIMEOUT;
2179 }
2180 }
2181
2182 /* Set NBYTES to write and reload if size > 255 and generate RESTART */
2183 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
2184 {
2185 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
2186 }
2187 else
2188 {
2189 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
2190 }
2191
2192 /* Wait until RXNE flag is set */
2193 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, I2C_TIMEOUT_RXNE) != HAL_OK)
2194 {
2195 return HAL_TIMEOUT;
2196 }
2197
2198 /* Enable DMA Request */
2199 hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
2200
2201 /* Process Unlocked */
2202 __HAL_UNLOCK(hi2c);
2203
2204 return HAL_OK;
2205 }
2206 else
2207 {
2208 return HAL_BUSY;
2209 }
2210 }
2211
2212 /**
2213 * @brief Checks if target device is ready for communication.
2214 * @note This function is used with Memory devices
2215 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2216 * the configuration information for the specified I2C.
2217 * @param DevAddress: Target device address
2218 * @param Trials: Number of trials
2219 * @param Timeout: Timeout duration
2220 * @retval HAL status
2221 */
2222 HAL_StatusTypeDef HAL_I2C_IsDeviceReady(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout)
2223 {
2224 uint32_t tickstart = 0;
2225
2226 __IO uint32_t I2C_Trials = 0;
2227
2228 if(hi2c->State == HAL_I2C_STATE_READY)
2229 {
2230 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
2231 {
2232 return HAL_BUSY;
2233 }
2234
2235 /* Process Locked */
2236 __HAL_LOCK(hi2c);
2237
2238 hi2c->State = HAL_I2C_STATE_BUSY;
2239 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
2240
2241 do
2242 {
2243 /* Generate Start */
2244 hi2c->Instance->CR2 = __HAL_I2C_GENERATE_START(hi2c->Init.AddressingMode,DevAddress);
2245
2246 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
2247 /* Wait until STOPF flag is set or a NACK flag is set*/
2248 tickstart = HAL_GetTick();
2249 while((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET) && (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == RESET) && (hi2c->State != HAL_I2C_STATE_TIMEOUT))
2250 {
2251 if(Timeout != HAL_MAX_DELAY)
2252 {
2253 if((Timeout == 0) || ((HAL_GetTick() - tickstart) > Timeout))
2254 {
2255 /* Device is ready */
2256 hi2c->State = HAL_I2C_STATE_READY;
2257 /* Process Unlocked */
2258 __HAL_UNLOCK(hi2c);
2259 return HAL_TIMEOUT;
2260 }
2261 }
2262 }
2263
2264 /* Check if the NACKF flag has not been set */
2265 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == RESET)
2266 {
2267 /* Wait until STOPF flag is reset */
2268 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout) != HAL_OK)
2269 {
2270 return HAL_TIMEOUT;
2271 }
2272
2273 /* Clear STOP Flag */
2274 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
2275
2276 /* Device is ready */
2277 hi2c->State = HAL_I2C_STATE_READY;
2278
2279 /* Process Unlocked */
2280 __HAL_UNLOCK(hi2c);
2281
2282 return HAL_OK;
2283 }
2284 else
2285 {
2286 /* Wait until STOPF flag is reset */
2287 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout) != HAL_OK)
2288 {
2289 return HAL_TIMEOUT;
2290 }
2291
2292 /* Clear NACK Flag */
2293 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
2294
2295 /* Clear STOP Flag, auto generated with autoend*/
2296 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
2297 }
2298
2299 /* Check if the maximum allowed number of trials has been reached */
2300 if (I2C_Trials++ == Trials)
2301 {
2302 /* Generate Stop */
2303 hi2c->Instance->CR2 |= I2C_CR2_STOP;
2304
2305 /* Wait until STOPF flag is reset */
2306 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout) != HAL_OK)
2307 {
2308 return HAL_TIMEOUT;
2309 }
2310
2311 /* Clear STOP Flag */
2312 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
2313 }
2314 }while(I2C_Trials < Trials);
2315
2316 hi2c->State = HAL_I2C_STATE_READY;
2317
2318 /* Process Unlocked */
2319 __HAL_UNLOCK(hi2c);
2320
2321 return HAL_TIMEOUT;
2322 }
2323 else
2324 {
2325 return HAL_BUSY;
2326 }
2327 }
2328
2329 /**
2330 * @brief This function handles I2C event interrupt request.
2331 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2332 * the configuration information for the specified I2C.
2333 * @retval None
2334 */
2335 void HAL_I2C_EV_IRQHandler(I2C_HandleTypeDef *hi2c)
2336 {
2337 /* I2C in mode Transmitter ---------------------------------------------------*/
2338 if (((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TCR) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TC) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == SET)) && (__HAL_I2C_GET_IT_SOURCE(hi2c, (I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_TXI | I2C_IT_ADDRI)) == SET))
2339 {
2340 /* Slave mode selected */
2341 if (hi2c->State == HAL_I2C_STATE_SLAVE_BUSY_TX)
2342 {
2343 I2C_SlaveTransmit_ISR(hi2c);
2344 }
2345 }
2346
2347 if (((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TCR) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TC) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)) && (__HAL_I2C_GET_IT_SOURCE(hi2c, (I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_TXI)) == SET))
2348 {
2349 /* Master mode selected */
2350 if ((hi2c->State == HAL_I2C_STATE_MASTER_BUSY_TX) || (hi2c->State == HAL_I2C_STATE_MEM_BUSY_TX))
2351 {
2352 I2C_MasterTransmit_ISR(hi2c);
2353 }
2354 }
2355
2356 /* I2C in mode Receiver ----------------------------------------------------*/
2357 if (((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TCR) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TC) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == SET)) && (__HAL_I2C_GET_IT_SOURCE(hi2c, (I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_RXI | I2C_IT_ADDRI)) == SET))
2358 {
2359 /* Slave mode selected */
2360 if (hi2c->State == HAL_I2C_STATE_SLAVE_BUSY_RX)
2361 {
2362 I2C_SlaveReceive_ISR(hi2c);
2363 }
2364 }
2365 if (((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TCR) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TC) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)) && (__HAL_I2C_GET_IT_SOURCE(hi2c, (I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_RXI)) == SET))
2366 {
2367 /* Master mode selected */
2368 if ((hi2c->State == HAL_I2C_STATE_MASTER_BUSY_RX) || (hi2c->State == HAL_I2C_STATE_MEM_BUSY_RX))
2369 {
2370 I2C_MasterReceive_ISR(hi2c);
2371 }
2372 }
2373 }
2374
2375 /**
2376 * @brief This function handles I2C error interrupt request.
2377 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2378 * the configuration information for the specified I2C.
2379 * @retval None
2380 */
2381 void HAL_I2C_ER_IRQHandler(I2C_HandleTypeDef *hi2c)
2382 {
2383 /* I2C Bus error interrupt occurred ------------------------------------*/
2384 if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BERR) == SET) && (__HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_ERRI) == SET))
2385 {
2386 hi2c->ErrorCode |= HAL_I2C_ERROR_BERR;
2387
2388 /* Clear BERR flag */
2389 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_BERR);
2390 }
2391
2392 /* I2C Over-Run/Under-Run interrupt occurred ----------------------------------------*/
2393 if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_OVR) == SET) && (__HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_ERRI) == SET))
2394 {
2395 hi2c->ErrorCode |= HAL_I2C_ERROR_OVR;
2396
2397 /* Clear OVR flag */
2398 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_OVR);
2399 }
2400
2401 /* I2C Arbitration Loss error interrupt occurred -------------------------------------*/
2402 if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ARLO) == SET) && (__HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_ERRI) == SET))
2403 {
2404 hi2c->ErrorCode |= HAL_I2C_ERROR_ARLO;
2405
2406 /* Clear ARLO flag */
2407 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ARLO);
2408 }
2409
2410 /* Call the Error Callback in case of Error detected */
2411 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
2412 {
2413 hi2c->State = HAL_I2C_STATE_READY;
2414
2415 HAL_I2C_ErrorCallback(hi2c);
2416 }
2417 }
2418
2419 /**
2420 * @brief Master Tx Transfer completed callbacks.
2421 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2422 * the configuration information for the specified I2C.
2423 * @retval None
2424 */
2425 __weak void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c)
2426 {
2427 /* NOTE : This function Should not be modified, when the callback is needed,
2428 the HAL_I2C_TxCpltCallback could be implemented in the user file
2429 */
2430 }
2431
2432 /**
2433 * @brief Master Rx Transfer completed callbacks.
2434 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2435 * the configuration information for the specified I2C.
2436 * @retval None
2437 */
2438 __weak void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c)
2439 {
2440 /* NOTE : This function Should not be modified, when the callback is needed,
2441 the HAL_I2C_TxCpltCallback could be implemented in the user file
2442 */
2443 }
2444
2445 /** @brief Slave Tx Transfer completed callbacks.
2446 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2447 * the configuration information for the specified I2C.
2448 * @retval None
2449 */
2450 __weak void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c)
2451 {
2452 /* NOTE : This function Should not be modified, when the callback is needed,
2453 the HAL_I2C_TxCpltCallback could be implemented in the user file
2454 */
2455 }
2456
2457 /**
2458 * @brief Slave Rx Transfer completed callbacks.
2459 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2460 * the configuration information for the specified I2C.
2461 * @retval None
2462 */
2463 __weak void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c)
2464 {
2465 /* NOTE : This function Should not be modified, when the callback is needed,
2466 the HAL_I2C_TxCpltCallback could be implemented in the user file
2467 */
2468 }
2469
2470 /**
2471 * @brief Memory Tx Transfer completed callbacks.
2472 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2473 * the configuration information for the specified I2C.
2474 * @retval None
2475 */
2476 __weak void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef *hi2c)
2477 {
2478 /* NOTE : This function Should not be modified, when the callback is needed,
2479 the HAL_I2C_TxCpltCallback could be implemented in the user file
2480 */
2481 }
2482
2483 /**
2484 * @brief Memory Rx Transfer completed callbacks.
2485 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2486 * the configuration information for the specified I2C.
2487 * @retval None
2488 */
2489 __weak void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c)
2490 {
2491 /* NOTE : This function Should not be modified, when the callback is needed,
2492 the HAL_I2C_TxCpltCallback could be implemented in the user file
2493 */
2494 }
2495
2496 /**
2497 * @brief I2C error callbacks.
2498 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2499 * the configuration information for the specified I2C.
2500 * @retval None
2501 */
2502 __weak void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
2503 {
2504 /* NOTE : This function Should not be modified, when the callback is needed,
2505 the HAL_I2C_ErrorCallback could be implemented in the user file
2506 */
2507 }
2508
2509 /**
2510 * @}
2511 */
2512
2513 /** @defgroup I2C_Exported_Functions_Group3 Peripheral State and Errors functions
2514 * @brief Peripheral State and Errors functions
2515 *
2516 @verbatim
2517 ===============================================================================
2518 ##### Peripheral State and Errors functions #####
2519 ===============================================================================
2520 [..]
2521 This subsection permit to get in run-time the status of the peripheral
2522 and the data flow.
2523
2524 @endverbatim
2525 * @{
2526 */
2527
2528 /**
2529 * @brief Returns the I2C state.
2530 * @param hi2c : I2C handle
2531 * @retval HAL state
2532 */
2533 HAL_I2C_StateTypeDef HAL_I2C_GetState(I2C_HandleTypeDef *hi2c)
2534 {
2535 return hi2c->State;
2536 }
2537
2538 /**
2539 * @brief Return the I2C error code
2540 * @param hi2c : pointer to a I2C_HandleTypeDef structure that contains
2541 * the configuration information for the specified I2C.
2542 * @retval I2C Error Code
2543 */
2544 uint32_t HAL_I2C_GetError(I2C_HandleTypeDef *hi2c)
2545 {
2546 return hi2c->ErrorCode;
2547 }
2548
2549 /**
2550 * @}
2551 */
2552
2553 /**
2554 * @}
2555 */
2556
2557 /** @addtogroup I2C_Private_Functions
2558 * @{
2559 */
2560
2561 /**
2562 * @brief Handle Interrupt Flags Master Transmit Mode
2563 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2564 * the configuration information for the specified I2C.
2565 * @retval HAL status
2566 */
2567 static HAL_StatusTypeDef I2C_MasterTransmit_ISR(I2C_HandleTypeDef *hi2c)
2568 {
2569 uint16_t DevAddress;
2570
2571 /* Process Locked */
2572 __HAL_LOCK(hi2c);
2573
2574 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == SET)
2575 {
2576 /* Write data to TXDR */
2577 hi2c->Instance->TXDR = (*hi2c->pBuffPtr++);
2578 hi2c->XferSize--;
2579 hi2c->XferCount--;
2580 }
2581 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TCR) == SET)
2582 {
2583 if((hi2c->XferSize == 0)&&(hi2c->XferCount!=0))
2584 {
2585 DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);
2586
2587 if(hi2c->XferCount > 255)
2588 {
2589 I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
2590 hi2c->XferSize = 255;
2591 }
2592 else
2593 {
2594 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferCount, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
2595 hi2c->XferSize = hi2c->XferCount;
2596 }
2597 }
2598 else
2599 {
2600 /* Process Unlocked */
2601 __HAL_UNLOCK(hi2c);
2602
2603 /* Wrong size Status regarding TCR flag event */
2604 hi2c->ErrorCode |= HAL_I2C_ERROR_SIZE;
2605 HAL_I2C_ErrorCallback(hi2c);
2606 }
2607 }
2608 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TC) == SET)
2609 {
2610 if(hi2c->XferCount == 0)
2611 {
2612 /* Generate Stop */
2613 hi2c->Instance->CR2 |= I2C_CR2_STOP;
2614 }
2615 else
2616 {
2617 /* Process Unlocked */
2618 __HAL_UNLOCK(hi2c);
2619
2620 /* Wrong size Status regarding TCR flag event */
2621 hi2c->ErrorCode |= HAL_I2C_ERROR_SIZE;
2622 HAL_I2C_ErrorCallback(hi2c);
2623 }
2624 }
2625 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
2626 {
2627 /* Disable ERR, TC, STOP, NACK, TXI interrupt */
2628 __HAL_I2C_DISABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_TXI );
2629
2630 /* Clear STOP Flag */
2631 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
2632
2633 /* Clear Configuration Register 2 */
2634 __HAL_I2C_RESET_CR2(hi2c);
2635
2636 hi2c->State = HAL_I2C_STATE_READY;
2637
2638 /* Process Unlocked */
2639 __HAL_UNLOCK(hi2c);
2640
2641 if(hi2c->State == HAL_I2C_STATE_MEM_BUSY_TX)
2642 {
2643 HAL_I2C_MemTxCpltCallback(hi2c);
2644 }
2645 else
2646 {
2647 HAL_I2C_MasterTxCpltCallback(hi2c);
2648 }
2649 }
2650 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
2651 {
2652 /* Clear NACK Flag */
2653 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
2654
2655 /* Process Unlocked */
2656 __HAL_UNLOCK(hi2c);
2657
2658 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
2659 HAL_I2C_ErrorCallback(hi2c);
2660 }
2661
2662 /* Process Unlocked */
2663 __HAL_UNLOCK(hi2c);
2664
2665 return HAL_OK;
2666 }
2667
2668 /**
2669 * @brief Handle Interrupt Flags Master Receive Mode
2670 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2671 * the configuration information for the specified I2C.
2672 * @retval HAL status
2673 */
2674 static HAL_StatusTypeDef I2C_MasterReceive_ISR(I2C_HandleTypeDef *hi2c)
2675 {
2676 uint16_t DevAddress;
2677
2678 /* Process Locked */
2679 __HAL_LOCK(hi2c);
2680
2681 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
2682 {
2683 /* Read data from RXDR */
2684 (*hi2c->pBuffPtr++) = hi2c->Instance->RXDR;
2685 hi2c->XferSize--;
2686 hi2c->XferCount--;
2687 }
2688 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TCR) == SET)
2689 {
2690 if((hi2c->XferSize == 0)&&(hi2c->XferCount!=0))
2691 {
2692 DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);
2693
2694 if(hi2c->XferCount > 255)
2695 {
2696 I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
2697 hi2c->XferSize = 255;
2698 }
2699 else
2700 {
2701 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferCount, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
2702 hi2c->XferSize = hi2c->XferCount;
2703 }
2704 }
2705 else
2706 {
2707 /* Process Unlocked */
2708 __HAL_UNLOCK(hi2c);
2709
2710 /* Wrong size Status regarding TCR flag event */
2711 hi2c->ErrorCode |= HAL_I2C_ERROR_SIZE;
2712 HAL_I2C_ErrorCallback(hi2c);
2713 }
2714 }
2715 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TC) == SET)
2716 {
2717 if(hi2c->XferCount == 0)
2718 {
2719 /* Generate Stop */
2720 hi2c->Instance->CR2 |= I2C_CR2_STOP;
2721 }
2722 else
2723 {
2724 /* Process Unlocked */
2725 __HAL_UNLOCK(hi2c);
2726
2727 /* Wrong size Status regarding TCR flag event */
2728 hi2c->ErrorCode |= HAL_I2C_ERROR_SIZE;
2729 HAL_I2C_ErrorCallback(hi2c);
2730 }
2731 }
2732 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
2733 {
2734 /* Disable ERR, TC, STOP, NACK, TXI interrupt */
2735 __HAL_I2C_DISABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_RXI );
2736
2737 /* Clear STOP Flag */
2738 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
2739
2740 /* Clear Configuration Register 2 */
2741 __HAL_I2C_RESET_CR2(hi2c);
2742
2743 hi2c->State = HAL_I2C_STATE_READY;
2744
2745 /* Process Unlocked */
2746 __HAL_UNLOCK(hi2c);
2747
2748 if(hi2c->State == HAL_I2C_STATE_MEM_BUSY_RX)
2749 {
2750 HAL_I2C_MemRxCpltCallback(hi2c);
2751 }
2752 else
2753 {
2754 HAL_I2C_MasterRxCpltCallback(hi2c);
2755 }
2756 }
2757 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
2758 {
2759 /* Clear NACK Flag */
2760 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
2761
2762 /* Process Unlocked */
2763 __HAL_UNLOCK(hi2c);
2764
2765 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
2766 HAL_I2C_ErrorCallback(hi2c);
2767 }
2768
2769 /* Process Unlocked */
2770 __HAL_UNLOCK(hi2c);
2771
2772 return HAL_OK;
2773
2774 }
2775
2776 /**
2777 * @brief Handle Interrupt Flags Slave Transmit Mode
2778 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2779 * the configuration information for the specified I2C.
2780 * @retval HAL status
2781 */
2782 static HAL_StatusTypeDef I2C_SlaveTransmit_ISR(I2C_HandleTypeDef *hi2c)
2783 {
2784 /* Process locked */
2785 __HAL_LOCK(hi2c);
2786
2787 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) != RESET)
2788 {
2789 /* Check that I2C transfer finished */
2790 /* if yes, normal usecase, a NACK is sent by the MASTER when Transfer is finished */
2791 /* Mean XferCount == 0*/
2792 /* So clear Flag NACKF only */
2793 if(hi2c->XferCount == 0)
2794 {
2795 /* Clear NACK Flag */
2796 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
2797
2798 /* Process Unlocked */
2799 __HAL_UNLOCK(hi2c);
2800 }
2801 else
2802 {
2803 /* if no, error usecase, a Non-Acknowledge of last Data is generated by the MASTER*/
2804 /* Clear NACK Flag */
2805 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
2806
2807 /* Set ErrorCode corresponding to a Non-Acknowledge */
2808 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
2809
2810 /* Process Unlocked */
2811 __HAL_UNLOCK(hi2c);
2812
2813 /* Call the Error callback to prevent upper layer */
2814 HAL_I2C_ErrorCallback(hi2c);
2815 }
2816 }
2817 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == SET)
2818 {
2819 /* Clear ADDR flag */
2820 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
2821 }
2822 /* Check first if STOPF is set */
2823 /* to prevent a Write Data in TX buffer */
2824 /* which is stuck in TXDR until next */
2825 /* communication with Master */
2826 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
2827 {
2828 /* Disable ERRI, TCI, STOPI, NACKI, ADDRI, RXI, TXI interrupt */
2829 __HAL_I2C_DISABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI );
2830
2831 /* Disable Address Acknowledge */
2832 hi2c->Instance->CR2 |= I2C_CR2_NACK;
2833
2834 /* Clear STOP Flag */
2835 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
2836
2837 hi2c->State = HAL_I2C_STATE_READY;
2838
2839 /* Process Unlocked */
2840 __HAL_UNLOCK(hi2c);
2841
2842 HAL_I2C_SlaveTxCpltCallback(hi2c);
2843 }
2844 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == SET)
2845 {
2846 /* Write data to TXDR only if XferCount not reach "0" */
2847 /* A TXIS flag can be set, during STOP treatment */
2848 if(hi2c->XferCount > 0)
2849 {
2850 /* Write data to TXDR */
2851 hi2c->Instance->TXDR = (*hi2c->pBuffPtr++);
2852 hi2c->XferCount--;
2853 }
2854 }
2855
2856 /* Process Unlocked */
2857 __HAL_UNLOCK(hi2c);
2858
2859 return HAL_OK;
2860 }
2861
2862 /**
2863 * @brief Handle Interrupt Flags Slave Receive Mode
2864 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2865 * the configuration information for the specified I2C.
2866 * @retval HAL status
2867 */
2868 static HAL_StatusTypeDef I2C_SlaveReceive_ISR(I2C_HandleTypeDef *hi2c)
2869 {
2870 /* Process Locked */
2871 __HAL_LOCK(hi2c);
2872
2873 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) != RESET)
2874 {
2875 /* Clear NACK Flag */
2876 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
2877
2878 /* Process Unlocked */
2879 __HAL_UNLOCK(hi2c);
2880
2881 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
2882 HAL_I2C_ErrorCallback(hi2c);
2883 }
2884 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == SET)
2885 {
2886 /* Clear ADDR flag */
2887 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
2888 }
2889 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
2890 {
2891 /* Read data from RXDR */
2892 (*hi2c->pBuffPtr++) = hi2c->Instance->RXDR;
2893 hi2c->XferSize--;
2894 hi2c->XferCount--;
2895 }
2896 else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
2897 {
2898 /* Disable ERRI, TCI, STOPI, NACKI, ADDRI, RXI, TXI interrupt */
2899 __HAL_I2C_DISABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_RXI );
2900
2901 /* Disable Address Acknowledge */
2902 hi2c->Instance->CR2 |= I2C_CR2_NACK;
2903
2904 /* Clear STOP Flag */
2905 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
2906
2907 hi2c->State = HAL_I2C_STATE_READY;
2908
2909 /* Process Unlocked */
2910 __HAL_UNLOCK(hi2c);
2911
2912 HAL_I2C_SlaveRxCpltCallback(hi2c);
2913 }
2914
2915 /* Process Unlocked */
2916 __HAL_UNLOCK(hi2c);
2917
2918 return HAL_OK;
2919 }
2920
2921 /**
2922 * @brief Master sends target device address followed by internal memory address for write request.
2923 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2924 * the configuration information for the specified I2C.
2925 * @param DevAddress: Target device address
2926 * @param MemAddress: Internal memory address
2927 * @param MemAddSize: Size of internal memory address
2928 * @param Timeout: Timeout duration
2929 * @retval HAL status
2930 */
2931 static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout)
2932 {
2933 I2C_TransferConfig(hi2c,DevAddress,MemAddSize, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);
2934
2935 /* Wait until TXIS flag is set */
2936 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
2937 {
2938 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
2939 {
2940 return HAL_ERROR;
2941 }
2942 else
2943 {
2944 return HAL_TIMEOUT;
2945 }
2946 }
2947
2948 /* If Memory address size is 8Bit */
2949 if(MemAddSize == I2C_MEMADD_SIZE_8BIT)
2950 {
2951 /* Send Memory Address */
2952 hi2c->Instance->TXDR = __HAL_I2C_MEM_ADD_LSB(MemAddress);
2953 }
2954 /* If Memory address size is 16Bit */
2955 else
2956 {
2957 /* Send MSB of Memory Address */
2958 hi2c->Instance->TXDR = __HAL_I2C_MEM_ADD_MSB(MemAddress);
2959
2960 /* Wait until TXIS flag is set */
2961 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
2962 {
2963 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
2964 {
2965 return HAL_ERROR;
2966 }
2967 else
2968 {
2969 return HAL_TIMEOUT;
2970 }
2971 }
2972
2973 /* Send LSB of Memory Address */
2974 hi2c->Instance->TXDR = __HAL_I2C_MEM_ADD_LSB(MemAddress);
2975 }
2976
2977 /* Wait until TCR flag is set */
2978 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout) != HAL_OK)
2979 {
2980 return HAL_TIMEOUT;
2981 }
2982
2983 return HAL_OK;
2984 }
2985
2986 /**
2987 * @brief Master sends target device address followed by internal memory address for read request.
2988 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2989 * the configuration information for the specified I2C.
2990 * @param DevAddress: Target device address
2991 * @param MemAddress: Internal memory address
2992 * @param MemAddSize: Size of internal memory address
2993 * @param Timeout: Timeout duration
2994 * @retval HAL status
2995 */
2996 static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout)
2997 {
2998 I2C_TransferConfig(hi2c,DevAddress,MemAddSize, I2C_SOFTEND_MODE, I2C_GENERATE_START_WRITE);
2999
3000 /* Wait until TXIS flag is set */
3001 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
3002 {
3003 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3004 {
3005 return HAL_ERROR;
3006 }
3007 else
3008 {
3009 return HAL_TIMEOUT;
3010 }
3011 }
3012
3013 /* If Memory address size is 8Bit */
3014 if(MemAddSize == I2C_MEMADD_SIZE_8BIT)
3015 {
3016 /* Send Memory Address */
3017 hi2c->Instance->TXDR = __HAL_I2C_MEM_ADD_LSB(MemAddress);
3018 }
3019 /* If Mememory address size is 16Bit */
3020 else
3021 {
3022 /* Send MSB of Memory Address */
3023 hi2c->Instance->TXDR = __HAL_I2C_MEM_ADD_MSB(MemAddress);
3024
3025 /* Wait until TXIS flag is set */
3026 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
3027 {
3028 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3029 {
3030 return HAL_ERROR;
3031 }
3032 else
3033 {
3034 return HAL_TIMEOUT;
3035 }
3036 }
3037
3038 /* Send LSB of Memory Address */
3039 hi2c->Instance->TXDR = __HAL_I2C_MEM_ADD_LSB(MemAddress);
3040 }
3041
3042 /* Wait until TC flag is set */
3043 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TC, RESET, Timeout) != HAL_OK)
3044 {
3045 return HAL_TIMEOUT;
3046 }
3047
3048 return HAL_OK;
3049 }
3050
3051 /**
3052 * @brief DMA I2C master transmit process complete callback.
3053 * @param hdma: DMA handle
3054 * @retval None
3055 */
3056 static void I2C_DMAMasterTransmitCplt(DMA_HandleTypeDef *hdma)
3057 {
3058 uint16_t DevAddress;
3059 I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3060
3061 /* Check if last DMA request was done with RELOAD */
3062 /* Set NBYTES to write and reload if size > 255 */
3063 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
3064 {
3065 /* Wait until TCR flag is set */
3066 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, I2C_TIMEOUT_TCR) != HAL_OK)
3067 {
3068 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3069 }
3070
3071 /* Disable DMA Request */
3072 hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
3073
3074 /* Check if Errors has been detected during transfer */
3075 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3076 {
3077 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3078 /* Wait until STOPF flag is reset */
3079 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3080 {
3081 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3082 {
3083 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3084 }
3085 else
3086 {
3087 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3088 }
3089 }
3090
3091 /* Clear STOP Flag */
3092 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3093
3094 /* Clear Configuration Register 2 */
3095 __HAL_I2C_RESET_CR2(hi2c);
3096
3097 hi2c->XferCount = 0;
3098
3099 hi2c->State = HAL_I2C_STATE_READY;
3100 HAL_I2C_ErrorCallback(hi2c);
3101 }
3102 else
3103 {
3104 hi2c->pBuffPtr += hi2c->XferSize;
3105 hi2c->XferCount -= hi2c->XferSize;
3106 if(hi2c->XferCount > 255)
3107 {
3108 hi2c->XferSize = 255;
3109 }
3110 else
3111 {
3112 hi2c->XferSize = hi2c->XferCount;
3113 }
3114
3115 DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);
3116
3117 /* Enable the DMA channel */
3118 HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
3119
3120 /* Send Slave Address */
3121 /* Set NBYTES to write and reload if size > 255 */
3122 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
3123 {
3124 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
3125 }
3126 else
3127 {
3128 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
3129 }
3130
3131 /* Wait until TXIS flag is set */
3132 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, I2C_TIMEOUT_TXIS) != HAL_OK)
3133 {
3134 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3135 /* Wait until STOPF flag is reset */
3136 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3137 {
3138 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3139 {
3140 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3141 }
3142 else
3143 {
3144 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3145 }
3146 }
3147
3148 /* Clear STOP Flag */
3149 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3150
3151 /* Clear Configuration Register 2 */
3152 __HAL_I2C_RESET_CR2(hi2c);
3153
3154 hi2c->XferCount = 0;
3155
3156 hi2c->State = HAL_I2C_STATE_READY;
3157 HAL_I2C_ErrorCallback(hi2c);
3158 }
3159 else
3160 {
3161 /* Enable DMA Request */
3162 hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
3163 }
3164 }
3165 }
3166 else
3167 {
3168 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3169 /* Wait until STOPF flag is reset */
3170 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3171 {
3172 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3173 {
3174 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3175 }
3176 else
3177 {
3178 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3179 }
3180 }
3181
3182 /* Clear STOP Flag */
3183 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3184
3185 /* Clear Configuration Register 2 */
3186 __HAL_I2C_RESET_CR2(hi2c);
3187
3188 /* Disable DMA Request */
3189 hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
3190
3191 hi2c->XferCount = 0;
3192
3193 hi2c->State = HAL_I2C_STATE_READY;
3194
3195 /* Check if Errors has been detected during transfer */
3196 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3197 {
3198 HAL_I2C_ErrorCallback(hi2c);
3199 }
3200 else
3201 {
3202 HAL_I2C_MasterTxCpltCallback(hi2c);
3203 }
3204 }
3205 }
3206
3207 /**
3208 * @brief DMA I2C slave transmit process complete callback.
3209 * @param hdma: DMA handle
3210 * @retval None
3211 */
3212 static void I2C_DMASlaveTransmitCplt(DMA_HandleTypeDef *hdma)
3213 {
3214 I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3215
3216 /* Wait until STOP flag is set */
3217 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3218 {
3219 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3220 {
3221 /* Normal Use case, a AF is generated by master */
3222 /* to inform slave the end of transfer */
3223 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
3224 }
3225 else
3226 {
3227 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3228 }
3229 }
3230
3231 /* Clear STOP flag */
3232 __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_STOPF);
3233
3234 /* Wait until BUSY flag is reset */
3235 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY) != HAL_OK)
3236 {
3237 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3238 }
3239
3240 /* Disable DMA Request */
3241 hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
3242
3243 hi2c->XferCount = 0;
3244
3245 hi2c->State = HAL_I2C_STATE_READY;
3246
3247 /* Check if Errors has been detected during transfer */
3248 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3249 {
3250 HAL_I2C_ErrorCallback(hi2c);
3251 }
3252 else
3253 {
3254 HAL_I2C_SlaveTxCpltCallback(hi2c);
3255 }
3256 }
3257
3258 /**
3259 * @brief DMA I2C master receive process complete callback
3260 * @param hdma: DMA handle
3261 * @retval None
3262 */
3263 static void I2C_DMAMasterReceiveCplt(DMA_HandleTypeDef *hdma)
3264 {
3265 I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3266 uint16_t DevAddress;
3267
3268 /* Check if last DMA request was done with RELOAD */
3269 /* Set NBYTES to write and reload if size > 255 */
3270 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
3271 {
3272 /* Wait until TCR flag is set */
3273 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, I2C_TIMEOUT_TCR) != HAL_OK)
3274 {
3275 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3276 }
3277
3278 /* Disable DMA Request */
3279 hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
3280
3281 /* Check if Errors has been detected during transfer */
3282 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3283 {
3284 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3285 /* Wait until STOPF flag is reset */
3286 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3287 {
3288 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3289 {
3290 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3291 }
3292 else
3293 {
3294 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3295 }
3296 }
3297
3298 /* Clear STOP Flag */
3299 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3300
3301 /* Clear Configuration Register 2 */
3302 __HAL_I2C_RESET_CR2(hi2c);
3303
3304 hi2c->XferCount = 0;
3305
3306 hi2c->State = HAL_I2C_STATE_READY;
3307 HAL_I2C_ErrorCallback(hi2c);
3308 }
3309 else
3310 {
3311 hi2c->pBuffPtr += hi2c->XferSize;
3312 hi2c->XferCount -= hi2c->XferSize;
3313 if(hi2c->XferCount > 255)
3314 {
3315 hi2c->XferSize = 255;
3316 }
3317 else
3318 {
3319 hi2c->XferSize = hi2c->XferCount;
3320 }
3321
3322 DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);
3323
3324 /* Enable the DMA channel */
3325 HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);
3326
3327 /* Send Slave Address */
3328 /* Set NBYTES to write and reload if size > 255 */
3329 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
3330 {
3331 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
3332 }
3333 else
3334 {
3335 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
3336 }
3337
3338 /* Wait until RXNE flag is set */
3339 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, I2C_TIMEOUT_RXNE) != HAL_OK)
3340 {
3341 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3342 }
3343
3344 /* Check if Errors has been detected during transfer */
3345 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3346 {
3347 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3348 /* Wait until STOPF flag is reset */
3349 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3350 {
3351 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3352 {
3353 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3354 }
3355 else
3356 {
3357 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3358 }
3359 }
3360
3361 /* Clear STOP Flag */
3362 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3363
3364 /* Clear Configuration Register 2 */
3365 __HAL_I2C_RESET_CR2(hi2c);
3366
3367 hi2c->XferCount = 0;
3368
3369 hi2c->State = HAL_I2C_STATE_READY;
3370
3371 HAL_I2C_ErrorCallback(hi2c);
3372 }
3373 else
3374 {
3375 /* Enable DMA Request */
3376 hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
3377 }
3378 }
3379 }
3380 else
3381 {
3382 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3383 /* Wait until STOPF flag is reset */
3384 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3385 {
3386 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3387 {
3388 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3389 }
3390 else
3391 {
3392 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3393 }
3394 }
3395
3396 /* Clear STOP Flag */
3397 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3398
3399 /* Clear Configuration Register 2 */
3400 __HAL_I2C_RESET_CR2(hi2c);
3401
3402 /* Disable DMA Request */
3403 hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
3404
3405 hi2c->XferCount = 0;
3406
3407 hi2c->State = HAL_I2C_STATE_READY;
3408
3409 /* Check if Errors has been detected during transfer */
3410 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3411 {
3412 HAL_I2C_ErrorCallback(hi2c);
3413 }
3414 else
3415 {
3416 HAL_I2C_MasterRxCpltCallback(hi2c);
3417 }
3418 }
3419 }
3420
3421 /**
3422 * @brief DMA I2C slave receive process complete callback.
3423 * @param hdma: DMA handle
3424 * @retval None
3425 */
3426 static void I2C_DMASlaveReceiveCplt(DMA_HandleTypeDef *hdma)
3427 {
3428 I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3429
3430 /* Wait until STOPF flag is reset */
3431 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3432 {
3433 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3434 {
3435 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3436 }
3437 else
3438 {
3439 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3440 }
3441 }
3442
3443 /* Clear STOPF flag */
3444 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3445
3446 /* Wait until BUSY flag is reset */
3447 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY) != HAL_OK)
3448 {
3449 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3450 }
3451
3452 /* Disable DMA Request */
3453 hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
3454
3455 /* Disable Address Acknowledge */
3456 hi2c->Instance->CR2 |= I2C_CR2_NACK;
3457
3458 hi2c->XferCount = 0;
3459
3460 hi2c->State = HAL_I2C_STATE_READY;
3461
3462 /* Check if Errors has been detected during transfer */
3463 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3464 {
3465 HAL_I2C_ErrorCallback(hi2c);
3466 }
3467 else
3468 {
3469 HAL_I2C_SlaveRxCpltCallback(hi2c);
3470 }
3471 }
3472
3473 /**
3474 * @brief DMA I2C Memory Write process complete callback
3475 * @param hdma : DMA handle
3476 * @retval None
3477 */
3478 static void I2C_DMAMemTransmitCplt(DMA_HandleTypeDef *hdma)
3479 {
3480 uint16_t DevAddress;
3481 I2C_HandleTypeDef* hi2c = ( I2C_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
3482
3483 /* Check if last DMA request was done with RELOAD */
3484 /* Set NBYTES to write and reload if size > 255 */
3485 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
3486 {
3487 /* Wait until TCR flag is set */
3488 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, I2C_TIMEOUT_TCR) != HAL_OK)
3489 {
3490 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3491 }
3492
3493 /* Disable DMA Request */
3494 hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
3495
3496 /* Check if Errors has been detected during transfer */
3497 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3498 {
3499 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3500 /* Wait until STOPF flag is reset */
3501 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3502 {
3503 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3504 {
3505 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3506 }
3507 else
3508 {
3509 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3510 }
3511 }
3512
3513 /* Clear STOP Flag */
3514 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3515
3516 /* Clear Configuration Register 2 */
3517 __HAL_I2C_RESET_CR2(hi2c);
3518
3519 hi2c->XferCount = 0;
3520
3521 hi2c->State = HAL_I2C_STATE_READY;
3522 HAL_I2C_ErrorCallback(hi2c);
3523 }
3524 else
3525 {
3526 hi2c->pBuffPtr += hi2c->XferSize;
3527 hi2c->XferCount -= hi2c->XferSize;
3528 if(hi2c->XferCount > 255)
3529 {
3530 hi2c->XferSize = 255;
3531 }
3532 else
3533 {
3534 hi2c->XferSize = hi2c->XferCount;
3535 }
3536
3537 DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);
3538
3539 /* Enable the DMA channel */
3540 HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
3541
3542 /* Send Slave Address */
3543 /* Set NBYTES to write and reload if size > 255 */
3544 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
3545 {
3546 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
3547 }
3548 else
3549 {
3550 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
3551 }
3552
3553 /* Wait until TXIS flag is set */
3554 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, I2C_TIMEOUT_TXIS) != HAL_OK)
3555 {
3556 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3557 /* Wait until STOPF flag is reset */
3558 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3559 {
3560 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3561 {
3562 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3563 }
3564 else
3565 {
3566 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3567 }
3568 }
3569
3570 /* Clear STOP Flag */
3571 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3572
3573 /* Clear Configuration Register 2 */
3574 __HAL_I2C_RESET_CR2(hi2c);
3575
3576 hi2c->XferCount = 0;
3577
3578 hi2c->State = HAL_I2C_STATE_READY;
3579 HAL_I2C_ErrorCallback(hi2c);
3580 }
3581 else
3582 {
3583 /* Enable DMA Request */
3584 hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
3585 }
3586 }
3587 }
3588 else
3589 {
3590 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3591 /* Wait until STOPF flag is reset */
3592 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3593 {
3594 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3595 {
3596 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3597 }
3598 else
3599 {
3600 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3601 }
3602 }
3603
3604 /* Clear STOP Flag */
3605 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3606
3607 /* Clear Configuration Register 2 */
3608 __HAL_I2C_RESET_CR2(hi2c);
3609
3610 /* Disable DMA Request */
3611 hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
3612
3613 hi2c->XferCount = 0;
3614
3615 hi2c->State = HAL_I2C_STATE_READY;
3616
3617 /* Check if Errors has been detected during transfer */
3618 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3619 {
3620 HAL_I2C_ErrorCallback(hi2c);
3621 }
3622 else
3623 {
3624 HAL_I2C_MemTxCpltCallback(hi2c);
3625 }
3626 }
3627 }
3628
3629 /**
3630 * @brief DMA I2C Memory Read process complete callback
3631 * @param hdma: DMA handle
3632 * @retval None
3633 */
3634 static void I2C_DMAMemReceiveCplt(DMA_HandleTypeDef *hdma)
3635 {
3636 I2C_HandleTypeDef* hi2c = ( I2C_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
3637 uint16_t DevAddress;
3638
3639 /* Check if last DMA request was done with RELOAD */
3640 /* Set NBYTES to write and reload if size > 255 */
3641 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
3642 {
3643 /* Wait until TCR flag is set */
3644 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, I2C_TIMEOUT_TCR) != HAL_OK)
3645 {
3646 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3647 }
3648
3649 /* Disable DMA Request */
3650 hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
3651
3652 /* Check if Errors has been detected during transfer */
3653 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3654 {
3655 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3656 /* Wait until STOPF flag is reset */
3657 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3658 {
3659 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3660 {
3661 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3662 }
3663 else
3664 {
3665 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3666 }
3667 }
3668
3669 /* Clear STOP Flag */
3670 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3671
3672 /* Clear Configuration Register 2 */
3673 __HAL_I2C_RESET_CR2(hi2c);
3674
3675 hi2c->XferCount = 0;
3676
3677 hi2c->State = HAL_I2C_STATE_READY;
3678 HAL_I2C_ErrorCallback(hi2c);
3679 }
3680 else
3681 {
3682 hi2c->pBuffPtr += hi2c->XferSize;
3683 hi2c->XferCount -= hi2c->XferSize;
3684 if(hi2c->XferCount > 255)
3685 {
3686 hi2c->XferSize = 255;
3687 }
3688 else
3689 {
3690 hi2c->XferSize = hi2c->XferCount;
3691 }
3692
3693 DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);
3694
3695 /* Enable the DMA channel */
3696 HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);
3697
3698 /* Send Slave Address */
3699 /* Set NBYTES to write and reload if size > 255 */
3700 if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
3701 {
3702 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
3703 }
3704 else
3705 {
3706 I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
3707 }
3708
3709 /* Wait until RXNE flag is set */
3710 if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, I2C_TIMEOUT_RXNE) != HAL_OK)
3711 {
3712 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3713 }
3714
3715 /* Check if Errors has been detected during transfer */
3716 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3717 {
3718 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3719 /* Wait until STOPF flag is reset */
3720 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3721 {
3722 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3723 {
3724 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3725 }
3726 else
3727 {
3728 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3729 }
3730 }
3731
3732 /* Clear STOP Flag */
3733 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3734
3735 /* Clear Configuration Register 2 */
3736 __HAL_I2C_RESET_CR2(hi2c);
3737
3738 hi2c->XferCount = 0;
3739
3740 hi2c->State = HAL_I2C_STATE_READY;
3741 HAL_I2C_ErrorCallback(hi2c);
3742 }
3743 else
3744 {
3745 /* Enable DMA Request */
3746 hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
3747 }
3748 }
3749 }
3750 else
3751 {
3752 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3753 /* Wait until STOPF flag is reset */
3754 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
3755 {
3756 if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3757 {
3758 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
3759 }
3760 else
3761 {
3762 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3763 }
3764 }
3765
3766 /* Clear STOP Flag */
3767 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3768
3769 /* Clear Configuration Register 2 */
3770 __HAL_I2C_RESET_CR2(hi2c);
3771
3772 /* Disable DMA Request */
3773 hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
3774
3775 hi2c->XferCount = 0;
3776
3777 hi2c->State = HAL_I2C_STATE_READY;
3778
3779 /* Check if Errors has been detected during transfer */
3780 if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
3781 {
3782 HAL_I2C_ErrorCallback(hi2c);
3783 }
3784 else
3785 {
3786 HAL_I2C_MemRxCpltCallback(hi2c);
3787 }
3788 }
3789 }
3790
3791 /**
3792 * @brief DMA I2C communication error callback.
3793 * @param hdma : DMA handle
3794 * @retval None
3795 */
3796 static void I2C_DMAError(DMA_HandleTypeDef *hdma)
3797 {
3798 I2C_HandleTypeDef* hi2c = ( I2C_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
3799
3800 /* Disable Acknowledge */
3801 hi2c->Instance->CR2 |= I2C_CR2_NACK;
3802
3803 hi2c->XferCount = 0;
3804
3805 hi2c->State = HAL_I2C_STATE_READY;
3806
3807 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
3808
3809 HAL_I2C_ErrorCallback(hi2c);
3810 }
3811
3812 /**
3813 * @brief This function handles I2C Communication Timeout.
3814 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
3815 * the configuration information for the specified I2C.
3816 * @param Flag: specifies the I2C flag to check.
3817 * @param Status: The new Flag status (SET or RESET).
3818 * @param Timeout: Timeout duration
3819 * @retval HAL status
3820 */
3821 static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
3822 {
3823 uint32_t tickstart = HAL_GetTick();
3824
3825 /* Wait until flag is set */
3826 if(Status == RESET)
3827 {
3828 while(__HAL_I2C_GET_FLAG(hi2c, Flag) == RESET)
3829 {
3830 /* Check for the Timeout */
3831 if(Timeout != HAL_MAX_DELAY)
3832 {
3833 if((Timeout == 0) || ((HAL_GetTick() - tickstart) > Timeout))
3834 {
3835 hi2c->State= HAL_I2C_STATE_READY;
3836 /* Process Unlocked */
3837 __HAL_UNLOCK(hi2c);
3838 return HAL_TIMEOUT;
3839 }
3840 }
3841 }
3842 }
3843 else
3844 {
3845 while(__HAL_I2C_GET_FLAG(hi2c, Flag) != RESET)
3846 {
3847 /* Check for the Timeout */
3848 if(Timeout != HAL_MAX_DELAY)
3849 {
3850 if((Timeout == 0) || ((HAL_GetTick() - tickstart) > Timeout))
3851 {
3852 hi2c->State= HAL_I2C_STATE_READY;
3853 /* Process Unlocked */
3854 __HAL_UNLOCK(hi2c);
3855 return HAL_TIMEOUT;
3856 }
3857 }
3858 }
3859 }
3860 return HAL_OK;
3861 }
3862
3863 /**
3864 * @brief This function handles I2C Communication Timeout for specific usage of TXIS flag.
3865 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
3866 * the configuration information for the specified I2C.
3867 * @param Timeout: Timeout duration
3868 * @retval HAL status
3869 */
3870 static HAL_StatusTypeDef I2C_WaitOnTXISFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout)
3871 {
3872 uint32_t tickstart = HAL_GetTick();
3873
3874 while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == RESET)
3875 {
3876 /* Check if a NACK is detected */
3877 if(I2C_IsAcknowledgeFailed(hi2c, Timeout) != HAL_OK)
3878 {
3879 return HAL_ERROR;
3880 }
3881
3882 /* Check for the Timeout */
3883 if(Timeout != HAL_MAX_DELAY)
3884 {
3885 if((Timeout == 0) || ((HAL_GetTick() - tickstart) > Timeout))
3886 {
3887 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3888 hi2c->State= HAL_I2C_STATE_READY;
3889
3890 /* Process Unlocked */
3891 __HAL_UNLOCK(hi2c);
3892
3893 return HAL_TIMEOUT;
3894 }
3895 }
3896 }
3897 return HAL_OK;
3898 }
3899
3900 /**
3901 * @brief This function handles I2C Communication Timeout for specific usage of STOP flag.
3902 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
3903 * the configuration information for the specified I2C.
3904 * @param Timeout: Timeout duration
3905 * @retval HAL status
3906 */
3907 static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout)
3908 {
3909 uint32_t tickstart = 0x00;
3910 tickstart = HAL_GetTick();
3911
3912 while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET)
3913 {
3914 /* Check if a NACK is detected */
3915 if(I2C_IsAcknowledgeFailed(hi2c, Timeout) != HAL_OK)
3916 {
3917 return HAL_ERROR;
3918 }
3919
3920 /* Check for the Timeout */
3921 if((Timeout == 0) || ((HAL_GetTick() - tickstart) > Timeout))
3922 {
3923 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3924 hi2c->State= HAL_I2C_STATE_READY;
3925
3926 /* Process Unlocked */
3927 __HAL_UNLOCK(hi2c);
3928
3929 return HAL_TIMEOUT;
3930 }
3931 }
3932 return HAL_OK;
3933 }
3934
3935 /**
3936 * @brief This function handles I2C Communication Timeout for specific usage of RXNE flag.
3937 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
3938 * the configuration information for the specified I2C.
3939 * @param Timeout: Timeout duration
3940 * @retval HAL status
3941 */
3942 static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout)
3943 {
3944 uint32_t tickstart = 0x00;
3945 tickstart = HAL_GetTick();
3946
3947 while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == RESET)
3948 {
3949 /* Check if a STOPF is detected */
3950 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
3951 {
3952 /* Clear STOP Flag */
3953 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
3954
3955 /* Clear Configuration Register 2 */
3956 __HAL_I2C_RESET_CR2(hi2c);
3957
3958 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
3959 hi2c->State= HAL_I2C_STATE_READY;
3960
3961 /* Process Unlocked */
3962 __HAL_UNLOCK(hi2c);
3963
3964 return HAL_ERROR;
3965 }
3966
3967 /* Check for the Timeout */
3968 if((Timeout == 0) || ((HAL_GetTick() - tickstart) > Timeout))
3969 {
3970 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3971 hi2c->State= HAL_I2C_STATE_READY;
3972
3973 /* Process Unlocked */
3974 __HAL_UNLOCK(hi2c);
3975
3976 return HAL_TIMEOUT;
3977 }
3978 }
3979 return HAL_OK;
3980 }
3981
3982 /**
3983 * @brief This function handles Acknowledge failed detection during an I2C Communication.
3984 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
3985 * the configuration information for the specified I2C.
3986 * @param Timeout: Timeout duration
3987 * @retval HAL status
3988 */
3989 static HAL_StatusTypeDef I2C_IsAcknowledgeFailed(I2C_HandleTypeDef *hi2c, uint32_t Timeout)
3990 {
3991 uint32_t tickstart = 0x00;
3992 tickstart = HAL_GetTick();
3993
3994 if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
3995 {
3996 /* Generate stop if necessary only in case of I2C peripheral in MASTER mode */
3997 if((hi2c->State == HAL_I2C_STATE_MASTER_BUSY_TX) || (hi2c->State == HAL_I2C_STATE_MEM_BUSY_TX)
3998 || (hi2c->State == HAL_I2C_STATE_MEM_BUSY_RX))
3999 {
4000 /* No need to generate the STOP condition if AUTOEND mode is enabled */
4001 /* Generate the STOP condition only in case of SOFTEND mode is enabled */
4002 if((hi2c->Instance->CR2 & I2C_AUTOEND_MODE) != I2C_AUTOEND_MODE)
4003 {
4004 /* Generate Stop */
4005 hi2c->Instance->CR2 |= I2C_CR2_STOP;
4006 }
4007 }
4008
4009 /* Wait until STOP Flag is reset */
4010 /* AutoEnd should be initiate after AF */
4011 while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET)
4012 {
4013 /* Check for the Timeout */
4014 if(Timeout != HAL_MAX_DELAY)
4015 {
4016 if((Timeout == 0) || ((HAL_GetTick() - tickstart) > Timeout))
4017 {
4018 hi2c->State= HAL_I2C_STATE_READY;
4019 /* Process Unlocked */
4020 __HAL_UNLOCK(hi2c);
4021 return HAL_TIMEOUT;
4022 }
4023 }
4024 }
4025
4026 /* Clear NACKF Flag */
4027 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
4028
4029 /* Clear STOP Flag */
4030 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
4031
4032 /* Clear Configuration Register 2 */
4033 __HAL_I2C_RESET_CR2(hi2c);
4034
4035 hi2c->ErrorCode = HAL_I2C_ERROR_AF;
4036 hi2c->State= HAL_I2C_STATE_READY;
4037
4038 /* Process Unlocked */
4039 __HAL_UNLOCK(hi2c);
4040
4041 return HAL_ERROR;
4042 }
4043 return HAL_OK;
4044 }
4045
4046 /**
4047 * @brief Handles I2Cx communication when starting transfer or during transfer (TC or TCR flag are set).
4048 * @param hi2c: I2C handle.
4049 * @param DevAddress: specifies the slave address to be programmed.
4050 * @param Size: specifies the number of bytes to be programmed.
4051 * This parameter must be a value between 0 and 255.
4052 * @param Mode: new state of the I2C START condition generation.
4053 * This parameter can be one of the following values:
4054 * @arg I2C_RELOAD_MODE: Enable Reload mode .
4055 * @arg I2C_AUTOEND_MODE: Enable Automatic end mode.
4056 * @arg I2C_SOFTEND_MODE: Enable Software end mode.
4057 * @param Request: new state of the I2C START condition generation.
4058 * This parameter can be one of the following values:
4059 * @arg I2C_NO_STARTSTOP: Don't Generate stop and start condition.
4060 * @arg I2C_GENERATE_STOP: Generate stop condition (Size should be set to 0).
4061 * @arg I2C_GENERATE_START_READ: Generate Restart for read request.
4062 * @arg I2C_GENERATE_START_WRITE: Generate Restart for write request.
4063 * @retval None
4064 */
4065 static void I2C_TransferConfig(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t Size, uint32_t Mode, uint32_t Request)
4066 {
4067 uint32_t tmpreg = 0;
4068
4069 /* Check the parameters */
4070 assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
4071 assert_param(IS_TRANSFER_MODE(Mode));
4072 assert_param(IS_TRANSFER_REQUEST(Request));
4073
4074 /* Get the CR2 register value */
4075 tmpreg = hi2c->Instance->CR2;
4076
4077 /* clear tmpreg specific bits */
4078 tmpreg &= (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP));
4079
4080 /* update tmpreg */
4081 tmpreg |= (uint32_t)(((uint32_t)DevAddress & I2C_CR2_SADD) | (((uint32_t)Size << 16 ) & I2C_CR2_NBYTES) | \
4082 (uint32_t)Mode | (uint32_t)Request);
4083
4084 /* update CR2 register */
4085 hi2c->Instance->CR2 = tmpreg;
4086 }
4087
4088 /**
4089 * @}
4090 */
4091
4092 #endif /* HAL_I2C_MODULE_ENABLED */
4093 /**
4094 * @}
4095 */
4096
4097 /**
4098 * @}
4099 */
4100
4101 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Imprint / Impressum