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