1 /*!
2     \file    gd32f4xx_i2c.h
3     \brief   definitions for the I2C
4 
5     \version 2016-08-15, V1.0.0, firmware for GD32F4xx
6     \version 2018-12-12, V2.0.0, firmware for GD32F4xx
7     \version 2019-04-16, V2.0.1, firmware for GD32F4xx
8     \version 2020-09-30, V2.1.0, firmware for GD32F4xx
9     \version 2022-03-09, V3.0.0, firmware for GD32F4xx
10 */
11 
12 /*
13     Copyright (c) 2022, GigaDevice Semiconductor Inc.
14 
15     Redistribution and use in source and binary forms, with or without modification,
16 are permitted provided that the following conditions are met:
17 
18     1. Redistributions of source code must retain the above copyright notice, this
19        list of conditions and the following disclaimer.
20     2. Redistributions in binary form must reproduce the above copyright notice,
21        this list of conditions and the following disclaimer in the documentation
22        and/or other materials provided with the distribution.
23     3. Neither the name of the copyright holder nor the names of its contributors
24        may be used to endorse or promote products derived from this software without
25        specific prior written permission.
26 
27     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
29 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
31 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
34 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
36 OF SUCH DAMAGE.
37 */
38 
39 #ifndef GD32F4XX_I2C_H
40 #define GD32F4XX_I2C_H
41 
42 #include "gd32f4xx.h"
43 
44 /* I2Cx(x=0,1,2) definitions */
45 #define I2C0                          I2C_BASE                          /*!< I2C0 base address */
46 #define I2C1                          (I2C_BASE + 0x00000400U)          /*!< I2C1 base address */
47 #define I2C2                          (I2C_BASE + 0x00000800U)          /*!< I2C2 base address */
48 
49 /* registers definitions */
50 #define I2C_CTL0(i2cx)                REG32((i2cx) + 0x00000000U)       /*!< I2C control register 0 */
51 #define I2C_CTL1(i2cx)                REG32((i2cx) + 0x00000004U)       /*!< I2C control register 1 */
52 #define I2C_SADDR0(i2cx)              REG32((i2cx) + 0x00000008U)       /*!< I2C slave address register 0 */
53 #define I2C_SADDR1(i2cx)              REG32((i2cx) + 0x0000000CU)       /*!< I2C slave address register 1 */
54 #define I2C_DATA(i2cx)                REG32((i2cx) + 0x00000010U)       /*!< I2C transfer buffer register */
55 #define I2C_STAT0(i2cx)               REG32((i2cx) + 0x00000014U)       /*!< I2C transfer status register 0 */
56 #define I2C_STAT1(i2cx)               REG32((i2cx) + 0x00000018U)       /*!< I2C transfer status register */
57 #define I2C_CKCFG(i2cx)               REG32((i2cx) + 0x0000001CU)       /*!< I2C clock configure register */
58 #define I2C_RT(i2cx)                  REG32((i2cx) + 0x00000020U)       /*!< I2C rise time register */
59 #define I2C_FCTL(i2cx)                REG32((i2cx) + 0x00000024U)       /*!< I2C filter control register */
60 #define I2C_SAMCS(i2cx)               REG32((i2cx) + 0x00000080U)       /*!< I2C SAM control and status register */
61 
62 /* bits definitions */
63 /* I2Cx_CTL0 */
64 #define I2C_CTL0_I2CEN                BIT(0)                            /*!< peripheral enable */
65 #define I2C_CTL0_SMBEN                BIT(1)                            /*!< SMBus mode */
66 #define I2C_CTL0_SMBSEL               BIT(3)                            /*!< SMBus type */
67 #define I2C_CTL0_ARPEN                BIT(4)                            /*!< ARP enable */
68 #define I2C_CTL0_PECEN                BIT(5)                            /*!< PEC enable */
69 #define I2C_CTL0_GCEN                 BIT(6)                            /*!< general call enable */
70 #define I2C_CTL0_SS                   BIT(7)                            /*!< clock stretching disable (slave mode) */
71 #define I2C_CTL0_START                BIT(8)                            /*!< start generation */
72 #define I2C_CTL0_STOP                 BIT(9)                            /*!< stop generation */
73 #define I2C_CTL0_ACKEN                BIT(10)                           /*!< acknowledge enable */
74 #define I2C_CTL0_POAP                 BIT(11)                           /*!< acknowledge/PEC position (for data reception) */
75 #define I2C_CTL0_PECTRANS             BIT(12)                           /*!< packet error checking */
76 #define I2C_CTL0_SALT                 BIT(13)                           /*!< SMBus alert */
77 #define I2C_CTL0_SRESET               BIT(15)                           /*!< software reset */
78 
79 /* I2Cx_CTL1 */
80 #define I2C_CTL1_I2CCLK               BITS(0,5)                         /*!< I2CCLK[5:0] bits (peripheral clock frequency) */
81 #define I2C_CTL1_ERRIE                BIT(8)                            /*!< error interrupt enable */
82 #define I2C_CTL1_EVIE                 BIT(9)                            /*!< event interrupt enable */
83 #define I2C_CTL1_BUFIE                BIT(10)                           /*!< buffer interrupt enable */
84 #define I2C_CTL1_DMAON                BIT(11)                           /*!< DMA requests enable */
85 #define I2C_CTL1_DMALST               BIT(12)                           /*!< DMA last transfer */
86 
87 /* I2Cx_SADDR0 */
88 #define I2C_SADDR0_ADDRESS0           BIT(0)                            /*!< bit 0 of a 10-bit address */
89 #define I2C_SADDR0_ADDRESS            BITS(1,7)                         /*!< 7-bit address or bits 7:1 of a 10-bit address */
90 #define I2C_SADDR0_ADDRESS_H          BITS(8,9)                         /*!< highest two bits of a 10-bit address */
91 #define I2C_SADDR0_ADDFORMAT          BIT(15)                           /*!< address mode for the I2C slave */
92 
93 /* I2Cx_SADDR1 */
94 #define I2C_SADDR1_DUADEN             BIT(0)                            /*!< aual-address mode switch */
95 #define I2C_SADDR1_ADDRESS2           BITS(1,7)                         /*!< second I2C address for the slave in dual-address mode */
96 
97 /* I2Cx_DATA */
98 #define I2C_DATA_TRB                  BITS(0,7)                         /*!< 8-bit data register */
99 
100 /* I2Cx_STAT0 */
101 #define I2C_STAT0_SBSEND              BIT(0)                            /*!< start bit (master mode) */
102 #define I2C_STAT0_ADDSEND             BIT(1)                            /*!< address sent (master mode)/matched (slave mode) */
103 #define I2C_STAT0_BTC                 BIT(2)                            /*!< byte transfer finished */
104 #define I2C_STAT0_ADD10SEND           BIT(3)                            /*!< 10-bit header sent (master mode) */
105 #define I2C_STAT0_STPDET              BIT(4)                            /*!< stop detection (slave mode) */
106 #define I2C_STAT0_RBNE                BIT(6)                            /*!< data register not empty (receivers) */
107 #define I2C_STAT0_TBE                 BIT(7)                            /*!< data register empty (transmitters) */
108 #define I2C_STAT0_BERR                BIT(8)                            /*!< bus error */
109 #define I2C_STAT0_LOSTARB             BIT(9)                            /*!< arbitration lost (master mode) */
110 #define I2C_STAT0_AERR                BIT(10)                           /*!< acknowledge failure */
111 #define I2C_STAT0_OUERR               BIT(11)                           /*!< overrun/underrun */
112 #define I2C_STAT0_PECERR              BIT(12)                           /*!< PEC error in reception */
113 #define I2C_STAT0_SMBTO               BIT(14)                           /*!< timeout signal in SMBus mode */
114 #define I2C_STAT0_SMBALT              BIT(15)                           /*!< SMBus alert status */
115 
116 /* I2Cx_STAT1 */
117 #define I2C_STAT1_MASTER              BIT(0)                            /*!< master/slave */
118 #define I2C_STAT1_I2CBSY              BIT(1)                            /*!< bus busy */
119 #define I2C_STAT1_TR                  BIT(2)                            /*!< transmitter/receiver */
120 #define I2C_STAT1_RXGC                BIT(4)                            /*!< general call address (slave mode) */
121 #define I2C_STAT1_DEFSMB              BIT(5)                            /*!< SMBus device default address (slave mode) */
122 #define I2C_STAT1_HSTSMB              BIT(6)                            /*!< SMBus host header (slave mode) */
123 #define I2C_STAT1_DUMODF              BIT(7)                            /*!< dual flag (slave mode) */
124 #define I2C_STAT1_PECV                BITS(8,15)                        /*!< packet error checking value */
125 
126 /* I2Cx_CKCFG */
127 #define I2C_CKCFG_CLKC                BITS(0,11)                        /*!< clock control register in fast/standard mode (master mode) */
128 #define I2C_CKCFG_DTCY                BIT(14)                           /*!< fast mode duty cycle */
129 #define I2C_CKCFG_FAST                BIT(15)                           /*!< I2C speed selection in master mode */
130 
131 /* I2Cx_RT */
132 #define I2C_RT_RISETIME               BITS(0,5)                         /*!< maximum rise time in fast/standard mode (Master mode) */
133 
134 /* I2Cx_FCTL */
135 #define I2C_FCTL_DF                   BITS(0,3)                         /*!< digital noise filter */
136 #define I2C_FCTL_AFD                  BIT(4)                            /*!< analog noise filter disable */
137 
138 /* I2Cx_SAMCS */
139 #define I2C_SAMCS_SAMEN               BIT(0)                            /*!< SAM_V interface enable */
140 #define I2C_SAMCS_STOEN               BIT(1)                            /*!< SAM_V interface timeout detect enable */
141 #define I2C_SAMCS_TFFIE               BIT(4)                            /*!< txframe fall interrupt enable */
142 #define I2C_SAMCS_TFRIE               BIT(5)                            /*!< txframe rise interrupt enable */
143 #define I2C_SAMCS_RFFIE               BIT(6)                            /*!< rxframe fall interrupt enable */
144 #define I2C_SAMCS_RFRIE               BIT(7)                            /*!< rxframe rise interrupt enable */
145 #define I2C_SAMCS_TXF                 BIT(8)                            /*!< level of txframe signal */
146 #define I2C_SAMCS_RXF                 BIT(9)                            /*!< level of rxframe signal */
147 #define I2C_SAMCS_TFF                 BIT(12)                           /*!< txframe fall flag, cleared by software write 0 */
148 #define I2C_SAMCS_TFR                 BIT(13)                           /*!< txframe rise flag, cleared by software write 0 */
149 #define I2C_SAMCS_RFF                 BIT(14)                           /*!< rxframe fall flag, cleared by software write 0 */
150 #define I2C_SAMCS_RFR                 BIT(15)                           /*!< rxframe rise flag, cleared by software write 0 */
151 
152 /* constants definitions */
153 /* define the I2C bit position and its register index offset */
154 #define I2C_REGIDX_BIT(regidx, bitpos)  (((uint32_t)(regidx) << 6) | (uint32_t)(bitpos))
155 #define I2C_REG_VAL(i2cx, offset)       (REG32((i2cx) + (((uint32_t)(offset) & 0x0000FFFFU) >> 6)))
156 #define I2C_BIT_POS(val)                ((uint32_t)(val) & 0x0000001FU)
157 #define I2C_REGIDX_BIT2(regidx, bitpos, regidx2, bitpos2)   (((uint32_t)(regidx2) << 22) | (uint32_t)((bitpos2) << 16)\
158                                                             | (((uint32_t)(regidx) << 6) | (uint32_t)(bitpos)))
159 #define I2C_REG_VAL2(i2cx, offset)      (REG32((i2cx) + ((uint32_t)(offset) >> 22)))
160 #define I2C_BIT_POS2(val)               (((uint32_t)(val) & 0x001F0000U) >> 16)
161 
162 /* register offset */
163 #define I2C_CTL1_REG_OFFSET           ((uint32_t)0x00000004U)           /*!< CTL1 register offset */
164 #define I2C_STAT0_REG_OFFSET          ((uint32_t)0x00000014U)           /*!< STAT0 register offset */
165 #define I2C_STAT1_REG_OFFSET          ((uint32_t)0x00000018U)           /*!< STAT1 register offset */
166 #define I2C_SAMCS_REG_OFFSET          ((uint32_t)0x00000080U)           /*!< SAMCS register offset */
167 
168 /* I2C flags */
169 typedef enum {
170     /* flags in STAT0 register */
171     I2C_FLAG_SBSEND = I2C_REGIDX_BIT(I2C_STAT0_REG_OFFSET, 0U),         /*!< start condition sent out in master mode */
172     I2C_FLAG_ADDSEND = I2C_REGIDX_BIT(I2C_STAT0_REG_OFFSET, 1U),        /*!< address is sent in master mode or received and matches in slave mode */
173     I2C_FLAG_BTC = I2C_REGIDX_BIT(I2C_STAT0_REG_OFFSET, 2U),            /*!< byte transmission finishes */
174     I2C_FLAG_ADD10SEND = I2C_REGIDX_BIT(I2C_STAT0_REG_OFFSET, 3U),      /*!< header of 10-bit address is sent in master mode */
175     I2C_FLAG_STPDET = I2C_REGIDX_BIT(I2C_STAT0_REG_OFFSET, 4U),         /*!< stop condition detected in slave mode */
176     I2C_FLAG_RBNE = I2C_REGIDX_BIT(I2C_STAT0_REG_OFFSET, 6U),           /*!< I2C_DATA is not empty during receiving */
177     I2C_FLAG_TBE = I2C_REGIDX_BIT(I2C_STAT0_REG_OFFSET, 7U),            /*!< I2C_DATA is empty during transmitting */
178     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 */
179     I2C_FLAG_LOSTARB = I2C_REGIDX_BIT(I2C_STAT0_REG_OFFSET, 9U),        /*!< arbitration lost in master mode */
180     I2C_FLAG_AERR = I2C_REGIDX_BIT(I2C_STAT0_REG_OFFSET, 10U),          /*!< acknowledge error */
181     I2C_FLAG_OUERR = I2C_REGIDX_BIT(I2C_STAT0_REG_OFFSET, 11U),         /*!< over-run or under-run situation occurs in slave mode */
182     I2C_FLAG_PECERR = I2C_REGIDX_BIT(I2C_STAT0_REG_OFFSET, 12U),        /*!< PEC error when receiving data */
183     I2C_FLAG_SMBTO = I2C_REGIDX_BIT(I2C_STAT0_REG_OFFSET, 14U),         /*!< timeout signal in SMBus mode */
184     I2C_FLAG_SMBALT = I2C_REGIDX_BIT(I2C_STAT0_REG_OFFSET, 15U),        /*!< SMBus alert status */
185     /* flags in STAT1 register */
186     I2C_FLAG_MASTER = I2C_REGIDX_BIT(I2C_STAT1_REG_OFFSET, 0U),         /*!< a flag indicating whether I2C block is in master or slave mode */
187     I2C_FLAG_I2CBSY = I2C_REGIDX_BIT(I2C_STAT1_REG_OFFSET, 1U),         /*!< busy flag */
188     I2C_FLAG_TR = I2C_REGIDX_BIT(I2C_STAT1_REG_OFFSET, 2U),             /*!< whether the I2C is a transmitter or a receiver */
189     I2C_FLAG_RXGC = I2C_REGIDX_BIT(I2C_STAT1_REG_OFFSET, 4U),           /*!< general call address (00h) received */
190     I2C_FLAG_DEFSMB = I2C_REGIDX_BIT(I2C_STAT1_REG_OFFSET, 5U),         /*!< default address of SMBus device */
191     I2C_FLAG_HSTSMB = I2C_REGIDX_BIT(I2C_STAT1_REG_OFFSET, 6U),         /*!< SMBus host header detected in slave mode */
192     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 */
193     /* flags in SAMCS register */
194     I2C_FLAG_TFF = I2C_REGIDX_BIT(I2C_SAMCS_REG_OFFSET, 12U),           /*!< txframe fall flag */
195     I2C_FLAG_TFR = I2C_REGIDX_BIT(I2C_SAMCS_REG_OFFSET, 13U),           /*!< txframe rise flag */
196     I2C_FLAG_RFF = I2C_REGIDX_BIT(I2C_SAMCS_REG_OFFSET, 14U),           /*!< rxframe fall flag */
197     I2C_FLAG_RFR = I2C_REGIDX_BIT(I2C_SAMCS_REG_OFFSET, 15U)            /*!< rxframe rise flag */
198 } i2c_flag_enum;
199 
200 /* I2C interrupt flags */
201 typedef enum {
202     /* interrupt flags in CTL1 register */
203     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 */
204     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 */
205     I2C_INT_FLAG_BTC =  I2C_REGIDX_BIT2(I2C_CTL1_REG_OFFSET, 9U, I2C_STAT0_REG_OFFSET, 2U),          /*!< byte transmission finishes interrupt flag */
206     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 */
207     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 */
208     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 */
209     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 */
210     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 */
211     I2C_INT_FLAG_LOSTARB = I2C_REGIDX_BIT2(I2C_CTL1_REG_OFFSET, 8U, I2C_STAT0_REG_OFFSET, 9U),       /*!< arbitration lost in master mode interrupt flag */
212     I2C_INT_FLAG_AERR = I2C_REGIDX_BIT2(I2C_CTL1_REG_OFFSET, 8U, I2C_STAT0_REG_OFFSET, 10U),         /*!< acknowledge error interrupt flag */
213     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 */
214     I2C_INT_FLAG_PECERR = I2C_REGIDX_BIT2(I2C_CTL1_REG_OFFSET, 8U, I2C_STAT0_REG_OFFSET, 12U),       /*!< PEC error when receiving data interrupt flag */
215     I2C_INT_FLAG_SMBTO = I2C_REGIDX_BIT2(I2C_CTL1_REG_OFFSET, 8U, I2C_STAT0_REG_OFFSET, 14U),        /*!< timeout signal in SMBus mode interrupt flag */
216     I2C_INT_FLAG_SMBALT = I2C_REGIDX_BIT2(I2C_CTL1_REG_OFFSET, 8U, I2C_STAT0_REG_OFFSET, 15U),       /*!< SMBus alert status interrupt flag */
217     /* interrupt flags in SAMCS register */
218     I2C_INT_FLAG_TFF = I2C_REGIDX_BIT2(I2C_SAMCS_REG_OFFSET, 4U, I2C_SAMCS_REG_OFFSET, 12U),         /*!< txframe fall interrupt flag */
219     I2C_INT_FLAG_TFR = I2C_REGIDX_BIT2(I2C_SAMCS_REG_OFFSET, 5U, I2C_SAMCS_REG_OFFSET, 13U),         /*!< txframe rise interrupt flag */
220     I2C_INT_FLAG_RFF = I2C_REGIDX_BIT2(I2C_SAMCS_REG_OFFSET, 6U, I2C_SAMCS_REG_OFFSET, 14U),         /*!< rxframe fall interrupt flag */
221     I2C_INT_FLAG_RFR = I2C_REGIDX_BIT2(I2C_SAMCS_REG_OFFSET, 7U, I2C_SAMCS_REG_OFFSET, 15U)          /*!< rxframe rise interrupt flag */
222 } i2c_interrupt_flag_enum;
223 
224 /* I2C interrupt */
225 typedef enum {
226     /* interrupt in CTL1 register */
227     I2C_INT_ERR = I2C_REGIDX_BIT(I2C_CTL1_REG_OFFSET, 8U),              /*!< error interrupt */
228     I2C_INT_EV = I2C_REGIDX_BIT(I2C_CTL1_REG_OFFSET, 9U),               /*!< event interrupt */
229     I2C_INT_BUF = I2C_REGIDX_BIT(I2C_CTL1_REG_OFFSET, 10U),             /*!< buffer interrupt */
230     /* interrupt in SAMCS register */
231     I2C_INT_TFF = I2C_REGIDX_BIT(I2C_SAMCS_REG_OFFSET, 4U),             /*!< txframe fall interrupt */
232     I2C_INT_TFR = I2C_REGIDX_BIT(I2C_SAMCS_REG_OFFSET, 5U),             /*!< txframe rise interrupt */
233     I2C_INT_RFF = I2C_REGIDX_BIT(I2C_SAMCS_REG_OFFSET, 6U),             /*!< rxframe fall interrupt */
234     I2C_INT_RFR = I2C_REGIDX_BIT(I2C_SAMCS_REG_OFFSET, 7U)              /*!< rxframe rise interrupt */
235 } i2c_interrupt_enum;
236 
237 /* the digital noise filter can filter spikes's length */
238 typedef enum {
239     I2C_DF_DISABLE = 0,                                                 /*!< disable digital noise filter */
240     I2C_DF_1PCLK,                                                       /*!< enable digital noise filter and the maximum filtered spiker's length 1 PCLK1 */
241     I2C_DF_2PCLKS,                                                      /*!< enable digital noise filter and the maximum filtered spiker's length 2 PCLK1 */
242     I2C_DF_3PCLKS,                                                      /*!< enable digital noise filter and the maximum filtered spiker's length 3 PCLK1 */
243     I2C_DF_4PCLKS,                                                      /*!< enable digital noise filter and the maximum filtered spiker's length 4 PCLK1 */
244     I2C_DF_5PCLKS,                                                      /*!< enable digital noise filter and the maximum filtered spiker's length 5 PCLK1 */
245     I2C_DF_6PCLKS,                                                      /*!< enable digital noise filter and the maximum filtered spiker's length 6 PCLK1 */
246     I2C_DF_7PCLKS,                                                      /*!< enable digital noise filter and the maximum filtered spiker's length 7 PCLK1 */
247     I2C_DF_8PCLKS,                                                      /*!< enable digital noise filter and the maximum filtered spiker's length 8 PCLK1 */
248     I2C_DF_9PCLKS,                                                      /*!< enable digital noise filter and the maximum filtered spiker's length 9 PCLK1 */
249     I2C_DF_10PCLKS,                                                     /*!< enable digital noise filter and the maximum filtered spiker's length 10 PCLK1 */
250     I2C_DF_11PCLKS,                                                     /*!< enable digital noise filter and the maximum filtered spiker's length 11 PCLK1 */
251     I2C_DF_12PCLKS,                                                     /*!< enable digital noise filter and the maximum filtered spiker's length 12 PCLK1 */
252     I2C_DF_13PCLKS,                                                     /*!< enable digital noise filter and the maximum filtered spiker's length 13 PCLK1 */
253     I2C_DF_14PCLKS,                                                     /*!< enable digital noise filter and the maximum filtered spiker's length 14 PCLK1 */
254     I2C_DF_15PCLKS                                                      /*!< enable digital noise filter and the maximum filtered spiker's length 15 PCLK1 */
255 } i2c_digital_filter_enum;
256 
257 /* SMBus/I2C mode switch and SMBus type selection */
258 #define I2C_I2CMODE_ENABLE            ((uint32_t)0x00000000U)           /*!< I2C mode */
259 #define I2C_SMBUSMODE_ENABLE          I2C_CTL0_SMBEN                    /*!< SMBus mode */
260 
261 /* SMBus/I2C mode switch and SMBus type selection */
262 #define I2C_SMBUS_DEVICE              ((uint32_t)0x00000000U)           /*!< SMBus mode device type */
263 #define I2C_SMBUS_HOST                I2C_CTL0_SMBSEL                   /*!< SMBus mode host type */
264 
265 /* I2C transfer direction */
266 #define I2C_RECEIVER                  ((uint32_t)0x00000001U)           /*!< receiver */
267 #define I2C_TRANSMITTER               ((uint32_t)0xFFFFFFFEU)           /*!< transmitter */
268 
269 /* whether or not to send an ACK */
270 #define I2C_ACK_DISABLE               ((uint32_t)0x00000000U)           /*!< ACK will be not sent */
271 #define I2C_ACK_ENABLE                I2C_CTL0_ACKEN                    /*!< ACK will be sent */
272 
273 /* I2C POAP position*/
274 #define I2C_ACKPOS_CURRENT            ((uint32_t)0x00000000U)           /*!< ACKEN bit decides whether or not to send ACK or not for the current byte */
275 #define I2C_ACKPOS_NEXT               I2C_CTL0_POAP                     /*!< ACKEN bit decides whether or not to send ACK for the next byte */
276 
277 /* whether or not to stretch SCL low */
278 #define I2C_SCLSTRETCH_ENABLE         ((uint32_t)0x00000000U)           /*!< enable SCL stretching */
279 #define I2C_SCLSTRETCH_DISABLE        I2C_CTL0_SS                       /*!< disable SCL stretching */
280 
281 /* whether or not to response to a general call */
282 #define I2C_GCEN_ENABLE               I2C_CTL0_GCEN                     /*!< slave will response to a general call */
283 #define I2C_GCEN_DISABLE              ((uint32_t)0x00000000U)           /*!< slave will not response to a general call */
284 
285 /* software reset I2C */
286 #define I2C_SRESET_RESET              ((uint32_t)0x00000000U)           /*!< I2C is not under reset */
287 #define I2C_SRESET_SET                I2C_CTL0_SRESET                   /*!< I2C is under reset */
288 
289 /* I2C DMA mode configure */
290 /* DMA mode switch */
291 #define I2C_DMA_OFF                   ((uint32_t)0x00000000U)           /*!< disable DMA mode */
292 #define I2C_DMA_ON                    I2C_CTL1_DMAON                    /*!< enable DMA mode */
293 
294 /* flag indicating DMA last transfer */
295 #define I2C_DMALST_OFF                ((uint32_t)0x00000000U)           /*!< next DMA EOT is not the last transfer */
296 #define I2C_DMALST_ON                 I2C_CTL1_DMALST                   /*!< next DMA EOT is the last transfer */
297 
298 /* I2C PEC configure */
299 /* PEC enable */
300 #define I2C_PEC_DISABLE              ((uint32_t)0x00000000U)            /*!< PEC calculation off */
301 #define I2C_PEC_ENABLE                I2C_CTL0_PECEN                    /*!< PEC calculation on */
302 
303 /* PEC transfer */
304 #define I2C_PECTRANS_DISABLE          ((uint32_t)0x00000000U)           /*!< not transfer PEC value */
305 #define I2C_PECTRANS_ENABLE           I2C_CTL0_PECTRANS                 /*!< transfer PEC value */
306 
307 /* I2C SMBus configure */
308 /* issue or not alert through SMBA pin */
309 #define I2C_SALTSEND_DISABLE          ((uint32_t)0x00000000U)           /*!< not issue alert through SMBA */
310 #define I2C_SALTSEND_ENABLE           I2C_CTL0_SALT                     /*!< issue alert through SMBA pin */
311 
312 /* ARP protocol in SMBus switch */
313 #define I2C_ARP_DISABLE               ((uint32_t)0x00000000U)           /*!< disable ARP */
314 #define I2C_ARP_ENABLE                I2C_CTL0_ARPEN                    /*!< enable ARP */
315 
316 /* transmit I2C data */
317 #define DATA_TRANS(regval)            (BITS(0,7) & ((uint32_t)(regval) << 0))
318 
319 /* receive I2C data */
320 #define DATA_RECV(regval)             GET_BITS((uint32_t)(regval), 0, 7)
321 
322 /* I2C duty cycle in fast mode */
323 #define I2C_DTCY_2                    ((uint32_t)0x00000000U)           /*!< T_low/T_high = 2 in fast mode */
324 #define I2C_DTCY_16_9                 I2C_CKCFG_DTCY                    /*!< T_low/T_high = 16/9 in fast mode */
325 
326 /* address mode for the I2C slave */
327 #define I2C_ADDFORMAT_7BITS           ((uint32_t)0x00000000U)           /*!< address format is 7 bits */
328 #define I2C_ADDFORMAT_10BITS          I2C_SADDR0_ADDFORMAT              /*!< address format is 10 bits */
329 
330 /* I2C clock frequency, MHz */
331 #define I2CCLK_MAX                    ((uint32_t)0x0000003CU)           /*!< i2cclk maximum value */
332 #define I2CCLK_MIN                    ((uint32_t)0x00000002U)           /*!< i2cclk minimum value */
333 #define I2CCLK_FM_MIN                 ((uint32_t)0x00000008U)           /*!< i2cclk minimum value for fast mode */
334 #define I2CCLK_FM_PLUS_MIN            ((uint32_t)0x00000018U)           /*!< i2cclk minimum value for fast mode plus */
335 
336 /* function declarations */
337 /* initialization functions */
338 /* reset I2C */
339 void i2c_deinit(uint32_t i2c_periph);
340 /* configure I2C clock */
341 void i2c_clock_config(uint32_t i2c_periph, uint32_t clkspeed, uint32_t dutycyc);
342 /* configure I2C address */
343 void i2c_mode_addr_config(uint32_t i2c_periph, uint32_t mode, uint32_t addformat, uint32_t addr);
344 
345 /* application function declarations */
346 /* select SMBus type */
347 void i2c_smbus_type_config(uint32_t i2c_periph, uint32_t type);
348 /* whether or not to send an ACK */
349 void i2c_ack_config(uint32_t i2c_periph, uint32_t ack);
350 /* configure I2C POAP position */
351 void i2c_ackpos_config(uint32_t i2c_periph, uint32_t pos);
352 /* master sends slave address */
353 void i2c_master_addressing(uint32_t i2c_periph, uint32_t addr, uint32_t trandirection);
354 /* enable dual-address mode */
355 void i2c_dualaddr_enable(uint32_t i2c_periph, uint32_t addr);
356 /* disable dual-address mode */
357 void i2c_dualaddr_disable(uint32_t i2c_periph);
358 /* enable I2C */
359 void i2c_enable(uint32_t i2c_periph);
360 /* disable I2C */
361 void i2c_disable(uint32_t i2c_periph);
362 /* generate a START condition on I2C bus */
363 void i2c_start_on_bus(uint32_t i2c_periph);
364 /* generate a STOP condition on I2C bus */
365 void i2c_stop_on_bus(uint32_t i2c_periph);
366 /* I2C transmit data function */
367 void i2c_data_transmit(uint32_t i2c_periph, uint8_t data);
368 /* I2C receive data function */
369 uint8_t i2c_data_receive(uint32_t i2c_periph);
370 /* configure I2C DMA mode */
371 void i2c_dma_config(uint32_t i2c_periph, uint32_t dmastate);
372 /* configure whether next DMA EOT is DMA last transfer or not */
373 void i2c_dma_last_transfer_config(uint32_t i2c_periph, uint32_t dmalast);
374 /* whether to stretch SCL low when data is not ready in slave mode */
375 void i2c_stretch_scl_low_config(uint32_t i2c_periph, uint32_t stretchpara);
376 /* whether or not to response to a general call */
377 void i2c_slave_response_to_gcall_config(uint32_t i2c_periph, uint32_t gcallpara);
378 /* configure software reset of I2C */
379 void i2c_software_reset_config(uint32_t i2c_periph, uint32_t sreset);
380 /* configure I2C PEC calculation */
381 void i2c_pec_config(uint32_t i2c_periph, uint32_t pecstate);
382 /* configure whether to transfer PEC value */
383 void i2c_pec_transfer_config(uint32_t i2c_periph, uint32_t pecpara);
384 /* get packet error checking value */
385 uint8_t i2c_pec_value_get(uint32_t i2c_periph);
386 /* configure I2C alert through SMBA pin */
387 void i2c_smbus_alert_config(uint32_t i2c_periph, uint32_t smbuspara);
388 /* configure I2C ARP protocol in SMBus */
389 void i2c_smbus_arp_config(uint32_t i2c_periph, uint32_t arpstate);
390 /* disable analog noise filter */
391 void i2c_analog_noise_filter_disable(uint32_t i2c_periph);
392 /* enable analog noise filter */
393 void i2c_analog_noise_filter_enable(uint32_t i2c_periph);
394 /* configure digital noise filter */
395 void i2c_digital_noise_filter_config(uint32_t i2c_periph, i2c_digital_filter_enum dfilterpara);
396 /* enable SAM_V interface */
397 void i2c_sam_enable(uint32_t i2c_periph);
398 /* disable SAM_V interface */
399 void i2c_sam_disable(uint32_t i2c_periph);
400 /* enable SAM_V interface timeout detect */
401 void i2c_sam_timeout_enable(uint32_t i2c_periph);
402 /* disable SAM_V interface timeout detect */
403 void i2c_sam_timeout_disable(uint32_t i2c_periph);
404 
405 /* interrupt & flag functions */
406 /* get I2C flag status */
407 FlagStatus i2c_flag_get(uint32_t i2c_periph, i2c_flag_enum flag);
408 /* clear I2C flag status */
409 void i2c_flag_clear(uint32_t i2c_periph, i2c_flag_enum flag);
410 /* enable I2C interrupt */
411 void i2c_interrupt_enable(uint32_t i2c_periph, i2c_interrupt_enum interrupt);
412 /* disable I2C interrupt */
413 void i2c_interrupt_disable(uint32_t i2c_periph, i2c_interrupt_enum interrupt);
414 /* get I2C interrupt flag status */
415 FlagStatus i2c_interrupt_flag_get(uint32_t i2c_periph, i2c_interrupt_flag_enum int_flag);
416 /* clear I2C interrupt flag status */
417 void i2c_interrupt_flag_clear(uint32_t i2c_periph, i2c_interrupt_flag_enum int_flag);
418 
419 #endif /* GD32F4XX_I2C_H */
420