An I2C device wrapped in a CoAP resource
This expreses the bus as a CBOR resource to which a [int, bytes, int] object is posted indicating the address, any to-be-written bytes and the number of bytes to be read, returning an application/octet-stream representation of the read data.
Writes and reads are performed in separate steps; this allows the CoAP library to swap out the message buffers inbetween. Thus, if the bus is multi-master or shared in the application, there may be cross-talk (eg. by another master selecting something different for reading in the meantime). Two mitigations are possible, neither currently implemented:
Use embedded_hal::blocking::i2c::WriteRead. This requires a buffer to be kept across the request and response processing.
Add a bus locking feature to embedded_hal (similar to RIOT’s bus acquisition, but not only against other local applications performing operations, but also against other bus masters taking the bus inbetween).
Note that if this handler is not run at a message deduplicating CoAP handler, bus reads and writes may happen multiple times.
- There is nothing RIOT specific about this; maybe there should be embedded-hal-coap-demos?
- There is no good error reporting yet; this should be just enough error reporting to implement an I2C scanner, but only because an empty-read-empty-write is successful if there’s a SACK on the address.