├── .github └── workflows │ └── ci.yml ├── .gitignore ├── .gitmodules ├── CMakeLists.txt ├── LICENSE.md ├── README.md ├── build.sh ├── tusb_config.h ├── uart-bridge.c └── usb-descriptors.c /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | - push 5 | - pull_request 6 | 7 | jobs: 8 | CI: 9 | runs-on: ubuntu-22.04 10 | steps: 11 | - name: 'Check out code' 12 | uses: actions/checkout@v4 13 | 14 | - name: 'Install dependencies' 15 | run: | 16 | sudo apt-get install cmake gcc-arm-none-eabi libnewlib-arm-none-eabi 17 | 18 | - name: 'Update Submodules' 19 | run: | 20 | git submodule sync --recursive 21 | git submodule update --init --recursive 22 | 23 | - name: 'Configure' 24 | run: | 25 | mkdir -p build 26 | cmake -B build 27 | 28 | - name: 'Build' 29 | run: | 30 | make -C build 31 | 32 | - name: 'Upload binary' 33 | uses: actions/upload-artifact@v4 34 | with: 35 | name: pico-uart-bridge.uf2 36 | path: build/uart_bridge.uf2 37 | retention-days: 5 38 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode 2 | build 3 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "pico-sdk"] 2 | path = pico-sdk 3 | url = https://github.com/raspberrypi/pico-sdk.git 4 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: MIT 2 | 3 | cmake_minimum_required(VERSION 3.13) 4 | 5 | include(pico-sdk/pico_sdk_init.cmake) 6 | 7 | project(pico_uart_bridge) 8 | 9 | pico_sdk_init() 10 | 11 | add_executable(uart_bridge uart-bridge.c usb-descriptors.c) 12 | 13 | target_include_directories(uart_bridge PUBLIC 14 | ./ 15 | pico-sdk/lib/tinyusb/src) 16 | 17 | target_link_libraries(uart_bridge 18 | hardware_flash 19 | pico_multicore 20 | pico_stdlib 21 | tinyusb_device) 22 | 23 | pico_add_extra_outputs(uart_bridge) 24 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | ===================== 3 | 4 | Copyright © 2021 Álvaro Fernández Rojas 5 | 6 | Permission is hereby granted, free of charge, to any person 7 | obtaining a copy of this software and associated documentation 8 | files (the “Software”), to deal in the Software without 9 | restriction, including without limitation the rights to use, 10 | copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the 12 | Software is furnished to do so, subject to the following 13 | conditions: 14 | 15 | The above copyright notice and this permission notice shall be 16 | included in all copies or substantial portions of the Software. 17 | 18 | THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, 19 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | OTHER DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Raspberry Pi Pico USB-UART Bridge 2 | ================================= 3 | 4 | This program bridges the Raspberry Pi Pico HW UARTs to two independent USB CDC serial devices in order to behave like any other USB-to-UART Bridge controllers. 5 | 6 | Disclaimer 7 | ---------- 8 | 9 | This software is provided without warranty, according to the MIT License, and should therefore not be used where it may endanger life, financial stakes, or cause discomfort and inconvenience to others. 10 | 11 | Raspberry Pi Pico Pinout 12 | ------------------------ 13 | 14 | | Raspberry Pi Pico GPIO | Function | 15 | |:----------------------:|:--------:| 16 | | GPIO16 (Pin 21) | UART0 TX | 17 | | GPIO17 (Pin 22) | UART0 RX | 18 | | GPIO4 (Pin 6) | UART1 TX | 19 | | GPIO5 (Pin 7) | UART1 RX | 20 | -------------------------------------------------------------------------------- /build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | BASE_DIR="$(dirname ${BASH_SOURCE[0]})" 4 | BUILD_DIR=$BASE_DIR/build 5 | PICO_SDK_DIR=$BASE_DIR/pico-sdk 6 | 7 | main() { 8 | if [ ! -d "$PICO_SDK_DIR/.git" ]; then 9 | git submodule sync --recursive 10 | git submodule update --init --recursive 11 | fi 12 | 13 | cmake -B $BUILD_DIR -S $BASE_DIR 14 | make -C $BUILD_DIR 15 | } 16 | 17 | main $@ 18 | -------------------------------------------------------------------------------- /tusb_config.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | /* 3 | * Copyright (c) 2021 Álvaro Fernández Rojas 4 | * Copyright (c) 2020 Raspberry Pi (Trading) Ltd. 5 | * Copyright (c) 2020 Damien P. George 6 | */ 7 | 8 | #if !defined(_TUSB_CONFIG_H_) 9 | #define _TUSB_CONFIG_H_ 10 | 11 | #include 12 | 13 | #define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE 14 | 15 | #define CFG_TUD_CDC 2 16 | #define CFG_TUD_CDC_RX_BUFSIZE 1024 17 | #define CFG_TUD_CDC_TX_BUFSIZE 1024 18 | 19 | void usbd_serial_init(void); 20 | 21 | #endif /* _TUSB_CONFIG_H_ */ 22 | -------------------------------------------------------------------------------- /uart-bridge.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | /* 3 | * Copyright 2021 Álvaro Fernández Rojas 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #if !defined(MIN) 15 | #define MIN(a, b) ((a > b) ? b : a) 16 | #endif /* MIN */ 17 | 18 | #define LED_PIN 25 19 | 20 | #define BUFFER_SIZE 2560 21 | 22 | #define DEF_BIT_RATE 115200 23 | #define DEF_STOP_BITS 1 24 | #define DEF_PARITY 0 25 | #define DEF_DATA_BITS 8 26 | 27 | typedef struct { 28 | uart_inst_t *const inst; 29 | uint irq; 30 | void *irq_fn; 31 | uint8_t tx_pin; 32 | uint8_t rx_pin; 33 | } uart_id_t; 34 | 35 | typedef struct { 36 | cdc_line_coding_t usb_lc; 37 | cdc_line_coding_t uart_lc; 38 | mutex_t lc_mtx; 39 | uint8_t uart_buffer[BUFFER_SIZE]; 40 | uint32_t uart_pos; 41 | mutex_t uart_mtx; 42 | uint8_t usb_buffer[BUFFER_SIZE]; 43 | uint32_t usb_pos; 44 | mutex_t usb_mtx; 45 | } uart_data_t; 46 | 47 | void uart0_irq_fn(void); 48 | void uart1_irq_fn(void); 49 | 50 | const uart_id_t UART_ID[CFG_TUD_CDC] = { 51 | { 52 | .inst = uart0, 53 | .irq = UART0_IRQ, 54 | .irq_fn = &uart0_irq_fn, 55 | .tx_pin = 16, 56 | .rx_pin = 17, 57 | }, { 58 | .inst = uart1, 59 | .irq = UART1_IRQ, 60 | .irq_fn = &uart1_irq_fn, 61 | .tx_pin = 4, 62 | .rx_pin = 5, 63 | } 64 | }; 65 | 66 | uart_data_t UART_DATA[CFG_TUD_CDC]; 67 | 68 | static inline uint databits_usb2uart(uint8_t data_bits) 69 | { 70 | switch (data_bits) { 71 | case 5: 72 | return 5; 73 | case 6: 74 | return 6; 75 | case 7: 76 | return 7; 77 | default: 78 | return 8; 79 | } 80 | } 81 | 82 | static inline uart_parity_t parity_usb2uart(uint8_t usb_parity) 83 | { 84 | switch (usb_parity) { 85 | case 1: 86 | return UART_PARITY_ODD; 87 | case 2: 88 | return UART_PARITY_EVEN; 89 | default: 90 | return UART_PARITY_NONE; 91 | } 92 | } 93 | 94 | static inline uint stopbits_usb2uart(uint8_t stop_bits) 95 | { 96 | switch (stop_bits) { 97 | case 2: 98 | return 2; 99 | default: 100 | return 1; 101 | } 102 | } 103 | 104 | void update_uart_cfg(uint8_t itf) 105 | { 106 | const uart_id_t *ui = &UART_ID[itf]; 107 | uart_data_t *ud = &UART_DATA[itf]; 108 | 109 | mutex_enter_blocking(&ud->lc_mtx); 110 | 111 | if (ud->usb_lc.bit_rate != ud->uart_lc.bit_rate) { 112 | uart_set_baudrate(ui->inst, ud->usb_lc.bit_rate); 113 | ud->uart_lc.bit_rate = ud->usb_lc.bit_rate; 114 | } 115 | 116 | if ((ud->usb_lc.stop_bits != ud->uart_lc.stop_bits) || 117 | (ud->usb_lc.parity != ud->uart_lc.parity) || 118 | (ud->usb_lc.data_bits != ud->uart_lc.data_bits)) { 119 | uart_set_format(ui->inst, 120 | databits_usb2uart(ud->usb_lc.data_bits), 121 | stopbits_usb2uart(ud->usb_lc.stop_bits), 122 | parity_usb2uart(ud->usb_lc.parity)); 123 | ud->uart_lc.data_bits = ud->usb_lc.data_bits; 124 | ud->uart_lc.parity = ud->usb_lc.parity; 125 | ud->uart_lc.stop_bits = ud->usb_lc.stop_bits; 126 | } 127 | 128 | mutex_exit(&ud->lc_mtx); 129 | } 130 | 131 | void usb_read_bytes(uint8_t itf) 132 | { 133 | uart_data_t *ud = &UART_DATA[itf]; 134 | uint32_t len = tud_cdc_n_available(itf); 135 | 136 | if (len && 137 | mutex_try_enter(&ud->usb_mtx, NULL)) { 138 | len = MIN(len, BUFFER_SIZE - ud->usb_pos); 139 | if (len) { 140 | uint32_t count; 141 | 142 | count = tud_cdc_n_read(itf, &ud->usb_buffer[ud->usb_pos], len); 143 | ud->usb_pos += count; 144 | } 145 | 146 | mutex_exit(&ud->usb_mtx); 147 | } 148 | } 149 | 150 | void usb_write_bytes(uint8_t itf) 151 | { 152 | uart_data_t *ud = &UART_DATA[itf]; 153 | 154 | if (ud->uart_pos && 155 | mutex_try_enter(&ud->uart_mtx, NULL)) { 156 | uint32_t count; 157 | 158 | count = tud_cdc_n_write(itf, ud->uart_buffer, ud->uart_pos); 159 | if (count < ud->uart_pos) 160 | memmove(ud->uart_buffer, &ud->uart_buffer[count], 161 | ud->uart_pos - count); 162 | ud->uart_pos -= count; 163 | 164 | mutex_exit(&ud->uart_mtx); 165 | 166 | if (count) 167 | tud_cdc_n_write_flush(itf); 168 | } 169 | } 170 | 171 | void usb_cdc_process(uint8_t itf) 172 | { 173 | uart_data_t *ud = &UART_DATA[itf]; 174 | 175 | mutex_enter_blocking(&ud->lc_mtx); 176 | tud_cdc_n_get_line_coding(itf, &ud->usb_lc); 177 | mutex_exit(&ud->lc_mtx); 178 | 179 | usb_read_bytes(itf); 180 | usb_write_bytes(itf); 181 | } 182 | 183 | void core1_entry(void) 184 | { 185 | tusb_init(); 186 | 187 | while (1) { 188 | int itf; 189 | int con = 0; 190 | 191 | tud_task(); 192 | 193 | for (itf = 0; itf < CFG_TUD_CDC; itf++) { 194 | if (tud_cdc_n_connected(itf)) { 195 | con = 1; 196 | usb_cdc_process(itf); 197 | } 198 | } 199 | 200 | gpio_put(LED_PIN, con); 201 | } 202 | } 203 | 204 | static inline void uart_read_bytes(uint8_t itf) 205 | { 206 | uart_data_t *ud = &UART_DATA[itf]; 207 | const uart_id_t *ui = &UART_ID[itf]; 208 | 209 | if (uart_is_readable(ui->inst)) { 210 | mutex_enter_blocking(&ud->uart_mtx); 211 | 212 | while (uart_is_readable(ui->inst) && 213 | (ud->uart_pos < BUFFER_SIZE)) { 214 | ud->uart_buffer[ud->uart_pos] = uart_getc(ui->inst); 215 | ud->uart_pos++; 216 | } 217 | 218 | mutex_exit(&ud->uart_mtx); 219 | } 220 | } 221 | 222 | void uart0_irq_fn(void) 223 | { 224 | uart_read_bytes(0); 225 | } 226 | 227 | void uart1_irq_fn(void) 228 | { 229 | uart_read_bytes(1); 230 | } 231 | 232 | void uart_write_bytes(uint8_t itf) 233 | { 234 | uart_data_t *ud = &UART_DATA[itf]; 235 | 236 | if (ud->usb_pos && 237 | mutex_try_enter(&ud->usb_mtx, NULL)) { 238 | const uart_id_t *ui = &UART_ID[itf]; 239 | uint32_t count = 0; 240 | 241 | while (uart_is_writable(ui->inst) && 242 | count < ud->usb_pos) { 243 | uart_putc_raw(ui->inst, ud->usb_buffer[count]); 244 | count++; 245 | } 246 | 247 | if (count < ud->usb_pos) 248 | memmove(ud->usb_buffer, &ud->usb_buffer[count], 249 | ud->usb_pos - count); 250 | ud->usb_pos -= count; 251 | 252 | mutex_exit(&ud->usb_mtx); 253 | } 254 | } 255 | 256 | void init_uart_data(uint8_t itf) 257 | { 258 | const uart_id_t *ui = &UART_ID[itf]; 259 | uart_data_t *ud = &UART_DATA[itf]; 260 | 261 | /* Pinmux */ 262 | gpio_set_function(ui->tx_pin, GPIO_FUNC_UART); 263 | gpio_set_function(ui->rx_pin, GPIO_FUNC_UART); 264 | 265 | /* USB CDC LC */ 266 | ud->usb_lc.bit_rate = DEF_BIT_RATE; 267 | ud->usb_lc.data_bits = DEF_DATA_BITS; 268 | ud->usb_lc.parity = DEF_PARITY; 269 | ud->usb_lc.stop_bits = DEF_STOP_BITS; 270 | 271 | /* UART LC */ 272 | ud->uart_lc.bit_rate = DEF_BIT_RATE; 273 | ud->uart_lc.data_bits = DEF_DATA_BITS; 274 | ud->uart_lc.parity = DEF_PARITY; 275 | ud->uart_lc.stop_bits = DEF_STOP_BITS; 276 | 277 | /* Buffer */ 278 | ud->uart_pos = 0; 279 | ud->usb_pos = 0; 280 | 281 | /* Mutex */ 282 | mutex_init(&ud->lc_mtx); 283 | mutex_init(&ud->uart_mtx); 284 | mutex_init(&ud->usb_mtx); 285 | 286 | /* UART start */ 287 | uart_init(ui->inst, ud->usb_lc.bit_rate); 288 | uart_set_hw_flow(ui->inst, false, false); 289 | uart_set_format(ui->inst, databits_usb2uart(ud->usb_lc.data_bits), 290 | stopbits_usb2uart(ud->usb_lc.stop_bits), 291 | parity_usb2uart(ud->usb_lc.parity)); 292 | uart_set_fifo_enabled(ui->inst, false); 293 | 294 | /* UART RX Interrupt */ 295 | irq_set_exclusive_handler(ui->irq, ui->irq_fn); 296 | irq_set_enabled(ui->irq, true); 297 | uart_set_irq_enables(ui->inst, true, false); 298 | } 299 | 300 | int main(void) 301 | { 302 | int itf; 303 | 304 | usbd_serial_init(); 305 | 306 | for (itf = 0; itf < CFG_TUD_CDC; itf++) 307 | init_uart_data(itf); 308 | 309 | gpio_init(LED_PIN); 310 | gpio_set_dir(LED_PIN, GPIO_OUT); 311 | 312 | multicore_launch_core1(core1_entry); 313 | 314 | while (1) { 315 | for (itf = 0; itf < CFG_TUD_CDC; itf++) { 316 | update_uart_cfg(itf); 317 | uart_write_bytes(itf); 318 | } 319 | } 320 | 321 | return 0; 322 | } 323 | -------------------------------------------------------------------------------- /usb-descriptors.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | /* 3 | * Copyright (c) 2021 Álvaro Fernández Rojas 4 | * 5 | * This file is based on a file originally part of the 6 | * MicroPython project, http://micropython.org/ 7 | * 8 | * Copyright (c) 2020 Raspberry Pi (Trading) Ltd. 9 | * Copyright (c) 2019 Damien P. George 10 | */ 11 | 12 | #include 13 | #include 14 | 15 | #define DESC_STR_MAX 20 16 | 17 | #define USBD_VID 0x2E8A /* Raspberry Pi */ 18 | #define USBD_PID 0x000A /* Raspberry Pi Pico SDK CDC */ 19 | 20 | #define USBD_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN * CFG_TUD_CDC) 21 | #define USBD_MAX_POWER_MA 500 22 | 23 | #define USBD_ITF_CDC_0 0 24 | #define USBD_ITF_CDC_1 2 25 | #define USBD_ITF_MAX 4 26 | 27 | #define USBD_CDC_0_EP_CMD 0x81 28 | #define USBD_CDC_1_EP_CMD 0x83 29 | 30 | #define USBD_CDC_0_EP_OUT 0x01 31 | #define USBD_CDC_1_EP_OUT 0x03 32 | 33 | #define USBD_CDC_0_EP_IN 0x82 34 | #define USBD_CDC_1_EP_IN 0x84 35 | 36 | #define USBD_CDC_CMD_MAX_SIZE 8 37 | #define USBD_CDC_IN_OUT_MAX_SIZE 64 38 | 39 | #define USBD_STR_0 0x00 40 | #define USBD_STR_MANUF 0x01 41 | #define USBD_STR_PRODUCT 0x02 42 | #define USBD_STR_SERIAL 0x03 43 | #define USBD_STR_SERIAL_LEN 17 44 | #define USBD_STR_CDC 0x04 45 | 46 | static const tusb_desc_device_t usbd_desc_device = { 47 | .bLength = sizeof(tusb_desc_device_t), 48 | .bDescriptorType = TUSB_DESC_DEVICE, 49 | .bcdUSB = 0x0200, 50 | .bDeviceClass = TUSB_CLASS_MISC, 51 | .bDeviceSubClass = MISC_SUBCLASS_COMMON, 52 | .bDeviceProtocol = MISC_PROTOCOL_IAD, 53 | .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE, 54 | .idVendor = USBD_VID, 55 | .idProduct = USBD_PID, 56 | .bcdDevice = 0x0100, 57 | .iManufacturer = USBD_STR_MANUF, 58 | .iProduct = USBD_STR_PRODUCT, 59 | .iSerialNumber = USBD_STR_SERIAL, 60 | .bNumConfigurations = 1, 61 | }; 62 | 63 | static const uint8_t usbd_desc_cfg[USBD_DESC_LEN] = { 64 | TUD_CONFIG_DESCRIPTOR(1, USBD_ITF_MAX, USBD_STR_0, USBD_DESC_LEN, 65 | TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, USBD_MAX_POWER_MA), 66 | 67 | TUD_CDC_DESCRIPTOR(USBD_ITF_CDC_0, USBD_STR_CDC, USBD_CDC_0_EP_CMD, 68 | USBD_CDC_CMD_MAX_SIZE, USBD_CDC_0_EP_OUT, USBD_CDC_0_EP_IN, 69 | USBD_CDC_IN_OUT_MAX_SIZE), 70 | 71 | TUD_CDC_DESCRIPTOR(USBD_ITF_CDC_1, USBD_STR_CDC, USBD_CDC_1_EP_CMD, 72 | USBD_CDC_CMD_MAX_SIZE, USBD_CDC_1_EP_OUT, USBD_CDC_1_EP_IN, 73 | USBD_CDC_IN_OUT_MAX_SIZE), 74 | }; 75 | 76 | static char usbd_serial[USBD_STR_SERIAL_LEN] = "000000000000"; 77 | 78 | static const char *const usbd_desc_str[] = { 79 | [USBD_STR_MANUF] = "Raspberry Pi", 80 | [USBD_STR_PRODUCT] = "Pico", 81 | [USBD_STR_SERIAL] = usbd_serial, 82 | [USBD_STR_CDC] = "Board CDC", 83 | }; 84 | 85 | const uint8_t *tud_descriptor_device_cb(void) 86 | { 87 | return (const uint8_t *) &usbd_desc_device; 88 | } 89 | 90 | const uint8_t *tud_descriptor_configuration_cb(uint8_t index) 91 | { 92 | return usbd_desc_cfg; 93 | } 94 | 95 | const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid) 96 | { 97 | static uint16_t desc_str[DESC_STR_MAX]; 98 | uint8_t len; 99 | 100 | if (index == 0) { 101 | desc_str[1] = 0x0409; 102 | len = 1; 103 | } else { 104 | const char *str; 105 | char serial[USBD_STR_SERIAL_LEN]; 106 | 107 | if (index >= sizeof(usbd_desc_str) / sizeof(usbd_desc_str[0])) 108 | return NULL; 109 | 110 | str = usbd_desc_str[index]; 111 | for (len = 0; len < DESC_STR_MAX - 1 && str[len]; ++len) 112 | desc_str[1 + len] = str[len]; 113 | } 114 | 115 | desc_str[0] = (TUSB_DESC_STRING << 8) | (2 * len + 2); 116 | 117 | return desc_str; 118 | } 119 | 120 | void usbd_serial_init(void) 121 | { 122 | uint8_t id[8]; 123 | 124 | flash_get_unique_id(id); 125 | 126 | snprintf(usbd_serial, USBD_STR_SERIAL_LEN, "%02X%02X%02X%02X%02X%02X%02X%02X", 127 | id[0], id[1], id[2], id[3], id[4], id[5], id[6], id[7]); 128 | } 129 | --------------------------------------------------------------------------------