Compare commits

...

16 commits
next ... master

Author SHA1 Message Date
Carsten Schmiemann c0e2d44f19 build_w_line_flow_ctrl.sh hinzugefügt 2023-12-31 22:01:18 +01:00
Carsten Schmiemann 14eba4cd40 README.md aktualisiert 2023-12-31 22:00:37 +01:00
Carsten Schmiemann ed07f6dfab Add build line control script 2023-12-31 21:58:24 +01:00
Carsten Schmiemann 249c7fb383 Add build flow control script 2023-12-31 21:57:47 +01:00
Carsten Schmiemann 88122c9365 Add line control comment 2023-12-31 21:57:14 +01:00
Carsten Schmiemann 313f2b3ac7 Fork information 2023-12-31 21:46:30 +01:00
Carsten Schmiemann 388a34e5bd Add hardware handshake to readme 2023-12-31 21:45:31 +01:00
Carsten Schmiemann afc635a053 Add hardware handshake 2023-12-31 21:43:58 +01:00
Carsten Schmiemann 93f66b01fc Prepare for hardware handshaking 2023-12-31 21:43:31 +01: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
8 changed files with 167 additions and 19 deletions

View file

@ -2,12 +2,15 @@
cmake_minimum_required(VERSION 3.13)
option(FLOW_CONTROL "Enable Hardware Flow-control on the UARTs" FALSE)
option(LINE_CONTROL "Enable Hardware Line-control on the UARTs" FALSE)
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
@ -20,4 +23,12 @@ target_link_libraries(uart_bridge
pico_stdlib
tinyusb_device)
if(FLOW_CONTROL)
target_compile_definitions(uart_bridge PUBLIC FLOW_CONTROL=1)
endif()
if(LINE_CONTROL)
target_compile_definitions(uart_bridge PUBLIC LINE_CONTROL=1)
endif()
pico_add_extra_outputs(uart_bridge)

View file

@ -1,6 +1,9 @@
Raspberry Pi Pico USB-UART Bridge
=================================
This is a fork of https://github.com/Noltari/pico-uart-bridge and a pull request which takes too long time to include.
Some fixes applied by myself for a successful build.
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
@ -11,9 +14,42 @@ This software is provided without warranty, according to the MIT License, and sh
Raspberry Pi Pico Pinout
------------------------
| Raspberry Pi Pico GPIO | Function |
|:----------------------:|:--------:|
| GPIO0 (Pin 1) | UART0 TX |
| GPIO1 (Pin 2) | UART0 RX |
| GPIO4 (Pin 6) | UART1 TX |
| GPIO5 (Pin 7) | UART1 RX |
UART0:
| Raspberry Pi Pico GPIO | Function |
|:----------------------:|:---------:|
| GPIO0 (Pin 1) | TX |
| GPIO1 (Pin 2) | RX |
| GPIO2 (Pin 4) | CTS |
| GPIO3 (Pin 5) | RTS |
| GPIO4 (Pin 6) | DTR |
| GPIO5 (Pin 7) | DSR |
UART1:
| Raspberry Pi Pico GPIO | Function |
|:----------------------:|:---------:|
| GPIO8 (Pin 11) | TX |
| GPIO9 (Pin 12) | RX |
| GPIO10 (Pin 14) | CTS |
| GPIO11 (Pin 15) | RTS |
| GPIO12 (Pin 16) | DTR |
| GPIO13 (Pin 17) | DSR |
Optional Hardware Flow and Line control
------------------------------
Hardware Flow-control (RTS/CTS) is disabled if you build it just with build.sh, but there are other buld scripts:
``` bash
./build_w_flow_ctrl.sh
```
Line control (DTR/DSR):
``` bash
./build_w_line_ctrl.sh
```
To enable both:
``` bash
./build_w_line_flow_ctrl.sh
```

17
build_w_flow_ctrl.sh Normal file
View file

@ -0,0 +1,17 @@
#!/bin/bash
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
cmake -DFLOW_CONTROL=true -B $BUILD_DIR -S $BASE_DIR
make -C $BUILD_DIR
}
main $@

17
build_w_line_ctrl.sh Normal file
View file

@ -0,0 +1,17 @@
#!/bin/bash
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
cmake -DLINE_CONTROL=true -B $BUILD_DIR -S $BASE_DIR
make -C $BUILD_DIR
}
main $@

17
build_w_line_flow_ctrl.sh Normal file
View file

@ -0,0 +1,17 @@
#!/bin/bash
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
cmake -DLINE_CONTROL=true -DFLOW_CONTROL=true -B $BUILD_DIR -S $BASE_DIR
make -C $BUILD_DIR
}
main $@

View file

@ -13,8 +13,8 @@
#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);

View file

@ -28,6 +28,14 @@ typedef struct {
uart_inst_t *const inst;
uint8_t tx_pin;
uint8_t rx_pin;
#ifdef FLOW_CONTROL
uint8_t rts_pin;
uint8_t cts_pin;
#endif
#ifdef LINE_CONTROL
uint8_t dtr_pin;
uint8_t dsr_pin;
#endif
} uart_id_t;
typedef struct {
@ -47,10 +55,26 @@ const uart_id_t UART_ID[CFG_TUD_CDC] = {
.inst = uart0,
.tx_pin = 0,
.rx_pin = 1,
#ifdef FLOW_CONTROL
.cts_pin = 2,
.rts_pin = 3,
#endif
#ifdef LINE_CONTROL
.dtr_pin = 4,
.dsr_pin = 5,
#endif
}, {
.inst = uart1,
.tx_pin = 4,
.rx_pin = 5,
.tx_pin = 8,
.rx_pin = 9,
#ifdef FLOW_CONTROL
.cts_pin = 10,
.rts_pin = 11,
#endif
#ifdef LINE_CONTROL
.dtr_pin = 12,
.dsr_pin = 13,
#endif
}
};
@ -181,7 +205,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;
@ -233,6 +257,17 @@ void init_uart_data(uint8_t itf) {
/* Pinmux */
gpio_set_function(ui->tx_pin, GPIO_FUNC_UART);
gpio_set_function(ui->rx_pin, GPIO_FUNC_UART);
#ifdef FLOW_CONTROL
gpio_set_function(ui->rts_pin, GPIO_FUNC_UART);
gpio_set_function(ui->cts_pin, GPIO_FUNC_UART);
#endif
#ifdef LINE_CONTROL
gpio_init(ui->dtr_pin);
gpio_set_dir(ui->dtr_pin, GPIO_OUT);
gpio_init(ui->dsr_pin);
gpio_set_dir(ui->dsr_pin, GPIO_IN);
#endif
/* USB CDC LC */
ud->usb_lc.bit_rate = DEF_BIT_RATE;
@ -257,20 +292,32 @@ void init_uart_data(uint8_t itf) {
/* UART start */
uart_init(ui->inst, ud->usb_lc.bit_rate);
#ifdef FLOW_CONTROL
uart_set_hw_flow(ui->inst, true, true);
#else
uart_set_hw_flow(ui->inst, false, false);
#endif
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));
}
#ifdef LINE_CONTROL
/* Invoked when line state has changed */
void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts)
{
const uart_id_t *ui = &UART_ID[itf];
gpio_put(ui->dtr_pin, dtr);
}
#endif
int main(void)
{
int itf;
set_sys_clock_khz(250000, false);
usbd_serial_init();
for (itf = 0; itf < CFG_TUD_CDC; itf++)
init_uart_data(itf);

View file

@ -18,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