1 /*!
2  * @file        apm32f4xx_usart.h
3  *
4  * @brief       This file contains all the functions prototypes for the USART firmware library
5  *
6  * @version     V1.0.2
7  *
8  * @date        2022-06-23
9  *
10  * @attention
11  *
12  *  Copyright (C) 2021-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 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 __APM32F4XX_USART_H
28 #define __APM32F4XX_USART_H
29 
30 #ifdef __cplusplus
31   extern "C" {
32 #endif
33 
34 /* Includes */
35 #include "apm32f4xx.h"
36 
37 /** @addtogroup APM32F4xx_StdPeriphDriver
38   @{
39 */
40 
41 /** @addtogroup USART_Driver
42   @{
43 */
44 
45 /** @defgroup USART_Enumerations
46   @{
47 */
48 
49 /**
50  * @brief USART Word Length
51  */
52 typedef enum
53 {
54     USART_WORD_LEN_8B,  /*!< 8-bit data length */
55     USART_WORD_LEN_9B   /*!< 9-bit data length */
56 } USART_WORD_LEN_T;
57 
58 /**
59  * @brief USART Stop bits
60  */
61 typedef enum
62 {
63     USART_STOP_BIT_1,       /*!< 1-bit stop bit */
64     USART_STOP_BIT_0_5,     /*!< 0.5-bit stop bit */
65     USART_STOP_BIT_2,       /*!< 2-bit stop bit */
66     USART_STOP_BIT_1_5      /*!< 1.5-bit stop bit */
67 } USART_STOP_BIT_T;
68 
69 /**
70  * @brief USART Parity
71  */
72 typedef enum
73 {
74     USART_PARITY_NONE  = (uint8_t)0x00, /*!< Disable parity control */
75     USART_PARITY_EVEN  = (uint8_t)0x01, /*!< Enable even parity control */
76     USART_PARITY_ODD   = (uint8_t)0x03  /*!< Enable odd parity control*/
77 } USART_PARITY_T;
78 
79 /**
80  * @brief USART mode
81  */
82 typedef enum
83 {
84     USART_MODE_RX    = (uint8_t)0x01,   /*!< Enable USART Receive mode */
85     USART_MODE_TX    = (uint8_t)0x02,   /*!< Enable USART transmit mode */
86     USART_MODE_TX_RX = (uint8_t)0x03    /*!< Enable USART receive and transmit mode */
87 } USART_MODE_T;
88 
89 /**
90  * @brief USART hardware flow control
91  */
92 typedef enum
93 {
94     USART_HARDWARE_FLOW_NONE,   /*!< Disable hardware flow control */
95     USART_HARDWARE_FLOW_RTS,    /*!< Enable RTS hardware flow control */
96     USART_HARDWARE_FLOW_CTS,    /*!< Enable CTS hardware flow control */
97     USART_HARDWARE_FLOW_RTS_CTS /*!< Enable RTS and CTS hardware flow control */
98 } USART_HARDWARE_FLOW_T;
99 
100 /**
101  * @brief USART Clock enable
102  */
103 typedef enum
104 {
105     USART_CLKEN_DISABLE,    /*!< Disable usart clock */
106     USART_CLKEN_ENABLE      /*!< Enable usart clock */
107 } USART_CLKEN_T;
108 
109 /**
110  * @brief USART Clock polarity
111  */
112 typedef enum
113 {
114     USART_CLKPOL_LOW,   /*!< Set clock polarity to low */
115     USART_CLKPOL_HIGH   /*!< Set clock polarity to high */
116 } USART_CLKPOL_T;
117 
118 /**
119  * @brief USART Clock phase
120  */
121 typedef enum
122 {
123     USART_CLKPHA_1EDGE, /*!< Set usart to sample at the edge of the first clock */
124     USART_CLKPHA_2EDGE  /*!< Set usart to sample at the edge of the second clock */
125 } USART_CLKPHA_T;
126 
127 /**
128  * @brief USART Last bit clock pulse
129  */
130 typedef enum
131 {
132     USART_LBCP_DISABLE, /*!< Enable output last bit clock pulse */
133     USART_LBCP_ENABLE,  /*!< Disable output last bit clock pulse */
134 } USART_LBCP_T;
135 
136 /**
137  * @brief USART Interrupt Source
138  */
139 typedef enum
140 {
141     USART_INT_PE       = 0x0010100, /*!< Parity error interrupt */
142     USART_INT_TXBE     = 0x7010080, /*!< Tansmit data buffer empty interrupt */
143     USART_INT_TXC      = 0x6010040, /*!< Transmission complete interrupt */
144     USART_INT_RXBNE    = 0x5010020, /*!< Receive data buffer not empty interrupt */
145     USART_INT_IDLE     = 0x4010010, /*!< Idle line detection interrupt */
146     USART_INT_OVRE_RX  = 0x3010008, /*!< OverRun Error interruptpt if the RXBNFLG bit is set */
147     USART_INT_LBD      = 0x8020040, /*!< LIN break detection interrupt */
148     USART_INT_CTS      = 0x9040400, /*!< CTS change interrupt */
149     USART_INT_ERR      = 0x0040001, /*!< Error interrupt(Frame error, noise error, overrun error) */
150     USART_INT_OVRE_ER  = 0x3040001, /*!< OverRun Error interruptpt if the EIE bit is set */
151     USART_INT_NE       = 0x2040001, /*!< Noise Error interrupt */
152     USART_INT_FE       = 0x1040001  /*!< Framing Error interrupt */
153 } USART_INT_T;
154 
155 /**
156  * @brief USART DMA enable
157  */
158 typedef enum
159 {
160     USART_DMA_RX    = (uint8_t)0x01,    /*!< USART DMA transmit request */
161     USART_DMA_TX    = (uint8_t)0x02,    /*!< USART DMA receive request */
162     USART_DMA_TX_RX = (uint8_t)0x03     /*!< USART DMA transmit/receive request */
163 } USART_DMA_T;
164 
165 /**
166  * @brief USART Wakeup method
167  */
168 typedef enum
169 {
170     USART_WAKEUP_IDLE_LINE,     /*!< WakeUp by an idle line detection */
171     USART_WAKEUP_ADDRESS_MARK   /*!< WakeUp by an address mark */
172 } USART_WAKEUP_T;
173 
174 /**
175  * @brief USART LIN break detection length
176  */
177 typedef enum
178 {
179     USART_LBDL_10B, /*!< 10-bit break detection */
180     USART_LBDL_11B  /*!< 11-bit break detection */
181 } USART_LBDL_T;
182 
183 /**
184  * @brief USART IrDA low-power
185  */
186 typedef enum
187 {
188     USART_IRDALP_NORMAL,    /*!< Normal */
189     USART_IRDALP_LOWPOWER   /*!< Low-Power */
190 } USART_IRDALP_T;
191 
192 /**
193  * @brief USART flag
194  */
195 typedef enum
196 {
197     USART_FLAG_CTS       = 0x0200,  /*!< CTS Change flag (not available for UART4 and UART5) */
198     USART_FLAG_LBD       = 0x0100,  /*!< LIN Break detection flag */
199     USART_FLAG_TXBE      = 0x0080,  /*!< Transmit data buffer empty flag */
200     USART_FLAG_TXC       = 0x0040,  /*!< Transmission Complete flag */
201     USART_FLAG_RXBNE     = 0x0020,  /*!< Receive data buffer not empty flag */
202     USART_FLAG_IDLE      = 0x0010,  /*!< Idle Line detection flag */
203     USART_FLAG_OVRE      = 0x0008,  /*!< OverRun Error flag */
204     USART_FLAG_NE        = 0x0004,  /*!< Noise Error flag */
205     USART_FLAG_FE        = 0x0002,  /*!< Framing Error flag */
206     USART_FLAG_PE        = 0x0001   /*!< Parity Error flag */
207 } USART_FLAG_T;
208 
209 /**@} end of group USART_Enumerations*/
210 
211 /** @addtogroup USART_Structure Data Structure
212   @{
213 */
214 
215 /**
216  * @brief USART Config struct definition
217  */
218 typedef struct
219 {
220     uint32_t                  baudRate;          /*!< Specifies the baud rate */
221     USART_WORD_LEN_T          wordLength;        /*!< Specifies the word length */
222     USART_STOP_BIT_T          stopBits;          /*!< Specifies the stop bits */
223     USART_PARITY_T            parity;            /*!< Specifies the parity */
224     USART_MODE_T              mode;              /*!< Specifies the mode */
225     USART_HARDWARE_FLOW_T     hardwareFlow;      /*!< Specifies the hardware flow control */
226 } USART_Config_T;
227 
228 /**
229  * @brief USART synchronous communication clock config struct definition
230  */
231 typedef struct
232 {
233     USART_CLKEN_T             clock;             /*!< Enable or Disable Clock */
234     USART_CLKPOL_T            polarity;          /*!< Specifies the clock polarity */
235     USART_CLKPHA_T            phase;             /*!< Specifies the clock phase */
236     USART_LBCP_T              lastBit;           /*!< Enable or Disable last bit clock */
237 } USART_ClockConfig_T;
238 
239 /**@} end of group USART_Structure*/
240 
241 /** @defgroup USART_Functions
242   @{
243 */
244 
245 /* USART Reset */
246 void USART_Reset(USART_T* usart);
247 
248 /* USART Configuration */
249 void USART_Config(USART_T* uart, USART_Config_T* usartConfig);
250 void USART_ConfigStructInit(USART_Config_T* usartConfig);
251 void USART_ConfigClock(USART_T* usart, USART_ClockConfig_T* clockConfig);
252 void USART_ConfigClockStructInit(USART_ClockConfig_T* clockConfig);
253 void USART_Enable(USART_T* usart);
254 void USART_Disable(USART_T* usart);
255 void USART_ConfigPrescaler(USART_T* usart, uint8_t div);
256 void USART_EnableOverSampling8(USART_T* usart);
257 void USART_DisableOverSampling8(USART_T* usart);
258 void USART_EnableOverSampling(USART_T* usart);
259 void USART_DisableOverSampling(USART_T* usart);
260 
261 /* Data transfers */
262 void USART_TxData(USART_T* usart, uint16_t data);
263 uint16_t USART_RxData(USART_T* usart);
264 
265 /* Multi-Processor Communication */
266 void USART_Address(USART_T* usart, uint8_t address);
267 void USART_EnableMuteMode(USART_T* usart);
268 void USART_DisableMuteMode(USART_T* usart);
269 void USART_ConfigWakeUp(USART_T* usart, USART_WAKEUP_T wakeup);
270 
271 /* LIN mode */
272 void USART_ConfigLINBreakDetectLength(USART_T* usart, USART_LBDL_T length);
273 void USART_EnableLIN(USART_T* usart);
274 void USART_DisableLIN(USART_T* usart);
275 void USART_TxBreak(USART_T* usart);
276 
277 /* Half-duplex mode */
278 void USART_EnableHalfDuplex(USART_T* usart);
279 void USART_DisableHalfDuplex(USART_T* usart);
280 
281 /* Smartcard mode */
282 void USART_ConfigGuardTime(USART_T* usart, uint8_t guardTime);
283 void USART_EnableSmartCard(USART_T* usart);
284 void USART_DisableSmartCard(USART_T* usart);
285 void USART_EnableSmartCardNACK(USART_T* usart);
286 void USART_DisableSmartCardNACK(USART_T* usart);
287 
288 /* IrDA mode */
289 void USART_ConfigIrDA(USART_T* usart, USART_IRDALP_T IrDAMode);
290 
291 void USART_EnableIrDA(USART_T* usart);
292 void USART_DisableIrDA(USART_T* usart);
293 
294 /* DMA transfers management */
295 void USART_EnableDMA(USART_T* usart, USART_DMA_T dmaReq);
296 void USART_DisableDMA(USART_T* usart, USART_DMA_T dmaReq);
297 
298 /* Interrupts and flags management */
299 void USART_EnableInterrupt(USART_T* usart, USART_INT_T interrupt);
300 void USART_DisableInterrupt(USART_T* usart, USART_INT_T interrupt);
301 uint8_t USART_ReadStatusFlag(USART_T* usart, USART_FLAG_T flag);
302 void USART_ClearStatusFlag(USART_T* usart, USART_FLAG_T flag);
303 uint8_t USART_ReadIntFlag(USART_T* usart, USART_INT_T flag);
304 void USART_ClearIntFlag(USART_T* usart, USART_INT_T flag);
305 
306 #ifdef __cplusplus
307 }
308 #endif
309 
310 #endif /* __APM32F4XX_USART_H */
311 
312 /**@} end of group USART_Enumerations */
313 /**@} end of group USART_Driver */
314 /**@} end of group APM32F4xx_StdPeriphDriver */
315