|
| 1 | + |
| 2 | +#include "xs.h" |
| 3 | +#include "xsHost.h" |
| 4 | + |
| 5 | +#include <zephyr/kernel.h> |
| 6 | +#include <zephyr/device.h> |
| 7 | +#include <zephyr/drivers/uart.h> |
| 8 | +#include <zephyr/sys/ring_buffer.h> |
| 9 | + |
| 10 | +#include "mc.defines.h" |
| 11 | + |
| 12 | +#ifndef DEBUGGER_SPEED |
| 13 | + #define DEBUGGER_SPEED 115200 // in .dts? |
| 14 | +#endif |
| 15 | + |
| 16 | +#define DEBUGGER_STACK 2048 |
| 17 | +#define DEBUGGER_PRIORITY 3 |
| 18 | + |
| 19 | +k_tid_t gDebugTID; |
| 20 | +struct k_thread gDebugThread; |
| 21 | +K_THREAD_STACK_DEFINE(gDebugStack, DEBUGGER_STACK); |
| 22 | + |
| 23 | +#define DEBUG_QUEUE_LEN 2 |
| 24 | + |
| 25 | +#define RING_BUF_SIZE 1024 |
| 26 | +static struct ring_buf m_rx_ringbuf; |
| 27 | +static uint8_t rx_ringbuf_buffer[RING_BUF_SIZE]; |
| 28 | + |
| 29 | +#define UART_DEVICE_NODE DT_CHOSEN(zephyr_shell_uart) |
| 30 | +static const struct device *const uart_dev = DEVICE_DT_GET(UART_DEVICE_NODE); |
| 31 | + |
| 32 | +static uint8_t gNotifyOutstanding = 0; |
| 33 | +struct k_msgq dbgServiceQueue; |
| 34 | + |
| 35 | +static void die(char *x) { |
| 36 | + while (1) |
| 37 | + ; |
| 38 | +} |
| 39 | + |
| 40 | +void serial_cb(const struct device *dev, void *user_data) |
| 41 | +{ |
| 42 | + if (!uart_irq_update(uart_dev)) |
| 43 | + return; |
| 44 | + |
| 45 | + while (uart_irq_rx_ready(uart_dev)) { |
| 46 | + uint8_t buf[32]; |
| 47 | + |
| 48 | + uint32_t space = ring_buf_space_get(&m_rx_ringbuf); |
| 49 | + if (space == 0) |
| 50 | + die("rx ring buffer full"); |
| 51 | + |
| 52 | + uint32_t use = (space < sizeof(buf)) ? space : sizeof(buf); |
| 53 | + uint32_t read = uart_fifo_read(uart_dev, buf, use); |
| 54 | + if (read > 0) { |
| 55 | + uint32_t written = ring_buf_put(&m_rx_ringbuf, buf, read); |
| 56 | + if (written != read) |
| 57 | + die("rx ring buffer full"); |
| 58 | + if (!gNotifyOutstanding) { |
| 59 | + gNotifyOutstanding = 1; |
| 60 | + k_msgq_put(&dbgServiceQueue, &gNotifyOutstanding, K_NO_WAIT); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +static void debugLoop(void *a, void *b, void *c) |
| 67 | +{ |
| 68 | + uint32_t msg; |
| 69 | + uint32_t *running = a; |
| 70 | + |
| 71 | + int ret = uart_irq_callback_user_data_set(uart_dev, serial_cb, NULL); |
| 72 | + if (ret < 0) { |
| 73 | + if ((ret == -ENOTSUP) || (ret == -ENOSYS)) |
| 74 | + printk("Interrupt-driven UART not enabled.\n"); |
| 75 | + else |
| 76 | + printk("error setting UART callback: %d\n", ret); |
| 77 | + *running = -1; |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + uart_irq_rx_enable(uart_dev); |
| 82 | + *running = 1; |
| 83 | + |
| 84 | + while (true) { |
| 85 | + int err = k_msgq_get(&dbgServiceQueue, &msg, K_MSEC(10000)); |
| 86 | + if (err) continue; |
| 87 | + |
| 88 | + gNotifyOutstanding = 0; |
| 89 | +#ifdef mxDebug |
| 90 | + if (ring_buf_size_get(&m_rx_ringbuf) > 0) |
| 91 | + fxReceiveLoop(); |
| 92 | +#endif |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | + |
| 97 | +void setupDebugger(uint32_t *running) |
| 98 | +{ |
| 99 | + if (!device_is_ready(uart_dev)) { |
| 100 | + printk("UART device not found!\n"); |
| 101 | + *running = -1; |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + k_msgq_alloc_init(&dbgServiceQueue, sizeof(uint8_t), DEBUG_QUEUE_LEN); |
| 106 | + |
| 107 | + // initial ringbuf |
| 108 | + ring_buf_init(&m_rx_ringbuf, sizeof(rx_ringbuf_buffer), rx_ringbuf_buffer); |
| 109 | + |
| 110 | + // make debugger thread |
| 111 | + gDebugTID = k_thread_create(&gDebugThread, gDebugStack, K_THREAD_STACK_SIZEOF(gDebugStack), debugLoop, running, NULL, NULL, DEBUGGER_PRIORITY, 0, K_NO_WAIT); |
| 112 | +} |
| 113 | + |
| 114 | +void ESP_put(uint8_t *c, int count) |
| 115 | +{ |
| 116 | + while (count) { |
| 117 | + ESP_putc(*c++); |
| 118 | + count--; |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +void ESP_putc(int c) |
| 123 | +{ |
| 124 | + uint8_t ch = c; |
| 125 | + |
| 126 | + uart_poll_out(uart_dev, ch); |
| 127 | +} |
| 128 | + |
| 129 | +int ESP_getc(void) |
| 130 | +{ |
| 131 | + uint8_t c; |
| 132 | + uint32_t len; |
| 133 | + |
| 134 | + len = ring_buf_get(&m_rx_ringbuf, &c, 1); |
| 135 | + if (1 == len) |
| 136 | + return c; |
| 137 | + |
| 138 | + return -1; |
| 139 | +} |
| 140 | + |
| 141 | + |
0 commit comments