Skip to content

Commit a37de1c

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 a37de1c

File tree

3 files changed

+200
-2
lines changed

3 files changed

+200
-2
lines changed

platform/mbed_lib.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
"value": false
1717
},
1818

19+
"stdio-minimal-console-only": {
20+
"help": "(Applies if target.console-uart is true. 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.",
21+
"value": true
22+
},
23+
1924
"stdio-baud-rate": {
2025
"help": "(Applies if target.console-uart is true.) Baud rate for stdio",
2126
"value": 9600

platform/mbed_retarget.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ namespace mbed {
9494
class FileHandle;
9595
class DirHandle;
9696

97+
#if !(MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY)
9798
/** Targets may implement this to change stdin, stdout, stderr.
9899
*
99100
* If the application hasn't provided mbed_override_console, this is called
@@ -179,6 +180,7 @@ FileHandle *mbed_override_console(int fd);
179180
* possible if it's not open with current implementation).
180181
*/
181182
FileHandle *mbed_file_handle(int fd);
183+
#endif // !(MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY)
182184
}
183185

184186
typedef mbed::DirHandle DIR;
@@ -559,6 +561,7 @@ struct pollfd {
559561
#if __cplusplus
560562
extern "C" {
561563
#endif
564+
#if !(MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY)
562565
int open(const char *path, int oflag, ...);
563566
#ifndef __IAR_SYSTEMS_ICC__ /* IAR provides fdopen itself */
564567
#if __cplusplus
@@ -567,8 +570,10 @@ extern "C" {
567570
FILE *fdopen(int fildes, const char *mode);
568571
#endif
569572
#endif
573+
#endif // !(MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY)
570574
ssize_t write(int fildes, const void *buf, size_t nbyte);
571575
ssize_t read(int fildes, void *buf, size_t nbyte);
576+
#if !(MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY)
572577
off_t lseek(int fildes, off_t offset, int whence);
573578
int ftruncate(int fildes, off_t length);
574579
int isatty(int fildes);
@@ -586,6 +591,7 @@ extern "C" {
586591
long telldir(DIR *);
587592
void seekdir(DIR *, long);
588593
int mkdir(const char *name, mode_t n);
594+
#endif // !(MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY)
589595
#if __cplusplus
590596
}; // extern "C"
591597

@@ -619,6 +625,34 @@ std::FILE *fdopen(mbed::FileHandle *fh, const char *mode);
619625
*/
620626
int bind_to_fd(mbed::FileHandle *fh);
621627

628+
#if MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
629+
/** Targets may implement this to override how to write to the console.
630+
*
631+
* If the target hasn't provided minimal_console_write, this is called
632+
* to give the target a chance to specify a serial access to the console.
633+
*
634+
* If this is not provided, the console will be MinimalConsole
635+
*
636+
* @param buffer The buffer to write from
637+
* @param length The number of bytes to write
638+
* @return The number of bytes written
639+
*/
640+
ssize_t minimal_console_write(const void *buffer, size_t length);
641+
642+
/** Targets may implement this to override how to read from the console.
643+
*
644+
* If the target hasn't provided minimal_console_read, 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+
* @param buffer The buffer to read in
650+
* @param length The number of bytes to read
651+
* @return 0 length is zero or buffer is nullptr, 1 an attempt to read was made
652+
*/
653+
ssize_t minimal_console_read(void *buffer, size_t length);
654+
#endif // MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
655+
622656
} // namespace mbed
623657

624658
#endif // __cplusplus

0 commit comments

Comments
 (0)