mirror of
https://github.com/espressif/esp-idf.git
synced 2025-12-07 17:08:49 +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:
@@ -300,7 +300,7 @@ struct SPITransactionDescriptorFix {
|
||||
};
|
||||
|
||||
struct I2CMasterFix {
|
||||
I2CMasterFix(i2c_port_t port_arg = 0) : i2c_conf(), port(port_arg)
|
||||
I2CMasterFix(i2c_port_t port_arg = I2C_NUM_0) : i2c_conf(), port(port_arg)
|
||||
{
|
||||
i2c_conf.mode = i2c_mode_t::I2C_MODE_MASTER;
|
||||
i2c_conf.sda_io_num = 2;
|
||||
@@ -320,7 +320,7 @@ struct I2CMasterFix {
|
||||
|
||||
#if CONFIG_SOC_I2C_SUPPORT_SLAVE
|
||||
struct I2CSlaveFix {
|
||||
I2CSlaveFix(CreateAnd flags, i2c_port_t port_arg = 0, size_t buffer_size = 64) : i2c_conf(), port(port_arg)
|
||||
I2CSlaveFix(CreateAnd flags, i2c_port_t port_arg = I2C_NUM_0, size_t buffer_size = 64) : i2c_conf(), port(port_arg)
|
||||
{
|
||||
if (flags == CreateAnd::SUCCEED) {
|
||||
i2c_conf.mode = i2c_mode_t::I2C_MODE_SLAVE;
|
||||
|
||||
@@ -183,7 +183,7 @@ TEST_CASE("I2CWrite calls driver correctly")
|
||||
// will actually write the data but for the tests it is enough for now
|
||||
i2c_master_write_ExpectWithArrayAndReturn(&cmd_fix.dummy_handle, expected_write, WRITE_SIZE, EXPECTED_DATA_LEN, true, ESP_OK);
|
||||
i2c_master_stop_ExpectAndReturn(&cmd_fix.dummy_handle, ESP_OK);
|
||||
i2c_master_cmd_begin_ExpectAndReturn(0, &cmd_fix.dummy_handle, 1000 / portTICK_PERIOD_MS, ESP_OK);
|
||||
i2c_master_cmd_begin_ExpectAndReturn(I2C_NUM_0, &cmd_fix.dummy_handle, 1000 / portTICK_PERIOD_MS, ESP_OK);
|
||||
|
||||
std::vector<uint8_t> WRITE_BYTES = {0xAB, 0xBA};
|
||||
I2CWrite write(WRITE_BYTES);
|
||||
@@ -218,7 +218,7 @@ TEST_CASE("I2CRead calls driver correctly")
|
||||
// will actually read the data but for the tests it is enough for now
|
||||
i2c_master_read_ReturnArrayThruPtr_data(READ_DATA, READ_SIZE);
|
||||
i2c_master_stop_ExpectAndReturn(&cmd_fix.dummy_handle, ESP_OK);
|
||||
i2c_master_cmd_begin_ExpectAndReturn(0, &cmd_fix.dummy_handle, 1000 / portTICK_PERIOD_MS, ESP_OK);
|
||||
i2c_master_cmd_begin_ExpectAndReturn(I2C_NUM_0, &cmd_fix.dummy_handle, 1000 / portTICK_PERIOD_MS, ESP_OK);
|
||||
|
||||
I2CRead reader(READ_SIZE);
|
||||
std::vector<uint8_t> result = reader.do_transfer(I2CNumber::I2C0(), I2CAddress(0x47));
|
||||
@@ -261,7 +261,7 @@ TEST_CASE("I2CComposed calls driver correctly")
|
||||
// will actually read the data but for the tests it is enough for now
|
||||
i2c_master_read_ReturnArrayThruPtr_data(READ_DATA, READ_SIZE);
|
||||
i2c_master_stop_ExpectAndReturn(&cmd_fix.dummy_handle, ESP_OK);
|
||||
i2c_master_cmd_begin_ExpectAndReturn(0, &cmd_fix.dummy_handle, 1000 / portTICK_PERIOD_MS, ESP_OK);
|
||||
i2c_master_cmd_begin_ExpectAndReturn(I2C_NUM_0, &cmd_fix.dummy_handle, 1000 / portTICK_PERIOD_MS, ESP_OK);
|
||||
|
||||
I2CComposed composed_transfer;
|
||||
composed_transfer.add_write({0x47, 0x48, 0x49});
|
||||
@@ -289,7 +289,7 @@ TEST_CASE("I2CWrite transfer calls driver correctly")
|
||||
// will actually write the data but for the tests it is enough for now
|
||||
i2c_master_write_ExpectWithArrayAndReturn(&cmd_fix.dummy_handle, expected_write, WRITE_SIZE, EXPECTED_DATA_LEN, true, ESP_OK);
|
||||
i2c_master_stop_ExpectAndReturn(&cmd_fix.dummy_handle, ESP_OK);
|
||||
i2c_master_cmd_begin_ExpectAndReturn(0, &cmd_fix.dummy_handle, 1000 / portTICK_PERIOD_MS, ESP_OK);
|
||||
i2c_master_cmd_begin_ExpectAndReturn(I2C_NUM_0, &cmd_fix.dummy_handle, 1000 / portTICK_PERIOD_MS, ESP_OK);
|
||||
|
||||
I2CMaster master(I2CNumber::I2C0(), SCL_GPIO(1), SDA_GPIO(2), Frequency(400000));
|
||||
std::vector<uint8_t> WRITE_BYTES = {0xAB, 0xBA};
|
||||
@@ -310,7 +310,7 @@ TEST_CASE("I2CMaster synchronous write")
|
||||
// will actually write the data but for the tests it is enough for now
|
||||
i2c_master_write_ExpectWithArrayAndReturn(&cmd_fix.dummy_handle, expected_write, WRITE_SIZE, EXPECTED_DATA_LEN, true, ESP_OK);
|
||||
i2c_master_stop_ExpectAndReturn(&cmd_fix.dummy_handle, ESP_OK);
|
||||
i2c_master_cmd_begin_ExpectAndReturn(0, &cmd_fix.dummy_handle, 1000 / portTICK_PERIOD_MS, ESP_OK);
|
||||
i2c_master_cmd_begin_ExpectAndReturn(I2C_NUM_0, &cmd_fix.dummy_handle, 1000 / portTICK_PERIOD_MS, ESP_OK);
|
||||
|
||||
I2CMaster master(I2CNumber::I2C0(), SCL_GPIO(1), SDA_GPIO(2), Frequency(400000));
|
||||
std::vector<uint8_t> WRITE_BYTES = {0xAB, 0xBA};
|
||||
@@ -332,7 +332,7 @@ TEST_CASE("I2CMaster synchronous read")
|
||||
// will actually read the data but for the tests it is enough for now
|
||||
i2c_master_read_ReturnArrayThruPtr_data(READ_DATA, READ_SIZE);
|
||||
i2c_master_stop_ExpectAndReturn(&cmd_fix.dummy_handle, ESP_OK);
|
||||
i2c_master_cmd_begin_ExpectAndReturn(0, &cmd_fix.dummy_handle, 1000 / portTICK_PERIOD_MS, ESP_OK);
|
||||
i2c_master_cmd_begin_ExpectAndReturn(I2C_NUM_0, &cmd_fix.dummy_handle, 1000 / portTICK_PERIOD_MS, ESP_OK);
|
||||
|
||||
I2CMaster master(I2CNumber::I2C0(), SCL_GPIO(1), SDA_GPIO(2), Frequency(400000));
|
||||
std::vector<uint8_t> result = master.sync_read(I2CAddress(0x47), READ_SIZE);
|
||||
@@ -365,7 +365,7 @@ TEST_CASE("I2CMaster syncronous transfer (read and write)")
|
||||
// will actually read the data but for the tests it is enough for now
|
||||
i2c_master_read_ReturnArrayThruPtr_data(READ_DATA, READ_SIZE);
|
||||
i2c_master_stop_ExpectAndReturn(&cmd_fix.dummy_handle, ESP_OK);
|
||||
i2c_master_cmd_begin_ExpectAndReturn(0, &cmd_fix.dummy_handle, 1000 / portTICK_PERIOD_MS, ESP_OK);
|
||||
i2c_master_cmd_begin_ExpectAndReturn(I2C_NUM_0, &cmd_fix.dummy_handle, 1000 / portTICK_PERIOD_MS, ESP_OK);
|
||||
|
||||
I2CMaster master(I2CNumber::I2C0(), SCL_GPIO(1), SDA_GPIO(2), Frequency(400000));
|
||||
vector<uint8_t> read_result = master.sync_transfer(I2CAddress(0x47), {0x47, 0x48, 0x49}, READ_SIZE);
|
||||
|
||||
@@ -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