1 /*!
2  * @file        apm32s10x_usart.h
3  *
4  * @brief       This file contains all the functions prototypes for the USART firmware library
5  *
6  * @version     V1.0.1
7  *
8  * @date        2022-12-31
9  *
10  * @attention
11  *
12  *  Copyright (C) 2022-2023 Geehy Semiconductor
13  *
14  *  You may not use this file except in compliance with the
15  *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
16  *
17  *  The program is only for reference, which is distributed in the hope
18  *  that it will be usefull and instructional for customers to develop
19  *  their software. Unless required by applicable law or agreed to in
20  *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
21  *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
22  *  See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
23  *  and limitations under the License.
24  */
25 
26 /* Define to prevent recursive inclusion */
27 #ifndef __APM32S10X_USART_H
28 #define __APM32S10X_USART_H
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 /* Includes */
35 #include "apm32s10x.h"
36 
37 /** @addtogroup APM32S10x_StdPeriphDriver
38   @{
39 */
40 
41 /** @addtogroup USART_Driver USART Driver
42   @{
43 */
44 
45 /** @defgroup USART_Enumerations Enumerations
46   @{
47 */
48 
49 /**
50  * @brief   USART Word Length define
51  */
52 typedef enum
53 {
54     USART_WORD_LEN_8B = 0,
55     USART_WORD_LEN_9B = BIT12
56 } USART_WORD_LEN_T;
57 
58 /**
59  * @brief   USART Stop bits define
60  */
61 typedef enum
62 {
63     USART_STOP_BIT_1   = 0,
64     USART_STOP_BIT_0_5 = BIT12,
65     USART_STOP_BIT_2   = BIT13,
66     USART_STOP_BIT_1_5 = BIT12 | BIT13
67 } USART_STOP_BIT_T;
68 
69 /**
70  * @brief   USART Parity define
71  */
72 typedef enum
73 {
74     USART_PARITY_NONE  = 0,
75     USART_PARITY_EVEN  = BIT10,
76     USART_PARITY_ODD   = BIT10 | BIT9
77 } USART_PARITY_T;
78 
79 /**
80  * @brief   USART mode define
81  */
82 typedef enum
83 {
84     USART_MODE_RX      = BIT2,
85     USART_MODE_TX      = BIT3,
86     USART_MODE_TX_RX   = BIT2 | BIT3
87 } USART_MODE_T;
88 
89 /**
90  * @brief   USART hardware flow control define
91  */
92 typedef enum
93 {
94     USART_HARDWARE_FLOW_NONE    = 0,
95     USART_HARDWARE_FLOW_RTS     = BIT8,
96     USART_HARDWARE_FLOW_CTS     = BIT9,
97     USART_HARDWARE_FLOW_RTS_CTS = BIT8 | BIT9
98 } USART_HARDWARE_FLOW_T;
99 
100 /**
101  * @brief   USART Clock enable
102  */
103 typedef enum
104 {
105     USART_CLKEN_DISABLE,
106     USART_CLKEN_ENABLE
107 } USART_CLKEN_T;
108 
109 /**
110  * @brief   USART Clock polarity define
111  */
112 typedef enum
113 {
114     USART_CLKPOL_LOW,
115     USART_CLKPOL_HIGH
116 } USART_CLKPOL_T;
117 
118 /**
119  * @brief   USART Clock phase define
120  */
121 typedef enum
122 {
123     USART_CLKPHA_1EDGE,
124     USART_CLKPHA_2EDGE
125 } USART_CLKPHA_T;
126 
127 /**
128  * @brief   USART Last bit clock pulse enable
129  */
130 typedef enum
131 {
132     USART_LBCP_DISABLE,
133     USART_LBCP_ENABLE,
134 } USART_LBCP_T;
135 
136 /**
137  * @brief   USART Interrupt Source
138  */
139 typedef enum
140 {
141     USART_INT_PE     = 0x0010100,
142     USART_INT_TXBE   = 0x7010080,
143     USART_INT_TXC    = 0x6010040,
144     USART_INT_RXBNE  = 0x5010020,
145     USART_INT_IDLE   = 0x4010010,
146     USART_INT_LBD    = 0x8020040,
147     USART_INT_CTS    = 0x9040400,
148     USART_INT_ERR    = 0x0040001,
149     USART_INT_OVRE   = 0x3040001,
150     USART_INT_NE     = 0x2040001,
151     USART_INT_FE     = 0x1040001
152 } USART_INT_T;
153 
154 /**
155  * @brief   USART DMA enable
156  */
157 typedef enum
158 {
159     USART_DMA_TX    = BIT7,
160     USART_DMA_RX    = BIT6,
161     USART_DMA_TX_RX = BIT6 | BIT7
162 } USART_DMA_T;
163 
164 /**
165  * @brief   USART Wakeup method
166  */
167 typedef enum
168 {
169     USART_WAKEUP_IDLE_LINE,
170     USART_WAKEUP_ADDRESS_MARK
171 } USART_WAKEUP_T;
172 
173 /**
174  * @brief   USART LIN break detection length
175  */
176 typedef enum
177 {
178     USART_LBDL_10B,
179     USART_LBDL_11B
180 } USART_LBDL_T;
181 
182 /**
183  * @brief   USART IrDA low-power
184  */
185 typedef enum
186 {
187     USART_IRDALP_NORMAL,
188     USART_IRDALP_LOWPOWER
189 } USART_IRDALP_T;
190 
191 /**
192  * @brief   USART flag define
193  */
194 typedef enum
195 {
196     USART_FLAG_CTS       = 0x0200,
197     USART_FLAG_LBD       = 0x0100,
198     USART_FLAG_TXBE      = 0x0080,
199     USART_FLAG_TXC       = 0x0040,
200     USART_FLAG_RXBNE     = 0x0020,
201     USART_FLAG_IDLE      = 0x0010,
202     USART_FLAG_OVRE      = 0x0008,
203     USART_FLAG_NE        = 0x0004,
204     USART_FLAG_FE        = 0x0002,
205     USART_FLAG_PE        = 0x0001
206 } USART_FLAG_T;
207 
208 /**@} end of group USART_Enumerations */
209 
210 /** @defgroup USART_Structures Structures
211   @{
212 */
213 
214 /**
215  * @brief   USART Config struct definition
216  */
217 typedef struct
218 {
219     uint32_t                  baudRate;          /*!< Specifies the baud rate */
220     USART_WORD_LEN_T          wordLength;        /*!< Specifies the word length */
221     USART_STOP_BIT_T          stopBits;          /*!< Specifies the stop bits */
222     USART_PARITY_T            parity;            /*!< Specifies the parity */
223     USART_MODE_T              mode;              /*!< Specifies the mode */
224     USART_HARDWARE_FLOW_T     hardwareFlow;      /*!< Specifies the hardware flow control */
225 } USART_Config_T;
226 
227 /**
228  * @brief   USART synchronous communication clock configure structure definition
229  */
230 typedef struct
231 {
232     USART_CLKEN_T             clock;             /*!< Enable or Disable Clock */
233     USART_CLKPOL_T            polarity;          /*!< Specifies the clock polarity */
234     USART_CLKPHA_T            phase;             /*!< Specifies the clock phase */
235     USART_LBCP_T              lastBit;           /*!< Enable or Disable last bit clock */
236 } USART_ClockConfig_T;
237 
238 /**@} end of group USART_Structures */
239 
240 /** @defgroup USART_Functions Functions
241   @{
242 */
243 
244 /* USART Reset and Configuration */
245 void USART_Reset(USART_T* usart);
246 void USART_Config(USART_T* uart, USART_Config_T* usartConfig);
247 void USART_ConfigStructInit(USART_Config_T* usartConfig);
248 void USART_Address(USART_T* usart, uint8_t address);
249 void USART_Enable(USART_T* usart);
250 void USART_Disable(USART_T* usart);
251 
252 /* Clock communication */
253 void USART_ConfigClock(USART_T* usart, USART_ClockConfig_T* clockConfig);
254 void USART_ConfigClockStructInit(USART_ClockConfig_T* clockConfig);
255 
256 /* DMA mode */
257 void USART_EnableDMA(USART_T* usart, USART_DMA_T dmaReq);
258 void USART_DisableDMA(USART_T* usart, USART_DMA_T dmaReq);
259 
260 /* Mute mode */
261 void USART_ConfigWakeUp(USART_T* usart, USART_WAKEUP_T wakeup);
262 void USART_EnableMuteMode(USART_T* usart);
263 void USART_DisableMuteMode(USART_T* usart);
264 
265 /* LIN mode */
266 void USART_ConfigLINBreakDetectLength(USART_T* usart, USART_LBDL_T length);
267 void USART_EnableLIN(USART_T* usart);
268 void USART_DisableLIN(USART_T* usart);
269 
270 /* Transmit and receive */
271 void USART_EnableTx(USART_T* usart);
272 void USART_DisableTx(USART_T* usart);
273 void USART_EnableRx(USART_T* usart);
274 void USART_DisableRx(USART_T* usart);
275 void USART_TxData(USART_T* usart, uint16_t data);
276 uint16_t USART_RxData(USART_T* usart);
277 void USART_TxBreak(USART_T* usart);
278 
279 /* Smartcard mode */
280 void USART_ConfigGuardTime(USART_T* usart, uint8_t guardTime);
281 void USART_ConfigPrescaler(USART_T* usart, uint8_t div);
282 void USART_EnableSmartCard(USART_T* usart);
283 void USART_DisableSmartCard(USART_T* usart);
284 void USART_EnableSmartCardNACK(USART_T* usart);
285 void USART_DisableSmartCardNACK(USART_T* usart);
286 
287 /* Half-duplex mode*/
288 void USART_EnableHalfDuplex(USART_T* usart);
289 void USART_DisableHalfDuplex(USART_T* usart);
290 
291 /* IrDA mode */
292 void USART_ConfigIrDA(USART_T* usart, USART_IRDALP_T IrDAMode);
293 void USART_EnableIrDA(USART_T* usart);
294 void USART_DisableIrDA(USART_T* usart);
295 
296 /* Interrupt and flag */
297 void USART_EnableInterrupt(USART_T* usart, USART_INT_T interrupt);
298 void USART_DisableInterrupt(USART_T* usart, USART_INT_T interrupt);
299 uint8_t USART_ReadStatusFlag(USART_T* usart, USART_FLAG_T flag);
300 void USART_ClearStatusFlag(USART_T* usart, USART_FLAG_T flag);
301 uint8_t USART_ReadIntFlag(USART_T* usart, USART_INT_T flag);
302 void USART_ClearIntFlag(USART_T* usart, USART_INT_T flag);
303 
304 /**@} end of group USART_Functions */
305 /**@} end of group USART_Driver */
306 /**@} end of group APM32S10x_StdPeriphDriver */
307 
308 #ifdef __cplusplus
309 }
310 #endif
311 
312 #endif /* __APM32S10X_USART_H */
313