#include <hw/i2c.h>

#define DCMD_I2C_SENDRECV           __DIOTF(_DCMD_I2C, 7, i2c_sendrecv_t)

The arguments to devctl() are:

Argument Value
filedes A file descriptor that you obtained by opening the device.
dev_data_ptr A pointer to a i2c_sendrecv_t that's followed by a buffer
n_bytes sizeof(i2c_sendrecv_t) plus the size of the buffer
dev_info_ptr NULL

The DCMD_I2C_SENDRECV command executes a send followed by a receive. This sequence is typically used to read a slave device's register value. When multiple applications access the same slave device, it is necessary to execute this sequence atomically to prevent register reads from being interrupted.

  • i2c_sendrecv_t — the message header
  • uint8_t[] — a buffer, containing the send data, that's large enough to hold the receive data
  • i2c_sendrecv_t — the message header (unchanged)
  • uint8_t[] — the receive data in the buffer

The i2c_sendrecv_t structure is defined as:

typedef struct {
    i2c_addr_t slave;      /* slave address */
    uint32_t   send_len;   /* length of send data in bytes */
    uint32_t   recv_len;   /* length of receive data in bytes */
    uint32_t   stop;       /* set stop when complete? */
} i2c_sendrecv_t;

If an error occurs, the command returns:

The master send failed. Causes include: bad slave address, bad bus speed, bus is busy.
An error occurred while accessing the data buffer.
Bad message format.
Insufficient memory.
The master is locked by another connection.