|
| 1 | +// This file is part of the CircuitPython project: https://circuitpython.org |
| 2 | +// |
| 3 | +// SPDX-FileCopyrightText: Copyright (c) 2017 Scott Shawcroft for Adafruit Industries |
| 4 | +// |
| 5 | +// SPDX-License-Identifier: MIT |
| 6 | + |
| 7 | +#include "supervisor/shared/serial.h" |
| 8 | +#include "py/ringbuf.h" |
| 9 | + |
| 10 | +#include <zephyr/autoconf.h> |
| 11 | + |
| 12 | +// #if defined(CONFIG_CONSOLE_SUBSYS) |
| 13 | + |
| 14 | +#include <zephyr/console/console.h> |
| 15 | +#include <zephyr/sys/ring_buffer.h> |
| 16 | + |
| 17 | +// init the console ourselves so we can set the timeout to zero. |
| 18 | +static struct tty_serial console_serial; |
| 19 | + |
| 20 | +static uint8_t console_rxbuf[CONFIG_CONSOLE_GETCHAR_BUFSIZE]; |
| 21 | +static uint8_t console_txbuf[CONFIG_CONSOLE_PUTCHAR_BUFSIZE]; |
| 22 | + |
| 23 | +void port_serial_early_init(void) { |
| 24 | + const struct device *uart_dev; |
| 25 | + int ret; |
| 26 | + |
| 27 | + uart_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_console)); |
| 28 | + if (!device_is_ready(uart_dev)) { |
| 29 | + return -ENODEV; |
| 30 | + } |
| 31 | + |
| 32 | + ret = tty_init(&console_serial, uart_dev); |
| 33 | + |
| 34 | + if (ret) { |
| 35 | + return ret; |
| 36 | + } |
| 37 | + |
| 38 | + /* Checks device driver supports for interrupt driven data transfers. */ |
| 39 | + if (CONFIG_CONSOLE_GETCHAR_BUFSIZE + CONFIG_CONSOLE_PUTCHAR_BUFSIZE) { |
| 40 | + const struct uart_driver_api *api = |
| 41 | + (const struct uart_driver_api *)uart_dev->api; |
| 42 | + if (!api->irq_callback_set) { |
| 43 | + return -ENOTSUP; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + tty_set_tx_buf(&console_serial, console_txbuf, sizeof(console_txbuf)); |
| 48 | + tty_set_rx_buf(&console_serial, console_rxbuf, sizeof(console_rxbuf)); |
| 49 | + |
| 50 | + printk("port_serial_early_init done"); |
| 51 | +} |
| 52 | + |
| 53 | +void port_serial_init(void) { |
| 54 | +} |
| 55 | + |
| 56 | +bool port_serial_connected(void) { |
| 57 | + return true; |
| 58 | +} |
| 59 | + |
| 60 | +char port_serial_read(void) { |
| 61 | + char buf[1]; |
| 62 | + ssize_t read = tty_read(&console_serial, buf, 1); |
| 63 | + if (read == 0) { |
| 64 | + return -1; |
| 65 | + } |
| 66 | + return buf[0]; |
| 67 | +} |
| 68 | + |
| 69 | +uint32_t port_serial_bytes_available(void) { |
| 70 | + _fill_ring_buf(); |
| 71 | + return ring_buf_size_get(&rb); |
| 72 | +} |
| 73 | + |
| 74 | +void port_serial_write_substring(const char *text, uint32_t length) { |
| 75 | + uint32_t total_written = 0; |
| 76 | + while (total_written < length) { |
| 77 | + total_written += tty_write(NULL, text + total_written, length - total_written); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +// #endif |
0 commit comments