1 /*!
2     \file    gd32f3x0_usart.h
3     \brief   definitions for the USART
4 
5     \version 2017-06-06, V1.0.0, firmware for GD32F3x0
6     \version 2019-06-01, V2.0.0, firmware for GD32F3x0
7     \version 2020-09-30, V2.1.0, firmware for GD32F3x0
8 */
9 
10 /*
11     Copyright (c) 2020, GigaDevice Semiconductor Inc.
12 
13     Redistribution and use in source and binary forms, with or without modification,
14 are permitted provided that the following conditions are met:
15 
16     1. Redistributions of source code must retain the above copyright notice, this
17        list of conditions and the following disclaimer.
18     2. Redistributions in binary form must reproduce the above copyright notice,
19        this list of conditions and the following disclaimer in the documentation
20        and/or other materials provided with the distribution.
21     3. Neither the name of the copyright holder nor the names of its contributors
22        may be used to endorse or promote products derived from this software without
23        specific prior written permission.
24 
25     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
34 OF SUCH DAMAGE.
35 */
36 
37 #ifndef GD32F3X0_USART_H
38 #define GD32F3X0_USART_H
39 
40 #include "gd32f3x0.h"
41 
42 /* USARTx(x=0,1) definitions */
43 #define USART0                        (USART_BASE + 0x0000F400U)
44 #define USART1                        USART_BASE
45 
46 /* registers definitions */
47 #define USART_CTL0(usartx)            REG32((usartx) + 0x00000000U)  /*!< USART control register 0 */
48 #define USART_CTL1(usartx)            REG32((usartx) + 0x00000004U)  /*!< USART control register 1 */
49 #define USART_CTL2(usartx)            REG32((usartx) + 0x00000008U)  /*!< USART control register 2 */
50 #define USART_BAUD(usartx)            REG32((usartx) + 0x0000000CU)  /*!< USART baud rate register */
51 #define USART_GP(usartx)              REG32((usartx) + 0x00000010U)  /*!< USART guard time and prescaler register */
52 #define USART_RT(usartx)              REG32((usartx) + 0x00000014U)  /*!< USART receiver timeout register */
53 #define USART_CMD(usartx)             REG32((usartx) + 0x00000018U)  /*!< USART command register */
54 #define USART_STAT(usartx)            REG32((usartx) + 0x0000001CU)  /*!< USART status register */
55 #define USART_INTC(usartx)            REG32((usartx) + 0x00000020U)  /*!< USART status clear register */
56 #define USART_RDATA(usartx)           REG32((usartx) + 0x00000024U)  /*!< USART receive data register */
57 #define USART_TDATA(usartx)           REG32((usartx) + 0x00000028U)  /*!< USART transmit data register */
58 #define USART_RFCS(usartx)            REG32((usartx) + 0x000000D0U)  /*!< USART receive FIFO control and status register */
59 
60 /* bits definitions */
61 /* USARTx_CTL0 */
62 #define USART_CTL0_UEN                BIT(0)                         /*!< USART enable */
63 #define USART_CTL0_UESM               BIT(1)                         /*!< USART enable in deep-sleep mode */
64 #define USART_CTL0_REN                BIT(2)                         /*!< receiver enable */
65 #define USART_CTL0_TEN                BIT(3)                         /*!< transmitter enable */
66 #define USART_CTL0_IDLEIE             BIT(4)                         /*!< idle line detected interrupt enable */
67 #define USART_CTL0_RBNEIE             BIT(5)                         /*!< read data buffer not empty interrupt and overrun error interrupt enable */
68 #define USART_CTL0_TCIE               BIT(6)                         /*!< transmission complete interrupt enable */
69 #define USART_CTL0_TBEIE              BIT(7)                         /*!< transmitter register empty interrupt enable */
70 #define USART_CTL0_PERRIE             BIT(8)                         /*!< parity error interrupt enable */
71 #define USART_CTL0_PM                 BIT(9)                         /*!< parity mode */
72 #define USART_CTL0_PCEN               BIT(10)                        /*!< parity control enable */
73 #define USART_CTL0_WM                 BIT(11)                        /*!< wakeup method in mute mode */
74 #define USART_CTL0_WL                 BIT(12)                        /*!< word length */
75 #define USART_CTL0_MEN                BIT(13)                        /*!< mute mode enable */
76 #define USART_CTL0_AMIE               BIT(14)                        /*!< address match interrupt enable */
77 #define USART_CTL0_OVSMOD             BIT(15)                        /*!< oversample mode */
78 #define USART_CTL0_DED                BITS(16,20)                    /*!< driver enable deassertion time */
79 #define USART_CTL0_DEA                BITS(21,25)                    /*!< driver enable assertion time */
80 #define USART_CTL0_RTIE               BIT(26)                        /*!< receiver timeout interrupt enable */
81 #define USART_CTL0_EBIE               BIT(27)                        /*!< end of block interrupt enable */
82 
83 /* USARTx_CTL1 */
84 #define USART_CTL1_ADDM               BIT(4)                         /*!< address detection mode */
85 #define USART_CTL1_LBLEN              BIT(5)                         /*!< LIN break frame length */
86 #define USART_CTL1_LBDIE              BIT(6)                         /*!< LIN break detection interrupt enable */
87 #define USART_CTL1_CLEN               BIT(8)                         /*!< last bit clock pulse */
88 #define USART_CTL1_CPH                BIT(9)                         /*!< clock phase */
89 #define USART_CTL1_CPL                BIT(10)                        /*!< clock polarity */
90 #define USART_CTL1_CKEN               BIT(11)                        /*!< ck pin enable */
91 #define USART_CTL1_STB                BITS(12,13)                    /*!< stop bits length */
92 #define USART_CTL1_LMEN               BIT(14)                        /*!< LIN mode enable */
93 #define USART_CTL1_STRP               BIT(15)                        /*!< swap TX/RX pins */
94 #define USART_CTL1_RINV               BIT(16)                        /*!< RX pin level inversion */
95 #define USART_CTL1_TINV               BIT(17)                        /*!< TX pin level inversion */
96 #define USART_CTL1_DINV               BIT(18)                        /*!< data bit level inversion */
97 #define USART_CTL1_MSBF               BIT(19)                        /*!< most significant bit first */
98 #define USART_CTL1_ABDEN              BIT(20)                        /*!< auto baud rate enable */
99 #define USART_CTL1_ABDM               BITS(21,22)                    /*!< auto baud rate mode */
100 #define USART_CTL1_RTEN               BIT(23)                        /*!< receiver timeout enable */
101 #define USART_CTL1_ADDR               BITS(24,31)                    /*!< address of the USART terminal */
102 
103 /* USARTx_CTL2 */
104 #define USART_CTL2_ERRIE              BIT(0)                         /*!< error interrupt enable in multibuffer communication */
105 #define USART_CTL2_IREN               BIT(1)                         /*!< IrDA mode enable */
106 #define USART_CTL2_IRLP               BIT(2)                         /*!< IrDA low-power */
107 #define USART_CTL2_HDEN               BIT(3)                         /*!< half-duplex enable */
108 #define USART_CTL2_NKEN               BIT(4)                         /*!< NACK enable in smartcard mode */
109 #define USART_CTL2_SCEN               BIT(5)                         /*!< smartcard mode enable */
110 #define USART_CTL2_DENR               BIT(6)                         /*!< DMA enable for reception */
111 #define USART_CTL2_DENT               BIT(7)                         /*!< DMA enable for transmission */
112 #define USART_CTL2_RTSEN              BIT(8)                         /*!< RTS enable */
113 #define USART_CTL2_CTSEN              BIT(9)                         /*!< CTS enable */
114 #define USART_CTL2_CTSIE              BIT(10)                        /*!< CTS interrupt enable */
115 #define USART_CTL2_OSB                BIT(11)                        /*!< one sample bit mode */
116 #define USART_CTL2_OVRD               BIT(12)                        /*!< overrun disable */
117 #define USART_CTL2_DDRE               BIT(13)                        /*!< disable DMA on reception error */
118 #define USART_CTL2_DEM                BIT(14)                        /*!< driver enable mode */
119 #define USART_CTL2_DEP                BIT(15)                        /*!< driver enable polarity mode */
120 #define USART_CTL2_SCRTNUM            BITS(17,19)                    /*!< smartcard auto-retry number */
121 #define USART_CTL2_WUM                BITS(20,21)                    /*!< wakeup mode from deep-sleep mode */
122 #define USART_CTL2_WUIE               BIT(22)                        /*!< wakeup from deep-sleep mode interrupt enable */
123 
124 /* USARTx_BAUD */
125 #define USART_BAUD_FRADIV             BITS(0,3)                      /*!< fraction of baud-rate divider */
126 #define USART_BAUD_INTDIV             BITS(4,15)                     /*!< integer of baud-rate divider */
127 
128 /* USARTx_GP */
129 #define USART_GP_PSC                  BITS(0,7)                      /*!< prescaler value for dividing the system clock */
130 #define USART_GP_GUAT                 BITS(8,15)                     /*!< guard time value in smartcard mode */
131 
132 /* USARTx_RT */
133 #define USART_RT_RT                   BITS(0,23)                     /*!< receiver timeout threshold */
134 #define USART_RT_BL                   BITS(24,31)                    /*!< block length */
135 
136 /* USARTx_CMD */
137 #define USART_CMD_ABDCMD              BIT(0)                         /*!< auto baudrate detection command */
138 #define USART_CMD_SBKCMD              BIT(1)                         /*!< send break command */
139 #define USART_CMD_MMCMD               BIT(2)                         /*!< mute mode command */
140 #define USART_CMD_RXFCMD              BIT(3)                         /*!< receive data flush command */
141 #define USART_CMD_TXFCMD              BIT(4)                         /*!< transmit data flush request */
142 
143 /* USARTx_STAT */
144 #define USART_STAT_PERR               BIT(0)                         /*!< parity error flag */
145 #define USART_STAT_FERR               BIT(1)                         /*!< frame error flag */
146 #define USART_STAT_NERR               BIT(2)                         /*!< noise error flag */
147 #define USART_STAT_ORERR              BIT(3)                         /*!< overrun error */
148 #define USART_STAT_IDLEF              BIT(4)                         /*!< idle line detected flag */
149 #define USART_STAT_RBNE               BIT(5)                         /*!< read data buffer not empty */
150 #define USART_STAT_TC                 BIT(6)                         /*!< transmission completed */
151 #define USART_STAT_TBE                BIT(7)                         /*!< transmit data register empty */
152 #define USART_STAT_LBDF               BIT(8)                         /*!< LIN break detected flag */
153 #define USART_STAT_CTSF               BIT(9)                         /*!< CTS change flag */
154 #define USART_STAT_CTS                BIT(10)                        /*!< CTS level */
155 #define USART_STAT_RTF                BIT(11)                        /*!< receiver timeout flag */
156 #define USART_STAT_EBF                BIT(12)                        /*!< end of block flag */
157 #define USART_STAT_ABDE               BIT(14)                        /*!< auto baudrate detection error */
158 #define USART_STAT_ABDF               BIT(15)                        /*!< auto baudrate detection flag */
159 #define USART_STAT_BSY                BIT(16)                        /*!< busy flag */
160 #define USART_STAT_AMF                BIT(17)                        /*!< address match flag */
161 #define USART_STAT_SBF                BIT(18)                        /*!< send break flag */
162 #define USART_STAT_RWU                BIT(19)                        /*!< receiver wakeup from mute mode */
163 #define USART_STAT_WUF                BIT(20)                        /*!< wakeup from deep-sleep mode flag */
164 #define USART_STAT_TEA                BIT(21)                        /*!< transmit enable acknowledge flag */
165 #define USART_STAT_REA                BIT(22)                        /*!< receive enable acknowledge flag */
166 
167 /* USARTx_INTC */
168 #define USART_INTC_PEC                BIT(0)                         /*!< parity error clear */
169 #define USART_INTC_FEC                BIT(1)                         /*!< frame error flag clear */
170 #define USART_INTC_NEC                BIT(2)                         /*!< noise detected clear */
171 #define USART_INTC_OREC               BIT(3)                         /*!< overrun error clear */
172 #define USART_INTC_IDLEC              BIT(4)                         /*!< idle line detected clear */
173 #define USART_INTC_TCC                BIT(6)                         /*!< transmission complete clear */
174 #define USART_INTC_LBDC               BIT(8)                         /*!< LIN break detected clear */
175 #define USART_INTC_CTSC               BIT(9)                         /*!< CTS change clear */
176 #define USART_INTC_RTC                BIT(11)                        /*!< receiver timeout clear */
177 #define USART_INTC_EBC                BIT(12)                        /*!< end of timeout clear */
178 #define USART_INTC_AMC                BIT(17)                        /*!< address match clear */
179 #define USART_INTC_WUC                BIT(20)                        /*!< wakeup from deep-sleep mode clear */
180 
181 /* USARTx_RDATA */
182 #define USART_RDATA_RDATA             BITS(0,8)                      /*!< receive data value */
183 
184 /* USARTx_TDATA */
185 #define USART_TDATA_TDATA             BITS(0,8)                      /*!< transmit data value */
186 
187 /* USARTx_RFCS */
188 #define USART_RFCS_ELNACK             BIT(0)                         /*!< early NACK */
189 #define USART_RFCS_RFEN               BIT(8)                         /*!< receive FIFO enable */
190 #define USART_RFCS_RFFIE              BIT(9)                         /*!< receive FIFO full interrupt enable */
191 #define USART_RFCS_RFE                BIT(10)                        /*!< receive FIFO empty flag */
192 #define USART_RFCS_RFF                BIT(11)                        /*!< receive FIFO full flag */
193 #define USART_RFCS_RFCNT              BITS(12,14)                    /*!< receive FIFO counter number */
194 #define USART_RFCS_RFFINT             BIT(15)                        /*!< receive FIFO full interrupt flag */
195 
196 /* constants definitions */
197 
198 /* define the USART bit position and its register index offset */
199 #define USART_REGIDX_BIT(regidx, bitpos)    (((uint32_t)(regidx) << 6) | (uint32_t)(bitpos))
200 #define USART_REG_VAL(usartx, offset)       (REG32((usartx) + (((uint32_t)(offset) & 0x0000FFFFU) >> 6)))
201 #define USART_BIT_POS(val)                  ((uint32_t)(val) & 0x0000001FU)
202 #define USART_REGIDX_BIT2(regidx, bitpos, regidx2, bitpos2)   (((uint32_t)(regidx2) << 22) | (uint32_t)((bitpos2) << 16)\
203                                                               | (((uint32_t)(regidx) << 6) | (uint32_t)(bitpos)))
204 #define USART_REG_VAL2(usartx, offset)       (REG32((usartx) + ((uint32_t)(offset) >> 22)))
205 #define USART_BIT_POS2(val)                  (((uint32_t)(val) & 0x001F0000U) >> 16)
206 
207 /* register offset */
208 #define USART_CTL0_REG_OFFSET              (0x00000000U)                   /*!< CTL0 register offset */
209 #define USART_CTL1_REG_OFFSET              (0x00000004U)                   /*!< CTL1 register offset */
210 #define USART_CTL2_REG_OFFSET              (0x00000008U)                   /*!< CTL2 register offset */
211 #define USART_STAT_REG_OFFSET              (0x0000001CU)                   /*!< STAT register offset */
212 #define USART_RFCS_REG_OFFSET              (0x000000D0U)                   /*!< RFCS register offset */
213 
214 /* USART flags */
215 typedef enum{
216     /* flags in STAT register */
217     USART_FLAG_REA = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 22U),         /*!< receive enable acknowledge flag */
218     USART_FLAG_TEA = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 21U),         /*!< transmit enable acknowledge flag */
219     USART_FLAG_WU = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 20U),          /*!< wakeup from deep-sleep mode flag */
220     USART_FLAG_RWU = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 19U),         /*!< receiver wakeup from mute mode */
221     USART_FLAG_SB = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 18U),          /*!< send break flag */
222     USART_FLAG_AM = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 17U),          /*!< ADDR match flag */
223     USART_FLAG_BSY = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 16U),         /*!< busy flag */
224     USART_FLAG_ABD = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 15U),         /*!< auto baudrate detection flag */
225     USART_FLAG_ABDE = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 14U),        /*!< auto baudrate detection error */
226     USART_FLAG_EB = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 12U),          /*!< end of block flag */
227     USART_FLAG_RT = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 11U),          /*!< receiver timeout flag */
228     USART_FLAG_CTS = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 10U),         /*!< CTS level */
229     USART_FLAG_CTSF = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 9U),         /*!< CTS change flag */
230     USART_FLAG_LBD = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 8U),          /*!< LIN break detected flag */
231     USART_FLAG_TBE = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 7U),          /*!< transmit data buffer empty */
232     USART_FLAG_TC = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 6U),           /*!< transmission complete */
233     USART_FLAG_RBNE = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 5U),         /*!< read data buffer not empty */
234     USART_FLAG_IDLE = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 4U),         /*!< IDLE line detected flag */
235     USART_FLAG_ORERR = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 3U),        /*!< overrun error */
236     USART_FLAG_NERR = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 2U),         /*!< noise error flag */
237     USART_FLAG_FERR = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 1U),         /*!< frame error flag */
238     USART_FLAG_PERR = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 0U),         /*!< parity error flag */
239     /* flags in RFCS register */
240     USART_FLAG_RFF = USART_REGIDX_BIT(USART_RFCS_REG_OFFSET, 11U),         /*!< receive FIFO full flag */
241     USART_FLAG_RFE = USART_REGIDX_BIT(USART_RFCS_REG_OFFSET, 10U),         /*!< receive FIFO empty flag */
242 }usart_flag_enum;
243 
244 /* USART interrupt flags */
245 typedef enum
246 {
247     /* interrupt flags in CTL0 register */
248     USART_INT_FLAG_EB = USART_REGIDX_BIT2(USART_CTL0_REG_OFFSET, 27U, USART_STAT_REG_OFFSET, 12U),       /*!< end of block interrupt flag */
249     USART_INT_FLAG_RT = USART_REGIDX_BIT2(USART_CTL0_REG_OFFSET, 26U, USART_STAT_REG_OFFSET, 11U),       /*!< receiver timeout interrupt flag */
250     USART_INT_FLAG_AM = USART_REGIDX_BIT2(USART_CTL0_REG_OFFSET, 14U, USART_STAT_REG_OFFSET, 17U),       /*!< address match interrupt flag */
251     USART_INT_FLAG_PERR = USART_REGIDX_BIT2(USART_CTL0_REG_OFFSET, 8U, USART_STAT_REG_OFFSET, 0U),       /*!< parity error interrupt flag */
252     USART_INT_FLAG_TBE = USART_REGIDX_BIT2(USART_CTL0_REG_OFFSET, 7U, USART_STAT_REG_OFFSET, 7U),        /*!< transmitter buffer empty interrupt flag */
253     USART_INT_FLAG_TC = USART_REGIDX_BIT2(USART_CTL0_REG_OFFSET, 6U, USART_STAT_REG_OFFSET, 6U),         /*!< transmission complete interrupt flag */
254     USART_INT_FLAG_RBNE = USART_REGIDX_BIT2(USART_CTL0_REG_OFFSET, 5U, USART_STAT_REG_OFFSET, 5U),       /*!< read data buffer not empty interrupt flag */
255     USART_INT_FLAG_RBNE_ORERR = USART_REGIDX_BIT2(USART_CTL0_REG_OFFSET, 5U, USART_STAT_REG_OFFSET, 3U), /*!< overrun error interrupt flag */
256     USART_INT_FLAG_IDLE = USART_REGIDX_BIT2(USART_CTL0_REG_OFFSET, 4U, USART_STAT_REG_OFFSET, 4U),       /*!< IDLE line detected interrupt flag */
257     /* interrupt flags in CTL1 register */
258     USART_INT_FLAG_LBD = USART_REGIDX_BIT2(USART_CTL1_REG_OFFSET, 6U, USART_STAT_REG_OFFSET, 8U),        /*!< LIN break detected interrupt flag */
259     /* interrupt flags in CTL2 register */
260     USART_INT_FLAG_WU = USART_REGIDX_BIT2(USART_CTL2_REG_OFFSET, 22U, USART_STAT_REG_OFFSET, 20U),       /*!< wakeup from deep-sleep mode interrupt flag */
261     USART_INT_FLAG_CTS = USART_REGIDX_BIT2(USART_CTL2_REG_OFFSET, 10U, USART_STAT_REG_OFFSET, 9U),       /*!< CTS interrupt flag */
262     USART_INT_FLAG_ERR_NERR = USART_REGIDX_BIT2(USART_CTL2_REG_OFFSET, 0U, USART_STAT_REG_OFFSET, 2U),   /*!< noise error interrupt flag */
263     USART_INT_FLAG_ERR_ORERR = USART_REGIDX_BIT2(USART_CTL2_REG_OFFSET, 0U, USART_STAT_REG_OFFSET, 3U),  /*!< overrun error interrupt flag */
264     USART_INT_FLAG_ERR_FERR = USART_REGIDX_BIT2(USART_CTL2_REG_OFFSET, 0U, USART_STAT_REG_OFFSET, 1U),   /*!< frame error interrupt flag */
265     /* interrupt flags in RFCS register */
266     USART_INT_FLAG_RFFINT = USART_REGIDX_BIT2(USART_RFCS_REG_OFFSET, 9U, USART_RFCS_REG_OFFSET, 15U),    /*!< receive FIFO full interrupt flag */
267 }usart_interrupt_flag_enum;
268 
269 /* USART interrupt enable or disable */
270 typedef enum
271 {
272     /* interrupt in CTL0 register */
273     USART_INT_EB = USART_REGIDX_BIT(USART_CTL0_REG_OFFSET, 27U),     /*!< end of block interrupt */
274     USART_INT_RT = USART_REGIDX_BIT(USART_CTL0_REG_OFFSET, 26U),     /*!< receiver timeout interrupt */
275     USART_INT_AM = USART_REGIDX_BIT(USART_CTL0_REG_OFFSET, 14U),     /*!< address match interrupt */
276     USART_INT_PERR = USART_REGIDX_BIT(USART_CTL0_REG_OFFSET, 8U),    /*!< parity error interrupt */
277     USART_INT_TBE = USART_REGIDX_BIT(USART_CTL0_REG_OFFSET, 7U),     /*!< transmitter buffer empty interrupt */
278     USART_INT_TC = USART_REGIDX_BIT(USART_CTL0_REG_OFFSET, 6U),      /*!< transmission complete interrupt */
279     USART_INT_RBNE = USART_REGIDX_BIT(USART_CTL0_REG_OFFSET, 5U),    /*!< read data buffer not empty interrupt and overrun error interrupt */
280     USART_INT_IDLE = USART_REGIDX_BIT(USART_CTL0_REG_OFFSET, 4U),    /*!< IDLE line detected interrupt */
281     /* interrupt in CTL1 register */
282     USART_INT_LBD = USART_REGIDX_BIT(USART_CTL1_REG_OFFSET, 6U),     /*!< LIN break detected interrupt */
283     /* interrupt in CTL2 register */
284     USART_INT_WU = USART_REGIDX_BIT(USART_CTL2_REG_OFFSET, 22U),     /*!< wakeup from deep-sleep mode interrupt */
285     USART_INT_CTS = USART_REGIDX_BIT(USART_CTL2_REG_OFFSET, 10U),    /*!< CTS interrupt */
286     USART_INT_ERR = USART_REGIDX_BIT(USART_CTL2_REG_OFFSET, 0U),     /*!< error interrupt */
287     /* interrupt in RFCS register */
288     USART_INT_RFF = USART_REGIDX_BIT(USART_RFCS_REG_OFFSET, 9U),     /*!< receive FIFO full interrupt */
289 }usart_interrupt_enum;
290 
291 /* USART invert configure */
292 typedef enum {
293     /* data bit level inversion */
294     USART_DINV_ENABLE,                                               /*!< data bit level inversion */
295     USART_DINV_DISABLE,                                              /*!< data bit level not inversion */
296     /* TX pin level inversion */
297     USART_TXPIN_ENABLE,                                              /*!< TX pin level inversion */
298     USART_TXPIN_DISABLE,                                             /*!< TX pin level not inversion */
299     /* RX pin level inversion */
300     USART_RXPIN_ENABLE,                                              /*!< RX pin level inversion */
301     USART_RXPIN_DISABLE,                                             /*!< RX pin level not inversion */
302     /* swap TX/RX pins */
303     USART_SWAP_ENABLE,                                               /*!< swap TX/RX pins */
304     USART_SWAP_DISABLE,                                              /*!< not swap TX/RX pins */
305 }usart_invert_enum;
306 
307 /* USART receiver configure */
308 #define CTL0_REN(regval)              (BIT(2) & ((uint32_t)(regval) << 2))
309 #define USART_RECEIVE_ENABLE          CTL0_REN(1)                    /*!< enable receiver */
310 #define USART_RECEIVE_DISABLE         CTL0_REN(0)                    /*!< disable receiver */
311 
312 /* USART transmitter configure */
313 #define CTL0_TEN(regval)              (BIT(3) & ((uint32_t)(regval) << 3))
314 #define USART_TRANSMIT_ENABLE         CTL0_TEN(1)                    /*!< enable transmitter */
315 #define USART_TRANSMIT_DISABLE        CTL0_TEN(0)                    /*!< disable transmitter */
316 
317 /* USART parity bits definitions */
318 #define CTL0_PM(regval)               (BITS(9,10) & ((uint32_t)(regval) << 9))
319 #define USART_PM_NONE                 CTL0_PM(0)                     /*!< no parity */
320 #define USART_PM_EVEN                 CTL0_PM(2)                     /*!< even parity */
321 #define USART_PM_ODD                  CTL0_PM(3)                     /*!< odd parity */
322 
323 /* USART wakeup method in mute mode */
324 #define CTL0_WM(regval)               (BIT(11) & ((uint32_t)(regval) << 11))
325 #define USART_WM_IDLE                 CTL0_WM(0)                     /*!< idle line */
326 #define USART_WM_ADDR                 CTL0_WM(1)                     /*!< address match */
327 
328 /* USART word length definitions */
329 #define CTL0_WL(regval)               (BIT(12) & ((uint32_t)(regval) << 12))
330 #define USART_WL_8BIT                 CTL0_WL(0)                     /*!< 8 bits */
331 #define USART_WL_9BIT                 CTL0_WL(1)                     /*!< 9 bits */
332 
333 /* USART oversample mode */
334 #define CTL0_OVSMOD(regval)           (BIT(15) & ((uint32_t)(regval) << 15))
335 #define USART_OVSMOD_8                CTL0_OVSMOD(1)                 /*!< oversampling by 8 */
336 #define USART_OVSMOD_16               CTL0_OVSMOD(0)                 /*!< oversampling by 16 */
337 
338 /* USART address detection mode */
339 #define CTL1_ADDM(regval)             (BIT(4) & ((uint32_t)(regval) << 4))
340 #define USART_ADDM_4BIT               CTL1_ADDM(0)                   /*!< 4-bit address detection */
341 #define USART_ADDM_FULLBIT            CTL1_ADDM(1)                   /*!< full-bit address detection */
342 
343 /* USART LIN break frame length */
344 #define CTL1_LBLEN(regval)            (BIT(5) & ((uint32_t)(regval) << 5))
345 #define USART_LBLEN_10B               CTL1_LBLEN(0)                  /*!< 10 bits break detection */
346 #define USART_LBLEN_11B               CTL1_LBLEN(1)                  /*!< 11 bits break detection */
347 
348 /* USART last bit clock pulse */
349 #define CTL1_CLEN(regval)             (BIT(8) & ((uint32_t)(regval) << 8))
350 #define USART_CLEN_NONE               CTL1_CLEN(0)                   /*!< clock pulse of the last data bit (MSB) is not output to the CK pin */
351 #define USART_CLEN_EN                 CTL1_CLEN(1)                   /*!< clock pulse of the last data bit (MSB) is output to the CK pin */
352 
353 /* USART clock phase */
354 #define CTL1_CPH(regval)              (BIT(9) & ((uint32_t)(regval) << 9))
355 #define USART_CPH_1CK                 CTL1_CPH(0)                    /*!< first clock transition is the first data capture edge */
356 #define USART_CPH_2CK                 CTL1_CPH(1)                    /*!< second clock transition is the first data capture edge */
357 
358 /* USART clock polarity */
359 #define CTL1_CPL(regval)              (BIT(10) & ((uint32_t)(regval) << 10))
360 #define USART_CPL_LOW                 CTL1_CPL(0)                    /*!< steady low value on CK pin */
361 #define USART_CPL_HIGH                CTL1_CPL(1)                    /*!< steady high value on CK pin */
362 
363 /* USART stop bits definitions */
364 #define CTL1_STB(regval)              (BITS(12,13) & ((uint32_t)(regval) << 12))
365 #define USART_STB_1BIT                CTL1_STB(0)                    /*!< 1 bit */
366 #define USART_STB_0_5BIT              CTL1_STB(1)                    /*!< 0.5 bit */
367 #define USART_STB_2BIT                CTL1_STB(2)                    /*!< 2 bits */
368 #define USART_STB_1_5BIT              CTL1_STB(3)                    /*!< 1.5 bits */
369 
370 /* USART data is transmitted/received with the LSB/MSB first */
371 #define CTL1_MSBF(regval)             (BIT(19) & ((uint32_t)(regval) << 19))
372 #define USART_MSBF_LSB                CTL1_MSBF(0)                   /*!< LSB first */
373 #define USART_MSBF_MSB                CTL1_MSBF(1)                   /*!< MSB first */
374 
375 /* USART auto baud rate detection mode bits definitions */
376 #define CTL1_ABDM(regval)             (BITS(21,22) & ((uint32_t)(regval) << 21))
377 #define USART_ABDM_FTOR               CTL1_ABDM(0)                   /*!< falling edge to rising edge measurement */
378 #define USART_ABDM_FTOF               CTL1_ABDM(1)                   /*!< falling edge to falling edge measurement */
379 
380 /* USART IrDA low-power enable */
381 #define CTL2_IRLP(regval)             (BIT(2) & ((uint32_t)(regval) << 2))
382 #define USART_IRLP_LOW                CTL2_IRLP(1)                   /*!< low-power */
383 #define USART_IRLP_NORMAL             CTL2_IRLP(0)                   /*!< normal */
384 
385 /* DMA enable for reception */
386 #define CTL2_DENR(regval)             (BIT(6) & ((uint32_t)(regval) << 6))
387 #define USART_DENR_ENABLE             CTL2_DENR(1)                   /*!< enable for reception */
388 #define USART_DENR_DISABLE            CTL2_DENR(0)                   /*!< disable for reception */
389 
390 /* DMA enable for transmission */
391 #define CTL2_DENT(regval)             (BIT(7) & ((uint32_t)(regval) << 7))
392 #define USART_DENT_ENABLE             CTL2_DENT(1)                   /*!< enable for transmission */
393 #define USART_DENT_DISABLE            CTL2_DENT(0)                   /*!< disable for transmission */
394 
395 /* USART RTS hardware flow control configure */
396 #define CTL2_RTSEN(regval)            (BIT(8) & ((uint32_t)(regval) << 8))
397 #define USART_RTS_ENABLE              CTL2_RTSEN(1)                  /*!< RTS hardware flow control enabled */
398 #define USART_RTS_DISABLE             CTL2_RTSEN(0)                  /*!< RTS hardware flow control disabled */
399 
400 /* USART CTS hardware flow control configure */
401 #define CTL2_CTSEN(regval)            (BIT(9) & ((uint32_t)(regval) << 9))
402 #define USART_CTS_ENABLE              CTL2_CTSEN(1)                  /*!< CTS hardware flow control enabled */
403 #define USART_CTS_DISABLE             CTL2_CTSEN(0)                  /*!< CTS hardware flow control disabled */
404 
405 /* USART one sample bit method configure */
406 #define CTL2_OSB(regval)              (BIT(11) & ((uint32_t)(regval) << 11))
407 #define USART_OSB_1BIT                CTL2_OSB(1)                    /*!< 1 sample bit */
408 #define USART_OSB_3BIT                CTL2_OSB(0)                    /*!< 3 sample bits */
409 
410 /* USART driver enable polarity mode */
411 #define CTL2_DEP(regval)              (BIT(15) & ((uint32_t)(regval) << 15))
412 #define USART_DEP_HIGH                CTL2_DEP(0)                    /*!< DE signal is active high */
413 #define USART_DEP_LOW                 CTL2_DEP(1)                    /*!< DE signal is active low */
414 
415 /* USART wakeup mode from deep-sleep mode */
416 #define CTL2_WUM(regval)              (BITS(20,21) & ((uint32_t)(regval) << 20))
417 #define USART_WUM_ADDR                CTL2_WUM(0)                    /*!< WUF active on address match */
418 #define USART_WUM_STARTB              CTL2_WUM(2)                    /*!< WUF active on start bit */
419 #define USART_WUM_RBNE                CTL2_WUM(3)                    /*!< WUF active on RBNE */
420 
421 /* function declarations */
422 /* initialization functions */
423 /* reset USART */
424 void usart_deinit(uint32_t usart_periph);
425 /* configure USART baud rate value */
426 void usart_baudrate_set(uint32_t usart_periph, uint32_t baudval);
427 /* configure USART parity function */
428 void usart_parity_config(uint32_t usart_periph, uint32_t paritycfg);
429 /* configure USART word length */
430 void usart_word_length_set(uint32_t usart_periph, uint32_t wlen);
431 /* configure USART stop bit length */
432 void usart_stop_bit_set(uint32_t usart_periph, uint32_t stblen);
433 /* enable USART */
434 void usart_enable(uint32_t usart_periph);
435 /* disable USART */
436 void usart_disable(uint32_t usart_periph);
437 /* configure USART transmitter */
438 void usart_transmit_config(uint32_t usart_periph, uint32_t txconfig);
439 /* configure USART receiver */
440 void usart_receive_config(uint32_t usart_periph, uint32_t rxconfig);
441 
442 /* USART normal mode communication */
443 /* data is transmitted/received with the LSB/MSB first */
444 void usart_data_first_config(uint32_t usart_periph, uint32_t msbf);
445 /* configure USART inverted */
446 void usart_invert_config(uint32_t usart_periph, usart_invert_enum invertpara);
447 /* enable the USART overrun function */
448 void usart_overrun_enable(uint32_t usart_periph);
449 /* disable the USART overrun function */
450 void usart_overrun_disable(uint32_t usart_periph);
451 /* configure the USART oversample mode */
452 void usart_oversample_config(uint32_t usart_periph, uint32_t oversamp);
453 /* configure sample bit method */
454 void usart_sample_bit_config(uint32_t usart_periph, uint32_t osb);
455 /* enable receiver timeout */
456 void usart_receiver_timeout_enable(uint32_t usart_periph);
457 /* disable receiver timeout */
458 void usart_receiver_timeout_disable(uint32_t usart_periph);
459 /* configure receiver timeout threshold */
460 void usart_receiver_timeout_threshold_config(uint32_t usart_periph, uint32_t rtimeout);
461 /* USART transmit data function */
462 void usart_data_transmit(uint32_t usart_periph, uint32_t data);
463 /* USART receive data function */
464 uint16_t usart_data_receive(uint32_t usart_periph);
465 
466 /* auto baud rate detection */
467 /* enable auto baud rate detection */
468 void usart_autobaud_detection_enable(uint32_t usart_periph);
469 /* disable auto baud rate detection */
470 void usart_autobaud_detection_disable(uint32_t usart_periph);
471 /* configure auto baud rate detection mode */
472 void usart_autobaud_detection_mode_config(uint32_t usart_periph, uint32_t abdmod);
473 
474 /* multi-processor communication */
475 /* configure the address of the USART in wake up by address match mode */
476 void usart_address_config(uint32_t usart_periph, uint8_t addr);
477 /* configure address detection mode */
478 void usart_address_detection_mode_config(uint32_t usart_periph, uint32_t addmod);
479 /* enable mute mode */
480 void usart_mute_mode_enable(uint32_t usart_periph);
481 /* disable mute mode */
482 void usart_mute_mode_disable(uint32_t usart_periph);
483 /* configure wakeup method in mute mode */
484 void usart_mute_mode_wakeup_config(uint32_t usart_periph, uint32_t wmethod);
485 
486 /* LIN mode communication */
487 /* enable LIN mode */
488 void usart_lin_mode_enable(uint32_t usart_periph);
489 /* disable LIN mode */
490 void usart_lin_mode_disable(uint32_t usart_periph);
491 /* configure LIN break frame length */
492 void usart_lin_break_detection_length_config(uint32_t usart_periph, uint32_t lblen);
493 
494 /* half-duplex communication */
495 /* enable half-duplex mode */
496 void usart_halfduplex_enable(uint32_t usart_periph);
497 /* disable half-duplex mode */
498 void usart_halfduplex_disable(uint32_t usart_periph);
499 
500 /* synchronous communication */
501 /* enable USART clock */
502 void usart_clock_enable(uint32_t usart_periph);
503 /* disable USART clock */
504 void usart_clock_disable(uint32_t usart_periph);
505 /* configure USART synchronous mode parameters */
506 void usart_synchronous_clock_config(uint32_t usart_periph, uint32_t clen, uint32_t cph, uint32_t cpl);
507 
508 /* smartcard communication */
509 /* configure guard time value in smartcard mode */
510 void usart_guard_time_config(uint32_t usart_periph, uint32_t guat);
511 /* enable smartcard mode */
512 void usart_smartcard_mode_enable(uint32_t usart_periph);
513 /* disable smartcard mode */
514 void usart_smartcard_mode_disable(uint32_t usart_periph);
515 /* enable NACK in smartcard mode */
516 void usart_smartcard_mode_nack_enable(uint32_t usart_periph);
517 /* disable NACK in smartcard mode */
518 void usart_smartcard_mode_nack_disable(uint32_t usart_periph);
519 /* enable early NACK in smartcard mode */
520 void usart_smartcard_mode_early_nack_enable(uint32_t usart_periph);
521 /* disable early NACK in smartcard mode */
522 void usart_smartcard_mode_early_nack_disable(uint32_t usart_periph);
523 /* configure smartcard auto-retry number */
524 void usart_smartcard_autoretry_config(uint32_t usart_periph, uint32_t scrtnum);
525 /* configure block length */
526 void usart_block_length_config(uint32_t usart_periph, uint32_t bl);
527 
528 /* IrDA communication */
529 /* enable IrDA mode */
530 void usart_irda_mode_enable(uint32_t usart_periph);
531 /* disable IrDA mode */
532 void usart_irda_mode_disable(uint32_t usart_periph);
533 /* configure the peripheral clock prescaler in USART IrDA low-power mode or SmartCard mode */
534 void usart_prescaler_config(uint32_t usart_periph, uint32_t psc);
535 /* configure IrDA low-power */
536 void usart_irda_lowpower_config(uint32_t usart_periph, uint32_t irlp);
537 
538 /* hardware flow communication */
539 /* configure hardware flow control RTS */
540 void usart_hardware_flow_rts_config(uint32_t usart_periph, uint32_t rtsconfig);
541 /* configure hardware flow control CTS */
542 void usart_hardware_flow_cts_config(uint32_t usart_periph, uint32_t ctsconfig);
543 
544 /* enable RS485 driver */
545 void usart_rs485_driver_enable(uint32_t usart_periph);
546 /* disable RS485 driver */
547 void usart_rs485_driver_disable(uint32_t usart_periph);
548 /* configure driver enable assertion time */
549 void usart_driver_assertime_config(uint32_t usart_periph, uint32_t deatime);
550 /* configure driver enable de-assertion time */
551 void usart_driver_deassertime_config(uint32_t usart_periph, uint32_t dedtime);
552 /* configure driver enable polarity mode */
553 void usart_depolarity_config(uint32_t usart_periph, uint32_t dep);
554 
555 /* USART DMA */
556 /* configure USART DMA for reception */
557 void usart_dma_receive_config(uint32_t usart_periph, uint32_t dmacmd);
558 /* configure USART DMA for transmission */
559 void usart_dma_transmit_config(uint32_t usart_periph, uint32_t dmacmd);
560 /* disable DMA on reception error */
561 void usart_reception_error_dma_disable(uint32_t usart_periph);
562 /* enable DMA on reception error */
563 void usart_reception_error_dma_enable(uint32_t usart_periph);
564 
565 /* enable USART to wakeup the mcu from deep-sleep mode */
566 void usart_wakeup_enable(uint32_t usart_periph);
567 /* disable USART to wakeup the mcu from deep-sleep mode */
568 void usart_wakeup_disable(uint32_t usart_periph);
569 /* configure the USART wakeup mode from deep-sleep mode */
570 void usart_wakeup_mode_config(uint32_t usart_periph, uint32_t wum);
571 /* enable USART command */
572 void usart_command_enable(uint32_t usart_periph, uint32_t cmdtype);
573 
574 /* USART receive FIFO */
575 /* enable receive FIFO */
576 void usart_receive_fifo_enable(uint32_t usart_periph);
577 /* disable receive FIFO */
578 void usart_receive_fifo_disable(uint32_t usart_periph);
579 /* read receive FIFO counter number */
580 uint8_t usart_receive_fifo_counter_number(uint32_t usart_periph);
581 
582 /* flag & interrupt functions */
583 /* get flag in STAT/RFCS register */
584 FlagStatus usart_flag_get(uint32_t usart_periph, usart_flag_enum flag);
585 /* clear flag in STAT register */
586 void usart_flag_clear(uint32_t usart_periph, usart_flag_enum flag);
587 /* enable USART interrupt */
588 void usart_interrupt_enable(uint32_t usart_periph, usart_interrupt_enum interrupt);
589 /* disable USART interrupt */
590 void usart_interrupt_disable(uint32_t usart_periph, usart_interrupt_enum interrupt);
591 /* get USART interrupt and flag status */
592 FlagStatus usart_interrupt_flag_get(uint32_t usart_periph, usart_interrupt_flag_enum int_flag);
593 /* clear USART interrupt flag */
594 void usart_interrupt_flag_clear(uint32_t usart_periph, usart_interrupt_flag_enum int_flag);
595 
596 #endif /* GD32F3X0_USART_H */
597