2 ******************************************************************************
\r
3 * @file stm32l4xx_hal_i2c.c
\r
4 * @author MCD Application Team
\r
5 * @brief I2C HAL module driver.
\r
6 * This file provides firmware functions to manage the following
\r
7 * functionalities of the Inter Integrated Circuit (I2C) peripheral:
\r
8 * + Initialization and de-initialization functions
\r
9 * + IO operation functions
\r
10 * + Peripheral State and Errors functions
\r
13 ==============================================================================
\r
14 ##### How to use this driver #####
\r
15 ==============================================================================
\r
17 The I2C HAL driver can be used as follows:
\r
19 (#) Declare a I2C_HandleTypeDef handle structure, for example:
\r
20 I2C_HandleTypeDef hi2c;
\r
22 (#)Initialize the I2C low level resources by implementing the @ref HAL_I2C_MspInit() API:
\r
23 (##) Enable the I2Cx interface clock
\r
24 (##) I2C pins configuration
\r
25 (+++) Enable the clock for the I2C GPIOs
\r
26 (+++) Configure I2C pins as alternate function open-drain
\r
27 (##) NVIC configuration if you need to use interrupt process
\r
28 (+++) Configure the I2Cx interrupt priority
\r
29 (+++) Enable the NVIC I2C IRQ Channel
\r
30 (##) DMA Configuration if you need to use DMA process
\r
31 (+++) Declare a DMA_HandleTypeDef handle structure for the transmit or receive channel
\r
32 (+++) Enable the DMAx interface clock using
\r
33 (+++) Configure the DMA handle parameters
\r
34 (+++) Configure the DMA Tx or Rx channel
\r
35 (+++) Associate the initialized DMA handle to the hi2c DMA Tx or Rx handle
\r
36 (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on
\r
37 the DMA Tx or Rx channel
\r
39 (#) Configure the Communication Clock Timing, Own Address1, Master Addressing mode, Dual Addressing mode,
\r
40 Own Address2, Own Address2 Mask, General call and Nostretch mode in the hi2c Init structure.
\r
42 (#) Initialize the I2C registers by calling the @ref HAL_I2C_Init(), configures also the low level Hardware
\r
43 (GPIO, CLOCK, NVIC...etc) by calling the customized @ref HAL_I2C_MspInit(&hi2c) API.
\r
45 (#) To check if target device is ready for communication, use the function @ref HAL_I2C_IsDeviceReady()
\r
47 (#) For I2C IO and IO MEM operations, three operation modes are available within this driver :
\r
49 *** Polling mode IO operation ***
\r
50 =================================
\r
52 (+) Transmit in master mode an amount of data in blocking mode using @ref HAL_I2C_Master_Transmit()
\r
53 (+) Receive in master mode an amount of data in blocking mode using @ref HAL_I2C_Master_Receive()
\r
54 (+) Transmit in slave mode an amount of data in blocking mode using @ref HAL_I2C_Slave_Transmit()
\r
55 (+) Receive in slave mode an amount of data in blocking mode using @ref HAL_I2C_Slave_Receive()
\r
57 *** Polling mode IO MEM operation ***
\r
58 =====================================
\r
60 (+) Write an amount of data in blocking mode to a specific memory address using @ref HAL_I2C_Mem_Write()
\r
61 (+) Read an amount of data in blocking mode from a specific memory address using @ref HAL_I2C_Mem_Read()
\r
64 *** Interrupt mode IO operation ***
\r
65 ===================================
\r
67 (+) Transmit in master mode an amount of data in non-blocking mode using @ref HAL_I2C_Master_Transmit_IT()
\r
68 (+) At transmission end of transfer, @ref HAL_I2C_MasterTxCpltCallback() is executed and user can
\r
69 add his own code by customization of function pointer @ref HAL_I2C_MasterTxCpltCallback()
\r
70 (+) Receive in master mode an amount of data in non-blocking mode using @ref HAL_I2C_Master_Receive_IT()
\r
71 (+) At reception end of transfer, @ref HAL_I2C_MasterRxCpltCallback() is executed and user can
\r
72 add his own code by customization of function pointer @ref HAL_I2C_MasterRxCpltCallback()
\r
73 (+) Transmit in slave mode an amount of data in non-blocking mode using @ref HAL_I2C_Slave_Transmit_IT()
\r
74 (+) At transmission end of transfer, @ref HAL_I2C_SlaveTxCpltCallback() is executed and user can
\r
75 add his own code by customization of function pointer @ref HAL_I2C_SlaveTxCpltCallback()
\r
76 (+) Receive in slave mode an amount of data in non-blocking mode using @ref HAL_I2C_Slave_Receive_IT()
\r
77 (+) At reception end of transfer, @ref HAL_I2C_SlaveRxCpltCallback() is executed and user can
\r
78 add his own code by customization of function pointer @ref HAL_I2C_SlaveRxCpltCallback()
\r
79 (+) In case of transfer Error, @ref HAL_I2C_ErrorCallback() function is executed and user can
\r
80 add his own code by customization of function pointer @ref HAL_I2C_ErrorCallback()
\r
81 (+) Abort a master I2C process communication with Interrupt using @ref HAL_I2C_Master_Abort_IT()
\r
82 (+) End of abort process, @ref HAL_I2C_AbortCpltCallback() is executed and user can
\r
83 add his own code by customization of function pointer @ref HAL_I2C_AbortCpltCallback()
\r
84 (+) Discard a slave I2C process communication using @ref __HAL_I2C_GENERATE_NACK() macro.
\r
85 This action will inform Master to generate a Stop condition to discard the communication.
\r
88 *** Interrupt mode or DMA mode IO sequential operation ***
\r
89 ==========================================================
\r
91 (@) These interfaces allow to manage a sequential transfer with a repeated start condition
\r
92 when a direction change during transfer
\r
94 (+) A specific option field manage the different steps of a sequential transfer
\r
95 (+) Option field values are defined through @ref I2C_XFEROPTIONS and are listed below:
\r
96 (++) I2C_FIRST_AND_LAST_FRAME: No sequential usage, functionnal is same as associated interfaces in no sequential mode
\r
97 (++) I2C_FIRST_FRAME: Sequential usage, this option allow to manage a sequence with start condition, address
\r
98 and data to transfer without a final stop condition
\r
99 (++) I2C_FIRST_AND_NEXT_FRAME: Sequential usage (Master only), this option allow to manage a sequence with start condition, address
\r
100 and data to transfer without a final stop condition, an then permit a call the same master sequential interface
\r
101 several times (like @ref HAL_I2C_Master_Seq_Transmit_IT() then @ref HAL_I2C_Master_Seq_Transmit_IT()
\r
102 or @ref HAL_I2C_Master_Seq_Transmit_DMA() then @ref HAL_I2C_Master_Seq_Transmit_DMA())
\r
103 (++) I2C_NEXT_FRAME: Sequential usage, this option allow to manage a sequence with a restart condition, address
\r
104 and with new data to transfer if the direction change or manage only the new data to transfer
\r
105 if no direction change and without a final stop condition in both cases
\r
106 (++) I2C_LAST_FRAME: Sequential usage, this option allow to manage a sequance with a restart condition, address
\r
107 and with new data to transfer if the direction change or manage only the new data to transfer
\r
108 if no direction change and with a final stop condition in both cases
\r
109 (++) 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
\r
110 interface several times (link with option I2C_FIRST_AND_NEXT_FRAME).
\r
111 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)
\r
112 or HAL_I2C_Master_Seq_Receive_IT(option I2C_FIRST_AND_NEXT_FRAME then I2C_NEXT_FRAME)
\r
113 or HAL_I2C_Master_Seq_Transmit_DMA(option I2C_FIRST_AND_NEXT_FRAME then I2C_NEXT_FRAME)
\r
114 or HAL_I2C_Master_Seq_Receive_DMA(option I2C_FIRST_AND_NEXT_FRAME then I2C_NEXT_FRAME).
\r
115 Then usage of this option I2C_LAST_FRAME_NO_STOP at the last Transmit or Receive sequence permit to call the oposite interface Receive or Transmit
\r
116 without stopping the communication and so generate a restart condition.
\r
117 (++) I2C_OTHER_FRAME: Sequential usage (Master only), this option allow to manage a restart condition after each call of the same master sequential
\r
119 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)
\r
120 or HAL_I2C_Master_Seq_Receive_IT(option I2C_FIRST_FRAME then I2C_OTHER_FRAME)
\r
121 or HAL_I2C_Master_Seq_Transmit_DMA(option I2C_FIRST_FRAME then I2C_OTHER_FRAME)
\r
122 or HAL_I2C_Master_Seq_Receive_DMA(option I2C_FIRST_FRAME then I2C_OTHER_FRAME).
\r
123 Then usage of this option I2C_OTHER_AND_LAST_FRAME at the last frame to help automatic generation of STOP condition.
\r
125 (+) Differents sequential I2C interfaces are listed below:
\r
126 (++) Sequential transmit in master I2C mode an amount of data in non-blocking mode using @ref HAL_I2C_Master_Seq_Transmit_IT()
\r
127 or using @ref HAL_I2C_Master_Seq_Transmit_DMA()
\r
128 (+++) At transmission end of current frame transfer, @ref HAL_I2C_MasterTxCpltCallback() is executed and user can
\r
129 add his own code by customization of function pointer @ref HAL_I2C_MasterTxCpltCallback()
\r
130 (++) Sequential receive in master I2C mode an amount of data in non-blocking mode using @ref HAL_I2C_Master_Seq_Receive_IT()
\r
131 or using @ref HAL_I2C_Master_Seq_Receive_DMA()
\r
132 (+++) At reception end of current frame transfer, @ref HAL_I2C_MasterRxCpltCallback() is executed and user can
\r
133 add his own code by customization of function pointer @ref HAL_I2C_MasterRxCpltCallback()
\r
134 (++) Abort a master IT or DMA I2C process communication with Interrupt using @ref HAL_I2C_Master_Abort_IT()
\r
135 (+++) End of abort process, @ref HAL_I2C_AbortCpltCallback() is executed and user can
\r
136 add his own code by customization of function pointer @ref HAL_I2C_AbortCpltCallback()
\r
137 (++) Enable/disable the Address listen mode in slave I2C mode using @ref HAL_I2C_EnableListen_IT() @ref HAL_I2C_DisableListen_IT()
\r
138 (+++) When address slave I2C match, @ref HAL_I2C_AddrCallback() is executed and user can
\r
139 add his own code to check the Address Match Code and the transmission direction request by master (Write/Read).
\r
140 (+++) At Listen mode end @ref HAL_I2C_ListenCpltCallback() is executed and user can
\r
141 add his own code by customization of function pointer @ref HAL_I2C_ListenCpltCallback()
\r
142 (++) Sequential transmit in slave I2C mode an amount of data in non-blocking mode using @ref HAL_I2C_Slave_Seq_Transmit_IT()
\r
143 or using @ref HAL_I2C_Slave_Seq_Transmit_DMA()
\r
144 (+++) At transmission end of current frame transfer, @ref HAL_I2C_SlaveTxCpltCallback() is executed and user can
\r
145 add his own code by customization of function pointer @ref HAL_I2C_SlaveTxCpltCallback()
\r
146 (++) Sequential receive in slave I2C mode an amount of data in non-blocking mode using @ref HAL_I2C_Slave_Seq_Receive_IT()
\r
147 or using @ref HAL_I2C_Slave_Seq_Receive_DMA()
\r
148 (+++) At reception end of current frame transfer, @ref HAL_I2C_SlaveRxCpltCallback() is executed and user can
\r
149 add his own code by customization of function pointer @ref HAL_I2C_SlaveRxCpltCallback()
\r
150 (++) In case of transfer Error, @ref HAL_I2C_ErrorCallback() function is executed and user can
\r
151 add his own code by customization of function pointer @ref HAL_I2C_ErrorCallback()
\r
152 (++) Discard a slave I2C process communication using @ref __HAL_I2C_GENERATE_NACK() macro.
\r
153 This action will inform Master to generate a Stop condition to discard the communication.
\r
155 *** Interrupt mode IO MEM operation ***
\r
156 =======================================
\r
158 (+) Write an amount of data in non-blocking mode with Interrupt to a specific memory address using
\r
159 @ref HAL_I2C_Mem_Write_IT()
\r
160 (+) At Memory end of write transfer, @ref HAL_I2C_MemTxCpltCallback() is executed and user can
\r
161 add his own code by customization of function pointer @ref HAL_I2C_MemTxCpltCallback()
\r
162 (+) Read an amount of data in non-blocking mode with Interrupt from a specific memory address using
\r
163 @ref HAL_I2C_Mem_Read_IT()
\r
164 (+) At Memory end of read transfer, @ref HAL_I2C_MemRxCpltCallback() is executed and user can
\r
165 add his own code by customization of function pointer @ref HAL_I2C_MemRxCpltCallback()
\r
166 (+) In case of transfer Error, @ref HAL_I2C_ErrorCallback() function is executed and user can
\r
167 add his own code by customization of function pointer @ref HAL_I2C_ErrorCallback()
\r
169 *** DMA mode IO operation ***
\r
170 ==============================
\r
172 (+) Transmit in master mode an amount of data in non-blocking mode (DMA) using
\r
173 @ref HAL_I2C_Master_Transmit_DMA()
\r
174 (+) At transmission end of transfer, @ref HAL_I2C_MasterTxCpltCallback() is executed and user can
\r
175 add his own code by customization of function pointer @ref HAL_I2C_MasterTxCpltCallback()
\r
176 (+) Receive in master mode an amount of data in non-blocking mode (DMA) using
\r
177 @ref HAL_I2C_Master_Receive_DMA()
\r
178 (+) At reception end of transfer, @ref HAL_I2C_MasterRxCpltCallback() is executed and user can
\r
179 add his own code by customization of function pointer @ref HAL_I2C_MasterRxCpltCallback()
\r
180 (+) Transmit in slave mode an amount of data in non-blocking mode (DMA) using
\r
181 @ref HAL_I2C_Slave_Transmit_DMA()
\r
182 (+) At transmission end of transfer, @ref HAL_I2C_SlaveTxCpltCallback() is executed and user can
\r
183 add his own code by customization of function pointer @ref HAL_I2C_SlaveTxCpltCallback()
\r
184 (+) Receive in slave mode an amount of data in non-blocking mode (DMA) using
\r
185 @ref HAL_I2C_Slave_Receive_DMA()
\r
186 (+) At reception end of transfer, @ref HAL_I2C_SlaveRxCpltCallback() is executed and user can
\r
187 add his own code by customization of function pointer @ref HAL_I2C_SlaveRxCpltCallback()
\r
188 (+) In case of transfer Error, @ref HAL_I2C_ErrorCallback() function is executed and user can
\r
189 add his own code by customization of function pointer @ref HAL_I2C_ErrorCallback()
\r
190 (+) Abort a master I2C process communication with Interrupt using @ref HAL_I2C_Master_Abort_IT()
\r
191 (+) End of abort process, @ref HAL_I2C_AbortCpltCallback() is executed and user can
\r
192 add his own code by customization of function pointer @ref HAL_I2C_AbortCpltCallback()
\r
193 (+) Discard a slave I2C process communication using @ref __HAL_I2C_GENERATE_NACK() macro.
\r
194 This action will inform Master to generate a Stop condition to discard the communication.
\r
196 *** DMA mode IO MEM operation ***
\r
197 =================================
\r
199 (+) Write an amount of data in non-blocking mode with DMA to a specific memory address using
\r
200 @ref HAL_I2C_Mem_Write_DMA()
\r
201 (+) At Memory end of write transfer, @ref HAL_I2C_MemTxCpltCallback() is executed and user can
\r
202 add his own code by customization of function pointer @ref HAL_I2C_MemTxCpltCallback()
\r
203 (+) Read an amount of data in non-blocking mode with DMA from a specific memory address using
\r
204 @ref HAL_I2C_Mem_Read_DMA()
\r
205 (+) At Memory end of read transfer, @ref HAL_I2C_MemRxCpltCallback() is executed and user can
\r
206 add his own code by customization of function pointer @ref HAL_I2C_MemRxCpltCallback()
\r
207 (+) In case of transfer Error, @ref HAL_I2C_ErrorCallback() function is executed and user can
\r
208 add his own code by customization of function pointer @ref HAL_I2C_ErrorCallback()
\r
211 *** I2C HAL driver macros list ***
\r
212 ==================================
\r
214 Below the list of most used macros in I2C HAL driver.
\r
216 (+) @ref __HAL_I2C_ENABLE: Enable the I2C peripheral
\r
217 (+) @ref __HAL_I2C_DISABLE: Disable the I2C peripheral
\r
218 (+) @ref __HAL_I2C_GENERATE_NACK: Generate a Non-Acknowledge I2C peripheral in Slave mode
\r
219 (+) @ref __HAL_I2C_GET_FLAG: Check whether the specified I2C flag is set or not
\r
220 (+) @ref __HAL_I2C_CLEAR_FLAG: Clear the specified I2C pending flag
\r
221 (+) @ref __HAL_I2C_ENABLE_IT: Enable the specified I2C interrupt
\r
222 (+) @ref __HAL_I2C_DISABLE_IT: Disable the specified I2C interrupt
\r
224 *** Callback registration ***
\r
225 =============================================
\r
227 The compilation flag USE_HAL_I2C_REGISTER_CALLBACKS when set to 1
\r
228 allows the user to configure dynamically the driver callbacks.
\r
229 Use Functions @ref HAL_I2C_RegisterCallback() or @ref HAL_I2C_RegisterAddrCallback()
\r
230 to register an interrupt callback.
\r
232 Function @ref HAL_I2C_RegisterCallback() allows to register following callbacks:
\r
233 (+) MasterTxCpltCallback : callback for Master transmission end of transfer.
\r
234 (+) MasterRxCpltCallback : callback for Master reception end of transfer.
\r
235 (+) SlaveTxCpltCallback : callback for Slave transmission end of transfer.
\r
236 (+) SlaveRxCpltCallback : callback for Slave reception end of transfer.
\r
237 (+) ListenCpltCallback : callback for end of listen mode.
\r
238 (+) MemTxCpltCallback : callback for Memory transmission end of transfer.
\r
239 (+) MemRxCpltCallback : callback for Memory reception end of transfer.
\r
240 (+) ErrorCallback : callback for error detection.
\r
241 (+) AbortCpltCallback : callback for abort completion process.
\r
242 (+) MspInitCallback : callback for Msp Init.
\r
243 (+) MspDeInitCallback : callback for Msp DeInit.
\r
244 This function takes as parameters the HAL peripheral handle, the Callback ID
\r
245 and a pointer to the user callback function.
\r
247 For specific callback AddrCallback use dedicated register callbacks : @ref HAL_I2C_RegisterAddrCallback().
\r
249 Use function @ref HAL_I2C_UnRegisterCallback to reset a callback to the default
\r
251 @ref HAL_I2C_UnRegisterCallback takes as parameters the HAL peripheral handle,
\r
252 and the Callback ID.
\r
253 This function allows to reset following callbacks:
\r
254 (+) MasterTxCpltCallback : callback for Master transmission end of transfer.
\r
255 (+) MasterRxCpltCallback : callback for Master reception end of transfer.
\r
256 (+) SlaveTxCpltCallback : callback for Slave transmission end of transfer.
\r
257 (+) SlaveRxCpltCallback : callback for Slave reception end of transfer.
\r
258 (+) ListenCpltCallback : callback for end of listen mode.
\r
259 (+) MemTxCpltCallback : callback for Memory transmission end of transfer.
\r
260 (+) MemRxCpltCallback : callback for Memory reception end of transfer.
\r
261 (+) ErrorCallback : callback for error detection.
\r
262 (+) AbortCpltCallback : callback for abort completion process.
\r
263 (+) MspInitCallback : callback for Msp Init.
\r
264 (+) MspDeInitCallback : callback for Msp DeInit.
\r
266 For callback AddrCallback use dedicated register callbacks : @ref HAL_I2C_UnRegisterAddrCallback().
\r
268 By default, after the @ref HAL_I2C_Init() and when the state is @ref HAL_I2C_STATE_RESET
\r
269 all callbacks are set to the corresponding weak functions:
\r
270 examples @ref HAL_I2C_MasterTxCpltCallback(), @ref HAL_I2C_MasterRxCpltCallback().
\r
271 Exception done for MspInit and MspDeInit functions that are
\r
272 reset to the legacy weak functions in the @ref HAL_I2C_Init()/ @ref HAL_I2C_DeInit() only when
\r
273 these callbacks are null (not registered beforehand).
\r
274 If MspInit or MspDeInit are not null, the @ref HAL_I2C_Init()/ @ref HAL_I2C_DeInit()
\r
275 keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
\r
277 Callbacks can be registered/unregistered in @ref HAL_I2C_STATE_READY state only.
\r
278 Exception done MspInit/MspDeInit functions that can be registered/unregistered
\r
279 in @ref HAL_I2C_STATE_READY or @ref HAL_I2C_STATE_RESET state,
\r
280 thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
\r
281 Then, the user first registers the MspInit/MspDeInit user callbacks
\r
282 using @ref HAL_I2C_RegisterCallback() before calling @ref HAL_I2C_DeInit()
\r
283 or @ref HAL_I2C_Init() function.
\r
285 When the compilation flag USE_HAL_I2C_REGISTER_CALLBACKS is set to 0 or
\r
286 not defined, the callback registration feature is not available and all callbacks
\r
287 are set to the corresponding weak functions.
\r
290 (@) You can refer to the I2C HAL driver header file for more useful macros
\r
293 ******************************************************************************
\r
296 * <h2><center>© Copyright (c) 2017 STMicroelectronics.
\r
297 * All rights reserved.</center></h2>
\r
299 * This software component is licensed by ST under BSD 3-Clause license,
\r
300 * the "License"; You may not use this file except in compliance with the
\r
301 * License. You may obtain a copy of the License at:
\r
302 * opensource.org/licenses/BSD-3-Clause
\r
304 ******************************************************************************
\r
307 /* Includes ------------------------------------------------------------------*/
\r
308 #include "stm32l4xx_hal.h"
\r
310 /** @addtogroup STM32L4xx_HAL_Driver
\r
314 /** @defgroup I2C I2C
\r
315 * @brief I2C HAL module driver
\r
319 #ifdef HAL_I2C_MODULE_ENABLED
\r
321 /* Private typedef -----------------------------------------------------------*/
\r
322 /* Private define ------------------------------------------------------------*/
\r
324 /** @defgroup I2C_Private_Define I2C Private Define
\r
327 #define TIMING_CLEAR_MASK (0xF0FFFFFFU) /*!< I2C TIMING clear register Mask */
\r
328 #define I2C_TIMEOUT_ADDR (10000U) /*!< 10 s */
\r
329 #define I2C_TIMEOUT_BUSY (25U) /*!< 25 ms */
\r
330 #define I2C_TIMEOUT_DIR (25U) /*!< 25 ms */
\r
331 #define I2C_TIMEOUT_RXNE (25U) /*!< 25 ms */
\r
332 #define I2C_TIMEOUT_STOPF (25U) /*!< 25 ms */
\r
333 #define I2C_TIMEOUT_TC (25U) /*!< 25 ms */
\r
334 #define I2C_TIMEOUT_TCR (25U) /*!< 25 ms */
\r
335 #define I2C_TIMEOUT_TXIS (25U) /*!< 25 ms */
\r
336 #define I2C_TIMEOUT_FLAG (25U) /*!< 25 ms */
\r
338 #define MAX_NBYTE_SIZE 255U
\r
339 #define SlaveAddr_SHIFT 7U
\r
340 #define SlaveAddr_MSK 0x06U
\r
342 /* Private define for @ref PreviousState usage */
\r
343 #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 */
\r
344 #define I2C_STATE_NONE ((uint32_t)(HAL_I2C_MODE_NONE)) /*!< Default Value */
\r
345 #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 */
\r
346 #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 */
\r
347 #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 */
\r
348 #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 */
\r
349 #define I2C_STATE_MEM_BUSY_TX ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_TX & I2C_STATE_MSK) | (uint32_t)HAL_I2C_MODE_MEM)) /*!< Memory Busy TX, combinaison of State LSB and Mode enum */
\r
350 #define I2C_STATE_MEM_BUSY_RX ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_RX & I2C_STATE_MSK) | (uint32_t)HAL_I2C_MODE_MEM)) /*!< Memory Busy RX, combinaison of State LSB and Mode enum */
\r
353 /* Private define to centralize the enable/disable of Interrupts */
\r
354 #define I2C_XFER_TX_IT (0x00000001U)
\r
355 #define I2C_XFER_RX_IT (0x00000002U)
\r
356 #define I2C_XFER_LISTEN_IT (0x00000004U)
\r
358 #define I2C_XFER_ERROR_IT (0x00000011U)
\r
359 #define I2C_XFER_CPLT_IT (0x00000012U)
\r
360 #define I2C_XFER_RELOAD_IT (0x00000012U)
\r
362 /* Private define Sequential Transfer Options default/reset value */
\r
363 #define I2C_NO_OPTION_FRAME (0xFFFF0000U)
\r
368 /* Private macro -------------------------------------------------------------*/
\r
369 /* Private variables ---------------------------------------------------------*/
\r
370 /* Private function prototypes -----------------------------------------------*/
\r
372 /** @defgroup I2C_Private_Functions I2C Private Functions
\r
375 /* Private functions to handle DMA transfer */
\r
376 static void I2C_DMAMasterTransmitCplt(DMA_HandleTypeDef *hdma);
\r
377 static void I2C_DMAMasterReceiveCplt(DMA_HandleTypeDef *hdma);
\r
378 static void I2C_DMASlaveTransmitCplt(DMA_HandleTypeDef *hdma);
\r
379 static void I2C_DMASlaveReceiveCplt(DMA_HandleTypeDef *hdma);
\r
380 static void I2C_DMAError(DMA_HandleTypeDef *hdma);
\r
381 static void I2C_DMAAbort(DMA_HandleTypeDef *hdma);
\r
383 /* Private functions to handle IT transfer */
\r
384 static void I2C_ITAddrCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags);
\r
385 static void I2C_ITMasterSeqCplt(I2C_HandleTypeDef *hi2c);
\r
386 static void I2C_ITSlaveSeqCplt(I2C_HandleTypeDef *hi2c);
\r
387 static void I2C_ITMasterCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags);
\r
388 static void I2C_ITSlaveCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags);
\r
389 static void I2C_ITListenCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags);
\r
390 static void I2C_ITError(I2C_HandleTypeDef *hi2c, uint32_t ErrorCode);
\r
392 /* Private functions to handle IT transfer */
\r
393 static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart);
\r
394 static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart);
\r
396 /* Private functions for I2C transfer IRQ handler */
\r
397 static HAL_StatusTypeDef I2C_Master_ISR_IT(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags, uint32_t ITSources);
\r
398 static HAL_StatusTypeDef I2C_Slave_ISR_IT(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags, uint32_t ITSources);
\r
399 static HAL_StatusTypeDef I2C_Master_ISR_DMA(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags, uint32_t ITSources);
\r
400 static HAL_StatusTypeDef I2C_Slave_ISR_DMA(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags, uint32_t ITSources);
\r
402 /* Private functions to handle flags during polling transfer */
\r
403 static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout, uint32_t Tickstart);
\r
404 static HAL_StatusTypeDef I2C_WaitOnTXISFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart);
\r
405 static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart);
\r
406 static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart);
\r
407 static HAL_StatusTypeDef I2C_IsAcknowledgeFailed(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart);
\r
409 /* Private functions to centralize the enable/disable of Interrupts */
\r
410 static void I2C_Enable_IRQ(I2C_HandleTypeDef *hi2c, uint16_t InterruptRequest);
\r
411 static void I2C_Disable_IRQ(I2C_HandleTypeDef *hi2c, uint16_t InterruptRequest);
\r
413 /* Private function to flush TXDR register */
\r
414 static void I2C_Flush_TXDR(I2C_HandleTypeDef *hi2c);
\r
416 /* Private function to handle start, restart or stop a transfer */
\r
417 static void I2C_TransferConfig(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t Size, uint32_t Mode, uint32_t Request);
\r
419 /* Private function to Convert Specific options */
\r
420 static void I2C_ConvertOtherXferOptions(I2C_HandleTypeDef *hi2c);
\r
425 /* Exported functions --------------------------------------------------------*/
\r
427 /** @defgroup I2C_Exported_Functions I2C Exported Functions
\r
431 /** @defgroup I2C_Exported_Functions_Group1 Initialization and de-initialization functions
\r
432 * @brief Initialization and Configuration functions
\r
435 ===============================================================================
\r
436 ##### Initialization and de-initialization functions #####
\r
437 ===============================================================================
\r
438 [..] This subsection provides a set of functions allowing to initialize and
\r
439 deinitialize the I2Cx peripheral:
\r
441 (+) User must Implement HAL_I2C_MspInit() function in which he configures
\r
442 all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
\r
444 (+) Call the function HAL_I2C_Init() to configure the selected device with
\r
445 the selected configuration:
\r
448 (++) Addressing mode (Master, Slave)
\r
449 (++) Dual Addressing mode
\r
451 (++) Own Address 2 Mask
\r
452 (++) General call mode
\r
453 (++) Nostretch mode
\r
455 (+) Call the function HAL_I2C_DeInit() to restore the default configuration
\r
456 of the selected I2Cx peripheral.
\r
463 * @brief Initializes the I2C according to the specified parameters
\r
464 * in the I2C_InitTypeDef and initialize the associated handle.
\r
465 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
466 * the configuration information for the specified I2C.
\r
467 * @retval HAL status
\r
469 HAL_StatusTypeDef HAL_I2C_Init(I2C_HandleTypeDef *hi2c)
\r
471 /* Check the I2C handle allocation */
\r
477 /* Check the parameters */
\r
478 assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
\r
479 assert_param(IS_I2C_OWN_ADDRESS1(hi2c->Init.OwnAddress1));
\r
480 assert_param(IS_I2C_ADDRESSING_MODE(hi2c->Init.AddressingMode));
\r
481 assert_param(IS_I2C_DUAL_ADDRESS(hi2c->Init.DualAddressMode));
\r
482 assert_param(IS_I2C_OWN_ADDRESS2(hi2c->Init.OwnAddress2));
\r
483 assert_param(IS_I2C_OWN_ADDRESS2_MASK(hi2c->Init.OwnAddress2Masks));
\r
484 assert_param(IS_I2C_GENERAL_CALL(hi2c->Init.GeneralCallMode));
\r
485 assert_param(IS_I2C_NO_STRETCH(hi2c->Init.NoStretchMode));
\r
487 if (hi2c->State == HAL_I2C_STATE_RESET)
\r
489 /* Allocate lock resource and initialize it */
\r
490 hi2c->Lock = HAL_UNLOCKED;
\r
492 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
\r
493 /* Init the I2C Callback settings */
\r
494 hi2c->MasterTxCpltCallback = HAL_I2C_MasterTxCpltCallback; /* Legacy weak MasterTxCpltCallback */
\r
495 hi2c->MasterRxCpltCallback = HAL_I2C_MasterRxCpltCallback; /* Legacy weak MasterRxCpltCallback */
\r
496 hi2c->SlaveTxCpltCallback = HAL_I2C_SlaveTxCpltCallback; /* Legacy weak SlaveTxCpltCallback */
\r
497 hi2c->SlaveRxCpltCallback = HAL_I2C_SlaveRxCpltCallback; /* Legacy weak SlaveRxCpltCallback */
\r
498 hi2c->ListenCpltCallback = HAL_I2C_ListenCpltCallback; /* Legacy weak ListenCpltCallback */
\r
499 hi2c->MemTxCpltCallback = HAL_I2C_MemTxCpltCallback; /* Legacy weak MemTxCpltCallback */
\r
500 hi2c->MemRxCpltCallback = HAL_I2C_MemRxCpltCallback; /* Legacy weak MemRxCpltCallback */
\r
501 hi2c->ErrorCallback = HAL_I2C_ErrorCallback; /* Legacy weak ErrorCallback */
\r
502 hi2c->AbortCpltCallback = HAL_I2C_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
\r
503 hi2c->AddrCallback = HAL_I2C_AddrCallback; /* Legacy weak AddrCallback */
\r
505 if (hi2c->MspInitCallback == NULL)
\r
507 hi2c->MspInitCallback = HAL_I2C_MspInit; /* Legacy weak MspInit */
\r
510 /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
\r
511 hi2c->MspInitCallback(hi2c);
\r
513 /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
\r
514 HAL_I2C_MspInit(hi2c);
\r
515 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
\r
518 hi2c->State = HAL_I2C_STATE_BUSY;
\r
520 /* Disable the selected I2C peripheral */
\r
521 __HAL_I2C_DISABLE(hi2c);
\r
523 /*---------------------------- I2Cx TIMINGR Configuration ------------------*/
\r
524 /* Configure I2Cx: Frequency range */
\r
525 hi2c->Instance->TIMINGR = hi2c->Init.Timing & TIMING_CLEAR_MASK;
\r
527 /*---------------------------- I2Cx OAR1 Configuration ---------------------*/
\r
528 /* Disable Own Address1 before set the Own Address1 configuration */
\r
529 hi2c->Instance->OAR1 &= ~I2C_OAR1_OA1EN;
\r
531 /* Configure I2Cx: Own Address1 and ack own address1 mode */
\r
532 if (hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_7BIT)
\r
534 hi2c->Instance->OAR1 = (I2C_OAR1_OA1EN | hi2c->Init.OwnAddress1);
\r
536 else /* I2C_ADDRESSINGMODE_10BIT */
\r
538 hi2c->Instance->OAR1 = (I2C_OAR1_OA1EN | I2C_OAR1_OA1MODE | hi2c->Init.OwnAddress1);
\r
541 /*---------------------------- I2Cx CR2 Configuration ----------------------*/
\r
542 /* Configure I2Cx: Addressing Master mode */
\r
543 if (hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT)
\r
545 hi2c->Instance->CR2 = (I2C_CR2_ADD10);
\r
547 /* Enable the AUTOEND by default, and enable NACK (should be disable only during Slave process */
\r
548 hi2c->Instance->CR2 |= (I2C_CR2_AUTOEND | I2C_CR2_NACK);
\r
550 /*---------------------------- I2Cx OAR2 Configuration ---------------------*/
\r
551 /* Disable Own Address2 before set the Own Address2 configuration */
\r
552 hi2c->Instance->OAR2 &= ~I2C_DUALADDRESS_ENABLE;
\r
554 /* Configure I2Cx: Dual mode and Own Address2 */
\r
555 hi2c->Instance->OAR2 = (hi2c->Init.DualAddressMode | hi2c->Init.OwnAddress2 | (hi2c->Init.OwnAddress2Masks << 8));
\r
557 /*---------------------------- I2Cx CR1 Configuration ----------------------*/
\r
558 /* Configure I2Cx: Generalcall and NoStretch mode */
\r
559 hi2c->Instance->CR1 = (hi2c->Init.GeneralCallMode | hi2c->Init.NoStretchMode);
\r
561 /* Enable the selected I2C peripheral */
\r
562 __HAL_I2C_ENABLE(hi2c);
\r
564 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
\r
565 hi2c->State = HAL_I2C_STATE_READY;
\r
566 hi2c->PreviousState = I2C_STATE_NONE;
\r
567 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
573 * @brief DeInitialize the I2C peripheral.
\r
574 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
575 * the configuration information for the specified I2C.
\r
576 * @retval HAL status
\r
578 HAL_StatusTypeDef HAL_I2C_DeInit(I2C_HandleTypeDef *hi2c)
\r
580 /* Check the I2C handle allocation */
\r
586 /* Check the parameters */
\r
587 assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
\r
589 hi2c->State = HAL_I2C_STATE_BUSY;
\r
591 /* Disable the I2C Peripheral Clock */
\r
592 __HAL_I2C_DISABLE(hi2c);
\r
594 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
\r
595 if (hi2c->MspDeInitCallback == NULL)
\r
597 hi2c->MspDeInitCallback = HAL_I2C_MspDeInit; /* Legacy weak MspDeInit */
\r
600 /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
\r
601 hi2c->MspDeInitCallback(hi2c);
\r
603 /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
\r
604 HAL_I2C_MspDeInit(hi2c);
\r
605 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
\r
607 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
\r
608 hi2c->State = HAL_I2C_STATE_RESET;
\r
609 hi2c->PreviousState = I2C_STATE_NONE;
\r
610 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
613 __HAL_UNLOCK(hi2c);
\r
619 * @brief Initialize the I2C MSP.
\r
620 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
621 * the configuration information for the specified I2C.
\r
624 __weak void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)
\r
626 /* Prevent unused argument(s) compilation warning */
\r
629 /* NOTE : This function should not be modified, when the callback is needed,
\r
630 the HAL_I2C_MspInit could be implemented in the user file
\r
635 * @brief DeInitialize the I2C MSP.
\r
636 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
637 * the configuration information for the specified I2C.
\r
640 __weak void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c)
\r
642 /* Prevent unused argument(s) compilation warning */
\r
645 /* NOTE : This function should not be modified, when the callback is needed,
\r
646 the HAL_I2C_MspDeInit could be implemented in the user file
\r
650 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
\r
652 * @brief Register a User I2C Callback
\r
653 * To be used instead of the weak predefined callback
\r
654 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
655 * the configuration information for the specified I2C.
\r
656 * @param CallbackID ID of the callback to be registered
\r
657 * This parameter can be one of the following values:
\r
658 * @arg @ref HAL_I2C_MASTER_TX_COMPLETE_CB_ID Master Tx Transfer completed callback ID
\r
659 * @arg @ref HAL_I2C_MASTER_RX_COMPLETE_CB_ID Master Rx Transfer completed callback ID
\r
660 * @arg @ref HAL_I2C_SLAVE_TX_COMPLETE_CB_ID Slave Tx Transfer completed callback ID
\r
661 * @arg @ref HAL_I2C_SLAVE_RX_COMPLETE_CB_ID Slave Rx Transfer completed callback ID
\r
662 * @arg @ref HAL_I2C_LISTEN_COMPLETE_CB_ID Listen Complete callback ID
\r
663 * @arg @ref HAL_I2C_MEM_TX_COMPLETE_CB_ID Memory Tx Transfer callback ID
\r
664 * @arg @ref HAL_I2C_MEM_RX_COMPLETE_CB_ID Memory Rx Transfer completed callback ID
\r
665 * @arg @ref HAL_I2C_ERROR_CB_ID Error callback ID
\r
666 * @arg @ref HAL_I2C_ABORT_CB_ID Abort callback ID
\r
667 * @arg @ref HAL_I2C_MSPINIT_CB_ID MspInit callback ID
\r
668 * @arg @ref HAL_I2C_MSPDEINIT_CB_ID MspDeInit callback ID
\r
669 * @param pCallback pointer to the Callback function
\r
670 * @retval HAL status
\r
672 HAL_StatusTypeDef HAL_I2C_RegisterCallback(I2C_HandleTypeDef *hi2c, HAL_I2C_CallbackIDTypeDef CallbackID, pI2C_CallbackTypeDef pCallback)
\r
674 HAL_StatusTypeDef status = HAL_OK;
\r
676 if (pCallback == NULL)
\r
678 /* Update the error code */
\r
679 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
\r
683 /* Process locked */
\r
686 if (HAL_I2C_STATE_READY == hi2c->State)
\r
688 switch (CallbackID)
\r
690 case HAL_I2C_MASTER_TX_COMPLETE_CB_ID :
\r
691 hi2c->MasterTxCpltCallback = pCallback;
\r
694 case HAL_I2C_MASTER_RX_COMPLETE_CB_ID :
\r
695 hi2c->MasterRxCpltCallback = pCallback;
\r
698 case HAL_I2C_SLAVE_TX_COMPLETE_CB_ID :
\r
699 hi2c->SlaveTxCpltCallback = pCallback;
\r
702 case HAL_I2C_SLAVE_RX_COMPLETE_CB_ID :
\r
703 hi2c->SlaveRxCpltCallback = pCallback;
\r
706 case HAL_I2C_LISTEN_COMPLETE_CB_ID :
\r
707 hi2c->ListenCpltCallback = pCallback;
\r
710 case HAL_I2C_MEM_TX_COMPLETE_CB_ID :
\r
711 hi2c->MemTxCpltCallback = pCallback;
\r
714 case HAL_I2C_MEM_RX_COMPLETE_CB_ID :
\r
715 hi2c->MemRxCpltCallback = pCallback;
\r
718 case HAL_I2C_ERROR_CB_ID :
\r
719 hi2c->ErrorCallback = pCallback;
\r
722 case HAL_I2C_ABORT_CB_ID :
\r
723 hi2c->AbortCpltCallback = pCallback;
\r
726 case HAL_I2C_MSPINIT_CB_ID :
\r
727 hi2c->MspInitCallback = pCallback;
\r
730 case HAL_I2C_MSPDEINIT_CB_ID :
\r
731 hi2c->MspDeInitCallback = pCallback;
\r
735 /* Update the error code */
\r
736 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
\r
738 /* Return error status */
\r
739 status = HAL_ERROR;
\r
743 else if (HAL_I2C_STATE_RESET == hi2c->State)
\r
745 switch (CallbackID)
\r
747 case HAL_I2C_MSPINIT_CB_ID :
\r
748 hi2c->MspInitCallback = pCallback;
\r
751 case HAL_I2C_MSPDEINIT_CB_ID :
\r
752 hi2c->MspDeInitCallback = pCallback;
\r
756 /* Update the error code */
\r
757 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
\r
759 /* Return error status */
\r
760 status = HAL_ERROR;
\r
766 /* Update the error code */
\r
767 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
\r
769 /* Return error status */
\r
770 status = HAL_ERROR;
\r
774 __HAL_UNLOCK(hi2c);
\r
779 * @brief Unregister an I2C Callback
\r
780 * I2C callback is redirected to the weak predefined callback
\r
781 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
782 * the configuration information for the specified I2C.
\r
783 * @param CallbackID ID of the callback to be unregistered
\r
784 * This parameter can be one of the following values:
\r
785 * This parameter can be one of the following values:
\r
786 * @arg @ref HAL_I2C_MASTER_TX_COMPLETE_CB_ID Master Tx Transfer completed callback ID
\r
787 * @arg @ref HAL_I2C_MASTER_RX_COMPLETE_CB_ID Master Rx Transfer completed callback ID
\r
788 * @arg @ref HAL_I2C_SLAVE_TX_COMPLETE_CB_ID Slave Tx Transfer completed callback ID
\r
789 * @arg @ref HAL_I2C_SLAVE_RX_COMPLETE_CB_ID Slave Rx Transfer completed callback ID
\r
790 * @arg @ref HAL_I2C_LISTEN_COMPLETE_CB_ID Listen Complete callback ID
\r
791 * @arg @ref HAL_I2C_MEM_TX_COMPLETE_CB_ID Memory Tx Transfer callback ID
\r
792 * @arg @ref HAL_I2C_MEM_RX_COMPLETE_CB_ID Memory Rx Transfer completed callback ID
\r
793 * @arg @ref HAL_I2C_ERROR_CB_ID Error callback ID
\r
794 * @arg @ref HAL_I2C_ABORT_CB_ID Abort callback ID
\r
795 * @arg @ref HAL_I2C_MSPINIT_CB_ID MspInit callback ID
\r
796 * @arg @ref HAL_I2C_MSPDEINIT_CB_ID MspDeInit callback ID
\r
797 * @retval HAL status
\r
799 HAL_StatusTypeDef HAL_I2C_UnRegisterCallback(I2C_HandleTypeDef *hi2c, HAL_I2C_CallbackIDTypeDef CallbackID)
\r
801 HAL_StatusTypeDef status = HAL_OK;
\r
803 /* Process locked */
\r
806 if (HAL_I2C_STATE_READY == hi2c->State)
\r
808 switch (CallbackID)
\r
810 case HAL_I2C_MASTER_TX_COMPLETE_CB_ID :
\r
811 hi2c->MasterTxCpltCallback = HAL_I2C_MasterTxCpltCallback; /* Legacy weak MasterTxCpltCallback */
\r
814 case HAL_I2C_MASTER_RX_COMPLETE_CB_ID :
\r
815 hi2c->MasterRxCpltCallback = HAL_I2C_MasterRxCpltCallback; /* Legacy weak MasterRxCpltCallback */
\r
818 case HAL_I2C_SLAVE_TX_COMPLETE_CB_ID :
\r
819 hi2c->SlaveTxCpltCallback = HAL_I2C_SlaveTxCpltCallback; /* Legacy weak SlaveTxCpltCallback */
\r
822 case HAL_I2C_SLAVE_RX_COMPLETE_CB_ID :
\r
823 hi2c->SlaveRxCpltCallback = HAL_I2C_SlaveRxCpltCallback; /* Legacy weak SlaveRxCpltCallback */
\r
826 case HAL_I2C_LISTEN_COMPLETE_CB_ID :
\r
827 hi2c->ListenCpltCallback = HAL_I2C_ListenCpltCallback; /* Legacy weak ListenCpltCallback */
\r
830 case HAL_I2C_MEM_TX_COMPLETE_CB_ID :
\r
831 hi2c->MemTxCpltCallback = HAL_I2C_MemTxCpltCallback; /* Legacy weak MemTxCpltCallback */
\r
834 case HAL_I2C_MEM_RX_COMPLETE_CB_ID :
\r
835 hi2c->MemRxCpltCallback = HAL_I2C_MemRxCpltCallback; /* Legacy weak MemRxCpltCallback */
\r
838 case HAL_I2C_ERROR_CB_ID :
\r
839 hi2c->ErrorCallback = HAL_I2C_ErrorCallback; /* Legacy weak ErrorCallback */
\r
842 case HAL_I2C_ABORT_CB_ID :
\r
843 hi2c->AbortCpltCallback = HAL_I2C_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
\r
846 case HAL_I2C_MSPINIT_CB_ID :
\r
847 hi2c->MspInitCallback = HAL_I2C_MspInit; /* Legacy weak MspInit */
\r
850 case HAL_I2C_MSPDEINIT_CB_ID :
\r
851 hi2c->MspDeInitCallback = HAL_I2C_MspDeInit; /* Legacy weak MspDeInit */
\r
855 /* Update the error code */
\r
856 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
\r
858 /* Return error status */
\r
859 status = HAL_ERROR;
\r
863 else if (HAL_I2C_STATE_RESET == hi2c->State)
\r
865 switch (CallbackID)
\r
867 case HAL_I2C_MSPINIT_CB_ID :
\r
868 hi2c->MspInitCallback = HAL_I2C_MspInit; /* Legacy weak MspInit */
\r
871 case HAL_I2C_MSPDEINIT_CB_ID :
\r
872 hi2c->MspDeInitCallback = HAL_I2C_MspDeInit; /* Legacy weak MspDeInit */
\r
876 /* Update the error code */
\r
877 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
\r
879 /* Return error status */
\r
880 status = HAL_ERROR;
\r
886 /* Update the error code */
\r
887 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
\r
889 /* Return error status */
\r
890 status = HAL_ERROR;
\r
894 __HAL_UNLOCK(hi2c);
\r
899 * @brief Register the Slave Address Match I2C Callback
\r
900 * To be used instead of the weak HAL_I2C_AddrCallback() predefined callback
\r
901 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
902 * the configuration information for the specified I2C.
\r
903 * @param pCallback pointer to the Address Match Callback function
\r
904 * @retval HAL status
\r
906 HAL_StatusTypeDef HAL_I2C_RegisterAddrCallback(I2C_HandleTypeDef *hi2c, pI2C_AddrCallbackTypeDef pCallback)
\r
908 HAL_StatusTypeDef status = HAL_OK;
\r
910 if (pCallback == NULL)
\r
912 /* Update the error code */
\r
913 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
\r
917 /* Process locked */
\r
920 if (HAL_I2C_STATE_READY == hi2c->State)
\r
922 hi2c->AddrCallback = pCallback;
\r
926 /* Update the error code */
\r
927 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
\r
929 /* Return error status */
\r
930 status = HAL_ERROR;
\r
934 __HAL_UNLOCK(hi2c);
\r
939 * @brief UnRegister the Slave Address Match I2C Callback
\r
940 * Info Ready I2C Callback is redirected to the weak HAL_I2C_AddrCallback() predefined callback
\r
941 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
942 * the configuration information for the specified I2C.
\r
943 * @retval HAL status
\r
945 HAL_StatusTypeDef HAL_I2C_UnRegisterAddrCallback(I2C_HandleTypeDef *hi2c)
\r
947 HAL_StatusTypeDef status = HAL_OK;
\r
949 /* Process locked */
\r
952 if (HAL_I2C_STATE_READY == hi2c->State)
\r
954 hi2c->AddrCallback = HAL_I2C_AddrCallback; /* Legacy weak AddrCallback */
\r
958 /* Update the error code */
\r
959 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
\r
961 /* Return error status */
\r
962 status = HAL_ERROR;
\r
966 __HAL_UNLOCK(hi2c);
\r
970 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
\r
976 /** @defgroup I2C_Exported_Functions_Group2 Input and Output operation functions
\r
977 * @brief Data transfers functions
\r
980 ===============================================================================
\r
981 ##### IO operation functions #####
\r
982 ===============================================================================
\r
984 This subsection provides a set of functions allowing to manage the I2C data
\r
987 (#) There are two modes of transfer:
\r
988 (++) Blocking mode : The communication is performed in the polling mode.
\r
989 The status of all data processing is returned by the same function
\r
990 after finishing transfer.
\r
991 (++) No-Blocking mode : The communication is performed using Interrupts
\r
992 or DMA. These functions return the status of the transfer startup.
\r
993 The end of the data processing will be indicated through the
\r
994 dedicated I2C IRQ when using Interrupt mode or the DMA IRQ when
\r
997 (#) Blocking mode functions are :
\r
998 (++) HAL_I2C_Master_Transmit()
\r
999 (++) HAL_I2C_Master_Receive()
\r
1000 (++) HAL_I2C_Slave_Transmit()
\r
1001 (++) HAL_I2C_Slave_Receive()
\r
1002 (++) HAL_I2C_Mem_Write()
\r
1003 (++) HAL_I2C_Mem_Read()
\r
1004 (++) HAL_I2C_IsDeviceReady()
\r
1006 (#) No-Blocking mode functions with Interrupt are :
\r
1007 (++) HAL_I2C_Master_Transmit_IT()
\r
1008 (++) HAL_I2C_Master_Receive_IT()
\r
1009 (++) HAL_I2C_Slave_Transmit_IT()
\r
1010 (++) HAL_I2C_Slave_Receive_IT()
\r
1011 (++) HAL_I2C_Mem_Write_IT()
\r
1012 (++) HAL_I2C_Mem_Read_IT()
\r
1013 (++) HAL_I2C_Master_Seq_Transmit_IT()
\r
1014 (++) HAL_I2C_Master_Seq_Receive_IT()
\r
1015 (++) HAL_I2C_Slave_Seq_Transmit_IT()
\r
1016 (++) HAL_I2C_Slave_Seq_Receive_IT()
\r
1017 (++) HAL_I2C_EnableListen_IT()
\r
1018 (++) HAL_I2C_DisableListen_IT()
\r
1019 (++) HAL_I2C_Master_Abort_IT()
\r
1021 (#) No-Blocking mode functions with DMA are :
\r
1022 (++) HAL_I2C_Master_Transmit_DMA()
\r
1023 (++) HAL_I2C_Master_Receive_DMA()
\r
1024 (++) HAL_I2C_Slave_Transmit_DMA()
\r
1025 (++) HAL_I2C_Slave_Receive_DMA()
\r
1026 (++) HAL_I2C_Mem_Write_DMA()
\r
1027 (++) HAL_I2C_Mem_Read_DMA()
\r
1028 (++) HAL_I2C_Master_Seq_Transmit_DMA()
\r
1029 (++) HAL_I2C_Master_Seq_Receive_DMA()
\r
1030 (++) HAL_I2C_Slave_Seq_Transmit_DMA()
\r
1031 (++) HAL_I2C_Slave_Seq_Receive_DMA()
\r
1033 (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
\r
1034 (++) HAL_I2C_MasterTxCpltCallback()
\r
1035 (++) HAL_I2C_MasterRxCpltCallback()
\r
1036 (++) HAL_I2C_SlaveTxCpltCallback()
\r
1037 (++) HAL_I2C_SlaveRxCpltCallback()
\r
1038 (++) HAL_I2C_MemTxCpltCallback()
\r
1039 (++) HAL_I2C_MemRxCpltCallback()
\r
1040 (++) HAL_I2C_AddrCallback()
\r
1041 (++) HAL_I2C_ListenCpltCallback()
\r
1042 (++) HAL_I2C_ErrorCallback()
\r
1043 (++) HAL_I2C_AbortCpltCallback()
\r
1050 * @brief Transmits in master mode an amount of data in blocking mode.
\r
1051 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
1052 * the configuration information for the specified I2C.
\r
1053 * @param DevAddress Target device address: The device 7 bits address value
\r
1054 * in datasheet must be shifted to the left before calling the interface
\r
1055 * @param pData Pointer to data buffer
\r
1056 * @param Size Amount of data to be sent
\r
1057 * @param Timeout Timeout duration
\r
1058 * @retval HAL status
\r
1060 HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
\r
1062 uint32_t tickstart;
\r
1064 if (hi2c->State == HAL_I2C_STATE_READY)
\r
1066 /* Process Locked */
\r
1069 /* Init tickstart for timeout management*/
\r
1070 tickstart = HAL_GetTick();
\r
1072 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY, tickstart) != HAL_OK)
\r
1077 hi2c->State = HAL_I2C_STATE_BUSY_TX;
\r
1078 hi2c->Mode = HAL_I2C_MODE_MASTER;
\r
1079 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
\r
1081 /* Prepare transfer parameters */
\r
1082 hi2c->pBuffPtr = pData;
\r
1083 hi2c->XferCount = Size;
\r
1084 hi2c->XferISR = NULL;
\r
1086 /* Send Slave Address */
\r
1087 /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
\r
1088 if (hi2c->XferCount > MAX_NBYTE_SIZE)
\r
1090 hi2c->XferSize = MAX_NBYTE_SIZE;
\r
1091 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);
\r
1095 hi2c->XferSize = hi2c->XferCount;
\r
1096 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_WRITE);
\r
1099 while (hi2c->XferCount > 0U)
\r
1101 /* Wait until TXIS flag is set */
\r
1102 if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
\r
1106 /* Write data to TXDR */
\r
1107 hi2c->Instance->TXDR = *hi2c->pBuffPtr;
\r
1109 /* Increment Buffer pointer */
\r
1112 hi2c->XferCount--;
\r
1115 if ((hi2c->XferCount != 0U) && (hi2c->XferSize == 0U))
\r
1117 /* Wait until TCR flag is set */
\r
1118 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout, tickstart) != HAL_OK)
\r
1123 if (hi2c->XferCount > MAX_NBYTE_SIZE)
\r
1125 hi2c->XferSize = MAX_NBYTE_SIZE;
\r
1126 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
\r
1130 hi2c->XferSize = hi2c->XferCount;
\r
1131 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
\r
1136 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
\r
1137 /* Wait until STOPF flag is set */
\r
1138 if (I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
\r
1143 /* Clear STOP Flag */
\r
1144 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
\r
1146 /* Clear Configuration Register 2 */
\r
1147 I2C_RESET_CR2(hi2c);
\r
1149 hi2c->State = HAL_I2C_STATE_READY;
\r
1150 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
1152 /* Process Unlocked */
\r
1153 __HAL_UNLOCK(hi2c);
\r
1164 * @brief Receives in master mode an amount of data in blocking mode.
\r
1165 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
1166 * the configuration information for the specified I2C.
\r
1167 * @param DevAddress Target device address: The device 7 bits address value
\r
1168 * in datasheet must be shifted to the left before calling the interface
\r
1169 * @param pData Pointer to data buffer
\r
1170 * @param Size Amount of data to be sent
\r
1171 * @param Timeout Timeout duration
\r
1172 * @retval HAL status
\r
1174 HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
\r
1176 uint32_t tickstart;
\r
1178 if (hi2c->State == HAL_I2C_STATE_READY)
\r
1180 /* Process Locked */
\r
1183 /* Init tickstart for timeout management*/
\r
1184 tickstart = HAL_GetTick();
\r
1186 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY, tickstart) != HAL_OK)
\r
1191 hi2c->State = HAL_I2C_STATE_BUSY_RX;
\r
1192 hi2c->Mode = HAL_I2C_MODE_MASTER;
\r
1193 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
\r
1195 /* Prepare transfer parameters */
\r
1196 hi2c->pBuffPtr = pData;
\r
1197 hi2c->XferCount = Size;
\r
1198 hi2c->XferISR = NULL;
\r
1200 /* Send Slave Address */
\r
1201 /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
\r
1202 if (hi2c->XferCount > MAX_NBYTE_SIZE)
\r
1204 hi2c->XferSize = MAX_NBYTE_SIZE;
\r
1205 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
\r
1209 hi2c->XferSize = hi2c->XferCount;
\r
1210 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
\r
1213 while (hi2c->XferCount > 0U)
\r
1215 /* Wait until RXNE flag is set */
\r
1216 if (I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
\r
1221 /* Read data from RXDR */
\r
1222 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR;
\r
1224 /* Increment Buffer pointer */
\r
1228 hi2c->XferCount--;
\r
1230 if ((hi2c->XferCount != 0U) && (hi2c->XferSize == 0U))
\r
1232 /* Wait until TCR flag is set */
\r
1233 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout, tickstart) != HAL_OK)
\r
1238 if (hi2c->XferCount > MAX_NBYTE_SIZE)
\r
1240 hi2c->XferSize = MAX_NBYTE_SIZE;
\r
1241 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
\r
1245 hi2c->XferSize = hi2c->XferCount;
\r
1246 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
\r
1251 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
\r
1252 /* Wait until STOPF flag is set */
\r
1253 if (I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
\r
1258 /* Clear STOP Flag */
\r
1259 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
\r
1261 /* Clear Configuration Register 2 */
\r
1262 I2C_RESET_CR2(hi2c);
\r
1264 hi2c->State = HAL_I2C_STATE_READY;
\r
1265 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
1267 /* Process Unlocked */
\r
1268 __HAL_UNLOCK(hi2c);
\r
1279 * @brief Transmits in slave mode an amount of data in blocking mode.
\r
1280 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
1281 * the configuration information for the specified I2C.
\r
1282 * @param pData Pointer to data buffer
\r
1283 * @param Size Amount of data to be sent
\r
1284 * @param Timeout Timeout duration
\r
1285 * @retval HAL status
\r
1287 HAL_StatusTypeDef HAL_I2C_Slave_Transmit(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
\r
1289 uint32_t tickstart;
\r
1291 if (hi2c->State == HAL_I2C_STATE_READY)
\r
1293 if ((pData == NULL) || (Size == 0U))
\r
1295 hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
\r
1298 /* Process Locked */
\r
1301 /* Init tickstart for timeout management*/
\r
1302 tickstart = HAL_GetTick();
\r
1304 hi2c->State = HAL_I2C_STATE_BUSY_TX;
\r
1305 hi2c->Mode = HAL_I2C_MODE_SLAVE;
\r
1306 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
\r
1308 /* Prepare transfer parameters */
\r
1309 hi2c->pBuffPtr = pData;
\r
1310 hi2c->XferCount = Size;
\r
1311 hi2c->XferISR = NULL;
\r
1313 /* Enable Address Acknowledge */
\r
1314 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
\r
1316 /* Wait until ADDR flag is set */
\r
1317 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK)
\r
1319 /* Disable Address Acknowledge */
\r
1320 hi2c->Instance->CR2 |= I2C_CR2_NACK;
\r
1324 /* Clear ADDR flag */
\r
1325 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
\r
1327 /* If 10bit addressing mode is selected */
\r
1328 if (hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT)
\r
1330 /* Wait until ADDR flag is set */
\r
1331 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK)
\r
1333 /* Disable Address Acknowledge */
\r
1334 hi2c->Instance->CR2 |= I2C_CR2_NACK;
\r
1338 /* Clear ADDR flag */
\r
1339 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
\r
1342 /* Wait until DIR flag is set Transmitter mode */
\r
1343 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_DIR, RESET, Timeout, tickstart) != HAL_OK)
\r
1345 /* Disable Address Acknowledge */
\r
1346 hi2c->Instance->CR2 |= I2C_CR2_NACK;
\r
1350 while (hi2c->XferCount > 0U)
\r
1352 /* Wait until TXIS flag is set */
\r
1353 if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
\r
1355 /* Disable Address Acknowledge */
\r
1356 hi2c->Instance->CR2 |= I2C_CR2_NACK;
\r
1360 /* Write data to TXDR */
\r
1361 hi2c->Instance->TXDR = *hi2c->pBuffPtr;
\r
1363 /* Increment Buffer pointer */
\r
1366 hi2c->XferCount--;
\r
1369 /* Wait until STOP flag is set */
\r
1370 if (I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
\r
1372 /* Disable Address Acknowledge */
\r
1373 hi2c->Instance->CR2 |= I2C_CR2_NACK;
\r
1375 if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
\r
1377 /* Normal use case for Transmitter mode */
\r
1378 /* A NACK is generated to confirm the end of transfer */
\r
1379 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
\r
1387 /* Clear STOP flag */
\r
1388 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
\r
1390 /* Wait until BUSY flag is reset */
\r
1391 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout, tickstart) != HAL_OK)
\r
1393 /* Disable Address Acknowledge */
\r
1394 hi2c->Instance->CR2 |= I2C_CR2_NACK;
\r
1398 /* Disable Address Acknowledge */
\r
1399 hi2c->Instance->CR2 |= I2C_CR2_NACK;
\r
1401 hi2c->State = HAL_I2C_STATE_READY;
\r
1402 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
1404 /* Process Unlocked */
\r
1405 __HAL_UNLOCK(hi2c);
\r
1416 * @brief Receive in slave mode an amount of data in blocking mode
\r
1417 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
1418 * the configuration information for the specified I2C.
\r
1419 * @param pData Pointer to data buffer
\r
1420 * @param Size Amount of data to be sent
\r
1421 * @param Timeout Timeout duration
\r
1422 * @retval HAL status
\r
1424 HAL_StatusTypeDef HAL_I2C_Slave_Receive(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
\r
1426 uint32_t tickstart;
\r
1428 if (hi2c->State == HAL_I2C_STATE_READY)
\r
1430 if ((pData == NULL) || (Size == 0U))
\r
1432 hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
\r
1435 /* Process Locked */
\r
1438 /* Init tickstart for timeout management*/
\r
1439 tickstart = HAL_GetTick();
\r
1441 hi2c->State = HAL_I2C_STATE_BUSY_RX;
\r
1442 hi2c->Mode = HAL_I2C_MODE_SLAVE;
\r
1443 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
\r
1445 /* Prepare transfer parameters */
\r
1446 hi2c->pBuffPtr = pData;
\r
1447 hi2c->XferCount = Size;
\r
1448 hi2c->XferISR = NULL;
\r
1450 /* Enable Address Acknowledge */
\r
1451 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
\r
1453 /* Wait until ADDR flag is set */
\r
1454 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK)
\r
1456 /* Disable Address Acknowledge */
\r
1457 hi2c->Instance->CR2 |= I2C_CR2_NACK;
\r
1461 /* Clear ADDR flag */
\r
1462 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
\r
1464 /* Wait until DIR flag is reset Receiver mode */
\r
1465 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_DIR, SET, Timeout, tickstart) != HAL_OK)
\r
1467 /* Disable Address Acknowledge */
\r
1468 hi2c->Instance->CR2 |= I2C_CR2_NACK;
\r
1472 while (hi2c->XferCount > 0U)
\r
1474 /* Wait until RXNE flag is set */
\r
1475 if (I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
\r
1477 /* Disable Address Acknowledge */
\r
1478 hi2c->Instance->CR2 |= I2C_CR2_NACK;
\r
1480 /* Store Last receive data if any */
\r
1481 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
\r
1483 /* Read data from RXDR */
\r
1484 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR;
\r
1486 /* Increment Buffer pointer */
\r
1489 hi2c->XferCount--;
\r
1495 /* Read data from RXDR */
\r
1496 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR;
\r
1498 /* Increment Buffer pointer */
\r
1501 hi2c->XferCount--;
\r
1504 /* Wait until STOP flag is set */
\r
1505 if (I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
\r
1507 /* Disable Address Acknowledge */
\r
1508 hi2c->Instance->CR2 |= I2C_CR2_NACK;
\r
1512 /* Clear STOP flag */
\r
1513 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
\r
1515 /* Wait until BUSY flag is reset */
\r
1516 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout, tickstart) != HAL_OK)
\r
1518 /* Disable Address Acknowledge */
\r
1519 hi2c->Instance->CR2 |= I2C_CR2_NACK;
\r
1523 /* Disable Address Acknowledge */
\r
1524 hi2c->Instance->CR2 |= I2C_CR2_NACK;
\r
1526 hi2c->State = HAL_I2C_STATE_READY;
\r
1527 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
1529 /* Process Unlocked */
\r
1530 __HAL_UNLOCK(hi2c);
\r
1541 * @brief Transmit in master mode an amount of data in non-blocking mode with Interrupt
\r
1542 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
1543 * the configuration information for the specified I2C.
\r
1544 * @param DevAddress Target device address: The device 7 bits address value
\r
1545 * in datasheet must be shifted to the left before calling the interface
\r
1546 * @param pData Pointer to data buffer
\r
1547 * @param Size Amount of data to be sent
\r
1548 * @retval HAL status
\r
1550 HAL_StatusTypeDef HAL_I2C_Master_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
\r
1552 uint32_t xfermode;
\r
1554 if (hi2c->State == HAL_I2C_STATE_READY)
\r
1556 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
\r
1561 /* Process Locked */
\r
1564 hi2c->State = HAL_I2C_STATE_BUSY_TX;
\r
1565 hi2c->Mode = HAL_I2C_MODE_MASTER;
\r
1566 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
\r
1568 /* Prepare transfer parameters */
\r
1569 hi2c->pBuffPtr = pData;
\r
1570 hi2c->XferCount = Size;
\r
1571 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
\r
1572 hi2c->XferISR = I2C_Master_ISR_IT;
\r
1574 if (hi2c->XferCount > MAX_NBYTE_SIZE)
\r
1576 hi2c->XferSize = MAX_NBYTE_SIZE;
\r
1577 xfermode = I2C_RELOAD_MODE;
\r
1581 hi2c->XferSize = hi2c->XferCount;
\r
1582 xfermode = I2C_AUTOEND_MODE;
\r
1585 /* Send Slave Address */
\r
1586 /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE */
\r
1587 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, I2C_GENERATE_START_WRITE);
\r
1589 /* Process Unlocked */
\r
1590 __HAL_UNLOCK(hi2c);
\r
1592 /* Note : The I2C interrupts must be enabled after unlocking current process
\r
1593 to avoid the risk of I2C interrupt handle execution before current
\r
1596 /* Enable ERR, TC, STOP, NACK, TXI interrupt */
\r
1597 /* possible to enable all of these */
\r
1598 /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
\r
1599 I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT);
\r
1610 * @brief Receive in master mode an amount of data in non-blocking mode with Interrupt
\r
1611 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
1612 * the configuration information for the specified I2C.
\r
1613 * @param DevAddress Target device address: The device 7 bits address value
\r
1614 * in datasheet must be shifted to the left before calling the interface
\r
1615 * @param pData Pointer to data buffer
\r
1616 * @param Size Amount of data to be sent
\r
1617 * @retval HAL status
\r
1619 HAL_StatusTypeDef HAL_I2C_Master_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
\r
1621 uint32_t xfermode;
\r
1623 if (hi2c->State == HAL_I2C_STATE_READY)
\r
1625 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
\r
1630 /* Process Locked */
\r
1633 hi2c->State = HAL_I2C_STATE_BUSY_RX;
\r
1634 hi2c->Mode = HAL_I2C_MODE_MASTER;
\r
1635 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
\r
1637 /* Prepare transfer parameters */
\r
1638 hi2c->pBuffPtr = pData;
\r
1639 hi2c->XferCount = Size;
\r
1640 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
\r
1641 hi2c->XferISR = I2C_Master_ISR_IT;
\r
1643 if (hi2c->XferCount > MAX_NBYTE_SIZE)
\r
1645 hi2c->XferSize = MAX_NBYTE_SIZE;
\r
1646 xfermode = I2C_RELOAD_MODE;
\r
1650 hi2c->XferSize = hi2c->XferCount;
\r
1651 xfermode = I2C_AUTOEND_MODE;
\r
1654 /* Send Slave Address */
\r
1655 /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE */
\r
1656 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, I2C_GENERATE_START_READ);
\r
1658 /* Process Unlocked */
\r
1659 __HAL_UNLOCK(hi2c);
\r
1661 /* Note : The I2C interrupts must be enabled after unlocking current process
\r
1662 to avoid the risk of I2C interrupt handle execution before current
\r
1665 /* Enable ERR, TC, STOP, NACK, RXI interrupt */
\r
1666 /* possible to enable all of these */
\r
1667 /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
\r
1668 I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT);
\r
1679 * @brief Transmit in slave mode an amount of data in non-blocking mode with Interrupt
\r
1680 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
1681 * the configuration information for the specified I2C.
\r
1682 * @param pData Pointer to data buffer
\r
1683 * @param Size Amount of data to be sent
\r
1684 * @retval HAL status
\r
1686 HAL_StatusTypeDef HAL_I2C_Slave_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
\r
1688 if (hi2c->State == HAL_I2C_STATE_READY)
\r
1690 /* Process Locked */
\r
1693 hi2c->State = HAL_I2C_STATE_BUSY_TX;
\r
1694 hi2c->Mode = HAL_I2C_MODE_SLAVE;
\r
1695 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
\r
1697 /* Enable Address Acknowledge */
\r
1698 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
\r
1700 /* Prepare transfer parameters */
\r
1701 hi2c->pBuffPtr = pData;
\r
1702 hi2c->XferCount = Size;
\r
1703 hi2c->XferSize = hi2c->XferCount;
\r
1704 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
\r
1705 hi2c->XferISR = I2C_Slave_ISR_IT;
\r
1707 /* Process Unlocked */
\r
1708 __HAL_UNLOCK(hi2c);
\r
1710 /* Note : The I2C interrupts must be enabled after unlocking current process
\r
1711 to avoid the risk of I2C interrupt handle execution before current
\r
1714 /* Enable ERR, TC, STOP, NACK, TXI interrupt */
\r
1715 /* possible to enable all of these */
\r
1716 /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
\r
1717 I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT | I2C_XFER_LISTEN_IT);
\r
1728 * @brief Receive in slave mode an amount of data in non-blocking mode with Interrupt
\r
1729 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
1730 * the configuration information for the specified I2C.
\r
1731 * @param pData Pointer to data buffer
\r
1732 * @param Size Amount of data to be sent
\r
1733 * @retval HAL status
\r
1735 HAL_StatusTypeDef HAL_I2C_Slave_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
\r
1737 if (hi2c->State == HAL_I2C_STATE_READY)
\r
1739 /* Process Locked */
\r
1742 hi2c->State = HAL_I2C_STATE_BUSY_RX;
\r
1743 hi2c->Mode = HAL_I2C_MODE_SLAVE;
\r
1744 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
\r
1746 /* Enable Address Acknowledge */
\r
1747 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
\r
1749 /* Prepare transfer parameters */
\r
1750 hi2c->pBuffPtr = pData;
\r
1751 hi2c->XferCount = Size;
\r
1752 hi2c->XferSize = hi2c->XferCount;
\r
1753 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
\r
1754 hi2c->XferISR = I2C_Slave_ISR_IT;
\r
1756 /* Process Unlocked */
\r
1757 __HAL_UNLOCK(hi2c);
\r
1759 /* Note : The I2C interrupts must be enabled after unlocking current process
\r
1760 to avoid the risk of I2C interrupt handle execution before current
\r
1763 /* Enable ERR, TC, STOP, NACK, RXI interrupt */
\r
1764 /* possible to enable all of these */
\r
1765 /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
\r
1766 I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT | I2C_XFER_LISTEN_IT);
\r
1777 * @brief Transmit in master mode an amount of data in non-blocking mode with DMA
\r
1778 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
1779 * the configuration information for the specified I2C.
\r
1780 * @param DevAddress Target device address: The device 7 bits address value
\r
1781 * in datasheet must be shifted to the left before calling the interface
\r
1782 * @param pData Pointer to data buffer
\r
1783 * @param Size Amount of data to be sent
\r
1784 * @retval HAL status
\r
1786 HAL_StatusTypeDef HAL_I2C_Master_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
\r
1788 uint32_t xfermode;
\r
1789 HAL_StatusTypeDef dmaxferstatus;
\r
1791 if (hi2c->State == HAL_I2C_STATE_READY)
\r
1793 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
\r
1798 /* Process Locked */
\r
1801 hi2c->State = HAL_I2C_STATE_BUSY_TX;
\r
1802 hi2c->Mode = HAL_I2C_MODE_MASTER;
\r
1803 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
\r
1805 /* Prepare transfer parameters */
\r
1806 hi2c->pBuffPtr = pData;
\r
1807 hi2c->XferCount = Size;
\r
1808 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
\r
1809 hi2c->XferISR = I2C_Master_ISR_DMA;
\r
1811 if (hi2c->XferCount > MAX_NBYTE_SIZE)
\r
1813 hi2c->XferSize = MAX_NBYTE_SIZE;
\r
1814 xfermode = I2C_RELOAD_MODE;
\r
1818 hi2c->XferSize = hi2c->XferCount;
\r
1819 xfermode = I2C_AUTOEND_MODE;
\r
1822 if (hi2c->XferSize > 0U)
\r
1824 if (hi2c->hdmatx != NULL)
\r
1826 /* Set the I2C DMA transfer complete callback */
\r
1827 hi2c->hdmatx->XferCpltCallback = I2C_DMAMasterTransmitCplt;
\r
1829 /* Set the DMA error callback */
\r
1830 hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
\r
1832 /* Set the unused DMA callbacks to NULL */
\r
1833 hi2c->hdmatx->XferHalfCpltCallback = NULL;
\r
1834 hi2c->hdmatx->XferAbortCallback = NULL;
\r
1836 /* Enable the DMA channel */
\r
1837 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
\r
1841 /* Update I2C state */
\r
1842 hi2c->State = HAL_I2C_STATE_READY;
\r
1843 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
1845 /* Update I2C error code */
\r
1846 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
\r
1848 /* Process Unlocked */
\r
1849 __HAL_UNLOCK(hi2c);
\r
1854 if (dmaxferstatus == HAL_OK)
\r
1856 /* Send Slave Address */
\r
1857 /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
\r
1858 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, I2C_GENERATE_START_WRITE);
\r
1860 /* Update XferCount value */
\r
1861 hi2c->XferCount -= hi2c->XferSize;
\r
1863 /* Process Unlocked */
\r
1864 __HAL_UNLOCK(hi2c);
\r
1866 /* Note : The I2C interrupts must be enabled after unlocking current process
\r
1867 to avoid the risk of I2C interrupt handle execution before current
\r
1869 /* Enable ERR and NACK interrupts */
\r
1870 I2C_Enable_IRQ(hi2c, I2C_XFER_ERROR_IT);
\r
1872 /* Enable DMA Request */
\r
1873 hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
\r
1877 /* Update I2C state */
\r
1878 hi2c->State = HAL_I2C_STATE_READY;
\r
1879 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
1881 /* Update I2C error code */
\r
1882 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
\r
1884 /* Process Unlocked */
\r
1885 __HAL_UNLOCK(hi2c);
\r
1892 /* Update Transfer ISR function pointer */
\r
1893 hi2c->XferISR = I2C_Master_ISR_IT;
\r
1895 /* Send Slave Address */
\r
1896 /* Set NBYTES to write and generate START condition */
\r
1897 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_WRITE);
\r
1899 /* Process Unlocked */
\r
1900 __HAL_UNLOCK(hi2c);
\r
1902 /* Note : The I2C interrupts must be enabled after unlocking current process
\r
1903 to avoid the risk of I2C interrupt handle execution before current
\r
1905 /* Enable ERR, TC, STOP, NACK, TXI interrupt */
\r
1906 /* possible to enable all of these */
\r
1907 /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
\r
1908 I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT);
\r
1920 * @brief Receive in master mode an amount of data in non-blocking mode with DMA
\r
1921 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
1922 * the configuration information for the specified I2C.
\r
1923 * @param DevAddress Target device address: The device 7 bits address value
\r
1924 * in datasheet must be shifted to the left before calling the interface
\r
1925 * @param pData Pointer to data buffer
\r
1926 * @param Size Amount of data to be sent
\r
1927 * @retval HAL status
\r
1929 HAL_StatusTypeDef HAL_I2C_Master_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
\r
1931 uint32_t xfermode;
\r
1932 HAL_StatusTypeDef dmaxferstatus;
\r
1934 if (hi2c->State == HAL_I2C_STATE_READY)
\r
1936 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
\r
1941 /* Process Locked */
\r
1944 hi2c->State = HAL_I2C_STATE_BUSY_RX;
\r
1945 hi2c->Mode = HAL_I2C_MODE_MASTER;
\r
1946 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
\r
1948 /* Prepare transfer parameters */
\r
1949 hi2c->pBuffPtr = pData;
\r
1950 hi2c->XferCount = Size;
\r
1951 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
\r
1952 hi2c->XferISR = I2C_Master_ISR_DMA;
\r
1954 if (hi2c->XferCount > MAX_NBYTE_SIZE)
\r
1956 hi2c->XferSize = MAX_NBYTE_SIZE;
\r
1957 xfermode = I2C_RELOAD_MODE;
\r
1961 hi2c->XferSize = hi2c->XferCount;
\r
1962 xfermode = I2C_AUTOEND_MODE;
\r
1965 if (hi2c->XferSize > 0U)
\r
1967 if (hi2c->hdmarx != NULL)
\r
1969 /* Set the I2C DMA transfer complete callback */
\r
1970 hi2c->hdmarx->XferCpltCallback = I2C_DMAMasterReceiveCplt;
\r
1972 /* Set the DMA error callback */
\r
1973 hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
\r
1975 /* Set the unused DMA callbacks to NULL */
\r
1976 hi2c->hdmarx->XferHalfCpltCallback = NULL;
\r
1977 hi2c->hdmarx->XferAbortCallback = NULL;
\r
1979 /* Enable the DMA channel */
\r
1980 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData, hi2c->XferSize);
\r
1984 /* Update I2C state */
\r
1985 hi2c->State = HAL_I2C_STATE_READY;
\r
1986 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
1988 /* Update I2C error code */
\r
1989 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
\r
1991 /* Process Unlocked */
\r
1992 __HAL_UNLOCK(hi2c);
\r
1997 if (dmaxferstatus == HAL_OK)
\r
1999 /* Send Slave Address */
\r
2000 /* Set NBYTES to read and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
\r
2001 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, I2C_GENERATE_START_READ);
\r
2003 /* Update XferCount value */
\r
2004 hi2c->XferCount -= hi2c->XferSize;
\r
2006 /* Process Unlocked */
\r
2007 __HAL_UNLOCK(hi2c);
\r
2009 /* Note : The I2C interrupts must be enabled after unlocking current process
\r
2010 to avoid the risk of I2C interrupt handle execution before current
\r
2012 /* Enable ERR and NACK interrupts */
\r
2013 I2C_Enable_IRQ(hi2c, I2C_XFER_ERROR_IT);
\r
2015 /* Enable DMA Request */
\r
2016 hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
\r
2020 /* Update I2C state */
\r
2021 hi2c->State = HAL_I2C_STATE_READY;
\r
2022 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
2024 /* Update I2C error code */
\r
2025 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
\r
2027 /* Process Unlocked */
\r
2028 __HAL_UNLOCK(hi2c);
\r
2035 /* Update Transfer ISR function pointer */
\r
2036 hi2c->XferISR = I2C_Master_ISR_IT;
\r
2038 /* Send Slave Address */
\r
2039 /* Set NBYTES to read and generate START condition */
\r
2040 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
\r
2042 /* Process Unlocked */
\r
2043 __HAL_UNLOCK(hi2c);
\r
2045 /* Note : The I2C interrupts must be enabled after unlocking current process
\r
2046 to avoid the risk of I2C interrupt handle execution before current
\r
2048 /* Enable ERR, TC, STOP, NACK, TXI interrupt */
\r
2049 /* possible to enable all of these */
\r
2050 /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
\r
2051 I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT);
\r
2063 * @brief Transmit in slave mode an amount of data in non-blocking mode with DMA
\r
2064 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
2065 * the configuration information for the specified I2C.
\r
2066 * @param pData Pointer to data buffer
\r
2067 * @param Size Amount of data to be sent
\r
2068 * @retval HAL status
\r
2070 HAL_StatusTypeDef HAL_I2C_Slave_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
\r
2072 HAL_StatusTypeDef dmaxferstatus;
\r
2074 if (hi2c->State == HAL_I2C_STATE_READY)
\r
2076 if ((pData == NULL) || (Size == 0U))
\r
2078 hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
\r
2081 /* Process Locked */
\r
2084 hi2c->State = HAL_I2C_STATE_BUSY_TX;
\r
2085 hi2c->Mode = HAL_I2C_MODE_SLAVE;
\r
2086 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
\r
2088 /* Prepare transfer parameters */
\r
2089 hi2c->pBuffPtr = pData;
\r
2090 hi2c->XferCount = Size;
\r
2091 hi2c->XferSize = hi2c->XferCount;
\r
2092 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
\r
2093 hi2c->XferISR = I2C_Slave_ISR_DMA;
\r
2095 if (hi2c->hdmatx != NULL)
\r
2097 /* Set the I2C DMA transfer complete callback */
\r
2098 hi2c->hdmatx->XferCpltCallback = I2C_DMASlaveTransmitCplt;
\r
2100 /* Set the DMA error callback */
\r
2101 hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
\r
2103 /* Set the unused DMA callbacks to NULL */
\r
2104 hi2c->hdmatx->XferHalfCpltCallback = NULL;
\r
2105 hi2c->hdmatx->XferAbortCallback = NULL;
\r
2107 /* Enable the DMA channel */
\r
2108 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
\r
2112 /* Update I2C state */
\r
2113 hi2c->State = HAL_I2C_STATE_LISTEN;
\r
2114 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
2116 /* Update I2C error code */
\r
2117 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
\r
2119 /* Process Unlocked */
\r
2120 __HAL_UNLOCK(hi2c);
\r
2125 if (dmaxferstatus == HAL_OK)
\r
2127 /* Enable Address Acknowledge */
\r
2128 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
\r
2130 /* Process Unlocked */
\r
2131 __HAL_UNLOCK(hi2c);
\r
2133 /* Note : The I2C interrupts must be enabled after unlocking current process
\r
2134 to avoid the risk of I2C interrupt handle execution before current
\r
2136 /* Enable ERR, STOP, NACK, ADDR interrupts */
\r
2137 I2C_Enable_IRQ(hi2c, I2C_XFER_LISTEN_IT);
\r
2139 /* Enable DMA Request */
\r
2140 hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
\r
2144 /* Update I2C state */
\r
2145 hi2c->State = HAL_I2C_STATE_LISTEN;
\r
2146 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
2148 /* Update I2C error code */
\r
2149 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
\r
2151 /* Process Unlocked */
\r
2152 __HAL_UNLOCK(hi2c);
\r
2166 * @brief Receive in slave mode an amount of data in non-blocking mode with DMA
\r
2167 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
2168 * the configuration information for the specified I2C.
\r
2169 * @param pData Pointer to data buffer
\r
2170 * @param Size Amount of data to be sent
\r
2171 * @retval HAL status
\r
2173 HAL_StatusTypeDef HAL_I2C_Slave_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
\r
2175 HAL_StatusTypeDef dmaxferstatus;
\r
2177 if (hi2c->State == HAL_I2C_STATE_READY)
\r
2179 if ((pData == NULL) || (Size == 0U))
\r
2181 hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
\r
2184 /* Process Locked */
\r
2187 hi2c->State = HAL_I2C_STATE_BUSY_RX;
\r
2188 hi2c->Mode = HAL_I2C_MODE_SLAVE;
\r
2189 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
\r
2191 /* Prepare transfer parameters */
\r
2192 hi2c->pBuffPtr = pData;
\r
2193 hi2c->XferCount = Size;
\r
2194 hi2c->XferSize = hi2c->XferCount;
\r
2195 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
\r
2196 hi2c->XferISR = I2C_Slave_ISR_DMA;
\r
2198 if (hi2c->hdmarx != NULL)
\r
2200 /* Set the I2C DMA transfer complete callback */
\r
2201 hi2c->hdmarx->XferCpltCallback = I2C_DMASlaveReceiveCplt;
\r
2203 /* Set the DMA error callback */
\r
2204 hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
\r
2206 /* Set the unused DMA callbacks to NULL */
\r
2207 hi2c->hdmarx->XferHalfCpltCallback = NULL;
\r
2208 hi2c->hdmarx->XferAbortCallback = NULL;
\r
2210 /* Enable the DMA channel */
\r
2211 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData, hi2c->XferSize);
\r
2215 /* Update I2C state */
\r
2216 hi2c->State = HAL_I2C_STATE_LISTEN;
\r
2217 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
2219 /* Update I2C error code */
\r
2220 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
\r
2222 /* Process Unlocked */
\r
2223 __HAL_UNLOCK(hi2c);
\r
2228 if (dmaxferstatus == HAL_OK)
\r
2230 /* Enable Address Acknowledge */
\r
2231 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
\r
2233 /* Process Unlocked */
\r
2234 __HAL_UNLOCK(hi2c);
\r
2236 /* Note : The I2C interrupts must be enabled after unlocking current process
\r
2237 to avoid the risk of I2C interrupt handle execution before current
\r
2239 /* Enable ERR, STOP, NACK, ADDR interrupts */
\r
2240 I2C_Enable_IRQ(hi2c, I2C_XFER_LISTEN_IT);
\r
2242 /* Enable DMA Request */
\r
2243 hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
\r
2247 /* Update I2C state */
\r
2248 hi2c->State = HAL_I2C_STATE_LISTEN;
\r
2249 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
2251 /* Update I2C error code */
\r
2252 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
\r
2254 /* Process Unlocked */
\r
2255 __HAL_UNLOCK(hi2c);
\r
2268 * @brief Write an amount of data in blocking mode to a specific memory address
\r
2269 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
2270 * the configuration information for the specified I2C.
\r
2271 * @param DevAddress Target device address: The device 7 bits address value
\r
2272 * in datasheet must be shifted to the left before calling the interface
\r
2273 * @param MemAddress Internal memory address
\r
2274 * @param MemAddSize Size of internal memory address
\r
2275 * @param pData Pointer to data buffer
\r
2276 * @param Size Amount of data to be sent
\r
2277 * @param Timeout Timeout duration
\r
2278 * @retval HAL status
\r
2280 HAL_StatusTypeDef HAL_I2C_Mem_Write(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
\r
2282 uint32_t tickstart;
\r
2284 /* Check the parameters */
\r
2285 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
\r
2287 if (hi2c->State == HAL_I2C_STATE_READY)
\r
2289 if ((pData == NULL) || (Size == 0U))
\r
2291 hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
\r
2295 /* Process Locked */
\r
2298 /* Init tickstart for timeout management*/
\r
2299 tickstart = HAL_GetTick();
\r
2301 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY, tickstart) != HAL_OK)
\r
2306 hi2c->State = HAL_I2C_STATE_BUSY_TX;
\r
2307 hi2c->Mode = HAL_I2C_MODE_MEM;
\r
2308 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
\r
2310 /* Prepare transfer parameters */
\r
2311 hi2c->pBuffPtr = pData;
\r
2312 hi2c->XferCount = Size;
\r
2313 hi2c->XferISR = NULL;
\r
2315 /* Send Slave Address and Memory Address */
\r
2316 if (I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, Timeout, tickstart) != HAL_OK)
\r
2318 /* Process Unlocked */
\r
2319 __HAL_UNLOCK(hi2c);
\r
2323 /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE */
\r
2324 if (hi2c->XferCount > MAX_NBYTE_SIZE)
\r
2326 hi2c->XferSize = MAX_NBYTE_SIZE;
\r
2327 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
\r
2331 hi2c->XferSize = hi2c->XferCount;
\r
2332 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
\r
2337 /* Wait until TXIS flag is set */
\r
2338 if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
\r
2343 /* Write data to TXDR */
\r
2344 hi2c->Instance->TXDR = *hi2c->pBuffPtr;
\r
2346 /* Increment Buffer pointer */
\r
2349 hi2c->XferCount--;
\r
2352 if ((hi2c->XferCount != 0U) && (hi2c->XferSize == 0U))
\r
2354 /* Wait until TCR flag is set */
\r
2355 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout, tickstart) != HAL_OK)
\r
2360 if (hi2c->XferCount > MAX_NBYTE_SIZE)
\r
2362 hi2c->XferSize = MAX_NBYTE_SIZE;
\r
2363 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
\r
2367 hi2c->XferSize = hi2c->XferCount;
\r
2368 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
\r
2373 while (hi2c->XferCount > 0U);
\r
2375 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
\r
2376 /* Wait until STOPF flag is reset */
\r
2377 if (I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
\r
2382 /* Clear STOP Flag */
\r
2383 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
\r
2385 /* Clear Configuration Register 2 */
\r
2386 I2C_RESET_CR2(hi2c);
\r
2388 hi2c->State = HAL_I2C_STATE_READY;
\r
2389 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
2391 /* Process Unlocked */
\r
2392 __HAL_UNLOCK(hi2c);
\r
2403 * @brief Read an amount of data in blocking mode from a specific memory address
\r
2404 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
2405 * the configuration information for the specified I2C.
\r
2406 * @param DevAddress Target device address: The device 7 bits address value
\r
2407 * in datasheet must be shifted to the left before calling the interface
\r
2408 * @param MemAddress Internal memory address
\r
2409 * @param MemAddSize Size of internal memory address
\r
2410 * @param pData Pointer to data buffer
\r
2411 * @param Size Amount of data to be sent
\r
2412 * @param Timeout Timeout duration
\r
2413 * @retval HAL status
\r
2415 HAL_StatusTypeDef HAL_I2C_Mem_Read(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
\r
2417 uint32_t tickstart;
\r
2419 /* Check the parameters */
\r
2420 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
\r
2422 if (hi2c->State == HAL_I2C_STATE_READY)
\r
2424 if ((pData == NULL) || (Size == 0U))
\r
2426 hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
\r
2430 /* Process Locked */
\r
2433 /* Init tickstart for timeout management*/
\r
2434 tickstart = HAL_GetTick();
\r
2436 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY, tickstart) != HAL_OK)
\r
2441 hi2c->State = HAL_I2C_STATE_BUSY_RX;
\r
2442 hi2c->Mode = HAL_I2C_MODE_MEM;
\r
2443 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
\r
2445 /* Prepare transfer parameters */
\r
2446 hi2c->pBuffPtr = pData;
\r
2447 hi2c->XferCount = Size;
\r
2448 hi2c->XferISR = NULL;
\r
2450 /* Send Slave Address and Memory Address */
\r
2451 if (I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, Timeout, tickstart) != HAL_OK)
\r
2453 /* Process Unlocked */
\r
2454 __HAL_UNLOCK(hi2c);
\r
2458 /* Send Slave Address */
\r
2459 /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
\r
2460 if (hi2c->XferCount > MAX_NBYTE_SIZE)
\r
2462 hi2c->XferSize = MAX_NBYTE_SIZE;
\r
2463 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
\r
2467 hi2c->XferSize = hi2c->XferCount;
\r
2468 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
\r
2473 /* Wait until RXNE flag is set */
\r
2474 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, Timeout, tickstart) != HAL_OK)
\r
2479 /* Read data from RXDR */
\r
2480 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR;
\r
2482 /* Increment Buffer pointer */
\r
2486 hi2c->XferCount--;
\r
2488 if ((hi2c->XferCount != 0U) && (hi2c->XferSize == 0U))
\r
2490 /* Wait until TCR flag is set */
\r
2491 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout, tickstart) != HAL_OK)
\r
2496 if (hi2c->XferCount > MAX_NBYTE_SIZE)
\r
2498 hi2c->XferSize = MAX_NBYTE_SIZE;
\r
2499 I2C_TransferConfig(hi2c, DevAddress, (uint8_t) hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
\r
2503 hi2c->XferSize = hi2c->XferCount;
\r
2504 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
\r
2508 while (hi2c->XferCount > 0U);
\r
2510 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
\r
2511 /* Wait until STOPF flag is reset */
\r
2512 if (I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
\r
2517 /* Clear STOP Flag */
\r
2518 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
\r
2520 /* Clear Configuration Register 2 */
\r
2521 I2C_RESET_CR2(hi2c);
\r
2523 hi2c->State = HAL_I2C_STATE_READY;
\r
2524 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
2526 /* Process Unlocked */
\r
2527 __HAL_UNLOCK(hi2c);
\r
2537 * @brief Write an amount of data in non-blocking mode with Interrupt to a specific memory address
\r
2538 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
2539 * the configuration information for the specified I2C.
\r
2540 * @param DevAddress Target device address: The device 7 bits address value
\r
2541 * in datasheet must be shifted to the left before calling the interface
\r
2542 * @param MemAddress Internal memory address
\r
2543 * @param MemAddSize Size of internal memory address
\r
2544 * @param pData Pointer to data buffer
\r
2545 * @param Size Amount of data to be sent
\r
2546 * @retval HAL status
\r
2548 HAL_StatusTypeDef HAL_I2C_Mem_Write_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
\r
2550 uint32_t tickstart;
\r
2551 uint32_t xfermode;
\r
2553 /* Check the parameters */
\r
2554 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
\r
2556 if (hi2c->State == HAL_I2C_STATE_READY)
\r
2558 if ((pData == NULL) || (Size == 0U))
\r
2560 hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
\r
2564 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
\r
2569 /* Process Locked */
\r
2572 /* Init tickstart for timeout management*/
\r
2573 tickstart = HAL_GetTick();
\r
2575 hi2c->State = HAL_I2C_STATE_BUSY_TX;
\r
2576 hi2c->Mode = HAL_I2C_MODE_MEM;
\r
2577 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
\r
2579 /* Prepare transfer parameters */
\r
2580 hi2c->pBuffPtr = pData;
\r
2581 hi2c->XferCount = Size;
\r
2582 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
\r
2583 hi2c->XferISR = I2C_Master_ISR_IT;
\r
2585 if (hi2c->XferCount > MAX_NBYTE_SIZE)
\r
2587 hi2c->XferSize = MAX_NBYTE_SIZE;
\r
2588 xfermode = I2C_RELOAD_MODE;
\r
2592 hi2c->XferSize = hi2c->XferCount;
\r
2593 xfermode = I2C_AUTOEND_MODE;
\r
2596 /* Send Slave Address and Memory Address */
\r
2597 if (I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
\r
2599 /* Process Unlocked */
\r
2600 __HAL_UNLOCK(hi2c);
\r
2604 /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
\r
2605 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, I2C_NO_STARTSTOP);
\r
2607 /* Process Unlocked */
\r
2608 __HAL_UNLOCK(hi2c);
\r
2610 /* Note : The I2C interrupts must be enabled after unlocking current process
\r
2611 to avoid the risk of I2C interrupt handle execution before current
\r
2614 /* Enable ERR, TC, STOP, NACK, TXI interrupt */
\r
2615 /* possible to enable all of these */
\r
2616 /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
\r
2617 I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT);
\r
2628 * @brief Read an amount of data in non-blocking mode with Interrupt from a specific memory address
\r
2629 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
2630 * the configuration information for the specified I2C.
\r
2631 * @param DevAddress Target device address: The device 7 bits address value
\r
2632 * in datasheet must be shifted to the left before calling the interface
\r
2633 * @param MemAddress Internal memory address
\r
2634 * @param MemAddSize Size of internal memory address
\r
2635 * @param pData Pointer to data buffer
\r
2636 * @param Size Amount of data to be sent
\r
2637 * @retval HAL status
\r
2639 HAL_StatusTypeDef HAL_I2C_Mem_Read_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
\r
2641 uint32_t tickstart;
\r
2642 uint32_t xfermode;
\r
2644 /* Check the parameters */
\r
2645 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
\r
2647 if (hi2c->State == HAL_I2C_STATE_READY)
\r
2649 if ((pData == NULL) || (Size == 0U))
\r
2651 hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
\r
2655 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
\r
2660 /* Process Locked */
\r
2663 /* Init tickstart for timeout management*/
\r
2664 tickstart = HAL_GetTick();
\r
2666 hi2c->State = HAL_I2C_STATE_BUSY_RX;
\r
2667 hi2c->Mode = HAL_I2C_MODE_MEM;
\r
2668 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
\r
2670 /* Prepare transfer parameters */
\r
2671 hi2c->pBuffPtr = pData;
\r
2672 hi2c->XferCount = Size;
\r
2673 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
\r
2674 hi2c->XferISR = I2C_Master_ISR_IT;
\r
2676 if (hi2c->XferCount > MAX_NBYTE_SIZE)
\r
2678 hi2c->XferSize = MAX_NBYTE_SIZE;
\r
2679 xfermode = I2C_RELOAD_MODE;
\r
2683 hi2c->XferSize = hi2c->XferCount;
\r
2684 xfermode = I2C_AUTOEND_MODE;
\r
2687 /* Send Slave Address and Memory Address */
\r
2688 if (I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
\r
2690 /* Process Unlocked */
\r
2691 __HAL_UNLOCK(hi2c);
\r
2695 /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
\r
2696 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, I2C_GENERATE_START_READ);
\r
2698 /* Process Unlocked */
\r
2699 __HAL_UNLOCK(hi2c);
\r
2701 /* Note : The I2C interrupts must be enabled after unlocking current process
\r
2702 to avoid the risk of I2C interrupt handle execution before current
\r
2705 /* Enable ERR, TC, STOP, NACK, RXI interrupt */
\r
2706 /* possible to enable all of these */
\r
2707 /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
\r
2708 I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT);
\r
2718 * @brief Write an amount of data in non-blocking mode with DMA to a specific memory address
\r
2719 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
2720 * the configuration information for the specified I2C.
\r
2721 * @param DevAddress Target device address: The device 7 bits address value
\r
2722 * in datasheet must be shifted to the left before calling the interface
\r
2723 * @param MemAddress Internal memory address
\r
2724 * @param MemAddSize Size of internal memory address
\r
2725 * @param pData Pointer to data buffer
\r
2726 * @param Size Amount of data to be sent
\r
2727 * @retval HAL status
\r
2729 HAL_StatusTypeDef HAL_I2C_Mem_Write_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
\r
2731 uint32_t tickstart;
\r
2732 uint32_t xfermode;
\r
2733 HAL_StatusTypeDef dmaxferstatus;
\r
2735 /* Check the parameters */
\r
2736 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
\r
2738 if (hi2c->State == HAL_I2C_STATE_READY)
\r
2740 if ((pData == NULL) || (Size == 0U))
\r
2742 hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
\r
2746 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
\r
2751 /* Process Locked */
\r
2754 /* Init tickstart for timeout management*/
\r
2755 tickstart = HAL_GetTick();
\r
2757 hi2c->State = HAL_I2C_STATE_BUSY_TX;
\r
2758 hi2c->Mode = HAL_I2C_MODE_MEM;
\r
2759 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
\r
2761 /* Prepare transfer parameters */
\r
2762 hi2c->pBuffPtr = pData;
\r
2763 hi2c->XferCount = Size;
\r
2764 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
\r
2765 hi2c->XferISR = I2C_Master_ISR_DMA;
\r
2767 if (hi2c->XferCount > MAX_NBYTE_SIZE)
\r
2769 hi2c->XferSize = MAX_NBYTE_SIZE;
\r
2770 xfermode = I2C_RELOAD_MODE;
\r
2774 hi2c->XferSize = hi2c->XferCount;
\r
2775 xfermode = I2C_AUTOEND_MODE;
\r
2778 /* Send Slave Address and Memory Address */
\r
2779 if (I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
\r
2781 /* Process Unlocked */
\r
2782 __HAL_UNLOCK(hi2c);
\r
2787 if (hi2c->hdmatx != NULL)
\r
2789 /* Set the I2C DMA transfer complete callback */
\r
2790 hi2c->hdmatx->XferCpltCallback = I2C_DMAMasterTransmitCplt;
\r
2792 /* Set the DMA error callback */
\r
2793 hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
\r
2795 /* Set the unused DMA callbacks to NULL */
\r
2796 hi2c->hdmatx->XferHalfCpltCallback = NULL;
\r
2797 hi2c->hdmatx->XferAbortCallback = NULL;
\r
2799 /* Enable the DMA channel */
\r
2800 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
\r
2804 /* Update I2C state */
\r
2805 hi2c->State = HAL_I2C_STATE_READY;
\r
2806 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
2808 /* Update I2C error code */
\r
2809 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
\r
2811 /* Process Unlocked */
\r
2812 __HAL_UNLOCK(hi2c);
\r
2817 if (dmaxferstatus == HAL_OK)
\r
2819 /* Send Slave Address */
\r
2820 /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
\r
2821 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, I2C_NO_STARTSTOP);
\r
2823 /* Update XferCount value */
\r
2824 hi2c->XferCount -= hi2c->XferSize;
\r
2826 /* Process Unlocked */
\r
2827 __HAL_UNLOCK(hi2c);
\r
2829 /* Note : The I2C interrupts must be enabled after unlocking current process
\r
2830 to avoid the risk of I2C interrupt handle execution before current
\r
2832 /* Enable ERR and NACK interrupts */
\r
2833 I2C_Enable_IRQ(hi2c, I2C_XFER_ERROR_IT);
\r
2835 /* Enable DMA Request */
\r
2836 hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
\r
2840 /* Update I2C state */
\r
2841 hi2c->State = HAL_I2C_STATE_READY;
\r
2842 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
2844 /* Update I2C error code */
\r
2845 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
\r
2847 /* Process Unlocked */
\r
2848 __HAL_UNLOCK(hi2c);
\r
2862 * @brief Reads an amount of data in non-blocking mode with DMA from a specific memory address.
\r
2863 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
2864 * the configuration information for the specified I2C.
\r
2865 * @param DevAddress Target device address: The device 7 bits address value
\r
2866 * in datasheet must be shifted to the left before calling the interface
\r
2867 * @param MemAddress Internal memory address
\r
2868 * @param MemAddSize Size of internal memory address
\r
2869 * @param pData Pointer to data buffer
\r
2870 * @param Size Amount of data to be read
\r
2871 * @retval HAL status
\r
2873 HAL_StatusTypeDef HAL_I2C_Mem_Read_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
\r
2875 uint32_t tickstart;
\r
2876 uint32_t xfermode;
\r
2877 HAL_StatusTypeDef dmaxferstatus;
\r
2879 /* Check the parameters */
\r
2880 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
\r
2882 if (hi2c->State == HAL_I2C_STATE_READY)
\r
2884 if ((pData == NULL) || (Size == 0U))
\r
2886 hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
\r
2890 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
\r
2895 /* Process Locked */
\r
2898 /* Init tickstart for timeout management*/
\r
2899 tickstart = HAL_GetTick();
\r
2901 hi2c->State = HAL_I2C_STATE_BUSY_RX;
\r
2902 hi2c->Mode = HAL_I2C_MODE_MEM;
\r
2903 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
\r
2905 /* Prepare transfer parameters */
\r
2906 hi2c->pBuffPtr = pData;
\r
2907 hi2c->XferCount = Size;
\r
2908 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
\r
2909 hi2c->XferISR = I2C_Master_ISR_DMA;
\r
2911 if (hi2c->XferCount > MAX_NBYTE_SIZE)
\r
2913 hi2c->XferSize = MAX_NBYTE_SIZE;
\r
2914 xfermode = I2C_RELOAD_MODE;
\r
2918 hi2c->XferSize = hi2c->XferCount;
\r
2919 xfermode = I2C_AUTOEND_MODE;
\r
2922 /* Send Slave Address and Memory Address */
\r
2923 if (I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
\r
2925 /* Process Unlocked */
\r
2926 __HAL_UNLOCK(hi2c);
\r
2930 if (hi2c->hdmarx != NULL)
\r
2932 /* Set the I2C DMA transfer complete callback */
\r
2933 hi2c->hdmarx->XferCpltCallback = I2C_DMAMasterReceiveCplt;
\r
2935 /* Set the DMA error callback */
\r
2936 hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
\r
2938 /* Set the unused DMA callbacks to NULL */
\r
2939 hi2c->hdmarx->XferHalfCpltCallback = NULL;
\r
2940 hi2c->hdmarx->XferAbortCallback = NULL;
\r
2942 /* Enable the DMA channel */
\r
2943 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData, hi2c->XferSize);
\r
2947 /* Update I2C state */
\r
2948 hi2c->State = HAL_I2C_STATE_READY;
\r
2949 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
2951 /* Update I2C error code */
\r
2952 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
\r
2954 /* Process Unlocked */
\r
2955 __HAL_UNLOCK(hi2c);
\r
2960 if (dmaxferstatus == HAL_OK)
\r
2962 /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
\r
2963 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, I2C_GENERATE_START_READ);
\r
2965 /* Update XferCount value */
\r
2966 hi2c->XferCount -= hi2c->XferSize;
\r
2968 /* Process Unlocked */
\r
2969 __HAL_UNLOCK(hi2c);
\r
2971 /* Note : The I2C interrupts must be enabled after unlocking current process
\r
2972 to avoid the risk of I2C interrupt handle execution before current
\r
2974 /* Enable ERR and NACK interrupts */
\r
2975 I2C_Enable_IRQ(hi2c, I2C_XFER_ERROR_IT);
\r
2977 /* Enable DMA Request */
\r
2978 hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
\r
2982 /* Update I2C state */
\r
2983 hi2c->State = HAL_I2C_STATE_READY;
\r
2984 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
2986 /* Update I2C error code */
\r
2987 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
\r
2989 /* Process Unlocked */
\r
2990 __HAL_UNLOCK(hi2c);
\r
3004 * @brief Checks if target device is ready for communication.
\r
3005 * @note This function is used with Memory devices
\r
3006 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
3007 * the configuration information for the specified I2C.
\r
3008 * @param DevAddress Target device address: The device 7 bits address value
\r
3009 * in datasheet must be shifted to the left before calling the interface
\r
3010 * @param Trials Number of trials
\r
3011 * @param Timeout Timeout duration
\r
3012 * @retval HAL status
\r
3014 HAL_StatusTypeDef HAL_I2C_IsDeviceReady(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout)
\r
3016 uint32_t tickstart;
\r
3018 __IO uint32_t I2C_Trials = 0UL;
\r
3023 if (hi2c->State == HAL_I2C_STATE_READY)
\r
3025 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
\r
3030 /* Process Locked */
\r
3033 hi2c->State = HAL_I2C_STATE_BUSY;
\r
3034 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
\r
3038 /* Generate Start */
\r
3039 hi2c->Instance->CR2 = I2C_GENERATE_START(hi2c->Init.AddressingMode, DevAddress);
\r
3041 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
\r
3042 /* Wait until STOPF flag is set or a NACK flag is set*/
\r
3043 tickstart = HAL_GetTick();
\r
3045 tmp1 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF);
\r
3046 tmp2 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF);
\r
3048 while ((tmp1 == RESET) && (tmp2 == RESET))
\r
3050 if (Timeout != HAL_MAX_DELAY)
\r
3052 if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
\r
3054 /* Update I2C state */
\r
3055 hi2c->State = HAL_I2C_STATE_READY;
\r
3057 /* Update I2C error code */
\r
3058 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
\r
3060 /* Process Unlocked */
\r
3061 __HAL_UNLOCK(hi2c);
\r
3067 tmp1 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF);
\r
3068 tmp2 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF);
\r
3071 /* Check if the NACKF flag has not been set */
\r
3072 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == RESET)
\r
3074 /* Wait until STOPF flag is reset */
\r
3075 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout, tickstart) != HAL_OK)
\r
3080 /* Clear STOP Flag */
\r
3081 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
\r
3083 /* Device is ready */
\r
3084 hi2c->State = HAL_I2C_STATE_READY;
\r
3086 /* Process Unlocked */
\r
3087 __HAL_UNLOCK(hi2c);
\r
3093 /* Wait until STOPF flag is reset */
\r
3094 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout, tickstart) != HAL_OK)
\r
3099 /* Clear NACK Flag */
\r
3100 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
\r
3102 /* Clear STOP Flag, auto generated with autoend*/
\r
3103 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
\r
3106 /* Check if the maximum allowed number of trials has been reached */
\r
3107 if (I2C_Trials == Trials)
\r
3109 /* Generate Stop */
\r
3110 hi2c->Instance->CR2 |= I2C_CR2_STOP;
\r
3112 /* Wait until STOPF flag is reset */
\r
3113 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout, tickstart) != HAL_OK)
\r
3118 /* Clear STOP Flag */
\r
3119 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
\r
3122 /* Increment Trials */
\r
3125 while (I2C_Trials < Trials);
\r
3127 /* Update I2C state */
\r
3128 hi2c->State = HAL_I2C_STATE_READY;
\r
3130 /* Update I2C error code */
\r
3131 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
\r
3133 /* Process Unlocked */
\r
3134 __HAL_UNLOCK(hi2c);
\r
3145 * @brief Sequential transmit in master I2C mode an amount of data in non-blocking mode with Interrupt.
\r
3146 * @note This interface allow to manage repeated start condition when a direction change during transfer
\r
3147 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
3148 * the configuration information for the specified I2C.
\r
3149 * @param DevAddress Target device address: The device 7 bits address value
\r
3150 * in datasheet must be shifted to the left before calling the interface
\r
3151 * @param pData Pointer to data buffer
\r
3152 * @param Size Amount of data to be sent
\r
3153 * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
\r
3154 * @retval HAL status
\r
3156 HAL_StatusTypeDef HAL_I2C_Master_Seq_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
\r
3158 uint32_t xfermode;
\r
3159 uint32_t xferrequest = I2C_GENERATE_START_WRITE;
\r
3161 /* Check the parameters */
\r
3162 assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
\r
3164 if (hi2c->State == HAL_I2C_STATE_READY)
\r
3166 /* Process Locked */
\r
3169 hi2c->State = HAL_I2C_STATE_BUSY_TX;
\r
3170 hi2c->Mode = HAL_I2C_MODE_MASTER;
\r
3171 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
\r
3173 /* Prepare transfer parameters */
\r
3174 hi2c->pBuffPtr = pData;
\r
3175 hi2c->XferCount = Size;
\r
3176 hi2c->XferOptions = XferOptions;
\r
3177 hi2c->XferISR = I2C_Master_ISR_IT;
\r
3179 /* If hi2c->XferCount > MAX_NBYTE_SIZE, use reload mode */
\r
3180 if (hi2c->XferCount > MAX_NBYTE_SIZE)
\r
3182 hi2c->XferSize = MAX_NBYTE_SIZE;
\r
3183 xfermode = I2C_RELOAD_MODE;
\r
3187 hi2c->XferSize = hi2c->XferCount;
\r
3188 xfermode = hi2c->XferOptions;
\r
3191 /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */
\r
3192 /* Mean Previous state is same as current state */
\r
3193 if ((hi2c->PreviousState == I2C_STATE_MASTER_BUSY_TX) && (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 0))
\r
3195 xferrequest = I2C_NO_STARTSTOP;
\r
3199 /* Convert OTHER_xxx XferOptions if any */
\r
3200 I2C_ConvertOtherXferOptions(hi2c);
\r
3202 /* Update xfermode accordingly if no reload is necessary */
\r
3203 if (hi2c->XferCount < MAX_NBYTE_SIZE)
\r
3205 xfermode = hi2c->XferOptions;
\r
3209 /* Send Slave Address and set NBYTES to write */
\r
3210 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, xferrequest);
\r
3212 /* Process Unlocked */
\r
3213 __HAL_UNLOCK(hi2c);
\r
3215 /* Note : The I2C interrupts must be enabled after unlocking current process
\r
3216 to avoid the risk of I2C interrupt handle execution before current
\r
3218 I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT);
\r
3229 * @brief Sequential transmit in master I2C mode an amount of data in non-blocking mode with DMA.
\r
3230 * @note This interface allow to manage repeated start condition when a direction change during transfer
\r
3231 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
3232 * the configuration information for the specified I2C.
\r
3233 * @param DevAddress Target device address: The device 7 bits address value
\r
3234 * in datasheet must be shifted to the left before calling the interface
\r
3235 * @param pData Pointer to data buffer
\r
3236 * @param Size Amount of data to be sent
\r
3237 * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
\r
3238 * @retval HAL status
\r
3240 HAL_StatusTypeDef HAL_I2C_Master_Seq_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
\r
3242 uint32_t xfermode;
\r
3243 uint32_t xferrequest = I2C_GENERATE_START_WRITE;
\r
3244 HAL_StatusTypeDef dmaxferstatus;
\r
3246 /* Check the parameters */
\r
3247 assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
\r
3249 if (hi2c->State == HAL_I2C_STATE_READY)
\r
3251 /* Process Locked */
\r
3254 hi2c->State = HAL_I2C_STATE_BUSY_TX;
\r
3255 hi2c->Mode = HAL_I2C_MODE_MASTER;
\r
3256 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
\r
3258 /* Prepare transfer parameters */
\r
3259 hi2c->pBuffPtr = pData;
\r
3260 hi2c->XferCount = Size;
\r
3261 hi2c->XferOptions = XferOptions;
\r
3262 hi2c->XferISR = I2C_Master_ISR_DMA;
\r
3264 /* If hi2c->XferCount > MAX_NBYTE_SIZE, use reload mode */
\r
3265 if (hi2c->XferCount > MAX_NBYTE_SIZE)
\r
3267 hi2c->XferSize = MAX_NBYTE_SIZE;
\r
3268 xfermode = I2C_RELOAD_MODE;
\r
3272 hi2c->XferSize = hi2c->XferCount;
\r
3273 xfermode = hi2c->XferOptions;
\r
3276 /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */
\r
3277 /* Mean Previous state is same as current state */
\r
3278 if ((hi2c->PreviousState == I2C_STATE_MASTER_BUSY_TX) && (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 0))
\r
3280 xferrequest = I2C_NO_STARTSTOP;
\r
3284 /* Convert OTHER_xxx XferOptions if any */
\r
3285 I2C_ConvertOtherXferOptions(hi2c);
\r
3287 /* Update xfermode accordingly if no reload is necessary */
\r
3288 if (hi2c->XferCount < MAX_NBYTE_SIZE)
\r
3290 xfermode = hi2c->XferOptions;
\r
3294 if (hi2c->XferSize > 0U)
\r
3296 if (hi2c->hdmatx != NULL)
\r
3298 /* Set the I2C DMA transfer complete callback */
\r
3299 hi2c->hdmatx->XferCpltCallback = I2C_DMAMasterTransmitCplt;
\r
3301 /* Set the DMA error callback */
\r
3302 hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
\r
3304 /* Set the unused DMA callbacks to NULL */
\r
3305 hi2c->hdmatx->XferHalfCpltCallback = NULL;
\r
3306 hi2c->hdmatx->XferAbortCallback = NULL;
\r
3308 /* Enable the DMA channel */
\r
3309 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
\r
3313 /* Update I2C state */
\r
3314 hi2c->State = HAL_I2C_STATE_READY;
\r
3315 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
3317 /* Update I2C error code */
\r
3318 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
\r
3320 /* Process Unlocked */
\r
3321 __HAL_UNLOCK(hi2c);
\r
3326 if (dmaxferstatus == HAL_OK)
\r
3328 /* Send Slave Address and set NBYTES to write */
\r
3329 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, xferrequest);
\r
3331 /* Update XferCount value */
\r
3332 hi2c->XferCount -= hi2c->XferSize;
\r
3334 /* Process Unlocked */
\r
3335 __HAL_UNLOCK(hi2c);
\r
3337 /* Note : The I2C interrupts must be enabled after unlocking current process
\r
3338 to avoid the risk of I2C interrupt handle execution before current
\r
3340 /* Enable ERR and NACK interrupts */
\r
3341 I2C_Enable_IRQ(hi2c, I2C_XFER_ERROR_IT);
\r
3343 /* Enable DMA Request */
\r
3344 hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
\r
3348 /* Update I2C state */
\r
3349 hi2c->State = HAL_I2C_STATE_READY;
\r
3350 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
3352 /* Update I2C error code */
\r
3353 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
\r
3355 /* Process Unlocked */
\r
3356 __HAL_UNLOCK(hi2c);
\r
3363 /* Update Transfer ISR function pointer */
\r
3364 hi2c->XferISR = I2C_Master_ISR_IT;
\r
3366 /* Send Slave Address */
\r
3367 /* Set NBYTES to write and generate START condition */
\r
3368 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_WRITE);
\r
3370 /* Process Unlocked */
\r
3371 __HAL_UNLOCK(hi2c);
\r
3373 /* Note : The I2C interrupts must be enabled after unlocking current process
\r
3374 to avoid the risk of I2C interrupt handle execution before current
\r
3376 /* Enable ERR, TC, STOP, NACK, TXI interrupt */
\r
3377 /* possible to enable all of these */
\r
3378 /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
\r
3379 I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT);
\r
3391 * @brief Sequential receive in master I2C mode an amount of data in non-blocking mode with Interrupt
\r
3392 * @note This interface allow to manage repeated start condition when a direction change during transfer
\r
3393 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
3394 * the configuration information for the specified I2C.
\r
3395 * @param DevAddress Target device address: The device 7 bits address value
\r
3396 * in datasheet must be shifted to the left before calling the interface
\r
3397 * @param pData Pointer to data buffer
\r
3398 * @param Size Amount of data to be sent
\r
3399 * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
\r
3400 * @retval HAL status
\r
3402 HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
\r
3404 uint32_t xfermode;
\r
3405 uint32_t xferrequest = I2C_GENERATE_START_READ;
\r
3407 /* Check the parameters */
\r
3408 assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
\r
3410 if (hi2c->State == HAL_I2C_STATE_READY)
\r
3412 /* Process Locked */
\r
3415 hi2c->State = HAL_I2C_STATE_BUSY_RX;
\r
3416 hi2c->Mode = HAL_I2C_MODE_MASTER;
\r
3417 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
\r
3419 /* Prepare transfer parameters */
\r
3420 hi2c->pBuffPtr = pData;
\r
3421 hi2c->XferCount = Size;
\r
3422 hi2c->XferOptions = XferOptions;
\r
3423 hi2c->XferISR = I2C_Master_ISR_IT;
\r
3425 /* If hi2c->XferCount > MAX_NBYTE_SIZE, use reload mode */
\r
3426 if (hi2c->XferCount > MAX_NBYTE_SIZE)
\r
3428 hi2c->XferSize = MAX_NBYTE_SIZE;
\r
3429 xfermode = I2C_RELOAD_MODE;
\r
3433 hi2c->XferSize = hi2c->XferCount;
\r
3434 xfermode = hi2c->XferOptions;
\r
3437 /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */
\r
3438 /* Mean Previous state is same as current state */
\r
3439 if ((hi2c->PreviousState == I2C_STATE_MASTER_BUSY_RX) && (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 0))
\r
3441 xferrequest = I2C_NO_STARTSTOP;
\r
3445 /* Convert OTHER_xxx XferOptions if any */
\r
3446 I2C_ConvertOtherXferOptions(hi2c);
\r
3448 /* Update xfermode accordingly if no reload is necessary */
\r
3449 if (hi2c->XferCount < MAX_NBYTE_SIZE)
\r
3451 xfermode = hi2c->XferOptions;
\r
3455 /* Send Slave Address and set NBYTES to read */
\r
3456 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, xferrequest);
\r
3458 /* Process Unlocked */
\r
3459 __HAL_UNLOCK(hi2c);
\r
3461 /* Note : The I2C interrupts must be enabled after unlocking current process
\r
3462 to avoid the risk of I2C interrupt handle execution before current
\r
3464 I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT);
\r
3475 * @brief Sequential receive in master I2C mode an amount of data in non-blocking mode with DMA
\r
3476 * @note This interface allow to manage repeated start condition when a direction change during transfer
\r
3477 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
3478 * the configuration information for the specified I2C.
\r
3479 * @param DevAddress Target device address: The device 7 bits address value
\r
3480 * in datasheet must be shifted to the left before calling the interface
\r
3481 * @param pData Pointer to data buffer
\r
3482 * @param Size Amount of data to be sent
\r
3483 * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
\r
3484 * @retval HAL status
\r
3486 HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
\r
3488 uint32_t xfermode;
\r
3489 uint32_t xferrequest = I2C_GENERATE_START_READ;
\r
3490 HAL_StatusTypeDef dmaxferstatus;
\r
3492 /* Check the parameters */
\r
3493 assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
\r
3495 if (hi2c->State == HAL_I2C_STATE_READY)
\r
3497 /* Process Locked */
\r
3500 hi2c->State = HAL_I2C_STATE_BUSY_RX;
\r
3501 hi2c->Mode = HAL_I2C_MODE_MASTER;
\r
3502 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
\r
3504 /* Prepare transfer parameters */
\r
3505 hi2c->pBuffPtr = pData;
\r
3506 hi2c->XferCount = Size;
\r
3507 hi2c->XferOptions = XferOptions;
\r
3508 hi2c->XferISR = I2C_Master_ISR_DMA;
\r
3510 /* If hi2c->XferCount > MAX_NBYTE_SIZE, use reload mode */
\r
3511 if (hi2c->XferCount > MAX_NBYTE_SIZE)
\r
3513 hi2c->XferSize = MAX_NBYTE_SIZE;
\r
3514 xfermode = I2C_RELOAD_MODE;
\r
3518 hi2c->XferSize = hi2c->XferCount;
\r
3519 xfermode = hi2c->XferOptions;
\r
3522 /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */
\r
3523 /* Mean Previous state is same as current state */
\r
3524 if ((hi2c->PreviousState == I2C_STATE_MASTER_BUSY_RX) && (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 0))
\r
3526 xferrequest = I2C_NO_STARTSTOP;
\r
3530 /* Convert OTHER_xxx XferOptions if any */
\r
3531 I2C_ConvertOtherXferOptions(hi2c);
\r
3533 /* Update xfermode accordingly if no reload is necessary */
\r
3534 if (hi2c->XferCount < MAX_NBYTE_SIZE)
\r
3536 xfermode = hi2c->XferOptions;
\r
3540 if (hi2c->XferSize > 0U)
\r
3542 if (hi2c->hdmarx != NULL)
\r
3544 /* Set the I2C DMA transfer complete callback */
\r
3545 hi2c->hdmarx->XferCpltCallback = I2C_DMAMasterReceiveCplt;
\r
3547 /* Set the DMA error callback */
\r
3548 hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
\r
3550 /* Set the unused DMA callbacks to NULL */
\r
3551 hi2c->hdmarx->XferHalfCpltCallback = NULL;
\r
3552 hi2c->hdmarx->XferAbortCallback = NULL;
\r
3554 /* Enable the DMA channel */
\r
3555 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData, hi2c->XferSize);
\r
3559 /* Update I2C state */
\r
3560 hi2c->State = HAL_I2C_STATE_READY;
\r
3561 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
3563 /* Update I2C error code */
\r
3564 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
\r
3566 /* Process Unlocked */
\r
3567 __HAL_UNLOCK(hi2c);
\r
3572 if (dmaxferstatus == HAL_OK)
\r
3574 /* Send Slave Address and set NBYTES to read */
\r
3575 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, xferrequest);
\r
3577 /* Update XferCount value */
\r
3578 hi2c->XferCount -= hi2c->XferSize;
\r
3580 /* Process Unlocked */
\r
3581 __HAL_UNLOCK(hi2c);
\r
3583 /* Note : The I2C interrupts must be enabled after unlocking current process
\r
3584 to avoid the risk of I2C interrupt handle execution before current
\r
3586 /* Enable ERR and NACK interrupts */
\r
3587 I2C_Enable_IRQ(hi2c, I2C_XFER_ERROR_IT);
\r
3589 /* Enable DMA Request */
\r
3590 hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
\r
3594 /* Update I2C state */
\r
3595 hi2c->State = HAL_I2C_STATE_READY;
\r
3596 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
3598 /* Update I2C error code */
\r
3599 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
\r
3601 /* Process Unlocked */
\r
3602 __HAL_UNLOCK(hi2c);
\r
3609 /* Update Transfer ISR function pointer */
\r
3610 hi2c->XferISR = I2C_Master_ISR_IT;
\r
3612 /* Send Slave Address */
\r
3613 /* Set NBYTES to read and generate START condition */
\r
3614 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
\r
3616 /* Process Unlocked */
\r
3617 __HAL_UNLOCK(hi2c);
\r
3619 /* Note : The I2C interrupts must be enabled after unlocking current process
\r
3620 to avoid the risk of I2C interrupt handle execution before current
\r
3622 /* Enable ERR, TC, STOP, NACK, TXI interrupt */
\r
3623 /* possible to enable all of these */
\r
3624 /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
\r
3625 I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT);
\r
3637 * @brief Sequential transmit in slave/device I2C mode an amount of data in non-blocking mode with Interrupt
\r
3638 * @note This interface allow to manage repeated start condition when a direction change during transfer
\r
3639 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
3640 * the configuration information for the specified I2C.
\r
3641 * @param pData Pointer to data buffer
\r
3642 * @param Size Amount of data to be sent
\r
3643 * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
\r
3644 * @retval HAL status
\r
3646 HAL_StatusTypeDef HAL_I2C_Slave_Seq_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
\r
3648 /* Check the parameters */
\r
3649 assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
\r
3651 if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
\r
3653 if ((pData == NULL) || (Size == 0U))
\r
3655 hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
\r
3659 /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
\r
3660 I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_TX_IT);
\r
3662 /* Process Locked */
\r
3665 /* I2C cannot manage full duplex exchange so disable previous IT enabled if any */
\r
3666 /* and then toggle the HAL slave RX state to TX state */
\r
3667 if (hi2c->State == HAL_I2C_STATE_BUSY_RX_LISTEN)
\r
3669 /* Disable associated Interrupts */
\r
3670 I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT);
\r
3672 /* Abort DMA Xfer if any */
\r
3673 if ((hi2c->Instance->CR1 & I2C_CR1_RXDMAEN) == I2C_CR1_RXDMAEN)
\r
3675 hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
\r
3677 if (hi2c->hdmarx != NULL)
\r
3679 /* Set the I2C DMA Abort callback :
\r
3680 will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
\r
3681 hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort;
\r
3683 /* Abort DMA RX */
\r
3684 if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK)
\r
3686 /* Call Directly XferAbortCallback function in case of error */
\r
3687 hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx);
\r
3693 hi2c->State = HAL_I2C_STATE_BUSY_TX_LISTEN;
\r
3694 hi2c->Mode = HAL_I2C_MODE_SLAVE;
\r
3695 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
\r
3697 /* Enable Address Acknowledge */
\r
3698 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
\r
3700 /* Prepare transfer parameters */
\r
3701 hi2c->pBuffPtr = pData;
\r
3702 hi2c->XferCount = Size;
\r
3703 hi2c->XferSize = hi2c->XferCount;
\r
3704 hi2c->XferOptions = XferOptions;
\r
3705 hi2c->XferISR = I2C_Slave_ISR_IT;
\r
3707 if (I2C_GET_DIR(hi2c) == I2C_DIRECTION_RECEIVE)
\r
3709 /* Clear ADDR flag after prepare the transfer parameters */
\r
3710 /* This action will generate an acknowledge to the Master */
\r
3711 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
\r
3714 /* Process Unlocked */
\r
3715 __HAL_UNLOCK(hi2c);
\r
3717 /* Note : The I2C interrupts must be enabled after unlocking current process
\r
3718 to avoid the risk of I2C interrupt handle execution before current
\r
3720 /* REnable ADDR interrupt */
\r
3721 I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT | I2C_XFER_LISTEN_IT);
\r
3732 * @brief Sequential transmit in slave/device I2C mode an amount of data in non-blocking mode with DMA
\r
3733 * @note This interface allow to manage repeated start condition when a direction change during transfer
\r
3734 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
3735 * the configuration information for the specified I2C.
\r
3736 * @param pData Pointer to data buffer
\r
3737 * @param Size Amount of data to be sent
\r
3738 * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
\r
3739 * @retval HAL status
\r
3741 HAL_StatusTypeDef HAL_I2C_Slave_Seq_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
\r
3743 HAL_StatusTypeDef dmaxferstatus;
\r
3745 /* Check the parameters */
\r
3746 assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
\r
3748 if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
\r
3750 if ((pData == NULL) || (Size == 0U))
\r
3752 hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
\r
3756 /* Process Locked */
\r
3759 /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
\r
3760 I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_TX_IT);
\r
3762 /* I2C cannot manage full duplex exchange so disable previous IT enabled if any */
\r
3763 /* and then toggle the HAL slave RX state to TX state */
\r
3764 if (hi2c->State == HAL_I2C_STATE_BUSY_RX_LISTEN)
\r
3766 /* Disable associated Interrupts */
\r
3767 I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT);
\r
3769 if ((hi2c->Instance->CR1 & I2C_CR1_RXDMAEN) == I2C_CR1_RXDMAEN)
\r
3771 /* Abort DMA Xfer if any */
\r
3772 if (hi2c->hdmarx != NULL)
\r
3774 hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
\r
3776 /* Set the I2C DMA Abort callback :
\r
3777 will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
\r
3778 hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort;
\r
3780 /* Abort DMA RX */
\r
3781 if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK)
\r
3783 /* Call Directly XferAbortCallback function in case of error */
\r
3784 hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx);
\r
3789 else if (hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN)
\r
3791 if ((hi2c->Instance->CR1 & I2C_CR1_TXDMAEN) == I2C_CR1_TXDMAEN)
\r
3793 hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
\r
3795 /* Abort DMA Xfer if any */
\r
3796 if (hi2c->hdmatx != NULL)
\r
3798 /* Set the I2C DMA Abort callback :
\r
3799 will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
\r
3800 hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort;
\r
3802 /* Abort DMA TX */
\r
3803 if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK)
\r
3805 /* Call Directly XferAbortCallback function in case of error */
\r
3806 hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx);
\r
3813 /* Nothing to do */
\r
3816 hi2c->State = HAL_I2C_STATE_BUSY_TX_LISTEN;
\r
3817 hi2c->Mode = HAL_I2C_MODE_SLAVE;
\r
3818 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
\r
3820 /* Enable Address Acknowledge */
\r
3821 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
\r
3823 /* Prepare transfer parameters */
\r
3824 hi2c->pBuffPtr = pData;
\r
3825 hi2c->XferCount = Size;
\r
3826 hi2c->XferSize = hi2c->XferCount;
\r
3827 hi2c->XferOptions = XferOptions;
\r
3828 hi2c->XferISR = I2C_Slave_ISR_DMA;
\r
3830 if (hi2c->hdmatx != NULL)
\r
3832 /* Set the I2C DMA transfer complete callback */
\r
3833 hi2c->hdmatx->XferCpltCallback = I2C_DMASlaveTransmitCplt;
\r
3835 /* Set the DMA error callback */
\r
3836 hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
\r
3838 /* Set the unused DMA callbacks to NULL */
\r
3839 hi2c->hdmatx->XferHalfCpltCallback = NULL;
\r
3840 hi2c->hdmatx->XferAbortCallback = NULL;
\r
3842 /* Enable the DMA channel */
\r
3843 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
\r
3847 /* Update I2C state */
\r
3848 hi2c->State = HAL_I2C_STATE_LISTEN;
\r
3849 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
3851 /* Update I2C error code */
\r
3852 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
\r
3854 /* Process Unlocked */
\r
3855 __HAL_UNLOCK(hi2c);
\r
3860 if (dmaxferstatus == HAL_OK)
\r
3862 /* Update XferCount value */
\r
3863 hi2c->XferCount -= hi2c->XferSize;
\r
3865 /* Reset XferSize */
\r
3866 hi2c->XferSize = 0;
\r
3870 /* Update I2C state */
\r
3871 hi2c->State = HAL_I2C_STATE_LISTEN;
\r
3872 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
3874 /* Update I2C error code */
\r
3875 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
\r
3877 /* Process Unlocked */
\r
3878 __HAL_UNLOCK(hi2c);
\r
3883 if (I2C_GET_DIR(hi2c) == I2C_DIRECTION_RECEIVE)
\r
3885 /* Clear ADDR flag after prepare the transfer parameters */
\r
3886 /* This action will generate an acknowledge to the Master */
\r
3887 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
\r
3890 /* Process Unlocked */
\r
3891 __HAL_UNLOCK(hi2c);
\r
3893 /* Note : The I2C interrupts must be enabled after unlocking current process
\r
3894 to avoid the risk of I2C interrupt handle execution before current
\r
3896 /* Enable ERR, STOP, NACK, ADDR interrupts */
\r
3897 I2C_Enable_IRQ(hi2c, I2C_XFER_LISTEN_IT);
\r
3899 /* Enable DMA Request */
\r
3900 hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
\r
3911 * @brief Sequential receive in slave/device I2C mode an amount of data in non-blocking mode with Interrupt
\r
3912 * @note This interface allow to manage repeated start condition when a direction change during transfer
\r
3913 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
3914 * the configuration information for the specified I2C.
\r
3915 * @param pData Pointer to data buffer
\r
3916 * @param Size Amount of data to be sent
\r
3917 * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
\r
3918 * @retval HAL status
\r
3920 HAL_StatusTypeDef HAL_I2C_Slave_Seq_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
\r
3922 /* Check the parameters */
\r
3923 assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
\r
3925 if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
\r
3927 if ((pData == NULL) || (Size == 0U))
\r
3929 hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
\r
3933 /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
\r
3934 I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_RX_IT);
\r
3936 /* Process Locked */
\r
3939 /* I2C cannot manage full duplex exchange so disable previous IT enabled if any */
\r
3940 /* and then toggle the HAL slave TX state to RX state */
\r
3941 if (hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN)
\r
3943 /* Disable associated Interrupts */
\r
3944 I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT);
\r
3946 if ((hi2c->Instance->CR1 & I2C_CR1_TXDMAEN) == I2C_CR1_TXDMAEN)
\r
3948 hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
\r
3950 /* Abort DMA Xfer if any */
\r
3951 if (hi2c->hdmatx != NULL)
\r
3953 /* Set the I2C DMA Abort callback :
\r
3954 will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
\r
3955 hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort;
\r
3957 /* Abort DMA TX */
\r
3958 if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK)
\r
3960 /* Call Directly XferAbortCallback function in case of error */
\r
3961 hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx);
\r
3967 hi2c->State = HAL_I2C_STATE_BUSY_RX_LISTEN;
\r
3968 hi2c->Mode = HAL_I2C_MODE_SLAVE;
\r
3969 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
\r
3971 /* Enable Address Acknowledge */
\r
3972 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
\r
3974 /* Prepare transfer parameters */
\r
3975 hi2c->pBuffPtr = pData;
\r
3976 hi2c->XferCount = Size;
\r
3977 hi2c->XferSize = hi2c->XferCount;
\r
3978 hi2c->XferOptions = XferOptions;
\r
3979 hi2c->XferISR = I2C_Slave_ISR_IT;
\r
3981 if (I2C_GET_DIR(hi2c) == I2C_DIRECTION_TRANSMIT)
\r
3983 /* Clear ADDR flag after prepare the transfer parameters */
\r
3984 /* This action will generate an acknowledge to the Master */
\r
3985 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
\r
3988 /* Process Unlocked */
\r
3989 __HAL_UNLOCK(hi2c);
\r
3991 /* Note : The I2C interrupts must be enabled after unlocking current process
\r
3992 to avoid the risk of I2C interrupt handle execution before current
\r
3994 /* REnable ADDR interrupt */
\r
3995 I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT | I2C_XFER_LISTEN_IT);
\r
4006 * @brief Sequential receive in slave/device I2C mode an amount of data in non-blocking mode with DMA
\r
4007 * @note This interface allow to manage repeated start condition when a direction change during transfer
\r
4008 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
4009 * the configuration information for the specified I2C.
\r
4010 * @param pData Pointer to data buffer
\r
4011 * @param Size Amount of data to be sent
\r
4012 * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
\r
4013 * @retval HAL status
\r
4015 HAL_StatusTypeDef HAL_I2C_Slave_Seq_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
\r
4017 HAL_StatusTypeDef dmaxferstatus;
\r
4019 /* Check the parameters */
\r
4020 assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
\r
4022 if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
\r
4024 if ((pData == NULL) || (Size == 0U))
\r
4026 hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
\r
4030 /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
\r
4031 I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_RX_IT);
\r
4033 /* Process Locked */
\r
4036 /* I2C cannot manage full duplex exchange so disable previous IT enabled if any */
\r
4037 /* and then toggle the HAL slave TX state to RX state */
\r
4038 if (hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN)
\r
4040 /* Disable associated Interrupts */
\r
4041 I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT);
\r
4043 if ((hi2c->Instance->CR1 & I2C_CR1_TXDMAEN) == I2C_CR1_TXDMAEN)
\r
4045 /* Abort DMA Xfer if any */
\r
4046 if (hi2c->hdmatx != NULL)
\r
4048 hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
\r
4050 /* Set the I2C DMA Abort callback :
\r
4051 will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
\r
4052 hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort;
\r
4054 /* Abort DMA TX */
\r
4055 if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK)
\r
4057 /* Call Directly XferAbortCallback function in case of error */
\r
4058 hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx);
\r
4063 else if (hi2c->State == HAL_I2C_STATE_BUSY_RX_LISTEN)
\r
4065 if ((hi2c->Instance->CR1 & I2C_CR1_RXDMAEN) == I2C_CR1_RXDMAEN)
\r
4067 hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
\r
4069 /* Abort DMA Xfer if any */
\r
4070 if (hi2c->hdmarx != NULL)
\r
4072 /* Set the I2C DMA Abort callback :
\r
4073 will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
\r
4074 hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort;
\r
4076 /* Abort DMA RX */
\r
4077 if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK)
\r
4079 /* Call Directly XferAbortCallback function in case of error */
\r
4080 hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx);
\r
4087 /* Nothing to do */
\r
4090 hi2c->State = HAL_I2C_STATE_BUSY_RX_LISTEN;
\r
4091 hi2c->Mode = HAL_I2C_MODE_SLAVE;
\r
4092 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
\r
4094 /* Enable Address Acknowledge */
\r
4095 hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
\r
4097 /* Prepare transfer parameters */
\r
4098 hi2c->pBuffPtr = pData;
\r
4099 hi2c->XferCount = Size;
\r
4100 hi2c->XferSize = hi2c->XferCount;
\r
4101 hi2c->XferOptions = XferOptions;
\r
4102 hi2c->XferISR = I2C_Slave_ISR_DMA;
\r
4104 if (hi2c->hdmarx != NULL)
\r
4106 /* Set the I2C DMA transfer complete callback */
\r
4107 hi2c->hdmarx->XferCpltCallback = I2C_DMASlaveReceiveCplt;
\r
4109 /* Set the DMA error callback */
\r
4110 hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
\r
4112 /* Set the unused DMA callbacks to NULL */
\r
4113 hi2c->hdmarx->XferHalfCpltCallback = NULL;
\r
4114 hi2c->hdmarx->XferAbortCallback = NULL;
\r
4116 /* Enable the DMA channel */
\r
4117 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData, hi2c->XferSize);
\r
4121 /* Update I2C state */
\r
4122 hi2c->State = HAL_I2C_STATE_LISTEN;
\r
4123 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
4125 /* Update I2C error code */
\r
4126 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
\r
4128 /* Process Unlocked */
\r
4129 __HAL_UNLOCK(hi2c);
\r
4134 if (dmaxferstatus == HAL_OK)
\r
4136 /* Update XferCount value */
\r
4137 hi2c->XferCount -= hi2c->XferSize;
\r
4139 /* Reset XferSize */
\r
4140 hi2c->XferSize = 0;
\r
4144 /* Update I2C state */
\r
4145 hi2c->State = HAL_I2C_STATE_LISTEN;
\r
4146 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
4148 /* Update I2C error code */
\r
4149 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
\r
4151 /* Process Unlocked */
\r
4152 __HAL_UNLOCK(hi2c);
\r
4157 if (I2C_GET_DIR(hi2c) == I2C_DIRECTION_TRANSMIT)
\r
4159 /* Clear ADDR flag after prepare the transfer parameters */
\r
4160 /* This action will generate an acknowledge to the Master */
\r
4161 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
\r
4164 /* Process Unlocked */
\r
4165 __HAL_UNLOCK(hi2c);
\r
4167 /* Note : The I2C interrupts must be enabled after unlocking current process
\r
4168 to avoid the risk of I2C interrupt handle execution before current
\r
4170 /* REnable ADDR interrupt */
\r
4171 I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT | I2C_XFER_LISTEN_IT);
\r
4173 /* Enable DMA Request */
\r
4174 hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
\r
4185 * @brief Enable the Address listen mode with Interrupt.
\r
4186 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
4187 * the configuration information for the specified I2C.
\r
4188 * @retval HAL status
\r
4190 HAL_StatusTypeDef HAL_I2C_EnableListen_IT(I2C_HandleTypeDef *hi2c)
\r
4192 if (hi2c->State == HAL_I2C_STATE_READY)
\r
4194 hi2c->State = HAL_I2C_STATE_LISTEN;
\r
4195 hi2c->XferISR = I2C_Slave_ISR_IT;
\r
4197 /* Enable the Address Match interrupt */
\r
4198 I2C_Enable_IRQ(hi2c, I2C_XFER_LISTEN_IT);
\r
4209 * @brief Disable the Address listen mode with Interrupt.
\r
4210 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
4211 * the configuration information for the specified I2C
\r
4212 * @retval HAL status
\r
4214 HAL_StatusTypeDef HAL_I2C_DisableListen_IT(I2C_HandleTypeDef *hi2c)
\r
4216 /* Declaration of tmp to prevent undefined behavior of volatile usage */
\r
4219 /* Disable Address listen mode only if a transfer is not ongoing */
\r
4220 if (hi2c->State == HAL_I2C_STATE_LISTEN)
\r
4222 tmp = (uint32_t)(hi2c->State) & I2C_STATE_MSK;
\r
4223 hi2c->PreviousState = tmp | (uint32_t)(hi2c->Mode);
\r
4224 hi2c->State = HAL_I2C_STATE_READY;
\r
4225 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
4226 hi2c->XferISR = NULL;
\r
4228 /* Disable the Address Match interrupt */
\r
4229 I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT);
\r
4240 * @brief Abort a master I2C IT or DMA process communication with Interrupt.
\r
4241 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
4242 * the configuration information for the specified I2C.
\r
4243 * @param DevAddress Target device address: The device 7 bits address value
\r
4244 * in datasheet must be shifted to the left before calling the interface
\r
4245 * @retval HAL status
\r
4247 HAL_StatusTypeDef HAL_I2C_Master_Abort_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress)
\r
4249 if (hi2c->Mode == HAL_I2C_MODE_MASTER)
\r
4251 /* Process Locked */
\r
4254 /* Disable Interrupts */
\r
4255 I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT);
\r
4256 I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT);
\r
4258 /* Set State at HAL_I2C_STATE_ABORT */
\r
4259 hi2c->State = HAL_I2C_STATE_ABORT;
\r
4261 /* Set NBYTES to 1 to generate a dummy read on I2C peripheral */
\r
4262 /* Set AUTOEND mode, this will generate a NACK then STOP condition to abort the current transfer */
\r
4263 I2C_TransferConfig(hi2c, DevAddress, 1, I2C_AUTOEND_MODE, I2C_GENERATE_STOP);
\r
4265 /* Process Unlocked */
\r
4266 __HAL_UNLOCK(hi2c);
\r
4268 /* Note : The I2C interrupts must be enabled after unlocking current process
\r
4269 to avoid the risk of I2C interrupt handle execution before current
\r
4271 I2C_Enable_IRQ(hi2c, I2C_XFER_CPLT_IT);
\r
4277 /* Wrong usage of abort function */
\r
4278 /* This function should be used only in case of abort monitored by master device */
\r
4287 /** @defgroup I2C_IRQ_Handler_and_Callbacks IRQ Handler and Callbacks
\r
4292 * @brief This function handles I2C event interrupt request.
\r
4293 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
4294 * the configuration information for the specified I2C.
\r
4297 void HAL_I2C_EV_IRQHandler(I2C_HandleTypeDef *hi2c)
\r
4299 /* Get current IT Flags and IT sources value */
\r
4300 uint32_t itflags = READ_REG(hi2c->Instance->ISR);
\r
4301 uint32_t itsources = READ_REG(hi2c->Instance->CR1);
\r
4303 /* I2C events treatment -------------------------------------*/
\r
4304 if (hi2c->XferISR != NULL)
\r
4306 hi2c->XferISR(hi2c, itflags, itsources);
\r
4311 * @brief This function handles I2C error interrupt request.
\r
4312 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
4313 * the configuration information for the specified I2C.
\r
4316 void HAL_I2C_ER_IRQHandler(I2C_HandleTypeDef *hi2c)
\r
4318 uint32_t itflags = READ_REG(hi2c->Instance->ISR);
\r
4319 uint32_t itsources = READ_REG(hi2c->Instance->CR1);
\r
4320 uint32_t tmperror;
\r
4322 /* I2C Bus error interrupt occurred ------------------------------------*/
\r
4323 if ((I2C_CHECK_FLAG(itflags, I2C_FLAG_BERR) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_ERRI) != RESET))
\r
4325 hi2c->ErrorCode |= HAL_I2C_ERROR_BERR;
\r
4327 /* Clear BERR flag */
\r
4328 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_BERR);
\r
4331 /* I2C Over-Run/Under-Run interrupt occurred ----------------------------------------*/
\r
4332 if ((I2C_CHECK_FLAG(itflags, I2C_FLAG_OVR) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_ERRI) != RESET))
\r
4334 hi2c->ErrorCode |= HAL_I2C_ERROR_OVR;
\r
4336 /* Clear OVR flag */
\r
4337 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_OVR);
\r
4340 /* I2C Arbitration Loss error interrupt occurred -------------------------------------*/
\r
4341 if ((I2C_CHECK_FLAG(itflags, I2C_FLAG_ARLO) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_ERRI) != RESET))
\r
4343 hi2c->ErrorCode |= HAL_I2C_ERROR_ARLO;
\r
4345 /* Clear ARLO flag */
\r
4346 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ARLO);
\r
4349 /* Store current volatile hi2c->ErrorCode, misra rule */
\r
4350 tmperror = hi2c->ErrorCode;
\r
4352 /* Call the Error Callback in case of Error detected */
\r
4353 if ((tmperror & (HAL_I2C_ERROR_BERR | HAL_I2C_ERROR_OVR | HAL_I2C_ERROR_ARLO)) != HAL_I2C_ERROR_NONE)
\r
4355 I2C_ITError(hi2c, tmperror);
\r
4360 * @brief Master Tx Transfer completed callback.
\r
4361 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
4362 * the configuration information for the specified I2C.
\r
4365 __weak void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c)
\r
4367 /* Prevent unused argument(s) compilation warning */
\r
4370 /* NOTE : This function should not be modified, when the callback is needed,
\r
4371 the HAL_I2C_MasterTxCpltCallback could be implemented in the user file
\r
4376 * @brief Master Rx Transfer completed callback.
\r
4377 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
4378 * the configuration information for the specified I2C.
\r
4381 __weak void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c)
\r
4383 /* Prevent unused argument(s) compilation warning */
\r
4386 /* NOTE : This function should not be modified, when the callback is needed,
\r
4387 the HAL_I2C_MasterRxCpltCallback could be implemented in the user file
\r
4391 /** @brief Slave Tx Transfer completed callback.
\r
4392 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
4393 * the configuration information for the specified I2C.
\r
4396 __weak void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c)
\r
4398 /* Prevent unused argument(s) compilation warning */
\r
4401 /* NOTE : This function should not be modified, when the callback is needed,
\r
4402 the HAL_I2C_SlaveTxCpltCallback could be implemented in the user file
\r
4407 * @brief Slave Rx Transfer completed callback.
\r
4408 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
4409 * the configuration information for the specified I2C.
\r
4412 __weak void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c)
\r
4414 /* Prevent unused argument(s) compilation warning */
\r
4417 /* NOTE : This function should not be modified, when the callback is needed,
\r
4418 the HAL_I2C_SlaveRxCpltCallback could be implemented in the user file
\r
4423 * @brief Slave Address Match callback.
\r
4424 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
4425 * the configuration information for the specified I2C.
\r
4426 * @param TransferDirection Master request Transfer Direction (Write/Read), value of @ref I2C_XFERDIRECTION
\r
4427 * @param AddrMatchCode Address Match Code
\r
4430 __weak void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode)
\r
4432 /* Prevent unused argument(s) compilation warning */
\r
4434 UNUSED(TransferDirection);
\r
4435 UNUSED(AddrMatchCode);
\r
4437 /* NOTE : This function should not be modified, when the callback is needed,
\r
4438 the HAL_I2C_AddrCallback() could be implemented in the user file
\r
4443 * @brief Listen Complete callback.
\r
4444 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
4445 * the configuration information for the specified I2C.
\r
4448 __weak void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c)
\r
4450 /* Prevent unused argument(s) compilation warning */
\r
4453 /* NOTE : This function should not be modified, when the callback is needed,
\r
4454 the HAL_I2C_ListenCpltCallback() could be implemented in the user file
\r
4459 * @brief Memory Tx Transfer completed callback.
\r
4460 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
4461 * the configuration information for the specified I2C.
\r
4464 __weak void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef *hi2c)
\r
4466 /* Prevent unused argument(s) compilation warning */
\r
4469 /* NOTE : This function should not be modified, when the callback is needed,
\r
4470 the HAL_I2C_MemTxCpltCallback could be implemented in the user file
\r
4475 * @brief Memory Rx Transfer completed callback.
\r
4476 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
4477 * the configuration information for the specified I2C.
\r
4480 __weak void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c)
\r
4482 /* Prevent unused argument(s) compilation warning */
\r
4485 /* NOTE : This function should not be modified, when the callback is needed,
\r
4486 the HAL_I2C_MemRxCpltCallback could be implemented in the user file
\r
4491 * @brief I2C error callback.
\r
4492 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
4493 * the configuration information for the specified I2C.
\r
4496 __weak void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
\r
4498 /* Prevent unused argument(s) compilation warning */
\r
4501 /* NOTE : This function should not be modified, when the callback is needed,
\r
4502 the HAL_I2C_ErrorCallback could be implemented in the user file
\r
4507 * @brief I2C abort callback.
\r
4508 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
4509 * the configuration information for the specified I2C.
\r
4512 __weak void HAL_I2C_AbortCpltCallback(I2C_HandleTypeDef *hi2c)
\r
4514 /* Prevent unused argument(s) compilation warning */
\r
4517 /* NOTE : This function should not be modified, when the callback is needed,
\r
4518 the HAL_I2C_AbortCpltCallback could be implemented in the user file
\r
4526 /** @defgroup I2C_Exported_Functions_Group3 Peripheral State, Mode and Error functions
\r
4527 * @brief Peripheral State, Mode and Error functions
\r
4530 ===============================================================================
\r
4531 ##### Peripheral State, Mode and Error functions #####
\r
4532 ===============================================================================
\r
4534 This subsection permit to get in run-time the status of the peripheral
\r
4535 and the data flow.
\r
4542 * @brief Return the I2C handle state.
\r
4543 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
4544 * the configuration information for the specified I2C.
\r
4545 * @retval HAL state
\r
4547 HAL_I2C_StateTypeDef HAL_I2C_GetState(I2C_HandleTypeDef *hi2c)
\r
4549 /* Return I2C handle state */
\r
4550 return hi2c->State;
\r
4554 * @brief Returns the I2C Master, Slave, Memory or no mode.
\r
4555 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
4556 * the configuration information for I2C module
\r
4557 * @retval HAL mode
\r
4559 HAL_I2C_ModeTypeDef HAL_I2C_GetMode(I2C_HandleTypeDef *hi2c)
\r
4561 return hi2c->Mode;
\r
4565 * @brief Return the I2C error code.
\r
4566 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
4567 * the configuration information for the specified I2C.
\r
4568 * @retval I2C Error Code
\r
4570 uint32_t HAL_I2C_GetError(I2C_HandleTypeDef *hi2c)
\r
4572 return hi2c->ErrorCode;
\r
4583 /** @addtogroup I2C_Private_Functions
\r
4588 * @brief Interrupt Sub-Routine which handle the Interrupt Flags Master Mode with Interrupt.
\r
4589 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
4590 * the configuration information for the specified I2C.
\r
4591 * @param ITFlags Interrupt flags to handle.
\r
4592 * @param ITSources Interrupt sources enabled.
\r
4593 * @retval HAL status
\r
4595 static HAL_StatusTypeDef I2C_Master_ISR_IT(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags, uint32_t ITSources)
\r
4597 uint16_t devaddress;
\r
4598 uint32_t tmpITFlags = ITFlags;
\r
4600 /* Process Locked */
\r
4603 if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_AF) != RESET) && (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_NACKI) != RESET))
\r
4605 /* Clear NACK Flag */
\r
4606 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
\r
4608 /* Set corresponding Error Code */
\r
4609 /* No need to generate STOP, it is automatically done */
\r
4610 /* Error callback will be send during stop flag treatment */
\r
4611 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
\r
4613 /* Flush TX register */
\r
4614 I2C_Flush_TXDR(hi2c);
\r
4616 else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_RXNE) != RESET) && (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_RXI) != RESET))
\r
4618 /* Remove RXNE flag on temporary variable as read done */
\r
4619 tmpITFlags &= ~I2C_FLAG_RXNE;
\r
4621 /* Read data from RXDR */
\r
4622 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR;
\r
4624 /* Increment Buffer pointer */
\r
4628 hi2c->XferCount--;
\r
4630 else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_TXIS) != RESET) && (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TXI) != RESET))
\r
4632 /* Write data to TXDR */
\r
4633 hi2c->Instance->TXDR = *hi2c->pBuffPtr;
\r
4635 /* Increment Buffer pointer */
\r
4639 hi2c->XferCount--;
\r
4641 else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_TCR) != RESET) && (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TCI) != RESET))
\r
4643 if ((hi2c->XferCount != 0U) && (hi2c->XferSize == 0U))
\r
4645 devaddress = (uint16_t)(hi2c->Instance->CR2 & I2C_CR2_SADD);
\r
4647 if (hi2c->XferCount > MAX_NBYTE_SIZE)
\r
4649 hi2c->XferSize = MAX_NBYTE_SIZE;
\r
4650 I2C_TransferConfig(hi2c, devaddress, (uint8_t)hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
\r
4654 hi2c->XferSize = hi2c->XferCount;
\r
4655 if (hi2c->XferOptions != I2C_NO_OPTION_FRAME)
\r
4657 I2C_TransferConfig(hi2c, devaddress, (uint8_t)hi2c->XferSize, hi2c->XferOptions, I2C_NO_STARTSTOP);
\r
4661 I2C_TransferConfig(hi2c, devaddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
\r
4667 /* Call TxCpltCallback() if no stop mode is set */
\r
4668 if (I2C_GET_STOP_MODE(hi2c) != I2C_AUTOEND_MODE)
\r
4670 /* Call I2C Master Sequential complete process */
\r
4671 I2C_ITMasterSeqCplt(hi2c);
\r
4675 /* Wrong size Status regarding TCR flag event */
\r
4676 /* Call the corresponding callback to inform upper layer of End of Transfer */
\r
4677 I2C_ITError(hi2c, HAL_I2C_ERROR_SIZE);
\r
4681 else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_TC) != RESET) && (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TCI) != RESET))
\r
4683 if (hi2c->XferCount == 0U)
\r
4685 if (I2C_GET_STOP_MODE(hi2c) != I2C_AUTOEND_MODE)
\r
4687 /* Generate a stop condition in case of no transfer option */
\r
4688 if (hi2c->XferOptions == I2C_NO_OPTION_FRAME)
\r
4690 /* Generate Stop */
\r
4691 hi2c->Instance->CR2 |= I2C_CR2_STOP;
\r
4695 /* Call I2C Master Sequential complete process */
\r
4696 I2C_ITMasterSeqCplt(hi2c);
\r
4702 /* Wrong size Status regarding TC flag event */
\r
4703 /* Call the corresponding callback to inform upper layer of End of Transfer */
\r
4704 I2C_ITError(hi2c, HAL_I2C_ERROR_SIZE);
\r
4709 /* Nothing to do */
\r
4712 if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_STOPF) != RESET) && (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_STOPI) != RESET))
\r
4714 /* Call I2C Master complete process */
\r
4715 I2C_ITMasterCplt(hi2c, tmpITFlags);
\r
4718 /* Process Unlocked */
\r
4719 __HAL_UNLOCK(hi2c);
\r
4725 * @brief Interrupt Sub-Routine which handle the Interrupt Flags Slave Mode with Interrupt.
\r
4726 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
4727 * the configuration information for the specified I2C.
\r
4728 * @param ITFlags Interrupt flags to handle.
\r
4729 * @param ITSources Interrupt sources enabled.
\r
4730 * @retval HAL status
\r
4732 static HAL_StatusTypeDef I2C_Slave_ISR_IT(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags, uint32_t ITSources)
\r
4734 uint32_t tmpoptions = hi2c->XferOptions;
\r
4735 uint32_t tmpITFlags = ITFlags;
\r
4737 /* Process locked */
\r
4740 if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_AF) != RESET) && (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_NACKI) != RESET))
\r
4742 /* Check that I2C transfer finished */
\r
4743 /* if yes, normal use case, a NACK is sent by the MASTER when Transfer is finished */
\r
4744 /* Mean XferCount == 0*/
\r
4745 /* So clear Flag NACKF only */
\r
4746 if (hi2c->XferCount == 0U)
\r
4748 if ((hi2c->State == HAL_I2C_STATE_LISTEN) && (tmpoptions == I2C_FIRST_AND_LAST_FRAME)) /* Same action must be done for (tmpoptions == I2C_LAST_FRAME) which removed for Warning[Pa134]: left and right operands are identical */
\r
4750 /* Call I2C Listen complete process */
\r
4751 I2C_ITListenCplt(hi2c, tmpITFlags);
\r
4753 else if ((hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN) && (tmpoptions != I2C_NO_OPTION_FRAME))
\r
4755 /* Clear NACK Flag */
\r
4756 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
\r
4758 /* Flush TX register */
\r
4759 I2C_Flush_TXDR(hi2c);
\r
4761 /* Last Byte is Transmitted */
\r
4762 /* Call I2C Slave Sequential complete process */
\r
4763 I2C_ITSlaveSeqCplt(hi2c);
\r
4767 /* Clear NACK Flag */
\r
4768 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
\r
4773 /* if no, error use case, a Non-Acknowledge of last Data is generated by the MASTER*/
\r
4774 /* Clear NACK Flag */
\r
4775 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
\r
4777 /* Set ErrorCode corresponding to a Non-Acknowledge */
\r
4778 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
\r
4780 if ((tmpoptions == I2C_FIRST_FRAME) || (tmpoptions == I2C_NEXT_FRAME))
\r
4782 /* Call the corresponding callback to inform upper layer of End of Transfer */
\r
4783 I2C_ITError(hi2c, hi2c->ErrorCode);
\r
4787 else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_RXNE) != RESET) && (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_RXI) != RESET))
\r
4789 if (hi2c->XferCount > 0U)
\r
4791 /* Remove RXNE flag on temporary variable as read done */
\r
4792 tmpITFlags &= ~I2C_FLAG_RXNE;
\r
4794 /* Read data from RXDR */
\r
4795 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR;
\r
4797 /* Increment Buffer pointer */
\r
4801 hi2c->XferCount--;
\r
4804 if ((hi2c->XferCount == 0U) && \
\r
4805 (tmpoptions != I2C_NO_OPTION_FRAME))
\r
4807 /* Call I2C Slave Sequential complete process */
\r
4808 I2C_ITSlaveSeqCplt(hi2c);
\r
4811 else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_ADDR) != RESET) && (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_ADDRI) != RESET))
\r
4813 I2C_ITAddrCplt(hi2c, tmpITFlags);
\r
4815 else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_TXIS) != RESET) && (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TXI) != RESET))
\r
4817 /* Write data to TXDR only if XferCount not reach "0" */
\r
4818 /* A TXIS flag can be set, during STOP treatment */
\r
4819 /* Check if all Datas have already been sent */
\r
4820 /* If it is the case, this last write in TXDR is not sent, correspond to a dummy TXIS event */
\r
4821 if (hi2c->XferCount > 0U)
\r
4823 /* Write data to TXDR */
\r
4824 hi2c->Instance->TXDR = *hi2c->pBuffPtr;
\r
4826 /* Increment Buffer pointer */
\r
4829 hi2c->XferCount--;
\r
4834 if ((tmpoptions == I2C_NEXT_FRAME) || (tmpoptions == I2C_FIRST_FRAME))
\r
4836 /* Last Byte is Transmitted */
\r
4837 /* Call I2C Slave Sequential complete process */
\r
4838 I2C_ITSlaveSeqCplt(hi2c);
\r
4844 /* Nothing to do */
\r
4847 /* Check if STOPF is set */
\r
4848 if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_STOPF) != RESET) && (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_STOPI) != RESET))
\r
4850 /* Call I2C Slave complete process */
\r
4851 I2C_ITSlaveCplt(hi2c, tmpITFlags);
\r
4854 /* Process Unlocked */
\r
4855 __HAL_UNLOCK(hi2c);
\r
4861 * @brief Interrupt Sub-Routine which handle the Interrupt Flags Master Mode with DMA.
\r
4862 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
4863 * the configuration information for the specified I2C.
\r
4864 * @param ITFlags Interrupt flags to handle.
\r
4865 * @param ITSources Interrupt sources enabled.
\r
4866 * @retval HAL status
\r
4868 static HAL_StatusTypeDef I2C_Master_ISR_DMA(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags, uint32_t ITSources)
\r
4870 uint16_t devaddress;
\r
4871 uint32_t xfermode;
\r
4873 /* Process Locked */
\r
4876 if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_AF) != RESET) && (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_NACKI) != RESET))
\r
4878 /* Clear NACK Flag */
\r
4879 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
\r
4881 /* Set corresponding Error Code */
\r
4882 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
\r
4884 /* No need to generate STOP, it is automatically done */
\r
4885 /* But enable STOP interrupt, to treat it */
\r
4886 /* Error callback will be send during stop flag treatment */
\r
4887 I2C_Enable_IRQ(hi2c, I2C_XFER_CPLT_IT);
\r
4889 /* Flush TX register */
\r
4890 I2C_Flush_TXDR(hi2c);
\r
4892 else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_TCR) != RESET) && (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TCI) != RESET))
\r
4894 /* Disable TC interrupt */
\r
4895 __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_TCI);
\r
4897 if (hi2c->XferCount != 0U)
\r
4899 /* Recover Slave address */
\r
4900 devaddress = (uint16_t)(hi2c->Instance->CR2 & I2C_CR2_SADD);
\r
4902 /* Prepare the new XferSize to transfer */
\r
4903 if (hi2c->XferCount > MAX_NBYTE_SIZE)
\r
4905 hi2c->XferSize = MAX_NBYTE_SIZE;
\r
4906 xfermode = I2C_RELOAD_MODE;
\r
4910 hi2c->XferSize = hi2c->XferCount;
\r
4911 if (hi2c->XferOptions != I2C_NO_OPTION_FRAME)
\r
4913 xfermode = hi2c->XferOptions;
\r
4917 xfermode = I2C_AUTOEND_MODE;
\r
4921 /* Set the new XferSize in Nbytes register */
\r
4922 I2C_TransferConfig(hi2c, devaddress, (uint8_t)hi2c->XferSize, xfermode, I2C_NO_STARTSTOP);
\r
4924 /* Update XferCount value */
\r
4925 hi2c->XferCount -= hi2c->XferSize;
\r
4927 /* Enable DMA Request */
\r
4928 if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
\r
4930 hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
\r
4934 hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
\r
4939 /* Call TxCpltCallback() if no stop mode is set */
\r
4940 if (I2C_GET_STOP_MODE(hi2c) != I2C_AUTOEND_MODE)
\r
4942 /* Call I2C Master Sequential complete process */
\r
4943 I2C_ITMasterSeqCplt(hi2c);
\r
4947 /* Wrong size Status regarding TCR flag event */
\r
4948 /* Call the corresponding callback to inform upper layer of End of Transfer */
\r
4949 I2C_ITError(hi2c, HAL_I2C_ERROR_SIZE);
\r
4953 else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_TC) != RESET) && (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TCI) != RESET))
\r
4955 if (hi2c->XferCount == 0U)
\r
4957 if (I2C_GET_STOP_MODE(hi2c) != I2C_AUTOEND_MODE)
\r
4959 /* Generate a stop condition in case of no transfer option */
\r
4960 if (hi2c->XferOptions == I2C_NO_OPTION_FRAME)
\r
4962 /* Generate Stop */
\r
4963 hi2c->Instance->CR2 |= I2C_CR2_STOP;
\r
4967 /* Call I2C Master Sequential complete process */
\r
4968 I2C_ITMasterSeqCplt(hi2c);
\r
4974 /* Wrong size Status regarding TC flag event */
\r
4975 /* Call the corresponding callback to inform upper layer of End of Transfer */
\r
4976 I2C_ITError(hi2c, HAL_I2C_ERROR_SIZE);
\r
4979 else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_STOPF) != RESET) && (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_STOPI) != RESET))
\r
4981 /* Call I2C Master complete process */
\r
4982 I2C_ITMasterCplt(hi2c, ITFlags);
\r
4986 /* Nothing to do */
\r
4989 /* Process Unlocked */
\r
4990 __HAL_UNLOCK(hi2c);
\r
4996 * @brief Interrupt Sub-Routine which handle the Interrupt Flags Slave Mode with DMA.
\r
4997 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
4998 * the configuration information for the specified I2C.
\r
4999 * @param ITFlags Interrupt flags to handle.
\r
5000 * @param ITSources Interrupt sources enabled.
\r
5001 * @retval HAL status
\r
5003 static HAL_StatusTypeDef I2C_Slave_ISR_DMA(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags, uint32_t ITSources)
\r
5005 uint32_t tmpoptions = hi2c->XferOptions;
\r
5006 uint32_t treatdmanack = 0U;
\r
5008 /* Process locked */
\r
5011 if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_AF) != RESET) && (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_NACKI) != RESET))
\r
5013 /* Check that I2C transfer finished */
\r
5014 /* if yes, normal use case, a NACK is sent by the MASTER when Transfer is finished */
\r
5015 /* Mean XferCount == 0 */
\r
5016 /* So clear Flag NACKF only */
\r
5017 if ((I2C_CHECK_IT_SOURCE(ITSources, I2C_CR1_TXDMAEN) != RESET) ||
\r
5018 (I2C_CHECK_IT_SOURCE(ITSources, I2C_CR1_RXDMAEN) != RESET))
\r
5020 /* Split check of hdmarx, for MISRA compliance */
\r
5021 if (hi2c->hdmarx != NULL)
\r
5023 if (I2C_CHECK_IT_SOURCE(ITSources, I2C_CR1_RXDMAEN) != RESET)
\r
5025 if (__HAL_DMA_GET_COUNTER(hi2c->hdmarx) == 0U)
\r
5027 treatdmanack = 1U;
\r
5032 /* Split check of hdmatx, for MISRA compliance */
\r
5033 if (hi2c->hdmatx != NULL)
\r
5035 if (I2C_CHECK_IT_SOURCE(ITSources, I2C_CR1_TXDMAEN) != RESET)
\r
5037 if (__HAL_DMA_GET_COUNTER(hi2c->hdmatx) == 0U)
\r
5039 treatdmanack = 1U;
\r
5044 if (treatdmanack == 1U)
\r
5046 if ((hi2c->State == HAL_I2C_STATE_LISTEN) && (tmpoptions == I2C_FIRST_AND_LAST_FRAME)) /* Same action must be done for (tmpoptions == I2C_LAST_FRAME) which removed for Warning[Pa134]: left and right operands are identical */
\r
5048 /* Call I2C Listen complete process */
\r
5049 I2C_ITListenCplt(hi2c, ITFlags);
\r
5051 else if ((hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN) && (tmpoptions != I2C_NO_OPTION_FRAME))
\r
5053 /* Clear NACK Flag */
\r
5054 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
\r
5056 /* Flush TX register */
\r
5057 I2C_Flush_TXDR(hi2c);
\r
5059 /* Last Byte is Transmitted */
\r
5060 /* Call I2C Slave Sequential complete process */
\r
5061 I2C_ITSlaveSeqCplt(hi2c);
\r
5065 /* Clear NACK Flag */
\r
5066 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
\r
5071 /* if no, error use case, a Non-Acknowledge of last Data is generated by the MASTER*/
\r
5072 /* Clear NACK Flag */
\r
5073 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
\r
5075 /* Set ErrorCode corresponding to a Non-Acknowledge */
\r
5076 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
\r
5078 if ((tmpoptions == I2C_FIRST_FRAME) || (tmpoptions == I2C_NEXT_FRAME))
\r
5080 /* Call the corresponding callback to inform upper layer of End of Transfer */
\r
5081 I2C_ITError(hi2c, hi2c->ErrorCode);
\r
5087 /* Only Clear NACK Flag, no DMA treatment is pending */
\r
5088 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
\r
5091 else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_ADDR) != RESET) && (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_ADDRI) != RESET))
\r
5093 I2C_ITAddrCplt(hi2c, ITFlags);
\r
5095 else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_STOPF) != RESET) && (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_STOPI) != RESET))
\r
5097 /* Call I2C Slave complete process */
\r
5098 I2C_ITSlaveCplt(hi2c, ITFlags);
\r
5102 /* Nothing to do */
\r
5105 /* Process Unlocked */
\r
5106 __HAL_UNLOCK(hi2c);
\r
5112 * @brief Master sends target device address followed by internal memory address for write request.
\r
5113 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
5114 * the configuration information for the specified I2C.
\r
5115 * @param DevAddress Target device address: The device 7 bits address value
\r
5116 * in datasheet must be shifted to the left before calling the interface
\r
5117 * @param MemAddress Internal memory address
\r
5118 * @param MemAddSize Size of internal memory address
\r
5119 * @param Timeout Timeout duration
\r
5120 * @param Tickstart Tick start value
\r
5121 * @retval HAL status
\r
5123 static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart)
\r
5125 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)MemAddSize, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);
\r
5127 /* Wait until TXIS flag is set */
\r
5128 if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
\r
5133 /* If Memory address size is 8Bit */
\r
5134 if (MemAddSize == I2C_MEMADD_SIZE_8BIT)
\r
5136 /* Send Memory Address */
\r
5137 hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress);
\r
5139 /* If Memory address size is 16Bit */
\r
5142 /* Send MSB of Memory Address */
\r
5143 hi2c->Instance->TXDR = I2C_MEM_ADD_MSB(MemAddress);
\r
5145 /* Wait until TXIS flag is set */
\r
5146 if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
\r
5151 /* Send LSB of Memory Address */
\r
5152 hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress);
\r
5155 /* Wait until TCR flag is set */
\r
5156 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout, Tickstart) != HAL_OK)
\r
5165 * @brief Master sends target device address followed by internal memory address for read request.
\r
5166 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
5167 * the configuration information for the specified I2C.
\r
5168 * @param DevAddress Target device address: The device 7 bits address value
\r
5169 * in datasheet must be shifted to the left before calling the interface
\r
5170 * @param MemAddress Internal memory address
\r
5171 * @param MemAddSize Size of internal memory address
\r
5172 * @param Timeout Timeout duration
\r
5173 * @param Tickstart Tick start value
\r
5174 * @retval HAL status
\r
5176 static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart)
\r
5178 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)MemAddSize, I2C_SOFTEND_MODE, I2C_GENERATE_START_WRITE);
\r
5180 /* Wait until TXIS flag is set */
\r
5181 if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
\r
5186 /* If Memory address size is 8Bit */
\r
5187 if (MemAddSize == I2C_MEMADD_SIZE_8BIT)
\r
5189 /* Send Memory Address */
\r
5190 hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress);
\r
5192 /* If Memory address size is 16Bit */
\r
5195 /* Send MSB of Memory Address */
\r
5196 hi2c->Instance->TXDR = I2C_MEM_ADD_MSB(MemAddress);
\r
5198 /* Wait until TXIS flag is set */
\r
5199 if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
\r
5204 /* Send LSB of Memory Address */
\r
5205 hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress);
\r
5208 /* Wait until TC flag is set */
\r
5209 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TC, RESET, Timeout, Tickstart) != HAL_OK)
\r
5218 * @brief I2C Address complete process callback.
\r
5219 * @param hi2c I2C handle.
\r
5220 * @param ITFlags Interrupt flags to handle.
\r
5223 static void I2C_ITAddrCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags)
\r
5225 uint8_t transferdirection;
\r
5226 uint16_t slaveaddrcode;
\r
5227 uint16_t ownadd1code;
\r
5228 uint16_t ownadd2code;
\r
5230 /* Prevent unused argument(s) compilation warning */
\r
5233 /* In case of Listen state, need to inform upper layer of address match code event */
\r
5234 if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
\r
5236 transferdirection = I2C_GET_DIR(hi2c);
\r
5237 slaveaddrcode = I2C_GET_ADDR_MATCH(hi2c);
\r
5238 ownadd1code = I2C_GET_OWN_ADDRESS1(hi2c);
\r
5239 ownadd2code = I2C_GET_OWN_ADDRESS2(hi2c);
\r
5241 /* If 10bits addressing mode is selected */
\r
5242 if (hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT)
\r
5244 if ((slaveaddrcode & SlaveAddr_MSK) == ((ownadd1code >> SlaveAddr_SHIFT) & SlaveAddr_MSK))
\r
5246 slaveaddrcode = ownadd1code;
\r
5247 hi2c->AddrEventCount++;
\r
5248 if (hi2c->AddrEventCount == 2U)
\r
5250 /* Reset Address Event counter */
\r
5251 hi2c->AddrEventCount = 0U;
\r
5253 /* Clear ADDR flag */
\r
5254 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
\r
5256 /* Process Unlocked */
\r
5257 __HAL_UNLOCK(hi2c);
\r
5259 /* Call Slave Addr callback */
\r
5260 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
\r
5261 hi2c->AddrCallback(hi2c, transferdirection, slaveaddrcode);
\r
5263 HAL_I2C_AddrCallback(hi2c, transferdirection, slaveaddrcode);
\r
5264 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
\r
5269 slaveaddrcode = ownadd2code;
\r
5271 /* Disable ADDR Interrupts */
\r
5272 I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT);
\r
5274 /* Process Unlocked */
\r
5275 __HAL_UNLOCK(hi2c);
\r
5277 /* Call Slave Addr callback */
\r
5278 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
\r
5279 hi2c->AddrCallback(hi2c, transferdirection, slaveaddrcode);
\r
5281 HAL_I2C_AddrCallback(hi2c, transferdirection, slaveaddrcode);
\r
5282 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
\r
5285 /* else 7 bits addressing mode is selected */
\r
5288 /* Disable ADDR Interrupts */
\r
5289 I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT);
\r
5291 /* Process Unlocked */
\r
5292 __HAL_UNLOCK(hi2c);
\r
5294 /* Call Slave Addr callback */
\r
5295 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
\r
5296 hi2c->AddrCallback(hi2c, transferdirection, slaveaddrcode);
\r
5298 HAL_I2C_AddrCallback(hi2c, transferdirection, slaveaddrcode);
\r
5299 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
\r
5302 /* Else clear address flag only */
\r
5305 /* Clear ADDR flag */
\r
5306 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
\r
5308 /* Process Unlocked */
\r
5309 __HAL_UNLOCK(hi2c);
\r
5314 * @brief I2C Master sequential complete process.
\r
5315 * @param hi2c I2C handle.
\r
5318 static void I2C_ITMasterSeqCplt(I2C_HandleTypeDef *hi2c)
\r
5320 /* Reset I2C handle mode */
\r
5321 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
5323 /* No Generate Stop, to permit restart mode */
\r
5324 /* The stop will be done at the end of transfer, when I2C_AUTOEND_MODE enable */
\r
5325 if (hi2c->State == HAL_I2C_STATE_BUSY_TX)
\r
5327 hi2c->State = HAL_I2C_STATE_READY;
\r
5328 hi2c->PreviousState = I2C_STATE_MASTER_BUSY_TX;
\r
5329 hi2c->XferISR = NULL;
\r
5331 /* Disable Interrupts */
\r
5332 I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT);
\r
5334 /* Process Unlocked */
\r
5335 __HAL_UNLOCK(hi2c);
\r
5337 /* Call the corresponding callback to inform upper layer of End of Transfer */
\r
5338 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
\r
5339 hi2c->MasterTxCpltCallback(hi2c);
\r
5341 HAL_I2C_MasterTxCpltCallback(hi2c);
\r
5342 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
\r
5344 /* hi2c->State == HAL_I2C_STATE_BUSY_RX */
\r
5347 hi2c->State = HAL_I2C_STATE_READY;
\r
5348 hi2c->PreviousState = I2C_STATE_MASTER_BUSY_RX;
\r
5349 hi2c->XferISR = NULL;
\r
5351 /* Disable Interrupts */
\r
5352 I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT);
\r
5354 /* Process Unlocked */
\r
5355 __HAL_UNLOCK(hi2c);
\r
5357 /* Call the corresponding callback to inform upper layer of End of Transfer */
\r
5358 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
\r
5359 hi2c->MasterRxCpltCallback(hi2c);
\r
5361 HAL_I2C_MasterRxCpltCallback(hi2c);
\r
5362 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
\r
5367 * @brief I2C Slave sequential complete process.
\r
5368 * @param hi2c I2C handle.
\r
5371 static void I2C_ITSlaveSeqCplt(I2C_HandleTypeDef *hi2c)
\r
5373 /* Reset I2C handle mode */
\r
5374 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
5376 if (hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN)
\r
5378 /* Remove HAL_I2C_STATE_SLAVE_BUSY_TX, keep only HAL_I2C_STATE_LISTEN */
\r
5379 hi2c->State = HAL_I2C_STATE_LISTEN;
\r
5380 hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_TX;
\r
5382 /* Disable Interrupts */
\r
5383 I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT);
\r
5385 /* Process Unlocked */
\r
5386 __HAL_UNLOCK(hi2c);
\r
5388 /* Call the corresponding callback to inform upper layer of End of Transfer */
\r
5389 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
\r
5390 hi2c->SlaveTxCpltCallback(hi2c);
\r
5392 HAL_I2C_SlaveTxCpltCallback(hi2c);
\r
5393 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
\r
5396 else if (hi2c->State == HAL_I2C_STATE_BUSY_RX_LISTEN)
\r
5398 /* Remove HAL_I2C_STATE_SLAVE_BUSY_RX, keep only HAL_I2C_STATE_LISTEN */
\r
5399 hi2c->State = HAL_I2C_STATE_LISTEN;
\r
5400 hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_RX;
\r
5402 /* Disable Interrupts */
\r
5403 I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT);
\r
5405 /* Process Unlocked */
\r
5406 __HAL_UNLOCK(hi2c);
\r
5408 /* Call the corresponding callback to inform upper layer of End of Transfer */
\r
5409 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
\r
5410 hi2c->SlaveRxCpltCallback(hi2c);
\r
5412 HAL_I2C_SlaveRxCpltCallback(hi2c);
\r
5413 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
\r
5417 /* Nothing to do */
\r
5422 * @brief I2C Master complete process.
\r
5423 * @param hi2c I2C handle.
\r
5424 * @param ITFlags Interrupt flags to handle.
\r
5427 static void I2C_ITMasterCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags)
\r
5429 uint32_t tmperror;
\r
5431 /* Clear STOP Flag */
\r
5432 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
\r
5434 /* Clear Configuration Register 2 */
\r
5435 I2C_RESET_CR2(hi2c);
\r
5437 /* Reset handle parameters */
\r
5438 hi2c->PreviousState = I2C_STATE_NONE;
\r
5439 hi2c->XferISR = NULL;
\r
5440 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
\r
5442 if (I2C_CHECK_FLAG(ITFlags, I2C_FLAG_AF) != RESET)
\r
5444 /* Clear NACK Flag */
\r
5445 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
\r
5447 /* Set acknowledge error code */
\r
5448 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
\r
5451 /* Flush TX register */
\r
5452 I2C_Flush_TXDR(hi2c);
\r
5454 /* Disable Interrupts */
\r
5455 I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT | I2C_XFER_RX_IT);
\r
5457 /* Store current volatile hi2c->ErrorCode, misra rule */
\r
5458 tmperror = hi2c->ErrorCode;
\r
5460 /* Call the corresponding callback to inform upper layer of End of Transfer */
\r
5461 if ((hi2c->State == HAL_I2C_STATE_ABORT) || (tmperror != HAL_I2C_ERROR_NONE))
\r
5463 /* Call the corresponding callback to inform upper layer of End of Transfer */
\r
5464 I2C_ITError(hi2c, hi2c->ErrorCode);
\r
5466 /* hi2c->State == HAL_I2C_STATE_BUSY_TX */
\r
5467 else if (hi2c->State == HAL_I2C_STATE_BUSY_TX)
\r
5469 hi2c->State = HAL_I2C_STATE_READY;
\r
5471 if (hi2c->Mode == HAL_I2C_MODE_MEM)
\r
5473 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
5475 /* Process Unlocked */
\r
5476 __HAL_UNLOCK(hi2c);
\r
5478 /* Call the corresponding callback to inform upper layer of End of Transfer */
\r
5479 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
\r
5480 hi2c->MemTxCpltCallback(hi2c);
\r
5482 HAL_I2C_MemTxCpltCallback(hi2c);
\r
5483 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
\r
5487 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
5489 /* Process Unlocked */
\r
5490 __HAL_UNLOCK(hi2c);
\r
5492 /* Call the corresponding callback to inform upper layer of End of Transfer */
\r
5493 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
\r
5494 hi2c->MasterTxCpltCallback(hi2c);
\r
5496 HAL_I2C_MasterTxCpltCallback(hi2c);
\r
5497 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
\r
5500 /* hi2c->State == HAL_I2C_STATE_BUSY_RX */
\r
5501 else if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
\r
5503 hi2c->State = HAL_I2C_STATE_READY;
\r
5505 if (hi2c->Mode == HAL_I2C_MODE_MEM)
\r
5507 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
5509 /* Process Unlocked */
\r
5510 __HAL_UNLOCK(hi2c);
\r
5512 /* Call the corresponding callback to inform upper layer of End of Transfer */
\r
5513 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
\r
5514 hi2c->MemRxCpltCallback(hi2c);
\r
5516 HAL_I2C_MemRxCpltCallback(hi2c);
\r
5517 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
\r
5521 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
5523 /* Process Unlocked */
\r
5524 __HAL_UNLOCK(hi2c);
\r
5526 /* Call the corresponding callback to inform upper layer of End of Transfer */
\r
5527 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
\r
5528 hi2c->MasterRxCpltCallback(hi2c);
\r
5530 HAL_I2C_MasterRxCpltCallback(hi2c);
\r
5531 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
\r
5536 /* Nothing to do */
\r
5541 * @brief I2C Slave complete process.
\r
5542 * @param hi2c I2C handle.
\r
5543 * @param ITFlags Interrupt flags to handle.
\r
5546 static void I2C_ITSlaveCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags)
\r
5548 uint32_t tmpcr1value = READ_REG(hi2c->Instance->CR1);
\r
5549 uint32_t tmpITFlags = ITFlags;
\r
5551 /* Clear STOP Flag */
\r
5552 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
\r
5554 /* Disable all interrupts */
\r
5555 I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_TX_IT | I2C_XFER_RX_IT);
\r
5557 /* Disable Address Acknowledge */
\r
5558 hi2c->Instance->CR2 |= I2C_CR2_NACK;
\r
5560 /* Clear Configuration Register 2 */
\r
5561 I2C_RESET_CR2(hi2c);
\r
5563 /* Flush TX register */
\r
5564 I2C_Flush_TXDR(hi2c);
\r
5566 /* If a DMA is ongoing, Update handle size context */
\r
5567 if (I2C_CHECK_IT_SOURCE(tmpcr1value, I2C_CR1_TXDMAEN) != RESET)
\r
5569 if (hi2c->hdmatx != NULL)
\r
5571 hi2c->XferCount = (uint16_t)__HAL_DMA_GET_COUNTER(hi2c->hdmatx);
\r
5574 else if (I2C_CHECK_IT_SOURCE(tmpcr1value, I2C_CR1_RXDMAEN) != RESET)
\r
5576 if (hi2c->hdmarx != NULL)
\r
5578 hi2c->XferCount = (uint16_t)__HAL_DMA_GET_COUNTER(hi2c->hdmarx);
\r
5586 /* Store Last receive data if any */
\r
5587 if (I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_RXNE) != RESET)
\r
5589 /* Remove RXNE flag on temporary variable as read done */
\r
5590 tmpITFlags &= ~I2C_FLAG_RXNE;
\r
5592 /* Read data from RXDR */
\r
5593 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR;
\r
5595 /* Increment Buffer pointer */
\r
5598 if ((hi2c->XferSize > 0U))
\r
5601 hi2c->XferCount--;
\r
5605 /* All data are not transferred, so set error code accordingly */
\r
5606 if (hi2c->XferCount != 0U)
\r
5608 /* Set ErrorCode corresponding to a Non-Acknowledge */
\r
5609 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
\r
5612 hi2c->PreviousState = I2C_STATE_NONE;
\r
5613 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
5614 hi2c->XferISR = NULL;
\r
5616 if (hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
\r
5618 /* Call the corresponding callback to inform upper layer of End of Transfer */
\r
5619 I2C_ITError(hi2c, hi2c->ErrorCode);
\r
5621 /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
\r
5622 if (hi2c->State == HAL_I2C_STATE_LISTEN)
\r
5624 /* Call I2C Listen complete process */
\r
5625 I2C_ITListenCplt(hi2c, tmpITFlags);
\r
5628 else if (hi2c->XferOptions != I2C_NO_OPTION_FRAME)
\r
5630 /* Call the Sequential Complete callback, to inform upper layer of the end of Tranfer */
\r
5631 I2C_ITSlaveSeqCplt(hi2c);
\r
5633 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
\r
5634 hi2c->State = HAL_I2C_STATE_READY;
\r
5636 /* Process Unlocked */
\r
5637 __HAL_UNLOCK(hi2c);
\r
5639 /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
\r
5640 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
\r
5641 hi2c->ListenCpltCallback(hi2c);
\r
5643 HAL_I2C_ListenCpltCallback(hi2c);
\r
5644 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
\r
5646 /* Call the corresponding callback to inform upper layer of End of Transfer */
\r
5647 else if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
\r
5649 hi2c->State = HAL_I2C_STATE_READY;
\r
5651 /* Process Unlocked */
\r
5652 __HAL_UNLOCK(hi2c);
\r
5654 /* Call the corresponding callback to inform upper layer of End of Transfer */
\r
5655 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
\r
5656 hi2c->SlaveRxCpltCallback(hi2c);
\r
5658 HAL_I2C_SlaveRxCpltCallback(hi2c);
\r
5659 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
\r
5663 hi2c->State = HAL_I2C_STATE_READY;
\r
5665 /* Process Unlocked */
\r
5666 __HAL_UNLOCK(hi2c);
\r
5668 /* Call the corresponding callback to inform upper layer of End of Transfer */
\r
5669 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
\r
5670 hi2c->SlaveTxCpltCallback(hi2c);
\r
5672 HAL_I2C_SlaveTxCpltCallback(hi2c);
\r
5673 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
\r
5678 * @brief I2C Listen complete process.
\r
5679 * @param hi2c I2C handle.
\r
5680 * @param ITFlags Interrupt flags to handle.
\r
5683 static void I2C_ITListenCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags)
\r
5685 /* Reset handle parameters */
\r
5686 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
\r
5687 hi2c->PreviousState = I2C_STATE_NONE;
\r
5688 hi2c->State = HAL_I2C_STATE_READY;
\r
5689 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
5690 hi2c->XferISR = NULL;
\r
5692 /* Store Last receive data if any */
\r
5693 if (I2C_CHECK_FLAG(ITFlags, I2C_FLAG_RXNE) != RESET)
\r
5695 /* Read data from RXDR */
\r
5696 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR;
\r
5698 /* Increment Buffer pointer */
\r
5701 if ((hi2c->XferSize > 0U))
\r
5704 hi2c->XferCount--;
\r
5706 /* Set ErrorCode corresponding to a Non-Acknowledge */
\r
5707 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
\r
5711 /* Disable all Interrupts*/
\r
5712 I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_RX_IT | I2C_XFER_TX_IT);
\r
5714 /* Clear NACK Flag */
\r
5715 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
\r
5717 /* Process Unlocked */
\r
5718 __HAL_UNLOCK(hi2c);
\r
5720 /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
\r
5721 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
\r
5722 hi2c->ListenCpltCallback(hi2c);
\r
5724 HAL_I2C_ListenCpltCallback(hi2c);
\r
5725 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
\r
5729 * @brief I2C interrupts error process.
\r
5730 * @param hi2c I2C handle.
\r
5731 * @param ErrorCode Error code to handle.
\r
5734 static void I2C_ITError(I2C_HandleTypeDef *hi2c, uint32_t ErrorCode)
\r
5736 HAL_I2C_StateTypeDef tmpstate = hi2c->State;
\r
5738 /* Reset handle parameters */
\r
5739 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
5740 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
\r
5741 hi2c->XferCount = 0U;
\r
5743 /* Set new error code */
\r
5744 hi2c->ErrorCode |= ErrorCode;
\r
5746 /* Disable Interrupts */
\r
5747 if ((tmpstate == HAL_I2C_STATE_LISTEN) ||
\r
5748 (tmpstate == HAL_I2C_STATE_BUSY_TX_LISTEN) ||
\r
5749 (tmpstate == HAL_I2C_STATE_BUSY_RX_LISTEN))
\r
5751 /* Disable all interrupts, except interrupts related to LISTEN state */
\r
5752 I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT | I2C_XFER_TX_IT);
\r
5754 /* keep HAL_I2C_STATE_LISTEN if set */
\r
5755 hi2c->State = HAL_I2C_STATE_LISTEN;
\r
5756 hi2c->PreviousState = I2C_STATE_NONE;
\r
5757 hi2c->XferISR = I2C_Slave_ISR_IT;
\r
5761 /* Disable all interrupts */
\r
5762 I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_RX_IT | I2C_XFER_TX_IT);
\r
5764 /* If state is an abort treatment on goind, don't change state */
\r
5765 /* This change will be do later */
\r
5766 if (hi2c->State != HAL_I2C_STATE_ABORT)
\r
5768 /* Set HAL_I2C_STATE_READY */
\r
5769 hi2c->State = HAL_I2C_STATE_READY;
\r
5771 hi2c->PreviousState = I2C_STATE_NONE;
\r
5772 hi2c->XferISR = NULL;
\r
5775 /* Abort DMA TX transfer if any */
\r
5776 if ((hi2c->Instance->CR1 & I2C_CR1_TXDMAEN) == I2C_CR1_TXDMAEN)
\r
5778 hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
\r
5780 if (hi2c->hdmatx != NULL)
\r
5782 /* Set the I2C DMA Abort callback :
\r
5783 will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
\r
5784 hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort;
\r
5786 /* Process Unlocked */
\r
5787 __HAL_UNLOCK(hi2c);
\r
5789 /* Abort DMA TX */
\r
5790 if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK)
\r
5792 /* Call Directly XferAbortCallback function in case of error */
\r
5793 hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx);
\r
5797 /* Abort DMA RX transfer if any */
\r
5798 else if ((hi2c->Instance->CR1 & I2C_CR1_RXDMAEN) == I2C_CR1_RXDMAEN)
\r
5800 hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
\r
5802 if (hi2c->hdmarx != NULL)
\r
5804 /* Set the I2C DMA Abort callback :
\r
5805 will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
\r
5806 hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort;
\r
5808 /* Process Unlocked */
\r
5809 __HAL_UNLOCK(hi2c);
\r
5811 /* Abort DMA RX */
\r
5812 if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK)
\r
5814 /* Call Directly hi2c->hdmarx->XferAbortCallback function in case of error */
\r
5815 hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx);
\r
5819 else if (hi2c->State == HAL_I2C_STATE_ABORT)
\r
5821 hi2c->State = HAL_I2C_STATE_READY;
\r
5823 /* Process Unlocked */
\r
5824 __HAL_UNLOCK(hi2c);
\r
5826 /* Call the corresponding callback to inform upper layer of End of Transfer */
\r
5827 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
\r
5828 hi2c->AbortCpltCallback(hi2c);
\r
5830 HAL_I2C_AbortCpltCallback(hi2c);
\r
5831 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
\r
5835 /* Process Unlocked */
\r
5836 __HAL_UNLOCK(hi2c);
\r
5838 /* Call the corresponding callback to inform upper layer of End of Transfer */
\r
5839 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
\r
5840 hi2c->ErrorCallback(hi2c);
\r
5842 HAL_I2C_ErrorCallback(hi2c);
\r
5843 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
\r
5848 * @brief I2C Tx data register flush process.
\r
5849 * @param hi2c I2C handle.
\r
5852 static void I2C_Flush_TXDR(I2C_HandleTypeDef *hi2c)
\r
5854 /* If a pending TXIS flag is set */
\r
5855 /* Write a dummy data in TXDR to clear it */
\r
5856 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) != RESET)
\r
5858 hi2c->Instance->TXDR = 0x00U;
\r
5861 /* Flush TX register if not empty */
\r
5862 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXE) == RESET)
\r
5864 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_TXE);
\r
5869 * @brief DMA I2C master transmit process complete callback.
\r
5870 * @param hdma DMA handle
\r
5873 static void I2C_DMAMasterTransmitCplt(DMA_HandleTypeDef *hdma)
\r
5875 I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
\r
5877 /* Disable DMA Request */
\r
5878 hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
\r
5880 /* If last transfer, enable STOP interrupt */
\r
5881 if (hi2c->XferCount == 0U)
\r
5883 /* Enable STOP interrupt */
\r
5884 I2C_Enable_IRQ(hi2c, I2C_XFER_CPLT_IT);
\r
5886 /* else prepare a new DMA transfer and enable TCReload interrupt */
\r
5889 /* Update Buffer pointer */
\r
5890 hi2c->pBuffPtr += hi2c->XferSize;
\r
5892 /* Set the XferSize to transfer */
\r
5893 if (hi2c->XferCount > MAX_NBYTE_SIZE)
\r
5895 hi2c->XferSize = MAX_NBYTE_SIZE;
\r
5899 hi2c->XferSize = hi2c->XferCount;
\r
5902 /* Enable the DMA channel */
\r
5903 if (HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize) != HAL_OK)
\r
5905 /* Call the corresponding callback to inform upper layer of End of Transfer */
\r
5906 I2C_ITError(hi2c, HAL_I2C_ERROR_DMA);
\r
5910 /* Enable TC interrupts */
\r
5911 I2C_Enable_IRQ(hi2c, I2C_XFER_RELOAD_IT);
\r
5917 * @brief DMA I2C slave transmit process complete callback.
\r
5918 * @param hdma DMA handle
\r
5921 static void I2C_DMASlaveTransmitCplt(DMA_HandleTypeDef *hdma)
\r
5923 I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
\r
5924 uint32_t tmpoptions = hi2c->XferOptions;
\r
5926 if ((tmpoptions == I2C_NEXT_FRAME) || (tmpoptions == I2C_FIRST_FRAME))
\r
5928 /* Disable DMA Request */
\r
5929 hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
\r
5931 /* Last Byte is Transmitted */
\r
5932 /* Call I2C Slave Sequential complete process */
\r
5933 I2C_ITSlaveSeqCplt(hi2c);
\r
5937 /* No specific action, Master fully manage the generation of STOP condition */
\r
5938 /* Mean that this generation can arrive at any time, at the end or during DMA process */
\r
5939 /* So STOP condition should be manage through Interrupt treatment */
\r
5944 * @brief DMA I2C master receive process complete callback.
\r
5945 * @param hdma DMA handle
\r
5948 static void I2C_DMAMasterReceiveCplt(DMA_HandleTypeDef *hdma)
\r
5950 I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
\r
5952 /* Disable DMA Request */
\r
5953 hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
\r
5955 /* If last transfer, enable STOP interrupt */
\r
5956 if (hi2c->XferCount == 0U)
\r
5958 /* Enable STOP interrupt */
\r
5959 I2C_Enable_IRQ(hi2c, I2C_XFER_CPLT_IT);
\r
5961 /* else prepare a new DMA transfer and enable TCReload interrupt */
\r
5964 /* Update Buffer pointer */
\r
5965 hi2c->pBuffPtr += hi2c->XferSize;
\r
5967 /* Set the XferSize to transfer */
\r
5968 if (hi2c->XferCount > MAX_NBYTE_SIZE)
\r
5970 hi2c->XferSize = MAX_NBYTE_SIZE;
\r
5974 hi2c->XferSize = hi2c->XferCount;
\r
5977 /* Enable the DMA channel */
\r
5978 if (HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize) != HAL_OK)
\r
5980 /* Call the corresponding callback to inform upper layer of End of Transfer */
\r
5981 I2C_ITError(hi2c, HAL_I2C_ERROR_DMA);
\r
5985 /* Enable TC interrupts */
\r
5986 I2C_Enable_IRQ(hi2c, I2C_XFER_RELOAD_IT);
\r
5992 * @brief DMA I2C slave receive process complete callback.
\r
5993 * @param hdma DMA handle
\r
5996 static void I2C_DMASlaveReceiveCplt(DMA_HandleTypeDef *hdma)
\r
5998 I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
\r
5999 uint32_t tmpoptions = hi2c->XferOptions;
\r
6001 if ((__HAL_DMA_GET_COUNTER(hi2c->hdmarx) == 0U) && \
\r
6002 (tmpoptions != I2C_NO_OPTION_FRAME))
\r
6004 /* Disable DMA Request */
\r
6005 hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
\r
6007 /* Call I2C Slave Sequential complete process */
\r
6008 I2C_ITSlaveSeqCplt(hi2c);
\r
6012 /* No specific action, Master fully manage the generation of STOP condition */
\r
6013 /* Mean that this generation can arrive at any time, at the end or during DMA process */
\r
6014 /* So STOP condition should be manage through Interrupt treatment */
\r
6019 * @brief DMA I2C communication error callback.
\r
6020 * @param hdma DMA handle
\r
6023 static void I2C_DMAError(DMA_HandleTypeDef *hdma)
\r
6025 I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
\r
6027 /* Disable Acknowledge */
\r
6028 hi2c->Instance->CR2 |= I2C_CR2_NACK;
\r
6030 /* Call the corresponding callback to inform upper layer of End of Transfer */
\r
6031 I2C_ITError(hi2c, HAL_I2C_ERROR_DMA);
\r
6035 * @brief DMA I2C communication abort callback
\r
6036 * (To be called at end of DMA Abort procedure).
\r
6037 * @param hdma DMA handle.
\r
6040 static void I2C_DMAAbort(DMA_HandleTypeDef *hdma)
\r
6042 I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
\r
6044 /* Reset AbortCpltCallback */
\r
6045 hi2c->hdmatx->XferAbortCallback = NULL;
\r
6046 hi2c->hdmarx->XferAbortCallback = NULL;
\r
6048 /* Check if come from abort from user */
\r
6049 if (hi2c->State == HAL_I2C_STATE_ABORT)
\r
6051 hi2c->State = HAL_I2C_STATE_READY;
\r
6053 /* Call the corresponding callback to inform upper layer of End of Transfer */
\r
6054 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
\r
6055 hi2c->AbortCpltCallback(hi2c);
\r
6057 HAL_I2C_AbortCpltCallback(hi2c);
\r
6058 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
\r
6062 /* Call the corresponding callback to inform upper layer of End of Transfer */
\r
6063 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
\r
6064 hi2c->ErrorCallback(hi2c);
\r
6066 HAL_I2C_ErrorCallback(hi2c);
\r
6067 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
\r
6072 * @brief This function handles I2C Communication Timeout.
\r
6073 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
6074 * the configuration information for the specified I2C.
\r
6075 * @param Flag Specifies the I2C flag to check.
\r
6076 * @param Status The new Flag status (SET or RESET).
\r
6077 * @param Timeout Timeout duration
\r
6078 * @param Tickstart Tick start value
\r
6079 * @retval HAL status
\r
6081 static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout, uint32_t Tickstart)
\r
6083 while (__HAL_I2C_GET_FLAG(hi2c, Flag) == Status)
\r
6085 /* Check for the Timeout */
\r
6086 if (Timeout != HAL_MAX_DELAY)
\r
6088 if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
\r
6090 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
\r
6091 hi2c->State = HAL_I2C_STATE_READY;
\r
6092 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
6094 /* Process Unlocked */
\r
6095 __HAL_UNLOCK(hi2c);
\r
6104 * @brief This function handles I2C Communication Timeout for specific usage of TXIS flag.
\r
6105 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
6106 * the configuration information for the specified I2C.
\r
6107 * @param Timeout Timeout duration
\r
6108 * @param Tickstart Tick start value
\r
6109 * @retval HAL status
\r
6111 static HAL_StatusTypeDef I2C_WaitOnTXISFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart)
\r
6113 while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == RESET)
\r
6115 /* Check if a NACK is detected */
\r
6116 if (I2C_IsAcknowledgeFailed(hi2c, Timeout, Tickstart) != HAL_OK)
\r
6121 /* Check for the Timeout */
\r
6122 if (Timeout != HAL_MAX_DELAY)
\r
6124 if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
\r
6126 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
\r
6127 hi2c->State = HAL_I2C_STATE_READY;
\r
6128 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
6130 /* Process Unlocked */
\r
6131 __HAL_UNLOCK(hi2c);
\r
6141 * @brief This function handles I2C Communication Timeout for specific usage of STOP flag.
\r
6142 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
6143 * the configuration information for the specified I2C.
\r
6144 * @param Timeout Timeout duration
\r
6145 * @param Tickstart Tick start value
\r
6146 * @retval HAL status
\r
6148 static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart)
\r
6150 while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET)
\r
6152 /* Check if a NACK is detected */
\r
6153 if (I2C_IsAcknowledgeFailed(hi2c, Timeout, Tickstart) != HAL_OK)
\r
6158 /* Check for the Timeout */
\r
6159 if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
\r
6161 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
\r
6162 hi2c->State = HAL_I2C_STATE_READY;
\r
6163 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
6165 /* Process Unlocked */
\r
6166 __HAL_UNLOCK(hi2c);
\r
6175 * @brief This function handles I2C Communication Timeout for specific usage of RXNE flag.
\r
6176 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
6177 * the configuration information for the specified I2C.
\r
6178 * @param Timeout Timeout duration
\r
6179 * @param Tickstart Tick start value
\r
6180 * @retval HAL status
\r
6182 static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart)
\r
6184 while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == RESET)
\r
6186 /* Check if a NACK is detected */
\r
6187 if (I2C_IsAcknowledgeFailed(hi2c, Timeout, Tickstart) != HAL_OK)
\r
6192 /* Check if a STOPF is detected */
\r
6193 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
\r
6195 /* Check if an RXNE is pending */
\r
6196 /* Store Last receive data if any */
\r
6197 if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET) && (hi2c->XferSize > 0U))
\r
6199 /* Return HAL_OK */
\r
6200 /* The Reading of data from RXDR will be done in caller function */
\r
6205 /* Clear STOP Flag */
\r
6206 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
\r
6208 /* Clear Configuration Register 2 */
\r
6209 I2C_RESET_CR2(hi2c);
\r
6211 hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
\r
6212 hi2c->State = HAL_I2C_STATE_READY;
\r
6213 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
6215 /* Process Unlocked */
\r
6216 __HAL_UNLOCK(hi2c);
\r
6222 /* Check for the Timeout */
\r
6223 if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
\r
6225 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
\r
6226 hi2c->State = HAL_I2C_STATE_READY;
\r
6228 /* Process Unlocked */
\r
6229 __HAL_UNLOCK(hi2c);
\r
6238 * @brief This function handles Acknowledge failed detection during an I2C Communication.
\r
6239 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
6240 * the configuration information for the specified I2C.
\r
6241 * @param Timeout Timeout duration
\r
6242 * @param Tickstart Tick start value
\r
6243 * @retval HAL status
\r
6245 static HAL_StatusTypeDef I2C_IsAcknowledgeFailed(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart)
\r
6247 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
\r
6249 /* Wait until STOP Flag is reset */
\r
6250 /* AutoEnd should be initiate after AF */
\r
6251 while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET)
\r
6253 /* Check for the Timeout */
\r
6254 if (Timeout != HAL_MAX_DELAY)
\r
6256 if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
\r
6258 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
\r
6259 hi2c->State = HAL_I2C_STATE_READY;
\r
6260 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
6262 /* Process Unlocked */
\r
6263 __HAL_UNLOCK(hi2c);
\r
6270 /* Clear NACKF Flag */
\r
6271 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
\r
6273 /* Clear STOP Flag */
\r
6274 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
\r
6276 /* Flush TX register */
\r
6277 I2C_Flush_TXDR(hi2c);
\r
6279 /* Clear Configuration Register 2 */
\r
6280 I2C_RESET_CR2(hi2c);
\r
6282 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
\r
6283 hi2c->State = HAL_I2C_STATE_READY;
\r
6284 hi2c->Mode = HAL_I2C_MODE_NONE;
\r
6286 /* Process Unlocked */
\r
6287 __HAL_UNLOCK(hi2c);
\r
6295 * @brief Handles I2Cx communication when starting transfer or during transfer (TC or TCR flag are set).
\r
6296 * @param hi2c I2C handle.
\r
6297 * @param DevAddress Specifies the slave address to be programmed.
\r
6298 * @param Size Specifies the number of bytes to be programmed.
\r
6299 * This parameter must be a value between 0 and 255.
\r
6300 * @param Mode New state of the I2C START condition generation.
\r
6301 * This parameter can be one of the following values:
\r
6302 * @arg @ref I2C_RELOAD_MODE Enable Reload mode .
\r
6303 * @arg @ref I2C_AUTOEND_MODE Enable Automatic end mode.
\r
6304 * @arg @ref I2C_SOFTEND_MODE Enable Software end mode.
\r
6305 * @param Request New state of the I2C START condition generation.
\r
6306 * This parameter can be one of the following values:
\r
6307 * @arg @ref I2C_NO_STARTSTOP Don't Generate stop and start condition.
\r
6308 * @arg @ref I2C_GENERATE_STOP Generate stop condition (Size should be set to 0).
\r
6309 * @arg @ref I2C_GENERATE_START_READ Generate Restart for read request.
\r
6310 * @arg @ref I2C_GENERATE_START_WRITE Generate Restart for write request.
\r
6313 static void I2C_TransferConfig(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t Size, uint32_t Mode, uint32_t Request)
\r
6315 /* Check the parameters */
\r
6316 assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
\r
6317 assert_param(IS_TRANSFER_MODE(Mode));
\r
6318 assert_param(IS_TRANSFER_REQUEST(Request));
\r
6320 /* update CR2 register */
\r
6321 MODIFY_REG(hi2c->Instance->CR2, ((I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | (I2C_CR2_RD_WRN & (uint32_t)(Request >> (31U - I2C_CR2_RD_WRN_Pos))) | I2C_CR2_START | I2C_CR2_STOP)), \
\r
6322 (uint32_t)(((uint32_t)DevAddress & I2C_CR2_SADD) | (((uint32_t)Size << I2C_CR2_NBYTES_Pos) & I2C_CR2_NBYTES) | (uint32_t)Mode | (uint32_t)Request));
\r
6326 * @brief Manage the enabling of Interrupts.
\r
6327 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
6328 * the configuration information for the specified I2C.
\r
6329 * @param InterruptRequest Value of @ref I2C_Interrupt_configuration_definition.
\r
6332 static void I2C_Enable_IRQ(I2C_HandleTypeDef *hi2c, uint16_t InterruptRequest)
\r
6334 uint32_t tmpisr = 0U;
\r
6336 if ((hi2c->XferISR == I2C_Master_ISR_DMA) || \
\r
6337 (hi2c->XferISR == I2C_Slave_ISR_DMA))
\r
6339 if ((InterruptRequest & I2C_XFER_LISTEN_IT) == I2C_XFER_LISTEN_IT)
\r
6341 /* Enable ERR, STOP, NACK and ADDR interrupts */
\r
6342 tmpisr |= I2C_IT_ADDRI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ERRI;
\r
6345 if ((InterruptRequest & I2C_XFER_ERROR_IT) == I2C_XFER_ERROR_IT)
\r
6347 /* Enable ERR and NACK interrupts */
\r
6348 tmpisr |= I2C_IT_ERRI | I2C_IT_NACKI;
\r
6351 if ((InterruptRequest & I2C_XFER_CPLT_IT) == I2C_XFER_CPLT_IT)
\r
6353 /* Enable STOP interrupts */
\r
6354 tmpisr |= I2C_IT_STOPI;
\r
6357 if ((InterruptRequest & I2C_XFER_RELOAD_IT) == I2C_XFER_RELOAD_IT)
\r
6359 /* Enable TC interrupts */
\r
6360 tmpisr |= I2C_IT_TCI;
\r
6365 if ((InterruptRequest & I2C_XFER_LISTEN_IT) == I2C_XFER_LISTEN_IT)
\r
6367 /* Enable ERR, STOP, NACK, and ADDR interrupts */
\r
6368 tmpisr |= I2C_IT_ADDRI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ERRI;
\r
6371 if ((InterruptRequest & I2C_XFER_TX_IT) == I2C_XFER_TX_IT)
\r
6373 /* Enable ERR, TC, STOP, NACK and RXI interrupts */
\r
6374 tmpisr |= I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_TXI;
\r
6377 if ((InterruptRequest & I2C_XFER_RX_IT) == I2C_XFER_RX_IT)
\r
6379 /* Enable ERR, TC, STOP, NACK and TXI interrupts */
\r
6380 tmpisr |= I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_RXI;
\r
6383 if ((InterruptRequest & I2C_XFER_CPLT_IT) == I2C_XFER_CPLT_IT)
\r
6385 /* Enable STOP interrupts */
\r
6386 tmpisr |= I2C_IT_STOPI;
\r
6390 /* Enable interrupts only at the end */
\r
6391 /* to avoid the risk of I2C interrupt handle execution before */
\r
6392 /* all interrupts requested done */
\r
6393 __HAL_I2C_ENABLE_IT(hi2c, tmpisr);
\r
6397 * @brief Manage the disabling of Interrupts.
\r
6398 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
\r
6399 * the configuration information for the specified I2C.
\r
6400 * @param InterruptRequest Value of @ref I2C_Interrupt_configuration_definition.
\r
6403 static void I2C_Disable_IRQ(I2C_HandleTypeDef *hi2c, uint16_t InterruptRequest)
\r
6405 uint32_t tmpisr = 0U;
\r
6407 if ((InterruptRequest & I2C_XFER_TX_IT) == I2C_XFER_TX_IT)
\r
6409 /* Disable TC and TXI interrupts */
\r
6410 tmpisr |= I2C_IT_TCI | I2C_IT_TXI;
\r
6412 if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) != (uint32_t)HAL_I2C_STATE_LISTEN)
\r
6414 /* Disable NACK and STOP interrupts */
\r
6415 tmpisr |= I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ERRI;
\r
6419 if ((InterruptRequest & I2C_XFER_RX_IT) == I2C_XFER_RX_IT)
\r
6421 /* Disable TC and RXI interrupts */
\r
6422 tmpisr |= I2C_IT_TCI | I2C_IT_RXI;
\r
6424 if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) != (uint32_t)HAL_I2C_STATE_LISTEN)
\r
6426 /* Disable NACK and STOP interrupts */
\r
6427 tmpisr |= I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ERRI;
\r
6431 if ((InterruptRequest & I2C_XFER_LISTEN_IT) == I2C_XFER_LISTEN_IT)
\r
6433 /* Disable ADDR, NACK and STOP interrupts */
\r
6434 tmpisr |= I2C_IT_ADDRI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ERRI;
\r
6437 if ((InterruptRequest & I2C_XFER_ERROR_IT) == I2C_XFER_ERROR_IT)
\r
6439 /* Enable ERR and NACK interrupts */
\r
6440 tmpisr |= I2C_IT_ERRI | I2C_IT_NACKI;
\r
6443 if ((InterruptRequest & I2C_XFER_CPLT_IT) == I2C_XFER_CPLT_IT)
\r
6445 /* Enable STOP interrupts */
\r
6446 tmpisr |= I2C_IT_STOPI;
\r
6449 if ((InterruptRequest & I2C_XFER_RELOAD_IT) == I2C_XFER_RELOAD_IT)
\r
6451 /* Enable TC interrupts */
\r
6452 tmpisr |= I2C_IT_TCI;
\r
6455 /* Disable interrupts only at the end */
\r
6456 /* to avoid a breaking situation like at "t" time */
\r
6457 /* all disable interrupts request are not done */
\r
6458 __HAL_I2C_DISABLE_IT(hi2c, tmpisr);
\r
6462 * @brief Convert I2Cx OTHER_xxx XferOptions to functionnal XferOptions.
\r
6463 * @param hi2c I2C handle.
\r
6466 static void I2C_ConvertOtherXferOptions(I2C_HandleTypeDef *hi2c)
\r
6468 /* if user set XferOptions to I2C_OTHER_FRAME */
\r
6469 /* it request implicitly to generate a restart condition */
\r
6470 /* set XferOptions to I2C_FIRST_FRAME */
\r
6471 if (hi2c->XferOptions == I2C_OTHER_FRAME)
\r
6473 hi2c->XferOptions = I2C_FIRST_FRAME;
\r
6475 /* else if user set XferOptions to I2C_OTHER_AND_LAST_FRAME */
\r
6476 /* it request implicitly to generate a restart condition */
\r
6477 /* then generate a stop condition at the end of transfer */
\r
6478 /* set XferOptions to I2C_FIRST_AND_LAST_FRAME */
\r
6479 else if (hi2c->XferOptions == I2C_OTHER_AND_LAST_FRAME)
\r
6481 hi2c->XferOptions = I2C_FIRST_AND_LAST_FRAME;
\r
6485 /* Nothing to do */
\r
6493 #endif /* HAL_I2C_MODULE_ENABLED */
\r
6502 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
\r