hal_uart.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. /*
  3. * Copyright (c) 2020-2021 Rockchip Electronics Co., Ltd.
  4. */
  5. #include "hal_conf.h"
  6. #ifdef HAL_UART_MODULE_ENABLED
  7. /** @addtogroup RK_HAL_Driver
  8. * @{
  9. */
  10. /** @addtogroup UART
  11. * @{
  12. */
  13. #ifndef _HAL_UART_H_
  14. #define _HAL_UART_H_
  15. #include "hal_def.h"
  16. #include "hal_base.h"
  17. /***************************** MACRO Definition ******************************/
  18. /** @defgroup UART_Exported_Definition_Group1 Basic Definition
  19. * @{
  20. */
  21. /* Out: Interrupt Enable Register */
  22. #define UART_IER_PTIME 0x80 /**< Enable Programmable THRE Interrupt Mode */
  23. #define UART_IER_MSI 0x08 /**< Enable Modem status interrupt */
  24. #define UART_IER_RLSI 0x04 /**< Enable receiver line status interrupt */
  25. #define UART_IER_THRI 0x02 /**< Enable Transmitter holding register int. */
  26. #define UART_IER_RDI 0x01 /**< Enable receiver data interrupt */
  27. /* In: Interrupt ID Register */
  28. #define UART_IIR_MASK 0x0f /**< Interrupt ID Mask */
  29. #define UART_IIR_NO_INT 0x01 /**< No interrupts pending */
  30. #define UART_IIR_ID 0x0e /**< Mask for the interrupt ID */
  31. #define UART_IIR_MSI 0x00 /**< Modem status interrupt */
  32. #define UART_IIR_THRI 0x02 /**< Transmitter holding register empty */
  33. #define UART_IIR_RDI 0x04 /**< Receiver data interrupt */
  34. #define UART_IIR_RLSI 0x06 /**< Receiver line status interrupt */
  35. #define UART_IIR_BUSY 0x07 /**< DesignWare APB Busy Detect */
  36. #define UART_IIR_RX_TIMEOUT 0x0c /**< RX Timeout interrupt */
  37. /* Out: FIFO Control Register */
  38. #define UART_FCR_ENABLE_FIFO 0x01 /**< Enable the FIFO */
  39. #define UART_FCR_CLEAR_RCVR 0x02 /**< Clear the RCVR FIFO */
  40. #define UART_FCR_CLEAR_XMIT 0x04 /**< Clear the XMIT FIFO */
  41. #define UART_FCR_DMA_SELECT 0x08 /**< For DMA applications */
  42. #define UART_FCR_R_TRIG_00 0x00
  43. #define UART_FCR_R_TRIG_01 0x40
  44. #define UART_FCR_R_TRIG_10 0x80
  45. #define UART_FCR_R_TRIG_11 0xc0
  46. #define UART_FCR_T_TRIG_00 0x00
  47. #define UART_FCR_T_TRIG_01 0x10
  48. #define UART_FCR_T_TRIG_10 0x20
  49. #define UART_FCR_T_TRIG_11 0x30
  50. #define UART_FCR_TRIGGER_MASK 0xC0 /**< Mask for the FIFO trigger range */
  51. #define UART_FCR_TRIGGER_1 0x00 /**< Mask for trigger set at 1 */
  52. #define UART_FCR_TRIGGER_4 0x40 /**< Mask for trigger set at 4 */
  53. #define UART_FCR_TRIGGER_8 0x80 /**< Mask for trigger set at 8 */
  54. #define UART_FCR_TRIGGER_14 0xC0 /**< Mask for trigger set at 14 */
  55. /* 16650 definitions */
  56. #define UART_FCR6_R_TRIGGER_8 0x00 /**< Mask for receive trigger set at 1 */
  57. #define UART_FCR6_R_TRIGGER_16 0x40 /**< Mask for receive trigger set at 4 */
  58. #define UART_FCR6_R_TRIGGER_24 0x80 /**< Mask for receive trigger set at 8 */
  59. #define UART_FCR6_R_TRIGGER_28 0xC0 /**< Mask for receive trigger set at 14 */
  60. #define UART_FCR6_T_TRIGGER_16 0x00 /**< Mask for transmit trigger set at 16 */
  61. #define UART_FCR6_T_TRIGGER_8 0x10 /**< Mask for transmit trigger set at 8 */
  62. #define UART_FCR6_T_TRIGGER_24 0x20 /**< Mask for transmit trigger set at 24 */
  63. #define UART_FCR6_T_TRIGGER_30 0x30 /**< Mask for transmit trigger set at 30 */
  64. #define UART_FCR7_64BYTE 0x20
  65. /* Go into 64 byte mode (TI16C750 and some Freescale UARTs) */
  66. #define UART_FCR_R_TRIG_SHIFT 6
  67. #define UART_FCR_R_TRIG_BITS(x) (((x)&UART_FCR_TRIGGER_MASK) >> UART_FCR_R_TRIG_SHIFT)
  68. #define UART_FCR_R_TRIG_MAX_STATE 4
  69. /* Out: Line Control Register */
  70. /*
  71. * Note: if the word length is 5 bits (UART_LCR_WLEN5), then setting
  72. * UART_LCR_STOP will select 1.5 stop bits, not 2 stop bits.
  73. */
  74. #define UART_LCR_DLAB 0x80 /**< Divisor latch access bit */
  75. #define UART_LCR_SBC 0x40 /**< Set break control */
  76. #define UART_LCR_SPAR 0x20 /**< Stick parity (?) */
  77. #define UART_LCR_EPAR 0x10 /**< Even parity select */
  78. #define UART_LCR_PARITY 0x08 /**< Parity Enable */
  79. #define UART_LCR_STOP 0x04 /**< Stop bits: 0=1 bit, 1=2 bits */
  80. #define UART_LCR_WLEN5 0x00 /**< Wordlength: 5 bits */
  81. #define UART_LCR_WLEN6 0x01 /**< Wordlength: 6 bits */
  82. #define UART_LCR_WLEN7 0x02 /**< Wordlength: 7 bits */
  83. #define UART_LCR_WLEN8 0x03 /**< Wordlength: 8 bits */
  84. /* Out: Modem Control Register */
  85. #define UART_MCR_CLKSEL 0x80 /**< Divide clock by 4 (TI16C752, EFR[4]=1) */
  86. #define UART_MCR_TCRTLR 0x40 /**< Access TCR/TLR (TI16C752, EFR[4]=1) */
  87. #define UART_MCR_XONANY 0x20 /**< Enable Xon Any (TI16C752, EFR[4]=1) */
  88. #define UART_MCR_AFE 0x20 /**< Enable auto-RTS/CTS (TI16C550C/TI16C750) */
  89. #define UART_MCR_LOOP 0x10 /**< Enable loopback test mode */
  90. #define UART_MCR_OUT2 0x08 /**< Out2 complement */
  91. #define UART_MCR_OUT1 0x04 /**< Out1 complement */
  92. #define UART_MCR_RTS 0x02 /**< RTS complement */
  93. #define UART_MCR_DTR 0x01 /**< DTR complement */
  94. /* In: Line Status Register */
  95. #define UART_LSR_FIFOE 0x80 /**< Fifo error */
  96. #define UART_LSR_TEMT 0x40 /**< Transmitter empty */
  97. #define UART_LSR_THRE 0x20 /**< Transmit-hold-register empty */
  98. #define UART_LSR_BI 0x10 /**< Break interrupt indicator */
  99. #define UART_LSR_FE 0x08 /**< Frame error indicator */
  100. #define UART_LSR_PE 0x04 /**< Parity error indicator */
  101. #define UART_LSR_OE 0x02 /**< Overrun error indicator */
  102. #define UART_LSR_DR 0x01 /**< Receiver data ready */
  103. #define UART_LSR_BRK_ERROR_BITS 0x1E /**< BI, FE, PE, OE bits */
  104. /* In: Modem Status Register */
  105. #define UART_MSR_DCD 0x80 /**< Data Carrier Detect */
  106. #define UART_MSR_RI 0x40 /**< Ring Indicator */
  107. #define UART_MSR_DSR 0x20 /**< Data Set Ready */
  108. #define UART_MSR_CTS 0x10 /**< Clear to Send */
  109. #define UART_MSR_DDCD 0x08 /**< Delta DCD */
  110. #define UART_MSR_TERI 0x04 /**< Trailing edge ring indicator */
  111. #define UART_MSR_DDSR 0x02 /**< Delta DSR */
  112. #define UART_MSR_DCTS 0x01 /**< Delta CTS */
  113. #define UART_MSR_ANY_DELTA 0x0F /**< Any of the delta bits! */
  114. #define UART_USR_RX_FIFO_FULL 0x10 /**< Receive FIFO full */
  115. #define UART_USR_RX_FIFO_NOT_EMPTY 0x08 /**< Receive FIFO not empty */
  116. #define UART_USR_TX_FIFO_EMPTY 0x04 /**< Transmit FIFO empty */
  117. #define UART_USR_TX_FIFO_NOT_FULL 0x02 /**< Transmit FIFO not full */
  118. #define UART_USR_BUSY 0x01 /**< UART busy indicator */
  119. #define UART_SRR_UR 0x1 /**< UART Reset */
  120. #define UART_SRR_RFR 0X2 /**< RCVR FIFO Reset */
  121. #define UART_SRR_XFR 0x4 /**< XMIT FIFO Reset */
  122. #define MODE_X_DIV 16 /**< baud = f / 16 / div */
  123. /***************************** Structure Definition **************************/
  124. /**
  125. * @brief UART baud rate definition
  126. */
  127. typedef enum {
  128. UART_BR_110 = 110,
  129. UART_BR_300 = 300,
  130. UART_BR_600 = 600,
  131. UART_BR_1200 = 1200,
  132. UART_BR_2400 = 2400,
  133. UART_BR_4800 = 4800,
  134. UART_BR_9600 = 9600,
  135. UART_BR_14400 = 14400,
  136. UART_BR_19200 = 19200,
  137. UART_BR_38400 = 38400,
  138. UART_BR_57600 = 57600,
  139. UART_BR_115200 = 115200,
  140. UART_BR_230400 = 230400,
  141. UART_BR_380400 = 380400,
  142. UART_BR_460800 = 460800,
  143. UART_BR_921600 = 921600,
  144. UART_BR_1000000 = 1000000,
  145. UART_BR_1500000 = 1500000,
  146. UART_BR_2000000 = 2000000,
  147. UART_BR_3000000 = 3000000,
  148. UART_BR_4000000 = 4000000,
  149. } eUART_baudRate;
  150. /**
  151. * @brief UART data bit definition
  152. */
  153. typedef enum {
  154. UART_DATA_5B = 5,
  155. UART_DATA_6B,
  156. UART_DATA_7B,
  157. UART_DATA_8B
  158. } eUART_dataLen;
  159. /**
  160. * @brief UART stop bit definition
  161. */
  162. typedef enum {
  163. UART_ONE_STOPBIT,
  164. UART_ONE_AND_HALF_OR_TWO_STOPBIT
  165. } eUART_stopBit;
  166. /**
  167. * @brief UART parity definition
  168. */
  169. typedef enum {
  170. UART_ODD_PARITY,
  171. UART_EVEN_PARITY,
  172. UART_PARITY_DISABLE
  173. } eUART_parityEn;
  174. /**
  175. * @brief UART config definition
  176. */
  177. struct HAL_UART_CONFIG {
  178. eUART_baudRate baudRate;
  179. eUART_dataLen dataBit;
  180. eUART_stopBit stopBit;
  181. eUART_parityEn parity;
  182. };
  183. /**
  184. * @brief UART HW information definition on a soc
  185. */
  186. struct HAL_UART_DEV {
  187. struct UART_REG *pReg; /**< registers base address */
  188. /* sclk is for uart logic, pclk is for register access */
  189. eCLOCK_Name sclkID;
  190. uint32_t sclkGateID;
  191. uint32_t pclkGateID;
  192. IRQn_Type irqNum;
  193. bool isAutoFlow;
  194. ePM_RUNTIME_ID runtimeID;
  195. DMA_REQ_Type dmaTxReqNum; /**< peri dma tx request num */
  196. DMA_REQ_Type dmaRxReqNum; /**< peri dma rx request num */
  197. struct DMA_REG *dmac; /**< dmac reg base ptr */
  198. };
  199. /**
  200. * @brief Save UART regist
  201. */
  202. struct UART_SAVE_CONFIG {
  203. uint32_t DLL;
  204. uint32_t DLH;
  205. uint32_t IER;
  206. uint32_t LCR;
  207. uint32_t MCR;
  208. uint32_t SRT;
  209. uint32_t STET;
  210. };
  211. /** @} */
  212. /***************************** Function Declare ******************************/
  213. /** @defgroup UART_Public_Function_Declare Public Function Declare
  214. * @{
  215. */
  216. void HAL_UART_EnableIrq(struct UART_REG *pReg, uint32_t uartIntNumb);
  217. void HAL_UART_DisableIrq(struct UART_REG *pReg, uint32_t uartIntNumb);
  218. void HAL_UART_EnableLoopback(struct UART_REG *pReg);
  219. void HAL_UART_DisableLoopback(struct UART_REG *pReg);
  220. void HAL_UART_EnableAutoFlowControl(struct UART_REG *pReg);
  221. void HAL_UART_DisableAutoFlowControl(struct UART_REG *pReg);
  222. uint32_t HAL_UART_GetIrqID(struct UART_REG *pReg);
  223. uint32_t HAL_UART_GetLsr(struct UART_REG *pReg);
  224. uint32_t HAL_UART_GetUsr(struct UART_REG *pReg);
  225. uint32_t HAL_UART_GetMsr(struct UART_REG *pReg);
  226. void HAL_UART_SerialOutChar(struct UART_REG *pReg, char c);
  227. int HAL_UART_SerialOut(struct UART_REG *pReg, const uint8_t *pdata, uint32_t cnt);
  228. int HAL_UART_SerialIn(struct UART_REG *pReg, uint8_t *pdata, uint32_t cnt);
  229. HAL_Status HAL_UART_HandleIrq(struct UART_REG *pReg);
  230. void HAL_UART_Reset(struct UART_REG *pReg);
  231. HAL_Status HAL_UART_Init(const struct HAL_UART_DEV *dev, const struct HAL_UART_CONFIG *config);
  232. HAL_Status HAL_UART_DeInit(struct UART_REG *pReg);
  233. HAL_Status HAL_UART_Suspend(struct UART_REG *pReg, struct UART_SAVE_CONFIG *pUartSave);
  234. HAL_Status HAL_UART_Resume(struct UART_REG *pReg, struct UART_SAVE_CONFIG *pUartSave);
  235. /** @} */
  236. #endif
  237. /** @} */
  238. /** @} */
  239. #endif /* HAL_UART_MODULE_ENABLED */