mirror of
https://github.com/Noltari/pico-uart-bridge.git
synced 2025-05-14 08:12:20 +00:00
Compare commits
31 commits
Author | SHA1 | Date | |
---|---|---|---|
|
9d0df3277d | ||
|
c8a4bc513f | ||
|
b74af3b0d8 | ||
|
2f05798e36 | ||
|
2e3f10f756 | ||
|
ca81e5c242 | ||
|
67ce07178f | ||
|
9d05ed4b1d | ||
|
3aa5d05fe3 | ||
|
01e7831501 | ||
|
3e1672f2c9 | ||
|
8db03b41ac | ||
|
71fd38df8a | ||
|
d0925bfd33 | ||
|
6aa7cf2958 | ||
|
05e4815f6b | ||
|
ebbc862b65 | ||
|
71faf3097a | ||
|
982b071d6c | ||
|
a7d61bb4c8 | ||
|
2460b10523 | ||
|
7a8e3bf16c | ||
|
206b612bae | ||
|
7f8226a3a1 | ||
|
beb34da36d | ||
|
3f9c9fadde | ||
|
8d9d952dde | ||
|
f2531be879 | ||
|
b6c906e55e | ||
|
a41adfcfdd | ||
|
410eef18ef |
9 changed files with 394 additions and 204 deletions
37
.github/workflows/ci.yml
vendored
Normal file
37
.github/workflows/ci.yml
vendored
Normal file
|
@ -0,0 +1,37 @@
|
|||
name: CI
|
||||
|
||||
on:
|
||||
- push
|
||||
- pull_request
|
||||
|
||||
jobs:
|
||||
CI:
|
||||
runs-on: ubuntu-22.04
|
||||
steps:
|
||||
- name: 'Check out code'
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: 'Install dependencies'
|
||||
run: |
|
||||
sudo apt-get install cmake gcc-arm-none-eabi libnewlib-arm-none-eabi
|
||||
|
||||
- name: 'Update Submodules'
|
||||
run: |
|
||||
git submodule sync --recursive
|
||||
git submodule update --init --recursive
|
||||
|
||||
- name: 'Configure'
|
||||
run: |
|
||||
mkdir -p build
|
||||
cmake -B build
|
||||
|
||||
- name: 'Build'
|
||||
run: |
|
||||
make -C build
|
||||
|
||||
- name: 'Upload binary'
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: pico-uart-bridge.uf2
|
||||
path: build/uart_bridge.uf2
|
||||
retention-days: 5
|
2
.gitmodules
vendored
2
.gitmodules
vendored
|
@ -1,3 +1,3 @@
|
|||
[submodule "pico-sdk"]
|
||||
path = pico-sdk
|
||||
url = git@github.com:raspberrypi/pico-sdk.git
|
||||
url = https://github.com/raspberrypi/pico-sdk.git
|
||||
|
|
|
@ -7,14 +7,15 @@ 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)
|
||||
./
|
||||
pico-sdk/lib/tinyusb/src)
|
||||
|
||||
target_link_libraries(uart_bridge
|
||||
hardware_flash
|
||||
pico_multicore
|
||||
pico_stdlib
|
||||
tinyusb_device)
|
||||
|
|
12
README.md
12
README.md
|
@ -1,9 +1,19 @@
|
|||
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.
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
Raspberry Pi Pico Pinout
|
||||
------------------------
|
||||
|
||||
| Raspberry Pi Pico GPIO | Function |
|
||||
|:----------------------:|:--------:|
|
||||
| GPIO16 (Pin 21) | UART0 TX |
|
||||
| GPIO17 (Pin 22) | UART0 RX |
|
||||
| GPIO4 (Pin 6) | UART1 TX |
|
||||
| GPIO5 (Pin 7) | UART1 RX |
|
||||
|
|
20
build.sh
20
build.sh
|
@ -1,15 +1,17 @@
|
|||
#!/bin/sh
|
||||
#!/bin/bash
|
||||
|
||||
BUILD_DIR=build
|
||||
BASE_DIR="$(dirname ${BASH_SOURCE[0]})"
|
||||
BUILD_DIR=$BASE_DIR/build
|
||||
PICO_SDK_DIR=$BASE_DIR/pico-sdk
|
||||
|
||||
main () {
|
||||
local cur_dir=$PWD
|
||||
main() {
|
||||
if [ ! -d "$PICO_SDK_DIR/.git" ]; then
|
||||
git submodule sync --recursive
|
||||
git submodule update --init --recursive
|
||||
fi
|
||||
|
||||
mkdir -p $BUILD_DIR
|
||||
cd $BUILD_DIR
|
||||
cmake ..
|
||||
make
|
||||
cd $cur_dir
|
||||
cmake -B $BUILD_DIR -S $BASE_DIR
|
||||
make -C $BUILD_DIR
|
||||
}
|
||||
|
||||
main $@
|
||||
|
|
2
pico-sdk
2
pico-sdk
|
@ -1 +1 @@
|
|||
Subproject commit 2d5789eca89658a7f0a01e2d6010c0f254605d72
|
||||
Subproject commit 6a7db34ff63345a7badec79ebea3aaef1712f374
|
21
tusb_config.h
Normal file
21
tusb_config.h
Normal file
|
@ -0,0 +1,21 @@
|
|||
// SPDX-License-Identifier: MIT
|
||||
/*
|
||||
* Copyright (c) 2021 Álvaro Fernández Rojas <noltari@gmail.com>
|
||||
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
|
||||
* Copyright (c) 2020 Damien P. George
|
||||
*/
|
||||
|
||||
#if !defined(_TUSB_CONFIG_H_)
|
||||
#define _TUSB_CONFIG_H_
|
||||
|
||||
#include <tusb_option.h>
|
||||
|
||||
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE
|
||||
|
||||
#define CFG_TUD_CDC 2
|
||||
#define CFG_TUD_CDC_RX_BUFSIZE 1024
|
||||
#define CFG_TUD_CDC_TX_BUFSIZE 1024
|
||||
|
||||
void usbd_serial_init(void);
|
||||
|
||||
#endif /* _TUSB_CONFIG_H_ */
|
318
uart-bridge.c
318
uart-bridge.c
|
@ -7,10 +7,9 @@
|
|||
#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>
|
||||
#include <tusb.h>
|
||||
|
||||
#if !defined(MIN)
|
||||
#define MIN(a, b) ((a > b) ? b : a)
|
||||
|
@ -18,31 +17,53 @@
|
|||
|
||||
#define LED_PIN 25
|
||||
|
||||
#define UART_ID uart0
|
||||
#define UART_TX_PIN 0
|
||||
#define UART_RX_PIN 1
|
||||
|
||||
#define BUFFER_SIZE 64
|
||||
#define BUFFER_SIZE 2560
|
||||
|
||||
#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_BIT_RATE,
|
||||
.stop_bits = DEF_STOP_BITS,
|
||||
.parity = DEF_PARITY,
|
||||
.data_bits = DEF_DATA_BITS,
|
||||
typedef struct {
|
||||
uart_inst_t *const inst;
|
||||
uint irq;
|
||||
void *irq_fn;
|
||||
uint8_t tx_pin;
|
||||
uint8_t rx_pin;
|
||||
} uart_id_t;
|
||||
|
||||
typedef struct {
|
||||
cdc_line_coding_t usb_lc;
|
||||
cdc_line_coding_t uart_lc;
|
||||
mutex_t lc_mtx;
|
||||
uint8_t uart_buffer[BUFFER_SIZE];
|
||||
uint32_t uart_pos;
|
||||
mutex_t uart_mtx;
|
||||
uint8_t usb_buffer[BUFFER_SIZE];
|
||||
uint32_t usb_pos;
|
||||
mutex_t usb_mtx;
|
||||
} uart_data_t;
|
||||
|
||||
void uart0_irq_fn(void);
|
||||
void uart1_irq_fn(void);
|
||||
|
||||
const uart_id_t UART_ID[CFG_TUD_CDC] = {
|
||||
{
|
||||
.inst = uart0,
|
||||
.irq = UART0_IRQ,
|
||||
.irq_fn = &uart0_irq_fn,
|
||||
.tx_pin = 16,
|
||||
.rx_pin = 17,
|
||||
}, {
|
||||
.inst = uart1,
|
||||
.irq = UART1_IRQ,
|
||||
.irq_fn = &uart1_irq_fn,
|
||||
.tx_pin = 4,
|
||||
.rx_pin = 5,
|
||||
}
|
||||
};
|
||||
|
||||
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;
|
||||
uart_data_t UART_DATA[CFG_TUD_CDC];
|
||||
|
||||
static inline uint databits_usb2uart(uint8_t data_bits)
|
||||
{
|
||||
|
@ -80,133 +101,220 @@ static inline uint stopbits_usb2uart(uint8_t stop_bits)
|
|||
}
|
||||
}
|
||||
|
||||
int update_uart_cfg(void)
|
||||
void update_uart_cfg(uint8_t itf)
|
||||
{
|
||||
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;
|
||||
const uart_id_t *ui = &UART_ID[itf];
|
||||
uart_data_t *ud = &UART_DATA[itf];
|
||||
|
||||
if (last_cdc_lc.bit_rate != CDC_LC.bit_rate) {
|
||||
uart_set_baudrate(UART_ID, CDC_LC.bit_rate);
|
||||
updated = 1;
|
||||
mutex_enter_blocking(&ud->lc_mtx);
|
||||
|
||||
if (ud->usb_lc.bit_rate != ud->uart_lc.bit_rate) {
|
||||
uart_set_baudrate(ui->inst, ud->usb_lc.bit_rate);
|
||||
ud->uart_lc.bit_rate = ud->usb_lc.bit_rate;
|
||||
}
|
||||
|
||||
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 ((ud->usb_lc.stop_bits != ud->uart_lc.stop_bits) ||
|
||||
(ud->usb_lc.parity != ud->uart_lc.parity) ||
|
||||
(ud->usb_lc.data_bits != ud->uart_lc.data_bits)) {
|
||||
uart_set_format(ui->inst,
|
||||
databits_usb2uart(ud->usb_lc.data_bits),
|
||||
stopbits_usb2uart(ud->usb_lc.stop_bits),
|
||||
parity_usb2uart(ud->usb_lc.parity));
|
||||
ud->uart_lc.data_bits = ud->usb_lc.data_bits;
|
||||
ud->uart_lc.parity = ud->usb_lc.parity;
|
||||
ud->uart_lc.stop_bits = ud->usb_lc.stop_bits;
|
||||
}
|
||||
|
||||
if (updated)
|
||||
memcpy(&last_cdc_lc, &CDC_LC, sizeof(cdc_line_coding_t));
|
||||
|
||||
return updated;
|
||||
mutex_exit(&ud->lc_mtx);
|
||||
}
|
||||
|
||||
void usb_cdc_process(void)
|
||||
void usb_read_bytes(uint8_t itf)
|
||||
{
|
||||
uint32_t count;
|
||||
uint32_t len;
|
||||
uart_data_t *ud = &UART_DATA[itf];
|
||||
uint32_t len = tud_cdc_n_available(itf);
|
||||
|
||||
tud_cdc_get_line_coding(&CDC_LC);
|
||||
|
||||
/* Read bytes from USB */
|
||||
if (tud_cdc_available()) {
|
||||
mutex_enter_blocking(&USB_MTX);
|
||||
|
||||
len = MIN(tud_cdc_available(), BUFFER_SIZE - USB_POS);
|
||||
if (len &&
|
||||
mutex_try_enter(&ud->usb_mtx, NULL)) {
|
||||
len = MIN(len, BUFFER_SIZE - ud->usb_pos);
|
||||
if (len) {
|
||||
count = tud_cdc_read(USB_BUFFER, len);
|
||||
USB_POS += count;
|
||||
uint32_t count;
|
||||
|
||||
count = tud_cdc_n_read(itf, &ud->usb_buffer[ud->usb_pos], len);
|
||||
ud->usb_pos += count;
|
||||
}
|
||||
|
||||
mutex_exit(&USB_MTX);
|
||||
mutex_exit(&ud->usb_mtx);
|
||||
}
|
||||
}
|
||||
|
||||
/* Write bytes to USB */
|
||||
if (UART_POS) {
|
||||
mutex_enter_blocking(&UART_MTX);
|
||||
void usb_write_bytes(uint8_t itf)
|
||||
{
|
||||
uart_data_t *ud = &UART_DATA[itf];
|
||||
|
||||
count = tud_cdc_write(UART_BUFFER, UART_POS);
|
||||
if (count) {
|
||||
UART_POS -= count;
|
||||
tud_cdc_write_flush();
|
||||
}
|
||||
if (ud->uart_pos &&
|
||||
mutex_try_enter(&ud->uart_mtx, NULL)) {
|
||||
uint32_t count;
|
||||
|
||||
mutex_exit(&UART_MTX);
|
||||
count = tud_cdc_n_write(itf, ud->uart_buffer, ud->uart_pos);
|
||||
if (count < ud->uart_pos)
|
||||
memmove(ud->uart_buffer, &ud->uart_buffer[count],
|
||||
ud->uart_pos - count);
|
||||
ud->uart_pos -= count;
|
||||
|
||||
mutex_exit(&ud->uart_mtx);
|
||||
|
||||
if (count)
|
||||
tud_cdc_n_write_flush(itf);
|
||||
}
|
||||
}
|
||||
|
||||
void usb_cdc_process(uint8_t itf)
|
||||
{
|
||||
uart_data_t *ud = &UART_DATA[itf];
|
||||
|
||||
mutex_enter_blocking(&ud->lc_mtx);
|
||||
tud_cdc_n_get_line_coding(itf, &ud->usb_lc);
|
||||
mutex_exit(&ud->lc_mtx);
|
||||
|
||||
usb_read_bytes(itf);
|
||||
usb_write_bytes(itf);
|
||||
}
|
||||
|
||||
void core1_entry(void)
|
||||
{
|
||||
tusb_init();
|
||||
|
||||
while (1) {
|
||||
int itf;
|
||||
int con = 0;
|
||||
|
||||
tud_task();
|
||||
|
||||
if (tud_cdc_connected()) {
|
||||
usb_cdc_process();
|
||||
gpio_put(LED_PIN, 1);
|
||||
} else {
|
||||
gpio_put(LED_PIN, 0);
|
||||
for (itf = 0; itf < CFG_TUD_CDC; itf++) {
|
||||
if (tud_cdc_n_connected(itf)) {
|
||||
con = 1;
|
||||
usb_cdc_process(itf);
|
||||
}
|
||||
}
|
||||
|
||||
gpio_put(LED_PIN, con);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void uart_read_bytes(uint8_t itf)
|
||||
{
|
||||
uart_data_t *ud = &UART_DATA[itf];
|
||||
const uart_id_t *ui = &UART_ID[itf];
|
||||
|
||||
if (uart_is_readable(ui->inst)) {
|
||||
mutex_enter_blocking(&ud->uart_mtx);
|
||||
|
||||
while (uart_is_readable(ui->inst) &&
|
||||
(ud->uart_pos < BUFFER_SIZE)) {
|
||||
ud->uart_buffer[ud->uart_pos] = uart_getc(ui->inst);
|
||||
ud->uart_pos++;
|
||||
}
|
||||
|
||||
mutex_exit(&ud->uart_mtx);
|
||||
}
|
||||
}
|
||||
|
||||
void uart0_irq_fn(void)
|
||||
{
|
||||
uart_read_bytes(0);
|
||||
}
|
||||
|
||||
void uart1_irq_fn(void)
|
||||
{
|
||||
uart_read_bytes(1);
|
||||
}
|
||||
|
||||
void uart_write_bytes(uint8_t itf)
|
||||
{
|
||||
uart_data_t *ud = &UART_DATA[itf];
|
||||
|
||||
if (ud->usb_pos &&
|
||||
mutex_try_enter(&ud->usb_mtx, NULL)) {
|
||||
const uart_id_t *ui = &UART_ID[itf];
|
||||
uint32_t count = 0;
|
||||
|
||||
while (uart_is_writable(ui->inst) &&
|
||||
count < ud->usb_pos) {
|
||||
uart_putc_raw(ui->inst, ud->usb_buffer[count]);
|
||||
count++;
|
||||
}
|
||||
|
||||
if (count < ud->usb_pos)
|
||||
memmove(ud->usb_buffer, &ud->usb_buffer[count],
|
||||
ud->usb_pos - count);
|
||||
ud->usb_pos -= count;
|
||||
|
||||
mutex_exit(&ud->usb_mtx);
|
||||
}
|
||||
}
|
||||
|
||||
void init_uart_data(uint8_t itf)
|
||||
{
|
||||
const uart_id_t *ui = &UART_ID[itf];
|
||||
uart_data_t *ud = &UART_DATA[itf];
|
||||
|
||||
/* Pinmux */
|
||||
gpio_set_function(ui->tx_pin, GPIO_FUNC_UART);
|
||||
gpio_set_function(ui->rx_pin, GPIO_FUNC_UART);
|
||||
|
||||
/* USB CDC LC */
|
||||
ud->usb_lc.bit_rate = DEF_BIT_RATE;
|
||||
ud->usb_lc.data_bits = DEF_DATA_BITS;
|
||||
ud->usb_lc.parity = DEF_PARITY;
|
||||
ud->usb_lc.stop_bits = DEF_STOP_BITS;
|
||||
|
||||
/* UART LC */
|
||||
ud->uart_lc.bit_rate = DEF_BIT_RATE;
|
||||
ud->uart_lc.data_bits = DEF_DATA_BITS;
|
||||
ud->uart_lc.parity = DEF_PARITY;
|
||||
ud->uart_lc.stop_bits = DEF_STOP_BITS;
|
||||
|
||||
/* Buffer */
|
||||
ud->uart_pos = 0;
|
||||
ud->usb_pos = 0;
|
||||
|
||||
/* Mutex */
|
||||
mutex_init(&ud->lc_mtx);
|
||||
mutex_init(&ud->uart_mtx);
|
||||
mutex_init(&ud->usb_mtx);
|
||||
|
||||
/* UART start */
|
||||
uart_init(ui->inst, ud->usb_lc.bit_rate);
|
||||
uart_set_hw_flow(ui->inst, false, false);
|
||||
uart_set_format(ui->inst, databits_usb2uart(ud->usb_lc.data_bits),
|
||||
stopbits_usb2uart(ud->usb_lc.stop_bits),
|
||||
parity_usb2uart(ud->usb_lc.parity));
|
||||
uart_set_fifo_enabled(ui->inst, false);
|
||||
|
||||
/* UART RX Interrupt */
|
||||
irq_set_exclusive_handler(ui->irq, ui->irq_fn);
|
||||
irq_set_enabled(ui->irq, true);
|
||||
uart_set_irq_enables(ui->inst, true, false);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
uint8_t ch;
|
||||
int rc;
|
||||
int itf;
|
||||
|
||||
mutex_init(&UART_MTX);
|
||||
mutex_init(&USB_MTX);
|
||||
usbd_serial_init();
|
||||
|
||||
for (itf = 0; itf < CFG_TUD_CDC; itf++)
|
||||
init_uart_data(itf);
|
||||
|
||||
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);
|
||||
for (itf = 0; itf < CFG_TUD_CDC; itf++) {
|
||||
update_uart_cfg(itf);
|
||||
uart_write_bytes(itf);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,117 +1,128 @@
|
|||
// SPDX-License-Identifier: MIT
|
||||
/*
|
||||
* Copyright (c) 2021 Álvaro Fernández Rojas <noltari@gmail.com>
|
||||
*
|
||||
* 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"
|
||||
#include <hardware/flash.h>
|
||||
#include <tusb.h>
|
||||
|
||||
#define USBD_VID (0x2E8A) // Raspberry Pi
|
||||
#define USBD_PID (0x000a) // Raspberry Pi Pico SDK CDC
|
||||
#define DESC_STR_MAX 20
|
||||
|
||||
#define USBD_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN)
|
||||
#define USBD_MAX_POWER_MA (250)
|
||||
#define USBD_VID 0x2E8A /* Raspberry Pi */
|
||||
#define USBD_PID 0x000A /* Raspberry Pi Pico SDK CDC */
|
||||
|
||||
#define USBD_ITF_CDC (0) // needs 2 interfaces
|
||||
#define USBD_ITF_MAX (2)
|
||||
#define USBD_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN * CFG_TUD_CDC)
|
||||
#define USBD_MAX_POWER_MA 500
|
||||
|
||||
#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_ITF_CDC_0 0
|
||||
#define USBD_ITF_CDC_1 2
|
||||
#define USBD_ITF_MAX 4
|
||||
|
||||
#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)
|
||||
#define USBD_CDC_0_EP_CMD 0x81
|
||||
#define USBD_CDC_1_EP_CMD 0x83
|
||||
|
||||
// Note: descriptors returned from callbacks must exist long enough for transfer to complete
|
||||
#define USBD_CDC_0_EP_OUT 0x01
|
||||
#define USBD_CDC_1_EP_OUT 0x03
|
||||
|
||||
#define USBD_CDC_0_EP_IN 0x82
|
||||
#define USBD_CDC_1_EP_IN 0x84
|
||||
|
||||
#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_SERIAL_LEN 17
|
||||
#define USBD_STR_CDC 0x04
|
||||
|
||||
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,
|
||||
.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_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),
|
||||
TUD_CDC_DESCRIPTOR(USBD_ITF_CDC_0, USBD_STR_CDC, USBD_CDC_0_EP_CMD,
|
||||
USBD_CDC_CMD_MAX_SIZE, USBD_CDC_0_EP_OUT, USBD_CDC_0_EP_IN,
|
||||
USBD_CDC_IN_OUT_MAX_SIZE),
|
||||
|
||||
TUD_CDC_DESCRIPTOR(USBD_ITF_CDC_1, USBD_STR_CDC, USBD_CDC_1_EP_CMD,
|
||||
USBD_CDC_CMD_MAX_SIZE, USBD_CDC_1_EP_OUT, USBD_CDC_1_EP_IN,
|
||||
USBD_CDC_IN_OUT_MAX_SIZE),
|
||||
};
|
||||
|
||||
static char usbd_serial[USBD_STR_SERIAL_LEN] = "000000000000";
|
||||
|
||||
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",
|
||||
[USBD_STR_MANUF] = "Raspberry Pi",
|
||||
[USBD_STR_PRODUCT] = "Pico",
|
||||
[USBD_STR_SERIAL] = usbd_serial,
|
||||
[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_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 uint8_t *tud_descriptor_configuration_cb(uint8_t 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];
|
||||
const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid)
|
||||
{
|
||||
static uint16_t desc_str[DESC_STR_MAX];
|
||||
uint8_t len;
|
||||
|
||||
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];
|
||||
}
|
||||
}
|
||||
if (index == 0) {
|
||||
desc_str[1] = 0x0409;
|
||||
len = 1;
|
||||
} else {
|
||||
const char *str;
|
||||
char serial[USBD_STR_SERIAL_LEN];
|
||||
|
||||
// first byte is length (including header), second byte is string type
|
||||
desc_str[0] = (TUSB_DESC_STRING << 8) | (2 * len + 2);
|
||||
if (index >= sizeof(usbd_desc_str) / sizeof(usbd_desc_str[0]))
|
||||
return NULL;
|
||||
|
||||
return desc_str;
|
||||
str = usbd_desc_str[index];
|
||||
for (len = 0; len < DESC_STR_MAX - 1 && str[len]; ++len)
|
||||
desc_str[1 + len] = str[len];
|
||||
}
|
||||
|
||||
desc_str[0] = (TUSB_DESC_STRING << 8) | (2 * len + 2);
|
||||
|
||||
return desc_str;
|
||||
}
|
||||
|
||||
void usbd_serial_init(void)
|
||||
{
|
||||
uint8_t id[8];
|
||||
|
||||
flash_get_unique_id(id);
|
||||
|
||||
snprintf(usbd_serial, USBD_STR_SERIAL_LEN, "%02X%02X%02X%02X%02X%02X%02X%02X",
|
||||
id[0], id[1], id[2], id[3], id[4], id[5], id[6], id[7]);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue