mirror of
https://github.com/Noltari/pico-uart-bridge.git
synced 2025-05-14 08:12:20 +00:00
uart-bridge: add HW flow control
Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
This commit is contained in:
parent
05e4815f6b
commit
f64b2ace58
2 changed files with 36 additions and 7 deletions
16
README.md
16
README.md
|
@ -11,9 +11,13 @@ This software is provided without warranty, according to the MIT License, and sh
|
||||||
Raspberry Pi Pico Pinout
|
Raspberry Pi Pico Pinout
|
||||||
------------------------
|
------------------------
|
||||||
|
|
||||||
| Raspberry Pi Pico GPIO | Function |
|
| Raspberry Pi Pico GPIO | Function |
|
||||||
|:----------------------:|:--------:|
|
|:----------------------:|:---------:|
|
||||||
| GPIO0 (Pin 1) | UART0 TX |
|
| GPIO0 (Pin 1) | UART0 TX |
|
||||||
| GPIO1 (Pin 2) | UART0 RX |
|
| GPIO1 (Pin 2) | UART0 RX |
|
||||||
| GPIO4 (Pin 6) | UART1 TX |
|
| GPIO2 (Pin 4) | UART0 CTS |
|
||||||
| GPIO5 (Pin 7) | UART1 RX |
|
| GPIO3 (Pin 5) | UART0 RTS |
|
||||||
|
| GPIO4 (Pin 6) | UART1 TX |
|
||||||
|
| GPIO5 (Pin 7) | UART1 RX |
|
||||||
|
| GPIO6 (Pin 9) | UART1 CTS |
|
||||||
|
| GPIO7 (Pin 10) | UART1 RTS |
|
||||||
|
|
|
@ -11,6 +11,10 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <tusb.h>
|
#include <tusb.h>
|
||||||
|
|
||||||
|
#if !defined(BIT)
|
||||||
|
#define BIT(a) (1 << a)
|
||||||
|
#endif /* BIT */
|
||||||
|
|
||||||
#if !defined(MIN)
|
#if !defined(MIN)
|
||||||
#define MIN(a, b) ((a > b) ? b : a)
|
#define MIN(a, b) ((a > b) ? b : a)
|
||||||
#endif /* MIN */
|
#endif /* MIN */
|
||||||
|
@ -19,19 +23,26 @@
|
||||||
|
|
||||||
#define BUFFER_SIZE 256
|
#define BUFFER_SIZE 256
|
||||||
|
|
||||||
|
#define DEF_HW_FLOW false
|
||||||
#define DEF_BIT_RATE 115200
|
#define DEF_BIT_RATE 115200
|
||||||
#define DEF_STOP_BITS 1
|
#define DEF_STOP_BITS 1
|
||||||
#define DEF_PARITY 0
|
#define DEF_PARITY 0
|
||||||
#define DEF_DATA_BITS 8
|
#define DEF_DATA_BITS 8
|
||||||
|
|
||||||
|
#define USB_LS_RTS BIT(1)
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uart_inst_t *const inst;
|
uart_inst_t *const inst;
|
||||||
uint8_t tx_pin;
|
uint8_t tx_pin;
|
||||||
uint8_t rx_pin;
|
uint8_t rx_pin;
|
||||||
|
uint8_t cts_pin;
|
||||||
|
uint8_t rts_pin;
|
||||||
} uart_id_t;
|
} uart_id_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
bool usb_hf;
|
||||||
cdc_line_coding_t usb_lc;
|
cdc_line_coding_t usb_lc;
|
||||||
|
bool uart_hf;
|
||||||
cdc_line_coding_t uart_lc;
|
cdc_line_coding_t uart_lc;
|
||||||
mutex_t lc_mtx;
|
mutex_t lc_mtx;
|
||||||
uint8_t uart_buffer[BUFFER_SIZE];
|
uint8_t uart_buffer[BUFFER_SIZE];
|
||||||
|
@ -47,10 +58,14 @@ const uart_id_t UART_ID[CFG_TUD_CDC] = {
|
||||||
.inst = uart0,
|
.inst = uart0,
|
||||||
.tx_pin = 0,
|
.tx_pin = 0,
|
||||||
.rx_pin = 1,
|
.rx_pin = 1,
|
||||||
|
.cts_pin = 2,
|
||||||
|
.rts_pin = 3,
|
||||||
}, {
|
}, {
|
||||||
.inst = uart1,
|
.inst = uart1,
|
||||||
.tx_pin = 4,
|
.tx_pin = 4,
|
||||||
.rx_pin = 5,
|
.rx_pin = 5,
|
||||||
|
.cts_pin = 6,
|
||||||
|
.rts_pin = 7,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -99,6 +114,11 @@ void update_uart_cfg(uint8_t itf)
|
||||||
|
|
||||||
mutex_enter_blocking(&ud->lc_mtx);
|
mutex_enter_blocking(&ud->lc_mtx);
|
||||||
|
|
||||||
|
if (ud->usb_hf != ud->uart_hf) {
|
||||||
|
uart_set_hw_flow(ui->inst, ud->usb_hf, ud->usb_hf);
|
||||||
|
ud->uart_hf = ud->usb_hf;
|
||||||
|
}
|
||||||
|
|
||||||
if (ud->usb_lc.bit_rate != ud->uart_lc.bit_rate) {
|
if (ud->usb_lc.bit_rate != ud->uart_lc.bit_rate) {
|
||||||
uart_set_baudrate(ui->inst, ud->usb_lc.bit_rate);
|
uart_set_baudrate(ui->inst, ud->usb_lc.bit_rate);
|
||||||
ud->uart_lc.bit_rate = ud->usb_lc.bit_rate;
|
ud->uart_lc.bit_rate = ud->usb_lc.bit_rate;
|
||||||
|
@ -165,6 +185,7 @@ void usb_cdc_process(uint8_t itf)
|
||||||
uart_data_t *ud = &UART_DATA[itf];
|
uart_data_t *ud = &UART_DATA[itf];
|
||||||
|
|
||||||
mutex_enter_blocking(&ud->lc_mtx);
|
mutex_enter_blocking(&ud->lc_mtx);
|
||||||
|
ud->usb_hf = !!(tud_cdc_n_get_line_state(itf) & USB_LS_RTS);
|
||||||
tud_cdc_n_get_line_coding(itf, &ud->usb_lc);
|
tud_cdc_n_get_line_coding(itf, &ud->usb_lc);
|
||||||
mutex_exit(&ud->lc_mtx);
|
mutex_exit(&ud->lc_mtx);
|
||||||
|
|
||||||
|
@ -233,14 +254,18 @@ void init_uart_data(uint8_t itf) {
|
||||||
/* Pinmux */
|
/* Pinmux */
|
||||||
gpio_set_function(ui->tx_pin, GPIO_FUNC_UART);
|
gpio_set_function(ui->tx_pin, GPIO_FUNC_UART);
|
||||||
gpio_set_function(ui->rx_pin, GPIO_FUNC_UART);
|
gpio_set_function(ui->rx_pin, GPIO_FUNC_UART);
|
||||||
|
gpio_set_function(ui->cts_pin, GPIO_FUNC_UART);
|
||||||
|
gpio_set_function(ui->rts_pin, GPIO_FUNC_UART);
|
||||||
|
|
||||||
/* USB CDC LC */
|
/* USB CDC LC */
|
||||||
|
ud->usb_hf = DEF_HW_FLOW;
|
||||||
ud->usb_lc.bit_rate = DEF_BIT_RATE;
|
ud->usb_lc.bit_rate = DEF_BIT_RATE;
|
||||||
ud->usb_lc.data_bits = DEF_DATA_BITS;
|
ud->usb_lc.data_bits = DEF_DATA_BITS;
|
||||||
ud->usb_lc.parity = DEF_PARITY;
|
ud->usb_lc.parity = DEF_PARITY;
|
||||||
ud->usb_lc.stop_bits = DEF_STOP_BITS;
|
ud->usb_lc.stop_bits = DEF_STOP_BITS;
|
||||||
|
|
||||||
/* UART LC */
|
/* UART LC */
|
||||||
|
ud->uart_hf = DEF_HW_FLOW;
|
||||||
ud->uart_lc.bit_rate = DEF_BIT_RATE;
|
ud->uart_lc.bit_rate = DEF_BIT_RATE;
|
||||||
ud->uart_lc.data_bits = DEF_DATA_BITS;
|
ud->uart_lc.data_bits = DEF_DATA_BITS;
|
||||||
ud->uart_lc.parity = DEF_PARITY;
|
ud->uart_lc.parity = DEF_PARITY;
|
||||||
|
@ -257,7 +282,7 @@ void init_uart_data(uint8_t itf) {
|
||||||
|
|
||||||
/* UART start */
|
/* UART start */
|
||||||
uart_init(ui->inst, ud->usb_lc.bit_rate);
|
uart_init(ui->inst, ud->usb_lc.bit_rate);
|
||||||
uart_set_hw_flow(ui->inst, false, false);
|
uart_set_hw_flow(ui->inst, DEF_HW_FLOW, DEF_HW_FLOW);
|
||||||
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));
|
||||||
|
|
Loading…
Add table
Reference in a new issue