I2C - allowed use cases for the command-link
Posted: Sat Aug 19, 2017 11:02 pm
I wrote s simple I2C bus scanner using the following code:
where I2CCommandLink is a simple class to allow scope-based creation/destruction of the I2C command link like so:
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()?
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);
}
}
}
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);
}
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()?