USART_LPC18xx.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /* --------------------------------------------------------------------------
  2. * Copyright (c) 2013-2016 ARM Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the License); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * $Date: 02. March 2016
  19. * $Revision: V2.5
  20. *
  21. * Project: USART Driver Definitions for NXP LPC18xx
  22. * -------------------------------------------------------------------------- */
  23. #ifndef __USART_LPC18XX_H
  24. #define __USART_LPC18XX_H
  25. #include "LPC18xx.h"
  26. #include "Driver_USART.h"
  27. #include "SCU_LPC18xx.h"
  28. #include "GPDMA_LPC18xx.h"
  29. // Clock Control Unit register
  30. #define CCU_CLK_CFG_RUN (1 << 0)
  31. #define CCU_CLK_CFG_AUTO (1 << 1)
  32. #define CCU_CLK_STAT_RUN (1 << 0)
  33. // USART register interface definitions
  34. // USART Divisor Latch register LSB
  35. #define USART_DLL_DLLSB_POS ( 0U)
  36. #define USART_DLL_DLLSB_MSK (0xFFU << USART_DLL_DLLSB_POS)
  37. // USART Divisor Latch register MSB
  38. #define USART_DLM_DLMSB_POS ( 0U)
  39. #define USART_DLM_DLMSB_MSK (0xFFU << USART_DLM_DLMSB_POS)
  40. // USART Interrupt enable register
  41. #define USART_IER_RBRIE (1U << 0)
  42. #define USART_IER_THREIE (1U << 1)
  43. #define USART_IER_RXIE (1U << 2)
  44. #define UART_IER_MSIE (1U << 3) // Only for UART1 - modem status interrupt enable
  45. #define USART_IER_ABEOINTEN (1U << 8)
  46. #define USART_IER_ABTOINTEN (1U << 9)
  47. // USART Interrupt identification register
  48. #define USART_IIR_INTSTATUS (1U << 0)
  49. #define USART_IIR_INTID_POS ( 1U)
  50. #define USART_IIR_INTID_MSK (7U << USART_IIR_INTID_POS)
  51. #define USART_IIR_FIFOENABLE_POS ( 6U)
  52. #define USART_IIR_FIFOENABLE_MSK (3U << USART_IIR_FIFOENABLE_POS)
  53. #define USART_IIR_ABEOINT (1U << 8)
  54. #define USART_IIR_ABTOINT (1U << 9)
  55. #define USART_IIR_INTID_RLS (3U << USART_IIR_INTID_POS)
  56. #define USART_IIR_INTID_RDA (2U << USART_IIR_INTID_POS)
  57. #define USART_IIR_INTID_CTI (6U << USART_IIR_INTID_POS)
  58. #define USART_IIR_INTID_THRE (1U << USART_IIR_INTID_POS)
  59. #define UART_IIR_INTID_MS (0U << USART_IIR_INTID_POS) // UART1 only
  60. // USART FIFO control register
  61. #define USART_FCR_FIFOEN (1U << 0)
  62. #define USART_FCR_RXFIFORES (1U << 1)
  63. #define USART_FCR_TXFIFORES (1U << 2)
  64. #define USART_FCR_DMAMODE (1U << 3)
  65. #define USART_FCR_RXTRIGLVL_POS ( 6U)
  66. #define USART_FCR_RXTRIGLVL_MSK (3U << USART_FCR_RXTRIGLVL_POS)
  67. // USART Line control register
  68. #define USART_LCR_WLS_POS ( 0U)
  69. #define USART_LCR_WLS_MSK (3U << USART_LCR_WLS_POS)
  70. #define USART_LCR_SBS (1U << 2)
  71. #define USART_LCR_PE (1U << 3)
  72. #define USART_LCR_PS_POS ( 4U)
  73. #define USART_LCR_PS_MSK (3U << USART_LCR_PS_POS)
  74. #define USART_LCR_BC (1U << 6)
  75. #define USART_LCR_DLAB (1U << 7)
  76. // USART Line status register
  77. #define USART_LSR_RDR (1U << 0)
  78. #define USART_LSR_OE (1U << 1)
  79. #define USART_LSR_PE (1U << 2)
  80. #define USART_LSR_FE (1U << 3)
  81. #define USART_LSR_BI (1U << 4)
  82. #define USART_LSR_THRE (1U << 5)
  83. #define USART_LSR_TEMT (1U << 6)
  84. #define USART_LSR_RXFE (1U << 7)
  85. #define USART_LSR_TXERR (1U << 8)
  86. #define USART_LSR_LINE_INT (USART_LSR_OE | USART_LSR_PE | USART_LSR_FE | USART_LSR_BI)
  87. // USART IrDA control register
  88. #define USART_ICR_IRDAEN (1U << 0)
  89. #define USART_ICR_FIXPULSEEN (1U << 1)
  90. #define USART_ICR_IRDAINV (1U << 2)
  91. #define USART_ICR_PULSEDIV_POS ( 3U)
  92. #define USART_ICR_PULSEDIV_MSK (7U << USART_ICR_PULSEDIV_POS)
  93. // USART Fractional divider register
  94. #define USART_FDR_DIVADDVAL_POS ( 0U)
  95. #define USART_FDR_DIVADDVAL_MSK (0x0FU << USART_FDR_DIVADDVAL_POS)
  96. #define USART_FDR_MULVAL_POS ( 4U)
  97. #define USART_FDR_MULVAL_MSK (0x0FU << USART_FDR_MULVAL_POS)
  98. // USART oversampling register
  99. #define USART_OSR_OSFRAC_POS ( 1U)
  100. #define USART_OSR_OSFRAC_MSK (7U << USART_OSR_OSFRAC_POS)
  101. #define USART_OSR_OSINT_POS ( 4U)
  102. #define USART_OSR_OSINT_MSK (0x0FU << USART_OSR_OSINT_POS)
  103. #define USART_OSR_FDINT_POS ( 8U)
  104. #define USART_OSR_FDINT_MSK (0x7FU << USART_OSR_FDINT_MSK)
  105. // USART Half duplex enable register
  106. #define USART_HDEN_HDEN (1U << 0U)
  107. // USART SmartCard interface control register
  108. #define USART_SCICTRL_SCIEN (1U << 0)
  109. #define USART_SCICTRL_NACKDIS (1U << 1)
  110. #define USART_SCICTRL_PROTSEL (1U << 2)
  111. #define USART_SCICTRL_TXRETRY_POS ( 5U)
  112. #define USART_SCICTRL_TXRETRY_MSK (7U << USART_SCICTRL_TXRETRY_POS)
  113. #define USART_SCICTRL_GUARDTIME_POS ( 8U)
  114. #define USART_SCICTRL_GUARDTIME_MSK (0xFFU << USART_SCICTRL_GUARDTIME_POS)
  115. // USART Synchronous mode control register
  116. #define USART_SYNCCTRL_SYNC (1U << 0)
  117. #define USART_SYNCCTRL_CSRC (1U << 1)
  118. #define USART_SYNCCTRL_FES (1U << 2)
  119. #define USART_SYNCCTRL_TSBYPASS (1U << 3)
  120. #define USART_SYNCCTRL_CSCEN (1U << 4)
  121. #define USART_SYNCCTRL_SSSDIS (1U << 5)
  122. #define USART_SYNCCTRL_CCCLR (1U << 6)
  123. // UART Modem control register
  124. #define UART_MCR_DTRCTRL (1U << 0)
  125. #define UART_MCR_RTSCTRL (1U << 1)
  126. #define UART_MCR_LMS (1U << 4)
  127. #define UART_MCR_RTSEN (1U << 6)
  128. #define UART_MCR_CTSEN (1U << 7)
  129. // UART Modem status register
  130. #define UART_MSR_DCTS (1U << 0)
  131. #define UART_MSR_DDSR (1U << 1)
  132. #define UART_MSR_TERI (1U << 2)
  133. #define UART_MSR_DDCD (1U << 3)
  134. #define UART_MSR_CTS (1U << 4)
  135. #define UART_MSR_DSR (1U << 5)
  136. #define UART_MSR_RI (1U << 6)
  137. #define UART_MSR_DCD (1U << 7)
  138. // USART RS485 control register
  139. #define USART_RS485CTRL_NMMEN (1U << 0)
  140. #define USART_RS485CTRL_RXDIS (1U << 1)
  141. #define USART_RS485CTRL_AADEN (1U << 2)
  142. #define USART_RS485CTRL_DCTRL (1U << 4)
  143. #define USART_RS485CTRL_OINV (1U << 5)
  144. // USART Transmitter enable register
  145. #define USART_TER_TXEN (1U << 0)
  146. // USART flags
  147. #define USART_FLAG_INITIALIZED (1U << 0)
  148. #define USART_FLAG_POWERED (1U << 1)
  149. #define USART_FLAG_CONFIGURED (1U << 2)
  150. #define USART_FLAG_TX_ENABLED (1U << 3)
  151. #define USART_FLAG_RX_ENABLED (1U << 4)
  152. #define USART_FLAG_SEND_ACTIVE (1U << 5)
  153. // USART synchronous xfer modes
  154. #define USART_SYNC_MODE_TX ( 1U )
  155. #define USART_SYNC_MODE_RX ( 2U )
  156. #define USART_SYNC_MODE_TX_RX (USART_SYNC_MODE_TX | \
  157. USART_SYNC_MODE_RX)
  158. #define FRACT_BITS ( 12U )
  159. #define FRACT_MASK (0XFFFU)
  160. #define FIXED_OVERSAMPLING_DIVIDER_LIMIT (51U << FRACT_BITS)
  161. #define INTEGER_OVERSAMPLING_DIVIDER_LIMIT ((12U << FRACT_BITS) + (8 << FRACT_BITS) / 10)
  162. // Baudrate accepted error
  163. #define USART_MAX_BAUDRATE_ERROR ( 3U )
  164. #define USART_MAX_DIVIDER_ERROR ( 3U )
  165. // USART TX FIFO trigger level
  166. #define USART_TRIG_LVL_1 (0x00U)
  167. #define USART_TRIG_LVL_4 (0x40U)
  168. #define USART_TRIG_LVL_8 (0x80U)
  169. #define USART_TRIG_LVL_14 (0xC0U)
  170. #define FRACT_DIV(add, mul) { ((uint16_t)((1U << 12) + (((uint32_t)(add << 24) / (mul)) >> 12))), ((uint8_t) (((mul) << 4) | add)), }
  171. typedef struct _FRACT_DIV {
  172. uint16_t val;
  173. uint8_t add_mul;
  174. } FRACT_DIVIDER;
  175. // USART Transfer Information (Run-Time)
  176. typedef struct _USART_TRANSFER_INFO {
  177. uint32_t rx_num; // Total number of data to be received
  178. uint32_t tx_num; // Total number of data to be send
  179. uint8_t *rx_buf; // Pointer to in data buffer
  180. uint8_t *tx_buf; // Pointer to out data buffer
  181. uint32_t rx_cnt; // Number of data received
  182. uint32_t tx_cnt; // Number of data sent
  183. uint8_t tx_def_val; // Transmit default value (used in USART_SYNC_MASTER_MODE_RX)
  184. uint8_t rx_dump_val; // Receive dump value (used in USART_SYNC_MASTER_MODE_TX)
  185. uint8_t send_active; // Send active flag
  186. uint8_t sync_mode; // Synchronous mode
  187. } USART_TRANSFER_INFO;
  188. typedef struct _USART_RX_STATUS {
  189. uint8_t rx_busy; // Receiver busy flag
  190. uint8_t rx_overflow; // Receive data overflow detected (cleared on start of next receive operation)
  191. uint8_t rx_break; // Break detected on receive (cleared on start of next receive operation)
  192. uint8_t rx_framing_error; // Framing error detected on receive (cleared on start of next receive operation)
  193. uint8_t rx_parity_error; // Parity error detected on receive (cleared on start of next receive operation)
  194. } USART_RX_STATUS;
  195. // USART Information (Run-Time)
  196. typedef struct _USART_INFO {
  197. ARM_USART_SignalEvent_t cb_event; // Event callback
  198. USART_RX_STATUS rx_status; // Receive status flags
  199. USART_TRANSFER_INFO xfer; // Transfer information
  200. uint8_t mode; // USART mode
  201. uint8_t flags; // USART driver flags
  202. uint32_t baudrate; // Baudrate
  203. } USART_INFO;
  204. // USART DMA
  205. typedef const struct _USART_DMA {
  206. uint8_t channel; // DMA Channel
  207. uint8_t peripheral; // DMA mux
  208. uint8_t peripheral_sel;// DMA mux selection
  209. GPDMA_SignalEvent_t cb_event; // DMA Event callback
  210. } USART_DMA;
  211. // USART Pin Configuration
  212. typedef const struct _USART_PINS {
  213. PIN_ID *tx; // TX Pin identifier
  214. PIN_ID *rx; // RX Pin identifier
  215. PIN_ID *clk; // CLK Pin identifier
  216. PIN_ID *cts; // CTS Pin identifier
  217. PIN_ID *rts; // RTS Pin identifier
  218. PIN_ID *dcd; // DCD Pin identifier
  219. PIN_ID *dsr; // DSR Pin identifier
  220. PIN_ID *dtr; // DTR Pin identifier
  221. PIN_ID *ri; // RI Pin identifier
  222. } USART_PINS;
  223. // USART Clocks Configuration
  224. typedef const struct _USART_CLOCK {
  225. __IO uint32_t *reg_cfg; // USART register interface clock configuration register
  226. __I uint32_t *reg_stat; // USART register interface clock status register
  227. __IO uint32_t *peri_cfg; // USART peripheral clock configuration register
  228. __I uint32_t *peri_stat; // USART peripheral clock status register
  229. __IO uint32_t *base_clk; // USART base clock
  230. } USART_CLOCKS;
  231. // USART Reset Configuration
  232. typedef const struct _USART_RESET {
  233. uint32_t reg_cfg_val; // USART reset bit
  234. __IO uint32_t *reg_cfg; // USART reset control register
  235. __I uint32_t *reg_stat; // USART reset active status register
  236. } USART_RESET;
  237. // USART Resources definitions
  238. typedef struct {
  239. ARM_USART_CAPABILITIES capabilities; // Capabilities
  240. LPC_USARTn_Type *reg; // Pointer to USART peripheral
  241. LPC_UART1_Type *uart_reg; // Pointer to UART peripheral
  242. USART_PINS pins; // USART pins configuration
  243. USART_CLOCKS clk; // USART clocks configuration
  244. USART_RESET rst; // USART reset configuration
  245. IRQn_Type irq_num; // USART IRQ Number
  246. uint32_t trig_lvl; // FIFO Trigger level
  247. USART_DMA *dma_tx;
  248. USART_DMA *dma_rx;
  249. USART_INFO *info; // Run-Time Information
  250. float sc_oversamp; // SmartCard oversampling ratio
  251. } const USART_RESOURCES;
  252. #endif /* __USART_LPC18XX_H */