Skip to content

Commit c6c3621

Browse files
author
Jaakko Korhonen
committed
Add semihosting and SWO examples to mbed_override_console().
1 parent 97e555d commit c6c3621

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

docs/api/platform/FileHandle.md

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,15 @@ If a target has serial support, by default a serial port is used for the console
6969
The target can override this by providing `mbed::mbed_target_override_console` to specify an alternative `FileHandle`. For example, a target using SWO might have:
7070

7171
```
72-
namespace mbed
72+
namespace mbed
73+
{
74+
FileHandle *mbed_target_override_console(int)
7375
{
74-
FileHandle *mbed_target_override_console(int)
75-
{
76-
static SerialWireOutput swo;
77-
return &swo;
78-
}
76+
// SerialWireOutput
77+
static SerialWireOutput swo;
78+
return &swo;
7979
}
80+
}
8081
```
8182

8283
Then any program using `printf` on that target sends its output over the SWO, rather than serial.
@@ -87,12 +88,44 @@ Because targets can redirect the console in this way, portable applications shou
8788
// Don't do:
8889
Serial serial(USBTX, USBRX);
8990
serial.printf("Hello!\r\n");
90-
91+
9192
// Do do:
9293
printf("Hello!\n"); // assume platform.stdio-convert-newlines is true
9394
```
9495

95-
Beyond the target-specific override, an application can override the target's default behavior itself by providing `mbed::mbed_override_console`.
96+
Beyond the target-specific override, an application can override the target's default behavior itself by providing `mbed::mbed_override_console`. Below are two examples that show how the console can be redirected to a debugger using semihosting or through another application specific serial port:
97+
98+
```
99+
namespace mbed
100+
{
101+
FileHandle *mbed_override_console(int fileno)
102+
{
103+
// Semihosting allows "virtual" console access through a debugger.
104+
static LocalFileSystem fs("host");
105+
if (fileno == STDIN_FILENO) {
106+
static FileHandle *in_terminal;
107+
static int in_open_result = fs.open(&in_terminal, ":tt", O_RDONLY);
108+
return in_terminal;
109+
} else {
110+
static FileHandle *out_terminal;
111+
static int out_open_result = fs.open(&out_terminal, ":tt", O_WRONLY);
112+
return out_terminal;
113+
}
114+
}
115+
}
116+
```
117+
Application can redirect the console to a different serial port in case the default one is needed for another use:
118+
119+
```
120+
namespace
121+
{
122+
FileHandle *mbed_override_console(int)
123+
{
124+
static UARTSerial uart(PA_0, PA_1);
125+
return &uart;
126+
}
127+
}
128+
```
96129

97130
Alternatively, an application could use the standard C `freopen` function to redirect `stdout` to a named file or device while running. However there is no `fdreopen` analogue to redirect to an unnamed device by file descriptor or `FileHandle` pointer.
98131

0 commit comments

Comments
 (0)