stm32l0xx_hal_i2c.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. /**
  2. ******************************************************************************
  3. * @file stm32l0xx_hal_i2c.h
  4. * @author MCD Application Team
  5. * @version V1.7.0
  6. * @date 31-May-2016
  7. * @brief Header file of I2C HAL module.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  12. *
  13. * Redistribution and use in source and binary forms, with or without modification,
  14. * are permitted provided that the following conditions are met:
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  30. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  32. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ******************************************************************************
  36. */
  37. /* Define to prevent recursive inclusion -------------------------------------*/
  38. #ifndef __STM32L0xx_HAL_I2C_H
  39. #define __STM32L0xx_HAL_I2C_H
  40. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43. /* Includes ------------------------------------------------------------------*/
  44. #include "stm32l0xx_hal_def.h"
  45. /** @addtogroup STM32L0xx_HAL_Driver
  46. * @{
  47. */
  48. /** @defgroup I2C I2C
  49. * @{
  50. */
  51. /* Exported types ------------------------------------------------------------*/
  52. /** @defgroup I2C_Exported_Types I2C Exported Types
  53. * @{
  54. */
  55. /** @defgroup I2C_Configuration_Structure_definition I2C Configuration Structure definition
  56. * @brief I2C Configuration Structure definition
  57. * @{
  58. */
  59. typedef struct
  60. {
  61. uint32_t Timing; /*!< Specifies the I2C_TIMINGR_register value.
  62. This parameter calculated by referring to I2C initialization
  63. section in Reference manual */
  64. uint32_t OwnAddress1; /*!< Specifies the first device own address.
  65. This parameter can be a 7-bit or 10-bit address. */
  66. uint32_t AddressingMode; /*!< Specifies if 7-bit or 10-bit addressing mode is selected.
  67. This parameter can be a value of @ref I2C_addressing_mode */
  68. uint32_t DualAddressMode; /*!< Specifies if dual addressing mode is selected.
  69. This parameter can be a value of @ref I2C_dual_addressing_mode */
  70. uint32_t OwnAddress2; /*!< Specifies the second device own address if dual addressing mode is selected
  71. This parameter can be a 7-bit address. */
  72. uint32_t OwnAddress2Masks; /*!< Specifies the acknoledge mask address second device own address if dual addressing mode is selected
  73. This parameter can be a value of @ref I2C_own_address2_masks */
  74. uint32_t GeneralCallMode; /*!< Specifies if general call mode is selected.
  75. This parameter can be a value of @ref I2C_general_call_addressing_mode */
  76. uint32_t NoStretchMode; /*!< Specifies if nostretch mode is selected.
  77. This parameter can be a value of @ref I2C_nostretch_mode */
  78. }I2C_InitTypeDef;
  79. /**
  80. * @}
  81. */
  82. /** @defgroup HAL_state_structure_definition HAL state structure definition
  83. * @brief HAL State structure definition
  84. * @{
  85. */
  86. typedef enum
  87. {
  88. HAL_I2C_STATE_RESET = 0x00U, /*!< I2C not yet initialized or disabled */
  89. HAL_I2C_STATE_READY = 0x01U, /*!< I2C initialized and ready for use */
  90. HAL_I2C_STATE_BUSY = 0x02U, /*!< I2C internal process is ongoing */
  91. HAL_I2C_STATE_MASTER_BUSY_TX = 0x12U, /*!< Master Data Transmission process is ongoing */
  92. HAL_I2C_STATE_MASTER_BUSY_RX = 0x22U, /*!< Master Data Reception process is ongoing */
  93. HAL_I2C_STATE_SLAVE_BUSY_TX = 0x32U, /*!< Slave Data Transmission process is ongoing */
  94. HAL_I2C_STATE_SLAVE_BUSY_RX = 0x42U, /*!< Slave Data Reception process is ongoing */
  95. HAL_I2C_STATE_MEM_BUSY_TX = 0x52U, /*!< Memory Data Transmission process is ongoing */
  96. HAL_I2C_STATE_MEM_BUSY_RX = 0x62U, /*!< Memory Data Reception process is ongoing */
  97. HAL_I2C_STATE_TIMEOUT = 0x03U, /*!< Timeout state */
  98. HAL_I2C_STATE_ERROR = 0x04U /*!< Reception process is ongoing */
  99. }HAL_I2C_StateTypeDef;
  100. /**
  101. * @}
  102. */
  103. /** @defgroup I2C_Error_Code I2C Error Code
  104. * @brief I2C Error Code
  105. * @{
  106. */
  107. #define HAL_I2C_ERROR_NONE 0x00U /*!< No error */
  108. #define HAL_I2C_ERROR_BERR 0x01U /*!< BERR error */
  109. #define HAL_I2C_ERROR_ARLO 0x02U /*!< ARLO error */
  110. #define HAL_I2C_ERROR_AF 0x04U /*!< ACKF error */
  111. #define HAL_I2C_ERROR_OVR 0x08U /*!< OVR error */
  112. #define HAL_I2C_ERROR_DMA 0x10U /*!< DMA transfer error */
  113. #define HAL_I2C_ERROR_TIMEOUT 0x20U /*!< Timeout error */
  114. #define HAL_I2C_ERROR_SIZE 0x40U /*!< Size Management error */
  115. /**
  116. * @}
  117. */
  118. /** @defgroup I2C_handle_Structure_definition I2C handle Structure definition
  119. * @brief I2C handle Structure definition
  120. * @{
  121. */
  122. typedef struct
  123. {
  124. I2C_TypeDef *Instance; /*!< I2C registers base address */
  125. I2C_InitTypeDef Init; /*!< I2C communication parameters */
  126. uint8_t *pBuffPtr; /*!< Pointer to I2C transfer buffer */
  127. uint16_t XferSize; /*!< I2C transfer size */
  128. __IO uint16_t XferCount; /*!< I2C transfer counter */
  129. DMA_HandleTypeDef *hdmatx; /*!< I2C Tx DMA handle parameters */
  130. DMA_HandleTypeDef *hdmarx; /*!< I2C Rx DMA handle parameters */
  131. HAL_LockTypeDef Lock; /*!< I2C locking object */
  132. __IO HAL_I2C_StateTypeDef State; /*!< I2C communication state */
  133. __IO uint32_t ErrorCode; /*!< I2C Error code, see I2C_Error_Code */
  134. }I2C_HandleTypeDef;
  135. /**
  136. * @}
  137. */
  138. /**
  139. * @}
  140. */
  141. /* Exported constants --------------------------------------------------------*/
  142. /** @defgroup I2C_Exported_Constants I2C Exported Constants
  143. * @{
  144. */
  145. /** @defgroup I2C_addressing_mode I2C addressing mode
  146. * @{
  147. */
  148. #define I2C_ADDRESSINGMODE_7BIT ((uint32_t)0x00000001U)
  149. #define I2C_ADDRESSINGMODE_10BIT ((uint32_t)0x00000002U)
  150. /**
  151. * @}
  152. */
  153. /** @defgroup I2C_dual_addressing_mode I2C dual addressing mode
  154. * @{
  155. */
  156. #define I2C_DUALADDRESS_DISABLE ((uint32_t)0x00000000U)
  157. #define I2C_DUALADDRESS_ENABLE I2C_OAR2_OA2EN
  158. /**
  159. * @}
  160. */
  161. /** @defgroup I2C_own_address2_masks I2C own address2 masks
  162. * @{
  163. */
  164. #define I2C_OA2_NOMASK ((uint8_t)0x00U)
  165. #define I2C_OA2_MASK01 ((uint8_t)0x01U)
  166. #define I2C_OA2_MASK02 ((uint8_t)0x02U)
  167. #define I2C_OA2_MASK03 ((uint8_t)0x03U)
  168. #define I2C_OA2_MASK04 ((uint8_t)0x04U)
  169. #define I2C_OA2_MASK05 ((uint8_t)0x05U)
  170. #define I2C_OA2_MASK06 ((uint8_t)0x06U)
  171. #define I2C_OA2_MASK07 ((uint8_t)0x07U)
  172. /**
  173. * @}
  174. */
  175. /** @defgroup I2C_general_call_addressing_mode I2C general call addressing mode
  176. * @{
  177. */
  178. #define I2C_GENERALCALL_DISABLE ((uint32_t)0x00000000U)
  179. #define I2C_GENERALCALL_ENABLE I2C_CR1_GCEN
  180. /**
  181. * @}
  182. */
  183. /** @defgroup I2C_nostretch_mode I2C nostretch mode
  184. * @{
  185. */
  186. #define I2C_NOSTRETCH_DISABLE ((uint32_t)0x00000000U)
  187. #define I2C_NOSTRETCH_ENABLE I2C_CR1_NOSTRETCH
  188. /**
  189. * @}
  190. */
  191. /** @defgroup I2C_Memory_Address_Size I2C Memory Address Size
  192. * @{
  193. */
  194. #define I2C_MEMADD_SIZE_8BIT ((uint32_t)0x00000001U)
  195. #define I2C_MEMADD_SIZE_16BIT ((uint32_t)0x00000002U)
  196. /**
  197. * @}
  198. */
  199. /** @defgroup I2C_ReloadEndMode_definition I2C ReloadEndMode definition
  200. * @{
  201. */
  202. #define I2C_RELOAD_MODE I2C_CR2_RELOAD
  203. #define I2C_AUTOEND_MODE I2C_CR2_AUTOEND
  204. #define I2C_SOFTEND_MODE ((uint32_t)0x00000000U)
  205. /**
  206. * @}
  207. */
  208. /** @defgroup I2C_StartStopMode_definition I2C StartStopMode definition
  209. * @{
  210. */
  211. #define I2C_NO_STARTSTOP ((uint32_t)0x00000000U)
  212. #define I2C_GENERATE_STOP I2C_CR2_STOP
  213. #define I2C_GENERATE_START_READ (uint32_t)(I2C_CR2_START | I2C_CR2_RD_WRN)
  214. #define I2C_GENERATE_START_WRITE I2C_CR2_START
  215. /**
  216. * @}
  217. */
  218. /** @defgroup I2C_Interrupt_configuration_definition I2C Interrupt configuration definition
  219. * @brief I2C Interrupt definition
  220. * Elements values convention: 0xXXXXXXXX
  221. * - XXXXXXXX : Interrupt control mask
  222. * @{
  223. */
  224. #define I2C_IT_ERRI I2C_CR1_ERRIE
  225. #define I2C_IT_TCI I2C_CR1_TCIE
  226. #define I2C_IT_STOPI I2C_CR1_STOPIE
  227. #define I2C_IT_NACKI I2C_CR1_NACKIE
  228. #define I2C_IT_ADDRI I2C_CR1_ADDRIE
  229. #define I2C_IT_RXI I2C_CR1_RXIE
  230. #define I2C_IT_TXI I2C_CR1_TXIE
  231. /**
  232. * @}
  233. */
  234. /** @defgroup I2C_Flag_definition I2C Flag definition
  235. * @{
  236. */
  237. #define I2C_FLAG_TXE I2C_ISR_TXE
  238. #define I2C_FLAG_TXIS I2C_ISR_TXIS
  239. #define I2C_FLAG_RXNE I2C_ISR_RXNE
  240. #define I2C_FLAG_ADDR I2C_ISR_ADDR
  241. #define I2C_FLAG_AF I2C_ISR_NACKF
  242. #define I2C_FLAG_STOPF I2C_ISR_STOPF
  243. #define I2C_FLAG_TC I2C_ISR_TC
  244. #define I2C_FLAG_TCR I2C_ISR_TCR
  245. #define I2C_FLAG_BERR I2C_ISR_BERR
  246. #define I2C_FLAG_ARLO I2C_ISR_ARLO
  247. #define I2C_FLAG_OVR I2C_ISR_OVR
  248. #define I2C_FLAG_PECERR I2C_ISR_PECERR
  249. #define I2C_FLAG_TIMEOUT I2C_ISR_TIMEOUT
  250. #define I2C_FLAG_ALERT I2C_ISR_ALERT
  251. #define I2C_FLAG_BUSY I2C_ISR_BUSY
  252. #define I2C_FLAG_DIR I2C_ISR_DIR
  253. /**
  254. * @}
  255. */
  256. /* Exported macros -----------------------------------------------------------*/
  257. /** @defgroup I2C_Exported_Macros I2C Exported Macros
  258. * @{
  259. */
  260. /** @brief Reset I2C handle state
  261. * @param __HANDLE__: specifies the I2C Handle.
  262. * @retval None
  263. */
  264. #define __HAL_I2C_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_I2C_STATE_RESET)
  265. /** @brief Enable the specified I2C interrupts.
  266. * @param __HANDLE__: specifies the I2C Handle.
  267. * @param __INTERRUPT__: specifies the interrupt source to enable.
  268. * This parameter can be one of the following values:
  269. * @arg I2C_IT_ERRI: Errors interrupt enable
  270. * @arg I2C_IT_TCI: Transfer complete interrupt enable
  271. * @arg I2C_IT_STOPI: STOP detection interrupt enable
  272. * @arg I2C_IT_NACKI: NACK received interrupt enable
  273. * @arg I2C_IT_ADDRI: Address match interrupt enable
  274. * @arg I2C_IT_RXI: RX interrupt enable
  275. * @arg I2C_IT_TXI: TX interrupt enable
  276. *
  277. * @retval None
  278. */
  279. #define __HAL_I2C_ENABLE_IT(__HANDLE__, __INTERRUPT__) ((__HANDLE__)->Instance->CR1 |= (__INTERRUPT__))
  280. /** @brief Disable the specified I2C interrupts.
  281. * @param __HANDLE__: specifies the I2C Handle.
  282. * @param __INTERRUPT__: specifies the interrupt source to disable.
  283. * This parameter can be one of the following values:
  284. * @arg I2C_IT_ERRI: Errors interrupt enable
  285. * @arg I2C_IT_TCI: Transfer complete interrupt enable
  286. * @arg I2C_IT_STOPI: STOP detection interrupt enable
  287. * @arg I2C_IT_NACKI: NACK received interrupt enable
  288. * @arg I2C_IT_ADDRI: Address match interrupt enable
  289. * @arg I2C_IT_RXI: RX interrupt enable
  290. * @arg I2C_IT_TXI: TX interrupt enable
  291. *
  292. * @retval None
  293. */
  294. #define __HAL_I2C_DISABLE_IT(__HANDLE__, __INTERRUPT__) ((__HANDLE__)->Instance->CR1 &= (~(__INTERRUPT__)))
  295. /** @brief Checks if the specified I2C interrupt source is enabled or disabled.
  296. * @param __HANDLE__: specifies the I2C Handle.
  297. * @param __INTERRUPT__: specifies the I2C interrupt source to check.
  298. * This parameter can be one of the following values:
  299. * @arg I2C_IT_ERRI: Errors interrupt enable
  300. * @arg I2C_IT_TCI: Transfer complete interrupt enable
  301. * @arg I2C_IT_STOPI: STOP detection interrupt enable
  302. * @arg I2C_IT_NACKI: NACK received interrupt enable
  303. * @arg I2C_IT_ADDRI: Address match interrupt enable
  304. * @arg I2C_IT_RXI: RX interrupt enable
  305. * @arg I2C_IT_TXI: TX interrupt enable
  306. *
  307. * @retval The new state of __INTERRUPT__ (TRUE or FALSE).
  308. */
  309. #define __HAL_I2C_GET_IT_SOURCE(__HANDLE__, __INTERRUPT__) ((((__HANDLE__)->Instance->CR1 & (__INTERRUPT__)) == (__INTERRUPT__)) ? SET : RESET)
  310. /** @brief Checks whether the specified I2C flag is set or not.
  311. * @param __HANDLE__: specifies the I2C Handle.
  312. * @param __FLAG__: specifies the flag to check.
  313. * This parameter can be one of the following values:
  314. * @arg I2C_FLAG_TXE: Transmit data register empty
  315. * @arg I2C_FLAG_TXIS: Transmit interrupt status
  316. * @arg I2C_FLAG_RXNE: Receive data register not empty
  317. * @arg I2C_FLAG_ADDR: Address matched (slave mode)
  318. * @arg I2C_FLAG_AF: Acknowledge failure received flag
  319. * @arg I2C_FLAG_STOPF: STOP detection flag
  320. * @arg I2C_FLAG_TC: Transfer complete (master mode)
  321. * @arg I2C_FLAG_TCR: Transfer complete reload
  322. * @arg I2C_FLAG_BERR: Bus error
  323. * @arg I2C_FLAG_ARLO: Arbitration lost
  324. * @arg I2C_FLAG_OVR: Overrun/Underrun
  325. * @arg I2C_FLAG_PECERR: PEC error in reception
  326. * @arg I2C_FLAG_TIMEOUT: Timeout or Tlow detection flag
  327. * @arg I2C_FLAG_ALERT: SMBus alert
  328. * @arg I2C_FLAG_BUSY: Bus busy
  329. * @arg I2C_FLAG_DIR: Transfer direction (slave mode)
  330. *
  331. * @retval The new state of __FLAG__ (TRUE or FALSE).
  332. */
  333. #define I2C_FLAG_MASK ((uint32_t)0x0001FFFF)
  334. #define __HAL_I2C_GET_FLAG(__HANDLE__, __FLAG__) (((((__HANDLE__)->Instance->ISR) & ((__FLAG__) & I2C_FLAG_MASK)) == ((__FLAG__) & I2C_FLAG_MASK)))
  335. /** @brief Clears the I2C pending flags which are cleared by writing 1 in a specific bit.
  336. * @param __HANDLE__: specifies the I2C Handle.
  337. * @param __FLAG__: specifies the flag to clear.
  338. * This parameter can be any combination of the following values:
  339. * @arg I2C_FLAG_ADDR: Address matched (slave mode)
  340. * @arg I2C_FLAG_AF: Acknowledge failure received flag
  341. * @arg I2C_FLAG_STOPF: STOP detection flag
  342. * @arg I2C_FLAG_BERR: Bus error
  343. * @arg I2C_FLAG_ARLO: Arbitration lost
  344. * @arg I2C_FLAG_OVR: Overrun/Underrun
  345. * @arg I2C_FLAG_PECERR: PEC error in reception
  346. * @arg I2C_FLAG_TIMEOUT: Timeout or Tlow detection flag
  347. * @arg I2C_FLAG_ALERT: SMBus alert
  348. *
  349. * @retval None
  350. */
  351. #define __HAL_I2C_CLEAR_FLAG(__HANDLE__, __FLAG__) ((__HANDLE__)->Instance->ICR = ((__FLAG__) & I2C_FLAG_MASK))
  352. /** @brief Enable the specified I2C peripheral.
  353. * @param __HANDLE__: specifies the I2C Handle.
  354. * @retval None
  355. */
  356. #define __HAL_I2C_ENABLE(__HANDLE__) (SET_BIT((__HANDLE__)->Instance->CR1, I2C_CR1_PE))
  357. /** @brief Disable the specified I2C peripheral.
  358. * @param __HANDLE__: specifies the I2C Handle.
  359. * @retval None
  360. */
  361. #define __HAL_I2C_DISABLE(__HANDLE__) (CLEAR_BIT((__HANDLE__)->Instance->CR1, I2C_CR1_PE))
  362. /**
  363. * @}
  364. */
  365. /**
  366. * @}
  367. */
  368. /* Include I2C HAL Extension module */
  369. #include "stm32l0xx_hal_i2c_ex.h"
  370. /* Private macros ------------------------------------------------------------*/
  371. /** @addtogroup I2C_Private
  372. * @{
  373. */
  374. #define IS_I2C_ADDRESSING_MODE(MODE) (((MODE) == I2C_ADDRESSINGMODE_7BIT) || \
  375. ((MODE) == I2C_ADDRESSINGMODE_10BIT))
  376. #define IS_I2C_DUAL_ADDRESS(ADDRESS) (((ADDRESS) == I2C_DUALADDRESS_DISABLE) || \
  377. ((ADDRESS) == I2C_DUALADDRESS_ENABLE))
  378. #define IS_I2C_OWN_ADDRESS2_MASK(MASK) (((MASK) == I2C_OA2_NOMASK) || \
  379. ((MASK) == I2C_OA2_MASK01) || \
  380. ((MASK) == I2C_OA2_MASK02) || \
  381. ((MASK) == I2C_OA2_MASK03) || \
  382. ((MASK) == I2C_OA2_MASK04) || \
  383. ((MASK) == I2C_OA2_MASK05) || \
  384. ((MASK) == I2C_OA2_MASK06) || \
  385. ((MASK) == I2C_OA2_MASK07))
  386. #define IS_I2C_GENERAL_CALL(CALL) (((CALL) == I2C_GENERALCALL_DISABLE) || \
  387. ((CALL) == I2C_GENERALCALL_ENABLE))
  388. #define IS_I2C_NO_STRETCH(STRETCH) (((STRETCH) == I2C_NOSTRETCH_DISABLE) || \
  389. ((STRETCH) == I2C_NOSTRETCH_ENABLE))
  390. #define IS_I2C_MEMADD_SIZE(SIZE) (((SIZE) == I2C_MEMADD_SIZE_8BIT) || \
  391. ((SIZE) == I2C_MEMADD_SIZE_16BIT))
  392. #define IS_TRANSFER_MODE(MODE) (((MODE) == I2C_RELOAD_MODE) || \
  393. ((MODE) == I2C_AUTOEND_MODE) || \
  394. ((MODE) == I2C_SOFTEND_MODE))
  395. #define IS_TRANSFER_REQUEST(REQUEST) (((REQUEST) == I2C_GENERATE_STOP) || \
  396. ((REQUEST) == I2C_GENERATE_START_READ) || \
  397. ((REQUEST) == I2C_GENERATE_START_WRITE) || \
  398. ((REQUEST) == I2C_NO_STARTSTOP))
  399. #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)))
  400. #define IS_I2C_OWN_ADDRESS1(ADDRESS1) ((ADDRESS1) <= (uint32_t)0x000003FFU)
  401. #define IS_I2C_OWN_ADDRESS2(ADDRESS2) ((ADDRESS2) <= (uint16_t)0x00FFU)
  402. #define __I2C_MEM_ADD_MSB(__ADDRESS__) ((uint8_t)((uint16_t)(((uint16_t)((__ADDRESS__) & (uint16_t)(0xFF00U))) >> 8)))
  403. #define __I2C_MEM_ADD_LSB(__ADDRESS__) ((uint8_t)((uint16_t)((__ADDRESS__) & (uint16_t)(0x00FFU))))
  404. #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)) : \
  405. (uint32_t)((((uint32_t)(__ADDRESS__) & (I2C_CR2_SADD)) | (I2C_CR2_ADD10) | (I2C_CR2_START)) & (~I2C_CR2_RD_WRN)))
  406. /**
  407. * @}
  408. */
  409. /* Exported functions --------------------------------------------------------*/
  410. /** @defgroup I2C_Exported_Functions I2C Exported Functions
  411. * @{
  412. */
  413. /** @defgroup I2C_Exported_Functions_Group1 Initialization and de-initialization functions
  414. * @{
  415. */
  416. /* Initialization and de-initialization functions******************************/
  417. HAL_StatusTypeDef HAL_I2C_Init(I2C_HandleTypeDef *hi2c);
  418. HAL_StatusTypeDef HAL_I2C_DeInit (I2C_HandleTypeDef *hi2c);
  419. void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c);
  420. void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c);
  421. /**
  422. * @}
  423. */
  424. /** @defgroup I2C_Exported_Functions_Group2 Input and Output operation functions
  425. * @{
  426. */
  427. /* IO operation functions ****************************************************/
  428. /******* Blocking mode: Polling */
  429. HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  430. HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  431. HAL_StatusTypeDef HAL_I2C_Slave_Transmit(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  432. HAL_StatusTypeDef HAL_I2C_Slave_Receive(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  433. 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);
  434. 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);
  435. HAL_StatusTypeDef HAL_I2C_IsDeviceReady(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout);
  436. /******* Non-Blocking mode: Interrupt */
  437. HAL_StatusTypeDef HAL_I2C_Master_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size);
  438. HAL_StatusTypeDef HAL_I2C_Master_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size);
  439. HAL_StatusTypeDef HAL_I2C_Slave_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size);
  440. HAL_StatusTypeDef HAL_I2C_Slave_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size);
  441. 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);
  442. 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);
  443. /******* Non-Blocking mode: DMA */
  444. HAL_StatusTypeDef HAL_I2C_Master_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size);
  445. HAL_StatusTypeDef HAL_I2C_Master_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size);
  446. HAL_StatusTypeDef HAL_I2C_Slave_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size);
  447. HAL_StatusTypeDef HAL_I2C_Slave_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size);
  448. 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);
  449. 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);
  450. /**
  451. * @}
  452. */
  453. /** @defgroup IRQ_Handler_and_Callbacks RQ Handler and Callbacks
  454. * @{
  455. */
  456. /******* I2C IRQHandler and Callbacks used in non blocking modes (Interrupt and DMA) */
  457. void HAL_I2C_EV_IRQHandler(I2C_HandleTypeDef *hi2c);
  458. void HAL_I2C_ER_IRQHandler(I2C_HandleTypeDef *hi2c);
  459. void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c);
  460. void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c);
  461. void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c);
  462. void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c);
  463. void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef *hi2c);
  464. void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c);
  465. void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c);
  466. /**
  467. * @}
  468. */
  469. /** @defgroup I2C_Exported_Functions_Group3 Peripheral State and Errors functions
  470. * @{
  471. */
  472. /* Peripheral State and Errors functions *************************************/
  473. HAL_I2C_StateTypeDef HAL_I2C_GetState(I2C_HandleTypeDef *hi2c);
  474. uint32_t HAL_I2C_GetError(I2C_HandleTypeDef *hi2c);
  475. /**
  476. * @}
  477. */
  478. /**
  479. * @}
  480. */
  481. /* Define the private group ***********************************/
  482. /**************************************************************/
  483. /** @defgroup I2C_Private I2C Private
  484. * @{
  485. */
  486. /**
  487. * @}
  488. */
  489. /**************************************************************/
  490. /**
  491. * @}
  492. */
  493. /**
  494. * @}
  495. */
  496. #ifdef __cplusplus
  497. }
  498. #endif
  499. #endif /* __STM32L0xx_HAL_I2C_H */
  500. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/