refactor (cxx)!: I2C C++ classes use strong value types now

Added host-based I2C C++ unit tests

BREAKING CHANGE: I2C C++ interface changes, raw values for
    arguments are mostly not allowed anymore.
pull/8117/head
Jakob Hasse 2021-10-25 18:58:15 +08:00
rodzic 47ab8f8e63
commit 7572f75d6b
10 zmienionych plików z 855 dodań i 417 usunięć

Wyświetl plik

@ -298,7 +298,7 @@ i2c_cmd_handle_t i2c_cmd_link_create_static(uint8_t* buffer, uint32_t size);
* to release and return the resources.
* The required bytes will be dynamically allocated.
*
* @return Handle to the I2C command link
* @return Handle to the I2C command link or NULL in case of insufficient dynamic memory.
*/
i2c_cmd_handle_t i2c_cmd_link_create(void);

Wyświetl plik

@ -1,7 +1,7 @@
/*
* SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*
* This example code is in the Public Domain (or CC0 licensed, at your option.)
*
@ -14,10 +14,12 @@
#include "gpio_cxx.hpp"
#include "driver/spi_master.h"
#include "spi_cxx.hpp"
#include "i2c_cxx.hpp"
extern "C" {
#include "Mockgpio.h"
#include "Mockspi_master.h"
#include "Mockspi_common.h"
#include "Mocki2c.h"
}
static const idf::GPIONum VALID_GPIO(18);
@ -296,3 +298,62 @@ struct SPITransactionDescriptorFix {
std::vector<uint8_t> tx_data;
std::vector<uint8_t> rx_data;
};
struct I2CMasterFix {
I2CMasterFix(i2c_port_t port_arg = 0) : i2c_conf(), port(port_arg)
{
i2c_conf.mode = i2c_mode_t::I2C_MODE_MASTER;
i2c_conf.sda_io_num = 2;
i2c_conf.scl_io_num = 1;
i2c_conf.sda_pullup_en = true;
i2c_conf.scl_pullup_en = true;
i2c_conf.master.clk_speed = 400000;
i2c_conf.clk_flags = 0;
i2c_param_config_ExpectWithArrayAndReturn(i2c_port_t(0), &i2c_conf, 1, ESP_OK);
i2c_driver_install_ExpectAndReturn(i2c_port_t(0), i2c_mode_t::I2C_MODE_MASTER, 0, 0, 0, ESP_OK);
i2c_driver_delete_ExpectAndReturn(i2c_port_t(0), ESP_OK);
}
i2c_config_t i2c_conf;
i2c_port_t port;
};
struct I2CSlaveFix {
I2CSlaveFix(CreateAnd flags, i2c_port_t port_arg = 0, size_t buffer_size = 64) : i2c_conf(), port(port_arg)
{
if (flags == CreateAnd::SUCCEED) {
i2c_conf.mode = i2c_mode_t::I2C_MODE_SLAVE;
i2c_conf.sda_io_num = 2;
i2c_conf.scl_io_num = 1;
i2c_conf.sda_pullup_en = true;
i2c_conf.scl_pullup_en = true;
i2c_conf.slave.addr_10bit_en = 0;
i2c_conf.slave.slave_addr = 0x47;
i2c_param_config_ExpectWithArrayAndReturn(port, &i2c_conf, 1, ESP_OK);
i2c_driver_install_ExpectAndReturn(port, i2c_mode_t::I2C_MODE_SLAVE, buffer_size, buffer_size, 0, ESP_OK);
i2c_driver_delete_ExpectAndReturn(port, ESP_OK);
} else if (flags == CreateAnd::IGNORE) {
i2c_param_config_IgnoreAndReturn(ESP_OK);
i2c_driver_install_IgnoreAndReturn(ESP_OK);
i2c_driver_delete_IgnoreAndReturn(ESP_OK);
} else {
throw idf::I2CException(ESP_ERR_INVALID_ARG);
}
}
i2c_config_t i2c_conf;
i2c_port_t port;
};
struct I2CCmdLinkFix
{
I2CCmdLinkFix(uint8_t expected_addr, i2c_rw_t type = I2C_MASTER_WRITE) : dummy_handle(reinterpret_cast<i2c_cmd_handle_t>(0xbeef))
{
i2c_cmd_link_create_ExpectAndReturn(&dummy_handle);
i2c_master_start_ExpectAndReturn(&dummy_handle, ESP_OK);
i2c_master_write_byte_ExpectAndReturn(&dummy_handle, expected_addr << 1 | type, true, ESP_OK);
i2c_cmd_link_delete_Expect(&dummy_handle);
}
i2c_cmd_handle_t dummy_handle;
};

Wyświetl plik

@ -7,3 +7,5 @@ idf_component_register(SRCS "i2c_cxx_test.cpp"
"${cpp_component}/private_include"
$ENV{IDF_PATH}/tools/catch
REQUIRES cmock driver experimental_cpp_component)
target_link_libraries(${COMPONENT_LIB} -lpthread)

Wyświetl plik

@ -1,7 +1,7 @@
/*
* SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*
* I2C C++ unit tests
*
@ -15,6 +15,7 @@
#include <stdio.h>
#include "unity.h"
#include "freertos/portmacro.h"
#include "driver/i2c.h"
#include "i2c_cxx.hpp"
#include "system_cxx.hpp"
#include "test_fixtures.hpp"
@ -33,9 +34,22 @@ const char *esp_err_to_name(esp_err_t code) {
using namespace std;
using namespace idf;
TEST_CASE("I2CBus")
TEST_CASE("I2CNumber")
{
I2CBus bus(static_cast<i2c_port_t>(0));
CMockFixture fix;
CHECK(I2CNumber::I2C0().get_num() == 0);
}
TEST_CASE("I2CAddr")
{
CMockFixture fix;
CHECK_THROWS_AS(I2CAddress(-1), I2CException&);
I2CAddress(0);
I2CAddress(127);
CHECK_THROWS_AS(I2CAddress(128), I2CException&);
I2CAddress addr(47);
CHECK(addr.get_addr() == 47);
}
TEST_CASE("I2CMaster parameter configuration fails")
@ -43,7 +57,7 @@ TEST_CASE("I2CMaster parameter configuration fails")
CMockFixture fix;
i2c_param_config_ExpectAnyArgsAndReturn(ESP_FAIL);
CHECK_THROWS_AS(I2CMaster(0, 1, 2, 400000), I2CException&);
CHECK_THROWS_AS(I2CMaster(I2CNumber::I2C0(), SCL_GPIO(1), SDA_GPIO(2), Frequency(400000)), I2CException&);
}
TEST_CASE("I2CMaster driver install failure")
@ -52,5 +66,395 @@ TEST_CASE("I2CMaster driver install failure")
i2c_param_config_ExpectAnyArgsAndReturn(ESP_OK);
i2c_driver_install_ExpectAnyArgsAndReturn(ESP_FAIL);
CHECK_THROWS_AS(I2CMaster(0, 1, 2, 400000), I2CException&);
CHECK_THROWS_AS(I2CMaster(I2CNumber::I2C0(), SCL_GPIO(1), SDA_GPIO(2), Frequency(400000)), I2CException&);
}
TEST_CASE("I2CMaster success")
{
CMockFixture fix;
I2CMasterFix master_fix;
I2CMaster(I2CNumber::I2C0(), SCL_GPIO(1), SDA_GPIO(2), Frequency(400000));
}
TEST_CASE("I2CWrite empty data throws")
{
CMockFixture fix;
std::vector<uint8_t> empty;
CHECK_THROWS_AS(I2CWrite writer(empty), I2CException&);
}
TEST_CASE("I2CRead zero length throws")
{
CMockFixture fix;
std::vector<uint8_t> empty;
CHECK_THROWS_AS(I2CRead reader(0), I2CException&);
}
TEST_CASE("I2CWrite do_transfer fails at link creation")
{
CMockFixture fix;
i2c_cmd_link_create_ExpectAndReturn(nullptr);
i2c_cmd_link_delete_Ignore();
I2CWrite writer({47});
CHECK_THROWS_AS(writer.do_transfer(I2CNumber::I2C0(), I2CAddress(0x47)), I2CException&);
}
TEST_CASE("I2CWrite do_transfer fails at start")
{
CMockFixture fix;
i2c_cmd_handle_t dummy_handle = reinterpret_cast<i2c_cmd_handle_t>(0xbeef);
i2c_cmd_link_create_IgnoreAndReturn(&dummy_handle);
i2c_master_start_ExpectAnyArgsAndReturn(ESP_FAIL);
i2c_cmd_link_delete_Ignore();
I2CWrite writer({47});
CHECK_THROWS_AS(writer.do_transfer(I2CNumber::I2C0(), I2CAddress(0x47)), I2CException&);
}
TEST_CASE("I2CWrite do_transfer fails at write byte")
{
CMockFixture fix;
i2c_cmd_handle_t dummy_handle = reinterpret_cast<i2c_cmd_handle_t>(0xbeef);
i2c_cmd_link_create_IgnoreAndReturn(&dummy_handle);
i2c_master_start_IgnoreAndReturn(ESP_OK);
i2c_master_write_byte_ExpectAnyArgsAndReturn(ESP_FAIL);
i2c_cmd_link_delete_Ignore();
I2CWrite writer({47});
CHECK_THROWS_AS(writer.do_transfer(I2CNumber::I2C0(), I2CAddress(0x47)), I2CException&);
}
TEST_CASE("I2CWrite do_transfer fails at write")
{
CMockFixture fix;
i2c_cmd_handle_t dummy_handle = reinterpret_cast<i2c_cmd_handle_t>(0xbeef);
i2c_cmd_link_create_IgnoreAndReturn(&dummy_handle);
i2c_master_start_IgnoreAndReturn(ESP_OK);
i2c_master_write_byte_IgnoreAndReturn(ESP_OK);
i2c_master_write_ExpectAnyArgsAndReturn(ESP_FAIL);
i2c_cmd_link_delete_Ignore();
I2CWrite writer({47});
CHECK_THROWS_AS(writer.do_transfer(I2CNumber::I2C0(), I2CAddress(0x47)), I2CException&);
}
TEST_CASE("I2CWrite do_transfer fails at stop")
{
CMockFixture fix;
i2c_cmd_handle_t dummy_handle = reinterpret_cast<i2c_cmd_handle_t>(0xbeef);
i2c_cmd_link_create_IgnoreAndReturn(&dummy_handle);
i2c_master_start_IgnoreAndReturn(ESP_OK);
i2c_master_write_byte_IgnoreAndReturn(ESP_OK);
i2c_master_write_IgnoreAndReturn(ESP_OK);
i2c_master_stop_ExpectAnyArgsAndReturn(ESP_FAIL);
i2c_cmd_link_delete_Ignore();
I2CWrite writer({47});
CHECK_THROWS_AS(writer.do_transfer(I2CNumber::I2C0(), I2CAddress(0x47)), I2CException&);
}
TEST_CASE("I2CWrite do_transfer execution times out")
{
CMockFixture fix;
i2c_cmd_handle_t dummy_handle = reinterpret_cast<i2c_cmd_handle_t>(0xbeef);
i2c_cmd_link_create_IgnoreAndReturn(&dummy_handle);
i2c_master_start_IgnoreAndReturn(ESP_OK);
i2c_master_write_byte_IgnoreAndReturn(ESP_OK);
i2c_master_write_IgnoreAndReturn(ESP_OK);
i2c_master_stop_IgnoreAndReturn(ESP_OK);
i2c_master_cmd_begin_ExpectAnyArgsAndReturn(ESP_ERR_TIMEOUT);
i2c_cmd_link_delete_Ignore();
I2CWrite writer({47});
CHECK_THROWS_AS(writer.do_transfer(I2CNumber::I2C0(), I2CAddress(0x47)), I2CTransferException&);
}
TEST_CASE("I2CWrite calls driver correctly")
{
CMockFixture fix;
I2CCmdLinkFix cmd_fix(0x47, I2C_MASTER_WRITE);
uint8_t expected_write [] = {0xAB, 0xBA};
const size_t WRITE_SIZE = sizeof(expected_write);
const size_t EXPECTED_DATA_LEN = WRITE_SIZE;
// note that this behavior is not entirely correct, in th real driver, only i2c_master_cmd_begin()
// 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_RATE_MS, ESP_OK);
std::vector<uint8_t> WRITE_BYTES = {0xAB, 0xBA};
I2CWrite write(WRITE_BYTES);
write.do_transfer(I2CNumber::I2C0(), I2CAddress(0x47));
}
TEST_CASE("I2CRead do_transfer fails at read")
{
CMockFixture fix;
i2c_cmd_handle_t dummy_handle = reinterpret_cast<i2c_cmd_handle_t>(0xbeef);
i2c_cmd_link_create_ExpectAndReturn(&dummy_handle);
i2c_master_start_ExpectAnyArgsAndReturn(ESP_OK);
i2c_master_write_byte_ExpectAnyArgsAndReturn(ESP_OK);
i2c_master_read_ExpectAnyArgsAndReturn(ESP_FAIL);
i2c_cmd_link_delete_Ignore();
I2CRead reader(2);
CHECK_THROWS_AS(reader.do_transfer(I2CNumber::I2C0(), I2CAddress(0x47)), I2CException&);
}
TEST_CASE("I2CRead calls driver correctly")
{
CMockFixture fix;
I2CCmdLinkFix cmd_fix(0x47, I2C_MASTER_READ);
uint8_t READ_DATA [] = {0xAB, 0xBA};
const size_t READ_SIZE = sizeof(READ_DATA);
i2c_master_read_ExpectAndReturn(&cmd_fix.dummy_handle, nullptr, READ_SIZE, i2c_ack_type_t::I2C_MASTER_LAST_NACK, ESP_OK);
i2c_master_read_IgnoreArg_data();
// note that this behavior is not entirely correct, in th real driver, only i2c_master_cmd_begin()
// 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_RATE_MS, ESP_OK);
I2CRead reader(READ_SIZE);
std::vector<uint8_t> result = reader.do_transfer(I2CNumber::I2C0(), I2CAddress(0x47));
CHECK(result[0] == 0xAB);
CHECK(result[1] == 0xBA);
}
TEST_CASE("I2CComposed try to read size 0 throws")
{
CMockFixture fix;
I2CComposed composed_transfer;
CHECK_THROWS_AS(composed_transfer.add_read(0), I2CException&);
}
TEST_CASE("I2CComposed try to write empy vector throws")
{
CMockFixture fix;
I2CComposed composed_transfer;
CHECK_THROWS_AS(composed_transfer.add_write({}), I2CException&);
}
TEST_CASE("I2CComposed calls driver correctly")
{
CMockFixture fix;
I2CCmdLinkFix cmd_fix(0x47, I2C_MASTER_WRITE);
uint8_t expected_write [] = {0x47, 0x48, 0x49};
const size_t WRITE_SIZE = sizeof(expected_write);
const size_t EXPECTED_DATA_LEN = WRITE_SIZE;
uint8_t READ_DATA [] = {0xAB, 0xBA};
const size_t READ_SIZE = sizeof(READ_DATA);
// the write-read transaction with repeated start:
i2c_master_write_ExpectWithArrayAndReturn(&cmd_fix.dummy_handle, expected_write, WRITE_SIZE, EXPECTED_DATA_LEN, true, ESP_OK);
i2c_master_start_ExpectAndReturn(&cmd_fix.dummy_handle, ESP_OK);
i2c_master_write_byte_ExpectAndReturn(&cmd_fix.dummy_handle, 0x47 << 1 | I2C_MASTER_READ, true, ESP_OK);
i2c_master_read_ExpectAndReturn(&cmd_fix.dummy_handle, nullptr, 2, i2c_ack_type_t::I2C_MASTER_LAST_NACK, ESP_OK);
i2c_master_read_IgnoreArg_data();
// note that this behavior is not entirely correct, in th real driver, only i2c_master_cmd_begin()
// 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_RATE_MS, ESP_OK);
I2CComposed composed_transfer;
composed_transfer.add_write({0x47, 0x48, 0x49});
composed_transfer.add_read(READ_SIZE);
vector<vector<uint8_t> > read_result = composed_transfer.do_transfer(I2CNumber::I2C0(), I2CAddress(0x47));
TEST_ASSERT_EQUAL(1, read_result.size());
TEST_ASSERT_EQUAL(READ_SIZE, read_result[0].size());
for (int i = 0; i < READ_SIZE; i++) {
TEST_ASSERT_EQUAL(READ_DATA[i], read_result[0][i]);
}
}
TEST_CASE("I2CWrite transfer calls driver correctly")
{
CMockFixture fix;
I2CMasterFix master_fix;
I2CCmdLinkFix cmd_fix(0x47, I2C_MASTER_WRITE);
uint8_t expected_write [] = {0xAB, 0xBA};
const size_t WRITE_SIZE = sizeof(expected_write);
const size_t EXPECTED_DATA_LEN = WRITE_SIZE;
// note that this behavior is not entirely correct, in th real driver, only i2c_master_cmd_begin()
// 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_RATE_MS, ESP_OK);
I2CMaster master(I2CNumber::I2C0(), SCL_GPIO(1), SDA_GPIO(2), Frequency(400000));
std::vector<uint8_t> WRITE_BYTES = {0xAB, 0xBA};
auto writer = make_shared<I2CWrite>(WRITE_BYTES);
master.transfer(I2CAddress(0x47), writer);
}
TEST_CASE("I2CMaster synchronous write")
{
CMockFixture fix;
I2CMasterFix master_fix;
I2CCmdLinkFix cmd_fix(0x47, I2C_MASTER_WRITE);
uint8_t expected_write [] = {0xAB, 0xBA};
const size_t WRITE_SIZE = sizeof(expected_write);
const size_t EXPECTED_DATA_LEN = WRITE_SIZE;
// note that this behavior is not entirely correct, in th real driver, only i2c_master_cmd_begin()
// 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_RATE_MS, ESP_OK);
I2CMaster master(I2CNumber::I2C0(), SCL_GPIO(1), SDA_GPIO(2), Frequency(400000));
std::vector<uint8_t> WRITE_BYTES = {0xAB, 0xBA};
master.sync_write(I2CAddress(0x47), WRITE_BYTES);
}
TEST_CASE("I2CMaster synchronous read")
{
CMockFixture fix;
I2CMasterFix master_fix;
I2CCmdLinkFix cmd_fix(0x47, I2C_MASTER_READ);
uint8_t READ_DATA [] = {0xAB, 0xBA};
const size_t READ_SIZE = sizeof(READ_DATA);
i2c_master_read_ExpectAndReturn(&cmd_fix.dummy_handle, nullptr, READ_SIZE, i2c_ack_type_t::I2C_MASTER_LAST_NACK, ESP_OK);
i2c_master_read_IgnoreArg_data();
// note that this behavior is not entirely correct, in th real driver, only i2c_master_cmd_begin()
// 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_RATE_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);
REQUIRE(result.size() == READ_SIZE);
CHECK(result[0] == 0xAB);
CHECK(result[1] == 0xBA);
}
TEST_CASE("I2CMaster syncronous transfer (read and write)")
{
CMockFixture fix;
I2CMasterFix master_fix;
I2CCmdLinkFix cmd_fix(0x47, I2C_MASTER_WRITE);
i2c_cmd_handle_t dummy_handle = reinterpret_cast<i2c_cmd_handle_t>(0xbeef);
uint8_t expected_write [] = {0x47, 0x48, 0x49};
const size_t WRITE_SIZE = sizeof(expected_write);
const size_t EXPECTED_DATA_LEN = WRITE_SIZE;
uint8_t READ_DATA [] = {0xAB, 0xBA};
const size_t READ_SIZE = sizeof(READ_DATA);
// the write-read transaction with repeated start:
i2c_master_write_ExpectWithArrayAndReturn(&cmd_fix.dummy_handle, expected_write, WRITE_SIZE, EXPECTED_DATA_LEN, true, ESP_OK);
i2c_master_start_ExpectAndReturn(&cmd_fix.dummy_handle, ESP_OK);
i2c_master_write_byte_ExpectAndReturn(&cmd_fix.dummy_handle, 0x47 << 1 | I2C_MASTER_READ, true, ESP_OK);
i2c_master_read_ExpectAndReturn(&cmd_fix.dummy_handle, nullptr, 2, i2c_ack_type_t::I2C_MASTER_LAST_NACK, ESP_OK);
i2c_master_read_IgnoreArg_data();
// note that this behavior is not entirely correct, in th real driver, only i2c_master_cmd_begin()
// 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_RATE_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);
CHECK(read_result.size() == READ_SIZE);
for (int i = 0; i < READ_SIZE; i++) {
CHECK(read_result[i] == READ_DATA[i]);
}
}
TEST_CASE("I2CSlave parameter configuration fails")
{
CMockFixture fix;
i2c_param_config_ExpectAnyArgsAndReturn(ESP_FAIL);
CHECK_THROWS_AS(I2CSlave(I2CNumber::I2C0(), SCL_GPIO(1), SDA_GPIO(2), I2CAddress(0x47), 64, 64), I2CException&);
}
TEST_CASE("I2CSlave driver installation fails")
{
CMockFixture fix;
i2c_param_config_IgnoreAndReturn(ESP_OK);
i2c_driver_install_IgnoreAndReturn(ESP_FAIL);
CHECK_THROWS_AS(I2CSlave (I2CNumber::I2C0(), SCL_GPIO(1), SDA_GPIO(2), I2CAddress(0x47), 64, 64), I2CException&);
}
TEST_CASE("I2CSlave calls driver functions correctly")
{
CMockFixture fix;
I2CSlaveFix slave_fix(CreateAnd::SUCCEED);
I2CSlave slave(I2CNumber::I2C0(), SCL_GPIO(1), SDA_GPIO(2), I2CAddress(0x47), 64, 64);
}
TEST_CASE("I2CSlave write fails")
{
CMockFixture fix;
I2CSlaveFix slave_fix(CreateAnd::IGNORE);
const uint8_t WRITE_BUFFER[] = {0xAB, 0xCD};
const size_t WRITE_BUFFER_LEN = sizeof(WRITE_BUFFER);
i2c_slave_write_buffer_ExpectAnyArgsAndReturn(-1);
I2CSlave slave(I2CNumber::I2C0(), SCL_GPIO(3), SDA_GPIO(4), I2CAddress(0x47), 64, 64);
CHECK(slave.write_raw(WRITE_BUFFER, WRITE_BUFFER_LEN, chrono::milliseconds(0)) == -1);
}
TEST_CASE("I2CSlave write calls driver functions correctly")
{
CMockFixture fix;
I2CSlaveFix slave_fix(CreateAnd::IGNORE);
const uint8_t WRITE_BUFFER[] = {0xAB, 0xCD};
const size_t WRITE_BUFFER_LEN = sizeof(WRITE_BUFFER);
i2c_slave_write_buffer_ExpectWithArrayAndReturn(0,
WRITE_BUFFER,
WRITE_BUFFER_LEN,
WRITE_BUFFER_LEN,
500 / portTICK_PERIOD_MS,
WRITE_BUFFER_LEN);
I2CSlave slave(I2CNumber::I2C0(), SCL_GPIO(3), SDA_GPIO(4), I2CAddress(0x47), 64, 64);
CHECK(slave.write_raw(WRITE_BUFFER, WRITE_BUFFER_LEN, chrono::milliseconds(500)) == WRITE_BUFFER_LEN);
}
TEST_CASE("I2CSlave read fails")
{
CMockFixture fix;
I2CSlaveFix slave_fix(CreateAnd::IGNORE);
const size_t READ_BUFFER_LEN = 2;
uint8_t read_buffer[READ_BUFFER_LEN];
i2c_slave_read_buffer_ExpectAnyArgsAndReturn(-1);
I2CSlave slave(I2CNumber::I2C0(), SCL_GPIO(3), SDA_GPIO(4), I2CAddress(0x47), 64, 64);
CHECK(slave.read_raw(read_buffer, READ_BUFFER_LEN, chrono::milliseconds(0)) == -1);
}
TEST_CASE("I2CSlave read calls driver functions correctly")
{
CMockFixture fix;
I2CSlaveFix slave_fix(CreateAnd::IGNORE);
uint8_t WRITE_BUFFER[] = {0xAB, 0xCD};
const size_t BUFFER_LEN = sizeof(WRITE_BUFFER);
uint8_t read_buffer[BUFFER_LEN];
i2c_slave_read_buffer_ExpectAndReturn(0, read_buffer, BUFFER_LEN, 500 / portTICK_PERIOD_MS, BUFFER_LEN);
i2c_slave_read_buffer_ReturnArrayThruPtr_data(WRITE_BUFFER, BUFFER_LEN);
I2CSlave slave(I2CNumber::I2C0(), SCL_GPIO(3), SDA_GPIO(4), I2CAddress(0x47), 64, 64);
CHECK(slave.read_raw(read_buffer, BUFFER_LEN, chrono::milliseconds(500)) == BUFFER_LEN);
for (size_t i = 0; i < BUFFER_LEN; i++) {
CHECK(read_buffer[i] == WRITE_BUFFER[i]);
}
}

Wyświetl plik

@ -1,19 +1,12 @@
// Copyright 2020 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
* SPDX-FileCopyrightText: 2020-2021 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifdef __cpp_exceptions
#include "driver/i2c.h"
#include "i2c_cxx.hpp"
using namespace std;
@ -22,58 +15,139 @@ namespace idf {
#define I2C_CHECK_THROW(err) CHECK_THROW_SPECIFIC((err), I2CException)
esp_err_t check_i2c_num(uint32_t i2c_num) noexcept
{
if (i2c_num >= I2C_NUM_MAX) {
return ESP_ERR_INVALID_ARG;
}
return ESP_OK;
}
esp_err_t check_i2c_addr(uint32_t addr) noexcept
{
// maximum I2C address currently supported in the C++ classes is 127
if (addr > 0x7f) {
return ESP_ERR_INVALID_ARG;
}
return ESP_OK;
}
I2CException::I2CException(esp_err_t error) : ESPException(error) { }
I2CTransferException::I2CTransferException(esp_err_t error) : I2CException(error) { }
I2CBus::I2CBus(i2c_port_t i2c_number) : i2c_num(i2c_number) { }
uint32_t I2CNumber::get_num()
{
return get_value();
}
I2CAddress::I2CAddress(uint8_t addr) : StrongValueComparable<uint8_t> (addr)
{
esp_err_t error = check_i2c_addr(addr);
if (error != ESP_OK) {
throw I2CException(error);
}
}
uint8_t I2CAddress::get_addr()
{
return get_value();
}
I2CCommandLink::I2CCommandLink()
{
handle = i2c_cmd_link_create();
if (!handle) {
throw I2CException(ESP_ERR_NO_MEM);
}
}
I2CCommandLink::~I2CCommandLink()
{
i2c_cmd_link_delete(handle);
}
void I2CCommandLink::start()
{
I2C_CHECK_THROW(i2c_master_start(handle));
}
void I2CCommandLink::write(const std::vector<uint8_t> &bytes, bool expect_ack)
{
I2C_CHECK_THROW(i2c_master_write(handle, bytes.data(), bytes.size(), expect_ack));
}
void I2CCommandLink::write_byte(uint8_t byte, bool expect_ack)
{
I2C_CHECK_THROW(i2c_master_write_byte(handle, byte, expect_ack));
}
void I2CCommandLink::read(std::vector<uint8_t> &bytes)
{
I2C_CHECK_THROW(i2c_master_read(handle, bytes.data(), bytes.size(), I2C_MASTER_LAST_NACK));
}
void I2CCommandLink::stop()
{
I2C_CHECK_THROW(i2c_master_stop(handle));
}
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_RATE_MS);
if (err != ESP_OK) {
throw I2CTransferException(err);
}
}
I2CBus::I2CBus(I2CNumber i2c_number) : i2c_num(std::move(i2c_number)) { }
I2CBus::~I2CBus() { }
I2CMaster::I2CMaster(i2c_port_t i2c_number,
int scl_gpio,
int sda_gpio,
uint32_t clock_speed,
I2CMaster::I2CMaster(I2CNumber i2c_number,
SCL_GPIO scl_gpio,
SDA_GPIO sda_gpio,
Frequency clock_speed,
bool scl_pullup,
bool sda_pullup)
: I2CBus(i2c_number)
: I2CBus(std::move(i2c_number))
{
i2c_config_t conf = {};
conf.mode = I2C_MODE_MASTER;
conf.scl_io_num = scl_gpio;
conf.scl_io_num = scl_gpio.get_num();
conf.scl_pullup_en = scl_pullup;
conf.sda_io_num = sda_gpio;
conf.sda_io_num = sda_gpio.get_num();
conf.sda_pullup_en = sda_pullup;
conf.master.clk_speed = clock_speed;
I2C_CHECK_THROW(i2c_param_config(i2c_num, &conf));
I2C_CHECK_THROW(i2c_driver_install(i2c_num, conf.mode, 0, 0, 0));
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));
}
I2CMaster::~I2CMaster()
{
i2c_driver_delete(i2c_num);
i2c_driver_delete(i2c_num.get_value());
}
void I2CMaster::sync_write(uint8_t i2c_addr, const vector<uint8_t> &data)
void I2CMaster::sync_write(I2CAddress i2c_addr, const vector<uint8_t> &data)
{
I2CWrite writer(data);
writer.do_transfer(i2c_num, i2c_addr);
}
std::vector<uint8_t> I2CMaster::sync_read(uint8_t i2c_addr, size_t n_bytes)
std::vector<uint8_t> I2CMaster::sync_read(I2CAddress i2c_addr, size_t n_bytes)
{
I2CRead reader(n_bytes);
return reader.do_transfer(i2c_num, i2c_addr);
}
vector<uint8_t> I2CMaster::sync_transfer(uint8_t i2c_addr,
vector<uint8_t> I2CMaster::sync_transfer(I2CAddress i2c_addr,
const std::vector<uint8_t> &write_data,
size_t read_n_bytes)
{
if (!read_n_bytes) throw I2CException(ESP_ERR_INVALID_ARG);
I2CComposed composed_transfer;
composed_transfer.add_write(write_data);
composed_transfer.add_read(read_n_bytes);
@ -81,63 +155,73 @@ vector<uint8_t> I2CMaster::sync_transfer(uint8_t i2c_addr,
return composed_transfer.do_transfer(i2c_num, i2c_addr)[0];
}
I2CSlave::I2CSlave(i2c_port_t i2c_number,
int scl_gpio,
int sda_gpio,
uint8_t slave_addr,
I2CSlave::I2CSlave(I2CNumber i2c_number,
SCL_GPIO scl_gpio,
SDA_GPIO sda_gpio,
I2CAddress slave_addr,
size_t rx_buf_len,
size_t tx_buf_len,
bool scl_pullup,
bool sda_pullup)
: I2CBus(i2c_number)
: I2CBus(std::move(i2c_number))
{
i2c_config_t conf = {};
conf.mode = I2C_MODE_SLAVE;
conf.scl_io_num = scl_gpio;
conf.scl_io_num = scl_gpio.get_value();
conf.scl_pullup_en = scl_pullup;
conf.sda_io_num = sda_gpio;
conf.sda_io_num = sda_gpio.get_value();
conf.sda_pullup_en = sda_pullup;
conf.slave.addr_10bit_en = 0;
conf.slave.slave_addr = slave_addr;
I2C_CHECK_THROW(i2c_param_config(i2c_num, &conf));
I2C_CHECK_THROW(i2c_driver_install(i2c_num, conf.mode, rx_buf_len, tx_buf_len, 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));
}
I2CSlave::~I2CSlave()
{
i2c_driver_delete(i2c_num);
i2c_driver_delete(i2c_num.get_value());
}
int I2CSlave::write_raw(const uint8_t *data, size_t data_len, chrono::milliseconds timeout)
{
return i2c_slave_write_buffer(i2c_num, data, data_len, (TickType_t) timeout.count() / portTICK_RATE_MS);
return i2c_slave_write_buffer(i2c_num.get_value(), data, data_len, (TickType_t) timeout.count() / portTICK_RATE_MS);
}
int I2CSlave::read_raw(uint8_t *buffer, size_t buffer_len, chrono::milliseconds timeout)
{
return i2c_slave_read_buffer(i2c_num, buffer, buffer_len, (TickType_t) timeout.count() / portTICK_RATE_MS);
return i2c_slave_read_buffer(i2c_num.get_value(), buffer, buffer_len, (TickType_t) timeout.count() / portTICK_RATE_MS);
}
I2CWrite::I2CWrite(const vector<uint8_t> &bytes, chrono::milliseconds driver_timeout)
: I2CTransfer<void>(driver_timeout), bytes(bytes) { }
void I2CWrite::queue_cmd(i2c_cmd_handle_t handle, uint8_t i2c_addr)
: I2CTransfer<void>(driver_timeout), bytes(bytes)
{
I2C_CHECK_THROW(i2c_master_start(handle));
I2C_CHECK_THROW(i2c_master_write_byte(handle, i2c_addr << 1 | I2C_MASTER_WRITE, true));
I2C_CHECK_THROW(i2c_master_write(handle, bytes.data(), bytes.size(), true));
if (bytes.empty()) {
throw I2CException(ESP_ERR_INVALID_ARG);
}
}
void I2CWrite::queue_cmd(I2CCommandLink &handle, I2CAddress i2c_addr)
{
handle.start();
handle.write_byte(i2c_addr.get_value() << 1 | I2C_MASTER_WRITE);
handle.write(bytes);
}
void I2CWrite::process_result() { }
I2CRead::I2CRead(size_t size, chrono::milliseconds driver_timeout)
: I2CTransfer<vector<uint8_t> >(driver_timeout), bytes(size) { }
void I2CRead::queue_cmd(i2c_cmd_handle_t handle, uint8_t i2c_addr)
: I2CTransfer<vector<uint8_t> >(driver_timeout), bytes(size)
{
I2C_CHECK_THROW(i2c_master_start(handle));
I2C_CHECK_THROW(i2c_master_write_byte(handle, i2c_addr << 1 | I2C_MASTER_READ, true));
I2C_CHECK_THROW(i2c_master_read(handle, bytes.data(), bytes.size(), I2C_MASTER_LAST_NACK));
if (size == 0) {
throw I2CException(ESP_ERR_INVALID_ARG);
}
}
void I2CRead::queue_cmd(I2CCommandLink &handle, I2CAddress i2c_addr)
{
handle.start();
handle.write_byte(i2c_addr.get_value() << 1 | I2C_MASTER_READ);
handle.read(bytes);
}
vector<uint8_t> I2CRead::process_result()
@ -148,10 +232,10 @@ vector<uint8_t> I2CRead::process_result()
I2CComposed::I2CComposed(chrono::milliseconds driver_timeout)
: I2CTransfer<vector<vector<uint8_t> > >(driver_timeout), transfer_list() { }
void I2CComposed::CompTransferNodeRead::queue_cmd(i2c_cmd_handle_t handle, uint8_t i2c_addr)
void I2CComposed::CompTransferNodeRead::queue_cmd(I2CCommandLink &handle, I2CAddress i2c_addr)
{
I2C_CHECK_THROW(i2c_master_write_byte(handle, i2c_addr << 1 | I2C_MASTER_READ, true));
I2C_CHECK_THROW(i2c_master_read(handle, bytes.data(), bytes.size(), I2C_MASTER_LAST_NACK));
handle.write_byte(i2c_addr.get_value() << 1 | I2C_MASTER_READ);
handle.read(bytes);
}
void I2CComposed::CompTransferNodeRead::process_result(std::vector<std::vector<uint8_t> > &read_results)
@ -159,30 +243,34 @@ void I2CComposed::CompTransferNodeRead::process_result(std::vector<std::vector<u
read_results.push_back(bytes);
}
void I2CComposed::CompTransferNodeWrite::queue_cmd(i2c_cmd_handle_t handle, uint8_t i2c_addr)
void I2CComposed::CompTransferNodeWrite::queue_cmd(I2CCommandLink &handle, I2CAddress i2c_addr)
{
I2C_CHECK_THROW(i2c_master_write_byte(handle, i2c_addr << 1 | I2C_MASTER_WRITE, true));
I2C_CHECK_THROW(i2c_master_write(handle, bytes.data(), bytes.size(), true));
handle.write_byte(i2c_addr.get_value() << 1 | I2C_MASTER_WRITE);
handle.write(bytes);
}
void I2CComposed::add_read(size_t size)
{
if (!size) throw I2CException(ESP_ERR_INVALID_ARG);
if (!size) {
throw I2CException(ESP_ERR_INVALID_ARG);
}
transfer_list.push_back(make_shared<CompTransferNodeRead>(size));
}
void I2CComposed::add_write(std::vector<uint8_t> bytes)
{
if (bytes.empty()) throw I2CException(ESP_ERR_INVALID_ARG);
if (bytes.empty()) {
throw I2CException(ESP_ERR_INVALID_ARG);
}
transfer_list.push_back(make_shared<CompTransferNodeWrite>(bytes));
}
void I2CComposed::queue_cmd(i2c_cmd_handle_t handle, uint8_t i2c_addr)
void I2CComposed::queue_cmd(I2CCommandLink &handle, I2CAddress i2c_addr)
{
for (auto it = transfer_list.begin(); it != transfer_list.end(); it++) {
I2C_CHECK_THROW(i2c_master_start(handle));
handle.start();
(*it)->queue_cmd(handle, i2c_addr);
}
}

Wyświetl plik

@ -17,11 +17,21 @@
#include <list>
#include <future>
#include "driver/i2c.h"
#include "sdkconfig.h"
#include "esp_exception.hpp"
#include "system_cxx.hpp"
#include "gpio_cxx.hpp"
namespace idf {
/**
* @brief Check if the provided numerical value is a valid I2C address.
*
* @param addr raw number to be checked.
* @return ESP_OK if \c addr is a valid I2C address, otherwise ESP_ERR_INVALID_ARG.
*/
esp_err_t check_i2c_addr(uint32_t addr) noexcept;
struct I2CException : public ESPException {
I2CException(esp_err_t error);
};
@ -30,22 +40,162 @@ struct I2CTransferException : public I2CException {
I2CTransferException(esp_err_t error);
};
/**
* @brief Represents a valid SDA signal pin number.
*/
class SDA_type;
using SDA_GPIO = GPIONumBase<class SDA_type>;
/**
* @brief Represents a valid SCL signal pin number.
*/
class SCL_type;
using SCL_GPIO = GPIONumBase<class SCL_type>;
/**
* @brief Valid representation of I2C number.
*
* A chip can have multiple I2C interfaces, each identified by a bus number, subsequently called I2C number.
* Instances of this class are guaranteed to always contain a valid I2C number.
*/
class I2CNumber : public StrongValueComparable<uint32_t> {
/**
* Construct a valid representation of the I2C number.
*
* This constructor is private because the it can only be accessed but the static creation methods below.
* This guarantees that an instance of I2CNumber always carries a valid number.
*/
constexpr explicit I2CNumber(uint32_t number) : StrongValueComparable<uint32_t>(number) { }
public:
/**
* @brief create an I2C number representing the first I2C bus of the chip.
*/
constexpr static I2CNumber I2C0() {
return I2CNumber(0);
}
#if CONFIG_SOC_I2C_NUM == 2
/**
* @brief create an I2C number representing the second I2C bus of the chip.
*/
constexpr static I2CNumber I2C1() {
return I2CNumber(1);
}
#endif
/**
* Retrieves the valid numerical representation of the I2C number.
*/
uint32_t get_num();
};
/**
* @brief Valid representation of I2C address.
*
* Instances of this class are guaranteed to always contain a valid I2C address.
*/
class I2CAddress : public StrongValueComparable<uint8_t> {
public:
/**
*
*/
explicit I2CAddress(uint8_t addr);
/**
* Retrieves the valid numerical representation of the I2C adress.
*/
uint8_t get_addr();
};
/**
* @brief Low-level I2C transaction descriptor
*
* This class records and decribes a low-level transaction. Users use the methods (except \c execute_transfer)
* to record the transaction. Afterwards, the transaction will be executed by calling \c execute_transfer,
* which blocks until the transaction is finished.
*
* @note This is a low-level class, which only exists due to the underlying I2C driver. All data referenced in
* read and write calls must not be changed and must stay allocated until at least \c execute_transfer
* has finished.
*/
class I2CCommandLink {
public:
/**
* @brief Allocate and create the transaction descriptor.
*/
I2CCommandLink();
/**
* @brief Delete the transaction descriptor, de-allocate all resources.
*/
~I2CCommandLink();
I2CCommandLink(const I2CCommandLink&) = delete;
I2CCommandLink operator=(const I2CCommandLink&) = delete;
/**
* @brief Record a start signal on the I2C bus.
*/
void start();
/**
* @brief Record a write of the vector \c bytes on the I2C bus.
*
* @param[in] bytes The data to be written. Must stay allocated until execute_transfer has finished or
* destructor of this class has been called.
* @param[in] expect_ack If acknowledgement shall be requested after each written byte, pass true,
* otherwise false.
*/
void write(const std::vector<uint8_t> &bytes, bool expect_ack = true);
/**
* @brief Record a one-byte-write on the I2C bus.
*
* @param[in] byte The data to be written. No restrictions apply.
* @param[in] expect_ack If acknowledgement shall be requested after writing the byte, pass true,
* otherwise false.
*/
void write_byte(uint8_t byte, bool expect_ack = true);
/**
* @brief Record a read of the size of vector \c bytes on the I2C bus.
*
* @param[in] bytes Vector with the size of the data to be read (in bytes). Must stay allocated until
* execute_transfer has finished or destructor of this class has been called.
* @param[in] expect_ack If acknowledgement shall be requested after each written byte, pass true,
* otherwise false.
*/
void read(std::vector<uint8_t> &bytes);
/**
* @brief Record a stop command on the I2C bus.
*/
void stop();
/**
* @brief Execute the transaction and wait until it has finished.
*
* This method will issue the transaction with the operations in the order in which they have been recorded
* before.
*
* @param i2c_num I2C bus number on the chip.
* @param driver_timeout Timeout for this transaction.
*/
void execute_transfer(I2CNumber i2c_num, std::chrono::milliseconds driver_timeout);
private:
/**
* @brief Internal driver data.
*/
void *handle;
};
/**
* Superclass for all transfer objects which are accepted by \c I2CMaster::transfer().
*/
template<typename TReturn>
class I2CTransfer {
protected:
/**
* Wrapper around i2c_cmd_handle_t, makes it exception-safe.
*/
struct I2CCommandLink {
I2CCommandLink();
~I2CCommandLink();
i2c_cmd_handle_t handle;
};
public:
/**
* Helper typedef to facilitate type resolution during calls to I2CMaster::transfer().
@ -55,7 +205,7 @@ public:
/**
* @param driver_timeout The timeout used for calls like i2c_master_cmd_begin() to the underlying driver.
*/
I2CTransfer(std::chrono::milliseconds driver_timeout = std::chrono::milliseconds(1000));
I2CTransfer(std::chrono::milliseconds driver_timeout_arg = std::chrono::milliseconds(1000));
virtual ~I2CTransfer() { }
@ -74,7 +224,7 @@ public:
*
* @throws I2CException for any particular I2C error
*/
TReturn do_transfer(i2c_port_t i2c_num, uint8_t i2c_addr);
TReturn do_transfer(I2CNumber i2c_num, I2CAddress i2c_addr);
protected:
/**
@ -88,7 +238,7 @@ protected:
*
* @throw I2CException
*/
virtual void queue_cmd(i2c_cmd_handle_t handle, uint8_t i2c_addr) = 0;
virtual void queue_cmd(I2CCommandLink &handle, I2CAddress i2c_addr) = 0;
/**
* Implementation of whatever neccessary action after successfully sending the I2C command.
@ -101,7 +251,7 @@ protected:
/**
* For some calls to the underlying driver (e.g. \c i2c_master_cmd_begin() ), this general timeout will be passed.
*/
const TickType_t driver_timeout;
std::chrono::milliseconds driver_timeout;
};
/**
@ -116,7 +266,7 @@ public:
*
* @param i2c_number The I2C port number.
*/
I2CBus(i2c_port_t i2c_number);
explicit I2CBus(I2CNumber i2c_number);
/**
* @brief uninstall the bus driver.
@ -126,7 +276,7 @@ public:
/**
* The I2C port number.
*/
const i2c_port_t i2c_num;
const I2CNumber i2c_num;
};
/**
@ -154,10 +304,10 @@ public:
*
* @throws I2CException with the corrsponding esp_err_t return value if something goes wrong
*/
I2CMaster(i2c_port_t i2c_number,
int scl_gpio,
int sda_gpio,
uint32_t clock_speed,
explicit I2CMaster(I2CNumber i2c_number,
SCL_GPIO scl_gpio,
SDA_GPIO sda_gpio,
Frequency clock_speed,
bool scl_pullup = true,
bool sda_pullup = true);
@ -195,7 +345,7 @@ public:
* @throws std::exception for failures in libstdc++
*/
template<typename TransferT>
std::future<typename TransferT::TransferReturnT> transfer(std::shared_ptr<TransferT> xfer, uint8_t i2c_addr);
std::future<typename TransferT::TransferReturnT> transfer(I2CAddress i2c_addr, std::shared_ptr<TransferT> xfer);
/**
* Do a synchronous write.
@ -209,7 +359,7 @@ public:
* @throws I2CException with the corrsponding esp_err_t return value if something goes wrong
* @throws std::exception for failures in libstdc++
*/
void sync_write(uint8_t i2c_addr, const std::vector<uint8_t> &data);
void sync_write(I2CAddress i2c_addr, const std::vector<uint8_t> &data);
/**
* Do a synchronous read.
@ -226,7 +376,7 @@ public:
* @throws I2CException with the corrsponding esp_err_t return value if something goes wrong
* @throws std::exception for failures in libstdc++
*/
std::vector<uint8_t> sync_read(uint8_t i2c_addr, size_t n_bytes);
std::vector<uint8_t> sync_read(I2CAddress i2c_addr, size_t n_bytes);
/**
* Do a simple synchronous write-read transfer.
@ -245,7 +395,7 @@ public:
* @throws I2CException with the corrsponding esp_err_t return value if something goes wrong
* @throws std::exception for failures in libstdc++
*/
std::vector<uint8_t> sync_transfer(uint8_t i2c_addr,
std::vector<uint8_t> sync_transfer(I2CAddress i2c_addr,
const std::vector<uint8_t> &write_data,
size_t read_n_bytes);
};
@ -273,10 +423,10 @@ public:
*
* @throws
*/
I2CSlave(i2c_port_t i2c_number,
int scl_gpio,
int sda_gpio,
uint8_t slave_addr,
I2CSlave(I2CNumber i2c_number,
SCL_GPIO scl_gpio,
SDA_GPIO sda_gpio,
I2CAddress slave_addr,
size_t rx_buf_len,
size_t tx_buf_len,
bool scl_pullup = true,
@ -322,7 +472,7 @@ protected:
* @param handle The initialized I2C command handle.
* @param i2c_addr The I2C address of the slave.
*/
void queue_cmd(i2c_cmd_handle_t handle, uint8_t i2c_addr) override;
void queue_cmd(I2CCommandLink &handle, I2CAddress i2c_addr) override;
/**
* Set the value of the promise to unblock any callers waiting on it.
@ -356,7 +506,7 @@ protected:
* @param handle The initialized I2C command handle.
* @param i2c_addr The I2C address of the slave.
*/
void queue_cmd(i2c_cmd_handle_t handle, uint8_t i2c_addr) override;
void queue_cmd(I2CCommandLink &handle, I2CAddress i2c_addr) override;
/**
* Set the return value of the promise to unblock any callers waiting on it.
@ -401,7 +551,7 @@ protected:
* @param handle The initialized I2C command handle.
* @param i2c_addr The I2C address of the slave.
*/
void queue_cmd(i2c_cmd_handle_t handle, uint8_t i2c_addr) override;
void queue_cmd(I2CCommandLink &handle, I2CAddress i2c_addr) override;
/**
* Creates the vector with the vectors from all reads.
@ -412,14 +562,14 @@ private:
class CompTransferNode {
public:
virtual ~CompTransferNode() { }
virtual void queue_cmd(i2c_cmd_handle_t handle, uint8_t i2c_addr) = 0;
virtual void queue_cmd(I2CCommandLink &handle, I2CAddress i2c_addr) = 0;
virtual void process_result(std::vector<std::vector<uint8_t> > &read_results) { }
};
class CompTransferNodeRead : public CompTransferNode {
public:
CompTransferNodeRead(size_t size) : bytes(size) { }
void queue_cmd(i2c_cmd_handle_t handle, uint8_t i2c_addr) override;
void queue_cmd(I2CCommandLink &handle, I2CAddress i2c_addr) override;
void process_result(std::vector<std::vector<uint8_t> > &read_results) override;
private:
@ -429,7 +579,7 @@ private:
class CompTransferNodeWrite : public CompTransferNode {
public:
CompTransferNodeWrite(std::vector<uint8_t> bytes) : bytes(bytes) { }
void queue_cmd(i2c_cmd_handle_t handle, uint8_t i2c_addr) override;
void queue_cmd(I2CCommandLink &handle, I2CAddress i2c_addr) override;
private:
std::vector<uint8_t> bytes;
};
@ -441,44 +591,29 @@ private:
};
template<typename TReturn>
I2CTransfer<TReturn>::I2CTransfer(std::chrono::milliseconds driver_timeout)
: driver_timeout(driver_timeout.count()) { }
I2CTransfer<TReturn>::I2CTransfer(std::chrono::milliseconds driver_timeout_arg)
: driver_timeout(driver_timeout_arg) { }
template<typename TReturn>
I2CTransfer<TReturn>::I2CCommandLink::I2CCommandLink()
{
handle = i2c_cmd_link_create();
if (!handle) {
throw I2CException(ESP_ERR_NO_MEM);
}
}
template<typename TReturn>
I2CTransfer<TReturn>::I2CCommandLink::~I2CCommandLink()
{
i2c_cmd_link_delete(handle);
}
template<typename TReturn>
TReturn I2CTransfer<TReturn>::do_transfer(i2c_port_t i2c_num, uint8_t i2c_addr)
TReturn I2CTransfer<TReturn>::do_transfer(I2CNumber i2c_num, I2CAddress i2c_addr)
{
I2CCommandLink cmd_link;
queue_cmd(cmd_link.handle, i2c_addr);
queue_cmd(cmd_link, i2c_addr);
CHECK_THROW_SPECIFIC(i2c_master_stop(cmd_link.handle), I2CException);
cmd_link.stop();
CHECK_THROW_SPECIFIC(i2c_master_cmd_begin(i2c_num, cmd_link.handle, driver_timeout / portTICK_RATE_MS), I2CTransferException);
cmd_link.execute_transfer(i2c_num, driver_timeout);
return process_result();
}
template<typename TransferT>
std::future<typename TransferT::TransferReturnT> I2CMaster::transfer(std::shared_ptr<TransferT> xfer, uint8_t i2c_addr)
std::future<typename TransferT::TransferReturnT> I2CMaster::transfer(I2CAddress i2c_addr, std::shared_ptr<TransferT> xfer)
{
if (!xfer) throw I2CException(ESP_ERR_INVALID_ARG);
return std::async(std::launch::async, [this](std::shared_ptr<TransferT> xfer, uint8_t i2c_addr) {
return std::async(std::launch::async, [this](std::shared_ptr<TransferT> xfer, I2CAddress i2c_addr) {
return xfer->do_transfer(i2c_num, i2c_addr);
}, xfer, i2c_addr);
}

Wyświetl plik

@ -28,7 +28,7 @@
template<typename ValueT>
class StrongValue {
protected:
StrongValue(ValueT value_arg) : value(value_arg) { }
constexpr StrongValue(ValueT value_arg) : value(value_arg) { }
ValueT get_value() const {
return value;
@ -44,7 +44,7 @@ private:
template<typename ValueT>
class StrongValueComparable : public StrongValue<ValueT> {
protected:
StrongValueComparable(ValueT value_arg) : StrongValue<ValueT>(value_arg) { }
constexpr StrongValueComparable(ValueT value_arg) : StrongValue<ValueT>(value_arg) { }
public:
using StrongValue<ValueT>::get_value;

Wyświetl plik

@ -14,16 +14,16 @@
#ifdef __cpp_exceptions
#include "i2c_cxx.hpp"
#include "driver/i2c.h"
using namespace std;
using namespace idf;
#define TAG "I2C Test"
#define ADDR 0x47
#define MAGIC_TEST_NUMBER 47
#define I2C_SLAVE_NUM I2C_NUM_0 /*!<I2C port number for slave dev */
static constexpr I2CNumber I2C_SLAVE_NUM(I2CNumber::I2C0()); /*!<I2C port number for slave dev */
#if CONFIG_IDF_TARGET_ESP32C3
#define I2C_SLAVE_SCL_IO 5 /*!<gpio number for i2c slave clock */
#define I2C_SLAVE_SDA_IO 6 /*!<gpio number for i2c slave data */
@ -33,40 +33,33 @@ using namespace idf;
#endif
#if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP8684
#define I2C_MASTER_NUM I2C_NUM_0 /*!< I2C port number for master dev */
static constexpr I2CNumber I2C_MASTER_NUM(I2CNumber::I2C0()); /*!< I2C port number for master dev */
#define I2C_MASTER_SCL_IO 5 /*!<gpio number for i2c master clock */
#define I2C_MASTER_SDA_IO 6 /*!<gpio number for i2c master data */
#else
#define I2C_MASTER_NUM I2C_NUM_1 /*!< I2C port number for master dev */
static constexpr I2CNumber I2C_MASTER_NUM(I2CNumber::I2C1()); /*!< I2C port number for master dev */
#define I2C_MASTER_SCL_IO 19 /*!< gpio number for I2C master clock */
#define I2C_MASTER_SDA_IO 18 /*!< gpio number for I2C master data */
#endif
struct MasterFixture {
MasterFixture(const vector<uint8_t> &data_arg = {47u}) :
master(new I2CMaster(I2C_MASTER_NUM, I2C_MASTER_SCL_IO, I2C_MASTER_SDA_IO, 400000)),
master(new I2CMaster(I2CNumber(I2C_MASTER_NUM),
SCL_GPIO(I2C_MASTER_SCL_IO),
SDA_GPIO(I2C_MASTER_SDA_IO),
Frequency(400000))),
data(data_arg) { }
std::shared_ptr<I2CMaster> master;
vector<uint8_t> data;
};
TEST_CASE("I2Transfer timeout", "[cxx i2c][leaks=300]")
{
std::vector<uint8_t> data = {MAGIC_TEST_NUMBER};
// I2CWrite directly inherits from I2CTransfer; it's representative for I2CRead and I2CComposed, too.
I2CWrite writer(data, chrono::milliseconds(50));
TEST_THROW(writer.do_transfer(I2C_MASTER_NUM, ADDR), I2CTransferException);
}
// TODO The I2C driver tests are disabled, so disable them here, too. Probably due to no runners.
#if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2, ESP32S3)
static void i2c_slave_read_raw_byte(void)
{
I2CSlave slave(I2C_SLAVE_NUM, I2C_SLAVE_SCL_IO, I2C_SLAVE_SDA_IO, ADDR, 512, 512);
I2CSlave slave(I2CNumber(I2C_SLAVE_NUM), SCL_GPIO(I2C_SLAVE_SCL_IO), SDA_GPIO(I2C_SLAVE_SDA_IO), I2CAddress(ADDR), 512, 512);
uint8_t buffer = 0;
unity_send_signal("slave init");
@ -79,7 +72,7 @@ static void i2c_slave_read_raw_byte(void)
static void i2c_slave_write_raw_byte(void)
{
I2CSlave slave(I2C_SLAVE_NUM, I2C_SLAVE_SCL_IO, I2C_SLAVE_SDA_IO, ADDR, 512, 512);
I2CSlave slave(I2CNumber(I2C_SLAVE_NUM), SCL_GPIO(I2C_SLAVE_SCL_IO), SDA_GPIO(I2C_SLAVE_SDA_IO), I2CAddress(ADDR), 512, 512);
uint8_t WRITE_BUFFER = MAGIC_TEST_NUMBER;
unity_wait_for_signal("master init");
@ -95,7 +88,7 @@ static void i2c_slave_write_raw_byte(void)
static void i2c_slave_read_multiple_raw_bytes(void)
{
I2CSlave slave(I2C_SLAVE_NUM, I2C_SLAVE_SCL_IO, I2C_SLAVE_SDA_IO, ADDR, 512, 512);
I2CSlave slave(I2CNumber(I2C_SLAVE_NUM), SCL_GPIO(I2C_SLAVE_SCL_IO), SDA_GPIO(I2C_SLAVE_SDA_IO), I2CAddress(ADDR), 512, 512);
uint8_t buffer [8] = {};
unity_send_signal("slave init");
@ -110,7 +103,7 @@ static void i2c_slave_read_multiple_raw_bytes(void)
static void i2c_slave_write_multiple_raw_bytes(void)
{
I2CSlave slave(I2C_SLAVE_NUM, I2C_SLAVE_SCL_IO, I2C_SLAVE_SDA_IO, ADDR, 512, 512);
I2CSlave slave(I2CNumber(I2C_SLAVE_NUM), SCL_GPIO(I2C_SLAVE_SCL_IO), SDA_GPIO(I2C_SLAVE_SDA_IO), I2CAddress(ADDR), 512, 512);
uint8_t WRITE_BUFFER [8] = {0, 1, 2, 3, 4, 5, 6, 7};
unity_wait_for_signal("master init");
@ -123,7 +116,7 @@ static void i2c_slave_write_multiple_raw_bytes(void)
static void i2c_slave_composed_trans(void)
{
I2CSlave slave(I2C_SLAVE_NUM, I2C_SLAVE_SCL_IO, I2C_SLAVE_SDA_IO, ADDR, 512, 512);
I2CSlave slave(I2CNumber(I2C_SLAVE_NUM), SCL_GPIO(I2C_SLAVE_SCL_IO), SDA_GPIO(I2C_SLAVE_SDA_IO), I2CAddress(ADDR), 512, 512);
size_t BUF_SIZE = 2;
const uint8_t SLAVE_WRITE_BUFFER [BUF_SIZE] = {0xde, 0xad};
uint8_t slave_read_buffer = 0;
@ -139,41 +132,6 @@ static void i2c_slave_composed_trans(void)
TEST_ASSERT_EQUAL(MAGIC_TEST_NUMBER, slave_read_buffer);
}
static void i2c_I2CRead(void)
{
// here only to install/uninstall driver
MasterFixture fix;
unity_send_signal("master init");
unity_wait_for_signal("slave write");
I2CRead reader(1);
vector<uint8_t> data = reader.do_transfer(I2C_MASTER_NUM, ADDR);
unity_send_signal("master read done");
TEST_ASSERT_EQUAL(1, data.size());
TEST_ASSERT_EQUAL(MAGIC_TEST_NUMBER, data[0]);
}
TEST_CASE_MULTIPLE_DEVICES("I2CRead do_transfer", "[cxx i2c][test_env=UT_T2_I2C][timeout=150]",
i2c_I2CRead, i2c_slave_write_raw_byte);
static void i2c_I2CWrite(void)
{
MasterFixture fix;
I2CWrite writer(fix.data);
unity_wait_for_signal("slave init");
writer.do_transfer(I2C_MASTER_NUM, ADDR);
unity_send_signal("master write");
}
TEST_CASE_MULTIPLE_DEVICES("I2CWrite do_transfer", "[cxx i2c][test_env=UT_T2_I2C][timeout=150]",
i2c_I2CWrite, i2c_slave_read_raw_byte);
static void i2c_master_read_raw_byte(void)
{
MasterFixture fix;
@ -183,7 +141,7 @@ static void i2c_master_read_raw_byte(void)
std::shared_ptr<I2CRead> reader(new I2CRead(1));
future<vector<uint8_t> > fut = fix.master->transfer(reader, ADDR);
future<vector<uint8_t> > fut = fix.master->transfer(I2CAddress(ADDR), reader);
vector<uint8_t> data;
data = fut.get();
@ -203,7 +161,7 @@ static void i2c_master_write_raw_byte(void)
unity_wait_for_signal("slave init");
std::shared_ptr<I2CWrite> writer(new I2CWrite(fix.data));
future<void> fut = fix.master->transfer(writer, ADDR);
future<void> fut = fix.master->transfer(I2CAddress(ADDR), writer);
fut.get();
unity_send_signal("master write");
@ -221,7 +179,7 @@ static void i2c_master_read_multiple_raw_bytes(void)
std::shared_ptr<I2CRead> reader(new I2CRead(8));
future<vector<uint8_t> > fut = fix.master->transfer(reader, ADDR);
future<vector<uint8_t> > fut = fix.master->transfer(I2CAddress(ADDR), reader);
vector<uint8_t> data = fut.get();
unity_send_signal("master read done");
@ -242,7 +200,7 @@ static void i2c_master_write_multiple_raw_bytes(void)
unity_wait_for_signal("slave init");
std::shared_ptr<I2CWrite> writer(new I2CWrite(fix.data));
future<void> fut = fix.master->transfer(writer, ADDR);
future<void> fut = fix.master->transfer(I2CAddress(ADDR), writer);
fut.get();
unity_send_signal("master write");
@ -251,38 +209,6 @@ static void i2c_master_write_multiple_raw_bytes(void)
TEST_CASE_MULTIPLE_DEVICES("I2CMaster write multiple bytes", "[cxx i2c][test_env=UT_T2_I2C][timeout=150]",
i2c_master_write_multiple_raw_bytes, i2c_slave_read_multiple_raw_bytes);
static void i2c_master_sync_read(void)
{
MasterFixture fix;
unity_send_signal("master init");
unity_wait_for_signal("slave write");
vector<uint8_t> data = fix.master->sync_read(ADDR, 1);
unity_send_signal("master read done");
TEST_ASSERT_EQUAL(1, data.size());
TEST_ASSERT_EQUAL(MAGIC_TEST_NUMBER, data[0]);
}
TEST_CASE_MULTIPLE_DEVICES("I2CMaster sync read", "[cxx i2c][test_env=UT_T2_I2C][timeout=150]",
i2c_master_sync_read, i2c_slave_write_raw_byte);
static void i2c_master_sync_write(void)
{
MasterFixture fix;
unity_wait_for_signal("slave init");
fix.master->sync_write(ADDR, fix.data);
unity_send_signal("master write");
}
TEST_CASE_MULTIPLE_DEVICES("I2CMaster sync write", "[cxx i2c][test_env=UT_T2_I2C][timeout=150]",
i2c_master_sync_write, i2c_slave_read_raw_byte);
static void i2c_master_sync_transfer(void)
{
MasterFixture fix;
@ -291,7 +217,7 @@ static void i2c_master_sync_transfer(void)
unity_wait_for_signal("slave init");
vector<uint8_t> read_data = fix.master->sync_transfer(ADDR, fix.data, READ_SIZE);
vector<uint8_t> read_data = fix.master->sync_transfer(I2CAddress(ADDR), fix.data, READ_SIZE);
unity_send_signal("master transfer");
TEST_ASSERT_EQUAL(READ_SIZE, read_data.size());
@ -315,7 +241,7 @@ static void i2c_master_composed_trans(void)
unity_wait_for_signal("slave init");
future<vector<vector<uint8_t> > > result = fix.master->transfer(composed_transfer, ADDR);
future<vector<vector<uint8_t> > > result = fix.master->transfer(I2CAddress(ADDR), composed_transfer);
unity_send_signal("master transfer");
@ -331,183 +257,5 @@ static void i2c_master_composed_trans(void)
TEST_CASE_MULTIPLE_DEVICES("I2CMaster Composed transfer", "[cxx i2c][test_env=UT_T2_I2C][timeout=150]",
i2c_master_composed_trans, i2c_slave_composed_trans);
static void i2c_slave_write_multiple_raw_bytes_twice(void)
{
I2CSlave slave(I2C_SLAVE_NUM, I2C_SLAVE_SCL_IO, I2C_SLAVE_SDA_IO, ADDR, 512, 512);
const size_t BUF_SIZE = 8;
uint8_t WRITE_BUFFER [BUF_SIZE] = {0, 1, 2, 3, 4, 5, 6, 7};
unity_wait_for_signal("master init");
TEST_ASSERT_EQUAL(BUF_SIZE, slave.write_raw(WRITE_BUFFER, BUF_SIZE, chrono::milliseconds(1000)));
TEST_ASSERT_EQUAL(BUF_SIZE, slave.write_raw(WRITE_BUFFER, BUF_SIZE, chrono::milliseconds(1000)));
unity_send_signal("slave write");
unity_wait_for_signal("master read done");
}
static void i2c_master_reuse_read_multiple_raw_bytes(void)
{
MasterFixture fix;
unity_send_signal("master init");
unity_wait_for_signal("slave write");
const size_t BUF_SIZE = 8;
#if !CONFIG_IDF_TARGET_ESP32C3
std::shared_ptr<I2CRead> reader(new I2CRead(BUF_SIZE));
future<vector<uint8_t> > fut;
fut = fix.master->transfer(reader, ADDR);
vector<uint8_t> data1 = fut.get();
fut = fix.master->transfer(reader, ADDR);
vector<uint8_t> data2 = fut.get();
unity_send_signal("master read done");
TEST_ASSERT_EQUAL(BUF_SIZE, data1.size());
TEST_ASSERT_EQUAL(BUF_SIZE, data2.size());
for (int i = 0; i < BUF_SIZE; i++) {
TEST_ASSERT_EQUAL(i, data1[i]);
TEST_ASSERT_EQUAL(i, data2[i]);
}
#else // Cannot read twice because the `prefetch` behaviour on C3.
std::shared_ptr<I2CRead> reader(new I2CRead(BUF_SIZE * 2));
future<vector<uint8_t> > fut;
fut = fix.master->transfer(reader, ADDR);
vector<uint8_t> data = fut.get();
unity_send_signal("master read done");
TEST_ASSERT_EQUAL(BUF_SIZE * 2, data.size());
for (int i = 0; i < BUF_SIZE; i++) {
TEST_ASSERT_EQUAL((i % BUF_SIZE), data[i]);
}
#endif // !CONFIG_IDF_TARGET_ESP32C3
}
TEST_CASE_MULTIPLE_DEVICES("I2CMaster reuse read multiple bytes", "[cxx i2c][test_env=UT_T2_I2C][timeout=150]",
i2c_master_reuse_read_multiple_raw_bytes, i2c_slave_write_multiple_raw_bytes_twice);
static void i2c_slave_read_multiple_raw_bytes_twice(void)
{
I2CSlave slave(I2C_SLAVE_NUM, I2C_SLAVE_SCL_IO, I2C_SLAVE_SDA_IO, ADDR, 512, 512);
const size_t BUF_SIZE = 8;
uint8_t buffer1 [BUF_SIZE] = {};
uint8_t buffer2 [BUF_SIZE] = {};
unity_send_signal("slave init");
unity_wait_for_signal("master write");
TEST_ASSERT_EQUAL(BUF_SIZE, slave.read_raw(buffer1, BUF_SIZE, chrono::milliseconds(1000)));
TEST_ASSERT_EQUAL(BUF_SIZE, slave.read_raw(buffer2, BUF_SIZE, chrono::milliseconds(1000)));
for (int i = 0; i < BUF_SIZE; i++) {
TEST_ASSERT_EQUAL(i, buffer1[i]);
TEST_ASSERT_EQUAL(i, buffer2[i]);
}
}
static void i2c_master_reuse_write_multiple_raw_bytes(void)
{
MasterFixture fix({0, 1, 2, 3, 4, 5, 6, 7});
unity_wait_for_signal("slave init");
std::shared_ptr<I2CWrite> writer(new I2CWrite(fix.data));
future<void> fut;
fut = fix.master->transfer(writer, ADDR);
fut.get();
fut = fix.master->transfer(writer, ADDR);
fut.get();
unity_send_signal("master write");
}
TEST_CASE_MULTIPLE_DEVICES("I2CMaster reuse write multiple bytes", "[cxx i2c][test_env=UT_T2_I2C][timeout=150]",
i2c_master_reuse_write_multiple_raw_bytes, i2c_slave_read_multiple_raw_bytes_twice);
static void i2c_slave_composed_trans_twice(void)
{
I2CSlave slave(I2C_SLAVE_NUM, I2C_SLAVE_SCL_IO, I2C_SLAVE_SDA_IO, ADDR, 512, 512);
size_t BUF_SIZE = 2;
const uint8_t SLAVE_WRITE_BUFFER1 [BUF_SIZE] = {0xde, 0xad};
const uint8_t SLAVE_WRITE_BUFFER2 [BUF_SIZE] = {0xbe, 0xef};
uint8_t slave_read_buffer = 0;
unity_send_signal("slave init");
TEST_ASSERT_EQUAL(BUF_SIZE, slave.write_raw(SLAVE_WRITE_BUFFER1, BUF_SIZE, chrono::milliseconds(1000)));
TEST_ASSERT_EQUAL(BUF_SIZE, slave.write_raw(SLAVE_WRITE_BUFFER2, BUF_SIZE, chrono::milliseconds(1000)));
unity_wait_for_signal("master transfer");
TEST_ASSERT_EQUAL(1, slave.read_raw(&slave_read_buffer, 1, chrono::milliseconds(1000)));
TEST_ASSERT_EQUAL(MAGIC_TEST_NUMBER, slave_read_buffer);
#if !CONFIG_IDF_TARGET_ESP32C3
TEST_ASSERT_EQUAL(1, slave.read_raw(&slave_read_buffer, 1, chrono::milliseconds(1000)));
TEST_ASSERT_EQUAL(MAGIC_TEST_NUMBER, slave_read_buffer);
#endif // !CONFIG_IDF_TARGET_ESP32C3
}
static void i2c_master_reuse_composed_trans(void)
{
MasterFixture fix;
size_t BUF_SIZE = 2;
const uint8_t SLAVE_WRITE_BUFFER1 [BUF_SIZE] = {0xde, 0xad};
const uint8_t SLAVE_WRITE_BUFFER2 [BUF_SIZE] = {0xbe, 0xef};
std::shared_ptr<I2CComposed> composed_transfer(new I2CComposed);
composed_transfer->add_write({47u});
#if !CONFIG_IDF_TARGET_ESP32C3
composed_transfer->add_read(BUF_SIZE);
unity_wait_for_signal("slave init");
vector<vector<uint8_t> > read_data1 = fix.master->transfer(composed_transfer, ADDR).get();
vector<vector<uint8_t> > read_data2 = fix.master->transfer(composed_transfer, ADDR).get();
unity_send_signal("master transfer");
TEST_ASSERT_EQUAL(1, read_data1.size());
TEST_ASSERT_EQUAL(2, read_data1[0].size());
TEST_ASSERT_EQUAL(1, read_data2.size());
TEST_ASSERT_EQUAL(2, read_data2[0].size());
for (int i = 0; i < BUF_SIZE; i++) {
TEST_ASSERT_EQUAL(SLAVE_WRITE_BUFFER1[i], read_data1[0][i]);
TEST_ASSERT_EQUAL(SLAVE_WRITE_BUFFER2[i], read_data2[0][i]);
}
#else // Cannot read twice because the `prefetch` behaviour on C3.
composed_transfer->add_read(BUF_SIZE * 2);
unity_wait_for_signal("slave init");
vector<vector<uint8_t> > read_data = fix.master->transfer(composed_transfer, ADDR).get();
unity_send_signal("master transfer");
TEST_ASSERT_EQUAL(1, read_data.size());
TEST_ASSERT_EQUAL(4, read_data[0].size());
for (int i = 0; i < BUF_SIZE; i++) {
TEST_ASSERT_EQUAL(SLAVE_WRITE_BUFFER1[i], read_data[0][i]);
}
for (int i = BUF_SIZE; i < BUF_SIZE * 2; i++) {
TEST_ASSERT_EQUAL(SLAVE_WRITE_BUFFER2[i - BUF_SIZE], read_data[0][i]);
}
#endif //!CONFIG_IDF_TARGET_ESP32C3
}
TEST_CASE_MULTIPLE_DEVICES("I2CMaster reuse composed transfer", "[cxx i2c][test_env=UT_T2_I2C][timeout=150]",
i2c_master_reuse_composed_trans, i2c_slave_composed_trans_twice);
#endif //TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2, ESP32S3)
#endif // __cpp_exceptions

Wyświetl plik

@ -1,7 +1,7 @@
/*
* SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*
* MPU9250 I2C Sensor C++ Example
*
@ -12,7 +12,6 @@
* CONDITIONS OF ANY KIND, either express or implied.
*/
#include <iostream>
#include "esp_log.h"
#include "i2c_cxx.hpp"
@ -21,19 +20,22 @@ using namespace idf;
static const char *TAG = "i2c-cxx-simple-example";
#define I2C_MASTER_NUM 0 /*!< I2C master i2c port number, the number of i2c peripheral interfaces
available will depend on the chip */
#define I2C_MASTER_SCL_IO CONFIG_I2C_MASTER_SCL /*!< GPIO number used for I2C master clock */
#define I2C_MASTER_SDA_IO CONFIG_I2C_MASTER_SDA /*!< GPIO number used for I2C master data */
constexpr I2CNumber I2C_MASTER_NUM(I2CNumber::I2C0()); /*!< I2C master i2c port number, the number of i2c peripheral
interfaces available will depend on the chip */
#define I2C_MASTER_SCL_IO SCL_GPIO(CONFIG_I2C_MASTER_SCL) /*!< GPIO number used for I2C master clock */
#define I2C_MASTER_SDA_IO SDA_GPIO(CONFIG_I2C_MASTER_SDA) /*!< GPIO number used for I2C master data */
#define MPU9250_SENSOR_ADDR 0x68 /*!< Slave address of the MPU9250 sensor */
#define MPU9250_WHO_AM_I_REG_ADDR 0x75 /*!< Register addresses of the "who am I" register */
#define MPU9250_SENSOR_ADDR I2CAddress(0x68) /*!< Slave address of the MPU9250 sensor */
constexpr uint8_t MPU9250_WHO_AM_I_REG_ADDR = 0x75; /*!< Register addresses of the "who am I" register */
extern "C" void app_main(void)
{
try {
// creating master bus
shared_ptr<I2CMaster> master(new I2CMaster(I2C_MASTER_NUM, I2C_MASTER_SCL_IO, I2C_MASTER_SDA_IO, 400000));
shared_ptr<I2CMaster> master(new I2CMaster(I2C_MASTER_NUM,
I2C_MASTER_SCL_IO,
I2C_MASTER_SDA_IO,
Frequency(400000)));
ESP_LOGI(TAG, "I2C initialized successfully");
// writing the pointer to the WHO_AM_I register to the device
@ -44,9 +46,8 @@ extern "C" void app_main(void)
ESP_LOGI(TAG, "WHO_AM_I = %X", data[0]);
} catch (const I2CException &e) {
cout << "I2C Exception with error: " << e.what();
cout << " (" << e.error<< ")" << endl;
cout << "Couldn't read sensor!" << endl;
ESP_LOGI(TAG, "I2C Exception with error: %s (0x%X)", e.what(), e.error);
ESP_LOGI(TAG, "Couldn't read sensor!");
}
// The I2CMaster object is de-initialized in its destructor when going out of scope.

Wyświetl plik

@ -2656,7 +2656,6 @@ examples/cxx/experimental/experimental_cpp_component/esp_exception.cpp
examples/cxx/experimental/experimental_cpp_component/esp_timer_cxx.cpp
examples/cxx/experimental/experimental_cpp_component/host_test/esp_timer/main/esp_timer_test.cpp
examples/cxx/experimental/experimental_cpp_component/host_test/gpio/main/gpio_cxx_test.cpp
examples/cxx/experimental/experimental_cpp_component/i2c_cxx.cpp
examples/cxx/experimental/experimental_cpp_component/include/esp_event_api.hpp
examples/cxx/experimental/experimental_cpp_component/include/esp_event_cxx.hpp
examples/cxx/experimental/experimental_cpp_component/include/esp_timer_cxx.hpp