diff --git a/components/ocs_io/i2c/istore.h b/components/ocs_io/i2c/istore.h index ec3c9709..720b6963 100644 --- a/components/ocs_io/i2c/istore.h +++ b/components/ocs_io/i2c/istore.h @@ -9,6 +9,7 @@ #include "ocs_io/gpio/types.h" #include "ocs_io/i2c/itransceiver.h" +#include "ocs_io/i2c/types.h" namespace ocs { namespace io { @@ -16,25 +17,6 @@ namespace i2c { class IStore { public: - enum class TransferSpeed : uint8_t { - //! 100 kbit/s. - Default, - - //! 400 kbit/s. - Fast, - }; - - enum class AddressLength : uint8_t { - //! 7-bit I2C address. - Bit_7, - - //! 10-bit I2C address. - Bit_10, - }; - - //! I2C address. - using Address = uint16_t; - //! I2C transceiver to communicate with I2C device. using ITransceiverPtr = std::unique_ptr; diff --git a/components/ocs_io/i2c/target_esp32/master_store.cpp b/components/ocs_io/i2c/target_esp32/master_store.cpp index 89dd3062..0dc4f749 100644 --- a/components/ocs_io/i2c/target_esp32/master_store.cpp +++ b/components/ocs_io/i2c/target_esp32/master_store.cpp @@ -38,27 +38,25 @@ MasterStore::MasterStore(IStore::Params params) { ESP_ERROR_CHECK(i2c_new_master_bus(&config, &handle_)); } -IStore::ITransceiverPtr MasterStore::add(const char* id, - IStore::AddressLength len, - IStore::Address addr, - IStore::TransferSpeed speed) { +IStore::ITransceiverPtr +MasterStore::add(const char* id, AddressLength len, Address addr, TransferSpeed speed) { i2c_device_config_t config; memset(&config, 0, sizeof(config)); #if SOC_I2C_SUPPORT_10BIT_ADDR - if (len == IStore::AddressLength::Bit_10) { + if (len == AddressLength::Bit_10) { config.dev_addr_length = I2C_ADDR_BIT_LEN_10; } else { config.dev_addr_length = I2C_ADDR_BIT_LEN_7; } #else // !SOC_I2C_SUPPORT_10BIT_ADDR - configASSERT(len == IStore::AddressLength::Bit_7); + configASSERT(len == AddressLength::Bit_7); config.dev_addr_length = I2C_ADDR_BIT_LEN_7; #endif // SOC_I2C_SUPPORT_10BIT_ADDR config.device_address = addr; - if (speed == IStore::TransferSpeed::Fast) { + if (speed == TransferSpeed::Fast) { config.scl_speed_hz = 400000; } else { config.scl_speed_hz = 100000; diff --git a/components/ocs_io/i2c/types.h b/components/ocs_io/i2c/types.h new file mode 100644 index 00000000..9d960141 --- /dev/null +++ b/components/ocs_io/i2c/types.h @@ -0,0 +1,35 @@ +/* + * SPDX-FileCopyrightText: 2025 Tendry Lab + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include + +namespace ocs { +namespace io { +namespace i2c { + +enum class TransferSpeed : uint8_t { + //! 100 kbit/s. + Default, + + //! 400 kbit/s. + Fast, +}; + +enum class AddressLength : uint8_t { + //! 7-bit I2C address. + Bit_7, + + //! 10-bit I2C address. + Bit_10, +}; + +//! I2C address. +using Address = uint16_t; + +} // namespace i2c +} // namespace io +} // namespace ocs diff --git a/components/ocs_sensor/sht4x/sensor_pipeline.cpp b/components/ocs_sensor/sht4x/sensor_pipeline.cpp index d6462685..561f9a72 100644 --- a/components/ocs_sensor/sht4x/sensor_pipeline.cpp +++ b/components/ocs_sensor/sht4x/sensor_pipeline.cpp @@ -22,9 +22,8 @@ SensorPipeline::SensorPipeline(io::i2c::IStore& store, storage_ = storage_builder.make(storage_id_.c_str()); configASSERT(storage_); - transceiver_ = - store.add(transceiver_id_.c_str(), io::i2c::IStore::AddressLength::Bit_7, - params.i2c_addr, io::i2c::IStore::TransferSpeed::Fast); + transceiver_ = store.add(transceiver_id_.c_str(), io::i2c::AddressLength::Bit_7, + params.i2c_addr, io::i2c::TransferSpeed::Fast); configASSERT(transceiver_); sensor_.reset(new (std::nothrow) Sensor(*transceiver_, *storage_, id, params.sensor)); diff --git a/projects/sht4x-verifier/main/main.cpp b/projects/sht4x-verifier/main/main.cpp index cbe1e4f3..36025047 100644 --- a/projects/sht4x-verifier/main/main.cpp +++ b/projects/sht4x-verifier/main/main.cpp @@ -32,9 +32,9 @@ extern "C" void app_main(void) { })); configASSERT(store); - io::i2c::IStore::ITransceiverPtr bus_transceiver = store->add( - "bus", io::i2c::IStore::AddressLength::Bit_7, io::i2c::IStore::bus_fanout_address, - io::i2c::IStore::TransferSpeed::Fast); + io::i2c::IStore::ITransceiverPtr bus_transceiver = + store->add("bus", io::i2c::AddressLength::Bit_7, + io::i2c::IStore::bus_fanout_address, io::i2c::TransferSpeed::Fast); configASSERT(bus_transceiver); auto code = bus_transceiver->send(&io::i2c::IStore::bus_reset_command, @@ -44,9 +44,8 @@ extern "C" void app_main(void) { ocs_logw(log_tag, "failed to reset i2c bus: %s", status::code_to_str(code)); } - io::i2c::IStore::ITransceiverPtr sensor_transceiver = - store->add("sht4x", io::i2c::IStore::AddressLength::Bit_7, 0x44, - io::i2c::IStore::TransferSpeed::Fast); + io::i2c::IStore::ITransceiverPtr sensor_transceiver = store->add( + "sht4x", io::i2c::AddressLength::Bit_7, 0x44, io::i2c::TransferSpeed::Fast); configASSERT(sensor_transceiver); std::unique_ptr storage_builder(