Merge branch 'feature/i2c++' into 'master'

I2C C++ API

See merge request espressif/esp-idf!7812
pull/5688/head
Michael (XIAO Xufeng) 2020-07-27 20:05:42 +08:00
commit 2bfd725e66
25 zmienionych plików z 1354 dodań i 19 usunięć

Wyświetl plik

@ -1,2 +1,3 @@
idf_component_register(SRCS "esp_exception.cpp"
INCLUDE_DIRS "include")
idf_component_register(SRCS "esp_exception.cpp" "i2c_cxx.cpp"
INCLUDE_DIRS "include"
REQUIRES driver)

Wyświetl plik

@ -0,0 +1,4 @@
COMPONENT_ADD_INCLUDEDIRS := include
COMPONENT_SRCDIRS := ./ driver

Wyświetl plik

@ -1,3 +1,17 @@
// 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.
#ifdef __cpp_exceptions
#include "esp_exception.hpp"

Wyświetl plik

@ -0,0 +1,201 @@
// 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.
#ifdef __cpp_exceptions
#include "i2c_cxx.hpp"
using namespace std;
namespace idf {
#define I2C_CHECK_THROW(err) CHECK_THROW_SPECIFIC((err), I2CException)
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) { }
I2CBus::~I2CBus() { }
I2CMaster::I2CMaster(i2c_port_t i2c_number,
int scl_gpio,
int sda_gpio,
uint32_t clock_speed,
bool scl_pullup,
bool sda_pullup)
: I2CBus(i2c_number)
{
i2c_config_t conf = {};
conf.mode = I2C_MODE_MASTER;
conf.scl_io_num = scl_gpio;
conf.scl_pullup_en = scl_pullup;
conf.sda_io_num = sda_gpio;
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));
}
I2CMaster::~I2CMaster()
{
i2c_driver_delete(i2c_num);
}
void I2CMaster::sync_write(uint8_t 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)
{
I2CRead reader(n_bytes);
return reader.do_transfer(i2c_num, i2c_addr);
}
vector<uint8_t> I2CMaster::sync_transfer(uint8_t 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);
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,
size_t rx_buf_len,
size_t tx_buf_len,
bool scl_pullup,
bool sda_pullup)
: I2CBus(i2c_number)
{
i2c_config_t conf = {};
conf.mode = I2C_MODE_SLAVE;
conf.scl_io_num = scl_gpio;
conf.scl_pullup_en = scl_pullup;
conf.sda_io_num = sda_gpio;
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));
}
I2CSlave::~I2CSlave()
{
i2c_driver_delete(i2c_num);
}
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);
}
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);
}
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)
{
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));
}
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)
{
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));
}
vector<uint8_t> I2CRead::process_result()
{
return bytes;
}
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)
{
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));
}
void I2CComposed::CompTransferNodeRead::process_result(std::vector<std::vector<uint8_t> > &read_results)
{
read_results.push_back(bytes);
}
void I2CComposed::CompTransferNodeWrite::queue_cmd(i2c_cmd_handle_t handle, uint8_t 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));
}
void I2CComposed::add_read(size_t size)
{
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);
transfer_list.push_back(make_shared<CompTransferNodeWrite>(bytes));
}
void I2CComposed::queue_cmd(i2c_cmd_handle_t handle, uint8_t i2c_addr)
{
for (auto it = transfer_list.begin(); it != transfer_list.end(); it++) {
I2C_CHECK_THROW(i2c_master_start(handle));
(*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

Wyświetl plik

@ -12,8 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef ESP_EXCEPTION_HPP_
#define ESP_EXCEPTION_HPP_
#pragma once
#ifdef __cpp_exceptions
@ -35,10 +34,21 @@ struct ESPException : public std::exception {
/**
* Convenience macro to help converting IDF error codes into ESPException.
*/
#define CHECK_THROW(error_) if (error_ != ESP_OK) throw idf::ESPException(error_);
#define CHECK_THROW(error_) \
do { \
esp_err_t result = error_; \
if (result != ESP_OK) throw idf::ESPException(result); \
} while (0)
/**
* Convenience macro to help converting IDF error codes into a child of ESPException.
*/
#define CHECK_THROW_SPECIFIC(error_, exception_type_) \
do { \
esp_err_t result = error_; \
if (result != ESP_OK) throw idf::exception_type_(result); \
} while (0)
} // namespace idf
#endif // __cpp_exceptions
#endif // ESP_EXCEPTION_HPP_

Wyświetl plik

@ -0,0 +1,494 @@
// 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.
#pragma once
#ifndef __cpp_exceptions
#error I2C class can only be used when __cpp_exceptions is enabled. Enable CONFIG_COMPILER_CXX_EXCEPTIONS in Kconfig
#endif
#include <exception>
#include <memory>
#include <chrono>
#include <vector>
#include <list>
#include <future>
#include "driver/i2c.h"
#include "esp_exception.hpp"
namespace idf {
struct I2CException : public ESPException {
I2CException(esp_err_t error);
};
struct I2CTransferException : public I2CException {
I2CTransferException(esp_err_t error);
};
/**
* 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().
*/
typedef TReturn TransferReturnT;
/**
* @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));
virtual ~I2CTransfer() { }
/**
* Do all general parts of the I2C transfer:
* - initialize the command link
* - issuing a start to the command link queue
* - calling \c queue_cmd() in the subclass to issue specific commands to the command link queue
* - issuing a stop to the command link queue
* - executing the assembled commands on the I2C bus
* - calling \c process_result() to process the results of the commands or calling process_exception() if
* there was an exception
* - deleting the command link
* This method is normally called by I2CMaster, but can also be used stand-alone if the bus corresponding to
* \c i2c_num has be initialized.
*
* @throws I2CException for any particular I2C error
*/
TReturn do_transfer(i2c_port_t i2c_num, uint8_t i2c_addr);
protected:
/**
* Implementation of the I2C command is implemented by subclasses.
* The I2C command handle is initialized already at this stage.
* The first action is issuing the I2C address and the read/write bit, depending on what the subclass implements.
* On error, this method has to throw an instance of I2CException.
*
* @param handle the initialized command handle of the I2C driver.
* @param i2c_addr The slave's I2C address.
*
* @throw I2CException
*/
virtual void queue_cmd(i2c_cmd_handle_t handle, uint8_t i2c_addr) = 0;
/**
* Implementation of whatever neccessary action after successfully sending the I2C command.
* On error, this method has to throw an instance of I2CException.
*
* @throw I2CException
*/
virtual TReturn process_result() = 0;
/**
* 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;
};
/**
* @brief Super class for any I2C master or slave
*/
class I2CBus {
public:
/*
* @brief Initialize I2C master bus.
*
* Initialize and install the bus driver in master mode.
*
* @param i2c_number The I2C port number.
*/
I2CBus(i2c_port_t i2c_number);
/**
* @brief uninstall the bus driver.
*/
virtual ~I2CBus();
/**
* The I2C port number.
*/
const i2c_port_t i2c_num;
};
/**
* @brief Simple I2C Master object
*
* This class provides to ways to issue I2C read and write requests. The simplest way is to use \c sync_write() and
* sync_read() to write and read, respectively. As the name suggests, they block during the whole transfer.
* For all asynchrounous transfers as well as combined write-read transfers, use \c transfer().
*/
class I2CMaster : public I2CBus {
public:
/**
* Initialize and install the driver of an I2C master peripheral.
*
* Initialize and install the bus driver in master mode. Pullups will be enabled for both pins. If you want a
* different configuration, use configure() and i2c_set_pin() of the underlying driver to disable one or both
* pullups.
*
* @param i2c_number The number of the I2C device.
* @param scl_gpio GPIO number of the SCL line.
* @param sda_gpio GPIO number of the SDA line.
* @param clock_speed The master clock speed.
* @param scl_pullup Enable SCL pullup.
* @param sda_pullup Enable SDA pullup.
*
* @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,
bool scl_pullup = true,
bool sda_pullup = true);
/**
* Delete the driver.
*/
virtual ~I2CMaster();
/**
* Issue an asynchronous I2C transfer which is executed in the background.
*
* This method uses a C++ \c std::future as mechanism to wait for the asynchronous return value.
* The return value can be accessed with \c future::get(). \c future::get() also synchronizes with the thread
* doing the work in the background, i.e. it waits until the return value has been issued.
*
* The actual implementation is delegated to the TransferT object. It will be given the I2C number to work
* with.
*
* Requirements for TransferT: It should implement or imitate the interface of I2CTransfer.
*
* @param xfer The transfer to execute. What the transfer does, depends on it's implementation in
* \c TransferT::do_transfer(). It also determines the future template of this function, indicated by
* \c TransferT::TransferReturnT.
*
* @param i2c_addr The address of the I2C slave device targeted by the transfer.
*
* @return A future with \c TransferT::TransferReturnT. It depends on which template type is used for xfer.
* In case of a simple write (I2CWrite), it's future<void>.
* In case of a read (I2CRead), it's future<vector<uint8_t> > corresponding to the length of the read
* operation.
* If TransferT is a combined transfer with repeated reads (I2CComposed), then the return type is
* future<vector<vector<uint8_t> > >, a vector of results corresponding to the queued read operations.
*
* @throws I2CException with the corrsponding esp_err_t return value if something goes wrong
* @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);
/**
* Do a synchronous write.
*
* All data in data will be written to the I2C device with i2c_addr at once.
* This method will block until the I2C write is complete.
*
* @param i2c_addr The address of the I2C device to which the data shall be sent.
* @param data The data to send (size to be sent is determined by data.size()).
*
* @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);
/**
* Do a synchronous read.
* This method will block until the I2C read is complete.
*
* n_bytes bytes of data will be read from the I2C device with i2c_addr.
* While reading the last byte, the master finishes the reading by sending a NACK, before issuing a stop.
*
* @param i2c_addr The address of the I2C device from which to read.
* @param n_bytes The number of bytes to read.
*
* @return the read bytes
*
* @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);
/**
* Do a simple asynchronous write-read transfer.
*
* First, \c write_data will be written to the bus, then a number of \c read_n_bytes will be read from the bus
* with a repeated start condition. The slave device is determined by \c i2c_addr.
* While reading the last byte, the master finishes the reading by sending a NACK, before issuing a stop.
* This method will block until the I2C transfer is complete.
*
* @param i2c_addr The address of the I2C device from which to read.
* @param write_data The data to write to the bus before reading.
* @param read_n_bytes The number of bytes to read.
*
* @return the read bytes
*
* @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,
const std::vector<uint8_t> &write_data,
size_t read_n_bytes);
};
/**
* @brief Responsible for initialization and de-initialization of an I2C slave peripheral.
*/
class I2CSlave : public I2CBus {
public:
/**
* Initialize and install the driver of an I2C slave peripheral.
*
* Initialize and install the bus driver in slave mode. Pullups will be enabled for both pins. If you want a
* different configuration, use configure() and i2c_set_pin() of the underlying driver to disable one or both
* pullups.
*
* @param i2c_number The number of the I2C device.
* @param scl_gpio GPIO number of the SCL line.
* @param sda_gpio GPIO number of the SDA line.
* @param slave_addr The address of the slave device on the I2C bus.
* @param rx_buf_len Receive buffer length.
* @param tx_buf_len Transmit buffer length.
* @param scl_pullup Enable SCL pullup.
* @param sda_pullup Enable SDA pullup.
*
* @throws
*/
I2CSlave(i2c_port_t i2c_number,
int scl_gpio,
int sda_gpio,
uint8_t slave_addr,
size_t rx_buf_len,
size_t tx_buf_len,
bool scl_pullup = true,
bool sda_pullup = true);
/**
* Delete the driver.
*/
virtual ~I2CSlave();
/**
* Schedule a raw data write once master is ready.
*
* The data is saved in a buffer, waiting for the master to pick it up.
*/
virtual int write_raw(const uint8_t* data, size_t data_len, std::chrono::milliseconds timeout);
/**
* Read raw data from the bus.
*
* The data is read directly from the buffer. Hence, it has to be written already by master.
*/
virtual int read_raw(uint8_t* buffer, size_t buffer_len, std::chrono::milliseconds timeout);
};
/**
* Implementation for simple I2C writes, which can be executed by \c I2CMaster::transfer().
* It stores the bytes to be written as a vector.
*/
class I2CWrite : public I2CTransfer<void> {
public:
/**
* @param bytes The bytes which should be written.
* @param driver_timeout The timeout used for calls like i2c_master_cmd_begin() to the underlying driver.
*/
I2CWrite(const std::vector<uint8_t> &bytes, std::chrono::milliseconds driver_timeout = std::chrono::milliseconds(1000));
protected:
/**
* Write the address and set the read bit to 0 to issue the address and request a write.
* Then write the bytes.
*
* @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;
/**
* Set the value of the promise to unblock any callers waiting on it.
*/
void process_result() override;
private:
/**
* The bytes to write.
*/
std::vector<uint8_t> bytes;
};
/**
* Implementation for simple I2C reads, which can be executed by \c I2CMaster::transfer().
* It stores the bytes to be read as a vector to be returned later via a future.
*/
class I2CRead : public I2CTransfer<std::vector<uint8_t> > {
public:
/**
* @param The number of bytes to read.
* @param driver_timeout The timeout used for calls like i2c_master_cmd_begin() to the underlying driver.
*/
I2CRead(size_t size, std::chrono::milliseconds driver_timeout = std::chrono::milliseconds(1000));
protected:
/**
* Write the address and set the read bit to 1 to issue the address and request a read.
* Then read into bytes.
*
* @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;
/**
* Set the return value of the promise to unblock any callers waiting on it.
*/
std::vector<uint8_t> process_result() override;
private:
/**
* The bytes to read.
*/
std::vector<uint8_t> bytes;
};
/**
* This kind of transfer uses repeated start conditions to chain transfers coherently.
* In particular, this can be used to chain multiple single write and read transfers into a single transfer with
* repeated starts as it is commonly done for I2C devices.
* The result is a vector of vectors representing the reads in the order of how they were added using add_read().
*/
class I2CComposed : public I2CTransfer<std::vector<std::vector<uint8_t> > > {
public:
I2CComposed(std::chrono::milliseconds driver_timeout = std::chrono::milliseconds(1000));
/**
* Add a read to the chain.
*
* @param size The size of the read in bytes.
*/
void add_read(size_t size);
/**
* Add a write to the chain.
*
* @param bytes The bytes to write; size will be bytes.size()
*/
void add_write(std::vector<uint8_t> bytes);
protected:
/**
* Write all chained transfers, including a repeated start issue after each but the last transfer.
*
* @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;
/**
* Creates the vector with the vectors from all reads.
*/
std::vector<std::vector<uint8_t> > process_result() override;
private:
class CompTransferNode {
public:
virtual void queue_cmd(i2c_cmd_handle_t handle, uint8_t 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 process_result(std::vector<std::vector<uint8_t> > &read_results) override;
private:
std::vector<uint8_t> bytes;
};
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;
private:
std::vector<uint8_t> bytes;
};
/**
* The chained transfers.
*/
std::list<std::shared_ptr<CompTransferNode> > transfer_list;
};
template<typename TReturn>
I2CTransfer<TReturn>::I2CTransfer(std::chrono::milliseconds driver_timeout)
: driver_timeout(driver_timeout.count()) { }
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)
{
I2CCommandLink cmd_link;
queue_cmd(cmd_link.handle, i2c_addr);
CHECK_THROW_SPECIFIC(i2c_master_stop(cmd_link.handle), I2CException);
CHECK_THROW_SPECIFIC(i2c_master_cmd_begin(i2c_num, cmd_link.handle, 1000 / portTICK_RATE_MS), I2CTransferException);
return process_result();
}
template<typename TransferT>
std::future<typename TransferT::TransferReturnT> I2CMaster::transfer(std::shared_ptr<TransferT> xfer, uint8_t i2c_addr)
{
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 xfer->do_transfer(i2c_num, i2c_addr);
}, xfer, i2c_addr);
}
} // idf

Wyświetl plik

@ -11,7 +11,15 @@ using namespace idf;
#define TAG "CXX Exception Test"
TEST_CASE("TEST_THROW catches exception", "[cxx exception]")
#if CONFIG_IDF_TARGET_ESP32
#define LEAKS "300"
#elif CONFIG_IDF_TARGET_ESP32S2
#define LEAKS "800"
#else
#error "unknown target in CXX tests, can't set leaks threshold"
#endif
TEST_CASE("TEST_THROW catches exception", "[cxx exception][leaks=" LEAKS "]")
{
TEST_THROW(throw ESPException(ESP_FAIL);, ESPException);
}
@ -28,13 +36,13 @@ TEST_CASE("TEST_THROW asserts not catching any exception", "[cxx exception][igno
TEST_THROW(printf(" ");, ESPException); // need statement with effect
}
TEST_CASE("CHECK_THROW continues on ESP_OK", "[cxx exception]")
TEST_CASE("CHECK_THROW continues on ESP_OK", "[cxx exception][leaks=" LEAKS "]")
{
esp_err_t error = ESP_OK;
CHECK_THROW(error);
}
TEST_CASE("CHECK_THROW throws", "[cxx exception]")
TEST_CASE("CHECK_THROW throws", "[cxx exception][leaks=" LEAKS "]")
{
esp_err_t error = ESP_FAIL;
TEST_THROW(CHECK_THROW(error), ESPException);

Wyświetl plik

@ -0,0 +1,471 @@
// 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.
#include "unity.h"
#include "unity_cxx.hpp"
#include <limits>
#include <stdio.h>
#include <iostream>
#include "test_utils.h" // unity_send_signal
#include "i2c_cxx.hpp"
#ifdef __cpp_exceptions
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 */
#define I2C_SLAVE_SCL_IO 19 /*!<gpio number for i2c slave clock */
#define I2C_SLAVE_SDA_IO 18 /*!<gpio number for i2c slave data */
#define I2C_MASTER_NUM I2C_NUM_1 /*!< 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 */
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)),
data(data_arg) { }
std::shared_ptr<I2CMaster> master;
vector<uint8_t> data;
};
TEST_CASE("I2CMaster GPIO out of range", "[cxx i2c][leaks=300]")
{
TEST_THROW(I2CMaster(0, 255, 255, 400000), I2CException);
}
TEST_CASE("I2CMaster SDA and SCL equal", "[cxx i2c][leaks=300]")
{
TEST_THROW(I2CMaster(0, 0, 0, 400000), I2CException);
}
// TODO The I2C driver tests are disabled, so disable them here, too. Probably due to no runners.
#if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2)
static void i2c_slave_read_raw_byte(void)
{
I2CSlave slave(I2C_SLAVE_NUM, I2C_SLAVE_SCL_IO, I2C_SLAVE_SDA_IO, ADDR, 512, 512);
uint8_t buffer = 0;
unity_send_signal("slave init");
unity_wait_for_signal("master write");
TEST_ASSERT_EQUAL(1, slave.read_raw(&buffer, 1, chrono::milliseconds(1000)));
TEST_ASSERT_EQUAL(MAGIC_TEST_NUMBER, buffer);
}
static void i2c_slave_write_raw_byte(void)
{
I2CSlave slave(I2C_SLAVE_NUM, I2C_SLAVE_SCL_IO, I2C_SLAVE_SDA_IO, ADDR, 512, 512);
uint8_t WRITE_BUFFER = MAGIC_TEST_NUMBER;
unity_wait_for_signal("master init");
TEST_ASSERT_EQUAL(1, slave.write_raw(&WRITE_BUFFER, 1, chrono::milliseconds(1000)));
unity_send_signal("slave write");
// This last synchronization is necessary to prevent slave from going out of scope hence de-initializing already
// before master has read
unity_wait_for_signal("master read done");
}
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);
uint8_t buffer [8] = {};
unity_send_signal("slave init");
unity_wait_for_signal("master write");
TEST_ASSERT_EQUAL(8, slave.read_raw(buffer, 8, chrono::milliseconds(1000)));
for (int i = 0; i < 8; i++) {
TEST_ASSERT_EQUAL(i, buffer[i]);
}
}
static void i2c_slave_write_multiple_raw_bytes(void)
{
I2CSlave slave(1, I2C_SLAVE_SCL_IO, I2C_SLAVE_SDA_IO, ADDR, 512, 512);
uint8_t WRITE_BUFFER [8] = {0, 1, 2, 3, 4, 5, 6, 7};
unity_wait_for_signal("master init");
TEST_ASSERT_EQUAL(8, slave.write_raw(WRITE_BUFFER, 8, chrono::milliseconds(1000)));
unity_send_signal("slave write");
unity_wait_for_signal("master read done");
}
static void i2c_slave_composed_trans(void)
{
I2CSlave slave(1, I2C_SLAVE_SCL_IO, I2C_SLAVE_SDA_IO, ADDR, 512, 512);
size_t BUF_SIZE = 2;
const uint8_t SLAVE_WRITE_BUFFER [BUF_SIZE] = {0xde, 0xad};
uint8_t slave_read_buffer = 0;
unity_send_signal("slave init");
TEST_ASSERT_EQUAL(BUF_SIZE, slave.write_raw(SLAVE_WRITE_BUFFER, 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);
}
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;
unity_send_signal("master init");
unity_wait_for_signal("slave write");
std::shared_ptr<I2CRead> reader(new I2CRead(1));
future<vector<uint8_t> > fut = fix.master->transfer(reader, ADDR);
vector<uint8_t> data;
data = fut.get();
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 read one byte", "[cxx i2c][test_env=UT_T2_I2C][timeout=150]",
i2c_master_read_raw_byte, i2c_slave_write_raw_byte);
static void i2c_master_write_raw_byte(void)
{
MasterFixture fix;
unity_wait_for_signal("slave init");
std::shared_ptr<I2CWrite> writer(new I2CWrite(fix.data));
future<void> fut = fix.master->transfer(writer, ADDR);
fut.get();
unity_send_signal("master write");
}
TEST_CASE_MULTIPLE_DEVICES("I2CMaster write one byte", "[cxx i2c][test_env=UT_T2_I2C][timeout=150]",
i2c_master_write_raw_byte, i2c_slave_read_raw_byte);
static void i2c_master_read_multiple_raw_bytes(void)
{
MasterFixture fix;
unity_send_signal("master init");
unity_wait_for_signal("slave write");
std::shared_ptr<I2CRead> reader(new I2CRead(8));
future<vector<uint8_t> > fut = fix.master->transfer(reader, ADDR);
vector<uint8_t> data = fut.get();
unity_send_signal("master read done");
TEST_ASSERT_EQUAL(8, data.size());
for (int i = 0; i < 8; i++) {
TEST_ASSERT_EQUAL(i, data[i]);
}
}
TEST_CASE_MULTIPLE_DEVICES("I2CMaster read multiple bytes", "[cxx i2c][test_env=UT_T2_I2C][timeout=150]",
i2c_master_read_multiple_raw_bytes, i2c_slave_write_multiple_raw_bytes);
static void i2c_master_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 = fix.master->transfer(writer, ADDR);
fut.get();
unity_send_signal("master write");
}
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;
size_t READ_SIZE = 2;
const uint8_t DESIRED_READ [READ_SIZE] = {0xde, 0xad};
unity_wait_for_signal("slave init");
vector<uint8_t> read_data = fix.master->sync_transfer(ADDR, fix.data, READ_SIZE);
unity_send_signal("master transfer");
TEST_ASSERT_EQUAL(READ_SIZE, read_data.size());
for (int i = 0; i < READ_SIZE; i++) {
TEST_ASSERT_EQUAL(DESIRED_READ[i], read_data[i]);
}
}
TEST_CASE_MULTIPLE_DEVICES("I2CMaster sync transfer", "[cxx i2c][test_env=UT_T2_I2C][timeout=150]",
i2c_master_sync_transfer, i2c_slave_composed_trans);
static void i2c_master_composed_trans(void)
{
MasterFixture fix;
size_t BUF_SIZE = 2;
const uint8_t SLAVE_WRITE_BUFFER [BUF_SIZE] = {0xde, 0xad};
std::shared_ptr<I2CComposed> composed_transfer(new I2CComposed);
composed_transfer->add_write({47u});
composed_transfer->add_read(BUF_SIZE);
unity_wait_for_signal("slave init");
future<vector<vector<uint8_t> > > result = fix.master->transfer(composed_transfer, ADDR);
unity_send_signal("master transfer");
vector<vector<uint8_t> > read_data = result.get();
TEST_ASSERT_EQUAL(1, read_data.size());
TEST_ASSERT_EQUAL(2, read_data[0].size());
for (int i = 0; i < BUF_SIZE; i++) {
TEST_ASSERT_EQUAL(SLAVE_WRITE_BUFFER[i], read_data[0][i]);
}
}
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(1, 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;
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]);
}
}
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(1, 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);
TEST_ASSERT_EQUAL(1, slave.read_raw(&slave_read_buffer, 1, chrono::milliseconds(1000)));
TEST_ASSERT_EQUAL(MAGIC_TEST_NUMBER, slave_read_buffer);
}
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});
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]);
}
}
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)
#endif // __cpp_exceptions

Wyświetl plik

@ -0,0 +1,8 @@
# The following lines of boilerplate have to be in your project's CMakeLists
# in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)
set(EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/examples/cxx/experimental/experimental_cpp_component")
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(sensor_mcp9808)

Wyświetl plik

@ -0,0 +1,11 @@
#
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
# project subdirectory.
#
EXTRA_COMPONENT_DIRS += ${IDF_PATH}/examples/cxx/experimental/experimental_cpp_component
PROJECT_NAME := sensor_mcp9808
include $(IDF_PATH)/make/project.mk

Wyświetl plik

@ -0,0 +1,49 @@
# Example: C++ I2C sensor read for MCP9808
(See the README.md file in the upper level 'examples' directory for more information about examples.)
This example demonstrates usage of C++ exceptions in ESP-IDF.
In this example, the `sdkconfig.defaults` file sets the `CONFIG_COMPILER_CXX_EXCEPTIONS` option.
This enables both compile time support (`-fexceptions` compiler flag) and run-time support for C++ exception handling.
This is necessary for the C++ I2C API.
## How to use example
### Hardware Required
An MCP9808 sensor and any commonly available ESP32 development board.
Pullups aren't necessary as the default pullups are enabled in the I2CMaster class.
### Configure the project
```
idf.py menuconfig
```
### Build and Flash
```
idf.py -p PORT flash monitor
```
(Replace PORT with the name of the serial port.)
(To exit the serial monitor, type ``Ctrl-]``.)
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
## Example Output
If the sensor is read correctly:
```
Current temperature: 24.875
```
If something went wrong:
```
I2C Exception with error: -1
Coulnd't read sensor!
```

Wyświetl plik

@ -0,0 +1,2 @@
idf_component_register(SRCS "sensor_mcp9808.cpp"
INCLUDE_DIRS ".")

Wyświetl plik

@ -0,0 +1,4 @@
#
# "main" pseudo-component makefile.
#
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)

Wyświetl plik

@ -0,0 +1,44 @@
#include <iostream>
#include "i2c_cxx.hpp"
using namespace std;
using namespace idf;
#define ADDR 0x18
#define I2C_MASTER_NUM I2C_NUM_0 /*!< 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 */
#define MCP_9808_TEMP_REG 0x05
/**
* Calculates the temperature of the MCP9808 from the read msb and lsb. Loosely adapted from the MCP9808's datasheet.
*/
float calc_temp(uint8_t msb, uint8_t lsb) {
float temperature;
msb &= 0x1F;
bool sign = msb & 0x10;
if (sign) {
msb &= 0x0F;
temperature = 256 - (msb * 16 + (float) lsb / 16);
} else {
temperature = (msb * 16 + (float) lsb / 16);
}
return temperature;
}
extern "C" void app_main(void)
{
try {
// creating master bus, writing temperature register pointer and reading the value
shared_ptr<I2CMaster> master(new I2CMaster(I2C_MASTER_NUM, I2C_MASTER_SCL_IO, I2C_MASTER_SDA_IO, 400000));
master->sync_write(ADDR, {MCP_9808_TEMP_REG});
vector<uint8_t> data = master->sync_read(ADDR, 2);
cout << "Current temperature: " << calc_temp(data[0], data[1]) << endl;
} catch (const I2CException &e) {
cout << "I2C Exception with error: " << e.error << endl;
cout << "Coulnd't read sensor!" << endl;
}
}

Wyświetl plik

@ -0,0 +1,3 @@
# Enable C++ exceptions and set emergency pool size for exception objects
CONFIG_COMPILER_CXX_EXCEPTIONS=y
CONFIG_COMPILER_CXX_EXCEPTIONS_EMG_POOL_SIZE=1024

Wyświetl plik

@ -159,6 +159,7 @@ build_examples_make:
build_examples_cmake_esp32:
extends: .build_examples_cmake
parallel: 10
variables:
IDF_TARGET: esp32

Wyświetl plik

@ -24,6 +24,10 @@
/* include performance pass standards header file */
#include "idf_performance.h"
#ifdef __cplusplus
extern "C" {
#endif
/* For performance check with unity test on IDF */
/* These macros should only be used with ESP-IDF.
* To use performance check, we need to first define pass standard in idf_performance.h.
@ -277,3 +281,7 @@ void test_utils_free_exhausted_memory(test_utils_exhaust_memory_rec rec);
* @param[in] thandle Handle of task to be deleted (should not be NULL or self handle)
*/
void test_utils_task_delete(TaskHandle_t thandle);
#ifdef __cplusplus
}
#endif

Wyświetl plik

@ -0,0 +1,2 @@
TEST_COMPONENTS=experimental_cpp_component
CONFIG_COMPILER_CXX_EXCEPTIONS=y

Wyświetl plik

@ -1,3 +1,3 @@
# This config is split between targets since different component needs to be excluded (esp32, esp32s2)
CONFIG_IDF_TARGET="esp32"
TEST_EXCLUDE_COMPONENTS=libsodium bt app_update freertos esp32 esp_ipc esp_timer driver heap pthread soc spi_flash vfs test_utils
TEST_EXCLUDE_COMPONENTS=libsodium bt app_update freertos esp32 esp_ipc esp_timer driver heap pthread soc spi_flash vfs test_utils experimental_cpp_component

Wyświetl plik

@ -1,3 +1,3 @@
# This config is split between targets since different component needs to be excluded (esp32, esp32s2)
CONFIG_IDF_TARGET="esp32s2"
TEST_EXCLUDE_COMPONENTS=libsodium bt app_update freertos esp32s2 esp_ipc esp_timer driver heap pthread soc spi_flash vfs
TEST_EXCLUDE_COMPONENTS=libsodium bt app_update freertos esp32s2 esp_ipc esp_timer driver heap pthread soc spi_flash vfs experimental_cpp_component

Wyświetl plik

@ -1,5 +1,5 @@
CONFIG_IDF_TARGET="esp32"
TEST_EXCLUDE_COMPONENTS=libsodium bt app_update driver esp32 esp_ipc esp_timer mbedtls spi_flash test_utils heap pthread soc
TEST_EXCLUDE_COMPONENTS=libsodium bt app_update driver esp32 esp_ipc esp_timer mbedtls spi_flash test_utils heap pthread soc experimental_cpp_component
CONFIG_ESP32_SPIRAM_SUPPORT=y
CONFIG_ESP_INT_WDT_TIMEOUT_MS=800
CONFIG_SPIRAM_OCCUPY_NO_HOST=y

Wyświetl plik

@ -1,6 +1,6 @@
# This config is split between targets since different component needs to be included (esp32, esp32s2)
CONFIG_IDF_TARGET="esp32"
TEST_EXCLUDE_COMPONENTS=libsodium bt app_update freertos esp32 esp_ipc esp_timer driver heap pthread soc spi_flash vfs test_utils
TEST_EXCLUDE_COMPONENTS=libsodium bt app_update freertos esp32 esp_ipc esp_timer driver heap pthread soc spi_flash vfs test_utils experimental_cpp_component
CONFIG_COMPILER_OPTIMIZATION_SIZE=y
CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE=y
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT=y

Wyświetl plik

@ -1,6 +1,6 @@
# This config is split between targets since different component needs to be excluded (esp32, esp32s2)
CONFIG_IDF_TARGET="esp32s2"
TEST_EXCLUDE_COMPONENTS=libsodium bt app_update freertos esp32s2 esp_ipc esp_timer driver heap pthread soc spi_flash vfs test_utils
TEST_EXCLUDE_COMPONENTS=libsodium bt app_update freertos esp32s2 esp_ipc esp_timer driver heap pthread soc spi_flash vfs test_utils experimental_cpp_component
CONFIG_COMPILER_OPTIMIZATION_SIZE=y
CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE=y
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT=y

Wyświetl plik

@ -1,6 +1,6 @@
# This config is split between targets since different component needs to be excluded (esp32, esp32s2)
CONFIG_IDF_TARGET="esp32"
TEST_EXCLUDE_COMPONENTS=libsodium bt app_update freertos esp32 esp_ipc esp_timer driver heap pthread soc spi_flash vfs test_utils
TEST_EXCLUDE_COMPONENTS=libsodium bt app_update freertos esp32 esp_ipc esp_timer driver heap pthread soc spi_flash vfs test_utils experimental_cpp_component
CONFIG_MEMMAP_SMP=n
CONFIG_FREERTOS_UNICORE=y
CONFIG_ESP32_RTCDATA_IN_FAST_MEM=y

Wyświetl plik

@ -1,6 +1,6 @@
# This config is split between targets since different component needs to be excluded (esp32, esp32s2)
CONFIG_IDF_TARGET="esp32s2"
TEST_EXCLUDE_COMPONENTS=libsodium bt app_update freertos esp32s2 esp_ipc esp_timer driver heap pthread soc spi_flash vfs
TEST_EXCLUDE_COMPONENTS=libsodium bt app_update freertos esp32s2 esp_ipc esp_timer driver heap pthread soc spi_flash vfs experimental_cpp_component
CONFIG_MEMMAP_SMP=n
CONFIG_FREERTOS_UNICORE=y
CONFIG_ESP32S2_RTCDATA_IN_FAST_MEM=y