33 #define UART_REG_RBR_OFS 0x00000000UL 34 #define UART_REG_THR_OFS 0x00000000UL 35 #define UART_REG_DLL_OFS 0x000000D0UL 36 #define UART_REG_DLH_OFS 0x000000D4UL 37 #define UART_REG_IER_OFS 0x00000004UL 38 #define UART_REG_IIR_OFS 0x000000E0UL 39 #define UART_REG_FCR_OFS 0x00000008UL 40 #define UART_REG_LCR_OFS 0x0000000CUL 41 #define UART_REG_MCR_OFS 0x00000010UL 42 #define UART_REG_LSR_OFS 0x00000014UL 43 #define UART_REG_MSR_OFS 0x00000018UL 44 #define UART_REG_USR_OFS 0x0000007CUL 45 #define UART_REG_TFL_OFS 0x00000080UL 46 #define UART_REG_RFL_OFS 0x00000084UL 47 #define UART_REG_DLF_OFS 0x000000C0UL 48 #define UART_REG_CTO_OFS 0x00000028UL 50 #define UART_FIFO_DEPTH 16 52 #define UART_LSR_RFE (1<<7) 53 #define UART_LSR_TEMT (1<<6) 54 #define UART_LSR_THRE (1<<5) 55 #define UART_LSR_BI (1<<4) 56 #define UART_LSR_FE (1<<3) 57 #define UART_LSR_PE (1<<2) 58 #define UART_LSR_OE (1<<1) 59 #define UART_LSR_DR (1<<0) 61 #define UART_MSR_DCD (1<<7) 62 #define UART_MSR_RI (1<<6) 63 #define UART_MSR_DSR (1<<5) 64 #define UART_MSR_CTS (1<<4) 65 #define UART_MSR_DDCD (1<<3) 66 #define UART_MSR_TERI (1<<2) 67 #define UART_MSR_DDSR (1<<1) 68 #define UART_MSR_DCTS (1<<0) 70 #define UART_MCR_SIRE 0x00000040UL 71 #define UART_MCR_AFCE 0x00000020UL 72 #define UART_MCR_LOOPBACK 0x00000010UL 73 #define UART_MCR_OUT2 0x00000008UL 74 #define UART_MCR_OUT1 0x00000004UL 75 #define UART_MCR_RTS 0x00000002UL 76 #define UART_MCR_DTR 0x00000001UL 78 #define UART_LCR_DLAB 0x00000080UL 79 #define UART_LCR_BC 0x00000040UL 80 #define UART_LCR_SPS 0x00000020UL 81 #define UART_LCR_EPS 0x00000010UL 82 #define UART_LCR_PEN 0x00000008UL 83 #define UART_LCR_STOP 0x00000004UL 84 #define UART_LCR_DLS 0x00000003UL 86 #define UART_IER_EDSSI 0x00000008UL 87 #define UART_IER_ELSI 0x00000004UL 88 #define UART_IER_ETBEI 0x00000002UL 89 #define UART_IER_ERBFI 0x00000001UL 90 #define UART_IER_PTIME 0x00000080UL 91 #define UART_IER_CTO 0x00000010UL 92 #define UART_IER_ALL 0x0000001FUL 94 #define UART_IT_ID_NONE 0x1 95 #define UART_IT_ID_LINE_STATUS 0x6 96 #define UART_IT_ID_RCVR_DATA 0x4 97 #define UART_IT_ID_CHAR_TIME_OUT 0xC 98 #define UART_IT_ID_THR_EMPTY 0x2 99 #define UART_IT_ID_MODEM_STATUS 0x0 100 #define UART_IT_ID_BUSY_DET 0x7 101 #define UART_IT_ID_CTO_DET 0x10 103 #define UART_FCR_RT 0x000000C0UL 104 #define UART_FCR_TET 0x00000030UL 105 #define UART_FCR_DMAM 0x00000008UL 106 #define UART_FCR_XFIFOR 0x00000004UL 107 #define UART_FCR_RFIFOR 0x00000002UL 108 #define UART_FCR_FIFOE 0x00000001UL 110 #define UART_TET_EMPTY 0 111 #define UART_TET_2_CHARS 1 112 #define UART_TET_QUARTER_FULL 2 113 #define UART_TET_HALF_FULL 3 115 #define UART_USR_BUSY 0x00000001UL 116 #define UART_USR_TFNF 0x00000002UL 117 #define UART_USR_TFE 0x00000004UL 118 #define UART_USR_RFNE 0x00000008UL 119 #define UART_USR_RFF 0x00000010UL 121 #define UART_CTO_STATUS 0x00000010UL 122 #define UART_CTO_EN 0x00000008UL 123 #define UART_CTO_CYCLES 0x00000007UL 124 #define UART_CTO_WAIT 0x00000020UL 125 #define UART_CTO_AUTO_DEASSERT 0x00000040UL 126 #define UART_CTO_AUTO_CLEAR 0x00000080UL 127 #define UART_RTS_ASSERT_BELOW_TH 0x00000100UL 128 #define UART_RTS_STANDALONE_THRSH 0x00000200UL 143 UART_ERR_INVALID_PARAM = -1,
144 UART_ERR_DEV_BAD_STATE = -2,
149 UART_DMA_NOT_AVAIL = -7,
150 UART_DMA_TX_FAIL = -8,
151 UART_DMA_RX_FAIL = -9,
160 UART_LENGTH_5BITS = 0,
168 UART_RT_QUARTER_FULL,
177 typedef enum uart_rts_fifo_th {
178 UART_RTS_TH_FIFO_1_BYTE = 0,
179 UART_RTS_TH_FIFO_DEPTH_QUARTER,
180 UART_RTS_TH_FIFO_DEPTH_HALF,
181 UART_RTS_TH_FIFO_DEPTH_LESS_2,
182 } uart_rts_fifo_th_t;
184 typedef struct uart_init {
194 void (*rx_cb)(
void* arg,
int length,
int error);
196 void (*tx_cb)(
void* arg,
int length,
int error);
203 static __inline uint8_t uart_read_data(uint32_t uart_base)
205 return ((uint8_t)RD_WORD(uart_base + UART_REG_RBR_OFS));
208 static __inline
void uart_write_data(uint32_t uart_base, uint8_t byte)
210 WR_WORD(uart_base + UART_REG_THR_OFS, byte);
213 static __inline
void uart_set_baud(uint32_t uart_base,
int pclk,
int baud)
215 float div = ((float)pclk/(
float)baud)/16.0f;
216 uint16_t div_i = div;
217 uint8_t div_f = (div - div_i) *64;
219 WR_WORD(uart_base + UART_REG_LCR_OFS, (RD_WORD(uart_base + UART_REG_LCR_OFS) |UART_LCR_DLAB));
220 WR_WORD(uart_base + UART_REG_DLF_OFS, (div_f & 0x3F));
221 WR_WORD(uart_base + UART_REG_DLL_OFS, (div_i & 0xFF));
222 WR_WORD(uart_base + UART_REG_DLH_OFS, ((div_i>>8) & 0xFF));
223 WR_WORD(uart_base + UART_REG_LCR_OFS, (RD_WORD(uart_base + UART_REG_LCR_OFS) & ~UART_LCR_DLAB));
226 static __inline
void uart_set_lcr(uint32_t uart_base,
int stop_bit,
int par_en,
int ev_par,
int data_len)
228 uint32_t reg = RD_WORD(uart_base + UART_REG_LCR_OFS);
231 reg |= UART_LCR_STOP;
233 reg &= ~UART_LCR_STOP;
239 reg &= ~UART_LCR_PEN;
245 reg &= ~UART_LCR_EPS;
248 reg &= ~UART_LCR_DLS;
250 reg |= UART_LENGTH_5BITS;
251 }
else if (data_len == 6) {
252 reg |= UART_LENGTH_6BITS;
253 }
else if (data_len == 7) {
254 reg |= UART_LENGTH_7BITS;
256 reg |= UART_LENGTH_8BITS;
259 WR_WORD(uart_base + UART_REG_LCR_OFS, reg);
262 static __inline
void uart_set_stop_bits(uint32_t uart_base,
int bits)
264 uint32_t reg = RD_WORD(uart_base + UART_REG_LCR_OFS);
267 reg &= ~UART_LCR_STOP;
269 reg |= UART_LCR_STOP;
272 WR_WORD(uart_base + UART_REG_LCR_OFS, reg);
275 static __inline
void uart_set_parity(uint32_t uart_base,
int enable)
277 uint32_t reg = RD_WORD(uart_base + UART_REG_LCR_OFS);
282 reg &= ~UART_LCR_PEN;
284 WR_WORD(uart_base + UART_REG_LCR_OFS, reg);
287 static __inline
void uart_set_even_parity(uint32_t uart_base,
int even)
289 uint32_t reg = RD_WORD(uart_base + UART_REG_LCR_OFS);
294 reg &= ~UART_LCR_EPS;
297 WR_WORD(uart_base + UART_REG_LCR_OFS, reg);
300 static __inline
void uart_set_data_len(uint32_t uart_base,
int data_len)
302 uint32_t reg = RD_WORD(uart_base + UART_REG_LCR_OFS);
304 reg &= ~UART_LCR_DLS;
307 reg |= UART_LENGTH_5BITS;
308 }
else if (data_len == 6) {
309 reg |= UART_LENGTH_6BITS;
310 }
else if (data_len == 7) {
311 reg |= UART_LENGTH_7BITS;
313 reg |= UART_LENGTH_8BITS;
316 WR_WORD(uart_base + UART_REG_LCR_OFS, reg);
319 static __inline
void uart_intr_enable(uint32_t uart_base, uint32_t bits)
321 WR_WORD(uart_base + UART_REG_IER_OFS, (RD_WORD(uart_base + UART_REG_IER_OFS) |bits));
324 static __inline uint32_t uart_intr_enable_get(uint32_t uart_base)
326 return (RD_WORD(uart_base + UART_REG_IER_OFS));
329 static __inline
void uart_intr_disable(uint32_t uart_base, uint32_t bits)
331 WR_WORD(uart_base + UART_REG_IER_OFS, (RD_WORD(uart_base + UART_REG_IER_OFS) & ~bits));
334 static __inline uint32_t uart_intr_status(uint32_t uart_base)
336 return (RD_WORD(uart_base + UART_REG_IIR_OFS) & 0x1F);
339 static __inline
int uart_fifo_enable_status(uint32_t uart_base)
341 return ((RD_WORD(uart_base + UART_REG_IIR_OFS) & 0xC0) == 0xC0);
344 static __inline
void uart_fcr(uint32_t uart_base,
int fifo_enable,
int fifo_tx_thold,
int fifo_rx_thold,
int dma_mode,
int xfifo_reset,
int rfifo_reset)
350 reg |= UART_FCR_FIFOE;
353 reg |= (fifo_tx_thold & 0x3) << 4;
354 reg |= (fifo_rx_thold & 0x3) << 6;
356 reg |= UART_FCR_DMAM;
359 reg |= UART_FCR_XFIFOR;
362 reg |= UART_FCR_RFIFOR;
364 WR_WORD(uart_base + UART_REG_FCR_OFS, reg);
367 static __inline
void uart_fifo_disable(uint32_t uart_base)
369 WR_WORD(uart_base + UART_REG_FCR_OFS, 0);
372 static __inline
void uart_auto_fc(uint32_t uart_base,
int fc)
374 WR_WORD(uart_base + UART_REG_MCR_OFS, (fc ? (RD_WORD(uart_base + UART_REG_MCR_OFS)|UART_MCR_AFCE) : (RD_WORD(uart_base + UART_REG_MCR_OFS)&~UART_MCR_AFCE)));
378 static __inline
void uart_xmit_ready(uint32_t uart_base)
381 uint32_t reg = RD_WORD(uart_base + UART_REG_LSR_OFS);
382 if (reg & UART_LSR_THRE)
387 static __inline
void uart_rcvd_ready(uint32_t uart_base)
390 uint32_t reg = RD_WORD(uart_base + UART_REG_LSR_OFS);
391 if (reg & UART_LSR_DR)
397 static __inline uint32_t uart_line_status(uint32_t uart_base)
399 return (RD_WORD(uart_base + UART_REG_LSR_OFS));
402 static __inline uint32_t uart_modem_status(uint32_t uart_base)
404 return (RD_WORD(uart_base + UART_REG_MSR_OFS));
407 static __inline
void uart_rts_off(uint32_t uart_base)
409 WR_WORD(uart_base + UART_REG_MCR_OFS, (RD_WORD(uart_base + UART_REG_MCR_OFS) & ~UART_MCR_RTS));
412 static __inline
void uart_rts_on(uint32_t uart_base)
414 WR_WORD(uart_base + UART_REG_MCR_OFS, (RD_WORD(uart_base + UART_REG_MCR_OFS) | UART_MCR_RTS));
417 static __inline
void uart_rts_off_clear_cto_count_enable(uint32_t uart_base)
419 WR_WORD(uart_base + UART_REG_MCR_OFS, (RD_WORD(uart_base + UART_REG_MCR_OFS) | (1 << 7u)));
422 static __inline
void uart_rts_off_clear_cto_count_disable(uint32_t uart_base)
424 WR_WORD(uart_base + UART_REG_MCR_OFS, (RD_WORD(uart_base + UART_REG_MCR_OFS) & ~(1 << 7u)));
427 static __inline uint32_t uart_tfl(uint32_t uart_base)
429 return RD_WORD(uart_base + UART_REG_TFL_OFS);
432 static __inline uint32_t uart_rfl(uint32_t uart_base)
434 return RD_WORD(uart_base + UART_REG_RFL_OFS);
437 static __inline uint32_t uart_usr(uint32_t uart_base)
439 return RD_WORD(uart_base + UART_REG_USR_OFS);
442 static __inline
void uart_cto_enable(uint32_t uart_base)
444 WR_WORD(uart_base + UART_REG_CTO_OFS, (RD_WORD(uart_base + UART_REG_CTO_OFS) | UART_CTO_EN));
448 static __inline
void uart_cto_disable(uint32_t uart_base)
450 WR_WORD(uart_base + UART_REG_CTO_OFS, (RD_WORD(uart_base + UART_REG_CTO_OFS) & ~UART_CTO_EN));
453 static __inline
int uart_cto_status(uint32_t uart_base)
455 return ((RD_WORD(uart_base + UART_REG_CTO_OFS) >> 4) & 1);
459 static __inline
void uart_cto_cycles(uint32_t uart_base,
int cycle)
461 WR_WORD(uart_base + UART_REG_CTO_OFS, ((RD_WORD(uart_base + UART_REG_CTO_OFS) & ~UART_CTO_CYCLES) | (cycle & 0x7)));
464 static __inline
void uart_cto_wait_enable(uint32_t uart_base)
466 WR_WORD(uart_base + UART_REG_CTO_OFS, (RD_WORD(uart_base + UART_REG_CTO_OFS) | UART_CTO_WAIT));
469 static __inline
void uart_cto_wait_disable(uint32_t uart_base)
471 WR_WORD(uart_base + UART_REG_CTO_OFS, (RD_WORD(uart_base + UART_REG_CTO_OFS) & ~UART_CTO_WAIT));
474 static __inline
void uart_cto_auto_rts_deassert_enable(uint32_t uart_base)
476 WR_WORD(uart_base + UART_REG_CTO_OFS, (RD_WORD(uart_base + UART_REG_CTO_OFS) | UART_CTO_AUTO_DEASSERT));
479 static __inline
void uart_cto_auto_deassert_rts_disable(uint32_t uart_base)
481 WR_WORD(uart_base + UART_REG_CTO_OFS, (RD_WORD(uart_base + UART_REG_CTO_OFS) & ~UART_CTO_AUTO_DEASSERT));
484 static __inline
void uart_rts_below_th_assert_enable(uint32_t uart_base)
486 WR_WORD(uart_base + UART_REG_CTO_OFS, (RD_WORD(uart_base + UART_REG_CTO_OFS) | UART_RTS_ASSERT_BELOW_TH));
489 static __inline
void uart_rts_below_th_assert_disable(uint32_t uart_base)
491 WR_WORD(uart_base + UART_REG_CTO_OFS, (RD_WORD(uart_base + UART_REG_CTO_OFS) & ~UART_RTS_ASSERT_BELOW_TH));
494 static __inline
void uart_cto_auto_rts_manu_clear(uint32_t uart_base)
496 WR_WORD(uart_base + UART_REG_CTO_OFS, (RD_WORD(uart_base + UART_REG_CTO_OFS) | UART_CTO_AUTO_CLEAR));
497 WR_WORD(uart_base + UART_REG_CTO_OFS, (RD_WORD(uart_base + UART_REG_CTO_OFS) & ~UART_CTO_AUTO_CLEAR));
500 static __inline
void uart_rts_standalone_th_enable(uint32_t uart_base, uart_rts_fifo_th_t th)
502 WR_WORD(uart_base + UART_REG_CTO_OFS, (RD_WORD(uart_base + UART_REG_CTO_OFS) | (UART_RTS_STANDALONE_THRSH | ((th & 0x03) << 10u))));
505 static __inline
void uart_rts_standalone_th_disable(uint32_t uart_base)
507 WR_WORD(uart_base + UART_REG_CTO_OFS, (RD_WORD(uart_base + UART_REG_CTO_OFS) & ~UART_RTS_STANDALONE_THRSH));
612 int hal_uart_xmit_intr_hci(
void* hdl, uint8_t* buffer, uint32_t buffer_len,
void* cb_arg,
void (*callback)(
void* arg,
int length,
int error));
631 int hal_uart_rcvd_intr(
void *hdl, uint8_t *buffer, uint32_t buffer_len, uint32_t tmo, uint32_t *actual_rx_len);
647 int hal_uart_rcvd_intr_hci(
void* hdl, uint8_t* buffer, uint32_t buffer_len,
void* cb_arg,
void (*callback)(
void* arg,
int length,
int error));
696 int hal_uart_rcvd_dma(
void *hdl, uint8_t *buffer, uint32_t buffer_len, uint32_t tmo, uint32_t *actual_rx_len);
int hal_uart_rcvd_intr_hci(void *hdl, uint8_t *buffer, uint32_t buffer_len, void *cb_arg, void(*callback)(void *arg, int length, int error))
HCI Uart RX by interrupt.
int hal_uart_en_break(void *hdl, char en, void(*cb)(void *), void *arg)
enable uart break feature. Use uart break signal to wake chip.
int hal_uart_rcvd_intr(void *hdl, uint8_t *buffer, uint32_t buffer_len, uint32_t tmo, uint32_t *actual_rx_len)
Uart RX by interrupt.
int hal_uart_xmit_dma(void *hdl, uint8_t *buffer, uint32_t buffer_len)
Uart TX by DMA.
int fc
Uart controller flow control enable, 1: enable, 0: disable.
Definition: hal_uart.h:190
int baud_rate
Uart baud rate.
Definition: hal_uart.h:185
void * hal_uart_open(int id, uart_init_t *init)
Open the uart driver.
int hal_uart_flow_off(void *hdl)
Manually turn off RTS request.
int parity_en
Uart parity enable, 0: disable, 1: enable.
Definition: hal_uart.h:188
void * rx_arg
rx callback argument
Definition: hal_uart.h:193
int hal_uart_xmit_poll(void *hdl, uint8_t *buffer, uint32_t buffer_len)
Uart TX by polling.
int hal_uart_flow_on(void *hdl)
Manually turn on RTS request.
void hal_uart_close(void *hdl)
Close the uart driver.
int hal_uart_xmit_intr_hci(void *hdl, uint8_t *buffer, uint32_t buffer_len, void *cb_arg, void(*callback)(void *arg, int length, int error))
HCI Uart TX by interrupt.
int data_len
Uart char length,.
Definition: hal_uart.h:186
int stop_bit
Uart stop bits, 0: 1 stop bit, 1: 2 stop bits.
Definition: hal_uart.h:187
int prio
interrupt priority
Definition: hal_uart.h:192
int hal_uart_rcvd_poll(void *hdl, uint8_t *buffer, uint32_t buffer_len)
Uart RX by polling.
int hal_uart_rcvd_dma(void *hdl, uint8_t *buffer, uint32_t buffer_len, uint32_t tmo, uint32_t *actual_rx_len)
Uart RX by DMA.
void hal_uart_putc(void *hdl, char c)
Output 1 char.
void * tx_arg
tx callback argument
Definition: hal_uart.h:195
void hal_uart_cancel(void *hdl)
cancel uart rx
int hal_uart_en_unbreak_int(void *hdl)
enable uart break interrupt
int no_intr
Uart don't generate interrupt.
Definition: hal_uart.h:191
int even_parity
Uart even parity, 0: odd parity, 1: even parity.
Definition: hal_uart.h:189
int hal_uart_xmit_intr(void *hdl, uint8_t *buffer, uint32_t buffer_len)
Uart TX by interrupt.