mirror of
https://github.com/Noltari/pico-uart-bridge.git
synced 2025-05-14 08:12:20 +00:00
Compare commits
No commits in common. "master" and "v2.1" have entirely different histories.
8 changed files with 66 additions and 124 deletions
36
.github/workflows/ci.yml
vendored
36
.github/workflows/ci.yml
vendored
|
@ -4,33 +4,27 @@ on:
|
||||||
- push
|
- push
|
||||||
- pull_request
|
- pull_request
|
||||||
|
|
||||||
jobs:
|
env:
|
||||||
CI:
|
DEFAULT_PYTHON: 3.9
|
||||||
runs-on: ubuntu-22.04
|
|
||||||
steps:
|
|
||||||
- name: 'Check out code'
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: 'Install dependencies'
|
jobs:
|
||||||
|
pico:
|
||||||
|
name: RPi Pico compilation
|
||||||
|
runs-on: ubuntu-20.04
|
||||||
|
steps:
|
||||||
|
- name: Check out code from GitHub
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
|
||||||
|
- name: Install dependencies
|
||||||
run: |
|
run: |
|
||||||
sudo apt-get install cmake gcc-arm-none-eabi libnewlib-arm-none-eabi
|
sudo apt-get install cmake gcc-arm-none-eabi libnewlib-arm-none-eabi
|
||||||
|
|
||||||
- name: 'Update Submodules'
|
- name: Generate RPi Pico binaries
|
||||||
run: |
|
run: |
|
||||||
git submodule sync --recursive
|
./build.sh
|
||||||
git submodule update --init --recursive
|
|
||||||
|
|
||||||
- name: 'Configure'
|
- name: 'Upload RPi Pico binary'
|
||||||
run: |
|
uses: actions/upload-artifact@v2
|
||||||
mkdir -p build
|
|
||||||
cmake -B build
|
|
||||||
|
|
||||||
- name: 'Build'
|
|
||||||
run: |
|
|
||||||
make -C build
|
|
||||||
|
|
||||||
- name: 'Upload binary'
|
|
||||||
uses: actions/upload-artifact@v4
|
|
||||||
with:
|
with:
|
||||||
name: pico-uart-bridge.uf2
|
name: pico-uart-bridge.uf2
|
||||||
path: build/uart_bridge.uf2
|
path: build/uart_bridge.uf2
|
||||||
|
|
|
@ -15,7 +15,6 @@ target_include_directories(uart_bridge PUBLIC
|
||||||
pico-sdk/lib/tinyusb/src)
|
pico-sdk/lib/tinyusb/src)
|
||||||
|
|
||||||
target_link_libraries(uart_bridge
|
target_link_libraries(uart_bridge
|
||||||
hardware_flash
|
|
||||||
pico_multicore
|
pico_multicore
|
||||||
pico_stdlib
|
pico_stdlib
|
||||||
tinyusb_device)
|
tinyusb_device)
|
||||||
|
|
|
@ -13,7 +13,7 @@ Raspberry Pi Pico Pinout
|
||||||
|
|
||||||
| Raspberry Pi Pico GPIO | Function |
|
| Raspberry Pi Pico GPIO | Function |
|
||||||
|:----------------------:|:--------:|
|
|:----------------------:|:--------:|
|
||||||
| GPIO16 (Pin 21) | UART0 TX |
|
| GPIO0 (Pin 1) | UART0 TX |
|
||||||
| GPIO17 (Pin 22) | UART0 RX |
|
| GPIO1 (Pin 2) | UART0 RX |
|
||||||
| GPIO4 (Pin 6) | UART1 TX |
|
| GPIO4 (Pin 6) | UART1 TX |
|
||||||
| GPIO5 (Pin 7) | UART1 RX |
|
| GPIO5 (Pin 7) | UART1 RX |
|
||||||
|
|
20
build.sh
20
build.sh
|
@ -1,17 +1,21 @@
|
||||||
#!/bin/bash
|
#!/bin/sh
|
||||||
|
|
||||||
BASE_DIR="$(dirname ${BASH_SOURCE[0]})"
|
BUILD_DIR=build
|
||||||
BUILD_DIR=$BASE_DIR/build
|
PICO_SDK_DIR=pico-sdk
|
||||||
PICO_SDK_DIR=$BASE_DIR/pico-sdk
|
|
||||||
|
main () {
|
||||||
|
local cur_dir=$PWD
|
||||||
|
|
||||||
main() {
|
|
||||||
if [ ! -d "$PICO_SDK_DIR/.git" ]; then
|
if [ ! -d "$PICO_SDK_DIR/.git" ]; then
|
||||||
git submodule sync --recursive
|
|
||||||
git submodule update --init --recursive
|
git submodule update --init --recursive
|
||||||
fi
|
fi
|
||||||
|
|
||||||
cmake -B $BUILD_DIR -S $BASE_DIR
|
mkdir -p $BUILD_DIR
|
||||||
make -C $BUILD_DIR
|
cd $BUILD_DIR
|
||||||
|
cmake ../
|
||||||
|
make
|
||||||
|
|
||||||
|
cd $cur_dir
|
||||||
}
|
}
|
||||||
|
|
||||||
main $@
|
main $@
|
||||||
|
|
2
pico-sdk
2
pico-sdk
|
@ -1 +1 @@
|
||||||
Subproject commit 6a7db34ff63345a7badec79ebea3aaef1712f374
|
Subproject commit 2d5789eca89658a7f0a01e2d6010c0f254605d72
|
|
@ -13,9 +13,7 @@
|
||||||
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE
|
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE
|
||||||
|
|
||||||
#define CFG_TUD_CDC 2
|
#define CFG_TUD_CDC 2
|
||||||
#define CFG_TUD_CDC_RX_BUFSIZE 1024
|
#define CFG_TUD_CDC_RX_BUFSIZE 256
|
||||||
#define CFG_TUD_CDC_TX_BUFSIZE 1024
|
#define CFG_TUD_CDC_TX_BUFSIZE 256
|
||||||
|
|
||||||
void usbd_serial_init(void);
|
|
||||||
|
|
||||||
#endif /* _TUSB_CONFIG_H_ */
|
#endif /* _TUSB_CONFIG_H_ */
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
|
|
||||||
#define LED_PIN 25
|
#define LED_PIN 25
|
||||||
|
|
||||||
#define BUFFER_SIZE 2560
|
#define BUFFER_SIZE 64
|
||||||
|
|
||||||
#define DEF_BIT_RATE 115200
|
#define DEF_BIT_RATE 115200
|
||||||
#define DEF_STOP_BITS 1
|
#define DEF_STOP_BITS 1
|
||||||
|
@ -26,8 +26,6 @@
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uart_inst_t *const inst;
|
uart_inst_t *const inst;
|
||||||
uint irq;
|
|
||||||
void *irq_fn;
|
|
||||||
uint8_t tx_pin;
|
uint8_t tx_pin;
|
||||||
uint8_t rx_pin;
|
uint8_t rx_pin;
|
||||||
} uart_id_t;
|
} uart_id_t;
|
||||||
|
@ -44,20 +42,13 @@ typedef struct {
|
||||||
mutex_t usb_mtx;
|
mutex_t usb_mtx;
|
||||||
} uart_data_t;
|
} uart_data_t;
|
||||||
|
|
||||||
void uart0_irq_fn(void);
|
|
||||||
void uart1_irq_fn(void);
|
|
||||||
|
|
||||||
const uart_id_t UART_ID[CFG_TUD_CDC] = {
|
const uart_id_t UART_ID[CFG_TUD_CDC] = {
|
||||||
{
|
{
|
||||||
.inst = uart0,
|
.inst = uart0,
|
||||||
.irq = UART0_IRQ,
|
.tx_pin = 0,
|
||||||
.irq_fn = &uart0_irq_fn,
|
.rx_pin = 1,
|
||||||
.tx_pin = 16,
|
|
||||||
.rx_pin = 17,
|
|
||||||
}, {
|
}, {
|
||||||
.inst = uart1,
|
.inst = uart1,
|
||||||
.irq = UART1_IRQ,
|
|
||||||
.irq_fn = &uart1_irq_fn,
|
|
||||||
.tx_pin = 4,
|
.tx_pin = 4,
|
||||||
.rx_pin = 5,
|
.rx_pin = 5,
|
||||||
}
|
}
|
||||||
|
@ -128,18 +119,19 @@ void update_uart_cfg(uint8_t itf)
|
||||||
mutex_exit(&ud->lc_mtx);
|
mutex_exit(&ud->lc_mtx);
|
||||||
}
|
}
|
||||||
|
|
||||||
void usb_read_bytes(uint8_t itf)
|
void usb_read_bytes(uint8_t itf) {
|
||||||
{
|
|
||||||
uart_data_t *ud = &UART_DATA[itf];
|
|
||||||
uint32_t len = tud_cdc_n_available(itf);
|
uint32_t len = tud_cdc_n_available(itf);
|
||||||
|
|
||||||
if (len &&
|
if (len) {
|
||||||
mutex_try_enter(&ud->usb_mtx, NULL)) {
|
uart_data_t *ud = &UART_DATA[itf];
|
||||||
|
|
||||||
|
mutex_enter_blocking(&ud->usb_mtx);
|
||||||
|
|
||||||
len = MIN(len, BUFFER_SIZE - ud->usb_pos);
|
len = MIN(len, BUFFER_SIZE - ud->usb_pos);
|
||||||
if (len) {
|
if (len) {
|
||||||
uint32_t count;
|
uint32_t count;
|
||||||
|
|
||||||
count = tud_cdc_n_read(itf, &ud->usb_buffer[ud->usb_pos], len);
|
count = tud_cdc_n_read(itf, ud->usb_buffer, len);
|
||||||
ud->usb_pos += count;
|
ud->usb_pos += count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -147,17 +139,17 @@ void usb_read_bytes(uint8_t itf)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void usb_write_bytes(uint8_t itf)
|
void usb_write_bytes(uint8_t itf) {
|
||||||
{
|
|
||||||
uart_data_t *ud = &UART_DATA[itf];
|
uart_data_t *ud = &UART_DATA[itf];
|
||||||
|
|
||||||
if (ud->uart_pos &&
|
if (ud->uart_pos) {
|
||||||
mutex_try_enter(&ud->uart_mtx, NULL)) {
|
|
||||||
uint32_t count;
|
uint32_t count;
|
||||||
|
|
||||||
|
mutex_enter_blocking(&ud->uart_mtx);
|
||||||
|
|
||||||
count = tud_cdc_n_write(itf, ud->uart_buffer, ud->uart_pos);
|
count = tud_cdc_n_write(itf, ud->uart_buffer, ud->uart_pos);
|
||||||
if (count < ud->uart_pos)
|
if (count < ud->uart_pos)
|
||||||
memmove(ud->uart_buffer, &ud->uart_buffer[count],
|
memcpy(ud->uart_buffer, &ud->uart_buffer[count],
|
||||||
ud->uart_pos - count);
|
ud->uart_pos - count);
|
||||||
ud->uart_pos -= count;
|
ud->uart_pos -= count;
|
||||||
|
|
||||||
|
@ -201,16 +193,16 @@ void core1_entry(void)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void uart_read_bytes(uint8_t itf)
|
void uart_read_bytes(uint8_t itf) {
|
||||||
{
|
|
||||||
uart_data_t *ud = &UART_DATA[itf];
|
|
||||||
const uart_id_t *ui = &UART_ID[itf];
|
const uart_id_t *ui = &UART_ID[itf];
|
||||||
|
|
||||||
if (uart_is_readable(ui->inst)) {
|
if (uart_is_readable(ui->inst)) {
|
||||||
|
uart_data_t *ud = &UART_DATA[itf];
|
||||||
|
|
||||||
mutex_enter_blocking(&ud->uart_mtx);
|
mutex_enter_blocking(&ud->uart_mtx);
|
||||||
|
|
||||||
while (uart_is_readable(ui->inst) &&
|
while (uart_is_readable(ui->inst) &&
|
||||||
(ud->uart_pos < BUFFER_SIZE)) {
|
ud->uart_pos < BUFFER_SIZE) {
|
||||||
ud->uart_buffer[ud->uart_pos] = uart_getc(ui->inst);
|
ud->uart_buffer[ud->uart_pos] = uart_getc(ui->inst);
|
||||||
ud->uart_pos++;
|
ud->uart_pos++;
|
||||||
}
|
}
|
||||||
|
@ -219,42 +211,22 @@ static inline void uart_read_bytes(uint8_t itf)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void uart0_irq_fn(void)
|
void uart_write_bytes(uint8_t itf) {
|
||||||
{
|
|
||||||
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];
|
uart_data_t *ud = &UART_DATA[itf];
|
||||||
|
|
||||||
if (ud->usb_pos &&
|
if (ud->usb_pos) {
|
||||||
mutex_try_enter(&ud->usb_mtx, NULL)) {
|
|
||||||
const uart_id_t *ui = &UART_ID[itf];
|
const uart_id_t *ui = &UART_ID[itf];
|
||||||
uint32_t count = 0;
|
|
||||||
|
|
||||||
while (uart_is_writable(ui->inst) &&
|
mutex_enter_blocking(&ud->usb_mtx);
|
||||||
count < ud->usb_pos) {
|
|
||||||
uart_putc_raw(ui->inst, ud->usb_buffer[count]);
|
|
||||||
count++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (count < ud->usb_pos)
|
uart_write_blocking(ui->inst, ud->usb_buffer, ud->usb_pos);
|
||||||
memmove(ud->usb_buffer, &ud->usb_buffer[count],
|
ud->usb_pos = 0;
|
||||||
ud->usb_pos - count);
|
|
||||||
ud->usb_pos -= count;
|
|
||||||
|
|
||||||
mutex_exit(&ud->usb_mtx);
|
mutex_exit(&ud->usb_mtx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void init_uart_data(uint8_t itf)
|
void init_uart_data(uint8_t itf) {
|
||||||
{
|
|
||||||
const uart_id_t *ui = &UART_ID[itf];
|
const uart_id_t *ui = &UART_ID[itf];
|
||||||
uart_data_t *ud = &UART_DATA[itf];
|
uart_data_t *ud = &UART_DATA[itf];
|
||||||
|
|
||||||
|
@ -289,20 +261,12 @@ void init_uart_data(uint8_t itf)
|
||||||
uart_set_format(ui->inst, databits_usb2uart(ud->usb_lc.data_bits),
|
uart_set_format(ui->inst, databits_usb2uart(ud->usb_lc.data_bits),
|
||||||
stopbits_usb2uart(ud->usb_lc.stop_bits),
|
stopbits_usb2uart(ud->usb_lc.stop_bits),
|
||||||
parity_usb2uart(ud->usb_lc.parity));
|
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)
|
int main(void)
|
||||||
{
|
{
|
||||||
int itf;
|
int itf;
|
||||||
|
|
||||||
usbd_serial_init();
|
|
||||||
|
|
||||||
for (itf = 0; itf < CFG_TUD_CDC; itf++)
|
for (itf = 0; itf < CFG_TUD_CDC; itf++)
|
||||||
init_uart_data(itf);
|
init_uart_data(itf);
|
||||||
|
|
||||||
|
@ -314,6 +278,7 @@ int main(void)
|
||||||
while (1) {
|
while (1) {
|
||||||
for (itf = 0; itf < CFG_TUD_CDC; itf++) {
|
for (itf = 0; itf < CFG_TUD_CDC; itf++) {
|
||||||
update_uart_cfg(itf);
|
update_uart_cfg(itf);
|
||||||
|
uart_read_bytes(itf);
|
||||||
uart_write_bytes(itf);
|
uart_write_bytes(itf);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,6 @@
|
||||||
* Copyright (c) 2019 Damien P. George
|
* Copyright (c) 2019 Damien P. George
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <hardware/flash.h>
|
|
||||||
#include <tusb.h>
|
#include <tusb.h>
|
||||||
|
|
||||||
#define DESC_STR_MAX 20
|
#define DESC_STR_MAX 20
|
||||||
|
@ -18,21 +17,18 @@
|
||||||
#define USBD_PID 0x000A /* Raspberry Pi Pico SDK CDC */
|
#define USBD_PID 0x000A /* Raspberry Pi Pico SDK CDC */
|
||||||
|
|
||||||
#define USBD_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN * CFG_TUD_CDC)
|
#define USBD_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN * CFG_TUD_CDC)
|
||||||
#define USBD_MAX_POWER_MA 500
|
#define USBD_MAX_POWER_MA 250
|
||||||
|
|
||||||
#define USBD_ITF_CDC_0 0
|
#define USBD_ITF_CDC_0 0
|
||||||
#define USBD_ITF_CDC_1 2
|
#define USBD_ITF_CDC_1 2
|
||||||
#define USBD_ITF_MAX 4
|
#define USBD_ITF_MAX 4
|
||||||
|
|
||||||
#define USBD_CDC_0_EP_CMD 0x81
|
#define USBD_CDC_0_EP_CMD 0x81
|
||||||
#define USBD_CDC_1_EP_CMD 0x83
|
#define USBD_CDC_1_EP_CMD 0x84
|
||||||
|
#define USBD_CDC_0_EP_OUT 0x02
|
||||||
#define USBD_CDC_0_EP_OUT 0x01
|
#define USBD_CDC_1_EP_OUT 0x05
|
||||||
#define USBD_CDC_1_EP_OUT 0x03
|
|
||||||
|
|
||||||
#define USBD_CDC_0_EP_IN 0x82
|
#define USBD_CDC_0_EP_IN 0x82
|
||||||
#define USBD_CDC_1_EP_IN 0x84
|
#define USBD_CDC_1_EP_IN 0x85
|
||||||
|
|
||||||
#define USBD_CDC_CMD_MAX_SIZE 8
|
#define USBD_CDC_CMD_MAX_SIZE 8
|
||||||
#define USBD_CDC_IN_OUT_MAX_SIZE 64
|
#define USBD_CDC_IN_OUT_MAX_SIZE 64
|
||||||
|
|
||||||
|
@ -40,7 +36,6 @@
|
||||||
#define USBD_STR_MANUF 0x01
|
#define USBD_STR_MANUF 0x01
|
||||||
#define USBD_STR_PRODUCT 0x02
|
#define USBD_STR_PRODUCT 0x02
|
||||||
#define USBD_STR_SERIAL 0x03
|
#define USBD_STR_SERIAL 0x03
|
||||||
#define USBD_STR_SERIAL_LEN 17
|
|
||||||
#define USBD_STR_CDC 0x04
|
#define USBD_STR_CDC 0x04
|
||||||
|
|
||||||
static const tusb_desc_device_t usbd_desc_device = {
|
static const tusb_desc_device_t usbd_desc_device = {
|
||||||
|
@ -73,12 +68,10 @@ static const uint8_t usbd_desc_cfg[USBD_DESC_LEN] = {
|
||||||
USBD_CDC_IN_OUT_MAX_SIZE),
|
USBD_CDC_IN_OUT_MAX_SIZE),
|
||||||
};
|
};
|
||||||
|
|
||||||
static char usbd_serial[USBD_STR_SERIAL_LEN] = "000000000000";
|
|
||||||
|
|
||||||
static const char *const usbd_desc_str[] = {
|
static const char *const usbd_desc_str[] = {
|
||||||
[USBD_STR_MANUF] = "Raspberry Pi",
|
[USBD_STR_MANUF] = "Raspberry Pi",
|
||||||
[USBD_STR_PRODUCT] = "Pico",
|
[USBD_STR_PRODUCT] = "Pico",
|
||||||
[USBD_STR_SERIAL] = usbd_serial,
|
[USBD_STR_SERIAL] = "000000000000",
|
||||||
[USBD_STR_CDC] = "Board CDC",
|
[USBD_STR_CDC] = "Board CDC",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -102,7 +95,6 @@ const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid)
|
||||||
len = 1;
|
len = 1;
|
||||||
} else {
|
} else {
|
||||||
const char *str;
|
const char *str;
|
||||||
char serial[USBD_STR_SERIAL_LEN];
|
|
||||||
|
|
||||||
if (index >= sizeof(usbd_desc_str) / sizeof(usbd_desc_str[0]))
|
if (index >= sizeof(usbd_desc_str) / sizeof(usbd_desc_str[0]))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -116,13 +108,3 @@ const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid)
|
||||||
|
|
||||||
return desc_str;
|
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