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/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/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) ); diff --git a/test/serial_interrupt/main.cpp b/test/serial_interrupt/main.cpp index e512da60..7403279d 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() {