1 /*!
2  * @file        apm32f0xx_usart.h
3  *
4  * @brief       This file contains all the functions prototypes for the USART firmware library
5  *
6  * @version     V1.0.3
7  *
8  * @date        2022-09-20
9  *
10  * @attention
11  *
12  *  Copyright (C) 2020-2022 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 useful 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 #ifndef __APM32F0XX_USART_H
27 #define __APM32F0XX_USART_H
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 #include "apm32f0xx.h"
34 
35 /** @addtogroup APM32F0xx_StdPeriphDriver
36   @{
37 */
38 
39 /** @addtogroup USART_Driver USART Driver
40   @{
41 */
42 
43 /** @defgroup USART_Macros Macros
44   @{
45 */
46 
47 /** Macros description */
48 #define USART_MACROS      1
49 
50 /**@} end of group USART_Macros*/
51 
52 /** @defgroup USART_Enumerations Enumerations
53   @{
54 */
55 
56 /**
57  * @brief   USART Word Length define
58  */
59 typedef enum
60 {
61     USART_WORD_LEN_8B = 0,            /*!< 8-bit data length */
62     USART_WORD_LEN_9B = BIT12,        /*!< 9-bit data length */
63     USART_WORD_LEN_7B = BIT12 | BIT28 /*!< only available for APM32F072 and APM32F030 devices */
64 } USART_WORD_LEN_T;
65 
66 /**
67  * @brief   USART Stop bits define
68  */
69 typedef enum
70 {
71     USART_STOP_BIT_1    = 0,             /*!< 1-bit stop bit */
72     USART_STOP_BIT_2    = BIT13,         /*!< 2-bit stop bit */
73     USART_STOP_BIT_1_5  = BIT12 | BIT13  /*!< 1.5-bit stop bit */
74 } USART_STOP_BITS_T;
75 
76 /**
77  * @brief   USART Parity define
78  */
79 typedef enum
80 {
81     USART_PARITY_NONE   = 0,            /*!< Disable parity control */
82     USART_PARITY_EVEN   = BIT10,        /*!< Enable even parity control */
83     USART_PARITY_ODD    = BIT10 | BIT9  /*!< Enable odd parity control*/
84 } USART_PARITY_T;
85 
86 /**
87  * @brief   USART mode define
88  */
89 typedef enum
90 {
91     USART_MODE_RX       = BIT2,        /*!< Enable USART Receive mode */
92     USART_MODE_TX       = BIT3,        /*!< Enable USART transmit mode */
93     USART_MODE_TX_RX    = BIT2 | BIT3  /*!< Enable USART receive and transmit mode */
94 } USART_MODE_T;
95 
96 /**
97  * @brief   USART hardware flow control define
98  */
99 typedef enum
100 {
101     USART_FLOW_CTRL_NONE    = 0,           /*!< Disable hardware flow control */
102     USART_FLOW_CTRL_RTS     = BIT8,        /*!< Enable RTS hardware flow control */
103     USART_FLOW_CTRL_CTS     = BIT9,        /*!< Enable CTS hardware flow control */
104     USART_FLOW_CTRL_RTS_CTS = BIT8 | BIT9  /*!< Enable RTS and CTS hardware flow control */
105 } USART_HARDWARE_FLOW_CTRL_T;
106 
107 /**
108  * @brief    USART synchronization clock enable/disable
109  */
110 typedef enum
111 {
112     USART_CLKEN_DISABLE     = ((uint8_t)0),   /*!< Disable usart clock */
113     USART_CLKEN_ENABLE      = ((uint8_t)1)    /*!< Enable usart clock */
114 } USART_CLKEN_T;
115 
116 /**
117  * @brief    USART Clock polarity define
118  */
119 typedef enum
120 {
121     USART_CLKPOL_LOW        = ((uint8_t)0),  /*!< Set clock polarity to low */
122     USART_CLKPOL_HIGH       = ((uint8_t)1)   /*!< Set clock polarity to high */
123 } USART_CLKPOL_T;
124 
125 /**
126  * @brief    USART Clock phase define
127  */
128 typedef enum
129 {
130     USART_CLKPHA_1EDGE      = ((uint8_t)0),  /*!< Set usart to sample at the edge of the first clock */
131     USART_CLKPHA_2EDGE      = ((uint8_t)1)   /*!< Set usart to sample at the edge of the second clock */
132 } USART_CLKPHA_T;
133 
134 /**
135  * @brief    USART Last bit clock pulse enable
136  */
137 typedef enum
138 {
139     USART_LBCP_DISABLE      = ((uint8_t)0),  /*!< Disable output last bit clock pulse */
140     USART_LBCP_ENABLE       = ((uint8_t)1)   /*!< Enable output last bit clock pulse */
141 } USART_LBCP_T;
142 
143 /**
144  * @brief   USART DMA requests
145  */
146 typedef enum
147 {
148     USART_DMA_REQUEST_RX    = BIT6,  /*!< USART DMA receive request */
149     USART_DMA_REQUEST_TX    = BIT7   /*!< USART DMA transmit request */
150 } USART_DMA_REQUEST_T;
151 
152 /**
153  * @brief   USART DMA reception error
154  */
155 typedef enum
156 {
157     USART_DMA_RXERR_ENABLE  = ((uint8_t)0),  /*!< USART DMA reception error enable */
158     USART_DMA_RXERR_DISABLE = ((uint8_t)1)   /*!< USART DMA reception error disable */
159 } USART_DMA_RXERR_T;
160 
161 /**
162  * @brief    USART wakeup method
163  */
164 typedef enum
165 {
166     USART_WAKEUP_IDLE_LINE      = ((uint8_t)0),  /*!< WakeUp by an idle line detection */
167     USART_WAKEUP_ADDRESS_MARK   = ((uint8_t)1)   /*!< WakeUp by an address mark */
168 } USART_WAKEUP_T;
169 
170 /**
171  * @brief    USART LIN break detection length
172  */
173 typedef enum
174 {
175     USART_BREAK_LENGTH_10B   = ((uint8_t)0),  /*!< 10-bit break detection */
176     USART_BREAK_LENGTH_11B   = ((uint8_t)1)   /*!< 11-bit break detection */
177 } USART_BREAK_LENGTH_T;
178 
179 /**
180  * @brief    USART address mode
181  */
182 typedef enum
183 {
184     USART_ADDRESS_MODE_4B   = ((uint8_t)0),  /*!< 4-bit address detection */
185     USART_ADDRESS_MODE_7B   = ((uint8_t)1)   /*!< 7-bit address detection */
186 } USART_ADDRESS_MODE_T;
187 
188 /**
189  * @brief    USART address mode, only available for APM32F072 devices
190  */
191 typedef enum
192 {
193     USART_WAKEUP_SOURCE_ADDRESS = ((uint8_t)0),  /*!< usart wakeup by address */
194     USART_WAKEUP_SOURCE_START   = ((uint8_t)2),  /*!< usart wakeup by start bit */
195     USART_WAKEUP_SOURCE_RXNE    = ((uint8_t)3)   /*!< usart wakeup by RXNE */
196 } USART_WAKEUP_SOURCE_T;
197 
198 /**
199  * @brief    USART driver enable polarity select
200  */
201 typedef enum
202 {
203     USART_DE_POL_HIGH       = ((uint8_t)0),  /*!< driver enable polarity is high */
204     USART_DE_POL_LOW        = ((uint8_t)1)   /*!< driver enable polarity is low */
205 } USART_DE_POL_T;
206 
207 /**
208  * @brief    USART inversion Pins
209  */
210 typedef enum
211 {
212     USART_INVERSION_RX       = BIT16,       /*!< usart RX Pins active level inversion */
213     USART_INVERSION_TX       = BIT17,       /*!< usart TX Pins active level inversion */
214     USART_INVERSION_TX_RX    = BIT16 | BIT17 /*!< usart RX TX Pins active level inversion */
215 } USART_INVERSION_T;
216 
217 /**
218  * @brief    USART IrDA Low Power
219  */
220 typedef enum
221 {
222     USART_IRDA_MODE_NORMAL     = ((uint8_t)0),  /*!< usart irda works in normal mode */
223     USART_IRDA_MODE_LOWPOWER   = ((uint8_t)1)   /*!< usart irda works in low-power mode */
224 } USART_IRDA_MODE_T;
225 
226 /**
227  * @brief    USART auto baud rate mode
228  */
229 typedef enum
230 {
231     USART_AUTO_BAUD_RATE_STARTBIT     = ((uint8_t)0x00),  /*!< auto-baud measure start bit */
232     USART_AUTO_BAUD_RATE_FALLINGEDGE  = ((uint8_t)0x01)   /*!< auto-baud measure falling edge */
233 } USART_AUTO_BAUD_RATE_T;
234 
235 /**
236  * @brief    USART  over detection  disable
237  */
238 typedef enum
239 {
240     USART_OVER_DETECTION_ENABLE     = ((uint8_t)0),  /*!< enable overrun detection */
241     USART_OVER_DETECTION_DISABLE    = ((uint8_t)1)   /*!< disable overrun detection */
242 } USART_OVER_DETECTION_T;
243 
244 /**
245  * @brief   USART request
246  */
247 typedef enum
248 {
249     USART_REQUEST_ABRQ     = ((uint8_t)0x01),         /*!< Auto Baud Rate Request */
250     USART_REQUEST_SBQ      = ((uint8_t)0x02),         /*!< Send Break Request */
251     USART_REQUEST_MMQ      = ((uint8_t)0x04),         /*!< Mute Mode Request */
252     USART_REQUEST_RDFQ     = ((uint8_t)0x08),         /*!< Receive data flush Request */
253     USART_REQUEST_TDFQ     = ((uint8_t)0x10)          /*!< Transmit data flush Request */
254 } USART_REQUEST_T;
255 
256 /**
257  * @brief    USART flag definition
258  */
259 typedef enum
260 {
261     USART_FLAG_RXENACKF    = ((uint32_t)0x00400000),  /*!< Receive Enable Acknowledge Flag */
262     USART_FLAG_TXENACKF    = ((uint32_t)0x00200000),  /*!< Transmit Enable Acknowledge Flag */
263     USART_FLAG_WAKEUP      = ((uint32_t)0x00100000),  /*!< Wake Up from stop mode Flag (Not for APM32F030 devices) */
264     USART_FLAG_RWF         = ((uint32_t)0x00080000),  /*!< Send Break flag (Not for APM32F030 devices) */
265     USART_FLAG_SBF         = ((uint32_t)0x00040000),  /*!< Send Break flag */
266     USART_FLAG_CMF         = ((uint32_t)0X00020000),  /*!< Character match flag */
267     USART_FLAG_BUSY        = ((uint32_t)0X00010000),  /*!< Busy flag */
268     USART_FLAG_ABRTF       = ((uint32_t)0X00008000),  /*!< Auto baud rate flag */
269     USART_FLAG_ABRTE       = ((uint32_t)0X00004000),  /*!< Auto baud rate error flag */
270     USART_FLAG_EOBF        = ((uint32_t)0X00001000),  /*!< End of block flag (Not for APM32F030 devices) */
271     USART_FLAG_RXTOF       = ((uint32_t)0X00000800),  /*!< Receive time out flag */
272     USART_FLAG_CTSF        = ((uint32_t)0X00000400),  /*!< CTS Change flag */
273     USART_FLAG_CTSIF       = ((uint32_t)0X00000200),  /*!< CTS interrupt flag */
274     USART_FLAG_LBDF        = ((uint32_t)0x00000100),  /*!< LIN Break Detection Flag (Not for APM32F030 devices) */
275     USART_FLAG_TXBE        = ((uint32_t)0X00000080),  /*!< Transmit data register empty flag */
276     USART_FLAG_TXC         = ((uint32_t)0X00000040),  /*!< Transmission Complete flag */
277     USART_FLAG_RXBNE       = ((uint32_t)0X00000020),  /*!< Receive data buffer not empty flag */
278     USART_FLAG_IDLEF       = ((uint32_t)0X00000010),  /*!< Idle Line detection flag */
279     USART_FLAG_OVRE        = ((uint32_t)0X00000008),  /*!< OverRun Error flag */
280     USART_FLAG_NEF         = ((uint32_t)0X00000004),  /*!< Noise Error flag */
281     USART_FLAG_FEF         = ((uint32_t)0X00000002),  /*!< Framing Error flag */
282     USART_FLAG_PEF         = ((uint32_t)0X00000001),  /*!< Parity Error flag */
283 } USART_FLAG_T;
284 
285 /**
286  * @brief   USART interrupts source
287  */
288 typedef enum
289 {
290     USART_INT_WAKEUP       = ((uint32_t)0x00400000),  /*!< Wake up interrupt (Not for APM32F030 devices) */
291     USART_INT_CMIE         = ((uint32_t)0x00004000),  /*!< Character match interrupt */
292     USART_INT_EOBIE        = ((uint32_t)0x08000000),  /*!< End of Block interrupt (Not for APM32F030 devices) */
293     USART_INT_RXTOIE       = ((uint32_t)0x04000000),  /*!< Receive time out interrupt */
294     USART_INT_CTSIE        = ((uint32_t)0x00000400),  /*!< CTS change interrupt */
295     USART_INT_LBDIE        = ((uint32_t)0X00000040),  /*!< LIN Break detection interrupt (Not for APM32F030 devices) */
296     USART_INT_TXBEIE       = ((uint32_t)0x00000080),  /*!< Tansmit Data Register empty interrupt */
297     USART_INT_TXCIE        = ((uint32_t)0x40000040),  /*!< Transmission complete interrupt */
298     USART_INT_RXBNEIE      = ((uint32_t)0x00000020),  /*!< Receive Data buffer not empty interrupt */
299     USART_INT_IDLEIE       = ((uint32_t)0x00000010),  /*!< Idle line detection interrupt */
300     USART_INT_PEIE         = ((uint32_t)0x00000100),  /*!< Parity Error interrupt */
301     USART_INT_ERRIE        = ((uint32_t)0x00000001),  /*!< Error interrupt */
302 } USART_INT_T;
303 
304 /**
305  * @brief   USART Interrupt flag definition
306  */
307 typedef enum
308 {
309     USART_INT_FLAG_WAKEUP  = ((uint32_t)0X00100000),  /*!< Wake up flag (Not for APM32F030 devices) */
310     USART_INT_FLAG_CMF     = ((uint32_t)0X00020000),  /*!< Character match flag */
311     USART_INT_FLAG_EOBF    = ((uint32_t)0X00001000),  /*!< End of block flag (Not for APM32F030 devices) */
312     USART_INT_FLAG_RXTOF   = ((uint32_t)0X00000800),  /*!< Receive time out flag */
313     USART_INT_FLAG_CTSIF   = ((uint32_t)0X00000200),  /*!< CTS interrupt flag */
314     USART_INT_FLAG_LBDF    = ((uint32_t)0X00000100),  /*!< LIN Break detection flag (Not for APM32F030 devices) */
315     USART_INT_FLAG_TXBE    = ((uint32_t)0X00000080),  /*!< Transmit data register empty flag */
316     USART_INT_FLAG_TXC     = ((uint32_t)0X00000040),  /*!< Transmission Complete flag */
317     USART_INT_FLAG_RXBNE   = ((uint32_t)0X00000020),  /*!< Receive data buffer not empty flag */
318     USART_INT_FLAG_IDLE    = ((uint32_t)0X00000010),  /*!< Idle Line detection flag */
319     USART_INT_FLAG_OVRE    = ((uint32_t)0X00000008),  /*!< OverRun Error flag */
320     USART_INT_FLAG_NE      = ((uint32_t)0X00000004),  /*!< Noise Error flag */
321     USART_INT_FLAG_FE      = ((uint32_t)0X00000002),  /*!< Framing Error flag */
322     USART_INT_FLAG_PE      = ((uint32_t)0X00000001),  /*!< Parity Error flag */
323 } USART_INT_FLAG_T;
324 
325 /**@} end of group USART_Enumerations*/
326 
327 /** @defgroup USART_Structures Structures
328   @{
329 */
330 
331 /**
332  * @brief   USART Config struct definition
333  */
334 typedef struct
335 {
336     uint32_t                    baudRate;       /*!< Specifies the baud rate */
337     USART_WORD_LEN_T            wordLength;     /*!< Specifies the word length */
338     USART_STOP_BITS_T           stopBits;       /*!< Specifies the stop bits */
339     USART_PARITY_T              parity;         /*!< Specifies the parity */
340     USART_MODE_T                mode;           /*!< Specifies the mode */
341     USART_HARDWARE_FLOW_CTRL_T  hardwareFlowCtrl;
342 } USART_Config_T;
343 
344 /**
345  * @brief   USART synchronous communication clock config struct definition
346  */
347 typedef struct
348 {
349     USART_CLKEN_T               enable;          /*!< Enable or Disable Clock */
350     USART_CLKPOL_T              polarity;        /*!< Specifies the clock polarity */
351     USART_CLKPHA_T              phase;           /*!< Specifies the clock phase */
352     USART_LBCP_T                lastBitClock;    /*!< Enable or Disable last bit clock */
353 } USART_SyncClockConfig_T;
354 
355 /**@} end of group USART_Structures*/
356 
357 /** @defgroup USART_Variables Variables
358   @{
359   */
360 
361 /**@} end of group USART_Variables */
362 
363 /** @addtogroup USART_Functions Functions
364   @{
365 */
366 
367 /* USART peripheral Reset and Configuration */
368 void USART_Reset(USART_T* usart);
369 void USART_Config(USART_T* uart, USART_Config_T* configStruct);
370 void USART_ConfigStructInit(USART_Config_T* configStruct);
371 void USART_ConfigSyncClock(USART_T* usart, USART_SyncClockConfig_T* SyncClockConfig);
372 void USART_ConfigSyncClockStructInit(USART_SyncClockConfig_T* SyncClockConfig);
373 void USART_Enable(USART_T* usart);
374 void USART_Disable(USART_T* usart);
375 void USART_EnableDirectionMode(USART_T* usart, USART_MODE_T mode);
376 void USART_DisableDirectionMode(USART_T* usart, USART_MODE_T mode);
377 void USART_ConfigDivider(USART_T* usart, uint8_t divider);  /*!< Not for APM32F030 devices */
378 void USART_EnableOverSampling8(USART_T* usart);
379 void USART_DisableOverSampling8(USART_T* usart);
380 void USART_EnableMSBFirst(USART_T* usart);
381 void USART_DisableMSBFirst(USART_T* usart);
382 void USART_EnableOneBitMethod(USART_T* usart);
383 void USART_DisableOneBitMethod(USART_T* usart);
384 void USART_EnableDataInv(USART_T* usart);
385 void USART_DisableDataInv(USART_T* usart);
386 void USART_EnableInvPin(USART_T* usart, USART_INVERSION_T invPin);
387 void USART_DisableInvPin(USART_T* usart, USART_INVERSION_T invPin);
388 void USART_EnableSWAPPin(USART_T* usart);
389 void USART_DisableSWAPPin(USART_T* usart);
390 void USART_EnableReceiverTimeOut(USART_T* usart);
391 void USART_DisableReceiverTimeOut(USART_T* usart);
392 void USART_ReceiverTimeOutValue(USART_T* usart, uint32_t timeOut);
393 void USART_EnableAutoBaudRate(USART_T* usart);
394 void USART_DisableAutoBaudRate(USART_T* usart);
395 void USART_ConfigAutoBaudRate(USART_T* usart, USART_AUTO_BAUD_RATE_T mode);
396 void USART_ConfigOverrunDetection(USART_T* usart, USART_OVER_DETECTION_T overDetection);
397 
398 /* Stop mode */
399 void USART_EnableStopMode(USART_T* usart);
400 void USART_DisableStopMode(USART_T* usart);
401 void USART_ConfigStopModeWakeUpSource(USART_T* usart, USART_WAKEUP_SOURCE_T source);  /*!< Not for APM32F030 devices */
402 
403 /* Address */
404 void USART_Address(USART_T* usart, uint8_t address);
405 void USART_ConfigAddressDetection(USART_T* usart, USART_ADDRESS_MODE_T address);
406 
407 /* Transmit and receive */
408 void USART_TxData(USART_T* usart, uint16_t data);
409 uint16_t USART_RxData(USART_T* usart);
410 
411 /* Mute mode */
412 void USART_EnableMuteMode(USART_T* usart);
413 void USART_DisableMuteMode(USART_T* usart);
414 void USART_ConfigMuteModeWakeUp(USART_T* usart, USART_WAKEUP_T wakeup);
415 
416 /* LIN mode */
417 void USART_EnableLINmode(USART_T* usart);   /*!< Not for APM32F030 devices */
418 void USART_DisableLINmode(USART_T* usart);  /*!< Not for APM32F030 devices */
419 void USART_ConfigLINbreakDetectLength(USART_T* usart, USART_BREAK_LENGTH_T length);   /*!< Not for APM32F030 devices */
420 
421 /* Half-duplex mode  */
422 void USART_EnableHalfDuplex(USART_T* usart);
423 void USART_DisableHalfDuplex(USART_T* usart);
424 
425 /* Smartcard mode */
426 void USART_EnableSmartCard(USART_T* usart);   /*!< Not for APM32F030 devices */
427 void USART_DisableSmartCard(USART_T* usart);  /*!< Not for APM32F030 devices */
428 void USART_EnableSmartCardNACK(USART_T* usart);   /*!< Not for APM32F030 devices */
429 void USART_DisableSmartCardNACK(USART_T* usart);  /*!< Not for APM32F030 devices */
430 void USART_ConfigGuardTime(USART_T* usart, uint8_t guardTime);  /*!< Not for APM32F030 devices */
431 void USART_ConfigAutoCount(USART_T* usart, uint8_t autoCount);  /*!< Not for APM32F030 devices */
432 void USART_ConfigBlockSize(USART_T* usart, uint8_t blockSize);  /*!< Not for APM32F030 devices */
433 
434 /* IrDA mode */
435 void USART_EnableIrDA(USART_T* usart);   /*!< Not for APM32F030 devices */
436 void USART_DisableIrDA(USART_T* usart);  /*!< Not for APM32F030 devices */
437 void USART_ConfigIrDAMode(USART_T* usart, USART_IRDA_MODE_T IrDAMode);  /*!< Not for APM32F030 devices */
438 
439 /* Driver enable Configuration */
440 void USART_EnableDE(USART_T* usart);
441 void USART_DisableDE(USART_T* usart);
442 void USART_ConfigDEPolarity(USART_T* usart, USART_DE_POL_T polarity);
443 void USART_DEAssertionTimeValue(USART_T* usart, uint8_t value);
444 void USART_DEDeassertionTimeValue(USART_T* usart, uint8_t value);
445 
446 /* DMA */
447 void USART_EnableDMA(USART_T* usart, uint32_t dmaReq);
448 void USART_DisableDMA(USART_T* usart, uint32_t dmaReq);
449 void USART_ConfigDMAReceptionError(USART_T* usart, USART_DMA_RXERR_T error);
450 
451 /* Request */
452 void USART_EnableRequest(USART_T* usart, USART_REQUEST_T request);
453 void USART_DisableRequest(USART_T* usart, USART_REQUEST_T request);
454 
455 /* Interrupt */
456 void USART_EnableInterrupt(USART_T* usart, USART_INT_T interrupt);
457 void USART_DisableInterrupt(USART_T* usart, USART_INT_T interrupt);
458 uint8_t USART_ReadIntFlag(USART_T* usart, USART_INT_FLAG_T flag);
459 void USART_ClearIntFlag(USART_T* usart, uint32_t flag);
460 
461 /* Flag */
462 uint8_t USART_ReadStatusFlag(USART_T* usart, USART_FLAG_T flag);
463 void USART_ClearStatusFlag(USART_T* usart, uint32_t flag);
464 
465 #ifdef __cplusplus
466 }
467 #endif
468 
469 #endif /* __APM32F0XX_USART_H */
470 
471 /**@} end of group USART_Functions */
472 /**@} end of group USART_Driver */
473 /**@} end of group APM32F0xx_StdPeriphDriver */
474