gd32f4xx_i2c.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*!
  2. \file gd32f4xx_i2c.h
  3. \brief definitions for the I2C
  4. */
  5. /*
  6. Copyright (C) 2016 GigaDevice
  7. 2016-08-15, V1.0.0, firmware for GD32F4xx
  8. */
  9. #ifndef GD32F4XX_I2C_H
  10. #define GD32F4XX_I2C_H
  11. #include "gd32f4xx.h"
  12. /* I2Cx(x=0,1,2) definitions */
  13. #define I2C0 I2C_BASE
  14. #define I2C1 (I2C_BASE+0x400U)
  15. #define I2C2 (I2C_BASE+0x800U)
  16. /* registers definitions */
  17. #define I2C_CTL0(i2cx) REG32((i2cx) + 0x00U) /*!< I2C control register 0 */
  18. #define I2C_CTL1(i2cx) REG32((i2cx) + 0x04U) /*!< I2C control register 1 */
  19. #define I2C_SADDR0(i2cx) REG32((i2cx) + 0x08U) /*!< I2C slave address register 0*/
  20. #define I2C_SADDR1(i2cx) REG32((i2cx) + 0x0CU) /*!< I2C slave address register */
  21. #define I2C_DATA(i2cx) REG32((i2cx) + 0x10U) /*!< I2C transfer buffer register */
  22. #define I2C_STAT0(i2cx) REG32((i2cx) + 0x14U) /*!< I2C transfer status register 0 */
  23. #define I2C_STAT1(i2cx) REG32((i2cx) + 0x18U) /*!< I2C transfer status register */
  24. #define I2C_CKCFG(i2cx) REG32((i2cx) + 0x1CU) /*!< I2C clock configure register */
  25. #define I2C_RT(i2cx) REG32((i2cx) + 0x20U) /*!< I2C rise time register */
  26. #define I2C_FCTL(i2cx) REG32((i2cx) + 0x24U) /*!< I2C filter control register */
  27. #define I2C_SAMCS(i2cx) REG32((i2cx) + 0x80U) /*!< I2C SAM control and status register */
  28. /* bits definitions */
  29. /* I2Cx_CTL0 */
  30. #define I2C_CTL0_I2CEN BIT(0) /*!< peripheral enable */
  31. #define I2C_CTL0_SMBEN BIT(1) /*!< SMBus mode */
  32. #define I2C_CTL0_SMBSEL BIT(3) /*!< SMBus type */
  33. #define I2C_CTL0_ARPEN BIT(4) /*!< ARP enable */
  34. #define I2C_CTL0_PECEN BIT(5) /*!< PEC enable */
  35. #define I2C_CTL0_GCEN BIT(6) /*!< general call enable */
  36. #define I2C_CTL0_DISSTRC BIT(7) /*!< clock stretching disable (slave mode) */
  37. #define I2C_CTL0_START BIT(8) /*!< start generation */
  38. #define I2C_CTL0_STOP BIT(9) /*!< stop generation */
  39. #define I2C_CTL0_ACKEN BIT(10) /*!< acknowledge enable */
  40. #define I2C_CTL0_POAP BIT(11) /*!< acknowledge/PEC position (for data reception) */
  41. #define I2C_CTL0_PECTRANS BIT(12) /*!< packet error checking */
  42. #define I2C_CTL0_SALT BIT(13) /*!< SMBus alert */
  43. #define I2C_CTL0_SRESET BIT(15) /*!< software reset */
  44. /* I2Cx_CTL1 */
  45. #define I2C_CTL1_I2CCLK BITS(0,5) /*!< I2CCLK[5:0] bits (peripheral clock frequency) */
  46. #define I2C_CTL1_ERRIE BIT(8) /*!< error interrupt inable */
  47. #define I2C_CTL1_EVIE BIT(9) /*!< event interrupt enable */
  48. #define I2C_CTL1_BUFIE BIT(10) /*!< buffer interrupt enable */
  49. #define I2C_CTL1_DMAON BIT(11) /*!< DMA requests enable */
  50. #define I2C_CTL1_DMALST BIT(12) /*!< DMA last transfer */
  51. /* I2Cx_SADDR0 */
  52. #define I2C_SADDR0_ADDRESS0 BIT(0) /*!< bit 0 of a 10-bit address */
  53. #define I2C_SADDR0_ADDRESS BITS(1,7) /*!< 7-bit address or bits 7:1 of a 10-bit address */
  54. #define I2C_SADDR0_ADDRESS_H BITS(8,9) /*!< highest two bits of a 10-bit address */
  55. #define I2C_SADDR0_ADDFORMAT BIT(15) /*!< address mode for the I2C slave */
  56. /* I2Cx_SADDR1 */
  57. #define I2C_SADDR1_DUADEN BIT(0) /*!< aual-address mode switch */
  58. #define I2C_SADDR1_ADDRESS2 BITS(1,7) /*!< second I2C address for the slave in dual-address mode */
  59. /* I2Cx_DATA */
  60. #define I2C_DATA_TRB BITS(0,7) /*!< 8-bit data register */
  61. /* I2Cx_STAT0 */
  62. #define I2C_STAT0_SBSEND BIT(0) /*!< start bit (master mode) */
  63. #define I2C_STAT0_ADDSEND BIT(1) /*!< address sent (master mode)/matched (slave mode) */
  64. #define I2C_STAT0_BTC BIT(2) /*!< byte transfer finished */
  65. #define I2C_STAT0_ADD10SEND BIT(3) /*!< 10-bit header sent (master mode) */
  66. #define I2C_STAT0_STPDET BIT(4) /*!< stop detection (slave mode) */
  67. #define I2C_STAT0_RBNE BIT(6) /*!< data register not empty (receivers) */
  68. #define I2C_STAT0_TBE BIT(7) /*!< data register empty (transmitters) */
  69. #define I2C_STAT0_BERR BIT(8) /*!< bus error */
  70. #define I2C_STAT0_LOSTARB BIT(9) /*!< arbitration lost (master mode) */
  71. #define I2C_STAT0_AERR BIT(10) /*!< acknowledge failure */
  72. #define I2C_STAT0_OUERR BIT(11) /*!< overrun/underrun */
  73. #define I2C_STAT0_PECERR BIT(12) /*!< PEC error in reception */
  74. #define I2C_STAT0_SMBTO BIT(14) /*!< timeout signal in SMBus mode */
  75. #define I2C_STAT0_SMBALT BIT(15) /*!< SMBus alert status */
  76. /* I2Cx_STAT1 */
  77. #define I2C_STAT1_MASTER BIT(0) /*!< master/slave */
  78. #define I2C_STAT1_I2CBSY BIT(1) /*!< bus busy */
  79. #define I2C_STAT1_TRS BIT(2) /*!< transmitter/receiver */
  80. #define I2C_STAT1_RXGC BIT(4) /*!< general call address (slave mode) */
  81. #define I2C_STAT1_DEFSMB BIT(5) /*!< SMBus device default address (slave mode) */
  82. #define I2C_STAT1_HSTSMB BIT(6) /*!< SMBus host header (slave mode) */
  83. #define I2C_STAT1_DUMODF BIT(7) /*!< dual flag (slave mode) */
  84. #define I2C_STAT1_ECV BITS(8,15) /*!< packet error checking register */
  85. /* I2Cx_CKCFG */
  86. #define I2C_CKCFG_CLKC BITS(0,11) /*!< clock control register in fast/standard mode (master mode) */
  87. #define I2C_CKCFG_DTCY BIT(14) /*!< fast mode duty cycle */
  88. #define I2C_CKCFG_FAST BIT(15) /*!< I2C speed selection in master mode */
  89. /* I2Cx_RT */
  90. #define I2C_RT_RISETIME BITS(0,5) /*!< maximum rise time in fast/standard mode (Master mode) */
  91. /* I2Cx_FCTL */
  92. #define I2C_FCTL_DF BITS(0,3) /*!< digital noise filter */
  93. #define I2C_FCTL_AFD BIT(4) /*!< analog noise filter disable */
  94. /* I2Cx_SAMCS */
  95. #define I2C_SAMCS_SAMEN BIT(0) /*!< SAM_V interface enable */
  96. #define I2C_SAMCS_STOEN BIT(1) /*!< SAM_V interface timeout detect enable */
  97. #define I2C_SAMCS_TFFIE BIT(4) /*!< txframe fall interrupt enable */
  98. #define I2C_SAMCS_TFRIE BIT(5) /*!< txframe rise interrupt enable */
  99. #define I2C_SAMCS_RFFIE BIT(6) /*!< rxframe fall interrupt enable */
  100. #define I2C_SAMCS_RFRIE BIT(7) /*!< rxframe rise interrupt enable */
  101. #define I2C_SAMCS_TXF BIT(8) /*!< level of txframe signal */
  102. #define I2C_SAMCS_RXF BIT(9) /*!< level of rxframe signal */
  103. #define I2C_SAMCS_TFF BIT(12) /*!< txframe fall flag, cleared by software write 0 */
  104. #define I2C_SAMCS_TFR BIT(13) /*!< txframe rise flag, cleared by software write 0 */
  105. #define I2C_SAMCS_RFF BIT(14) /*!< rxframe fall flag, cleared by software write 0 */
  106. #define I2C_SAMCS_RFR BIT(15) /*!< rxframe rise flag, cleared by software write 0 */
  107. /* constants definitions */
  108. /* the digital noise filter can filter spikes's length */
  109. typedef enum {
  110. I2C_DF_DISABLE, /*!< disable digital noise filter */
  111. I2C_DF_1PCLK, /*!< enable digital noise filter and the maximum filtered spiker's length 1 PCLK1 */
  112. I2C_DF_2PCLKS, /*!< enable digital noise filter and the maximum filtered spiker's length 2 PCLK1 */
  113. I2C_DF_3PCLKS, /*!< enable digital noise filter and the maximum filtered spiker's length 3 PCLK1 */
  114. I2C_DF_4PCLKS, /*!< enable digital noise filter and the maximum filtered spiker's length 4 PCLK1 */
  115. I2C_DF_5PCLKS, /*!< enable digital noise filter and the maximum filtered spiker's length 5 PCLK1 */
  116. I2C_DF_6PCLKS, /*!< enable digital noise filter and the maximum filtered spiker's length 6 PCLK1 */
  117. I2C_DF_7PCLKS, /*!< enable digital noise filter and the maximum filtered spiker's length 7 PCLK1 */
  118. I2C_DF_8PCLKS, /*!< enable digital noise filter and the maximum filtered spiker's length 8 PCLK1 */
  119. I2C_DF_9PCLKS, /*!< enable digital noise filter and the maximum filtered spiker's length 9 PCLK1 */
  120. I2C_DF_10PCLKS, /*!< enable digital noise filter and the maximum filtered spiker's length 10 PCLK1 */
  121. I2C_DF_11PCLKS, /*!< enable digital noise filter and the maximum filtered spiker's length 11 PCLK1 */
  122. I2C_DF_12PCLKS, /*!< enable digital noise filter and the maximum filtered spiker's length 12 PCLK1 */
  123. I2C_DF_13PCLKS, /*!< enable digital noise filter and the maximum filtered spiker's length 13 PCLK1 */
  124. I2C_DF_14PCLKS, /*!< enable digital noise filter and the maximum filtered spiker's length 14 PCLK1 */
  125. I2C_DF_15PCLKS /*!< enable digital noise filter and the maximum filtered spiker's length 15 PCLK1 */
  126. }i2c_digital_filter_enum;
  127. /* SMBus/I2C mode switch and SMBus type selection */
  128. #define I2C_I2CMODE_ENABLE ((uint32_t)0x00000000U) /*!< I2C mode */
  129. #define I2C_SMBUSMODE_ENABLE I2C_CTL0_SMBEN /*!< SMBus mode */
  130. /* SMBus/I2C mode switch and SMBus type selection */
  131. #define I2C_SMBUS_DEVICE ((uint32_t)0x00000000U) /*!< SMBus mode device type */
  132. #define I2C_SMBUS_HOST I2C_CTL0_SMBSEL /*!< SMBus mode host type */
  133. /* I2C transfer direction */
  134. #define I2C_TRANSMITTER (~BIT(0)) /*!< transmitter */
  135. #define I2C_RECEIVER BIT(0) /*!< receiver */
  136. /* whether or not to send an ACK */
  137. #define I2C_ACK_ENABLE ((uint8_t)0x01U) /*!< ACK will be sent */
  138. #define I2C_ACK_DISABLE ((uint8_t)0x00U) /*!< ACK will be not sent */
  139. /* I2C POAP position*/
  140. #define I2C_ACKPOS_CURRENT ((uint8_t)0x01U) /*!< ACKEN bit decides whether to send ACK or not for the current */
  141. #define I2C_ACKPOS_NEXT ((uint8_t)0x00U) /*!< ACKEN bit decides whether to send ACK or not for the next byte */
  142. /* I2C dual-address mode switch */
  143. #define I2C_DUADEN_DISABLE ((uint8_t)0x00U) /*!< dual-address mode disabled */
  144. #define I2C_DUADEN_ENABLE ((uint8_t)0x01U) /*!< dual-address mode enabled */
  145. /* whether or not to stretch SCL low */
  146. #define I2C_SCLSTRETCH_ENABLE ((uint32_t)0x00000000U) /*!< SCL stretching is enabled */
  147. #define I2C_SCLSTRETCH_DISABLE I2C_CTL0_DISSTRC /*!< SCL stretching is disabled */
  148. /* whether or not to response to a General Call */
  149. #define I2C_GCEN_ENABLE I2C_CTL0_GCEN /*!< slave will response to a general call */
  150. #define I2C_GCEN_DISABLE ((uint32_t)0x00000000U) /*!< slave will not response to a general call */
  151. /* software reset I2C */
  152. #define I2C_SRESET_SET I2C_CTL0_SRESET /*!< I2C is under reset */
  153. #define I2C_SRESET_RESET ((uint32_t)0x00000000U) /*!< I2C is not under reset */
  154. /* I2C DMA mode configure */
  155. /* DMA mode switch */
  156. #define I2C_DMA_ON I2C_CTL1_DMAON /*!< DMA mode enabled */
  157. #define I2C_DMA_OFF ((uint32_t)0x00000000U) /*!< DMA mode disabled */
  158. /* flag indicating DMA last transfer */
  159. #define I2C_DMALST_ON I2C_CTL1_DMALST /*!< next DMA EOT is the last transfer */
  160. #define I2C_DMALST_OFF ((uint32_t)0x00000000U) /*!< next DMA EOT is not the last transfer */
  161. /* I2C PEC configure */
  162. /* PEC enable */
  163. #define I2C_PEC_ENABLE I2C_CTL0_PECEN /*!< PEC calculation on */
  164. #define I2C_PEC_DISABLE ((uint32_t)0x00000000U) /*!< PEC calculation off */
  165. /* PEC transfer */
  166. #define I2C_PECTRANS_ENABLE I2C_CTL0_PECTRANS /*!< transfer PEC */
  167. #define I2C_PECTRANS_DISABLE ((uint32_t)0x00000000U) /*!< not transfer PEC value */
  168. /* I2C SMBus configure */
  169. /* issue or not alert through SMBA pin */
  170. #define I2C_SALTSEND_ENABLE I2C_CTL0_SALT /*!< issue alert through SMBA pin */
  171. #define I2C_SALTSEND_DISABLE ((uint32_t)0x00000000U) /*!< not issue alert through SMBA */
  172. /* ARP protocol in SMBus switch */
  173. #define I2C_ARP_ENABLE I2C_CTL0_ARPEN /*!< ARP is enabled */
  174. #define I2C_ARP_DISABLE ((uint32_t)0x00000000U) /*!< ARP is disabled */
  175. /* I2C state */
  176. /* I2C bit state */
  177. #define I2C_SBSEND BIT(0) /*!< start condition sent out in master mode */
  178. #define I2C_ADDSEND BIT(1) /*!< address is sent in master mode or received and matches in slave mode */
  179. #define I2C_BTC BIT(2) /*!< byte transmission finishes */
  180. #define I2C_ADD10SEND BIT(3) /*!< header of 10-bit address is sent in master mode */
  181. #define I2C_STPDET BIT(4) /*!< etop condition detected in slave mode */
  182. #define I2C_RBNE BIT(6) /*!< I2C_DATA is not Empty during receiving */
  183. #define I2C_TBE BIT(7) /*!< I2C_DATA is empty during transmitting */
  184. #define I2C_BERR BIT(8) /*!< a bus error occurs indication a unexpected start or stop condition on I2C bus */
  185. #define I2C_LOSTARB BIT(9) /*!< arbitration lost in master mode */
  186. #define I2C_AERR BIT(10) /*!< acknowledge error */
  187. #define I2C_OUERR BIT(11) /*!< over-run or under-run situation occurs in slave mode */
  188. #define I2C_PECERR BIT(12) /*!< PEC error when receiving data */
  189. #define I2C_SMBTO BIT(14) /*!< timeout signal in SMBus mode */
  190. #define I2C_SMBALT BIT(15) /*!< SMBus alert status */
  191. #define I2C_MASTER (BIT(0)|BIT(31)) /*!< a flag indicating whether I2C block is in master or slave mode */
  192. #define I2C_I2CBSY (BIT(1)|BIT(31)) /*!< busy flag */
  193. #define I2C_TRS (BIT(2)|BIT(31)) /*!< whether the I2C is a transmitter or a receiver */
  194. #define I2C_RXGC (BIT(4)|BIT(31)) /*!< general call address (00h) received */
  195. #define I2C_DEFSMB (BIT(5)|BIT(31)) /*!< default address of SMBus device */
  196. #define I2C_HSTSMB (BIT(6)|BIT(31)) /*!< SMBus host header detected in slave mode */
  197. #define I2C_DUMODF (BIT(7)|BIT(31)) /*!< dual flag in slave mode indicating which address is matched in dual-address mode */
  198. /* I2C duty cycle in fast mode */
  199. #define CKCFG_DTCY(regval) (BIT(14) & ((uint32_t)(regval) << 14))
  200. #define I2C_DTCY_2 CKCFG_DTCY(0) /*!< I2C fast mode Tlow/Thigh = 2 */
  201. #define I2C_DTCY_16_9 CKCFG_DTCY(1) /*!< I2C fast mode Tlow/Thigh = 16/9 */
  202. /* address mode for the I2C slave */
  203. #define SADDR0_ADDFORMAT(regval) (BIT(15) & ((regval) << 15))
  204. #define I2C_ADDFORMAT_7BITS SADDR0_ADDFORMAT(0) /*!< address:7 bits */
  205. #define I2C_ADDFORMAT_10BITS SADDR0_ADDFORMAT(1) /*!< address:10 bits */
  206. /* function declarations */
  207. /* reset I2C */
  208. void i2c_deinit(uint32_t i2c_periph);
  209. /* I2C clock configure */
  210. void i2c_clock_config(uint32_t i2c_periph,uint32_t clkspeed,uint32_t dutycyc);
  211. /* I2C address configure */
  212. void i2c_mode_addr_config(uint32_t i2c_periph,uint32_t i2cmod,uint32_t addformat,uint32_t addr);
  213. /* SMBus type selection */
  214. void i2c_smbus_type_config(uint32_t i2c_periph,uint32_t type);
  215. /* whether or not to send an ACK */
  216. void i2c_ack_config(uint32_t i2c_periph,uint8_t ack);
  217. /* I2C POAP position configure */
  218. void i2c_ackpos_config(uint32_t i2c_periph,uint8_t pos);
  219. /* master send slave address */
  220. void i2c_master_addressing(uint32_t i2c_periph,uint8_t addr,uint32_t trandirection);
  221. /* dual-address mode switch */
  222. void i2c_dualaddr_enable(uint32_t i2c_periph, uint8_t dualaddr);
  223. /* enable i2c */
  224. void i2c_enable(uint32_t i2c_periph);
  225. /* disable i2c */
  226. void i2c_disable(uint32_t i2c_periph);
  227. /* generate a START condition on I2C bus */
  228. void i2c_start_on_bus(uint32_t i2c_periph);
  229. /* generate a STOP condition on I2C bus */
  230. void i2c_stop_on_bus(uint32_t i2c_periph);
  231. /* i2c transmit data function */
  232. void i2c_transmit_data(uint32_t i2c_periph,uint8_t data);
  233. /* i2c receive data function */
  234. uint8_t i2c_receive_data(uint32_t i2c_periph);
  235. /* I2C DMA mode enable */
  236. void i2c_dma_enable(uint32_t i2c_periph,uint32_t dmastste);
  237. /* flag indicating DMA last transfer */
  238. void i2c_dma_last_transfer_enable(uint32_t i2c_periph,uint32_t dmalast);
  239. /* whether to stretch SCL low when data is not ready in slave mode */
  240. void i2c_stretch_scl_low_config(uint32_t i2c_periph,uint32_t stretchpara );
  241. /* whether or not to response to a general call */
  242. void i2c_slave_response_to_gcall_config(uint32_t i2c_periph,uint32_t gcallpara);
  243. /* software reset I2C */
  244. void i2c_software_reset_config(uint32_t i2c_periph, uint32_t sreset);
  245. /* check i2c state */
  246. FlagStatus i2c_flag_get(uint32_t i2c_periph,uint32_t state);
  247. /* clear i2c state */
  248. void i2c_flag_clear(uint32_t i2c_periph,uint32_t state);
  249. /* enable i2c interrupt */
  250. void i2c_interrupt_enable(uint32_t i2c_periph,uint32_t inttype);
  251. /* disable i2c interrupt */
  252. void i2c_interrupt_disable(uint32_t i2c_periph,uint32_t inttype);
  253. /* I2C PEC calculation on or off */
  254. void i2c_pec_enable(uint32_t i2c_periph,uint32_t pecstate);
  255. /* I2C whether to transfer PEC value */
  256. void i2c_pec_transfer_enable(uint32_t i2c_periph,uint32_t pecpara);
  257. /* packet error checking value */
  258. uint8_t i2c_pec_value(uint32_t i2c_periph);
  259. /* I2C issue alert through SMBA pin */
  260. void i2c_smbus_alert_issue(uint32_t i2c_periph,uint32_t smbuspara);
  261. /* I2C ARP protocol in SMBus switch */
  262. void i2c_smbus_arp_enable(uint32_t i2c_periph,uint32_t arpstate);
  263. /* I2C analog noise filter disable */
  264. void i2c_analog_noise_filter_disable(uint32_t i2c_periph);
  265. /* I2C analog noise filter enable */
  266. void i2c_analog_noise_filter_enable(uint32_t i2c_periph);
  267. /* digital noise filter */
  268. void i2c_digital_noise_filter_config(uint32_t i2c_periph,i2c_digital_filter_enum dfilterpara);
  269. /* enable SAM_V interface */
  270. void i2c_sam_enable(uint32_t i2c_periph);
  271. /* disable SAM_V interface */
  272. void i2c_sam_disable(uint32_t i2c_periph);
  273. /* enable SAM_V interface timeout detect */
  274. void i2c_sam_timeout_enable(uint32_t i2c_periph);
  275. /* disable SAM_V interface timeout detect */
  276. void i2c_sam_timeout_disable(uint32_t i2c_periph);
  277. /* enable the specified I2C SAM interrupt */
  278. void i2c_sam_interrupt_enable(uint32_t i2c_periph,uint32_t inttype);
  279. /* disable the specified I2C SAM interrupt */
  280. void i2c_sam_interrupt_disable(uint32_t i2c_periph,uint32_t inttype);
  281. /* check i2c SAM state */
  282. FlagStatus i2c_sam_flag_get(uint32_t i2c_periph,uint32_t samstate);
  283. /* clear i2c SAM state */
  284. void i2c_sam_flag_clear(uint32_t i2c_periph,uint32_t samstate);
  285. #endif /* GD32F4XX_I2C_H */