Uint8_t david_digikey_coffee_cup_zephyr_i2c_read_autoincrement(slave_address, address)

In our previous post we defined how to read single bytes in the I2C interface from the Raspberry Pico 2 using Zephyr RTOS. There are some situations where there is need to perform autoincrement (burst) read operations on the I2C bus from a device. Here is the modification needed to create a autoincrement (burst) read via the I2C interface.


#define CONFIG_I2C_RTIO_LOOPBACK_DATA_WRITE_MAX_SIZE 1
#define CONFIG_I2C_RTIO_LOOPBACK_DATA_READ_MAX_SIZE 14
#define I2C_TARGET_ADDR 0x69

static const struct device *scan_dev = DEVICE_DT_GET(DT_ALIAS(scan_i2c));

static uint8_t sample_write_data[CONFIG_I2C_RTIO_LOOPBACK_DATA_WRITE_MAX_SIZE];

static uint8_t sample_read_data[CONFIG_I2C_RTIO_LOOPBACK_DATA_READ_MAX_SIZE];

static uint8_t sample_read_buf[sizeof(sample_read_data)];

uint8_t david_digikey_coffee_cup_zephyr_i2c_read_autoinc(uint8_t slave_address, uint8_t address)
{
 
    sample_write_data[0] = address;
    sample_read_buf[0] = 0x00;
    struct i2c_msg msgs[2];
    msgs[0].buf = sample_write_data;
    msgs[0].len = sizeof(sample_write_data);
    msgs[0].flags = I2C_MSG_WRITE;
    msgs[1].buf = sample_read_buf;
    msgs[1].len = sizeof(sample_read_buf);
    msgs[1].flags = I2C_MSG_RESTART | I2C_MSG_READ | I2C_MSG_STOP;

    int ret = i2c_transfer(scan_dev, msgs, ARRAY_SIZE(msgs), slave_address);
    if (ret) {
            return -EIO;
    }
    
    return ret;
    
}

In this case the CONFIG_I2C_RTIO_LOOPBACK_DATA_READ_MAX_SIZE was changed to 14 bytes which is needed for our demo here as shown above. (6 bytes for MEMS accelerometer data, 6 bytes for MEMS gyroscope data and 2 bytes for the internal temperature sensor.)

The TDK ICM-20948 Sensor Qwiic Platform Evaluation Expansion Board from Sparkfun is the sensor used for this demo. Using the demo setup described here.

The Sparkfun 8-channel USB Logic Analyzer previously described in this Techforum article was setup as shown in the next picture using these Qwiic cables to monitor the I2C bus using the proper voltage source, PIN6 (SDA), PIN7 (SCL) (I2C Port 0) and PIN 38 (GND) on the Raspberry Pico 2 to the TDK ICM-20948 Sensor Qwiic Platform Evaluation Expansion Board from Sparkfun. The next Sparkfun 8-channel USB Logic Analyzer snapshot confirms the proper I2C interface operation of the autoincrement (burst) read function of 14 bytes from address 0x2D in the sensor slave address 0x69 from Zephyr OS in the Raspberry Pico 2

The i2c write function can also be modified similarly for write autoincrement (burst) mode to the I2C slave device as needed by changing the number of bytes that are written in the bus as shown previously. Have a nice day!

This article is also available in spanish here.

Este artículo está disponible en español aquí.

1 Like