1 /******************************************************************************
2 *
3 * @brief provide commond UART utilities.
4 *
5 *******************************************************************************/
6 #ifndef _UART_H_
7 #define _UART_H_
8
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12
13 /******************************************************************************
14 * Includes
15 ******************************************************************************/
16 #include "common.h"
17 #include "wdog.h"
18 /******************************************************************************
19 * Constants
20 ******************************************************************************/
21
22 /******************************************************************************
23 * Macros
24 ******************************************************************************/
25 #define MAX_UART_NO 3
26
27 /******************************************************************************
28 * Types
29 ******************************************************************************/
30
31 /******************************************************************************
32 *define uart setting type
33 *
34 *//*! @addtogroup uart_setting_type
35 * @{
36 *******************************************************************************/
37
38 /*!
39 * @brief UART setting type.
40 *
41 */
42
43 typedef struct
44 {
45 uint32_t bEnable : 1; /*!< 1: enable, 0: disable */
46 uint32_t resvd : 31; /*!< 1: reserved bit field */
47 } UART_SettingType;
48 /*! @} End of uart_setting_type */
49
50 /******************************************************************************
51 *define uart config type
52 *
53 *//*! @addtogroup uart_config_type
54 * @{
55 ******************************************************************************/
56 /*!
57 * @brief UART Configuration structure.
58 *
59 */
60 typedef struct
61 {
62 UART_SettingType sSettings; /*!< UART settings */
63 uint32_t u32SysClkHz; /*!< system clock */
64 uint32_t u32Baudrate; /*!< UART baudrate */
65 } UART_ConfigType;
66 /*! @} End of uart_config_type */
67
68 /******************************************************************************
69 *define uart config baudrate type
70 *
71 *//*! @addtogroup uart_config_baudrate_type
72 * @{
73 ******************************************************************************/
74 /*!
75 * @brief UART baudrate type structure.
76 *
77 */
78 typedef struct
79 {
80 uint32_t u32SysClkHz; /*!< system clock */
81 uint32_t u32Baudrate; /*!< UART baudrate */
82 } UART_ConfigBaudrateType;
83 /*! @} End of uart_config_baudrate_type */
84
85 /******************************************************************************
86 *define uart config mode type list
87 *
88 *//*! @addtogroup uart_mode_type_list
89 * @{
90 ******************************************************************************/
91 typedef enum
92 {
93 UART_Mode8Bit, /*!< 8 bit mode */
94 UART_Mode9Bit, /*!< 9 bit mode */
95 UART_ModeEnableLoopback, /*!< enable looback mode */
96 UART_ModeDisableLoopback, /*!< disable loopback mode*/
97 UART_ModeEnableSingleWire, /*!< enable single wire mode */
98 UART_ModeDisableSingleWire, /*!< disable single wire mode */
99 } UART_ModeType;
100 /*! @} End of uart_mode_type_list */
101
102 /******************************************************************************
103 *define uart interrupt type list
104 *
105 *//*! @addtogroup uart_interrupt_type_list
106 * @{
107 ******************************************************************************/
108
109 typedef enum
110 {
111 UART_TxBuffEmptyInt, /*!< transmit buffer empty interrupt */
112 UART_TxCompleteInt, /*!< transmit complete interrupt */
113 UART_RxBuffFullInt, /*!< receive buffer full interrupt */
114
115 UART_IdleLineInt, /*!< idle line interrupt */
116
117 UART_RxOverrunInt, /*!< receive overrun interrupt */
118 UART_NoiseErrorInt, /*!< noise error interrupt */
119 UART_FramingErrorInt, /*!< framing error interrupt */
120 UART_ParityErrorInt, /*!< parity error interrupt */
121 } UART_InterruptType;
122 /*! @} End of uart_interrupt_type_list */
123
124 /******************************************************************************
125 *define uart flag type list
126 *
127 *//*! @addtogroup uart_flag_type_list
128 * @{
129 ******************************************************************************/
130 typedef enum
131 {
132 UART_FlagPF = 0, /*!< Parity error flag */
133 UART_FlagFE, /*!< Framing error flag */
134 UART_FlagNF, /*!< Noise flag */
135 UART_FlagOR, /*!< Receive overrun */
136 UART_FlagIDLE, /*!< Idle line flag */
137 UART_FlagRDRF, /*!< Receive data register full flag */
138 UART_FlagTC, /*!< Transmission complete flag */
139 UART_FlagTDRE, /*!< Transmit data register flag */
140
141 UART_FlagRAF, /*!< Receiver active flag */
142 UART_FlagLBKDE, /*!< LIN break detection enable */
143 UART_FlagBRK13, /*!< Break character generation length */
144 UART_FlagRWUID, /*!< Receive wake up idle detect */
145 UART_FlagRXINV, /*!< Receive data inversion */
146 UART_FlagRev1, /*!< Reserved */
147 UART_FlagRXEDGIF, /*!< RxD pin active edge interrupt flag */
148 UART_FlagLBKDIF, /*!< LIN break detect interrupt flag */
149 } UART_FlagType;
150 /*! @} End of uart_flag_type_list */
151
152 /* callback types */
153 typedef void (*UART_CallbackType)(UART_Type *pUART);
154
155 /******************************************************************************
156 * Global variables
157 ******************************************************************************/
158
159 /******************************************************************************
160 * Inline functions
161 ******************************************************************************/
162
163 /******************************************************************************
164 * define UART APIs
165 *
166 *//*! @addtogroup uart_api_list
167 * @{
168 *******************************************************************************/
169
170 /*****************************************************************************//*!
171 *
172 * @brief read receive buffer
173 *
174 * @param[in] pUART base of UART port
175 *
176 * @return unsign char received char
177 *
178 *****************************************************************************/
UART_ReadDataReg(UART_Type * pUART)179 __STATIC_INLINE uint8_t UART_ReadDataReg(UART_Type *pUART)
180 {
181 /* Return the 8-bit data from the receiver */
182 return pUART->D;
183 }
184 /*****************************************************************************//*!
185 *
186 * @brief write transmit buffer
187 *
188 * @param[in] pUART base of UART port
189 * @param[in] u8Char char to send
190 *
191 * @return none
192 *
193 *****************************************************************************/
UART_WriteDataReg(UART_Type * pUART,uint8_t u8Char)194 __STATIC_INLINE void UART_WriteDataReg(UART_Type *pUART, uint8_t u8Char)
195 {
196 /* Send the character */
197 pUART->D = (uint8_t)u8Char;
198 }
199
200 /*****************************************************************************//*!
201 *
202 * @brief check if a character has been received
203 *
204 * @param[in] pUART base of UART port
205 *
206 * @return 0, No character received; no-zero, Character has been received
207 *
208 * @ Pass/ Fail criteria:
209 *****************************************************************************/
UART_CharPresent(UART_Type * pUART)210 __STATIC_INLINE uint8_t UART_CharPresent(UART_Type *pUART)
211 {
212 return (pUART->S1 & UART_S1_RDRF_MASK);
213 }
214 /*****************************************************************************//*!
215 *
216 * @brief enable transmit
217 *
218 * @param[in] pUART base of UART port
219 *
220 * @return none
221 *
222 *****************************************************************************/
UART_EnableTx(UART_Type * pUART)223 __STATIC_INLINE void UART_EnableTx(UART_Type *pUART)
224 {
225
226 pUART->C2 |= UART_C2_TE_MASK;
227 }
228 /*****************************************************************************//*!
229 *
230 * @brief disable transmit
231 *
232 * @param[in] pUART base of UART port
233 *
234 * @return none
235 *
236 *****************************************************************************/
UART_DisableTx(UART_Type * pUART)237 __STATIC_INLINE void UART_DisableTx(UART_Type *pUART)
238 {
239 pUART->C2 &= (~UART_C2_TE_MASK);
240 }
241 /*****************************************************************************//*!
242 *
243 * @brief enable receive
244 *
245 * @param[in] pUART base of UART port
246 *
247 * @return none
248 *
249 *****************************************************************************/
UART_EnableRx(UART_Type * pUART)250 __STATIC_INLINE void UART_EnableRx(UART_Type *pUART)
251 {
252 pUART->C2 |= UART_C2_RE_MASK;
253 }
254 /*****************************************************************************//*!
255 *
256 * @brief disable receive
257 *
258 * @param[in] pUART base of UART port
259 *
260 * @return none
261 *
262 *****************************************************************************/
UART_DisableRx(UART_Type * pUART)263 __STATIC_INLINE void UART_DisableRx(UART_Type *pUART)
264 {
265 pUART->C2 &= (~UART_C2_RE_MASK);
266 }
267 /*****************************************************************************//*!
268 *
269 * @brief Enable loopback mode
270 *
271 * @param[in] pUART base of UART port
272 *
273 * @return none
274 *
275 *****************************************************************************/
UART_EnableLoopback(UART_Type * pUART)276 __STATIC_INLINE void UART_EnableLoopback(UART_Type *pUART)
277 {
278 pUART->C1 |= UART_C1_LOOPS_MASK;
279 pUART->C1 &= (~UART_C1_RSRC_MASK);
280 }
281 /*****************************************************************************//*!
282 *
283 * @brief enable single wire mode
284 *
285 * @param[in] pUART base of UART port
286 *
287 * @return none
288 *
289 *****************************************************************************/
UART_EnableSingleWire(UART_Type * pUART)290 __STATIC_INLINE void UART_EnableSingleWire(UART_Type *pUART)
291 {
292 pUART->C1 |= UART_C1_LOOPS_MASK;
293 pUART->C1 |= UART_C1_RSRC_MASK;
294 }
295 /*****************************************************************************//*!
296 *
297 * @brief set 8-bit mode
298 *
299 * @param[in] pUART base of UART port
300 *
301 * @return none
302 *
303 *****************************************************************************/
UART_Set8BitMode(UART_Type * pUART)304 __STATIC_INLINE void UART_Set8BitMode(UART_Type *pUART)
305 {
306 pUART->C1 &= (~UART_C1_M_MASK);
307 }
308 /*****************************************************************************//*!
309 *
310 * @brief set 9-bit mode
311 *
312 * @param[in] pUART base of UART port
313 *
314 * @return none
315 *
316 *****************************************************************************/
UART_Set9BitMode(UART_Type * pUART)317 __STATIC_INLINE void UART_Set9BitMode(UART_Type *pUART)
318 {
319 pUART->C1 |= UART_C1_M_MASK;
320 }
321 /*****************************************************************************//*!
322 *
323 * @brief enable transmit buffer empty interrupt
324 *
325 * @param[in] pUART base of UART port
326 *
327 * @return none
328 *
329 * @ Pass/ Fail criteria:
330 *****************************************************************************/
UART_EnableTxBuffEmptyInt(UART_Type * pUART)331 __STATIC_INLINE void UART_EnableTxBuffEmptyInt(UART_Type *pUART)
332 {
333 pUART->C2 |= UART_C2_TIE_MASK;
334 }
335 /*****************************************************************************//*!
336 *
337 * @brief enable transmit complete interrupt
338 *
339 * @param[in] pUART base of UART port
340 *
341 * @return none
342 *
343 * @ Pass/ Fail criteria:
344 *****************************************************************************/
UART_EnableTxCompleteInt(UART_Type * pUART)345 __STATIC_INLINE void UART_EnableTxCompleteInt(UART_Type *pUART)
346 {
347 pUART->C2 |= UART_C2_TCIE_MASK;
348 }
349 /*****************************************************************************//*!
350 *
351 * @brief enable receive buffer full interrupt
352 *
353 * @param[in] pUART base of UART port
354 *
355 * @return none
356 *
357 * @ Pass/ Fail criteria:
358 *****************************************************************************/
UART_EnableRxBuffFullInt(UART_Type * pUART)359 __STATIC_INLINE void UART_EnableRxBuffFullInt(UART_Type *pUART)
360 {
361 pUART->C2 |= UART_C2_RIE_MASK;
362 }
363 /*****************************************************************************//*!
364 *
365 * @brief disable transmit buffer empty interrupt
366 *
367 * @param[in] pUART base of UART port
368 *
369 * @return none
370 *
371 * @ Pass/ Fail criteria:
372 *****************************************************************************/
UART_DisableTxBuffEmptyInt(UART_Type * pUART)373 __STATIC_INLINE void UART_DisableTxBuffEmptyInt(UART_Type *pUART)
374 {
375 pUART->C2 &= (~UART_C2_TIE_MASK);
376 }
377 /*****************************************************************************//*!
378 *
379 * @brief disable transmit complete interrupt
380 *
381 * @param[in] pUART base of UART port
382 *
383 * @return none
384 *
385 * @ Pass/ Fail criteria:
386 *****************************************************************************/
UART_DisableTxCompleteInt(UART_Type * pUART)387 __STATIC_INLINE void UART_DisableTxCompleteInt(UART_Type *pUART)
388 {
389 pUART->C2 &= (~UART_C2_TCIE_MASK);
390 }
391 /*****************************************************************************//*!
392 *
393 * @brief disable receive buffer full interrupt
394 *
395 * @param[in] pUART base of UART port
396 *
397 * @return none
398 *
399 * @ Pass/ Fail criteria:
400 *****************************************************************************/
UART_DisableRxBuffFullInt(UART_Type * pUART)401 __STATIC_INLINE void UART_DisableRxBuffFullInt(UART_Type *pUART)
402 {
403 pUART->C2 &= (~UART_C2_RIE_MASK);
404 }
405 /*****************************************************************************//*!
406 *
407 * @brief print out break character
408 *
409 * @param[in] pUART base of UART port
410 *
411 * @return none
412 *
413 * @ Pass/ Fail criteria:
414 *****************************************************************************/
UART_PutBreak(UART_Type * pUART)415 __STATIC_INLINE void UART_PutBreak(UART_Type *pUART)
416 {
417 /* Write 1 then write 0 to UART_C2[SBK] bit, will put break character */
418 pUART->C2 |= UART_C2_SBK_MASK;
419 pUART->C2 &= (~UART_C2_SBK_MASK);
420 }
421
422 /*****************************************************************************//*!
423 *
424 * @brief check whether tx is complete,i.e. data has been sent out.
425 *
426 * @param[in] pUART base of UART port
427 *
428 * @return
429 * 1, Tx complete flag is set
430 * 0, Tx complete flag is clear
431 *
432 * @ Pass/ Fail criteria: none
433 *****************************************************************************/
UART_IsTxComplete(UART_Type * pUART)434 __STATIC_INLINE uint8_t UART_IsTxComplete(UART_Type *pUART)
435 {
436 return (pUART->S1 & UART_S1_TC_MASK);
437 }
438 /*****************************************************************************//*!
439 *
440 * @brief check whether Tx buffer is empty
441 *
442 * @param[in] pUART base of UART port
443 *
444 * @return
445 * 1, Tx buffer is empty
446 * 0, Tx buffer is not empty
447 *
448 * @ Pass/ Fail criteria: none
449 *****************************************************************************/
UART_IsTxBuffEmpty(UART_Type * pUART)450 __STATIC_INLINE uint8_t UART_IsTxBuffEmpty(UART_Type *pUART)
451 {
452 return (pUART->S1 & UART_S1_TDRE_MASK);
453 }
454 /*****************************************************************************//*!
455 *
456 * @brief check whether Rx buffer is full, i.e. receive a character
457 *
458 * @param[in] pUART base of UART port
459 *
460 * @return
461 * 1, Rx buffer is full
462 * 0, Rx buffer is not full
463 *
464 * @ Pass/ Fail criteria: none
465 *****************************************************************************/
UART_IsRxBuffFull(UART_Type * pUART)466 __STATIC_INLINE uint8_t UART_IsRxBuffFull(UART_Type *pUART)
467 {
468 return (pUART->S1 & UART_S1_RDRF_MASK);
469 }
470 /*! @} End of uart_api_list */
471
472
473 /******************************************************************************
474 * Global functions declaration
475 ******************************************************************************/
476 void UART_Init(UART_Type *pUART, UART_ConfigType *pConfig);
477 uint8_t UART_GetChar(UART_Type *pUART);
478 void UART_PutChar(UART_Type *pUART, uint8_t u8Char);
479 void UART_SetBaudrate(UART_Type *pUART, UART_ConfigBaudrateType *pConfig);
480 void UART_EnableInterrupt(UART_Type *pUART, UART_InterruptType InterruptType);
481 void UART_DisableInterrupt(UART_Type *pUART, UART_InterruptType InterruptType);
482 uint16_t UART_GetFlags(UART_Type *pUART);
483 uint8_t UART_CheckFlag(UART_Type *pUART, UART_FlagType FlagType);
484 void UART_SendWait(UART_Type *pUART, uint8_t *pSendBuff, uint32_t u32Length);
485 void UART_ReceiveWait(UART_Type *pUART, uint8_t *pReceiveBuff, uint32_t u32Length);
486 void UART_WaitTxComplete(UART_Type *pUART);
487 void UART_SetCallback(UART_CallbackType pfnCallback);
488 void UART0_Isr(void);
489 void UART1_Isr(void);
490 void UART2_Isr(void);
491
492
493 #ifdef __cplusplus
494 }
495 #endif
496 #endif /* #ifndef _UART_H_ */
497