2 ******************************************************************************
\r
3 * @file stm32l4xx_hal_i2c.h
\r
4 * @author MCD Application Team
\r
5 * @brief Header file of I2C HAL module.
\r
6 ******************************************************************************
\r
9 * <h2><center>© Copyright (c) 2017 STMicroelectronics.
\r
10 * All rights reserved.</center></h2>
\r
12 * This software component is licensed by ST under BSD 3-Clause license,
\r
13 * the "License"; You may not use this file except in compliance with the
\r
14 * License. You may obtain a copy of the License at:
\r
15 * opensource.org/licenses/BSD-3-Clause
\r
17 ******************************************************************************
\r
20 /* Define to prevent recursive inclusion -------------------------------------*/
\r
21 #ifndef STM32L4xx_HAL_I2C_H
\r
22 #define STM32L4xx_HAL_I2C_H
\r
28 /* Includes ------------------------------------------------------------------*/
\r
29 #include "stm32l4xx_hal_def.h"
\r
31 /** @addtogroup STM32L4xx_HAL_Driver
\r
39 /* Exported types ------------------------------------------------------------*/
\r
40 /** @defgroup I2C_Exported_Types I2C Exported Types
\r
44 /** @defgroup I2C_Configuration_Structure_definition I2C Configuration Structure definition
\r
45 * @brief I2C Configuration Structure definition
\r
50 uint32_t Timing; /*!< Specifies the I2C_TIMINGR_register value.
\r
51 This parameter calculated by referring to I2C initialization
\r
52 section in Reference manual */
\r
54 uint32_t OwnAddress1; /*!< Specifies the first device own address.
\r
55 This parameter can be a 7-bit or 10-bit address. */
\r
57 uint32_t AddressingMode; /*!< Specifies if 7-bit or 10-bit addressing mode is selected.
\r
58 This parameter can be a value of @ref I2C_ADDRESSING_MODE */
\r
60 uint32_t DualAddressMode; /*!< Specifies if dual addressing mode is selected.
\r
61 This parameter can be a value of @ref I2C_DUAL_ADDRESSING_MODE */
\r
63 uint32_t OwnAddress2; /*!< Specifies the second device own address if dual addressing mode is selected
\r
64 This parameter can be a 7-bit address. */
\r
66 uint32_t OwnAddress2Masks; /*!< Specifies the acknowledge mask address second device own address if dual addressing mode is selected
\r
67 This parameter can be a value of @ref I2C_OWN_ADDRESS2_MASKS */
\r
69 uint32_t GeneralCallMode; /*!< Specifies if general call mode is selected.
\r
70 This parameter can be a value of @ref I2C_GENERAL_CALL_ADDRESSING_MODE */
\r
72 uint32_t NoStretchMode; /*!< Specifies if nostretch mode is selected.
\r
73 This parameter can be a value of @ref I2C_NOSTRETCH_MODE */
\r
81 /** @defgroup HAL_state_structure_definition HAL state structure definition
\r
82 * @brief HAL State structure definition
\r
83 * @note HAL I2C State value coding follow below described bitmap :\n
\r
84 * b7-b6 Error information\n
\r
86 * 01 : Abort (Abort user request on going)\n
\r
89 * b5 Peripheral initialization status\n
\r
90 * 0 : Reset (peripheral not initialized)\n
\r
91 * 1 : Init done (peripheral initialized and ready to use. HAL I2C Init function called)\n
\r
93 * x : Should be set to 0\n
\r
95 * 0 : Ready or Busy (No Listen mode ongoing)\n
\r
96 * 1 : Listen (peripheral in Address Listen Mode)\n
\r
97 * b2 Intrinsic process state\n
\r
99 * 1 : Busy (peripheral busy with some configuration or internal operations)\n
\r
101 * 0 : Ready (no Rx operation ongoing)\n
\r
102 * 1 : Busy (Rx operation ongoing)\n
\r
104 * 0 : Ready (no Tx operation ongoing)\n
\r
105 * 1 : Busy (Tx operation ongoing)
\r
110 HAL_I2C_STATE_RESET = 0x00U, /*!< Peripheral is not yet Initialized */
\r
111 HAL_I2C_STATE_READY = 0x20U, /*!< Peripheral Initialized and ready for use */
\r
112 HAL_I2C_STATE_BUSY = 0x24U, /*!< An internal process is ongoing */
\r
113 HAL_I2C_STATE_BUSY_TX = 0x21U, /*!< Data Transmission process is ongoing */
\r
114 HAL_I2C_STATE_BUSY_RX = 0x22U, /*!< Data Reception process is ongoing */
\r
115 HAL_I2C_STATE_LISTEN = 0x28U, /*!< Address Listen Mode is ongoing */
\r
116 HAL_I2C_STATE_BUSY_TX_LISTEN = 0x29U, /*!< Address Listen Mode and Data Transmission
\r
117 process is ongoing */
\r
118 HAL_I2C_STATE_BUSY_RX_LISTEN = 0x2AU, /*!< Address Listen Mode and Data Reception
\r
119 process is ongoing */
\r
120 HAL_I2C_STATE_ABORT = 0x60U, /*!< Abort user request ongoing */
\r
121 HAL_I2C_STATE_TIMEOUT = 0xA0U, /*!< Timeout state */
\r
122 HAL_I2C_STATE_ERROR = 0xE0U /*!< Error */
\r
124 } HAL_I2C_StateTypeDef;
\r
130 /** @defgroup HAL_mode_structure_definition HAL mode structure definition
\r
131 * @brief HAL Mode structure definition
\r
132 * @note HAL I2C Mode value coding follow below described bitmap :\n
\r
134 * x : Should be set to 0\n
\r
137 * 1 : Memory (HAL I2C communication is in Memory Mode)\n
\r
140 * 1 : Slave (HAL I2C communication is in Slave Mode)\n
\r
143 * 1 : Master (HAL I2C communication is in Master Mode)\n
\r
144 * b3-b2-b1-b0 (not used)\n
\r
145 * xxxx : Should be set to 0000
\r
150 HAL_I2C_MODE_NONE = 0x00U, /*!< No I2C communication on going */
\r
151 HAL_I2C_MODE_MASTER = 0x10U, /*!< I2C communication is in Master Mode */
\r
152 HAL_I2C_MODE_SLAVE = 0x20U, /*!< I2C communication is in Slave Mode */
\r
153 HAL_I2C_MODE_MEM = 0x40U /*!< I2C communication is in Memory Mode */
\r
155 } HAL_I2C_ModeTypeDef;
\r
161 /** @defgroup I2C_Error_Code_definition I2C Error Code definition
\r
162 * @brief I2C Error Code definition
\r
165 #define HAL_I2C_ERROR_NONE (0x00000000U) /*!< No error */
\r
166 #define HAL_I2C_ERROR_BERR (0x00000001U) /*!< BERR error */
\r
167 #define HAL_I2C_ERROR_ARLO (0x00000002U) /*!< ARLO error */
\r
168 #define HAL_I2C_ERROR_AF (0x00000004U) /*!< ACKF error */
\r
169 #define HAL_I2C_ERROR_OVR (0x00000008U) /*!< OVR error */
\r
170 #define HAL_I2C_ERROR_DMA (0x00000010U) /*!< DMA transfer error */
\r
171 #define HAL_I2C_ERROR_TIMEOUT (0x00000020U) /*!< Timeout error */
\r
172 #define HAL_I2C_ERROR_SIZE (0x00000040U) /*!< Size Management error */
\r
173 #define HAL_I2C_ERROR_DMA_PARAM (0x00000080U) /*!< DMA Parameter Error */
\r
174 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
\r
175 #define HAL_I2C_ERROR_INVALID_CALLBACK (0x00000100U) /*!< Invalid Callback error */
\r
176 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
\r
177 #define HAL_I2C_ERROR_INVALID_PARAM (0x00000200U) /*!< Invalid Parameters error */
\r
182 /** @defgroup I2C_handle_Structure_definition I2C handle Structure definition
\r
183 * @brief I2C handle Structure definition
\r
186 typedef struct __I2C_HandleTypeDef
\r
188 I2C_TypeDef *Instance; /*!< I2C registers base address */
\r
190 I2C_InitTypeDef Init; /*!< I2C communication parameters */
\r
192 uint8_t *pBuffPtr; /*!< Pointer to I2C transfer buffer */
\r
194 uint16_t XferSize; /*!< I2C transfer size */
\r
196 __IO uint16_t XferCount; /*!< I2C transfer counter */
\r
198 __IO uint32_t XferOptions; /*!< I2C sequantial transfer options, this parameter can
\r
199 be a value of @ref I2C_XFEROPTIONS */
\r
201 __IO uint32_t PreviousState; /*!< I2C communication Previous state */
\r
203 HAL_StatusTypeDef(*XferISR)(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags, uint32_t ITSources); /*!< I2C transfer IRQ handler function pointer */
\r
205 DMA_HandleTypeDef *hdmatx; /*!< I2C Tx DMA handle parameters */
\r
207 DMA_HandleTypeDef *hdmarx; /*!< I2C Rx DMA handle parameters */
\r
209 HAL_LockTypeDef Lock; /*!< I2C locking object */
\r
211 __IO HAL_I2C_StateTypeDef State; /*!< I2C communication state */
\r
213 __IO HAL_I2C_ModeTypeDef Mode; /*!< I2C communication mode */
\r
215 __IO uint32_t ErrorCode; /*!< I2C Error code */
\r
217 __IO uint32_t AddrEventCount; /*!< I2C Address Event counter */
\r
219 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
\r
220 void (* MasterTxCpltCallback)(struct __I2C_HandleTypeDef *hi2c); /*!< I2C Master Tx Transfer completed callback */
\r
221 void (* MasterRxCpltCallback)(struct __I2C_HandleTypeDef *hi2c); /*!< I2C Master Rx Transfer completed callback */
\r
222 void (* SlaveTxCpltCallback)(struct __I2C_HandleTypeDef *hi2c); /*!< I2C Slave Tx Transfer completed callback */
\r
223 void (* SlaveRxCpltCallback)(struct __I2C_HandleTypeDef *hi2c); /*!< I2C Slave Rx Transfer completed callback */
\r
224 void (* ListenCpltCallback)(struct __I2C_HandleTypeDef *hi2c); /*!< I2C Listen Complete callback */
\r
225 void (* MemTxCpltCallback)(struct __I2C_HandleTypeDef *hi2c); /*!< I2C Memory Tx Transfer completed callback */
\r
226 void (* MemRxCpltCallback)(struct __I2C_HandleTypeDef *hi2c); /*!< I2C Memory Rx Transfer completed callback */
\r
227 void (* ErrorCallback)(struct __I2C_HandleTypeDef *hi2c); /*!< I2C Error callback */
\r
228 void (* AbortCpltCallback)(struct __I2C_HandleTypeDef *hi2c); /*!< I2C Abort callback */
\r
230 void (* AddrCallback)(struct __I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode); /*!< I2C Slave Address Match callback */
\r
232 void (* MspInitCallback)(struct __I2C_HandleTypeDef *hi2c); /*!< I2C Msp Init callback */
\r
233 void (* MspDeInitCallback)(struct __I2C_HandleTypeDef *hi2c); /*!< I2C Msp DeInit callback */
\r
235 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
\r
236 } I2C_HandleTypeDef;
\r
238 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
\r
240 * @brief HAL I2C Callback ID enumeration definition
\r
244 HAL_I2C_MASTER_TX_COMPLETE_CB_ID = 0x00U, /*!< I2C Master Tx Transfer completed callback ID */
\r
245 HAL_I2C_MASTER_RX_COMPLETE_CB_ID = 0x01U, /*!< I2C Master Rx Transfer completed callback ID */
\r
246 HAL_I2C_SLAVE_TX_COMPLETE_CB_ID = 0x02U, /*!< I2C Slave Tx Transfer completed callback ID */
\r
247 HAL_I2C_SLAVE_RX_COMPLETE_CB_ID = 0x03U, /*!< I2C Slave Rx Transfer completed callback ID */
\r
248 HAL_I2C_LISTEN_COMPLETE_CB_ID = 0x04U, /*!< I2C Listen Complete callback ID */
\r
249 HAL_I2C_MEM_TX_COMPLETE_CB_ID = 0x05U, /*!< I2C Memory Tx Transfer callback ID */
\r
250 HAL_I2C_MEM_RX_COMPLETE_CB_ID = 0x06U, /*!< I2C Memory Rx Transfer completed callback ID */
\r
251 HAL_I2C_ERROR_CB_ID = 0x07U, /*!< I2C Error callback ID */
\r
252 HAL_I2C_ABORT_CB_ID = 0x08U, /*!< I2C Abort callback ID */
\r
254 HAL_I2C_MSPINIT_CB_ID = 0x09U, /*!< I2C Msp Init callback ID */
\r
255 HAL_I2C_MSPDEINIT_CB_ID = 0x0AU /*!< I2C Msp DeInit callback ID */
\r
257 } HAL_I2C_CallbackIDTypeDef;
\r
260 * @brief HAL I2C Callback pointer definition
\r
262 typedef void (*pI2C_CallbackTypeDef)(I2C_HandleTypeDef *hi2c); /*!< pointer to an I2C callback function */
\r
263 typedef void (*pI2C_AddrCallbackTypeDef)(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode); /*!< pointer to an I2C Address Match callback function */
\r
265 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
\r
273 /* Exported constants --------------------------------------------------------*/
\r
275 /** @defgroup I2C_Exported_Constants I2C Exported Constants
\r
279 /** @defgroup I2C_XFEROPTIONS I2C Sequential Transfer Options
\r
282 #define I2C_FIRST_FRAME ((uint32_t)I2C_SOFTEND_MODE)
\r
283 #define I2C_FIRST_AND_NEXT_FRAME ((uint32_t)(I2C_RELOAD_MODE | I2C_SOFTEND_MODE))
\r
284 #define I2C_NEXT_FRAME ((uint32_t)(I2C_RELOAD_MODE | I2C_SOFTEND_MODE))
\r
285 #define I2C_FIRST_AND_LAST_FRAME ((uint32_t)I2C_AUTOEND_MODE)
\r
286 #define I2C_LAST_FRAME ((uint32_t)I2C_AUTOEND_MODE)
\r
287 #define I2C_LAST_FRAME_NO_STOP ((uint32_t)I2C_SOFTEND_MODE)
\r
289 /* List of XferOptions in usage of :
\r
290 * 1- Restart condition in all use cases (direction change or not)
\r
292 #define I2C_OTHER_FRAME (0x000000AAU)
\r
293 #define I2C_OTHER_AND_LAST_FRAME (0x0000AA00U)
\r
298 /** @defgroup I2C_ADDRESSING_MODE I2C Addressing Mode
\r
301 #define I2C_ADDRESSINGMODE_7BIT (0x00000001U)
\r
302 #define I2C_ADDRESSINGMODE_10BIT (0x00000002U)
\r
307 /** @defgroup I2C_DUAL_ADDRESSING_MODE I2C Dual Addressing Mode
\r
310 #define I2C_DUALADDRESS_DISABLE (0x00000000U)
\r
311 #define I2C_DUALADDRESS_ENABLE I2C_OAR2_OA2EN
\r
316 /** @defgroup I2C_OWN_ADDRESS2_MASKS I2C Own Address2 Masks
\r
319 #define I2C_OA2_NOMASK ((uint8_t)0x00U)
\r
320 #define I2C_OA2_MASK01 ((uint8_t)0x01U)
\r
321 #define I2C_OA2_MASK02 ((uint8_t)0x02U)
\r
322 #define I2C_OA2_MASK03 ((uint8_t)0x03U)
\r
323 #define I2C_OA2_MASK04 ((uint8_t)0x04U)
\r
324 #define I2C_OA2_MASK05 ((uint8_t)0x05U)
\r
325 #define I2C_OA2_MASK06 ((uint8_t)0x06U)
\r
326 #define I2C_OA2_MASK07 ((uint8_t)0x07U)
\r
331 /** @defgroup I2C_GENERAL_CALL_ADDRESSING_MODE I2C General Call Addressing Mode
\r
334 #define I2C_GENERALCALL_DISABLE (0x00000000U)
\r
335 #define I2C_GENERALCALL_ENABLE I2C_CR1_GCEN
\r
340 /** @defgroup I2C_NOSTRETCH_MODE I2C No-Stretch Mode
\r
343 #define I2C_NOSTRETCH_DISABLE (0x00000000U)
\r
344 #define I2C_NOSTRETCH_ENABLE I2C_CR1_NOSTRETCH
\r
349 /** @defgroup I2C_MEMORY_ADDRESS_SIZE I2C Memory Address Size
\r
352 #define I2C_MEMADD_SIZE_8BIT (0x00000001U)
\r
353 #define I2C_MEMADD_SIZE_16BIT (0x00000002U)
\r
358 /** @defgroup I2C_XFERDIRECTION I2C Transfer Direction Master Point of View
\r
361 #define I2C_DIRECTION_TRANSMIT (0x00000000U)
\r
362 #define I2C_DIRECTION_RECEIVE (0x00000001U)
\r
367 /** @defgroup I2C_RELOAD_END_MODE I2C Reload End Mode
\r
370 #define I2C_RELOAD_MODE I2C_CR2_RELOAD
\r
371 #define I2C_AUTOEND_MODE I2C_CR2_AUTOEND
\r
372 #define I2C_SOFTEND_MODE (0x00000000U)
\r
377 /** @defgroup I2C_START_STOP_MODE I2C Start or Stop Mode
\r
380 #define I2C_NO_STARTSTOP (0x00000000U)
\r
381 #define I2C_GENERATE_STOP (uint32_t)(0x80000000U | I2C_CR2_STOP)
\r
382 #define I2C_GENERATE_START_READ (uint32_t)(0x80000000U | I2C_CR2_START | I2C_CR2_RD_WRN)
\r
383 #define I2C_GENERATE_START_WRITE (uint32_t)(0x80000000U | I2C_CR2_START)
\r
388 /** @defgroup I2C_Interrupt_configuration_definition I2C Interrupt configuration definition
\r
389 * @brief I2C Interrupt definition
\r
390 * Elements values convention: 0xXXXXXXXX
\r
391 * - XXXXXXXX : Interrupt control mask
\r
394 #define I2C_IT_ERRI I2C_CR1_ERRIE
\r
395 #define I2C_IT_TCI I2C_CR1_TCIE
\r
396 #define I2C_IT_STOPI I2C_CR1_STOPIE
\r
397 #define I2C_IT_NACKI I2C_CR1_NACKIE
\r
398 #define I2C_IT_ADDRI I2C_CR1_ADDRIE
\r
399 #define I2C_IT_RXI I2C_CR1_RXIE
\r
400 #define I2C_IT_TXI I2C_CR1_TXIE
\r
405 /** @defgroup I2C_Flag_definition I2C Flag definition
\r
408 #define I2C_FLAG_TXE I2C_ISR_TXE
\r
409 #define I2C_FLAG_TXIS I2C_ISR_TXIS
\r
410 #define I2C_FLAG_RXNE I2C_ISR_RXNE
\r
411 #define I2C_FLAG_ADDR I2C_ISR_ADDR
\r
412 #define I2C_FLAG_AF I2C_ISR_NACKF
\r
413 #define I2C_FLAG_STOPF I2C_ISR_STOPF
\r
414 #define I2C_FLAG_TC I2C_ISR_TC
\r
415 #define I2C_FLAG_TCR I2C_ISR_TCR
\r
416 #define I2C_FLAG_BERR I2C_ISR_BERR
\r
417 #define I2C_FLAG_ARLO I2C_ISR_ARLO
\r
418 #define I2C_FLAG_OVR I2C_ISR_OVR
\r
419 #define I2C_FLAG_PECERR I2C_ISR_PECERR
\r
420 #define I2C_FLAG_TIMEOUT I2C_ISR_TIMEOUT
\r
421 #define I2C_FLAG_ALERT I2C_ISR_ALERT
\r
422 #define I2C_FLAG_BUSY I2C_ISR_BUSY
\r
423 #define I2C_FLAG_DIR I2C_ISR_DIR
\r
432 /* Exported macros -----------------------------------------------------------*/
\r
434 /** @defgroup I2C_Exported_Macros I2C Exported Macros
\r
438 /** @brief Reset I2C handle state.
\r
439 * @param __HANDLE__ specifies the I2C Handle.
\r
442 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
\r
443 #define __HAL_I2C_RESET_HANDLE_STATE(__HANDLE__) do{ \
\r
444 (__HANDLE__)->State = HAL_I2C_STATE_RESET; \
\r
445 (__HANDLE__)->MspInitCallback = NULL; \
\r
446 (__HANDLE__)->MspDeInitCallback = NULL; \
\r
449 #define __HAL_I2C_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_I2C_STATE_RESET)
\r
452 /** @brief Enable the specified I2C interrupt.
\r
453 * @param __HANDLE__ specifies the I2C Handle.
\r
454 * @param __INTERRUPT__ specifies the interrupt source to enable.
\r
455 * This parameter can be one of the following values:
\r
456 * @arg @ref I2C_IT_ERRI Errors interrupt enable
\r
457 * @arg @ref I2C_IT_TCI Transfer complete interrupt enable
\r
458 * @arg @ref I2C_IT_STOPI STOP detection interrupt enable
\r
459 * @arg @ref I2C_IT_NACKI NACK received interrupt enable
\r
460 * @arg @ref I2C_IT_ADDRI Address match interrupt enable
\r
461 * @arg @ref I2C_IT_RXI RX interrupt enable
\r
462 * @arg @ref I2C_IT_TXI TX interrupt enable
\r
466 #define __HAL_I2C_ENABLE_IT(__HANDLE__, __INTERRUPT__) ((__HANDLE__)->Instance->CR1 |= (__INTERRUPT__))
\r
468 /** @brief Disable the specified I2C interrupt.
\r
469 * @param __HANDLE__ specifies the I2C Handle.
\r
470 * @param __INTERRUPT__ specifies the interrupt source to disable.
\r
471 * This parameter can be one of the following values:
\r
472 * @arg @ref I2C_IT_ERRI Errors interrupt enable
\r
473 * @arg @ref I2C_IT_TCI Transfer complete interrupt enable
\r
474 * @arg @ref I2C_IT_STOPI STOP detection interrupt enable
\r
475 * @arg @ref I2C_IT_NACKI NACK received interrupt enable
\r
476 * @arg @ref I2C_IT_ADDRI Address match interrupt enable
\r
477 * @arg @ref I2C_IT_RXI RX interrupt enable
\r
478 * @arg @ref I2C_IT_TXI TX interrupt enable
\r
482 #define __HAL_I2C_DISABLE_IT(__HANDLE__, __INTERRUPT__) ((__HANDLE__)->Instance->CR1 &= (~(__INTERRUPT__)))
\r
484 /** @brief Check whether the specified I2C interrupt source is enabled or not.
\r
485 * @param __HANDLE__ specifies the I2C Handle.
\r
486 * @param __INTERRUPT__ specifies the I2C interrupt source to check.
\r
487 * This parameter can be one of the following values:
\r
488 * @arg @ref I2C_IT_ERRI Errors interrupt enable
\r
489 * @arg @ref I2C_IT_TCI Transfer complete interrupt enable
\r
490 * @arg @ref I2C_IT_STOPI STOP detection interrupt enable
\r
491 * @arg @ref I2C_IT_NACKI NACK received interrupt enable
\r
492 * @arg @ref I2C_IT_ADDRI Address match interrupt enable
\r
493 * @arg @ref I2C_IT_RXI RX interrupt enable
\r
494 * @arg @ref I2C_IT_TXI TX interrupt enable
\r
496 * @retval The new state of __INTERRUPT__ (SET or RESET).
\r
498 #define __HAL_I2C_GET_IT_SOURCE(__HANDLE__, __INTERRUPT__) ((((__HANDLE__)->Instance->CR1 & (__INTERRUPT__)) == (__INTERRUPT__)) ? SET : RESET)
\r
500 /** @brief Check whether the specified I2C flag is set or not.
\r
501 * @param __HANDLE__ specifies the I2C Handle.
\r
502 * @param __FLAG__ specifies the flag to check.
\r
503 * This parameter can be one of the following values:
\r
504 * @arg @ref I2C_FLAG_TXE Transmit data register empty
\r
505 * @arg @ref I2C_FLAG_TXIS Transmit interrupt status
\r
506 * @arg @ref I2C_FLAG_RXNE Receive data register not empty
\r
507 * @arg @ref I2C_FLAG_ADDR Address matched (slave mode)
\r
508 * @arg @ref I2C_FLAG_AF Acknowledge failure received flag
\r
509 * @arg @ref I2C_FLAG_STOPF STOP detection flag
\r
510 * @arg @ref I2C_FLAG_TC Transfer complete (master mode)
\r
511 * @arg @ref I2C_FLAG_TCR Transfer complete reload
\r
512 * @arg @ref I2C_FLAG_BERR Bus error
\r
513 * @arg @ref I2C_FLAG_ARLO Arbitration lost
\r
514 * @arg @ref I2C_FLAG_OVR Overrun/Underrun
\r
515 * @arg @ref I2C_FLAG_PECERR PEC error in reception
\r
516 * @arg @ref I2C_FLAG_TIMEOUT Timeout or Tlow detection flag
\r
517 * @arg @ref I2C_FLAG_ALERT SMBus alert
\r
518 * @arg @ref I2C_FLAG_BUSY Bus busy
\r
519 * @arg @ref I2C_FLAG_DIR Transfer direction (slave mode)
\r
521 * @retval The new state of __FLAG__ (SET or RESET).
\r
523 #define I2C_FLAG_MASK (0x0001FFFFU)
\r
524 #define __HAL_I2C_GET_FLAG(__HANDLE__, __FLAG__) (((((__HANDLE__)->Instance->ISR) & (__FLAG__)) == (__FLAG__)) ? SET : RESET)
\r
526 /** @brief Clear the I2C pending flags which are cleared by writing 1 in a specific bit.
\r
527 * @param __HANDLE__ specifies the I2C Handle.
\r
528 * @param __FLAG__ specifies the flag to clear.
\r
529 * This parameter can be any combination of the following values:
\r
530 * @arg @ref I2C_FLAG_TXE Transmit data register empty
\r
531 * @arg @ref I2C_FLAG_ADDR Address matched (slave mode)
\r
532 * @arg @ref I2C_FLAG_AF Acknowledge failure received flag
\r
533 * @arg @ref I2C_FLAG_STOPF STOP detection flag
\r
534 * @arg @ref I2C_FLAG_BERR Bus error
\r
535 * @arg @ref I2C_FLAG_ARLO Arbitration lost
\r
536 * @arg @ref I2C_FLAG_OVR Overrun/Underrun
\r
537 * @arg @ref I2C_FLAG_PECERR PEC error in reception
\r
538 * @arg @ref I2C_FLAG_TIMEOUT Timeout or Tlow detection flag
\r
539 * @arg @ref I2C_FLAG_ALERT SMBus alert
\r
543 #define __HAL_I2C_CLEAR_FLAG(__HANDLE__, __FLAG__) (((__FLAG__) == I2C_FLAG_TXE) ? ((__HANDLE__)->Instance->ISR |= (__FLAG__)) \
\r
544 : ((__HANDLE__)->Instance->ICR = (__FLAG__)))
\r
546 /** @brief Enable the specified I2C peripheral.
\r
547 * @param __HANDLE__ specifies the I2C Handle.
\r
550 #define __HAL_I2C_ENABLE(__HANDLE__) (SET_BIT((__HANDLE__)->Instance->CR1, I2C_CR1_PE))
\r
552 /** @brief Disable the specified I2C peripheral.
\r
553 * @param __HANDLE__ specifies the I2C Handle.
\r
556 #define __HAL_I2C_DISABLE(__HANDLE__) (CLEAR_BIT((__HANDLE__)->Instance->CR1, I2C_CR1_PE))
\r
558 /** @brief Generate a Non-Acknowledge I2C peripheral in Slave mode.
\r
559 * @param __HANDLE__ specifies the I2C Handle.
\r
562 #define __HAL_I2C_GENERATE_NACK(__HANDLE__) (SET_BIT((__HANDLE__)->Instance->CR2, I2C_CR2_NACK))
\r
567 /* Include I2C HAL Extended module */
\r
568 #include "stm32l4xx_hal_i2c_ex.h"
\r
570 /* Exported functions --------------------------------------------------------*/
\r
571 /** @addtogroup I2C_Exported_Functions
\r
575 /** @addtogroup I2C_Exported_Functions_Group1 Initialization and de-initialization functions
\r
578 /* Initialization and de-initialization functions******************************/
\r
579 HAL_StatusTypeDef HAL_I2C_Init(I2C_HandleTypeDef *hi2c);
\r
580 HAL_StatusTypeDef HAL_I2C_DeInit(I2C_HandleTypeDef *hi2c);
\r
581 void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c);
\r
582 void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c);
\r
584 /* Callbacks Register/UnRegister functions ***********************************/
\r
585 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
\r
586 HAL_StatusTypeDef HAL_I2C_RegisterCallback(I2C_HandleTypeDef *hi2c, HAL_I2C_CallbackIDTypeDef CallbackID, pI2C_CallbackTypeDef pCallback);
\r
587 HAL_StatusTypeDef HAL_I2C_UnRegisterCallback(I2C_HandleTypeDef *hi2c, HAL_I2C_CallbackIDTypeDef CallbackID);
\r
589 HAL_StatusTypeDef HAL_I2C_RegisterAddrCallback(I2C_HandleTypeDef *hi2c, pI2C_AddrCallbackTypeDef pCallback);
\r
590 HAL_StatusTypeDef HAL_I2C_UnRegisterAddrCallback(I2C_HandleTypeDef *hi2c);
\r
591 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
\r
596 /** @addtogroup I2C_Exported_Functions_Group2 Input and Output operation functions
\r
599 /* IO operation functions ****************************************************/
\r
600 /******* Blocking mode: Polling */
\r
601 HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout);
\r
602 HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout);
\r
603 HAL_StatusTypeDef HAL_I2C_Slave_Transmit(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout);
\r
604 HAL_StatusTypeDef HAL_I2C_Slave_Receive(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout);
\r
605 HAL_StatusTypeDef HAL_I2C_Mem_Write(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout);
\r
606 HAL_StatusTypeDef HAL_I2C_Mem_Read(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout);
\r
607 HAL_StatusTypeDef HAL_I2C_IsDeviceReady(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout);
\r
609 /******* Non-Blocking mode: Interrupt */
\r
610 HAL_StatusTypeDef HAL_I2C_Master_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size);
\r
611 HAL_StatusTypeDef HAL_I2C_Master_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size);
\r
612 HAL_StatusTypeDef HAL_I2C_Slave_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size);
\r
613 HAL_StatusTypeDef HAL_I2C_Slave_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size);
\r
614 HAL_StatusTypeDef HAL_I2C_Mem_Write_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size);
\r
615 HAL_StatusTypeDef HAL_I2C_Mem_Read_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size);
\r
617 HAL_StatusTypeDef HAL_I2C_Master_Seq_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions);
\r
618 HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions);
\r
619 HAL_StatusTypeDef HAL_I2C_Slave_Seq_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions);
\r
620 HAL_StatusTypeDef HAL_I2C_Slave_Seq_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions);
\r
621 HAL_StatusTypeDef HAL_I2C_EnableListen_IT(I2C_HandleTypeDef *hi2c);
\r
622 HAL_StatusTypeDef HAL_I2C_DisableListen_IT(I2C_HandleTypeDef *hi2c);
\r
623 HAL_StatusTypeDef HAL_I2C_Master_Abort_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress);
\r
625 /******* Non-Blocking mode: DMA */
\r
626 HAL_StatusTypeDef HAL_I2C_Master_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size);
\r
627 HAL_StatusTypeDef HAL_I2C_Master_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size);
\r
628 HAL_StatusTypeDef HAL_I2C_Slave_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size);
\r
629 HAL_StatusTypeDef HAL_I2C_Slave_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size);
\r
630 HAL_StatusTypeDef HAL_I2C_Mem_Write_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size);
\r
631 HAL_StatusTypeDef HAL_I2C_Mem_Read_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size);
\r
633 HAL_StatusTypeDef HAL_I2C_Master_Seq_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions);
\r
634 HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions);
\r
635 HAL_StatusTypeDef HAL_I2C_Slave_Seq_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions);
\r
636 HAL_StatusTypeDef HAL_I2C_Slave_Seq_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions);
\r
641 /** @addtogroup I2C_IRQ_Handler_and_Callbacks IRQ Handler and Callbacks
\r
644 /******* I2C IRQHandler and Callbacks used in non blocking modes (Interrupt and DMA) */
\r
645 void HAL_I2C_EV_IRQHandler(I2C_HandleTypeDef *hi2c);
\r
646 void HAL_I2C_ER_IRQHandler(I2C_HandleTypeDef *hi2c);
\r
647 void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c);
\r
648 void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c);
\r
649 void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c);
\r
650 void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c);
\r
651 void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode);
\r
652 void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c);
\r
653 void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef *hi2c);
\r
654 void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c);
\r
655 void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c);
\r
656 void HAL_I2C_AbortCpltCallback(I2C_HandleTypeDef *hi2c);
\r
661 /** @addtogroup I2C_Exported_Functions_Group3 Peripheral State, Mode and Error functions
\r
664 /* Peripheral State, Mode and Error functions *********************************/
\r
665 HAL_I2C_StateTypeDef HAL_I2C_GetState(I2C_HandleTypeDef *hi2c);
\r
666 HAL_I2C_ModeTypeDef HAL_I2C_GetMode(I2C_HandleTypeDef *hi2c);
\r
667 uint32_t HAL_I2C_GetError(I2C_HandleTypeDef *hi2c);
\r
677 /* Private constants ---------------------------------------------------------*/
\r
678 /** @defgroup I2C_Private_Constants I2C Private Constants
\r
686 /* Private macros ------------------------------------------------------------*/
\r
687 /** @defgroup I2C_Private_Macro I2C Private Macros
\r
691 #define IS_I2C_ADDRESSING_MODE(MODE) (((MODE) == I2C_ADDRESSINGMODE_7BIT) || \
\r
692 ((MODE) == I2C_ADDRESSINGMODE_10BIT))
\r
694 #define IS_I2C_DUAL_ADDRESS(ADDRESS) (((ADDRESS) == I2C_DUALADDRESS_DISABLE) || \
\r
695 ((ADDRESS) == I2C_DUALADDRESS_ENABLE))
\r
697 #define IS_I2C_OWN_ADDRESS2_MASK(MASK) (((MASK) == I2C_OA2_NOMASK) || \
\r
698 ((MASK) == I2C_OA2_MASK01) || \
\r
699 ((MASK) == I2C_OA2_MASK02) || \
\r
700 ((MASK) == I2C_OA2_MASK03) || \
\r
701 ((MASK) == I2C_OA2_MASK04) || \
\r
702 ((MASK) == I2C_OA2_MASK05) || \
\r
703 ((MASK) == I2C_OA2_MASK06) || \
\r
704 ((MASK) == I2C_OA2_MASK07))
\r
706 #define IS_I2C_GENERAL_CALL(CALL) (((CALL) == I2C_GENERALCALL_DISABLE) || \
\r
707 ((CALL) == I2C_GENERALCALL_ENABLE))
\r
709 #define IS_I2C_NO_STRETCH(STRETCH) (((STRETCH) == I2C_NOSTRETCH_DISABLE) || \
\r
710 ((STRETCH) == I2C_NOSTRETCH_ENABLE))
\r
712 #define IS_I2C_MEMADD_SIZE(SIZE) (((SIZE) == I2C_MEMADD_SIZE_8BIT) || \
\r
713 ((SIZE) == I2C_MEMADD_SIZE_16BIT))
\r
715 #define IS_TRANSFER_MODE(MODE) (((MODE) == I2C_RELOAD_MODE) || \
\r
716 ((MODE) == I2C_AUTOEND_MODE) || \
\r
717 ((MODE) == I2C_SOFTEND_MODE))
\r
719 #define IS_TRANSFER_REQUEST(REQUEST) (((REQUEST) == I2C_GENERATE_STOP) || \
\r
720 ((REQUEST) == I2C_GENERATE_START_READ) || \
\r
721 ((REQUEST) == I2C_GENERATE_START_WRITE) || \
\r
722 ((REQUEST) == I2C_NO_STARTSTOP))
\r
724 #define IS_I2C_TRANSFER_OPTIONS_REQUEST(REQUEST) (((REQUEST) == I2C_FIRST_FRAME) || \
\r
725 ((REQUEST) == I2C_FIRST_AND_NEXT_FRAME) || \
\r
726 ((REQUEST) == I2C_NEXT_FRAME) || \
\r
727 ((REQUEST) == I2C_FIRST_AND_LAST_FRAME) || \
\r
728 ((REQUEST) == I2C_LAST_FRAME) || \
\r
729 ((REQUEST) == I2C_LAST_FRAME_NO_STOP) || \
\r
730 IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(REQUEST))
\r
732 #define IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(REQUEST) (((REQUEST) == I2C_OTHER_FRAME) || \
\r
733 ((REQUEST) == I2C_OTHER_AND_LAST_FRAME))
\r
735 #define I2C_RESET_CR2(__HANDLE__) ((__HANDLE__)->Instance->CR2 &= (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_HEAD10R | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_RD_WRN)))
\r
737 #define I2C_GET_ADDR_MATCH(__HANDLE__) ((uint16_t)(((__HANDLE__)->Instance->ISR & I2C_ISR_ADDCODE) >> 16U))
\r
738 #define I2C_GET_DIR(__HANDLE__) ((uint8_t)(((__HANDLE__)->Instance->ISR & I2C_ISR_DIR) >> 16U))
\r
739 #define I2C_GET_STOP_MODE(__HANDLE__) ((__HANDLE__)->Instance->CR2 & I2C_CR2_AUTOEND)
\r
740 #define I2C_GET_OWN_ADDRESS1(__HANDLE__) ((uint16_t)((__HANDLE__)->Instance->OAR1 & I2C_OAR1_OA1))
\r
741 #define I2C_GET_OWN_ADDRESS2(__HANDLE__) ((uint16_t)((__HANDLE__)->Instance->OAR2 & I2C_OAR2_OA2))
\r
743 #define IS_I2C_OWN_ADDRESS1(ADDRESS1) ((ADDRESS1) <= 0x000003FFU)
\r
744 #define IS_I2C_OWN_ADDRESS2(ADDRESS2) ((ADDRESS2) <= (uint16_t)0x00FFU)
\r
746 #define I2C_MEM_ADD_MSB(__ADDRESS__) ((uint8_t)((uint16_t)(((uint16_t)((__ADDRESS__) & (uint16_t)(0xFF00U))) >> 8U)))
\r
747 #define I2C_MEM_ADD_LSB(__ADDRESS__) ((uint8_t)((uint16_t)((__ADDRESS__) & (uint16_t)(0x00FFU))))
\r
749 #define I2C_GENERATE_START(__ADDMODE__,__ADDRESS__) (((__ADDMODE__) == I2C_ADDRESSINGMODE_7BIT) ? (uint32_t)((((uint32_t)(__ADDRESS__) & (I2C_CR2_SADD)) | (I2C_CR2_START) | (I2C_CR2_AUTOEND)) & (~I2C_CR2_RD_WRN)) : \
\r
750 (uint32_t)((((uint32_t)(__ADDRESS__) & (I2C_CR2_SADD)) | (I2C_CR2_ADD10) | (I2C_CR2_START)) & (~I2C_CR2_RD_WRN)))
\r
752 #define I2C_CHECK_FLAG(__ISR__, __FLAG__) ((((__ISR__) & ((__FLAG__) & I2C_FLAG_MASK)) == ((__FLAG__) & I2C_FLAG_MASK)) ? SET : RESET)
\r
753 #define I2C_CHECK_IT_SOURCE(__CR1__, __IT__) ((((__CR1__) & (__IT__)) == (__IT__)) ? SET : RESET)
\r
758 /* Private Functions ---------------------------------------------------------*/
\r
759 /** @defgroup I2C_Private_Functions I2C Private Functions
\r
762 /* Private functions are defined in stm32l4xx_hal_i2c.c file */
\r
780 #endif /* STM32L4xx_HAL_I2C_H */
\r
782 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
\r