Skip to content

Commit feac173

Browse files
committed
Add line control (DTR/DSR)
DSR doesn't do anything yet.
1 parent e729db5 commit feac173

File tree

3 files changed

+70
-15
lines changed

3 files changed

+70
-15
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,8 @@ if(FLOW_CONTROL)
2525
target_compile_definitions(uart_bridge PUBLIC FLOW_CONTROL=1)
2626
endif()
2727

28+
if(LINE_CONTROL)
29+
target_compile_definitions(uart_bridge PUBLIC LINE_CONTROL=1)
30+
endif()
31+
2832
pico_add_extra_outputs(uart_bridge)

README.md

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

14+
UART0:
1415
| Raspberry Pi Pico GPIO | Function |
1516
|:----------------------:|:---------:|
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
17+
| GPIO0 (Pin 1) | TX |
18+
| GPIO1 (Pin 2) | RX |
19+
| GPIO2 (Pin 4) | CTS |
20+
| GPIO3 (Pin 5) | RTS |
21+
| GPIO4 (Pin 6) | DTR |
22+
| GPIO5 (Pin 7) | DSR |
23+
24+
UART1:
25+
| Raspberry Pi Pico GPIO | Function |
26+
|:----------------------:|:---------:|
27+
| GPIO8 (Pin 11) | TX |
28+
| GPIO9 (Pin 12) | RX |
29+
| GPIO10 (Pin 14) | CTS |
30+
| GPIO11 (Pin 15) | RTS |
31+
| GPIO12 (Pin 16) | DTR |
32+
| GPIO13 (Pin 17) | DSR |
33+
34+
Optional Hardware Flow and Line control
2635
------------------------------
2736

28-
Hardware Flow-control is disabled by default, but can be compiled in by running:
37+
Hardware Flow-control (RTS/CTS) is disabled by default, but can be compiled in by running:
2938

3039
``` bash
3140
cmake -DFLOW_CONTROL .
3241
make
3342
```
43+
44+
Line control (DTR/DSR) is disabled by default, but can be compiled in by running:
45+
46+
``` bash
47+
cmake -DLINE_CONTROL .
48+
make
49+
```
50+
51+
To enable both:
52+
``` bash
53+
cmake -DLINE_CONTROL -DFLOW_CONTROL .
54+
make
55+
```

uart-bridge.c

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ typedef struct {
3232
uint8_t rts_pin;
3333
uint8_t cts_pin;
3434
#endif
35+
#ifdef LINE_CONTROL
36+
uint8_t dtr_pin;
37+
uint8_t dsr_pin;
38+
#endif
3539
} uart_id_t;
3640

3741
typedef struct {
@@ -54,14 +58,22 @@ const uart_id_t UART_ID[CFG_TUD_CDC] = {
5458
#ifdef FLOW_CONTROL
5559
.cts_pin = 2,
5660
.rts_pin = 3,
61+
#endif
62+
#ifdef LINE_CONTROL
63+
.dtr_pin = 4,
64+
.dsr_pin = 5,
5765
#endif
5866
}, {
5967
.inst = uart1,
60-
.tx_pin = 4,
61-
.rx_pin = 5,
68+
.tx_pin = 8,
69+
.rx_pin = 9,
6270
#ifdef FLOW_CONTROL
63-
.cts_pin = 6,
64-
.rts_pin = 7,
71+
.cts_pin = 10,
72+
.rts_pin = 11,
73+
#endif
74+
#ifdef LINE_CONTROL
75+
.dtr_pin = 12,
76+
.dsr_pin = 13,
6577
#endif
6678
}
6779
};
@@ -250,6 +262,13 @@ void init_uart_data(uint8_t itf) {
250262
gpio_set_function(ui->cts_pin, GPIO_FUNC_UART);
251263
#endif
252264

265+
#ifdef LINE_CONTROL
266+
gpio_init(ui->dtr_pin);
267+
gpio_set_dir(ui->dtr_pin, GPIO_OUT);
268+
gpio_init(ui->dsr_pin);
269+
gpio_set_dir(ui->dsr_pin, GPIO_IN);
270+
#endif
271+
253272
/* USB CDC LC */
254273
ud->usb_lc.bit_rate = DEF_BIT_RATE;
255274
ud->usb_lc.data_bits = DEF_DATA_BITS;
@@ -283,6 +302,16 @@ void init_uart_data(uint8_t itf) {
283302
parity_usb2uart(ud->usb_lc.parity));
284303
}
285304

305+
#ifdef LINE_CONTROL
306+
/* Invoked when line state has changed */
307+
void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts)
308+
{
309+
const uart_id_t *ui = &UART_ID[itf];
310+
311+
gpio_put(ui->dtr_pin, dtr);
312+
}
313+
#endif
314+
286315
int main(void)
287316
{
288317
int itf;

0 commit comments

Comments
 (0)