ch56x_uart.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-07-15 Emuzit first version
  9. */
  10. #ifndef __CH56X_UART_H__
  11. #define __CH56X_UART_H__
  12. #include "soc.h"
  13. #include "ch56x_gpio.h"
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. #define UART_RXD0_ALT GET_PIN(A, 5)
  18. #define UART_TXD0_ALT GET_PIN(A, 6)
  19. #define UART_RXD0_PIN GET_PIN(B, 5)
  20. #define UART_TXD0_PIN GET_PIN(B, 6)
  21. #define UART_RXD1_PIN GET_PIN(A, 7)
  22. #define UART_TXD1_PIN GET_PIN(A, 8)
  23. #define UART_RXD2_PIN GET_PIN(A, 2)
  24. #define UART_TXD2_PIN GET_PIN(A, 3)
  25. #define UART_RXD3_PIN GET_PIN(B, 3)
  26. #define UART_TXD3_PIN GET_PIN(B, 4)
  27. #ifndef UART_FIFO_SIZE
  28. #define UART_FIFO_SIZE 8
  29. #endif
  30. #ifndef UART_RECV_RDY_SZ
  31. #define UART_RECV_RDY_SZ 7 // FIFO trigger level for rx data available
  32. #endif
  33. union _uart_mcr
  34. {
  35. uint8_t reg;
  36. struct
  37. {
  38. uint8_t dtr : 1; // B.0 : RW, DTR output (UART0 only)
  39. uint8_t rts : 1; // B.1 : RW, RTS output (UART0 only)
  40. uint8_t out1 : 1; // B.2 : RW, user defined modem control (UART0 only)
  41. uint8_t int_oe : 1; // B.3 : RW, interrupt output enable / OUT2
  42. uint8_t loop : 1; // B.4 : RW, enable internal loop test (UART0 only)
  43. uint8_t au_flow_en : 1; // B.5 : RW, enable CTS/RTS autoflow control
  44. uint8_t tnow : 1; // B.6 : RW, enable DTR TNOW output (UART0 only)
  45. uint8_t half : 1; // B.7 : RW, enable half-duplex mode (UART0 only)
  46. };
  47. };
  48. #define RB_MCR_DTR 0x01
  49. #define RB_MCR_RTS 0x02
  50. #define RB_MCR_OUT1 0x04
  51. #define RB_MCR_OUT2 0x08
  52. #define RB_MCR_INT_OE 0x08
  53. #define RB_MCR_LOOP 0x10
  54. #define RB_MCR_AU_FLOW_EN 0x20
  55. #define RB_MCR_TNOW 0x40
  56. #define RB_MCR_HALF 0x80
  57. union _uart_ier
  58. {
  59. uint8_t reg;
  60. struct
  61. {
  62. uint8_t recv_rdy : 1; // B.0 : RW, enable rx data ready intr
  63. uint8_t thr_empty : 1; // B.1 : RW, enable THR empty intr
  64. uint8_t line_stat : 1; // B.2 : RW, enable rx line status intr
  65. uint8_t modem_chg : 1; // B.3 : RW, enable modem status change intr (UART0 only)
  66. uint8_t dtr_en : 1; // B.4 : RW, DTR/TNOW output pin enable (UART0 only)
  67. uint8_t rts_en : 1; // B.5 : RW, RTS output pin enable (UART0 only)
  68. uint8_t txd_en : 1; // B.6 : RW, TXD pin enable
  69. uint8_t reset : 1; // B.7 : WZ, software reset control, active high, auto clear
  70. };
  71. };
  72. #define RB_IER_RECV_RDY 0x01
  73. #define RB_IER_THR_EMPTY 0x02
  74. #define RB_IER_LINE_STAT 0x04
  75. #define RB_IER_MODEM_CHG 0x08
  76. #define RB_IER_DTR_EN 0x10
  77. #define RB_IER_RTS_EN 0x20
  78. #define RB_IER_TXD_EN 0x40
  79. #define RB_IER_RESET 0x80
  80. union _uart_fcr
  81. {
  82. uint8_t reg;
  83. struct
  84. {
  85. uint8_t fifo_en : 1; // B.0 : RW, FIFO enable
  86. uint8_t rx_fifo_clr : 1; // B.1 : WZ, write 1 to clear rx FIFO, auto clear
  87. uint8_t tx_fifo_clr : 1; // B.2 : WZ, write 1 to clear tx FIFO, auto clear
  88. uint8_t resv_3 : 3;
  89. uint8_t fifo_trig : 2; // B.7-6 : RW, rx FIFO trigger level, 1/2/4/7 bytes
  90. };
  91. };
  92. #define RB_FCR_FIFO_EN 0x01
  93. #define RB_FCR_RX_FIFO_CLR 0x02
  94. #define RB_FCR_TX_FIFO_CLR 0x04
  95. #define RB_FCR_FIFO_TRIG 0xc0
  96. #define UART_1BYTE_TRIG 0
  97. #define UART_2BYTE_TRIG 1
  98. #define UART_4BYTE_TRIG 2
  99. #define UART_7BYTE_TRIG 3
  100. union _uart_lcr
  101. {
  102. uint8_t reg;
  103. struct
  104. {
  105. uint8_t word_sz : 2; // B.1-0 : RW, word bit length, 5/6/7/8 bits
  106. uint8_t stop_bit : 1; // B.2 : RW, stop bit length, 1/2 bits
  107. uint8_t par_en : 1; // B.3 : RW, parity enable
  108. uint8_t par_mod : 2; // B.5-4 : RW, parity mode, odd/even/mark/space
  109. uint8_t break_en : 1; // B.6 : RW, force BREAK line condition
  110. uint8_t dlab : 1; // B.7 : RW, user defined general purpose bit
  111. };
  112. };
  113. #define RB_LCR_WORD_SZ 0x03
  114. #define RB_LCR_STOP_BIT 0x04
  115. #define RB_LCR_PAR_EN 0x08
  116. #define RB_LCR_PAR_MOD 0x30
  117. #define RB_LCR_BREAK_EN 0x40
  118. #define RB_LCR_DLAB 0x80
  119. #define RB_LCR_GP_BIT 0x80
  120. #define LCR_DATA_BITS_5 0
  121. #define LCR_DATA_BITS_6 1
  122. #define LCR_DATA_BITS_7 2
  123. #define LCR_DATA_BITS_8 3
  124. #define LCR_STOP_BITS_1 0
  125. #define LCR_STOP_BITS_2 1
  126. #define LCR_PARITY_ODD 0
  127. #define LCR_PARITY_EVEN 1
  128. #define LCR_PARITY_MARK 2
  129. #define LCR_PARITY_SPACE 3
  130. union _uart_iir
  131. {
  132. uint8_t reg;
  133. struct
  134. {
  135. uint8_t int_mask : 4; // B.3-0 : RO, interrupt mask (intr if B.0 is 0)
  136. uint8_t resv_4 : 2;
  137. uint8_t fifo_id : 2; // B.7-6 : RO, FIFO enabled flag
  138. };
  139. };
  140. #define RB_IIR_NO_INT 0x01
  141. #define RB_IIR_INT_MASK 0x0f
  142. #define RB_IIR_FIFO_ID 0xc0
  143. /* RB_IIR_INT_MASK (IIR bits 3:0) definition
  144. */
  145. #define UART_II_SLV_ADDR 0x0e // UART0 slave address match interrupt
  146. #define UART_II_LINE_STAT 0x06 // rx line status interrupt
  147. #define UART_II_RECV_RDY 0x04 // rx data available interrupt
  148. #define UART_II_RECV_TOUT 0x0c // rx fifo timeout interrupt
  149. #define UART_II_THR_EMPTY 0x02 // THR empty interrupt
  150. #define UART_II_MODEM_CHG 0x00 // UART0 modem status change interrupt
  151. #define UART_II_NO_INTER 0x01 // no interrupt pending
  152. union _uart_lsr
  153. {
  154. uint8_t reg;
  155. struct
  156. {
  157. uint8_t data_rdy : 1; // B.0 : RO, rx FIFO data ready
  158. uint8_t over_err : 1; // B.1 : RZ, rx FIFO data overrun
  159. uint8_t par_err : 1; // B.2 : RZ, rx parity error
  160. uint8_t frame_err : 1; // B.3 : RZ, rx frame error
  161. uint8_t break_err : 1; // B.4 : RZ, rx BREAK detected
  162. uint8_t tx_fifo_emp : 1; // B.5 : RO, tx FIFO empty
  163. uint8_t tx_all_emp : 1; // B.6 : RO, THR/TSR all empty
  164. uint8_t err_rx_fifo : 1; // B.7 : RO, PAR/FRAME/BREAK ERR in rx FIFO
  165. };
  166. };
  167. #define RB_LSR_DATA_RDY 0x01
  168. #define RB_LSR_OVER_ERR 0x02
  169. #define RB_LSR_PAR_ERR 0x04
  170. #define RB_LSR_FRAME_ERR 0x08
  171. #define RB_LSR_BREAK_ERR 0x10
  172. #define RB_LSR_TX_FIFO_EMP 0x20
  173. #define RB_LSR_TX_ALL_EMP 0x40
  174. #define RB_LSR_ERR_RX_FIFO 0x80
  175. union _uart_msr
  176. {
  177. uint8_t reg;
  178. struct
  179. {
  180. uint8_t cts_chg : 1; // B.0 : RZ, CTS input changed
  181. uint8_t dsr_chg : 1; // B.1 : RZ, DSR input changed
  182. uint8_t ri_chg : 1; // B.2 : RZ, RI input changed
  183. uint8_t dcd_chg : 1; // B.3 : RZ, DCD input changed
  184. uint8_t cts : 1; // B.4 : RO, CTS action status
  185. uint8_t dsr : 1; // B.5 : RO, DSR action status
  186. uint8_t ri : 1; // B.6 : RO, RI action status
  187. uint8_t dcd : 1; // B.7 : RO, DCD action status
  188. };
  189. };
  190. #define RB_MSR_CTS_CHG 0x01
  191. #define RB_MSR_DSR_CHG 0x02
  192. #define RB_MSR_RI_CHG 0x04
  193. #define RB_MSR_DCD_CHG 0x08
  194. #define RB_MSR_CTS 0x10
  195. #define RB_MSR_DSR 0x20
  196. #define RB_MSR_RI 0x40
  197. #define RB_MSR_DCD 0x80
  198. /*
  199. * 0x00 R8_UARTx_MCR: Modem Control Register
  200. * 0x01 R8_UARTx_IER: Interrupt Enable Register
  201. * 0x02 R8_UARTx_FCR: FIFO Control Register
  202. * 0x03 R8_UARTx_LCR: Line Control Register
  203. * 0x04 R8_UARTx_IIR: Interrupt Identification Register
  204. * 0x05 R8_UARTx_LSR: Line Status Register
  205. * 0x06 R8_UARTx_MSR: Modem Status Register (UART0 only)
  206. * 0x08 R8_UARTx_RBR: Rx Buffer Register
  207. * 0x08 R8_UARTx_THR: Tx Hold Register
  208. * 0x0a R8_UARTx_RFC: Rx FIFO count register
  209. * 0x0b R8_UARTx_TFC: Tx FIFO count register
  210. * 0x0c R16_UARTx_DL: Divisor Latch
  211. * 0x0e R8_UARTx_DIV: frequency pre divider
  212. * 0x0f R8_UARTx_ADR: Address Register (UART0 only)
  213. *
  214. * CAVEAT: gcc (as of 8.2.0) tends to read 32-bit word for bit field test.
  215. * Be careful for those with side effect for read (e.g. RBR, IIR).
  216. */
  217. struct uart_registers
  218. {
  219. union _uart_mcr MCR;
  220. union _uart_ier IER;
  221. union _uart_fcr FCR;
  222. union _uart_lcr LCR;
  223. union _uart_iir IIR;
  224. union _uart_lsr LSR;
  225. union _uart_lsr MSR;
  226. uint8_t resv_7;
  227. union
  228. {
  229. uint8_t RBR;
  230. uint8_t THR;
  231. };
  232. uint8_t resv_9;
  233. uint8_t RFC;
  234. uint8_t TFC;
  235. uint16_t DL;
  236. uint8_t DIV;
  237. uint8_t ADR;
  238. } __packed;
  239. CHECK_STRUCT_SIZE(struct uart_registers, 0x10);
  240. int rt_hw_uart_init(void);
  241. #ifdef __cplusplus
  242. }
  243. #endif
  244. #endif