|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2021 Raspberry Pi (Trading) Ltd. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + * |
| 24 | + */ |
| 25 | + |
| 26 | +#include <pico/stdlib.h> |
| 27 | + |
| 28 | +#include "tusb.h" |
| 29 | + |
| 30 | +#include "picoprobe_config.h" |
| 31 | + |
| 32 | +void cdc_uart_init(void) { |
| 33 | + gpio_set_function(PICOPROBE_UART_TX, GPIO_FUNC_UART); |
| 34 | + gpio_set_function(PICOPROBE_UART_RX, GPIO_FUNC_UART); |
| 35 | + uart_init(PICOPROBE_UART_INTERFACE, PICOPROBE_UART_BAUDRATE); |
| 36 | +} |
| 37 | + |
| 38 | +#define MAX_UART_PKT 64 |
| 39 | +void cdc_task(void) { |
| 40 | + uint8_t rx_buf[MAX_UART_PKT]; |
| 41 | + uint8_t tx_buf[MAX_UART_PKT]; |
| 42 | + |
| 43 | + // Consume uart fifo regardless even if not connected |
| 44 | + uint rx_len = 0; |
| 45 | + while(uart_is_readable(PICOPROBE_UART_INTERFACE) && (rx_len < MAX_UART_PKT)) { |
| 46 | + rx_buf[rx_len++] = uart_getc(PICOPROBE_UART_INTERFACE); |
| 47 | + } |
| 48 | + |
| 49 | + if (tud_cdc_connected()) { |
| 50 | + // Do we have anything to display on the host's terminal? |
| 51 | + if (rx_len) { |
| 52 | + for (uint i = 0; i < rx_len; i++) { |
| 53 | + tud_cdc_write_char(rx_buf[i]); |
| 54 | + } |
| 55 | + tud_cdc_write_flush(); |
| 56 | + } |
| 57 | + |
| 58 | + if (tud_cdc_available()) { |
| 59 | + // Is there any data from the host for us to tx |
| 60 | + uint tx_len = tud_cdc_read(tx_buf, sizeof(tx_buf)); |
| 61 | + uart_write_blocking(PICOPROBE_UART_INTERFACE, tx_buf, tx_len); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +void tud_cdc_line_coding_cb(uint8_t itf, cdc_line_coding_t const* line_coding) { |
| 67 | + picoprobe_info("New baud rate %d\n", line_coding->bit_rate); |
| 68 | + uart_init(PICOPROBE_UART_INTERFACE, line_coding->bit_rate); |
| 69 | +} |
0 commit comments