1 /********************************** (C) COPYRIGHT  *******************************
2  * File Name          : ch32v10x_usart.h
3  * Author             : WCH
4  * Version            : V1.0.0
5  * Date               : 2020/04/30
6  * Description        : This file contains all the functions prototypes for the
7  *                      USART firmware library.
8  * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
9  * SPDX-License-Identifier: Apache-2.0
10  *******************************************************************************/
11 #ifndef __CH32V10x_USART_H
12 #define __CH32V10x_USART_H
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 #include "ch32v10x.h"
19 
20 /* USART Init Structure definition */
21 typedef struct
22 {
23     uint32_t USART_BaudRate; /* This member configures the USART communication baud rate.
24                                 The baud rate is computed using the following formula:
25                                  - IntegerDivider = ((PCLKx) / (16 * (USART_InitStruct->USART_BaudRate)))
26                                  - FractionalDivider = ((IntegerDivider - ((u32) IntegerDivider)) * 16) + 0.5 */
27 
28     uint16_t USART_WordLength; /* Specifies the number of data bits transmitted or received in a frame.
29                                   This parameter can be a value of @ref USART_Word_Length */
30 
31     uint16_t USART_StopBits; /* Specifies the number of stop bits transmitted.
32                                 This parameter can be a value of @ref USART_Stop_Bits */
33 
34     uint16_t USART_Parity; /* Specifies the parity mode.
35                               This parameter can be a value of @ref USART_Parity
36                               @note When parity is enabled, the computed parity is inserted
37                                     at the MSB position of the transmitted data (9th bit when
38                                     the word length is set to 9 data bits; 8th bit when the
39                                     word length is set to 8 data bits). */
40 
41     uint16_t USART_Mode; /* Specifies wether the Receive or Transmit mode is enabled or disabled.
42                             This parameter can be a value of @ref USART_Mode */
43 
44     uint16_t USART_HardwareFlowControl; /* Specifies wether the hardware flow control mode is enabled
45                                            or disabled.
46                                            This parameter can be a value of @ref USART_Hardware_Flow_Control */
47 } USART_InitTypeDef;
48 
49 /* USART Clock Init Structure definition */
50 typedef struct
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 void       USART_DeInit(USART_TypeDef *USARTx);
152 void       USART_Init(USART_TypeDef *USARTx, USART_InitTypeDef *USART_InitStruct);
153 void       USART_StructInit(USART_InitTypeDef *USART_InitStruct);
154 void       USART_ClockInit(USART_TypeDef *USARTx, USART_ClockInitTypeDef *USART_ClockInitStruct);
155 void       USART_ClockStructInit(USART_ClockInitTypeDef *USART_ClockInitStruct);
156 void       USART_Cmd(USART_TypeDef *USARTx, FunctionalState NewState);
157 void       USART_ITConfig(USART_TypeDef *USARTx, uint16_t USART_IT, FunctionalState NewState);
158 void       USART_DMACmd(USART_TypeDef *USARTx, uint16_t USART_DMAReq, FunctionalState NewState);
159 void       USART_SetAddress(USART_TypeDef *USARTx, uint8_t USART_Address);
160 void       USART_WakeUpConfig(USART_TypeDef *USARTx, uint16_t USART_WakeUp);
161 void       USART_ReceiverWakeUpCmd(USART_TypeDef *USARTx, FunctionalState NewState);
162 void       USART_LINBreakDetectLengthConfig(USART_TypeDef *USARTx, uint16_t USART_LINBreakDetectLength);
163 void       USART_LINCmd(USART_TypeDef *USARTx, FunctionalState NewState);
164 void       USART_SendData(USART_TypeDef *USARTx, uint16_t Data);
165 uint16_t   USART_ReceiveData(USART_TypeDef *USARTx);
166 void       USART_SendBreak(USART_TypeDef *USARTx);
167 void       USART_SetGuardTime(USART_TypeDef *USARTx, uint8_t USART_GuardTime);
168 void       USART_SetPrescaler(USART_TypeDef *USARTx, uint8_t USART_Prescaler);
169 void       USART_SmartCardCmd(USART_TypeDef *USARTx, FunctionalState NewState);
170 void       USART_SmartCardNACKCmd(USART_TypeDef *USARTx, FunctionalState NewState);
171 void       USART_HalfDuplexCmd(USART_TypeDef *USARTx, FunctionalState NewState);
172 void       USART_OverSampling8Cmd(USART_TypeDef *USARTx, FunctionalState NewState);
173 void       USART_OneBitMethodCmd(USART_TypeDef *USARTx, FunctionalState NewState);
174 void       USART_IrDAConfig(USART_TypeDef *USARTx, uint16_t USART_IrDAMode);
175 void       USART_IrDACmd(USART_TypeDef *USARTx, FunctionalState NewState);
176 FlagStatus USART_GetFlagStatus(USART_TypeDef *USARTx, uint16_t USART_FLAG);
177 void       USART_ClearFlag(USART_TypeDef *USARTx, uint16_t USART_FLAG);
178 ITStatus   USART_GetITStatus(USART_TypeDef *USARTx, uint16_t USART_IT);
179 void       USART_ClearITPendingBit(USART_TypeDef *USARTx, uint16_t USART_IT);
180 
181 #ifdef __cplusplus
182 }
183 #endif
184 
185 #endif /* __CH32V10x_USART_H */
186