Skip to content

Commit 72a8cf2

Browse files
committed
Add hardware flow-control
1 parent 7a8e3bf commit 72a8cf2

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
cmake_minimum_required(VERSION 3.13)
44

5+
option(FLOW_CONTROL "Enable Hardware Flow-control on the UARTs" FALSE)
6+
57
include(pico-sdk/pico_sdk_init.cmake)
68

79
project(pico_uart_bridge)
@@ -19,4 +21,8 @@ target_link_libraries(uart_bridge
1921
pico_stdlib
2022
tinyusb_device)
2123

24+
if(FLOW_CONTROL)
25+
target_compile_definitions(uart_bridge PUBLIC FLOW_CONTROL=1)
26+
endif()
27+
2228
pico_add_extra_outputs(uart_bridge)

README.md

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,24 @@ This software is provided without warranty, according to the MIT License, and sh
1111
Raspberry Pi Pico Pinout
1212
------------------------
1313

14-
| Raspberry Pi Pico GPIO | Function |
15-
|:----------------------:|:--------:|
16-
| GPIO0 (Pin 1) | UART0 TX |
17-
| GPIO1 (Pin 2) | UART0 RX |
18-
| GPIO4 (Pin 6) | UART1 TX |
19-
| GPIO5 (Pin 7) | UART1 RX |
14+
| Raspberry Pi Pico GPIO | Function |
15+
|:----------------------:|:---------:|
16+
| GPIO0 (Pin 1) | UART0 TX |
17+
| GPIO1 (Pin 2) | UART0 RX |
18+
| GPIO2 (Pin 4) | UART0 CTS |
19+
| GPIO3 (Pin 5) | UART0 RTS |
20+
| GPIO4 (Pin 6) | UART1 TX |
21+
| GPIO5 (Pin 7) | UART1 RX |
22+
| GPIO6 (Pin 9) | UART1 CTS |
23+
| GPIO7 (Pin 10) | UART1 RTS |
24+
25+
Optional Hardware Flow-control
26+
------------------------------
27+
28+
Hardware Flow-control is disabled by default, but can be compiled in by running:
29+
30+
``` bash
31+
cmake -DFLOW_CONTROL .
32+
make
33+
```
34+

uart-bridge.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717

1818
#define LED_PIN 25
1919

20+
#ifdef FLOW_CONTROL
21+
#define BUFFER_SIZE 256
22+
#else
2023
#define BUFFER_SIZE 64
24+
#endif
2125

2226
#define DEF_BIT_RATE 115200
2327
#define DEF_STOP_BITS 1
@@ -28,6 +32,10 @@ typedef struct {
2832
uart_inst_t *const inst;
2933
uint8_t tx_pin;
3034
uint8_t rx_pin;
35+
#ifdef FLOW_CONTROL
36+
uint8_t rts_pin;
37+
uint8_t cts_pin;
38+
#endif
3139
} uart_id_t;
3240

3341
typedef struct {
@@ -47,10 +55,18 @@ const uart_id_t UART_ID[CFG_TUD_CDC] = {
4755
.inst = uart0,
4856
.tx_pin = 0,
4957
.rx_pin = 1,
58+
#ifdef FLOW_CONTROL
59+
.cts_pin = 2,
60+
.rts_pin = 3,
61+
#endif
5062
}, {
5163
.inst = uart1,
5264
.tx_pin = 4,
5365
.rx_pin = 5,
66+
#ifdef FLOW_CONTROL
67+
.cts_pin = 6,
68+
.rts_pin = 7,
69+
#endif
5470
}
5571
};
5672

@@ -233,6 +249,10 @@ void init_uart_data(uint8_t itf) {
233249
/* Pinmux */
234250
gpio_set_function(ui->tx_pin, GPIO_FUNC_UART);
235251
gpio_set_function(ui->rx_pin, GPIO_FUNC_UART);
252+
#ifdef FLOW_CONTROL
253+
gpio_set_function(ui->rts_pin, GPIO_FUNC_UART);
254+
gpio_set_function(ui->cts_pin, GPIO_FUNC_UART);
255+
#endif
236256

237257
/* USB CDC LC */
238258
ud->usb_lc.bit_rate = DEF_BIT_RATE;
@@ -257,7 +277,11 @@ void init_uart_data(uint8_t itf) {
257277

258278
/* UART start */
259279
uart_init(ui->inst, ud->usb_lc.bit_rate);
280+
#ifdef FLOW_CONTROL
281+
uart_set_hw_flow(ui->inst, true, true);
282+
#else
260283
uart_set_hw_flow(ui->inst, false, false);
284+
#endif
261285
uart_set_format(ui->inst, databits_usb2uart(ud->usb_lc.data_bits),
262286
stopbits_usb2uart(ud->usb_lc.stop_bits),
263287
parity_usb2uart(ud->usb_lc.parity));

0 commit comments

Comments
 (0)