Skip to content

Commit af99484

Browse files
committed
mbed_retarget: Add an internal class to provide basic console functionality
The retarget code allocates an array of FileHandle* for console and file handling (filehandles). A tiny target only needs a console (putc/getc). There is no need for file handling. The POSIX layer and the array of FileHandle* is not required for small targets that only need a console ; this code is optionally compiled out if the configuration parameter platform.stdio-minimal-console-only is set to `"true"`.
1 parent 734072f commit af99484

File tree

3 files changed

+207
-6
lines changed

3 files changed

+207
-6
lines changed

platform/mbed_lib.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@
1212
},
1313

1414
"stdio-buffered-serial": {
15-
"help": "(Applies if target.console-uart is true.) Use UARTSerial driver to obtain buffered serial I/O on stdin/stdout/stderr. If false, unbuffered serial_getc and serial_putc are used directly.",
15+
"help": "(Applies if target.console-uart is true and stdio-minimal-console-only is false.) Use UARTSerial driver to obtain buffered serial I/O on stdin/stdout/stderr. If false, unbuffered serial_getc and serial_putc are used directly.",
16+
"value": false
17+
},
18+
19+
"stdio-minimal-console-only": {
20+
"help": "(Ignores stdio-buffered-serial) Creates a console for basic unbuffered I/O operations. Enable if your application does not require file handles to access the serial interface. The POSIX `fsync` function will always an error.",
1621
"value": false
1722
},
1823

platform/mbed_retarget.h

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,11 @@ typedef unsigned int gid_t; ///< Group ID
9191
#if __cplusplus
9292
namespace mbed {
9393

94+
9495
class FileHandle;
9596
class DirHandle;
9697

98+
#if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
9799
/** Targets may implement this to change stdin, stdout, stderr.
98100
*
99101
* If the application hasn't provided mbed_override_console, this is called
@@ -179,6 +181,7 @@ FileHandle *mbed_override_console(int fd);
179181
* possible if it's not open with current implementation).
180182
*/
181183
FileHandle *mbed_file_handle(int fd);
184+
#endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
182185
}
183186

184187
typedef mbed::DirHandle DIR;
@@ -559,6 +562,7 @@ struct pollfd {
559562
#if __cplusplus
560563
extern "C" {
561564
#endif
565+
#if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
562566
int open(const char *path, int oflag, ...);
563567
#ifndef __IAR_SYSTEMS_ICC__ /* IAR provides fdopen itself */
564568
#if __cplusplus
@@ -567,12 +571,14 @@ extern "C" {
567571
FILE *fdopen(int fildes, const char *mode);
568572
#endif
569573
#endif
574+
#endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
570575
ssize_t write(int fildes, const void *buf, size_t nbyte);
571576
ssize_t read(int fildes, void *buf, size_t nbyte);
577+
int fsync(int fildes);
578+
#if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
572579
off_t lseek(int fildes, off_t offset, int whence);
573580
int ftruncate(int fildes, off_t length);
574581
int isatty(int fildes);
575-
int fsync(int fildes);
576582
int fstat(int fildes, struct stat *st);
577583
int fcntl(int fildes, int cmd, ...);
578584
int poll(struct pollfd fds[], nfds_t nfds, int timeout);
@@ -586,6 +592,7 @@ extern "C" {
586592
long telldir(DIR *);
587593
void seekdir(DIR *, long);
588594
int mkdir(const char *name, mode_t n);
595+
#endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
589596
#if __cplusplus
590597
}; // extern "C"
591598

@@ -619,6 +626,31 @@ std::FILE *fdopen(mbed::FileHandle *fh, const char *mode);
619626
*/
620627
int bind_to_fd(mbed::FileHandle *fh);
621628

629+
#if MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
630+
/** Targets may implement this to override how to write to the console.
631+
*
632+
* If the target hasn't provided minimal_console_putc, this is called
633+
* to give the target a chance to specify a serial access to the console.
634+
*
635+
* If this is not provided, the console will be MinimalConsole.
636+
*
637+
* @param c The char to write
638+
* @return The written char
639+
*/
640+
int minimal_console_putc(int c);
641+
642+
/** Targets may implement this to override how to read from the console.
643+
*
644+
* If the target hasn't provided minimal_console_getc, this is called
645+
* to give the target a chance to specify a serial access to the console.
646+
*
647+
* If this is not provided, the console will be MinimalConsole.
648+
*
649+
* @return The char read from the serial port
650+
*/
651+
int minimal_console_getc();
652+
#endif // MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
653+
622654
} // namespace mbed
623655

624656
#endif // __cplusplus

0 commit comments

Comments
 (0)