1 /* 2 * Copyright (C) 2015-2020 Alibaba Group Holding Limited 3 */ 4 #ifndef __HAL_UART_H__ 5 #define __HAL_UART_H__ 6 7 #ifdef __cplusplus 8 extern "C" { 9 #endif 10 11 #ifdef CHIP_HAS_UART 12 13 #include "plat_types.h" 14 #include "stdbool.h" 15 #include "stdint.h" 16 #include "hal_dma.h" 17 18 //#define BT_UART 19 20 #define HAL_UART_DMA_TRANSFER_STEP 0xFFF 21 #ifdef __KNOWLES 22 #define HAL_UART_DMA_TRANSFER_STEP_PINGPANG 640 23 #else 24 #define HAL_UART_DMA_TRANSFER_STEP_PINGPANG 4 25 #endif 26 27 enum HAL_UART_ID_T { 28 HAL_UART_ID_0 = 0, 29 #if (CHIP_HAS_UART > 1) 30 HAL_UART_ID_1, 31 #endif 32 #if (CHIP_HAS_UART > 2) 33 HAL_UART_ID_2, 34 #endif 35 #ifdef BT_UART 36 HAL_UART_ID_BT, 37 #endif 38 39 HAL_UART_ID_QTY 40 }; 41 42 enum HAL_UART_PARITY_T { 43 HAL_UART_PARITY_NONE, 44 HAL_UART_PARITY_ODD, 45 HAL_UART_PARITY_EVEN, 46 HAL_UART_PARITY_FORCE1, 47 HAL_UART_PARITY_FORCE0, 48 }; 49 50 enum HAL_UART_STOP_BITS_T { 51 HAL_UART_STOP_BITS_1, 52 HAL_UART_STOP_BITS_2, 53 }; 54 55 enum HAL_UART_DATA_BITS_T { 56 HAL_UART_DATA_BITS_5, 57 HAL_UART_DATA_BITS_6, 58 HAL_UART_DATA_BITS_7, 59 HAL_UART_DATA_BITS_8, 60 }; 61 62 enum HAL_UART_FLOW_CONTROL_T { 63 HAL_UART_FLOW_CONTROL_NONE, 64 HAL_UART_FLOW_CONTROL_RTS, 65 HAL_UART_FLOW_CONTROL_CTS, 66 HAL_UART_FLOW_CONTROL_RTSCTS, 67 }; 68 69 enum HAL_UART_FIFO_LEVEL_T { 70 HAL_UART_FIFO_LEVEL_1_8, 71 HAL_UART_FIFO_LEVEL_1_4, 72 HAL_UART_FIFO_LEVEL_1_2, 73 HAL_UART_FIFO_LEVEL_3_4, 74 HAL_UART_FIFO_LEVEL_7_8, 75 }; 76 77 struct HAL_UART_CFG_T { 78 enum HAL_UART_PARITY_T parity; 79 enum HAL_UART_STOP_BITS_T stop; 80 enum HAL_UART_DATA_BITS_T data; 81 enum HAL_UART_FLOW_CONTROL_T flow; 82 enum HAL_UART_FIFO_LEVEL_T rx_level; 83 enum HAL_UART_FIFO_LEVEL_T tx_level; 84 uint32_t baud; 85 bool dma_rx : 1; 86 bool dma_tx : 1; 87 bool dma_rx_stop_on_err : 1; 88 }; 89 90 union HAL_UART_STATUS_T { 91 struct { 92 uint32_t FE :1; // frame error 93 uint32_t PE :1; // parity error 94 uint32_t BE :1; // break error 95 uint32_t OE :1; // overrun error 96 }; 97 uint32_t reg; 98 }; 99 100 union HAL_UART_FLAG_T { 101 struct { 102 uint32_t CTS :1; 103 uint32_t DSR :1; 104 uint32_t DCD :1; 105 uint32_t BUSY :1; 106 uint32_t RXFE :1; // rx fifo empty 107 uint32_t TXFF :1; // tx fifo full 108 uint32_t RXFF :1; // rx fifo full 109 uint32_t TXFE :1; // tx fifo empty 110 uint32_t RI :1; // ring indicator 111 }; 112 uint32_t reg; 113 }; 114 115 union HAL_UART_IRQ_T { 116 struct { 117 uint32_t RIM :1; // ri 118 uint32_t CTSM :1; // cts 119 uint32_t DCDM :1; // dcd 120 uint32_t DSRM :1; // dsr 121 uint32_t RX :1; // rx 122 uint32_t TX :1; // tx 123 uint32_t RT :1; // receive timeout 124 uint32_t FE :1; // framing error 125 uint32_t PE :1; // parity error 126 uint32_t BE :1; // break error 127 uint32_t OE :1; // overrun 128 }; 129 uint32_t reg; 130 }; 131 132 typedef void (*HAL_UART_IRQ_HANDLER_T)(enum HAL_UART_ID_T id, union HAL_UART_IRQ_T status); 133 134 typedef void (*HAL_UART_IRQ_RXDMA_HANDLER_T)(uint32_t xfer_size, int dma_error, union HAL_UART_IRQ_T status); 135 136 typedef void (*HAL_UART_IRQ_TXDMA_HANDLER_T)(uint32_t xfer_size, int dma_error); 137 138 typedef void (*HAL_UART_IRQ_BREAK_HANDLER_T)(void); 139 140 int hal_uart_open(enum HAL_UART_ID_T id, const struct HAL_UART_CFG_T *cfg); 141 142 int hal_uart_reopen(enum HAL_UART_ID_T id, const struct HAL_UART_CFG_T *cfg); 143 144 int hal_uart_close(enum HAL_UART_ID_T id); 145 146 int hal_uart_opened(enum HAL_UART_ID_T id); 147 148 int hal_uart_pause(enum HAL_UART_ID_T id); 149 150 int hal_uart_continue(enum HAL_UART_ID_T id); 151 152 int hal_uart_readable(enum HAL_UART_ID_T id); 153 154 int hal_uart_writable(enum HAL_UART_ID_T id); 155 156 uint8_t hal_uart_getc(enum HAL_UART_ID_T id); 157 158 int hal_uart_putc(enum HAL_UART_ID_T id, uint8_t c); 159 160 uint8_t hal_uart_blocked_getc(enum HAL_UART_ID_T id); 161 162 int hal_uart_blocked_putc(enum HAL_UART_ID_T id, uint8_t c); 163 164 union HAL_UART_FLAG_T hal_uart_get_flag(enum HAL_UART_ID_T id); 165 166 union HAL_UART_STATUS_T hal_uart_get_status(enum HAL_UART_ID_T id); 167 168 void hal_uart_clear_status(enum HAL_UART_ID_T id); 169 170 void hal_uart_break_set(enum HAL_UART_ID_T id); 171 172 void hal_uart_break_clear(enum HAL_UART_ID_T id); 173 174 void hal_uart_flush(enum HAL_UART_ID_T id, uint32_t ticks); 175 176 union HAL_UART_IRQ_T hal_uart_get_raw_irq(enum HAL_UART_ID_T id); 177 178 void hal_uart_clear_irq(enum HAL_UART_ID_T id, union HAL_UART_IRQ_T irq); 179 180 union HAL_UART_IRQ_T hal_uart_irq_get_mask(enum HAL_UART_ID_T id); 181 182 union HAL_UART_IRQ_T hal_uart_irq_set_mask(enum HAL_UART_ID_T id, union HAL_UART_IRQ_T mask); 183 184 HAL_UART_IRQ_HANDLER_T hal_uart_irq_set_handler(enum HAL_UART_ID_T id, HAL_UART_IRQ_HANDLER_T handler); 185 186 void hal_uart_irq_set_dma_handler(enum HAL_UART_ID_T id, HAL_UART_IRQ_RXDMA_HANDLER_T rxdma, 187 HAL_UART_IRQ_TXDMA_HANDLER_T txdma, HAL_UART_IRQ_BREAK_HANDLER_T brk); 188 189 int hal_uart_dma_recv(enum HAL_UART_ID_T id, uint8_t *buf, uint32_t len, 190 struct HAL_DMA_DESC_T *desc, uint32_t *desc_cnt); 191 192 int hal_uart_dma_recv_pingpang(enum HAL_UART_ID_T id, uint8_t *buf, uint32_t len, 193 struct HAL_DMA_DESC_T *desc, uint32_t *desc_cnt); 194 195 196 int hal_uart_dma_recv_mask(enum HAL_UART_ID_T id, uint8_t *buf, uint32_t len, 197 struct HAL_DMA_DESC_T *desc, uint32_t *desc_cnt, 198 const union HAL_UART_IRQ_T *mask); 199 200 int hal_uart_dma_recv_mask2(enum HAL_UART_ID_T id, uint8_t *buf0, uint32_t len0, 201 uint8_t *buf1, uint32_t len1, 202 struct HAL_DMA_DESC_T desc[2], const union HAL_UART_IRQ_T *mask); 203 204 int hal_uart_dma_recv_mask_pingpang(enum HAL_UART_ID_T id, uint8_t *buf, uint32_t len, 205 struct HAL_DMA_DESC_T *desc, uint32_t *desc_cnt, 206 const union HAL_UART_IRQ_T *mask, uint32_t step); 207 208 int hal_uart_dma_recv_mask_stream(enum HAL_UART_ID_T id, uint8_t *buf, uint32_t len, 209 struct HAL_DMA_DESC_T *desc, uint32_t *desc_cnt, 210 const union HAL_UART_IRQ_T *mask, uint32_t step); 211 212 uint32_t hal_uart_get_dma_recv_addr(enum HAL_UART_ID_T id); 213 214 uint32_t hal_uart_stop_dma_recv(enum HAL_UART_ID_T id); 215 216 int hal_uart_dma_send(enum HAL_UART_ID_T id, const uint8_t *buf, uint32_t len, 217 struct HAL_DMA_DESC_T *desc, uint32_t *desc_cnt); 218 219 int hal_uart_dma_send2(enum HAL_UART_ID_T id, const uint8_t *buf0, uint32_t len0, 220 const uint8_t *buf1, uint32_t len1, 221 struct HAL_DMA_DESC_T desc[2]); 222 223 uint32_t hal_uart_stop_dma_send(enum HAL_UART_ID_T id); 224 225 // addd for ali tihngs uart2 dma send 226 int hal_uart_dma_send_sync_cache(enum HAL_UART_ID_T id, const uint8_t *buf, uint32_t len, 227 struct HAL_DMA_DESC_T *desc, uint32_t *desc_cnt); 228 // ======================================================================== 229 // Test function 230 231 int hal_uart_printf_init(void); 232 233 void hal_uart_printf(const char *fmt, ...); 234 235 void hal_uart_output(const unsigned char *buf, int size); 236 237 #endif // CHIP_HAS_UART 238 239 #ifdef __cplusplus 240 } 241 #endif 242 243 #endif 244 245