gd32f30x_i2c.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*!
  2. \file gd32f30x_i2c.h
  3. \brief definitions for the I2C
  4. \version 2017-02-10, V1.0.0, firmware for GD32F30x
  5. \version 2018-10-10, V1.1.0, firmware for GD32F30x
  6. \version 2018-12-25, V2.0.0, firmware for GD32F30x
  7. \version 2019-04-16, V2.0.1, firmware for GD32F30x
  8. \version 2020-09-30, V2.1.0, firmware for GD32F30x
  9. */
  10. /*
  11. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  12. Redistribution and use in source and binary forms, with or without modification,
  13. are permitted provided that the following conditions are met:
  14. 1. Redistributions of source code must retain the above copyright notice, this
  15. list of conditions and the following disclaimer.
  16. 2. Redistributions in binary form must reproduce the above copyright notice,
  17. this list of conditions and the following disclaimer in the documentation
  18. and/or other materials provided with the distribution.
  19. 3. Neither the name of the copyright holder nor the names of its contributors
  20. may be used to endorse or promote products derived from this software without
  21. specific prior written permission.
  22. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  24. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  25. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  26. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  27. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  31. OF SUCH DAMAGE.
  32. */
  33. #ifndef GD32F30X_I2C_H
  34. #define GD32F30X_I2C_H
  35. #include "gd32f30x.h"
  36. /* I2Cx(x=0,1) definitions */
  37. #define I2C0 I2C_BASE /*!< I2C0 base address */
  38. #define I2C1 (I2C_BASE + 0x00000400U) /*!< I2C1 base address */
  39. /* registers definitions */
  40. #define I2C_CTL0(i2cx) REG32((i2cx) + 0x00000000U) /*!< I2C control register 0 */
  41. #define I2C_CTL1(i2cx) REG32((i2cx) + 0x00000004U) /*!< I2C control register 1 */
  42. #define I2C_SADDR0(i2cx) REG32((i2cx) + 0x00000008U) /*!< I2C slave address register 0*/
  43. #define I2C_SADDR1(i2cx) REG32((i2cx) + 0x0000000CU) /*!< I2C slave address register */
  44. #define I2C_DATA(i2cx) REG32((i2cx) + 0x00000010U) /*!< I2C transfer buffer register */
  45. #define I2C_STAT0(i2cx) REG32((i2cx) + 0x00000014U) /*!< I2C transfer status register 0 */
  46. #define I2C_STAT1(i2cx) REG32((i2cx) + 0x00000018U) /*!< I2C transfer status register */
  47. #define I2C_CKCFG(i2cx) REG32((i2cx) + 0x0000001CU) /*!< I2C clock configure register */
  48. #define I2C_RT(i2cx) REG32((i2cx) + 0x00000020U) /*!< I2C rise time register */
  49. #define I2C_FMPCFG(i2cx) REG32((i2cx) + 0x00000090U) /*!< I2C fast-mode-plus configure register */
  50. /* bits definitions */
  51. /* I2Cx_CTL0 */
  52. #define I2C_CTL0_I2CEN BIT(0) /*!< peripheral enable */
  53. #define I2C_CTL0_SMBEN BIT(1) /*!< SMBus mode */
  54. #define I2C_CTL0_SMBSEL BIT(3) /*!< SMBus type */
  55. #define I2C_CTL0_ARPEN BIT(4) /*!< ARP enable */
  56. #define I2C_CTL0_PECEN BIT(5) /*!< PEC enable */
  57. #define I2C_CTL0_GCEN BIT(6) /*!< general call enable */
  58. #define I2C_CTL0_SS BIT(7) /*!< clock stretching disable (slave mode) */
  59. #define I2C_CTL0_START BIT(8) /*!< start generation */
  60. #define I2C_CTL0_STOP BIT(9) /*!< stop generation */
  61. #define I2C_CTL0_ACKEN BIT(10) /*!< acknowledge enable */
  62. #define I2C_CTL0_POAP BIT(11) /*!< acknowledge/PEC position (for data reception) */
  63. #define I2C_CTL0_PECTRANS BIT(12) /*!< packet error checking */
  64. #define I2C_CTL0_SALT BIT(13) /*!< SMBus alert */
  65. #define I2C_CTL0_SRESET BIT(15) /*!< software reset */
  66. /* I2Cx_CTL1 */
  67. #define I2C_CTL1_I2CCLK BITS(0,6) /*!< I2CCLK[6:0] bits (peripheral clock frequency) */
  68. #define I2C_CTL1_ERRIE BIT(8) /*!< error interrupt inable */
  69. #define I2C_CTL1_EVIE BIT(9) /*!< event interrupt enable */
  70. #define I2C_CTL1_BUFIE BIT(10) /*!< buffer interrupt enable */
  71. #define I2C_CTL1_DMAON BIT(11) /*!< DMA requests enable */
  72. #define I2C_CTL1_DMALST BIT(12) /*!< DMA last transfer */
  73. /* I2Cx_SADDR0 */
  74. #define I2C_SADDR0_ADDRESS0 BIT(0) /*!< bit 0 of a 10-bit address */
  75. #define I2C_SADDR0_ADDRESS BITS(1,7) /*!< 7-bit address or bits 7:1 of a 10-bit address */
  76. #define I2C_SADDR0_ADDRESS_H BITS(8,9) /*!< highest two bits of a 10-bit address */
  77. #define I2C_SADDR0_ADDFORMAT BIT(15) /*!< address mode for the I2C slave */
  78. /* I2Cx_SADDR1 */
  79. #define I2C_SADDR1_DUADEN BIT(0) /*!< aual-address mode switch */
  80. #define I2C_SADDR1_ADDRESS2 BITS(1,7) /*!< second I2C address for the slave in dual-address mode */
  81. /* I2Cx_DATA */
  82. #define I2C_DATA_TRB BITS(0,7) /*!< 8-bit data register */
  83. /* I2Cx_STAT0 */
  84. #define I2C_STAT0_SBSEND BIT(0) /*!< start bit (master mode) */
  85. #define I2C_STAT0_ADDSEND BIT(1) /*!< address sent (master mode)/matched (slave mode) */
  86. #define I2C_STAT0_BTC BIT(2) /*!< byte transfer finished */
  87. #define I2C_STAT0_ADD10SEND BIT(3) /*!< 10-bit header sent (master mode) */
  88. #define I2C_STAT0_STPDET BIT(4) /*!< stop detection (slave mode) */
  89. #define I2C_STAT0_RBNE BIT(6) /*!< data register not empty (receivers) */
  90. #define I2C_STAT0_TBE BIT(7) /*!< data register empty (transmitters) */
  91. #define I2C_STAT0_BERR BIT(8) /*!< bus error */
  92. #define I2C_STAT0_LOSTARB BIT(9) /*!< arbitration lost (master mode) */
  93. #define I2C_STAT0_AERR BIT(10) /*!< acknowledge failure */
  94. #define I2C_STAT0_OUERR BIT(11) /*!< overrun/underrun */
  95. #define I2C_STAT0_PECERR BIT(12) /*!< PEC error in reception */
  96. #define I2C_STAT0_SMBTO BIT(14) /*!< timeout signal in SMBus mode */
  97. #define I2C_STAT0_SMBALT BIT(15) /*!< SMBus alert status */
  98. /* I2Cx_STAT1 */
  99. #define I2C_STAT1_MASTER BIT(0) /*!< master/slave */
  100. #define I2C_STAT1_I2CBSY BIT(1) /*!< bus busy */
  101. #define I2C_STAT1_TR BIT(2) /*!< transmitter/receiver */
  102. #define I2C_STAT1_RXGC BIT(4) /*!< general call address (slave mode) */
  103. #define I2C_STAT1_DEFSMB BIT(5) /*!< SMBus device default address (slave mode) */
  104. #define I2C_STAT1_HSTSMB BIT(6) /*!< SMBus host header (slave mode) */
  105. #define I2C_STAT1_DUMODF BIT(7) /*!< dual flag (slave mode) */
  106. #define I2C_STAT1_PECV BITS(8,15) /*!< packet error checking value */
  107. /* I2Cx_CKCFG */
  108. #define I2C_CKCFG_CLKC BITS(0,11) /*!< clock control register in fast/standard mode or fast mode plus(master mode) */
  109. #define I2C_CKCFG_DTCY BIT(14) /*!< duty cycle of fast mode or fast mode plus */
  110. #define I2C_CKCFG_FAST BIT(15) /*!< I2C speed selection in master mode */
  111. /* I2Cx_RT */
  112. #define I2C_RT_RISETIME BITS(0,6) /*!< maximum rise time in fast/standard mode or fast mode plus(master mode) */
  113. /* I2Cx_FMPCFG */
  114. #define I2C_FMPCFG_FMPEN BIT(0) /*!< fast mode plus enable bit */
  115. /* constants definitions */
  116. /* define the I2C bit position and its register index offset */
  117. #define I2C_REGIDX_BIT(regidx, bitpos) (((uint32_t)(regidx) << 6) | (uint32_t)(bitpos))
  118. #define I2C_REG_VAL(i2cx, offset) (REG32((i2cx) + (((uint32_t)(offset) & 0xFFFFU) >> 6)))
  119. #define I2C_BIT_POS(val) ((uint32_t)(val) & 0x1FU)
  120. #define I2C_REGIDX_BIT2(regidx, bitpos, regidx2, bitpos2) (((uint32_t)(regidx2) << 22) | (uint32_t)((bitpos2) << 16)\
  121. | (((uint32_t)(regidx) << 6) | (uint32_t)(bitpos)))
  122. #define I2C_REG_VAL2(i2cx, offset) (REG32((i2cx) + ((uint32_t)(offset) >> 22)))
  123. #define I2C_BIT_POS2(val) (((uint32_t)(val) & 0x1F0000U) >> 16)
  124. /* register offset */
  125. #define I2C_CTL1_REG_OFFSET 0x04U /*!< CTL1 register offset */
  126. #define I2C_STAT0_REG_OFFSET 0x14U /*!< STAT0 register offset */
  127. #define I2C_STAT1_REG_OFFSET 0x18U /*!< STAT1 register offset */
  128. /* I2C flags */
  129. typedef enum
  130. {
  131. /* flags in STAT0 register */
  132. I2C_FLAG_SBSEND = I2C_REGIDX_BIT(I2C_STAT0_REG_OFFSET, 0U), /*!< start condition sent out in master mode */
  133. I2C_FLAG_ADDSEND = I2C_REGIDX_BIT(I2C_STAT0_REG_OFFSET, 1U), /*!< address is sent in master mode or received and matches in slave mode */
  134. I2C_FLAG_BTC = I2C_REGIDX_BIT(I2C_STAT0_REG_OFFSET, 2U), /*!< byte transmission finishes */
  135. I2C_FLAG_ADD10SEND = I2C_REGIDX_BIT(I2C_STAT0_REG_OFFSET, 3U), /*!< header of 10-bit address is sent in master mode */
  136. I2C_FLAG_STPDET = I2C_REGIDX_BIT(I2C_STAT0_REG_OFFSET, 4U), /*!< stop condition detected in slave mode */
  137. I2C_FLAG_RBNE = I2C_REGIDX_BIT(I2C_STAT0_REG_OFFSET, 6U), /*!< I2C_DATA is not Empty during receiving */
  138. I2C_FLAG_TBE = I2C_REGIDX_BIT(I2C_STAT0_REG_OFFSET, 7U), /*!< I2C_DATA is empty during transmitting */
  139. I2C_FLAG_BERR = I2C_REGIDX_BIT(I2C_STAT0_REG_OFFSET, 8U), /*!< a bus error occurs indication a unexpected start or stop condition on I2C bus */
  140. I2C_FLAG_LOSTARB = I2C_REGIDX_BIT(I2C_STAT0_REG_OFFSET, 9U), /*!< arbitration lost in master mode */
  141. I2C_FLAG_AERR = I2C_REGIDX_BIT(I2C_STAT0_REG_OFFSET, 10U), /*!< acknowledge error */
  142. I2C_FLAG_OUERR = I2C_REGIDX_BIT(I2C_STAT0_REG_OFFSET, 11U), /*!< over-run or under-run situation occurs in slave mode */
  143. I2C_FLAG_PECERR = I2C_REGIDX_BIT(I2C_STAT0_REG_OFFSET, 12U), /*!< PEC error when receiving data */
  144. I2C_FLAG_SMBTO = I2C_REGIDX_BIT(I2C_STAT0_REG_OFFSET, 14U), /*!< timeout signal in SMBus mode */
  145. I2C_FLAG_SMBALT = I2C_REGIDX_BIT(I2C_STAT0_REG_OFFSET, 15U), /*!< SMBus alert status */
  146. /* flags in STAT1 register */
  147. I2C_FLAG_MASTER = I2C_REGIDX_BIT(I2C_STAT1_REG_OFFSET, 0U), /*!< a flag indicating whether I2C block is in master or slave mode */
  148. I2C_FLAG_I2CBSY = I2C_REGIDX_BIT(I2C_STAT1_REG_OFFSET, 1U), /*!< busy flag */
  149. I2C_FLAG_TRS = I2C_REGIDX_BIT(I2C_STAT1_REG_OFFSET, 2U), /*!< whether the I2C is a transmitter or a receiver */
  150. I2C_FLAG_RXGC = I2C_REGIDX_BIT(I2C_STAT1_REG_OFFSET, 4U), /*!< general call address (00h) received */
  151. I2C_FLAG_DEFSMB = I2C_REGIDX_BIT(I2C_STAT1_REG_OFFSET, 5U), /*!< default address of SMBus device */
  152. I2C_FLAG_HSTSMB = I2C_REGIDX_BIT(I2C_STAT1_REG_OFFSET, 6U), /*!< SMBus host header detected in slave mode */
  153. I2C_FLAG_DUMOD = I2C_REGIDX_BIT(I2C_STAT1_REG_OFFSET, 7U) /*!< dual flag in slave mode indicating which address is matched in dual-address mode */
  154. }i2c_flag_enum;
  155. /* I2C interrupt flags */
  156. typedef enum
  157. {
  158. /* interrupt flags in CTL1 register */
  159. I2C_INT_FLAG_SBSEND = I2C_REGIDX_BIT2(I2C_CTL1_REG_OFFSET, 9U, I2C_STAT0_REG_OFFSET, 0U), /*!< start condition sent out in master mode interrupt flag */
  160. I2C_INT_FLAG_ADDSEND = I2C_REGIDX_BIT2(I2C_CTL1_REG_OFFSET, 9U, I2C_STAT0_REG_OFFSET, 1U), /*!< address is sent in master mode or received and matches in slave mode interrupt flag */
  161. I2C_INT_FLAG_BTC = I2C_REGIDX_BIT2(I2C_CTL1_REG_OFFSET, 9U, I2C_STAT0_REG_OFFSET, 2U), /*!< byte transmission finishes */
  162. I2C_INT_FLAG_ADD10SEND = I2C_REGIDX_BIT2(I2C_CTL1_REG_OFFSET, 9U, I2C_STAT0_REG_OFFSET, 3U), /*!< header of 10-bit address is sent in master mode interrupt flag */
  163. I2C_INT_FLAG_STPDET = I2C_REGIDX_BIT2(I2C_CTL1_REG_OFFSET, 9U, I2C_STAT0_REG_OFFSET, 4U), /*!< stop condition detected in slave mode interrupt flag */
  164. I2C_INT_FLAG_RBNE = I2C_REGIDX_BIT2(I2C_CTL1_REG_OFFSET, 9U, I2C_STAT0_REG_OFFSET, 6U), /*!< I2C_DATA is not Empty during receiving interrupt flag */
  165. I2C_INT_FLAG_TBE = I2C_REGIDX_BIT2(I2C_CTL1_REG_OFFSET, 9U, I2C_STAT0_REG_OFFSET, 7U), /*!< I2C_DATA is empty during transmitting interrupt flag */
  166. I2C_INT_FLAG_BERR = I2C_REGIDX_BIT2(I2C_CTL1_REG_OFFSET, 8U, I2C_STAT0_REG_OFFSET, 8U), /*!< a bus error occurs indication a unexpected start or stop condition on I2C bus interrupt flag */
  167. I2C_INT_FLAG_LOSTARB = I2C_REGIDX_BIT2(I2C_CTL1_REG_OFFSET, 8U, I2C_STAT0_REG_OFFSET, 9U), /*!< arbitration lost in master mode interrupt flag */
  168. I2C_INT_FLAG_AERR = I2C_REGIDX_BIT2(I2C_CTL1_REG_OFFSET, 8U, I2C_STAT0_REG_OFFSET, 10U), /*!< acknowledge error interrupt flag */
  169. I2C_INT_FLAG_OUERR = I2C_REGIDX_BIT2(I2C_CTL1_REG_OFFSET, 8U, I2C_STAT0_REG_OFFSET, 11U), /*!< over-run or under-run situation occurs in slave mode interrupt flag */
  170. I2C_INT_FLAG_PECERR = I2C_REGIDX_BIT2(I2C_CTL1_REG_OFFSET, 8U, I2C_STAT0_REG_OFFSET, 12U), /*!< PEC error when receiving data interrupt flag */
  171. I2C_INT_FLAG_SMBTO = I2C_REGIDX_BIT2(I2C_CTL1_REG_OFFSET, 8U, I2C_STAT0_REG_OFFSET, 14U), /*!< timeout signal in SMBus mode interrupt flag */
  172. I2C_INT_FLAG_SMBALT = I2C_REGIDX_BIT2(I2C_CTL1_REG_OFFSET, 8U, I2C_STAT0_REG_OFFSET, 15U), /*!< SMBus Alert status interrupt flag */
  173. }i2c_interrupt_flag_enum;
  174. /* I2C interrupt enable or disable */
  175. typedef enum
  176. {
  177. /* interrupt in CTL1 register */
  178. I2C_INT_ERR = I2C_REGIDX_BIT(I2C_CTL1_REG_OFFSET, 8U), /*!< error interrupt enable */
  179. I2C_INT_EV = I2C_REGIDX_BIT(I2C_CTL1_REG_OFFSET, 9U), /*!< event interrupt enable */
  180. I2C_INT_BUF = I2C_REGIDX_BIT(I2C_CTL1_REG_OFFSET, 10U), /*!< buffer interrupt enable */
  181. }i2c_interrupt_enum;
  182. /* SMBus/I2C mode switch and SMBus type selection */
  183. #define I2C_I2CMODE_ENABLE ((uint32_t)0x00000000U) /*!< I2C mode */
  184. #define I2C_SMBUSMODE_ENABLE I2C_CTL0_SMBEN /*!< SMBus mode */
  185. /* SMBus/I2C mode switch and SMBus type selection */
  186. #define I2C_SMBUS_DEVICE ((uint32_t)0x00000000U) /*!< SMBus mode device type */
  187. #define I2C_SMBUS_HOST I2C_CTL0_SMBSEL /*!< SMBus mode host type */
  188. /* I2C transfer direction */
  189. #define I2C_RECEIVER ((uint32_t)0x00000001U) /*!< receiver */
  190. #define I2C_TRANSMITTER ((uint32_t)0xFFFFFFFEU) /*!< transmitter */
  191. /* whether or not to send an ACK */
  192. #define I2C_ACK_DISABLE ((uint32_t)0x00000000U) /*!< ACK will be not sent */
  193. #define I2C_ACK_ENABLE I2C_CTL0_ACKEN /*!< ACK will be sent */
  194. /* I2C POAP position*/
  195. #define I2C_ACKPOS_CURRENT ((uint32_t)0x00000000U) /*!< ACKEN bit decides whether or not to send ACK or not for the current byte */
  196. #define I2C_ACKPOS_NEXT I2C_CTL0_POAP /*!< ACKEN bit decides whether or not to send ACK for the next byte */
  197. /* I2C dual-address mode switch */
  198. #define I2C_DUADEN_DISABLE ((uint32_t)0x00000000U) /*!< dual-address mode disabled */
  199. #define I2C_DUADEN_ENABLE ((uint32_t)0x00000001U) /*!< dual-address mode enabled */
  200. /* whether or not to stretch SCL low */
  201. #define I2C_SCLSTRETCH_ENABLE ((uint32_t)0x00000000U) /*!< SCL stretching is enabled */
  202. #define I2C_SCLSTRETCH_DISABLE I2C_CTL0_SS /*!< SCL stretching is disabled */
  203. /* whether or not to response to a general call */
  204. #define I2C_GCEN_ENABLE I2C_CTL0_GCEN /*!< slave will response to a general call */
  205. #define I2C_GCEN_DISABLE ((uint32_t)0x00000000U) /*!< slave will not response to a general call */
  206. /* software reset I2C */
  207. #define I2C_SRESET_SET I2C_CTL0_SRESET /*!< I2C is under reset */
  208. #define I2C_SRESET_RESET ((uint32_t)0x00000000U) /*!< I2C is not under reset */
  209. /* I2C DMA mode configure */
  210. /* DMA mode switch */
  211. #define I2C_DMA_ON I2C_CTL1_DMAON /*!< DMA mode enabled */
  212. #define I2C_DMA_OFF ((uint32_t)0x00000000U) /*!< DMA mode disabled */
  213. /* flag indicating DMA last transfer */
  214. #define I2C_DMALST_ON I2C_CTL1_DMALST /*!< next DMA EOT is the last transfer */
  215. #define I2C_DMALST_OFF ((uint32_t)0x00000000U) /*!< next DMA EOT is not the last transfer */
  216. /* I2C PEC configure */
  217. /* PEC enable */
  218. #define I2C_PEC_ENABLE I2C_CTL0_PECEN /*!< PEC calculation on */
  219. #define I2C_PEC_DISABLE ((uint32_t)0x00000000U) /*!< PEC calculation off */
  220. /* PEC transfer */
  221. #define I2C_PECTRANS_ENABLE I2C_CTL0_PECTRANS /*!< transfer PEC */
  222. #define I2C_PECTRANS_DISABLE ((uint32_t)0x00000000U) /*!< not transfer PEC value */
  223. /* I2C SMBus configure */
  224. /* issue or not alert through SMBA pin */
  225. #define I2C_SALTSEND_ENABLE I2C_CTL0_SALT /*!< issue alert through SMBA pin */
  226. #define I2C_SALTSEND_DISABLE ((uint32_t)0x00000000U) /*!< not issue alert through SMBA */
  227. /* ARP protocol in SMBus switch */
  228. #define I2C_ARP_ENABLE I2C_CTL0_ARPEN /*!< ARP enable */
  229. #define I2C_ARP_DISABLE ((uint32_t)0x00000000U) /*!< ARP disable */
  230. /* fast mode plus enable */
  231. #define I2C_FAST_MODE_PLUS_ENABLE I2C_FMPCFG_FMPEN /*!< fast mode plus enable */
  232. #define I2C_FAST_MODE_PLUS_DISABLE ((uint32_t)0x00000000U) /*!< fast mode plus disable */
  233. /* transmit I2C data */
  234. #define DATA_TRANS(regval) (BITS(0,7) & ((uint32_t)(regval) << 0))
  235. /* receive I2C data */
  236. #define DATA_RECV(regval) GET_BITS((uint32_t)(regval), 0, 7)
  237. /* I2C duty cycle in fast mode or fast mode plus */
  238. #define I2C_DTCY_2 ((uint32_t)0x00000000U) /*!< in I2C fast mode or fast mode plus Tlow/Thigh = 2 */
  239. #define I2C_DTCY_16_9 I2C_CKCFG_DTCY /*!< in I2C fast mode or fast mode plus Tlow/Thigh = 16/9 */
  240. /* address mode for the I2C slave */
  241. #define I2C_ADDFORMAT_7BITS ((uint32_t)0x00000000U) /*!< address:7 bits */
  242. #define I2C_ADDFORMAT_10BITS I2C_SADDR0_ADDFORMAT /*!< address:10 bits */
  243. /* function declarations */
  244. /* initialization functions */
  245. /* reset I2C */
  246. void i2c_deinit(uint32_t i2c_periph);
  247. /* configure I2C clock */
  248. void i2c_clock_config(uint32_t i2c_periph, uint32_t clkspeed, uint32_t dutycyc);
  249. /* configure I2C address */
  250. void i2c_mode_addr_config(uint32_t i2c_periph, uint32_t mode, uint32_t addformat, uint32_t addr);
  251. /* application function declarations */
  252. /* select SMBus type */
  253. void i2c_smbus_type_config(uint32_t i2c_periph, uint32_t type);
  254. /* whether or not to send an ACK */
  255. void i2c_ack_config(uint32_t i2c_periph, uint32_t ack);
  256. /* configure I2C POAP position */
  257. void i2c_ackpos_config(uint32_t i2c_periph, uint32_t pos);
  258. /* master sends slave address */
  259. void i2c_master_addressing(uint32_t i2c_periph, uint32_t addr, uint32_t trandirection);
  260. /* enable dual-address mode */
  261. void i2c_dualaddr_enable(uint32_t i2c_periph, uint32_t dualaddr);
  262. /* disable dual-address mode */
  263. void i2c_dualaddr_disable(uint32_t i2c_periph);
  264. /* enable I2C */
  265. void i2c_enable(uint32_t i2c_periph);
  266. /* disable I2C */
  267. void i2c_disable(uint32_t i2c_periph);
  268. /* generate a START condition on I2C bus */
  269. void i2c_start_on_bus(uint32_t i2c_periph);
  270. /* generate a STOP condition on I2C bus */
  271. void i2c_stop_on_bus(uint32_t i2c_periph);
  272. /* I2C transmit data function */
  273. void i2c_data_transmit(uint32_t i2c_periph, uint8_t data);
  274. /* I2C receive data function */
  275. uint8_t i2c_data_receive(uint32_t i2c_periph);
  276. /* configure I2C DMA mode */
  277. void i2c_dma_config(uint32_t i2c_periph, uint32_t dmastate);
  278. /* configure whether next DMA EOT is DMA last transfer or not */
  279. void i2c_dma_last_transfer_config(uint32_t i2c_periph, uint32_t dmalast);
  280. /* whether to stretch SCL low when data is not ready in slave mode */
  281. void i2c_stretch_scl_low_config(uint32_t i2c_periph, uint32_t stretchpara);
  282. /* whether or not to response to a general call */
  283. void i2c_slave_response_to_gcall_config(uint32_t i2c_periph, uint32_t gcallpara);
  284. /* configure software reset of I2C */
  285. void i2c_software_reset_config(uint32_t i2c_periph, uint32_t sreset);
  286. /* configure I2C PEC calculation */
  287. void i2c_pec_config(uint32_t i2c_periph, uint32_t pecstate);
  288. /* configure whether to transfer PEC value */
  289. void i2c_pec_transfer_config(uint32_t i2c_periph, uint32_t pecpara);
  290. /* get packet error checking value */
  291. uint8_t i2c_pec_value_get(uint32_t i2c_periph);
  292. /* configure I2C alert through SMBA pin */
  293. void i2c_smbus_alert_config(uint32_t i2c_periph, uint32_t smbuspara);
  294. /* configure I2C ARP protocol in SMBus */
  295. void i2c_smbus_arp_config(uint32_t i2c_periph, uint32_t arpstate);
  296. /* interrupt & flag functions */
  297. /* get I2C flag status */
  298. FlagStatus i2c_flag_get(uint32_t i2c_periph, i2c_flag_enum flag);
  299. /* clear I2C flag status */
  300. void i2c_flag_clear(uint32_t i2c_periph, i2c_flag_enum flag);
  301. /* enable I2C interrupt */
  302. void i2c_interrupt_enable(uint32_t i2c_periph, i2c_interrupt_enum interrupt);
  303. /* disable I2C interrupt */
  304. void i2c_interrupt_disable(uint32_t i2c_periph, i2c_interrupt_enum interrupt);
  305. /* get I2C interrupt flag status */
  306. FlagStatus i2c_interrupt_flag_get(uint32_t i2c_periph, i2c_interrupt_flag_enum int_flag);
  307. /* clear I2C interrupt flag status */
  308. void i2c_interrupt_flag_clear(uint32_t i2c_periph, i2c_interrupt_flag_enum int_flag);
  309. #endif /* GD32F30X_I2C_H */