Skip to content

Commit 8eec656

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 8eec656

File tree

3 files changed

+209
-6
lines changed

3 files changed

+209
-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: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ typedef unsigned int gid_t; ///< Group ID
9191
#if __cplusplus
9292
namespace mbed {
9393

94+
9495
class FileHandle;
96+
97+
#if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
98+
9599
class DirHandle;
96100

97101
/** Targets may implement this to change stdin, stdout, stderr.
@@ -179,6 +183,7 @@ FileHandle *mbed_override_console(int fd);
179183
* possible if it's not open with current implementation).
180184
*/
181185
FileHandle *mbed_file_handle(int fd);
186+
#endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
182187
}
183188

184189
typedef mbed::DirHandle DIR;
@@ -559,6 +564,7 @@ struct pollfd {
559564
#if __cplusplus
560565
extern "C" {
561566
#endif
567+
#if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
562568
int open(const char *path, int oflag, ...);
563569
#ifndef __IAR_SYSTEMS_ICC__ /* IAR provides fdopen itself */
564570
#if __cplusplus
@@ -567,12 +573,14 @@ extern "C" {
567573
FILE *fdopen(int fildes, const char *mode);
568574
#endif
569575
#endif
576+
#endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
570577
ssize_t write(int fildes, const void *buf, size_t nbyte);
571578
ssize_t read(int fildes, void *buf, size_t nbyte);
579+
int fsync(int fildes);
580+
#if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
572581
off_t lseek(int fildes, off_t offset, int whence);
573582
int ftruncate(int fildes, off_t length);
574583
int isatty(int fildes);
575-
int fsync(int fildes);
576584
int fstat(int fildes, struct stat *st);
577585
int fcntl(int fildes, int cmd, ...);
578586
int poll(struct pollfd fds[], nfds_t nfds, int timeout);
@@ -586,6 +594,7 @@ extern "C" {
586594
long telldir(DIR *);
587595
void seekdir(DIR *, long);
588596
int mkdir(const char *name, mode_t n);
597+
#endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
589598
#if __cplusplus
590599
}; // extern "C"
591600

@@ -619,6 +628,31 @@ std::FILE *fdopen(mbed::FileHandle *fh, const char *mode);
619628
*/
620629
int bind_to_fd(mbed::FileHandle *fh);
621630

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

624658
#endif // __cplusplus

0 commit comments

Comments
 (0)