Skip to content

Commit

Permalink
i2c: redesign slave module address ownership
Browse files Browse the repository at this point in the history
  • Loading branch information
benedekkupper committed Jun 29, 2024
1 parent 7fe6f80 commit 67dc05e
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 22 deletions.
2 changes: 2 additions & 0 deletions c2usb/i2c/base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class address
bool is_10bit() const { return (_code & MODE_MASK) == static_cast<uint16_t>(mode::_10BIT); }
uint16_t raw() const { return _code; }

constexpr bool operator==(const address& rhs) const = default;

// special reserved addresses
constexpr static address general_call() { return address(0); }
constexpr static address start_byte() { return address(1); }
Expand Down
8 changes: 4 additions & 4 deletions c2usb/i2c/hid/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ using namespace i2c::hid;

device::device(application& app, const product_info& pinfo, i2c::slave& slave, i2c::address address,
uint16_t hid_descriptor_reg_address)
: app_(app),
: module(address),
app_(app),
pinfo_(pinfo),
slave_(slave),
bus_address_(address),
hid_descriptor_reg_(hid_descriptor_reg_address)
{
slave_.register_module(this, address);
slave_.register_module(*this);
}

device::~device()
Expand All @@ -32,7 +32,7 @@ device::~device()
slave().set_pin_interrupt(false);

// disable I2C
slave().unregister_module(this);
slave().unregister_module(*this);

// clear context
get_report_.clear();
Expand Down
2 changes: 0 additions & 2 deletions c2usb/i2c/hid/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ class device : public slave::module, public ::hid::transport
private:
void link_reset();

i2c::address bus_address() const { return bus_address_; }
uint16_t hid_descriptor_reg_address() const { return hid_descriptor_reg_; }
void get_hid_descriptor(descriptor& desc) const;
result send_report(const std::span<const uint8_t>& data, ::hid::report::type type) override;
Expand Down Expand Up @@ -112,7 +111,6 @@ class device : public slave::module, public ::hid::transport
power_event_delegate power_event_delegate_{};
::hid::reports_receiver rx_buffers_{};
i2c::slave& slave_;
i2c::address bus_address_;
uint16_t hid_descriptor_reg_;
single_elem_queue<std::span<const uint8_t>> in_queue_{};
uint8_t stage_{};
Expand Down
37 changes: 24 additions & 13 deletions c2usb/i2c/slave.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,44 +19,54 @@ namespace i2c
class slave : public polymorphic
{
public:
/// @brief The module class is a callback interface for I2C slave functionality
/// @brief The module class is an abstract base for I2C slave functionality
/// at a specific address.
class module : public interface
class module : public polymorphic
{
public:
constexpr module(i2c::address slave_addr)
: slave_addr_(slave_addr)
{}
virtual bool on_start(direction dir, size_t data_length) = 0;
virtual void on_stop(direction dir, size_t data_length) = 0;
i2c::address address() const { return slave_addr_; }

private:
i2c::address slave_addr_;
};

bool has_module([[maybe_unused]] address slave_addr) const { return module_ != nullptr; }
bool has_module() const { return module_ != nullptr; }
bool has_module([[maybe_unused]] address slave_addr) const
{
return has_module() and (module_->address() == slave_addr);
}

/// @brief Registers a slave module on the I2C bus.
/// @param m: module pointer
/// @param slave_addr: the I2C slave address to listen to
/// @param m: module reference
/// @return true if module registered
bool register_module(module* m, address slave_addr)
bool register_module(module& m)
{
// single module design at the moment
if (module_ != nullptr)
{
return false;
}
module_ = m;
start_listen(slave_addr);
module_ = &m;
start_listen(module_->address());
return true;
}

/// @brief Unregisters the active module from the I2C bus interface.
/// @param m: module pointer
/// @param m: module reference
/// @return true if module unregistered
bool unregister_module(module* m)
bool unregister_module(module& m)
{
// single module design at the moment
if (module_ != m)
if (module_ != &m)
{
return false;
}
stop_listen();
stop_listen(module_->address());
module_ = nullptr;
return true;
}
Expand Down Expand Up @@ -118,7 +128,8 @@ class slave : public polymorphic
virtual void start_listen(address slave_addr) = 0;

/// @brief Stop listening to I2C transfers
virtual void stop_listen() = 0;
/// @param slave_addr: the I2C address to stop listening on
virtual void stop_listen(address slave_addr) = 0;

/// @brief Call the module to handle I2C (re)start events.
/// @param dir: the transfer's direction
Expand Down
1 change: 1 addition & 0 deletions c2usb/port/zephyr/bluetooth/hid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ template <::hid::report_protocol_properties REPORT_PROPS, auto BOOT = boot_proto
class service_instance : public service
{
friend class service;

public:
service_instance(::hid::application& app, security sec,
flags f = (flags)((uint8_t)flags::REMOTE_WAKE |
Expand Down
1 change: 0 additions & 1 deletion c2usb/usb/df/class/cdc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include "usb/class/cdc.hpp"
#include "usb/df/function.hpp"


namespace usb::standard::descriptor
{
struct interface;
Expand Down
4 changes: 2 additions & 2 deletions c2usb/usb/df/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ class power
constexpr auto max_power_mA() const { return value_ >> 7; }
constexpr bool valid() const { return value_ != 0; }

friend auto operator<<(standard::descriptor::configuration* desc, const power& p)
-> standard::descriptor::configuration*;
friend auto operator<<(standard::descriptor::configuration* desc,
const power& p) -> standard::descriptor::configuration*;

protected:
constexpr power(source src = source::BUS, uint16_t max_current_mA = 100,
Expand Down

0 comments on commit 67dc05e

Please sign in to comment.