2021-10-25 06:58:15 -04:00
|
|
|
/*
|
2022-02-08 04:39:38 -05:00
|
|
|
* SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD
|
2021-10-25 06:58:15 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2020-02-04 13:08:41 -05:00
|
|
|
|
|
|
|
#ifdef __cpp_exceptions
|
|
|
|
|
2021-10-25 06:58:15 -04:00
|
|
|
#include "driver/i2c.h"
|
2020-02-04 13:08:41 -05:00
|
|
|
#include "i2c_cxx.hpp"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
namespace idf {
|
|
|
|
|
|
|
|
#define I2C_CHECK_THROW(err) CHECK_THROW_SPECIFIC((err), I2CException)
|
|
|
|
|
2022-05-24 04:22:49 -04:00
|
|
|
/**
|
|
|
|
* 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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-25 06:58:15 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-02-04 13:08:41 -05:00
|
|
|
I2CException::I2CException(esp_err_t error) : ESPException(error) { }
|
|
|
|
|
|
|
|
I2CTransferException::I2CTransferException(esp_err_t error) : I2CException(error) { }
|
|
|
|
|
2021-10-25 06:58:15 -04:00
|
|
|
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)
|
|
|
|
{
|
2022-05-24 04:22:49 -04:00
|
|
|
esp_err_t err = i2c_master_cmd_begin(i2c_num_to_driver_type(i2c_num), handle, driver_timeout.count() / portTICK_PERIOD_MS);
|
2021-10-25 06:58:15 -04:00
|
|
|
if (err != ESP_OK) {
|
|
|
|
throw I2CTransferException(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
I2CBus::I2CBus(I2CNumber i2c_number) : i2c_num(std::move(i2c_number)) { }
|
2020-02-04 13:08:41 -05:00
|
|
|
|
|
|
|
I2CBus::~I2CBus() { }
|
|
|
|
|
2021-10-25 06:58:15 -04:00
|
|
|
I2CMaster::I2CMaster(I2CNumber i2c_number,
|
|
|
|
SCL_GPIO scl_gpio,
|
|
|
|
SDA_GPIO sda_gpio,
|
|
|
|
Frequency clock_speed,
|
2020-02-04 13:08:41 -05:00
|
|
|
bool scl_pullup,
|
|
|
|
bool sda_pullup)
|
2021-10-25 06:58:15 -04:00
|
|
|
: I2CBus(std::move(i2c_number))
|
2020-02-04 13:08:41 -05:00
|
|
|
{
|
|
|
|
i2c_config_t conf = {};
|
|
|
|
conf.mode = I2C_MODE_MASTER;
|
2021-10-25 06:58:15 -04:00
|
|
|
conf.scl_io_num = scl_gpio.get_num();
|
2020-02-04 13:08:41 -05:00
|
|
|
conf.scl_pullup_en = scl_pullup;
|
2021-10-25 06:58:15 -04:00
|
|
|
conf.sda_io_num = sda_gpio.get_num();
|
2020-02-04 13:08:41 -05:00
|
|
|
conf.sda_pullup_en = sda_pullup;
|
2021-10-25 06:58:15 -04:00
|
|
|
conf.master.clk_speed = clock_speed.get_value();
|
2022-05-24 04:22:49 -04:00
|
|
|
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));
|
2020-02-04 13:08:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
I2CMaster::~I2CMaster()
|
|
|
|
{
|
2022-05-24 04:22:49 -04:00
|
|
|
i2c_driver_delete(i2c_num_to_driver_type(i2c_num));
|
2020-02-04 13:08:41 -05:00
|
|
|
}
|
|
|
|
|
2021-10-25 06:58:15 -04:00
|
|
|
void I2CMaster::sync_write(I2CAddress i2c_addr, const vector<uint8_t> &data)
|
2020-02-04 13:08:41 -05:00
|
|
|
{
|
|
|
|
I2CWrite writer(data);
|
|
|
|
|
|
|
|
writer.do_transfer(i2c_num, i2c_addr);
|
|
|
|
}
|
|
|
|
|
2021-10-25 06:58:15 -04:00
|
|
|
std::vector<uint8_t> I2CMaster::sync_read(I2CAddress i2c_addr, size_t n_bytes)
|
2020-02-04 13:08:41 -05:00
|
|
|
{
|
|
|
|
I2CRead reader(n_bytes);
|
|
|
|
|
|
|
|
return reader.do_transfer(i2c_num, i2c_addr);
|
|
|
|
}
|
|
|
|
|
2021-10-25 06:58:15 -04:00
|
|
|
vector<uint8_t> I2CMaster::sync_transfer(I2CAddress i2c_addr,
|
2020-02-04 13:08:41 -05:00
|
|
|
const std::vector<uint8_t> &write_data,
|
|
|
|
size_t read_n_bytes)
|
|
|
|
{
|
|
|
|
I2CComposed composed_transfer;
|
|
|
|
composed_transfer.add_write(write_data);
|
|
|
|
composed_transfer.add_read(read_n_bytes);
|
|
|
|
|
|
|
|
return composed_transfer.do_transfer(i2c_num, i2c_addr)[0];
|
|
|
|
}
|
|
|
|
|
2022-01-04 06:48:12 -05:00
|
|
|
#if CONFIG_SOC_I2C_SUPPORT_SLAVE
|
2021-10-25 06:58:15 -04:00
|
|
|
I2CSlave::I2CSlave(I2CNumber i2c_number,
|
|
|
|
SCL_GPIO scl_gpio,
|
|
|
|
SDA_GPIO sda_gpio,
|
|
|
|
I2CAddress slave_addr,
|
2020-02-04 13:08:41 -05:00
|
|
|
size_t rx_buf_len,
|
|
|
|
size_t tx_buf_len,
|
|
|
|
bool scl_pullup,
|
|
|
|
bool sda_pullup)
|
2021-10-25 06:58:15 -04:00
|
|
|
: I2CBus(std::move(i2c_number))
|
2020-02-04 13:08:41 -05:00
|
|
|
{
|
|
|
|
i2c_config_t conf = {};
|
|
|
|
conf.mode = I2C_MODE_SLAVE;
|
2021-10-25 06:58:15 -04:00
|
|
|
conf.scl_io_num = scl_gpio.get_value();
|
2020-02-04 13:08:41 -05:00
|
|
|
conf.scl_pullup_en = scl_pullup;
|
2021-10-25 06:58:15 -04:00
|
|
|
conf.sda_io_num = sda_gpio.get_value();
|
2020-02-04 13:08:41 -05:00
|
|
|
conf.sda_pullup_en = sda_pullup;
|
|
|
|
conf.slave.addr_10bit_en = 0;
|
2021-10-25 06:58:15 -04:00
|
|
|
conf.slave.slave_addr = slave_addr.get_addr();
|
2022-05-24 04:22:49 -04:00
|
|
|
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));
|
2020-02-04 13:08:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
I2CSlave::~I2CSlave()
|
|
|
|
{
|
2022-05-24 04:22:49 -04:00
|
|
|
i2c_driver_delete(i2c_num_to_driver_type(i2c_num));
|
2020-02-04 13:08:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int I2CSlave::write_raw(const uint8_t *data, size_t data_len, chrono::milliseconds timeout)
|
|
|
|
{
|
2022-05-24 04:22:49 -04:00
|
|
|
return i2c_slave_write_buffer(i2c_num_to_driver_type(i2c_num), data, data_len, (TickType_t) timeout.count() / portTICK_PERIOD_MS);
|
2020-02-04 13:08:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int I2CSlave::read_raw(uint8_t *buffer, size_t buffer_len, chrono::milliseconds timeout)
|
|
|
|
{
|
2022-05-24 04:22:49 -04:00
|
|
|
return i2c_slave_read_buffer(i2c_num_to_driver_type(i2c_num), buffer, buffer_len, (TickType_t) timeout.count() / portTICK_PERIOD_MS);
|
2020-02-04 13:08:41 -05:00
|
|
|
}
|
2022-01-04 06:48:12 -05:00
|
|
|
#endif // CONFIG_SOC_I2C_SUPPORT_SLAVE
|
2020-02-04 13:08:41 -05:00
|
|
|
|
|
|
|
I2CWrite::I2CWrite(const vector<uint8_t> &bytes, chrono::milliseconds driver_timeout)
|
2021-10-25 06:58:15 -04:00
|
|
|
: I2CTransfer<void>(driver_timeout), bytes(bytes)
|
|
|
|
{
|
|
|
|
if (bytes.empty()) {
|
|
|
|
throw I2CException(ESP_ERR_INVALID_ARG);
|
|
|
|
}
|
|
|
|
}
|
2020-02-04 13:08:41 -05:00
|
|
|
|
2021-10-25 06:58:15 -04:00
|
|
|
void I2CWrite::queue_cmd(I2CCommandLink &handle, I2CAddress i2c_addr)
|
2020-02-04 13:08:41 -05:00
|
|
|
{
|
2021-10-25 06:58:15 -04:00
|
|
|
handle.start();
|
|
|
|
handle.write_byte(i2c_addr.get_value() << 1 | I2C_MASTER_WRITE);
|
|
|
|
handle.write(bytes);
|
2020-02-04 13:08:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void I2CWrite::process_result() { }
|
|
|
|
|
|
|
|
I2CRead::I2CRead(size_t size, chrono::milliseconds driver_timeout)
|
2021-10-25 06:58:15 -04:00
|
|
|
: I2CTransfer<vector<uint8_t> >(driver_timeout), bytes(size)
|
|
|
|
{
|
|
|
|
if (size == 0) {
|
|
|
|
throw I2CException(ESP_ERR_INVALID_ARG);
|
|
|
|
}
|
|
|
|
}
|
2020-02-04 13:08:41 -05:00
|
|
|
|
2021-10-25 06:58:15 -04:00
|
|
|
void I2CRead::queue_cmd(I2CCommandLink &handle, I2CAddress i2c_addr)
|
2020-02-04 13:08:41 -05:00
|
|
|
{
|
2021-10-25 06:58:15 -04:00
|
|
|
handle.start();
|
|
|
|
handle.write_byte(i2c_addr.get_value() << 1 | I2C_MASTER_READ);
|
|
|
|
handle.read(bytes);
|
2020-02-04 13:08:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
vector<uint8_t> I2CRead::process_result()
|
|
|
|
{
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
I2CComposed::I2CComposed(chrono::milliseconds driver_timeout)
|
|
|
|
: I2CTransfer<vector<vector<uint8_t> > >(driver_timeout), transfer_list() { }
|
|
|
|
|
2021-10-25 06:58:15 -04:00
|
|
|
void I2CComposed::CompTransferNodeRead::queue_cmd(I2CCommandLink &handle, I2CAddress i2c_addr)
|
2020-02-04 13:08:41 -05:00
|
|
|
{
|
2021-10-25 06:58:15 -04:00
|
|
|
handle.write_byte(i2c_addr.get_value() << 1 | I2C_MASTER_READ);
|
|
|
|
handle.read(bytes);
|
2020-02-04 13:08:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void I2CComposed::CompTransferNodeRead::process_result(std::vector<std::vector<uint8_t> > &read_results)
|
|
|
|
{
|
|
|
|
read_results.push_back(bytes);
|
|
|
|
}
|
|
|
|
|
2021-10-25 06:58:15 -04:00
|
|
|
void I2CComposed::CompTransferNodeWrite::queue_cmd(I2CCommandLink &handle, I2CAddress i2c_addr)
|
2020-02-04 13:08:41 -05:00
|
|
|
{
|
2021-10-25 06:58:15 -04:00
|
|
|
handle.write_byte(i2c_addr.get_value() << 1 | I2C_MASTER_WRITE);
|
|
|
|
handle.write(bytes);
|
2020-02-04 13:08:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void I2CComposed::add_read(size_t size)
|
|
|
|
{
|
2021-10-25 06:58:15 -04:00
|
|
|
if (!size) {
|
|
|
|
throw I2CException(ESP_ERR_INVALID_ARG);
|
|
|
|
}
|
2020-02-04 13:08:41 -05:00
|
|
|
|
|
|
|
transfer_list.push_back(make_shared<CompTransferNodeRead>(size));
|
|
|
|
}
|
|
|
|
|
|
|
|
void I2CComposed::add_write(std::vector<uint8_t> bytes)
|
|
|
|
{
|
2021-10-25 06:58:15 -04:00
|
|
|
if (bytes.empty()) {
|
|
|
|
throw I2CException(ESP_ERR_INVALID_ARG);
|
|
|
|
}
|
2020-02-04 13:08:41 -05:00
|
|
|
|
|
|
|
transfer_list.push_back(make_shared<CompTransferNodeWrite>(bytes));
|
|
|
|
}
|
|
|
|
|
2021-10-25 06:58:15 -04:00
|
|
|
void I2CComposed::queue_cmd(I2CCommandLink &handle, I2CAddress i2c_addr)
|
2020-02-04 13:08:41 -05:00
|
|
|
{
|
|
|
|
for (auto it = transfer_list.begin(); it != transfer_list.end(); it++) {
|
2021-10-25 06:58:15 -04:00
|
|
|
handle.start();
|
2020-02-04 13:08:41 -05:00
|
|
|
(*it)->queue_cmd(handle, i2c_addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::vector<uint8_t> > I2CComposed::process_result()
|
|
|
|
{
|
|
|
|
std::vector<std::vector<uint8_t> > results;
|
|
|
|
for (auto it = transfer_list.begin(); it != transfer_list.end(); it++) {
|
|
|
|
(*it)->process_result(results);
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // idf
|
|
|
|
|
|
|
|
#endif // __cpp_exceptions
|