|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, Yonatan Schachter |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <drivers/uart.h> |
| 8 | +#include <hardware/uart.h> |
| 9 | +#include <hardware/gpio.h> |
| 10 | + |
| 11 | +struct uart_rpi_config { |
| 12 | + uart_inst_t *const uart_dev; |
| 13 | + uint32_t baudrate; |
| 14 | + uint32_t tx_pin; |
| 15 | + uint32_t rx_pin; |
| 16 | +}; |
| 17 | + |
| 18 | +struct uart_rpi_data { |
| 19 | + |
| 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 | + *c = (unsigned char)uart_get_hw(config->uart_dev)->dr; |
| 30 | + return 0; |
| 31 | +} |
| 32 | + |
| 33 | +static void uart_rpi_poll_out(const struct device *dev, unsigned char c) |
| 34 | +{ |
| 35 | + const struct uart_rpi_config *config = dev->config; |
| 36 | + |
| 37 | + uart_putc_raw(config->uart_dev, c); |
| 38 | +} |
| 39 | + |
| 40 | +static int uart_rpi_init(const struct device *dev) |
| 41 | +{ |
| 42 | + const struct uart_rpi_config *config = dev->config; |
| 43 | + |
| 44 | + gpio_init(config->tx_pin); |
| 45 | + gpio_init(config->rx_pin); |
| 46 | + gpio_set_function(config->tx_pin, GPIO_FUNC_UART); |
| 47 | + gpio_set_function(config->rx_pin, GPIO_FUNC_UART); |
| 48 | + |
| 49 | + return !uart_init(config->uart_dev, config->baudrate); |
| 50 | +} |
| 51 | + |
| 52 | +static const struct uart_driver_api uart_rpi_driver_api = { |
| 53 | + .poll_in = uart_rpi_poll_in, |
| 54 | + .poll_out = uart_rpi_poll_out, |
| 55 | +}; |
| 56 | + |
| 57 | +#undef DT_DRV_COMPAT |
| 58 | +#define DT_DRV_COMPAT raspberrypi_rp2_uart |
| 59 | + |
| 60 | +#define RPI_UART_INIT(idx) \ |
| 61 | + static const struct uart_rpi_config uart_rpi_cfg_##idx = { \ |
| 62 | + .uart_dev = (uart_inst_t *)DT_INST_REG_ADDR(idx), \ |
| 63 | + .baudrate = DT_INST_PROP(idx, current_speed), \ |
| 64 | + .tx_pin = DT_INST_PROP(idx, tx_pin), \ |
| 65 | + .rx_pin = DT_INST_PROP(idx, rx_pin), \ |
| 66 | + }; \ |
| 67 | + \ |
| 68 | + static struct uart_rpi_data uart_rpi_data_##idx; \ |
| 69 | + \ |
| 70 | + DEVICE_DT_INST_DEFINE(idx, &uart_rpi_init, \ |
| 71 | + device_pm_control_nop, \ |
| 72 | + &uart_rpi_data_##idx, \ |
| 73 | + &uart_rpi_cfg_##idx, PRE_KERNEL_1, \ |
| 74 | + CONFIG_KERNEL_INIT_PRIORITY_DEVICE, \ |
| 75 | + &uart_rpi_driver_api); |
| 76 | + |
| 77 | +DT_INST_FOREACH_STATUS_OKAY(RPI_UART_INIT) |
0 commit comments