Skip to content

Commit cb32e73

Browse files
committed
FileHandle: Mbed OS 5.12 updates
Cover changes in ARMmbed/mbed-os#9797
1 parent 5903617 commit cb32e73

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

docs/api/platform/FileHandle.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Blocking I/O | Yes (always) | Yes (default)
3434
Nonblocking I/O | No | Yes | Yes
3535
Poll | No | Yes (struct pollfd) | Yes (struct pollfh)
3636
Sigio | No | No | Yes
37+
Disable input or output | No | No | Yes
3738
Device-specific extensions | No | No | Possible using derived types
3839
Newline conversion | Yes (enabled with JSON) | No | No
3940
Error indications | EOF, ferror, set errno | Return -1, set errno | Return negative error code
@@ -51,9 +52,13 @@ Calls are provided to attach already-opened lower levels to the higher levels:
5152
- `FILE *fdopen(int fd, const char *mode)` bind a POSIX file descriptor to a stdio FILE.
5253
- `FILE *fdopen(FileHandle *fh, const char *mode)` bind a FileHandle to a stdio FILE.
5354

55+
The only call provided to map from higher level to lower-level is:
56+
57+
- `FileHandle *mbed_file_handle(int fd)` obtain the FileHandle for a POSIX file descriptor
58+
5459
The standard POSIX function `int fileno(FILE *stream)` may be available to map from `FILE` to file descriptor, depending on the toolchain and C library in use - it is not usable in fully portable Mbed OS code.
5560

56-
As it is not possible to map from higher levels to lower levels, if code needs to access the lower levels, use a lower-level open call, so the lower-level handle is known. Then, bind that to the higher level..
61+
As it is not possible to map from `FILE` to lower levels, if code needs to access the lower levels, rather than use `fopen`, use a lower-level open call then `fdopen` to create the `FILE`.
5762

5863
The POSIX file descriptors for the console are available as `STDIN_FILENO`, `STDOUT_FILENO` and `STDERR_FILENO`, permitting operations such as `fsync(STDERR_FILENO)`, which would for example drain `UARTSerial`s output buffer.
5964

@@ -112,6 +117,14 @@ Important notes on sigio:
112117

113118
Ordinary files do not generate sigio callbacks because they are always readable and writable.
114119

120+
#### Suspending a device
121+
122+
Having a device open via a `FileHandle` may cost power, especially if open for input. For example, for `UARTSerial` to be able to receive data, the system must not enter deep sleep, so deep sleep will be prevented while the `UARTSerial` is active.
123+
124+
To permit power saving, the device can be closed or destroyed, or you can indicate that you do not currently require input or output by calling `FileHandle::enable_input` or `FileHandle::enable_output`. Disabling input or output effectively suspends the device in that direction, which can permit power saving.
125+
126+
This is particularly useful when an application does not require console input - it can indicate this by calling `mbed_file_handle(STDIN_FILENO)->enable_input(false)` once at the start of the program. This will permit deep sleep when `platform.stdio-buffered-serial` is set to true.
127+
115128
#### Stream-derived FileHandles
116129

117130
`Stream` is a legacy class that provides an abstract interface for streams similar to the `FileHandle` class. The difference is that the `Stream` API is built around the `getc` and `putc` set of functions, whereas `FileHandle` is built around `read` and `write`. This makes implementations simpler but limits what is possible with the API. Because of this, implementing the `FileHandle` API directly is suggested API for new device drivers.
@@ -214,3 +227,4 @@ int main()
214227
- [File](file.html).
215228
- [FileSystem](filesystem.html).
216229
- [Poll](poll.html).
230+
- [Power management](powermanagement.html).

docs/api/platform/PowerManagement.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,15 @@ These Mbed OS drivers can lock the deep sleep:
3838
- `SPI`.
3939
- `I2C`.
4040
- `CAN`.
41-
- `SerialBase`.
41+
- `SerialBase` (and hence `Serial` and `UARTSerial`)
42+
43+
#### Console versus deep sleep
44+
45+
By default, on entry to `main`, the deep sleep lock will not be held, so deep sleep will be possible until a driver or other code locks it.
46+
47+
However, if `platform.stdio-buffered-serial` is set to true, then `UARTSerial` installs an interrupt handler to receive serial data for `stdin`. This will block deep sleep. To permit deep sleep, input must be suspended (permanently or temporarily). Making the call `mbed_file_handle(STDIN_FILENO)->enable_input(false)` from the application gives the console driver, whatever it is, permission to stop reception. If `UARTSerial` is providing `stdin`, this removes the receive interrupt handler and releases the deep sleep lock.
48+
49+
For more information see [`FileHandle`](filehandle.html).
4250

4351
#### Sleep/Deep sleep profiling tool
4452

0 commit comments

Comments
 (0)