stm32l4xx_hal_i2c.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_i2c.h
  4. * @author MCD Application Team
  5. * @brief Header file of I2C HAL module.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  10. *
  11. * Redistribution and use in source and binary forms, with or without modification,
  12. * are permitted provided that the following conditions are met:
  13. * 1. Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  28. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. ******************************************************************************
  34. */
  35. /* Define to prevent recursive inclusion -------------------------------------*/
  36. #ifndef __STM32L4xx_HAL_I2C_H
  37. #define __STM32L4xx_HAL_I2C_H
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. /* Includes ------------------------------------------------------------------*/
  42. #include "stm32l4xx_hal_def.h"
  43. /** @addtogroup STM32L4xx_HAL_Driver
  44. * @{
  45. */
  46. /** @addtogroup I2C
  47. * @{
  48. */
  49. /* Exported types ------------------------------------------------------------*/
  50. /** @defgroup I2C_Exported_Types I2C Exported Types
  51. * @{
  52. */
  53. /** @defgroup I2C_Configuration_Structure_definition I2C Configuration Structure definition
  54. * @brief I2C Configuration Structure definition
  55. * @{
  56. */
  57. typedef struct
  58. {
  59. uint32_t Timing; /*!< Specifies the I2C_TIMINGR_register value.
  60. This parameter calculated by referring to I2C initialization
  61. section in Reference manual */
  62. uint32_t OwnAddress1; /*!< Specifies the first device own address.
  63. This parameter can be a 7-bit or 10-bit address. */
  64. uint32_t AddressingMode; /*!< Specifies if 7-bit or 10-bit addressing mode is selected.
  65. This parameter can be a value of @ref I2C_ADDRESSING_MODE */
  66. uint32_t DualAddressMode; /*!< Specifies if dual addressing mode is selected.
  67. This parameter can be a value of @ref I2C_DUAL_ADDRESSING_MODE */
  68. uint32_t OwnAddress2; /*!< Specifies the second device own address if dual addressing mode is selected
  69. This parameter can be a 7-bit address. */
  70. uint32_t OwnAddress2Masks; /*!< Specifies the acknowledge mask address second device own address if dual addressing mode is selected
  71. This parameter can be a value of @ref I2C_OWN_ADDRESS2_MASKS */
  72. uint32_t GeneralCallMode; /*!< Specifies if general call mode is selected.
  73. This parameter can be a value of @ref I2C_GENERAL_CALL_ADDRESSING_MODE */
  74. uint32_t NoStretchMode; /*!< Specifies if nostretch mode is selected.
  75. This parameter can be a value of @ref I2C_NOSTRETCH_MODE */
  76. } I2C_InitTypeDef;
  77. /**
  78. * @}
  79. */
  80. /** @defgroup HAL_state_structure_definition HAL state structure definition
  81. * @brief HAL State structure definition
  82. * @note HAL I2C State value coding follow below described bitmap :\n
  83. * b7-b6 Error information\n
  84. * 00 : No Error\n
  85. * 01 : Abort (Abort user request on going)\n
  86. * 10 : Timeout\n
  87. * 11 : Error\n
  88. * b5 IP initilisation status\n
  89. * 0 : Reset (IP not initialized)\n
  90. * 1 : Init done (IP initialized and ready to use. HAL I2C Init function called)\n
  91. * b4 (not used)\n
  92. * x : Should be set to 0\n
  93. * b3\n
  94. * 0 : Ready or Busy (No Listen mode ongoing)\n
  95. * 1 : Listen (IP in Address Listen Mode)\n
  96. * b2 Intrinsic process state\n
  97. * 0 : Ready\n
  98. * 1 : Busy (IP busy with some configuration or internal operations)\n
  99. * b1 Rx state\n
  100. * 0 : Ready (no Rx operation ongoing)\n
  101. * 1 : Busy (Rx operation ongoing)\n
  102. * b0 Tx state\n
  103. * 0 : Ready (no Tx operation ongoing)\n
  104. * 1 : Busy (Tx operation ongoing)
  105. * @{
  106. */
  107. typedef enum
  108. {
  109. HAL_I2C_STATE_RESET = 0x00U, /*!< Peripheral is not yet Initialized */
  110. HAL_I2C_STATE_READY = 0x20U, /*!< Peripheral Initialized and ready for use */
  111. HAL_I2C_STATE_BUSY = 0x24U, /*!< An internal process is ongoing */
  112. HAL_I2C_STATE_BUSY_TX = 0x21U, /*!< Data Transmission process is ongoing */
  113. HAL_I2C_STATE_BUSY_RX = 0x22U, /*!< Data Reception process is ongoing */
  114. HAL_I2C_STATE_LISTEN = 0x28U, /*!< Address Listen Mode is ongoing */
  115. HAL_I2C_STATE_BUSY_TX_LISTEN = 0x29U, /*!< Address Listen Mode and Data Transmission
  116. process is ongoing */
  117. HAL_I2C_STATE_BUSY_RX_LISTEN = 0x2AU, /*!< Address Listen Mode and Data Reception
  118. process is ongoing */
  119. HAL_I2C_STATE_ABORT = 0x60U, /*!< Abort user request ongoing */
  120. HAL_I2C_STATE_TIMEOUT = 0xA0U, /*!< Timeout state */
  121. HAL_I2C_STATE_ERROR = 0xE0U /*!< Error */
  122. } HAL_I2C_StateTypeDef;
  123. /**
  124. * @}
  125. */
  126. /** @defgroup HAL_mode_structure_definition HAL mode structure definition
  127. * @brief HAL Mode structure definition
  128. * @note HAL I2C Mode value coding follow below described bitmap :\n
  129. * b7 (not used)\n
  130. * x : Should be set to 0\n
  131. * b6\n
  132. * 0 : None\n
  133. * 1 : Memory (HAL I2C communication is in Memory Mode)\n
  134. * b5\n
  135. * 0 : None\n
  136. * 1 : Slave (HAL I2C communication is in Slave Mode)\n
  137. * b4\n
  138. * 0 : None\n
  139. * 1 : Master (HAL I2C communication is in Master Mode)\n
  140. * b3-b2-b1-b0 (not used)\n
  141. * xxxx : Should be set to 0000
  142. * @{
  143. */
  144. typedef enum
  145. {
  146. HAL_I2C_MODE_NONE = 0x00U, /*!< No I2C communication on going */
  147. HAL_I2C_MODE_MASTER = 0x10U, /*!< I2C communication is in Master Mode */
  148. HAL_I2C_MODE_SLAVE = 0x20U, /*!< I2C communication is in Slave Mode */
  149. HAL_I2C_MODE_MEM = 0x40U /*!< I2C communication is in Memory Mode */
  150. } HAL_I2C_ModeTypeDef;
  151. /**
  152. * @}
  153. */
  154. /** @defgroup I2C_Error_Code_definition I2C Error Code definition
  155. * @brief I2C Error Code definition
  156. * @{
  157. */
  158. #define HAL_I2C_ERROR_NONE (0x00000000U) /*!< No error */
  159. #define HAL_I2C_ERROR_BERR (0x00000001U) /*!< BERR error */
  160. #define HAL_I2C_ERROR_ARLO (0x00000002U) /*!< ARLO error */
  161. #define HAL_I2C_ERROR_AF (0x00000004U) /*!< ACKF error */
  162. #define HAL_I2C_ERROR_OVR (0x00000008U) /*!< OVR error */
  163. #define HAL_I2C_ERROR_DMA (0x00000010U) /*!< DMA transfer error */
  164. #define HAL_I2C_ERROR_TIMEOUT (0x00000020U) /*!< Timeout error */
  165. #define HAL_I2C_ERROR_SIZE (0x00000040U) /*!< Size Management error */
  166. /**
  167. * @}
  168. */
  169. /** @defgroup I2C_handle_Structure_definition I2C handle Structure definition
  170. * @brief I2C handle Structure definition
  171. * @{
  172. */
  173. typedef struct __I2C_HandleTypeDef
  174. {
  175. I2C_TypeDef *Instance; /*!< I2C registers base address */
  176. I2C_InitTypeDef Init; /*!< I2C communication parameters */
  177. uint8_t *pBuffPtr; /*!< Pointer to I2C transfer buffer */
  178. uint16_t XferSize; /*!< I2C transfer size */
  179. __IO uint16_t XferCount; /*!< I2C transfer counter */
  180. __IO uint32_t XferOptions; /*!< I2C sequantial transfer options, this parameter can
  181. be a value of @ref I2C_XFEROPTIONS */
  182. __IO uint32_t PreviousState; /*!< I2C communication Previous state */
  183. HAL_StatusTypeDef(*XferISR)(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags, uint32_t ITSources); /*!< I2C transfer IRQ handler function pointer */
  184. DMA_HandleTypeDef *hdmatx; /*!< I2C Tx DMA handle parameters */
  185. DMA_HandleTypeDef *hdmarx; /*!< I2C Rx DMA handle parameters */
  186. HAL_LockTypeDef Lock; /*!< I2C locking object */
  187. __IO HAL_I2C_StateTypeDef State; /*!< I2C communication state */
  188. __IO HAL_I2C_ModeTypeDef Mode; /*!< I2C communication mode */
  189. __IO uint32_t ErrorCode; /*!< I2C Error code */
  190. __IO uint32_t AddrEventCount; /*!< I2C Address Event counter */
  191. } I2C_HandleTypeDef;
  192. /**
  193. * @}
  194. */
  195. /**
  196. * @}
  197. */
  198. /* Exported constants --------------------------------------------------------*/
  199. /** @defgroup I2C_Exported_Constants I2C Exported Constants
  200. * @{
  201. */
  202. /** @defgroup I2C_XFEROPTIONS I2C Sequential Transfer Options
  203. * @{
  204. */
  205. #define I2C_FIRST_FRAME ((uint32_t)I2C_SOFTEND_MODE)
  206. #define I2C_FIRST_AND_NEXT_FRAME ((uint32_t)(I2C_RELOAD_MODE | I2C_SOFTEND_MODE))
  207. #define I2C_NEXT_FRAME ((uint32_t)(I2C_RELOAD_MODE | I2C_SOFTEND_MODE))
  208. #define I2C_FIRST_AND_LAST_FRAME ((uint32_t)I2C_AUTOEND_MODE)
  209. #define I2C_LAST_FRAME ((uint32_t)I2C_AUTOEND_MODE)
  210. /**
  211. * @}
  212. */
  213. /** @defgroup I2C_ADDRESSING_MODE I2C Addressing Mode
  214. * @{
  215. */
  216. #define I2C_ADDRESSINGMODE_7BIT (0x00000001U)
  217. #define I2C_ADDRESSINGMODE_10BIT (0x00000002U)
  218. /**
  219. * @}
  220. */
  221. /** @defgroup I2C_DUAL_ADDRESSING_MODE I2C Dual Addressing Mode
  222. * @{
  223. */
  224. #define I2C_DUALADDRESS_DISABLE (0x00000000U)
  225. #define I2C_DUALADDRESS_ENABLE I2C_OAR2_OA2EN
  226. /**
  227. * @}
  228. */
  229. /** @defgroup I2C_OWN_ADDRESS2_MASKS I2C Own Address2 Masks
  230. * @{
  231. */
  232. #define I2C_OA2_NOMASK ((uint8_t)0x00U)
  233. #define I2C_OA2_MASK01 ((uint8_t)0x01U)
  234. #define I2C_OA2_MASK02 ((uint8_t)0x02U)
  235. #define I2C_OA2_MASK03 ((uint8_t)0x03U)
  236. #define I2C_OA2_MASK04 ((uint8_t)0x04U)
  237. #define I2C_OA2_MASK05 ((uint8_t)0x05U)
  238. #define I2C_OA2_MASK06 ((uint8_t)0x06U)
  239. #define I2C_OA2_MASK07 ((uint8_t)0x07U)
  240. /**
  241. * @}
  242. */
  243. /** @defgroup I2C_GENERAL_CALL_ADDRESSING_MODE I2C General Call Addressing Mode
  244. * @{
  245. */
  246. #define I2C_GENERALCALL_DISABLE (0x00000000U)
  247. #define I2C_GENERALCALL_ENABLE I2C_CR1_GCEN
  248. /**
  249. * @}
  250. */
  251. /** @defgroup I2C_NOSTRETCH_MODE I2C No-Stretch Mode
  252. * @{
  253. */
  254. #define I2C_NOSTRETCH_DISABLE (0x00000000U)
  255. #define I2C_NOSTRETCH_ENABLE I2C_CR1_NOSTRETCH
  256. /**
  257. * @}
  258. */
  259. /** @defgroup I2C_MEMORY_ADDRESS_SIZE I2C Memory Address Size
  260. * @{
  261. */
  262. #define I2C_MEMADD_SIZE_8BIT (0x00000001U)
  263. #define I2C_MEMADD_SIZE_16BIT (0x00000002U)
  264. /**
  265. * @}
  266. */
  267. /** @defgroup I2C_XFERDIRECTION I2C Transfer Direction Master Point of View
  268. * @{
  269. */
  270. #define I2C_DIRECTION_TRANSMIT (0x00000000U)
  271. #define I2C_DIRECTION_RECEIVE (0x00000001U)
  272. /**
  273. * @}
  274. */
  275. /** @defgroup I2C_RELOAD_END_MODE I2C Reload End Mode
  276. * @{
  277. */
  278. #define I2C_RELOAD_MODE I2C_CR2_RELOAD
  279. #define I2C_AUTOEND_MODE I2C_CR2_AUTOEND
  280. #define I2C_SOFTEND_MODE (0x00000000U)
  281. /**
  282. * @}
  283. */
  284. /** @defgroup I2C_START_STOP_MODE I2C Start or Stop Mode
  285. * @{
  286. */
  287. #define I2C_NO_STARTSTOP (0x00000000U)
  288. #define I2C_GENERATE_STOP (uint32_t)(0x80000000U | I2C_CR2_STOP)
  289. #define I2C_GENERATE_START_READ (uint32_t)(0x80000000U | I2C_CR2_START | I2C_CR2_RD_WRN)
  290. #define I2C_GENERATE_START_WRITE (uint32_t)(0x80000000U | I2C_CR2_START)
  291. /**
  292. * @}
  293. */
  294. /** @defgroup I2C_Interrupt_configuration_definition I2C Interrupt configuration definition
  295. * @brief I2C Interrupt definition
  296. * Elements values convention: 0xXXXXXXXX
  297. * - XXXXXXXX : Interrupt control mask
  298. * @{
  299. */
  300. #define I2C_IT_ERRI I2C_CR1_ERRIE
  301. #define I2C_IT_TCI I2C_CR1_TCIE
  302. #define I2C_IT_STOPI I2C_CR1_STOPIE
  303. #define I2C_IT_NACKI I2C_CR1_NACKIE
  304. #define I2C_IT_ADDRI I2C_CR1_ADDRIE
  305. #define I2C_IT_RXI I2C_CR1_RXIE
  306. #define I2C_IT_TXI I2C_CR1_TXIE
  307. /**
  308. * @}
  309. */
  310. /** @defgroup I2C_Flag_definition I2C Flag definition
  311. * @{
  312. */
  313. #define I2C_FLAG_TXE I2C_ISR_TXE
  314. #define I2C_FLAG_TXIS I2C_ISR_TXIS
  315. #define I2C_FLAG_RXNE I2C_ISR_RXNE
  316. #define I2C_FLAG_ADDR I2C_ISR_ADDR
  317. #define I2C_FLAG_AF I2C_ISR_NACKF
  318. #define I2C_FLAG_STOPF I2C_ISR_STOPF
  319. #define I2C_FLAG_TC I2C_ISR_TC
  320. #define I2C_FLAG_TCR I2C_ISR_TCR
  321. #define I2C_FLAG_BERR I2C_ISR_BERR
  322. #define I2C_FLAG_ARLO I2C_ISR_ARLO
  323. #define I2C_FLAG_OVR I2C_ISR_OVR
  324. #define I2C_FLAG_PECERR I2C_ISR_PECERR
  325. #define I2C_FLAG_TIMEOUT I2C_ISR_TIMEOUT
  326. #define I2C_FLAG_ALERT I2C_ISR_ALERT
  327. #define I2C_FLAG_BUSY I2C_ISR_BUSY
  328. #define I2C_FLAG_DIR I2C_ISR_DIR
  329. /**
  330. * @}
  331. */
  332. /**
  333. * @}
  334. */
  335. /* Exported macros -----------------------------------------------------------*/
  336. /** @defgroup I2C_Exported_Macros I2C Exported Macros
  337. * @{
  338. */
  339. /** @brief Reset I2C handle state.
  340. * @param __HANDLE__ specifies the I2C Handle.
  341. * @retval None
  342. */
  343. #define __HAL_I2C_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_I2C_STATE_RESET)
  344. /** @brief Enable the specified I2C interrupt.
  345. * @param __HANDLE__ specifies the I2C Handle.
  346. * @param __INTERRUPT__ specifies the interrupt source to enable.
  347. * This parameter can be one of the following values:
  348. * @arg @ref I2C_IT_ERRI Errors interrupt enable
  349. * @arg @ref I2C_IT_TCI Transfer complete interrupt enable
  350. * @arg @ref I2C_IT_STOPI STOP detection interrupt enable
  351. * @arg @ref I2C_IT_NACKI NACK received interrupt enable
  352. * @arg @ref I2C_IT_ADDRI Address match interrupt enable
  353. * @arg @ref I2C_IT_RXI RX interrupt enable
  354. * @arg @ref I2C_IT_TXI TX interrupt enable
  355. *
  356. * @retval None
  357. */
  358. #define __HAL_I2C_ENABLE_IT(__HANDLE__, __INTERRUPT__) ((__HANDLE__)->Instance->CR1 |= (__INTERRUPT__))
  359. /** @brief Disable the specified I2C interrupt.
  360. * @param __HANDLE__ specifies the I2C Handle.
  361. * @param __INTERRUPT__ specifies the interrupt source to disable.
  362. * This parameter can be one of the following values:
  363. * @arg @ref I2C_IT_ERRI Errors interrupt enable
  364. * @arg @ref I2C_IT_TCI Transfer complete interrupt enable
  365. * @arg @ref I2C_IT_STOPI STOP detection interrupt enable
  366. * @arg @ref I2C_IT_NACKI NACK received interrupt enable
  367. * @arg @ref I2C_IT_ADDRI Address match interrupt enable
  368. * @arg @ref I2C_IT_RXI RX interrupt enable
  369. * @arg @ref I2C_IT_TXI TX interrupt enable
  370. *
  371. * @retval None
  372. */
  373. #define __HAL_I2C_DISABLE_IT(__HANDLE__, __INTERRUPT__) ((__HANDLE__)->Instance->CR1 &= (~(__INTERRUPT__)))
  374. /** @brief Check whether the specified I2C interrupt source is enabled or not.
  375. * @param __HANDLE__ specifies the I2C Handle.
  376. * @param __INTERRUPT__ specifies the I2C interrupt source to check.
  377. * This parameter can be one of the following values:
  378. * @arg @ref I2C_IT_ERRI Errors interrupt enable
  379. * @arg @ref I2C_IT_TCI Transfer complete interrupt enable
  380. * @arg @ref I2C_IT_STOPI STOP detection interrupt enable
  381. * @arg @ref I2C_IT_NACKI NACK received interrupt enable
  382. * @arg @ref I2C_IT_ADDRI Address match interrupt enable
  383. * @arg @ref I2C_IT_RXI RX interrupt enable
  384. * @arg @ref I2C_IT_TXI TX interrupt enable
  385. *
  386. * @retval The new state of __INTERRUPT__ (SET or RESET).
  387. */
  388. #define __HAL_I2C_GET_IT_SOURCE(__HANDLE__, __INTERRUPT__) ((((__HANDLE__)->Instance->CR1 & (__INTERRUPT__)) == (__INTERRUPT__)) ? SET : RESET)
  389. /** @brief Check whether the specified I2C flag is set or not.
  390. * @param __HANDLE__ specifies the I2C Handle.
  391. * @param __FLAG__ specifies the flag to check.
  392. * This parameter can be one of the following values:
  393. * @arg @ref I2C_FLAG_TXE Transmit data register empty
  394. * @arg @ref I2C_FLAG_TXIS Transmit interrupt status
  395. * @arg @ref I2C_FLAG_RXNE Receive data register not empty
  396. * @arg @ref I2C_FLAG_ADDR Address matched (slave mode)
  397. * @arg @ref I2C_FLAG_AF Acknowledge failure received flag
  398. * @arg @ref I2C_FLAG_STOPF STOP detection flag
  399. * @arg @ref I2C_FLAG_TC Transfer complete (master mode)
  400. * @arg @ref I2C_FLAG_TCR Transfer complete reload
  401. * @arg @ref I2C_FLAG_BERR Bus error
  402. * @arg @ref I2C_FLAG_ARLO Arbitration lost
  403. * @arg @ref I2C_FLAG_OVR Overrun/Underrun
  404. * @arg @ref I2C_FLAG_PECERR PEC error in reception
  405. * @arg @ref I2C_FLAG_TIMEOUT Timeout or Tlow detection flag
  406. * @arg @ref I2C_FLAG_ALERT SMBus alert
  407. * @arg @ref I2C_FLAG_BUSY Bus busy
  408. * @arg @ref I2C_FLAG_DIR Transfer direction (slave mode)
  409. *
  410. * @retval The new state of __FLAG__ (SET or RESET).
  411. */
  412. #define __HAL_I2C_GET_FLAG(__HANDLE__, __FLAG__) (((((__HANDLE__)->Instance->ISR) & (__FLAG__)) == (__FLAG__)) ? SET : RESET)
  413. /** @brief Clear the I2C pending flags which are cleared by writing 1 in a specific bit.
  414. * @param __HANDLE__ specifies the I2C Handle.
  415. * @param __FLAG__ specifies the flag to clear.
  416. * This parameter can be any combination of the following values:
  417. * @arg @ref I2C_FLAG_TXE Transmit data register empty
  418. * @arg @ref I2C_FLAG_ADDR Address matched (slave mode)
  419. * @arg @ref I2C_FLAG_AF Acknowledge failure received flag
  420. * @arg @ref I2C_FLAG_STOPF STOP detection flag
  421. * @arg @ref I2C_FLAG_BERR Bus error
  422. * @arg @ref I2C_FLAG_ARLO Arbitration lost
  423. * @arg @ref I2C_FLAG_OVR Overrun/Underrun
  424. * @arg @ref I2C_FLAG_PECERR PEC error in reception
  425. * @arg @ref I2C_FLAG_TIMEOUT Timeout or Tlow detection flag
  426. * @arg @ref I2C_FLAG_ALERT SMBus alert
  427. *
  428. * @retval None
  429. */
  430. #define __HAL_I2C_CLEAR_FLAG(__HANDLE__, __FLAG__) (((__FLAG__) == I2C_FLAG_TXE) ? ((__HANDLE__)->Instance->ISR |= (__FLAG__)) \
  431. : ((__HANDLE__)->Instance->ICR = (__FLAG__)))
  432. /** @brief Enable the specified I2C peripheral.
  433. * @param __HANDLE__ specifies the I2C Handle.
  434. * @retval None
  435. */
  436. #define __HAL_I2C_ENABLE(__HANDLE__) (SET_BIT((__HANDLE__)->Instance->CR1, I2C_CR1_PE))
  437. /** @brief Disable the specified I2C peripheral.
  438. * @param __HANDLE__ specifies the I2C Handle.
  439. * @retval None
  440. */
  441. #define __HAL_I2C_DISABLE(__HANDLE__) (CLEAR_BIT((__HANDLE__)->Instance->CR1, I2C_CR1_PE))
  442. /** @brief Generate a Non-Acknowledge I2C peripheral in Slave mode.
  443. * @param __HANDLE__ specifies the I2C Handle.
  444. * @retval None
  445. */
  446. #define __HAL_I2C_GENERATE_NACK(__HANDLE__) (SET_BIT((__HANDLE__)->Instance->CR2, I2C_CR2_NACK))
  447. /**
  448. * @}
  449. */
  450. /* Include I2C HAL Extended module */
  451. #include "stm32l4xx_hal_i2c_ex.h"
  452. /* Exported functions --------------------------------------------------------*/
  453. /** @addtogroup I2C_Exported_Functions
  454. * @{
  455. */
  456. /** @addtogroup I2C_Exported_Functions_Group1 Initialization and de-initialization functions
  457. * @{
  458. */
  459. /* Initialization and de-initialization functions******************************/
  460. HAL_StatusTypeDef HAL_I2C_Init(I2C_HandleTypeDef *hi2c);
  461. HAL_StatusTypeDef HAL_I2C_DeInit(I2C_HandleTypeDef *hi2c);
  462. void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c);
  463. void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c);
  464. /**
  465. * @}
  466. */
  467. /** @addtogroup I2C_Exported_Functions_Group2 Input and Output operation functions
  468. * @{
  469. */
  470. /* IO operation functions ****************************************************/
  471. /******* Blocking mode: Polling */
  472. HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  473. HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  474. HAL_StatusTypeDef HAL_I2C_Slave_Transmit(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  475. HAL_StatusTypeDef HAL_I2C_Slave_Receive(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  476. 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);
  477. 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);
  478. HAL_StatusTypeDef HAL_I2C_IsDeviceReady(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout);
  479. /******* Non-Blocking mode: Interrupt */
  480. HAL_StatusTypeDef HAL_I2C_Master_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size);
  481. HAL_StatusTypeDef HAL_I2C_Master_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size);
  482. HAL_StatusTypeDef HAL_I2C_Slave_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size);
  483. HAL_StatusTypeDef HAL_I2C_Slave_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size);
  484. 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);
  485. 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);
  486. HAL_StatusTypeDef HAL_I2C_Master_Sequential_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions);
  487. HAL_StatusTypeDef HAL_I2C_Master_Sequential_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions);
  488. HAL_StatusTypeDef HAL_I2C_Slave_Sequential_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions);
  489. HAL_StatusTypeDef HAL_I2C_Slave_Sequential_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions);
  490. HAL_StatusTypeDef HAL_I2C_EnableListen_IT(I2C_HandleTypeDef *hi2c);
  491. HAL_StatusTypeDef HAL_I2C_DisableListen_IT(I2C_HandleTypeDef *hi2c);
  492. HAL_StatusTypeDef HAL_I2C_Master_Abort_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress);
  493. /******* Non-Blocking mode: DMA */
  494. HAL_StatusTypeDef HAL_I2C_Master_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size);
  495. HAL_StatusTypeDef HAL_I2C_Master_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size);
  496. HAL_StatusTypeDef HAL_I2C_Slave_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size);
  497. HAL_StatusTypeDef HAL_I2C_Slave_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size);
  498. 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);
  499. 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);
  500. /**
  501. * @}
  502. */
  503. /** @addtogroup I2C_IRQ_Handler_and_Callbacks IRQ Handler and Callbacks
  504. * @{
  505. */
  506. /******* I2C IRQHandler and Callbacks used in non blocking modes (Interrupt and DMA) */
  507. void HAL_I2C_EV_IRQHandler(I2C_HandleTypeDef *hi2c);
  508. void HAL_I2C_ER_IRQHandler(I2C_HandleTypeDef *hi2c);
  509. void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c);
  510. void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c);
  511. void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c);
  512. void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c);
  513. void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode);
  514. void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c);
  515. void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef *hi2c);
  516. void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c);
  517. void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c);
  518. void HAL_I2C_AbortCpltCallback(I2C_HandleTypeDef *hi2c);
  519. /**
  520. * @}
  521. */
  522. /** @addtogroup I2C_Exported_Functions_Group3 Peripheral State, Mode and Error functions
  523. * @{
  524. */
  525. /* Peripheral State, Mode and Error functions *********************************/
  526. HAL_I2C_StateTypeDef HAL_I2C_GetState(I2C_HandleTypeDef *hi2c);
  527. HAL_I2C_ModeTypeDef HAL_I2C_GetMode(I2C_HandleTypeDef *hi2c);
  528. uint32_t HAL_I2C_GetError(I2C_HandleTypeDef *hi2c);
  529. /**
  530. * @}
  531. */
  532. /**
  533. * @}
  534. */
  535. /* Private constants ---------------------------------------------------------*/
  536. /** @defgroup I2C_Private_Constants I2C Private Constants
  537. * @{
  538. */
  539. /**
  540. * @}
  541. */
  542. /* Private macros ------------------------------------------------------------*/
  543. /** @defgroup I2C_Private_Macro I2C Private Macros
  544. * @{
  545. */
  546. #define IS_I2C_ADDRESSING_MODE(MODE) (((MODE) == I2C_ADDRESSINGMODE_7BIT) || \
  547. ((MODE) == I2C_ADDRESSINGMODE_10BIT))
  548. #define IS_I2C_DUAL_ADDRESS(ADDRESS) (((ADDRESS) == I2C_DUALADDRESS_DISABLE) || \
  549. ((ADDRESS) == I2C_DUALADDRESS_ENABLE))
  550. #define IS_I2C_OWN_ADDRESS2_MASK(MASK) (((MASK) == I2C_OA2_NOMASK) || \
  551. ((MASK) == I2C_OA2_MASK01) || \
  552. ((MASK) == I2C_OA2_MASK02) || \
  553. ((MASK) == I2C_OA2_MASK03) || \
  554. ((MASK) == I2C_OA2_MASK04) || \
  555. ((MASK) == I2C_OA2_MASK05) || \
  556. ((MASK) == I2C_OA2_MASK06) || \
  557. ((MASK) == I2C_OA2_MASK07))
  558. #define IS_I2C_GENERAL_CALL(CALL) (((CALL) == I2C_GENERALCALL_DISABLE) || \
  559. ((CALL) == I2C_GENERALCALL_ENABLE))
  560. #define IS_I2C_NO_STRETCH(STRETCH) (((STRETCH) == I2C_NOSTRETCH_DISABLE) || \
  561. ((STRETCH) == I2C_NOSTRETCH_ENABLE))
  562. #define IS_I2C_MEMADD_SIZE(SIZE) (((SIZE) == I2C_MEMADD_SIZE_8BIT) || \
  563. ((SIZE) == I2C_MEMADD_SIZE_16BIT))
  564. #define IS_TRANSFER_MODE(MODE) (((MODE) == I2C_RELOAD_MODE) || \
  565. ((MODE) == I2C_AUTOEND_MODE) || \
  566. ((MODE) == I2C_SOFTEND_MODE))
  567. #define IS_TRANSFER_REQUEST(REQUEST) (((REQUEST) == I2C_GENERATE_STOP) || \
  568. ((REQUEST) == I2C_GENERATE_START_READ) || \
  569. ((REQUEST) == I2C_GENERATE_START_WRITE) || \
  570. ((REQUEST) == I2C_NO_STARTSTOP))
  571. #define IS_I2C_TRANSFER_OPTIONS_REQUEST(REQUEST) (((REQUEST) == I2C_FIRST_FRAME) || \
  572. ((REQUEST) == I2C_FIRST_AND_NEXT_FRAME) || \
  573. ((REQUEST) == I2C_NEXT_FRAME) || \
  574. ((REQUEST) == I2C_FIRST_AND_LAST_FRAME) || \
  575. ((REQUEST) == I2C_LAST_FRAME))
  576. #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)))
  577. #define I2C_GET_ADDR_MATCH(__HANDLE__) (((__HANDLE__)->Instance->ISR & I2C_ISR_ADDCODE) >> 16U)
  578. #define I2C_GET_DIR(__HANDLE__) (((__HANDLE__)->Instance->ISR & I2C_ISR_DIR) >> 16U)
  579. #define I2C_GET_STOP_MODE(__HANDLE__) ((__HANDLE__)->Instance->CR2 & I2C_CR2_AUTOEND)
  580. #define I2C_GET_OWN_ADDRESS1(__HANDLE__) ((__HANDLE__)->Instance->OAR1 & I2C_OAR1_OA1)
  581. #define I2C_GET_OWN_ADDRESS2(__HANDLE__) ((__HANDLE__)->Instance->OAR2 & I2C_OAR2_OA2)
  582. #define IS_I2C_OWN_ADDRESS1(ADDRESS1) ((ADDRESS1) <= 0x000003FFU)
  583. #define IS_I2C_OWN_ADDRESS2(ADDRESS2) ((ADDRESS2) <= (uint16_t)0x00FFU)
  584. #define I2C_MEM_ADD_MSB(__ADDRESS__) ((uint8_t)((uint16_t)(((uint16_t)((__ADDRESS__) & (uint16_t)(0xFF00U))) >> 8U)))
  585. #define I2C_MEM_ADD_LSB(__ADDRESS__) ((uint8_t)((uint16_t)((__ADDRESS__) & (uint16_t)(0x00FFU))))
  586. #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)) : \
  587. (uint32_t)((((uint32_t)(__ADDRESS__) & (I2C_CR2_SADD)) | (I2C_CR2_ADD10) | (I2C_CR2_START)) & (~I2C_CR2_RD_WRN)))
  588. /**
  589. * @}
  590. */
  591. /* Private Functions ---------------------------------------------------------*/
  592. /** @defgroup I2C_Private_Functions I2C Private Functions
  593. * @{
  594. */
  595. /* Private functions are defined in stm32l4xx_hal_i2c.c file */
  596. /**
  597. * @}
  598. */
  599. /**
  600. * @}
  601. */
  602. /**
  603. * @}
  604. */
  605. #ifdef __cplusplus
  606. }
  607. #endif
  608. #endif /* __STM32L4xx_HAL_I2C_H */
  609. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/