Skip to content

Commit 7e6538f

Browse files
author
Marcus Chang
committed
Optional hardware flow control for STDOUT
Some platforms have interface chips with hardware flow control enabled by default. This commit adds configurable flow control to STDOUT. Usage: * Define pin names STDIO_UART_RTS for Rx-flow-control and STDIO_UART_CTS for Tx-flow-control. * Set target.console-uart-flow-control. Valid options are: null, RTS, CTS, and RTSCTS.
1 parent 3bc2d2e commit 7e6538f

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

platform/mbed_retarget.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ static SingletonPtr<PlatformMutex> _mutex;
7272

7373
#define FILE_HANDLE_RESERVED ((FileHandle*)0xFFFFFFFF)
7474

75+
/**
76+
* Macros for setting console flow control.
77+
*/
78+
#define CONSOLE_FLOWCONTROL_RTS 1
79+
#define CONSOLE_FLOWCONTROL_CTS 2
80+
#define CONSOLE_FLOWCONTROL_RTSCTS 3
81+
#define mbed_console_concat_(x) CONSOLE_FLOWCONTROL_##x
82+
#define mbed_console_concat(x) mbed_console_concat_(x)
83+
#define CONSOLE_FLOWCONTROL mbed_console_concat(MBED_CONF_TARGET_CONSOLE_UART_FLOW_CONTROL)
84+
7585
using namespace mbed;
7686

7787
#if defined(__MICROLIB) && (__ARMCC_VERSION>5030000)
@@ -147,6 +157,13 @@ DirectSerial::DirectSerial(PinName tx, PinName rx, int baud) {
147157
if (stdio_uart_inited) return;
148158
serial_init(&stdio_uart, tx, rx);
149159
serial_baud(&stdio_uart, baud);
160+
#if CONSOLE_FLOWCONTROL == CONSOLE_FLOWCONTROL_RTS
161+
serial_set_flow_control(&stdio_uart, FlowControlRTS, STDIO_UART_RTS, NC);
162+
#elif CONSOLE_FLOWCONTROL == CONSOLE_FLOWCONTROL_CTS
163+
serial_set_flow_control(&stdio_uart, FlowControlCTS, NC, STDIO_UART_CTS);
164+
#elif CONSOLE_FLOWCONTROL == CONSOLE_FLOWCONTROL_RTSCTS
165+
serial_set_flow_control(&stdio_uart, FlowControlRTSCTS, STDIO_UART_RTS, STDIO_UART_CTS);
166+
#endif
150167
}
151168

152169
ssize_t DirectSerial::write(const void *buffer, size_t size) {
@@ -216,6 +233,13 @@ static FileHandle* default_console()
216233
#if DEVICE_SERIAL
217234
# if MBED_CONF_PLATFORM_STDIO_BUFFERED_SERIAL
218235
static UARTSerial console(STDIO_UART_TX, STDIO_UART_RX, MBED_CONF_PLATFORM_STDIO_BAUD_RATE);
236+
# if CONSOLE_FLOWCONTROL == CONSOLE_FLOWCONTROL_RTS
237+
console.set_flow_control(SerialBase::RTS, STDIO_UART_RTS, NC);
238+
# elif CONSOLE_FLOWCONTROL == CONSOLE_FLOWCONTROL_CTS
239+
console.set_flow_control(SerialBase::CTS, NC, STDIO_UART_CTS);
240+
# elif CONSOLE_FLOWCONTROL == CONSOLE_FLOWCONTROL_RTSCTS
241+
console.set_flow_control(SerialBase::RTSCTS, STDIO_UART_RTS, STDIO_UART_CTS);
242+
# endif
219243
# else
220244
static DirectSerial console(STDIO_UART_TX, STDIO_UART_RX, MBED_CONF_PLATFORM_STDIO_BAUD_RATE);
221245
# endif

targets/targets.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@
1111
"detect_code": [],
1212
"public": false,
1313
"default_lib": "std",
14-
"bootloader_supported": false
14+
"bootloader_supported": false,
15+
"config": {
16+
"console-uart-flow-control": {
17+
"help": "Console hardware flow control. Options: null, RTS, CTS, RTSCTS.",
18+
"value": null
19+
}
20+
}
1521
},
1622
"CM4_UARM": {
1723
"inherits": ["Target"],

0 commit comments

Comments
 (0)