hc32_ll_i2c.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /**
  2. *******************************************************************************
  3. * @file hc32_ll_i2c.h
  4. * @brief This file contains all the functions prototypes of the Inter-Integrated
  5. * Circuit(I2C).
  6. @verbatim
  7. Change Logs:
  8. Date Author Notes
  9. 2022-12-31 CDT First version
  10. @endverbatim
  11. *******************************************************************************
  12. * Copyright (C) 2022, Xiaohua Semiconductor Co., Ltd. All rights reserved.
  13. *
  14. * This software component is licensed by XHSC under BSD 3-Clause license
  15. * (the "License"); You may not use this file except in compliance with the
  16. * License. You may obtain a copy of the License at:
  17. * opensource.org/licenses/BSD-3-Clause
  18. *
  19. *******************************************************************************
  20. */
  21. #ifndef __HC32_LL_I2C_H__
  22. #define __HC32_LL_I2C_H__
  23. /* C binding of definitions if building with C++ compiler */
  24. #ifdef __cplusplus
  25. extern "C"
  26. {
  27. #endif
  28. /*******************************************************************************
  29. * Include files
  30. ******************************************************************************/
  31. #include "hc32_ll_def.h"
  32. #include "hc32f4xx.h"
  33. #include "hc32f4xx_conf.h"
  34. /**
  35. * @addtogroup LL_Driver
  36. * @{
  37. */
  38. /**
  39. * @addtogroup LL_I2C
  40. * @{
  41. */
  42. #if (LL_I2C_ENABLE == DDL_ON)
  43. /*******************************************************************************
  44. * Global type definitions ('typedef')
  45. ******************************************************************************/
  46. /**
  47. * @defgroup I2C_Global_Types I2C Global Types
  48. * @{
  49. */
  50. /**
  51. * @brief I2c configuration structure
  52. */
  53. typedef struct {
  54. uint32_t u32ClockDiv; /*!< I2C clock division for i2c source clock */
  55. uint32_t u32Baudrate; /*!< I2C baudrate config */
  56. uint32_t u32SclTime; /*!< The SCL rising and falling time, count of T(i2c source clock after frequency divider) */
  57. } stc_i2c_init_t;
  58. /**
  59. * @}
  60. */
  61. /*******************************************************************************
  62. * Global pre-processor symbols/macros ('#define')
  63. ******************************************************************************/
  64. /**
  65. * @defgroup I2C_Global_Macros I2C Global Macros
  66. * @{
  67. */
  68. /**
  69. * @defgroup I2C_Trans_Dir I2C Transfer Direction
  70. * @{
  71. */
  72. #define I2C_DIR_TX (0x0U)
  73. #define I2C_DIR_RX (0x1U)
  74. /**
  75. * @}
  76. */
  77. /**
  78. * @defgroup I2C_Addr_Config I2C Address Configurate
  79. * @{
  80. */
  81. #define I2C_ADDR_DISABLE (0U)
  82. #define I2C_ADDR_7BIT (I2C_SLR0_SLADDR0EN)
  83. #define I2C_ADDR_10BIT (I2C_SLR0_ADDRMOD0 | I2C_SLR0_SLADDR0EN)
  84. /**
  85. * @}
  86. */
  87. /**
  88. * @defgroup I2C_Clock_Division I2C Clock Division
  89. * @{
  90. */
  91. #define I2C_CLK_DIV1 (0UL) /*!< I2c source clock/1 */
  92. #define I2C_CLK_DIV2 (1UL) /*!< I2c source clock/2 */
  93. #define I2C_CLK_DIV4 (2UL) /*!< I2c source clock/4 */
  94. #define I2C_CLK_DIV8 (3UL) /*!< I2c source clock/8 */
  95. #define I2C_CLK_DIV16 (4UL) /*!< I2c source clock/16 */
  96. #define I2C_CLK_DIV32 (5UL) /*!< I2c source clock/32 */
  97. #define I2C_CLK_DIV64 (6UL) /*!< I2c source clock/64 */
  98. #define I2C_CLK_DIV128 (7UL) /*!< I2c source clock/128 */
  99. /**
  100. * @}
  101. */
  102. /**
  103. * @defgroup I2C_Address_Num I2C Address Number
  104. * @{
  105. */
  106. #define I2C_ADDR0 (0UL)
  107. #define I2C_ADDR1 (1UL)
  108. /**
  109. * @}
  110. */
  111. /**
  112. * @defgroup I2C_Ack_Config I2C ACK Configurate
  113. * @{
  114. */
  115. #define I2C_ACK (0UL) /*!< Send ACK after date receive */
  116. #define I2C_NACK (I2C_CR1_ACK) /*!< Send NACK after date received */
  117. /**
  118. * @}
  119. */
  120. /**
  121. * @defgroup I2C_Smbus_Match_Config I2C SMBUS Address Match Configurate
  122. * @{
  123. */
  124. #define I2C_SMBUS_MATCH_ALARM (I2C_CR1_SMBALRTEN)
  125. #define I2C_SMBUS_MATCH_DEFAULT (I2C_CR1_SMBDEFAULTEN)
  126. #define I2C_SMBUS_MATCH_HOST (I2C_CR1_SMBHOSTEN)
  127. #define I2C_SMBUS_MATCH_ALL (I2C_CR1_SMBALRTEN | I2C_CR1_SMBDEFAULTEN | I2C_CR1_SMBHOSTEN)
  128. /**
  129. * @}
  130. */
  131. /**
  132. * @defgroup I2C_Digital_Filter_Clock I2C Digital Filter Clock
  133. * @{
  134. */
  135. #define I2C_DIG_FILTER_CLK_DIV1 (0UL << I2C_FLTR_DNF_POS) /*!< I2C Clock/1 */
  136. #define I2C_DIG_FILTER_CLK_DIV2 (1UL << I2C_FLTR_DNF_POS) /*!< I2C Clock/2 */
  137. #define I2C_DIG_FILTER_CLK_DIV3 (2UL << I2C_FLTR_DNF_POS) /*!< I2C Clock/3 */
  138. #define I2C_DIG_FILTER_CLK_DIV4 (3UL << I2C_FLTR_DNF_POS) /*!< I2C Clock/4 */
  139. /**
  140. * @}
  141. */
  142. /**
  143. * @defgroup I2C_Flag I2C Flag
  144. * @{
  145. */
  146. #define I2C_FLAG_START (I2C_SR_STARTF) /*!< Start condition detected */
  147. #define I2C_FLAG_MATCH_ADDR0 (I2C_SR_SLADDR0F) /*!< Address 0 detected */
  148. #define I2C_FLAG_MATCH_ADDR1 (I2C_SR_SLADDR1F) /*!< Address 1 detected */
  149. #define I2C_FLAG_TX_CPLT (I2C_SR_TENDF) /*!< Transfer end */
  150. #define I2C_FLAG_STOP (I2C_SR_STOPF) /*!< Stop condition detected */
  151. #define I2C_FLAG_RX_FULL (I2C_SR_RFULLF) /*!< Receive buffer full */
  152. #define I2C_FLAG_TX_EMPTY (I2C_SR_TEMPTYF) /*!< Transfer buffer empty */
  153. #define I2C_FLAG_ARBITRATE_FAIL (I2C_SR_ARLOF) /*!< Arbitration fails */
  154. #define I2C_FLAG_ACKR (I2C_SR_ACKRF) /*!< ACK status */
  155. #define I2C_FLAG_NACKF (I2C_SR_NACKF) /*!< NACK detected */
  156. #define I2C_FLAG_TMOUTF (I2C_SR_TMOUTF) /*!< Time out detected */
  157. #define I2C_FLAG_MASTER (I2C_SR_MSL) /*!< Master mode flag */
  158. #define I2C_FLAG_BUSY (I2C_SR_BUSY) /*!< Bus busy status */
  159. #define I2C_FLAG_TRA (I2C_SR_TRA) /*!< Transfer mode flag */
  160. #define I2C_FLAG_GENERAL_CALL (I2C_SR_GENCALLF) /*!< General call detected */
  161. #define I2C_FLAG_SMBUS_DEFAULT_MATCH (I2C_SR_SMBDEFAULTF) /*!< SMBUS default address detected */
  162. #define I2C_FLAG_SMBUS_HOST_MATCH (I2C_SR_SMBHOSTF) /*!< SMBUS host address detected */
  163. #define I2C_FLAG_SMBUS_ALARM_MATCH (I2C_SR_SMBALRTF) /*!< SMBUS alarm address detected */
  164. #define I2C_FLAG_CLR_ALL (I2C_FLAG_START | I2C_FLAG_MATCH_ADDR0 | I2C_FLAG_MATCH_ADDR1 \
  165. | I2C_FLAG_TX_CPLT | I2C_FLAG_STOP | I2C_FLAG_RX_FULL | I2C_FLAG_TX_EMPTY \
  166. | I2C_FLAG_ARBITRATE_FAIL | I2C_FLAG_NACKF | I2C_FLAG_TMOUTF \
  167. | I2C_FLAG_GENERAL_CALL | I2C_FLAG_SMBUS_DEFAULT_MATCH \
  168. | I2C_FLAG_SMBUS_HOST_MATCH | I2C_FLAG_SMBUS_ALARM_MATCH)
  169. #define I2C_FLAG_ALL (I2C_FLAG_START | I2C_FLAG_MATCH_ADDR0 | I2C_FLAG_MATCH_ADDR1 | I2C_FLAG_TX_CPLT \
  170. | I2C_FLAG_STOP | I2C_FLAG_RX_FULL | I2C_FLAG_TX_EMPTY | I2C_FLAG_ARBITRATE_FAIL\
  171. | I2C_FLAG_ACKR | I2C_FLAG_NACKF | I2C_FLAG_TMOUTF | I2C_FLAG_MASTER \
  172. | I2C_FLAG_BUSY | I2C_FLAG_TRA | I2C_FLAG_GENERAL_CALL \
  173. | I2C_FLAG_SMBUS_DEFAULT_MATCH | I2C_FLAG_SMBUS_HOST_MATCH \
  174. | I2C_FLAG_SMBUS_ALARM_MATCH)
  175. /**
  176. * @}
  177. */
  178. /**
  179. * @defgroup I2C_FIFO_Flag I2C FIFO status Flag Bits
  180. * @{
  181. */
  182. #define I2C_FIFO_FLAG_RFREQ (I2C_SR_RFREQ) /*!< Receive FIFO request */
  183. #define I2C_FIFO_FLAG_RFFULL (I2C_SR_RFFULL) /*!< Receive FIFO full */
  184. #define I2C_FIFO_FLAG_RFEMPTY (I2C_SR_RFEMPTY) /*!< Receive FIFO empty */
  185. #define I2C_FIFO_FLAG_TFFULL (I2C_SR_TFFULL) /*!< Transfer FIFO full */
  186. #define I2C_FIFO_FLAG_TFEMPTY (I2C_SR_TFEMPTY) /*!< Transfer FIFO empty */
  187. #define I2C_FIFO_FLAG_ALL (I2C_FIFO_FLAG_RFREQ | I2C_FIFO_FLAG_RFFULL | I2C_FIFO_FLAG_RFEMPTY \
  188. | I2C_FIFO_FLAG_TFFULL | I2C_FIFO_FLAG_TFEMPTY)
  189. /**
  190. * @}
  191. */
  192. /**
  193. * @defgroup I2C_FIFO_Data_Status I2C FIFO status
  194. * @{
  195. */
  196. #define I2C_FIFO_EMPTY (0U)
  197. #define I2C_FIFO_HALFFULL (1U)
  198. #define I2C_FIFO_FULL (2U)
  199. /**
  200. * @}
  201. */
  202. /**
  203. * @defgroup I2C_Int_Flag I2C Interrupt Flag Bits
  204. * @{
  205. */
  206. #define I2C_INT_START (I2C_CR2_STARTIE)
  207. #define I2C_INT_MATCH_ADDR0 (I2C_CR2_SLADDR0IE)
  208. #define I2C_INT_MATCH_ADDR1 (I2C_CR2_SLADDR1IE)
  209. #define I2C_INT_TX_CPLT (I2C_CR2_TENDIE)
  210. #define I2C_INT_STOP (I2C_CR2_STOPIE)
  211. #define I2C_INT_RX_FULL (I2C_CR2_RFULLIE)
  212. #define I2C_INT_TX_EMPTY (I2C_CR2_TEMPTYIE)
  213. #define I2C_INT_ARBITRATE_FAIL (I2C_CR2_ARLOIE)
  214. #define I2C_INT_NACK (I2C_CR2_NACKIE)
  215. #define I2C_INT_TMOUTIE (I2C_CR2_TMOUTIE)
  216. #define I2C_INT_GENERAL_CALL (I2C_CR2_GENCALLIE)
  217. #define I2C_INT_SMBUS_DEFAULT_MATCH (I2C_CR2_SMBDEFAULTIE)
  218. #define I2C_INT_SMBUS_HOST_MATCH (I2C_CR2_SMBHOSTIE)
  219. #define I2C_INT_SMBUS_ALARM_MATCH (I2C_CR2_SMBALRTIE)
  220. #define I2C_INT_RFREQ (I2C_CR2_RFREQIE)
  221. #define I2C_INT_ALL (I2C_INT_START | I2C_INT_MATCH_ADDR0 | I2C_INT_MATCH_ADDR1 | I2C_INT_TX_CPLT \
  222. | I2C_INT_STOP | I2C_INT_RX_FULL | I2C_INT_TX_EMPTY | I2C_INT_ARBITRATE_FAIL \
  223. | I2C_INT_NACK | I2C_INT_TMOUTIE | I2C_INT_GENERAL_CALL \
  224. | I2C_INT_SMBUS_DEFAULT_MATCH | I2C_INT_SMBUS_HOST_MATCH \
  225. | I2C_INT_SMBUS_ALARM_MATCH | I2C_INT_RFREQ)
  226. /**
  227. * @}
  228. */
  229. /**
  230. * @}
  231. */
  232. /*******************************************************************************
  233. * Global variable definitions ('extern')
  234. ******************************************************************************/
  235. /*******************************************************************************
  236. Global function prototypes (definition in C source)
  237. ******************************************************************************/
  238. /**
  239. * @addtogroup I2C_Global_Functions
  240. * @{
  241. */
  242. /* Initialization and Configuration **********************************/
  243. int32_t I2C_StructInit(stc_i2c_init_t *pstcI2cInit);
  244. int32_t I2C_BaudrateConfig(CM_I2C_TypeDef *I2Cx, const stc_i2c_init_t *pstcI2cInit, float32_t *pf32Error);
  245. void I2C_DeInit(CM_I2C_TypeDef *I2Cx);
  246. int32_t I2C_Init(CM_I2C_TypeDef *I2Cx, const stc_i2c_init_t *pstcI2cInit, float32_t *pf32Error);
  247. void I2C_SlaveAddrConfig(CM_I2C_TypeDef *I2Cx, uint32_t u32AddrNum, uint32_t u32AddrMode, uint32_t u32Addr);
  248. void I2C_SlaveAddrCmd(CM_I2C_TypeDef *I2Cx, uint32_t u32AddrNum, en_functional_state_t enNewState);
  249. void I2C_Cmd(CM_I2C_TypeDef *I2Cx, en_functional_state_t enNewState);
  250. void I2C_FastAckCmd(CM_I2C_TypeDef *I2Cx, en_functional_state_t enNewState);
  251. void I2C_BusWaitCmd(CM_I2C_TypeDef *I2Cx, en_functional_state_t enNewState);
  252. void I2C_SmbusConfig(CM_I2C_TypeDef *I2Cx, uint32_t u32SmbusConfig, en_functional_state_t enNewState);
  253. void I2C_SmbusCmd(CM_I2C_TypeDef *I2Cx, en_functional_state_t enNewState);
  254. void I2C_DigitalFilterConfig(CM_I2C_TypeDef *I2Cx, uint32_t u32FilterClock);
  255. void I2C_DigitalFilterCmd(CM_I2C_TypeDef *I2Cx, en_functional_state_t enNewState);
  256. void I2C_AnalogFilterCmd(CM_I2C_TypeDef *I2Cx, en_functional_state_t enNewState);
  257. void I2C_GeneralCallCmd(CM_I2C_TypeDef *I2Cx, en_functional_state_t enNewState);
  258. void I2C_SWResetCmd(CM_I2C_TypeDef *I2Cx, en_functional_state_t enNewState);
  259. void I2C_IntCmd(CM_I2C_TypeDef *I2Cx, uint32_t u32IntType, en_functional_state_t enNewState);
  260. void I2C_SlaveMaskAddrConfig(CM_I2C_TypeDef *I2Cx, uint32_t u32AddrNum, uint32_t u32AddrMode, uint32_t u32MaskAddr);
  261. void I2C_SlaveMaskAddrCmd(CM_I2C_TypeDef *I2Cx, uint32_t u32AddrNum, en_functional_state_t enNewState);
  262. void I2C_BusFreeClearCmd(CM_I2C_TypeDef *I2Cx, en_functional_state_t enNewState);
  263. /* Start/Restart/Stop ************************************************/
  264. void I2C_GenerateStart(CM_I2C_TypeDef *I2Cx);
  265. void I2C_GenerateRestart(CM_I2C_TypeDef *I2Cx);
  266. void I2C_GenerateStop(CM_I2C_TypeDef *I2Cx);
  267. /* Status management *************************************************/
  268. en_flag_status_t I2C_GetStatus(const CM_I2C_TypeDef *I2Cx, uint32_t u32Flag);
  269. void I2C_ClearStatus(CM_I2C_TypeDef *I2Cx, uint32_t u32Flag);
  270. /* FIFO *************************************************************/
  271. void I2C_FIFO_Cmd(CM_I2C_TypeDef *I2Cx, en_functional_state_t enNewState);
  272. void I2C_FIFO_ClearRequestStatus(CM_I2C_TypeDef *I2Cx);
  273. en_flag_status_t I2C_FIFO_GetStatus(const CM_I2C_TypeDef *I2Cx, uint32_t u32Flag);
  274. uint32_t I2C_FIFO_GetTxDataNum(const CM_I2C_TypeDef *I2Cx);
  275. uint32_t I2C_FIFO_GetRxDataStatus(const CM_I2C_TypeDef *I2Cx);
  276. uint32_t I2C_FIFO_GetTxDataStatus(const CM_I2C_TypeDef *I2Cx);
  277. void I2C_FIFO_NackResetFIFOCmd(CM_I2C_TypeDef *I2Cx, en_functional_state_t enNewState);
  278. void I2C_FIFO_TxFlush(CM_I2C_TypeDef *I2Cx);
  279. void I2C_FIFO_RxFlush(CM_I2C_TypeDef *I2Cx);
  280. /* Data transfer *****************************************************/
  281. void I2C_WriteData(CM_I2C_TypeDef *I2Cx, uint8_t u8Data);
  282. uint8_t I2C_ReadData(const CM_I2C_TypeDef *I2Cx);
  283. void I2C_AckConfig(CM_I2C_TypeDef *I2Cx, uint32_t u32AckConfig);
  284. /* Time out function *************************************************/
  285. void I2C_SCLHighTimeoutConfig(CM_I2C_TypeDef *I2Cx, uint16_t u16TimeoutH);
  286. void I2C_SCLLowTimeoutConfig(CM_I2C_TypeDef *I2Cx, uint16_t u16TimeoutL);
  287. void I2C_SCLHighTimeoutCmd(CM_I2C_TypeDef *I2Cx, en_functional_state_t enNewState);
  288. void I2C_SCLLowTimeoutCmd(CM_I2C_TypeDef *I2Cx, en_functional_state_t enNewState);
  289. void I2C_SCLTimeoutCmd(CM_I2C_TypeDef *I2Cx, en_functional_state_t enNewState);
  290. /* High level functions for reference ********************************/
  291. int32_t I2C_Start(CM_I2C_TypeDef *I2Cx, uint32_t u32Timeout);
  292. int32_t I2C_Restart(CM_I2C_TypeDef *I2Cx, uint32_t u32Timeout);
  293. int32_t I2C_TransAddr(CM_I2C_TypeDef *I2Cx, uint16_t u16Addr, uint8_t u8Dir, uint32_t u32Timeout);
  294. int32_t I2C_Trans10BitAddr(CM_I2C_TypeDef *I2Cx, uint16_t u16Addr, uint8_t u8Dir, uint32_t u32Timeout);
  295. int32_t I2C_TransData(CM_I2C_TypeDef *I2Cx, uint8_t const au8TxData[], uint32_t u32Size, uint32_t u32Timeout);
  296. int32_t I2C_ReceiveData(CM_I2C_TypeDef *I2Cx, uint8_t au8RxData[], uint32_t u32Size, uint32_t u32Timeout);
  297. int32_t I2C_MasterReceiveDataAndStop(CM_I2C_TypeDef *I2Cx, uint8_t au8RxData[], uint32_t u32Size, uint32_t u32Timeout);
  298. int32_t I2C_Stop(CM_I2C_TypeDef *I2Cx, uint32_t u32Timeout);
  299. int32_t I2C_WaitStatus(const CM_I2C_TypeDef *I2Cx, uint32_t u32Flag, en_flag_status_t enStatus, uint32_t u32Timeout);
  300. /**
  301. * @}
  302. */
  303. #endif /* LL_I2C_ENABLE */
  304. /**
  305. * @}
  306. */
  307. /**
  308. * @}
  309. */
  310. #ifdef __cplusplus
  311. }
  312. #endif
  313. #endif /* __HC32_LL_I2C_H__ */
  314. /*******************************************************************************
  315. * EOF (not truncated)
  316. ******************************************************************************/