STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
stm32h7xx_hal_i2c.c
Go to the documentation of this file.
1/**
2 ******************************************************************************
3 * @file stm32h7xx_hal_i2c.c
4 * @author MCD Application Team
5 * @brief I2C HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the Inter Integrated Circuit (I2C) peripheral:
8 * + Initialization and de-initialization functions
9 * + IO operation functions
10 * + Peripheral State and Errors functions
11 *
12 ******************************************************************************
13 * @attention
14 *
15 * Copyright (c) 2017 STMicroelectronics.
16 * All rights reserved.
17 *
18 * This software is licensed under terms that can be found in the LICENSE file
19 * in the root directory of this software component.
20 * If no LICENSE file comes with this software, it is provided AS-IS.
21 *
22 ******************************************************************************
23 @verbatim
24 ==============================================================================
25 ##### How to use this driver #####
26 ==============================================================================
27 [..]
28 The I2C HAL driver can be used as follows:
29
30 (#) Declare a I2C_HandleTypeDef handle structure, for example:
31 I2C_HandleTypeDef hi2c;
32
33 (#)Initialize the I2C low level resources by implementing the HAL_I2C_MspInit() API:
34 (##) Enable the I2Cx interface clock
35 (##) I2C pins configuration
36 (+++) Enable the clock for the I2C GPIOs
37 (+++) Configure I2C pins as alternate function open-drain
38 (##) NVIC configuration if you need to use interrupt process
39 (+++) Configure the I2Cx interrupt priority
40 (+++) Enable the NVIC I2C IRQ Channel
41 (##) DMA Configuration if you need to use DMA process
42 (+++) Declare a DMA_HandleTypeDef handle structure for
43 the transmit or receive stream or channel depends on Instance
44 (+++) Enable the DMAx interface clock using
45 (+++) Configure the DMA handle parameters
46 (+++) Configure the DMA Tx or Rx stream or channel depends on Instance
47 (+++) Associate the initialized DMA handle to the hi2c DMA Tx or Rx handle
48 (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on
49 the DMA Tx or Rx stream or channel depends on Instance
50
51 (#) Configure the Communication Clock Timing, Own Address1, Master Addressing mode, Dual Addressing mode,
52 Own Address2, Own Address2 Mask, General call and Nostretch mode in the hi2c Init structure.
53
54 (#) Initialize the I2C registers by calling the HAL_I2C_Init(), configures also the low level Hardware
55 (GPIO, CLOCK, NVIC...etc) by calling the customized HAL_I2C_MspInit(&hi2c) API.
56
57 (#) To check if target device is ready for communication, use the function HAL_I2C_IsDeviceReady()
58
59 (#) For I2C IO and IO MEM operations, three operation modes are available within this driver :
60
61 *** Polling mode IO operation ***
62 =================================
63 [..]
64 (+) Transmit in master mode an amount of data in blocking mode using HAL_I2C_Master_Transmit()
65 (+) Receive in master mode an amount of data in blocking mode using HAL_I2C_Master_Receive()
66 (+) Transmit in slave mode an amount of data in blocking mode using HAL_I2C_Slave_Transmit()
67 (+) Receive in slave mode an amount of data in blocking mode using HAL_I2C_Slave_Receive()
68
69 *** Polling mode IO MEM operation ***
70 =====================================
71 [..]
72 (+) Write an amount of data in blocking mode to a specific memory address using HAL_I2C_Mem_Write()
73 (+) Read an amount of data in blocking mode from a specific memory address using HAL_I2C_Mem_Read()
74
75
76 *** Interrupt mode IO operation ***
77 ===================================
78 [..]
79 (+) Transmit in master mode an amount of data in non-blocking mode using HAL_I2C_Master_Transmit_IT()
80 (+) At transmission end of transfer, HAL_I2C_MasterTxCpltCallback() is executed and users can
81 add their own code by customization of function pointer HAL_I2C_MasterTxCpltCallback()
82 (+) Receive in master mode an amount of data in non-blocking mode using HAL_I2C_Master_Receive_IT()
83 (+) At reception end of transfer, HAL_I2C_MasterRxCpltCallback() is executed and users can
84 add their own code by customization of function pointer HAL_I2C_MasterRxCpltCallback()
85 (+) Transmit in slave mode an amount of data in non-blocking mode using HAL_I2C_Slave_Transmit_IT()
86 (+) At transmission end of transfer, HAL_I2C_SlaveTxCpltCallback() is executed and users can
87 add their own code by customization of function pointer HAL_I2C_SlaveTxCpltCallback()
88 (+) Receive in slave mode an amount of data in non-blocking mode using HAL_I2C_Slave_Receive_IT()
89 (+) At reception end of transfer, HAL_I2C_SlaveRxCpltCallback() is executed and users can
90 add their own code by customization of function pointer HAL_I2C_SlaveRxCpltCallback()
91 (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and users can
92 add their own code by customization of function pointer HAL_I2C_ErrorCallback()
93 (+) Abort a master or memory I2C process communication with Interrupt using HAL_I2C_Master_Abort_IT()
94 (+) End of abort process, HAL_I2C_AbortCpltCallback() is executed and users can
95 add their own code by customization of function pointer HAL_I2C_AbortCpltCallback()
96 (+) Discard a slave I2C process communication using __HAL_I2C_GENERATE_NACK() macro.
97 This action will inform Master to generate a Stop condition to discard the communication.
98
99
100 *** Interrupt mode or DMA mode IO sequential operation ***
101 ==========================================================
102 [..]
103 (@) These interfaces allow to manage a sequential transfer with a repeated start condition
104 when a direction change during transfer
105 [..]
106 (+) A specific option field manage the different steps of a sequential transfer
107 (+) Option field values are defined through I2C_XFEROPTIONS and are listed below:
108 (++) I2C_FIRST_AND_LAST_FRAME: No sequential usage, functional is same as associated interfaces in
109 no sequential mode
110 (++) I2C_FIRST_FRAME: Sequential usage, this option allow to manage a sequence with start condition, address
111 and data to transfer without a final stop condition
112 (++) I2C_FIRST_AND_NEXT_FRAME: Sequential usage (Master only), this option allow to manage a sequence with
113 start condition, address and data to transfer without a final stop condition,
114 an then permit a call the same master sequential interface several times
115 (like HAL_I2C_Master_Seq_Transmit_IT() then HAL_I2C_Master_Seq_Transmit_IT()
116 or HAL_I2C_Master_Seq_Transmit_DMA() then HAL_I2C_Master_Seq_Transmit_DMA())
117 (++) I2C_NEXT_FRAME: Sequential usage, this option allow to manage a sequence with a restart condition, address
118 and with new data to transfer if the direction change or manage only the new data to
119 transfer
120 if no direction change and without a final stop condition in both cases
121 (++) I2C_LAST_FRAME: Sequential usage, this option allow to manage a sequance with a restart condition, address
122 and with new data to transfer if the direction change or manage only the new data to
123 transfer
124 if no direction change and with a final stop condition in both cases
125 (++) I2C_LAST_FRAME_NO_STOP: Sequential usage (Master only), this option allow to manage a restart condition
126 after several call of the same master sequential interface several times
127 (link with option I2C_FIRST_AND_NEXT_FRAME).
128 Usage can, transfer several bytes one by one using
129 HAL_I2C_Master_Seq_Transmit_IT
130 or HAL_I2C_Master_Seq_Receive_IT
131 or HAL_I2C_Master_Seq_Transmit_DMA
132 or HAL_I2C_Master_Seq_Receive_DMA
133 with option I2C_FIRST_AND_NEXT_FRAME then I2C_NEXT_FRAME.
134 Then usage of this option I2C_LAST_FRAME_NO_STOP at the last Transmit or
135 Receive sequence permit to call the opposite interface Receive or Transmit
136 without stopping the communication and so generate a restart condition.
137 (++) I2C_OTHER_FRAME: Sequential usage (Master only), this option allow to manage a restart condition after
138 each call of the same master sequential
139 interface.
140 Usage can, transfer several bytes one by one with a restart with slave address between
141 each bytes using
142 HAL_I2C_Master_Seq_Transmit_IT
143 or HAL_I2C_Master_Seq_Receive_IT
144 or HAL_I2C_Master_Seq_Transmit_DMA
145 or HAL_I2C_Master_Seq_Receive_DMA
146 with option I2C_FIRST_FRAME then I2C_OTHER_FRAME.
147 Then usage of this option I2C_OTHER_AND_LAST_FRAME at the last frame to help automatic
148 generation of STOP condition.
149
150 (+) Different sequential I2C interfaces are listed below:
151 (++) Sequential transmit in master I2C mode an amount of data in non-blocking mode using
152 HAL_I2C_Master_Seq_Transmit_IT() or using HAL_I2C_Master_Seq_Transmit_DMA()
153 (+++) At transmission end of current frame transfer, HAL_I2C_MasterTxCpltCallback() is executed and
154 users can add their own code by customization of function pointer HAL_I2C_MasterTxCpltCallback()
155 (++) Sequential receive in master I2C mode an amount of data in non-blocking mode using
156 HAL_I2C_Master_Seq_Receive_IT() or using HAL_I2C_Master_Seq_Receive_DMA()
157 (+++) At reception end of current frame transfer, HAL_I2C_MasterRxCpltCallback() is executed and users can
158 add their own code by customization of function pointer HAL_I2C_MasterRxCpltCallback()
159 (++) Abort a master or memory IT or DMA I2C process communication with Interrupt using HAL_I2C_Master_Abort_IT()
160 (+++) End of abort process, HAL_I2C_AbortCpltCallback() is executed and users can
161 add their own code by customization of function pointer HAL_I2C_AbortCpltCallback()
162 (++) Enable/disable the Address listen mode in slave I2C mode using HAL_I2C_EnableListen_IT()
163 HAL_I2C_DisableListen_IT()
164 (+++) When address slave I2C match, HAL_I2C_AddrCallback() is executed and users can
165 add their own code to check the Address Match Code and the transmission direction request by master
166 (Write/Read).
167 (+++) At Listen mode end HAL_I2C_ListenCpltCallback() is executed and users can
168 add their own code by customization of function pointer HAL_I2C_ListenCpltCallback()
169 (++) Sequential transmit in slave I2C mode an amount of data in non-blocking mode using
170 HAL_I2C_Slave_Seq_Transmit_IT() or using HAL_I2C_Slave_Seq_Transmit_DMA()
171 (+++) At transmission end of current frame transfer, HAL_I2C_SlaveTxCpltCallback() is executed and
172 users can add their own code by customization of function pointer HAL_I2C_SlaveTxCpltCallback()
173 (++) Sequential receive in slave I2C mode an amount of data in non-blocking mode using
174 HAL_I2C_Slave_Seq_Receive_IT() or using HAL_I2C_Slave_Seq_Receive_DMA()
175 (+++) At reception end of current frame transfer, HAL_I2C_SlaveRxCpltCallback() is executed and users can
176 add their own code by customization of function pointer HAL_I2C_SlaveRxCpltCallback()
177 (++) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and users can
178 add their own code by customization of function pointer HAL_I2C_ErrorCallback()
179 (++) Discard a slave I2C process communication using __HAL_I2C_GENERATE_NACK() macro.
180 This action will inform Master to generate a Stop condition to discard the communication.
181
182 *** Interrupt mode IO MEM operation ***
183 =======================================
184 [..]
185 (+) Write an amount of data in non-blocking mode with Interrupt to a specific memory address using
186 HAL_I2C_Mem_Write_IT()
187 (+) At Memory end of write transfer, HAL_I2C_MemTxCpltCallback() is executed and users can
188 add their own code by customization of function pointer HAL_I2C_MemTxCpltCallback()
189 (+) Read an amount of data in non-blocking mode with Interrupt from a specific memory address using
190 HAL_I2C_Mem_Read_IT()
191 (+) At Memory end of read transfer, HAL_I2C_MemRxCpltCallback() is executed and users can
192 add their own code by customization of function pointer HAL_I2C_MemRxCpltCallback()
193 (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and users can
194 add their own code by customization of function pointer HAL_I2C_ErrorCallback()
195
196 *** DMA mode IO operation ***
197 ==============================
198 [..]
199 (+) Transmit in master mode an amount of data in non-blocking mode (DMA) using
200 HAL_I2C_Master_Transmit_DMA()
201 (+) At transmission end of transfer, HAL_I2C_MasterTxCpltCallback() is executed and users can
202 add their own code by customization of function pointer HAL_I2C_MasterTxCpltCallback()
203 (+) Receive in master mode an amount of data in non-blocking mode (DMA) using
204 HAL_I2C_Master_Receive_DMA()
205 (+) At reception end of transfer, HAL_I2C_MasterRxCpltCallback() is executed and users can
206 add their own code by customization of function pointer HAL_I2C_MasterRxCpltCallback()
207 (+) Transmit in slave mode an amount of data in non-blocking mode (DMA) using
208 HAL_I2C_Slave_Transmit_DMA()
209 (+) At transmission end of transfer, HAL_I2C_SlaveTxCpltCallback() is executed and users can
210 add their own code by customization of function pointer HAL_I2C_SlaveTxCpltCallback()
211 (+) Receive in slave mode an amount of data in non-blocking mode (DMA) using
212 HAL_I2C_Slave_Receive_DMA()
213 (+) At reception end of transfer, HAL_I2C_SlaveRxCpltCallback() is executed and users can
214 add their own code by customization of function pointer HAL_I2C_SlaveRxCpltCallback()
215 (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and users can
216 add their own code by customization of function pointer HAL_I2C_ErrorCallback()
217 (+) Abort a master or memory I2C process communication with Interrupt using HAL_I2C_Master_Abort_IT()
218 (+) End of abort process, HAL_I2C_AbortCpltCallback() is executed and users can
219 add their own code by customization of function pointer HAL_I2C_AbortCpltCallback()
220 (+) Discard a slave I2C process communication using __HAL_I2C_GENERATE_NACK() macro.
221 This action will inform Master to generate a Stop condition to discard the communication.
222
223 *** DMA mode IO MEM operation ***
224 =================================
225 [..]
226 (+) Write an amount of data in non-blocking mode with DMA to a specific memory address using
227 HAL_I2C_Mem_Write_DMA()
228 (+) At Memory end of write transfer, HAL_I2C_MemTxCpltCallback() is executed and users can
229 add their own code by customization of function pointer HAL_I2C_MemTxCpltCallback()
230 (+) Read an amount of data in non-blocking mode with DMA from a specific memory address using
231 HAL_I2C_Mem_Read_DMA()
232 (+) At Memory end of read transfer, HAL_I2C_MemRxCpltCallback() is executed and users can
233 add their own code by customization of function pointer HAL_I2C_MemRxCpltCallback()
234 (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and users can
235 add their own code by customization of function pointer HAL_I2C_ErrorCallback()
236
237
238 *** I2C HAL driver macros list ***
239 ==================================
240 [..]
241 Below the list of most used macros in I2C HAL driver.
242
243 (+) __HAL_I2C_ENABLE: Enable the I2C peripheral
244 (+) __HAL_I2C_DISABLE: Disable the I2C peripheral
245 (+) __HAL_I2C_GENERATE_NACK: Generate a Non-Acknowledge I2C peripheral in Slave mode
246 (+) __HAL_I2C_GET_FLAG: Check whether the specified I2C flag is set or not
247 (+) __HAL_I2C_CLEAR_FLAG: Clear the specified I2C pending flag
248 (+) __HAL_I2C_ENABLE_IT: Enable the specified I2C interrupt
249 (+) __HAL_I2C_DISABLE_IT: Disable the specified I2C interrupt
250
251 *** Callback registration ***
252 =============================================
253 [..]
254 The compilation flag USE_HAL_I2C_REGISTER_CALLBACKS when set to 1
255 allows the user to configure dynamically the driver callbacks.
256 Use Functions HAL_I2C_RegisterCallback() or HAL_I2C_RegisterAddrCallback()
257 to register an interrupt callback.
258 [..]
259 Function HAL_I2C_RegisterCallback() allows to register following callbacks:
260 (+) MasterTxCpltCallback : callback for Master transmission end of transfer.
261 (+) MasterRxCpltCallback : callback for Master reception end of transfer.
262 (+) SlaveTxCpltCallback : callback for Slave transmission end of transfer.
263 (+) SlaveRxCpltCallback : callback for Slave reception end of transfer.
264 (+) ListenCpltCallback : callback for end of listen mode.
265 (+) MemTxCpltCallback : callback for Memory transmission end of transfer.
266 (+) MemRxCpltCallback : callback for Memory reception end of transfer.
267 (+) ErrorCallback : callback for error detection.
268 (+) AbortCpltCallback : callback for abort completion process.
269 (+) MspInitCallback : callback for Msp Init.
270 (+) MspDeInitCallback : callback for Msp DeInit.
271 This function takes as parameters the HAL peripheral handle, the Callback ID
272 and a pointer to the user callback function.
273 [..]
274 For specific callback AddrCallback use dedicated register callbacks : HAL_I2C_RegisterAddrCallback().
275 [..]
276 Use function HAL_I2C_UnRegisterCallback to reset a callback to the default
277 weak function.
278 HAL_I2C_UnRegisterCallback takes as parameters the HAL peripheral handle,
279 and the Callback ID.
280 This function allows to reset following callbacks:
281 (+) MasterTxCpltCallback : callback for Master transmission end of transfer.
282 (+) MasterRxCpltCallback : callback for Master reception end of transfer.
283 (+) SlaveTxCpltCallback : callback for Slave transmission end of transfer.
284 (+) SlaveRxCpltCallback : callback for Slave reception end of transfer.
285 (+) ListenCpltCallback : callback for end of listen mode.
286 (+) MemTxCpltCallback : callback for Memory transmission end of transfer.
287 (+) MemRxCpltCallback : callback for Memory reception end of transfer.
288 (+) ErrorCallback : callback for error detection.
289 (+) AbortCpltCallback : callback for abort completion process.
290 (+) MspInitCallback : callback for Msp Init.
291 (+) MspDeInitCallback : callback for Msp DeInit.
292 [..]
293 For callback AddrCallback use dedicated register callbacks : HAL_I2C_UnRegisterAddrCallback().
294 [..]
295 By default, after the HAL_I2C_Init() and when the state is HAL_I2C_STATE_RESET
296 all callbacks are set to the corresponding weak functions:
297 examples HAL_I2C_MasterTxCpltCallback(), HAL_I2C_MasterRxCpltCallback().
298 Exception done for MspInit and MspDeInit functions that are
299 reset to the legacy weak functions in the HAL_I2C_Init()/ HAL_I2C_DeInit() only when
300 these callbacks are null (not registered beforehand).
301 If MspInit or MspDeInit are not null, the HAL_I2C_Init()/ HAL_I2C_DeInit()
302 keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
303 [..]
304 Callbacks can be registered/unregistered in HAL_I2C_STATE_READY state only.
305 Exception done MspInit/MspDeInit functions that can be registered/unregistered
306 in HAL_I2C_STATE_READY or HAL_I2C_STATE_RESET state,
307 thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
308 Then, the user first registers the MspInit/MspDeInit user callbacks
309 using HAL_I2C_RegisterCallback() before calling HAL_I2C_DeInit()
310 or HAL_I2C_Init() function.
311 [..]
312 When the compilation flag USE_HAL_I2C_REGISTER_CALLBACKS is set to 0 or
313 not defined, the callback registration feature is not available and all callbacks
314 are set to the corresponding weak functions.
315
316 [..]
317 (@) You can refer to the I2C HAL driver header file for more useful macros
318
319 @endverbatim
320 */
321
322/* Includes ------------------------------------------------------------------*/
323#include "stm32h7xx_hal.h"
324
325/** @addtogroup STM32H7xx_HAL_Driver
326 * @{
327 */
328
329/** @defgroup I2C I2C
330 * @brief I2C HAL module driver
331 * @{
332 */
333
334#ifdef HAL_I2C_MODULE_ENABLED
335
336/* Private typedef -----------------------------------------------------------*/
337/* Private define ------------------------------------------------------------*/
338
339/** @defgroup I2C_Private_Define I2C Private Define
340 * @{
341 */
342#define TIMING_CLEAR_MASK (0xF0FFFFFFU) /*!< I2C TIMING clear register Mask */
343#define I2C_TIMEOUT_ADDR (10000U) /*!< 10 s */
344#define I2C_TIMEOUT_BUSY (25U) /*!< 25 ms */
345#define I2C_TIMEOUT_DIR (25U) /*!< 25 ms */
346#define I2C_TIMEOUT_RXNE (25U) /*!< 25 ms */
347#define I2C_TIMEOUT_STOPF (25U) /*!< 25 ms */
348#define I2C_TIMEOUT_TC (25U) /*!< 25 ms */
349#define I2C_TIMEOUT_TCR (25U) /*!< 25 ms */
350#define I2C_TIMEOUT_TXIS (25U) /*!< 25 ms */
351#define I2C_TIMEOUT_FLAG (25U) /*!< 25 ms */
352
353#define MAX_NBYTE_SIZE 255U
354#define SLAVE_ADDR_SHIFT 7U
355#define SLAVE_ADDR_MSK 0x06U
356
357/* Private define for @ref PreviousState usage */
358#define I2C_STATE_MSK ((uint32_t)((uint32_t)((uint32_t)HAL_I2C_STATE_BUSY_TX | \
359 (uint32_t)HAL_I2C_STATE_BUSY_RX) & \
360 (uint32_t)(~((uint32_t)HAL_I2C_STATE_READY))))
361/*!< Mask State define, keep only RX and TX bits */
362#define I2C_STATE_NONE ((uint32_t)(HAL_I2C_MODE_NONE))
363/*!< Default Value */
364#define I2C_STATE_MASTER_BUSY_TX ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_TX & I2C_STATE_MSK) | \
365 (uint32_t)HAL_I2C_MODE_MASTER))
366/*!< Master Busy TX, combinaison of State LSB and Mode enum */
367#define I2C_STATE_MASTER_BUSY_RX ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_RX & I2C_STATE_MSK) | \
368 (uint32_t)HAL_I2C_MODE_MASTER))
369/*!< Master Busy RX, combinaison of State LSB and Mode enum */
370#define I2C_STATE_SLAVE_BUSY_TX ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_TX & I2C_STATE_MSK) | \
371 (uint32_t)HAL_I2C_MODE_SLAVE))
372/*!< Slave Busy TX, combinaison of State LSB and Mode enum */
373#define I2C_STATE_SLAVE_BUSY_RX ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_RX & I2C_STATE_MSK) | \
374 (uint32_t)HAL_I2C_MODE_SLAVE))
375/*!< Slave Busy RX, combinaison of State LSB and Mode enum */
376#define I2C_STATE_MEM_BUSY_TX ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_TX & I2C_STATE_MSK) | \
377 (uint32_t)HAL_I2C_MODE_MEM))
378/*!< Memory Busy TX, combinaison of State LSB and Mode enum */
379#define I2C_STATE_MEM_BUSY_RX ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_RX & I2C_STATE_MSK) | \
380 (uint32_t)HAL_I2C_MODE_MEM))
381/*!< Memory Busy RX, combinaison of State LSB and Mode enum */
382
383
384/* Private define to centralize the enable/disable of Interrupts */
385#define I2C_XFER_TX_IT (uint16_t)(0x0001U) /*!< Bit field can be combinated with
386 @ref I2C_XFER_LISTEN_IT */
387#define I2C_XFER_RX_IT (uint16_t)(0x0002U) /*!< Bit field can be combinated with
388 @ref I2C_XFER_LISTEN_IT */
389#define I2C_XFER_LISTEN_IT (uint16_t)(0x8000U) /*!< Bit field can be combinated with @ref I2C_XFER_TX_IT
390 and @ref I2C_XFER_RX_IT */
391
392#define I2C_XFER_ERROR_IT (uint16_t)(0x0010U) /*!< Bit definition to manage addition of global Error
393 and NACK treatment */
394#define I2C_XFER_CPLT_IT (uint16_t)(0x0020U) /*!< Bit definition to manage only STOP evenement */
395#define I2C_XFER_RELOAD_IT (uint16_t)(0x0040U) /*!< Bit definition to manage only Reload of NBYTE */
396
397/* Private define Sequential Transfer Options default/reset value */
398#define I2C_NO_OPTION_FRAME (0xFFFF0000U)
399/**
400 * @}
401 */
402
403/* Private macros ------------------------------------------------------------*/
404/** @addtogroup I2C_Private_Macro
405 * @{
406 */
407/* Macro to get remaining data to transfer on DMA side */
408#define I2C_GET_DMA_REMAIN_DATA(__HANDLE__) __HAL_DMA_GET_COUNTER(__HANDLE__)
409/**
410 * @}
411 */
412
413/* Private variables ---------------------------------------------------------*/
414/* Private function prototypes -----------------------------------------------*/
415
416/** @defgroup I2C_Private_Functions I2C Private Functions
417 * @{
418 */
419/* Private functions to handle DMA transfer */
420static void I2C_DMAMasterTransmitCplt(DMA_HandleTypeDef *hdma);
421static void I2C_DMAMasterReceiveCplt(DMA_HandleTypeDef *hdma);
422static void I2C_DMASlaveTransmitCplt(DMA_HandleTypeDef *hdma);
423static void I2C_DMASlaveReceiveCplt(DMA_HandleTypeDef *hdma);
424static void I2C_DMAError(DMA_HandleTypeDef *hdma);
425static void I2C_DMAAbort(DMA_HandleTypeDef *hdma);
426
427
428/* Private functions to handle IT transfer */
429static void I2C_ITAddrCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags);
430static void I2C_ITMasterSeqCplt(I2C_HandleTypeDef *hi2c);
431static void I2C_ITSlaveSeqCplt(I2C_HandleTypeDef *hi2c);
432static void I2C_ITMasterCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags);
433static void I2C_ITSlaveCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags);
434static void I2C_ITListenCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags);
435static void I2C_ITError(I2C_HandleTypeDef *hi2c, uint32_t ErrorCode);
436
437/* Private functions to handle IT transfer */
438static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress,
439 uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout,
440 uint32_t Tickstart);
441static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress,
442 uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout,
443 uint32_t Tickstart);
444
445/* Private functions for I2C transfer IRQ handler */
446static HAL_StatusTypeDef I2C_Master_ISR_IT(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags,
447 uint32_t ITSources);
448static HAL_StatusTypeDef I2C_Mem_ISR_IT(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags,
449 uint32_t ITSources);
450static HAL_StatusTypeDef I2C_Slave_ISR_IT(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags,
451 uint32_t ITSources);
452static HAL_StatusTypeDef I2C_Master_ISR_DMA(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags,
453 uint32_t ITSources);
454static HAL_StatusTypeDef I2C_Mem_ISR_DMA(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags,
455 uint32_t ITSources);
456static HAL_StatusTypeDef I2C_Slave_ISR_DMA(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags,
457 uint32_t ITSources);
458
459/* Private functions to handle flags during polling transfer */
460static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status,
461 uint32_t Timeout, uint32_t Tickstart);
462static HAL_StatusTypeDef I2C_WaitOnTXISFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout,
463 uint32_t Tickstart);
464static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout,
465 uint32_t Tickstart);
466static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout,
467 uint32_t Tickstart);
468static HAL_StatusTypeDef I2C_IsErrorOccurred(I2C_HandleTypeDef *hi2c, uint32_t Timeout,
469 uint32_t Tickstart);
470
471/* Private functions to centralize the enable/disable of Interrupts */
472static void I2C_Enable_IRQ(I2C_HandleTypeDef *hi2c, uint16_t InterruptRequest);
473static void I2C_Disable_IRQ(I2C_HandleTypeDef *hi2c, uint16_t InterruptRequest);
474
475/* Private function to treat different error callback */
476static void I2C_TreatErrorCallback(I2C_HandleTypeDef *hi2c);
477
478/* Private function to flush TXDR register */
479static void I2C_Flush_TXDR(I2C_HandleTypeDef *hi2c);
480
481/* Private function to handle start, restart or stop a transfer */
482static void I2C_TransferConfig(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t Size, uint32_t Mode,
483 uint32_t Request);
484
485/* Private function to Convert Specific options */
486static void I2C_ConvertOtherXferOptions(I2C_HandleTypeDef *hi2c);
487/**
488 * @}
489 */
490
491/* Exported functions --------------------------------------------------------*/
492
493/** @defgroup I2C_Exported_Functions I2C Exported Functions
494 * @{
495 */
496
497/** @defgroup I2C_Exported_Functions_Group1 Initialization and de-initialization functions
498 * @brief Initialization and Configuration functions
499 *
500@verbatim
501 ===============================================================================
502 ##### Initialization and de-initialization functions #####
503 ===============================================================================
504 [..] This subsection provides a set of functions allowing to initialize and
505 deinitialize the I2Cx peripheral:
506
507 (+) User must Implement HAL_I2C_MspInit() function in which he configures
508 all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
509
510 (+) Call the function HAL_I2C_Init() to configure the selected device with
511 the selected configuration:
512 (++) Clock Timing
513 (++) Own Address 1
514 (++) Addressing mode (Master, Slave)
515 (++) Dual Addressing mode
516 (++) Own Address 2
517 (++) Own Address 2 Mask
518 (++) General call mode
519 (++) Nostretch mode
520
521 (+) Call the function HAL_I2C_DeInit() to restore the default configuration
522 of the selected I2Cx peripheral.
523
524@endverbatim
525 * @{
526 */
527
528/**
529 * @brief Initializes the I2C according to the specified parameters
530 * in the I2C_InitTypeDef and initialize the associated handle.
531 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
532 * the configuration information for the specified I2C.
533 * @retval HAL status
534 */
536{
537 /* Check the I2C handle allocation */
538 if (hi2c == NULL)
539 {
540 return HAL_ERROR;
541 }
542
543 /* Check the parameters */
552
553 if (hi2c->State == HAL_I2C_STATE_RESET)
554 {
555 /* Allocate lock resource and initialize it */
556 hi2c->Lock = HAL_UNLOCKED;
557
558#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
559 /* Init the I2C Callback settings */
560 hi2c->MasterTxCpltCallback = HAL_I2C_MasterTxCpltCallback; /* Legacy weak MasterTxCpltCallback */
561 hi2c->MasterRxCpltCallback = HAL_I2C_MasterRxCpltCallback; /* Legacy weak MasterRxCpltCallback */
562 hi2c->SlaveTxCpltCallback = HAL_I2C_SlaveTxCpltCallback; /* Legacy weak SlaveTxCpltCallback */
563 hi2c->SlaveRxCpltCallback = HAL_I2C_SlaveRxCpltCallback; /* Legacy weak SlaveRxCpltCallback */
564 hi2c->ListenCpltCallback = HAL_I2C_ListenCpltCallback; /* Legacy weak ListenCpltCallback */
565 hi2c->MemTxCpltCallback = HAL_I2C_MemTxCpltCallback; /* Legacy weak MemTxCpltCallback */
566 hi2c->MemRxCpltCallback = HAL_I2C_MemRxCpltCallback; /* Legacy weak MemRxCpltCallback */
567 hi2c->ErrorCallback = HAL_I2C_ErrorCallback; /* Legacy weak ErrorCallback */
568 hi2c->AbortCpltCallback = HAL_I2C_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
569 hi2c->AddrCallback = HAL_I2C_AddrCallback; /* Legacy weak AddrCallback */
570
571 if (hi2c->MspInitCallback == NULL)
572 {
573 hi2c->MspInitCallback = HAL_I2C_MspInit; /* Legacy weak MspInit */
574 }
575
576 /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
577 hi2c->MspInitCallback(hi2c);
578#else
579 /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
580 HAL_I2C_MspInit(hi2c);
581#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
582 }
583
585
586 /* Disable the selected I2C peripheral */
587 __HAL_I2C_DISABLE(hi2c);
588
589 /*---------------------------- I2Cx TIMINGR Configuration ------------------*/
590 /* Configure I2Cx: Frequency range */
591 hi2c->Instance->TIMINGR = hi2c->Init.Timing & TIMING_CLEAR_MASK;
592
593 /*---------------------------- I2Cx OAR1 Configuration ---------------------*/
594 /* Disable Own Address1 before set the Own Address1 configuration */
595 hi2c->Instance->OAR1 &= ~I2C_OAR1_OA1EN;
596
597 /* Configure I2Cx: Own Address1 and ack own address1 mode */
599 {
600 hi2c->Instance->OAR1 = (I2C_OAR1_OA1EN | hi2c->Init.OwnAddress1);
601 }
602 else /* I2C_ADDRESSINGMODE_10BIT */
603 {
605 }
606
607 /*---------------------------- I2Cx CR2 Configuration ----------------------*/
608 /* Configure I2Cx: Addressing Master mode */
610 {
612 }
613 else
614 {
615 /* Clear the I2C ADD10 bit */
617 }
618 /* Enable the AUTOEND by default, and enable NACK (should be disable only during Slave process */
620
621 /*---------------------------- I2Cx OAR2 Configuration ---------------------*/
622 /* Disable Own Address2 before set the Own Address2 configuration */
624
625 /* Configure I2Cx: Dual mode and Own Address2 */
626 hi2c->Instance->OAR2 = (hi2c->Init.DualAddressMode | hi2c->Init.OwnAddress2 | \
627 (hi2c->Init.OwnAddress2Masks << 8));
628
629 /*---------------------------- I2Cx CR1 Configuration ----------------------*/
630 /* Configure I2Cx: Generalcall and NoStretch mode */
631 hi2c->Instance->CR1 = (hi2c->Init.GeneralCallMode | hi2c->Init.NoStretchMode);
632
633 /* Enable the selected I2C peripheral */
634 __HAL_I2C_ENABLE(hi2c);
635
638 hi2c->PreviousState = I2C_STATE_NONE;
639 hi2c->Mode = HAL_I2C_MODE_NONE;
640
641 return HAL_OK;
642}
643
644/**
645 * @brief DeInitialize the I2C peripheral.
646 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
647 * the configuration information for the specified I2C.
648 * @retval HAL status
649 */
651{
652 /* Check the I2C handle allocation */
653 if (hi2c == NULL)
654 {
655 return HAL_ERROR;
656 }
657
658 /* Check the parameters */
660
662
663 /* Disable the I2C Peripheral Clock */
664 __HAL_I2C_DISABLE(hi2c);
665
666#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
667 if (hi2c->MspDeInitCallback == NULL)
668 {
669 hi2c->MspDeInitCallback = HAL_I2C_MspDeInit; /* Legacy weak MspDeInit */
670 }
671
672 /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
673 hi2c->MspDeInitCallback(hi2c);
674#else
675 /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
676 HAL_I2C_MspDeInit(hi2c);
677#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
678
681 hi2c->PreviousState = I2C_STATE_NONE;
682 hi2c->Mode = HAL_I2C_MODE_NONE;
683
684 /* Release Lock */
685 __HAL_UNLOCK(hi2c);
686
687 return HAL_OK;
688}
689
690/**
691 * @brief Initialize the I2C MSP.
692 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
693 * the configuration information for the specified I2C.
694 * @retval None
695 */
696__weak void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)
697{
698 /* Prevent unused argument(s) compilation warning */
699 UNUSED(hi2c);
700
701 /* NOTE : This function should not be modified, when the callback is needed,
702 the HAL_I2C_MspInit could be implemented in the user file
703 */
704}
705
706/**
707 * @brief DeInitialize the I2C MSP.
708 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
709 * the configuration information for the specified I2C.
710 * @retval None
711 */
712__weak void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c)
713{
714 /* Prevent unused argument(s) compilation warning */
715 UNUSED(hi2c);
716
717 /* NOTE : This function should not be modified, when the callback is needed,
718 the HAL_I2C_MspDeInit could be implemented in the user file
719 */
720}
721
722#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
723/**
724 * @brief Register a User I2C Callback
725 * To be used instead of the weak predefined callback
726 * @note The HAL_I2C_RegisterCallback() may be called before HAL_I2C_Init() in HAL_I2C_STATE_RESET
727 * to register callbacks for HAL_I2C_MSPINIT_CB_ID and HAL_I2C_MSPDEINIT_CB_ID.
728 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
729 * the configuration information for the specified I2C.
730 * @param CallbackID ID of the callback to be registered
731 * This parameter can be one of the following values:
732 * @arg @ref HAL_I2C_MASTER_TX_COMPLETE_CB_ID Master Tx Transfer completed callback ID
733 * @arg @ref HAL_I2C_MASTER_RX_COMPLETE_CB_ID Master Rx Transfer completed callback ID
734 * @arg @ref HAL_I2C_SLAVE_TX_COMPLETE_CB_ID Slave Tx Transfer completed callback ID
735 * @arg @ref HAL_I2C_SLAVE_RX_COMPLETE_CB_ID Slave Rx Transfer completed callback ID
736 * @arg @ref HAL_I2C_LISTEN_COMPLETE_CB_ID Listen Complete callback ID
737 * @arg @ref HAL_I2C_MEM_TX_COMPLETE_CB_ID Memory Tx Transfer callback ID
738 * @arg @ref HAL_I2C_MEM_RX_COMPLETE_CB_ID Memory Rx Transfer completed callback ID
739 * @arg @ref HAL_I2C_ERROR_CB_ID Error callback ID
740 * @arg @ref HAL_I2C_ABORT_CB_ID Abort callback ID
741 * @arg @ref HAL_I2C_MSPINIT_CB_ID MspInit callback ID
742 * @arg @ref HAL_I2C_MSPDEINIT_CB_ID MspDeInit callback ID
743 * @param pCallback pointer to the Callback function
744 * @retval HAL status
745 */
746HAL_StatusTypeDef HAL_I2C_RegisterCallback(I2C_HandleTypeDef *hi2c, HAL_I2C_CallbackIDTypeDef CallbackID,
747 pI2C_CallbackTypeDef pCallback)
748{
750
751 if (pCallback == NULL)
752 {
753 /* Update the error code */
754 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
755
756 return HAL_ERROR;
757 }
758
759 if (HAL_I2C_STATE_READY == hi2c->State)
760 {
761 switch (CallbackID)
762 {
763 case HAL_I2C_MASTER_TX_COMPLETE_CB_ID :
764 hi2c->MasterTxCpltCallback = pCallback;
765 break;
766
767 case HAL_I2C_MASTER_RX_COMPLETE_CB_ID :
768 hi2c->MasterRxCpltCallback = pCallback;
769 break;
770
771 case HAL_I2C_SLAVE_TX_COMPLETE_CB_ID :
772 hi2c->SlaveTxCpltCallback = pCallback;
773 break;
774
775 case HAL_I2C_SLAVE_RX_COMPLETE_CB_ID :
776 hi2c->SlaveRxCpltCallback = pCallback;
777 break;
778
779 case HAL_I2C_LISTEN_COMPLETE_CB_ID :
780 hi2c->ListenCpltCallback = pCallback;
781 break;
782
783 case HAL_I2C_MEM_TX_COMPLETE_CB_ID :
784 hi2c->MemTxCpltCallback = pCallback;
785 break;
786
787 case HAL_I2C_MEM_RX_COMPLETE_CB_ID :
788 hi2c->MemRxCpltCallback = pCallback;
789 break;
790
791 case HAL_I2C_ERROR_CB_ID :
792 hi2c->ErrorCallback = pCallback;
793 break;
794
795 case HAL_I2C_ABORT_CB_ID :
796 hi2c->AbortCpltCallback = pCallback;
797 break;
798
799 case HAL_I2C_MSPINIT_CB_ID :
800 hi2c->MspInitCallback = pCallback;
801 break;
802
803 case HAL_I2C_MSPDEINIT_CB_ID :
804 hi2c->MspDeInitCallback = pCallback;
805 break;
806
807 default :
808 /* Update the error code */
809 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
810
811 /* Return error status */
813 break;
814 }
815 }
816 else if (HAL_I2C_STATE_RESET == hi2c->State)
817 {
818 switch (CallbackID)
819 {
820 case HAL_I2C_MSPINIT_CB_ID :
821 hi2c->MspInitCallback = pCallback;
822 break;
823
824 case HAL_I2C_MSPDEINIT_CB_ID :
825 hi2c->MspDeInitCallback = pCallback;
826 break;
827
828 default :
829 /* Update the error code */
830 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
831
832 /* Return error status */
834 break;
835 }
836 }
837 else
838 {
839 /* Update the error code */
840 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
841
842 /* Return error status */
844 }
845
846 return status;
847}
848
849/**
850 * @brief Unregister an I2C Callback
851 * I2C callback is redirected to the weak predefined callback
852 * @note The HAL_I2C_UnRegisterCallback() may be called before HAL_I2C_Init() in HAL_I2C_STATE_RESET
853 * to un-register callbacks for HAL_I2C_MSPINIT_CB_ID and HAL_I2C_MSPDEINIT_CB_ID.
854 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
855 * the configuration information for the specified I2C.
856 * @param CallbackID ID of the callback to be unregistered
857 * This parameter can be one of the following values:
858 * This parameter can be one of the following values:
859 * @arg @ref HAL_I2C_MASTER_TX_COMPLETE_CB_ID Master Tx Transfer completed callback ID
860 * @arg @ref HAL_I2C_MASTER_RX_COMPLETE_CB_ID Master Rx Transfer completed callback ID
861 * @arg @ref HAL_I2C_SLAVE_TX_COMPLETE_CB_ID Slave Tx Transfer completed callback ID
862 * @arg @ref HAL_I2C_SLAVE_RX_COMPLETE_CB_ID Slave Rx Transfer completed callback ID
863 * @arg @ref HAL_I2C_LISTEN_COMPLETE_CB_ID Listen Complete callback ID
864 * @arg @ref HAL_I2C_MEM_TX_COMPLETE_CB_ID Memory Tx Transfer callback ID
865 * @arg @ref HAL_I2C_MEM_RX_COMPLETE_CB_ID Memory Rx Transfer completed callback ID
866 * @arg @ref HAL_I2C_ERROR_CB_ID Error callback ID
867 * @arg @ref HAL_I2C_ABORT_CB_ID Abort callback ID
868 * @arg @ref HAL_I2C_MSPINIT_CB_ID MspInit callback ID
869 * @arg @ref HAL_I2C_MSPDEINIT_CB_ID MspDeInit callback ID
870 * @retval HAL status
871 */
872HAL_StatusTypeDef HAL_I2C_UnRegisterCallback(I2C_HandleTypeDef *hi2c, HAL_I2C_CallbackIDTypeDef CallbackID)
873{
875
876 if (HAL_I2C_STATE_READY == hi2c->State)
877 {
878 switch (CallbackID)
879 {
880 case HAL_I2C_MASTER_TX_COMPLETE_CB_ID :
881 hi2c->MasterTxCpltCallback = HAL_I2C_MasterTxCpltCallback; /* Legacy weak MasterTxCpltCallback */
882 break;
883
884 case HAL_I2C_MASTER_RX_COMPLETE_CB_ID :
885 hi2c->MasterRxCpltCallback = HAL_I2C_MasterRxCpltCallback; /* Legacy weak MasterRxCpltCallback */
886 break;
887
888 case HAL_I2C_SLAVE_TX_COMPLETE_CB_ID :
889 hi2c->SlaveTxCpltCallback = HAL_I2C_SlaveTxCpltCallback; /* Legacy weak SlaveTxCpltCallback */
890 break;
891
892 case HAL_I2C_SLAVE_RX_COMPLETE_CB_ID :
893 hi2c->SlaveRxCpltCallback = HAL_I2C_SlaveRxCpltCallback; /* Legacy weak SlaveRxCpltCallback */
894 break;
895
896 case HAL_I2C_LISTEN_COMPLETE_CB_ID :
897 hi2c->ListenCpltCallback = HAL_I2C_ListenCpltCallback; /* Legacy weak ListenCpltCallback */
898 break;
899
900 case HAL_I2C_MEM_TX_COMPLETE_CB_ID :
901 hi2c->MemTxCpltCallback = HAL_I2C_MemTxCpltCallback; /* Legacy weak MemTxCpltCallback */
902 break;
903
904 case HAL_I2C_MEM_RX_COMPLETE_CB_ID :
905 hi2c->MemRxCpltCallback = HAL_I2C_MemRxCpltCallback; /* Legacy weak MemRxCpltCallback */
906 break;
907
908 case HAL_I2C_ERROR_CB_ID :
909 hi2c->ErrorCallback = HAL_I2C_ErrorCallback; /* Legacy weak ErrorCallback */
910 break;
911
912 case HAL_I2C_ABORT_CB_ID :
913 hi2c->AbortCpltCallback = HAL_I2C_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
914 break;
915
916 case HAL_I2C_MSPINIT_CB_ID :
917 hi2c->MspInitCallback = HAL_I2C_MspInit; /* Legacy weak MspInit */
918 break;
919
920 case HAL_I2C_MSPDEINIT_CB_ID :
921 hi2c->MspDeInitCallback = HAL_I2C_MspDeInit; /* Legacy weak MspDeInit */
922 break;
923
924 default :
925 /* Update the error code */
926 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
927
928 /* Return error status */
930 break;
931 }
932 }
933 else if (HAL_I2C_STATE_RESET == hi2c->State)
934 {
935 switch (CallbackID)
936 {
937 case HAL_I2C_MSPINIT_CB_ID :
938 hi2c->MspInitCallback = HAL_I2C_MspInit; /* Legacy weak MspInit */
939 break;
940
941 case HAL_I2C_MSPDEINIT_CB_ID :
942 hi2c->MspDeInitCallback = HAL_I2C_MspDeInit; /* Legacy weak MspDeInit */
943 break;
944
945 default :
946 /* Update the error code */
947 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
948
949 /* Return error status */
951 break;
952 }
953 }
954 else
955 {
956 /* Update the error code */
957 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
958
959 /* Return error status */
961 }
962
963 return status;
964}
965
966/**
967 * @brief Register the Slave Address Match I2C Callback
968 * To be used instead of the weak HAL_I2C_AddrCallback() predefined callback
969 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
970 * the configuration information for the specified I2C.
971 * @param pCallback pointer to the Address Match Callback function
972 * @retval HAL status
973 */
974HAL_StatusTypeDef HAL_I2C_RegisterAddrCallback(I2C_HandleTypeDef *hi2c, pI2C_AddrCallbackTypeDef pCallback)
975{
977
978 if (pCallback == NULL)
979 {
980 /* Update the error code */
981 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
982
983 return HAL_ERROR;
984 }
985
986 if (HAL_I2C_STATE_READY == hi2c->State)
987 {
988 hi2c->AddrCallback = pCallback;
989 }
990 else
991 {
992 /* Update the error code */
993 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
994
995 /* Return error status */
997 }
998
999 return status;
1000}
1001
1002/**
1003 * @brief UnRegister the Slave Address Match I2C Callback
1004 * Info Ready I2C Callback is redirected to the weak HAL_I2C_AddrCallback() predefined callback
1005 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
1006 * the configuration information for the specified I2C.
1007 * @retval HAL status
1008 */
1009HAL_StatusTypeDef HAL_I2C_UnRegisterAddrCallback(I2C_HandleTypeDef *hi2c)
1010{
1012
1013 if (HAL_I2C_STATE_READY == hi2c->State)
1014 {
1015 hi2c->AddrCallback = HAL_I2C_AddrCallback; /* Legacy weak AddrCallback */
1016 }
1017 else
1018 {
1019 /* Update the error code */
1020 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
1021
1022 /* Return error status */
1023 status = HAL_ERROR;
1024 }
1025
1026 return status;
1027}
1028
1029#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
1030
1031/**
1032 * @}
1033 */
1034
1035/** @defgroup I2C_Exported_Functions_Group2 Input and Output operation functions
1036 * @brief Data transfers functions
1037 *
1038@verbatim
1039 ===============================================================================
1040 ##### IO operation functions #####
1041 ===============================================================================
1042 [..]
1043 This subsection provides a set of functions allowing to manage the I2C data
1044 transfers.
1045
1046 (#) There are two modes of transfer:
1047 (++) Blocking mode : The communication is performed in the polling mode.
1048 The status of all data processing is returned by the same function
1049 after finishing transfer.
1050 (++) No-Blocking mode : The communication is performed using Interrupts
1051 or DMA. These functions return the status of the transfer startup.
1052 The end of the data processing will be indicated through the
1053 dedicated I2C IRQ when using Interrupt mode or the DMA IRQ when
1054 using DMA mode.
1055
1056 (#) Blocking mode functions are :
1057 (++) HAL_I2C_Master_Transmit()
1058 (++) HAL_I2C_Master_Receive()
1059 (++) HAL_I2C_Slave_Transmit()
1060 (++) HAL_I2C_Slave_Receive()
1061 (++) HAL_I2C_Mem_Write()
1062 (++) HAL_I2C_Mem_Read()
1063 (++) HAL_I2C_IsDeviceReady()
1064
1065 (#) No-Blocking mode functions with Interrupt are :
1066 (++) HAL_I2C_Master_Transmit_IT()
1067 (++) HAL_I2C_Master_Receive_IT()
1068 (++) HAL_I2C_Slave_Transmit_IT()
1069 (++) HAL_I2C_Slave_Receive_IT()
1070 (++) HAL_I2C_Mem_Write_IT()
1071 (++) HAL_I2C_Mem_Read_IT()
1072 (++) HAL_I2C_Master_Seq_Transmit_IT()
1073 (++) HAL_I2C_Master_Seq_Receive_IT()
1074 (++) HAL_I2C_Slave_Seq_Transmit_IT()
1075 (++) HAL_I2C_Slave_Seq_Receive_IT()
1076 (++) HAL_I2C_EnableListen_IT()
1077 (++) HAL_I2C_DisableListen_IT()
1078 (++) HAL_I2C_Master_Abort_IT()
1079
1080 (#) No-Blocking mode functions with DMA are :
1081 (++) HAL_I2C_Master_Transmit_DMA()
1082 (++) HAL_I2C_Master_Receive_DMA()
1083 (++) HAL_I2C_Slave_Transmit_DMA()
1084 (++) HAL_I2C_Slave_Receive_DMA()
1085 (++) HAL_I2C_Mem_Write_DMA()
1086 (++) HAL_I2C_Mem_Read_DMA()
1087 (++) HAL_I2C_Master_Seq_Transmit_DMA()
1088 (++) HAL_I2C_Master_Seq_Receive_DMA()
1089 (++) HAL_I2C_Slave_Seq_Transmit_DMA()
1090 (++) HAL_I2C_Slave_Seq_Receive_DMA()
1091
1092 (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
1093 (++) HAL_I2C_MasterTxCpltCallback()
1094 (++) HAL_I2C_MasterRxCpltCallback()
1095 (++) HAL_I2C_SlaveTxCpltCallback()
1096 (++) HAL_I2C_SlaveRxCpltCallback()
1097 (++) HAL_I2C_MemTxCpltCallback()
1098 (++) HAL_I2C_MemRxCpltCallback()
1099 (++) HAL_I2C_AddrCallback()
1100 (++) HAL_I2C_ListenCpltCallback()
1101 (++) HAL_I2C_ErrorCallback()
1102 (++) HAL_I2C_AbortCpltCallback()
1103
1104@endverbatim
1105 * @{
1106 */
1107
1108/**
1109 * @brief Transmits in master mode an amount of data in blocking mode.
1110 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
1111 * the configuration information for the specified I2C.
1112 * @param DevAddress Target device address: The device 7 bits address value
1113 * in datasheet must be shifted to the left before calling the interface
1114 * @param pData Pointer to data buffer
1115 * @param Size Amount of data to be sent
1116 * @param Timeout Timeout duration
1117 * @retval HAL status
1118 */
1119HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData,
1120 uint16_t Size, uint32_t Timeout)
1121{
1122 uint32_t tickstart;
1123 uint32_t xfermode;
1124
1125 if (hi2c->State == HAL_I2C_STATE_READY)
1126 {
1127 /* Process Locked */
1128 __HAL_LOCK(hi2c);
1129
1130 /* Init tickstart for timeout management*/
1131 tickstart = HAL_GetTick();
1132
1133 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY, tickstart) != HAL_OK)
1134 {
1135 return HAL_ERROR;
1136 }
1137
1139 hi2c->Mode = HAL_I2C_MODE_MASTER;
1141
1142 /* Prepare transfer parameters */
1143 hi2c->pBuffPtr = pData;
1144 hi2c->XferCount = Size;
1145 hi2c->XferISR = NULL;
1146
1147 if (hi2c->XferCount > MAX_NBYTE_SIZE)
1148 {
1149 hi2c->XferSize = MAX_NBYTE_SIZE;
1150 xfermode = I2C_RELOAD_MODE;
1151 }
1152 else
1153 {
1154 hi2c->XferSize = hi2c->XferCount;
1155 xfermode = I2C_AUTOEND_MODE;
1156 }
1157
1158 if (hi2c->XferSize > 0U)
1159 {
1160 /* Preload TX register */
1161 /* Write data to TXDR */
1162 hi2c->Instance->TXDR = *hi2c->pBuffPtr;
1163
1164 /* Increment Buffer pointer */
1165 hi2c->pBuffPtr++;
1166
1167 hi2c->XferCount--;
1168 hi2c->XferSize--;
1169
1170 /* Send Slave Address */
1171 /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
1172 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)(hi2c->XferSize + 1U), xfermode,
1174 }
1175 else
1176 {
1177 /* Send Slave Address */
1178 /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
1179 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode,
1181 }
1182
1183 while (hi2c->XferCount > 0U)
1184 {
1185 /* Wait until TXIS flag is set */
1186 if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
1187 {
1188 return HAL_ERROR;
1189 }
1190 /* Write data to TXDR */
1191 hi2c->Instance->TXDR = *hi2c->pBuffPtr;
1192
1193 /* Increment Buffer pointer */
1194 hi2c->pBuffPtr++;
1195
1196 hi2c->XferCount--;
1197 hi2c->XferSize--;
1198
1199 if ((hi2c->XferCount != 0U) && (hi2c->XferSize == 0U))
1200 {
1201 /* Wait until TCR flag is set */
1202 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout, tickstart) != HAL_OK)
1203 {
1204 return HAL_ERROR;
1205 }
1206
1207 if (hi2c->XferCount > MAX_NBYTE_SIZE)
1208 {
1209 hi2c->XferSize = MAX_NBYTE_SIZE;
1210 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_RELOAD_MODE,
1212 }
1213 else
1214 {
1215 hi2c->XferSize = hi2c->XferCount;
1216 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE,
1218 }
1219 }
1220 }
1221
1222 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
1223 /* Wait until STOPF flag is set */
1224 if (I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
1225 {
1226 return HAL_ERROR;
1227 }
1228
1229 /* Clear STOP Flag */
1231
1232 /* Clear Configuration Register 2 */
1233 I2C_RESET_CR2(hi2c);
1234
1235 hi2c->State = HAL_I2C_STATE_READY;
1236 hi2c->Mode = HAL_I2C_MODE_NONE;
1237
1238 /* Process Unlocked */
1239 __HAL_UNLOCK(hi2c);
1240
1241 return HAL_OK;
1242 }
1243 else
1244 {
1245 return HAL_BUSY;
1246 }
1247}
1248
1249/**
1250 * @brief Receives in master mode an amount of data in blocking mode.
1251 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
1252 * the configuration information for the specified I2C.
1253 * @param DevAddress Target device address: The device 7 bits address value
1254 * in datasheet must be shifted to the left before calling the interface
1255 * @param pData Pointer to data buffer
1256 * @param Size Amount of data to be sent
1257 * @param Timeout Timeout duration
1258 * @retval HAL status
1259 */
1260HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData,
1261 uint16_t Size, uint32_t Timeout)
1262{
1263 uint32_t tickstart;
1264
1265 if (hi2c->State == HAL_I2C_STATE_READY)
1266 {
1267 /* Process Locked */
1268 __HAL_LOCK(hi2c);
1269
1270 /* Init tickstart for timeout management*/
1271 tickstart = HAL_GetTick();
1272
1273 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY, tickstart) != HAL_OK)
1274 {
1275 return HAL_ERROR;
1276 }
1277
1279 hi2c->Mode = HAL_I2C_MODE_MASTER;
1281
1282 /* Prepare transfer parameters */
1283 hi2c->pBuffPtr = pData;
1284 hi2c->XferCount = Size;
1285 hi2c->XferISR = NULL;
1286
1287 /* Send Slave Address */
1288 /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
1289 if (hi2c->XferCount > MAX_NBYTE_SIZE)
1290 {
1291 hi2c->XferSize = MAX_NBYTE_SIZE;
1292 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_RELOAD_MODE,
1294 }
1295 else
1296 {
1297 hi2c->XferSize = hi2c->XferCount;
1298 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE,
1300 }
1301
1302 while (hi2c->XferCount > 0U)
1303 {
1304 /* Wait until RXNE flag is set */
1305 if (I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
1306 {
1307 return HAL_ERROR;
1308 }
1309
1310 /* Read data from RXDR */
1311 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR;
1312
1313 /* Increment Buffer pointer */
1314 hi2c->pBuffPtr++;
1315
1316 hi2c->XferSize--;
1317 hi2c->XferCount--;
1318
1319 if ((hi2c->XferCount != 0U) && (hi2c->XferSize == 0U))
1320 {
1321 /* Wait until TCR flag is set */
1322 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout, tickstart) != HAL_OK)
1323 {
1324 return HAL_ERROR;
1325 }
1326
1327 if (hi2c->XferCount > MAX_NBYTE_SIZE)
1328 {
1329 hi2c->XferSize = MAX_NBYTE_SIZE;
1330 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_RELOAD_MODE,
1332 }
1333 else
1334 {
1335 hi2c->XferSize = hi2c->XferCount;
1336 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE,
1338 }
1339 }
1340 }
1341
1342 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
1343 /* Wait until STOPF flag is set */
1344 if (I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
1345 {
1346 return HAL_ERROR;
1347 }
1348
1349 /* Clear STOP Flag */
1351
1352 /* Clear Configuration Register 2 */
1353 I2C_RESET_CR2(hi2c);
1354
1355 hi2c->State = HAL_I2C_STATE_READY;
1356 hi2c->Mode = HAL_I2C_MODE_NONE;
1357
1358 /* Process Unlocked */
1359 __HAL_UNLOCK(hi2c);
1360
1361 return HAL_OK;
1362 }
1363 else
1364 {
1365 return HAL_BUSY;
1366 }
1367}
1368
1369/**
1370 * @brief Transmits in slave mode an amount of data in blocking mode.
1371 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
1372 * the configuration information for the specified I2C.
1373 * @param pData Pointer to data buffer
1374 * @param Size Amount of data to be sent
1375 * @param Timeout Timeout duration
1376 * @retval HAL status
1377 */
1378HAL_StatusTypeDef HAL_I2C_Slave_Transmit(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size,
1379 uint32_t Timeout)
1380{
1381 uint32_t tickstart;
1382 uint16_t tmpXferCount;
1384
1385 if (hi2c->State == HAL_I2C_STATE_READY)
1386 {
1387 if ((pData == NULL) || (Size == 0U))
1388 {
1390 return HAL_ERROR;
1391 }
1392 /* Process Locked */
1393 __HAL_LOCK(hi2c);
1394
1395 /* Init tickstart for timeout management*/
1396 tickstart = HAL_GetTick();
1397
1399 hi2c->Mode = HAL_I2C_MODE_SLAVE;
1401
1402 /* Prepare transfer parameters */
1403 hi2c->pBuffPtr = pData;
1404 hi2c->XferCount = Size;
1405 hi2c->XferISR = NULL;
1406
1407 /* Enable Address Acknowledge */
1408 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
1409
1410 /* Preload TX data if no stretch enable */
1412 {
1413 /* Preload TX register */
1414 /* Write data to TXDR */
1415 hi2c->Instance->TXDR = *hi2c->pBuffPtr;
1416
1417 /* Increment Buffer pointer */
1418 hi2c->pBuffPtr++;
1419
1420 hi2c->XferCount--;
1421 }
1422
1423 /* Wait until ADDR flag is set */
1424 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK)
1425 {
1426 /* Disable Address Acknowledge */
1427 hi2c->Instance->CR2 |= I2C_CR2_NACK;
1428
1429 /* Flush TX register */
1430 I2C_Flush_TXDR(hi2c);
1431
1432 return HAL_ERROR;
1433 }
1434
1435 /* Clear ADDR flag */
1437
1438 /* If 10bit addressing mode is selected */
1440 {
1441 /* Wait until ADDR flag is set */
1442 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK)
1443 {
1444 /* Disable Address Acknowledge */
1445 hi2c->Instance->CR2 |= I2C_CR2_NACK;
1446
1447 /* Flush TX register */
1448 I2C_Flush_TXDR(hi2c);
1449
1450 return HAL_ERROR;
1451 }
1452
1453 /* Clear ADDR flag */
1455 }
1456
1457 /* Wait until DIR flag is set Transmitter mode */
1458 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_DIR, RESET, Timeout, tickstart) != HAL_OK)
1459 {
1460 /* Disable Address Acknowledge */
1461 hi2c->Instance->CR2 |= I2C_CR2_NACK;
1462
1463 /* Flush TX register */
1464 I2C_Flush_TXDR(hi2c);
1465
1466 return HAL_ERROR;
1467 }
1468
1469 while (hi2c->XferCount > 0U)
1470 {
1471 /* Wait until TXIS flag is set */
1472 if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
1473 {
1474 /* Disable Address Acknowledge */
1475 hi2c->Instance->CR2 |= I2C_CR2_NACK;
1476 return HAL_ERROR;
1477 }
1478
1479 /* Write data to TXDR */
1480 hi2c->Instance->TXDR = *hi2c->pBuffPtr;
1481
1482 /* Increment Buffer pointer */
1483 hi2c->pBuffPtr++;
1484
1485 hi2c->XferCount--;
1486 }
1487
1488 /* Wait until AF flag is set */
1489 error = I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_AF, RESET, Timeout, tickstart);
1490
1491 if (error != HAL_OK)
1492 {
1493 /* Check that I2C transfer finished */
1494 /* if yes, normal use case, a NACK is sent by the MASTER when Transfer is finished */
1495 /* Mean XferCount == 0 */
1496
1497 tmpXferCount = hi2c->XferCount;
1498 if ((hi2c->ErrorCode == HAL_I2C_ERROR_AF) && (tmpXferCount == 0U))
1499 {
1500 /* Reset ErrorCode to NONE */
1502 }
1503 else
1504 {
1505 /* Disable Address Acknowledge */
1506 hi2c->Instance->CR2 |= I2C_CR2_NACK;
1507 return HAL_ERROR;
1508 }
1509 }
1510 else
1511 {
1512 /* Flush TX register */
1513 I2C_Flush_TXDR(hi2c);
1514
1515 /* Clear AF flag */
1517
1518 /* Wait until STOP flag is set */
1519 if (I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
1520 {
1521 /* Disable Address Acknowledge */
1522 hi2c->Instance->CR2 |= I2C_CR2_NACK;
1523
1524 return HAL_ERROR;
1525 }
1526
1527 /* Clear STOP flag */
1529 }
1530
1531 /* Wait until BUSY flag is reset */
1532 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout, tickstart) != HAL_OK)
1533 {
1534 /* Disable Address Acknowledge */
1535 hi2c->Instance->CR2 |= I2C_CR2_NACK;
1536 return HAL_ERROR;
1537 }
1538
1539 /* Disable Address Acknowledge */
1540 hi2c->Instance->CR2 |= I2C_CR2_NACK;
1541
1542 hi2c->State = HAL_I2C_STATE_READY;
1543 hi2c->Mode = HAL_I2C_MODE_NONE;
1544
1545 /* Process Unlocked */
1546 __HAL_UNLOCK(hi2c);
1547
1548 return HAL_OK;
1549 }
1550 else
1551 {
1552 return HAL_BUSY;
1553 }
1554}
1555
1556/**
1557 * @brief Receive in slave mode an amount of data in blocking mode
1558 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
1559 * the configuration information for the specified I2C.
1560 * @param pData Pointer to data buffer
1561 * @param Size Amount of data to be sent
1562 * @param Timeout Timeout duration
1563 * @retval HAL status
1564 */
1565HAL_StatusTypeDef HAL_I2C_Slave_Receive(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size,
1566 uint32_t Timeout)
1567{
1568 uint32_t tickstart;
1569
1570 if (hi2c->State == HAL_I2C_STATE_READY)
1571 {
1572 if ((pData == NULL) || (Size == 0U))
1573 {
1575 return HAL_ERROR;
1576 }
1577 /* Process Locked */
1578 __HAL_LOCK(hi2c);
1579
1580 /* Init tickstart for timeout management*/
1581 tickstart = HAL_GetTick();
1582
1584 hi2c->Mode = HAL_I2C_MODE_SLAVE;
1586
1587 /* Prepare transfer parameters */
1588 hi2c->pBuffPtr = pData;
1589 hi2c->XferCount = Size;
1590 hi2c->XferSize = hi2c->XferCount;
1591 hi2c->XferISR = NULL;
1592
1593 /* Enable Address Acknowledge */
1594 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
1595
1596 /* Wait until ADDR flag is set */
1597 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK)
1598 {
1599 /* Disable Address Acknowledge */
1600 hi2c->Instance->CR2 |= I2C_CR2_NACK;
1601 return HAL_ERROR;
1602 }
1603
1604 /* Clear ADDR flag */
1606
1607 /* Wait until DIR flag is reset Receiver mode */
1608 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_DIR, SET, Timeout, tickstart) != HAL_OK)
1609 {
1610 /* Disable Address Acknowledge */
1611 hi2c->Instance->CR2 |= I2C_CR2_NACK;
1612 return HAL_ERROR;
1613 }
1614
1615 while (hi2c->XferCount > 0U)
1616 {
1617 /* Wait until RXNE flag is set */
1618 if (I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
1619 {
1620 /* Disable Address Acknowledge */
1621 hi2c->Instance->CR2 |= I2C_CR2_NACK;
1622
1623 /* Store Last receive data if any */
1624 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
1625 {
1626 /* Read data from RXDR */
1627 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR;
1628
1629 /* Increment Buffer pointer */
1630 hi2c->pBuffPtr++;
1631
1632 hi2c->XferCount--;
1633 hi2c->XferSize--;
1634 }
1635
1636 return HAL_ERROR;
1637 }
1638
1639 /* Read data from RXDR */
1640 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR;
1641
1642 /* Increment Buffer pointer */
1643 hi2c->pBuffPtr++;
1644
1645 hi2c->XferCount--;
1646 hi2c->XferSize--;
1647 }
1648
1649 /* Wait until STOP flag is set */
1650 if (I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
1651 {
1652 /* Disable Address Acknowledge */
1653 hi2c->Instance->CR2 |= I2C_CR2_NACK;
1654 return HAL_ERROR;
1655 }
1656
1657 /* Clear STOP flag */
1659
1660 /* Wait until BUSY flag is reset */
1661 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout, tickstart) != HAL_OK)
1662 {
1663 /* Disable Address Acknowledge */
1664 hi2c->Instance->CR2 |= I2C_CR2_NACK;
1665 return HAL_ERROR;
1666 }
1667
1668 /* Disable Address Acknowledge */
1669 hi2c->Instance->CR2 |= I2C_CR2_NACK;
1670
1671 hi2c->State = HAL_I2C_STATE_READY;
1672 hi2c->Mode = HAL_I2C_MODE_NONE;
1673
1674 /* Process Unlocked */
1675 __HAL_UNLOCK(hi2c);
1676
1677 return HAL_OK;
1678 }
1679 else
1680 {
1681 return HAL_BUSY;
1682 }
1683}
1684
1685/**
1686 * @brief Transmit in master mode an amount of data in non-blocking mode with Interrupt
1687 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
1688 * the configuration information for the specified I2C.
1689 * @param DevAddress Target device address: The device 7 bits address value
1690 * in datasheet must be shifted to the left before calling the interface
1691 * @param pData Pointer to data buffer
1692 * @param Size Amount of data to be sent
1693 * @retval HAL status
1694 */
1695HAL_StatusTypeDef HAL_I2C_Master_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData,
1696 uint16_t Size)
1697{
1698 uint32_t xfermode;
1699
1700 if (hi2c->State == HAL_I2C_STATE_READY)
1701 {
1702 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1703 {
1704 return HAL_BUSY;
1705 }
1706
1707 /* Process Locked */
1708 __HAL_LOCK(hi2c);
1709
1711 hi2c->Mode = HAL_I2C_MODE_MASTER;
1713
1714 /* Prepare transfer parameters */
1715 hi2c->pBuffPtr = pData;
1716 hi2c->XferCount = Size;
1717 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
1718 hi2c->XferISR = I2C_Master_ISR_IT;
1719
1720 if (hi2c->XferCount > MAX_NBYTE_SIZE)
1721 {
1722 hi2c->XferSize = MAX_NBYTE_SIZE;
1723 xfermode = I2C_RELOAD_MODE;
1724 }
1725 else
1726 {
1727 hi2c->XferSize = hi2c->XferCount;
1728 xfermode = I2C_AUTOEND_MODE;
1729 }
1730
1731 /* Send Slave Address */
1732 /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE */
1733 if (hi2c->XferSize > 0U)
1734 {
1735 /* Preload TX register */
1736 /* Write data to TXDR */
1737 hi2c->Instance->TXDR = *hi2c->pBuffPtr;
1738
1739 /* Increment Buffer pointer */
1740 hi2c->pBuffPtr++;
1741
1742 hi2c->XferCount--;
1743 hi2c->XferSize--;
1744
1745 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)(hi2c->XferSize + 1U), xfermode,
1747 }
1748 else
1749 {
1750 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode,
1752 }
1753
1754 /* Process Unlocked */
1755 __HAL_UNLOCK(hi2c);
1756
1757 /* Note : The I2C interrupts must be enabled after unlocking current process
1758 to avoid the risk of I2C interrupt handle execution before current
1759 process unlock */
1760
1761 /* Enable ERR, TC, STOP, NACK, TXI interrupt */
1762 /* possible to enable all of these */
1763 /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI |
1764 I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
1765 I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT);
1766
1767 return HAL_OK;
1768 }
1769 else
1770 {
1771 return HAL_BUSY;
1772 }
1773}
1774
1775/**
1776 * @brief Receive in master mode an amount of data in non-blocking mode with Interrupt
1777 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
1778 * the configuration information for the specified I2C.
1779 * @param DevAddress Target device address: The device 7 bits address value
1780 * in datasheet must be shifted to the left before calling the interface
1781 * @param pData Pointer to data buffer
1782 * @param Size Amount of data to be sent
1783 * @retval HAL status
1784 */
1785HAL_StatusTypeDef HAL_I2C_Master_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData,
1786 uint16_t Size)
1787{
1788 uint32_t xfermode;
1789
1790 if (hi2c->State == HAL_I2C_STATE_READY)
1791 {
1792 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1793 {
1794 return HAL_BUSY;
1795 }
1796
1797 /* Process Locked */
1798 __HAL_LOCK(hi2c);
1799
1801 hi2c->Mode = HAL_I2C_MODE_MASTER;
1803
1804 /* Prepare transfer parameters */
1805 hi2c->pBuffPtr = pData;
1806 hi2c->XferCount = Size;
1807 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
1808 hi2c->XferISR = I2C_Master_ISR_IT;
1809
1810 if (hi2c->XferCount > MAX_NBYTE_SIZE)
1811 {
1812 hi2c->XferSize = MAX_NBYTE_SIZE;
1813 xfermode = I2C_RELOAD_MODE;
1814 }
1815 else
1816 {
1817 hi2c->XferSize = hi2c->XferCount;
1818 xfermode = I2C_AUTOEND_MODE;
1819 }
1820
1821 /* Send Slave Address */
1822 /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE */
1823 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, I2C_GENERATE_START_READ);
1824
1825 /* Process Unlocked */
1826 __HAL_UNLOCK(hi2c);
1827
1828 /* Note : The I2C interrupts must be enabled after unlocking current process
1829 to avoid the risk of I2C interrupt handle execution before current
1830 process unlock */
1831
1832 /* Enable ERR, TC, STOP, NACK, RXI interrupt */
1833 /* possible to enable all of these */
1834 /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI |
1835 I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
1836 I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT);
1837
1838 return HAL_OK;
1839 }
1840 else
1841 {
1842 return HAL_BUSY;
1843 }
1844}
1845
1846/**
1847 * @brief Transmit in slave mode an amount of data in non-blocking mode with Interrupt
1848 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
1849 * the configuration information for the specified I2C.
1850 * @param pData Pointer to data buffer
1851 * @param Size Amount of data to be sent
1852 * @retval HAL status
1853 */
1854HAL_StatusTypeDef HAL_I2C_Slave_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
1855{
1856 if (hi2c->State == HAL_I2C_STATE_READY)
1857 {
1858 /* Process Locked */
1859 __HAL_LOCK(hi2c);
1860
1862 hi2c->Mode = HAL_I2C_MODE_SLAVE;
1864
1865 /* Enable Address Acknowledge */
1866 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
1867
1868 /* Prepare transfer parameters */
1869 hi2c->pBuffPtr = pData;
1870 hi2c->XferCount = Size;
1871 hi2c->XferSize = hi2c->XferCount;
1872 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
1873 hi2c->XferISR = I2C_Slave_ISR_IT;
1874
1875 /* Preload TX data if no stretch enable */
1877 {
1878 /* Preload TX register */
1879 /* Write data to TXDR */
1880 hi2c->Instance->TXDR = *hi2c->pBuffPtr;
1881
1882 /* Increment Buffer pointer */
1883 hi2c->pBuffPtr++;
1884
1885 hi2c->XferCount--;
1886 hi2c->XferSize--;
1887 }
1888
1889 /* Process Unlocked */
1890 __HAL_UNLOCK(hi2c);
1891
1892 /* Note : The I2C interrupts must be enabled after unlocking current process
1893 to avoid the risk of I2C interrupt handle execution before current
1894 process unlock */
1895
1896 /* Enable ERR, TC, STOP, NACK, TXI interrupt */
1897 /* possible to enable all of these */
1898 /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI |
1899 I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
1900 I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT | I2C_XFER_LISTEN_IT);
1901
1902 return HAL_OK;
1903 }
1904 else
1905 {
1906 return HAL_BUSY;
1907 }
1908}
1909
1910/**
1911 * @brief Receive in slave mode an amount of data in non-blocking mode with Interrupt
1912 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
1913 * the configuration information for the specified I2C.
1914 * @param pData Pointer to data buffer
1915 * @param Size Amount of data to be sent
1916 * @retval HAL status
1917 */
1918HAL_StatusTypeDef HAL_I2C_Slave_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
1919{
1920 if (hi2c->State == HAL_I2C_STATE_READY)
1921 {
1922 /* Process Locked */
1923 __HAL_LOCK(hi2c);
1924
1926 hi2c->Mode = HAL_I2C_MODE_SLAVE;
1928
1929 /* Enable Address Acknowledge */
1930 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
1931
1932 /* Prepare transfer parameters */
1933 hi2c->pBuffPtr = pData;
1934 hi2c->XferCount = Size;
1935 hi2c->XferSize = hi2c->XferCount;
1936 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
1937 hi2c->XferISR = I2C_Slave_ISR_IT;
1938
1939 /* Process Unlocked */
1940 __HAL_UNLOCK(hi2c);
1941
1942 /* Note : The I2C interrupts must be enabled after unlocking current process
1943 to avoid the risk of I2C interrupt handle execution before current
1944 process unlock */
1945
1946 /* Enable ERR, TC, STOP, NACK, RXI interrupt */
1947 /* possible to enable all of these */
1948 /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI |
1949 I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
1950 I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT | I2C_XFER_LISTEN_IT);
1951
1952 return HAL_OK;
1953 }
1954 else
1955 {
1956 return HAL_BUSY;
1957 }
1958}
1959
1960/**
1961 * @brief Transmit in master mode an amount of data in non-blocking mode with DMA
1962 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
1963 * the configuration information for the specified I2C.
1964 * @param DevAddress Target device address: The device 7 bits address value
1965 * in datasheet must be shifted to the left before calling the interface
1966 * @param pData Pointer to data buffer
1967 * @param Size Amount of data to be sent
1968 * @retval HAL status
1969 */
1970HAL_StatusTypeDef HAL_I2C_Master_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData,
1971 uint16_t Size)
1972{
1973 uint32_t xfermode;
1974 HAL_StatusTypeDef dmaxferstatus;
1975 uint32_t sizetoxfer = 0U;
1976
1977 if (hi2c->State == HAL_I2C_STATE_READY)
1978 {
1979 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
1980 {
1981 return HAL_BUSY;
1982 }
1983
1984 /* Process Locked */
1985 __HAL_LOCK(hi2c);
1986
1988 hi2c->Mode = HAL_I2C_MODE_MASTER;
1990
1991 /* Prepare transfer parameters */
1992 hi2c->pBuffPtr = pData;
1993 hi2c->XferCount = Size;
1994 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
1995 hi2c->XferISR = I2C_Master_ISR_DMA;
1996
1997 if (hi2c->XferCount > MAX_NBYTE_SIZE)
1998 {
1999 hi2c->XferSize = MAX_NBYTE_SIZE;
2000 xfermode = I2C_RELOAD_MODE;
2001 }
2002 else
2003 {
2004 hi2c->XferSize = hi2c->XferCount;
2005 xfermode = I2C_AUTOEND_MODE;
2006 }
2007
2008 if (hi2c->XferSize > 0U)
2009 {
2010 /* Preload TX register */
2011 /* Write data to TXDR */
2012 hi2c->Instance->TXDR = *hi2c->pBuffPtr;
2013
2014 /* Increment Buffer pointer */
2015 hi2c->pBuffPtr++;
2016
2017 sizetoxfer = hi2c->XferSize;
2018 hi2c->XferCount--;
2019 hi2c->XferSize--;
2020 }
2021
2022 if (hi2c->XferSize > 0U)
2023 {
2024 if (hi2c->hdmatx != NULL)
2025 {
2026 /* Set the I2C DMA transfer complete callback */
2027 hi2c->hdmatx->XferCpltCallback = I2C_DMAMasterTransmitCplt;
2028
2029 /* Set the DMA error callback */
2030 hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
2031
2032 /* Set the unused DMA callbacks to NULL */
2034 hi2c->hdmatx->XferAbortCallback = NULL;
2035
2036 /* Enable the DMA stream or channel depends on Instance */
2037 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr,
2038 (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
2039 }
2040 else
2041 {
2042 /* Update I2C state */
2043 hi2c->State = HAL_I2C_STATE_READY;
2044 hi2c->Mode = HAL_I2C_MODE_NONE;
2045
2046 /* Update I2C error code */
2048
2049 /* Process Unlocked */
2050 __HAL_UNLOCK(hi2c);
2051
2052 return HAL_ERROR;
2053 }
2054
2055 if (dmaxferstatus == HAL_OK)
2056 {
2057 /* Send Slave Address */
2058 /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
2059 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)(hi2c->XferSize + 1U),
2060 xfermode, I2C_GENERATE_START_WRITE);
2061
2062 /* Update XferCount value */
2063 hi2c->XferCount -= hi2c->XferSize;
2064
2065 /* Process Unlocked */
2066 __HAL_UNLOCK(hi2c);
2067
2068 /* Note : The I2C interrupts must be enabled after unlocking current process
2069 to avoid the risk of I2C interrupt handle execution before current
2070 process unlock */
2071 /* Enable ERR and NACK interrupts */
2072 I2C_Enable_IRQ(hi2c, I2C_XFER_ERROR_IT);
2073
2074 /* Enable DMA Request */
2075 hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
2076 }
2077 else
2078 {
2079 /* Update I2C state */
2080 hi2c->State = HAL_I2C_STATE_READY;
2081 hi2c->Mode = HAL_I2C_MODE_NONE;
2082
2083 /* Update I2C error code */
2085
2086 /* Process Unlocked */
2087 __HAL_UNLOCK(hi2c);
2088
2089 return HAL_ERROR;
2090 }
2091 }
2092 else
2093 {
2094 /* Update Transfer ISR function pointer */
2095 hi2c->XferISR = I2C_Master_ISR_IT;
2096
2097 /* Send Slave Address */
2098 /* Set NBYTES to write and generate START condition */
2099 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)sizetoxfer, I2C_AUTOEND_MODE,
2101
2102 /* Process Unlocked */
2103 __HAL_UNLOCK(hi2c);
2104
2105 /* Note : The I2C interrupts must be enabled after unlocking current process
2106 to avoid the risk of I2C interrupt handle execution before current
2107 process unlock */
2108 /* Enable ERR, TC, STOP, NACK, TXI interrupt */
2109 /* possible to enable all of these */
2110 /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI |
2111 I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
2112 I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT);
2113 }
2114
2115 return HAL_OK;
2116 }
2117 else
2118 {
2119 return HAL_BUSY;
2120 }
2121}
2122
2123/**
2124 * @brief Receive in master mode an amount of data in non-blocking mode with DMA
2125 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
2126 * the configuration information for the specified I2C.
2127 * @param DevAddress Target device address: The device 7 bits address value
2128 * in datasheet must be shifted to the left before calling the interface
2129 * @param pData Pointer to data buffer
2130 * @param Size Amount of data to be sent
2131 * @retval HAL status
2132 */
2133HAL_StatusTypeDef HAL_I2C_Master_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData,
2134 uint16_t Size)
2135{
2136 uint32_t xfermode;
2137 HAL_StatusTypeDef dmaxferstatus;
2138
2139 if (hi2c->State == HAL_I2C_STATE_READY)
2140 {
2141 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
2142 {
2143 return HAL_BUSY;
2144 }
2145
2146 /* Process Locked */
2147 __HAL_LOCK(hi2c);
2148
2150 hi2c->Mode = HAL_I2C_MODE_MASTER;
2152
2153 /* Prepare transfer parameters */
2154 hi2c->pBuffPtr = pData;
2155 hi2c->XferCount = Size;
2156 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
2157 hi2c->XferISR = I2C_Master_ISR_DMA;
2158
2159 if (hi2c->XferCount > MAX_NBYTE_SIZE)
2160 {
2161 hi2c->XferSize = MAX_NBYTE_SIZE;
2162 xfermode = I2C_RELOAD_MODE;
2163 }
2164 else
2165 {
2166 hi2c->XferSize = hi2c->XferCount;
2167 xfermode = I2C_AUTOEND_MODE;
2168 }
2169
2170 if (hi2c->XferSize > 0U)
2171 {
2172 if (hi2c->hdmarx != NULL)
2173 {
2174 /* Set the I2C DMA transfer complete callback */
2175 hi2c->hdmarx->XferCpltCallback = I2C_DMAMasterReceiveCplt;
2176
2177 /* Set the DMA error callback */
2178 hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
2179
2180 /* Set the unused DMA callbacks to NULL */
2182 hi2c->hdmarx->XferAbortCallback = NULL;
2183
2184 /* Enable the DMA stream or channel depends on Instance */
2185 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData,
2186 hi2c->XferSize);
2187 }
2188 else
2189 {
2190 /* Update I2C state */
2191 hi2c->State = HAL_I2C_STATE_READY;
2192 hi2c->Mode = HAL_I2C_MODE_NONE;
2193
2194 /* Update I2C error code */
2196
2197 /* Process Unlocked */
2198 __HAL_UNLOCK(hi2c);
2199
2200 return HAL_ERROR;
2201 }
2202
2203 if (dmaxferstatus == HAL_OK)
2204 {
2205 /* Send Slave Address */
2206 /* Set NBYTES to read and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
2207 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, I2C_GENERATE_START_READ);
2208
2209 /* Update XferCount value */
2210 hi2c->XferCount -= hi2c->XferSize;
2211
2212 /* Process Unlocked */
2213 __HAL_UNLOCK(hi2c);
2214
2215 /* Note : The I2C interrupts must be enabled after unlocking current process
2216 to avoid the risk of I2C interrupt handle execution before current
2217 process unlock */
2218 /* Enable ERR and NACK interrupts */
2219 I2C_Enable_IRQ(hi2c, I2C_XFER_ERROR_IT);
2220
2221 /* Enable DMA Request */
2222 hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
2223 }
2224 else
2225 {
2226 /* Update I2C state */
2227 hi2c->State = HAL_I2C_STATE_READY;
2228 hi2c->Mode = HAL_I2C_MODE_NONE;
2229
2230 /* Update I2C error code */
2232
2233 /* Process Unlocked */
2234 __HAL_UNLOCK(hi2c);
2235
2236 return HAL_ERROR;
2237 }
2238 }
2239 else
2240 {
2241 /* Update Transfer ISR function pointer */
2242 hi2c->XferISR = I2C_Master_ISR_IT;
2243
2244 /* Send Slave Address */
2245 /* Set NBYTES to read and generate START condition */
2246 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE,
2248
2249 /* Process Unlocked */
2250 __HAL_UNLOCK(hi2c);
2251
2252 /* Note : The I2C interrupts must be enabled after unlocking current process
2253 to avoid the risk of I2C interrupt handle execution before current
2254 process unlock */
2255 /* Enable ERR, TC, STOP, NACK, RXI interrupt */
2256 /* possible to enable all of these */
2257 /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI |
2258 I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
2259 I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT);
2260 }
2261
2262 return HAL_OK;
2263 }
2264 else
2265 {
2266 return HAL_BUSY;
2267 }
2268}
2269
2270/**
2271 * @brief Transmit in slave mode an amount of data in non-blocking mode with DMA
2272 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
2273 * the configuration information for the specified I2C.
2274 * @param pData Pointer to data buffer
2275 * @param Size Amount of data to be sent
2276 * @retval HAL status
2277 */
2278HAL_StatusTypeDef HAL_I2C_Slave_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
2279{
2280 HAL_StatusTypeDef dmaxferstatus;
2281
2282 if (hi2c->State == HAL_I2C_STATE_READY)
2283 {
2284 if ((pData == NULL) || (Size == 0U))
2285 {
2287 return HAL_ERROR;
2288 }
2289 /* Process Locked */
2290 __HAL_LOCK(hi2c);
2291
2293 hi2c->Mode = HAL_I2C_MODE_SLAVE;
2295
2296 /* Prepare transfer parameters */
2297 hi2c->pBuffPtr = pData;
2298 hi2c->XferCount = Size;
2299 hi2c->XferSize = hi2c->XferCount;
2300 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
2301 hi2c->XferISR = I2C_Slave_ISR_DMA;
2302
2303 /* Preload TX data if no stretch enable */
2305 {
2306 /* Preload TX register */
2307 /* Write data to TXDR */
2308 hi2c->Instance->TXDR = *hi2c->pBuffPtr;
2309
2310 /* Increment Buffer pointer */
2311 hi2c->pBuffPtr++;
2312
2313 hi2c->XferCount--;
2314 hi2c->XferSize--;
2315 }
2316
2317 if (hi2c->XferCount != 0U)
2318 {
2319 if (hi2c->hdmatx != NULL)
2320 {
2321 /* Set the I2C DMA transfer complete callback */
2322 hi2c->hdmatx->XferCpltCallback = I2C_DMASlaveTransmitCplt;
2323
2324 /* Set the DMA error callback */
2325 hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
2326
2327 /* Set the unused DMA callbacks to NULL */
2329 hi2c->hdmatx->XferAbortCallback = NULL;
2330
2331 /* Enable the DMA stream or channel depends on Instance */
2332 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx,
2333 (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->TXDR,
2334 hi2c->XferSize);
2335 }
2336 else
2337 {
2338 /* Update I2C state */
2340 hi2c->Mode = HAL_I2C_MODE_NONE;
2341
2342 /* Update I2C error code */
2344
2345 /* Process Unlocked */
2346 __HAL_UNLOCK(hi2c);
2347
2348 return HAL_ERROR;
2349 }
2350
2351 if (dmaxferstatus == HAL_OK)
2352 {
2353 /* Enable Address Acknowledge */
2354 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
2355
2356 /* Process Unlocked */
2357 __HAL_UNLOCK(hi2c);
2358
2359 /* Note : The I2C interrupts must be enabled after unlocking current process
2360 to avoid the risk of I2C interrupt handle execution before current
2361 process unlock */
2362 /* Enable ERR, STOP, NACK, ADDR interrupts */
2363 I2C_Enable_IRQ(hi2c, I2C_XFER_LISTEN_IT);
2364
2365 /* Enable DMA Request */
2366 hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
2367 }
2368 else
2369 {
2370 /* Update I2C state */
2372 hi2c->Mode = HAL_I2C_MODE_NONE;
2373
2374 /* Update I2C error code */
2376
2377 /* Process Unlocked */
2378 __HAL_UNLOCK(hi2c);
2379
2380 return HAL_ERROR;
2381 }
2382 }
2383 else
2384 {
2385 /* Enable Address Acknowledge */
2386 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
2387
2388 /* Process Unlocked */
2389 __HAL_UNLOCK(hi2c);
2390
2391 /* Note : The I2C interrupts must be enabled after unlocking current process
2392 to avoid the risk of I2C interrupt handle execution before current
2393 process unlock */
2394 /* Enable ERR, STOP, NACK, ADDR interrupts */
2395 I2C_Enable_IRQ(hi2c, I2C_XFER_LISTEN_IT);
2396 }
2397
2398 return HAL_OK;
2399 }
2400 else
2401 {
2402 return HAL_BUSY;
2403 }
2404}
2405
2406/**
2407 * @brief Receive in slave mode an amount of data in non-blocking mode with DMA
2408 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
2409 * the configuration information for the specified I2C.
2410 * @param pData Pointer to data buffer
2411 * @param Size Amount of data to be sent
2412 * @retval HAL status
2413 */
2414HAL_StatusTypeDef HAL_I2C_Slave_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
2415{
2416 HAL_StatusTypeDef dmaxferstatus;
2417
2418 if (hi2c->State == HAL_I2C_STATE_READY)
2419 {
2420 if ((pData == NULL) || (Size == 0U))
2421 {
2423 return HAL_ERROR;
2424 }
2425 /* Process Locked */
2426 __HAL_LOCK(hi2c);
2427
2429 hi2c->Mode = HAL_I2C_MODE_SLAVE;
2431
2432 /* Prepare transfer parameters */
2433 hi2c->pBuffPtr = pData;
2434 hi2c->XferCount = Size;
2435 hi2c->XferSize = hi2c->XferCount;
2436 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
2437 hi2c->XferISR = I2C_Slave_ISR_DMA;
2438
2439 if (hi2c->hdmarx != NULL)
2440 {
2441 /* Set the I2C DMA transfer complete callback */
2442 hi2c->hdmarx->XferCpltCallback = I2C_DMASlaveReceiveCplt;
2443
2444 /* Set the DMA error callback */
2445 hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
2446
2447 /* Set the unused DMA callbacks to NULL */
2449 hi2c->hdmarx->XferAbortCallback = NULL;
2450
2451 /* Enable the DMA stream or channel depends on Instance */
2452 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData,
2453 hi2c->XferSize);
2454 }
2455 else
2456 {
2457 /* Update I2C state */
2459 hi2c->Mode = HAL_I2C_MODE_NONE;
2460
2461 /* Update I2C error code */
2463
2464 /* Process Unlocked */
2465 __HAL_UNLOCK(hi2c);
2466
2467 return HAL_ERROR;
2468 }
2469
2470 if (dmaxferstatus == HAL_OK)
2471 {
2472 /* Enable Address Acknowledge */
2473 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
2474
2475 /* Process Unlocked */
2476 __HAL_UNLOCK(hi2c);
2477
2478 /* Note : The I2C interrupts must be enabled after unlocking current process
2479 to avoid the risk of I2C interrupt handle execution before current
2480 process unlock */
2481 /* Enable ERR, STOP, NACK, ADDR interrupts */
2482 I2C_Enable_IRQ(hi2c, I2C_XFER_LISTEN_IT);
2483
2484 /* Enable DMA Request */
2485 hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
2486 }
2487 else
2488 {
2489 /* Update I2C state */
2491 hi2c->Mode = HAL_I2C_MODE_NONE;
2492
2493 /* Update I2C error code */
2495
2496 /* Process Unlocked */
2497 __HAL_UNLOCK(hi2c);
2498
2499 return HAL_ERROR;
2500 }
2501
2502 return HAL_OK;
2503 }
2504 else
2505 {
2506 return HAL_BUSY;
2507 }
2508}
2509
2510/**
2511 * @brief Write an amount of data in blocking mode to a specific memory address
2512 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
2513 * the configuration information for the specified I2C.
2514 * @param DevAddress Target device address: The device 7 bits address value
2515 * in datasheet must be shifted to the left before calling the interface
2516 * @param MemAddress Internal memory address
2517 * @param MemAddSize Size of internal memory address
2518 * @param pData Pointer to data buffer
2519 * @param Size Amount of data to be sent
2520 * @param Timeout Timeout duration
2521 * @retval HAL status
2522 */
2523HAL_StatusTypeDef HAL_I2C_Mem_Write(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress,
2524 uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
2525{
2526 uint32_t tickstart;
2527
2528 /* Check the parameters */
2529 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
2530
2531 if (hi2c->State == HAL_I2C_STATE_READY)
2532 {
2533 if ((pData == NULL) || (Size == 0U))
2534 {
2536 return HAL_ERROR;
2537 }
2538
2539 /* Process Locked */
2540 __HAL_LOCK(hi2c);
2541
2542 /* Init tickstart for timeout management*/
2543 tickstart = HAL_GetTick();
2544
2545 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY, tickstart) != HAL_OK)
2546 {
2547 return HAL_ERROR;
2548 }
2549
2551 hi2c->Mode = HAL_I2C_MODE_MEM;
2553
2554 /* Prepare transfer parameters */
2555 hi2c->pBuffPtr = pData;
2556 hi2c->XferCount = Size;
2557 hi2c->XferISR = NULL;
2558
2559 /* Send Slave Address and Memory Address */
2560 if (I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, Timeout, tickstart) != HAL_OK)
2561 {
2562 /* Process Unlocked */
2563 __HAL_UNLOCK(hi2c);
2564 return HAL_ERROR;
2565 }
2566
2567 /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE */
2568 if (hi2c->XferCount > MAX_NBYTE_SIZE)
2569 {
2570 hi2c->XferSize = MAX_NBYTE_SIZE;
2571 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
2572 }
2573 else
2574 {
2575 hi2c->XferSize = hi2c->XferCount;
2576 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
2577 }
2578
2579 do
2580 {
2581 /* Wait until TXIS flag is set */
2582 if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
2583 {
2584 return HAL_ERROR;
2585 }
2586
2587 /* Write data to TXDR */
2588 hi2c->Instance->TXDR = *hi2c->pBuffPtr;
2589
2590 /* Increment Buffer pointer */
2591 hi2c->pBuffPtr++;
2592
2593 hi2c->XferCount--;
2594 hi2c->XferSize--;
2595
2596 if ((hi2c->XferCount != 0U) && (hi2c->XferSize == 0U))
2597 {
2598 /* Wait until TCR flag is set */
2599 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout, tickstart) != HAL_OK)
2600 {
2601 return HAL_ERROR;
2602 }
2603
2604 if (hi2c->XferCount > MAX_NBYTE_SIZE)
2605 {
2606 hi2c->XferSize = MAX_NBYTE_SIZE;
2607 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_RELOAD_MODE,
2609 }
2610 else
2611 {
2612 hi2c->XferSize = hi2c->XferCount;
2613 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE,
2615 }
2616 }
2617
2618 } while (hi2c->XferCount > 0U);
2619
2620 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
2621 /* Wait until STOPF flag is reset */
2622 if (I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
2623 {
2624 return HAL_ERROR;
2625 }
2626
2627 /* Clear STOP Flag */
2629
2630 /* Clear Configuration Register 2 */
2631 I2C_RESET_CR2(hi2c);
2632
2633 hi2c->State = HAL_I2C_STATE_READY;
2634 hi2c->Mode = HAL_I2C_MODE_NONE;
2635
2636 /* Process Unlocked */
2637 __HAL_UNLOCK(hi2c);
2638
2639 return HAL_OK;
2640 }
2641 else
2642 {
2643 return HAL_BUSY;
2644 }
2645}
2646
2647/**
2648 * @brief Read an amount of data in blocking mode from a specific memory address
2649 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
2650 * the configuration information for the specified I2C.
2651 * @param DevAddress Target device address: The device 7 bits address value
2652 * in datasheet must be shifted to the left before calling the interface
2653 * @param MemAddress Internal memory address
2654 * @param MemAddSize Size of internal memory address
2655 * @param pData Pointer to data buffer
2656 * @param Size Amount of data to be sent
2657 * @param Timeout Timeout duration
2658 * @retval HAL status
2659 */
2660HAL_StatusTypeDef HAL_I2C_Mem_Read(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress,
2661 uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
2662{
2663 uint32_t tickstart;
2664
2665 /* Check the parameters */
2666 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
2667
2668 if (hi2c->State == HAL_I2C_STATE_READY)
2669 {
2670 if ((pData == NULL) || (Size == 0U))
2671 {
2673 return HAL_ERROR;
2674 }
2675
2676 /* Process Locked */
2677 __HAL_LOCK(hi2c);
2678
2679 /* Init tickstart for timeout management*/
2680 tickstart = HAL_GetTick();
2681
2682 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY, tickstart) != HAL_OK)
2683 {
2684 return HAL_ERROR;
2685 }
2686
2688 hi2c->Mode = HAL_I2C_MODE_MEM;
2690
2691 /* Prepare transfer parameters */
2692 hi2c->pBuffPtr = pData;
2693 hi2c->XferCount = Size;
2694 hi2c->XferISR = NULL;
2695
2696 /* Send Slave Address and Memory Address */
2697 if (I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, Timeout, tickstart) != HAL_OK)
2698 {
2699 /* Process Unlocked */
2700 __HAL_UNLOCK(hi2c);
2701 return HAL_ERROR;
2702 }
2703
2704 /* Send Slave Address */
2705 /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
2706 if (hi2c->XferCount > MAX_NBYTE_SIZE)
2707 {
2708 hi2c->XferSize = MAX_NBYTE_SIZE;
2709 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_RELOAD_MODE,
2711 }
2712 else
2713 {
2714 hi2c->XferSize = hi2c->XferCount;
2715 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE,
2717 }
2718
2719 do
2720 {
2721 /* Wait until RXNE flag is set */
2722 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, Timeout, tickstart) != HAL_OK)
2723 {
2724 return HAL_ERROR;
2725 }
2726
2727 /* Read data from RXDR */
2728 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR;
2729
2730 /* Increment Buffer pointer */
2731 hi2c->pBuffPtr++;
2732
2733 hi2c->XferSize--;
2734 hi2c->XferCount--;
2735
2736 if ((hi2c->XferCount != 0U) && (hi2c->XferSize == 0U))
2737 {
2738 /* Wait until TCR flag is set */
2739 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout, tickstart) != HAL_OK)
2740 {
2741 return HAL_ERROR;
2742 }
2743
2744 if (hi2c->XferCount > MAX_NBYTE_SIZE)
2745 {
2746 hi2c->XferSize = MAX_NBYTE_SIZE;
2747 I2C_TransferConfig(hi2c, DevAddress, (uint8_t) hi2c->XferSize, I2C_RELOAD_MODE,
2749 }
2750 else
2751 {
2752 hi2c->XferSize = hi2c->XferCount;
2753 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE,
2755 }
2756 }
2757 } while (hi2c->XferCount > 0U);
2758
2759 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
2760 /* Wait until STOPF flag is reset */
2761 if (I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
2762 {
2763 return HAL_ERROR;
2764 }
2765
2766 /* Clear STOP Flag */
2768
2769 /* Clear Configuration Register 2 */
2770 I2C_RESET_CR2(hi2c);
2771
2772 hi2c->State = HAL_I2C_STATE_READY;
2773 hi2c->Mode = HAL_I2C_MODE_NONE;
2774
2775 /* Process Unlocked */
2776 __HAL_UNLOCK(hi2c);
2777
2778 return HAL_OK;
2779 }
2780 else
2781 {
2782 return HAL_BUSY;
2783 }
2784}
2785/**
2786 * @brief Write an amount of data in non-blocking mode with Interrupt to a specific memory address
2787 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
2788 * the configuration information for the specified I2C.
2789 * @param DevAddress Target device address: The device 7 bits address value
2790 * in datasheet must be shifted to the left before calling the interface
2791 * @param MemAddress Internal memory address
2792 * @param MemAddSize Size of internal memory address
2793 * @param pData Pointer to data buffer
2794 * @param Size Amount of data to be sent
2795 * @retval HAL status
2796 */
2797HAL_StatusTypeDef HAL_I2C_Mem_Write_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress,
2798 uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
2799{
2800 /* Check the parameters */
2801 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
2802
2803 if (hi2c->State == HAL_I2C_STATE_READY)
2804 {
2805 if ((pData == NULL) || (Size == 0U))
2806 {
2808 return HAL_ERROR;
2809 }
2810
2811 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
2812 {
2813 return HAL_BUSY;
2814 }
2815
2816 /* Process Locked */
2817 __HAL_LOCK(hi2c);
2818
2820 hi2c->Mode = HAL_I2C_MODE_MEM;
2822
2823 /* Prepare transfer parameters */
2824 hi2c->XferSize = 0U;
2825 hi2c->pBuffPtr = pData;
2826 hi2c->XferCount = Size;
2827 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
2828 hi2c->XferISR = I2C_Mem_ISR_IT;
2829 hi2c->Devaddress = DevAddress;
2830
2831 /* If Memory address size is 8Bit */
2832 if (MemAddSize == I2C_MEMADD_SIZE_8BIT)
2833 {
2834 /* Prefetch Memory Address */
2835 hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress);
2836
2837 /* Reset Memaddress content */
2838 hi2c->Memaddress = 0xFFFFFFFFU;
2839 }
2840 /* If Memory address size is 16Bit */
2841 else
2842 {
2843 /* Prefetch Memory Address (MSB part, LSB will be manage through interrupt) */
2844 hi2c->Instance->TXDR = I2C_MEM_ADD_MSB(MemAddress);
2845
2846 /* Prepare Memaddress buffer for LSB part */
2847 hi2c->Memaddress = I2C_MEM_ADD_LSB(MemAddress);
2848 }
2849 /* Send Slave Address and Memory Address */
2850 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)MemAddSize, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);
2851
2852 /* Process Unlocked */
2853 __HAL_UNLOCK(hi2c);
2854
2855 /* Note : The I2C interrupts must be enabled after unlocking current process
2856 to avoid the risk of I2C interrupt handle execution before current
2857 process unlock */
2858
2859 /* Enable ERR, TC, STOP, NACK, TXI interrupt */
2860 /* possible to enable all of these */
2861 /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI |
2862 I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
2863 I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT);
2864
2865 return HAL_OK;
2866 }
2867 else
2868 {
2869 return HAL_BUSY;
2870 }
2871}
2872
2873/**
2874 * @brief Read an amount of data in non-blocking mode with Interrupt from a specific memory address
2875 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
2876 * the configuration information for the specified I2C.
2877 * @param DevAddress Target device address: The device 7 bits address value
2878 * in datasheet must be shifted to the left before calling the interface
2879 * @param MemAddress Internal memory address
2880 * @param MemAddSize Size of internal memory address
2881 * @param pData Pointer to data buffer
2882 * @param Size Amount of data to be sent
2883 * @retval HAL status
2884 */
2885HAL_StatusTypeDef HAL_I2C_Mem_Read_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress,
2886 uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
2887{
2888 /* Check the parameters */
2889 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
2890
2891 if (hi2c->State == HAL_I2C_STATE_READY)
2892 {
2893 if ((pData == NULL) || (Size == 0U))
2894 {
2896 return HAL_ERROR;
2897 }
2898
2899 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
2900 {
2901 return HAL_BUSY;
2902 }
2903
2904 /* Process Locked */
2905 __HAL_LOCK(hi2c);
2906
2908 hi2c->Mode = HAL_I2C_MODE_MEM;
2910
2911 /* Prepare transfer parameters */
2912 hi2c->pBuffPtr = pData;
2913 hi2c->XferCount = Size;
2914 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
2915 hi2c->XferISR = I2C_Mem_ISR_IT;
2916 hi2c->Devaddress = DevAddress;
2917
2918 /* If Memory address size is 8Bit */
2919 if (MemAddSize == I2C_MEMADD_SIZE_8BIT)
2920 {
2921 /* Prefetch Memory Address */
2922 hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress);
2923
2924 /* Reset Memaddress content */
2925 hi2c->Memaddress = 0xFFFFFFFFU;
2926 }
2927 /* If Memory address size is 16Bit */
2928 else
2929 {
2930 /* Prefetch Memory Address (MSB part, LSB will be manage through interrupt) */
2931 hi2c->Instance->TXDR = I2C_MEM_ADD_MSB(MemAddress);
2932
2933 /* Prepare Memaddress buffer for LSB part */
2934 hi2c->Memaddress = I2C_MEM_ADD_LSB(MemAddress);
2935 }
2936 /* Send Slave Address and Memory Address */
2937 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)MemAddSize, I2C_SOFTEND_MODE, I2C_GENERATE_START_WRITE);
2938
2939 /* Process Unlocked */
2940 __HAL_UNLOCK(hi2c);
2941
2942 /* Note : The I2C interrupts must be enabled after unlocking current process
2943 to avoid the risk of I2C interrupt handle execution before current
2944 process unlock */
2945
2946 /* Enable ERR, TC, STOP, NACK, TXI interrupt */
2947 /* possible to enable all of these */
2948 /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI |
2949 I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
2950 I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT);
2951
2952 return HAL_OK;
2953 }
2954 else
2955 {
2956 return HAL_BUSY;
2957 }
2958}
2959
2960/**
2961 * @brief Write an amount of data in non-blocking mode with DMA to a specific memory address
2962 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
2963 * the configuration information for the specified I2C.
2964 * @param DevAddress Target device address: The device 7 bits address value
2965 * in datasheet must be shifted to the left before calling the interface
2966 * @param MemAddress Internal memory address
2967 * @param MemAddSize Size of internal memory address
2968 * @param pData Pointer to data buffer
2969 * @param Size Amount of data to be sent
2970 * @retval HAL status
2971 */
2972HAL_StatusTypeDef HAL_I2C_Mem_Write_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress,
2973 uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
2974{
2975 HAL_StatusTypeDef dmaxferstatus;
2976
2977 /* Check the parameters */
2978 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
2979
2980 if (hi2c->State == HAL_I2C_STATE_READY)
2981 {
2982 if ((pData == NULL) || (Size == 0U))
2983 {
2985 return HAL_ERROR;
2986 }
2987
2988 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
2989 {
2990 return HAL_BUSY;
2991 }
2992
2993 /* Process Locked */
2994 __HAL_LOCK(hi2c);
2995
2997 hi2c->Mode = HAL_I2C_MODE_MEM;
2999
3000 /* Prepare transfer parameters */
3001 hi2c->pBuffPtr = pData;
3002 hi2c->XferCount = Size;
3003 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
3004 hi2c->XferISR = I2C_Mem_ISR_DMA;
3005 hi2c->Devaddress = DevAddress;
3006
3007 if (hi2c->XferCount > MAX_NBYTE_SIZE)
3008 {
3009 hi2c->XferSize = MAX_NBYTE_SIZE;
3010 }
3011 else
3012 {
3013 hi2c->XferSize = hi2c->XferCount;
3014 }
3015
3016 /* If Memory address size is 8Bit */
3017 if (MemAddSize == I2C_MEMADD_SIZE_8BIT)
3018 {
3019 /* Prefetch Memory Address */
3020 hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress);
3021
3022 /* Reset Memaddress content */
3023 hi2c->Memaddress = 0xFFFFFFFFU;
3024 }
3025 /* If Memory address size is 16Bit */
3026 else
3027 {
3028 /* Prefetch Memory Address (MSB part, LSB will be manage through interrupt) */
3029 hi2c->Instance->TXDR = I2C_MEM_ADD_MSB(MemAddress);
3030
3031 /* Prepare Memaddress buffer for LSB part */
3032 hi2c->Memaddress = I2C_MEM_ADD_LSB(MemAddress);
3033 }
3034
3035 if (hi2c->hdmatx != NULL)
3036 {
3037 /* Set the I2C DMA transfer complete callback */
3038 hi2c->hdmatx->XferCpltCallback = I2C_DMAMasterTransmitCplt;
3039
3040 /* Set the DMA error callback */
3041 hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
3042
3043 /* Set the unused DMA callbacks to NULL */
3045 hi2c->hdmatx->XferAbortCallback = NULL;
3046
3047 /* Enable the DMA stream or channel depends on Instance */
3048 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR,
3049 hi2c->XferSize);
3050 }
3051 else
3052 {
3053 /* Update I2C state */
3054 hi2c->State = HAL_I2C_STATE_READY;
3055 hi2c->Mode = HAL_I2C_MODE_NONE;
3056
3057 /* Update I2C error code */
3059
3060 /* Process Unlocked */
3061 __HAL_UNLOCK(hi2c);
3062
3063 return HAL_ERROR;
3064 }
3065
3066 if (dmaxferstatus == HAL_OK)
3067 {
3068 /* Send Slave Address and Memory Address */
3069 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)MemAddSize, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);
3070
3071 /* Process Unlocked */
3072 __HAL_UNLOCK(hi2c);
3073
3074 /* Note : The I2C interrupts must be enabled after unlocking current process
3075 to avoid the risk of I2C interrupt handle execution before current
3076 process unlock */
3077 /* Enable ERR, TC, STOP, NACK, TXI interrupt */
3078 /* possible to enable all of these */
3079 /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI |
3080 I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
3081 I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT);
3082 }
3083 else
3084 {
3085 /* Update I2C state */
3086 hi2c->State = HAL_I2C_STATE_READY;
3087 hi2c->Mode = HAL_I2C_MODE_NONE;
3088
3089 /* Update I2C error code */
3091
3092 /* Process Unlocked */
3093 __HAL_UNLOCK(hi2c);
3094
3095 return HAL_ERROR;
3096 }
3097
3098 return HAL_OK;
3099 }
3100 else
3101 {
3102 return HAL_BUSY;
3103 }
3104}
3105
3106/**
3107 * @brief Reads an amount of data in non-blocking mode with DMA from a specific memory address.
3108 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
3109 * the configuration information for the specified I2C.
3110 * @param DevAddress Target device address: The device 7 bits address value
3111 * in datasheet must be shifted to the left before calling the interface
3112 * @param MemAddress Internal memory address
3113 * @param MemAddSize Size of internal memory address
3114 * @param pData Pointer to data buffer
3115 * @param Size Amount of data to be read
3116 * @retval HAL status
3117 */
3118HAL_StatusTypeDef HAL_I2C_Mem_Read_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress,
3119 uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
3120{
3121 HAL_StatusTypeDef dmaxferstatus;
3122
3123 /* Check the parameters */
3124 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
3125
3126 if (hi2c->State == HAL_I2C_STATE_READY)
3127 {
3128 if ((pData == NULL) || (Size == 0U))
3129 {
3131 return HAL_ERROR;
3132 }
3133
3134 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
3135 {
3136 return HAL_BUSY;
3137 }
3138
3139 /* Process Locked */
3140 __HAL_LOCK(hi2c);
3141
3143 hi2c->Mode = HAL_I2C_MODE_MEM;
3145
3146 /* Prepare transfer parameters */
3147 hi2c->pBuffPtr = pData;
3148 hi2c->XferCount = Size;
3149 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
3150 hi2c->XferISR = I2C_Mem_ISR_DMA;
3151 hi2c->Devaddress = DevAddress;
3152
3153 if (hi2c->XferCount > MAX_NBYTE_SIZE)
3154 {
3155 hi2c->XferSize = MAX_NBYTE_SIZE;
3156 }
3157 else
3158 {
3159 hi2c->XferSize = hi2c->XferCount;
3160 }
3161
3162 /* If Memory address size is 8Bit */
3163 if (MemAddSize == I2C_MEMADD_SIZE_8BIT)
3164 {
3165 /* Prefetch Memory Address */
3166 hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress);
3167
3168 /* Reset Memaddress content */
3169 hi2c->Memaddress = 0xFFFFFFFFU;
3170 }
3171 /* If Memory address size is 16Bit */
3172 else
3173 {
3174 /* Prefetch Memory Address (MSB part, LSB will be manage through interrupt) */
3175 hi2c->Instance->TXDR = I2C_MEM_ADD_MSB(MemAddress);
3176
3177 /* Prepare Memaddress buffer for LSB part */
3178 hi2c->Memaddress = I2C_MEM_ADD_LSB(MemAddress);
3179 }
3180
3181 if (hi2c->hdmarx != NULL)
3182 {
3183 /* Set the I2C DMA transfer complete callback */
3184 hi2c->hdmarx->XferCpltCallback = I2C_DMAMasterReceiveCplt;
3185
3186 /* Set the DMA error callback */
3187 hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
3188
3189 /* Set the unused DMA callbacks to NULL */
3191 hi2c->hdmarx->XferAbortCallback = NULL;
3192
3193 /* Enable the DMA stream or channel depends on Instance */
3194 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData,
3195 hi2c->XferSize);
3196 }
3197 else
3198 {
3199 /* Update I2C state */
3200 hi2c->State = HAL_I2C_STATE_READY;
3201 hi2c->Mode = HAL_I2C_MODE_NONE;
3202
3203 /* Update I2C error code */
3205
3206 /* Process Unlocked */
3207 __HAL_UNLOCK(hi2c);
3208
3209 return HAL_ERROR;
3210 }
3211
3212 if (dmaxferstatus == HAL_OK)
3213 {
3214 /* Send Slave Address and Memory Address */
3215 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)MemAddSize, I2C_SOFTEND_MODE, I2C_GENERATE_START_WRITE);
3216
3217 /* Process Unlocked */
3218 __HAL_UNLOCK(hi2c);
3219
3220 /* Note : The I2C interrupts must be enabled after unlocking current process
3221 to avoid the risk of I2C interrupt handle execution before current
3222 process unlock */
3223 /* Enable ERR, TC, STOP, NACK, TXI interrupt */
3224 /* possible to enable all of these */
3225 /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI |
3226 I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
3227 I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT);
3228 }
3229 else
3230 {
3231 /* Update I2C state */
3232 hi2c->State = HAL_I2C_STATE_READY;
3233 hi2c->Mode = HAL_I2C_MODE_NONE;
3234
3235 /* Update I2C error code */
3237
3238 /* Process Unlocked */
3239 __HAL_UNLOCK(hi2c);
3240
3241 return HAL_ERROR;
3242 }
3243
3244 return HAL_OK;
3245 }
3246 else
3247 {
3248 return HAL_BUSY;
3249 }
3250}
3251
3252/**
3253 * @brief Checks if target device is ready for communication.
3254 * @note This function is used with Memory devices
3255 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
3256 * the configuration information for the specified I2C.
3257 * @param DevAddress Target device address: The device 7 bits address value
3258 * in datasheet must be shifted to the left before calling the interface
3259 * @param Trials Number of trials
3260 * @param Timeout Timeout duration
3261 * @retval HAL status
3262 */
3263HAL_StatusTypeDef HAL_I2C_IsDeviceReady(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Trials,
3264 uint32_t Timeout)
3265{
3266 uint32_t tickstart;
3267
3268 __IO uint32_t I2C_Trials = 0UL;
3269
3270 FlagStatus tmp1;
3271 FlagStatus tmp2;
3272
3273 if (hi2c->State == HAL_I2C_STATE_READY)
3274 {
3275 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
3276 {
3277 return HAL_BUSY;
3278 }
3279
3280 /* Process Locked */
3281 __HAL_LOCK(hi2c);
3282
3283 hi2c->State = HAL_I2C_STATE_BUSY;
3285
3286 do
3287 {
3288 /* Generate Start */
3289 hi2c->Instance->CR2 = I2C_GENERATE_START(hi2c->Init.AddressingMode, DevAddress);
3290
3291 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
3292 /* Wait until STOPF flag is set or a NACK flag is set*/
3293 tickstart = HAL_GetTick();
3294
3295 tmp1 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF);
3296 tmp2 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF);
3297
3298 while ((tmp1 == RESET) && (tmp2 == RESET))
3299 {
3300 if (Timeout != HAL_MAX_DELAY)
3301 {
3302 if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
3303 {
3304 /* Update I2C state */
3305 hi2c->State = HAL_I2C_STATE_READY;
3306
3307 /* Update I2C error code */
3309
3310 /* Process Unlocked */
3311 __HAL_UNLOCK(hi2c);
3312
3313 return HAL_ERROR;
3314 }
3315 }
3316
3317 tmp1 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF);
3318 tmp2 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF);
3319 }
3320
3321 /* Check if the NACKF flag has not been set */
3322 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == RESET)
3323 {
3324 /* Wait until STOPF flag is reset */
3325 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout, tickstart) != HAL_OK)
3326 {
3327 /* A non acknowledge appear during STOP Flag waiting process, a new trial must be performed */
3328 if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
3329 {
3330 /* Clear STOP Flag */
3332
3333 /* Reset the error code for next trial */
3335 }
3336 }
3337 else
3338 {
3339 /* A acknowledge appear during STOP Flag waiting process, this mean that device respond to its address */
3340
3341 /* Clear STOP Flag */
3343
3344 /* Device is ready */
3345 hi2c->State = HAL_I2C_STATE_READY;
3346
3347 /* Process Unlocked */
3348 __HAL_UNLOCK(hi2c);
3349
3350 return HAL_OK;
3351 }
3352 }
3353 else
3354 {
3355 /* A non acknowledge is detected, this mean that device not respond to its address,
3356 a new trial must be performed */
3357
3358 /* Clear NACK Flag */
3360
3361 /* Wait until STOPF flag is reset */
3362 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout, tickstart) == HAL_OK)
3363 {
3364 /* Clear STOP Flag, auto generated with autoend*/
3366 }
3367 }
3368
3369 /* Increment Trials */
3370 I2C_Trials++;
3371 } while (I2C_Trials < Trials);
3372
3373 /* Update I2C state */
3374 hi2c->State = HAL_I2C_STATE_READY;
3375
3376 /* Update I2C error code */
3378
3379 /* Process Unlocked */
3380 __HAL_UNLOCK(hi2c);
3381
3382 return HAL_ERROR;
3383 }
3384 else
3385 {
3386 return HAL_BUSY;
3387 }
3388}
3389
3390/**
3391 * @brief Sequential transmit in master I2C mode an amount of data in non-blocking mode with Interrupt.
3392 * @note This interface allow to manage repeated start condition when a direction change during transfer
3393 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
3394 * the configuration information for the specified I2C.
3395 * @param DevAddress Target device address: The device 7 bits address value
3396 * in datasheet must be shifted to the left before calling the interface
3397 * @param pData Pointer to data buffer
3398 * @param Size Amount of data to be sent
3399 * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
3400 * @retval HAL status
3401 */
3402HAL_StatusTypeDef HAL_I2C_Master_Seq_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData,
3403 uint16_t Size, uint32_t XferOptions)
3404{
3405 uint32_t xfermode;
3406 uint32_t xferrequest = I2C_GENERATE_START_WRITE;
3407 uint32_t sizetoxfer = 0U;
3408
3409 /* Check the parameters */
3411
3412 if (hi2c->State == HAL_I2C_STATE_READY)
3413 {
3414 /* Process Locked */
3415 __HAL_LOCK(hi2c);
3416
3418 hi2c->Mode = HAL_I2C_MODE_MASTER;
3420
3421 /* Prepare transfer parameters */
3422 hi2c->pBuffPtr = pData;
3423 hi2c->XferCount = Size;
3424 hi2c->XferOptions = XferOptions;
3425 hi2c->XferISR = I2C_Master_ISR_IT;
3426
3427 /* If hi2c->XferCount > MAX_NBYTE_SIZE, use reload mode */
3428 if (hi2c->XferCount > MAX_NBYTE_SIZE)
3429 {
3430 hi2c->XferSize = MAX_NBYTE_SIZE;
3431 xfermode = I2C_RELOAD_MODE;
3432 }
3433 else
3434 {
3435 hi2c->XferSize = hi2c->XferCount;
3436 xfermode = hi2c->XferOptions;
3437 }
3438
3439 if ((hi2c->XferSize > 0U) && ((XferOptions == I2C_FIRST_FRAME) || \
3440 (XferOptions == I2C_FIRST_AND_LAST_FRAME)))
3441 {
3442 /* Preload TX register */
3443 /* Write data to TXDR */
3444 hi2c->Instance->TXDR = *hi2c->pBuffPtr;
3445
3446 /* Increment Buffer pointer */
3447 hi2c->pBuffPtr++;
3448
3449 sizetoxfer = hi2c->XferSize;
3450 hi2c->XferCount--;
3451 hi2c->XferSize--;
3452 }
3453
3454 /* If transfer direction not change and there is no request to start another frame,
3455 do not generate Restart Condition */
3456 /* Mean Previous state is same as current state */
3457 if ((hi2c->PreviousState == I2C_STATE_MASTER_BUSY_TX) && \
3458 (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 0))
3459 {
3460 xferrequest = I2C_NO_STARTSTOP;
3461 }
3462 else
3463 {
3464 /* Convert OTHER_xxx XferOptions if any */
3465 I2C_ConvertOtherXferOptions(hi2c);
3466
3467 /* Update xfermode accordingly if no reload is necessary */
3468 if (hi2c->XferCount <= MAX_NBYTE_SIZE)
3469 {
3470 xfermode = hi2c->XferOptions;
3471 }
3472 }
3473
3474 /* Send Slave Address and set NBYTES to write */
3475 if ((XferOptions == I2C_FIRST_FRAME) || (XferOptions == I2C_FIRST_AND_LAST_FRAME))
3476 {
3477 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)sizetoxfer, xfermode, xferrequest);
3478 }
3479 else
3480 {
3481 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, xferrequest);
3482 }
3483
3484 /* Process Unlocked */
3485 __HAL_UNLOCK(hi2c);
3486
3487 /* Note : The I2C interrupts must be enabled after unlocking current process
3488 to avoid the risk of I2C interrupt handle execution before current
3489 process unlock */
3490 /* Enable ERR, TC, STOP, NACK, TXI interrupt */
3491 /* possible to enable all of these */
3492 /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI |
3493 I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
3494 I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT);
3495
3496 return HAL_OK;
3497 }
3498 else
3499 {
3500 return HAL_BUSY;
3501 }
3502}
3503
3504/**
3505 * @brief Sequential transmit in master I2C mode an amount of data in non-blocking mode with DMA.
3506 * @note This interface allow to manage repeated start condition when a direction change during transfer
3507 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
3508 * the configuration information for the specified I2C.
3509 * @param DevAddress Target device address: The device 7 bits address value
3510 * in datasheet must be shifted to the left before calling the interface
3511 * @param pData Pointer to data buffer
3512 * @param Size Amount of data to be sent
3513 * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
3514 * @retval HAL status
3515 */
3516HAL_StatusTypeDef HAL_I2C_Master_Seq_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData,
3517 uint16_t Size, uint32_t XferOptions)
3518{
3519 uint32_t xfermode;
3520 uint32_t xferrequest = I2C_GENERATE_START_WRITE;
3521 HAL_StatusTypeDef dmaxferstatus;
3522 uint32_t sizetoxfer = 0U;
3523
3524 /* Check the parameters */
3526
3527 if (hi2c->State == HAL_I2C_STATE_READY)
3528 {
3529 /* Process Locked */
3530 __HAL_LOCK(hi2c);
3531
3533 hi2c->Mode = HAL_I2C_MODE_MASTER;
3535
3536 /* Prepare transfer parameters */
3537 hi2c->pBuffPtr = pData;
3538 hi2c->XferCount = Size;
3539 hi2c->XferOptions = XferOptions;
3540 hi2c->XferISR = I2C_Master_ISR_DMA;
3541
3542 /* If hi2c->XferCount > MAX_NBYTE_SIZE, use reload mode */
3543 if (hi2c->XferCount > MAX_NBYTE_SIZE)
3544 {
3545 hi2c->XferSize = MAX_NBYTE_SIZE;
3546 xfermode = I2C_RELOAD_MODE;
3547 }
3548 else
3549 {
3550 hi2c->XferSize = hi2c->XferCount;
3551 xfermode = hi2c->XferOptions;
3552 }
3553
3554 if ((hi2c->XferSize > 0U) && ((XferOptions == I2C_FIRST_FRAME) || \
3555 (XferOptions == I2C_FIRST_AND_LAST_FRAME)))
3556 {
3557 /* Preload TX register */
3558 /* Write data to TXDR */
3559 hi2c->Instance->TXDR = *hi2c->pBuffPtr;
3560
3561 /* Increment Buffer pointer */
3562 hi2c->pBuffPtr++;
3563
3564 sizetoxfer = hi2c->XferSize;
3565 hi2c->XferCount--;
3566 hi2c->XferSize--;
3567 }
3568
3569 /* If transfer direction not change and there is no request to start another frame,
3570 do not generate Restart Condition */
3571 /* Mean Previous state is same as current state */
3572 if ((hi2c->PreviousState == I2C_STATE_MASTER_BUSY_TX) && \
3573 (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 0))
3574 {
3575 xferrequest = I2C_NO_STARTSTOP;
3576 }
3577 else
3578 {
3579 /* Convert OTHER_xxx XferOptions if any */
3580 I2C_ConvertOtherXferOptions(hi2c);
3581
3582 /* Update xfermode accordingly if no reload is necessary */
3583 if (hi2c->XferCount <= MAX_NBYTE_SIZE)
3584 {
3585 xfermode = hi2c->XferOptions;
3586 }
3587 }
3588
3589 if (hi2c->XferSize > 0U)
3590 {
3591 if (hi2c->hdmatx != NULL)
3592 {
3593 /* Set the I2C DMA transfer complete callback */
3594 hi2c->hdmatx->XferCpltCallback = I2C_DMAMasterTransmitCplt;
3595
3596 /* Set the DMA error callback */
3597 hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
3598
3599 /* Set the unused DMA callbacks to NULL */
3601 hi2c->hdmatx->XferAbortCallback = NULL;
3602
3603 /* Enable the DMA stream or channel depends on Instance */
3604 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr,
3605 (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
3606 }
3607 else
3608 {
3609 /* Update I2C state */
3610 hi2c->State = HAL_I2C_STATE_READY;
3611 hi2c->Mode = HAL_I2C_MODE_NONE;
3612
3613 /* Update I2C error code */
3615
3616 /* Process Unlocked */
3617 __HAL_UNLOCK(hi2c);
3618
3619 return HAL_ERROR;
3620 }
3621
3622 if (dmaxferstatus == HAL_OK)
3623 {
3624 /* Send Slave Address and set NBYTES to write */
3625 if ((XferOptions == I2C_FIRST_FRAME) || (XferOptions == I2C_FIRST_AND_LAST_FRAME))
3626 {
3627 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)sizetoxfer, xfermode, xferrequest);
3628 }
3629 else
3630 {
3631 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, xferrequest);
3632 }
3633
3634 /* Update XferCount value */
3635 hi2c->XferCount -= hi2c->XferSize;
3636
3637 /* Process Unlocked */
3638 __HAL_UNLOCK(hi2c);
3639
3640 /* Note : The I2C interrupts must be enabled after unlocking current process
3641 to avoid the risk of I2C interrupt handle execution before current
3642 process unlock */
3643 /* Enable ERR and NACK interrupts */
3644 I2C_Enable_IRQ(hi2c, I2C_XFER_ERROR_IT);
3645
3646 /* Enable DMA Request */
3647 hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
3648 }
3649 else
3650 {
3651 /* Update I2C state */
3652 hi2c->State = HAL_I2C_STATE_READY;
3653 hi2c->Mode = HAL_I2C_MODE_NONE;
3654
3655 /* Update I2C error code */
3657
3658 /* Process Unlocked */
3659 __HAL_UNLOCK(hi2c);
3660
3661 return HAL_ERROR;
3662 }
3663 }
3664 else
3665 {
3666 /* Update Transfer ISR function pointer */
3667 hi2c->XferISR = I2C_Master_ISR_IT;
3668
3669 /* Send Slave Address */
3670 /* Set NBYTES to write and generate START condition */
3671 if ((XferOptions == I2C_FIRST_FRAME) || (XferOptions == I2C_FIRST_AND_LAST_FRAME))
3672 {
3673 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)sizetoxfer, xfermode, xferrequest);
3674 }
3675 else
3676 {
3677 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, xferrequest);
3678 }
3679
3680 /* Process Unlocked */
3681 __HAL_UNLOCK(hi2c);
3682
3683 /* Note : The I2C interrupts must be enabled after unlocking current process
3684 to avoid the risk of I2C interrupt handle execution before current
3685 process unlock */
3686 /* Enable ERR, TC, STOP, NACK, TXI interrupt */
3687 /* possible to enable all of these */
3688 /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI |
3689 I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
3690 I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT);
3691 }
3692
3693 return HAL_OK;
3694 }
3695 else
3696 {
3697 return HAL_BUSY;
3698 }
3699}
3700
3701/**
3702 * @brief Sequential receive in master I2C mode an amount of data in non-blocking mode with Interrupt
3703 * @note This interface allow to manage repeated start condition when a direction change during transfer
3704 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
3705 * the configuration information for the specified I2C.
3706 * @param DevAddress Target device address: The device 7 bits address value
3707 * in datasheet must be shifted to the left before calling the interface
3708 * @param pData Pointer to data buffer
3709 * @param Size Amount of data to be sent
3710 * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
3711 * @retval HAL status
3712 */
3713HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData,
3714 uint16_t Size, uint32_t XferOptions)
3715{
3716 uint32_t xfermode;
3717 uint32_t xferrequest = I2C_GENERATE_START_READ;
3718
3719 /* Check the parameters */
3721
3722 if (hi2c->State == HAL_I2C_STATE_READY)
3723 {
3724 /* Process Locked */
3725 __HAL_LOCK(hi2c);
3726
3728 hi2c->Mode = HAL_I2C_MODE_MASTER;
3730
3731 /* Prepare transfer parameters */
3732 hi2c->pBuffPtr = pData;
3733 hi2c->XferCount = Size;
3734 hi2c->XferOptions = XferOptions;
3735 hi2c->XferISR = I2C_Master_ISR_IT;
3736
3737 /* If hi2c->XferCount > MAX_NBYTE_SIZE, use reload mode */
3738 if (hi2c->XferCount > MAX_NBYTE_SIZE)
3739 {
3740 hi2c->XferSize = MAX_NBYTE_SIZE;
3741 xfermode = I2C_RELOAD_MODE;
3742 }
3743 else
3744 {
3745 hi2c->XferSize = hi2c->XferCount;
3746 xfermode = hi2c->XferOptions;
3747 }
3748
3749 /* If transfer direction not change and there is no request to start another frame,
3750 do not generate Restart Condition */
3751 /* Mean Previous state is same as current state */
3752 if ((hi2c->PreviousState == I2C_STATE_MASTER_BUSY_RX) && \
3753 (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 0))
3754 {
3755 xferrequest = I2C_NO_STARTSTOP;
3756 }
3757 else
3758 {
3759 /* Convert OTHER_xxx XferOptions if any */
3760 I2C_ConvertOtherXferOptions(hi2c);
3761
3762 /* Update xfermode accordingly if no reload is necessary */
3763 if (hi2c->XferCount <= MAX_NBYTE_SIZE)
3764 {
3765 xfermode = hi2c->XferOptions;
3766 }
3767 }
3768
3769 /* Send Slave Address and set NBYTES to read */
3770 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, xferrequest);
3771
3772 /* Process Unlocked */
3773 __HAL_UNLOCK(hi2c);
3774
3775 /* Note : The I2C interrupts must be enabled after unlocking current process
3776 to avoid the risk of I2C interrupt handle execution before current
3777 process unlock */
3778 I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT);
3779
3780 return HAL_OK;
3781 }
3782 else
3783 {
3784 return HAL_BUSY;
3785 }
3786}
3787
3788/**
3789 * @brief Sequential receive in master I2C mode an amount of data in non-blocking mode with DMA
3790 * @note This interface allow to manage repeated start condition when a direction change during transfer
3791 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
3792 * the configuration information for the specified I2C.
3793 * @param DevAddress Target device address: The device 7 bits address value
3794 * in datasheet must be shifted to the left before calling the interface
3795 * @param pData Pointer to data buffer
3796 * @param Size Amount of data to be sent
3797 * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
3798 * @retval HAL status
3799 */
3800HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData,
3801 uint16_t Size, uint32_t XferOptions)
3802{
3803 uint32_t xfermode;
3804 uint32_t xferrequest = I2C_GENERATE_START_READ;
3805 HAL_StatusTypeDef dmaxferstatus;
3806
3807 /* Check the parameters */
3809
3810 if (hi2c->State == HAL_I2C_STATE_READY)
3811 {
3812 /* Process Locked */
3813 __HAL_LOCK(hi2c);
3814
3816 hi2c->Mode = HAL_I2C_MODE_MASTER;
3818
3819 /* Prepare transfer parameters */
3820 hi2c->pBuffPtr = pData;
3821 hi2c->XferCount = Size;
3822 hi2c->XferOptions = XferOptions;
3823 hi2c->XferISR = I2C_Master_ISR_DMA;
3824
3825 /* If hi2c->XferCount > MAX_NBYTE_SIZE, use reload mode */
3826 if (hi2c->XferCount > MAX_NBYTE_SIZE)
3827 {
3828 hi2c->XferSize = MAX_NBYTE_SIZE;
3829 xfermode = I2C_RELOAD_MODE;
3830 }
3831 else
3832 {
3833 hi2c->XferSize = hi2c->XferCount;
3834 xfermode = hi2c->XferOptions;
3835 }
3836
3837 /* If transfer direction not change and there is no request to start another frame,
3838 do not generate Restart Condition */
3839 /* Mean Previous state is same as current state */
3840 if ((hi2c->PreviousState == I2C_STATE_MASTER_BUSY_RX) && \
3841 (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 0))
3842 {
3843 xferrequest = I2C_NO_STARTSTOP;
3844 }
3845 else
3846 {
3847 /* Convert OTHER_xxx XferOptions if any */
3848 I2C_ConvertOtherXferOptions(hi2c);
3849
3850 /* Update xfermode accordingly if no reload is necessary */
3851 if (hi2c->XferCount <= MAX_NBYTE_SIZE)
3852 {
3853 xfermode = hi2c->XferOptions;
3854 }
3855 }
3856
3857 if (hi2c->XferSize > 0U)
3858 {
3859 if (hi2c->hdmarx != NULL)
3860 {
3861 /* Set the I2C DMA transfer complete callback */
3862 hi2c->hdmarx->XferCpltCallback = I2C_DMAMasterReceiveCplt;
3863
3864 /* Set the DMA error callback */
3865 hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
3866
3867 /* Set the unused DMA callbacks to NULL */
3869 hi2c->hdmarx->XferAbortCallback = NULL;
3870
3871 /* Enable the DMA stream or channel depends on Instance */
3872 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData,
3873 hi2c->XferSize);
3874 }
3875 else
3876 {
3877 /* Update I2C state */
3878 hi2c->State = HAL_I2C_STATE_READY;
3879 hi2c->Mode = HAL_I2C_MODE_NONE;
3880
3881 /* Update I2C error code */
3883
3884 /* Process Unlocked */
3885 __HAL_UNLOCK(hi2c);
3886
3887 return HAL_ERROR;
3888 }
3889
3890 if (dmaxferstatus == HAL_OK)
3891 {
3892 /* Send Slave Address and set NBYTES to read */
3893 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, xferrequest);
3894
3895 /* Update XferCount value */
3896 hi2c->XferCount -= hi2c->XferSize;
3897
3898 /* Process Unlocked */
3899 __HAL_UNLOCK(hi2c);
3900
3901 /* Note : The I2C interrupts must be enabled after unlocking current process
3902 to avoid the risk of I2C interrupt handle execution before current
3903 process unlock */
3904 /* Enable ERR and NACK interrupts */
3905 I2C_Enable_IRQ(hi2c, I2C_XFER_ERROR_IT);
3906
3907 /* Enable DMA Request */
3908 hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
3909 }
3910 else
3911 {
3912 /* Update I2C state */
3913 hi2c->State = HAL_I2C_STATE_READY;
3914 hi2c->Mode = HAL_I2C_MODE_NONE;
3915
3916 /* Update I2C error code */
3918
3919 /* Process Unlocked */
3920 __HAL_UNLOCK(hi2c);
3921
3922 return HAL_ERROR;
3923 }
3924 }
3925 else
3926 {
3927 /* Update Transfer ISR function pointer */
3928 hi2c->XferISR = I2C_Master_ISR_IT;
3929
3930 /* Send Slave Address */
3931 /* Set NBYTES to read and generate START condition */
3932 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE,
3934
3935 /* Process Unlocked */
3936 __HAL_UNLOCK(hi2c);
3937
3938 /* Note : The I2C interrupts must be enabled after unlocking current process
3939 to avoid the risk of I2C interrupt handle execution before current
3940 process unlock */
3941 /* Enable ERR, TC, STOP, NACK, RXI interrupt */
3942 /* possible to enable all of these */
3943 /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI |
3944 I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
3945 I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT);
3946 }
3947
3948 return HAL_OK;
3949 }
3950 else
3951 {
3952 return HAL_BUSY;
3953 }
3954}
3955
3956/**
3957 * @brief Sequential transmit in slave/device I2C mode an amount of data in non-blocking mode with Interrupt
3958 * @note This interface allow to manage repeated start condition when a direction change during transfer
3959 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
3960 * the configuration information for the specified I2C.
3961 * @param pData Pointer to data buffer
3962 * @param Size Amount of data to be sent
3963 * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
3964 * @retval HAL status
3965 */
3966HAL_StatusTypeDef HAL_I2C_Slave_Seq_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size,
3967 uint32_t XferOptions)
3968{
3969 /* Declaration of tmp to prevent undefined behavior of volatile usage */
3970 FlagStatus tmp;
3971
3972 /* Check the parameters */
3974
3975 if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
3976 {
3977 if ((pData == NULL) || (Size == 0U))
3978 {
3980 return HAL_ERROR;
3981 }
3982
3983 /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
3984 I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_TX_IT);
3985
3986 /* Process Locked */
3987 __HAL_LOCK(hi2c);
3988
3989 /* I2C cannot manage full duplex exchange so disable previous IT enabled if any */
3990 /* and then toggle the HAL slave RX state to TX state */
3992 {
3993 /* Disable associated Interrupts */
3994 I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT);
3995
3996 /* Abort DMA Xfer if any */
3997 if ((hi2c->Instance->CR1 & I2C_CR1_RXDMAEN) == I2C_CR1_RXDMAEN)
3998 {
3999 hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
4000
4001 if (hi2c->hdmarx != NULL)
4002 {
4003 /* Set the I2C DMA Abort callback :
4004 will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
4005 hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort;
4006
4007 /* Abort DMA RX */
4008 if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK)
4009 {
4010 /* Call Directly XferAbortCallback function in case of error */
4011 hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx);
4012 }
4013 }
4014 }
4015 }
4016
4018 hi2c->Mode = HAL_I2C_MODE_SLAVE;
4020
4021 /* Enable Address Acknowledge */
4022 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
4023
4024 /* Prepare transfer parameters */
4025 hi2c->pBuffPtr = pData;
4026 hi2c->XferCount = Size;
4027 hi2c->XferSize = hi2c->XferCount;
4028 hi2c->XferOptions = XferOptions;
4029 hi2c->XferISR = I2C_Slave_ISR_IT;
4030
4031 tmp = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR);
4032 if ((I2C_GET_DIR(hi2c) == I2C_DIRECTION_RECEIVE) && (tmp != RESET))
4033 {
4034 /* Clear ADDR flag after prepare the transfer parameters */
4035 /* This action will generate an acknowledge to the Master */
4037 }
4038
4039 /* Process Unlocked */
4040 __HAL_UNLOCK(hi2c);
4041
4042 /* Note : The I2C interrupts must be enabled after unlocking current process
4043 to avoid the risk of I2C interrupt handle execution before current
4044 process unlock */
4045 /* REnable ADDR interrupt */
4046 I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT | I2C_XFER_LISTEN_IT);
4047
4048 return HAL_OK;
4049 }
4050 else
4051 {
4052 return HAL_ERROR;
4053 }
4054}
4055
4056/**
4057 * @brief Sequential transmit in slave/device I2C mode an amount of data in non-blocking mode with DMA
4058 * @note This interface allow to manage repeated start condition when a direction change during transfer
4059 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
4060 * the configuration information for the specified I2C.
4061 * @param pData Pointer to data buffer
4062 * @param Size Amount of data to be sent
4063 * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
4064 * @retval HAL status
4065 */
4066HAL_StatusTypeDef HAL_I2C_Slave_Seq_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size,
4067 uint32_t XferOptions)
4068{
4069 /* Declaration of tmp to prevent undefined behavior of volatile usage */
4070 FlagStatus tmp;
4071 HAL_StatusTypeDef dmaxferstatus;
4072
4073 /* Check the parameters */
4075
4076 if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
4077 {
4078 if ((pData == NULL) || (Size == 0U))
4079 {
4081 return HAL_ERROR;
4082 }
4083
4084 /* Process Locked */
4085 __HAL_LOCK(hi2c);
4086
4087 /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
4088 I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_TX_IT);
4089
4090 /* I2C cannot manage full duplex exchange so disable previous IT enabled if any */
4091 /* and then toggle the HAL slave RX state to TX state */
4093 {
4094 /* Disable associated Interrupts */
4095 I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT);
4096
4097 if ((hi2c->Instance->CR1 & I2C_CR1_RXDMAEN) == I2C_CR1_RXDMAEN)
4098 {
4099 /* Abort DMA Xfer if any */
4100 if (hi2c->hdmarx != NULL)
4101 {
4102 hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
4103
4104 /* Set the I2C DMA Abort callback :
4105 will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
4106 hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort;
4107
4108 /* Abort DMA RX */
4109 if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK)
4110 {
4111 /* Call Directly XferAbortCallback function in case of error */
4112 hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx);
4113 }
4114 }
4115 }
4116 }
4117 else if (hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN)
4118 {
4119 if ((hi2c->Instance->CR1 & I2C_CR1_TXDMAEN) == I2C_CR1_TXDMAEN)
4120 {
4121 hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
4122
4123 /* Abort DMA Xfer if any */
4124 if (hi2c->hdmatx != NULL)
4125 {
4126 /* Set the I2C DMA Abort callback :
4127 will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
4128 hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort;
4129
4130 /* Abort DMA TX */
4131 if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK)
4132 {
4133 /* Call Directly XferAbortCallback function in case of error */
4134 hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx);
4135 }
4136 }
4137 }
4138 }
4139 else
4140 {
4141 /* Nothing to do */
4142 }
4143
4145 hi2c->Mode = HAL_I2C_MODE_SLAVE;
4147
4148 /* Enable Address Acknowledge */
4149 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
4150
4151 /* Prepare transfer parameters */
4152 hi2c->pBuffPtr = pData;
4153 hi2c->XferCount = Size;
4154 hi2c->XferSize = hi2c->XferCount;
4155 hi2c->XferOptions = XferOptions;
4156 hi2c->XferISR = I2C_Slave_ISR_DMA;
4157
4158 if (hi2c->hdmatx != NULL)
4159 {
4160 /* Set the I2C DMA transfer complete callback */
4161 hi2c->hdmatx->XferCpltCallback = I2C_DMASlaveTransmitCplt;
4162
4163 /* Set the DMA error callback */
4164 hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
4165
4166 /* Set the unused DMA callbacks to NULL */
4168 hi2c->hdmatx->XferAbortCallback = NULL;
4169
4170 /* Enable the DMA stream or channel depends on Instance */
4171 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR,
4172 hi2c->XferSize);
4173 }
4174 else
4175 {
4176 /* Update I2C state */
4178 hi2c->Mode = HAL_I2C_MODE_NONE;
4179
4180 /* Update I2C error code */
4182
4183 /* Process Unlocked */
4184 __HAL_UNLOCK(hi2c);
4185
4186 return HAL_ERROR;
4187 }
4188
4189 if (dmaxferstatus == HAL_OK)
4190 {
4191 /* Update XferCount value */
4192 hi2c->XferCount -= hi2c->XferSize;
4193
4194 /* Reset XferSize */
4195 hi2c->XferSize = 0;
4196 }
4197 else
4198 {
4199 /* Update I2C state */
4201 hi2c->Mode = HAL_I2C_MODE_NONE;
4202
4203 /* Update I2C error code */
4205
4206 /* Process Unlocked */
4207 __HAL_UNLOCK(hi2c);
4208
4209 return HAL_ERROR;
4210 }
4211
4212 tmp = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR);
4213 if ((I2C_GET_DIR(hi2c) == I2C_DIRECTION_RECEIVE) && (tmp != RESET))
4214 {
4215 /* Clear ADDR flag after prepare the transfer parameters */
4216 /* This action will generate an acknowledge to the Master */
4218 }
4219
4220 /* Process Unlocked */
4221 __HAL_UNLOCK(hi2c);
4222
4223 /* Enable DMA Request */
4224 hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
4225
4226 /* Note : The I2C interrupts must be enabled after unlocking current process
4227 to avoid the risk of I2C interrupt handle execution before current
4228 process unlock */
4229 /* Enable ERR, STOP, NACK, ADDR interrupts */
4230 I2C_Enable_IRQ(hi2c, I2C_XFER_LISTEN_IT);
4231
4232 return HAL_OK;
4233 }
4234 else
4235 {
4236 return HAL_ERROR;
4237 }
4238}
4239
4240/**
4241 * @brief Sequential receive in slave/device I2C mode an amount of data in non-blocking mode with Interrupt
4242 * @note This interface allow to manage repeated start condition when a direction change during transfer
4243 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
4244 * the configuration information for the specified I2C.
4245 * @param pData Pointer to data buffer
4246 * @param Size Amount of data to be sent
4247 * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
4248 * @retval HAL status
4249 */
4250HAL_StatusTypeDef HAL_I2C_Slave_Seq_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size,
4251 uint32_t XferOptions)
4252{
4253 /* Declaration of tmp to prevent undefined behavior of volatile usage */
4254 FlagStatus tmp;
4255
4256 /* Check the parameters */
4258
4259 if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
4260 {
4261 if ((pData == NULL) || (Size == 0U))
4262 {
4264 return HAL_ERROR;
4265 }
4266
4267 /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
4268 I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_RX_IT);
4269
4270 /* Process Locked */
4271 __HAL_LOCK(hi2c);
4272
4273 /* I2C cannot manage full duplex exchange so disable previous IT enabled if any */
4274 /* and then toggle the HAL slave TX state to RX state */
4276 {
4277 /* Disable associated Interrupts */
4278 I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT);
4279
4280 if ((hi2c->Instance->CR1 & I2C_CR1_TXDMAEN) == I2C_CR1_TXDMAEN)
4281 {
4282 hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
4283
4284 /* Abort DMA Xfer if any */
4285 if (hi2c->hdmatx != NULL)
4286 {
4287 /* Set the I2C DMA Abort callback :
4288 will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
4289 hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort;
4290
4291 /* Abort DMA TX */
4292 if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK)
4293 {
4294 /* Call Directly XferAbortCallback function in case of error */
4295 hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx);
4296 }
4297 }
4298 }
4299 }
4300
4302 hi2c->Mode = HAL_I2C_MODE_SLAVE;
4304
4305 /* Enable Address Acknowledge */
4306 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
4307
4308 /* Prepare transfer parameters */
4309 hi2c->pBuffPtr = pData;
4310 hi2c->XferCount = Size;
4311 hi2c->XferSize = hi2c->XferCount;
4312 hi2c->XferOptions = XferOptions;
4313 hi2c->XferISR = I2C_Slave_ISR_IT;
4314
4315 tmp = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR);
4316 if ((I2C_GET_DIR(hi2c) == I2C_DIRECTION_TRANSMIT) && (tmp != RESET))
4317 {
4318 /* Clear ADDR flag after prepare the transfer parameters */
4319 /* This action will generate an acknowledge to the Master */
4321 }
4322
4323 /* Process Unlocked */
4324 __HAL_UNLOCK(hi2c);
4325
4326 /* Note : The I2C interrupts must be enabled after unlocking current process
4327 to avoid the risk of I2C interrupt handle execution before current
4328 process unlock */
4329 /* REnable ADDR interrupt */
4330 I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT | I2C_XFER_LISTEN_IT);
4331
4332 return HAL_OK;
4333 }
4334 else
4335 {
4336 return HAL_ERROR;
4337 }
4338}
4339
4340/**
4341 * @brief Sequential receive in slave/device I2C mode an amount of data in non-blocking mode with DMA
4342 * @note This interface allow to manage repeated start condition when a direction change during transfer
4343 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
4344 * the configuration information for the specified I2C.
4345 * @param pData Pointer to data buffer
4346 * @param Size Amount of data to be sent
4347 * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
4348 * @retval HAL status
4349 */
4350HAL_StatusTypeDef HAL_I2C_Slave_Seq_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size,
4351 uint32_t XferOptions)
4352{
4353 /* Declaration of tmp to prevent undefined behavior of volatile usage */
4354 FlagStatus tmp;
4355 HAL_StatusTypeDef dmaxferstatus;
4356
4357 /* Check the parameters */
4359
4360 if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
4361 {
4362 if ((pData == NULL) || (Size == 0U))
4363 {
4365 return HAL_ERROR;
4366 }
4367
4368 /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
4369 I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_RX_IT);
4370
4371 /* Process Locked */
4372 __HAL_LOCK(hi2c);
4373
4374 /* I2C cannot manage full duplex exchange so disable previous IT enabled if any */
4375 /* and then toggle the HAL slave TX state to RX state */
4377 {
4378 /* Disable associated Interrupts */
4379 I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT);
4380
4381 if ((hi2c->Instance->CR1 & I2C_CR1_TXDMAEN) == I2C_CR1_TXDMAEN)
4382 {
4383 /* Abort DMA Xfer if any */
4384 if (hi2c->hdmatx != NULL)
4385 {
4386 hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
4387
4388 /* Set the I2C DMA Abort callback :
4389 will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
4390 hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort;
4391
4392 /* Abort DMA TX */
4393 if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK)
4394 {
4395 /* Call Directly XferAbortCallback function in case of error */
4396 hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx);
4397 }
4398 }
4399 }
4400 }
4401 else if (hi2c->State == HAL_I2C_STATE_BUSY_RX_LISTEN)
4402 {
4403 if ((hi2c->Instance->CR1 & I2C_CR1_RXDMAEN) == I2C_CR1_RXDMAEN)
4404 {
4405 hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
4406
4407 /* Abort DMA Xfer if any */
4408 if (hi2c->hdmarx != NULL)
4409 {
4410 /* Set the I2C DMA Abort callback :
4411 will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
4412 hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort;
4413
4414 /* Abort DMA RX */
4415 if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK)
4416 {
4417 /* Call Directly XferAbortCallback function in case of error */
4418 hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx);
4419 }
4420 }
4421 }
4422 }
4423 else
4424 {
4425 /* Nothing to do */
4426 }
4427
4429 hi2c->Mode = HAL_I2C_MODE_SLAVE;
4431
4432 /* Enable Address Acknowledge */
4433 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
4434
4435 /* Prepare transfer parameters */
4436 hi2c->pBuffPtr = pData;
4437 hi2c->XferCount = Size;
4438 hi2c->XferSize = hi2c->XferCount;
4439 hi2c->XferOptions = XferOptions;
4440 hi2c->XferISR = I2C_Slave_ISR_DMA;
4441
4442 if (hi2c->hdmarx != NULL)
4443 {
4444 /* Set the I2C DMA transfer complete callback */
4445 hi2c->hdmarx->XferCpltCallback = I2C_DMASlaveReceiveCplt;
4446
4447 /* Set the DMA error callback */
4448 hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
4449
4450 /* Set the unused DMA callbacks to NULL */
4452 hi2c->hdmarx->XferAbortCallback = NULL;
4453
4454 /* Enable the DMA stream or channel depends on Instance */
4455 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR,
4456 (uint32_t)pData, hi2c->XferSize);
4457 }
4458 else
4459 {
4460 /* Update I2C state */
4462 hi2c->Mode = HAL_I2C_MODE_NONE;
4463
4464 /* Update I2C error code */
4466
4467 /* Process Unlocked */
4468 __HAL_UNLOCK(hi2c);
4469
4470 return HAL_ERROR;
4471 }
4472
4473 if (dmaxferstatus == HAL_OK)
4474 {
4475 /* Update XferCount value */
4476 hi2c->XferCount -= hi2c->XferSize;
4477
4478 /* Reset XferSize */
4479 hi2c->XferSize = 0;
4480 }
4481 else
4482 {
4483 /* Update I2C state */
4485 hi2c->Mode = HAL_I2C_MODE_NONE;
4486
4487 /* Update I2C error code */
4489
4490 /* Process Unlocked */
4491 __HAL_UNLOCK(hi2c);
4492
4493 return HAL_ERROR;
4494 }
4495
4496 tmp = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR);
4497 if ((I2C_GET_DIR(hi2c) == I2C_DIRECTION_TRANSMIT) && (tmp != RESET))
4498 {
4499 /* Clear ADDR flag after prepare the transfer parameters */
4500 /* This action will generate an acknowledge to the Master */
4502 }
4503
4504 /* Process Unlocked */
4505 __HAL_UNLOCK(hi2c);
4506
4507 /* Enable DMA Request */
4508 hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
4509
4510 /* Note : The I2C interrupts must be enabled after unlocking current process
4511 to avoid the risk of I2C interrupt handle execution before current
4512 process unlock */
4513 /* REnable ADDR interrupt */
4514 I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT | I2C_XFER_LISTEN_IT);
4515
4516 return HAL_OK;
4517 }
4518 else
4519 {
4520 return HAL_ERROR;
4521 }
4522}
4523
4524/**
4525 * @brief Enable the Address listen mode with Interrupt.
4526 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
4527 * the configuration information for the specified I2C.
4528 * @retval HAL status
4529 */
4531{
4532 if (hi2c->State == HAL_I2C_STATE_READY)
4533 {
4535 hi2c->XferISR = I2C_Slave_ISR_IT;
4536
4537 /* Enable the Address Match interrupt */
4538 I2C_Enable_IRQ(hi2c, I2C_XFER_LISTEN_IT);
4539
4540 return HAL_OK;
4541 }
4542 else
4543 {
4544 return HAL_BUSY;
4545 }
4546}
4547
4548/**
4549 * @brief Disable the Address listen mode with Interrupt.
4550 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
4551 * the configuration information for the specified I2C
4552 * @retval HAL status
4553 */
4555{
4556 /* Declaration of tmp to prevent undefined behavior of volatile usage */
4557 uint32_t tmp;
4558
4559 /* Disable Address listen mode only if a transfer is not ongoing */
4560 if (hi2c->State == HAL_I2C_STATE_LISTEN)
4561 {
4562 tmp = (uint32_t)(hi2c->State) & I2C_STATE_MSK;
4563 hi2c->PreviousState = tmp | (uint32_t)(hi2c->Mode);
4564 hi2c->State = HAL_I2C_STATE_READY;
4565 hi2c->Mode = HAL_I2C_MODE_NONE;
4566 hi2c->XferISR = NULL;
4567
4568 /* Disable the Address Match interrupt */
4569 I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT);
4570
4571 return HAL_OK;
4572 }
4573 else
4574 {
4575 return HAL_BUSY;
4576 }
4577}
4578
4579/**
4580 * @brief Abort a master or memory I2C IT or DMA process communication with Interrupt.
4581 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
4582 * the configuration information for the specified I2C.
4583 * @param DevAddress Target device address: The device 7 bits address value
4584 * in datasheet must be shifted to the left before calling the interface
4585 * @retval HAL status
4586 */
4588{
4589 HAL_I2C_ModeTypeDef tmp_mode = hi2c->Mode;
4590
4591 if ((tmp_mode == HAL_I2C_MODE_MASTER) || (tmp_mode == HAL_I2C_MODE_MEM))
4592 {
4593 /* Process Locked */
4594 __HAL_LOCK(hi2c);
4595
4596 /* Disable Interrupts and Store Previous state */
4597 if (hi2c->State == HAL_I2C_STATE_BUSY_TX)
4598 {
4599 I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT);
4600 hi2c->PreviousState = I2C_STATE_MASTER_BUSY_TX;
4601 }
4602 else if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
4603 {
4604 I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT);
4605 hi2c->PreviousState = I2C_STATE_MASTER_BUSY_RX;
4606 }
4607 else
4608 {
4609 /* Do nothing */
4610 }
4611
4612 /* Set State at HAL_I2C_STATE_ABORT */
4613 hi2c->State = HAL_I2C_STATE_ABORT;
4614
4615 /* Set NBYTES to 1 to generate a dummy read on I2C peripheral */
4616 /* Set AUTOEND mode, this will generate a NACK then STOP condition to abort the current transfer */
4617 I2C_TransferConfig(hi2c, DevAddress, 1, I2C_AUTOEND_MODE, I2C_GENERATE_STOP);
4618
4619 /* Process Unlocked */
4620 __HAL_UNLOCK(hi2c);
4621
4622 /* Note : The I2C interrupts must be enabled after unlocking current process
4623 to avoid the risk of I2C interrupt handle execution before current
4624 process unlock */
4625 I2C_Enable_IRQ(hi2c, I2C_XFER_CPLT_IT);
4626
4627 return HAL_OK;
4628 }
4629 else
4630 {
4631 /* Wrong usage of abort function */
4632 /* This function should be used only in case of abort monitored by master device */
4633 return HAL_ERROR;
4634 }
4635}
4636
4637/**
4638 * @}
4639 */
4640
4641/** @defgroup I2C_IRQ_Handler_and_Callbacks IRQ Handler and Callbacks
4642 * @{
4643 */
4644
4645/**
4646 * @brief This function handles I2C event interrupt request.
4647 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
4648 * the configuration information for the specified I2C.
4649 * @retval None
4650 */
4651void HAL_I2C_EV_IRQHandler(I2C_HandleTypeDef *hi2c) /* Derogation MISRAC2012-Rule-8.13 */
4652{
4653 /* Get current IT Flags and IT sources value */
4654 uint32_t itflags = READ_REG(hi2c->Instance->ISR);
4655 uint32_t itsources = READ_REG(hi2c->Instance->CR1);
4656
4657 /* I2C events treatment -------------------------------------*/
4658 if (hi2c->XferISR != NULL)
4659 {
4660 hi2c->XferISR(hi2c, itflags, itsources);
4661 }
4662}
4663
4664/**
4665 * @brief This function handles I2C error interrupt request.
4666 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
4667 * the configuration information for the specified I2C.
4668 * @retval None
4669 */
4671{
4672 uint32_t itflags = READ_REG(hi2c->Instance->ISR);
4673 uint32_t itsources = READ_REG(hi2c->Instance->CR1);
4674 uint32_t tmperror;
4675
4676 /* I2C Bus error interrupt occurred ------------------------------------*/
4677 if ((I2C_CHECK_FLAG(itflags, I2C_FLAG_BERR) != RESET) && \
4678 (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_ERRI) != RESET))
4679 {
4681
4682 /* Clear BERR flag */
4684 }
4685
4686 /* I2C Over-Run/Under-Run interrupt occurred ----------------------------------------*/
4687 if ((I2C_CHECK_FLAG(itflags, I2C_FLAG_OVR) != RESET) && \
4688 (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_ERRI) != RESET))
4689 {
4691
4692 /* Clear OVR flag */
4694 }
4695
4696 /* I2C Arbitration Loss error interrupt occurred -------------------------------------*/
4697 if ((I2C_CHECK_FLAG(itflags, I2C_FLAG_ARLO) != RESET) && \
4698 (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_ERRI) != RESET))
4699 {
4701
4702 /* Clear ARLO flag */
4704 }
4705
4706 /* Store current volatile hi2c->ErrorCode, misra rule */
4707 tmperror = hi2c->ErrorCode;
4708
4709 /* Call the Error Callback in case of Error detected */
4711 {
4712 I2C_ITError(hi2c, tmperror);
4713 }
4714}
4715
4716/**
4717 * @brief Master Tx Transfer completed callback.
4718 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
4719 * the configuration information for the specified I2C.
4720 * @retval None
4721 */
4723{
4724 /* Prevent unused argument(s) compilation warning */
4725 UNUSED(hi2c);
4726
4727 /* NOTE : This function should not be modified, when the callback is needed,
4728 the HAL_I2C_MasterTxCpltCallback could be implemented in the user file
4729 */
4730}
4731
4732/**
4733 * @brief Master Rx Transfer completed callback.
4734 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
4735 * the configuration information for the specified I2C.
4736 * @retval None
4737 */
4739{
4740 /* Prevent unused argument(s) compilation warning */
4741 UNUSED(hi2c);
4742
4743 /* NOTE : This function should not be modified, when the callback is needed,
4744 the HAL_I2C_MasterRxCpltCallback could be implemented in the user file
4745 */
4746}
4747
4748/** @brief Slave Tx Transfer completed callback.
4749 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
4750 * the configuration information for the specified I2C.
4751 * @retval None
4752 */
4754{
4755 /* Prevent unused argument(s) compilation warning */
4756 UNUSED(hi2c);
4757
4758 /* NOTE : This function should not be modified, when the callback is needed,
4759 the HAL_I2C_SlaveTxCpltCallback could be implemented in the user file
4760 */
4761}
4762
4763/**
4764 * @brief Slave Rx Transfer completed callback.
4765 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
4766 * the configuration information for the specified I2C.
4767 * @retval None
4768 */
4770{
4771 /* Prevent unused argument(s) compilation warning */
4772 UNUSED(hi2c);
4773
4774 /* NOTE : This function should not be modified, when the callback is needed,
4775 the HAL_I2C_SlaveRxCpltCallback could be implemented in the user file
4776 */
4777}
4778
4779/**
4780 * @brief Slave Address Match callback.
4781 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
4782 * the configuration information for the specified I2C.
4783 * @param TransferDirection Master request Transfer Direction (Write/Read), value of @ref I2C_XFERDIRECTION
4784 * @param AddrMatchCode Address Match Code
4785 * @retval None
4786 */
4787__weak void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode)
4788{
4789 /* Prevent unused argument(s) compilation warning */
4790 UNUSED(hi2c);
4791 UNUSED(TransferDirection);
4792 UNUSED(AddrMatchCode);
4793
4794 /* NOTE : This function should not be modified, when the callback is needed,
4795 the HAL_I2C_AddrCallback() could be implemented in the user file
4796 */
4797}
4798
4799/**
4800 * @brief Listen Complete callback.
4801 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
4802 * the configuration information for the specified I2C.
4803 * @retval None
4804 */
4806{
4807 /* Prevent unused argument(s) compilation warning */
4808 UNUSED(hi2c);
4809
4810 /* NOTE : This function should not be modified, when the callback is needed,
4811 the HAL_I2C_ListenCpltCallback() could be implemented in the user file
4812 */
4813}
4814
4815/**
4816 * @brief Memory Tx Transfer completed callback.
4817 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
4818 * the configuration information for the specified I2C.
4819 * @retval None
4820 */
4822{
4823 /* Prevent unused argument(s) compilation warning */
4824 UNUSED(hi2c);
4825
4826 /* NOTE : This function should not be modified, when the callback is needed,
4827 the HAL_I2C_MemTxCpltCallback could be implemented in the user file
4828 */
4829}
4830
4831/**
4832 * @brief Memory Rx Transfer completed callback.
4833 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
4834 * the configuration information for the specified I2C.
4835 * @retval None
4836 */
4838{
4839 /* Prevent unused argument(s) compilation warning */
4840 UNUSED(hi2c);
4841
4842 /* NOTE : This function should not be modified, when the callback is needed,
4843 the HAL_I2C_MemRxCpltCallback could be implemented in the user file
4844 */
4845}
4846
4847/**
4848 * @brief I2C error callback.
4849 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
4850 * the configuration information for the specified I2C.
4851 * @retval None
4852 */
4854{
4855 /* Prevent unused argument(s) compilation warning */
4856 UNUSED(hi2c);
4857
4858 /* NOTE : This function should not be modified, when the callback is needed,
4859 the HAL_I2C_ErrorCallback could be implemented in the user file
4860 */
4861}
4862
4863/**
4864 * @brief I2C abort callback.
4865 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
4866 * the configuration information for the specified I2C.
4867 * @retval None
4868 */
4870{
4871 /* Prevent unused argument(s) compilation warning */
4872 UNUSED(hi2c);
4873
4874 /* NOTE : This function should not be modified, when the callback is needed,
4875 the HAL_I2C_AbortCpltCallback could be implemented in the user file
4876 */
4877}
4878
4879/**
4880 * @}
4881 */
4882
4883/** @defgroup I2C_Exported_Functions_Group3 Peripheral State, Mode and Error functions
4884 * @brief Peripheral State, Mode and Error functions
4885 *
4886@verbatim
4887 ===============================================================================
4888 ##### Peripheral State, Mode and Error functions #####
4889 ===============================================================================
4890 [..]
4891 This subsection permit to get in run-time the status of the peripheral
4892 and the data flow.
4893
4894@endverbatim
4895 * @{
4896 */
4897
4898/**
4899 * @brief Return the I2C handle state.
4900 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
4901 * the configuration information for the specified I2C.
4902 * @retval HAL state
4903 */
4905{
4906 /* Return I2C handle state */
4907 return hi2c->State;
4908}
4909
4910/**
4911 * @brief Returns the I2C Master, Slave, Memory or no mode.
4912 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
4913 * the configuration information for I2C module
4914 * @retval HAL mode
4915 */
4917{
4918 return hi2c->Mode;
4919}
4920
4921/**
4922 * @brief Return the I2C error code.
4923 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
4924 * the configuration information for the specified I2C.
4925 * @retval I2C Error Code
4926 */
4927uint32_t HAL_I2C_GetError(const I2C_HandleTypeDef *hi2c)
4928{
4929 return hi2c->ErrorCode;
4930}
4931
4932/**
4933 * @}
4934 */
4935
4936/**
4937 * @}
4938 */
4939
4940/** @addtogroup I2C_Private_Functions
4941 * @{
4942 */
4943
4944/**
4945 * @brief Interrupt Sub-Routine which handle the Interrupt Flags Master Mode with Interrupt.
4946 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
4947 * the configuration information for the specified I2C.
4948 * @param ITFlags Interrupt flags to handle.
4949 * @param ITSources Interrupt sources enabled.
4950 * @retval HAL status
4951 */
4952static HAL_StatusTypeDef I2C_Master_ISR_IT(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags,
4953 uint32_t ITSources)
4954{
4955 uint16_t devaddress;
4956 uint32_t tmpITFlags = ITFlags;
4957
4958 /* Process Locked */
4959 __HAL_LOCK(hi2c);
4960
4961 if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_AF) != RESET) && \
4962 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_NACKI) != RESET))
4963 {
4964 /* Clear NACK Flag */
4966
4967 /* Set corresponding Error Code */
4968 /* No need to generate STOP, it is automatically done */
4969 /* Error callback will be send during stop flag treatment */
4970 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
4971
4972 /* Flush TX register */
4973 I2C_Flush_TXDR(hi2c);
4974 }
4975 else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_RXNE) != RESET) && \
4976 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_RXI) != RESET))
4977 {
4978 /* Remove RXNE flag on temporary variable as read done */
4979 tmpITFlags &= ~I2C_FLAG_RXNE;
4980
4981 /* Read data from RXDR */
4982 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR;
4983
4984 /* Increment Buffer pointer */
4985 hi2c->pBuffPtr++;
4986
4987 hi2c->XferSize--;
4988 hi2c->XferCount--;
4989 }
4990 else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_TC) == RESET) && \
4991 ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_TXIS) != RESET) && \
4992 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TXI) != RESET)))
4993 {
4994 /* Write data to TXDR */
4995 if (hi2c->XferCount != 0U)
4996 {
4997 /* Write data to TXDR */
4998 hi2c->Instance->TXDR = *hi2c->pBuffPtr;
4999
5000 /* Increment Buffer pointer */
5001 hi2c->pBuffPtr++;
5002
5003 hi2c->XferSize--;
5004 hi2c->XferCount--;
5005 }
5006 }
5007 else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_TCR) != RESET) && \
5008 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TCI) != RESET))
5009 {
5010 if ((hi2c->XferCount != 0U) && (hi2c->XferSize == 0U))
5011 {
5012 devaddress = (uint16_t)(hi2c->Instance->CR2 & I2C_CR2_SADD);
5013
5014 if (hi2c->XferCount > MAX_NBYTE_SIZE)
5015 {
5016 hi2c->XferSize = MAX_NBYTE_SIZE;
5017 I2C_TransferConfig(hi2c, devaddress, (uint8_t)hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
5018 }
5019 else
5020 {
5021 hi2c->XferSize = hi2c->XferCount;
5022 if (hi2c->XferOptions != I2C_NO_OPTION_FRAME)
5023 {
5024 I2C_TransferConfig(hi2c, devaddress, (uint8_t)hi2c->XferSize,
5026 }
5027 else
5028 {
5029 I2C_TransferConfig(hi2c, devaddress, (uint8_t)hi2c->XferSize,
5031 }
5032 }
5033 }
5034 else
5035 {
5036 /* Call TxCpltCallback() if no stop mode is set */
5038 {
5039 /* Call I2C Master Sequential complete process */
5040 I2C_ITMasterSeqCplt(hi2c);
5041 }
5042 else
5043 {
5044 /* Wrong size Status regarding TCR flag event */
5045 /* Call the corresponding callback to inform upper layer of End of Transfer */
5046 I2C_ITError(hi2c, HAL_I2C_ERROR_SIZE);
5047 }
5048 }
5049 }
5050 else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_TC) != RESET) && \
5051 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TCI) != RESET))
5052 {
5053 if (hi2c->XferCount == 0U)
5054 {
5056 {
5057 /* Generate a stop condition in case of no transfer option */
5058 if (hi2c->XferOptions == I2C_NO_OPTION_FRAME)
5059 {
5060 /* Generate Stop */
5061 hi2c->Instance->CR2 |= I2C_CR2_STOP;
5062 }
5063 else
5064 {
5065 /* Call I2C Master Sequential complete process */
5066 I2C_ITMasterSeqCplt(hi2c);
5067 }
5068 }
5069 }
5070 else
5071 {
5072 /* Wrong size Status regarding TC flag event */
5073 /* Call the corresponding callback to inform upper layer of End of Transfer */
5074 I2C_ITError(hi2c, HAL_I2C_ERROR_SIZE);
5075 }
5076 }
5077 else
5078 {
5079 /* Nothing to do */
5080 }
5081
5082 if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_STOPF) != RESET) && \
5083 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_STOPI) != RESET))
5084 {
5085 /* Call I2C Master complete process */
5086 I2C_ITMasterCplt(hi2c, tmpITFlags);
5087 }
5088
5089 /* Process Unlocked */
5090 __HAL_UNLOCK(hi2c);
5091
5092 return HAL_OK;
5093}
5094
5095/**
5096 * @brief Interrupt Sub-Routine which handle the Interrupt Flags Memory Mode with Interrupt.
5097 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
5098 * the configuration information for the specified I2C.
5099 * @param ITFlags Interrupt flags to handle.
5100 * @param ITSources Interrupt sources enabled.
5101 * @retval HAL status
5102 */
5103static HAL_StatusTypeDef I2C_Mem_ISR_IT(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags,
5104 uint32_t ITSources)
5105{
5106 uint32_t direction = I2C_GENERATE_START_WRITE;
5107 uint32_t tmpITFlags = ITFlags;
5108
5109 /* Process Locked */
5110 __HAL_LOCK(hi2c);
5111
5112 if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_AF) != RESET) && \
5113 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_NACKI) != RESET))
5114 {
5115 /* Clear NACK Flag */
5117
5118 /* Set corresponding Error Code */
5119 /* No need to generate STOP, it is automatically done */
5120 /* Error callback will be send during stop flag treatment */
5121 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
5122
5123 /* Flush TX register */
5124 I2C_Flush_TXDR(hi2c);
5125 }
5126 else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_RXNE) != RESET) && \
5127 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_RXI) != RESET))
5128 {
5129 /* Remove RXNE flag on temporary variable as read done */
5130 tmpITFlags &= ~I2C_FLAG_RXNE;
5131
5132 /* Read data from RXDR */
5133 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR;
5134
5135 /* Increment Buffer pointer */
5136 hi2c->pBuffPtr++;
5137
5138 hi2c->XferSize--;
5139 hi2c->XferCount--;
5140 }
5141 else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_TXIS) != RESET) && \
5142 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TXI) != RESET))
5143 {
5144 if (hi2c->Memaddress == 0xFFFFFFFFU)
5145 {
5146 /* Write data to TXDR */
5147 hi2c->Instance->TXDR = *hi2c->pBuffPtr;
5148
5149 /* Increment Buffer pointer */
5150 hi2c->pBuffPtr++;
5151
5152 hi2c->XferSize--;
5153 hi2c->XferCount--;
5154 }
5155 else
5156 {
5157 /* Write LSB part of Memory Address */
5158 hi2c->Instance->TXDR = hi2c->Memaddress;
5159
5160 /* Reset Memaddress content */
5161 hi2c->Memaddress = 0xFFFFFFFFU;
5162 }
5163 }
5164 else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_TCR) != RESET) && \
5165 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TCI) != RESET))
5166 {
5167 if ((hi2c->XferCount != 0U) && (hi2c->XferSize == 0U))
5168 {
5169 if (hi2c->XferCount > MAX_NBYTE_SIZE)
5170 {
5171 hi2c->XferSize = MAX_NBYTE_SIZE;
5172 I2C_TransferConfig(hi2c, (uint16_t)hi2c->Devaddress, (uint8_t)hi2c->XferSize,
5174 }
5175 else
5176 {
5177 hi2c->XferSize = hi2c->XferCount;
5178 I2C_TransferConfig(hi2c, (uint16_t)hi2c->Devaddress, (uint8_t)hi2c->XferSize,
5180 }
5181 }
5182 else
5183 {
5184 /* Wrong size Status regarding TCR flag event */
5185 /* Call the corresponding callback to inform upper layer of End of Transfer */
5186 I2C_ITError(hi2c, HAL_I2C_ERROR_SIZE);
5187 }
5188 }
5189 else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_TC) != RESET) && \
5190 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TCI) != RESET))
5191 {
5192 /* Disable Interrupt related to address step */
5193 I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT);
5194
5195 /* Enable ERR, TC, STOP, NACK and RXI interrupts */
5196 I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT);
5197
5198 if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
5199 {
5200 direction = I2C_GENERATE_START_READ;
5201 }
5202
5203 if (hi2c->XferCount > MAX_NBYTE_SIZE)
5204 {
5205 hi2c->XferSize = MAX_NBYTE_SIZE;
5206
5207 /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
5208 I2C_TransferConfig(hi2c, (uint16_t)hi2c->Devaddress, (uint8_t)hi2c->XferSize,
5209 I2C_RELOAD_MODE, direction);
5210 }
5211 else
5212 {
5213 hi2c->XferSize = hi2c->XferCount;
5214
5215 /* Set NBYTES to write and generate RESTART */
5216 I2C_TransferConfig(hi2c, (uint16_t)hi2c->Devaddress, (uint8_t)hi2c->XferSize,
5217 I2C_AUTOEND_MODE, direction);
5218 }
5219 }
5220 else
5221 {
5222 /* Nothing to do */
5223 }
5224
5225 if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_STOPF) != RESET) && \
5226 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_STOPI) != RESET))
5227 {
5228 /* Call I2C Master complete process */
5229 I2C_ITMasterCplt(hi2c, tmpITFlags);
5230 }
5231
5232 /* Process Unlocked */
5233 __HAL_UNLOCK(hi2c);
5234
5235 return HAL_OK;
5236}
5237
5238/**
5239 * @brief Interrupt Sub-Routine which handle the Interrupt Flags Slave Mode with Interrupt.
5240 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
5241 * the configuration information for the specified I2C.
5242 * @param ITFlags Interrupt flags to handle.
5243 * @param ITSources Interrupt sources enabled.
5244 * @retval HAL status
5245 */
5246static HAL_StatusTypeDef I2C_Slave_ISR_IT(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags,
5247 uint32_t ITSources)
5248{
5249 uint32_t tmpoptions = hi2c->XferOptions;
5250 uint32_t tmpITFlags = ITFlags;
5251
5252 /* Process locked */
5253 __HAL_LOCK(hi2c);
5254
5255 /* Check if STOPF is set */
5256 if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_STOPF) != RESET) && \
5257 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_STOPI) != RESET))
5258 {
5259 /* Call I2C Slave complete process */
5260 I2C_ITSlaveCplt(hi2c, tmpITFlags);
5261 }
5262 else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_AF) != RESET) && \
5263 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_NACKI) != RESET))
5264 {
5265 /* Check that I2C transfer finished */
5266 /* if yes, normal use case, a NACK is sent by the MASTER when Transfer is finished */
5267 /* Mean XferCount == 0*/
5268 /* So clear Flag NACKF only */
5269 if (hi2c->XferCount == 0U)
5270 {
5271 if ((hi2c->State == HAL_I2C_STATE_LISTEN) && (tmpoptions == I2C_FIRST_AND_LAST_FRAME))
5272 /* Same action must be done for (tmpoptions == I2C_LAST_FRAME) which removed for
5273 Warning[Pa134]: left and right operands are identical */
5274 {
5275 /* Call I2C Listen complete process */
5276 I2C_ITListenCplt(hi2c, tmpITFlags);
5277 }
5278 else if ((hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN) && (tmpoptions != I2C_NO_OPTION_FRAME))
5279 {
5280 /* Clear NACK Flag */
5282
5283 /* Flush TX register */
5284 I2C_Flush_TXDR(hi2c);
5285
5286 /* Last Byte is Transmitted */
5287 /* Call I2C Slave Sequential complete process */
5288 I2C_ITSlaveSeqCplt(hi2c);
5289 }
5290 else
5291 {
5292 /* Clear NACK Flag */
5294 }
5295 }
5296 else
5297 {
5298 /* if no, error use case, a Non-Acknowledge of last Data is generated by the MASTER*/
5299 /* Clear NACK Flag */
5301
5302 /* Set ErrorCode corresponding to a Non-Acknowledge */
5303 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
5304
5305 if ((tmpoptions == I2C_FIRST_FRAME) || (tmpoptions == I2C_NEXT_FRAME))
5306 {
5307 /* Call the corresponding callback to inform upper layer of End of Transfer */
5308 I2C_ITError(hi2c, hi2c->ErrorCode);
5309 }
5310 }
5311 }
5312 else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_RXNE) != RESET) && \
5313 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_RXI) != RESET))
5314 {
5315 if (hi2c->XferCount > 0U)
5316 {
5317 /* Read data from RXDR */
5318 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR;
5319
5320 /* Increment Buffer pointer */
5321 hi2c->pBuffPtr++;
5322
5323 hi2c->XferSize--;
5324 hi2c->XferCount--;
5325 }
5326
5327 if ((hi2c->XferCount == 0U) && \
5328 (tmpoptions != I2C_NO_OPTION_FRAME))
5329 {
5330 /* Call I2C Slave Sequential complete process */
5331 I2C_ITSlaveSeqCplt(hi2c);
5332 }
5333 }
5334 else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_ADDR) != RESET) && \
5335 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_ADDRI) != RESET))
5336 {
5337 I2C_ITAddrCplt(hi2c, tmpITFlags);
5338 }
5339 else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_TXIS) != RESET) && \
5340 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TXI) != RESET))
5341 {
5342 /* Write data to TXDR only if XferCount not reach "0" */
5343 /* A TXIS flag can be set, during STOP treatment */
5344 /* Check if all Data have already been sent */
5345 /* If it is the case, this last write in TXDR is not sent, correspond to a dummy TXIS event */
5346 if (hi2c->XferCount > 0U)
5347 {
5348 /* Write data to TXDR */
5349 hi2c->Instance->TXDR = *hi2c->pBuffPtr;
5350
5351 /* Increment Buffer pointer */
5352 hi2c->pBuffPtr++;
5353
5354 hi2c->XferCount--;
5355 hi2c->XferSize--;
5356 }
5357 else
5358 {
5359 if ((tmpoptions == I2C_NEXT_FRAME) || (tmpoptions == I2C_FIRST_FRAME))
5360 {
5361 /* Last Byte is Transmitted */
5362 /* Call I2C Slave Sequential complete process */
5363 I2C_ITSlaveSeqCplt(hi2c);
5364 }
5365 }
5366 }
5367 else
5368 {
5369 /* Nothing to do */
5370 }
5371
5372 /* Process Unlocked */
5373 __HAL_UNLOCK(hi2c);
5374
5375 return HAL_OK;
5376}
5377
5378/**
5379 * @brief Interrupt Sub-Routine which handle the Interrupt Flags Master Mode with DMA.
5380 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
5381 * the configuration information for the specified I2C.
5382 * @param ITFlags Interrupt flags to handle.
5383 * @param ITSources Interrupt sources enabled.
5384 * @retval HAL status
5385 */
5386static HAL_StatusTypeDef I2C_Master_ISR_DMA(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags,
5387 uint32_t ITSources)
5388{
5389 uint16_t devaddress;
5390 uint32_t xfermode;
5391
5392 /* Process Locked */
5393 __HAL_LOCK(hi2c);
5394
5395 if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_AF) != RESET) && \
5396 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_NACKI) != RESET))
5397 {
5398 /* Clear NACK Flag */
5400
5401 /* Set corresponding Error Code */
5402 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
5403
5404 /* No need to generate STOP, it is automatically done */
5405 /* But enable STOP interrupt, to treat it */
5406 /* Error callback will be send during stop flag treatment */
5407 I2C_Enable_IRQ(hi2c, I2C_XFER_CPLT_IT);
5408
5409 /* Flush TX register */
5410 I2C_Flush_TXDR(hi2c);
5411 }
5412 else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_TCR) != RESET) && \
5413 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TCI) != RESET))
5414 {
5415 /* Disable TC interrupt */
5417
5418 if (hi2c->XferCount != 0U)
5419 {
5420 /* Recover Slave address */
5421 devaddress = (uint16_t)(hi2c->Instance->CR2 & I2C_CR2_SADD);
5422
5423 /* Prepare the new XferSize to transfer */
5424 if (hi2c->XferCount > MAX_NBYTE_SIZE)
5425 {
5426 hi2c->XferSize = MAX_NBYTE_SIZE;
5427 xfermode = I2C_RELOAD_MODE;
5428 }
5429 else
5430 {
5431 hi2c->XferSize = hi2c->XferCount;
5432 if (hi2c->XferOptions != I2C_NO_OPTION_FRAME)
5433 {
5434 xfermode = hi2c->XferOptions;
5435 }
5436 else
5437 {
5438 xfermode = I2C_AUTOEND_MODE;
5439 }
5440 }
5441
5442 /* Set the new XferSize in Nbytes register */
5443 I2C_TransferConfig(hi2c, devaddress, (uint8_t)hi2c->XferSize, xfermode, I2C_NO_STARTSTOP);
5444
5445 /* Update XferCount value */
5446 hi2c->XferCount -= hi2c->XferSize;
5447
5448 /* Enable DMA Request */
5449 if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
5450 {
5451 hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
5452 }
5453 else
5454 {
5455 hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
5456 }
5457 }
5458 else
5459 {
5460 /* Call TxCpltCallback() if no stop mode is set */
5462 {
5463 /* Call I2C Master Sequential complete process */
5464 I2C_ITMasterSeqCplt(hi2c);
5465 }
5466 else
5467 {
5468 /* Wrong size Status regarding TCR flag event */
5469 /* Call the corresponding callback to inform upper layer of End of Transfer */
5470 I2C_ITError(hi2c, HAL_I2C_ERROR_SIZE);
5471 }
5472 }
5473 }
5474 else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_TC) != RESET) && \
5475 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TCI) != RESET))
5476 {
5477 if (hi2c->XferCount == 0U)
5478 {
5480 {
5481 /* Generate a stop condition in case of no transfer option */
5482 if (hi2c->XferOptions == I2C_NO_OPTION_FRAME)
5483 {
5484 /* Generate Stop */
5485 hi2c->Instance->CR2 |= I2C_CR2_STOP;
5486 }
5487 else
5488 {
5489 /* Call I2C Master Sequential complete process */
5490 I2C_ITMasterSeqCplt(hi2c);
5491 }
5492 }
5493 }
5494 else
5495 {
5496 /* Wrong size Status regarding TC flag event */
5497 /* Call the corresponding callback to inform upper layer of End of Transfer */
5498 I2C_ITError(hi2c, HAL_I2C_ERROR_SIZE);
5499 }
5500 }
5501 else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_STOPF) != RESET) && \
5502 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_STOPI) != RESET))
5503 {
5504 /* Call I2C Master complete process */
5505 I2C_ITMasterCplt(hi2c, ITFlags);
5506 }
5507 else
5508 {
5509 /* Nothing to do */
5510 }
5511
5512 /* Process Unlocked */
5513 __HAL_UNLOCK(hi2c);
5514
5515 return HAL_OK;
5516}
5517
5518/**
5519 * @brief Interrupt Sub-Routine which handle the Interrupt Flags Memory Mode with DMA.
5520 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
5521 * the configuration information for the specified I2C.
5522 * @param ITFlags Interrupt flags to handle.
5523 * @param ITSources Interrupt sources enabled.
5524 * @retval HAL status
5525 */
5526static HAL_StatusTypeDef I2C_Mem_ISR_DMA(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags,
5527 uint32_t ITSources)
5528{
5529 uint32_t direction = I2C_GENERATE_START_WRITE;
5530
5531 /* Process Locked */
5532 __HAL_LOCK(hi2c);
5533
5534 if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_AF) != RESET) && \
5535 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_NACKI) != RESET))
5536 {
5537 /* Clear NACK Flag */
5539
5540 /* Set corresponding Error Code */
5541 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
5542
5543 /* No need to generate STOP, it is automatically done */
5544 /* But enable STOP interrupt, to treat it */
5545 /* Error callback will be send during stop flag treatment */
5546 I2C_Enable_IRQ(hi2c, I2C_XFER_CPLT_IT);
5547
5548 /* Flush TX register */
5549 I2C_Flush_TXDR(hi2c);
5550 }
5551 else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_TXIS) != RESET) && \
5552 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TXI) != RESET))
5553 {
5554 /* Write LSB part of Memory Address */
5555 hi2c->Instance->TXDR = hi2c->Memaddress;
5556
5557 /* Reset Memaddress content */
5558 hi2c->Memaddress = 0xFFFFFFFFU;
5559 }
5560 else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_TCR) != RESET) && \
5561 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TCI) != RESET))
5562 {
5563 /* Disable Interrupt related to address step */
5564 I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT);
5565
5566 /* Enable only Error interrupt */
5567 I2C_Enable_IRQ(hi2c, I2C_XFER_ERROR_IT);
5568
5569 if (hi2c->XferCount != 0U)
5570 {
5571 /* Prepare the new XferSize to transfer */
5572 if (hi2c->XferCount > MAX_NBYTE_SIZE)
5573 {
5574 hi2c->XferSize = MAX_NBYTE_SIZE;
5575 I2C_TransferConfig(hi2c, (uint16_t)hi2c->Devaddress, (uint8_t)hi2c->XferSize,
5577 }
5578 else
5579 {
5580 hi2c->XferSize = hi2c->XferCount;
5581 I2C_TransferConfig(hi2c, (uint16_t)hi2c->Devaddress, (uint8_t)hi2c->XferSize,
5583 }
5584
5585 /* Update XferCount value */
5586 hi2c->XferCount -= hi2c->XferSize;
5587
5588 /* Enable DMA Request */
5589 if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
5590 {
5591 hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
5592 }
5593 else
5594 {
5595 hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
5596 }
5597 }
5598 else
5599 {
5600 /* Wrong size Status regarding TCR flag event */
5601 /* Call the corresponding callback to inform upper layer of End of Transfer */
5602 I2C_ITError(hi2c, HAL_I2C_ERROR_SIZE);
5603 }
5604 }
5605 else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_TC) != RESET) && \
5606 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TCI) != RESET))
5607 {
5608 /* Disable Interrupt related to address step */
5609 I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT);
5610
5611 /* Enable only Error and NACK interrupt for data transfer */
5612 I2C_Enable_IRQ(hi2c, I2C_XFER_ERROR_IT);
5613
5614 if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
5615 {
5616 direction = I2C_GENERATE_START_READ;
5617 }
5618
5619 if (hi2c->XferCount > MAX_NBYTE_SIZE)
5620 {
5621 hi2c->XferSize = MAX_NBYTE_SIZE;
5622
5623 /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
5624 I2C_TransferConfig(hi2c, (uint16_t)hi2c->Devaddress, (uint8_t)hi2c->XferSize,
5625 I2C_RELOAD_MODE, direction);
5626 }
5627 else
5628 {
5629 hi2c->XferSize = hi2c->XferCount;
5630
5631 /* Set NBYTES to write and generate RESTART */
5632 I2C_TransferConfig(hi2c, (uint16_t)hi2c->Devaddress, (uint8_t)hi2c->XferSize,
5633 I2C_AUTOEND_MODE, direction);
5634 }
5635
5636 /* Update XferCount value */
5637 hi2c->XferCount -= hi2c->XferSize;
5638
5639 /* Enable DMA Request */
5640 if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
5641 {
5642 hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
5643 }
5644 else
5645 {
5646 hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
5647 }
5648 }
5649 else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_STOPF) != RESET) && \
5650 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_STOPI) != RESET))
5651 {
5652 /* Call I2C Master complete process */
5653 I2C_ITMasterCplt(hi2c, ITFlags);
5654 }
5655 else
5656 {
5657 /* Nothing to do */
5658 }
5659
5660 /* Process Unlocked */
5661 __HAL_UNLOCK(hi2c);
5662
5663 return HAL_OK;
5664}
5665
5666/**
5667 * @brief Interrupt Sub-Routine which handle the Interrupt Flags Slave Mode with DMA.
5668 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
5669 * the configuration information for the specified I2C.
5670 * @param ITFlags Interrupt flags to handle.
5671 * @param ITSources Interrupt sources enabled.
5672 * @retval HAL status
5673 */
5674static HAL_StatusTypeDef I2C_Slave_ISR_DMA(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags,
5675 uint32_t ITSources)
5676{
5677 uint32_t tmpoptions = hi2c->XferOptions;
5678 uint32_t treatdmanack = 0U;
5679 HAL_I2C_StateTypeDef tmpstate;
5680
5681 /* Process locked */
5682 __HAL_LOCK(hi2c);
5683
5684 /* Check if STOPF is set */
5685 if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_STOPF) != RESET) && \
5686 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_STOPI) != RESET))
5687 {
5688 /* Call I2C Slave complete process */
5689 I2C_ITSlaveCplt(hi2c, ITFlags);
5690 }
5691 else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_AF) != RESET) && \
5692 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_NACKI) != RESET))
5693 {
5694 /* Check that I2C transfer finished */
5695 /* if yes, normal use case, a NACK is sent by the MASTER when Transfer is finished */
5696 /* Mean XferCount == 0 */
5697 /* So clear Flag NACKF only */
5698 if ((I2C_CHECK_IT_SOURCE(ITSources, I2C_CR1_TXDMAEN) != RESET) ||
5699 (I2C_CHECK_IT_SOURCE(ITSources, I2C_CR1_RXDMAEN) != RESET))
5700 {
5701 /* Split check of hdmarx, for MISRA compliance */
5702 if (hi2c->hdmarx != NULL)
5703 {
5704 if (I2C_CHECK_IT_SOURCE(ITSources, I2C_CR1_RXDMAEN) != RESET)
5705 {
5706 if (I2C_GET_DMA_REMAIN_DATA(hi2c->hdmarx) == 0U)
5707 {
5708 treatdmanack = 1U;
5709 }
5710 }
5711 }
5712
5713 /* Split check of hdmatx, for MISRA compliance */
5714 if (hi2c->hdmatx != NULL)
5715 {
5716 if (I2C_CHECK_IT_SOURCE(ITSources, I2C_CR1_TXDMAEN) != RESET)
5717 {
5718 if (I2C_GET_DMA_REMAIN_DATA(hi2c->hdmatx) == 0U)
5719 {
5720 treatdmanack = 1U;
5721 }
5722 }
5723 }
5724
5725 if (treatdmanack == 1U)
5726 {
5727 if ((hi2c->State == HAL_I2C_STATE_LISTEN) && (tmpoptions == I2C_FIRST_AND_LAST_FRAME))
5728 /* Same action must be done for (tmpoptions == I2C_LAST_FRAME) which removed for
5729 Warning[Pa134]: left and right operands are identical */
5730 {
5731 /* Call I2C Listen complete process */
5732 I2C_ITListenCplt(hi2c, ITFlags);
5733 }
5734 else if ((hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN) && (tmpoptions != I2C_NO_OPTION_FRAME))
5735 {
5736 /* Clear NACK Flag */
5738
5739 /* Flush TX register */
5740 I2C_Flush_TXDR(hi2c);
5741
5742 /* Last Byte is Transmitted */
5743 /* Call I2C Slave Sequential complete process */
5744 I2C_ITSlaveSeqCplt(hi2c);
5745 }
5746 else
5747 {
5748 /* Clear NACK Flag */
5750 }
5751 }
5752 else
5753 {
5754 /* if no, error use case, a Non-Acknowledge of last Data is generated by the MASTER*/
5755 /* Clear NACK Flag */
5757
5758 /* Set ErrorCode corresponding to a Non-Acknowledge */
5759 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
5760
5761 /* Store current hi2c->State, solve MISRA2012-Rule-13.5 */
5762 tmpstate = hi2c->State;
5763
5764 if ((tmpoptions == I2C_FIRST_FRAME) || (tmpoptions == I2C_NEXT_FRAME))
5765 {
5766 if ((tmpstate == HAL_I2C_STATE_BUSY_TX) || (tmpstate == HAL_I2C_STATE_BUSY_TX_LISTEN))
5767 {
5768 hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_TX;
5769 }
5770 else if ((tmpstate == HAL_I2C_STATE_BUSY_RX) || (tmpstate == HAL_I2C_STATE_BUSY_RX_LISTEN))
5771 {
5772 hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_RX;
5773 }
5774 else
5775 {
5776 /* Do nothing */
5777 }
5778
5779 /* Call the corresponding callback to inform upper layer of End of Transfer */
5780 I2C_ITError(hi2c, hi2c->ErrorCode);
5781 }
5782 }
5783 }
5784 else
5785 {
5786 /* Only Clear NACK Flag, no DMA treatment is pending */
5788 }
5789 }
5790 else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_ADDR) != RESET) && \
5791 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_ADDRI) != RESET))
5792 {
5793 I2C_ITAddrCplt(hi2c, ITFlags);
5794 }
5795 else
5796 {
5797 /* Nothing to do */
5798 }
5799
5800 /* Process Unlocked */
5801 __HAL_UNLOCK(hi2c);
5802
5803 return HAL_OK;
5804}
5805
5806/**
5807 * @brief Master sends target device address followed by internal memory address for write request.
5808 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
5809 * the configuration information for the specified I2C.
5810 * @param DevAddress Target device address: The device 7 bits address value
5811 * in datasheet must be shifted to the left before calling the interface
5812 * @param MemAddress Internal memory address
5813 * @param MemAddSize Size of internal memory address
5814 * @param Timeout Timeout duration
5815 * @param Tickstart Tick start value
5816 * @retval HAL status
5817 */
5818static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress,
5819 uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout,
5820 uint32_t Tickstart)
5821{
5822 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)MemAddSize, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);
5823
5824 /* Wait until TXIS flag is set */
5825 if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
5826 {
5827 return HAL_ERROR;
5828 }
5829
5830 /* If Memory address size is 8Bit */
5831 if (MemAddSize == I2C_MEMADD_SIZE_8BIT)
5832 {
5833 /* Send Memory Address */
5834 hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress);
5835 }
5836 /* If Memory address size is 16Bit */
5837 else
5838 {
5839 /* Send MSB of Memory Address */
5840 hi2c->Instance->TXDR = I2C_MEM_ADD_MSB(MemAddress);
5841
5842 /* Wait until TXIS flag is set */
5843 if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
5844 {
5845 return HAL_ERROR;
5846 }
5847
5848 /* Send LSB of Memory Address */
5849 hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress);
5850 }
5851
5852 /* Wait until TCR flag is set */
5853 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout, Tickstart) != HAL_OK)
5854 {
5855 return HAL_ERROR;
5856 }
5857
5858 return HAL_OK;
5859}
5860
5861/**
5862 * @brief Master sends target device address followed by internal memory address for read request.
5863 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
5864 * the configuration information for the specified I2C.
5865 * @param DevAddress Target device address: The device 7 bits address value
5866 * in datasheet must be shifted to the left before calling the interface
5867 * @param MemAddress Internal memory address
5868 * @param MemAddSize Size of internal memory address
5869 * @param Timeout Timeout duration
5870 * @param Tickstart Tick start value
5871 * @retval HAL status
5872 */
5873static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress,
5874 uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout,
5875 uint32_t Tickstart)
5876{
5877 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)MemAddSize, I2C_SOFTEND_MODE, I2C_GENERATE_START_WRITE);
5878
5879 /* Wait until TXIS flag is set */
5880 if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
5881 {
5882 return HAL_ERROR;
5883 }
5884
5885 /* If Memory address size is 8Bit */
5886 if (MemAddSize == I2C_MEMADD_SIZE_8BIT)
5887 {
5888 /* Send Memory Address */
5889 hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress);
5890 }
5891 /* If Memory address size is 16Bit */
5892 else
5893 {
5894 /* Send MSB of Memory Address */
5895 hi2c->Instance->TXDR = I2C_MEM_ADD_MSB(MemAddress);
5896
5897 /* Wait until TXIS flag is set */
5898 if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
5899 {
5900 return HAL_ERROR;
5901 }
5902
5903 /* Send LSB of Memory Address */
5904 hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress);
5905 }
5906
5907 /* Wait until TC flag is set */
5908 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TC, RESET, Timeout, Tickstart) != HAL_OK)
5909 {
5910 return HAL_ERROR;
5911 }
5912
5913 return HAL_OK;
5914}
5915
5916/**
5917 * @brief I2C Address complete process callback.
5918 * @param hi2c I2C handle.
5919 * @param ITFlags Interrupt flags to handle.
5920 * @retval None
5921 */
5922static void I2C_ITAddrCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags)
5923{
5924 uint8_t transferdirection;
5925 uint16_t slaveaddrcode;
5926 uint16_t ownadd1code;
5927 uint16_t ownadd2code;
5928
5929 /* Prevent unused argument(s) compilation warning */
5930 UNUSED(ITFlags);
5931
5932 /* In case of Listen state, need to inform upper layer of address match code event */
5933 if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
5934 {
5935 transferdirection = I2C_GET_DIR(hi2c);
5936 slaveaddrcode = I2C_GET_ADDR_MATCH(hi2c);
5937 ownadd1code = I2C_GET_OWN_ADDRESS1(hi2c);
5938 ownadd2code = I2C_GET_OWN_ADDRESS2(hi2c);
5939
5940 /* If 10bits addressing mode is selected */
5942 {
5943 if ((slaveaddrcode & SLAVE_ADDR_MSK) == ((ownadd1code >> SLAVE_ADDR_SHIFT) & SLAVE_ADDR_MSK))
5944 {
5945 slaveaddrcode = ownadd1code;
5946 hi2c->AddrEventCount++;
5947 if (hi2c->AddrEventCount == 2U)
5948 {
5949 /* Reset Address Event counter */
5950 hi2c->AddrEventCount = 0U;
5951
5952 /* Clear ADDR flag */
5954
5955 /* Process Unlocked */
5956 __HAL_UNLOCK(hi2c);
5957
5958 /* Call Slave Addr callback */
5959#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
5960 hi2c->AddrCallback(hi2c, transferdirection, slaveaddrcode);
5961#else
5962 HAL_I2C_AddrCallback(hi2c, transferdirection, slaveaddrcode);
5963#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
5964 }
5965 }
5966 else
5967 {
5968 slaveaddrcode = ownadd2code;
5969
5970 /* Disable ADDR Interrupts */
5971 I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT);
5972
5973 /* Process Unlocked */
5974 __HAL_UNLOCK(hi2c);
5975
5976 /* Call Slave Addr callback */
5977#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
5978 hi2c->AddrCallback(hi2c, transferdirection, slaveaddrcode);
5979#else
5980 HAL_I2C_AddrCallback(hi2c, transferdirection, slaveaddrcode);
5981#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
5982 }
5983 }
5984 /* else 7 bits addressing mode is selected */
5985 else
5986 {
5987 /* Disable ADDR Interrupts */
5988 I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT);
5989
5990 /* Process Unlocked */
5991 __HAL_UNLOCK(hi2c);
5992
5993 /* Call Slave Addr callback */
5994#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
5995 hi2c->AddrCallback(hi2c, transferdirection, slaveaddrcode);
5996#else
5997 HAL_I2C_AddrCallback(hi2c, transferdirection, slaveaddrcode);
5998#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
5999 }
6000 }
6001 /* Else clear address flag only */
6002 else
6003 {
6004 /* Clear ADDR flag */
6006
6007 /* Process Unlocked */
6008 __HAL_UNLOCK(hi2c);
6009 }
6010}
6011
6012/**
6013 * @brief I2C Master sequential complete process.
6014 * @param hi2c I2C handle.
6015 * @retval None
6016 */
6017static void I2C_ITMasterSeqCplt(I2C_HandleTypeDef *hi2c)
6018{
6019 /* Reset I2C handle mode */
6020 hi2c->Mode = HAL_I2C_MODE_NONE;
6021
6022 /* No Generate Stop, to permit restart mode */
6023 /* The stop will be done at the end of transfer, when I2C_AUTOEND_MODE enable */
6024 if (hi2c->State == HAL_I2C_STATE_BUSY_TX)
6025 {
6026 hi2c->State = HAL_I2C_STATE_READY;
6027 hi2c->PreviousState = I2C_STATE_MASTER_BUSY_TX;
6028 hi2c->XferISR = NULL;
6029
6030 /* Disable Interrupts */
6031 I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT);
6032
6033 /* Process Unlocked */
6034 __HAL_UNLOCK(hi2c);
6035
6036 /* Call the corresponding callback to inform upper layer of End of Transfer */
6037#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6038 hi2c->MasterTxCpltCallback(hi2c);
6039#else
6041#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
6042 }
6043 /* hi2c->State == HAL_I2C_STATE_BUSY_RX */
6044 else
6045 {
6046 hi2c->State = HAL_I2C_STATE_READY;
6047 hi2c->PreviousState = I2C_STATE_MASTER_BUSY_RX;
6048 hi2c->XferISR = NULL;
6049
6050 /* Disable Interrupts */
6051 I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT);
6052
6053 /* Process Unlocked */
6054 __HAL_UNLOCK(hi2c);
6055
6056 /* Call the corresponding callback to inform upper layer of End of Transfer */
6057#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6058 hi2c->MasterRxCpltCallback(hi2c);
6059#else
6061#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
6062 }
6063}
6064
6065/**
6066 * @brief I2C Slave sequential complete process.
6067 * @param hi2c I2C handle.
6068 * @retval None
6069 */
6070static void I2C_ITSlaveSeqCplt(I2C_HandleTypeDef *hi2c)
6071{
6072 uint32_t tmpcr1value = READ_REG(hi2c->Instance->CR1);
6073
6074 /* Reset I2C handle mode */
6075 hi2c->Mode = HAL_I2C_MODE_NONE;
6076
6077 /* If a DMA is ongoing, Update handle size context */
6078 if (I2C_CHECK_IT_SOURCE(tmpcr1value, I2C_CR1_TXDMAEN) != RESET)
6079 {
6080 /* Disable DMA Request */
6081 hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
6082 }
6083 else if (I2C_CHECK_IT_SOURCE(tmpcr1value, I2C_CR1_RXDMAEN) != RESET)
6084 {
6085 /* Disable DMA Request */
6086 hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
6087 }
6088 else
6089 {
6090 /* Do nothing */
6091 }
6092
6094 {
6095 /* Remove HAL_I2C_STATE_SLAVE_BUSY_TX, keep only HAL_I2C_STATE_LISTEN */
6097 hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_TX;
6098
6099 /* Disable Interrupts */
6100 I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT);
6101
6102 /* Process Unlocked */
6103 __HAL_UNLOCK(hi2c);
6104
6105 /* Call the corresponding callback to inform upper layer of End of Transfer */
6106#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6107 hi2c->SlaveTxCpltCallback(hi2c);
6108#else
6110#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
6111 }
6112
6113 else if (hi2c->State == HAL_I2C_STATE_BUSY_RX_LISTEN)
6114 {
6115 /* Remove HAL_I2C_STATE_SLAVE_BUSY_RX, keep only HAL_I2C_STATE_LISTEN */
6117 hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_RX;
6118
6119 /* Disable Interrupts */
6120 I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT);
6121
6122 /* Process Unlocked */
6123 __HAL_UNLOCK(hi2c);
6124
6125 /* Call the corresponding callback to inform upper layer of End of Transfer */
6126#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6127 hi2c->SlaveRxCpltCallback(hi2c);
6128#else
6130#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
6131 }
6132 else
6133 {
6134 /* Nothing to do */
6135 }
6136}
6137
6138/**
6139 * @brief I2C Master complete process.
6140 * @param hi2c I2C handle.
6141 * @param ITFlags Interrupt flags to handle.
6142 * @retval None
6143 */
6144static void I2C_ITMasterCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags)
6145{
6146 uint32_t tmperror;
6147 uint32_t tmpITFlags = ITFlags;
6148 __IO uint32_t tmpreg;
6149
6150 /* Clear STOP Flag */
6152
6153 /* Disable Interrupts and Store Previous state */
6154 if (hi2c->State == HAL_I2C_STATE_BUSY_TX)
6155 {
6156 I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT);
6157 hi2c->PreviousState = I2C_STATE_MASTER_BUSY_TX;
6158 }
6159 else if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
6160 {
6161 I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT);
6162 hi2c->PreviousState = I2C_STATE_MASTER_BUSY_RX;
6163 }
6164 else
6165 {
6166 /* Do nothing */
6167 }
6168
6169 /* Clear Configuration Register 2 */
6170 I2C_RESET_CR2(hi2c);
6171
6172 /* Reset handle parameters */
6173 hi2c->XferISR = NULL;
6174 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
6175
6176 if (I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_AF) != RESET)
6177 {
6178 /* Clear NACK Flag */
6180
6181 /* Set acknowledge error code */
6182 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
6183 }
6184
6185 /* Fetch Last receive data if any */
6186 if ((hi2c->State == HAL_I2C_STATE_ABORT) && (I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_RXNE) != RESET))
6187 {
6188 /* Read data from RXDR */
6189 tmpreg = (uint8_t)hi2c->Instance->RXDR;
6190 UNUSED(tmpreg);
6191 }
6192
6193 /* Flush TX register */
6194 I2C_Flush_TXDR(hi2c);
6195
6196 /* Store current volatile hi2c->ErrorCode, misra rule */
6197 tmperror = hi2c->ErrorCode;
6198
6199 /* Call the corresponding callback to inform upper layer of End of Transfer */
6200 if ((hi2c->State == HAL_I2C_STATE_ABORT) || (tmperror != HAL_I2C_ERROR_NONE))
6201 {
6202 /* Call the corresponding callback to inform upper layer of End of Transfer */
6203 I2C_ITError(hi2c, hi2c->ErrorCode);
6204 }
6205 /* hi2c->State == HAL_I2C_STATE_BUSY_TX */
6206 else if (hi2c->State == HAL_I2C_STATE_BUSY_TX)
6207 {
6208 hi2c->State = HAL_I2C_STATE_READY;
6209 hi2c->PreviousState = I2C_STATE_NONE;
6210
6211 if (hi2c->Mode == HAL_I2C_MODE_MEM)
6212 {
6213 hi2c->Mode = HAL_I2C_MODE_NONE;
6214
6215 /* Process Unlocked */
6216 __HAL_UNLOCK(hi2c);
6217
6218 /* Call the corresponding callback to inform upper layer of End of Transfer */
6219#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6220 hi2c->MemTxCpltCallback(hi2c);
6221#else
6223#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
6224 }
6225 else
6226 {
6227 hi2c->Mode = HAL_I2C_MODE_NONE;
6228
6229 /* Process Unlocked */
6230 __HAL_UNLOCK(hi2c);
6231
6232 /* Call the corresponding callback to inform upper layer of End of Transfer */
6233#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6234 hi2c->MasterTxCpltCallback(hi2c);
6235#else
6237#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
6238 }
6239 }
6240 /* hi2c->State == HAL_I2C_STATE_BUSY_RX */
6241 else if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
6242 {
6243 hi2c->State = HAL_I2C_STATE_READY;
6244 hi2c->PreviousState = I2C_STATE_NONE;
6245
6246 if (hi2c->Mode == HAL_I2C_MODE_MEM)
6247 {
6248 hi2c->Mode = HAL_I2C_MODE_NONE;
6249
6250 /* Process Unlocked */
6251 __HAL_UNLOCK(hi2c);
6252
6253 /* Call the corresponding callback to inform upper layer of End of Transfer */
6254#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6255 hi2c->MemRxCpltCallback(hi2c);
6256#else
6258#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
6259 }
6260 else
6261 {
6262 hi2c->Mode = HAL_I2C_MODE_NONE;
6263
6264 /* Process Unlocked */
6265 __HAL_UNLOCK(hi2c);
6266
6267 /* Call the corresponding callback to inform upper layer of End of Transfer */
6268#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6269 hi2c->MasterRxCpltCallback(hi2c);
6270#else
6272#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
6273 }
6274 }
6275 else
6276 {
6277 /* Nothing to do */
6278 }
6279}
6280
6281/**
6282 * @brief I2C Slave complete process.
6283 * @param hi2c I2C handle.
6284 * @param ITFlags Interrupt flags to handle.
6285 * @retval None
6286 */
6287static void I2C_ITSlaveCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags)
6288{
6289 uint32_t tmpcr1value = READ_REG(hi2c->Instance->CR1);
6290 uint32_t tmpITFlags = ITFlags;
6291 uint32_t tmpoptions = hi2c->XferOptions;
6292 HAL_I2C_StateTypeDef tmpstate = hi2c->State;
6293
6294 /* Clear STOP Flag */
6296
6297 /* Disable Interrupts and Store Previous state */
6298 if ((tmpstate == HAL_I2C_STATE_BUSY_TX) || (tmpstate == HAL_I2C_STATE_BUSY_TX_LISTEN))
6299 {
6300 I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_TX_IT);
6301 hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_TX;
6302 }
6303 else if ((tmpstate == HAL_I2C_STATE_BUSY_RX) || (tmpstate == HAL_I2C_STATE_BUSY_RX_LISTEN))
6304 {
6305 I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_RX_IT);
6306 hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_RX;
6307 }
6308 else if (tmpstate == HAL_I2C_STATE_LISTEN)
6309 {
6310 I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_TX_IT | I2C_XFER_RX_IT);
6311 hi2c->PreviousState = I2C_STATE_NONE;
6312 }
6313 else
6314 {
6315 /* Do nothing */
6316 }
6317
6318 /* Disable Address Acknowledge */
6319 hi2c->Instance->CR2 |= I2C_CR2_NACK;
6320
6321 /* Clear Configuration Register 2 */
6322 I2C_RESET_CR2(hi2c);
6323
6324 /* Flush TX register */
6325 I2C_Flush_TXDR(hi2c);
6326
6327 /* If a DMA is ongoing, Update handle size context */
6328 if (I2C_CHECK_IT_SOURCE(tmpcr1value, I2C_CR1_TXDMAEN) != RESET)
6329 {
6330 /* Disable DMA Request */
6331 hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
6332
6333 if (hi2c->hdmatx != NULL)
6334 {
6335 hi2c->XferCount = (uint16_t)I2C_GET_DMA_REMAIN_DATA(hi2c->hdmatx);
6336 }
6337 }
6338 else if (I2C_CHECK_IT_SOURCE(tmpcr1value, I2C_CR1_RXDMAEN) != RESET)
6339 {
6340 /* Disable DMA Request */
6341 hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
6342
6343 if (hi2c->hdmarx != NULL)
6344 {
6345 hi2c->XferCount = (uint16_t)I2C_GET_DMA_REMAIN_DATA(hi2c->hdmarx);
6346 }
6347 }
6348 else
6349 {
6350 /* Do nothing */
6351 }
6352
6353 /* Store Last receive data if any */
6354 if (I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_RXNE) != RESET)
6355 {
6356 /* Remove RXNE flag on temporary variable as read done */
6357 tmpITFlags &= ~I2C_FLAG_RXNE;
6358
6359 /* Read data from RXDR */
6360 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR;
6361
6362 /* Increment Buffer pointer */
6363 hi2c->pBuffPtr++;
6364
6365 if (hi2c->XferSize > 0U)
6366 {
6367 hi2c->XferSize--;
6368 hi2c->XferCount--;
6369 }
6370 }
6371
6372 /* All data are not transferred, so set error code accordingly */
6373 if (hi2c->XferCount != 0U)
6374 {
6375 /* Set ErrorCode corresponding to a Non-Acknowledge */
6376 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
6377 }
6378
6379 if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_AF) != RESET) && \
6380 (I2C_CHECK_IT_SOURCE(tmpcr1value, I2C_IT_NACKI) != RESET))
6381 {
6382 /* Check that I2C transfer finished */
6383 /* if yes, normal use case, a NACK is sent by the MASTER when Transfer is finished */
6384 /* Mean XferCount == 0*/
6385 /* So clear Flag NACKF only */
6386 if (hi2c->XferCount == 0U)
6387 {
6388 if ((hi2c->State == HAL_I2C_STATE_LISTEN) && (tmpoptions == I2C_FIRST_AND_LAST_FRAME))
6389 /* Same action must be done for (tmpoptions == I2C_LAST_FRAME) which removed for
6390 Warning[Pa134]: left and right operands are identical */
6391 {
6392 /* Call I2C Listen complete process */
6393 I2C_ITListenCplt(hi2c, tmpITFlags);
6394 }
6395 else if ((hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN) && (tmpoptions != I2C_NO_OPTION_FRAME))
6396 {
6397 /* Clear NACK Flag */
6399
6400 /* Flush TX register */
6401 I2C_Flush_TXDR(hi2c);
6402
6403 /* Last Byte is Transmitted */
6404 /* Call I2C Slave Sequential complete process */
6405 I2C_ITSlaveSeqCplt(hi2c);
6406 }
6407 else
6408 {
6409 /* Clear NACK Flag */
6411 }
6412 }
6413 else
6414 {
6415 /* if no, error use case, a Non-Acknowledge of last Data is generated by the MASTER*/
6416 /* Clear NACK Flag */
6418
6419 /* Set ErrorCode corresponding to a Non-Acknowledge */
6420 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
6421
6422 if ((tmpoptions == I2C_FIRST_FRAME) || (tmpoptions == I2C_NEXT_FRAME))
6423 {
6424 /* Call the corresponding callback to inform upper layer of End of Transfer */
6425 I2C_ITError(hi2c, hi2c->ErrorCode);
6426 }
6427 }
6428 }
6429
6430 hi2c->Mode = HAL_I2C_MODE_NONE;
6431 hi2c->XferISR = NULL;
6432
6433 if (hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
6434 {
6435 /* Call the corresponding callback to inform upper layer of End of Transfer */
6436 I2C_ITError(hi2c, hi2c->ErrorCode);
6437
6438 /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
6439 if (hi2c->State == HAL_I2C_STATE_LISTEN)
6440 {
6441 /* Call I2C Listen complete process */
6442 I2C_ITListenCplt(hi2c, tmpITFlags);
6443 }
6444 }
6445 else if (hi2c->XferOptions != I2C_NO_OPTION_FRAME)
6446 {
6447 /* Call the Sequential Complete callback, to inform upper layer of the end of Transfer */
6448 I2C_ITSlaveSeqCplt(hi2c);
6449
6450 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
6451 hi2c->State = HAL_I2C_STATE_READY;
6452 hi2c->PreviousState = I2C_STATE_NONE;
6453
6454 /* Process Unlocked */
6455 __HAL_UNLOCK(hi2c);
6456
6457 /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
6458#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6459 hi2c->ListenCpltCallback(hi2c);
6460#else
6462#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
6463 }
6464 /* Call the corresponding callback to inform upper layer of End of Transfer */
6465 else if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
6466 {
6467 hi2c->State = HAL_I2C_STATE_READY;
6468 hi2c->PreviousState = I2C_STATE_NONE;
6469
6470 /* Process Unlocked */
6471 __HAL_UNLOCK(hi2c);
6472
6473 /* Call the corresponding callback to inform upper layer of End of Transfer */
6474#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6475 hi2c->SlaveRxCpltCallback(hi2c);
6476#else
6478#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
6479 }
6480 else
6481 {
6482 hi2c->State = HAL_I2C_STATE_READY;
6483 hi2c->PreviousState = I2C_STATE_NONE;
6484
6485 /* Process Unlocked */
6486 __HAL_UNLOCK(hi2c);
6487
6488 /* Call the corresponding callback to inform upper layer of End of Transfer */
6489#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6490 hi2c->SlaveTxCpltCallback(hi2c);
6491#else
6493#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
6494 }
6495}
6496
6497/**
6498 * @brief I2C Listen complete process.
6499 * @param hi2c I2C handle.
6500 * @param ITFlags Interrupt flags to handle.
6501 * @retval None
6502 */
6503static void I2C_ITListenCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags)
6504{
6505 /* Reset handle parameters */
6506 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
6507 hi2c->PreviousState = I2C_STATE_NONE;
6508 hi2c->State = HAL_I2C_STATE_READY;
6509 hi2c->Mode = HAL_I2C_MODE_NONE;
6510 hi2c->XferISR = NULL;
6511
6512 /* Store Last receive data if any */
6513 if (I2C_CHECK_FLAG(ITFlags, I2C_FLAG_RXNE) != RESET)
6514 {
6515 /* Read data from RXDR */
6516 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR;
6517
6518 /* Increment Buffer pointer */
6519 hi2c->pBuffPtr++;
6520
6521 if (hi2c->XferSize > 0U)
6522 {
6523 hi2c->XferSize--;
6524 hi2c->XferCount--;
6525
6526 /* Set ErrorCode corresponding to a Non-Acknowledge */
6527 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
6528 }
6529 }
6530
6531 /* Disable all Interrupts*/
6532 I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_RX_IT | I2C_XFER_TX_IT);
6533
6534 /* Clear NACK Flag */
6536
6537 /* Process Unlocked */
6538 __HAL_UNLOCK(hi2c);
6539
6540 /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
6541#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6542 hi2c->ListenCpltCallback(hi2c);
6543#else
6545#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
6546}
6547
6548/**
6549 * @brief I2C interrupts error process.
6550 * @param hi2c I2C handle.
6551 * @param ErrorCode Error code to handle.
6552 * @retval None
6553 */
6554static void I2C_ITError(I2C_HandleTypeDef *hi2c, uint32_t ErrorCode)
6555{
6556 HAL_I2C_StateTypeDef tmpstate = hi2c->State;
6557
6558 uint32_t tmppreviousstate;
6559
6560 /* Reset handle parameters */
6561 hi2c->Mode = HAL_I2C_MODE_NONE;
6562 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
6563 hi2c->XferCount = 0U;
6564
6565 /* Set new error code */
6566 hi2c->ErrorCode |= ErrorCode;
6567
6568 /* Disable Interrupts */
6569 if ((tmpstate == HAL_I2C_STATE_LISTEN) ||
6570 (tmpstate == HAL_I2C_STATE_BUSY_TX_LISTEN) ||
6571 (tmpstate == HAL_I2C_STATE_BUSY_RX_LISTEN))
6572 {
6573 /* Disable all interrupts, except interrupts related to LISTEN state */
6574 I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT | I2C_XFER_TX_IT);
6575
6576 /* keep HAL_I2C_STATE_LISTEN if set */
6578 hi2c->XferISR = I2C_Slave_ISR_IT;
6579 }
6580 else
6581 {
6582 /* Disable all interrupts */
6583 I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_RX_IT | I2C_XFER_TX_IT);
6584
6585 /* Flush TX register */
6586 I2C_Flush_TXDR(hi2c);
6587
6588 /* If state is an abort treatment on going, don't change state */
6589 /* This change will be do later */
6590 if (hi2c->State != HAL_I2C_STATE_ABORT)
6591 {
6592 /* Set HAL_I2C_STATE_READY */
6593 hi2c->State = HAL_I2C_STATE_READY;
6594
6595 /* Check if a STOPF is detected */
6597 {
6598 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
6599 {
6601 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
6602 }
6603
6604 /* Clear STOP Flag */
6606 }
6607
6608 }
6609 hi2c->XferISR = NULL;
6610 }
6611
6612 /* Abort DMA TX transfer if any */
6613 tmppreviousstate = hi2c->PreviousState;
6614
6615 if ((hi2c->hdmatx != NULL) && ((tmppreviousstate == I2C_STATE_MASTER_BUSY_TX) || \
6616 (tmppreviousstate == I2C_STATE_SLAVE_BUSY_TX)))
6617 {
6618 if ((hi2c->Instance->CR1 & I2C_CR1_TXDMAEN) == I2C_CR1_TXDMAEN)
6619 {
6620 hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
6621 }
6622
6624 {
6625 /* Set the I2C DMA Abort callback :
6626 will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
6627 hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort;
6628
6629 /* Process Unlocked */
6630 __HAL_UNLOCK(hi2c);
6631
6632 /* Abort DMA TX */
6633 if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK)
6634 {
6635 /* Call Directly XferAbortCallback function in case of error */
6636 hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx);
6637 }
6638 }
6639 else
6640 {
6641 I2C_TreatErrorCallback(hi2c);
6642 }
6643 }
6644 /* Abort DMA RX transfer if any */
6645 else if ((hi2c->hdmarx != NULL) && ((tmppreviousstate == I2C_STATE_MASTER_BUSY_RX) || \
6646 (tmppreviousstate == I2C_STATE_SLAVE_BUSY_RX)))
6647 {
6648 if ((hi2c->Instance->CR1 & I2C_CR1_RXDMAEN) == I2C_CR1_RXDMAEN)
6649 {
6650 hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
6651 }
6652
6654 {
6655 /* Set the I2C DMA Abort callback :
6656 will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
6657 hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort;
6658
6659 /* Process Unlocked */
6660 __HAL_UNLOCK(hi2c);
6661
6662 /* Abort DMA RX */
6663 if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK)
6664 {
6665 /* Call Directly hi2c->hdmarx->XferAbortCallback function in case of error */
6666 hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx);
6667 }
6668 }
6669 else
6670 {
6671 I2C_TreatErrorCallback(hi2c);
6672 }
6673 }
6674 else
6675 {
6676 I2C_TreatErrorCallback(hi2c);
6677 }
6678}
6679
6680/**
6681 * @brief I2C Error callback treatment.
6682 * @param hi2c I2C handle.
6683 * @retval None
6684 */
6685static void I2C_TreatErrorCallback(I2C_HandleTypeDef *hi2c)
6686{
6687 if (hi2c->State == HAL_I2C_STATE_ABORT)
6688 {
6689 hi2c->State = HAL_I2C_STATE_READY;
6690 hi2c->PreviousState = I2C_STATE_NONE;
6691
6692 /* Process Unlocked */
6693 __HAL_UNLOCK(hi2c);
6694
6695 /* Call the corresponding callback to inform upper layer of End of Transfer */
6696#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6697 hi2c->AbortCpltCallback(hi2c);
6698#else
6700#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
6701 }
6702 else
6703 {
6704 hi2c->PreviousState = I2C_STATE_NONE;
6705
6706 /* Process Unlocked */
6707 __HAL_UNLOCK(hi2c);
6708
6709 /* Call the corresponding callback to inform upper layer of End of Transfer */
6710#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6711 hi2c->ErrorCallback(hi2c);
6712#else
6714#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
6715 }
6716}
6717
6718/**
6719 * @brief I2C Tx data register flush process.
6720 * @param hi2c I2C handle.
6721 * @retval None
6722 */
6723static void I2C_Flush_TXDR(I2C_HandleTypeDef *hi2c)
6724{
6725 /* If a pending TXIS flag is set */
6726 /* Write a dummy data in TXDR to clear it */
6728 {
6729 hi2c->Instance->TXDR = 0x00U;
6730 }
6731
6732 /* Flush TX register if not empty */
6734 {
6736 }
6737}
6738
6739/**
6740 * @brief DMA I2C master transmit process complete callback.
6741 * @param hdma DMA handle
6742 * @retval None
6743 */
6744static void I2C_DMAMasterTransmitCplt(DMA_HandleTypeDef *hdma)
6745{
6746 /* Derogation MISRAC2012-Rule-11.5 */
6747 I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent);
6748
6749 /* Disable DMA Request */
6750 hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
6751
6752 /* If last transfer, enable STOP interrupt */
6753 if (hi2c->XferCount == 0U)
6754 {
6755 /* Enable STOP interrupt */
6756 I2C_Enable_IRQ(hi2c, I2C_XFER_CPLT_IT);
6757 }
6758 /* else prepare a new DMA transfer and enable TCReload interrupt */
6759 else
6760 {
6761 /* Update Buffer pointer */
6762 hi2c->pBuffPtr += hi2c->XferSize;
6763
6764 /* Set the XferSize to transfer */
6765 if (hi2c->XferCount > MAX_NBYTE_SIZE)
6766 {
6767 hi2c->XferSize = MAX_NBYTE_SIZE;
6768 }
6769 else
6770 {
6771 hi2c->XferSize = hi2c->XferCount;
6772 }
6773
6774 /* Enable the DMA stream or channel depends on Instance */
6775 if (HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->TXDR,
6776 hi2c->XferSize) != HAL_OK)
6777 {
6778 /* Call the corresponding callback to inform upper layer of End of Transfer */
6779 I2C_ITError(hi2c, HAL_I2C_ERROR_DMA);
6780 }
6781 else
6782 {
6783 /* Enable TC interrupts */
6784 I2C_Enable_IRQ(hi2c, I2C_XFER_RELOAD_IT);
6785 }
6786 }
6787}
6788
6789
6790/**
6791 * @brief DMA I2C slave transmit process complete callback.
6792 * @param hdma DMA handle
6793 * @retval None
6794 */
6795static void I2C_DMASlaveTransmitCplt(DMA_HandleTypeDef *hdma)
6796{
6797 /* Derogation MISRAC2012-Rule-11.5 */
6798 I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent);
6799 uint32_t tmpoptions = hi2c->XferOptions;
6800
6801 if ((tmpoptions == I2C_NEXT_FRAME) || (tmpoptions == I2C_FIRST_FRAME))
6802 {
6803 /* Disable DMA Request */
6804 hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
6805
6806 /* Last Byte is Transmitted */
6807 /* Call I2C Slave Sequential complete process */
6808 I2C_ITSlaveSeqCplt(hi2c);
6809 }
6810 else
6811 {
6812 /* No specific action, Master fully manage the generation of STOP condition */
6813 /* Mean that this generation can arrive at any time, at the end or during DMA process */
6814 /* So STOP condition should be manage through Interrupt treatment */
6815 }
6816}
6817
6818
6819/**
6820 * @brief DMA I2C master receive process complete callback.
6821 * @param hdma DMA handle
6822 * @retval None
6823 */
6824static void I2C_DMAMasterReceiveCplt(DMA_HandleTypeDef *hdma)
6825{
6826 /* Derogation MISRAC2012-Rule-11.5 */
6827 I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent);
6828
6829 /* Disable DMA Request */
6830 hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
6831
6832 /* If last transfer, enable STOP interrupt */
6833 if (hi2c->XferCount == 0U)
6834 {
6835 /* Enable STOP interrupt */
6836 I2C_Enable_IRQ(hi2c, I2C_XFER_CPLT_IT);
6837 }
6838 /* else prepare a new DMA transfer and enable TCReload interrupt */
6839 else
6840 {
6841 /* Update Buffer pointer */
6842 hi2c->pBuffPtr += hi2c->XferSize;
6843
6844 /* Set the XferSize to transfer */
6845 if (hi2c->XferCount > MAX_NBYTE_SIZE)
6846 {
6847 hi2c->XferSize = MAX_NBYTE_SIZE;
6848 }
6849 else
6850 {
6851 hi2c->XferSize = hi2c->XferCount;
6852 }
6853
6854 /* Enable the DMA stream or channel depends on Instance */
6855 if (HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)hi2c->pBuffPtr,
6856 hi2c->XferSize) != HAL_OK)
6857 {
6858 /* Call the corresponding callback to inform upper layer of End of Transfer */
6859 I2C_ITError(hi2c, HAL_I2C_ERROR_DMA);
6860 }
6861 else
6862 {
6863 /* Enable TC interrupts */
6864 I2C_Enable_IRQ(hi2c, I2C_XFER_RELOAD_IT);
6865 }
6866 }
6867}
6868
6869
6870/**
6871 * @brief DMA I2C slave receive process complete callback.
6872 * @param hdma DMA handle
6873 * @retval None
6874 */
6875static void I2C_DMASlaveReceiveCplt(DMA_HandleTypeDef *hdma)
6876{
6877 /* Derogation MISRAC2012-Rule-11.5 */
6878 I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent);
6879 uint32_t tmpoptions = hi2c->XferOptions;
6880
6881 if ((I2C_GET_DMA_REMAIN_DATA(hi2c->hdmarx) == 0U) && \
6882 (tmpoptions != I2C_NO_OPTION_FRAME))
6883 {
6884 /* Disable DMA Request */
6885 hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
6886
6887 /* Call I2C Slave Sequential complete process */
6888 I2C_ITSlaveSeqCplt(hi2c);
6889 }
6890 else
6891 {
6892 /* No specific action, Master fully manage the generation of STOP condition */
6893 /* Mean that this generation can arrive at any time, at the end or during DMA process */
6894 /* So STOP condition should be manage through Interrupt treatment */
6895 }
6896}
6897
6898
6899/**
6900 * @brief DMA I2C communication error callback.
6901 * @param hdma DMA handle
6902 * @retval None
6903 */
6904static void I2C_DMAError(DMA_HandleTypeDef *hdma)
6905{
6906 uint32_t treatdmaerror = 0U;
6907 /* Derogation MISRAC2012-Rule-11.5 */
6908 I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent);
6909
6910 if (hi2c->hdmatx != NULL)
6911 {
6912 if (I2C_GET_DMA_REMAIN_DATA(hi2c->hdmatx) == 0U)
6913 {
6914 treatdmaerror = 1U;
6915 }
6916 }
6917
6918 if (hi2c->hdmarx != NULL)
6919 {
6920 if (I2C_GET_DMA_REMAIN_DATA(hi2c->hdmarx) == 0U)
6921 {
6922 treatdmaerror = 1U;
6923 }
6924 }
6925
6926 /* Check if a FIFO error is detected, if true normal use case, so no specific action to perform */
6927 if (!((HAL_DMA_GetError(hdma) == HAL_DMA_ERROR_FE)) && (treatdmaerror != 0U))
6928 {
6929 /* Disable Acknowledge */
6930 hi2c->Instance->CR2 |= I2C_CR2_NACK;
6931
6932 /* Call the corresponding callback to inform upper layer of End of Transfer */
6933 I2C_ITError(hi2c, HAL_I2C_ERROR_DMA);
6934 }
6935}
6936
6937
6938/**
6939 * @brief DMA I2C communication abort callback
6940 * (To be called at end of DMA Abort procedure).
6941 * @param hdma DMA handle.
6942 * @retval None
6943 */
6944static void I2C_DMAAbort(DMA_HandleTypeDef *hdma)
6945{
6946 /* Derogation MISRAC2012-Rule-11.5 */
6947 I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent);
6948
6949 /* Reset AbortCpltCallback */
6950 if (hi2c->hdmatx != NULL)
6951 {
6952 hi2c->hdmatx->XferAbortCallback = NULL;
6953 }
6954 if (hi2c->hdmarx != NULL)
6955 {
6956 hi2c->hdmarx->XferAbortCallback = NULL;
6957 }
6958
6959 I2C_TreatErrorCallback(hi2c);
6960}
6961
6962
6963/**
6964 * @brief This function handles I2C Communication Timeout. It waits
6965 * until a flag is no longer in the specified status.
6966 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
6967 * the configuration information for the specified I2C.
6968 * @param Flag Specifies the I2C flag to check.
6969 * @param Status The actual Flag status (SET or RESET).
6970 * @param Timeout Timeout duration
6971 * @param Tickstart Tick start value
6972 * @retval HAL status
6973 */
6974static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status,
6975 uint32_t Timeout, uint32_t Tickstart)
6976{
6977 while (__HAL_I2C_GET_FLAG(hi2c, Flag) == Status)
6978 {
6979 /* Check if an error is detected */
6980 if (I2C_IsErrorOccurred(hi2c, Timeout, Tickstart) != HAL_OK)
6981 {
6982 return HAL_ERROR;
6983 }
6984
6985 /* Check for the Timeout */
6986 if (Timeout != HAL_MAX_DELAY)
6987 {
6988 if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
6989 {
6990 if (__HAL_I2C_GET_FLAG(hi2c, Flag) == Status)
6991 {
6993 hi2c->State = HAL_I2C_STATE_READY;
6994 hi2c->Mode = HAL_I2C_MODE_NONE;
6995
6996 /* Process Unlocked */
6997 __HAL_UNLOCK(hi2c);
6998 return HAL_ERROR;
6999 }
7000 }
7001 }
7002 }
7003 return HAL_OK;
7004}
7005
7006/**
7007 * @brief This function handles I2C Communication Timeout for specific usage of TXIS flag.
7008 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
7009 * the configuration information for the specified I2C.
7010 * @param Timeout Timeout duration
7011 * @param Tickstart Tick start value
7012 * @retval HAL status
7013 */
7014static HAL_StatusTypeDef I2C_WaitOnTXISFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout,
7015 uint32_t Tickstart)
7016{
7017 while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == RESET)
7018 {
7019 /* Check if an error is detected */
7020 if (I2C_IsErrorOccurred(hi2c, Timeout, Tickstart) != HAL_OK)
7021 {
7022 return HAL_ERROR;
7023 }
7024
7025 /* Check for the Timeout */
7026 if (Timeout != HAL_MAX_DELAY)
7027 {
7028 if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
7029 {
7031 {
7033 hi2c->State = HAL_I2C_STATE_READY;
7034 hi2c->Mode = HAL_I2C_MODE_NONE;
7035
7036 /* Process Unlocked */
7037 __HAL_UNLOCK(hi2c);
7038
7039 return HAL_ERROR;
7040 }
7041 }
7042 }
7043 }
7044 return HAL_OK;
7045}
7046
7047/**
7048 * @brief This function handles I2C Communication Timeout for specific usage of STOP flag.
7049 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
7050 * the configuration information for the specified I2C.
7051 * @param Timeout Timeout duration
7052 * @param Tickstart Tick start value
7053 * @retval HAL status
7054 */
7055static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout,
7056 uint32_t Tickstart)
7057{
7058 while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET)
7059 {
7060 /* Check if an error is detected */
7061 if (I2C_IsErrorOccurred(hi2c, Timeout, Tickstart) != HAL_OK)
7062 {
7063 return HAL_ERROR;
7064 }
7065
7066 /* Check for the Timeout */
7067 if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
7068 {
7070 {
7072 hi2c->State = HAL_I2C_STATE_READY;
7073 hi2c->Mode = HAL_I2C_MODE_NONE;
7074
7075 /* Process Unlocked */
7076 __HAL_UNLOCK(hi2c);
7077
7078 return HAL_ERROR;
7079 }
7080 }
7081 }
7082 return HAL_OK;
7083}
7084
7085/**
7086 * @brief This function handles I2C Communication Timeout for specific usage of RXNE flag.
7087 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
7088 * the configuration information for the specified I2C.
7089 * @param Timeout Timeout duration
7090 * @param Tickstart Tick start value
7091 * @retval HAL status
7092 */
7093static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout,
7094 uint32_t Tickstart)
7095{
7097
7098 while ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == RESET) && (status == HAL_OK))
7099 {
7100 /* Check if an error is detected */
7101 if (I2C_IsErrorOccurred(hi2c, Timeout, Tickstart) != HAL_OK)
7102 {
7103 status = HAL_ERROR;
7104 }
7105
7106 /* Check if a STOPF is detected */
7107 if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET) && (status == HAL_OK))
7108 {
7109 /* Check if an RXNE is pending */
7110 /* Store Last receive data if any */
7111 if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET) && (hi2c->XferSize > 0U))
7112 {
7113 /* Return HAL_OK */
7114 /* The Reading of data from RXDR will be done in caller function */
7115 status = HAL_OK;
7116 }
7117
7118 /* Check a no-acknowledge have been detected */
7119 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
7120 {
7123
7124 /* Clear STOP Flag */
7126
7127 /* Clear Configuration Register 2 */
7128 I2C_RESET_CR2(hi2c);
7129
7130 hi2c->State = HAL_I2C_STATE_READY;
7131 hi2c->Mode = HAL_I2C_MODE_NONE;
7132
7133 /* Process Unlocked */
7134 __HAL_UNLOCK(hi2c);
7135
7136 status = HAL_ERROR;
7137 }
7138 else
7139 {
7141 }
7142 }
7143
7144 /* Check for the Timeout */
7145 if ((((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U)) && (status == HAL_OK))
7146 {
7148 {
7150 hi2c->State = HAL_I2C_STATE_READY;
7151
7152 /* Process Unlocked */
7153 __HAL_UNLOCK(hi2c);
7154
7155 status = HAL_ERROR;
7156 }
7157 }
7158 }
7159 return status;
7160}
7161
7162/**
7163 * @brief This function handles errors detection during an I2C Communication.
7164 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
7165 * the configuration information for the specified I2C.
7166 * @param Timeout Timeout duration
7167 * @param Tickstart Tick start value
7168 * @retval HAL status
7169 */
7170static HAL_StatusTypeDef I2C_IsErrorOccurred(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart)
7171{
7173 uint32_t itflag = hi2c->Instance->ISR;
7174 uint32_t error_code = 0;
7175 uint32_t tickstart = Tickstart;
7176 uint32_t tmp1;
7178
7179 if (HAL_IS_BIT_SET(itflag, I2C_FLAG_AF))
7180 {
7181 /* Clear NACKF Flag */
7183
7184 /* Wait until STOP Flag is set or timeout occurred */
7185 /* AutoEnd should be initiate after AF */
7186 while ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET) && (status == HAL_OK))
7187 {
7188 /* Check for the Timeout */
7189 if (Timeout != HAL_MAX_DELAY)
7190 {
7191 if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
7192 {
7193 tmp1 = (uint32_t)(hi2c->Instance->CR2 & I2C_CR2_STOP);
7194 tmp2 = hi2c->Mode;
7195
7196 /* In case of I2C still busy, try to regenerate a STOP manually */
7197 if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET) && \
7198 (tmp1 != I2C_CR2_STOP) && \
7199 (tmp2 != HAL_I2C_MODE_SLAVE))
7200 {
7201 /* Generate Stop */
7202 hi2c->Instance->CR2 |= I2C_CR2_STOP;
7203
7204 /* Update Tick with new reference */
7205 tickstart = HAL_GetTick();
7206 }
7207
7208 while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET)
7209 {
7210 /* Check for the Timeout */
7211 if ((HAL_GetTick() - tickstart) > I2C_TIMEOUT_STOPF)
7212 {
7213 error_code |= HAL_I2C_ERROR_TIMEOUT;
7214
7215 status = HAL_ERROR;
7216
7217 break;
7218 }
7219 }
7220 }
7221 }
7222 }
7223
7224 /* In case STOP Flag is detected, clear it */
7225 if (status == HAL_OK)
7226 {
7227 /* Clear STOP Flag */
7229 }
7230
7231 error_code |= HAL_I2C_ERROR_AF;
7232
7233 status = HAL_ERROR;
7234 }
7235
7236 /* Refresh Content of Status register */
7237 itflag = hi2c->Instance->ISR;
7238
7239 /* Then verify if an additional errors occurs */
7240 /* Check if a Bus error occurred */
7241 if (HAL_IS_BIT_SET(itflag, I2C_FLAG_BERR))
7242 {
7243 error_code |= HAL_I2C_ERROR_BERR;
7244
7245 /* Clear BERR flag */
7247
7248 status = HAL_ERROR;
7249 }
7250
7251 /* Check if an Over-Run/Under-Run error occurred */
7252 if (HAL_IS_BIT_SET(itflag, I2C_FLAG_OVR))
7253 {
7254 error_code |= HAL_I2C_ERROR_OVR;
7255
7256 /* Clear OVR flag */
7258
7259 status = HAL_ERROR;
7260 }
7261
7262 /* Check if an Arbitration Loss error occurred */
7263 if (HAL_IS_BIT_SET(itflag, I2C_FLAG_ARLO))
7264 {
7265 error_code |= HAL_I2C_ERROR_ARLO;
7266
7267 /* Clear ARLO flag */
7269
7270 status = HAL_ERROR;
7271 }
7272
7273 if (status != HAL_OK)
7274 {
7275 /* Flush TX register */
7276 I2C_Flush_TXDR(hi2c);
7277
7278 /* Clear Configuration Register 2 */
7279 I2C_RESET_CR2(hi2c);
7280
7281 hi2c->ErrorCode |= error_code;
7282 hi2c->State = HAL_I2C_STATE_READY;
7283 hi2c->Mode = HAL_I2C_MODE_NONE;
7284
7285 /* Process Unlocked */
7286 __HAL_UNLOCK(hi2c);
7287 }
7288
7289 return status;
7290}
7291
7292/**
7293 * @brief Handles I2Cx communication when starting transfer or during transfer (TC or TCR flag are set).
7294 * @param hi2c I2C handle.
7295 * @param DevAddress Specifies the slave address to be programmed.
7296 * @param Size Specifies the number of bytes to be programmed.
7297 * This parameter must be a value between 0 and 255.
7298 * @param Mode New state of the I2C START condition generation.
7299 * This parameter can be one of the following values:
7300 * @arg @ref I2C_RELOAD_MODE Enable Reload mode .
7301 * @arg @ref I2C_AUTOEND_MODE Enable Automatic end mode.
7302 * @arg @ref I2C_SOFTEND_MODE Enable Software end mode.
7303 * @param Request New state of the I2C START condition generation.
7304 * This parameter can be one of the following values:
7305 * @arg @ref I2C_NO_STARTSTOP Don't Generate stop and start condition.
7306 * @arg @ref I2C_GENERATE_STOP Generate stop condition (Size should be set to 0).
7307 * @arg @ref I2C_GENERATE_START_READ Generate Restart for read request.
7308 * @arg @ref I2C_GENERATE_START_WRITE Generate Restart for write request.
7309 * @retval None
7310 */
7311static void I2C_TransferConfig(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t Size, uint32_t Mode,
7312 uint32_t Request)
7313{
7314 uint32_t tmp;
7315
7316 /* Check the parameters */
7320
7321 /* Declaration of tmp to prevent undefined behavior of volatile usage */
7322 tmp = ((uint32_t)(((uint32_t)DevAddress & I2C_CR2_SADD) | \
7323 (((uint32_t)Size << I2C_CR2_NBYTES_Pos) & I2C_CR2_NBYTES) | \
7324 (uint32_t)Mode | (uint32_t)Request) & (~0x80000000U));
7325
7326 /* update CR2 register */
7327 MODIFY_REG(hi2c->Instance->CR2, \
7329 (I2C_CR2_RD_WRN & (uint32_t)(Request >> (31U - I2C_CR2_RD_WRN_Pos))) | \
7330 I2C_CR2_START | I2C_CR2_STOP)), tmp);
7331}
7332
7333/**
7334 * @brief Manage the enabling of Interrupts.
7335 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
7336 * the configuration information for the specified I2C.
7337 * @param InterruptRequest Value of @ref I2C_Interrupt_configuration_definition.
7338 * @retval None
7339 */
7340static void I2C_Enable_IRQ(I2C_HandleTypeDef *hi2c, uint16_t InterruptRequest)
7341{
7342 uint32_t tmpisr = 0U;
7343
7344 if ((hi2c->XferISR != I2C_Master_ISR_DMA) && \
7345 (hi2c->XferISR != I2C_Slave_ISR_DMA) && \
7346 (hi2c->XferISR != I2C_Mem_ISR_DMA))
7347 {
7348 if ((InterruptRequest & I2C_XFER_LISTEN_IT) == I2C_XFER_LISTEN_IT)
7349 {
7350 /* Enable ERR, STOP, NACK and ADDR interrupts */
7352 }
7353
7354 if ((InterruptRequest & I2C_XFER_TX_IT) == I2C_XFER_TX_IT)
7355 {
7356 /* Enable ERR, TC, STOP, NACK and TXI interrupts */
7358 }
7359
7360 if ((InterruptRequest & I2C_XFER_RX_IT) == I2C_XFER_RX_IT)
7361 {
7362 /* Enable ERR, TC, STOP, NACK and RXI interrupts */
7364 }
7365
7366 if (InterruptRequest == I2C_XFER_ERROR_IT)
7367 {
7368 /* Enable ERR and NACK interrupts */
7369 tmpisr |= I2C_IT_ERRI | I2C_IT_NACKI;
7370 }
7371
7372 if (InterruptRequest == I2C_XFER_CPLT_IT)
7373 {
7374 /* Enable STOP interrupts */
7375 tmpisr |= I2C_IT_STOPI;
7376 }
7377 }
7378
7379 else
7380 {
7381 if ((InterruptRequest & I2C_XFER_LISTEN_IT) == I2C_XFER_LISTEN_IT)
7382 {
7383 /* Enable ERR, STOP, NACK and ADDR interrupts */
7385 }
7386
7387 if ((InterruptRequest & I2C_XFER_TX_IT) == I2C_XFER_TX_IT)
7388 {
7389 /* Enable ERR, TC, STOP, NACK and TXI interrupts */
7391 }
7392
7393 if ((InterruptRequest & I2C_XFER_RX_IT) == I2C_XFER_RX_IT)
7394 {
7395 /* Enable ERR, TC, STOP, NACK and RXI interrupts */
7397 }
7398
7399 if (InterruptRequest == I2C_XFER_ERROR_IT)
7400 {
7401 /* Enable ERR and NACK interrupts */
7402 tmpisr |= I2C_IT_ERRI | I2C_IT_NACKI;
7403 }
7404
7405 if (InterruptRequest == I2C_XFER_CPLT_IT)
7406 {
7407 /* Enable STOP interrupts */
7408 tmpisr |= (I2C_IT_STOPI | I2C_IT_TCI);
7409 }
7410
7411 if (InterruptRequest == I2C_XFER_RELOAD_IT)
7412 {
7413 /* Enable TC interrupts */
7414 tmpisr |= I2C_IT_TCI;
7415 }
7416 }
7417
7418 /* Enable interrupts only at the end */
7419 /* to avoid the risk of I2C interrupt handle execution before */
7420 /* all interrupts requested done */
7421 __HAL_I2C_ENABLE_IT(hi2c, tmpisr);
7422}
7423
7424/**
7425 * @brief Manage the disabling of Interrupts.
7426 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
7427 * the configuration information for the specified I2C.
7428 * @param InterruptRequest Value of @ref I2C_Interrupt_configuration_definition.
7429 * @retval None
7430 */
7431static void I2C_Disable_IRQ(I2C_HandleTypeDef *hi2c, uint16_t InterruptRequest)
7432{
7433 uint32_t tmpisr = 0U;
7434
7435 if ((InterruptRequest & I2C_XFER_TX_IT) == I2C_XFER_TX_IT)
7436 {
7437 /* Disable TC and TXI interrupts */
7438 tmpisr |= I2C_IT_TCI | I2C_IT_TXI;
7439
7440 if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) != (uint32_t)HAL_I2C_STATE_LISTEN)
7441 {
7442 /* Disable NACK and STOP interrupts */
7444 }
7445 }
7446
7447 if ((InterruptRequest & I2C_XFER_RX_IT) == I2C_XFER_RX_IT)
7448 {
7449 /* Disable TC and RXI interrupts */
7450 tmpisr |= I2C_IT_TCI | I2C_IT_RXI;
7451
7452 if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) != (uint32_t)HAL_I2C_STATE_LISTEN)
7453 {
7454 /* Disable NACK and STOP interrupts */
7456 }
7457 }
7458
7459 if ((InterruptRequest & I2C_XFER_LISTEN_IT) == I2C_XFER_LISTEN_IT)
7460 {
7461 /* Disable ADDR, NACK and STOP interrupts */
7463 }
7464
7465 if (InterruptRequest == I2C_XFER_ERROR_IT)
7466 {
7467 /* Enable ERR and NACK interrupts */
7468 tmpisr |= I2C_IT_ERRI | I2C_IT_NACKI;
7469 }
7470
7471 if (InterruptRequest == I2C_XFER_CPLT_IT)
7472 {
7473 /* Enable STOP interrupts */
7474 tmpisr |= I2C_IT_STOPI;
7475 }
7476
7477 if (InterruptRequest == I2C_XFER_RELOAD_IT)
7478 {
7479 /* Enable TC interrupts */
7480 tmpisr |= I2C_IT_TCI;
7481 }
7482
7483 /* Disable interrupts only at the end */
7484 /* to avoid a breaking situation like at "t" time */
7485 /* all disable interrupts request are not done */
7486 __HAL_I2C_DISABLE_IT(hi2c, tmpisr);
7487}
7488
7489/**
7490 * @brief Convert I2Cx OTHER_xxx XferOptions to functional XferOptions.
7491 * @param hi2c I2C handle.
7492 * @retval None
7493 */
7494static void I2C_ConvertOtherXferOptions(I2C_HandleTypeDef *hi2c)
7495{
7496 /* if user set XferOptions to I2C_OTHER_FRAME */
7497 /* it request implicitly to generate a restart condition */
7498 /* set XferOptions to I2C_FIRST_FRAME */
7499 if (hi2c->XferOptions == I2C_OTHER_FRAME)
7500 {
7502 }
7503 /* else if user set XferOptions to I2C_OTHER_AND_LAST_FRAME */
7504 /* it request implicitly to generate a restart condition */
7505 /* then generate a stop condition at the end of transfer */
7506 /* set XferOptions to I2C_FIRST_AND_LAST_FRAME */
7507 else if (hi2c->XferOptions == I2C_OTHER_AND_LAST_FRAME)
7508 {
7510 }
7511 else
7512 {
7513 /* Nothing to do */
7514 }
7515}
7516
7517/**
7518 * @}
7519 */
7520
7521#endif /* HAL_I2C_MODULE_ENABLED */
7522/**
7523 * @}
7524 */
7525
7526/**
7527 * @}
7528 */
#define __IO
Defines 'read / write' permissions.
#define HAL_DMA_ERROR_FE
FIFO error.
HAL_StatusTypeDef HAL_DMA_Abort_IT(DMA_HandleTypeDef *hdma)
HAL_StatusTypeDef HAL_DMA_Start_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
uint32_t HAL_DMA_GetError(const DMA_HandleTypeDef *hdma)
HAL_DMA_StateTypeDef HAL_DMA_GetState(const DMA_HandleTypeDef *hdma)
struct __DMA_HandleTypeDef DMA_HandleTypeDef
DMA handle Structure definition.
@ HAL_DMA_STATE_READY
DMA initialized and ready for use.
#define CLEAR_BIT(REG, BIT)
Definition stm32h7xx.h:209
#define SET_BIT(REG, BIT)
Definition stm32h7xx.h:207
#define MODIFY_REG(REG, CLEARMASK, SETMASK)
Definition stm32h7xx.h:219
#define IS_I2C_ALL_INSTANCE(INSTANCE)
#define READ_REG(REG)
Definition stm32h7xx.h:217
FlagStatus
Definition stm32h7xx.h:181
@ RESET
Definition stm32h7xx.h:182
@ SET
Definition stm32h7xx.h:183
uint32_t HAL_GetTick(void)
Provides a tick value in millisecond.
HAL_I2C_ModeTypeDef
@ HAL_I2C_MODE_MASTER
I2C communication is in Master Mode.
@ HAL_I2C_MODE_MEM
I2C communication is in Memory Mode.
@ HAL_I2C_MODE_SLAVE
I2C communication is in Slave Mode.
@ HAL_I2C_MODE_NONE
No I2C communication on going.
HAL_I2C_StateTypeDef
@ HAL_I2C_STATE_BUSY
An internal process is ongoing.
@ HAL_I2C_STATE_LISTEN
Address Listen Mode is ongoing.
@ HAL_I2C_STATE_BUSY_TX_LISTEN
Address Listen Mode and Data Transmission process is ongoing.
@ HAL_I2C_STATE_ABORT
Abort user request ongoing.
@ HAL_I2C_STATE_BUSY_RX
Data Reception process is ongoing.
@ HAL_I2C_STATE_BUSY_RX_LISTEN
Address Listen Mode and Data Reception process is ongoing.
@ HAL_I2C_STATE_RESET
Peripheral is not yet Initialized.
@ HAL_I2C_STATE_BUSY_TX
Data Transmission process is ongoing.
@ HAL_I2C_STATE_READY
Peripheral Initialized and ready for use.
#define I2C_ADDRESSINGMODE_10BIT
#define I2C_ADDRESSINGMODE_7BIT
#define I2C_DUALADDRESS_ENABLE
#define HAL_I2C_ERROR_ARLO
ARLO error.
#define HAL_I2C_ERROR_NONE
No error.
#define HAL_I2C_ERROR_OVR
OVR error.
#define HAL_I2C_ERROR_SIZE
Size Management error.
#define HAL_I2C_ERROR_BERR
BERR error.
#define HAL_I2C_ERROR_INVALID_PARAM
Invalid Parameters error.
#define HAL_I2C_ERROR_DMA_PARAM
DMA Parameter Error.
#define HAL_I2C_ERROR_AF
ACKF error.
#define HAL_I2C_ERROR_DMA
DMA transfer error.
#define HAL_I2C_ERROR_TIMEOUT
Timeout error.
void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c)
I2C MSP De-Initialization This function freeze the hardware resources used in this example.
HAL_StatusTypeDef HAL_I2C_Init(I2C_HandleTypeDef *hi2c)
HAL_StatusTypeDef HAL_I2C_DeInit(I2C_HandleTypeDef *hi2c)
void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)
I2C MSP Initialization This function configures the hardware resources used in this example.
HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
HAL_StatusTypeDef HAL_I2C_Slave_Seq_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
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)
HAL_StatusTypeDef HAL_I2C_Slave_Seq_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
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)
HAL_StatusTypeDef HAL_I2C_Master_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
HAL_StatusTypeDef HAL_I2C_Master_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
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)
HAL_StatusTypeDef HAL_I2C_Slave_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
HAL_StatusTypeDef HAL_I2C_Master_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
HAL_StatusTypeDef HAL_I2C_Slave_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
HAL_StatusTypeDef HAL_I2C_Master_Seq_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
HAL_StatusTypeDef HAL_I2C_Slave_Seq_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
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)
HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
HAL_StatusTypeDef HAL_I2C_Slave_Transmit(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
HAL_StatusTypeDef HAL_I2C_Slave_Receive(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
HAL_StatusTypeDef HAL_I2C_Master_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
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)
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)
HAL_StatusTypeDef HAL_I2C_Slave_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
HAL_StatusTypeDef HAL_I2C_Master_Seq_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
HAL_StatusTypeDef HAL_I2C_Master_Abort_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress)
HAL_StatusTypeDef HAL_I2C_EnableListen_IT(I2C_HandleTypeDef *hi2c)
HAL_StatusTypeDef HAL_I2C_Slave_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
HAL_StatusTypeDef HAL_I2C_IsDeviceReady(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout)
HAL_StatusTypeDef HAL_I2C_Slave_Seq_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
HAL_StatusTypeDef HAL_I2C_DisableListen_IT(I2C_HandleTypeDef *hi2c)
HAL_I2C_ModeTypeDef HAL_I2C_GetMode(const I2C_HandleTypeDef *hi2c)
uint32_t HAL_I2C_GetError(const I2C_HandleTypeDef *hi2c)
HAL_I2C_StateTypeDef HAL_I2C_GetState(const I2C_HandleTypeDef *hi2c)
#define __HAL_I2C_DISABLE_IT(__HANDLE__, __INTERRUPT__)
Disable the specified I2C interrupt.
#define __HAL_I2C_DISABLE(__HANDLE__)
Disable the specified I2C peripheral.
#define __HAL_I2C_CLEAR_FLAG(__HANDLE__, __FLAG__)
Clear the I2C pending flags which are cleared by writing 1 in a specific bit.
#define __HAL_I2C_ENABLE_IT(__HANDLE__, __INTERRUPT__)
Enable the specified I2C interrupt.
#define __HAL_I2C_ENABLE(__HANDLE__)
Enable the specified I2C peripheral.
#define __HAL_I2C_GET_FLAG(__HANDLE__, __FLAG__)
#define I2C_FLAG_BERR
#define I2C_FLAG_TC
#define I2C_FLAG_DIR
#define I2C_FLAG_AF
#define I2C_FLAG_BUSY
#define I2C_FLAG_ADDR
#define I2C_FLAG_OVR
#define I2C_FLAG_STOPF
#define I2C_FLAG_TXIS
#define I2C_FLAG_TCR
#define I2C_FLAG_RXNE
#define I2C_FLAG_ARLO
#define I2C_FLAG_TXE
void HAL_I2C_EV_IRQHandler(I2C_HandleTypeDef *hi2c)
void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c)
void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode)
void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c)
void HAL_I2C_ER_IRQHandler(I2C_HandleTypeDef *hi2c)
void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c)
void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef *hi2c)
void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c)
void HAL_I2C_AbortCpltCallback(I2C_HandleTypeDef *hi2c)
void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c)
void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c)
#define I2C_IT_STOPI
#define I2C_IT_ADDRI
#define I2C_IT_RXI
#define I2C_IT_TCI
#define I2C_IT_TXI
#define I2C_IT_ERRI
#define I2C_IT_NACKI
#define I2C_MEMADD_SIZE_8BIT
#define I2C_NOSTRETCH_ENABLE
#define IS_TRANSFER_REQUEST(REQUEST)
#define I2C_GET_DIR(__HANDLE__)
#define IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(REQUEST)
#define IS_I2C_ADDRESSING_MODE(MODE)
#define I2C_GET_STOP_MODE(__HANDLE__)
#define I2C_MEM_ADD_MSB(__ADDRESS__)
#define I2C_GET_ADDR_MATCH(__HANDLE__)
#define I2C_CHECK_FLAG(__ISR__, __FLAG__)
#define IS_I2C_GENERAL_CALL(CALL)
#define IS_I2C_OWN_ADDRESS2_MASK(MASK)
#define I2C_GENERATE_START(__ADDMODE__, __ADDRESS__)
#define IS_I2C_OWN_ADDRESS2(ADDRESS2)
#define I2C_RESET_CR2(__HANDLE__)
#define IS_I2C_TRANSFER_OPTIONS_REQUEST(REQUEST)
#define I2C_MEM_ADD_LSB(__ADDRESS__)
#define I2C_GET_OWN_ADDRESS1(__HANDLE__)
#define I2C_CHECK_IT_SOURCE(__CR1__, __IT__)
#define IS_I2C_MEMADD_SIZE(SIZE)
#define I2C_GET_OWN_ADDRESS2(__HANDLE__)
#define IS_I2C_OWN_ADDRESS1(ADDRESS1)
#define IS_I2C_DUAL_ADDRESS(ADDRESS)
#define IS_I2C_NO_STRETCH(STRETCH)
#define IS_TRANSFER_MODE(MODE)
#define I2C_SOFTEND_MODE
#define I2C_RELOAD_MODE
#define I2C_AUTOEND_MODE
#define I2C_GENERATE_START_READ
#define I2C_GENERATE_START_WRITE
#define I2C_NO_STARTSTOP
#define I2C_GENERATE_STOP
#define I2C_DIRECTION_TRANSMIT
#define I2C_DIRECTION_RECEIVE
#define I2C_OTHER_AND_LAST_FRAME
#define I2C_FIRST_AND_LAST_FRAME
#define I2C_NEXT_FRAME
#define I2C_FIRST_FRAME
#define I2C_OTHER_FRAME
struct __I2C_HandleTypeDef I2C_HandleTypeDef
#define I2C_CR2_RD_WRN_Pos
#define I2C_CR2_SADD
Slave address (master mode).
#define I2C_CR1_TXDMAEN
DMA transmission requests enable.
#define I2C_CR2_RELOAD
NBYTES reload mode.
#define I2C_CR2_NBYTES
Number of bytes.
#define I2C_CR2_RD_WRN
Transfer direction (master mode).
#define I2C_CR2_STOP
STOP generation (master mode).
#define I2C_CR2_NBYTES_Pos
#define I2C_CR2_ADD10
10-bit addressing mode (master mode)
#define I2C_CR2_START
START generation.
#define I2C_OAR1_OA1MODE
Own address 1 10-bit mode.
#define I2C_CR1_RXDMAEN
DMA reception requests enable.
#define I2C_CR2_NACK
NACK generation (slave mode).
#define I2C_OAR1_OA1EN
Own address 1 enable.
#define I2C_CR2_AUTOEND
Automatic end mode (master mode).
#define NULL
Definition usbd_def.h:44
@ error
throw a parse_error exception in case of a tag
Definition json.hpp:9186
This file contains all the functions prototypes for the HAL module driver.
#define assert_param(expr)
Uncomment the line below to expanse the "assert_param" macro in the HAL drivers code.
#define HAL_IS_BIT_SET(REG, BIT)
HAL_StatusTypeDef
HAL Status structures definition.
@ HAL_ERROR
@ HAL_OK
@ HAL_BUSY
#define __HAL_UNLOCK(__HANDLE__)
#define UNUSED(x)
#define HAL_MAX_DELAY
@ HAL_UNLOCKED
#define __HAL_LOCK(__HANDLE__)
uint32_t GeneralCallMode
Specifies if general call mode is selected.
uint32_t NoStretchMode
Specifies if nostretch mode is selected.
uint32_t OwnAddress2Masks
Specifies the acknowledge mask address second device own address if dual addressing mode is selected.
uint32_t AddressingMode
Specifies if 7-bit or 10-bit addressing mode is selected.
uint32_t OwnAddress2
Specifies the second device own address if dual addressing mode is selected This parameter can be a 7...
uint32_t Timing
Specifies the I2C_TIMINGR_register value.
uint32_t OwnAddress1
Specifies the first device own address.
uint32_t DualAddressMode
Specifies if dual addressing mode is selected.
__IO uint32_t ISR
I2C Interrupt and status register, Address offset: 0x18.
__IO uint32_t CR2
I2C Control register 2, Address offset: 0x04.
__IO uint32_t RXDR
I2C Receive data register, Address offset: 0x24.
__IO uint32_t OAR2
I2C Own address 2 register, Address offset: 0x0C.
__IO uint32_t CR1
I2C Control register 1, Address offset: 0x00.
__IO uint32_t TIMINGR
I2C Timing register, Address offset: 0x10.
__IO uint32_t TXDR
I2C Transmit data register, Address offset: 0x28.
__IO uint32_t OAR1
I2C Own address 1 register, Address offset: 0x08.
void(* XferAbortCallback)(struct __DMA_HandleTypeDef *hdma)
DMA transfer Abort callback.
void(* XferCpltCallback)(struct __DMA_HandleTypeDef *hdma)
DMA transfer complete callback.
void(* XferErrorCallback)(struct __DMA_HandleTypeDef *hdma)
DMA transfer error callback.
void(* XferHalfCpltCallback)(struct __DMA_HandleTypeDef *hdma)
DMA Half transfer complete callback.
__IO uint32_t PreviousState
I2C communication Previous state.
I2C_TypeDef * Instance
I2C registers base address.
uint16_t XferSize
I2C transfer size.
HAL_StatusTypeDef(* XferISR)(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags, uint32_t ITSources)
I2C transfer IRQ handler function pointer.
__IO uint16_t XferCount
I2C transfer counter.
__IO HAL_I2C_StateTypeDef State
I2C communication state.
__IO uint32_t Memaddress
I2C Target memory address.
DMA_HandleTypeDef * hdmarx
I2C Rx DMA handle parameters.
__IO uint32_t XferOptions
I2C sequantial transfer options, this parameter can be a value of I2C Sequential Transfer Options.
__IO uint32_t Devaddress
I2C Target device address.
__IO uint32_t ErrorCode
I2C Error code.
I2C_InitTypeDef Init
I2C communication parameters.
HAL_LockTypeDef Lock
I2C locking object.
__IO HAL_I2C_ModeTypeDef Mode
I2C communication mode.
__IO uint32_t AddrEventCount
I2C Address Event counter.
DMA_HandleTypeDef * hdmatx
I2C Tx DMA handle parameters.
uint8_t * pBuffPtr
Pointer to I2C transfer buffer.