hc32f4a0_i2c.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /**
  2. *******************************************************************************
  3. * @file hc32f4a0_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. 2020-06-12 Hexiao First version
  10. 2020-07-15 Hexiao Modify I2C_SmBusCmd to I2C_SetMode
  11. 2020-08-31 Hexiao Remove invalid clock division value
  12. 2020-10-30 Hexiao Optimize data transfer api,etc.
  13. @endverbatim
  14. *******************************************************************************
  15. * Copyright (C) 2020, Huada Semiconductor Co., Ltd. All rights reserved.
  16. *
  17. * This software component is licensed by HDSC under BSD 3-Clause license
  18. * (the "License"); You may not use this file except in compliance with the
  19. * License. You may obtain a copy of the License at:
  20. * opensource.org/licenses/BSD-3-Clause
  21. *
  22. *******************************************************************************
  23. */
  24. #ifndef __HC32F4A0_I2C_H__
  25. #define __HC32F4A0_I2C_H__
  26. /* C binding of definitions if building with C++ compiler */
  27. #ifdef __cplusplus
  28. extern "C"
  29. {
  30. #endif
  31. /*******************************************************************************
  32. * Include files
  33. ******************************************************************************/
  34. #include "hc32_common.h"
  35. #include "ddl_config.h"
  36. /**
  37. * @addtogroup HC32F4A0_DDL_Driver
  38. * @{
  39. */
  40. /**
  41. * @addtogroup DDL_I2C
  42. * @{
  43. */
  44. #if (DDL_I2C_ENABLE == DDL_ON)
  45. /*******************************************************************************
  46. * Global type definitions ('typedef')
  47. ******************************************************************************/
  48. /**
  49. * @defgroup I2C_Global_Types I2C Global Types
  50. * @{
  51. */
  52. /**
  53. * @brief I2c configuration structure
  54. */
  55. typedef struct
  56. {
  57. uint32_t u32ClkDiv; /*!< I2C clock division for pclk3*/
  58. uint32_t u32Baudrate; /*!< I2C baudrate config*/
  59. uint32_t u32SclTime; /*!< The SCL rising and falling time, count of T(pclk3)*/
  60. }stc_i2c_init_t;
  61. /**
  62. * @}
  63. */
  64. /*******************************************************************************
  65. * Global pre-processor symbols/macros ('#define')
  66. ******************************************************************************/
  67. /**
  68. * @defgroup I2C_Global_Macros I2C Global Macros
  69. * @{
  70. */
  71. /** @defgroup I2C_Buadrate_Max I2C baudrate max value
  72. * @{
  73. */
  74. #define I2C_BAUDRATE_MAX (400000UL)
  75. /**
  76. * @}
  77. */
  78. /** @defgroup I2C_Mode Peripheral Mode
  79. * @{
  80. */
  81. #define I2C_MODE_I2C (0UL)
  82. #define I2C_MODE_SMBUS (2UL)
  83. /**
  84. * @}
  85. */
  86. /** @defgroup I2C_Transfer_Direction I2C transfer direction
  87. * @{
  88. */
  89. #define I2C_DIR_TX (0x0U)
  90. #define I2C_DIR_RX (0x1U)
  91. /**
  92. * @}
  93. */
  94. /** @defgroup I2C_Addr_Config I2C address configuration
  95. * @{
  96. */
  97. #define I2C_ADDR_MODE_7BIT (0x0U)
  98. #define I2C_ADDR_MODE_10BIT (I2C_SLR0_ADDRMOD0)
  99. /**
  100. * @}
  101. */
  102. /** @defgroup I2C_Clock_division I2C clock division
  103. * @{
  104. */
  105. #define I2C_CLK_DIV1 (0UL)
  106. #define I2C_CLK_DIV2 (1UL)
  107. #define I2C_CLK_DIV4 (2UL)
  108. #define I2C_CLK_DIV8 (3UL)
  109. #define I2C_CLK_DIV16 (4UL)
  110. #define I2C_CLK_DIV32 (5UL)
  111. #define I2C_CLK_DIV64 (6UL)
  112. #define I2C_CLK_DIV128 (7UL)
  113. /**
  114. * @}
  115. */
  116. /** @defgroup I2C_Address_Num I2C address number
  117. * @{
  118. */
  119. #define I2C_ADDR_0 (0UL)
  120. #define I2C_ADDR_1 (1UL)
  121. /**
  122. * @}
  123. */
  124. /** @defgroup I2C_Ack_Type I2C ack type
  125. * @{
  126. */
  127. #define I2C_ACK (0UL)
  128. #define I2C_NACK (I2C_CR1_ACK)
  129. /**
  130. * @}
  131. */
  132. /** @defgroup I2C_Smbus_Match_Cfg I2C smbus address match configuration
  133. * @{
  134. */
  135. #define I2C_SMBUS_MATCH_ALRT (I2C_CR1_SMBALRTEN)
  136. #define I2C_SMBUS_MATCH_DEFAULT (I2C_CR1_SMBDEFAULTEN)
  137. #define I2C_SMBUS_MATCH_HOST (I2C_CR1_SMBHOSTEN)
  138. /**
  139. * @}
  140. */
  141. /** @defgroup I2C_Smbus_clear_mask I2C smbus clear mask
  142. * @{
  143. */
  144. #define I2C_SMBUS_CONFIG_CLEARMASK (I2C_CR1_SMBALRTEN|I2C_CR1_SMBDEFAULTEN|I2C_CR1_SMBHOSTEN)
  145. /**
  146. * @}
  147. */
  148. /** @defgroup I2C_Digital_Filter_mode I2C digital filter mode
  149. * @{
  150. */
  151. #define I2C_DIG_FILTMODE_1CYCLE (0UL << I2C_FLTR_DNF_POS)
  152. #define I2C_DIG_FILTMODE_2CYCLE (1UL << I2C_FLTR_DNF_POS)
  153. #define I2C_DIG_FILTMODE_3CYCLE (2UL << I2C_FLTR_DNF_POS)
  154. #define I2C_DIG_FILTMODE_4CYCLE (3UL << I2C_FLTR_DNF_POS)
  155. /**
  156. * @}
  157. */
  158. /**
  159. * @}
  160. */
  161. /*******************************************************************************
  162. * Global variable definitions ('extern')
  163. ******************************************************************************/
  164. /*******************************************************************************
  165. Global function prototypes (definition in C source)
  166. ******************************************************************************/
  167. /**
  168. * @addtogroup I2C_Global_Functions
  169. * @{
  170. */
  171. /* Initialization and Configuration **********************************/
  172. void I2C_DeInit(M4_I2C_TypeDef* I2Cx);
  173. en_result_t I2C_StructInit(stc_i2c_init_t* pstcI2C_InitStruct);
  174. en_result_t I2C_Init(M4_I2C_TypeDef* I2Cx, const stc_i2c_init_t *pstcI2C_InitStruct, float32_t *pf32Err);
  175. en_result_t I2C_BaudrateConfig(M4_I2C_TypeDef* I2Cx, const stc_i2c_init_t *pstcI2C_InitStruct, float32_t *pf32Err);
  176. void I2C_SlaveAddrCmd(M4_I2C_TypeDef* I2Cx, uint32_t u32AddrNum, en_functional_state_t enNewState);
  177. void I2C_SlaveAddrConfig(M4_I2C_TypeDef* I2Cx, uint32_t u32AddrNum, uint32_t u32AddrMode, uint32_t u32Addr);
  178. void I2C_SetMode(M4_I2C_TypeDef* I2Cx, uint32_t u32Mode);
  179. void I2C_Cmd(M4_I2C_TypeDef* I2Cx, en_functional_state_t enNewState);
  180. void I2C_FastAckCmd(M4_I2C_TypeDef* I2Cx, en_functional_state_t enNewState);
  181. void I2C_BusWaitCmd(M4_I2C_TypeDef* I2Cx, en_functional_state_t enNewState);
  182. void I2C_SoftwareResetCmd(M4_I2C_TypeDef* I2Cx, en_functional_state_t enNewState);
  183. void I2C_IntCmd(M4_I2C_TypeDef* I2Cx, uint32_t u32IntEn, en_functional_state_t enNewState);
  184. void I2C_ClkHighTimeoutConfig(M4_I2C_TypeDef* I2Cx, uint16_t u16TimeoutH);
  185. void I2C_ClkLowTimeoutConfig(M4_I2C_TypeDef* I2Cx, uint16_t u16TimeoutL);
  186. void I2C_ClkHighTimeoutCmd(M4_I2C_TypeDef* I2Cx, en_functional_state_t enNewState);
  187. void I2C_ClkLowTimeoutCmd(M4_I2C_TypeDef* I2Cx, en_functional_state_t enNewState);
  188. void I2C_ClkTimeoutCmd(M4_I2C_TypeDef* I2Cx, en_functional_state_t enNewState);
  189. void I2C_SmbusConfig(M4_I2C_TypeDef* I2Cx, uint32_t u32SmbusConfig, en_functional_state_t enNewState);
  190. void I2C_DigitalFilterConfig(M4_I2C_TypeDef* I2Cx, uint32_t u32DigFilterMode);
  191. void I2C_DigitalFilterCmd(M4_I2C_TypeDef* I2Cx, en_functional_state_t enNewState);
  192. void I2C_AnalogFilterCmd(M4_I2C_TypeDef* I2Cx, en_functional_state_t enNewState);
  193. void I2C_GeneralCallCmd(M4_I2C_TypeDef* I2Cx, en_functional_state_t enNewState);
  194. /* Start/Restart/Stop ************************************************/
  195. void I2C_GenerateStart(M4_I2C_TypeDef* I2Cx);
  196. void I2C_GenerateReStart(M4_I2C_TypeDef* I2Cx);
  197. void I2C_GenerateStop(M4_I2C_TypeDef* I2Cx);
  198. /* Status management *************************************************/
  199. en_flag_status_t I2C_GetStatus(const M4_I2C_TypeDef *I2Cx, uint32_t u32StatusBit);
  200. void I2C_ClearStatus(M4_I2C_TypeDef* I2Cx, uint32_t u32StatusBit);
  201. /* Data transfer *****************************************************/
  202. void I2C_WriteDataReg(M4_I2C_TypeDef* I2Cx, uint8_t u8Data);
  203. uint8_t I2C_ReadDataReg(const M4_I2C_TypeDef *I2Cx);
  204. void I2C_AckConfig(M4_I2C_TypeDef* I2Cx, uint32_t u32AckConfig);
  205. /* High level functions for reference ********************************/
  206. en_result_t I2C_Start(M4_I2C_TypeDef* I2Cx, uint32_t u32Timeout);
  207. en_result_t I2C_Restart(M4_I2C_TypeDef* I2Cx, uint32_t u32Timeout);
  208. en_result_t I2C_TransAddr(M4_I2C_TypeDef* I2Cx, uint8_t u8Addr, uint8_t u8Dir, uint32_t u32Timeout);
  209. en_result_t I2C_Trans10BitAddr(M4_I2C_TypeDef* I2Cx, uint16_t u16Addr, uint8_t u8Dir, uint32_t u32Timeout);
  210. en_result_t I2C_TransData(M4_I2C_TypeDef* I2Cx, uint8_t const pau8TxData[], uint32_t u32Size, uint32_t u32Timeout);
  211. en_result_t I2C_Receive(M4_I2C_TypeDef* I2Cx, uint8_t pau8RxData[], uint32_t u32Size, uint32_t u32Timeout);
  212. en_result_t I2C_Stop(M4_I2C_TypeDef* I2Cx, uint32_t u32Timeout);
  213. en_result_t I2C_MasterReceiveAndStop(M4_I2C_TypeDef* I2Cx, uint8_t au8RxData[], uint32_t u32Size, uint32_t u32Timeout);
  214. /**
  215. * @}
  216. */
  217. #endif /* DDL_I2C_ENABLE */
  218. /**
  219. * @}
  220. */
  221. /**
  222. * @}
  223. */
  224. #ifdef __cplusplus
  225. }
  226. #endif
  227. #endif /* __HC32F4A0_I2C_H__ */
  228. /*******************************************************************************
  229. * EOF (not truncated)
  230. ******************************************************************************/