Skip to content

Commit 3bbd53d

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 3bbd53d

File tree

6 files changed

+223
-17
lines changed

6 files changed

+223
-17
lines changed

platform/Stream.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class Stream : public FileLike, private NonCopyable<Stream> {
4545
Stream(const char *name = NULL);
4646
virtual ~Stream();
4747

48+
#if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
4849
int putc(int c);
4950
int puts(const char *s);
5051
int getc();
@@ -58,11 +59,12 @@ class Stream : public FileLike, private NonCopyable<Stream> {
5859
{
5960
return _file;
6061
}
62+
#endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
63+
virtual ssize_t write(const void *buffer, size_t length);
64+
virtual ssize_t read(void *buffer, size_t length);
6165

6266
protected:
6367
virtual int close();
64-
virtual ssize_t write(const void *buffer, size_t length);
65-
virtual ssize_t read(void *buffer, size_t length);
6668
virtual off_t seek(off_t offset, int whence);
6769
virtual off_t tell();
6870
virtual void rewind();
@@ -72,8 +74,9 @@ class Stream : public FileLike, private NonCopyable<Stream> {
7274

7375
virtual int _putc(int c) = 0;
7476
virtual int _getc() = 0;
75-
77+
#if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
7678
std::FILE *_file;
79+
#endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
7780

7881
/** Acquire exclusive access to this object.
7982
*/

platform/cxxsupport/mstd_mutex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ private:
373373
template <class Callable, class... Args>
374374
void call_once(once_flag &flag, Callable&& f, Args&&... args)
375375
{
376-
if (!(core_util_atomic_load_explicit(&flag.__guard, mbed_memory_order_acquire) & 1)) {
376+
if (!(core_util_atomic_load_explicit((uint8_t *)&flag.__guard, mbed_memory_order_acquire) & 1)) {
377377
if (__cxa_guard_acquire(&flag.__guard)) {
378378
mstd::invoke(mstd::forward<Callable>(f), mstd::forward<Args>(args)...);
379379
__cxa_guard_release(&flag.__guard);

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: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,12 @@ 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
99+
97100
/** Targets may implement this to change stdin, stdout, stderr.
98101
*
99102
* If the application hasn't provided mbed_override_console, this is called
@@ -179,6 +182,7 @@ FileHandle *mbed_override_console(int fd);
179182
* possible if it's not open with current implementation).
180183
*/
181184
FileHandle *mbed_file_handle(int fd);
185+
#endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
182186
}
183187

184188
typedef mbed::DirHandle DIR;
@@ -559,6 +563,7 @@ struct pollfd {
559563
#if __cplusplus
560564
extern "C" {
561565
#endif
566+
#if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
562567
int open(const char *path, int oflag, ...);
563568
#ifndef __IAR_SYSTEMS_ICC__ /* IAR provides fdopen itself */
564569
#if __cplusplus
@@ -567,12 +572,14 @@ extern "C" {
567572
FILE *fdopen(int fildes, const char *mode);
568573
#endif
569574
#endif
575+
#endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
570576
ssize_t write(int fildes, const void *buf, size_t nbyte);
571577
ssize_t read(int fildes, void *buf, size_t nbyte);
578+
int fsync(int fildes);
579+
#if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
572580
off_t lseek(int fildes, off_t offset, int whence);
573581
int ftruncate(int fildes, off_t length);
574582
int isatty(int fildes);
575-
int fsync(int fildes);
576583
int fstat(int fildes, struct stat *st);
577584
int fcntl(int fildes, int cmd, ...);
578585
int poll(struct pollfd fds[], nfds_t nfds, int timeout);
@@ -586,6 +593,7 @@ extern "C" {
586593
long telldir(DIR *);
587594
void seekdir(DIR *, long);
588595
int mkdir(const char *name, mode_t n);
596+
#endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
589597
#if __cplusplus
590598
}; // extern "C"
591599

@@ -619,6 +627,31 @@ std::FILE *fdopen(mbed::FileHandle *fh, const char *mode);
619627
*/
620628
int bind_to_fd(mbed::FileHandle *fh);
621629

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

624657
#endif // __cplusplus

platform/source/Stream.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,14 @@
2020

2121
namespace mbed {
2222

23-
Stream::Stream(const char *name) : FileLike(name), _file(NULL)
23+
Stream::Stream(const char *name) :
24+
FileLike(name)
25+
#if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
26+
, _file(NULL)
27+
#endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
2428
{
2529
// No lock needed in constructor
30+
#if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
2631
/* open ourselves */
2732
_file = fdopen(this, "w+");
2833
// fdopen() will make us buffered because Stream::isatty()
@@ -33,14 +38,18 @@ Stream::Stream(const char *name) : FileLike(name), _file(NULL)
3338
} else {
3439
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_OPEN_FAILED), "Stream obj failure", _file);
3540
}
41+
#endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
3642
}
3743

3844
Stream::~Stream()
3945
{
46+
#if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
4047
// No lock can be used in destructor
4148
fclose(_file);
49+
#endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
4250
}
4351

52+
#if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
4453
int Stream::putc(int c)
4554
{
4655
lock();
@@ -73,6 +82,7 @@ char *Stream::gets(char *s, int size)
7382
unlock();
7483
return ret;
7584
}
85+
#endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
7686

7787
int Stream::close()
7888
{
@@ -142,6 +152,8 @@ off_t Stream::size()
142152
return 0;
143153
}
144154

155+
#if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
156+
145157
int Stream::printf(const char *format, ...)
146158
{
147159
lock();
@@ -184,4 +196,6 @@ int Stream::vscanf(const char *format, std::va_list args)
184196
return r;
185197
}
186198

199+
#endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
200+
187201
} // namespace mbed

0 commit comments

Comments
 (0)