Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 1 addition & 19 deletions components/ocs_io/i2c/istore.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,14 @@

#include "ocs_io/gpio/types.h"
#include "ocs_io/i2c/itransceiver.h"
#include "ocs_io/i2c/types.h"

namespace ocs {
namespace io {
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<ITransceiver>;

Expand Down
12 changes: 5 additions & 7 deletions components/ocs_io/i2c/target_esp32/master_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
35 changes: 35 additions & 0 deletions components/ocs_io/i2c/types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* SPDX-FileCopyrightText: 2025 Tendry Lab
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include <cstdint>

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
5 changes: 2 additions & 3 deletions components/ocs_sensor/sht4x/sensor_pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
11 changes: 5 additions & 6 deletions projects/sht4x-verifier/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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::StorageBuilder> storage_builder(
Expand Down