stm32f1xx_hal_i2c.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_hal_i2c.h
  4. * @author MCD Application Team
  5. * @brief Header file of I2C HAL module.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2016 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* Define to prevent recursive inclusion -------------------------------------*/
  19. #ifndef __STM32F1xx_HAL_I2C_H
  20. #define __STM32F1xx_HAL_I2C_H
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. /* Includes ------------------------------------------------------------------*/
  25. #include "stm32f1xx_hal_def.h"
  26. /** @addtogroup STM32F1xx_HAL_Driver
  27. * @{
  28. */
  29. /** @addtogroup I2C
  30. * @{
  31. */
  32. /* Exported types ------------------------------------------------------------*/
  33. /** @defgroup I2C_Exported_Types I2C Exported Types
  34. * @{
  35. */
  36. /** @defgroup I2C_Configuration_Structure_definition I2C Configuration Structure definition
  37. * @brief I2C Configuration Structure definition
  38. * @{
  39. */
  40. typedef struct
  41. {
  42. uint32_t ClockSpeed; /*!< Specifies the clock frequency.
  43. This parameter must be set to a value lower than 400kHz */
  44. uint32_t DutyCycle; /*!< Specifies the I2C fast mode duty cycle.
  45. This parameter can be a value of @ref I2C_duty_cycle_in_fast_mode */
  46. uint32_t OwnAddress1; /*!< Specifies the first device own address.
  47. This parameter can be a 7-bit or 10-bit address. */
  48. uint32_t AddressingMode; /*!< Specifies if 7-bit or 10-bit addressing mode is selected.
  49. This parameter can be a value of @ref I2C_addressing_mode */
  50. uint32_t DualAddressMode; /*!< Specifies if dual addressing mode is selected.
  51. This parameter can be a value of @ref I2C_dual_addressing_mode */
  52. uint32_t OwnAddress2; /*!< Specifies the second device own address if dual addressing mode is selected
  53. This parameter can be a 7-bit address. */
  54. uint32_t GeneralCallMode; /*!< Specifies if general call mode is selected.
  55. This parameter can be a value of @ref I2C_general_call_addressing_mode */
  56. uint32_t NoStretchMode; /*!< Specifies if nostretch mode is selected.
  57. This parameter can be a value of @ref I2C_nostretch_mode */
  58. } I2C_InitTypeDef;
  59. /**
  60. * @}
  61. */
  62. /** @defgroup HAL_state_structure_definition HAL state structure definition
  63. * @brief HAL State structure definition
  64. * @note HAL I2C State value coding follow below described bitmap :
  65. * b7-b6 Error information
  66. * 00 : No Error
  67. * 01 : Abort (Abort user request on going)
  68. * 10 : Timeout
  69. * 11 : Error
  70. * b5 Peripheral initialization status
  71. * 0 : Reset (Peripheral not initialized)
  72. * 1 : Init done (Peripheral initialized and ready to use. HAL I2C Init function called)
  73. * b4 (not used)
  74. * x : Should be set to 0
  75. * b3
  76. * 0 : Ready or Busy (No Listen mode ongoing)
  77. * 1 : Listen (Peripheral in Address Listen Mode)
  78. * b2 Intrinsic process state
  79. * 0 : Ready
  80. * 1 : Busy (Peripheral busy with some configuration or internal operations)
  81. * b1 Rx state
  82. * 0 : Ready (no Rx operation ongoing)
  83. * 1 : Busy (Rx operation ongoing)
  84. * b0 Tx state
  85. * 0 : Ready (no Tx operation ongoing)
  86. * 1 : Busy (Tx operation ongoing)
  87. * @{
  88. */
  89. typedef enum
  90. {
  91. HAL_I2C_STATE_RESET = 0x00U, /*!< Peripheral is not yet Initialized */
  92. HAL_I2C_STATE_READY = 0x20U, /*!< Peripheral Initialized and ready for use */
  93. HAL_I2C_STATE_BUSY = 0x24U, /*!< An internal process is ongoing */
  94. HAL_I2C_STATE_BUSY_TX = 0x21U, /*!< Data Transmission process is ongoing */
  95. HAL_I2C_STATE_BUSY_RX = 0x22U, /*!< Data Reception process is ongoing */
  96. HAL_I2C_STATE_LISTEN = 0x28U, /*!< Address Listen Mode is ongoing */
  97. HAL_I2C_STATE_BUSY_TX_LISTEN = 0x29U, /*!< Address Listen Mode and Data Transmission
  98. process is ongoing */
  99. HAL_I2C_STATE_BUSY_RX_LISTEN = 0x2AU, /*!< Address Listen Mode and Data Reception
  100. process is ongoing */
  101. HAL_I2C_STATE_ABORT = 0x60U, /*!< Abort user request ongoing */
  102. HAL_I2C_STATE_TIMEOUT = 0xA0U, /*!< Timeout state */
  103. HAL_I2C_STATE_ERROR = 0xE0U /*!< Error */
  104. } HAL_I2C_StateTypeDef;
  105. /**
  106. * @}
  107. */
  108. /** @defgroup HAL_mode_structure_definition HAL mode structure definition
  109. * @brief HAL Mode structure definition
  110. * @note HAL I2C Mode value coding follow below described bitmap :\n
  111. * b7 (not used)\n
  112. * x : Should be set to 0\n
  113. * b6\n
  114. * 0 : None\n
  115. * 1 : Memory (HAL I2C communication is in Memory Mode)\n
  116. * b5\n
  117. * 0 : None\n
  118. * 1 : Slave (HAL I2C communication is in Slave Mode)\n
  119. * b4\n
  120. * 0 : None\n
  121. * 1 : Master (HAL I2C communication is in Master Mode)\n
  122. * b3-b2-b1-b0 (not used)\n
  123. * xxxx : Should be set to 0000
  124. * @{
  125. */
  126. typedef enum
  127. {
  128. HAL_I2C_MODE_NONE = 0x00U, /*!< No I2C communication on going */
  129. HAL_I2C_MODE_MASTER = 0x10U, /*!< I2C communication is in Master Mode */
  130. HAL_I2C_MODE_SLAVE = 0x20U, /*!< I2C communication is in Slave Mode */
  131. HAL_I2C_MODE_MEM = 0x40U /*!< I2C communication is in Memory Mode */
  132. } HAL_I2C_ModeTypeDef;
  133. /**
  134. * @}
  135. */
  136. /** @defgroup I2C_Error_Code_definition I2C Error Code definition
  137. * @brief I2C Error Code definition
  138. * @{
  139. */
  140. #define HAL_I2C_ERROR_NONE 0x00000000U /*!< No error */
  141. #define HAL_I2C_ERROR_BERR 0x00000001U /*!< BERR error */
  142. #define HAL_I2C_ERROR_ARLO 0x00000002U /*!< ARLO error */
  143. #define HAL_I2C_ERROR_AF 0x00000004U /*!< AF error */
  144. #define HAL_I2C_ERROR_OVR 0x00000008U /*!< OVR error */
  145. #define HAL_I2C_ERROR_DMA 0x00000010U /*!< DMA transfer error */
  146. #define HAL_I2C_ERROR_TIMEOUT 0x00000020U /*!< Timeout Error */
  147. #define HAL_I2C_ERROR_SIZE 0x00000040U /*!< Size Management error */
  148. #define HAL_I2C_ERROR_DMA_PARAM 0x00000080U /*!< DMA Parameter Error */
  149. #define HAL_I2C_WRONG_START 0x00000200U /*!< Wrong start Error */
  150. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  151. #define HAL_I2C_ERROR_INVALID_CALLBACK 0x00000100U /*!< Invalid Callback error */
  152. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  153. /**
  154. * @}
  155. */
  156. /** @defgroup I2C_handle_Structure_definition I2C handle Structure definition
  157. * @brief I2C handle Structure definition
  158. * @{
  159. */
  160. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  161. typedef struct __I2C_HandleTypeDef
  162. #else
  163. typedef struct
  164. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  165. {
  166. I2C_TypeDef *Instance; /*!< I2C registers base address */
  167. I2C_InitTypeDef Init; /*!< I2C communication parameters */
  168. uint8_t *pBuffPtr; /*!< Pointer to I2C transfer buffer */
  169. uint16_t XferSize; /*!< I2C transfer size */
  170. __IO uint16_t XferCount; /*!< I2C transfer counter */
  171. __IO uint32_t XferOptions; /*!< I2C transfer options */
  172. __IO uint32_t PreviousState; /*!< I2C communication Previous state and mode
  173. context for internal usage */
  174. DMA_HandleTypeDef *hdmatx; /*!< I2C Tx DMA handle parameters */
  175. DMA_HandleTypeDef *hdmarx; /*!< I2C Rx DMA handle parameters */
  176. HAL_LockTypeDef Lock; /*!< I2C locking object */
  177. __IO HAL_I2C_StateTypeDef State; /*!< I2C communication state */
  178. __IO HAL_I2C_ModeTypeDef Mode; /*!< I2C communication mode */
  179. __IO uint32_t ErrorCode; /*!< I2C Error code */
  180. __IO uint32_t Devaddress; /*!< I2C Target device address */
  181. __IO uint32_t Memaddress; /*!< I2C Target memory address */
  182. __IO uint32_t MemaddSize; /*!< I2C Target memory address size */
  183. __IO uint32_t EventCount; /*!< I2C Event counter */
  184. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  185. void (* MasterTxCpltCallback)(struct __I2C_HandleTypeDef *hi2c); /*!< I2C Master Tx Transfer completed callback */
  186. void (* MasterRxCpltCallback)(struct __I2C_HandleTypeDef *hi2c); /*!< I2C Master Rx Transfer completed callback */
  187. void (* SlaveTxCpltCallback)(struct __I2C_HandleTypeDef *hi2c); /*!< I2C Slave Tx Transfer completed callback */
  188. void (* SlaveRxCpltCallback)(struct __I2C_HandleTypeDef *hi2c); /*!< I2C Slave Rx Transfer completed callback */
  189. void (* ListenCpltCallback)(struct __I2C_HandleTypeDef *hi2c); /*!< I2C Listen Complete callback */
  190. void (* MemTxCpltCallback)(struct __I2C_HandleTypeDef *hi2c); /*!< I2C Memory Tx Transfer completed callback */
  191. void (* MemRxCpltCallback)(struct __I2C_HandleTypeDef *hi2c); /*!< I2C Memory Rx Transfer completed callback */
  192. void (* ErrorCallback)(struct __I2C_HandleTypeDef *hi2c); /*!< I2C Error callback */
  193. void (* AbortCpltCallback)(struct __I2C_HandleTypeDef *hi2c); /*!< I2C Abort callback */
  194. void (* AddrCallback)(struct __I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode); /*!< I2C Slave Address Match callback */
  195. void (* MspInitCallback)(struct __I2C_HandleTypeDef *hi2c); /*!< I2C Msp Init callback */
  196. void (* MspDeInitCallback)(struct __I2C_HandleTypeDef *hi2c); /*!< I2C Msp DeInit callback */
  197. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  198. } I2C_HandleTypeDef;
  199. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  200. /**
  201. * @brief HAL I2C Callback ID enumeration definition
  202. */
  203. typedef enum
  204. {
  205. HAL_I2C_MASTER_TX_COMPLETE_CB_ID = 0x00U, /*!< I2C Master Tx Transfer completed callback ID */
  206. HAL_I2C_MASTER_RX_COMPLETE_CB_ID = 0x01U, /*!< I2C Master Rx Transfer completed callback ID */
  207. HAL_I2C_SLAVE_TX_COMPLETE_CB_ID = 0x02U, /*!< I2C Slave Tx Transfer completed callback ID */
  208. HAL_I2C_SLAVE_RX_COMPLETE_CB_ID = 0x03U, /*!< I2C Slave Rx Transfer completed callback ID */
  209. HAL_I2C_LISTEN_COMPLETE_CB_ID = 0x04U, /*!< I2C Listen Complete callback ID */
  210. HAL_I2C_MEM_TX_COMPLETE_CB_ID = 0x05U, /*!< I2C Memory Tx Transfer callback ID */
  211. HAL_I2C_MEM_RX_COMPLETE_CB_ID = 0x06U, /*!< I2C Memory Rx Transfer completed callback ID */
  212. HAL_I2C_ERROR_CB_ID = 0x07U, /*!< I2C Error callback ID */
  213. HAL_I2C_ABORT_CB_ID = 0x08U, /*!< I2C Abort callback ID */
  214. HAL_I2C_MSPINIT_CB_ID = 0x09U, /*!< I2C Msp Init callback ID */
  215. HAL_I2C_MSPDEINIT_CB_ID = 0x0AU /*!< I2C Msp DeInit callback ID */
  216. } HAL_I2C_CallbackIDTypeDef;
  217. /**
  218. * @brief HAL I2C Callback pointer definition
  219. */
  220. typedef void (*pI2C_CallbackTypeDef)(I2C_HandleTypeDef *hi2c); /*!< pointer to an I2C callback function */
  221. typedef void (*pI2C_AddrCallbackTypeDef)(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode); /*!< pointer to an I2C Address Match callback function */
  222. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  223. /**
  224. * @}
  225. */
  226. /**
  227. * @}
  228. */
  229. /* Exported constants --------------------------------------------------------*/
  230. /** @defgroup I2C_Exported_Constants I2C Exported Constants
  231. * @{
  232. */
  233. /** @defgroup I2C_duty_cycle_in_fast_mode I2C duty cycle in fast mode
  234. * @{
  235. */
  236. #define I2C_DUTYCYCLE_2 0x00000000U
  237. #define I2C_DUTYCYCLE_16_9 I2C_CCR_DUTY
  238. /**
  239. * @}
  240. */
  241. /** @defgroup I2C_addressing_mode I2C addressing mode
  242. * @{
  243. */
  244. #define I2C_ADDRESSINGMODE_7BIT 0x00004000U
  245. #define I2C_ADDRESSINGMODE_10BIT (I2C_OAR1_ADDMODE | 0x00004000U)
  246. /**
  247. * @}
  248. */
  249. /** @defgroup I2C_dual_addressing_mode I2C dual addressing mode
  250. * @{
  251. */
  252. #define I2C_DUALADDRESS_DISABLE 0x00000000U
  253. #define I2C_DUALADDRESS_ENABLE I2C_OAR2_ENDUAL
  254. /**
  255. * @}
  256. */
  257. /** @defgroup I2C_general_call_addressing_mode I2C general call addressing mode
  258. * @{
  259. */
  260. #define I2C_GENERALCALL_DISABLE 0x00000000U
  261. #define I2C_GENERALCALL_ENABLE I2C_CR1_ENGC
  262. /**
  263. * @}
  264. */
  265. /** @defgroup I2C_nostretch_mode I2C nostretch mode
  266. * @{
  267. */
  268. #define I2C_NOSTRETCH_DISABLE 0x00000000U
  269. #define I2C_NOSTRETCH_ENABLE I2C_CR1_NOSTRETCH
  270. /**
  271. * @}
  272. */
  273. /** @defgroup I2C_Memory_Address_Size I2C Memory Address Size
  274. * @{
  275. */
  276. #define I2C_MEMADD_SIZE_8BIT 0x00000001U
  277. #define I2C_MEMADD_SIZE_16BIT 0x00000010U
  278. /**
  279. * @}
  280. */
  281. /** @defgroup I2C_XferDirection_definition I2C XferDirection definition
  282. * @{
  283. */
  284. #define I2C_DIRECTION_RECEIVE 0x00000000U
  285. #define I2C_DIRECTION_TRANSMIT 0x00000001U
  286. /**
  287. * @}
  288. */
  289. /** @defgroup I2C_XferOptions_definition I2C XferOptions definition
  290. * @{
  291. */
  292. #define I2C_FIRST_FRAME 0x00000001U
  293. #define I2C_FIRST_AND_NEXT_FRAME 0x00000002U
  294. #define I2C_NEXT_FRAME 0x00000004U
  295. #define I2C_FIRST_AND_LAST_FRAME 0x00000008U
  296. #define I2C_LAST_FRAME_NO_STOP 0x00000010U
  297. #define I2C_LAST_FRAME 0x00000020U
  298. /* List of XferOptions in usage of :
  299. * 1- Restart condition in all use cases (direction change or not)
  300. */
  301. #define I2C_OTHER_FRAME (0x00AA0000U)
  302. #define I2C_OTHER_AND_LAST_FRAME (0xAA000000U)
  303. /**
  304. * @}
  305. */
  306. /** @defgroup I2C_Interrupt_configuration_definition I2C Interrupt configuration definition
  307. * @brief I2C Interrupt definition
  308. * Elements values convention: 0xXXXXXXXX
  309. * - XXXXXXXX : Interrupt control mask
  310. * @{
  311. */
  312. #define I2C_IT_BUF I2C_CR2_ITBUFEN
  313. #define I2C_IT_EVT I2C_CR2_ITEVTEN
  314. #define I2C_IT_ERR I2C_CR2_ITERREN
  315. /**
  316. * @}
  317. */
  318. /** @defgroup I2C_Flag_definition I2C Flag definition
  319. * @{
  320. */
  321. #define I2C_FLAG_OVR 0x00010800U
  322. #define I2C_FLAG_AF 0x00010400U
  323. #define I2C_FLAG_ARLO 0x00010200U
  324. #define I2C_FLAG_BERR 0x00010100U
  325. #define I2C_FLAG_TXE 0x00010080U
  326. #define I2C_FLAG_RXNE 0x00010040U
  327. #define I2C_FLAG_STOPF 0x00010010U
  328. #define I2C_FLAG_ADD10 0x00010008U
  329. #define I2C_FLAG_BTF 0x00010004U
  330. #define I2C_FLAG_ADDR 0x00010002U
  331. #define I2C_FLAG_SB 0x00010001U
  332. #define I2C_FLAG_DUALF 0x00100080U
  333. #define I2C_FLAG_GENCALL 0x00100010U
  334. #define I2C_FLAG_TRA 0x00100004U
  335. #define I2C_FLAG_BUSY 0x00100002U
  336. #define I2C_FLAG_MSL 0x00100001U
  337. /**
  338. * @}
  339. */
  340. /**
  341. * @}
  342. */
  343. /* Exported macros -----------------------------------------------------------*/
  344. /** @defgroup I2C_Exported_Macros I2C Exported Macros
  345. * @{
  346. */
  347. /** @brief Reset I2C handle state.
  348. * @param __HANDLE__ specifies the I2C Handle.
  349. * @retval None
  350. */
  351. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  352. #define __HAL_I2C_RESET_HANDLE_STATE(__HANDLE__) do{ \
  353. (__HANDLE__)->State = HAL_I2C_STATE_RESET; \
  354. (__HANDLE__)->MspInitCallback = NULL; \
  355. (__HANDLE__)->MspDeInitCallback = NULL; \
  356. } while(0)
  357. #else
  358. #define __HAL_I2C_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_I2C_STATE_RESET)
  359. #endif
  360. /** @brief Enable or disable the specified I2C interrupts.
  361. * @param __HANDLE__ specifies the I2C Handle.
  362. * @param __INTERRUPT__ specifies the interrupt source to enable or disable.
  363. * This parameter can be one of the following values:
  364. * @arg I2C_IT_BUF: Buffer interrupt enable
  365. * @arg I2C_IT_EVT: Event interrupt enable
  366. * @arg I2C_IT_ERR: Error interrupt enable
  367. * @retval None
  368. */
  369. #define __HAL_I2C_ENABLE_IT(__HANDLE__, __INTERRUPT__) SET_BIT((__HANDLE__)->Instance->CR2,(__INTERRUPT__))
  370. #define __HAL_I2C_DISABLE_IT(__HANDLE__, __INTERRUPT__) CLEAR_BIT((__HANDLE__)->Instance->CR2, (__INTERRUPT__))
  371. /** @brief Checks if the specified I2C interrupt source is enabled or disabled.
  372. * @param __HANDLE__ specifies the I2C Handle.
  373. * @param __INTERRUPT__ specifies the I2C interrupt source to check.
  374. * This parameter can be one of the following values:
  375. * @arg I2C_IT_BUF: Buffer interrupt enable
  376. * @arg I2C_IT_EVT: Event interrupt enable
  377. * @arg I2C_IT_ERR: Error interrupt enable
  378. * @retval The new state of __INTERRUPT__ (TRUE or FALSE).
  379. */
  380. #define __HAL_I2C_GET_IT_SOURCE(__HANDLE__, __INTERRUPT__) ((((__HANDLE__)->Instance->CR2 & (__INTERRUPT__)) == (__INTERRUPT__)) ? SET : RESET)
  381. /** @brief Checks whether the specified I2C flag is set or not.
  382. * @param __HANDLE__ specifies the I2C Handle.
  383. * @param __FLAG__ specifies the flag to check.
  384. * This parameter can be one of the following values:
  385. * @arg I2C_FLAG_OVR: Overrun/Underrun flag
  386. * @arg I2C_FLAG_AF: Acknowledge failure flag
  387. * @arg I2C_FLAG_ARLO: Arbitration lost flag
  388. * @arg I2C_FLAG_BERR: Bus error flag
  389. * @arg I2C_FLAG_TXE: Data register empty flag
  390. * @arg I2C_FLAG_RXNE: Data register not empty flag
  391. * @arg I2C_FLAG_STOPF: Stop detection flag
  392. * @arg I2C_FLAG_ADD10: 10-bit header sent flag
  393. * @arg I2C_FLAG_BTF: Byte transfer finished flag
  394. * @arg I2C_FLAG_ADDR: Address sent flag
  395. * Address matched flag
  396. * @arg I2C_FLAG_SB: Start bit flag
  397. * @arg I2C_FLAG_DUALF: Dual flag
  398. * @arg I2C_FLAG_GENCALL: General call header flag
  399. * @arg I2C_FLAG_TRA: Transmitter/Receiver flag
  400. * @arg I2C_FLAG_BUSY: Bus busy flag
  401. * @arg I2C_FLAG_MSL: Master/Slave flag
  402. * @retval The new state of __FLAG__ (TRUE or FALSE).
  403. */
  404. #define __HAL_I2C_GET_FLAG(__HANDLE__, __FLAG__) ((((uint8_t)((__FLAG__) >> 16U)) == 0x01U) ? \
  405. (((((__HANDLE__)->Instance->SR1) & ((__FLAG__) & I2C_FLAG_MASK)) == ((__FLAG__) & I2C_FLAG_MASK)) ? SET : RESET) : \
  406. (((((__HANDLE__)->Instance->SR2) & ((__FLAG__) & I2C_FLAG_MASK)) == ((__FLAG__) & I2C_FLAG_MASK)) ? SET : RESET))
  407. /** @brief Clears the I2C pending flags which are cleared by writing 0 in a specific bit.
  408. * @param __HANDLE__ specifies the I2C Handle.
  409. * @param __FLAG__ specifies the flag to clear.
  410. * This parameter can be any combination of the following values:
  411. * @arg I2C_FLAG_OVR: Overrun/Underrun flag (Slave mode)
  412. * @arg I2C_FLAG_AF: Acknowledge failure flag
  413. * @arg I2C_FLAG_ARLO: Arbitration lost flag (Master mode)
  414. * @arg I2C_FLAG_BERR: Bus error flag
  415. * @retval None
  416. */
  417. #define __HAL_I2C_CLEAR_FLAG(__HANDLE__, __FLAG__) ((__HANDLE__)->Instance->SR1 = ~((__FLAG__) & I2C_FLAG_MASK))
  418. /** @brief Clears the I2C ADDR pending flag.
  419. * @param __HANDLE__ specifies the I2C Handle.
  420. * This parameter can be I2C where x: 1, 2, or 3 to select the I2C peripheral.
  421. * @retval None
  422. */
  423. #define __HAL_I2C_CLEAR_ADDRFLAG(__HANDLE__) \
  424. do{ \
  425. __IO uint32_t tmpreg = 0x00U; \
  426. tmpreg = (__HANDLE__)->Instance->SR1; \
  427. tmpreg = (__HANDLE__)->Instance->SR2; \
  428. UNUSED(tmpreg); \
  429. } while(0)
  430. /** @brief Clears the I2C STOPF pending flag.
  431. * @param __HANDLE__ specifies the I2C Handle.
  432. * @retval None
  433. */
  434. #define __HAL_I2C_CLEAR_STOPFLAG(__HANDLE__) \
  435. do{ \
  436. __IO uint32_t tmpreg = 0x00U; \
  437. tmpreg = (__HANDLE__)->Instance->SR1; \
  438. SET_BIT((__HANDLE__)->Instance->CR1, I2C_CR1_PE); \
  439. UNUSED(tmpreg); \
  440. } while(0)
  441. /** @brief Enable the specified I2C peripheral.
  442. * @param __HANDLE__ specifies the I2C Handle.
  443. * @retval None
  444. */
  445. #define __HAL_I2C_ENABLE(__HANDLE__) SET_BIT((__HANDLE__)->Instance->CR1, I2C_CR1_PE)
  446. /** @brief Disable the specified I2C peripheral.
  447. * @param __HANDLE__ specifies the I2C Handle.
  448. * @retval None
  449. */
  450. #define __HAL_I2C_DISABLE(__HANDLE__) CLEAR_BIT((__HANDLE__)->Instance->CR1, I2C_CR1_PE)
  451. /**
  452. * @}
  453. */
  454. /* Exported functions --------------------------------------------------------*/
  455. /** @addtogroup I2C_Exported_Functions
  456. * @{
  457. */
  458. /** @addtogroup I2C_Exported_Functions_Group1 Initialization and de-initialization functions
  459. * @{
  460. */
  461. /* Initialization and de-initialization functions******************************/
  462. HAL_StatusTypeDef HAL_I2C_Init(I2C_HandleTypeDef *hi2c);
  463. HAL_StatusTypeDef HAL_I2C_DeInit(I2C_HandleTypeDef *hi2c);
  464. void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c);
  465. void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c);
  466. /* Callbacks Register/UnRegister functions ***********************************/
  467. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  468. HAL_StatusTypeDef HAL_I2C_RegisterCallback(I2C_HandleTypeDef *hi2c, HAL_I2C_CallbackIDTypeDef CallbackID, pI2C_CallbackTypeDef pCallback);
  469. HAL_StatusTypeDef HAL_I2C_UnRegisterCallback(I2C_HandleTypeDef *hi2c, HAL_I2C_CallbackIDTypeDef CallbackID);
  470. HAL_StatusTypeDef HAL_I2C_RegisterAddrCallback(I2C_HandleTypeDef *hi2c, pI2C_AddrCallbackTypeDef pCallback);
  471. HAL_StatusTypeDef HAL_I2C_UnRegisterAddrCallback(I2C_HandleTypeDef *hi2c);
  472. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  473. /**
  474. * @}
  475. */
  476. /** @addtogroup I2C_Exported_Functions_Group2 Input and Output operation functions
  477. * @{
  478. */
  479. /* IO operation functions ****************************************************/
  480. /******* Blocking mode: Polling */
  481. HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  482. HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  483. HAL_StatusTypeDef HAL_I2C_Slave_Transmit(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  484. HAL_StatusTypeDef HAL_I2C_Slave_Receive(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  485. 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);
  486. 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);
  487. HAL_StatusTypeDef HAL_I2C_IsDeviceReady(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout);
  488. /******* Non-Blocking mode: Interrupt */
  489. HAL_StatusTypeDef HAL_I2C_Master_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size);
  490. HAL_StatusTypeDef HAL_I2C_Master_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size);
  491. HAL_StatusTypeDef HAL_I2C_Slave_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size);
  492. HAL_StatusTypeDef HAL_I2C_Slave_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size);
  493. 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);
  494. 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);
  495. HAL_StatusTypeDef HAL_I2C_Master_Seq_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions);
  496. HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions);
  497. HAL_StatusTypeDef HAL_I2C_Slave_Seq_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions);
  498. HAL_StatusTypeDef HAL_I2C_Slave_Seq_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions);
  499. HAL_StatusTypeDef HAL_I2C_EnableListen_IT(I2C_HandleTypeDef *hi2c);
  500. HAL_StatusTypeDef HAL_I2C_DisableListen_IT(I2C_HandleTypeDef *hi2c);
  501. HAL_StatusTypeDef HAL_I2C_Master_Abort_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress);
  502. /******* Non-Blocking mode: DMA */
  503. HAL_StatusTypeDef HAL_I2C_Master_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size);
  504. HAL_StatusTypeDef HAL_I2C_Master_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size);
  505. HAL_StatusTypeDef HAL_I2C_Slave_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size);
  506. HAL_StatusTypeDef HAL_I2C_Slave_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size);
  507. 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);
  508. 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);
  509. HAL_StatusTypeDef HAL_I2C_Master_Seq_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions);
  510. HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions);
  511. HAL_StatusTypeDef HAL_I2C_Slave_Seq_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions);
  512. HAL_StatusTypeDef HAL_I2C_Slave_Seq_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions);
  513. /**
  514. * @}
  515. */
  516. /** @addtogroup I2C_IRQ_Handler_and_Callbacks IRQ Handler and Callbacks
  517. * @{
  518. */
  519. /******* I2C IRQHandler and Callbacks used in non blocking modes (Interrupt and DMA) */
  520. void HAL_I2C_EV_IRQHandler(I2C_HandleTypeDef *hi2c);
  521. void HAL_I2C_ER_IRQHandler(I2C_HandleTypeDef *hi2c);
  522. void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c);
  523. void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c);
  524. void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c);
  525. void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c);
  526. void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode);
  527. void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c);
  528. void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef *hi2c);
  529. void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c);
  530. void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c);
  531. void HAL_I2C_AbortCpltCallback(I2C_HandleTypeDef *hi2c);
  532. /**
  533. * @}
  534. */
  535. /** @addtogroup I2C_Exported_Functions_Group3 Peripheral State, Mode and Error functions
  536. * @{
  537. */
  538. /* Peripheral State, Mode and Error functions *********************************/
  539. HAL_I2C_StateTypeDef HAL_I2C_GetState(I2C_HandleTypeDef *hi2c);
  540. HAL_I2C_ModeTypeDef HAL_I2C_GetMode(I2C_HandleTypeDef *hi2c);
  541. uint32_t HAL_I2C_GetError(I2C_HandleTypeDef *hi2c);
  542. /**
  543. * @}
  544. */
  545. /**
  546. * @}
  547. */
  548. /* Private types -------------------------------------------------------------*/
  549. /* Private variables ---------------------------------------------------------*/
  550. /* Private constants ---------------------------------------------------------*/
  551. /** @defgroup I2C_Private_Constants I2C Private Constants
  552. * @{
  553. */
  554. #define I2C_FLAG_MASK 0x0000FFFFU
  555. #define I2C_MIN_PCLK_FREQ_STANDARD 2000000U /*!< 2 MHz */
  556. #define I2C_MIN_PCLK_FREQ_FAST 4000000U /*!< 4 MHz */
  557. /**
  558. * @}
  559. */
  560. /* Private macros ------------------------------------------------------------*/
  561. /** @defgroup I2C_Private_Macros I2C Private Macros
  562. * @{
  563. */
  564. #define I2C_MIN_PCLK_FREQ(__PCLK__, __SPEED__) (((__SPEED__) <= 100000U) ? ((__PCLK__) < I2C_MIN_PCLK_FREQ_STANDARD) : ((__PCLK__) < I2C_MIN_PCLK_FREQ_FAST))
  565. #define I2C_CCR_CALCULATION(__PCLK__, __SPEED__, __COEFF__) (((((__PCLK__) - 1U)/((__SPEED__) * (__COEFF__))) + 1U) & I2C_CCR_CCR)
  566. #define I2C_FREQRANGE(__PCLK__) ((__PCLK__)/1000000U)
  567. #define I2C_RISE_TIME(__FREQRANGE__, __SPEED__) (((__SPEED__) <= 100000U) ? ((__FREQRANGE__) + 1U) : ((((__FREQRANGE__) * 300U) / 1000U) + 1U))
  568. #define I2C_SPEED_STANDARD(__PCLK__, __SPEED__) ((I2C_CCR_CALCULATION((__PCLK__), (__SPEED__), 2U) < 4U)? 4U:I2C_CCR_CALCULATION((__PCLK__), (__SPEED__), 2U))
  569. #define I2C_SPEED_FAST(__PCLK__, __SPEED__, __DUTYCYCLE__) (((__DUTYCYCLE__) == I2C_DUTYCYCLE_2)? I2C_CCR_CALCULATION((__PCLK__), (__SPEED__), 3U) : (I2C_CCR_CALCULATION((__PCLK__), (__SPEED__), 25U) | I2C_DUTYCYCLE_16_9))
  570. #define I2C_SPEED(__PCLK__, __SPEED__, __DUTYCYCLE__) (((__SPEED__) <= 100000U)? (I2C_SPEED_STANDARD((__PCLK__), (__SPEED__))) : \
  571. ((I2C_SPEED_FAST((__PCLK__), (__SPEED__), (__DUTYCYCLE__)) & I2C_CCR_CCR) == 0U)? 1U : \
  572. ((I2C_SPEED_FAST((__PCLK__), (__SPEED__), (__DUTYCYCLE__))) | I2C_CCR_FS))
  573. #define I2C_7BIT_ADD_WRITE(__ADDRESS__) ((uint8_t)((__ADDRESS__) & (uint8_t)(~I2C_OAR1_ADD0)))
  574. #define I2C_7BIT_ADD_READ(__ADDRESS__) ((uint8_t)((__ADDRESS__) | I2C_OAR1_ADD0))
  575. #define I2C_10BIT_ADDRESS(__ADDRESS__) ((uint8_t)((uint16_t)((__ADDRESS__) & (uint16_t)0x00FF)))
  576. #define I2C_10BIT_HEADER_WRITE(__ADDRESS__) ((uint8_t)((uint16_t)((uint16_t)(((uint16_t)((__ADDRESS__) & (uint16_t)0x0300)) >> 7) | (uint16_t)0x00F0)))
  577. #define I2C_10BIT_HEADER_READ(__ADDRESS__) ((uint8_t)((uint16_t)((uint16_t)(((uint16_t)((__ADDRESS__) & (uint16_t)0x0300)) >> 7) | (uint16_t)(0x00F1))))
  578. #define I2C_MEM_ADD_MSB(__ADDRESS__) ((uint8_t)((uint16_t)(((uint16_t)((__ADDRESS__) & (uint16_t)0xFF00)) >> 8)))
  579. #define I2C_MEM_ADD_LSB(__ADDRESS__) ((uint8_t)((uint16_t)((__ADDRESS__) & (uint16_t)0x00FF)))
  580. /** @defgroup I2C_IS_RTC_Definitions I2C Private macros to check input parameters
  581. * @{
  582. */
  583. #define IS_I2C_DUTY_CYCLE(CYCLE) (((CYCLE) == I2C_DUTYCYCLE_2) || \
  584. ((CYCLE) == I2C_DUTYCYCLE_16_9))
  585. #define IS_I2C_ADDRESSING_MODE(ADDRESS) (((ADDRESS) == I2C_ADDRESSINGMODE_7BIT) || \
  586. ((ADDRESS) == I2C_ADDRESSINGMODE_10BIT))
  587. #define IS_I2C_DUAL_ADDRESS(ADDRESS) (((ADDRESS) == I2C_DUALADDRESS_DISABLE) || \
  588. ((ADDRESS) == I2C_DUALADDRESS_ENABLE))
  589. #define IS_I2C_GENERAL_CALL(CALL) (((CALL) == I2C_GENERALCALL_DISABLE) || \
  590. ((CALL) == I2C_GENERALCALL_ENABLE))
  591. #define IS_I2C_NO_STRETCH(STRETCH) (((STRETCH) == I2C_NOSTRETCH_DISABLE) || \
  592. ((STRETCH) == I2C_NOSTRETCH_ENABLE))
  593. #define IS_I2C_MEMADD_SIZE(SIZE) (((SIZE) == I2C_MEMADD_SIZE_8BIT) || \
  594. ((SIZE) == I2C_MEMADD_SIZE_16BIT))
  595. #define IS_I2C_CLOCK_SPEED(SPEED) (((SPEED) > 0U) && ((SPEED) <= 400000U))
  596. #define IS_I2C_OWN_ADDRESS1(ADDRESS1) (((ADDRESS1) & 0xFFFFFC00U) == 0U)
  597. #define IS_I2C_OWN_ADDRESS2(ADDRESS2) (((ADDRESS2) & 0xFFFFFF01U) == 0U)
  598. #define IS_I2C_TRANSFER_OPTIONS_REQUEST(REQUEST) (((REQUEST) == I2C_FIRST_FRAME) || \
  599. ((REQUEST) == I2C_FIRST_AND_NEXT_FRAME) || \
  600. ((REQUEST) == I2C_NEXT_FRAME) || \
  601. ((REQUEST) == I2C_FIRST_AND_LAST_FRAME) || \
  602. ((REQUEST) == I2C_LAST_FRAME) || \
  603. ((REQUEST) == I2C_LAST_FRAME_NO_STOP) || \
  604. IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(REQUEST))
  605. #define IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(REQUEST) (((REQUEST) == I2C_OTHER_FRAME) || \
  606. ((REQUEST) == I2C_OTHER_AND_LAST_FRAME))
  607. #define I2C_CHECK_FLAG(__ISR__, __FLAG__) ((((__ISR__) & ((__FLAG__) & I2C_FLAG_MASK)) == ((__FLAG__) & I2C_FLAG_MASK)) ? SET : RESET)
  608. #define I2C_CHECK_IT_SOURCE(__CR1__, __IT__) ((((__CR1__) & (__IT__)) == (__IT__)) ? SET : RESET)
  609. /**
  610. * @}
  611. */
  612. /**
  613. * @}
  614. */
  615. /* Private functions ---------------------------------------------------------*/
  616. /** @defgroup I2C_Private_Functions I2C Private Functions
  617. * @{
  618. */
  619. /**
  620. * @}
  621. */
  622. /**
  623. * @}
  624. */
  625. /**
  626. * @}
  627. */
  628. #ifdef __cplusplus
  629. }
  630. #endif
  631. #endif /* __STM32F1xx_HAL_I2C_H */