|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +// Copyright (c) 2024-2025 The Pybricks Authors |
| 3 | + |
| 4 | +#include "pbdrv/config.h" |
| 5 | + |
| 6 | +#if PBDRV_CONFIG_UART_DEBUG_FIRST_PORT |
| 7 | + |
| 8 | +#include <pbdrv/uart.h> |
| 9 | +#include <stdio.h> |
| 10 | +#include <stdarg.h> |
| 11 | +#include <string.h> |
| 12 | + |
| 13 | +PROCESS(pbdrv_uart_debug_process, "UART Debug"); |
| 14 | + |
| 15 | +#define BUF_SIZE (256) |
| 16 | + |
| 17 | +static uint8_t ring_buf[BUF_SIZE]; |
| 18 | +static size_t ring_head = 0; |
| 19 | +static size_t ring_tail = 0; |
| 20 | + |
| 21 | +static pbdrv_uart_dev_t *debug_uart = NULL; |
| 22 | + |
| 23 | +/** |
| 24 | + * Formats and stores a string in the UART debug ring buffer. |
| 25 | + * |
| 26 | + * This function works similarly to printf, but instead of printing to the |
| 27 | + * standard output. The formatted string will be written to the UART when the |
| 28 | + * buffer is processed. |
| 29 | + * |
| 30 | + * @param format The format string, similar to printf. |
| 31 | + * @param ... The variable arguments, similar to printf. |
| 32 | + */ |
| 33 | +void pbdrv_uart_debug_printf(const char *format, ...) { |
| 34 | + |
| 35 | + if (!debug_uart) { |
| 36 | + // Not initialized yet. |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + char buf[BUF_SIZE]; |
| 41 | + va_list args; |
| 42 | + va_start(args, format); |
| 43 | + vsnprintf(buf, sizeof(ring_buf), format, args); |
| 44 | + va_end(args); |
| 45 | + |
| 46 | + size_t len = strlen(buf); |
| 47 | + for (size_t i = 0; i < len; i++) { |
| 48 | + ring_buf[ring_head] = buf[i]; |
| 49 | + ring_head = (ring_head + 1) % BUF_SIZE; |
| 50 | + } |
| 51 | + |
| 52 | + // Request print process to write out new data. |
| 53 | + process_poll(&pbdrv_uart_debug_process); |
| 54 | +} |
| 55 | + |
| 56 | +void pbdrv_uart_debug_init(void) { |
| 57 | + process_start(&pbdrv_uart_debug_process); |
| 58 | +} |
| 59 | + |
| 60 | +static void pbdrv_uart_debug_process_poll(void *port) { |
| 61 | + // Ticks process along in between writing bytes. |
| 62 | + process_poll(&pbdrv_uart_debug_process); |
| 63 | +} |
| 64 | + |
| 65 | +PROCESS_THREAD(pbdrv_uart_debug_process, ev, data) { |
| 66 | + |
| 67 | + static struct pt child; |
| 68 | + static pbio_error_t err; |
| 69 | + static size_t write_size; |
| 70 | + |
| 71 | + PROCESS_BEGIN(); |
| 72 | + |
| 73 | + PROCESS_WAIT_EVENT_UNTIL({ |
| 74 | + process_poll(&pbdrv_uart_debug_process); |
| 75 | + pbdrv_uart_get(0, &debug_uart) == PBIO_SUCCESS; |
| 76 | + }); |
| 77 | + |
| 78 | + pbdrv_uart_set_baud_rate(debug_uart, 115200); |
| 79 | + pbdrv_uart_set_poll_callback(debug_uart, pbdrv_uart_debug_process_poll, NULL); |
| 80 | + |
| 81 | + for (;;) { |
| 82 | + |
| 83 | + PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_POLL); |
| 84 | + if (ring_head == ring_tail) { |
| 85 | + continue; |
| 86 | + } |
| 87 | + |
| 88 | + // Write up to the end of the buffer without wrapping. |
| 89 | + size_t end = ring_head > ring_tail ? ring_head: BUF_SIZE; |
| 90 | + write_size = end - ring_tail; |
| 91 | + PROCESS_PT_SPAWN(&child, pbdrv_uart_write(&child, debug_uart, &ring_buf[ring_tail], write_size, 100, &err)); |
| 92 | + ring_tail = (ring_tail + write_size) % BUF_SIZE; |
| 93 | + |
| 94 | + // Reset on failure. |
| 95 | + if (err != PBIO_SUCCESS) { |
| 96 | + ring_head = 0; |
| 97 | + ring_tail = 0; |
| 98 | + continue; |
| 99 | + } |
| 100 | + |
| 101 | + // Poll to write again if not fully finished, i.e. when wrapping. |
| 102 | + if (ring_head != ring_tail) { |
| 103 | + process_poll(&pbdrv_uart_debug_process); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + PROCESS_END(); |
| 108 | +} |
| 109 | + |
| 110 | +#endif // PBDRV_CONFIG_UART_DEBUG_FIRST_PORT |
0 commit comments