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
|`int lw_open_bus(lw_i2c_bus *bus, const char *path);`| Opens `/dev/i2c-X` and populates the handle. Returns `0` on success, `-1` on error (sets `errno`). |
23
+
|`int lw_open_bus(lw_i2c_bus *bus, const char *path);`| Opens `/dev/i2c-X` and populates the handle. Returns `0` on success, `-1` on error (sets `errno`) and resets the handle to a closed state on failure. |
23
24
|`void lw_close_bus(lw_i2c_bus *bus);`| Closes the file descriptor if open. Safe to call multiple times. |
24
-
|`int lw_set_slave(lw_i2c_bus *bus, uint8_t addr);`| Issues `I2C_SLAVE` ioctl to select the target address. |
25
+
|`int lw_set_slave(lw_i2c_bus *bus, uint8_t addr);`| Issues `I2C_SLAVE` ioctl to select the target address. Rejects values above `0x7F` with `EINVAL`.|
25
26
|`int lw_set_timeout(lw_i2c_bus *bus, uint32_t timeout_us);`| Stores a timeout hint (currently informational). |
26
27
27
28
### Simple Read/Write
@@ -32,7 +33,7 @@ typedef struct
32
33
|`ssize_t lw_read(lw_i2c_bus *bus, uint8_t *data, size_t len);`| Reads up to `len` bytes via `read(2)`. |
33
34
|`void lw_set_error_logging(lw_i2c_bus *bus, int enable);`| Enable (`enable != 0`) or suppress (`enable == 0`) `perror` logging for that bus handle. |
34
35
35
-
Both functions return the number of bytes processed or `-1` on error (`errno` set).
36
+
Both functions return the number of bytes processed or `-1` on error (`errno` set). Closed handles report `EBADF`; malformed arguments report `EINVAL`.
36
37
37
38
### Combined Transactions
38
39
@@ -41,7 +42,7 @@ Both functions return the number of bytes processed or `-1` on error (`errno` se
41
42
|`ssize_t lw_ioctl_read(lw_i2c_bus *bus, uint16_t addr, const uint8_t *iaddr, size_t iaddr_len, uint8_t *data, size_t len, uint16_t flags);`| Issues an `I2C_RDWR` ioctl with an optional internal register write followed by a read (repeated-start behavior). |
42
43
|`ssize_t lw_ioctl_write(lw_i2c_bus *bus, uint16_t addr, const uint8_t *iaddr, size_t iaddr_len, const uint8_t *data, size_t len, uint16_t flags);`| Builds a single write message combining an optional internal address and payload. |
43
44
44
-
The helper validates inputs (non-null buffers, length ≤ 4096, etc.) before calling into the kernel.
45
+
The helpers validate inputs (non-null buffers, length ≤ 4096, etc.) before calling into the kernel. Closed handles report `EBADF`; malformed arguments report `EINVAL`.
45
46
46
47
---
47
48
@@ -53,39 +54,43 @@ The helper validates inputs (non-null buffers, length ≤ 4096, etc.) before cal
|`void begin(const char *device = "/dev/i2c-1");`| Opens the specified I²C device. Calling `begin()` again reopens the bus. |
57
+
|`void begin(const char *device = "/dev/i2c-1");`| Opens the specified I²C device. Calling `begin()` again reopens the bus and re-applies stored timeout/logging preferences.|
57
58
|`void begin(uint8_t); void begin(int);`| Provided for Arduino compatibility; they’re no-ops in Linux master mode. |
58
59
|`void end();`| Closes the bus and clears buffers. |
59
60
|`void setClock(uint32_t frequency);`| Currently a no-op (bus speed is controlled by the kernel). |
60
-
|`void setWireTimeout(uint32_t timeout_us = 25000, bool reset_with_timeout = false);`| Stores a timeout threshold; when the underlying I/O reports `ETIMEDOUT`, `getWireTimeoutFlag()` becomes true and (optionally) the bus is reopened. |
61
+
|`void setWireTimeout(uint32_t timeout_us = 25000, bool reset_with_timeout = false);`| Stores a timeout threshold; when the underlying I/O reports `ETIMEDOUT`, `getWireTimeoutFlag()` becomes true and (optionally) the bus is reopened with the same timeout hint still applied. |
|`void setErrorLogging(bool enable);`| Toggle low-level `perror` logging (handy when probing addresses that are expected to NACK). |
63
+
|`void setErrorLogging(bool enable);`| Toggle low-level `perror` logging (handy when probing addresses that are expected to NACK). The preference survives reopen operations.|
|`void beginTransmission(uint8_t address);`| Starts buffering data for the given device. |
69
70
|`void beginTransmission(int address);`| Overload that forwards to the `uint8_t` version. |
70
-
|`uint8_t endTransmission(uint8_t sendStop = 1);`| Writes the buffered bytes. Return codes match Arduino: `0` success, `1` buffer overflow, `4` other error. Passing `0` for `sendStop` defers the actual write until the next `requestFrom` (repeated-start semantics). |
71
+
|`uint8_t endTransmission(uint8_t sendStop = 1);`| Writes the buffered bytes. Return codes match Arduino: `0` success, `1` buffer overflow, `4` other error. Passing `0` for `sendStop` defers the actual write until the next `requestFrom` (repeated-start semantics). If an older deferred write must be auto-flushed first and that flush fails, this call returns `4`. |
71
72
|`size_t write(uint8_t data);` / `size_t write(const uint8_t *data, size_t len);` / `size_t write(const char *str);`| Append data to the TX buffer (up to 32 bytes). |
|`uint8_t requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop = 1);`| Reads up to `quantity` bytes. If prior `endTransmission(false)` buffered data for the same address, it performs a combined `I2C_RDWR` transaction. |
78
+
|`uint8_t requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop = 1);`| Reads up to `quantity` bytes. If prior `endTransmission(false)` buffered data for the same address, it performs a combined `I2C_RDWR` transaction. If a deferred write must be auto-flushed first and that flush fails, the read is aborted and `0` is returned. |
78
79
|`uint8_t requestFrom(uint8_t address, uint8_t quantity, uint32_t iaddress, uint8_t isize, uint8_t sendStop);`| Arduino-style register helper; `isize` is clamped to 4. |
79
80
|`uint8_t requestFrom(int address, int quantity);` / `uint8_t requestFrom(int address, int quantity, int sendStop);`| Compatibility overloads. |
80
81
|`int available() const; int read(); int peek(); void flush();`| Buffer inspection helpers matching Arduino semantics. |
81
82
82
83
### Repeated-start semantics
83
84
84
-
-`endTransmission(false)` leaves the TX buffer intact for the next `requestFrom` to the same address. If you later target a different address or call `beginTransmission` without a read in between, linux-wire automatically flushes the pending data with a STOP so nothing is silently dropped.
85
+
-`endTransmission(false)` leaves the TX buffer intact for the next `requestFrom` to the same address.
86
+
- If you later target a different address or call `beginTransmission` without a read in between, linux-wire automatically flushes the pending data with a STOP.
87
+
- If that automatic flush fails, the follow-on operation fails instead of silently proceeding.
85
88
86
89
### Timeout behavior
87
90
88
-
- When `lw_read`/`lw_ioctl_read` reports `ETIMEDOUT`, `wireTimeoutFlag_` is set. If `reset_with_timeout` was true when `setWireTimeout` was called, the bus descriptor is closed and reopened automatically.
91
+
- When `lw_read`/`lw_ioctl_read` reports `ETIMEDOUT`, `wireTimeoutFlag_` is set.
92
+
- If `reset_with_timeout` was true when `setWireTimeout` was called, the bus descriptor is closed and reopened automatically.
93
+
- Stored timeout and error-logging preferences are re-applied after that reopen.
89
94
90
95
---
91
96
@@ -97,4 +102,4 @@ See the `examples/` directory for concrete flows:
Copy file name to clipboardExpand all lines: docs/index.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -90,12 +90,12 @@ int main() {
90
90
Important Arduino-parity notes:
91
91
92
92
-`requestFrom(address, quantity, iaddress, isize, sendStop)` exists and clamps `isize` to 4 bytes, mirroring the upstream Wire overload.
93
-
- Calling `endTransmission(false)` leaves the TX buffer intact for the next `requestFrom` to the same address. If no read follows, the buffer is flushed with a STOP before the next transmission to avoid silently dropping data.
94
-
-`setWireTimeout(timeout_us, reset_on_timeout)` sets a flag when Linux reports `ETIMEDOUT`. If `reset_on_timeout` is true, the bus handle is closed and reopened automatically (matching the AVR `twi` reset behavior).
93
+
- Calling `endTransmission(false)` leaves the TX buffer intact for the next `requestFrom` to the same address. If no read follows, the buffer is flushed with a STOP before the next operation; if that automatic flush fails, the follow-on operation fails instead of silently continuing.
94
+
-`setWireTimeout(timeout_us, reset_on_timeout)` sets a flag when Linux reports `ETIMEDOUT`. If `reset_on_timeout` is true, the bus handle is closed and reopened automatically (matching the AVR `twi` reset behavior), and stored timeout/logging preferences are re-applied.
95
95
96
96
## Testing
97
97
98
-
Software-only tests live in `tests/`. They use a mock `lw_*` backend to exercise buffer management, repeated-start logic, and timeout handling without real hardware:
98
+
Software-only tests live in `tests/`. They use a mock `lw_*` backend to exercise buffer management, repeated-start logic, deferred-write failure handling, and timeout handling without real hardware:
0 commit comments