source: S-port/trunk/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c.c

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