uart.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /**************************************************************************//**
  2. * @file UART.h
  3. * @version V1.00
  4. * $Revision: 7 $
  5. * $Date: 14/02/05 10:27a $
  6. * @brief Mini51 series UART driver header file
  7. *
  8. * @note
  9. * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
  10. *****************************************************************************/
  11. #ifndef __UART_H__
  12. #define __UART_H__
  13. #include "M051Series.h"
  14. #ifdef __cplusplus
  15. extern "C"
  16. {
  17. #endif
  18. /** @addtogroup M051_Device_Driver M051 Device Driver
  19. @{
  20. */
  21. /** @addtogroup M051_UART_Driver UART Driver
  22. @{
  23. */
  24. /** @addtogroup M051_UART_EXPORTED_CONSTANTS UART Exported Constants
  25. @{
  26. */
  27. /*---------------------------------------------------------------------------------------------------------*/
  28. /* UA_FCR constants definitions */
  29. /*---------------------------------------------------------------------------------------------------------*/
  30. #define UART_FCR_RFITL_1BYTE (0x0 << UART_FCR_RFITL_Pos) /*!< UA_FCR setting to set RX FIFO Trigger Level to 1 bit */
  31. #define UART_FCR_RFITL_4BYTES (0x1 << UART_FCR_RFITL_Pos) /*!< UA_FCR setting to set RX FIFO Trigger Level to 4 bits */
  32. #define UART_FCR_RFITL_8BYTES (0x2 << UART_FCR_RFITL_Pos) /*!< UA_FCR setting to set RX FIFO Trigger Level to 8 bits */
  33. #define UART_FCR_RFITL_14BYTES (0x3 << UART_FCR_RFITL_Pos) /*!< UA_FCR setting to set RX FIFO Trigger Level to 14 bits */
  34. #define UART_FCR_RTS_TRI_LEV_1BYTE (0x0 << UART_FCR_RTS_TRI_LEV_Pos) /*!< UA_FCR setting to set RTS Trigger Level to 1 bit */
  35. #define UART_FCR_RTS_TRI_LEV_4BYTES (0x1 << UART_FCR_RTS_TRI_LEV_Pos) /*!< UA_FCR setting to set RTS Trigger Level to 4 bits */
  36. #define UART_FCR_RTS_TRI_LEV_8BYTES (0x2 << UART_FCR_RTS_TRI_LEV_Pos) /*!< UA_FCR setting to set RTS Trigger Level to 8 bits */
  37. #define UART_FCR_RTS_TRI_LEV_14BYTES (0x3 << UART_FCR_RTS_TRI_LEV_Pos) /*!< UA_FCR setting to set RTS Trigger Level to 14 bits */
  38. /*---------------------------------------------------------------------------------------------------------*/
  39. /* UA_LCR constants definitions */
  40. /*---------------------------------------------------------------------------------------------------------*/
  41. #define UART_WORD_LEN_5 (0) /*!< UA_LCR setting to set UART word length to 5 bits */
  42. #define UART_WORD_LEN_6 (1) /*!< UA_LCR setting to set UART word length to 6 bits */
  43. #define UART_WORD_LEN_7 (2) /*!< UA_LCR setting to set UART word length to 7 bits */
  44. #define UART_WORD_LEN_8 (3) /*!< UA_LCR setting to set UART word length to 8 bits */
  45. #define UART_PARITY_NONE (0x0 << UART_LCR_PBE_Pos) /*!< UA_LCR setting to set UART as no parity */
  46. #define UART_PARITY_ODD (0x1 << UART_LCR_PBE_Pos) /*!< UA_LCR setting to set UART as odd parity */
  47. #define UART_PARITY_EVEN (0x3 << UART_LCR_PBE_Pos) /*!< UA_LCR setting to set UART as even parity */
  48. #define UART_PARITY_MARK (0x5 << UART_LCR_PBE_Pos) /*!< UA_LCR setting to keep parity bit as '1' */
  49. #define UART_PARITY_SPACE (0x7 << UART_LCR_PBE_Pos) /*!< UA_LCR setting to keep parity bit as '0' */
  50. #define UART_STOP_BIT_1 (0x0 << UART_LCR_NSB_Pos) /*!< UA_LCR setting for one stop bit */
  51. #define UART_STOP_BIT_1_5 (0x1 << UART_LCR_NSB_Pos) /*!< UA_LCR setting for 1.5 stop bit when 5-bit word length */
  52. #define UART_STOP_BIT_2 (0x1 << UART_LCR_NSB_Pos) /*!< UA_LCR setting for two stop bit when 6, 7, 8-bit word length */
  53. /*---------------------------------------------------------------------------------------------------------*/
  54. /* UART RTS LEVEL TRIGGER constants definitions */
  55. /*---------------------------------------------------------------------------------------------------------*/
  56. #define UART_RTS_IS_HIGH_LEV_ACTIVE (0x1 << UART_MCR_LEV_RTS_Pos) /*!< Set RTS is High Level Active */
  57. #define UART_RTS_IS_LOW_LEV_ACTIVE (0x0 << UART_MCR_LEV_RTS_Pos) /*!< Set RTS is Low Level Active */
  58. /*---------------------------------------------------------------------------------------------------------*/
  59. /* UA_IRCR constants definitions */
  60. /*---------------------------------------------------------------------------------------------------------*/
  61. #define UART_IRCR_TX_SELECT (1) /*!< Set IrDA function Tx mode */
  62. #define UART_IRCR_RX_SELECT (0) /*!< Set IrDA function Rx mode */
  63. /*---------------------------------------------------------------------------------------------------------*/
  64. /* UA_FUNC_SEL constants definitions */
  65. /*---------------------------------------------------------------------------------------------------------*/
  66. #define UART_FUNC_SEL_UART (0x0 << UART_FUN_SEL_FUN_SEL_Pos) /*!< UA_FUNC_SEL setting to set UART Function (Default) */
  67. #define UART_FUNC_SEL_LIN (0x1 << UART_FUN_SEL_FUN_SEL_Pos) /*!< UA_FUNC_SEL setting to set LIN Function */
  68. #define UART_FUNC_SEL_IrDA (0x2 << UART_FUN_SEL_FUN_SEL_Pos) /*!< UA_FUNC_SEL setting to set IrDA Function */
  69. #define UART_FUNC_SEL_RS485 (0x3 << UART_FUN_SEL_FUN_SEL_Pos) /*!< UA_FUNC_SEL setting to set RS485 Function */
  70. /*@}*/ /* end of group M051_UART_EXPORTED_CONSTANTS */
  71. /** @addtogroup M051_UART_EXPORTED_FUNCTIONS UART Exported Functions
  72. @{
  73. */
  74. /**
  75. * @brief Calculate UART baudrate mode0 divider
  76. *
  77. * @param None
  78. *
  79. * @return UART baudrate mode0 register setting value
  80. *
  81. */
  82. #define UART_BAUD_MODE0 (0)
  83. /**
  84. * @brief Calculate UART baudrate mode0 divider
  85. *
  86. * @param None
  87. *
  88. * @return UART baudrate mode2 register setting value
  89. *
  90. */
  91. #define UART_BAUD_MODE2 (UART_BAUD_DIV_X_EN_Msk | UART_BAUD_DIV_X_ONE_Msk)
  92. /**
  93. * @brief Calculate UART baudrate mode0 divider
  94. *
  95. * @param u32SrcFreq UART clock frequency
  96. * @param u32BaudRate Baudrate of UART module
  97. *
  98. * @return UART baudrate mode0 divider
  99. *
  100. */
  101. #define UART_BAUD_MODE0_DIVIDER(u32SrcFreq, u32BaudRate) (((u32SrcFreq + (u32BaudRate*8)) / u32BaudRate >> 4)-2)
  102. /**
  103. * @brief Calculate UART baudrate mode2 divider
  104. *
  105. * @param u32SrcFreq UART clock frequency
  106. * @param u32BaudRate Baudrate of UART module
  107. *
  108. * @return UART baudrate mode2 divider
  109. */
  110. #define UART_BAUD_MODE2_DIVIDER(u32SrcFreq, u32BaudRate) (((u32SrcFreq + (u32BaudRate/2)) / u32BaudRate)-2)
  111. /**
  112. * @brief Write Data to Tx data register
  113. *
  114. * @param uart The base address of UART module.
  115. * @param u8Data Data byte to transmit
  116. *
  117. * @return None
  118. */
  119. #define UART_WRITE(uart, u8Data) (uart->THR = (u8Data))
  120. /**
  121. * @brief Read Rx data register
  122. *
  123. * @param uart The base address of UART module.
  124. *
  125. * @return The oldest data byte in RX FIFO
  126. */
  127. #define UART_READ(uart) (uart->RBR)
  128. /**
  129. * @brief Get Tx empty register value.
  130. *
  131. * @param uart The base address of UART module
  132. *
  133. * @return Tx empty register value.
  134. */
  135. #define UART_GET_TX_EMPTY(uart) (uart->FSR & UART_FSR_TX_EMPTY_Msk)
  136. /**
  137. * @brief Get Rx empty register value.
  138. *
  139. * @param uart The base address of UART module
  140. *
  141. * @return Rx empty register value.
  142. */
  143. #define UART_GET_RX_EMPTY(uart) (uart->FSR & UART_FSR_RX_EMPTY_Msk)
  144. /**
  145. * @brief Check specified uart port transmission is over.
  146. *
  147. * @param uart The base address of UART module
  148. *
  149. * @return TE_Flag.
  150. */
  151. #define UART_IS_TX_EMPTY(uart) ((uart->FSR & UART_FSR_TE_FLAG_Msk) >> UART_FSR_TE_FLAG_Pos)
  152. /**
  153. * @brief Wait specified uart port transmission is over
  154. *
  155. * @param uart The base address of UART module
  156. *
  157. * @return None
  158. */
  159. #define UART_WAIT_TX_EMPTY(uart) while(!(((uart->FSR) & UART_FSR_TE_FLAG_Msk) >> UART_FSR_TE_FLAG_Pos))
  160. /**
  161. * @brief Check RDA_IF is set or not
  162. *
  163. * @param uart The base address of UART module
  164. *
  165. * @return
  166. * 0 : The number of bytes in the RX FIFO is less than the RFITL
  167. * 1 : The number of bytes in the RX FIFO equals or larger than RFITL
  168. */
  169. #define UART_IS_RX_READY(uart) ((uart->ISR & UART_ISR_RDA_IF_Msk)>>UART_ISR_RDA_IF_Pos)
  170. /**
  171. * @brief Check TX FIFO is full or not
  172. *
  173. * @param uart The base address of UART module
  174. *
  175. * @return
  176. * 1 = TX FIFO is full
  177. * 0 = TX FIFO is not full
  178. */
  179. #define UART_IS_TX_FULL(uart) ((uart->FSR & UART_FSR_TX_FULL_Msk)>>UART_FSR_TX_FULL_Pos)
  180. /**
  181. * @brief Check RX FIFO is full or not
  182. *
  183. * @param uart The base address of UART module
  184. *
  185. * @return
  186. * 1 = RX FIFO is full
  187. * 0 = RX FIFO is not full
  188. *
  189. */
  190. #define UART_IS_RX_FULL(uart) ((uart->FSR & UART_FSR_RX_FULL_Msk)>>UART_FSR_RX_FULL_Pos)
  191. /**
  192. * @brief Get Tx full register value
  193. *
  194. * @param uart The base address of UART module
  195. *
  196. * @return Tx full register value
  197. */
  198. #define UART_GET_TX_FULL(uart) (uart->FSR & UART_FSR_TX_FULL_Msk)
  199. /**
  200. * @brief Get Rx full register value
  201. *
  202. * @param uart The base address of UART module
  203. *
  204. * @return Rx full register value
  205. */
  206. #define UART_GET_RX_FULL(uart) (uart->FSR & UART_FSR_RX_FULL_Msk)
  207. /**
  208. * @brief Enable specified interrupt
  209. *
  210. * @param uart The base address of UART module
  211. * @param u32eIntSel Interrupt type select
  212. * - UART_IER_LIN_RX_BRK_IEN_Msk : Lin bus Rx break field interrupt
  213. * - UART_IER_WAKE_EN_Msk : Wakeup interrupt
  214. * - UART_IER_BUF_ERR_IEN_Msk : Buffer Error interrupt
  215. * - UART_IER_RTO_IEN_Msk : Rx time-out interrupt
  216. * - UART_IER_MODEM_IEN_Msk : Modem interrupt
  217. * - UART_IER_RLS_IEN_Msk : Rx Line status interrupt
  218. * - UART_IER_THRE_IEN_Msk : Tx empty interrupt
  219. * - UART_IER_RDA_IEN_Msk : Rx ready interrupt
  220. *
  221. * @return None
  222. */
  223. #define UART_ENABLE_INT(uart, u32eIntSel) (uart->IER |= (u32eIntSel))
  224. /**
  225. * @brief Disable specified interrupt
  226. *
  227. * @param uart The base address of UART module
  228. * @param u32eIntSel Interrupt type select
  229. * - UART_IER_LIN_RX_BRK_IEN_Msk : Lin bus Rx break field interrupt
  230. * - UART_IER_WAKE_EN_Msk : Wakeup interrupt
  231. * - UART_IER_BUF_ERR_IEN_Msk : Buffer Error interrupt
  232. * - UART_IER_RTO_IEN_Msk : Rx time-out interrupt
  233. * - UART_IER_MODEM_IEN_Msk : Modem interrupt
  234. * - UART_IER_RLS_IEN_Msk : Rx Line status interrupt
  235. * - UART_IER_THRE_IEN_Msk : Tx empty interrupt
  236. * - UART_IER_RDA_IEN_Msk : Rx ready interrupt
  237. * @return None
  238. */
  239. #define UART_DISABLE_INT(uart, u32eIntSel) (uart->IER &= ~ (u32eIntSel))
  240. /**
  241. * @brief Get specified interrupt flag/status
  242. *
  243. * @param uart The base address of UART module
  244. * @param u32eIntTypeFlag Interrupt Type Flag,should be
  245. * - UART_ISR_LIN_RX_BREAK_INT_Msk : Lin bus interrupt
  246. * - UART_ISR_BUF_ERR_INT_Msk : Buffer Error interrupt
  247. * - UART_ISR_TOUT_INT_Msk : Rx time-out interrupt
  248. * - UART_ISR_MODEM_INT_Msk : Modem interrupt
  249. * - UART_ISR_RLS_INT_Msk : Rx Line status interrupt
  250. * - UART_ISR_THRE_INT_Msk : Tx empty interrupt
  251. * - UART_ISR_RDA_INT_Msk : Rx ready interrupt
  252. *
  253. * @return
  254. * 0 = The specified interrupt is not happened.
  255. * 1 = The specified interrupt is happened.
  256. */
  257. #define UART_GET_INT_FLAG(uart,u32eIntTypeFlag) ((uart->ISR & (u32eIntTypeFlag))?1:0)
  258. /**
  259. * @brief Set RTS pin is low
  260. *
  261. * @param uart The base address of UART module
  262. * @return None
  263. */
  264. __INLINE void UART_CLEAR_RTS(UART_T* uart)
  265. {
  266. uart->MCR |= UART_MCR_LEV_RTS_Msk;
  267. uart->MCR &= ~UART_MCR_RTS_Msk;
  268. }
  269. /**
  270. * @brief Set RTS pin is high
  271. *
  272. * @param uart The base address of UART module
  273. * @return None
  274. */
  275. __INLINE void UART_SET_RTS(UART_T* uart)
  276. {
  277. uart->MCR |= UART_MCR_LEV_RTS_Msk | UART_MCR_RTS_Msk;
  278. }
  279. /**
  280. * @brief Clear RS-485 Address Byte Detection Flag
  281. *
  282. * @param uart The base address of UART module
  283. * @return None
  284. */
  285. #define UART_RS485_CLEAR_ADDR_FLAG(uart) (uart->FSR |= UART_FSR_RS485_ADD_DETF_Msk)
  286. /**
  287. * @brief Get RS-485 Address Byte Detection Flag
  288. *
  289. * @param uart The base address of UART module
  290. * @return RS-485 Address Byte Detection Flag
  291. */
  292. #define UART_RS485_GET_ADDR_FLAG(uart) ((uart->FSR & UART_FSR_RS485_ADD_DETF_Msk) >> UART_FSR_RS485_ADD_DETF_Pos)
  293. void UART_ClearIntFlag(UART_T* uart , uint32_t u32InterruptFlag);
  294. void UART_Close(UART_T* uart);
  295. void UART_DisableFlowCtrl(UART_T* uart);
  296. void UART_DisableInt(UART_T* uart, uint32_t u32InterruptFlag);
  297. void UART_EnableFlowCtrl(UART_T* uart);
  298. void UART_EnableInt(UART_T* uart, uint32_t u32InterruptFlag);
  299. void UART_Open(UART_T* uart, uint32_t u32baudrate);
  300. uint32_t UART_Read(UART_T* uart, uint8_t *pu8RxBuf, uint32_t u32ReadBytes);
  301. void UART_SetLine_Config(UART_T* uart, uint32_t u32baudrate, uint32_t u32data_width, uint32_t u32parity, uint32_t u32stop_bits);
  302. void UART_SetTimeoutCnt(UART_T* uart, uint32_t u32TOC);
  303. void UART_SelectIrDAMode(UART_T* uart, uint32_t u32Buadrate, uint32_t u32Direction);
  304. void UART_SelectRS485Mode(UART_T* uart, uint32_t u32Mode, uint32_t u32Addr);
  305. void UART_SelectLINMode(UART_T* uart, uint32_t u32Mode, uint32_t u32BreakLength);
  306. uint32_t UART_Write(UART_T* uart, uint8_t *pu8TxBuf, uint32_t u32WriteBytes);
  307. /*@}*/ /* end of group M051_UART_EXPORTED_FUNCTIONS */
  308. /*@}*/ /* end of group M051_UART_Driver */
  309. /*@}*/ /* end of group M051_Device_Driver */
  310. #ifdef __cplusplus
  311. }
  312. #endif
  313. #endif //__UART_H__
  314. /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/