Skip to content

Commit 7f80e45

Browse files
committed
stdio serial object - get_stdio_serial() that returns Serial&
Adding a new function that is in a global scope, returns a reference to Serial object. There's one Serial stdio object, that is initialized once (uses conf baud rate if configured). An app should use this object to configure stdio peripheral.
1 parent 8d36877 commit 7f80e45

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

hal/api/Serial.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ class Serial : public SerialBase, public Stream {
7474
PlatformMutex _mutex;
7575
};
7676

77+
/** Get the stdio Serial object, which is lazy instantiated. There's only
78+
* one stdio object that should be used within an application.
79+
*
80+
* @returns stdio object
81+
*/
82+
Serial& get_stdio_serial();
83+
7784
} // namespace mbed
7885

7986
#endif

hal/common/retarget.cpp

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "SingletonPtr.h"
2525
#include "PlatformMutex.h"
2626
#include "mbed_error.h"
27+
#include "Serial.h"
2728
#include <stdlib.h>
2829
#if DEVICE_STDIO_MESSAGES
2930
#include <stdio.h>
@@ -94,24 +95,30 @@ FileHandle::~FileHandle() {
9495
}
9596

9697
#if DEVICE_SERIAL
97-
extern int stdio_uart_inited;
98-
extern serial_t stdio_uart;
9998
#if MBED_CONF_CORE_STDIO_CONVERT_NEWLINES
10099
static char stdio_in_prev;
101100
static char stdio_out_prev;
102101
#endif
103-
#endif
104102

105-
static void init_serial() {
106-
#if DEVICE_SERIAL
107-
if (stdio_uart_inited) return;
108-
serial_init(&stdio_uart, STDIO_UART_TX, STDIO_UART_RX);
103+
namespace mbed {
104+
105+
Serial& get_stdio_serial()
106+
{
107+
static bool stdio_uart_inited = false;
108+
static Serial stdio_serial(STDIO_UART_TX, STDIO_UART_RX);
109+
if (!stdio_uart_inited) {
109110
#if MBED_CONF_CORE_STDIO_BAUD_RATE
110-
serial_baud(&stdio_uart, MBED_CONF_CORE_STDIO_BAUD_RATE);
111-
#endif
111+
stdio_serial.baud(MBED_CONF_CORE_STDIO_BAUD_RATE);
112112
#endif
113+
stdio_uart_inited = true;
114+
}
115+
return stdio_serial;
113116
}
114117

118+
} // namespace mbed
119+
120+
#endif
121+
115122
static inline int openmode_to_posix(int openmode) {
116123
int posix = openmode;
117124
#ifdef __ARMCC_VERSION
@@ -158,13 +165,13 @@ extern "C" FILEHANDLE PREFIX(_open)(const char* name, int openmode) {
158165
/* Use the posix convention that stdin,out,err are filehandles 0,1,2.
159166
*/
160167
if (std::strcmp(name, __stdin_name) == 0) {
161-
init_serial();
168+
get_stdio_serial();
162169
return 0;
163170
} else if (std::strcmp(name, __stdout_name) == 0) {
164-
init_serial();
171+
get_stdio_serial();
165172
return 1;
166173
} else if (std::strcmp(name, __stderr_name) == 0) {
167-
init_serial();
174+
get_stdio_serial();
168175
return 2;
169176
}
170177
#endif
@@ -240,18 +247,17 @@ extern "C" int PREFIX(_write)(FILEHANDLE fh, const unsigned char *buffer, unsign
240247
int n; // n is the number of bytes written
241248
if (fh < 3) {
242249
#if DEVICE_SERIAL
243-
if (!stdio_uart_inited) init_serial();
244250
#if MBED_CONF_CORE_STDIO_CONVERT_NEWLINES
245251
for (unsigned int i = 0; i < length; i++) {
246252
if (buffer[i] == '\n' && stdio_out_prev != '\r') {
247-
serial_putc(&stdio_uart, '\r');
253+
get_stdio_serial().putc('\r');
248254
}
249-
serial_putc(&stdio_uart, buffer[i]);
255+
get_stdio_serial().putc(buffer[i]);
250256
stdio_out_prev = buffer[i];
251257
}
252258
#else
253259
for (unsigned int i = 0; i < length; i++) {
254-
serial_putc(&stdio_uart, buffer[i]);
260+
get_stdio_serial().putc(buffer[i]);
255261
}
256262
#endif
257263
#endif
@@ -278,10 +284,9 @@ extern "C" int PREFIX(_read)(FILEHANDLE fh, unsigned char *buffer, unsigned int
278284
if (fh < 3) {
279285
// only read a character at a time from stdin
280286
#if DEVICE_SERIAL
281-
if (!stdio_uart_inited) init_serial();
282287
#if MBED_CONF_CORE_STDIO_CONVERT_NEWLINES
283288
while (true) {
284-
char c = serial_getc(&stdio_uart);
289+
char c = get_stdio_serial().getc();
285290
if ((c == '\r' && stdio_in_prev != '\n') ||
286291
(c == '\n' && stdio_in_prev != '\r')) {
287292
stdio_in_prev = c;
@@ -299,7 +304,7 @@ extern "C" int PREFIX(_read)(FILEHANDLE fh, unsigned char *buffer, unsigned int
299304
}
300305
}
301306
#else
302-
*buffer = serial_getc(&stdio_uart);
307+
*buffer = get_stdio_serial().getc();
303308
#endif
304309
#endif
305310
n = 1;

0 commit comments

Comments
 (0)