I2C single byte write

sb_espressif
Posts: 28
Joined: Fri Dec 08, 2023 3:04 am

I2C single byte write

Postby sb_espressif » Fri Dec 08, 2023 3:12 am

Hi;

I'm fairly new to embedded systems, and am trying to write a driver for a breakout board I made for a tlv493d magnetic encoder. Specifically, on power on, I need to send a single reset byte to the encoder (0xFF). I got this working using i2c_master_write_byte(), but am interested in generalizing my code a bit, and I see there's a wrapper function called i2c_master_write_to_device() in 5.1.2 that seems like it might help me tidy up my code. However, I can't seem to figure out how to write a single byte using that function. Is it designed to only write more than 1 bytes? So I need to stick with the i2c_master_write_byte() function for this?

Here's a code snippet:

Code: Select all

    // 1) Send recovery frame to sensor

#define TLV493D_I2C_ADDR     (0x5E << 1)
#define I2C_MASTER_NUM I2C_NUM_0 

// For ease/clarity later, these are the pre-composed
// read/write addresses themselves. 
#define TLV493D_I2C_WRITE_ADDR    (TLV493D_I2C_ADDR | I2C_MASTER_WRITE)
#define TLV493D_I2C_READ_ADDR     (TLV493D_I2C_ADDR | I2C_MASTER_READ)

    uint8_t data 0xFF;
    status = i2c_master_write_to_device(  I2C_MASTER_NUM, 
                                        TLV493D_I2C_WRITE_ADDR, 
                                        &data, 
                                        1,
                                        1000 / portTICK_PERIOD_MS);

MicroController
Posts: 1708
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: I2C single byte write

Postby MicroController » Fri Dec 08, 2023 1:47 pm

However, I can't seem to figure out how to write a single byte using that function.
That's because i2c_master_write_to_device(...) doesn't work that way :)
This function performs a 'normal' write to an I2C device; therefore, it must first send the slave device's address + R/W bit.

The 'send a single 0xff byte' is actually the I2C's 'clear bus' operation, meant to get all connected slaves to release the bus in case one somehow got out-of-sync with the master mid-read. And this is what the IDF I2C driver's i2c_master_bus_reset(...) will do for you.

sb_espressif
Posts: 28
Joined: Fri Dec 08, 2023 3:04 am

Re: I2C single byte write

Postby sb_espressif » Tue Dec 12, 2023 2:10 am

Super helpful, thank you so much!

It turns out that - in my case - I was trying to be a bit too clever by half and pre-composing the read and write addresses of my sensor. The i2c_master_write_to_device function expects, however, the first 7 bits of the sensor address, and helpfully composes the read or write bits on the end internally. This is what was giving rise to my error.

But I'm grateful to have the i2c_master_bus_reset() function highlighted, I hadn't spotted that and it's indeed something I think I'd like to incorporate into my setup here. Thank you!

Who is online

Users browsing this forum: No registered users and 79 guests