Lines Matching +full:uart +full:- +full:dev
4 * SPDX-License-Identifier: Apache-2.0
11 #include <zephyr/drivers/uart.h>
20 /* Get UART instance */
21 #define GET_UART(dev) ((volatile struct uart_b91_t *) \ argument
22 ((const struct uart_b91_config *)dev->config)->uart_addr)
24 /* UART TX buffer count max value */
27 /* UART TX/RX data registers size */
44 /* B91 UART registers structure */
59 /* B91 UART data structure */
70 /* B91 UART config structure */
109 static inline uint8_t uart_b91_get_tx_bufcnt(volatile struct uart_b91_t *uart) in uart_b91_get_tx_bufcnt() argument
111 return (uart->bufcnt & FLD_UART_TX_BUF_CNT) >> FLD_UART_TX_BUF_CNT_OFFSET; in uart_b91_get_tx_bufcnt()
115 static inline uint8_t uart_b91_get_rx_bufcnt(volatile struct uart_b91_t *uart) in uart_b91_get_rx_bufcnt() argument
117 return (uart->bufcnt & FLD_UART_RX_BUF_CNT) >> FLD_UART_RX_BUF_CNT_OFFSET; in uart_b91_get_rx_bufcnt()
151 primeDec = 10 * pclk / baudrate - 10 * primeInt; in uart_b91_cal_div_and_bwpc()
158 primeInt -= 1; in uart_b91_cal_div_and_bwpc()
163 D_intdec[i - 3] = (10 * primeInt) / (i + 1); in uart_b91_cal_div_and_bwpc()
164 D_dec[i - 3] = D_intdec[i - 3] - 10 * (D_intdec[i - 3] / 10); in uart_b91_cal_div_and_bwpc()
165 D_int[i - 3] = D_intdec[i - 3] / 10; in uart_b91_cal_div_and_bwpc()
184 if (D_dec[position_min] < (10 - D_dec[position_max])) { in uart_b91_cal_div_and_bwpc()
186 *divider = D_int[position_min] - 1; in uart_b91_cal_div_and_bwpc()
193 *divider = D_int[position_min] - 1; in uart_b91_cal_div_and_bwpc()
200 /* Initializes the UART instance */
201 static void uart_b91_init(volatile struct uart_b91_t *uart, uint16_t divider, in uart_b91_init() argument
206 uart->ctrl0 = bwpc; in uart_b91_init()
207 uart->clk_div = divider; in uart_b91_init()
212 uart->ctrl1 |= FLD_UART_PARITY_ENABLE; in uart_b91_init()
216 uart->ctrl1 &= (~FLD_UART_PARITY_POLARITY); in uart_b91_init()
219 uart->ctrl1 |= FLD_UART_PARITY_POLARITY; in uart_b91_init()
222 uart->ctrl1 &= (~FLD_UART_PARITY_ENABLE); /* disable parity function */ in uart_b91_init()
226 uart->ctrl1 &= (~FLD_UART_STOP_SEL); in uart_b91_init()
227 uart->ctrl1 |= stop_bit; in uart_b91_init()
231 static void uart_b91_irq_handler(const struct device *dev) in uart_b91_irq_handler() argument
234 ARG_UNUSED(dev); in uart_b91_irq_handler()
236 struct uart_b91_data *data = dev->data; in uart_b91_irq_handler()
238 if (data->callback != NULL) { in uart_b91_irq_handler()
239 data->callback(dev, data->cb_data); in uart_b91_irq_handler()
246 static int uart_b91_configure(const struct device *dev, in uart_b91_configure() argument
249 struct uart_b91_data *data = dev->data; in uart_b91_configure()
255 volatile struct uart_b91_t *uart = GET_UART(dev); in uart_b91_configure() local
258 if (cfg->parity == UART_CFG_PARITY_NONE) { in uart_b91_configure()
260 } else if (cfg->parity == UART_CFG_PARITY_ODD) { in uart_b91_configure()
262 } else if (cfg->parity == UART_CFG_PARITY_EVEN) { in uart_b91_configure()
265 return -ENOTSUP; in uart_b91_configure()
269 if (cfg->stop_bits == UART_CFG_STOP_BITS_1) { in uart_b91_configure()
271 } else if (cfg->stop_bits == UART_CFG_STOP_BITS_1_5) { in uart_b91_configure()
273 } else if (cfg->stop_bits == UART_CFG_STOP_BITS_2) { in uart_b91_configure()
276 return -ENOTSUP; in uart_b91_configure()
280 if (cfg->flow_ctrl != UART_CFG_FLOW_CTRL_NONE) { in uart_b91_configure()
281 return -ENOTSUP; in uart_b91_configure()
284 /* UART configure */ in uart_b91_configure()
285 uart_b91_cal_div_and_bwpc(cfg->baudrate, sys_clk.pclk * 1000 * 1000, ÷r, &bwpc); in uart_b91_configure()
286 uart_b91_init(uart, divider, bwpc, parity, stop_bits); in uart_b91_configure()
289 data->cfg = *cfg; in uart_b91_configure()
295 static int uart_b91_config_get(const struct device *dev, in uart_b91_config_get() argument
298 struct uart_b91_data *data = dev->data; in uart_b91_config_get()
300 *cfg = data->cfg; in uart_b91_config_get()
307 static int uart_b91_driver_init(const struct device *dev) in uart_b91_driver_init() argument
312 volatile struct uart_b91_t *uart = GET_UART(dev); in uart_b91_driver_init() local
313 const struct uart_b91_config *cfg = dev->config; in uart_b91_driver_init()
314 struct uart_b91_data *data = dev->data; in uart_b91_driver_init()
317 uart->status |= UART_RX_RESET_BIT | UART_TX_RESET_BIT; in uart_b91_driver_init()
318 data->rx_byte_index = 0; in uart_b91_driver_init()
319 data->tx_byte_index = 0; in uart_b91_driver_init()
322 status = pinctrl_apply_state(cfg->pcfg, PINCTRL_STATE_DEFAULT); in uart_b91_driver_init()
327 uart_b91_cal_div_and_bwpc(cfg->baud_rate, sys_clk.pclk * 1000 * 1000, ÷r, &bwpc); in uart_b91_driver_init()
328 uart_b91_init(uart, divider, bwpc, UART_PARITY_NONE, UART_STOP_BIT_1); in uart_b91_driver_init()
331 cfg->pirq_connect(); in uart_b91_driver_init()
338 static void uart_b91_poll_out(const struct device *dev, uint8_t c) in uart_b91_poll_out() argument
340 volatile struct uart_b91_t *uart = GET_UART(dev); in uart_b91_poll_out() local
341 struct uart_b91_data *data = dev->data; in uart_b91_poll_out()
343 while (uart_b91_get_tx_bufcnt(uart) >= UART_TX_BUF_CNT) { in uart_b91_poll_out()
346 uart->data_buf[data->tx_byte_index] = c; in uart_b91_poll_out()
347 data->tx_byte_index = (data->tx_byte_index + 1) % ARRAY_SIZE(uart->data_buf); in uart_b91_poll_out()
351 static int uart_b91_poll_in(const struct device *dev, unsigned char *c) in uart_b91_poll_in() argument
353 volatile struct uart_b91_t *uart = GET_UART(dev); in uart_b91_poll_in() local
354 struct uart_b91_data *data = dev->data; in uart_b91_poll_in()
356 if (uart_b91_get_rx_bufcnt(uart) == 0) { in uart_b91_poll_in()
357 return -1; in uart_b91_poll_in()
360 *c = uart->data_buf[data->rx_byte_index]; in uart_b91_poll_in()
361 data->rx_byte_index = (data->rx_byte_index + 1) % ARRAY_SIZE(uart->data_buf); in uart_b91_poll_in()
367 static int uart_b91_err_check(const struct device *dev) in uart_b91_err_check() argument
369 volatile struct uart_b91_t *uart = GET_UART(dev); in uart_b91_err_check() local
371 return ((uart->status & UART_RX_ERR_STATUS) != 0) ? 1 : 0; in uart_b91_err_check()
377 static int uart_b91_fifo_fill(const struct device *dev, in uart_b91_fifo_fill() argument
382 volatile struct uart_b91_t *uart = GET_UART(dev); in uart_b91_fifo_fill() local
389 if (uart_b91_get_rx_bufcnt(uart) != 0) { in uart_b91_fifo_fill()
393 uart_b91_poll_out(dev, tx_data[i]); in uart_b91_fifo_fill()
400 static int uart_b91_fifo_read(const struct device *dev, in uart_b91_fifo_read() argument
405 volatile struct uart_b91_t *uart = GET_UART(dev); in uart_b91_fifo_read() local
408 if (uart_b91_get_rx_bufcnt(uart) == 0) { in uart_b91_fifo_read()
412 uart_b91_poll_in(dev, &rx_data[rx_count]); in uart_b91_fifo_read()
419 static void uart_b91_irq_tx_enable(const struct device *dev) in uart_b91_irq_tx_enable() argument
421 volatile struct uart_b91_t *uart = GET_UART(dev); in uart_b91_irq_tx_enable() local
423 uart->ctrl3 = (uart->ctrl3 & (~FLD_UART_TX_IRQ_TRIQ_LEV)) | in uart_b91_irq_tx_enable()
425 uart->ctrl0 |= UART_TX_IRQ_MASK; in uart_b91_irq_tx_enable()
429 static void uart_b91_irq_tx_disable(const struct device *dev) in uart_b91_irq_tx_disable() argument
431 volatile struct uart_b91_t *uart = GET_UART(dev); in uart_b91_irq_tx_disable() local
433 uart->ctrl0 &= ~UART_TX_IRQ_MASK; in uart_b91_irq_tx_disable()
437 static int uart_b91_irq_tx_ready(const struct device *dev) in uart_b91_irq_tx_ready() argument
439 volatile struct uart_b91_t *uart = GET_UART(dev); in uart_b91_irq_tx_ready() local
441 return ((uart_b91_get_tx_bufcnt(uart) < UART_TX_BUF_CNT) && in uart_b91_irq_tx_ready()
442 ((uart->ctrl0 & UART_TX_IRQ_MASK) != 0)) ? 1 : 0; in uart_b91_irq_tx_ready()
446 static int uart_b91_irq_tx_complete(const struct device *dev) in uart_b91_irq_tx_complete() argument
448 volatile struct uart_b91_t *uart = GET_UART(dev); in uart_b91_irq_tx_complete() local
450 return (uart_b91_get_tx_bufcnt(uart) == 0) ? 1 : 0; in uart_b91_irq_tx_complete()
454 static void uart_b91_irq_rx_enable(const struct device *dev) in uart_b91_irq_rx_enable() argument
456 volatile struct uart_b91_t *uart = GET_UART(dev); in uart_b91_irq_rx_enable() local
458 uart->ctrl3 = (uart->ctrl3 & (~FLD_UART_RX_IRQ_TRIQ_LEV)) | in uart_b91_irq_rx_enable()
460 uart->ctrl0 |= UART_RX_IRQ_MASK; in uart_b91_irq_rx_enable()
464 static void uart_b91_irq_rx_disable(const struct device *dev) in uart_b91_irq_rx_disable() argument
466 volatile struct uart_b91_t *uart = GET_UART(dev); in uart_b91_irq_rx_disable() local
468 uart->ctrl0 &= ~UART_RX_IRQ_MASK; in uart_b91_irq_rx_disable()
472 static int uart_b91_irq_rx_ready(const struct device *dev) in uart_b91_irq_rx_ready() argument
474 volatile struct uart_b91_t *uart = GET_UART(dev); in uart_b91_irq_rx_ready() local
476 return (uart_b91_get_rx_bufcnt(uart) > 0) ? 1 : 0; in uart_b91_irq_rx_ready()
480 static void uart_b91_irq_err_enable(const struct device *dev) in uart_b91_irq_err_enable() argument
482 volatile struct uart_b91_t *uart = GET_UART(dev); in uart_b91_irq_err_enable() local
484 uart->rxtimeout |= UART_ERR_IRQ_MASK; in uart_b91_irq_err_enable()
488 static void uart_b91_irq_err_disable(const struct device *dev) in uart_b91_irq_err_disable() argument
490 volatile struct uart_b91_t *uart = GET_UART(dev); in uart_b91_irq_err_disable() local
492 uart->rxtimeout &= ~UART_ERR_IRQ_MASK; in uart_b91_irq_err_disable()
496 static int uart_b91_irq_is_pending(const struct device *dev) in uart_b91_irq_is_pending() argument
498 volatile struct uart_b91_t *uart = GET_UART(dev); in uart_b91_irq_is_pending() local
500 return ((uart->status & UART_IRQ_STATUS) != 0) ? 1 : 0; in uart_b91_irq_is_pending()
504 static int uart_b91_irq_update(const struct device *dev) in uart_b91_irq_update() argument
506 ARG_UNUSED(dev); in uart_b91_irq_update()
513 static void uart_b91_irq_callback_set(const struct device *dev, in uart_b91_irq_callback_set() argument
517 struct uart_b91_data *data = dev->data; in uart_b91_irq_callback_set()
519 data->callback = cb; in uart_b91_irq_callback_set()
520 data->cb_data = cb_data; in uart_b91_irq_callback_set()
525 static DEVICE_API(uart, uart_b91_driver_api) = {