usart_001.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. /*
  2. * @brief Usart Registers and control functions
  3. *
  4. * @note
  5. * Copyright(C) NXP Semiconductors, 2012
  6. * All rights reserved.
  7. *
  8. * @par
  9. * Software that is described herein is for illustrative purposes only
  10. * which provides customers with programming information regarding the
  11. * LPC products. This software is supplied "AS IS" without any warranties of
  12. * any kind, and NXP Semiconductors and its licensor disclaim any and
  13. * all warranties, express or implied, including all implied warranties of
  14. * merchantability, fitness for a particular purpose and non-infringement of
  15. * intellectual property rights. NXP Semiconductors assumes no responsibility
  16. * or liability for the use of the software, conveys no license or rights under any
  17. * patent, copyright, mask work right, or any other intellectual property rights in
  18. * or to any products. NXP Semiconductors reserves the right to make changes
  19. * in the software without notification. NXP Semiconductors also makes no
  20. * representation or warranty that such application will be suitable for the
  21. * specified use without further testing or modification.
  22. *
  23. * @par
  24. * Permission to use, copy, modify, and distribute this software and its
  25. * documentation is hereby granted, under NXP Semiconductors' and its
  26. * licensor's relevant copyrights in the software, without fee, provided that it
  27. * is used in conjunction with NXP Semiconductors microcontrollers. This
  28. * copyright, permission, and disclaimer notice must appear in all copies of
  29. * this code.
  30. */
  31. #ifndef __USART_001_H_
  32. #define __USART_001_H_
  33. #include "sys_config.h"
  34. #include "cmsis.h"
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. /** @defgroup IP_USART_001 IP: USART register block and driver
  39. * @ingroup IP_Drivers
  40. * @{
  41. */
  42. /**
  43. * @brief USART register block structure
  44. */
  45. typedef struct { /*!< USARTn Structure */
  46. union {
  47. __IO uint32_t DLL; /*!< Divisor Latch LSB. Least significant byte of the baud rate divisor value. The full divisor is used to generate a baud rate from the fractional rate divider (DLAB = 1). */
  48. __O uint32_t THR; /*!< Transmit Holding Register. The next character to be transmitted is written here (DLAB = 0). */
  49. __I uint32_t RBR; /*!< Receiver Buffer Register. Contains the next received character to be read (DLAB = 0). */
  50. };
  51. union {
  52. __IO uint32_t IER; /*!< Interrupt Enable Register. Contains individual interrupt enable bits for the 7 potential UART interrupts (DLAB = 0). */
  53. __IO uint32_t DLM; /*!< Divisor Latch MSB. Most significant byte of the baud rate divisor value. The full divisor is used to generate a baud rate from the fractional rate divider (DLAB = 1). */
  54. };
  55. union {
  56. __O uint32_t FCR; /*!< FIFO Control Register. Controls UART FIFO usage and modes. */
  57. __I uint32_t IIR; /*!< Interrupt ID Register. Identifies which interrupt(s) are pending. */
  58. };
  59. __IO uint32_t LCR; /*!< Line Control Register. Contains controls for frame formatting and break generation. */
  60. __IO uint32_t MCR; /*!< Modem Control Register. Only present on USART ports with full modem support. */
  61. __I uint32_t LSR; /*!< Line Status Register. Contains flags for transmit and receive status, including line errors. */
  62. __I uint32_t MSR; /*!< Modem Status Register. Only present on USART ports with full modem support. */
  63. __IO uint32_t SCR; /*!< Scratch Pad Register. Eight-bit temporary storage for software. */
  64. __IO uint32_t ACR; /*!< Auto-baud Control Register. Contains controls for the auto-baud feature. */
  65. __IO uint32_t ICR; /*!< IrDA control register (not all UARTS) */
  66. __IO uint32_t FDR; /*!< Fractional Divider Register. Generates a clock input for the baud rate divider. */
  67. __IO uint32_t OSR; /*!< Oversampling Register. Controls the degree of oversampling during each bit time. Only on some UARTS. */
  68. __IO uint32_t TER1; /*!< Transmit Enable Register. Turns off USART transmitter for use with software flow control. */
  69. uint32_t RESERVED0[3];
  70. __IO uint32_t HDEN; /*!< Half-duplex enable Register- only on some UARTs */
  71. __I uint32_t RESERVED1[1];
  72. __IO uint32_t SCICTRL; /*!< Smart card interface control register- only on some UARTs */
  73. __IO uint32_t RS485CTRL; /*!< RS-485/EIA-485 Control. Contains controls to configure various aspects of RS-485/EIA-485 modes. */
  74. __IO uint32_t RS485ADRMATCH; /*!< RS-485/EIA-485 address match. Contains the address match value for RS-485/EIA-485 mode. */
  75. __IO uint32_t RS485DLY; /*!< RS-485/EIA-485 direction control delay. */
  76. union {
  77. __IO uint32_t SYNCCTRL; /*!< Synchronous mode control register. Only on USARTs. */
  78. __I uint32_t FIFOLVL; /*!< FIFO Level register. Provides the current fill levels of the transmit and receive FIFOs. */
  79. };
  80. __IO uint32_t TER2; /*!< Transmit Enable Register. Only on LPC177X_8X UART4 and LPC18XX/43XX USART0/2/3. */
  81. } IP_USART_001_Type;
  82. #define UART_BLOCKING_TIMEOUT (0xFFFFFFFFUL) /*!< UART time-out definitions in case of using Read/Write function with Blocking Flag mode */
  83. #define UART_ACCEPTED_BAUDRATE_ERROR (3) /*!< Acceptable UART baudrate error */
  84. /* --------------------- BIT DEFINITIONS -------------------------------------- */
  85. /**
  86. * @brief Macro defines for UARTn Receiver Buffer Register
  87. */
  88. #define UART_RBR_MASKBIT ((uint8_t) 0xFF) /*!< UART Received Buffer mask bit (8 bits) */
  89. /**
  90. * @brief Macro defines for UARTn Transmit Holding Register
  91. */
  92. #define UART_THR_MASKBIT ((uint8_t) 0xFF) /*!< UART Transmit Holding mask bit (8 bits) */
  93. /**
  94. * @brief Macro defines for UARTn Divisor Latch LSB register
  95. */
  96. #define UART_LOAD_DLL(div) ((div) & 0xFF) /*!< Macro for loading least significant halfs of divisors */
  97. #define UART_DLL_MASKBIT ((uint8_t) 0xFF) /*!< Divisor latch LSB bit mask */
  98. /**
  99. * @brief Macro defines for UARTn Divisor Latch MSB register
  100. */
  101. #define UART_DLM_MASKBIT ((uint8_t) 0xFF) /*!< Divisor latch MSB bit mask */
  102. #define UART_LOAD_DLM(div) (((div) >> 8) & 0xFF) /*!< Macro for loading most significant halfs of divisors */
  103. /**
  104. * @brief Macro defines for UART interrupt enable register
  105. */
  106. #define UART_IER_RBRINT_EN ((uint32_t) (1 << 0)) /*!< RBR Interrupt enable*/
  107. #define UART_IER_THREINT_EN ((uint32_t) (1 << 1)) /*!< THR Interrupt enable*/
  108. #define UART_IER_RLSINT_EN ((uint32_t) (1 << 2)) /*!< RX line status interrupt enable*/
  109. #define UART_IER_MSINT_EN ((uint32_t) (1 << 3)) /*!< Modem status interrupt enable */
  110. #define UART_IER_CTSINT_EN ((uint32_t) (1 << 7)) /*!< CTS1 signal transition interrupt enable */
  111. #define UART_IER_ABEOINT_EN ((uint32_t) (1 << 8)) /*!< Enables the end of auto-baud interrupt */
  112. #define UART_IER_ABTOINT_EN ((uint32_t) (1 << 9)) /*!< Enables the auto-baud time-out interrupt */
  113. #define UART_IER_BITMASK ((uint32_t) (0x307)) /*!< UART interrupt enable register bit mask */
  114. #define UART1_IER_BITMASK ((uint32_t) (0x38F)) /*!< UART1 interrupt enable register bit mask */
  115. /**
  116. * @brief Macro defines for UART interrupt identification register
  117. */
  118. #define UART_IIR_INTSTAT_PEND ((uint32_t) (1 << 0)) /*!<Interrupt Status - Active low */
  119. #define UART_IIR_INTID_RLS ((uint32_t) (3 << 1)) /*!<Interrupt identification: Receive line status*/
  120. #define UART_IIR_INTID_RDA ((uint32_t) (2 << 1)) /*!<Interrupt identification: Receive data available*/
  121. #define UART_IIR_INTID_CTI ((uint32_t) (6 << 1)) /*!<Interrupt identification: Character time-out indicator*/
  122. #define UART_IIR_INTID_THRE ((uint32_t) (1 << 1)) /*!<Interrupt identification: THRE interrupt*/
  123. #define UART_IIR_INTID_MODEM ((uint32_t) (0 << 1)) /*!<Interrupt identification: Modem interrupt*/
  124. #define UART_IIR_INTID_MASK ((uint32_t) (7 << 1)) /*!<Interrupt identification: Interrupt ID mask */
  125. #define UART_IIR_FIFO_EN ((uint32_t) (3 << 6)) /*!<These bits are equivalent to UnFCR[0] */
  126. #define UART_IIR_ABEO_INT ((uint32_t) (1 << 8)) /*!< End of auto-baud interrupt */
  127. #define UART_IIR_ABTO_INT ((uint32_t) (1 << 9)) /*!< Auto-baud time-out interrupt */
  128. #define UART_IIR_BITMASK ((uint32_t) (0x3CF)) /*!< UART interrupt identification register bit mask */
  129. /**
  130. * @brief Macro defines for UART FIFO control register
  131. */
  132. #define UART_FCR_FIFO_EN ((uint8_t) (1 << 0)) /*!< UART FIFO enable */
  133. #define UART_FCR_RX_RS ((uint8_t) (1 << 1)) /*!< UART FIFO RX reset */
  134. #define UART_FCR_TX_RS ((uint8_t) (1 << 2)) /*!< UART FIFO TX reset */
  135. #define UART_FCR_DMAMODE_SEL ((uint8_t) (1 << 3)) /*!< UART DMA mode selection */
  136. #define UART_FCR_TRG_LEV0 ((uint8_t) (0)) /*!< UART FIFO trigger level 0: 1 character */
  137. #define UART_FCR_TRG_LEV1 ((uint8_t) (1 << 6)) /*!< UART FIFO trigger level 1: 4 character */
  138. #define UART_FCR_TRG_LEV2 ((uint8_t) (2 << 6)) /*!< UART FIFO trigger level 2: 8 character */
  139. #define UART_FCR_TRG_LEV3 ((uint8_t) (3 << 6)) /*!< UART FIFO trigger level 3: 14 character */
  140. #define UART_FCR_BITMASK ((uint8_t) (0xCF)) /*!< UART FIFO control bit mask */
  141. #define UART_TX_FIFO_SIZE (16)
  142. /**
  143. * @brief Macro defines for UART line control register
  144. */
  145. #define UART_LCR_WLEN5 ((uint8_t) (0)) /*!< UART 5 bit data mode */
  146. #define UART_LCR_WLEN6 ((uint8_t) (1 << 0)) /*!< UART 6 bit data mode */
  147. #define UART_LCR_WLEN7 ((uint8_t) (2 << 0)) /*!< UART 7 bit data mode */
  148. #define UART_LCR_WLEN8 ((uint8_t) (3 << 0)) /*!< UART 8 bit data mode */
  149. #define UART_LCR_SBS_1BIT ((uint8_t) (0 << 2)) /*!< UART One Stop Bit Select */
  150. #define UART_LCR_SBS_2BIT ((uint8_t) (1 << 2)) /*!< UART Two Stop Bits Select */
  151. #define UART_LCR_PARITY_EN ((uint8_t) (1 << 3)) /*!< UART Parity Enable */
  152. #define UART_LCR_PARITY_DIS ((uint8_t) (0 << 3)) /*!< UART Parity Disable */
  153. #define UART_LCR_PARITY_ODD ((uint8_t) (0)) /*!< UART Odd Parity Select */
  154. #define UART_LCR_PARITY_EVEN ((uint8_t) (1 << 4)) /*!< UART Even Parity Select */
  155. #define UART_LCR_PARITY_F_1 ((uint8_t) (2 << 4)) /*!< UART force 1 stick parity */
  156. #define UART_LCR_PARITY_F_0 ((uint8_t) (3 << 4)) /*!< UART force 0 stick parity */
  157. #define UART_LCR_BREAK_EN ((uint8_t) (1 << 6)) /*!< UART Transmission Break enable */
  158. #define UART_LCR_DLAB_EN ((uint8_t) (1 << 7)) /*!< UART Divisor Latches Access bit enable */
  159. #define UART_LCR_BITMASK ((uint8_t) (0xFF)) /*!< UART line control bit mask */
  160. /**
  161. * @brief Macro defines for UART Modem control register
  162. */
  163. #define UART_MCR_DTR_CTRL ((uint8_t) (1 << 0)) /*!< Source for modem output pin DTR */
  164. #define UART_MCR_RTS_CTRL ((uint8_t) (1 << 1)) /*!< Source for modem output pin RTS */
  165. #define UART_MCR_LOOPB_EN ((uint8_t) (1 << 4)) /*!< Loop back mode select */
  166. #define UART_MCR_AUTO_RTS_EN ((uint8_t) (1 << 6)) /*!< Enable Auto RTS flow-control */
  167. #define UART_MCR_AUTO_CTS_EN ((uint8_t) (1 << 7)) /*!< Enable Auto CTS flow-control */
  168. #define UART_MCR_BITMASK ((uint8_t) (0x0F3)) /*!< UART1 bit mask value */
  169. /**
  170. * @brief Macro defines for UART line status register
  171. */
  172. #define UART_LSR_RDR ((uint8_t) (1 << 0)) /*!<Line status register: Receive data ready*/
  173. #define UART_LSR_OE ((uint8_t) (1 << 1)) /*!<Line status register: Overrun error*/
  174. #define UART_LSR_PE ((uint8_t) (1 << 2)) /*!<Line status register: Parity error*/
  175. #define UART_LSR_FE ((uint8_t) (1 << 3)) /*!<Line status register: Framing error*/
  176. #define UART_LSR_BI ((uint8_t) (1 << 4)) /*!<Line status register: Break interrupt*/
  177. #define UART_LSR_THRE ((uint8_t) (1 << 5)) /*!<Line status register: Transmit holding register empty*/
  178. #define UART_LSR_TEMT ((uint8_t) (1 << 6)) /*!<Line status register: Transmitter empty*/
  179. #define UART_LSR_RXFE ((uint8_t) (1 << 7)) /*!<Error in RX FIFO*/
  180. #define UART_LSR_BITMASK ((uint8_t) (0xFF)) /*!<UART Line status bit mask */
  181. /**
  182. * @brief Macro defines for UART Modem status register
  183. */
  184. #define UART_MSR_DELTA_CTS ((uint8_t) (1 << 0)) /*!< Set upon state change of input CTS */
  185. #define UART_MSR_DELTA_DSR ((uint8_t) (1 << 1)) /*!< Set upon state change of input DSR */
  186. #define UART_MSR_LO2HI_RI ((uint8_t) (1 << 2)) /*!< Set upon low to high transition of input RI */
  187. #define UART_MSR_DELTA_DCD ((uint8_t) (1 << 3)) /*!< Set upon state change of input DCD */
  188. #define UART_MSR_CTS ((uint8_t) (1 << 4)) /*!< Clear To Send State */
  189. #define UART_MSR_DSR ((uint8_t) (1 << 5)) /*!< Data Set Ready State */
  190. #define UART_MSR_RI ((uint8_t) (1 << 6)) /*!< Ring Indicator State */
  191. #define UART_MSR_DCD ((uint8_t) (1 << 7)) /*!< Data Carrier Detect State */
  192. #define UART_MSR_BITMASK ((uint8_t) (0xFF)) /*!< MSR register bit-mask value */
  193. /**
  194. * @brief Macro defines for UART Scratch Pad register
  195. */
  196. #define UART_SCR_BIMASK ((uint8_t) (0xFF)) /*!< UART Scratch Pad bit mask */
  197. /**
  198. * @brief Macro defines for UART Auto baudrate control register
  199. */
  200. #define UART_ACR_START ((uint32_t) (1 << 0)) /*!< UART Auto-baud start */
  201. #define UART_ACR_MODE ((uint32_t) (1 << 1)) /*!< UART Auto baudrate Mode 1 */
  202. #define UART_ACR_AUTO_RESTART ((uint32_t) (1 << 2)) /*!< UART Auto baudrate restart */
  203. #define UART_ACR_ABEOINT_CLR ((uint32_t) (1 << 8)) /*!< UART End of auto-baud interrupt clear */
  204. #define UART_ACR_ABTOINT_CLR ((uint32_t) (1 << 9)) /*!< UART Auto-baud time-out interrupt clear */
  205. #define UART_ACR_BITMASK ((uint32_t) (0x307)) /*!< UART Auto Baudrate register bit mask */
  206. /**
  207. * @brief Macro defines for UART IrDA control register
  208. */
  209. #define UART_ICR_IRDAEN ((uint32_t) (1 << 0)) /*!< IrDA mode enable */
  210. #define UART_ICR_IRDAINV ((uint32_t) (1 << 1)) /*!< IrDA serial input inverted */
  211. #define UART_ICR_FIXPULSE_EN ((uint32_t) (1 << 2)) /*!< IrDA fixed pulse width mode */
  212. #define UART_ICR_PULSEDIV(n) ((uint32_t) ((n & 0x07) << 3)) /*!< PulseDiv - Configures the pulse when FixPulseEn = 1 */
  213. #define UART_ICR_BITMASK ((uint32_t) (0x3F)) /*!< UART IRDA bit mask */
  214. /**
  215. * @brief Macro defines for UART half duplex register
  216. */
  217. #define UART_HDEN_HDEN ((uint32_t) (1 << 0)) /*!< enable half-duplex mode*/
  218. /**
  219. * @brief Macro defines for UART smart card interface control register
  220. */
  221. #define UART_SCICTRL_SCIEN ((uint32_t) (1 << 0)) /*!< enable asynchronous half-duplex smart card interface*/
  222. #define UART_SCICTRL_NACKDIS ((uint32_t) (1 << 1)) /*!< NACK response is inhibited*/
  223. #define UART_SCICTRL_PROTSEL_T1 ((uint32_t) (1 << 2)) /*!< ISO7816-3 protocol T1 is selected*/
  224. #define UART_SCICTRL_TXRETRY(n) ((uint32_t) ((n & 0x07) << 5)) /*!< number of retransmission*/
  225. #define UART_SCICTRL_GUARDTIME(n) ((uint32_t) ((n & 0xFF) << 8)) /*!< Extra guard time*/
  226. /**
  227. * @brief Macro defines for UART synchronous control register
  228. */
  229. #define UART_SYNCCTRL_SYNC ((uint32_t) (1 << 0)) /*!< enable synchronous mode*/
  230. #define UART_SYNCCTRL_CSRC_MASTER ((uint32_t) (1 << 1)) /*!< synchronous master mode*/
  231. #define UART_SYNCCTRL_FES ((uint32_t) (1 << 2)) /*!< sample on falling edge*/
  232. #define UART_SYNCCTRL_TSBYPASS ((uint32_t) (1 << 3)) /*!< to be defined*/
  233. #define UART_SYNCCTRL_CSCEN ((uint32_t) (1 << 4)) /*!< continuous running clock enable (master mode only)*/
  234. #define UART_SYNCCTRL_STARTSTOPDISABLE ((uint32_t) (1 << 5)) /*!< do not send start/stop bit*/
  235. #define UART_SYNCCTRL_CCCLR ((uint32_t) (1 << 6)) /*!< stop continuous clock*/
  236. /**
  237. * @brief Macro defines for UART Fractional divider register
  238. */
  239. #define UART_FDR_DIVADDVAL(n) ((uint32_t) (n & 0x0F)) /*!< Baud-rate generation pre-scaler divisor */
  240. #define UART_FDR_MULVAL(n) ((uint32_t) ((n << 4) & 0xF0)) /*!< Baud-rate pre-scaler multiplier value */
  241. #define UART_FDR_BITMASK ((uint32_t) (0xFF)) /*!< UART Fractional Divider register bit mask */
  242. /**
  243. * @brief Macro defines for UART Tx Enable register
  244. */
  245. #define UART_TER1_TXEN ((uint8_t) (1 << 7)) /*!< Transmit enable bit */
  246. #define UART_TER1_BITMASK ((uint8_t) (0x80)) /*!< UART Transmit Enable Register bit mask */
  247. #define UART_TER2_TXEN ((uint8_t) (1 << 0)) /*!< Transmit enable bit */
  248. #define UART_TER2_BITMASK ((uint8_t) (0x01)) /*!< UART Transmit Enable Register bit mask */
  249. /**
  250. * @brief Macro defines for UART1 RS485 Control register
  251. */
  252. #define UART_RS485CTRL_NMM_EN ((uint32_t) (1 << 0)) /*!< RS-485/EIA-485 Normal Multi-drop Mode (NMM) is disabled */
  253. #define UART_RS485CTRL_RX_DIS ((uint32_t) (1 << 1)) /*!< The receiver is disabled */
  254. #define UART_RS485CTRL_AADEN ((uint32_t) (1 << 2)) /*!< Auto Address Detect (AAD) is enabled */
  255. #define UART_RS485CTRL_SEL_DTR ((uint32_t) (1 << 3)) /*!< If direction control is enabled (bit DCTRL = 1), pin DTR is
  256. used for direction control */
  257. #define UART_RS485CTRL_DCTRL_EN ((uint32_t) (1 << 4)) /*!< Enable Auto Direction Control */
  258. #define UART_RS485CTRL_OINV_1 ((uint32_t) (1 << 5)) /*!< This bit reverses the polarity of the direction
  259. control signal on the RTS (or DTR) pin. The direction control pin
  260. will be driven to logic "1" when the transmitter has data to be sent */
  261. #define UART_RS485CTRL_BITMASK ((uint32_t) (0x3F)) /*!< RS485 control bit-mask value */
  262. /**
  263. * @brief Macro defines for UART1 RS-485 Address Match register
  264. */
  265. #define UART_RS485ADRMATCH_BITMASK ((uint8_t) (0xFF)) /*!< Bit mask value */
  266. /**
  267. * @brief Macro defines for UART1 RS-485 Delay value register
  268. */
  269. #define UART_RS485DLY_BITMASK ((uint8_t) (0xFF)) /*!< Bit mask value */
  270. /**
  271. * @brief Macro defines for UART FIFO Level register
  272. */
  273. #define UART_FIFOLVL_RXFIFOLVL(n) ((uint32_t) (n & 0x0F)) /*!< Reflects the current level of the UART receiver FIFO */
  274. #define UART_FIFOLVL_TXFIFOLVL(n) ((uint32_t) ((n >> 8) & 0x0F)) /*!< Reflects the current level of the UART transmitter FIFO */
  275. #define UART_FIFOLVL_BITMASK ((uint32_t) (0x0F0F)) /*!< UART FIFO Level Register bit mask */
  276. /**
  277. * @brief Macro defines for Ring Buffer
  278. */
  279. #define UART_RING_BUFSIZE 256 /*!< buffer size definition */
  280. #define __BUF_MASK (UART_RING_BUFSIZE - 1) /*!< Buf mask */
  281. #define __BUF_IS_FULL(head, tail) ((tail & __BUF_MASK) == ((head + 1) & __BUF_MASK)) /*!< Check buf is full or not */
  282. #define __BUF_WILL_FULL(head, tail) ((tail & __BUF_MASK) == ((head + 2) & __BUF_MASK)) /*!< Check buf will be full in next receiving or not */
  283. #define __BUF_IS_EMPTY(head, tail) ((head & __BUF_MASK) == (tail & __BUF_MASK)) /*!< Check buf is empty */
  284. #define __BUF_RESET(bufidx) (bufidx = 0) /*!< Reset buf */
  285. #define __BUF_INCR(bufidx) (bufidx = (bufidx + 1) & __BUF_MASK) /*!< Increase buf */
  286. /**
  287. * @brief UART Ring buffer structure
  288. */
  289. typedef struct {
  290. __IO uint32_t tx_head; /*!< UART Tx ring buffer head index */
  291. __IO uint32_t tx_tail; /*!< UART Tx ring buffer tail index */
  292. __IO uint32_t rx_head; /*!< UART Rx ring buffer head index */
  293. __IO uint32_t rx_tail; /*!< UART Rx ring buffer tail index */
  294. __IO uint8_t tx[UART_RING_BUFSIZE]; /*!< UART Tx data ring buffer */
  295. __IO uint8_t rx[UART_RING_BUFSIZE]; /*!< UART Rx data ring buffer */
  296. } UART_RingBuffer_Type;
  297. /**
  298. * @brief UART Line Status Type definition
  299. */
  300. typedef enum {
  301. UART_LINESTAT_RDR = UART_LSR_RDR, /*!< Line status register: Receive data ready*/
  302. UART_LINESTAT_OE = UART_LSR_OE, /*!< Line status register: Overrun error*/
  303. UART_LINESTAT_PE = UART_LSR_PE, /*!< Line status register: Parity error*/
  304. UART_LINESTAT_FE = UART_LSR_FE, /*!< Line status register: Framing error*/
  305. UART_LINESTAT_BI = UART_LSR_BI, /*!< Line status register: Break interrupt*/
  306. UART_LINESTAT_THRE = UART_LSR_THRE, /*!< Line status register: Transmit holding register empty*/
  307. UART_LINESTAT_TEMT = UART_LSR_TEMT, /*!< Line status register: Transmitter empty*/
  308. UART_LINESTAT_RXFE = UART_LSR_RXFE /*!< Error in RX FIFO*/
  309. } IP_UART_LS_Type;
  310. /**
  311. * @brief UART End of Auto-baudrate type definition
  312. */
  313. typedef enum {
  314. UART_AUTOBAUD_INTSTAT_ABEO = UART_IIR_ABEO_INT, /*!< UART End of auto-baud interrupt */
  315. UART_AUTOBAUD_INTSTAT_ABTO = UART_IIR_ABTO_INT /*!< UART Auto-baud time-out interrupt */
  316. } IP_UART_ABEO_Type;
  317. /**
  318. * @brief UART Full modem - Signal states definition
  319. */
  320. typedef enum {
  321. INACTIVE = 0, /*!< In-active state */
  322. ACTIVE = !INACTIVE /*!< Active state */
  323. } IP_UART_SignalState;
  324. /**
  325. * @brief UART modem status type definition
  326. */
  327. typedef enum {
  328. UART_MODEM_STAT_DELTA_CTS = UART_MSR_DELTA_CTS, /*!< Set upon state change of input CTS */
  329. UART_MODEM_STAT_DELTA_DSR = UART_MSR_DELTA_DSR, /*!< Set upon state change of input DSR */
  330. UART_MODEM_STAT_LO2HI_RI = UART_MSR_LO2HI_RI, /*!< Set upon low to high transition of input RI */
  331. UART_MODEM_STAT_DELTA_DCD = UART_MSR_DELTA_DCD, /*!< Set upon state change of input DCD */
  332. UART_MODEM_STAT_CTS = UART_MSR_CTS, /*!< Clear To Send State */
  333. UART_MODEM_STAT_DSR = UART_MSR_DSR, /*!< Data Set Ready State */
  334. UART_MODEM_STAT_RI = UART_MSR_RI, /*!< Ring Indicator State */
  335. UART_MODEM_STAT_DCD = UART_MSR_DCD /*!< Data Carrier Detect State */
  336. } IP_UART_MODEM_STAT_type;
  337. /**
  338. * @brief Modem output pin type definition
  339. */
  340. typedef enum {
  341. UART_MODEM_PIN_DTR = 0, /*!< Source for modem output pin DTR */
  342. UART_MODEM_PIN_RTS /*!< Source for modem output pin RTS */
  343. } IP_UART_MODEM_PIN_Type;
  344. /**
  345. * @brief UART Modem mode type definition
  346. */
  347. typedef enum {
  348. UART_MODEM_MODE_LOOPBACK = 0, /*!< Loop back mode select */
  349. UART_MODEM_MODE_AUTO_RTS, /*!< Enable Auto RTS flow-control */
  350. UART_MODEM_MODE_AUTO_CTS /*!< Enable Auto CTS flow-control */
  351. } IP_UART_MODEM_MODE_Type;
  352. /**
  353. * @brief UART Interrupt Type definitions
  354. */
  355. typedef enum {
  356. UART_INTCFG_RBR = 0, /*!< RBR Interrupt enable*/
  357. UART_INTCFG_THRE, /*!< THR Interrupt enable*/
  358. UART_INTCFG_RLS, /*!< RX line status interrupt enable*/
  359. UART_INTCFG_MS, /*!< Modem status interrupt enable */
  360. UART_INTCFG_CTS, /*!< CTS1 signal transition interrupt enable */
  361. UART_INTCFG_ABEO, /*!< Enables the end of auto-baud interrupt */
  362. UART_INTCFG_ABTO /*!< Enables the auto-baud time-out interrupt */
  363. } UART_INT_Type;
  364. /**
  365. * @brief UART Parity type definitions
  366. */
  367. typedef enum {
  368. UART_PARITY_NONE = 0, /*!< No parity */
  369. UART_PARITY_ODD = (4 << 3), /*!< Odd parity */
  370. UART_PARITY_EVEN = (5 << 3), /*!< Even parity */
  371. UART_PARITY_SP_1 = (6 << 3), /*!< Forced "1" stick parity */
  372. UART_PARITY_SP_0 = (7 << 3) /*!< Forced "0" stick parity */
  373. } UART_PARITY_Type;
  374. /**
  375. * @brief FIFO Level type definitions
  376. */
  377. typedef enum {
  378. UART_FIFO_TRGLEV0 = 0, /*!< UART FIFO trigger level 0: 1 character */
  379. UART_FIFO_TRGLEV1, /*!< UART FIFO trigger level 1: 4 character */
  380. UART_FIFO_TRGLEV2, /*!< UART FIFO trigger level 2: 8 character */
  381. UART_FIFO_TRGLEV3 /*!< UART FIFO trigger level 3: 14 character */
  382. } UART_FITO_LEVEL_Type;
  383. /**
  384. * @brief UART Stop bit type definitions
  385. */
  386. typedef enum {
  387. UART_STOPBIT_1 = 0, /*!< UART One Stop Bit Select */
  388. UART_STOPBIT_2 = (1 << 2) /*!< UART Two Stop Bits Select */
  389. } UART_STOPBIT_Type;
  390. /**
  391. * @brief UART Databit type definitions
  392. */
  393. typedef enum {
  394. UART_DATABIT_5 = 0, /*!< UART 5 bit data mode */
  395. UART_DATABIT_6, /*!< UART 6 bit data mode */
  396. UART_DATABIT_7, /*!< UART 7 bit data mode */
  397. UART_DATABIT_8 /*!< UART 8 bit data mode */
  398. } UART_DATABIT_Type;
  399. /**
  400. * @brief UART ID
  401. */
  402. typedef enum {
  403. UART_0 = 0,
  404. UART_1,
  405. UART_2,
  406. UART_3,
  407. UART_4,
  408. } UART_ID_Type;
  409. /**
  410. * @brief UART Interrupt Status
  411. */
  412. typedef enum {
  413. UART_ERROR = 1,
  414. READY_TO_SEND,
  415. READY_TO_RECEIVE = 4,
  416. } UART_Int_Status;
  417. /**
  418. * @brief UART FIFO Configuration Structure definition
  419. */
  420. typedef struct {
  421. FunctionalState FIFO_ResetRxBuf; /*!< Reset Rx FIFO command state , should be:
  422. - ENABLE: Reset Rx FIFO in UART
  423. - DISABLE: Do not reset Rx FIFO in UART
  424. */
  425. FunctionalState FIFO_ResetTxBuf; /*!< Reset Tx FIFO command state , should be:
  426. - ENABLE: Reset Tx FIFO in UART
  427. - DISABLE: Do not reset Tx FIFO in UART
  428. */
  429. FunctionalState FIFO_DMAMode; /*!< DMA mode, should be:
  430. - ENABLE: Enable DMA mode in UART
  431. - DISABLE: Disable DMA mode in UART
  432. */
  433. UART_FITO_LEVEL_Type FIFO_Level; /*!< Rx FIFO trigger level, should be:
  434. - UART_FIFO_TRGLEV0: UART FIFO trigger level 0: 1 character
  435. - UART_FIFO_TRGLEV1: UART FIFO trigger level 1: 4 character
  436. - UART_FIFO_TRGLEV2: UART FIFO trigger level 2: 8 character
  437. - UART_FIFO_TRGLEV3: UART FIFO trigger level 3: 14 character
  438. */
  439. } UART_FIFO_CFG_Type;
  440. /**
  441. * @brief Initializes the UARTx peripheral according to the specified parameters in the UART_ConfigStruct.
  442. * @param LPC_UART : Pointer to selected UARTx peripheral
  443. * @param UARTPort : UART ID type
  444. * @return Nothing
  445. */
  446. void IP_UART_Init(IP_USART_001_Type *LPC_UART, UART_ID_Type UARTPort);
  447. /**
  448. * @brief De-initializes the UARTx peripheral registers to their default reset values.
  449. * @param LPC_UART : Pointer to selected UARTx peripheral
  450. * @param UARTPort : UART ID type
  451. * @return Nothing
  452. */
  453. void IP_UART_DeInit(IP_USART_001_Type *LPC_UART, UART_ID_Type UARTPort);
  454. /**
  455. * @brief Determines best dividers to get a target clock rate
  456. * @param LPC_UART : Pointer to selected UARTx peripheral
  457. * @param baudrate : Desired UART baud rate.
  458. * @param uClk : Current Uart Block Clock.
  459. * @return Error status, could be SUCCESS or ERROR
  460. */
  461. Status IP_UART_SetBaud(IP_USART_001_Type *LPC_UART, uint32_t baudrate, uint32_t uClk);
  462. /**
  463. * @brief Configure data width, parity mode and stop bits
  464. * @param LPC_UART : Pointer to selected UARTx peripheral
  465. * @param Databits : UART Data width, should be:
  466. * UART_DATABIT_5: UART 5 bit data mode
  467. * UART_DATABIT_6: UART 6 bit data mode
  468. * UART_DATABIT_7: UART 7 bit data mode
  469. * UART_DATABIT_8: UART 8 bit data mode
  470. * @param Parity : UART Parity mode, should be:
  471. * UART_PARITY_NONE: No parity
  472. * UART_PARITY_ODD: Odd parity
  473. * UART_PARITY_EVEN: Even parity
  474. * UART_PARITY_SP_1: Forced "1" stick parity
  475. * UART_PARITY_SP_0: Forced "0" stick parity
  476. * @param Stopbits : Number of stop bits, should be:
  477. * UART_STOPBIT_1: One Stop Bit Select
  478. * UART_STOPBIT_2: Two Stop Bits Select
  479. * @return Nothing
  480. */
  481. void IP_UART_ConfigData(IP_USART_001_Type *LPC_UART,
  482. UART_DATABIT_Type Databits,
  483. UART_PARITY_Type Parity,
  484. UART_STOPBIT_Type Stopbits);
  485. /* UART Send/Receive functions -------------------------------------------------*/
  486. /**
  487. * @brief Transmit a single data through UART peripheral
  488. * @param LPC_UART : Pointer to selected UARTx peripheral
  489. * @param Data : Data to transmit (must be 8-bit long)
  490. * @return Status, should be ERROR (THR is empty, ready to send) or SUCCESS (THR is not empty)
  491. */
  492. Status IP_UART_SendByte(IP_USART_001_Type *LPC_UART, uint8_t Data);
  493. /**
  494. * @brief Receive a single data from UART peripheral
  495. * @param LPC_UART : Pointer to selected UARTx peripheral
  496. * @param *Data : Pointer to Data to receive (must be 8-bit long)
  497. * @return Status, should be ERROR or (Receive data is ready) or SUCCESS (Receive data is not ready yet)
  498. */
  499. Status IP_UART_ReceiveByte(IP_USART_001_Type *LPC_UART, uint8_t *Data);
  500. /**
  501. * @brief Send a block of data via UART peripheral
  502. * @param LPC_UART : Pointer to selected UARTx peripheral
  503. * @param txbuf : Pointer to Transmit buffer
  504. * @param buflen : Length of Transmit buffer
  505. * @param flag : Flag used in UART transfer, should be NONE_BLOCKING or BLOCKING
  506. * @return Number of bytes sent
  507. *
  508. * Note: when using UART in BLOCKING mode, a time-out condition is used
  509. * via defined symbol UART_BLOCKING_TIMEOUT.
  510. */
  511. uint32_t IP_UART_Send(IP_USART_001_Type *LPC_UART, uint8_t *txbuf, uint32_t buflen, TRANSFER_BLOCK_Type flag);
  512. /**
  513. * @brief Receive a block of data via UART peripheral
  514. * @param LPC_UART : Pointer to selected UARTx peripheral
  515. * @param rxbuf : Pointer to Received buffer
  516. * @param buflen : Length of Received buffer
  517. * @param flag : Flag mode, should be NONE_BLOCKING or BLOCKING
  518. * @return Number of bytes received
  519. *
  520. * Note: when using UART in BLOCKING mode, a time-out condition is used
  521. * via defined symbol UART_BLOCKING_TIMEOUT.
  522. */
  523. uint32_t IP_UART_Receive(IP_USART_001_Type *LPC_UART, uint8_t *rxbuf, uint32_t buflen, TRANSFER_BLOCK_Type flag);
  524. /* UART operate functions -------------------------------------------------------*/
  525. /**
  526. * @brief Enable or disable specified UART interrupt.
  527. * @param LPC_UART : Pointer to selected UARTx peripheral
  528. * @param UARTIntCfg : Specifies the interrupt flag, should be one of the following:
  529. * - UART_INTCFG_RBR : RBR Interrupt enable
  530. * - UART_INTCFG_THRE : THR Interrupt enable
  531. * - UART_INTCFG_RLS : RX line status interrupt enable
  532. * - UART1_INTCFG_MS : Modem status interrupt enable (UART1 only)
  533. * - UART1_INTCFG_CTS : CTS1 signal transition interrupt enable (UART1 only)
  534. * - UART_INTCFG_ABEO : Enables the end of auto-baud interrupt
  535. * - UART_INTCFG_ABTO : Enables the auto-baud time-out interrupt
  536. * @param NewState : New state of specified UART interrupt type, should be:
  537. * - ENALBE : Enable this UART interrupt type
  538. * - DISALBE : Disable this UART interrupt type
  539. * @return Nothing
  540. */
  541. void IP_UART_IntConfig(IP_USART_001_Type *LPC_UART, UART_INT_Type UARTIntCfg, FunctionalState NewState);
  542. /**
  543. * @brief Get Source Interrupt
  544. * @param LPC_UART : Pointer to selected UARTx peripheral
  545. * @return Return the value of IIR register
  546. */
  547. uint32_t IP_UART_IntGetStatus(IP_USART_001_Type *LPC_UART);
  548. /**
  549. * @brief Force BREAK character on UART line, output pin UARTx TXD is forced to logic 0
  550. * @param LPC_UART : Pointer to selected UARTx peripheral
  551. * @return Nothing
  552. */
  553. void IP_UART_ForceBreak(IP_USART_001_Type *LPC_UART);
  554. /**
  555. * @brief Get current value of Line Status register in UART peripheral.
  556. * @param LPC_UART : Pointer to selected UARTx peripheral
  557. * @return Current value of Line Status register in UART peripheral
  558. *
  559. * Note: The return value of this function must be ANDed with each member in UART_LS_Type
  560. * enumeration to determine current flag status corresponding to each Line status type. Because
  561. * some flags in Line Status register will be cleared after reading, the next reading Line
  562. * Status register could not be correct. So this function used to read Line status register
  563. * in one time only, then the return value used to check all flags.
  564. */
  565. uint8_t IP_UART_GetLineStatus(IP_USART_001_Type *LPC_UART);
  566. /**
  567. * @brief Check whether if UART is busy or not
  568. * @param LPC_UART : Pointer to selected UARTx peripheral
  569. * @return RESET if UART is not busy, otherwise return SET.
  570. */
  571. FlagStatus IP_UART_CheckBusy(IP_USART_001_Type *LPC_UART);
  572. /**
  573. * @brief Enable/Disable transmission on UART TxD pin
  574. * @param LPC_UART : Pointer to selected UARTx peripheral
  575. * @param UARTPort : UART ID type
  576. * @param NewState : New State of Tx transmission function, should be ENABLE or DISABLE
  577. * @return Nothing
  578. */
  579. void IP_UART_TxCmd(IP_USART_001_Type *LPC_UART, UART_ID_Type UARTPort, FunctionalState NewState);
  580. /* UART FIFO functions ----------------------------------------------------------*/
  581. /**
  582. * @brief Configure FIFO function on selected UART peripheral
  583. * @param LPC_UART : Pointer to selected UARTx peripheral
  584. * @param FIFOCfg : Pointer to a UART_FIFO_CFG_Type Structure that contains specified information about FIFO configuration
  585. * @return Nothing
  586. */
  587. void IP_UART_FIFOConfig(IP_USART_001_Type *LPC_UART, UART_FIFO_CFG_Type *FIFOCfg);
  588. /**
  589. * @brief Fills each UART_FIFOInitStruct member with its default value:
  590. * - FIFO_DMAMode = DISABLE
  591. * - FIFO_Level = UART_FIFO_TRGLEV0
  592. * - FIFO_ResetRxBuf = ENABLE
  593. * - FIFO_ResetTxBuf = ENABLE
  594. * - FIFO_State = ENABLE
  595. * @param UART_FIFOInitStruct : Pointer to a UART_FIFO_CFG_Type structure which will be initialized.
  596. * @return Nothing
  597. */
  598. void IP_UART_FIFOConfigStructInit(UART_FIFO_CFG_Type *UART_FIFOInitStruct);
  599. /**
  600. * @}
  601. */
  602. #ifdef __cplusplus
  603. }
  604. #endif
  605. #endif /* __USART_001_H_ */