I2C - allowed use cases for the command-link

permal
Posts: 384
Joined: Sun May 14, 2017 5:36 pm

I2C - allowed use cases for the command-link

Postby permal » Sat Aug 19, 2017 11:02 pm

I wrote s simple I2C bus scanner using the following code:

Code: Select all

                void I2CMasterDevice::scan_i2c_bus(std::vector<uint8_t>& found_devices)
                {

                    // Write the address of each possible device and see if an ACK is received or not.
                    // TODO: Ignore reserved addresses
                    for (uint8_t address = 1; address <= 127; ++address)
                    {
                        I2CCommandLink link;
                        auto read_address = (address << 1);

                        auto res = i2c_master_start(link);
                        res |= i2c_master_write_byte(link, read_address, true);
                        res |= i2c_master_stop(link);
                        res |= i2c_master_cmd_begin(port, link, pdMS_TO_TICKS(50));

                        if (res != ESP_OK)
                        {
                            // No ACK, no device on this address
                        }
                        else
                        {
                            found_devices.push_back(address);
                        }
                    }
                }
where I2CCommandLink is a simple class to allow scope-based creation/destruction of the I2C command link like so:

Code: Select all

                class I2CCommandLink
                {
                    public:
                        I2CCommandLink();
                        ~I2CCommandLink();

                        operator i2c_cmd_handle_t() const
                        {
                            return cmd_link;
                        }

                    private:
                        i2c_cmd_handle_t cmd_link;
                };
                
                I2CCommandLink::I2CCommandLink()
                {
                    cmd_link = i2c_cmd_link_create();
                }

                I2CCommandLink::~I2CCommandLink()
                {
                    i2c_cmd_link_delete(cmd_link);
                }                
                
This is working fine, but if I move I2CCommandLink link; outside the for-loop, the call to i2c_master_cmd_begin() timeout on the second and every call there after.

The documentation doesn't say much about how the command link returned by i2c_cmd_link_create() shall be used. In another part of my code I'm successfully using the same command-link for both read and writes, but multiple reads doesn't seem to be allowed?

So, my question is: What are the allowed use cases for an i2c command-link returned by i2c_cmd_link_create()?

permal
Posts: 384
Joined: Sun May 14, 2017 5:36 pm

Re: I2C - allowed use cases for the command-link

Postby permal » Mon Aug 21, 2017 9:47 pm

Anyone?

permal
Posts: 384
Joined: Sun May 14, 2017 5:36 pm

Re: I2C - allowed use cases for the command-link

Postby permal » Thu Sep 14, 2017 9:11 pm

Never got an answer to this, but in case someone else wonders the same thing, these are my conclusions:

* A command link can be used until a stop condition is issued. After that you must use a new command link.
* You can issue a multiple (re)start conditions on the same command link.

Who is online

Users browsing this forum: Bing [Bot], MicroController and 109 guests