1 /*
2  * Copyright (c); 2006-2020, YICHIP Development Team
3  * @file     yc_uart.h
4  * @brief    source file for setting uart
5  *
6  * Change Logs:
7  * Date            Author             Version        Notes
8  * 2020-11-06      wushengyan         V1.0.0         the first version
9  */
10 
11 #ifndef __YC_UART_H__
12 #define __YC_UART_H__
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 #include "yc3122.h"
19 #include "system.h"
20 
21 /**
22  * @defgroup UARTx
23  */
24 #define IS_UART(UARTx)              (((UARTx) == MUART0) ||\
25                                     ((UARTx) == MUART1) ||\
26                                     ((UARTx) == MUART2) ||\
27                                     ((UARTx) == MUART3))
28 
29 /**
30  * @defgroup UART_RxMode
31  */
32 #define MODE_RX_ENABLE              1
33 #define MODE_RX_DISABLE             0
34 #define IS_UART_RX_MODE(MODE)       (((MODE) == MODE_RX_ENABLE) ||\
35                                     ((MODE) == MODE_RX_DISABLE))
36 
37 /**
38  * @defgroup USART_Parity
39  */
40 #define YC_PARITY_NONE                 0
41 #define YC_PARITY_EVEN                 0
42 #define YC_PARITY_ODD                  1
43 #define IS_UART_PARITY(PARITY)      (((PARITY) == YC_PARITY_NONE) ||\
44                                     ((PARITY) == YC_PARITY_EVEN)  ||\
45                                     ((PARITY) == YC_PARITY_ODD))
46 
47 /**
48  * @defgroup UART_DataBits
49  */
50 #define DATABITS_8B                 0
51 #define DATABITS_9B                 1
52 #define IS_UART_WORD_LENGTH(LENGTH) (((LENGTH) == DATABITS_8B) || \
53                                     ((LENGTH) == DATABITS_9B))
54 
55 /**
56  * @defgroup UART_Stop_Bits
57  */
58 #define STOPBITS_1                  0
59 #define STOPBITS_2                  1
60 #define IS_UART_STOPBITS(STOPBITS)  (((STOPBITS) == STOPBITS_1) ||  \
61                                     ((STOPBITS) == STOPBITS_2) )
62 
63 /**
64  * @defgroup UART_Hardware_Flow_Control
65  */
66 #define FLOWCTRL_NONE               0
67 #define FLOWCTRL_ENABLE             1
68 #define IS_UART_FLOW_CTRL(CTRL)     (((CTRL) == FLOWCTRL_NONE) || \
69                                     ((CTRL) == FLOWCTRL_ENABLE))
70 
71 /**
72  * @defgroup UART_Smart_Card_Control
73  */
74 #define SMARTCARD_ENABLE            1
75 #define SMARTCARD_DISABLE           0
76 #define IS_UART_SMART_CARD(CTRL)    (((CTRL) == SMARTCARD_ENABLE) || \
77                                     ((CTRL) == SMARTCARD_DISABLE))
78 
79 /**
80  * @defgroup UART_CommMode
81  */
82 #define MODE_SINGLE_LINE            1
83 #define MODE_DUPLEX                 0
84 #define IS_UART_COMM_MODE(MODE)     (((MODE) == MODE_SINGLE_LINE) ||\
85                                     ((MODE) == MODE_DUPLEX))
86 
87 /**
88  * @defgroup USART_BaudRate
89  */
90 #define IS_UART_BAUDRATE(BAUDRATE)  (((BAUDRATE) > 0x5B8) &&\
91                                     ((BAUDRATE) < 0x0044AA21))
92 
93 /**
94  * @defgroup UART_Interrupt_Type_definition
95  */
96 #define UART_IT_TX                  0x01
97 #define UART_IT_RX                  0x02
98 #define IS_UART_IT(ITx)             (((ITx) == UART_IT_TX) || ((ITx) == UART_IT_RX))
99 
100 typedef struct
101 {
102     uint8_t RxMode;        /*!< Specifies wether the Receive or Transmit mode
103                                 is enabled or disabled. This parameter can be
104                                 a value of @ref UART_Mode */
105 
106     uint8_t Parity;        /*!< Specifies the parity mode.
107                                 This parameter can be a value of
108                                 @ref UART_Parity @note When parity is enabled,
109                                 the computed parity is inserted at
110                                 the MSB position of the transmitted data
111                                 (9th bit when the word length is set to
112                                 9 data bits; 8th bit when the word length is
113                                 set to 8 data bits);. */
114 
115     uint8_t DataBits;      /*!< Specifies the number of data bits transmitted
116                                 or received in a frame. This parameter can be
117                                 a value of @ref UART_DataBits */
118 
119     uint8_t StopBits;      /*!< Specifies the number of stop bits transmitted.
120                                 parameter can be a value of @ref UART_Stop_Bits */
121 
122     uint8_t FlowCtrl;      /*!< Specifies wether the hardware flow control mode
123                                 is enabled or disabled. This parameter can be
124                                 a value of @ref UART_Hardware_Flow_Control */
125 
126     uint8_t SmartCard;
127 
128     uint8_t CommMode;
129 
130     uint32_t BaudRate;     /*!< This member configures the USART
131                                 communication baud rate. */
132 } UART_InitTypeDef;
133 
134 void UART_DeInit(UART_TypeDef *UARTx);
135 void UART_Init(UART_TypeDef *UARTx, UART_InitTypeDef *UART_InitStruct);
136 void UART_StructInit(UART_InitTypeDef *UART_InitStruct);
137 void UART_ITConfig(UART_TypeDef *UARTx, uint32_t UART_IT, FunctionalState NewState);
138 void UART_SendData(UART_TypeDef *UARTx, uint8_t Data);
139 void UART_SendBuf(UART_TypeDef *UARTx, uint8_t *buf, uint32_t len);
140 uint8_t UART_ReceiveData(UART_TypeDef *UARTx);
141 uint32_t UART_ReceiveBuf(UART_TypeDef *UARTx, uint8_t *buf, uint32_t len);
142 void UART_AutoFlowCtrlCmd(UART_TypeDef *UARTx, FunctionalState NewState);
143 uint8_t UART_GetITIdentity(UART_TypeDef *UARTx);
144 Boolean UART_IsRXFIFOFull(UART_TypeDef *UARTx);
145 Boolean UART_IsRXFIFONotEmpty(UART_TypeDef *UARTx);
146 Boolean UART_IsBusy(UART_TypeDef *UARTx);
147 void UART_SetITTimeout(UART_TypeDef *UARTx, uint16_t timeout);
148 void UART_SetRxITNum(UART_TypeDef *UARTx, uint8_t Bcnt);
149 uint32_t UART_ReceiveDataLen(UART_TypeDef *UARTx);
150 
151 #ifdef __cplusplus
152 }
153 #endif
154 
155 #endif
156 
157 /************************ (C) COPYRIGHT Yichip Microelectronics *****END OF FILE****/
158