Skip to content

Commit a7d2612

Browse files
author
Amanda Butler
authored
Add code snippets to FileHandle.md
Add semihosting and SWO examples to mbed_override_console() by applying changes from PR #1063
1 parent 7151df4 commit a7d2612

File tree

1 file changed

+46
-11
lines changed

1 file changed

+46
-11
lines changed

docs/api/platform/FileHandle.md

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,20 @@ 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.
8384

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`.
8586

8687
```
8788
// Don't do:
@@ -92,13 +93,47 @@ Because targets can redirect the console in this way, portable applications shou
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 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.
96132

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
98134

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.
100136

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.
102137

103138
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.
104139

0 commit comments

Comments
 (0)