@@ -102,16 +102,29 @@ FileHandle::~FileHandle() {
102
102
}
103
103
104
104
#if DEVICE_SERIAL
105
+
106
+ #include " mbed-drivers/Serial.h"
107
+
105
108
static int stdio_uart_inited;
106
- static serial_t stdio_uart;
109
+
110
+ namespace mbed {
111
+
112
+ Serial& get_stdio_serial ()
113
+ {
114
+ static Serial stdio_serial (STDIO_UART_TX, STDIO_UART_RX);
115
+ if (stdio_uart_inited == 0 ) {
116
+ stdio_serial.baud (STDIO_DEFAULT_BAUD);
117
+ stdio_uart_inited = 1 ;
118
+ }
119
+ return stdio_serial;
120
+ }
121
+
122
+ }
107
123
#endif
108
124
109
125
static void init_serial () {
110
126
#if DEVICE_SERIAL
111
- if (stdio_uart_inited) return ;
112
- serial_init (&stdio_uart, STDIO_UART_TX, STDIO_UART_RX);
113
- serial_baud (&stdio_uart, STDIO_DEFAULT_BAUD);
114
- stdio_uart_inited = 1 ;
127
+ get_stdio_serial ();
115
128
#endif
116
129
}
117
130
@@ -243,9 +256,8 @@ extern "C" int PREFIX(_write)(FILEHANDLE fh, const unsigned char *buffer, unsign
243
256
int n; // n is the number of bytes written
244
257
if (fh < 3 ) {
245
258
#if DEVICE_SERIAL
246
- if (!stdio_uart_inited) init_serial ();
247
259
for (unsigned int i = 0 ; i < length; i++) {
248
- serial_putc (&stdio_uart, buffer[i]);
260
+ get_stdio_serial (). putc ( buffer[i]);
249
261
}
250
262
#endif
251
263
n = length;
@@ -281,8 +293,7 @@ extern "C" int PREFIX(_read)(FILEHANDLE fh, unsigned char *buffer, unsigned int
281
293
if (fh < 3 ) {
282
294
// only read a character at a time from stdin
283
295
#if DEVICE_SERIAL
284
- if (!stdio_uart_inited) init_serial ();
285
- *buffer = serial_getc (&stdio_uart);
296
+ *buffer = get_stdio_serial ().getc ();
286
297
#endif
287
298
n = 1 ;
288
299
} else {
@@ -493,8 +504,6 @@ extern "C" void __iar_argc_argv() {
493
504
// the user should set up their application in app_start
494
505
extern void app_start (int , char **);
495
506
extern " C" int main (void ) {
496
- // init serial if it has not been invoked prior main
497
- init_serial ();
498
507
minar::Scheduler::postCallback (
499
508
mbed::util::FunctionPointer2<void , int , char **>(&app_start).bind (0 , NULL )
500
509
);
0 commit comments