Skip to content

Commit 564ae22

Browse files
committed
mbed_retarget: Replace write/read methods of MinimalSerial with putc/getc methods
1 parent 47de453 commit 564ae22

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

platform/source/mbed_retarget.cpp

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,8 @@ short DirectSerial::poll(short events) const
218218
class MinimalSerial {
219219
public:
220220
MinimalSerial(PinName tx, PinName rx, int baud);
221-
virtual ssize_t write(const void *buffer, size_t size);
222-
virtual ssize_t read(void *buffer, size_t size);
221+
virtual int putc(int c);
222+
virtual int getc();
223223
};
224224

225225
MinimalSerial::MinimalSerial(PinName tx, PinName rx, int baud)
@@ -239,25 +239,18 @@ MinimalSerial::MinimalSerial(PinName tx, PinName rx, int baud)
239239
#endif
240240
}
241241

242-
ssize_t MinimalSerial::write(const void *buffer, size_t size)
242+
int MinimalSerial::putc(int c)
243243
{
244-
const unsigned char *buf = static_cast<const unsigned char *>(buffer);
245-
for (size_t i = 0; i < size; i++) {
246-
serial_putc(&stdio_uart, buf[i]);
247-
}
248-
return size;
244+
serial_putc(&stdio_uart, c);
245+
return c;
249246
}
250247

251-
ssize_t MinimalSerial::read(void *buffer, size_t size)
248+
int MinimalSerial::getc()
252249
{
253-
unsigned char *buf = static_cast<unsigned char *>(buffer);
254-
if (size == 0) {
255-
return 0;
256-
}
257-
buf[0] = serial_getc(&stdio_uart);
258-
return 1;
250+
return serial_getc(&stdio_uart);
259251
}
260252

253+
261254
/* Locate the default console */
262255
static MinimalSerial *get_minimal_console()
263256
{
@@ -793,7 +786,13 @@ MBED_WEAK ssize_t minimal_console_write(const void *buffer, size_t length)
793786
return -1;
794787
}
795788

796-
return mc->write(buffer, length);
789+
const unsigned char *buf = static_cast<const unsigned char *>(buffer);
790+
791+
for (size_t i = 0; i < length; i++) {
792+
mc->putc(buf[i]);
793+
}
794+
795+
return length;
797796
}
798797
#endif // MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
799798

@@ -906,13 +905,19 @@ extern "C" ssize_t read(int fildes, void *buf, size_t length)
906905
/* Read the serial interface and store the content to a buffer */
907906
MBED_WEAK ssize_t minimal_console_read(void *buffer, size_t length)
908907
{
908+
if (length == 0) {
909+
return 0;
910+
}
911+
909912
MinimalSerial *mc = get_minimal_console();
910913
if (mc == nullptr) {
911914
errno = EBADF;
912915
return -1;
913916
}
914917

915-
return mc->read(buffer, length);
918+
unsigned char *buf = static_cast<unsigned char *>(buffer);
919+
buf[0] = mc->getc();
920+
return 1;
916921
}
917922
#endif // MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
918923

0 commit comments

Comments
 (0)