uart.h 16 KB

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