From c0728ac3983f67b71d3410a07a7eb65b3d083774 Mon Sep 17 00:00:00 2001 From: Martin Kojtal Date: Fri, 22 Jan 2016 13:29:22 +0000 Subject: [PATCH 1/3] Retarget - stdio serial lazy initialization We provide mbed::get_serial_stdio() to get a reference for stdio Serial object. We should discourage to use Serial pc(USBTX, USBRX) --- mbed-drivers/Serial.h | 6 ++++++ source/retarget.cpp | 31 ++++++++++++++++++++----------- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/mbed-drivers/Serial.h b/mbed-drivers/Serial.h index aa065405..02004db8 100644 --- a/mbed-drivers/Serial.h +++ b/mbed-drivers/Serial.h @@ -67,6 +67,12 @@ class Serial : public SerialBase, public Stream { virtual int _putc(int c); }; +/** Get the stdio Serial object, which is lazy instantiated. + * + * @returns stdio object + */ +Serial& get_stdio_serial(); + } // namespace mbed #endif diff --git a/source/retarget.cpp b/source/retarget.cpp index c62efdbc..e6c0e8c9 100644 --- a/source/retarget.cpp +++ b/source/retarget.cpp @@ -102,16 +102,29 @@ FileHandle::~FileHandle() { } #if DEVICE_SERIAL + +#include "mbed-drivers/Serial.h" + static int stdio_uart_inited; -static serial_t stdio_uart; + +namespace mbed { + +Serial& get_stdio_serial() +{ + static Serial stdio_serial(STDIO_UART_TX, STDIO_UART_RX); + if (stdio_uart_inited == 0) { + stdio_serial.baud(STDIO_DEFAULT_BAUD); + stdio_uart_inited = 1; + } + return stdio_serial; +} + +} #endif static void init_serial() { #if DEVICE_SERIAL - if (stdio_uart_inited) return; - serial_init(&stdio_uart, STDIO_UART_TX, STDIO_UART_RX); - serial_baud(&stdio_uart, STDIO_DEFAULT_BAUD); - stdio_uart_inited = 1; + get_stdio_serial(); #endif } @@ -243,9 +256,8 @@ extern "C" int PREFIX(_write)(FILEHANDLE fh, const unsigned char *buffer, unsign int n; // n is the number of bytes written if (fh < 3) { #if DEVICE_SERIAL - if (!stdio_uart_inited) init_serial(); for (unsigned int i = 0; i < length; i++) { - serial_putc(&stdio_uart, buffer[i]); + get_stdio_serial().putc(buffer[i]); } #endif n = length; @@ -281,8 +293,7 @@ extern "C" int PREFIX(_read)(FILEHANDLE fh, unsigned char *buffer, unsigned int if (fh < 3) { // only read a character at a time from stdin #if DEVICE_SERIAL - if (!stdio_uart_inited) init_serial(); - *buffer = serial_getc(&stdio_uart); + *buffer = get_stdio_serial().getc(); #endif n = 1; } else { @@ -493,8 +504,6 @@ extern "C" void __iar_argc_argv() { // the user should set up their application in app_start extern void app_start(int, char**); extern "C" int main(void) { - // init serial if it has not been invoked prior main - init_serial(); minar::Scheduler::postCallback( mbed::util::FunctionPointer2(&app_start).bind(0, NULL) ); From 7c834dce79f16bbd404339bc0af4c3beab31c62e Mon Sep 17 00:00:00 2001 From: Martin Kojtal Date: Fri, 22 Jan 2016 14:39:31 +0000 Subject: [PATCH 2/3] Test serial - get stdio Serial object, baud set via config --- test/serial_interrupt/main.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/serial_interrupt/main.cpp b/test/serial_interrupt/main.cpp index e512da60..8642b8cf 100644 --- a/test/serial_interrupt/main.cpp +++ b/test/serial_interrupt/main.cpp @@ -20,7 +20,7 @@ DigitalOut led1(LED1); DigitalOut led2(LED2); -Serial computer(USBTX, USBRX); +Serial& computer = get_stdio_serial(); // This function is called when a character goes into the TX buffer. void txCallback() { @@ -38,7 +38,6 @@ void app_start(int, char*[]) { MBED_HOSTTEST_SELECT(echo); MBED_HOSTTEST_DESCRIPTION(serial interrupt test); MBED_HOSTTEST_START("MBED_14"); - computer.baud(115200); computer.attach(&txCallback, Serial::TxIrq); computer.attach(&rxCallback, Serial::RxIrq); } From ca7f4488624940f68e12accb603435cb0b0f5c42 Mon Sep 17 00:00:00 2001 From: Martin Kojtal Date: Mon, 25 Jan 2016 10:12:29 +0000 Subject: [PATCH 3/3] Readme - add STDIO section --- readme.md | 11 +++++++++++ test/serial_interrupt/main.cpp | 1 + 2 files changed, 12 insertions(+) diff --git a/readme.md b/readme.md index a91edd7a..a49e3ef8 100644 --- a/readme.md +++ b/readme.md @@ -9,3 +9,14 @@ for more details). Because of this, you'll always want to make `mbed-drivers` a dependency of your mbed OS application. Check [our "Getting started" guide](https://docs.mbed.com/docs/getting-started-mbed-os/) for more details about `mbed-drivers` and mbed OS in general. + +### STDIO retartgeting + +The mbed-drivers defines retargeting of stdin, stdout and stderr to UART. The default baudrate for STDIO UART peripheral is set via ```YOTTA_CFG_MBED_OS_STDIO_DEFAULT_BAUD```. If this yotta config is not defined, the default value is 115200. + +To change STDIO serial settings in the runtime, retrieve the Serial STDIO object ```get_stdio_serial()```. + +``` +Serial& pc = get_stdio_serial(); +pc.baud(9600); +``` diff --git a/test/serial_interrupt/main.cpp b/test/serial_interrupt/main.cpp index 8642b8cf..7403279d 100644 --- a/test/serial_interrupt/main.cpp +++ b/test/serial_interrupt/main.cpp @@ -38,6 +38,7 @@ void app_start(int, char*[]) { MBED_HOSTTEST_SELECT(echo); MBED_HOSTTEST_DESCRIPTION(serial interrupt test); MBED_HOSTTEST_START("MBED_14"); + computer.baud(115200); computer.attach(&txCallback, Serial::TxIrq); computer.attach(&rxCallback, Serial::RxIrq); }