You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/api/platform/FileHandle.md
+41-8Lines changed: 41 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -69,14 +69,15 @@ If a target has serial support, by default a serial port is used for the console
69
69
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:
70
70
71
71
```
72
-
namespace mbed
72
+
namespace mbed
73
+
{
74
+
FileHandle *mbed_target_override_console(int)
73
75
{
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;
79
79
}
80
+
}
80
81
```
81
82
82
83
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
87
88
// Don't do:
88
89
Serial serial(USBTX, USBRX);
89
90
serial.printf("Hello!\r\n");
90
-
91
+
91
92
// Do do:
92
93
printf("Hello!\n"); // assume platform.stdio-convert-newlines is true
93
94
```
94
95
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
+
```
96
129
97
130
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.
0 commit comments