Skip to content

Retarget - stdio serial lazy initialization #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions mbed-drivers/Serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
```
31 changes: 20 additions & 11 deletions source/retarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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<void, int, char**>(&app_start).bind(0, NULL)
);
Expand Down
2 changes: 1 addition & 1 deletion test/serial_interrupt/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down