Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | /*
* Copyright (c) 2022 Libre Solar Technologies GmbH
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT espressif_esp32_usb_serial
#include <hal/usb_serial_jtag_ll.h>
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <errno.h>
#include <soc.h>
#include <zephyr/drivers/uart.h>
#if defined(CONFIG_SOC_SERIES_ESP32C3)
#include <zephyr/drivers/interrupt_controller/intc_esp32c3.h>
#else
#include <zephyr/drivers/interrupt_controller/intc_esp32.h>
#endif
#include <zephyr/drivers/clock_control.h>
#include <zephyr/sys/util.h>
#include <esp_attr.h>
#ifdef CONFIG_SOC_SERIES_ESP32C3
#define ISR_HANDLER isr_handler_t
#else
#define ISR_HANDLER intr_handler_t
#endif
/*
* Timeout after which the poll_out function stops waiting for space in the tx fifo.
*
* Without this timeout, the function would get stuck forever and block the processor if no host is
* connected to the USB port.
*
* USB full-speed uses a frame rate of 1 ms. Thus, a timeout of 50 ms provides plenty of safety
* margin even for a loaded bus. This is the same value as used in the ESP-IDF.
*/
#define USBSERIAL_POLL_OUT_TIMEOUT_MS (50U)
struct serial_esp32_usb_config {
const struct device *clock_dev;
const clock_control_subsys_t clock_subsys;
int irq_source;
};
struct serial_esp32_usb_data {
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
uart_irq_callback_user_data_t irq_cb;
void *irq_cb_data;
#endif
int irq_line;
int64_t last_tx_time;
};
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
static void serial_esp32_usb_isr(void *arg);
#endif
static int serial_esp32_usb_poll_in(const struct device *dev, unsigned char *p_char)
{
struct serial_esp32_usb_data *data = dev->data;
if (!usb_serial_jtag_ll_rxfifo_data_available()) {
return -1;
}
usb_serial_jtag_ll_read_rxfifo(p_char, 1);
return 0;
}
static void serial_esp32_usb_poll_out(const struct device *dev, unsigned char c)
{
struct serial_esp32_usb_data *data = dev->data;
/*
* If there is no USB host connected, this function will busy-wait once for the timeout
* period, but return immediately for subsequent calls.
*/
do {
if (usb_serial_jtag_ll_txfifo_writable()) {
usb_serial_jtag_ll_write_txfifo(&c, 1);
usb_serial_jtag_ll_txfifo_flush();
data->last_tx_time = k_uptime_get();
return;
}
} while ((k_uptime_get() - data->last_tx_time) < USBSERIAL_POLL_OUT_TIMEOUT_MS);
}
static int serial_esp32_usb_err_check(const struct device *dev)
{
ARG_UNUSED(dev);
return 0;
}
static int serial_esp32_usb_init(const struct device *dev)
{
const struct serial_esp32_usb_config *config = dev->config;
struct serial_esp32_usb_data *data = dev->data;
if (!device_is_ready(config->clock_dev)) {
return -ENODEV;
}
int ret = clock_control_on(config->clock_dev, config->clock_subsys);
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
data->irq_line = esp_intr_alloc(config->irq_source, 0, (ISR_HANDLER)serial_esp32_usb_isr,
(void *)dev, NULL);
#endif
return ret;
}
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
static int serial_esp32_usb_fifo_fill(const struct device *dev, const uint8_t *tx_data, int len)
{
ARG_UNUSED(dev);
int ret = usb_serial_jtag_ll_write_txfifo(tx_data, len);
usb_serial_jtag_ll_txfifo_flush();
return ret;
}
static int serial_esp32_usb_fifo_read(const struct device *dev, uint8_t *rx_data, const int len)
{
ARG_UNUSED(dev);
return usb_serial_jtag_ll_read_rxfifo(rx_data, len);
}
static void serial_esp32_usb_irq_tx_enable(const struct device *dev)
{
struct serial_esp32_usb_data *data = dev->data;
usb_serial_jtag_ll_clr_intsts_mask(USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY);
usb_serial_jtag_ll_ena_intr_mask(USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY);
if (data->irq_cb != NULL) {
data->irq_cb(dev, data->irq_cb_data);
}
}
static void serial_esp32_usb_irq_tx_disable(const struct device *dev)
{
ARG_UNUSED(dev);
usb_serial_jtag_ll_disable_intr_mask(USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY);
}
static int serial_esp32_usb_irq_tx_ready(const struct device *dev)
{
ARG_UNUSED(dev);
return (usb_serial_jtag_ll_txfifo_writable() &&
usb_serial_jtag_ll_get_intr_ena_status() & USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY);
}
static void serial_esp32_usb_irq_rx_enable(const struct device *dev)
{
ARG_UNUSED(dev);
usb_serial_jtag_ll_clr_intsts_mask(USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT);
usb_serial_jtag_ll_ena_intr_mask(USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT);
}
static void serial_esp32_usb_irq_rx_disable(const struct device *dev)
{
ARG_UNUSED(dev);
usb_serial_jtag_ll_disable_intr_mask(USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT);
}
static int serial_esp32_usb_irq_tx_complete(const struct device *dev)
{
ARG_UNUSED(dev);
return usb_serial_jtag_ll_txfifo_writable();
}
static int serial_esp32_usb_irq_rx_ready(const struct device *dev)
{
ARG_UNUSED(dev);
return usb_serial_jtag_ll_rxfifo_data_available();
}
static void serial_esp32_usb_irq_err_enable(const struct device *dev)
{
ARG_UNUSED(dev);
}
static void serial_esp32_usb_irq_err_disable(const struct device *dev)
{
ARG_UNUSED(dev);
}
static int serial_esp32_usb_irq_is_pending(const struct device *dev)
{
return serial_esp32_usb_irq_rx_ready(dev) || serial_esp32_usb_irq_tx_ready(dev);
}
static int serial_esp32_usb_irq_update(const struct device *dev)
{
ARG_UNUSED(dev);
usb_serial_jtag_ll_clr_intsts_mask(USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT);
usb_serial_jtag_ll_clr_intsts_mask(USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY);
return 1;
}
static void serial_esp32_usb_irq_callback_set(const struct device *dev,
uart_irq_callback_user_data_t cb, void *cb_data)
{
struct serial_esp32_usb_data *data = dev->data;
data->irq_cb = cb;
data->irq_cb_data = cb_data;
}
static void serial_esp32_usb_isr(void *arg)
{
const struct device *dev = (const struct device *)arg;
struct serial_esp32_usb_data *data = dev->data;
uint32_t uart_intr_status = usb_serial_jtag_ll_get_intsts_mask();
if (uart_intr_status == 0) {
return;
}
usb_serial_jtag_ll_clr_intsts_mask(uart_intr_status);
if (data->irq_cb != NULL) {
data->irq_cb(dev, data->irq_cb_data);
}
}
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */
static const DRAM_ATTR struct uart_driver_api serial_esp32_usb_api = {
.poll_in = serial_esp32_usb_poll_in,
.poll_out = serial_esp32_usb_poll_out,
.err_check = serial_esp32_usb_err_check,
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
.fifo_fill = serial_esp32_usb_fifo_fill,
.fifo_read = serial_esp32_usb_fifo_read,
.irq_tx_enable = serial_esp32_usb_irq_tx_enable,
.irq_tx_disable = serial_esp32_usb_irq_tx_disable,
.irq_tx_ready = serial_esp32_usb_irq_tx_ready,
.irq_rx_enable = serial_esp32_usb_irq_rx_enable,
.irq_rx_disable = serial_esp32_usb_irq_rx_disable,
.irq_tx_complete = serial_esp32_usb_irq_tx_complete,
.irq_rx_ready = serial_esp32_usb_irq_rx_ready,
.irq_err_enable = serial_esp32_usb_irq_err_enable,
.irq_err_disable = serial_esp32_usb_irq_err_disable,
.irq_is_pending = serial_esp32_usb_irq_is_pending,
.irq_update = serial_esp32_usb_irq_update,
.irq_callback_set = serial_esp32_usb_irq_callback_set,
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */
};
static const DRAM_ATTR struct serial_esp32_usb_config serial_esp32_usb_cfg = {
.clock_dev = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(0)),
.clock_subsys = (clock_control_subsys_t)DT_INST_CLOCKS_CELL(0, offset),
.irq_source = DT_INST_IRQN(0)
};
static struct serial_esp32_usb_data serial_esp32_usb_data_0;
DEVICE_DT_INST_DEFINE(0, &serial_esp32_usb_init, NULL, &serial_esp32_usb_data_0,
&serial_esp32_usb_cfg, PRE_KERNEL_1,
CONFIG_SERIAL_INIT_PRIORITY, &serial_esp32_usb_api);
|