2 ******************************************************************************
3 * @file stm32f0xx_hal_i2c.c
4 * @author MCD Application Team
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
15 ==============================================================================
16 ##### How to use this driver #####
17 ==============================================================================
19 The I2C HAL driver can be used as follows:
21 (#) Declare a I2C_HandleTypeDef handle structure, for example:
22 I2C_HandleTypeDef hi2c;
24 (#)Initialize the I2C low level resources by implement the HAL_I2C_MspInit ()API:
25 (++) Enable the I2Cx interface clock
26 (++) I2C pins configuration
27 (+++) Enable the clock for the I2C GPIOs
28 (+++) Configure I2C pins as alternate function open-drain
29 (++) NVIC configuration if you need to use interrupt process
30 (+++) Configure the I2Cx interrupt priority
31 (+++) Enable the NVIC I2C IRQ Channel
32 (++) DMA Configuration if you need to use DMA process
33 (+++) Declare a DMA_HandleTypeDef handle structure for the transmit or receive channel
34 (+++) Enable the DMAx interface clock using
35 (+++) Configure the DMA handle parameters
36 (+++) Configure the DMA Tx or Rx channel
37 (+++) Associate the initilalized DMA handle to the hi2c DMA Tx or Rx handle
38 (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx or Rx channel
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.
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.
47 (#) To check if target device is ready for communication, use the function HAL_I2C_IsDeviceReady()
49 (#) For I2C IO and IO MEM operations, three modes of operations are available within this driver :
51 *** Polling mode IO operation ***
52 =================================
54 (+) Transmit in master mode an amount of data in blocking mode using HAL_I2C_Master_Transmit()
55 (+) Receive in master mode an amount of data in blocking mode using HAL_I2C_Master_Receive()
56 (+) Transmit in slave mode an amount of data in blocking mode using HAL_I2C_Slave_Transmit()
57 (+) Receive in slave mode an amount of data in blocking mode using HAL_I2C_Slave_Receive()
59 *** Polling mode IO MEM operation ***
60 =====================================
62 (+) Write an amount of data in blocking mode to a specific memory address using HAL_I2C_Mem_Write()
63 (+) Read an amount of data in blocking mode from a specific memory address using HAL_I2C_Mem_Read()
66 *** Interrupt mode IO operation ***
67 ===================================
69 (+) Transmit in master mode an amount of data in non blocking mode using HAL_I2C_Master_Transmit_IT()
70 (+) At transmission end of transfer HAL_I2C_MasterTxCpltCallback is executed and user can
71 add his own code by customization of function pointer HAL_I2C_MasterTxCpltCallback
72 (+) Receive in master mode an amount of data in non blocking mode using HAL_I2C_Master_Receive_IT()
73 (+) At reception end of transfer HAL_I2C_MasterRxCpltCallback is executed and user can
74 add his own code by customization of function pointer HAL_I2C_MasterRxCpltCallback
75 (+) Transmit in slave mode an amount of data in non blocking mode using HAL_I2C_Slave_Transmit_IT()
76 (+) At transmission end of transfer HAL_I2C_SlaveTxCpltCallback is executed and user can
77 add his own code by customization of function pointer HAL_I2C_SlaveTxCpltCallback
78 (+) Receive in slave mode an amount of data in non blocking mode using HAL_I2C_Slave_Receive_IT()
79 (+) At reception end of transfer HAL_I2C_SlaveRxCpltCallback is executed and user can
80 add his own code by customization of function pointer HAL_I2C_SlaveRxCpltCallback
81 (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
82 add his own code by customization of function pointer HAL_I2C_ErrorCallback
84 *** Interrupt mode IO MEM operation ***
85 =======================================
87 (+) Write an amount of data in no-blocking mode with Interrupt to a specific memory address using
88 HAL_I2C_Mem_Write_IT()
89 (+) At MEM end of write transfer HAL_I2C_MemTxCpltCallback is executed and user can
90 add his own code by customization of function pointer HAL_I2C_MemTxCpltCallback
91 (+) Read an amount of data in no-blocking mode with Interrupt from a specific memory address using
93 (+) At MEM end of read transfer HAL_I2C_MemRxCpltCallback is executed and user can
94 add his own code by customization of function pointer HAL_I2C_MemRxCpltCallback
95 (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
96 add his own code by customization of function pointer HAL_I2C_ErrorCallback
98 *** DMA mode IO operation ***
99 ==============================
101 (+) Transmit in master mode an amount of data in non blocking mode (DMA) using
102 HAL_I2C_Master_Transmit_DMA()
103 (+) At transmission end of transfer HAL_I2C_MasterTxCpltCallback is executed and user can
104 add his own code by customization of function pointer HAL_I2C_MasterTxCpltCallback
105 (+) Receive in master mode an amount of data in non blocking mode (DMA) using
106 HAL_I2C_Master_Receive_DMA()
107 (+) At reception end of transfer HAL_I2C_MasterRxCpltCallback is executed and user can
108 add his own code by customization of function pointer HAL_I2C_MasterRxCpltCallback
109 (+) Transmit in slave mode an amount of data in non blocking mode (DMA) using
110 HAL_I2C_Slave_Transmit_DMA()
111 (+) At transmission end of transfer HAL_I2C_SlaveTxCpltCallback is executed and user can
112 add his own code by customization of function pointer HAL_I2C_SlaveTxCpltCallback
113 (+) Receive in slave mode an amount of data in non blocking mode (DMA) using
114 HAL_I2C_Slave_Receive_DMA()
115 (+) At reception end of transfer HAL_I2C_SlaveRxCpltCallback is executed and user can
116 add his own code by customization of function pointer HAL_I2C_SlaveRxCpltCallback
117 (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
118 add his own code by customization of function pointer HAL_I2C_ErrorCallback
120 *** DMA mode IO MEM operation ***
121 =================================
123 (+) Write an amount of data in no-blocking mode with DMA to a specific memory address using
124 HAL_I2C_Mem_Write_DMA()
125 (+) At MEM end of write transfer HAL_I2C_MemTxCpltCallback is executed and user can
126 add his own code by customization of function pointer HAL_I2C_MemTxCpltCallback
127 (+) Read an amount of data in no-blocking mode with DMA from a specific memory address using
128 HAL_I2C_Mem_Read_DMA()
129 (+) At MEM end of read transfer HAL_I2C_MemRxCpltCallback is executed and user can
130 add his own code by customization of function pointer HAL_I2C_MemRxCpltCallback
131 (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
132 add his own code by customization of function pointer HAL_I2C_ErrorCallback
135 *** I2C HAL driver macros list ***
136 ==================================
138 Below the list of most used macros in I2C HAL driver.
140 (+) __HAL_I2C_ENABLE: Enable the I2C peripheral
141 (+) __HAL_I2C_DISABLE: Disable the I2C peripheral
142 (+) __HAL_I2C_GET_FLAG : 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
148 (@) You can refer to the I2C HAL driver header file for more useful macros
151 ******************************************************************************
154 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
156 * Redistribution and use in source and binary forms, with or without modification,
157 * are permitted provided that the following conditions are met:
158 * 1. Redistributions of source code must retain the above copyright notice,
159 * this list of conditions and the following disclaimer.
160 * 2. Redistributions in binary form must reproduce the above copyright notice,
161 * this list of conditions and the following disclaimer in the documentation
162 * and/or other materials provided with the distribution.
163 * 3. Neither the name of STMicroelectronics nor the names of its contributors
164 * may be used to endorse or promote products derived from this software
165 * without specific prior written permission.
167 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
168 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
169 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
170 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
171 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
172 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
173 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
174 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
175 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
176 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
178 ******************************************************************************
181 /* Includes ------------------------------------------------------------------*/
182 #include "stm32f0xx_hal.h"
184 /** @addtogroup STM32F0xx_HAL_Driver
188 /** @defgroup I2C I2C HAL module driver
189 * @brief I2C HAL module driver
193 #ifdef HAL_I2C_MODULE_ENABLED
195 /* Private typedef -----------------------------------------------------------*/
196 /* Private define ------------------------------------------------------------*/
198 /** @defgroup I2C_Private_Define I2C Private Define
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 */
215 /* Private macro -------------------------------------------------------------*/
216 /* Private variables ---------------------------------------------------------*/
217 /* Private function prototypes -----------------------------------------------*/
219 /** @defgroup I2C_Private_Functions I2C Private Functions
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
);
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
);
238 static HAL_StatusTypeDef
I2C_MasterTransmit_ISR(I2C_HandleTypeDef
*hi2c
);
239 static HAL_StatusTypeDef
I2C_MasterReceive_ISR(I2C_HandleTypeDef
*hi2c
);
241 static HAL_StatusTypeDef
I2C_SlaveTransmit_ISR(I2C_HandleTypeDef
*hi2c
);
242 static HAL_StatusTypeDef
I2C_SlaveReceive_ISR(I2C_HandleTypeDef
*hi2c
);
244 static void I2C_TransferConfig(I2C_HandleTypeDef
*hi2c
, uint16_t DevAddress
, uint8_t Size
, uint32_t Mode
, uint32_t Request
);
249 /* Exported functions ---------------------------------------------------------*/
251 /** @defgroup I2C_Exported_Functions I2C Exported Functions
255 /** @defgroup I2C_Exported_Functions_Group1 Initialization and de-initialization functions
256 * @brief Initialization and Configuration functions
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:
265 (+) User must Implement HAL_I2C_MspInit() function in which he configures
266 all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
268 (+) Call the function HAL_I2C_Init() to configure the selected device with
269 the selected configuration:
272 (++) Addressing mode (Master, Slave)
273 (++) Dual Addressing mode
275 (++) Own Address 2 Mask
276 (++) General call mode
279 (+) Call the function HAL_I2C_DeInit() to restore the default configuration
280 of the selected I2Cx periperal.
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.
293 HAL_StatusTypeDef
HAL_I2C_Init(I2C_HandleTypeDef
*hi2c
)
295 /* Check the I2C handle allocation */
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
));
311 if(hi2c
->State
== HAL_I2C_STATE_RESET
)
313 /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
314 HAL_I2C_MspInit(hi2c
);
317 hi2c
->State
= HAL_I2C_STATE_BUSY
;
319 /* Disable the selected I2C peripheral */
320 __HAL_I2C_DISABLE(hi2c
);
322 /*---------------------------- I2Cx TIMINGR Configuration ------------------*/
323 /* Configure I2Cx: Frequency range */
324 hi2c
->Instance
->TIMINGR
= hi2c
->Init
.Timing
& TIMING_CLEAR_MASK
;
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)
331 if(hi2c
->Init
.AddressingMode
== I2C_ADDRESSINGMODE_7BIT
)
333 hi2c
->Instance
->OAR1
= (I2C_OAR1_OA1EN
| hi2c
->Init
.OwnAddress1
);
335 else /* I2C_ADDRESSINGMODE_10BIT */
337 hi2c
->Instance
->OAR1
= (I2C_OAR1_OA1EN
| I2C_OAR1_OA1MODE
| hi2c
->Init
.OwnAddress1
);
341 /*---------------------------- I2Cx CR2 Configuration ----------------------*/
342 /* Configure I2Cx: Addressing Master mode */
343 if(hi2c
->Init
.AddressingMode
== I2C_ADDRESSINGMODE_10BIT
)
345 hi2c
->Instance
->CR2
= (I2C_CR2_ADD10
);
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
);
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));
354 /*---------------------------- I2Cx CR1 Configuration ----------------------*/
355 /* Configure I2Cx: Generalcall and NoStretch mode */
356 hi2c
->Instance
->CR1
= (hi2c
->Init
.GeneralCallMode
| hi2c
->Init
.NoStretchMode
);
358 /* Enable the selected I2C peripheral */
359 __HAL_I2C_ENABLE(hi2c
);
361 hi2c
->ErrorCode
= HAL_I2C_ERROR_NONE
;
362 hi2c
->State
= HAL_I2C_STATE_READY
;
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.
373 HAL_StatusTypeDef
HAL_I2C_DeInit(I2C_HandleTypeDef
*hi2c
)
375 /* Check the I2C handle allocation */
381 /* Check the parameters */
382 assert_param(IS_I2C_ALL_INSTANCE(hi2c
->Instance
));
384 hi2c
->State
= HAL_I2C_STATE_BUSY
;
386 /* Disable the I2C Peripheral Clock */
387 __HAL_I2C_DISABLE(hi2c
);
389 /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
390 HAL_I2C_MspDeInit(hi2c
);
392 hi2c
->ErrorCode
= HAL_I2C_ERROR_NONE
;
393 hi2c
->State
= HAL_I2C_STATE_RESET
;
402 * @brief I2C MSP Init.
403 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
404 * the configuration information for the specified I2C.
407 __weak
void HAL_I2C_MspInit(I2C_HandleTypeDef
*hi2c
)
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
415 * @brief I2C MSP DeInit
416 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
417 * the configuration information for the specified I2C.
420 __weak
void HAL_I2C_MspDeInit(I2C_HandleTypeDef
*hi2c
)
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
431 /** @defgroup I2C_Exported_Functions_Group2 Input and Output operation functions
432 * @brief Data transfers functions
435 ===============================================================================
436 ##### IO operation functions #####
437 ===============================================================================
439 This subsection provides a set of functions allowing to manage the I2C data
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
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()
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()
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()
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()
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
500 HAL_StatusTypeDef
HAL_I2C_Master_Transmit(I2C_HandleTypeDef
*hi2c
, uint16_t DevAddress
, uint8_t *pData
, uint16_t Size
, uint32_t Timeout
)
502 uint32_t sizetmp
= 0;
504 if(hi2c
->State
== HAL_I2C_STATE_READY
)
506 if((pData
== NULL
) || (Size
== 0))
511 if(__HAL_I2C_GET_FLAG(hi2c
, I2C_FLAG_BUSY
) == SET
)
519 hi2c
->State
= HAL_I2C_STATE_MASTER_BUSY_TX
;
520 hi2c
->ErrorCode
= HAL_I2C_ERROR_NONE
;
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 */
527 I2C_TransferConfig(hi2c
,DevAddress
,255, I2C_RELOAD_MODE
, I2C_GENERATE_START_WRITE
);
532 I2C_TransferConfig(hi2c
,DevAddress
,Size
, I2C_AUTOEND_MODE
, I2C_GENERATE_START_WRITE
);
538 /* Wait until TXIS flag is set */
539 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c
, Timeout
) != HAL_OK
)
541 if(hi2c
->ErrorCode
== HAL_I2C_ERROR_AF
)
550 /* Write data to TXDR */
551 hi2c
->Instance
->TXDR
= (*pData
++);
555 if((sizetmp
== 0)&&(Size
!=0))
557 /* Wait until TXE flag is set */
558 if(I2C_WaitOnFlagUntilTimeout(hi2c
, I2C_FLAG_TCR
, RESET
, Timeout
) != HAL_OK
)
565 I2C_TransferConfig(hi2c
,DevAddress
,255, I2C_RELOAD_MODE
, I2C_NO_STARTSTOP
);
570 I2C_TransferConfig(hi2c
,DevAddress
,Size
, I2C_AUTOEND_MODE
, I2C_NO_STARTSTOP
);
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
)
581 if(hi2c
->ErrorCode
== HAL_I2C_ERROR_AF
)
591 /* Clear STOP Flag */
592 __HAL_I2C_CLEAR_FLAG(hi2c
, I2C_FLAG_STOPF
);
594 /* Clear Configuration Register 2 */
595 __HAL_I2C_RESET_CR2(hi2c
);
597 hi2c
->State
= HAL_I2C_STATE_READY
;
599 /* Process Unlocked */
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
620 HAL_StatusTypeDef
HAL_I2C_Master_Receive(I2C_HandleTypeDef
*hi2c
, uint16_t DevAddress
, uint8_t *pData
, uint16_t Size
, uint32_t Timeout
)
622 uint32_t sizetmp
= 0;
624 if(hi2c
->State
== HAL_I2C_STATE_READY
)
626 if((pData
== NULL
) || (Size
== 0))
631 if(__HAL_I2C_GET_FLAG(hi2c
, I2C_FLAG_BUSY
) == SET
)
639 hi2c
->State
= HAL_I2C_STATE_MASTER_BUSY_RX
;
640 hi2c
->ErrorCode
= HAL_I2C_ERROR_NONE
;
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 */
647 I2C_TransferConfig(hi2c
,DevAddress
,255, I2C_RELOAD_MODE
, I2C_GENERATE_START_READ
);
652 I2C_TransferConfig(hi2c
,DevAddress
,Size
, I2C_AUTOEND_MODE
, I2C_GENERATE_START_READ
);
658 /* Wait until RXNE flag is set */
659 if(I2C_WaitOnFlagUntilTimeout(hi2c
, I2C_FLAG_RXNE
, RESET
, Timeout
) != HAL_OK
)
664 /* Write data to RXDR */
665 (*pData
++) =hi2c
->Instance
->RXDR
;
669 if((sizetmp
== 0)&&(Size
!=0))
671 /* Wait until TCR flag is set */
672 if(I2C_WaitOnFlagUntilTimeout(hi2c
, I2C_FLAG_TCR
, RESET
, Timeout
) != HAL_OK
)
679 I2C_TransferConfig(hi2c
,DevAddress
,255, I2C_RELOAD_MODE
, I2C_NO_STARTSTOP
);
684 I2C_TransferConfig(hi2c
,DevAddress
,Size
, I2C_AUTOEND_MODE
, I2C_NO_STARTSTOP
);
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
)
695 if(hi2c
->ErrorCode
== HAL_I2C_ERROR_AF
)
705 /* Clear STOP Flag */
706 __HAL_I2C_CLEAR_FLAG(hi2c
, I2C_FLAG_STOPF
);
708 /* Clear Configuration Register 2 */
709 __HAL_I2C_RESET_CR2(hi2c
);
711 hi2c
->State
= HAL_I2C_STATE_READY
;
713 /* Process Unlocked */
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
733 HAL_StatusTypeDef
HAL_I2C_Slave_Transmit(I2C_HandleTypeDef
*hi2c
, uint8_t *pData
, uint16_t Size
, uint32_t Timeout
)
735 if(hi2c
->State
== HAL_I2C_STATE_READY
)
737 if((pData
== NULL
) || (Size
== 0))
745 hi2c
->State
= HAL_I2C_STATE_SLAVE_BUSY_RX
;
746 hi2c
->ErrorCode
= HAL_I2C_ERROR_NONE
;
748 /* Enable Address Acknowledge */
749 hi2c
->Instance
->CR2
&= ~I2C_CR2_NACK
;
751 /* Wait until ADDR flag is set */
752 if(I2C_WaitOnFlagUntilTimeout(hi2c
, I2C_FLAG_ADDR
, RESET
, Timeout
) != HAL_OK
)
754 /* Disable Address Acknowledge */
755 hi2c
->Instance
->CR2
|= I2C_CR2_NACK
;
759 /* Clear ADDR flag */
760 __HAL_I2C_CLEAR_FLAG(hi2c
,I2C_FLAG_ADDR
);
762 /* If 10bit addressing mode is selected */
763 if(hi2c
->Init
.AddressingMode
== I2C_ADDRESSINGMODE_10BIT
)
765 /* Wait until ADDR flag is set */
766 if(I2C_WaitOnFlagUntilTimeout(hi2c
, I2C_FLAG_ADDR
, RESET
, Timeout
) != HAL_OK
)
768 /* Disable Address Acknowledge */
769 hi2c
->Instance
->CR2
|= I2C_CR2_NACK
;
773 /* Clear ADDR flag */
774 __HAL_I2C_CLEAR_FLAG(hi2c
,I2C_FLAG_ADDR
);
777 /* Wait until DIR flag is set Transmitter mode */
778 if(I2C_WaitOnFlagUntilTimeout(hi2c
, I2C_FLAG_DIR
, RESET
, Timeout
) != HAL_OK
)
780 /* Disable Address Acknowledge */
781 hi2c
->Instance
->CR2
|= I2C_CR2_NACK
;
787 /* Wait until TXIS flag is set */
788 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c
, Timeout
) != HAL_OK
)
790 /* Disable Address Acknowledge */
791 hi2c
->Instance
->CR2
|= I2C_CR2_NACK
;
793 if(hi2c
->ErrorCode
== HAL_I2C_ERROR_AF
)
803 /* Read data from TXDR */
804 hi2c
->Instance
->TXDR
= (*pData
++);
808 /* Wait until STOP flag is set */
809 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c
, I2C_TIMEOUT_STOPF
) != HAL_OK
)
811 /* Disable Address Acknowledge */
812 hi2c
->Instance
->CR2
|= I2C_CR2_NACK
;
814 if(hi2c
->ErrorCode
== HAL_I2C_ERROR_AF
)
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
;
826 /* Clear STOP flag */
827 __HAL_I2C_CLEAR_FLAG(hi2c
,I2C_FLAG_STOPF
);
829 /* Wait until BUSY flag is reset */
830 if(I2C_WaitOnFlagUntilTimeout(hi2c
, I2C_FLAG_BUSY
, SET
, Timeout
) != HAL_OK
)
832 /* Disable Address Acknowledge */
833 hi2c
->Instance
->CR2
|= I2C_CR2_NACK
;
837 /* Disable Address Acknowledge */
838 hi2c
->Instance
->CR2
|= I2C_CR2_NACK
;
840 hi2c
->State
= HAL_I2C_STATE_READY
;
842 /* Process Unlocked */
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
862 HAL_StatusTypeDef
HAL_I2C_Slave_Receive(I2C_HandleTypeDef
*hi2c
, uint8_t *pData
, uint16_t Size
, uint32_t Timeout
)
864 if(hi2c
->State
== HAL_I2C_STATE_READY
)
866 if((pData
== NULL
) || (Size
== 0))
874 hi2c
->State
= HAL_I2C_STATE_SLAVE_BUSY_RX
;
875 hi2c
->ErrorCode
= HAL_I2C_ERROR_NONE
;
877 /* Enable Address Acknowledge */
878 hi2c
->Instance
->CR2
&= ~I2C_CR2_NACK
;
880 /* Wait until ADDR flag is set */
881 if(I2C_WaitOnFlagUntilTimeout(hi2c
, I2C_FLAG_ADDR
, RESET
, Timeout
) != HAL_OK
)
883 /* Disable Address Acknowledge */
884 hi2c
->Instance
->CR2
|= I2C_CR2_NACK
;
888 /* Clear ADDR flag */
889 __HAL_I2C_CLEAR_FLAG(hi2c
,I2C_FLAG_ADDR
);
891 /* Wait until DIR flag is reset Receiver mode */
892 if(I2C_WaitOnFlagUntilTimeout(hi2c
, I2C_FLAG_DIR
, SET
, Timeout
) != HAL_OK
)
894 /* Disable Address Acknowledge */
895 hi2c
->Instance
->CR2
|= I2C_CR2_NACK
;
901 /* Wait until RXNE flag is set */
902 if(I2C_WaitOnRXNEFlagUntilTimeout(hi2c
, Timeout
) != HAL_OK
)
904 /* Disable Address Acknowledge */
905 hi2c
->Instance
->CR2
|= I2C_CR2_NACK
;
906 if(hi2c
->ErrorCode
== HAL_I2C_ERROR_TIMEOUT
)
916 /* Read data from RXDR */
917 (*pData
++) = hi2c
->Instance
->RXDR
;
921 /* Wait until STOP flag is set */
922 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c
, I2C_TIMEOUT_STOPF
) != HAL_OK
)
924 /* Disable Address Acknowledge */
925 hi2c
->Instance
->CR2
|= I2C_CR2_NACK
;
927 if(hi2c
->ErrorCode
== HAL_I2C_ERROR_AF
)
937 /* Clear STOP flag */
938 __HAL_I2C_CLEAR_FLAG(hi2c
,I2C_FLAG_STOPF
);
940 /* Wait until BUSY flag is reset */
941 if(I2C_WaitOnFlagUntilTimeout(hi2c
, I2C_FLAG_BUSY
, SET
, Timeout
) != HAL_OK
)
943 /* Disable Address Acknowledge */
944 hi2c
->Instance
->CR2
|= I2C_CR2_NACK
;
949 /* Disable Address Acknowledge */
950 hi2c
->Instance
->CR2
|= I2C_CR2_NACK
;
952 hi2c
->State
= HAL_I2C_STATE_READY
;
954 /* Process Unlocked */
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
974 HAL_StatusTypeDef
HAL_I2C_Master_Transmit_IT(I2C_HandleTypeDef
*hi2c
, uint16_t DevAddress
, uint8_t *pData
, uint16_t Size
)
976 if(hi2c
->State
== HAL_I2C_STATE_READY
)
978 if((pData
== NULL
) || (Size
== 0))
983 if(__HAL_I2C_GET_FLAG(hi2c
, I2C_FLAG_BUSY
) == SET
)
991 hi2c
->State
= HAL_I2C_STATE_MASTER_BUSY_TX
;
992 hi2c
->ErrorCode
= HAL_I2C_ERROR_NONE
;
994 hi2c
->pBuffPtr
= pData
;
995 hi2c
->XferCount
= Size
;
998 hi2c
->XferSize
= 255;
1002 hi2c
->XferSize
= Size
;
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
) )
1009 I2C_TransferConfig(hi2c
,DevAddress
,hi2c
->XferSize
, I2C_RELOAD_MODE
, I2C_GENERATE_START_WRITE
);
1013 I2C_TransferConfig(hi2c
,DevAddress
,hi2c
->XferSize
, I2C_AUTOEND_MODE
, I2C_GENERATE_START_WRITE
);
1016 /* Process Unlocked */
1019 /* Note : The I2C interrupts must be enabled after unlocking current process
1020 to avoid the risk of I2C interrupt handle execution before current
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
);
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
1046 HAL_StatusTypeDef
HAL_I2C_Master_Receive_IT(I2C_HandleTypeDef
*hi2c
, uint16_t DevAddress
, uint8_t *pData
, uint16_t Size
)
1048 if(hi2c
->State
== HAL_I2C_STATE_READY
)
1050 if((pData
== NULL
) || (Size
== 0))
1055 if(__HAL_I2C_GET_FLAG(hi2c
, I2C_FLAG_BUSY
) == SET
)
1060 /* Process Locked */
1063 hi2c
->State
= HAL_I2C_STATE_MASTER_BUSY_RX
;
1064 hi2c
->ErrorCode
= HAL_I2C_ERROR_NONE
;
1066 hi2c
->pBuffPtr
= pData
;
1067 hi2c
->XferCount
= Size
;
1070 hi2c
->XferSize
= 255;
1074 hi2c
->XferSize
= Size
;
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
) )
1081 I2C_TransferConfig(hi2c
,DevAddress
,hi2c
->XferSize
, I2C_RELOAD_MODE
, I2C_GENERATE_START_READ
);
1085 I2C_TransferConfig(hi2c
,DevAddress
,hi2c
->XferSize
, I2C_AUTOEND_MODE
, I2C_GENERATE_START_READ
);
1088 /* Process Unlocked */
1091 /* Note : The I2C interrupts must be enabled after unlocking current process
1092 to avoid the risk of I2C interrupt handle execution before current
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
);
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
1116 HAL_StatusTypeDef
HAL_I2C_Slave_Transmit_IT(I2C_HandleTypeDef
*hi2c
, uint8_t *pData
, uint16_t Size
)
1118 if(hi2c
->State
== HAL_I2C_STATE_READY
)
1120 if((pData
== NULL
) || (Size
== 0))
1125 /* Process Locked */
1128 hi2c
->State
= HAL_I2C_STATE_SLAVE_BUSY_TX
;
1129 hi2c
->ErrorCode
= HAL_I2C_ERROR_NONE
;
1131 /* Enable Address Acknowledge */
1132 hi2c
->Instance
->CR2
&= ~I2C_CR2_NACK
;
1134 hi2c
->pBuffPtr
= pData
;
1135 hi2c
->XferSize
= Size
;
1136 hi2c
->XferCount
= Size
;
1138 /* Process Unlocked */
1141 /* Note : The I2C interrupts must be enabled after unlocking current process
1142 to avoid the risk of I2C interrupt handle execution before current
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
);
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
1166 HAL_StatusTypeDef
HAL_I2C_Slave_Receive_IT(I2C_HandleTypeDef
*hi2c
, uint8_t *pData
, uint16_t Size
)
1168 if(hi2c
->State
== HAL_I2C_STATE_READY
)
1170 if((pData
== NULL
) || (Size
== 0))
1175 /* Process Locked */
1178 hi2c
->State
= HAL_I2C_STATE_SLAVE_BUSY_RX
;
1179 hi2c
->ErrorCode
= HAL_I2C_ERROR_NONE
;
1181 /* Enable Address Acknowledge */
1182 hi2c
->Instance
->CR2
&= ~I2C_CR2_NACK
;
1184 hi2c
->pBuffPtr
= pData
;
1185 hi2c
->XferSize
= Size
;
1186 hi2c
->XferCount
= Size
;
1188 /* Process Unlocked */
1191 /* Note : The I2C interrupts must be enabled after unlocking current process
1192 to avoid the risk of I2C interrupt handle execution before current
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
);
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
1217 HAL_StatusTypeDef
HAL_I2C_Master_Transmit_DMA(I2C_HandleTypeDef
*hi2c
, uint16_t DevAddress
, uint8_t *pData
, uint16_t Size
)
1219 if(hi2c
->State
== HAL_I2C_STATE_READY
)
1221 if((pData
== NULL
) || (Size
== 0))
1226 if(__HAL_I2C_GET_FLAG(hi2c
, I2C_FLAG_BUSY
) == SET
)
1231 /* Process Locked */
1234 hi2c
->State
= HAL_I2C_STATE_MASTER_BUSY_TX
;
1235 hi2c
->ErrorCode
= HAL_I2C_ERROR_NONE
;
1237 hi2c
->pBuffPtr
= pData
;
1238 hi2c
->XferCount
= Size
;
1241 hi2c
->XferSize
= 255;
1245 hi2c
->XferSize
= Size
;
1248 /* Set the I2C DMA transfer complete callback */
1249 hi2c
->hdmatx
->XferCpltCallback
= I2C_DMAMasterTransmitCplt
;
1251 /* Set the DMA error callback */
1252 hi2c
->hdmatx
->XferErrorCallback
= I2C_DMAError
;
1254 /* Enable the DMA channel */
1255 HAL_DMA_Start_IT(hi2c
->hdmatx
, (uint32_t)pData
, (uint32_t)&hi2c
->Instance
->TXDR
, hi2c
->XferSize
);
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
) )
1261 I2C_TransferConfig(hi2c
,DevAddress
,hi2c
->XferSize
, I2C_RELOAD_MODE
, I2C_GENERATE_START_WRITE
);
1265 I2C_TransferConfig(hi2c
,DevAddress
,hi2c
->XferSize
, I2C_AUTOEND_MODE
, I2C_GENERATE_START_WRITE
);
1268 /* Wait until TXIS flag is set */
1269 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c
, I2C_TIMEOUT_TXIS
) != HAL_OK
)
1271 /* Disable Address Acknowledge */
1272 hi2c
->Instance
->CR2
|= I2C_CR2_NACK
;
1274 if(hi2c
->ErrorCode
== HAL_I2C_ERROR_AF
)
1285 /* Enable DMA Request */
1286 hi2c
->Instance
->CR1
|= I2C_CR1_TXDMAEN
;
1288 /* Process Unlocked */
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
1308 HAL_StatusTypeDef
HAL_I2C_Master_Receive_DMA(I2C_HandleTypeDef
*hi2c
, uint16_t DevAddress
, uint8_t *pData
, uint16_t Size
)
1310 if(hi2c
->State
== HAL_I2C_STATE_READY
)
1312 if((pData
== NULL
) || (Size
== 0))
1317 if(__HAL_I2C_GET_FLAG(hi2c
, I2C_FLAG_BUSY
) == SET
)
1322 /* Process Locked */
1325 hi2c
->State
= HAL_I2C_STATE_MASTER_BUSY_RX
;
1326 hi2c
->ErrorCode
= HAL_I2C_ERROR_NONE
;
1328 hi2c
->pBuffPtr
= pData
;
1329 hi2c
->XferCount
= Size
;
1332 hi2c
->XferSize
= 255;
1336 hi2c
->XferSize
= Size
;
1339 /* Set the I2C DMA transfer complete callback */
1340 hi2c
->hdmarx
->XferCpltCallback
= I2C_DMAMasterReceiveCplt
;
1342 /* Set the DMA error callback */
1343 hi2c
->hdmarx
->XferErrorCallback
= I2C_DMAError
;
1345 /* Enable the DMA channel */
1346 HAL_DMA_Start_IT(hi2c
->hdmarx
, (uint32_t)&hi2c
->Instance
->RXDR
, (uint32_t)pData
, hi2c
->XferSize
);
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
) )
1352 I2C_TransferConfig(hi2c
,DevAddress
,hi2c
->XferSize
, I2C_RELOAD_MODE
, I2C_GENERATE_START_READ
);
1356 I2C_TransferConfig(hi2c
,DevAddress
,hi2c
->XferSize
, I2C_AUTOEND_MODE
, I2C_GENERATE_START_READ
);
1359 /* Wait until RXNE flag is set */
1360 if(I2C_WaitOnFlagUntilTimeout(hi2c
, I2C_FLAG_RXNE
, RESET
, I2C_TIMEOUT_RXNE
) != HAL_OK
)
1366 /* Enable DMA Request */
1367 hi2c
->Instance
->CR1
|= I2C_CR1_RXDMAEN
;
1369 /* Process Unlocked */
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
1388 HAL_StatusTypeDef
HAL_I2C_Slave_Transmit_DMA(I2C_HandleTypeDef
*hi2c
, uint8_t *pData
, uint16_t Size
)
1390 if(hi2c
->State
== HAL_I2C_STATE_READY
)
1392 if((pData
== NULL
) || (Size
== 0))
1396 /* Process Locked */
1399 hi2c
->State
= HAL_I2C_STATE_SLAVE_BUSY_TX
;
1400 hi2c
->ErrorCode
= HAL_I2C_ERROR_NONE
;
1402 hi2c
->pBuffPtr
= pData
;
1403 hi2c
->XferCount
= Size
;
1404 hi2c
->XferSize
= Size
;
1406 /* Set the I2C DMA transfer complete callback */
1407 hi2c
->hdmatx
->XferCpltCallback
= I2C_DMASlaveTransmitCplt
;
1409 /* Set the DMA error callback */
1410 hi2c
->hdmatx
->XferErrorCallback
= I2C_DMAError
;
1412 /* Enable the DMA channel */
1413 HAL_DMA_Start_IT(hi2c
->hdmatx
, (uint32_t)pData
, (uint32_t)&hi2c
->Instance
->TXDR
, hi2c
->XferSize
);
1415 /* Enable Address Acknowledge */
1416 hi2c
->Instance
->CR2
&= ~I2C_CR2_NACK
;
1418 /* Wait until ADDR flag is set */
1419 if(I2C_WaitOnFlagUntilTimeout(hi2c
, I2C_FLAG_ADDR
, RESET
, I2C_TIMEOUT_ADDR
) != HAL_OK
)
1421 /* Disable Address Acknowledge */
1422 hi2c
->Instance
->CR2
|= I2C_CR2_NACK
;
1426 /* Clear ADDR flag */
1427 __HAL_I2C_CLEAR_FLAG(hi2c
,I2C_FLAG_ADDR
);
1429 /* If 10bits addressing mode is selected */
1430 if(hi2c
->Init
.AddressingMode
== I2C_ADDRESSINGMODE_10BIT
)
1432 /* Wait until ADDR flag is set */
1433 if(I2C_WaitOnFlagUntilTimeout(hi2c
, I2C_FLAG_ADDR
, RESET
, I2C_TIMEOUT_ADDR
) != HAL_OK
)
1435 /* Disable Address Acknowledge */
1436 hi2c
->Instance
->CR2
|= I2C_CR2_NACK
;
1440 /* Clear ADDR flag */
1441 __HAL_I2C_CLEAR_FLAG(hi2c
,I2C_FLAG_ADDR
);
1444 /* Wait until DIR flag is set Transmitter mode */
1445 if(I2C_WaitOnFlagUntilTimeout(hi2c
, I2C_FLAG_DIR
, RESET
, I2C_TIMEOUT_BUSY
) != HAL_OK
)
1447 /* Disable Address Acknowledge */
1448 hi2c
->Instance
->CR2
|= I2C_CR2_NACK
;
1452 /* Enable DMA Request */
1453 hi2c
->Instance
->CR1
|= I2C_CR1_TXDMAEN
;
1455 /* Process Unlocked */
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
1474 HAL_StatusTypeDef
HAL_I2C_Slave_Receive_DMA(I2C_HandleTypeDef
*hi2c
, uint8_t *pData
, uint16_t Size
)
1476 if(hi2c
->State
== HAL_I2C_STATE_READY
)
1478 if((pData
== NULL
) || (Size
== 0))
1482 /* Process Locked */
1485 hi2c
->State
= HAL_I2C_STATE_SLAVE_BUSY_RX
;
1486 hi2c
->ErrorCode
= HAL_I2C_ERROR_NONE
;
1488 hi2c
->pBuffPtr
= pData
;
1489 hi2c
->XferSize
= Size
;
1490 hi2c
->XferCount
= Size
;
1492 /* Set the I2C DMA transfer complete callback */
1493 hi2c
->hdmarx
->XferCpltCallback
= I2C_DMASlaveReceiveCplt
;
1495 /* Set the DMA error callback */
1496 hi2c
->hdmarx
->XferErrorCallback
= I2C_DMAError
;
1498 /* Enable the DMA channel */
1499 HAL_DMA_Start_IT(hi2c
->hdmarx
, (uint32_t)&hi2c
->Instance
->RXDR
, (uint32_t)pData
, Size
);
1501 /* Enable Address Acknowledge */
1502 hi2c
->Instance
->CR2
&= ~I2C_CR2_NACK
;
1504 /* Wait until ADDR flag is set */
1505 if(I2C_WaitOnFlagUntilTimeout(hi2c
, I2C_FLAG_ADDR
, RESET
, I2C_TIMEOUT_ADDR
) != HAL_OK
)
1507 /* Disable Address Acknowledge */
1508 hi2c
->Instance
->CR2
|= I2C_CR2_NACK
;
1512 /* Clear ADDR flag */
1513 __HAL_I2C_CLEAR_FLAG(hi2c
,I2C_FLAG_ADDR
);
1515 /* Wait until DIR flag is set Receiver mode */
1516 if(I2C_WaitOnFlagUntilTimeout(hi2c
, I2C_FLAG_DIR
, SET
, I2C_TIMEOUT_DIR
) != HAL_OK
)
1518 /* Disable Address Acknowledge */
1519 hi2c
->Instance
->CR2
|= I2C_CR2_NACK
;
1523 /* Enable DMA Request */
1524 hi2c
->Instance
->CR1
|= I2C_CR1_RXDMAEN
;
1526 /* Process Unlocked */
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
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
)
1551 uint32_t Sizetmp
= 0;
1553 /* Check the parameters */
1554 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize
));
1556 if(hi2c
->State
== HAL_I2C_STATE_READY
)
1558 if((pData
== NULL
) || (Size
== 0))
1563 if(__HAL_I2C_GET_FLAG(hi2c
, I2C_FLAG_BUSY
) == SET
)
1568 /* Process Locked */
1571 hi2c
->State
= HAL_I2C_STATE_MEM_BUSY_TX
;
1572 hi2c
->ErrorCode
= HAL_I2C_ERROR_NONE
;
1574 /* Send Slave Address and Memory Address */
1575 if(I2C_RequestMemoryWrite(hi2c
, DevAddress
, MemAddress
, MemAddSize
, Timeout
) != HAL_OK
)
1577 if(hi2c
->ErrorCode
== HAL_I2C_ERROR_AF
)
1579 /* Process Unlocked */
1585 /* Process Unlocked */
1591 /* Set NBYTES to write and reload if size > 255 */
1592 /* Size > 255, need to set RELOAD bit */
1595 I2C_TransferConfig(hi2c
,DevAddress
,255, I2C_RELOAD_MODE
, I2C_NO_STARTSTOP
);
1600 I2C_TransferConfig(hi2c
,DevAddress
,Size
, I2C_AUTOEND_MODE
, I2C_NO_STARTSTOP
);
1606 /* Wait until TXIS flag is set */
1607 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c
, Timeout
) != HAL_OK
)
1609 if(hi2c
->ErrorCode
== HAL_I2C_ERROR_AF
)
1619 /* Write data to DR */
1620 hi2c
->Instance
->TXDR
= (*pData
++);
1624 if((Sizetmp
== 0)&&(Size
!=0))
1626 /* Wait until TCR flag is set */
1627 if(I2C_WaitOnFlagUntilTimeout(hi2c
, I2C_FLAG_TCR
, RESET
, Timeout
) != HAL_OK
)
1635 I2C_TransferConfig(hi2c
,DevAddress
,255, I2C_RELOAD_MODE
, I2C_NO_STARTSTOP
);
1640 I2C_TransferConfig(hi2c
,DevAddress
,Size
, I2C_AUTOEND_MODE
, I2C_NO_STARTSTOP
);
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
)
1651 if(hi2c
->ErrorCode
== HAL_I2C_ERROR_AF
)
1661 /* Clear STOP Flag */
1662 __HAL_I2C_CLEAR_FLAG(hi2c
, I2C_FLAG_STOPF
);
1664 /* Clear Configuration Register 2 */
1665 __HAL_I2C_RESET_CR2(hi2c
);
1667 hi2c
->State
= HAL_I2C_STATE_READY
;
1669 /* Process Unlocked */
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
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
)
1694 uint32_t Sizetmp
= 0;
1696 /* Check the parameters */
1697 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize
));
1699 if(hi2c
->State
== HAL_I2C_STATE_READY
)
1701 if((pData
== NULL
) || (Size
== 0))
1706 if(__HAL_I2C_GET_FLAG(hi2c
, I2C_FLAG_BUSY
) == SET
)
1711 /* Process Locked */
1714 hi2c
->State
= HAL_I2C_STATE_MEM_BUSY_RX
;
1715 hi2c
->ErrorCode
= HAL_I2C_ERROR_NONE
;
1717 /* Send Slave Address and Memory Address */
1718 if(I2C_RequestMemoryRead(hi2c
, DevAddress
, MemAddress
, MemAddSize
, Timeout
) != HAL_OK
)
1720 if(hi2c
->ErrorCode
== HAL_I2C_ERROR_AF
)
1722 /* Process Unlocked */
1728 /* Process Unlocked */
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 */
1739 I2C_TransferConfig(hi2c
,DevAddress
,255, I2C_RELOAD_MODE
, I2C_GENERATE_START_READ
);
1744 I2C_TransferConfig(hi2c
,DevAddress
,Size
, I2C_AUTOEND_MODE
, I2C_GENERATE_START_READ
);
1750 /* Wait until RXNE flag is set */
1751 if(I2C_WaitOnFlagUntilTimeout(hi2c
, I2C_FLAG_RXNE
, RESET
, Timeout
) != HAL_OK
)
1756 /* Read data from RXDR */
1757 (*pData
++) = hi2c
->Instance
->RXDR
;
1759 /* Decrement the Size counter */
1763 if((Sizetmp
== 0)&&(Size
!=0))
1765 /* Wait until TCR flag is set */
1766 if(I2C_WaitOnFlagUntilTimeout(hi2c
, I2C_FLAG_TCR
, RESET
, Timeout
) != HAL_OK
)
1773 I2C_TransferConfig(hi2c
,DevAddress
,255, I2C_RELOAD_MODE
, I2C_NO_STARTSTOP
);
1778 I2C_TransferConfig(hi2c
,DevAddress
,Size
, I2C_AUTOEND_MODE
, I2C_NO_STARTSTOP
);
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
)
1789 if(hi2c
->ErrorCode
== HAL_I2C_ERROR_AF
)
1799 /* Clear STOP Flag */
1800 __HAL_I2C_CLEAR_FLAG(hi2c
, I2C_FLAG_STOPF
);
1802 /* Clear Configuration Register 2 */
1803 __HAL_I2C_RESET_CR2(hi2c
);
1805 hi2c
->State
= HAL_I2C_STATE_READY
;
1807 /* Process Unlocked */
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
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
)
1831 /* Check the parameters */
1832 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize
));
1834 if(hi2c
->State
== HAL_I2C_STATE_READY
)
1836 if((pData
== NULL
) || (Size
== 0))
1841 if(__HAL_I2C_GET_FLAG(hi2c
, I2C_FLAG_BUSY
) == SET
)
1846 /* Process Locked */
1849 hi2c
->State
= HAL_I2C_STATE_MEM_BUSY_TX
;
1850 hi2c
->ErrorCode
= HAL_I2C_ERROR_NONE
;
1852 hi2c
->pBuffPtr
= pData
;
1853 hi2c
->XferCount
= Size
;
1856 hi2c
->XferSize
= 255;
1860 hi2c
->XferSize
= Size
;
1863 /* Send Slave Address and Memory Address */
1864 if(I2C_RequestMemoryWrite(hi2c
, DevAddress
, MemAddress
, MemAddSize
, I2C_TIMEOUT_FLAG
) != HAL_OK
)
1866 if(hi2c
->ErrorCode
== HAL_I2C_ERROR_AF
)
1868 /* Process Unlocked */
1874 /* Process Unlocked */
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
) )
1884 I2C_TransferConfig(hi2c
,DevAddress
,hi2c
->XferSize
, I2C_RELOAD_MODE
, I2C_NO_STARTSTOP
);
1888 I2C_TransferConfig(hi2c
,DevAddress
,hi2c
->XferSize
, I2C_AUTOEND_MODE
, I2C_NO_STARTSTOP
);
1891 /* Process Unlocked */
1894 /* Note : The I2C interrupts must be enabled after unlocking current process
1895 to avoid the risk of I2C interrupt handle execution before current
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
);
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
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
)
1924 /* Check the parameters */
1925 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize
));
1927 if(hi2c
->State
== HAL_I2C_STATE_READY
)
1929 if((pData
== NULL
) || (Size
== 0))
1934 if(__HAL_I2C_GET_FLAG(hi2c
, I2C_FLAG_BUSY
) == SET
)
1939 /* Process Locked */
1942 hi2c
->State
= HAL_I2C_STATE_MEM_BUSY_RX
;
1944 hi2c
->pBuffPtr
= pData
;
1945 hi2c
->XferCount
= Size
;
1948 hi2c
->XferSize
= 255;
1952 hi2c
->XferSize
= Size
;
1955 /* Send Slave Address and Memory Address */
1956 if(I2C_RequestMemoryRead(hi2c
, DevAddress
, MemAddress
, MemAddSize
, I2C_TIMEOUT_FLAG
) != HAL_OK
)
1958 if(hi2c
->ErrorCode
== HAL_I2C_ERROR_AF
)
1960 /* Process Unlocked */
1966 /* Process Unlocked */
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
) )
1976 I2C_TransferConfig(hi2c
,DevAddress
,hi2c
->XferSize
, I2C_RELOAD_MODE
, I2C_GENERATE_START_READ
);
1980 I2C_TransferConfig(hi2c
,DevAddress
,hi2c
->XferSize
, I2C_AUTOEND_MODE
, I2C_GENERATE_START_READ
);
1983 /* Process Unlocked */
1986 /* Note : The I2C interrupts must be enabled after unlocking current process
1987 to avoid the risk of I2C interrupt handle execution before current
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
);
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
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
)
2016 /* Check the parameters */
2017 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize
));
2019 if(hi2c
->State
== HAL_I2C_STATE_READY
)
2021 if((pData
== NULL
) || (Size
== 0))
2026 if(__HAL_I2C_GET_FLAG(hi2c
, I2C_FLAG_BUSY
) == SET
)
2031 /* Process Locked */
2034 hi2c
->State
= HAL_I2C_STATE_MEM_BUSY_TX
;
2035 hi2c
->ErrorCode
= HAL_I2C_ERROR_NONE
;
2037 hi2c
->pBuffPtr
= pData
;
2038 hi2c
->XferCount
= Size
;
2041 hi2c
->XferSize
= 255;
2045 hi2c
->XferSize
= Size
;
2048 /* Set the I2C DMA transfer complete callback */
2049 hi2c
->hdmatx
->XferCpltCallback
= I2C_DMAMemTransmitCplt
;
2051 /* Set the DMA error callback */
2052 hi2c
->hdmatx
->XferErrorCallback
= I2C_DMAError
;
2054 /* Enable the DMA channel */
2055 HAL_DMA_Start_IT(hi2c
->hdmatx
, (uint32_t)pData
, (uint32_t)&hi2c
->Instance
->TXDR
, hi2c
->XferSize
);
2057 /* Send Slave Address and Memory Address */
2058 if(I2C_RequestMemoryWrite(hi2c
, DevAddress
, MemAddress
, MemAddSize
, I2C_TIMEOUT_FLAG
) != HAL_OK
)
2060 if(hi2c
->ErrorCode
== HAL_I2C_ERROR_AF
)
2062 /* Process Unlocked */
2068 /* Process Unlocked */
2074 /* Send Slave Address */
2075 /* Set NBYTES to write and reload if size > 255 */
2076 if( (hi2c
->XferSize
== 255) && (hi2c
->XferSize
< hi2c
->XferCount
) )
2078 I2C_TransferConfig(hi2c
,DevAddress
,hi2c
->XferSize
, I2C_RELOAD_MODE
, I2C_NO_STARTSTOP
);
2082 I2C_TransferConfig(hi2c
,DevAddress
,hi2c
->XferSize
, I2C_AUTOEND_MODE
, I2C_NO_STARTSTOP
);
2085 /* Wait until TXIS flag is set */
2086 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c
, I2C_TIMEOUT_TXIS
) != HAL_OK
)
2088 if(hi2c
->ErrorCode
== HAL_I2C_ERROR_AF
)
2098 /* Enable DMA Request */
2099 hi2c
->Instance
->CR1
|= I2C_CR1_TXDMAEN
;
2101 /* Process Unlocked */
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
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
)
2125 /* Check the parameters */
2126 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize
));
2128 if(hi2c
->State
== HAL_I2C_STATE_READY
)
2130 if((pData
== NULL
) || (Size
== 0))
2135 if(__HAL_I2C_GET_FLAG(hi2c
, I2C_FLAG_BUSY
) == SET
)
2140 /* Process Locked */
2143 hi2c
->State
= HAL_I2C_STATE_MEM_BUSY_RX
;
2145 hi2c
->pBuffPtr
= pData
;
2146 hi2c
->XferCount
= Size
;
2149 hi2c
->XferSize
= 255;
2153 hi2c
->XferSize
= Size
;
2156 /* Set the I2C DMA transfer complete callback */
2157 hi2c
->hdmarx
->XferCpltCallback
= I2C_DMAMemReceiveCplt
;
2159 /* Set the DMA error callback */
2160 hi2c
->hdmarx
->XferErrorCallback
= I2C_DMAError
;
2162 /* Enable the DMA channel */
2163 HAL_DMA_Start_IT(hi2c
->hdmarx
, (uint32_t)&hi2c
->Instance
->RXDR
, (uint32_t)pData
, hi2c
->XferSize
);
2165 /* Send Slave Address and Memory Address */
2166 if(I2C_RequestMemoryRead(hi2c
, DevAddress
, MemAddress
, MemAddSize
, I2C_TIMEOUT_FLAG
) != HAL_OK
)
2168 if(hi2c
->ErrorCode
== HAL_I2C_ERROR_AF
)
2170 /* Process Unlocked */
2176 /* Process Unlocked */
2182 /* Set NBYTES to write and reload if size > 255 and generate RESTART */
2183 if( (hi2c
->XferSize
== 255) && (hi2c
->XferSize
< hi2c
->XferCount
) )
2185 I2C_TransferConfig(hi2c
,DevAddress
,hi2c
->XferSize
, I2C_RELOAD_MODE
, I2C_GENERATE_START_READ
);
2189 I2C_TransferConfig(hi2c
,DevAddress
,hi2c
->XferSize
, I2C_AUTOEND_MODE
, I2C_GENERATE_START_READ
);
2192 /* Wait until RXNE flag is set */
2193 if(I2C_WaitOnFlagUntilTimeout(hi2c
, I2C_FLAG_RXNE
, RESET
, I2C_TIMEOUT_RXNE
) != HAL_OK
)
2198 /* Enable DMA Request */
2199 hi2c
->Instance
->CR1
|= I2C_CR1_RXDMAEN
;
2201 /* Process Unlocked */
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
2222 HAL_StatusTypeDef
HAL_I2C_IsDeviceReady(I2C_HandleTypeDef
*hi2c
, uint16_t DevAddress
, uint32_t Trials
, uint32_t Timeout
)
2224 uint32_t tickstart
= 0;
2226 __IO
uint32_t I2C_Trials
= 0;
2228 if(hi2c
->State
== HAL_I2C_STATE_READY
)
2230 if(__HAL_I2C_GET_FLAG(hi2c
, I2C_FLAG_BUSY
) == SET
)
2235 /* Process Locked */
2238 hi2c
->State
= HAL_I2C_STATE_BUSY
;
2239 hi2c
->ErrorCode
= HAL_I2C_ERROR_NONE
;
2243 /* Generate Start */
2244 hi2c
->Instance
->CR2
= __HAL_I2C_GENERATE_START(hi2c
->Init
.AddressingMode
,DevAddress
);
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
))
2251 if(Timeout
!= HAL_MAX_DELAY
)
2253 if((Timeout
== 0) || ((HAL_GetTick() - tickstart
) > Timeout
))
2255 /* Device is ready */
2256 hi2c
->State
= HAL_I2C_STATE_READY
;
2257 /* Process Unlocked */
2264 /* Check if the NACKF flag has not been set */
2265 if (__HAL_I2C_GET_FLAG(hi2c
, I2C_FLAG_AF
) == RESET
)
2267 /* Wait until STOPF flag is reset */
2268 if(I2C_WaitOnFlagUntilTimeout(hi2c
, I2C_FLAG_STOPF
, RESET
, Timeout
) != HAL_OK
)
2273 /* Clear STOP Flag */
2274 __HAL_I2C_CLEAR_FLAG(hi2c
, I2C_FLAG_STOPF
);
2276 /* Device is ready */
2277 hi2c
->State
= HAL_I2C_STATE_READY
;
2279 /* Process Unlocked */
2286 /* Wait until STOPF flag is reset */
2287 if(I2C_WaitOnFlagUntilTimeout(hi2c
, I2C_FLAG_STOPF
, RESET
, Timeout
) != HAL_OK
)
2292 /* Clear NACK Flag */
2293 __HAL_I2C_CLEAR_FLAG(hi2c
, I2C_FLAG_AF
);
2295 /* Clear STOP Flag, auto generated with autoend*/
2296 __HAL_I2C_CLEAR_FLAG(hi2c
, I2C_FLAG_STOPF
);
2299 /* Check if the maximum allowed number of trials has been reached */
2300 if (I2C_Trials
++ == Trials
)
2303 hi2c
->Instance
->CR2
|= I2C_CR2_STOP
;
2305 /* Wait until STOPF flag is reset */
2306 if(I2C_WaitOnFlagUntilTimeout(hi2c
, I2C_FLAG_STOPF
, RESET
, Timeout
) != HAL_OK
)
2311 /* Clear STOP Flag */
2312 __HAL_I2C_CLEAR_FLAG(hi2c
, I2C_FLAG_STOPF
);
2314 }while(I2C_Trials
< Trials
);
2316 hi2c
->State
= HAL_I2C_STATE_READY
;
2318 /* Process Unlocked */
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.
2335 void HAL_I2C_EV_IRQHandler(I2C_HandleTypeDef
*hi2c
)
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
))
2340 /* Slave mode selected */
2341 if (hi2c
->State
== HAL_I2C_STATE_SLAVE_BUSY_TX
)
2343 I2C_SlaveTransmit_ISR(hi2c
);
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
))
2349 /* Master mode selected */
2350 if ((hi2c
->State
== HAL_I2C_STATE_MASTER_BUSY_TX
) || (hi2c
->State
== HAL_I2C_STATE_MEM_BUSY_TX
))
2352 I2C_MasterTransmit_ISR(hi2c
);
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
))
2359 /* Slave mode selected */
2360 if (hi2c
->State
== HAL_I2C_STATE_SLAVE_BUSY_RX
)
2362 I2C_SlaveReceive_ISR(hi2c
);
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
))
2367 /* Master mode selected */
2368 if ((hi2c
->State
== HAL_I2C_STATE_MASTER_BUSY_RX
) || (hi2c
->State
== HAL_I2C_STATE_MEM_BUSY_RX
))
2370 I2C_MasterReceive_ISR(hi2c
);
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.
2381 void HAL_I2C_ER_IRQHandler(I2C_HandleTypeDef
*hi2c
)
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
))
2386 hi2c
->ErrorCode
|= HAL_I2C_ERROR_BERR
;
2388 /* Clear BERR flag */
2389 __HAL_I2C_CLEAR_FLAG(hi2c
, I2C_FLAG_BERR
);
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
))
2395 hi2c
->ErrorCode
|= HAL_I2C_ERROR_OVR
;
2397 /* Clear OVR flag */
2398 __HAL_I2C_CLEAR_FLAG(hi2c
, I2C_FLAG_OVR
);
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
))
2404 hi2c
->ErrorCode
|= HAL_I2C_ERROR_ARLO
;
2406 /* Clear ARLO flag */
2407 __HAL_I2C_CLEAR_FLAG(hi2c
, I2C_FLAG_ARLO
);
2410 /* Call the Error Callback in case of Error detected */
2411 if(hi2c
->ErrorCode
!= HAL_I2C_ERROR_NONE
)
2413 hi2c
->State
= HAL_I2C_STATE_READY
;
2415 HAL_I2C_ErrorCallback(hi2c
);
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.
2425 __weak
void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef
*hi2c
)
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
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.
2438 __weak
void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef
*hi2c
)
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
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.
2450 __weak
void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef
*hi2c
)
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
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.
2463 __weak
void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef
*hi2c
)
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
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.
2476 __weak
void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef
*hi2c
)
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
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.
2489 __weak
void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef
*hi2c
)
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
2497 * @brief I2C error callbacks.
2498 * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
2499 * the configuration information for the specified I2C.
2502 __weak
void HAL_I2C_ErrorCallback(I2C_HandleTypeDef
*hi2c
)
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
2513 /** @defgroup I2C_Exported_Functions_Group3 Peripheral State and Errors functions
2514 * @brief Peripheral State and Errors functions
2517 ===============================================================================
2518 ##### Peripheral State and Errors functions #####
2519 ===============================================================================
2521 This subsection permit to get in run-time the status of the peripheral
2529 * @brief Returns the I2C state.
2530 * @param hi2c : I2C handle
2533 HAL_I2C_StateTypeDef
HAL_I2C_GetState(I2C_HandleTypeDef
*hi2c
)
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
2544 uint32_t HAL_I2C_GetError(I2C_HandleTypeDef
*hi2c
)
2546 return hi2c
->ErrorCode
;
2557 /** @addtogroup I2C_Private_Functions
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
2567 static HAL_StatusTypeDef
I2C_MasterTransmit_ISR(I2C_HandleTypeDef
*hi2c
)
2569 uint16_t DevAddress
;
2571 /* Process Locked */
2574 if(__HAL_I2C_GET_FLAG(hi2c
, I2C_FLAG_TXIS
) == SET
)
2576 /* Write data to TXDR */
2577 hi2c
->Instance
->TXDR
= (*hi2c
->pBuffPtr
++);
2581 else if(__HAL_I2C_GET_FLAG(hi2c
, I2C_FLAG_TCR
) == SET
)
2583 if((hi2c
->XferSize
== 0)&&(hi2c
->XferCount
!=0))
2585 DevAddress
= (hi2c
->Instance
->CR2
& I2C_CR2_SADD
);
2587 if(hi2c
->XferCount
> 255)
2589 I2C_TransferConfig(hi2c
,DevAddress
,255, I2C_RELOAD_MODE
, I2C_NO_STARTSTOP
);
2590 hi2c
->XferSize
= 255;
2594 I2C_TransferConfig(hi2c
,DevAddress
,hi2c
->XferCount
, I2C_AUTOEND_MODE
, I2C_NO_STARTSTOP
);
2595 hi2c
->XferSize
= hi2c
->XferCount
;
2600 /* Process Unlocked */
2603 /* Wrong size Status regarding TCR flag event */
2604 hi2c
->ErrorCode
|= HAL_I2C_ERROR_SIZE
;
2605 HAL_I2C_ErrorCallback(hi2c
);
2608 else if(__HAL_I2C_GET_FLAG(hi2c
, I2C_FLAG_TC
) == SET
)
2610 if(hi2c
->XferCount
== 0)
2613 hi2c
->Instance
->CR2
|= I2C_CR2_STOP
;
2617 /* Process Unlocked */
2620 /* Wrong size Status regarding TCR flag event */
2621 hi2c
->ErrorCode
|= HAL_I2C_ERROR_SIZE
;
2622 HAL_I2C_ErrorCallback(hi2c
);
2625 else if(__HAL_I2C_GET_FLAG(hi2c
, I2C_FLAG_STOPF
) == SET
)
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
);
2630 /* Clear STOP Flag */
2631 __HAL_I2C_CLEAR_FLAG(hi2c
, I2C_FLAG_STOPF
);
2633 /* Clear Configuration Register 2 */
2634 __HAL_I2C_RESET_CR2(hi2c
);
2636 hi2c
->State
= HAL_I2C_STATE_READY
;
2638 /* Process Unlocked */
2641 if(hi2c
->State
== HAL_I2C_STATE_MEM_BUSY_TX
)
2643 HAL_I2C_MemTxCpltCallback(hi2c
);
2647 HAL_I2C_MasterTxCpltCallback(hi2c
);
2650 else if(__HAL_I2C_GET_FLAG(hi2c
, I2C_FLAG_AF
) == SET
)
2652 /* Clear NACK Flag */
2653 __HAL_I2C_CLEAR_FLAG(hi2c
, I2C_FLAG_AF
);
2655 /* Process Unlocked */
2658 hi2c
->ErrorCode
|= HAL_I2C_ERROR_AF
;
2659 HAL_I2C_ErrorCallback(hi2c
);
2662 /* Process Unlocked */
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
2674 static HAL_StatusTypeDef
I2C_MasterReceive_ISR(I2C_HandleTypeDef
*hi2c
)
2676 uint16_t DevAddress
;
2678 /* Process Locked */
2681 if(__HAL_I2C_GET_FLAG(hi2c
, I2C_FLAG_RXNE
) == SET
)
2683 /* Read data from RXDR */
2684 (*hi2c
->pBuffPtr
++) = hi2c
->Instance
->RXDR
;
2688 else if(__HAL_I2C_GET_FLAG(hi2c
, I2C_FLAG_TCR
) == SET
)
2690 if((hi2c
->XferSize
== 0)&&(hi2c
->XferCount
!=0))
2692 DevAddress
= (hi2c
->Instance
->CR2
& I2C_CR2_SADD
);
2694 if(hi2c
->XferCount
> 255)
2696 I2C_TransferConfig(hi2c
,DevAddress
,255, I2C_RELOAD_MODE
, I2C_NO_STARTSTOP
);
2697 hi2c
->XferSize
= 255;
2701 I2C_TransferConfig(hi2c
,DevAddress
,hi2c
->XferCount
, I2C_AUTOEND_MODE
, I2C_NO_STARTSTOP
);
2702 hi2c
->XferSize
= hi2c
->XferCount
;
2707 /* Process Unlocked */
2710 /* Wrong size Status regarding TCR flag event */
2711 hi2c
->ErrorCode
|= HAL_I2C_ERROR_SIZE
;
2712 HAL_I2C_ErrorCallback(hi2c
);
2715 else if(__HAL_I2C_GET_FLAG(hi2c
, I2C_FLAG_TC
) == SET
)
2717 if(hi2c
->XferCount
== 0)
2720 hi2c
->Instance
->CR2
|= I2C_CR2_STOP
;
2724 /* Process Unlocked */
2727 /* Wrong size Status regarding TCR flag event */
2728 hi2c
->ErrorCode
|= HAL_I2C_ERROR_SIZE
;
2729 HAL_I2C_ErrorCallback(hi2c
);
2732 else if(__HAL_I2C_GET_FLAG(hi2c
, I2C_FLAG_STOPF
) == SET
)
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
);
2737 /* Clear STOP Flag */
2738 __HAL_I2C_CLEAR_FLAG(hi2c
, I2C_FLAG_STOPF
);
2740 /* Clear Configuration Register 2 */
2741 __HAL_I2C_RESET_CR2(hi2c
);
2743 hi2c
->State
= HAL_I2C_STATE_READY
;
2745 /* Process Unlocked */
2748 if(hi2c
->State
== HAL_I2C_STATE_MEM_BUSY_RX
)
2750 HAL_I2C_MemRxCpltCallback(hi2c
);
2754 HAL_I2C_MasterRxCpltCallback(hi2c
);
2757 else if(__HAL_I2C_GET_FLAG(hi2c
, I2C_FLAG_AF
) == SET
)
2759 /* Clear NACK Flag */
2760 __HAL_I2C_CLEAR_FLAG(hi2c
, I2C_FLAG_AF
);
2762 /* Process Unlocked */
2765 hi2c
->ErrorCode
|= HAL_I2C_ERROR_AF
;
2766 HAL_I2C_ErrorCallback(hi2c
);
2769 /* Process Unlocked */
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
2782 static HAL_StatusTypeDef
I2C_SlaveTransmit_ISR(I2C_HandleTypeDef
*hi2c
)
2784 /* Process locked */
2787 if(__HAL_I2C_GET_FLAG(hi2c
, I2C_FLAG_AF
) != RESET
)
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)
2795 /* Clear NACK Flag */
2796 __HAL_I2C_CLEAR_FLAG(hi2c
, I2C_FLAG_AF
);
2798 /* Process Unlocked */
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
);
2807 /* Set ErrorCode corresponding to a Non-Acknowledge */
2808 hi2c
->ErrorCode
|= HAL_I2C_ERROR_AF
;
2810 /* Process Unlocked */
2813 /* Call the Error callback to prevent upper layer */
2814 HAL_I2C_ErrorCallback(hi2c
);
2817 else if(__HAL_I2C_GET_FLAG(hi2c
, I2C_FLAG_ADDR
) == SET
)
2819 /* Clear ADDR flag */
2820 __HAL_I2C_CLEAR_FLAG(hi2c
, I2C_FLAG_ADDR
);
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
)
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
);
2831 /* Disable Address Acknowledge */
2832 hi2c
->Instance
->CR2
|= I2C_CR2_NACK
;
2834 /* Clear STOP Flag */
2835 __HAL_I2C_CLEAR_FLAG(hi2c
, I2C_FLAG_STOPF
);
2837 hi2c
->State
= HAL_I2C_STATE_READY
;
2839 /* Process Unlocked */
2842 HAL_I2C_SlaveTxCpltCallback(hi2c
);
2844 else if(__HAL_I2C_GET_FLAG(hi2c
, I2C_FLAG_TXIS
) == SET
)
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)
2850 /* Write data to TXDR */
2851 hi2c
->Instance
->TXDR
= (*hi2c
->pBuffPtr
++);
2856 /* Process Unlocked */
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
2868 static HAL_StatusTypeDef
I2C_SlaveReceive_ISR(I2C_HandleTypeDef
*hi2c
)
2870 /* Process Locked */
2873 if(__HAL_I2C_GET_FLAG(hi2c
, I2C_FLAG_AF
) != RESET
)
2875 /* Clear NACK Flag */
2876 __HAL_I2C_CLEAR_FLAG(hi2c
, I2C_FLAG_AF
);
2878 /* Process Unlocked */
2881 hi2c
->ErrorCode
|= HAL_I2C_ERROR_AF
;
2882 HAL_I2C_ErrorCallback(hi2c
);
2884 else if(__HAL_I2C_GET_FLAG(hi2c
, I2C_FLAG_ADDR
) == SET
)
2886 /* Clear ADDR flag */
2887 __HAL_I2C_CLEAR_FLAG(hi2c
, I2C_FLAG_ADDR
);
2889 else if(__HAL_I2C_GET_FLAG(hi2c
, I2C_FLAG_RXNE
) == SET
)
2891 /* Read data from RXDR */
2892 (*hi2c
->pBuffPtr
++) = hi2c
->Instance
->RXDR
;
2896 else if(__HAL_I2C_GET_FLAG(hi2c
, I2C_FLAG_STOPF
) == SET
)
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
);
2901 /* Disable Address Acknowledge */
2902 hi2c
->Instance
->CR2
|= I2C_CR2_NACK
;
2904 /* Clear STOP Flag */
2905 __HAL_I2C_CLEAR_FLAG(hi2c
, I2C_FLAG_STOPF
);
2907 hi2c
->State
= HAL_I2C_STATE_READY
;
2909 /* Process Unlocked */
2912 HAL_I2C_SlaveRxCpltCallback(hi2c
);
2915 /* Process Unlocked */
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
2931 static HAL_StatusTypeDef
I2C_RequestMemoryWrite(I2C_HandleTypeDef
*hi2c
, uint16_t DevAddress
, uint16_t MemAddress
, uint16_t MemAddSize
, uint32_t Timeout
)
2933 I2C_TransferConfig(hi2c
,DevAddress
,MemAddSize
, I2C_RELOAD_MODE
, I2C_GENERATE_START_WRITE
);
2935 /* Wait until TXIS flag is set */
2936 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c
, Timeout
) != HAL_OK
)
2938 if(hi2c
->ErrorCode
== HAL_I2C_ERROR_AF
)
2948 /* If Memory address size is 8Bit */
2949 if(MemAddSize
== I2C_MEMADD_SIZE_8BIT
)
2951 /* Send Memory Address */
2952 hi2c
->Instance
->TXDR
= __HAL_I2C_MEM_ADD_LSB(MemAddress
);
2954 /* If Memory address size is 16Bit */
2957 /* Send MSB of Memory Address */
2958 hi2c
->Instance
->TXDR
= __HAL_I2C_MEM_ADD_MSB(MemAddress
);
2960 /* Wait until TXIS flag is set */
2961 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c
, Timeout
) != HAL_OK
)
2963 if(hi2c
->ErrorCode
== HAL_I2C_ERROR_AF
)
2973 /* Send LSB of Memory Address */
2974 hi2c
->Instance
->TXDR
= __HAL_I2C_MEM_ADD_LSB(MemAddress
);
2977 /* Wait until TCR flag is set */
2978 if(I2C_WaitOnFlagUntilTimeout(hi2c
, I2C_FLAG_TCR
, RESET
, Timeout
) != HAL_OK
)
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
2996 static HAL_StatusTypeDef
I2C_RequestMemoryRead(I2C_HandleTypeDef
*hi2c
, uint16_t DevAddress
, uint16_t MemAddress
, uint16_t MemAddSize
, uint32_t Timeout
)
2998 I2C_TransferConfig(hi2c
,DevAddress
,MemAddSize
, I2C_SOFTEND_MODE
, I2C_GENERATE_START_WRITE
);
3000 /* Wait until TXIS flag is set */
3001 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c
, Timeout
) != HAL_OK
)
3003 if(hi2c
->ErrorCode
== HAL_I2C_ERROR_AF
)
3013 /* If Memory address size is 8Bit */
3014 if(MemAddSize
== I2C_MEMADD_SIZE_8BIT
)
3016 /* Send Memory Address */
3017 hi2c
->Instance
->TXDR
= __HAL_I2C_MEM_ADD_LSB(MemAddress
);
3019 /* If Mememory address size is 16Bit */
3022 /* Send MSB of Memory Address */
3023 hi2c
->Instance
->TXDR
= __HAL_I2C_MEM_ADD_MSB(MemAddress
);
3025 /* Wait until TXIS flag is set */
3026 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c
, Timeout
) != HAL_OK
)
3028 if(hi2c
->ErrorCode
== HAL_I2C_ERROR_AF
)
3038 /* Send LSB of Memory Address */
3039 hi2c
->Instance
->TXDR
= __HAL_I2C_MEM_ADD_LSB(MemAddress
);
3042 /* Wait until TC flag is set */
3043 if(I2C_WaitOnFlagUntilTimeout(hi2c
, I2C_FLAG_TC
, RESET
, Timeout
) != HAL_OK
)
3052 * @brief DMA I2C master transmit process complete callback.
3053 * @param hdma: DMA handle
3056 static void I2C_DMAMasterTransmitCplt(DMA_HandleTypeDef
*hdma
)
3058 uint16_t DevAddress
;
3059 I2C_HandleTypeDef
* hi2c
= (I2C_HandleTypeDef
*)((DMA_HandleTypeDef
*)hdma
)->Parent
;
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
) )
3065 /* Wait until TCR flag is set */
3066 if(I2C_WaitOnFlagUntilTimeout(hi2c
, I2C_FLAG_TCR
, RESET
, I2C_TIMEOUT_TCR
) != HAL_OK
)
3068 hi2c
->ErrorCode
|= HAL_I2C_ERROR_TIMEOUT
;
3071 /* Disable DMA Request */
3072 hi2c
->Instance
->CR1
&= ~I2C_CR1_TXDMAEN
;
3074 /* Check if Errors has been detected during transfer */
3075 if(hi2c
->ErrorCode
!= HAL_I2C_ERROR_NONE
)
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
)
3081 if(hi2c
->ErrorCode
== HAL_I2C_ERROR_AF
)
3083 hi2c
->ErrorCode
|= HAL_I2C_ERROR_AF
;
3087 hi2c
->ErrorCode
|= HAL_I2C_ERROR_TIMEOUT
;
3091 /* Clear STOP Flag */
3092 __HAL_I2C_CLEAR_FLAG(hi2c
, I2C_FLAG_STOPF
);
3094 /* Clear Configuration Register 2 */
3095 __HAL_I2C_RESET_CR2(hi2c
);
3097 hi2c
->XferCount
= 0;
3099 hi2c
->State
= HAL_I2C_STATE_READY
;
3100 HAL_I2C_ErrorCallback(hi2c
);
3104 hi2c
->pBuffPtr
+= hi2c
->XferSize
;
3105 hi2c
->XferCount
-= hi2c
->XferSize
;
3106 if(hi2c
->XferCount
> 255)
3108 hi2c
->XferSize
= 255;
3112 hi2c
->XferSize
= hi2c
->XferCount
;
3115 DevAddress
= (hi2c
->Instance
->CR2
& I2C_CR2_SADD
);
3117 /* Enable the DMA channel */
3118 HAL_DMA_Start_IT(hi2c
->hdmatx
, (uint32_t)hi2c
->pBuffPtr
, (uint32_t)&hi2c
->Instance
->TXDR
, hi2c
->XferSize
);
3120 /* Send Slave Address */
3121 /* Set NBYTES to write and reload if size > 255 */
3122 if( (hi2c
->XferSize
== 255) && (hi2c
->XferSize
< hi2c
->XferCount
) )
3124 I2C_TransferConfig(hi2c
,DevAddress
,hi2c
->XferSize
, I2C_RELOAD_MODE
, I2C_NO_STARTSTOP
);
3128 I2C_TransferConfig(hi2c
,DevAddress
,hi2c
->XferSize
, I2C_AUTOEND_MODE
, I2C_NO_STARTSTOP
);
3131 /* Wait until TXIS flag is set */
3132 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c
, I2C_TIMEOUT_TXIS
) != HAL_OK
)
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
)
3138 if(hi2c
->ErrorCode
== HAL_I2C_ERROR_AF
)
3140 hi2c
->ErrorCode
|= HAL_I2C_ERROR_AF
;
3144 hi2c
->ErrorCode
|= HAL_I2C_ERROR_TIMEOUT
;
3148 /* Clear STOP Flag */
3149 __HAL_I2C_CLEAR_FLAG(hi2c
, I2C_FLAG_STOPF
);
3151 /* Clear Configuration Register 2 */
3152 __HAL_I2C_RESET_CR2(hi2c
);
3154 hi2c
->XferCount
= 0;
3156 hi2c
->State
= HAL_I2C_STATE_READY
;
3157 HAL_I2C_ErrorCallback(hi2c
);
3161 /* Enable DMA Request */
3162 hi2c
->Instance
->CR1
|= I2C_CR1_TXDMAEN
;
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
)
3172 if(hi2c
->ErrorCode
== HAL_I2C_ERROR_AF
)
3174 hi2c
->ErrorCode
|= HAL_I2C_ERROR_AF
;
3178 hi2c
->ErrorCode
|= HAL_I2C_ERROR_TIMEOUT
;
3182 /* Clear STOP Flag */
3183 __HAL_I2C_CLEAR_FLAG(hi2c
, I2C_FLAG_STOPF
);
3185 /* Clear Configuration Register 2 */
3186 __HAL_I2C_RESET_CR2(hi2c
);
3188 /* Disable DMA Request */
3189 hi2c
->Instance
->CR1
&= ~I2C_CR1_TXDMAEN
;
3191 hi2c
->XferCount
= 0;
3193 hi2c
->State
= HAL_I2C_STATE_READY
;
3195 /* Check if Errors has been detected during transfer */
3196 if(hi2c
->ErrorCode
!= HAL_I2C_ERROR_NONE
)
3198 HAL_I2C_ErrorCallback(hi2c
);
3202 HAL_I2C_MasterTxCpltCallback(hi2c
);
3208 * @brief DMA I2C slave transmit process complete callback.
3209 * @param hdma: DMA handle
3212 static void I2C_DMASlaveTransmitCplt(DMA_HandleTypeDef
*hdma
)
3214 I2C_HandleTypeDef
* hi2c
= (I2C_HandleTypeDef
*)((DMA_HandleTypeDef
*)hdma
)->Parent
;
3216 /* Wait until STOP flag is set */
3217 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c
, I2C_TIMEOUT_STOPF
) != HAL_OK
)
3219 if(hi2c
->ErrorCode
== HAL_I2C_ERROR_AF
)
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
;
3227 hi2c
->ErrorCode
|= HAL_I2C_ERROR_TIMEOUT
;
3231 /* Clear STOP flag */
3232 __HAL_I2C_CLEAR_FLAG(hi2c
,I2C_FLAG_STOPF
);
3234 /* Wait until BUSY flag is reset */
3235 if(I2C_WaitOnFlagUntilTimeout(hi2c
, I2C_FLAG_BUSY
, SET
, I2C_TIMEOUT_BUSY
) != HAL_OK
)
3237 hi2c
->ErrorCode
|= HAL_I2C_ERROR_TIMEOUT
;
3240 /* Disable DMA Request */
3241 hi2c
->Instance
->CR1
&= ~I2C_CR1_TXDMAEN
;
3243 hi2c
->XferCount
= 0;
3245 hi2c
->State
= HAL_I2C_STATE_READY
;
3247 /* Check if Errors has been detected during transfer */
3248 if(hi2c
->ErrorCode
!= HAL_I2C_ERROR_NONE
)
3250 HAL_I2C_ErrorCallback(hi2c
);
3254 HAL_I2C_SlaveTxCpltCallback(hi2c
);
3259 * @brief DMA I2C master receive process complete callback
3260 * @param hdma: DMA handle
3263 static void I2C_DMAMasterReceiveCplt(DMA_HandleTypeDef
*hdma
)
3265 I2C_HandleTypeDef
* hi2c
= (I2C_HandleTypeDef
*)((DMA_HandleTypeDef
*)hdma
)->Parent
;
3266 uint16_t DevAddress
;
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
) )
3272 /* Wait until TCR flag is set */
3273 if(I2C_WaitOnFlagUntilTimeout(hi2c
, I2C_FLAG_TCR
, RESET
, I2C_TIMEOUT_TCR
) != HAL_OK
)
3275 hi2c
->ErrorCode
|= HAL_I2C_ERROR_TIMEOUT
;
3278 /* Disable DMA Request */
3279 hi2c
->Instance
->CR1
&= ~I2C_CR1_RXDMAEN
;
3281 /* Check if Errors has been detected during transfer */
3282 if(hi2c
->ErrorCode
!= HAL_I2C_ERROR_NONE
)
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
)
3288 if(hi2c
->ErrorCode
== HAL_I2C_ERROR_AF
)
3290 hi2c
->ErrorCode
|= HAL_I2C_ERROR_AF
;
3294 hi2c
->ErrorCode
|= HAL_I2C_ERROR_TIMEOUT
;
3298 /* Clear STOP Flag */
3299 __HAL_I2C_CLEAR_FLAG(hi2c
, I2C_FLAG_STOPF
);
3301 /* Clear Configuration Register 2 */
3302 __HAL_I2C_RESET_CR2(hi2c
);
3304 hi2c
->XferCount
= 0;
3306 hi2c
->State
= HAL_I2C_STATE_READY
;
3307 HAL_I2C_ErrorCallback(hi2c
);
3311 hi2c
->pBuffPtr
+= hi2c
->XferSize
;
3312 hi2c
->XferCount
-= hi2c
->XferSize
;
3313 if(hi2c
->XferCount
> 255)
3315 hi2c
->XferSize
= 255;
3319 hi2c
->XferSize
= hi2c
->XferCount
;
3322 DevAddress
= (hi2c
->Instance
->CR2
& I2C_CR2_SADD
);
3324 /* Enable the DMA channel */
3325 HAL_DMA_Start_IT(hi2c
->hdmarx
, (uint32_t)&hi2c
->Instance
->RXDR
, (uint32_t)hi2c
->pBuffPtr
, hi2c
->XferSize
);
3327 /* Send Slave Address */
3328 /* Set NBYTES to write and reload if size > 255 */
3329 if( (hi2c
->XferSize
== 255) && (hi2c
->XferSize
< hi2c
->XferCount
) )
3331 I2C_TransferConfig(hi2c
,DevAddress
,hi2c
->XferSize
, I2C_RELOAD_MODE
, I2C_NO_STARTSTOP
);
3335 I2C_TransferConfig(hi2c
,DevAddress
,hi2c
->XferSize
, I2C_AUTOEND_MODE
, I2C_NO_STARTSTOP
);
3338 /* Wait until RXNE flag is set */
3339 if(I2C_WaitOnFlagUntilTimeout(hi2c
, I2C_FLAG_RXNE
, RESET
, I2C_TIMEOUT_RXNE
) != HAL_OK
)
3341 hi2c
->ErrorCode
|= HAL_I2C_ERROR_TIMEOUT
;
3344 /* Check if Errors has been detected during transfer */
3345 if(hi2c
->ErrorCode
!= HAL_I2C_ERROR_NONE
)
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
)
3351 if(hi2c
->ErrorCode
== HAL_I2C_ERROR_AF
)
3353 hi2c
->ErrorCode
|= HAL_I2C_ERROR_AF
;
3357 hi2c
->ErrorCode
|= HAL_I2C_ERROR_TIMEOUT
;
3361 /* Clear STOP Flag */
3362 __HAL_I2C_CLEAR_FLAG(hi2c
, I2C_FLAG_STOPF
);
3364 /* Clear Configuration Register 2 */
3365 __HAL_I2C_RESET_CR2(hi2c
);
3367 hi2c
->XferCount
= 0;
3369 hi2c
->State
= HAL_I2C_STATE_READY
;
3371 HAL_I2C_ErrorCallback(hi2c
);
3375 /* Enable DMA Request */
3376 hi2c
->Instance
->CR1
|= I2C_CR1_RXDMAEN
;
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
)
3386 if(hi2c
->ErrorCode
== HAL_I2C_ERROR_AF
)
3388 hi2c
->ErrorCode
|= HAL_I2C_ERROR_AF
;
3392 hi2c
->ErrorCode
|= HAL_I2C_ERROR_TIMEOUT
;
3396 /* Clear STOP Flag */
3397 __HAL_I2C_CLEAR_FLAG(hi2c
, I2C_FLAG_STOPF
);
3399 /* Clear Configuration Register 2 */
3400 __HAL_I2C_RESET_CR2(hi2c
);
3402 /* Disable DMA Request */
3403 hi2c
->Instance
->CR1
&= ~I2C_CR1_RXDMAEN
;
3405 hi2c
->XferCount
= 0;
3407 hi2c
->State
= HAL_I2C_STATE_READY
;
3409 /* Check if Errors has been detected during transfer */
3410 if(hi2c
->ErrorCode
!= HAL_I2C_ERROR_NONE
)
3412 HAL_I2C_ErrorCallback(hi2c
);
3416 HAL_I2C_MasterRxCpltCallback(hi2c
);
3422 * @brief DMA I2C slave receive process complete callback.
3423 * @param hdma: DMA handle
3426 static void I2C_DMASlaveReceiveCplt(DMA_HandleTypeDef
*hdma
)
3428 I2C_HandleTypeDef
* hi2c
= (I2C_HandleTypeDef
*)((DMA_HandleTypeDef
*)hdma
)->Parent
;
3430 /* Wait until STOPF flag is reset */
3431 if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c
, I2C_TIMEOUT_STOPF
) != HAL_OK
)
3433 if(hi2c
->ErrorCode
== HAL_I2C_ERROR_AF
)
3435 hi2c
->ErrorCode
|= HAL_I2C_ERROR_AF
;
3439 hi2c
->ErrorCode
|= HAL_I2C_ERROR_TIMEOUT
;
3443 /* Clear STOPF flag */
3444 __HAL_I2C_CLEAR_FLAG(hi2c
, I2C_FLAG_STOPF
);
3446 /* Wait until BUSY flag is reset */
3447 if(I2C_WaitOnFlagUntilTimeout(hi2c
, I2C_FLAG_BUSY
, SET
, I2C_TIMEOUT_BUSY
) != HAL_OK
)
3449 hi2c
->ErrorCode
|= HAL_I2C_ERROR_TIMEOUT
;
3452 /* Disable DMA Request */
3453 hi2c
->Instance
->CR1
&= ~I2C_CR1_RXDMAEN
;
3455 /* Disable Address Acknowledge */
3456 hi2c
->Instance
->CR2
|= I2C_CR2_NACK
;
3458 hi2c
->XferCount
= 0;
3460 hi2c
->State
= HAL_I2C_STATE_READY
;
3462 /* Check if Errors has been detected during transfer */
3463 if(hi2c
->ErrorCode
!= HAL_I2C_ERROR_NONE
)
3465 HAL_I2C_ErrorCallback(hi2c
);
3469 HAL_I2C_SlaveRxCpltCallback(hi2c
);
3474 * @brief DMA I2C Memory Write process complete callback
3475 * @param hdma : DMA handle
3478 static void I2C_DMAMemTransmitCplt(DMA_HandleTypeDef
*hdma
)
3480 uint16_t DevAddress
;
3481 I2C_HandleTypeDef
* hi2c
= ( I2C_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
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
) )
3487 /* Wait until TCR flag is set */
3488 if(I2C_WaitOnFlagUntilTimeout(hi2c
, I2C_FLAG_TCR
, RESET
, I2C_TIMEOUT_TCR
) != HAL_OK
)
3490 hi2c
->ErrorCode
|= HAL_I2C_ERROR_TIMEOUT
;
3493 /* Disable DMA Request */
3494 hi2c
->Instance
->CR1
&= ~I2C_CR1_TXDMAEN
;
3496 /* Check if Errors has been detected during transfer */
3497 if(hi2c
->ErrorCode
!= HAL_I2C_ERROR_NONE
)
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
)
3503 if(hi2c
->ErrorCode
== HAL_I2C_ERROR_AF
)
3505 hi2c
->ErrorCode
|= HAL_I2C_ERROR_AF
;
3509 hi2c
->ErrorCode
|= HAL_I2C_ERROR_TIMEOUT
;
3513 /* Clear STOP Flag */
3514 __HAL_I2C_CLEAR_FLAG(hi2c
, I2C_FLAG_STOPF
);
3516 /* Clear Configuration Register 2 */
3517 __HAL_I2C_RESET_CR2(hi2c
);
3519 hi2c
->XferCount
= 0;
3521 hi2c
->State
= HAL_I2C_STATE_READY
;
3522 HAL_I2C_ErrorCallback(hi2c
);
3526 hi2c
->pBuffPtr
+= hi2c
->XferSize
;
3527 hi2c
->XferCount
-= hi2c
->XferSize
;
3528 if(hi2c
->XferCount
> 255)
3530 hi2c
->XferSize
= 255;
3534 hi2c
->XferSize
= hi2c
->XferCount
;
3537 DevAddress
= (hi2c
->Instance
->CR2
& I2C_CR2_SADD
);
3539 /* Enable the DMA channel */
3540 HAL_DMA_Start_IT(hi2c
->hdmatx
, (uint32_t)hi2c
->pBuffPtr
, (uint32_t)&hi2c
->Instance
->TXDR
, hi2c
->XferSize
);
3542 /* Send Slave Address */
3543 /* Set NBYTES to write and reload if size > 255 */
3544 if( (hi2c
->XferSize
== 255) && (hi2c
->XferSize
< hi2c
->XferCount
) )
3546 I2C_TransferConfig(hi2c
,DevAddress
,hi2c
->XferSize
, I2C_RELOAD_MODE
, I2C_NO_STARTSTOP
);
3550 I2C_TransferConfig(hi2c
,DevAddress
,hi2c
->XferSize
, I2C_AUTOEND_MODE
, I2C_NO_STARTSTOP
);
3553 /* Wait until TXIS flag is set */
3554 if(I2C_WaitOnTXISFlagUntilTimeout(hi2c
, I2C_TIMEOUT_TXIS
) != HAL_OK
)
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
)
3560 if(hi2c
->ErrorCode
== HAL_I2C_ERROR_AF
)
3562 hi2c
->ErrorCode
|= HAL_I2C_ERROR_AF
;
3566 hi2c
->ErrorCode
|= HAL_I2C_ERROR_TIMEOUT
;
3570 /* Clear STOP Flag */
3571 __HAL_I2C_CLEAR_FLAG(hi2c
, I2C_FLAG_STOPF
);
3573 /* Clear Configuration Register 2 */
3574 __HAL_I2C_RESET_CR2(hi2c
);
3576 hi2c
->XferCount
= 0;
3578 hi2c
->State
= HAL_I2C_STATE_READY
;
3579 HAL_I2C_ErrorCallback(hi2c
);
3583 /* Enable DMA Request */
3584 hi2c
->Instance
->CR1
|= I2C_CR1_TXDMAEN
;
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
)
3594 if(hi2c
->ErrorCode
== HAL_I2C_ERROR_AF
)
3596 hi2c
->ErrorCode
|= HAL_I2C_ERROR_AF
;
3600 hi2c
->ErrorCode
|= HAL_I2C_ERROR_TIMEOUT
;
3604 /* Clear STOP Flag */
3605 __HAL_I2C_CLEAR_FLAG(hi2c
, I2C_FLAG_STOPF
);
3607 /* Clear Configuration Register 2 */
3608 __HAL_I2C_RESET_CR2(hi2c
);
3610 /* Disable DMA Request */
3611 hi2c
->Instance
->CR1
&= ~I2C_CR1_TXDMAEN
;
3613 hi2c
->XferCount
= 0;
3615 hi2c
->State
= HAL_I2C_STATE_READY
;
3617 /* Check if Errors has been detected during transfer */
3618 if(hi2c
->ErrorCode
!= HAL_I2C_ERROR_NONE
)
3620 HAL_I2C_ErrorCallback(hi2c
);
3624 HAL_I2C_MemTxCpltCallback(hi2c
);
3630 * @brief DMA I2C Memory Read process complete callback
3631 * @param hdma: DMA handle
3634 static void I2C_DMAMemReceiveCplt(DMA_HandleTypeDef
*hdma
)
3636 I2C_HandleTypeDef
* hi2c
= ( I2C_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
3637 uint16_t DevAddress
;
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
) )
3643 /* Wait until TCR flag is set */
3644 if(I2C_WaitOnFlagUntilTimeout(hi2c
, I2C_FLAG_TCR
, RESET
, I2C_TIMEOUT_TCR
) != HAL_OK
)
3646 hi2c
->ErrorCode
|= HAL_I2C_ERROR_TIMEOUT
;
3649 /* Disable DMA Request */
3650 hi2c
->Instance
->CR1
&= ~I2C_CR1_RXDMAEN
;
3652 /* Check if Errors has been detected during transfer */
3653 if(hi2c
->ErrorCode
!= HAL_I2C_ERROR_NONE
)
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
)
3659 if(hi2c
->ErrorCode
== HAL_I2C_ERROR_AF
)
3661 hi2c
->ErrorCode
|= HAL_I2C_ERROR_AF
;
3665 hi2c
->ErrorCode
|= HAL_I2C_ERROR_TIMEOUT
;
3669 /* Clear STOP Flag */
3670 __HAL_I2C_CLEAR_FLAG(hi2c
, I2C_FLAG_STOPF
);
3672 /* Clear Configuration Register 2 */
3673 __HAL_I2C_RESET_CR2(hi2c
);
3675 hi2c
->XferCount
= 0;
3677 hi2c
->State
= HAL_I2C_STATE_READY
;
3678 HAL_I2C_ErrorCallback(hi2c
);
3682 hi2c
->pBuffPtr
+= hi2c
->XferSize
;
3683 hi2c
->XferCount
-= hi2c
->XferSize
;
3684 if(hi2c
->XferCount
> 255)
3686 hi2c
->XferSize
= 255;
3690 hi2c
->XferSize
= hi2c
->XferCount
;
3693 DevAddress
= (hi2c
->Instance
->CR2
& I2C_CR2_SADD
);
3695 /* Enable the DMA channel */
3696 HAL_DMA_Start_IT(hi2c
->hdmarx
, (uint32_t)&hi2c
->Instance
->RXDR
, (uint32_t)hi2c
->pBuffPtr
, hi2c
->XferSize
);
3698 /* Send Slave Address */
3699 /* Set NBYTES to write and reload if size > 255 */
3700 if( (hi2c
->XferSize
== 255) && (hi2c
->XferSize
< hi2c
->XferCount
) )
3702 I2C_TransferConfig(hi2c
,DevAddress
,hi2c
->XferSize
, I2C_RELOAD_MODE
, I2C_NO_STARTSTOP
);
3706 I2C_TransferConfig(hi2c
,DevAddress
,hi2c
->XferSize
, I2C_AUTOEND_MODE
, I2C_NO_STARTSTOP
);
3709 /* Wait until RXNE flag is set */
3710 if(I2C_WaitOnFlagUntilTimeout(hi2c
, I2C_FLAG_RXNE
, RESET
, I2C_TIMEOUT_RXNE
) != HAL_OK
)
3712 hi2c
->ErrorCode
|= HAL_I2C_ERROR_TIMEOUT
;
3715 /* Check if Errors has been detected during transfer */
3716 if(hi2c
->ErrorCode
!= HAL_I2C_ERROR_NONE
)
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
)
3722 if(hi2c
->ErrorCode
== HAL_I2C_ERROR_AF
)
3724 hi2c
->ErrorCode
|= HAL_I2C_ERROR_AF
;
3728 hi2c
->ErrorCode
|= HAL_I2C_ERROR_TIMEOUT
;
3732 /* Clear STOP Flag */
3733 __HAL_I2C_CLEAR_FLAG(hi2c
, I2C_FLAG_STOPF
);
3735 /* Clear Configuration Register 2 */
3736 __HAL_I2C_RESET_CR2(hi2c
);
3738 hi2c
->XferCount
= 0;
3740 hi2c
->State
= HAL_I2C_STATE_READY
;
3741 HAL_I2C_ErrorCallback(hi2c
);
3745 /* Enable DMA Request */
3746 hi2c
->Instance
->CR1
|= I2C_CR1_RXDMAEN
;
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
)
3756 if(hi2c
->ErrorCode
== HAL_I2C_ERROR_AF
)
3758 hi2c
->ErrorCode
|= HAL_I2C_ERROR_AF
;
3762 hi2c
->ErrorCode
|= HAL_I2C_ERROR_TIMEOUT
;
3766 /* Clear STOP Flag */
3767 __HAL_I2C_CLEAR_FLAG(hi2c
, I2C_FLAG_STOPF
);
3769 /* Clear Configuration Register 2 */
3770 __HAL_I2C_RESET_CR2(hi2c
);
3772 /* Disable DMA Request */
3773 hi2c
->Instance
->CR1
&= ~I2C_CR1_RXDMAEN
;
3775 hi2c
->XferCount
= 0;
3777 hi2c
->State
= HAL_I2C_STATE_READY
;
3779 /* Check if Errors has been detected during transfer */
3780 if(hi2c
->ErrorCode
!= HAL_I2C_ERROR_NONE
)
3782 HAL_I2C_ErrorCallback(hi2c
);
3786 HAL_I2C_MemRxCpltCallback(hi2c
);
3792 * @brief DMA I2C communication error callback.
3793 * @param hdma : DMA handle
3796 static void I2C_DMAError(DMA_HandleTypeDef
*hdma
)
3798 I2C_HandleTypeDef
* hi2c
= ( I2C_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
3800 /* Disable Acknowledge */
3801 hi2c
->Instance
->CR2
|= I2C_CR2_NACK
;
3803 hi2c
->XferCount
= 0;
3805 hi2c
->State
= HAL_I2C_STATE_READY
;
3807 hi2c
->ErrorCode
|= HAL_I2C_ERROR_DMA
;
3809 HAL_I2C_ErrorCallback(hi2c
);
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
3821 static HAL_StatusTypeDef
I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef
*hi2c
, uint32_t Flag
, FlagStatus Status
, uint32_t Timeout
)
3823 uint32_t tickstart
= HAL_GetTick();
3825 /* Wait until flag is set */
3828 while(__HAL_I2C_GET_FLAG(hi2c
, Flag
) == RESET
)
3830 /* Check for the Timeout */
3831 if(Timeout
!= HAL_MAX_DELAY
)
3833 if((Timeout
== 0) || ((HAL_GetTick() - tickstart
) > Timeout
))
3835 hi2c
->State
= HAL_I2C_STATE_READY
;
3836 /* Process Unlocked */
3845 while(__HAL_I2C_GET_FLAG(hi2c
, Flag
) != RESET
)
3847 /* Check for the Timeout */
3848 if(Timeout
!= HAL_MAX_DELAY
)
3850 if((Timeout
== 0) || ((HAL_GetTick() - tickstart
) > Timeout
))
3852 hi2c
->State
= HAL_I2C_STATE_READY
;
3853 /* Process Unlocked */
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
3870 static HAL_StatusTypeDef
I2C_WaitOnTXISFlagUntilTimeout(I2C_HandleTypeDef
*hi2c
, uint32_t Timeout
)
3872 uint32_t tickstart
= HAL_GetTick();
3874 while(__HAL_I2C_GET_FLAG(hi2c
, I2C_FLAG_TXIS
) == RESET
)
3876 /* Check if a NACK is detected */
3877 if(I2C_IsAcknowledgeFailed(hi2c
, Timeout
) != HAL_OK
)
3882 /* Check for the Timeout */
3883 if(Timeout
!= HAL_MAX_DELAY
)
3885 if((Timeout
== 0) || ((HAL_GetTick() - tickstart
) > Timeout
))
3887 hi2c
->ErrorCode
|= HAL_I2C_ERROR_TIMEOUT
;
3888 hi2c
->State
= HAL_I2C_STATE_READY
;
3890 /* Process Unlocked */
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
3907 static HAL_StatusTypeDef
I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef
*hi2c
, uint32_t Timeout
)
3909 uint32_t tickstart
= 0x00;
3910 tickstart
= HAL_GetTick();
3912 while(__HAL_I2C_GET_FLAG(hi2c
, I2C_FLAG_STOPF
) == RESET
)
3914 /* Check if a NACK is detected */
3915 if(I2C_IsAcknowledgeFailed(hi2c
, Timeout
) != HAL_OK
)
3920 /* Check for the Timeout */
3921 if((Timeout
== 0) || ((HAL_GetTick() - tickstart
) > Timeout
))
3923 hi2c
->ErrorCode
|= HAL_I2C_ERROR_TIMEOUT
;
3924 hi2c
->State
= HAL_I2C_STATE_READY
;
3926 /* Process Unlocked */
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
3942 static HAL_StatusTypeDef
I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef
*hi2c
, uint32_t Timeout
)
3944 uint32_t tickstart
= 0x00;
3945 tickstart
= HAL_GetTick();
3947 while(__HAL_I2C_GET_FLAG(hi2c
, I2C_FLAG_RXNE
) == RESET
)
3949 /* Check if a STOPF is detected */
3950 if(__HAL_I2C_GET_FLAG(hi2c
, I2C_FLAG_STOPF
) == SET
)
3952 /* Clear STOP Flag */
3953 __HAL_I2C_CLEAR_FLAG(hi2c
, I2C_FLAG_STOPF
);
3955 /* Clear Configuration Register 2 */
3956 __HAL_I2C_RESET_CR2(hi2c
);
3958 hi2c
->ErrorCode
= HAL_I2C_ERROR_NONE
;
3959 hi2c
->State
= HAL_I2C_STATE_READY
;
3961 /* Process Unlocked */
3967 /* Check for the Timeout */
3968 if((Timeout
== 0) || ((HAL_GetTick() - tickstart
) > Timeout
))
3970 hi2c
->ErrorCode
|= HAL_I2C_ERROR_TIMEOUT
;
3971 hi2c
->State
= HAL_I2C_STATE_READY
;
3973 /* Process Unlocked */
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
3989 static HAL_StatusTypeDef
I2C_IsAcknowledgeFailed(I2C_HandleTypeDef
*hi2c
, uint32_t Timeout
)
3991 uint32_t tickstart
= 0x00;
3992 tickstart
= HAL_GetTick();
3994 if(__HAL_I2C_GET_FLAG(hi2c
, I2C_FLAG_AF
) == SET
)
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
))
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
)
4005 hi2c
->Instance
->CR2
|= I2C_CR2_STOP
;
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
)
4013 /* Check for the Timeout */
4014 if(Timeout
!= HAL_MAX_DELAY
)
4016 if((Timeout
== 0) || ((HAL_GetTick() - tickstart
) > Timeout
))
4018 hi2c
->State
= HAL_I2C_STATE_READY
;
4019 /* Process Unlocked */
4026 /* Clear NACKF Flag */
4027 __HAL_I2C_CLEAR_FLAG(hi2c
, I2C_FLAG_AF
);
4029 /* Clear STOP Flag */
4030 __HAL_I2C_CLEAR_FLAG(hi2c
, I2C_FLAG_STOPF
);
4032 /* Clear Configuration Register 2 */
4033 __HAL_I2C_RESET_CR2(hi2c
);
4035 hi2c
->ErrorCode
= HAL_I2C_ERROR_AF
;
4036 hi2c
->State
= HAL_I2C_STATE_READY
;
4038 /* Process Unlocked */
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.
4065 static void I2C_TransferConfig(I2C_HandleTypeDef
*hi2c
, uint16_t DevAddress
, uint8_t Size
, uint32_t Mode
, uint32_t Request
)
4067 uint32_t tmpreg
= 0;
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
));
4074 /* Get the CR2 register value */
4075 tmpreg
= hi2c
->Instance
->CR2
;
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
));
4081 tmpreg
|= (uint32_t)(((uint32_t)DevAddress
& I2C_CR2_SADD
) | (((uint32_t)Size
<< 16 ) & I2C_CR2_NBYTES
) | \
4082 (uint32_t)Mode
| (uint32_t)Request
);
4084 /* update CR2 register */
4085 hi2c
->Instance
->CR2
= tmpreg
;
4092 #endif /* HAL_I2C_MODULE_ENABLED */
4101 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/