Open tobiaseul opened 1 year ago
Hello @tobiaseul ,
This part of the I2C has not been changed for the past few months, have you tried to use git-bisect
to see which commit broke your program? I suspect it could be a side-effect.
In fact, in your code you are using an array on the stack with a variable length uint8_t data[p_len];
. In the current situation, I don't think that's the problem because 6 is a reasonable amount of bytes to allocate, but overall, this can be dangerous and not recommended in embedded systems, mainly when the memory is limited.
Can you try something like:
esp_err_t I2C::readRegister(uint8_t p_i2cAddr, uint8_t p_regAddr, std::vector<int8_t>& p_data, size_t p_len)
{
p_data.resize(p_len);
int ret = i2c_master_read_from_device(static_cast<i2c_port_t>(m_interface), p_i2cAddr, p_data.data(), p_len, m_timeout / configTICK_RATE_HZ);
return ret;
}
Moreover, I notice that you are not using register address, so you are reading from the device without giving a register address, you should use i2c_master_write_read_device
instead:
esp_err_t I2C::readRegister(uint8_t p_i2cAddr, uint8_t p_regAddr, std::vector<int8_t>& p_data, size_t p_len)
{
p_data.resize(p_len);
int ret = i2c_master_write_read_device(static_cast<i2c_port_t>(m_interface), p_i2cAddr,
&p_regAddr, sizeof(uint8_t),
p_data.data(), p_len, m_timeout / configTICK_RATE_HZ);
return ret;
}
Out of curiosity, why not using the CXX I2C API? You can find it here: https://github.com/espressif/esp-idf-cxx/blob/main/include/i2c_cxx.hpp
Answers checklist.
IDF version.
ESP-IDF v5.2-dev-350-g56123c52aa-dirty
Operating System used.
Linux
How did you build your project?
Command line with idf.py
If you are using Windows, please specify command line type.
None
Development Kit.
ESP32-WROOM-32 dev kit
Power Supply used.
USB
What is the expected behavior?
Function should return ESP_OK
What is the actual behavior?
But crashes
Steps to reproduce.
I have following code:
constexpr int I2C_IFACE1 = 0; constexpr int I2C_SDA_PIN = 21; constexpr int I2C_SCL_PIN = 22; constexpr int I2C_FREQ_HZ = 400000; constexpr int I2C_TIMEOUT_MS = 20;
i2c_config_t i2cConfig = {};
i2cConfig.mode = I2C_MODE_MASTER; i2cConfig.sda_io_num = I2C_SDA_PIN; i2cConfig.scl_io_num = I2C_SCL_PIN; i2cConfig.sda_pullup_en = GPIO_PULLUP_ENABLE; i2cConfig.scl_pullup_en = GPIO_PULLUP_ENABLE; i2cConfig.master.clk_speed = I2C_FREQ_HZ;
I2C::SPtr i2c = std::make_shared(I2C_IFACE1, I2C_TIMEOUT_MS, i2cConfig);
I2C::I2C(int p_interface, int p_timeout, i2c_config_t p_config): m_interface(p_interface), m_timeout(p_timeout), m_config(p_config) { i2c_param_config(static_cast(m_interface), &m_config);
i2c_driver_install(static_cast(m_interface), m_config.mode, 0, 0, 0);
}
esp_err_t I2C::readRegister(uint8_t p_i2cAddr, uint8_t p_regAddr, std::vector& p_data, size_t p_len){
}
By running this code:
constexpr uint8_t I2C_ADDR = 0x68; constexpr uint8_t REG_GYRO_X = 0x43;
std::vector data = {};
m_i2c->readRegister(I2C_ADDR, REG_GYRO_X, data, 6);
Then it crashes at /esp/esp-idf/components/driver/i2c/i2c.c:1368
Debug Logs.
More Information.
I have tryed multiple dev boards to ensure it isn't a hardware defact.
I had this code running ~2 years back, with an older version of idf. But i have no more information about the version i used back then.
This error is like errors from v4.4 (https://github.com/espressif/esp-idf/issues/8348) but this is allready fixed.