@@ -215,14 +215,29 @@ short DirectSerial::poll(short events) const
215215 return revents;
216216}
217217#else
218+ /* * Class providing minimal UART communication functionalities
219+ */
218220class MinimalSerial {
219221public:
220222 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 );
223+ virtual int putc ( int c );
224+ virtual int getc ( );
223225};
224226
225- MinimalSerial::MinimalSerial (PinName tx, PinName rx, int baud)
227+ /* * Create a MinimalSerial port, connected to the specified transmit and receive pins, with the specified baud.
228+ *
229+ * @param tx Transmit pin
230+ * @param rx Receive pin
231+ * @param baud The baud rate of the serial port (optional, defaults to MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE)
232+ *
233+ * @note
234+ * Either tx or rx may be specified as NC if unused
235+ */
236+ MinimalSerial::MinimalSerial (
237+ PinName tx,
238+ PinName rx,
239+ int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE
240+ )
226241{
227242 if (stdio_uart_inited) {
228243 return ;
@@ -239,25 +254,28 @@ MinimalSerial::MinimalSerial(PinName tx, PinName rx, int baud)
239254#endif
240255}
241256
242- ssize_t MinimalSerial::write (const void *buffer, size_t size)
257+ /* * Write a char to the serial port
258+ *
259+ * @param c The char to write
260+ *
261+ * @returns The written char
262+ */
263+ int MinimalSerial::putc (int c)
243264{
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;
265+ serial_putc (&stdio_uart, c);
266+ return c;
249267}
250268
251- ssize_t MinimalSerial::read (void *buffer, size_t size)
269+ /* * Read a char to the serial port
270+ *
271+ * @returns The char read from the serial port
272+ */
273+ int MinimalSerial::getc ()
252274{
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 ;
275+ return serial_getc (&stdio_uart);
259276}
260277
278+
261279/* Locate the default console */
262280static MinimalSerial *get_minimal_console ()
263281{
@@ -793,7 +811,13 @@ MBED_WEAK ssize_t minimal_console_write(const void *buffer, size_t length)
793811 return -1 ;
794812 }
795813
796- return mc->write (buffer, length);
814+ const unsigned char *buf = static_cast <const unsigned char *>(buffer);
815+
816+ for (size_t i = 0 ; i < length; i++) {
817+ mc->putc (buf[i]);
818+ }
819+
820+ return length;
797821}
798822#endif // MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
799823
@@ -906,13 +930,19 @@ extern "C" ssize_t read(int fildes, void *buf, size_t length)
906930/* Read the serial interface and store the content to a buffer */
907931MBED_WEAK ssize_t minimal_console_read (void *buffer, size_t length)
908932{
933+ if (length == 0 ) {
934+ return 0 ;
935+ }
936+
909937 MinimalSerial *mc = get_minimal_console ();
910938 if (mc == nullptr ) {
911939 errno = EBADF;
912940 return -1 ;
913941 }
914942
915- return mc->read (buffer, length);
943+ unsigned char *buf = static_cast <unsigned char *>(buffer);
944+ buf[0 ] = mc->getc ();
945+ return 1 ;
916946}
917947#endif // MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
918948
0 commit comments