Import project files

Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
This commit is contained in:
Álvaro Fernández Rojas 2021-02-03 12:49:34 +01:00
commit 994eab59f5
9 changed files with 404 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.vscode
build

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "pico-sdk"]
path = pico-sdk
url = git@github.com:raspberrypi/pico-sdk.git

22
CMakeLists.txt Normal file
View File

@ -0,0 +1,22 @@
# SPDX-License-Identifier: MIT
cmake_minimum_required(VERSION 3.13)
include(pico-sdk/pico_sdk_init.cmake)
project(pico_uart_bridge)
pico_sdk_init()
add_executable(uart_bridge uart-bridge.c usb-descriptors.c)
target_include_directories(uart_bridge PUBLIC
pico-sdk/lib/tinyusb/src
pico-sdk/src/rp2_common/pico_stdio_usb/include)
target_link_libraries(uart_bridge
pico_multicore
pico_stdlib
tinyusb_device)
pico_add_extra_outputs(uart_bridge)

25
LICENSE.md Normal file
View File

@ -0,0 +1,25 @@
The MIT License (MIT)
=====================
Copyright © 2021 Álvaro Fernández Rojas <noltari@gmail.com>
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the “Software”), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

9
README.md Normal file
View File

@ -0,0 +1,9 @@
Raspberry Pi Pico USB-UART Bridge
=================================
This program bridges the Raspberry Pi Pico HW UART0 to USB CDC serial device in order to behave like any other USB-to-UART Bridge controller.
Disclaimer
----------
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.

15
build.sh Executable file
View File

@ -0,0 +1,15 @@
#!/bin/sh
BUILD_DIR=build
main () {
local cur_dir=$PWD
mkdir -p $BUILD_DIR
cd $BUILD_DIR
cmake ..
make
cd $cur_dir
}
main $@

1
pico-sdk Submodule

@ -0,0 +1 @@
Subproject commit 2d5789eca89658a7f0a01e2d6010c0f254605d72

210
uart-bridge.c Normal file
View File

@ -0,0 +1,210 @@
// SPDX-License-Identifier: MIT
/*
* Copyright 2021 Álvaro Fernández Rojas <noltari@gmail.com>
*/
#include <hardware/irq.h>
#include <hardware/structs/sio.h>
#include <hardware/uart.h>
#include <pico/multicore.h>
#include <pico/stdio_usb.h>
#include <pico/stdlib.h>
#include <tusb.h>
#include <string.h>
#if !defined(MIN)
#define MIN(a, b) ((a > b) ? b : a)
#endif /* MIN */
#define LED_PIN 25
#define UART_ID uart0
#define UART_TX_PIN 0
#define UART_RX_PIN 1
#define BUFFER_SIZE 64
#define DEF_BIT_RATE 115200
#define DEF_STOP_BITS 1
#define DEF_PARITY 0
#define DEF_DATA_BITS 8
static cdc_line_coding_t CDC_LC = {
.bit_rate = DEF_STOP_BITS,
.stop_bits = DEF_STOP_BITS,
.parity = DEF_PARITY,
.data_bits = DEF_DATA_BITS,
};
static uint8_t UART_BUFFER[BUFFER_SIZE];
static uint32_t UART_POS = 0;
static mutex_t UART_MTX;
static uint8_t USB_BUFFER[BUFFER_SIZE];
static uint32_t USB_POS = 0;
static mutex_t USB_MTX;
static inline uart_parity_t databits_usb2uart(uint8_t data_bits)
{
switch (data_bits) {
case 5:
return 5;
case 6:
return 6;
case 7:
return 7;
default:
return 8;
}
}
static inline uart_parity_t parity_usb2uart(uint8_t usb_parity)
{
switch (usb_parity) {
case 1:
return UART_PARITY_ODD;
case 2:
return UART_PARITY_EVEN;
default:
return UART_PARITY_NONE;
}
}
static inline uart_parity_t stopbits_usb2uart(uint8_t stop_bits)
{
switch (stop_bits) {
case 2:
return 2;
default:
return 1;
}
}
int update_uart_cfg(void)
{
static cdc_line_coding_t last_cdc_lc = {
.bit_rate = DEF_STOP_BITS,
.stop_bits = DEF_STOP_BITS,
.parity = DEF_PARITY,
.data_bits = DEF_DATA_BITS,
};
int updated = 0;
if (last_cdc_lc.bit_rate != CDC_LC.bit_rate) {
uart_set_baudrate(UART_ID, CDC_LC.bit_rate);
updated = 1;
}
if ((last_cdc_lc.stop_bits != CDC_LC.stop_bits) ||
(last_cdc_lc.parity != CDC_LC.parity) ||
(last_cdc_lc.data_bits != CDC_LC.data_bits)) {
uart_set_format(UART_ID, databits_usb2uart(CDC_LC.data_bits),
stopbits_usb2uart(CDC_LC.stop_bits),
parity_usb2uart(CDC_LC.parity));
updated = 1;
}
if (updated)
memcpy(&last_cdc_lc, &CDC_LC, sizeof(cdc_line_coding_t));
return updated;
}
void core1_entry(void)
{
tusb_init();
while (1) {
tud_task();
if (tud_cdc_connected()) {
uint32_t count;
tud_cdc_get_line_coding(&CDC_LC);
/* Read bytes from USB */
if (tud_cdc_available()) {
uint32_t len;
mutex_enter_blocking(&USB_MTX);
len = MIN(tud_cdc_available(), BUFFER_SIZE - USB_POS);
if (len) {
count = tud_cdc_read(USB_BUFFER, len);
USB_POS += count;
}
mutex_exit(&USB_MTX);
}
/* Write bytes to USB */
if (UART_POS) {
mutex_enter_blocking(&UART_MTX);
count = tud_cdc_write(UART_BUFFER, UART_POS);
if (count) {
UART_POS -= count;
tud_cdc_write_flush();
}
mutex_exit(&UART_MTX);
}
gpio_put(LED_PIN, 1);
} else {
gpio_put(LED_PIN, 0);
}
};
}
int main(void)
{
uint8_t ch;
int rc;
mutex_init(&UART_MTX);
mutex_init(&USB_MTX);
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);
uart_init(UART_ID, CDC_LC.bit_rate);
gpio_set_function(UART_TX_PIN, GPIO_FUNC_UART);
gpio_set_function(UART_RX_PIN, GPIO_FUNC_UART);
uart_set_hw_flow(UART_ID, false, false);
uart_set_format(UART_ID, databits_usb2uart(CDC_LC.data_bits),
stopbits_usb2uart(CDC_LC.stop_bits),
parity_usb2uart(CDC_LC.parity));
multicore_launch_core1(core1_entry);
while (1) {
update_uart_cfg();
/* Read bytes from UART */
if (uart_is_readable(UART_ID)) {
mutex_enter_blocking(&UART_MTX);
while (uart_is_readable(UART_ID) && UART_POS < BUFFER_SIZE) {
UART_BUFFER[UART_POS] = uart_getc(UART_ID);
UART_POS++;
}
mutex_exit(&UART_MTX);
}
/* Write bytes to UART */
if (USB_POS) {
mutex_enter_blocking(&USB_MTX);
uart_write_blocking(UART_ID, USB_BUFFER, USB_POS);
USB_POS = 0;
mutex_exit(&USB_MTX);
}
}
return 0;
}

117
usb-descriptors.c Normal file
View File

@ -0,0 +1,117 @@
/*
* This file is based on a file originally part of the
* MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
* Copyright (c) 2019 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "tusb.h"
#define USBD_VID (0x2E8A) // Raspberry Pi
#define USBD_PID (0x000a) // Raspberry Pi Pico SDK CDC
#define USBD_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN)
#define USBD_MAX_POWER_MA (250)
#define USBD_ITF_CDC (0) // needs 2 interfaces
#define USBD_ITF_MAX (2)
#define USBD_CDC_EP_CMD (0x81)
#define USBD_CDC_EP_OUT (0x02)
#define USBD_CDC_EP_IN (0x82)
#define USBD_CDC_CMD_MAX_SIZE (8)
#define USBD_CDC_IN_OUT_MAX_SIZE (64)
#define USBD_STR_0 (0x00)
#define USBD_STR_MANUF (0x01)
#define USBD_STR_PRODUCT (0x02)
#define USBD_STR_SERIAL (0x03)
#define USBD_STR_CDC (0x04)
// Note: descriptors returned from callbacks must exist long enough for transfer to complete
static const tusb_desc_device_t usbd_desc_device = {
.bLength = sizeof(tusb_desc_device_t),
.bDescriptorType = TUSB_DESC_DEVICE,
.bcdUSB = 0x0200,
.bDeviceClass = TUSB_CLASS_MISC,
.bDeviceSubClass = MISC_SUBCLASS_COMMON,
.bDeviceProtocol = MISC_PROTOCOL_IAD,
.bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
.idVendor = USBD_VID,
.idProduct = USBD_PID,
.bcdDevice = 0x0100,
.iManufacturer = USBD_STR_MANUF,
.iProduct = USBD_STR_PRODUCT,
.iSerialNumber = USBD_STR_SERIAL,
.bNumConfigurations = 1,
};
static const uint8_t usbd_desc_cfg[USBD_DESC_LEN] = {
TUD_CONFIG_DESCRIPTOR(1, USBD_ITF_MAX, USBD_STR_0, USBD_DESC_LEN,
TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, USBD_MAX_POWER_MA),
TUD_CDC_DESCRIPTOR(USBD_ITF_CDC, USBD_STR_CDC, USBD_CDC_EP_CMD,
USBD_CDC_CMD_MAX_SIZE, USBD_CDC_EP_OUT, USBD_CDC_EP_IN, USBD_CDC_IN_OUT_MAX_SIZE),
};
static const char *const usbd_desc_str[] = {
[USBD_STR_MANUF] = "Raspberry Pi",
[USBD_STR_PRODUCT] = "Pico",
[USBD_STR_SERIAL] = "000000000000", // TODO
[USBD_STR_CDC] = "Board CDC",
};
const uint8_t *tud_descriptor_device_cb(void) {
return (const uint8_t *)&usbd_desc_device;
}
const uint8_t *tud_descriptor_configuration_cb(uint8_t index) {
(void)index;
return usbd_desc_cfg;
}
const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
#define DESC_STR_MAX (20)
static uint16_t desc_str[DESC_STR_MAX];
uint8_t len;
if (index == 0) {
desc_str[1] = 0x0409; // supported language is English
len = 1;
} else {
if (index >= sizeof(usbd_desc_str) / sizeof(usbd_desc_str[0])) {
return NULL;
}
const char *str = usbd_desc_str[index];
for (len = 0; len < DESC_STR_MAX - 1 && str[len]; ++len) {
desc_str[1 + len] = str[len];
}
}
// first byte is length (including header), second byte is string type
desc_str[0] = (TUSB_DESC_STRING << 8) | (2 * len + 2);
return desc_str;
}