1 /********************************** (C) COPYRIGHT  *******************************
2 * File Name          : ch32f20x_usart.h
3 * Author             : WCH
4 * Version            : V1.0.0
5 * Date               : 2021/08/08
6 * Description        : This file contains all the functions prototypes for the
7 *                      USART firmware library.
8 *******************************************************************************/
9 #ifndef __CH32F20x_USART_H
10 #define __CH32F20x_USART_H
11 
12 #ifdef __cplusplus
13  extern "C" {
14 #endif
15 
16 #include "ch32f20x.h"
17 
18 
19 /* USART Init Structure definition */
20 typedef struct
21 {
22   uint32_t USART_BaudRate;            /* This member configures the USART communication baud rate.
23                                          The baud rate is computed using the following formula:
24                                           - IntegerDivider = ((PCLKx) / (16 * (USART_InitStruct->USART_BaudRate)))
25                                           - FractionalDivider = ((IntegerDivider - ((u32) IntegerDivider)) * 16) + 0.5 */
26 
27   uint16_t USART_WordLength;          /* Specifies the number of data bits transmitted or received in a frame.
28                                          This parameter can be a value of @ref USART_Word_Length */
29 
30   uint16_t USART_StopBits;            /* Specifies the number of stop bits transmitted.
31                                          This parameter can be a value of @ref USART_Stop_Bits */
32 
33   uint16_t USART_Parity;              /* Specifies the parity mode.
34                                          This parameter can be a value of @ref USART_Parity
35                                          @note When parity is enabled, the computed parity is inserted
36                                                at the MSB position of the transmitted data (9th bit when
37                                                the word length is set to 9 data bits; 8th bit when the
38                                                word length is set to 8 data bits). */
39 
40   uint16_t USART_Mode;                /* Specifies wether the Receive or Transmit mode is enabled or disabled.
41                                          This parameter can be a value of @ref USART_Mode */
42 
43   uint16_t USART_HardwareFlowControl; /* Specifies wether the hardware flow control mode is enabled
44                                          or disabled.
45                                          This parameter can be a value of @ref USART_Hardware_Flow_Control */
46 } USART_InitTypeDef;
47 
48 /* USART Clock Init Structure definition */
49 typedef struct
50 {
51 
52   uint16_t USART_Clock;   /* Specifies whether the USART clock is enabled or disabled.
53                              This parameter can be a value of @ref USART_Clock */
54 
55   uint16_t USART_CPOL;    /* Specifies the steady state value of the serial clock.
56                              This parameter can be a value of @ref USART_Clock_Polarity */
57 
58   uint16_t USART_CPHA;    /* Specifies the clock transition on which the bit capture is made.
59                              This parameter can be a value of @ref USART_Clock_Phase */
60 
61   uint16_t USART_LastBit; /* Specifies whether the clock pulse corresponding to the last transmitted
62                              data bit (MSB) has to be output on the SCLK pin in synchronous mode.
63                              This parameter can be a value of @ref USART_Last_Bit */
64 } USART_ClockInitTypeDef;
65 
66 /* USART_Word_Length */
67 #define USART_WordLength_8b                  ((uint16_t)0x0000)
68 #define USART_WordLength_9b                  ((uint16_t)0x1000)
69 
70 /* USART_Stop_Bits */
71 #define USART_StopBits_1                     ((uint16_t)0x0000)
72 #define USART_StopBits_0_5                   ((uint16_t)0x1000)
73 #define USART_StopBits_2                     ((uint16_t)0x2000)
74 #define USART_StopBits_1_5                   ((uint16_t)0x3000)
75 
76 /* USART_Parity */
77 #define USART_Parity_No                      ((uint16_t)0x0000)
78 #define USART_Parity_Even                    ((uint16_t)0x0400)
79 #define USART_Parity_Odd                     ((uint16_t)0x0600)
80 
81 /* USART_Mode */
82 #define USART_Mode_Rx                        ((uint16_t)0x0004)
83 #define USART_Mode_Tx                        ((uint16_t)0x0008)
84 
85 /* USART_Hardware_Flow_Control */
86 #define USART_HardwareFlowControl_None       ((uint16_t)0x0000)
87 #define USART_HardwareFlowControl_RTS        ((uint16_t)0x0100)
88 #define USART_HardwareFlowControl_CTS        ((uint16_t)0x0200)
89 #define USART_HardwareFlowControl_RTS_CTS    ((uint16_t)0x0300)
90 
91 /* USART_Clock */
92 #define USART_Clock_Disable                  ((uint16_t)0x0000)
93 #define USART_Clock_Enable                   ((uint16_t)0x0800)
94 
95 /* USART_Clock_Polarity */
96 #define USART_CPOL_Low                       ((uint16_t)0x0000)
97 #define USART_CPOL_High                      ((uint16_t)0x0400)
98 
99 /* USART_Clock_Phase */
100 #define USART_CPHA_1Edge                     ((uint16_t)0x0000)
101 #define USART_CPHA_2Edge                     ((uint16_t)0x0200)
102 
103 /* USART_Last_Bit */
104 #define USART_LastBit_Disable                ((uint16_t)0x0000)
105 #define USART_LastBit_Enable                 ((uint16_t)0x0100)
106 
107 /* USART_Interrupt_definition */
108 #define USART_IT_PE                          ((uint16_t)0x0028)
109 #define USART_IT_TXE                         ((uint16_t)0x0727)
110 #define USART_IT_TC                          ((uint16_t)0x0626)
111 #define USART_IT_RXNE                        ((uint16_t)0x0525)
112 #define USART_IT_ORE_RX                      ((uint16_t)0x0325)
113 #define USART_IT_IDLE                        ((uint16_t)0x0424)
114 #define USART_IT_LBD                         ((uint16_t)0x0846)
115 #define USART_IT_CTS                         ((uint16_t)0x096A)
116 #define USART_IT_ERR                         ((uint16_t)0x0060)
117 #define USART_IT_ORE_ER                      ((uint16_t)0x0360)
118 #define USART_IT_NE                          ((uint16_t)0x0260)
119 #define USART_IT_FE                          ((uint16_t)0x0160)
120 
121 #define USART_IT_ORE                          USART_IT_ORE_ER
122 
123 /* USART_DMA_Requests */
124 #define USART_DMAReq_Tx                      ((uint16_t)0x0080)
125 #define USART_DMAReq_Rx                      ((uint16_t)0x0040)
126 
127 /* USART_WakeUp_methods */
128 #define USART_WakeUp_IdleLine                ((uint16_t)0x0000)
129 #define USART_WakeUp_AddressMark             ((uint16_t)0x0800)
130 
131 /* USART_LIN_Break_Detection_Length */
132 #define USART_LINBreakDetectLength_10b       ((uint16_t)0x0000)
133 #define USART_LINBreakDetectLength_11b       ((uint16_t)0x0020)
134 
135 /* USART_IrDA_Low_Power */
136 #define USART_IrDAMode_LowPower              ((uint16_t)0x0004)
137 #define USART_IrDAMode_Normal                ((uint16_t)0x0000)
138 
139 /* USART_Flags */
140 #define USART_FLAG_CTS                       ((uint16_t)0x0200)
141 #define USART_FLAG_LBD                       ((uint16_t)0x0100)
142 #define USART_FLAG_TXE                       ((uint16_t)0x0080)
143 #define USART_FLAG_TC                        ((uint16_t)0x0040)
144 #define USART_FLAG_RXNE                      ((uint16_t)0x0020)
145 #define USART_FLAG_IDLE                      ((uint16_t)0x0010)
146 #define USART_FLAG_ORE                       ((uint16_t)0x0008)
147 #define USART_FLAG_NE                        ((uint16_t)0x0004)
148 #define USART_FLAG_FE                        ((uint16_t)0x0002)
149 #define USART_FLAG_PE                        ((uint16_t)0x0001)
150 
151 
152 void USART_DeInit(USART_TypeDef* USARTx);
153 void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct);
154 void USART_StructInit(USART_InitTypeDef* USART_InitStruct);
155 void USART_ClockInit(USART_TypeDef* USARTx, USART_ClockInitTypeDef* USART_ClockInitStruct);
156 void USART_ClockStructInit(USART_ClockInitTypeDef* USART_ClockInitStruct);
157 void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState);
158 void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState);
159 void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState);
160 void USART_SetAddress(USART_TypeDef* USARTx, uint8_t USART_Address);
161 void USART_WakeUpConfig(USART_TypeDef* USARTx, uint16_t USART_WakeUp);
162 void USART_ReceiverWakeUpCmd(USART_TypeDef* USARTx, FunctionalState NewState);
163 void USART_LINBreakDetectLengthConfig(USART_TypeDef* USARTx, uint16_t USART_LINBreakDetectLength);
164 void USART_LINCmd(USART_TypeDef* USARTx, FunctionalState NewState);
165 void USART_SendData(USART_TypeDef* USARTx, uint16_t Data);
166 uint16_t USART_ReceiveData(USART_TypeDef* USARTx);
167 void USART_SendBreak(USART_TypeDef* USARTx);
168 void USART_SetGuardTime(USART_TypeDef* USARTx, uint8_t USART_GuardTime);
169 void USART_SetPrescaler(USART_TypeDef* USARTx, uint8_t USART_Prescaler);
170 void USART_SmartCardCmd(USART_TypeDef* USARTx, FunctionalState NewState);
171 void USART_SmartCardNACKCmd(USART_TypeDef* USARTx, FunctionalState NewState);
172 void USART_HalfDuplexCmd(USART_TypeDef* USARTx, FunctionalState NewState);
173 void USART_OverSampling8Cmd(USART_TypeDef* USARTx, FunctionalState NewState);
174 void USART_OneBitMethodCmd(USART_TypeDef* USARTx, FunctionalState NewState);
175 void USART_IrDAConfig(USART_TypeDef* USARTx, uint16_t USART_IrDAMode);
176 void USART_IrDACmd(USART_TypeDef* USARTx, FunctionalState NewState);
177 FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG);
178 void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG);
179 ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT);
180 void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT);
181 
182 #ifdef __cplusplus
183 }
184 #endif
185 
186 #endif
187 
188 
189 
190 
191 
192 
193 
194