Compare commits

...

21 commits
v2.1 ... master

Author SHA1 Message Date
Álvaro Fernández Rojas
9d0df3277d
github: ci: switch to ubuntu-22.04
Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
2024-04-16 12:22:25 +02:00
Álvaro Fernández Rojas
c8a4bc513f pico-sdk: update to v1.5.1
Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
2024-04-11 19:11:33 +02:00
Álvaro Fernández Rojas
b74af3b0d8 uart-bridge: restore clock speed
Apparently, some boards do not support setting a higher CPU clock:
https://github.com/Noltari/pico-uart-bridge/issues/11#issuecomment-2048104347

Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
2024-04-10 20:04:20 +02:00
Álvaro Fernández Rojas
2f05798e36 github: bump upload-artifact to v4
Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
2024-04-10 20:02:38 +02:00
Álvaro Fernández Rojas
2e3f10f756 github: bump checkout to v4
Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
2024-04-10 20:01:41 +02:00
Álvaro Fernández Rojas
ca81e5c242
Merge pull request #15 from mgduda/use_memmove_fix
Switch from memcpy to memmove when copying within buffers
2023-01-31 19:25:12 +01:00
Michael Duda
67ce07178f Switch from memcpy to memmove when copying within buffers
In the usb_write_bytes and uart_write_bytes routines, a memcpy was previously
used to copy untransmitted bytes to the beginning of the buffer (ud->uart_buffer
and ud->usb_buffer, respectively). Since the source and destination regions of
memory may potentially overlap, the use of memcpy may lead to undefined results.

From the draft C89 standard:

    4.11.2.1 The memcpy function

    Synopsis

             #include <string.h>
             void *memcpy(void *s1, const void *s2, size_t n);

    Description

       The memcpy function copies n characters from the object pointed to
    by s2 into the object pointed to by s1 .  If copying takes place
    between objects that overlap, the behavior is undefined.

    Returns

       The memcpy function returns the value of s1 .

By using memmove rather than memcpy in the usb_write_bytes and uart_write_bytes
routines, the potential for undefined behavior can be avoided.
2023-01-29 11:49:11 -07:00
Álvaro Fernández Rojas
9d05ed4b1d uart-bridge: avoid CR/LF conversion
Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
2022-11-04 11:13:57 +01:00
Álvaro Fernández Rojas
3aa5d05fe3 Switch UART0 to GPIO 16 (TX) & GPIO 17 (RX)
Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
2022-11-04 11:08:49 +01:00
Álvaro Fernández Rojas
01e7831501 uart-bridge: add UART RX interrupts
Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
2022-11-04 11:00:31 +01:00
Álvaro Fernández Rojas
3e1672f2c9 Increase buffers and improve USB descriptors
Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
2022-11-04 10:59:54 +01:00
Álvaro Fernández Rojas
8db03b41ac Code cleanup
Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
2022-11-04 10:58:51 +01:00
Álvaro Fernández Rojas
71fd38df8a github: improve CI
Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
2022-11-04 09:19:25 +01:00
Álvaro Fernández Rojas
d0925bfd33 pico-sdk: update to 1.4.0
Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
2022-11-04 09:19:25 +01:00
cxxcoder
6aa7cf2958 usb-descriptors: use flash ID as USB serial
Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
2022-11-04 09:19:03 +01:00
Álvaro Fernández Rojas
05e4815f6b uart-bridge: bump clock to 250 MHz
Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
2022-05-03 19:59:45 +02:00
Álvaro Fernández Rojas
ebbc862b65 uart-bridge: increase BUFFER_SIZE
Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
2022-05-03 19:58:12 +02:00
Álvaro Fernández Rojas
71faf3097a build.sh: improve script
Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
2022-05-03 19:40:03 +02:00
Álvaro Fernández Rojas
982b071d6c github: update CI workflow
Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
2022-05-03 17:30:19 +02:00
Álvaro Fernández Rojas
a7d61bb4c8 pico-sdk: update v1.3.0
Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
2022-05-03 14:42:23 +02:00
Álvaro Fernández Rojas
2460b10523 uart-bridge: usb_read_bytes: fix usb_buffer access
Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
2021-10-25 18:42:30 +02:00
8 changed files with 122 additions and 64 deletions

View file

@ -4,27 +4,33 @@ on:
- push
- pull_request
env:
DEFAULT_PYTHON: 3.9
jobs:
pico:
name: RPi Pico compilation
runs-on: ubuntu-20.04
CI:
runs-on: ubuntu-22.04
steps:
- name: Check out code from GitHub
uses: actions/checkout@v2
- name: 'Check out code'
uses: actions/checkout@v4
- name: Install dependencies
- name: 'Install dependencies'
run: |
sudo apt-get install cmake gcc-arm-none-eabi libnewlib-arm-none-eabi
- name: Generate RPi Pico binaries
- name: 'Update Submodules'
run: |
./build.sh
git submodule sync --recursive
git submodule update --init --recursive
- name: 'Upload RPi Pico binary'
uses: actions/upload-artifact@v2
- 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

View file

@ -7,7 +7,7 @@ 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
@ -15,6 +15,7 @@ target_include_directories(uart_bridge PUBLIC
pico-sdk/lib/tinyusb/src)
target_link_libraries(uart_bridge
hardware_flash
pico_multicore
pico_stdlib
tinyusb_device)

View file

@ -13,7 +13,7 @@ Raspberry Pi Pico Pinout
| Raspberry Pi Pico GPIO | Function |
|:----------------------:|:--------:|
| GPIO0 (Pin 1) | UART0 TX |
| GPIO1 (Pin 2) | UART0 RX |
| GPIO16 (Pin 21) | UART0 TX |
| GPIO17 (Pin 22) | UART0 RX |
| GPIO4 (Pin 6) | UART1 TX |
| GPIO5 (Pin 7) | UART1 RX |

View file

@ -1,21 +1,17 @@
#!/bin/sh
#!/bin/bash
BUILD_DIR=build
PICO_SDK_DIR=pico-sdk
main () {
local cur_dir=$PWD
BASE_DIR="$(dirname ${BASH_SOURCE[0]})"
BUILD_DIR=$BASE_DIR/build
PICO_SDK_DIR=$BASE_DIR/pico-sdk
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 $@

@ -1 +1 @@
Subproject commit 2d5789eca89658a7f0a01e2d6010c0f254605d72
Subproject commit 6a7db34ff63345a7badec79ebea3aaef1712f374

View file

@ -13,7 +13,9 @@
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE
#define CFG_TUD_CDC 2
#define CFG_TUD_CDC_RX_BUFSIZE 256
#define CFG_TUD_CDC_TX_BUFSIZE 256
#define CFG_TUD_CDC_RX_BUFSIZE 1024
#define CFG_TUD_CDC_TX_BUFSIZE 1024
void usbd_serial_init(void);
#endif /* _TUSB_CONFIG_H_ */

View file

@ -17,7 +17,7 @@
#define LED_PIN 25
#define BUFFER_SIZE 64
#define BUFFER_SIZE 2560
#define DEF_BIT_RATE 115200
#define DEF_STOP_BITS 1
@ -26,6 +26,8 @@
typedef struct {
uart_inst_t *const inst;
uint irq;
void *irq_fn;
uint8_t tx_pin;
uint8_t rx_pin;
} uart_id_t;
@ -42,13 +44,20 @@ typedef struct {
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,
.tx_pin = 0,
.rx_pin = 1,
.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,
}
@ -119,19 +128,18 @@ void update_uart_cfg(uint8_t itf)
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);
if (len) {
uart_data_t *ud = &UART_DATA[itf];
mutex_enter_blocking(&ud->usb_mtx);
if (len &&
mutex_try_enter(&ud->usb_mtx, NULL)) {
len = MIN(len, BUFFER_SIZE - ud->usb_pos);
if (len) {
uint32_t count;
count = tud_cdc_n_read(itf, ud->usb_buffer, len);
count = tud_cdc_n_read(itf, &ud->usb_buffer[ud->usb_pos], len);
ud->usb_pos += count;
}
@ -139,17 +147,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];
if (ud->uart_pos) {
if (ud->uart_pos &&
mutex_try_enter(&ud->uart_mtx, NULL)) {
uint32_t count;
mutex_enter_blocking(&ud->uart_mtx);
count = tud_cdc_n_write(itf, ud->uart_buffer, ud->uart_pos);
if (count < ud->uart_pos)
memcpy(ud->uart_buffer, &ud->uart_buffer[count],
memmove(ud->uart_buffer, &ud->uart_buffer[count],
ud->uart_pos - count);
ud->uart_pos -= count;
@ -181,7 +189,7 @@ void core1_entry(void)
int con = 0;
tud_task();
for (itf = 0; itf < CFG_TUD_CDC; itf++) {
if (tud_cdc_n_connected(itf)) {
con = 1;
@ -193,16 +201,16 @@ void core1_entry(void)
}
}
void uart_read_bytes(uint8_t itf) {
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)) {
uart_data_t *ud = &UART_DATA[itf];
mutex_enter_blocking(&ud->uart_mtx);
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_pos++;
}
@ -211,22 +219,42 @@ void uart_read_bytes(uint8_t itf) {
}
}
void uart_write_bytes(uint8_t itf) {
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) {
if (ud->usb_pos &&
mutex_try_enter(&ud->usb_mtx, NULL)) {
const uart_id_t *ui = &UART_ID[itf];
uint32_t count = 0;
mutex_enter_blocking(&ud->usb_mtx);
while (uart_is_writable(ui->inst) &&
count < ud->usb_pos) {
uart_putc_raw(ui->inst, ud->usb_buffer[count]);
count++;
}
uart_write_blocking(ui->inst, ud->usb_buffer, ud->usb_pos);
ud->usb_pos = 0;
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) {
void init_uart_data(uint8_t itf)
{
const uart_id_t *ui = &UART_ID[itf];
uart_data_t *ud = &UART_DATA[itf];
@ -261,12 +289,20 @@ void init_uart_data(uint8_t itf) {
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)
{
int itf;
usbd_serial_init();
for (itf = 0; itf < CFG_TUD_CDC; itf++)
init_uart_data(itf);
@ -278,7 +314,6 @@ int main(void)
while (1) {
for (itf = 0; itf < CFG_TUD_CDC; itf++) {
update_uart_cfg(itf);
uart_read_bytes(itf);
uart_write_bytes(itf);
}
}

View file

@ -9,6 +9,7 @@
* Copyright (c) 2019 Damien P. George
*/
#include <hardware/flash.h>
#include <tusb.h>
#define DESC_STR_MAX 20
@ -17,18 +18,21 @@
#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_MAX_POWER_MA 250
#define USBD_MAX_POWER_MA 500
#define USBD_ITF_CDC_0 0
#define USBD_ITF_CDC_1 2
#define USBD_ITF_MAX 4
#define USBD_CDC_0_EP_CMD 0x81
#define USBD_CDC_1_EP_CMD 0x84
#define USBD_CDC_0_EP_OUT 0x02
#define USBD_CDC_1_EP_OUT 0x05
#define USBD_CDC_1_EP_CMD 0x83
#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 0x85
#define USBD_CDC_1_EP_IN 0x84
#define USBD_CDC_CMD_MAX_SIZE 8
#define USBD_CDC_IN_OUT_MAX_SIZE 64
@ -36,6 +40,7 @@
#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 = {
@ -68,10 +73,12 @@ static const uint8_t usbd_desc_cfg[USBD_DESC_LEN] = {
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",
[USBD_STR_SERIAL] = usbd_serial,
[USBD_STR_CDC] = "Board CDC",
};
@ -95,6 +102,7 @@ const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid)
len = 1;
} else {
const char *str;
char serial[USBD_STR_SERIAL_LEN];
if (index >= sizeof(usbd_desc_str) / sizeof(usbd_desc_str[0]))
return NULL;
@ -108,3 +116,13 @@ const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid)
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]);
}