1 /*!
2     \file    gd32a50x_i2c.h
3     \brief   definitions for the I2C
4 
5     \version 2022-01-30, V1.0.0, firmware for GD32A50x
6 */
7 
8 /*
9     Copyright (c) 2022, GigaDevice Semiconductor Inc.
10 
11     Redistribution and use in source and binary forms, with or without modification,
12 are permitted provided that the following conditions are met:
13 
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 
23     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32 OF SUCH DAMAGE.
33 */
34 
35 #ifndef GD32A50X_I2C_H
36 #define GD32A50X_I2C_H
37 
38 #include "gd32a50x.h"
39 
40 /* I2Cx(x=0,1) definitions */
41 #define I2C0                            I2C_BASE                        /*!< I2C0 base address */
42 #define I2C1                            (I2C_BASE+0x00000400U)          /*!< I2C1 base address */
43 
44 /* registers definitions */
45 #define I2C_CTL0(i2cx)                  REG32((i2cx) + 0x00000000U)     /*!< I2C control register 0 */
46 #define I2C_CTL1(i2cx)                  REG32((i2cx) + 0x00000004U)     /*!< I2C control register 1 */
47 #define I2C_SADDR0(i2cx)                REG32((i2cx) + 0x00000008U)     /*!< I2C slave address register 0*/
48 #define I2C_SADDR1(i2cx)                REG32((i2cx) + 0x0000000CU)     /*!< I2C slave address register 1*/
49 #define I2C_TIMING(i2cx)                REG32((i2cx) + 0x00000010U)     /*!< I2C timing register */
50 #define I2C_TIMEOUT(i2cx)               REG32((i2cx) + 0x00000014U)     /*!< I2C timeout register */
51 #define I2C_STAT(i2cx)                  REG32((i2cx) + 0x00000018U)     /*!< I2C status register */
52 #define I2C_STATC(i2cx)                 REG32((i2cx) + 0x0000001CU)     /*!< I2C status clear register */
53 #define I2C_PEC(i2cx)                   REG32((i2cx) + 0x00000020U)     /*!< I2C PEC register */
54 #define I2C_RDATA(i2cx)                 REG32((i2cx) + 0x00000024U)     /*!< I2C receive data register */
55 #define I2C_TDATA(i2cx)                 REG32((i2cx) + 0x00000028U)     /*!< I2C transmit data register */
56 #define I2C_CTL2(i2cx)                  REG32((i2cx) + 0x00000090U)     /*!< I2C control register 2 */
57 
58 /* bits definitions */
59 /* I2Cx_CTL0 */
60 #define I2C_CTL0_I2CEN                  BIT(0)                          /*!< I2C peripheral enable */
61 #define I2C_CTL0_TIE                    BIT(1)                          /*!< transmit interrupt enable */
62 #define I2C_CTL0_RBNEIE                 BIT(2)                          /*!< receive interrupt enable */
63 #define I2C_CTL0_ADDMIE                 BIT(3)                          /*!< address match interrupt enable in slave mode */
64 #define I2C_CTL0_NACKIE                 BIT(4)                          /*!< not acknowledge received interrupt enable */
65 #define I2C_CTL0_STPDETIE               BIT(5)                          /*!< stop detection interrupt enable */
66 #define I2C_CTL0_TCIE                   BIT(6)                          /*!< transfer complete interrupt enable */
67 #define I2C_CTL0_ERRIE                  BIT(7)                          /*!< error interrupt enable */
68 #define I2C_CTL0_DNF                    BITS(8,11)                      /*!< digital noise filter */
69 #define I2C_CTL0_ANOFF                  BIT(12)                         /*!< analog noise filter */
70 #define I2C_CTL0_DENT                   BIT(14)                         /*!< DMA enable for transmission */
71 #define I2C_CTL0_DENR                   BIT(15)                         /*!< DMA enable for reception */
72 #define I2C_CTL0_SBCTL                  BIT(16)                         /*!< slave byte control */
73 #define I2C_CTL0_SS                     BIT(17)                         /*!< whether to stretch SCL low when data is not ready in slave mode */
74 #define I2C_CTL0_GCEN                   BIT(19)                         /*!< whether or not to response to a general call (0x00) */
75 #define I2C_CTL0_SMBHAEN                BIT(20)                         /*!< SMBus host address enable */
76 #define I2C_CTL0_SMBDAEN                BIT(21)                         /*!< SMBus device default address enable */
77 #define I2C_CTL0_SMBALTEN               BIT(22)                         /*!< SMBus alert enable */
78 #define I2C_CTL0_PECEN                  BIT(23)                         /*!< PEC calculation switch */
79 
80 /* I2Cx_CTL1 */
81 #define I2C_CTL1_SADDRESS               BITS(0,9)                       /*!< received slave address */
82 #define I2C_CTL1_TRDIR                  BIT(10)                         /*!< transfer direction in master mode */
83 #define I2C_CTL1_ADD10EN                BIT(11)                         /*!< 10-bit addressing mode enable in master mode */
84 #define I2C_CTL1_HEAD10R                BIT(12)                         /*!< 10-bit address header executes read direction only in master receive mode */
85 #define I2C_CTL1_START                  BIT(13)                         /*!< generate a START condition on I2C bus */
86 #define I2C_CTL1_STOP                   BIT(14)                         /*!< generate a STOP condition on I2C bus */
87 #define I2C_CTL1_NACKEN                 BIT(15)                         /*!< generate NACK in slave mode */
88 #define I2C_CTL1_BYTENUM                BITS(16,23)                     /*!< number of bytes to be transferred */
89 #define I2C_CTL1_RELOAD                 BIT(24)                         /*!< reload mode enable */
90 #define I2C_CTL1_AUTOEND                BIT(25)                         /*!< automatic end mode in master mode */
91 #define I2C_CTL1_PECTRANS               BIT(26)                         /*!< PEC transfer */
92 
93 /* I2Cx_SADDR0 */
94 #define I2C_SADDR0_ADDRESS0             BIT(0)                          /*!< bit 0 of a 10-bit address */
95 #define I2C_SADDR0_ADDRESS              BITS(1,7)                       /*!< 7-bit address or bits 7:1 of a 10-bit address */
96 #define I2C_SADDR0_ADDRESS_H            BITS(8,9)                       /*!< highest two bits of a 10-bit address */
97 #define I2C_SADDR0_ADDFORMAT            BIT(10)                         /*!< address mode for the I2C slave */
98 #define I2C_SADDR0_ADDRESSEN            BIT(15)                         /*!< I2C address enable */
99 
100 /* I2Cx_SADDR1 */
101 #define I2C_SADDR1_ADDRESS2             BITS(1,7)                       /*!< second I2C address for the slave */
102 #define I2C_SADDR1_ADDMSK2              BITS(8,10)                      /*!< ADDRESS2[7:1] mask */
103 #define I2C_SADDR1_ADDRESS2EN           BIT(15)                         /*!< second I2C address enable */
104 
105 /* I2Cx_TIMING */
106 #define I2C_TIMING_SCLL                 BITS(0,7)                       /*!< SCL low period */
107 #define I2C_TIMING_SCLH                 BITS(8,15)                      /*!< SCL high period */
108 #define I2C_TIMING_SDADELY              BITS(16,19)                     /*!< data hold time */
109 #define I2C_TIMING_SCLDELY              BITS(20,23)                     /*!< data setup time */
110 #define I2C_TIMING_PSC                  BITS(28,31)                     /*!< timing prescaler */
111 
112 /* I2Cx_TIMEOUT */
113 #define I2C_TIMEOUT_BUSTOA              BITS(0,11)                      /*!< bus timeout A */
114 #define I2C_TIMEOUT_TOIDLE              BIT(12)                         /*!< idle clock timeout detection */
115 #define I2C_TIMEOUT_TOEN                BIT(15)                         /*!< clock timeout detection enable */
116 #define I2C_TIMEOUT_BUSTOB              BITS(16,27)                     /*!< bus timeout B */
117 #define I2C_TIMEOUT_EXTOEN              BIT(31)                         /*!< extended clock timeout detection enable */
118 
119 /* I2Cx_STAT */
120 #define I2C_STAT_TBE                    BIT(0)                          /*!< I2C_TDATA is empty during transmitting */
121 #define I2C_STAT_TI                     BIT(1)                          /*!< transmit interrupt */
122 #define I2C_STAT_RBNE                   BIT(2)                          /*!< I2C_RDATA is not empty during receiving */
123 #define I2C_STAT_ADDSEND                BIT(3)                          /*!< address received matches in slave mode */
124 #define I2C_STAT_NACK                   BIT(4)                          /*!< not acknowledge flag */
125 #define I2C_STAT_STPDET                 BIT(5)                          /*!< STOP condition detected in slave mode */
126 #define I2C_STAT_TC                     BIT(6)                          /*!< transfer complete in master mode */
127 #define I2C_STAT_TCR                    BIT(7)                          /*!< transfer complete reload */
128 #define I2C_STAT_BERR                   BIT(8)                          /*!< bus error */
129 #define I2C_STAT_LOSTARB                BIT(9)                          /*!< arbitration lost */
130 #define I2C_STAT_OUERR                  BIT(10)                         /*!< overrun/underrun error in slave mode */
131 #define I2C_STAT_PECERR                 BIT(11)                         /*!< PEC error */
132 #define I2C_STAT_TIMEOUT                BIT(12)                         /*!< timeout flag */
133 #define I2C_STAT_SMBALT                 BIT(13)                         /*!< SMBus Alert */
134 #define I2C_STAT_I2CBSY                 BIT(15)                         /*!< busy flag */
135 #define I2C_STAT_TR                     BIT(16)                         /*!< whether the I2C is a transmitter or a receiver in slave mode */
136 #define I2C_STAT_READDR                 BITS(17,23)                     /*!< received match address in slave mode */
137 
138 /* I2Cx_STATC */
139 #define I2C_STATC_ADDSENDC              BIT(3)                          /*!< ADDSEND flag clear */
140 #define I2C_STATC_NACKC                 BIT(4)                          /*!< not acknowledge flag clear */
141 #define I2C_STATC_STPDETC               BIT(5)                          /*!< STPDET flag clear */
142 #define I2C_STATC_BERRC                 BIT(8)                          /*!< bus error flag clear */
143 #define I2C_STATC_LOSTARBC              BIT(9)                          /*!< arbitration Lost flag clear */
144 #define I2C_STATC_OUERRC                BIT(10)                         /*!< overrun/underrun flag clear */
145 #define I2C_STATC_PECERRC               BIT(11)                         /*!< PEC error flag clear */
146 #define I2C_STATC_TIMEOUTC              BIT(12)                         /*!< TIMEOUT flag clear */
147 #define I2C_STATC_SMBALTC               BIT(13)                         /*!< SMBus Alert flag clear */
148 
149 /* I2Cx_PEC */
150 #define I2C_PEC_PECV                    BITS(0,7)                       /*!< Packet Error Checking Value that calculated by hardware when PEC is enabled */
151 
152 /* I2Cx_RDATA */
153 #define I2C_RDATA_RDATA                 BITS(0,7)                       /*!< receive data value */
154 
155 /* I2Cx_TDATA */
156 #define I2C_TDATA_TDATA                 BITS(0,7)                       /*!< transmit data value */
157 
158 /* I2Cx_CTL2 */
159 #define I2C_CTL2_ADDM                   BITS(9,15)                      /*!< address bit compare select */
160 
161 /* constants definitions */
162 /* define the I2C bit position and its register index offset */
163 #define I2C_REGIDX_BIT(regidx, bitpos)  (((uint32_t)(regidx) << 6) | (uint32_t)(bitpos))
164 #define I2C_REG_VAL(i2cx, offset)       (REG32((i2cx) + (((uint32_t)(offset) & 0x0000FFFFU) >> 6)))
165 #define I2C_BIT_POS(val)                ((uint32_t)(val) & 0x0000001FU)
166 #define I2C_REGIDX_BIT2(regidx, bitpos, regidx2, bitpos2)   (((uint32_t)(regidx2) << 22) | (uint32_t)((bitpos2) << 16)\
167                                                               | (((uint32_t)(regidx) << 6) | (uint32_t)(bitpos)))
168 #define I2C_REG_VAL2(i2cx, offset)      (REG32((i2cx) + ((uint32_t)(offset) >> 22)))
169 #define I2C_BIT_POS2(val)               (((uint32_t)(val) & 0x001F0000U) >> 16)
170 
171 /* register offset */
172 #define I2C_CTL0_REG_OFFSET             ((uint32_t)0x00000000U)         /*!< CTL0 register offset */
173 #define I2C_STAT_REG_OFFSET             ((uint32_t)0x00000018U)         /*!< STAT register offset */
174 
175 /* I2C interrupt flags */
176 typedef enum {
177     I2C_INT_FLAG_TI = I2C_REGIDX_BIT2(I2C_CTL0_REG_OFFSET, 1U, I2C_STAT_REG_OFFSET, 1U),            /*!< transmit interrupt flag */
178     I2C_INT_FLAG_RBNE = I2C_REGIDX_BIT2(I2C_CTL0_REG_OFFSET, 2U, I2C_STAT_REG_OFFSET, 2U),          /*!< I2C_RDATA is not empty during receiving interrupt flag */
179     I2C_INT_FLAG_ADDSEND = I2C_REGIDX_BIT2(I2C_CTL0_REG_OFFSET, 3U, I2C_STAT_REG_OFFSET, 3U),       /*!< address received matches in slave mode interrupt flag */
180     I2C_INT_FLAG_NACK = I2C_REGIDX_BIT2(I2C_CTL0_REG_OFFSET, 4U, I2C_STAT_REG_OFFSET, 4U),          /*!< not acknowledge interrupt flag */
181     I2C_INT_FLAG_STPDET = I2C_REGIDX_BIT2(I2C_CTL0_REG_OFFSET, 5U, I2C_STAT_REG_OFFSET, 5U),        /*!< stop condition detected in slave mode interrupt flag */
182     I2C_INT_FLAG_TC = I2C_REGIDX_BIT2(I2C_CTL0_REG_OFFSET, 6U, I2C_STAT_REG_OFFSET, 6U),            /*!< transfer complete in master mode interrupt flag */
183     I2C_INT_FLAG_TCR = I2C_REGIDX_BIT2(I2C_CTL0_REG_OFFSET, 6U, I2C_STAT_REG_OFFSET, 7U),           /*!< transfer complete reload interrupt flag */
184     I2C_INT_FLAG_BERR = I2C_REGIDX_BIT2(I2C_CTL0_REG_OFFSET, 7U, I2C_STAT_REG_OFFSET, 8U),          /*!< bus error interrupt flag */
185     I2C_INT_FLAG_LOSTARB = I2C_REGIDX_BIT2(I2C_CTL0_REG_OFFSET, 7U, I2C_STAT_REG_OFFSET, 9U),       /*!< arbitration lost interrupt flag */
186     I2C_INT_FLAG_OUERR = I2C_REGIDX_BIT2(I2C_CTL0_REG_OFFSET, 7U, I2C_STAT_REG_OFFSET, 10U),        /*!< overrun/underrun error in slave mode interrupt flag */
187     I2C_INT_FLAG_PECERR = I2C_REGIDX_BIT2(I2C_CTL0_REG_OFFSET, 7U, I2C_STAT_REG_OFFSET, 11U),       /*!< PEC error interrupt flag */
188     I2C_INT_FLAG_TIMEOUT = I2C_REGIDX_BIT2(I2C_CTL0_REG_OFFSET, 7U, I2C_STAT_REG_OFFSET, 12U),      /*!< timeout interrupt flag */
189     I2C_INT_FLAG_SMBALT = I2C_REGIDX_BIT2(I2C_CTL0_REG_OFFSET, 7U, I2C_STAT_REG_OFFSET, 13U)        /*!< SMBus Alert interrupt flag */
190 } i2c_interrupt_flag_enum;
191 
192 /* I2C DMA constants definitions */
193 #define I2C_DMA_TRANSMIT                ((uint32_t)0x00000000U)         /*!< I2C transmit data use DMA */
194 #define I2C_DMA_RECEIVE                 ((uint32_t)0x00000001U)         /*!< I2C receive data use DMA */
195 
196 /* I2C interrupt enable or disable */
197 #define I2C_INT_ERR                     I2C_CTL0_ERRIE                  /*!< error interrupt enable */
198 #define I2C_INT_TC                      I2C_CTL0_TCIE                   /*!< transfer complete interrupt enable */
199 #define I2C_INT_STPDET                  I2C_CTL0_STPDETIE               /*!< stop detection interrupt enable */
200 #define I2C_INT_NACK                    I2C_CTL0_NACKIE                 /*!< not acknowledge received interrupt enable */
201 #define I2C_INT_ADDM                    I2C_CTL0_ADDMIE                 /*!< address match interrupt enable */
202 #define I2C_INT_RBNE                    I2C_CTL0_RBNEIE                 /*!< receive interrupt enable */
203 #define I2C_INT_TI                      I2C_CTL0_TIE                    /*!< transmit interrupt enable */
204 
205 /* I2C transfer direction in master mode */
206 #define I2C_MASTER_TRANSMIT             ((uint32_t)0x00000000U)         /*!< I2C master transmit */
207 #define I2C_MASTER_RECEIVE              I2C_CTL1_TRDIR                  /*!< I2C master receive */
208 
209 /* address mode for the I2C slave */
210 #define I2C_ADDFORMAT_7BITS             ((uint32_t)0x00000000U)         /*!< address:7 bits */
211 #define I2C_ADDFORMAT_10BITS            I2C_SADDR0_ADDFORMAT            /*!< address:10 bits */
212 
213 /* the length of filter spikes */
214 #define FILTER_DISABLE                  ((uint32_t)0x00000000U)         /*!< digital filter is disabled */
215 #define FILTER_LENGTH_1                 ((uint32_t)0x00000001U)         /*!< digital filter is enabled and filter spikes with a length of up to 1 tI2CCLK */
216 #define FILTER_LENGTH_2                 ((uint32_t)0x00000002U)         /*!< digital filter is enabled and filter spikes with a length of up to 2 tI2CCLK */
217 #define FILTER_LENGTH_3                 ((uint32_t)0x00000003U)         /*!< digital filter is enabled and filter spikes with a length of up to 3 tI2CCLK */
218 #define FILTER_LENGTH_4                 ((uint32_t)0x00000004U)         /*!< digital filter is enabled and filter spikes with a length of up to 4 tI2CCLK */
219 #define FILTER_LENGTH_5                 ((uint32_t)0x00000005U)         /*!< digital filter is enabled and filter spikes with a length of up to 5 tI2CCLK */
220 #define FILTER_LENGTH_6                 ((uint32_t)0x00000006U)         /*!< digital filter is enabled and filter spikes with a length of up to 6 tI2CCLK */
221 #define FILTER_LENGTH_7                 ((uint32_t)0x00000007U)         /*!< digital filter is enabled and filter spikes with a length of up to 7 tI2CCLK */
222 #define FILTER_LENGTH_8                 ((uint32_t)0x00000008U)         /*!< digital filter is enabled and filter spikes with a length of up to 8 tI2CCLK */
223 #define FILTER_LENGTH_9                 ((uint32_t)0x00000009U)         /*!< digital filter is enabled and filter spikes with a length of up to 9 tI2CCLK */
224 #define FILTER_LENGTH_10                ((uint32_t)0x0000000AU)         /*!< digital filter is enabled and filter spikes with a length of up to 10 tI2CCLK */
225 #define FILTER_LENGTH_11                ((uint32_t)0x0000000BU)         /*!< digital filter is enabled and filter spikes with a length of up to 11 tI2CCLK */
226 #define FILTER_LENGTH_12                ((uint32_t)0x0000000CU)         /*!< digital filter is enabled and filter spikes with a length of up to 12 tI2CCLK */
227 #define FILTER_LENGTH_13                ((uint32_t)0x0000000DU)         /*!< digital filter is enabled and filter spikes with a length of up to 13 tI2CCLK */
228 #define FILTER_LENGTH_14                ((uint32_t)0x0000000EU)         /*!< digital filter is enabled and filter spikes with a length of up to 14 tI2CCLK */
229 #define FILTER_LENGTH_15                ((uint32_t)0x0000000FU)         /*!< digital filter is enabled and filter spikes with a length of up to 15 tI2CCLK */
230 
231 /* defines which bits of register ADDRESS[7:1] are compared with an incoming address byte */
232 #define ADDRESS_BIT1_COMPARE            ((uint32_t)0x00000200U)         /*!< address bit1 needs compare */
233 #define ADDRESS_BIT2_COMPARE            ((uint32_t)0x00000400U)         /*!< address bit2 needs compare */
234 #define ADDRESS_BIT3_COMPARE            ((uint32_t)0x00000800U)         /*!< address bit3 needs compare */
235 #define ADDRESS_BIT4_COMPARE            ((uint32_t)0x00001000U)         /*!< address bit4 needs compare */
236 #define ADDRESS_BIT5_COMPARE            ((uint32_t)0x00002000U)         /*!< address bit5 needs compare */
237 #define ADDRESS_BIT6_COMPARE            ((uint32_t)0x00004000U)         /*!< address bit6 needs compare */
238 #define ADDRESS_BIT7_COMPARE            ((uint32_t)0x00008000U)         /*!< address bit7 needs compare */
239 
240 /* defines which bits of ADDRESS2[7:1] are compared with an incoming address byte, and which bits are masked (don't care) */
241 #define ADDRESS2_NO_MASK                ((uint32_t)0x00000000U)         /*!< no mask, all the bits must be compared */
242 #define ADDRESS2_MASK_BIT1              ((uint32_t)0x00000001U)         /*!< ADDRESS2[1] is masked, only ADDRESS2[7:2] are compared */
243 #define ADDRESS2_MASK_BIT1_2            ((uint32_t)0x00000002U)         /*!< ADDRESS2[2:1] is masked, only ADDRESS2[7:3] are compared */
244 #define ADDRESS2_MASK_BIT1_3            ((uint32_t)0x00000003U)         /*!< ADDRESS2[3:1] is masked, only ADDRESS2[7:4] are compared */
245 #define ADDRESS2_MASK_BIT1_4            ((uint32_t)0x00000004U)         /*!< ADDRESS2[4:1] is masked, only ADDRESS2[7:5] are compared */
246 #define ADDRESS2_MASK_BIT1_5            ((uint32_t)0x00000005U)         /*!< ADDRESS2[5:1] is masked, only ADDRESS2[7:6] are compared */
247 #define ADDRESS2_MASK_BIT1_6            ((uint32_t)0x00000006U)         /*!< ADDRESS2[6:1] is masked, only ADDRESS2[7] are compared */
248 #define ADDRESS2_MASK_ALL               ((uint32_t)0x00000007U)         /*!< all the ADDRESS2[7:1] bits are masked */
249 
250 /* idle clock timeout detection */
251 #define BUSTOA_DETECT_SCL_LOW           ((uint32_t)0x00000000U)         /*!< BUSTOA is used to detect SCL low timeout */
252 #define BUSTOA_DETECT_IDLE              I2C_TIMEOUT_TOIDLE              /*!< BUSTOA is used to detect both SCL and SDA high timeout when the bus is idle */
253 
254 /* I2C flag definitions */
255 #define I2C_FLAG_TBE                    I2C_STAT_TBE                    /*!< I2C_TDATA is empty during transmitting */
256 #define I2C_FLAG_TI                     I2C_STAT_TI                     /*!< transmit interrupt */
257 #define I2C_FLAG_RBNE                   I2C_STAT_RBNE                   /*!< I2C_RDATA is not empty during receiving */
258 #define I2C_FLAG_ADDSEND                I2C_STAT_ADDSEND                /*!< address received matches in slave mode */
259 #define I2C_FLAG_NACK                   I2C_STAT_NACK                   /*!< not acknowledge flag */
260 #define I2C_FLAG_STPDET                 I2C_STAT_STPDET                 /*!< STOP condition detected in slave mode */
261 #define I2C_FLAG_TC                     I2C_STAT_TC                     /*!< transfer complete in master mode */
262 #define I2C_FLAG_TCR                    I2C_STAT_TCR                    /*!< transfer complete reload */
263 #define I2C_FLAG_BERR                   I2C_STAT_BERR                   /*!< bus error */
264 #define I2C_FLAG_LOSTARB                I2C_STAT_LOSTARB                /*!< arbitration lost */
265 #define I2C_FLAG_OUERR                  I2C_STAT_OUERR                  /*!< overrun/underrun error in slave mode */
266 #define I2C_FLAG_PECERR                 I2C_STAT_PECERR                 /*!< PEC error */
267 #define I2C_FLAG_TIMEOUT                I2C_STAT_TIMEOUT                /*!< timeout flag */
268 #define I2C_FLAG_SMBALT                 I2C_STAT_SMBALT                 /*!< SMBus Alert */
269 #define I2C_FLAG_I2CBSY                 I2C_STAT_I2CBSY                 /*!< busy flag */
270 #define I2C_FLAG_TR                     I2C_STAT_TR                     /*!< whether the I2C is a transmitter or a receiver in slave mode */
271 
272 #define I2CCLK_MAX ((uint32_t)0x0000003FU)/*!< i2cclk maximum value */
273 #define I2CCLK_MIN ((uint32_t)0x00000002U)/*!< i2cclk minimum value for standard mode */
274 #define I2CCLK_FM_MIN ((uint32_t)0x00000008U)/*!< i2cclk minimum value for fast mode */
275 #define I2CCLK_FM_PLUS_MIN ((uint32_t)0x00000018U)/*!< i2cclk minimum value for fast mode plus */
276 
277 /* function declarations */
278 /* initialization functions */
279 /* reset I2C */
280 void i2c_deinit(uint32_t i2c_periph);
281 /* configure the timing parameters */
282 void i2c_timing_config(uint32_t i2c_periph, uint32_t psc, uint32_t scl_dely, uint32_t sda_dely);
283 /* configure digital noise filter */
284 void i2c_digital_noise_filter_config(uint32_t i2c_periph, uint32_t filter_length);
285 /* enable analog noise filter */
286 void i2c_analog_noise_filter_enable(uint32_t i2c_periph);
287 /* disable analog noise filter */
288 void i2c_analog_noise_filter_disable(uint32_t i2c_periph);
289 /* configure the SCL high and low period of clock in master mode */
290 void i2c_master_clock_config(uint32_t i2c_periph, uint32_t sclh, uint32_t scll);
291 /* configure i2c slave address and transfer direction in master mode */
292 void i2c_master_addressing(uint32_t i2c_periph, uint32_t address, uint32_t trans_direction);
293 
294 /* application function declarations */
295 /* 10-bit address header executes read direction only in master receive mode */
296 void i2c_address10_header_enable(uint32_t i2c_periph);
297 /* 10-bit address header executes complete sequence in master receive mode */
298 void i2c_address10_header_disable(uint32_t i2c_periph);
299 /* enable 10-bit addressing mode in master mode */
300 void i2c_address10_enable(uint32_t i2c_periph);
301 /* disable 10-bit addressing mode in master mode */
302 void i2c_address10_disable(uint32_t i2c_periph);
303 /* enable I2C automatic end mode in master mode */
304 void i2c_automatic_end_enable(uint32_t i2c_periph);
305 /* disable I2C automatic end mode in master mode */
306 void i2c_automatic_end_disable(uint32_t i2c_periph);
307 /* enable the response to a general call */
308 void i2c_slave_response_to_gcall_enable(uint32_t i2c_periph);
309 /* disable the response to a general call */
310 void i2c_slave_response_to_gcall_disable(uint32_t i2c_periph);
311 /* enable to stretch SCL low when data is not ready in slave mode */
312 void i2c_stretch_scl_low_enable(uint32_t i2c_periph);
313 /* disable to stretch SCL low when data is not ready in slave mode */
314 void i2c_stretch_scl_low_disable(uint32_t i2c_periph);
315 /* configure i2c slave address */
316 void i2c_address_config(uint32_t i2c_periph, uint32_t address, uint32_t addr_format);
317 /* define which bits of ADDRESS[7:1] need to compare with the incoming address byte */
318 void i2c_address_bit_compare_config(uint32_t i2c_periph, uint32_t compare_bits);
319 /* disable i2c address in slave mode */
320 void i2c_address_disable(uint32_t i2c_periph);
321 /* configure i2c second slave address */
322 void i2c_second_address_config(uint32_t i2c_periph, uint32_t address, uint32_t addr_mask);
323 /* disable i2c second address in slave mode */
324 void i2c_second_address_disable(uint32_t i2c_periph);
325 /* get received match address in slave mode */
326 uint32_t i2c_recevied_address_get(uint32_t i2c_periph);
327 /* enable slave byte control */
328 void i2c_slave_byte_control_enable(uint32_t i2c_periph);
329 /* disable slave byte control */
330 void i2c_slave_byte_control_disable(uint32_t i2c_periph);
331 /* generate a NACK in slave mode */
332 void i2c_nack_enable(uint32_t i2c_periph);
333 /* generate an ACK in slave mode */
334 void i2c_nack_disable(uint32_t i2c_periph);
335 /* enable I2C */
336 void i2c_enable(uint32_t i2c_periph);
337 /* disable I2C */
338 void i2c_disable(uint32_t i2c_periph);
339 /* generate a START condition on I2C bus */
340 void i2c_start_on_bus(uint32_t i2c_periph);
341 /* generate a STOP condition on I2C bus */
342 void i2c_stop_on_bus(uint32_t i2c_periph);
343 /* I2C transmit data */
344 void i2c_data_transmit(uint32_t i2c_periph, uint32_t data);
345 /* I2C receive data */
346 uint32_t i2c_data_receive(uint32_t i2c_periph);
347 /* enable I2C reload mode */
348 void i2c_reload_enable(uint32_t i2c_periph);
349 /* disable I2C reload mode */
350 void i2c_reload_disable(uint32_t i2c_periph);
351 /* configure number of bytes to be transferred */
352 void i2c_transfer_byte_number_config(uint32_t i2c_periph, uint32_t byte_number);
353 /* enable I2C DMA for transmission or reception */
354 void i2c_dma_enable(uint32_t i2c_periph, uint8_t dma);
355 /* disable I2C DMA for transmission or reception */
356 void i2c_dma_disable(uint32_t i2c_periph, uint8_t dma);
357 /* I2C transfers PEC value */
358 void i2c_pec_transfer(uint32_t i2c_periph);
359 /* enable I2C PEC calculation */
360 void i2c_pec_enable(uint32_t i2c_periph);
361 /* disable I2C PEC calculation */
362 void i2c_pec_disable(uint32_t i2c_periph);
363 /* get packet error checking value */
364 uint32_t i2c_pec_value_get(uint32_t i2c_periph);
365 /* enable SMBus alert */
366 void i2c_smbus_alert_enable(uint32_t i2c_periph);
367 /* disable SMBus alert */
368 void i2c_smbus_alert_disable(uint32_t i2c_periph);
369 /* enable SMBus device default address */
370 void i2c_smbus_default_addr_enable(uint32_t i2c_periph);
371 /* disable SMBus device default address */
372 void i2c_smbus_default_addr_disable(uint32_t i2c_periph);
373 /* enable SMBus host address */
374 void i2c_smbus_host_addr_enable(uint32_t i2c_periph);
375 /* disable SMBus host address */
376 void i2c_smbus_host_addr_disable(uint32_t i2c_periph);
377 /* enable extended clock timeout detection */
378 void i2c_extented_clock_timeout_enable(uint32_t i2c_periph);
379 /* disable extended clock timeout detection */
380 void i2c_extented_clock_timeout_disable(uint32_t i2c_periph);
381 /* enable clock timeout detection */
382 void i2c_clock_timeout_enable(uint32_t i2c_periph);
383 /* disable clock timeout detection */
384 void i2c_clock_timeout_disable(uint32_t i2c_periph);
385 /* configure bus timeout B */
386 void i2c_bus_timeout_b_config(uint32_t i2c_periph, uint32_t timeout);
387 /* configure bus timeout A */
388 void i2c_bus_timeout_a_config(uint32_t i2c_periph, uint32_t timeout);
389 /* configure idle clock timeout detection */
390 void i2c_idle_clock_timeout_config(uint32_t i2c_periph, uint32_t timeout);
391 
392 /* interrupt & flag functions */
393 /* get I2C flag status */
394 FlagStatus i2c_flag_get(uint32_t i2c_periph, uint32_t flag);
395 /* clear I2C flag status */
396 void i2c_flag_clear(uint32_t i2c_periph, uint32_t flag);
397 /* enable I2C interrupt */
398 void i2c_interrupt_enable(uint32_t i2c_periph, uint32_t interrupt);
399 /* disable I2C interrupt */
400 void i2c_interrupt_disable(uint32_t i2c_periph, uint32_t interrupt);
401 /* get I2C interrupt flag status */
402 FlagStatus i2c_interrupt_flag_get(uint32_t i2c_periph, i2c_interrupt_flag_enum int_flag);
403 /* clear I2C interrupt flag status */
404 void i2c_interrupt_flag_clear(uint32_t i2c_periph, i2c_interrupt_flag_enum int_flag);
405 
406 #endif /* GD32A50X_I2C_H */
407