mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-08 20:21:04 +00:00
I2C: i2c_port_t type is now an enumeration
* Closes https://github.com/espressif/esp-idf/issues/9009
This commit is contained in:
@@ -15,6 +15,20 @@ namespace idf {
|
||||
|
||||
#define I2C_CHECK_THROW(err) CHECK_THROW_SPECIFIC((err), I2CException)
|
||||
|
||||
/**
|
||||
* I2C bus are defined in the header files, let's check that the values are correct
|
||||
*/
|
||||
#if SOC_I2C_NUM >= 2
|
||||
static_assert(I2C_NUM_1 == 1, "I2C_NUM_1 must be equal to 1");
|
||||
#endif // SOC_I2C_NUM >= 2
|
||||
static_assert(I2C_NUM_MAX == SOC_I2C_NUM, "I2C_NUM_MAX must be equal to SOC_I2C_NUM");
|
||||
|
||||
namespace {
|
||||
i2c_port_t i2c_num_to_driver_type(I2CNumber num) {
|
||||
return static_cast<i2c_port_t>(num.get_num());
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t check_i2c_num(uint32_t i2c_num) noexcept
|
||||
{
|
||||
if (i2c_num >= I2C_NUM_MAX) {
|
||||
@@ -96,7 +110,7 @@ void I2CCommandLink::stop()
|
||||
|
||||
void I2CCommandLink::execute_transfer(I2CNumber i2c_num, chrono::milliseconds driver_timeout)
|
||||
{
|
||||
esp_err_t err = i2c_master_cmd_begin(i2c_num.get_num(), handle, driver_timeout.count() / portTICK_PERIOD_MS);
|
||||
esp_err_t err = i2c_master_cmd_begin(i2c_num_to_driver_type(i2c_num), handle, driver_timeout.count() / portTICK_PERIOD_MS);
|
||||
if (err != ESP_OK) {
|
||||
throw I2CTransferException(err);
|
||||
}
|
||||
@@ -121,13 +135,13 @@ I2CMaster::I2CMaster(I2CNumber i2c_number,
|
||||
conf.sda_io_num = sda_gpio.get_num();
|
||||
conf.sda_pullup_en = sda_pullup;
|
||||
conf.master.clk_speed = clock_speed.get_value();
|
||||
I2C_CHECK_THROW(i2c_param_config(i2c_num.get_value(), &conf));
|
||||
I2C_CHECK_THROW(i2c_driver_install(i2c_num.get_value(), conf.mode, 0, 0, 0));
|
||||
I2C_CHECK_THROW(i2c_param_config(i2c_num_to_driver_type(i2c_num), &conf));
|
||||
I2C_CHECK_THROW(i2c_driver_install(i2c_num_to_driver_type(i2c_num), conf.mode, 0, 0, 0));
|
||||
}
|
||||
|
||||
I2CMaster::~I2CMaster()
|
||||
{
|
||||
i2c_driver_delete(i2c_num.get_value());
|
||||
i2c_driver_delete(i2c_num_to_driver_type(i2c_num));
|
||||
}
|
||||
|
||||
void I2CMaster::sync_write(I2CAddress i2c_addr, const vector<uint8_t> &data)
|
||||
@@ -174,23 +188,23 @@ I2CSlave::I2CSlave(I2CNumber i2c_number,
|
||||
conf.sda_pullup_en = sda_pullup;
|
||||
conf.slave.addr_10bit_en = 0;
|
||||
conf.slave.slave_addr = slave_addr.get_addr();
|
||||
I2C_CHECK_THROW(i2c_param_config(i2c_num.get_value(), &conf));
|
||||
I2C_CHECK_THROW(i2c_driver_install(i2c_num.get_value(), conf.mode, rx_buf_len, tx_buf_len, 0));
|
||||
I2C_CHECK_THROW(i2c_param_config(i2c_num_to_driver_type(i2c_num), &conf));
|
||||
I2C_CHECK_THROW(i2c_driver_install(i2c_num_to_driver_type(i2c_num), conf.mode, rx_buf_len, tx_buf_len, 0));
|
||||
}
|
||||
|
||||
I2CSlave::~I2CSlave()
|
||||
{
|
||||
i2c_driver_delete(i2c_num.get_value());
|
||||
i2c_driver_delete(i2c_num_to_driver_type(i2c_num));
|
||||
}
|
||||
|
||||
int I2CSlave::write_raw(const uint8_t *data, size_t data_len, chrono::milliseconds timeout)
|
||||
{
|
||||
return i2c_slave_write_buffer(i2c_num.get_value(), data, data_len, (TickType_t) timeout.count() / portTICK_PERIOD_MS);
|
||||
return i2c_slave_write_buffer(i2c_num_to_driver_type(i2c_num), data, data_len, (TickType_t) timeout.count() / portTICK_PERIOD_MS);
|
||||
}
|
||||
|
||||
int I2CSlave::read_raw(uint8_t *buffer, size_t buffer_len, chrono::milliseconds timeout)
|
||||
{
|
||||
return i2c_slave_read_buffer(i2c_num.get_value(), buffer, buffer_len, (TickType_t) timeout.count() / portTICK_PERIOD_MS);
|
||||
return i2c_slave_read_buffer(i2c_num_to_driver_type(i2c_num), buffer, buffer_len, (TickType_t) timeout.count() / portTICK_PERIOD_MS);
|
||||
}
|
||||
#endif // CONFIG_SOC_I2C_SUPPORT_SLAVE
|
||||
|
||||
|
Reference in New Issue
Block a user