gd32f10x_i2c.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /**
  2. ******************************************************************************
  3. * @brief I2C header file of the firmware library.
  4. ******************************************************************************
  5. */
  6. /* Define to prevent recursive inclusion -------------------------------------*/
  7. #ifndef __GD32F10X_I2C_H
  8. #define __GD32F10X_I2C_H
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. /* Includes ------------------------------------------------------------------*/
  13. #include "gd32f10x.h"
  14. /** @addtogroup GD32F10x_Firmware
  15. * @{
  16. */
  17. /** @addtogroup I2C
  18. * @{
  19. */
  20. /** @defgroup I2C_Exported_Types
  21. * @{
  22. */
  23. /**
  24. * @brief I2C Initial Parameters
  25. */
  26. typedef struct {
  27. uint16_t I2C_Protocol; /*!< The protocol type, detailed in @ref I2C_Protocol */
  28. uint16_t I2C_DutyCycle; /*!< The fast mode duty cycle, detailed in @ref I2C_Duty_Cycle */
  29. uint32_t I2C_BitRate; /*!< The I2C bit rate which must be lower than 400k bit/s */
  30. uint16_t I2C_AddressingMode; /*!< The I2C addressing mode, detailed in @ref I2C_Addressing_Mode */
  31. uint16_t I2C_DeviceAddress; /*!< The device address */
  32. } I2C_InitPara;
  33. /**
  34. * @}
  35. */
  36. /** @defgroup I2C_Exported_Constants
  37. * @{
  38. */
  39. /** @defgroup I2C_Protocol
  40. * @{
  41. */
  42. #define I2C_PROTOCOL_I2C ((uint16_t)0x0000)
  43. #define I2C_PROTOCOL_SMBUSDEVICE ((uint16_t)0x0002)
  44. #define I2C_PROTOCOL_SMBUSHOST ((uint16_t)0x000A)
  45. /**
  46. * @}
  47. */
  48. /** @defgroup I2C_Duty_Cycle
  49. * @{
  50. */
  51. #define I2C_DUTYCYCLE_16_9 ((uint16_t)0x4000) /*!< I2C fast mode Tlow/Thigh = 16/9 */
  52. #define I2C_DUTYCYCLE_2 ((uint16_t)0xBFFF) /*!< I2C fast mode Tlow/Thigh = 2 */
  53. /**
  54. * @}
  55. */
  56. /** @defgroup I2C_Addressing_Mode
  57. * @{
  58. */
  59. #define I2C_ADDRESSING_MODE_7BIT ((uint16_t)0x4000)
  60. #define I2C_ADDRESSING_MODE_10BIT ((uint16_t)0xC000)
  61. /**
  62. * @}
  63. */
  64. /** @defgroup I2C_Direction
  65. * @{
  66. */
  67. #define I2C_DIRECTION_TRANSMITTER ((uint8_t)0x00)
  68. #define I2C_DIRECTION_RECEIVER ((uint8_t)0x01)
  69. /**
  70. * @}
  71. */
  72. /** @defgroup I2C_Registers
  73. * @{
  74. */
  75. #define I2C_REGISTER_CTLR1 ((uint8_t)0x00)
  76. #define I2C_REGISTER_CTLR2 ((uint8_t)0x04)
  77. #define I2C_REGISTER_AR1 ((uint8_t)0x08)
  78. #define I2C_REGISTER_AR2 ((uint8_t)0x0C)
  79. #define I2C_REGISTER_DTR ((uint8_t)0x10)
  80. #define I2C_REGISTER_STR1 ((uint8_t)0x14)
  81. #define I2C_REGISTER_STR2 ((uint8_t)0x18)
  82. #define I2C_REGISTER_CLKR ((uint8_t)0x1C)
  83. #define I2C_REGISTER_RTR ((uint8_t)0x20)
  84. /**
  85. * @}
  86. */
  87. /** @defgroup I2C_PEC_Position
  88. * @{
  89. */
  90. #define I2C_PECPOSITION_NEXT I2C_CTLR1_POAP
  91. #define I2C_PECPOSITION_CURRENT ((uint16_t)~I2C_CTLR1_POAP)
  92. /**
  93. * @}
  94. */
  95. /** @defgroup I2C_NACK_Position
  96. * @{
  97. */
  98. #define I2C_NACKPOSITION_NEXT I2C_CTLR1_POAP
  99. #define I2C_NACKPOSITION_CURRENT ((uint16_t)~I2C_CTLR1_POAP)
  100. /**
  101. * @}
  102. */
  103. /** @defgroup I2C_Interrupt_Control
  104. * @{
  105. */
  106. #define I2C_INT_EIE I2C_CTLR2_EIE
  107. #define I2C_INT_EE I2C_CTLR2_EE
  108. #define I2C_INT_BIE I2C_CTLR2_BIE
  109. /**
  110. * @}
  111. */
  112. /** @defgroup I2C_Interrupt_Source
  113. * @{
  114. */
  115. #define I2C_INT_SMBALTS ((uint32_t)0x01008000)
  116. #define I2C_INT_SMBTO ((uint32_t)0x01004000)
  117. #define I2C_INT_PECE ((uint32_t)0x01001000)
  118. #define I2C_INT_RXORE ((uint32_t)0x01000800)
  119. #define I2C_INT_AE ((uint32_t)0x01000400)
  120. #define I2C_INT_LOSTARB ((uint32_t)0x01000200)
  121. #define I2C_INT_BE ((uint32_t)0x01000100)
  122. #define I2C_INT_TBE ((uint32_t)0x06000080)
  123. #define I2C_INT_RBNE ((uint32_t)0x06000040)
  124. #define I2C_INT_STPSEND ((uint32_t)0x02000010)
  125. #define I2C_INT_ADD10SEND ((uint32_t)0x02000008)
  126. #define I2C_INT_BTC ((uint32_t)0x02000004)
  127. #define I2C_INT_ADDSEND ((uint32_t)0x02000002)
  128. #define I2C_INT_SBSEND ((uint32_t)0x02000001)
  129. /**
  130. * @}
  131. */
  132. /** @defgroup I2C_FLAG
  133. * @{
  134. */
  135. /**
  136. * @brief STR2 register flags
  137. */
  138. #define I2C_FLAG_DUMODF ((uint32_t)0x00800000)
  139. #define I2C_FLAG_HSTSMB ((uint32_t)0x00400000)
  140. #define I2C_FLAG_DEFSMB ((uint32_t)0x00200000)
  141. #define I2C_FLAG_RXGC ((uint32_t)0x00100000)
  142. #define I2C_FLAG_TRS ((uint32_t)0x00040000)
  143. #define I2C_FLAG_I2CBSY ((uint32_t)0x00020000)
  144. #define I2C_FLAG_MASTER ((uint32_t)0x00010000)
  145. /**
  146. * @brief STR1 register flags
  147. */
  148. #define I2C_FLAG_SMBALTS ((uint32_t)0x10008000)
  149. #define I2C_FLAG_SMBTO ((uint32_t)0x10004000)
  150. #define I2C_FLAG_PECE ((uint32_t)0x10001000)
  151. #define I2C_FLAG_RXORE ((uint32_t)0x10000800)
  152. #define I2C_FLAG_AE ((uint32_t)0x10000400)
  153. #define I2C_FLAG_LOSTARB ((uint32_t)0x10000200)
  154. #define I2C_FLAG_BE ((uint32_t)0x10000100)
  155. #define I2C_FLAG_TBE ((uint32_t)0x10000080)
  156. #define I2C_FLAG_RBNE ((uint32_t)0x10000040)
  157. #define I2C_FLAG_STPSEND ((uint32_t)0x10000010)
  158. #define I2C_FLAG_ADD10SEND ((uint32_t)0x10000008)
  159. #define I2C_FLAG_BTC ((uint32_t)0x10000004)
  160. #define I2C_FLAG_ADDSEND ((uint32_t)0x10000002)
  161. #define I2C_FLAG_SBSEND ((uint32_t)0x10000001)
  162. /**
  163. * @}
  164. */
  165. /** @defgroup I2C_ProgrammingMode
  166. * @{
  167. */
  168. #define I2C_PROGRAMMINGMODE_MASTER_SBSEND ((uint32_t)0x00030001) /*!< I2CBSY, MASTER and SBSEND flag */
  169. #define I2C_PROGRAMMINGMODE_MASTER_TRANSMITTER_ADDSEND ((uint32_t)0x00070002) /*!< I2CBSY, MASTER, ADDSEND and TRS flags */
  170. #define I2C_PROGRAMMINGMODE_MASTER_RECEIVER_ADDSEND ((uint32_t)0x00030002) /*!< I2CBSY, MASTER and ADDSEND flags */
  171. #define I2C_PROGRAMMINGMODE_MASTER_ADD10SEND ((uint32_t)0x00030008) /*!< I2CBSY, MASTER and ADD10SEND flags */
  172. #define I2C_PROGRAMMINGMODE_MASTER_BYTE_RECEIVED ((uint32_t)0x00030040) /*!< I2CBSY, MASTER and RBNE flags */
  173. #define I2C_PROGRAMMINGMODE_MASTER_BYTE_TRANSMITTING ((uint32_t)0x00070080) /*!< TRS, I2CBSY, MASTER, TBE flags */
  174. #define I2C_PROGRAMMINGMODE_MASTER_BYTE_TRANSMITTED ((uint32_t)0x00070084) /*!< TRS, I2CBSY, MASTER, TBE and BTC flags */
  175. #define I2C_PROGRAMMINGMODE_SLAVE_RECEIVER_ADDSEND ((uint32_t)0x00020002) /*!< I2CBSY and ADDSEND flags */
  176. #define I2C_PROGRAMMINGMODE_SLAVE_TRANSMITTER_ADDSEND ((uint32_t)0x00060002) /*!< TRS, I2CBSY and ADDSEND flags */
  177. #define I2C_PROGRAMMINGMODE_SLAVE_RECEIVER_SECONDADDRESS_SELECTED ((uint32_t)0x00820000) /*!< DUMODF and I2CBSY flags */
  178. #define I2C_PROGRAMMINGMODE_SLAVE_TRANSMITTER_SECONDADDRESS_SELECTED ((uint32_t)0x00860080) /*!< DUMODF, TRS, I2CBSY and TBE flags */
  179. #define I2C_PROGRAMMINGMODE_SLAVE_GENERALCALLADDRESS_SELECTED ((uint32_t)0x00120000) /*!< RXGC and I2CBSY flags */
  180. #define I2C_PROGRAMMINGMODE_SLAVE_BYTE_RECEIVED ((uint32_t)0x00020040) /*!< I2CBSY and RBNE flags */
  181. #define I2C_PROGRAMMINGMODE_SLAVE_STOP_DETECTED ((uint32_t)0x00000010) /*!< STPSEND flag */
  182. #define I2C_PROGRAMMINGMODE_SLAVE_BYTE_TRANSMITTED ((uint32_t)0x00060084) /*!< TRS, I2CBSY, TBE and BTC flags */
  183. #define I2C_PROGRAMMINGMODE_SLAVE_BYTE_TRANSMITTING ((uint32_t)0x00060080) /*!< TRS, I2CBSY and TBE flags */
  184. #define I2C_PROGRAMMINGMODE_SLAVE_ACK_FAILURE ((uint32_t)0x00000400) /*!< AE flag */
  185. /**
  186. * @}
  187. */
  188. /**
  189. * @}
  190. */
  191. /** @defgroup I2C_Exported_Functions
  192. * @{
  193. */
  194. void I2C_DeInit(I2C_TypeDef *I2Cx);
  195. void I2C_Init(I2C_TypeDef *I2Cx, I2C_InitPara *I2C_InitParaStruct);
  196. void I2C_ParaInit(I2C_InitPara *I2C_InitParaStruct);
  197. void I2C_Enable(I2C_TypeDef *I2Cx, TypeState NewValue);
  198. void I2C_DMA_Enable(I2C_TypeDef *I2Cx, TypeState NewValue);
  199. void I2C_DMALastTransfer_Enable(I2C_TypeDef *I2Cx, TypeState NewValue);
  200. void I2C_StartOnBus_Enable(I2C_TypeDef *I2Cx, TypeState NewValue);
  201. void I2C_StopOnBus_Enable(I2C_TypeDef *I2Cx, TypeState NewValue);
  202. void I2C_Acknowledge_Enable(I2C_TypeDef *I2Cx, TypeState NewValue);
  203. void I2C_OwnAddress2(I2C_TypeDef *I2Cx, uint8_t Address);
  204. void I2C_DualAddress_Enable(I2C_TypeDef *I2Cx, TypeState NewValue);
  205. void I2C_GeneralCall_Enable(I2C_TypeDef *I2Cx, TypeState NewValue);
  206. void I2C_INTConfig(I2C_TypeDef *I2Cx, uint16_t I2C_INT, TypeState NewValue);
  207. void I2C_SendData(I2C_TypeDef *I2Cx, uint8_t Data);
  208. uint8_t I2C_ReceiveData(I2C_TypeDef *I2Cx);
  209. void I2C_AddressingDevice_7bit(I2C_TypeDef *I2Cx, uint8_t Address, uint8_t I2C_Direction);
  210. uint16_t I2C_ReadRegister(I2C_TypeDef *I2Cx, uint8_t I2C_Register);
  211. void I2C_SoftwareReset_Enable(I2C_TypeDef *I2Cx, TypeState NewValue);
  212. void I2C_NACKPosition_Enable(I2C_TypeDef *I2Cx, uint16_t I2C_NACKPosition);
  213. void I2C_SMBusAlertSend_Enable(I2C_TypeDef *I2Cx, TypeState NewValue);
  214. void I2C_PECTransmit_Enable(I2C_TypeDef *I2Cx, TypeState NewValue);
  215. void I2C_PECPosition_Enable(I2C_TypeDef *I2Cx, uint16_t I2C_PECPosition);
  216. void I2C_PEC_Enable(I2C_TypeDef *I2Cx, TypeState NewValue);
  217. uint8_t I2C_GetPECValue(I2C_TypeDef *I2Cx);
  218. void I2C_ARP_Enable(I2C_TypeDef *I2Cx, TypeState NewValue);
  219. void I2C_StretchClock_Enable(I2C_TypeDef *I2Cx, TypeState NewValue);
  220. void I2C_FastModeDutyCycle(I2C_TypeDef *I2Cx, uint16_t I2C_DutyCycle);
  221. TypeState I2C_StateDetect(I2C_TypeDef *I2Cx, uint32_t I2C_State);
  222. uint32_t I2C_GetCurrentState(I2C_TypeDef *I2Cx);
  223. TypeState I2C_GetBitState(I2C_TypeDef *I2Cx, uint32_t I2C_FLAG);
  224. void I2C_ClearBitState(I2C_TypeDef *I2Cx, uint32_t I2C_FLAG);
  225. TypeState I2C_GetIntBitState(I2C_TypeDef *I2Cx, uint32_t I2C_INT);
  226. void I2C_ClearIntBitState(I2C_TypeDef *I2Cx, uint32_t I2C_INT);
  227. #ifdef __cplusplus
  228. }
  229. #endif
  230. #endif /*__GD32F10X_I2C_H */
  231. /**
  232. * @}
  233. */
  234. /**
  235. * @}
  236. */
  237. /**
  238. * @}
  239. */