gd32f30x_i2c.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*!
  2. \file gd32f30x_i2c.h
  3. \brief definitions for the I2C
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-02-10, V1.0.1, firmware for GD32F30x
  8. */
  9. #ifndef GD32F30X_I2C_H
  10. #define GD32F30X_I2C_H
  11. #include "gd32f30x.h"
  12. /* I2Cx(x=0,1) definitions */
  13. #define I2C0 I2C_BASE /*!< I2C0 base address */
  14. #define I2C1 (I2C_BASE+0x400U) /*!< I2C1 base address */
  15. /* registers definitions */
  16. #define I2C_CTL0(i2cx) REG32((i2cx) + 0x00U) /*!< I2C control register 0 */
  17. #define I2C_CTL1(i2cx) REG32((i2cx) + 0x04U) /*!< I2C control register 1 */
  18. #define I2C_SADDR0(i2cx) REG32((i2cx) + 0x08U) /*!< I2C slave address register 0*/
  19. #define I2C_SADDR1(i2cx) REG32((i2cx) + 0x0CU) /*!< I2C slave address register */
  20. #define I2C_DATA(i2cx) REG32((i2cx) + 0x10U) /*!< I2C transfer buffer register */
  21. #define I2C_STAT0(i2cx) REG32((i2cx) + 0x14U) /*!< I2C transfer status register 0 */
  22. #define I2C_STAT1(i2cx) REG32((i2cx) + 0x18U) /*!< I2C transfer status register */
  23. #define I2C_CKCFG(i2cx) REG32((i2cx) + 0x1CU) /*!< I2C clock configure register */
  24. #define I2C_RT(i2cx) REG32((i2cx) + 0x20U) /*!< I2C rise time register */
  25. #define I2C_FMPCFG(i2cx) REG32((i2cx) + 0x90U) /*!< I2C fast-mode-plus configure register */
  26. /* bits definitions */
  27. /* I2Cx_CTL0 */
  28. #define I2C_CTL0_I2CEN BIT(0) /*!< peripheral enable */
  29. #define I2C_CTL0_SMBEN BIT(1) /*!< SMBus mode */
  30. #define I2C_CTL0_SMBSEL BIT(3) /*!< SMBus type */
  31. #define I2C_CTL0_ARPEN BIT(4) /*!< ARP enable */
  32. #define I2C_CTL0_PECEN BIT(5) /*!< PEC enable */
  33. #define I2C_CTL0_GCEN BIT(6) /*!< general call enable */
  34. #define I2C_CTL0_DISSTRC BIT(7) /*!< clock stretching disable (slave mode) */
  35. #define I2C_CTL0_START BIT(8) /*!< start generation */
  36. #define I2C_CTL0_STOP BIT(9) /*!< stop generation */
  37. #define I2C_CTL0_ACKEN BIT(10) /*!< acknowledge enable */
  38. #define I2C_CTL0_POAP BIT(11) /*!< acknowledge/PEC position (for data reception) */
  39. #define I2C_CTL0_PECTRANS BIT(12) /*!< packet error checking */
  40. #define I2C_CTL0_SALT BIT(13) /*!< SMBus alert */
  41. #define I2C_CTL0_SRESET BIT(15) /*!< software reset */
  42. /* I2Cx_CTL1 */
  43. #define I2C_CTL1_I2CCLK BITS(0,6) /*!< I2CCLK[6:0] bits (peripheral clock frequency) */
  44. #define I2C_CTL1_ERRIE BIT(8) /*!< error interrupt inable */
  45. #define I2C_CTL1_EVIE BIT(9) /*!< event interrupt enable */
  46. #define I2C_CTL1_BUFIE BIT(10) /*!< buffer interrupt enable */
  47. #define I2C_CTL1_DMAON BIT(11) /*!< DMA requests enable */
  48. #define I2C_CTL1_DMALST BIT(12) /*!< DMA last transfer */
  49. /* I2Cx_SADDR0 */
  50. #define I2C_SADDR0_ADDRESS0 BIT(0) /*!< bit 0 of a 10-bit address */
  51. #define I2C_SADDR0_ADDRESS BITS(1,7) /*!< 7-bit address or bits 7:1 of a 10-bit address */
  52. #define I2C_SADDR0_ADDRESS_H BITS(8,9) /*!< highest two bits of a 10-bit address */
  53. #define I2C_SADDR0_ADDFORMAT BIT(15) /*!< address mode for the I2C slave */
  54. /* I2Cx_SADDR1 */
  55. #define I2C_SADDR1_DUADEN BIT(0) /*!< aual-address mode switch */
  56. #define I2C_SADDR1_ADDRESS2 BITS(1,7) /*!< second I2C address for the slave in dual-address mode */
  57. /* I2Cx_DATA */
  58. #define I2C_DATA_TRB BITS(0,7) /*!< 8-bit data register */
  59. /* I2Cx_STAT0 */
  60. #define I2C_STAT0_SBSEND BIT(0) /*!< start bit (master mode) */
  61. #define I2C_STAT0_ADDSEND BIT(1) /*!< address sent (master mode)/matched (slave mode) */
  62. #define I2C_STAT0_BTC BIT(2) /*!< byte transfer finished */
  63. #define I2C_STAT0_ADD10SEND BIT(3) /*!< 10-bit header sent (master mode) */
  64. #define I2C_STAT0_STPDET BIT(4) /*!< stop detection (slave mode) */
  65. #define I2C_STAT0_RBNE BIT(6) /*!< data register not empty (receivers) */
  66. #define I2C_STAT0_TBE BIT(7) /*!< data register empty (transmitters) */
  67. #define I2C_STAT0_BERR BIT(8) /*!< bus error */
  68. #define I2C_STAT0_LOSTARB BIT(9) /*!< arbitration lost (master mode) */
  69. #define I2C_STAT0_AERR BIT(10) /*!< acknowledge failure */
  70. #define I2C_STAT0_OUERR BIT(11) /*!< overrun/underrun */
  71. #define I2C_STAT0_PECERR BIT(12) /*!< PEC error in reception */
  72. #define I2C_STAT0_SMBTO BIT(14) /*!< timeout signal in SMBus mode */
  73. #define I2C_STAT0_SMBALT BIT(15) /*!< SMBus alert status */
  74. /* I2Cx_STAT1 */
  75. #define I2C_STAT1_MASTER BIT(0) /*!< master/slave */
  76. #define I2C_STAT1_I2CBSY BIT(1) /*!< bus busy */
  77. #define I2C_STAT1_TRS BIT(2) /*!< transmitter/receiver */
  78. #define I2C_STAT1_RXGC BIT(4) /*!< general call address (slave mode) */
  79. #define I2C_STAT1_DEFSMB BIT(5) /*!< SMBus device default address (slave mode) */
  80. #define I2C_STAT1_HSTSMB BIT(6) /*!< SMBus host header (slave mode) */
  81. #define I2C_STAT1_DUMODF BIT(7) /*!< dual flag (slave mode) */
  82. #define I2C_STAT1_ECV BITS(8,15) /*!< packet error checking value */
  83. /* I2Cx_CKCFG */
  84. #define I2C_CKCFG_CLKC BITS(0,11) /*!< clock control register in fast/standard mode or fast mode plus(master mode) */
  85. #define I2C_CKCFG_DTCY BIT(14) /*!< duty cycle of fast mode or fast mode plus */
  86. #define I2C_CKCFG_FAST BIT(15) /*!< I2C speed selection in master mode */
  87. /* I2Cx_RT */
  88. #define I2C_RT_RISETIME BITS(0,6) /*!< maximum rise time in fast/standard mode or fast mode plus(master mode) */
  89. /* I2Cx_FMPCFG */
  90. #define I2C_FMPCFG_FMPEN BIT(0) /*!< fast mode plus enable bit */
  91. /* constants definitions */
  92. /* SMBus/I2C mode switch and SMBus type selection */
  93. #define I2C_I2CMODE_ENABLE ((uint32_t)0x00000000U) /*!< I2C mode */
  94. #define I2C_SMBUSMODE_ENABLE I2C_CTL0_SMBEN /*!< SMBus mode */
  95. /* SMBus/I2C mode switch and SMBus type selection */
  96. #define I2C_SMBUS_DEVICE ((uint32_t)0x00000000U) /*!< SMBus mode device type */
  97. #define I2C_SMBUS_HOST I2C_CTL0_SMBSEL /*!< SMBus mode host type */
  98. /* I2C transfer direction */
  99. #define I2C_RECEIVER ((uint32_t)0x00000001U) /*!< receiver */
  100. #define I2C_TRANSMITTER ((uint32_t)0xFFFFFFFEU) /*!< transmitter */
  101. /* whether or not to send an ACK */
  102. #define I2C_ACK_DISABLE ((uint32_t)0x00000000U) /*!< ACK will be not sent */
  103. #define I2C_ACK_ENABLE ((uint32_t)0x00000001U) /*!< ACK will be sent */
  104. /* I2C POAP position*/
  105. #define I2C_ACKPOS_NEXT ((uint32_t)0x00000000U) /*!< ACKEN bit decides whether or not to send ACK for the next byte */
  106. #define I2C_ACKPOS_CURRENT ((uint32_t)0x00000001U) /*!< ACKEN bit decides whether or not to send ACK or not for the current byte */
  107. /* I2C dual-address mode switch */
  108. #define I2C_DUADEN_DISABLE ((uint32_t)0x00000000U) /*!< dual-address mode disabled */
  109. #define I2C_DUADEN_ENABLE ((uint32_t)0x00000001U) /*!< dual-address mode enabled */
  110. /* whether or not to stretch SCL low */
  111. #define I2C_SCLSTRETCH_ENABLE ((uint32_t)0x00000000U) /*!< SCL stretching is enabled */
  112. #define I2C_SCLSTRETCH_DISABLE I2C_CTL0_DISSTRC /*!< SCL stretching is disabled */
  113. /* whether or not to response to a general call */
  114. #define I2C_GCEN_ENABLE I2C_CTL0_GCEN /*!< slave will response to a general call */
  115. #define I2C_GCEN_DISABLE ((uint32_t)0x00000000U) /*!< slave will not response to a general call */
  116. /* software reset I2C */
  117. #define I2C_SRESET_SET I2C_CTL0_SRESET /*!< I2C is under reset */
  118. #define I2C_SRESET_RESET ((uint32_t)0x00000000U) /*!< I2C is not under reset */
  119. /* I2C DMA mode configure */
  120. /* DMA mode switch */
  121. #define I2C_DMA_ON I2C_CTL1_DMAON /*!< DMA mode enabled */
  122. #define I2C_DMA_OFF ((uint32_t)0x00000000U) /*!< DMA mode disabled */
  123. /* flag indicating DMA last transfer */
  124. #define I2C_DMALST_ON I2C_CTL1_DMALST /*!< next DMA EOT is the last transfer */
  125. #define I2C_DMALST_OFF ((uint32_t)0x00000000U) /*!< next DMA EOT is not the last transfer */
  126. /* I2C PEC configure */
  127. /* PEC enable */
  128. #define I2C_PEC_ENABLE I2C_CTL0_PECEN /*!< PEC calculation on */
  129. #define I2C_PEC_DISABLE ((uint32_t)0x00000000U) /*!< PEC calculation off */
  130. /* PEC transfer */
  131. #define I2C_PECTRANS_ENABLE I2C_CTL0_PECTRANS /*!< transfer PEC */
  132. #define I2C_PECTRANS_DISABLE ((uint32_t)0x00000000U) /*!< not transfer PEC value */
  133. /* I2C SMBus configure */
  134. /* issue or not alert through SMBA pin */
  135. #define I2C_SALTSEND_ENABLE I2C_CTL0_SALT /*!< issue alert through SMBA pin */
  136. #define I2C_SALTSEND_DISABLE ((uint32_t)0x00000000U) /*!< not issue alert through SMBA */
  137. /* ARP protocol in SMBus switch */
  138. #define I2C_ARP_ENABLE I2C_CTL0_ARPEN /*!< ARP enable */
  139. #define I2C_ARP_DISABLE ((uint32_t)0x00000000U) /*!< ARP disable */
  140. /* fast mode plus enable */
  141. #define I2C_FAST_MODE_PLUS_ENABLE I2C_FMPCFG_FMPEN /*!< fast mode plus enable */
  142. #define I2C_FAST_MODE_PLUS_DISABLE ((uint32_t)0x00000000U) /*!< fast mode plus disable */
  143. /* transmit I2C data */
  144. #define DATA_TRANS(regval) (BITS(0,7) & ((uint32_t)(regval) << 0))
  145. /* receive I2C data */
  146. #define DATA_RECV(regval) GET_BITS((uint32_t)(regval), 0, 7)
  147. /* I2C flag definitions */
  148. #define I2C_FLAG_SBSEND BIT(0) /*!< start condition sent out in master mode */
  149. #define I2C_FLAG_ADDSEND BIT(1) /*!< address is sent in master mode or received and matches in slave mode */
  150. #define I2C_FLAG_BTC BIT(2) /*!< byte transmission finishes */
  151. #define I2C_FLAG_ADD10SEND BIT(3) /*!< header of 10-bit address is sent in master mode */
  152. #define I2C_FLAG_STPDET BIT(4) /*!< etop condition detected in slave mode */
  153. #define I2C_FLAG_RBNE BIT(6) /*!< I2C_DATA is not Empty during receiving */
  154. #define I2C_FLAG_TBE BIT(7) /*!< I2C_DATA is empty during transmitting */
  155. #define I2C_FLAG_BERR BIT(8) /*!< a bus error occurs indication a unexpected start or stop condition on I2C bus */
  156. #define I2C_FLAG_LOSTARB BIT(9) /*!< arbitration lost in master mode */
  157. #define I2C_FLAG_AERR BIT(10) /*!< acknowledge error */
  158. #define I2C_FLAG_OUERR BIT(11) /*!< over-run or under-run situation occurs in slave mode */
  159. #define I2C_FLAG_PECERR BIT(12) /*!< PEC error when receiving data */
  160. #define I2C_FLAG_SMBTO BIT(14) /*!< timeout signal in SMBus mode */
  161. #define I2C_FLAG_SMBALT BIT(15) /*!< SMBus alert status */
  162. #define I2C_FLAG_MASTER (BIT(0)|BIT(31)) /*!< a flag indicating whether I2C block is in master or slave mode */
  163. #define I2C_FLAG_I2CBSY (BIT(1)|BIT(31)) /*!< busy flag */
  164. #define I2C_FLAG_TRS (BIT(2)|BIT(31)) /*!< whether the I2C is a transmitter or a receiver */
  165. #define I2C_FLAG_RXGC (BIT(4)|BIT(31)) /*!< general call address (00h) received */
  166. #define I2C_FLAG_DEFSMB (BIT(5)|BIT(31)) /*!< default address of SMBus device */
  167. #define I2C_FLAG_HSTSMB (BIT(6)|BIT(31)) /*!< SMBus host header detected in slave mode */
  168. #define I2C_FLAG_DUMOD (BIT(7)|BIT(31)) /*!< dual flag in slave mode indicating which address is matched in dual-address mode */
  169. /* I2C interrupt flags */
  170. #define I2C_INT_FLAG_SBSEND I2C_FLAG_SBSEND /*!< start condition sent out in master mode interrupt flag */
  171. #define I2C_INT_FLAG_ADDSEND I2C_FLAG_ADDSEND /*!< address is sent in master mode or received and matches in slave mode interrupt flag */
  172. #define I2C_INT_FLAG_BTC I2C_FLAG_BTC /*!< byte transmission finishes */
  173. #define I2C_INT_FLAG_ADD10SEND I2C_FLAG_ADD10SEND /*!< header of 10-bit address is sent in master mode interrupt flag */
  174. #define I2C_INT_FLAG_STPDET I2C_FLAG_STPDET /*!< stop condition detected in slave mode interrupt flag */
  175. #define I2C_INT_FLAG_RBNE I2C_FLAG_RBNE /*!< I2C_DATA is not Empty during receiving interrupt flag */
  176. #define I2C_INT_FLAG_TBE I2C_FLAG_TBE /*!< I2C_DATA is empty during transmitting interrupt flag */
  177. #define I2C_INT_FLAG_BERR I2C_FLAG_BERR /*!< a bus error occurs indication a unexpected start or stop condition on I2C bus interrupt flag */
  178. #define I2C_INT_FLAG_LOSTARB I2C_FLAG_LOSTARB /*!< arbitration lost in master mode interrupt flag */
  179. #define I2C_INT_FLAG_AERR I2C_FLAG_AERR /*!< acknowledge error interrupt flag */
  180. #define I2C_INT_FLAG_OUERR I2C_FLAG_OUERR /*!< over-run or under-run situation occurs in slave mode interrupt flag */
  181. #define I2C_INT_FLAG_PECERR I2C_FLAG_PECERR /*!< PEC error when receiving data interrupt flag */
  182. #define I2C_INT_FLAG_SMBTO I2C_FLAG_SMBTO /*!< timeout signal in SMBus mode interrupt flag */
  183. #define I2C_INT_FLAG_SMBALT I2C_FLAG_SMBALT /*!< SMBus Alert status interrupt flag */
  184. /* I2C interrupt enable bit */
  185. #define I2C_INT_ERR I2C_CTL1_ERRIE /*!< error interrupt enable */
  186. #define I2C_INT_EV I2C_CTL1_EVIE /*!< event interrupt enable */
  187. #define I2C_INT_BUF I2C_CTL1_BUFIE /*!< buffer interrupt enable */
  188. /* I2C duty cycle in fast mode or fast mode plus */
  189. #define CKCFG_DTCY(regval) (BIT(14) & ((uint32_t)(regval) << 14))
  190. #define I2C_DTCY_2 CKCFG_DTCY(0) /*!< in I2C fast mode or fast mode plus Tlow/Thigh = 2 */
  191. #define I2C_DTCY_16_9 CKCFG_DTCY(1) /*!< in I2C fast mode or fast mode plus Tlow/Thigh = 16/9 */
  192. /* address mode for the I2C slave */
  193. #define SADDR0_ADDFORMAT(regval) (BIT(15) & ((uint32_t)(regval) << 15))
  194. #define I2C_ADDFORMAT_7BITS SADDR0_ADDFORMAT(0) /*!< address:7 bits */
  195. #define I2C_ADDFORMAT_10BITS SADDR0_ADDFORMAT(1) /*!< address:10 bits */
  196. /* function declarations */
  197. /* reset I2C */
  198. void i2c_deinit(uint32_t i2c_periph);
  199. /* configure I2C clock */
  200. void i2c_clock_config(uint32_t i2c_periph, uint32_t clkspeed, uint32_t dutycyc);
  201. /* configure I2C address */
  202. void i2c_mode_addr_config(uint32_t i2c_periph, uint32_t mode, uint32_t addformat, uint32_t addr);
  203. /* SMBus type selection */
  204. void i2c_smbus_type_config(uint32_t i2c_periph, uint32_t type);
  205. /* whether or not to send an ACK */
  206. void i2c_ack_config(uint32_t i2c_periph, uint32_t ack);
  207. /* configure I2C POAP position */
  208. void i2c_ackpos_config(uint32_t i2c_periph, uint32_t pos);
  209. /* master send slave address */
  210. void i2c_master_addressing(uint32_t i2c_periph, uint32_t addr, uint32_t trandirection);
  211. /* dual-address mode switch */
  212. void i2c_dualaddr_enable(uint32_t i2c_periph, uint32_t dualaddr);
  213. /* enable I2C */
  214. void i2c_enable(uint32_t i2c_periph);
  215. /* disable I2C */
  216. void i2c_disable(uint32_t i2c_periph);
  217. /* generate a START condition on I2C bus */
  218. void i2c_start_on_bus(uint32_t i2c_periph);
  219. /* generate a STOP condition on I2C bus */
  220. void i2c_stop_on_bus(uint32_t i2c_periph);
  221. /* I2C transmit data function */
  222. void i2c_data_transmit(uint32_t i2c_periph, uint8_t data);
  223. /* I2C receive data function */
  224. uint8_t i2c_data_receive(uint32_t i2c_periph);
  225. /* I2C DMA mode enable */
  226. void i2c_dma_enable(uint32_t i2c_periph, uint32_t dmastate);
  227. /* flag indicating DMA last transfer */
  228. void i2c_dma_last_transfer_enable(uint32_t i2c_periph, uint32_t dmalast);
  229. /* whether to stretch SCL low when data is not ready in slave mode */
  230. void i2c_stretch_scl_low_config(uint32_t i2c_periph, uint32_t stretchpara );
  231. /* whether or not to response to a general call */
  232. void i2c_slave_response_to_gcall_config(uint32_t i2c_periph, uint32_t gcallpara);
  233. /* software reset I2C */
  234. void i2c_software_reset_config(uint32_t i2c_periph, uint32_t sreset);
  235. /* check I2C flag is set or not */
  236. FlagStatus i2c_flag_get(uint32_t i2c_periph, uint32_t flag);
  237. /* clear I2C flag */
  238. void i2c_flag_clear(uint32_t i2c_periph, uint32_t flag);
  239. /* enable I2C interrupt */
  240. void i2c_interrupt_enable(uint32_t i2c_periph, uint32_t inttype);
  241. /* disable I2C interrupt */
  242. void i2c_interrupt_disable(uint32_t i2c_periph, uint32_t inttype);
  243. /* check I2C interrupt flag */
  244. FlagStatus i2c_interrupt_flag_get(uint32_t i2c_periph,uint32_t intflag);
  245. /* clear I2C interrupt flag */
  246. void i2c_interrupt_flag_clear(uint32_t i2c_periph,uint32_t intflag);
  247. /* I2C PEC calculation on or off */
  248. void i2c_pec_enable(uint32_t i2c_periph, uint32_t pecstate);
  249. /* I2C whether to transfer PEC value */
  250. void i2c_pec_transfer_enable(uint32_t i2c_periph, uint32_t pecpara);
  251. /* packet error checking value */
  252. uint8_t i2c_pec_value_get(uint32_t i2c_periph);
  253. /* I2C issue alert through SMBA pin */
  254. void i2c_smbus_issue_alert(uint32_t i2c_periph, uint32_t smbuspara);
  255. /* I2C ARP protocol in SMBus switch */
  256. void i2c_smbus_arp_enable(uint32_t i2c_periph, uint32_t arpstate);
  257. #endif /* GD32F30X_I2C_H */