Skip to content

Add semihosting and SWO examples to mbed_override_console(). #10553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 23, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions platform/mbed_retarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,38 @@ FileHandle *mbed_target_override_console(int fd);
* by mbed_target_override_console, else will default to serial - see
* mbed_target_override_console for more details.
*
* Example:
* Example using UARTSerial:
* @code
* FileHandle* mbed::mbed_override_console(int) {
* FileHandle *mbed::mbed_override_console(int) {
* static UARTSerial my_serial(D0, D1);
* return &my_serial;
* }
* @endcode

*
* Example using SingleWireOutput:
* @code
* FileHandle *mbed::mbed_override_console(int) {
* static SerialWireOutput swo;
* return &swo;
* }
* @endcode
*
* Example using arm semihosting:
* @code
* FileHandle *mbed::mbed_override_console(int fileno) {
* static LocalFileSystem fs("host");
* if (fileno == STDIN_FILENO) {
* static FileHandle *in_terminal;
* static int in_open_result = fs.open(&in_terminal, ":tt", O_RDONLY);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That code is a bit RAM-inefficient, as it has two extra "once-only" control structures, and unused open results, but it's probably the simplest way of writing it.

The following would minimise RAM usage, but rather ends up obfuscating it.:

static LocalFileSystem fs("host");
struct TerminalStream {
    TerminalStream(FileSystemHandle &fs, int flags) {
        if (fs.open(&filehandle, ":tt", flags) != 0) {
            filehandle = NULL;
        }
    }
    FileHandle *filehandle;
}
if (fileno == STDIN_FILENO) {
    static TerminalStream in_terminal(fs, O_RDONLY):
    return in_terminal.filehandle;
} else {
    static TerminalStream out_terminal(fs, O_WRONLY);
    return out_terminal.filehandle;
}

You could leave it as-is; depends whether we want a simple example or to save 16 bytes of RAM when it's copy-pasted.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another possible refinement would be to check if the debugger is attached before attempting the open (LocalFileSystem doesn't), so it doesn't crash.

semihost_connected() should tell you that in principle, but it seems that it's currently conditional on a DEVICE_DEBUG_AWARENESS that I've never heard of and is set on basically no platforms. Not sure why - it's a core CPU feature? All M-class devices should have the bit it's trying to check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this one is so bluntly visible on the main.cpp, so maybe it would be OK to leave it as it is without the optimal memory consumption. And as you said it's a bit easier to understand.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This semihost_connected() sounds like a nice enhancement idea. Maybe this could be checked in some future commit when the DEVICE_DEBUG_AWARENESS is more common.

* return in_terminal;
* } else {
* static FileHandle *out_terminal;
* static int out_open_result = fs.open(&out_terminal, ":tt", O_WRONLY);
* return out_terminal;
* }
* }
* @endcode
*
* @param fd file descriptor - STDIN_FILENO, STDOUT_FILENO or STDERR_FILENO
* @return pointer to FileHandle to override normal stream otherwise NULL
*/
Expand Down