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
+46-11Lines changed: 46 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -69,19 +69,20 @@ 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.
83
84
84
-
Because targets can redirect the console in this way, portable applications should not use constructs like`Serial(USBTX, USBRX)`, assuming that this will access the console. Instead they should use `stdin`/`stdout`/`stderr` or `STDIN_FILENO`/`STDOUT_FILENO`/`STDERR_FILENO`.
85
+
Because targets can redirect the console in this way, portable applications should not use constructs such as`Serial(USBTX, USBRX)`, assuming that this will access the console. Instead they should use `stdin`/`stdout`/`stderr` or `STDIN_FILENO`/`STDOUT_FILENO`/`STDERR_FILENO`.
85
86
86
87
```
87
88
// Don't do:
@@ -92,13 +93,47 @@ Because targets can redirect the console in this way, portable applications shou
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 you can redirect the console to a debugger using semihosting or 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
+
118
+
The application can redirect the console to a different serial port if you need the default port for another use:
119
+
120
+
```
121
+
namespace
122
+
{
123
+
FileHandle *mbed_override_console(int)
124
+
{
125
+
static UARTSerial uart(PA_0, PA_1);
126
+
return &uart;
127
+
}
128
+
}
129
+
```
130
+
131
+
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` analog to redirect to an unnamed device by file descriptor or `FileHandle` pointer.
96
132
97
-
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.
133
+
### Polling and nonblocking
98
134
99
-
## Polling and nonblocking
135
+
By default, `FileHandle`s conventionally block until a `read` or `write` operation completes. This is the only behavior supported by normal `File`s, and is expected by the C library's `stdio` functions.
100
136
101
-
By default `FileHandle`s conventionally block until a `read` or `write` operation completes. This is the only behavior supported by normal `File`s, and is expected by the C library's `stdio` functions.
102
137
103
138
Device-type `FileHandle`s, such as `UARTSerial`, are expected to also support nonblocking operation, which permits the `read` and `write` calls to return immediately when unable to transfer data. Please see the API reference pages of these functions for more information.
0 commit comments