|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, Yonatan Schachter |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <drivers/uart.h> |
| 8 | +#include <drivers/pinctrl.h> |
| 9 | + |
| 10 | +#pragma GCC diagnostic push |
| 11 | +#pragma GCC diagnostic ignored "-Wunused-function" |
| 12 | +#include <hardware/uart.h> |
| 13 | +#include <hardware/gpio.h> |
| 14 | +#pragma GCC diagnostic pop |
| 15 | + |
| 16 | +struct uart_rpi_config { |
| 17 | +uart_inst_t *const uart_dev; |
| 18 | +uint32_t baudrate; |
| 19 | +const struct pinctrl_dev_config *pcfg; |
| 20 | +}; |
| 21 | + |
| 22 | +static int uart_rpi_poll_in(const struct device *dev, unsigned char *c) |
| 23 | +{ |
| 24 | +const struct uart_rpi_config *config = dev->config; |
| 25 | + |
| 26 | +if (!uart_is_readable(config->uart_dev)) { |
| 27 | +return -1; |
| 28 | +} |
| 29 | + |
| 30 | +*c = (unsigned char)uart_get_hw(config->uart_dev)->dr; |
| 31 | +return 0; |
| 32 | +} |
| 33 | + |
| 34 | +static void uart_rpi_poll_out(const struct device *dev, unsigned char c) |
| 35 | +{ |
| 36 | +const struct uart_rpi_config *config = dev->config; |
| 37 | + |
| 38 | +uart_putc_raw(config->uart_dev, c); |
| 39 | +} |
| 40 | + |
| 41 | +static int uart_rpi_init(const struct device *dev) |
| 42 | +{ |
| 43 | +const struct uart_rpi_config *config = dev->config; |
| 44 | +int ret; |
| 45 | + |
| 46 | +ret = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT); |
| 47 | +if (ret < 0) { |
| 48 | +return ret; |
| 49 | +} |
| 50 | + |
| 51 | +return !uart_init(config->uart_dev, config->baudrate); |
| 52 | +} |
| 53 | + |
| 54 | +static const struct uart_driver_api uart_rpi_driver_api = { |
| 55 | +.poll_in = uart_rpi_poll_in, |
| 56 | +.poll_out = uart_rpi_poll_out, |
| 57 | +}; |
| 58 | + |
| 59 | +#define DT_DRV_COMPAT raspberrypi_pico_uart |
| 60 | + |
| 61 | +#define RPI_UART_INIT(idx) \ |
| 62 | +PINCTRL_DT_INST_DEFINE(idx); \ |
| 63 | +static const struct uart_rpi_config uart_rpi_cfg_##idx = { \ |
| 64 | +.uart_dev = (uart_inst_t *)DT_INST_REG_ADDR(idx), \ |
| 65 | +.baudrate = DT_INST_PROP(idx, current_speed), \ |
| 66 | +.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(idx), \ |
| 67 | +}; \ |
| 68 | +\ |
| 69 | +DEVICE_DT_INST_DEFINE(idx, &uart_rpi_init, \ |
| 70 | +NULL, \ |
| 71 | +NULL, \ |
| 72 | +&uart_rpi_cfg_##idx, PRE_KERNEL_1, \ |
| 73 | +CONFIG_KERNEL_INIT_PRIORITY_DEVICE, \ |
| 74 | +&uart_rpi_driver_api); |
| 75 | + |
| 76 | +DT_INST_FOREACH_STATUS_OKAY(RPI_UART_INIT) |
0 commit comments