Skip to content

Commit e986995

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 e986995

File tree

6 files changed

+228
-21
lines changed

6 files changed

+228
-21
lines changed

platform/Stream.h

Lines changed: 6 additions & 4 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,22 +59,23 @@ class Stream : public FileLike, private NonCopyable<Stream> {
5859
{
5960
return _file;
6061
}
61-
62-
protected:
63-
virtual int close();
62+
#endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
6463
virtual ssize_t write(const void *buffer, size_t length);
6564
virtual ssize_t read(void *buffer, size_t length);
65+
virtual int close();
6666
virtual off_t seek(off_t offset, int whence);
6767
virtual off_t tell();
6868
virtual void rewind();
6969
virtual int isatty();
7070
virtual int sync();
7171
virtual off_t size();
7272

73+
protected:
7374
virtual int _putc(int c) = 0;
7475
virtual int _getc() = 0;
75-
76+
#if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
7677
std::FILE *_file;
78+
#endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
7779

7880
/** Acquire exclusive access to this object.
7981
*/

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: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ typedef unsigned int gid_t; ///< Group ID
8282
/** \addtogroup platform-public-api */
8383
/** @{*/
8484

85+
#if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
8586
/**
8687
* \defgroup platform_retarget Retarget functions
8788
* @{
@@ -90,7 +91,6 @@ typedef unsigned int gid_t; ///< Group ID
9091
/* DIR declarations must also be here */
9192
#if __cplusplus
9293
namespace mbed {
93-
9494
class FileHandle;
9595
class DirHandle;
9696

@@ -185,6 +185,7 @@ typedef mbed::DirHandle DIR;
185185
#else
186186
typedef struct Dir DIR;
187187
#endif
188+
#endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
188189

189190
/* The intent of this section is to unify the errno error values to match
190191
* the POSIX definitions for the GCC_ARM, ARMCC and IAR compilers. This is
@@ -559,6 +560,7 @@ struct pollfd {
559560
#if __cplusplus
560561
extern "C" {
561562
#endif
563+
#if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
562564
int open(const char *path, int oflag, ...);
563565
#ifndef __IAR_SYSTEMS_ICC__ /* IAR provides fdopen itself */
564566
#if __cplusplus
@@ -567,12 +569,14 @@ extern "C" {
567569
FILE *fdopen(int fildes, const char *mode);
568570
#endif
569571
#endif
572+
#endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
570573
ssize_t write(int fildes, const void *buf, size_t nbyte);
571574
ssize_t read(int fildes, void *buf, size_t nbyte);
575+
int fsync(int fildes);
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);
575-
int fsync(int fildes);
576580
int fstat(int fildes, struct stat *st);
577581
int fcntl(int fildes, int cmd, ...);
578582
int poll(struct pollfd fds[], nfds_t nfds, int timeout);
@@ -586,11 +590,12 @@ extern "C" {
586590
long telldir(DIR *);
587591
void seekdir(DIR *, long);
588592
int mkdir(const char *name, mode_t n);
593+
#endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
589594
#if __cplusplus
590595
}; // extern "C"
591596

592597
namespace mbed {
593-
598+
#if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
594599
/** This call is an analogue to POSIX fdopen().
595600
*
596601
* It associates a C stream to an already-opened FileHandle, to allow you to
@@ -619,6 +624,31 @@ std::FILE *fdopen(mbed::FileHandle *fh, const char *mode);
619624
*/
620625
int bind_to_fd(mbed::FileHandle *fh);
621626

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

624654
#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)