24
24
#include " SingletonPtr.h"
25
25
#include " PlatformMutex.h"
26
26
#include " mbed_error.h"
27
+ #include " Serial.h"
27
28
#include < stdlib.h>
28
29
#if DEVICE_STDIO_MESSAGES
29
30
#include < stdio.h>
@@ -94,24 +95,30 @@ FileHandle::~FileHandle() {
94
95
}
95
96
96
97
#if DEVICE_SERIAL
97
- extern int stdio_uart_inited;
98
- extern serial_t stdio_uart;
99
98
#if MBED_CONF_CORE_STDIO_CONVERT_NEWLINES
100
99
static char stdio_in_prev;
101
100
static char stdio_out_prev;
102
101
#endif
103
- #endif
104
102
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) {
109
110
#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);
112
112
#endif
113
+ stdio_uart_inited = true ;
114
+ }
115
+ return stdio_serial;
113
116
}
114
117
118
+ } // namespace mbed
119
+
120
+ #endif
121
+
115
122
static inline int openmode_to_posix (int openmode) {
116
123
int posix = openmode;
117
124
#ifdef __ARMCC_VERSION
@@ -158,13 +165,13 @@ extern "C" FILEHANDLE PREFIX(_open)(const char* name, int openmode) {
158
165
/* Use the posix convention that stdin,out,err are filehandles 0,1,2.
159
166
*/
160
167
if (std::strcmp (name, __stdin_name) == 0 ) {
161
- init_serial ();
168
+ get_stdio_serial ();
162
169
return 0 ;
163
170
} else if (std::strcmp (name, __stdout_name) == 0 ) {
164
- init_serial ();
171
+ get_stdio_serial ();
165
172
return 1 ;
166
173
} else if (std::strcmp (name, __stderr_name) == 0 ) {
167
- init_serial ();
174
+ get_stdio_serial ();
168
175
return 2 ;
169
176
}
170
177
#endif
@@ -240,18 +247,17 @@ extern "C" int PREFIX(_write)(FILEHANDLE fh, const unsigned char *buffer, unsign
240
247
int n; // n is the number of bytes written
241
248
if (fh < 3 ) {
242
249
#if DEVICE_SERIAL
243
- if (!stdio_uart_inited) init_serial ();
244
250
#if MBED_CONF_CORE_STDIO_CONVERT_NEWLINES
245
251
for (unsigned int i = 0 ; i < length; i++) {
246
252
if (buffer[i] == ' \n ' && stdio_out_prev != ' \r ' ) {
247
- serial_putc (&stdio_uart, ' \r ' );
253
+ get_stdio_serial (). putc ( ' \r ' );
248
254
}
249
- serial_putc (&stdio_uart, buffer[i]);
255
+ get_stdio_serial (). putc ( buffer[i]);
250
256
stdio_out_prev = buffer[i];
251
257
}
252
258
#else
253
259
for (unsigned int i = 0 ; i < length; i++) {
254
- serial_putc (&stdio_uart, buffer[i]);
260
+ get_stdio_serial (). putc ( buffer[i]);
255
261
}
256
262
#endif
257
263
#endif
@@ -278,10 +284,9 @@ extern "C" int PREFIX(_read)(FILEHANDLE fh, unsigned char *buffer, unsigned int
278
284
if (fh < 3 ) {
279
285
// only read a character at a time from stdin
280
286
#if DEVICE_SERIAL
281
- if (!stdio_uart_inited) init_serial ();
282
287
#if MBED_CONF_CORE_STDIO_CONVERT_NEWLINES
283
288
while (true ) {
284
- char c = serial_getc (&stdio_uart );
289
+ char c = get_stdio_serial (). getc ( );
285
290
if ((c == ' \r ' && stdio_in_prev != ' \n ' ) ||
286
291
(c == ' \n ' && stdio_in_prev != ' \r ' )) {
287
292
stdio_in_prev = c;
@@ -299,7 +304,7 @@ extern "C" int PREFIX(_read)(FILEHANDLE fh, unsigned char *buffer, unsigned int
299
304
}
300
305
}
301
306
#else
302
- *buffer = serial_getc (&stdio_uart );
307
+ *buffer = get_stdio_serial (). getc ( );
303
308
#endif
304
309
#endif
305
310
n = 1 ;
0 commit comments