M5UnitUnified 0.6.3 git rev:e38fc73
Loading...
Searching...
No Matches
m5::unit::AdapterI2C Class Reference

I2C access adapter. More...

#include <adapter_i2c.hpp>

Inheritance diagram for m5::unit::AdapterI2C:
m5::unit::Adapter

Classes

class  BusImpl
 
class  I2CClassImpl
 
class  I2CImpl
 

Public Types

enum class  ImplType : uint8_t { Unknown , TwoWire , Bus , I2CClass }
 I2C implementation type. More...
 
- Public Types inherited from m5::unit::Adapter
enum class  Type : uint8_t {
  Unknown , I2C , GPIO , UART ,
  SPI
}
 Adapter type. More...
 

Public Member Functions

 AdapterI2C (m5::hal::bus::Bus *bus, const uint8_t addr, const uint32_t clock)
 
 AdapterI2C (m5::I2C_Class &i2c, const uint8_t addr, const uint32_t clock)
 
 AdapterI2C (m5::hal::bus::Bus &bus, const uint8_t addr, const uint32_t clock)
 
I2CImpl * impl ()
 
const I2CImpl * impl () const
 
uint8_t address () const
 
void setAddress (const uint8_t addr)
 
uint32_t clock () const
 
void setClock (const uint32_t clock)
 
ImplType implType () const
 Gets the I2C implementation type.
 
int16_t port () const
 I2C port number, or -1 if unknown (TwoWire, Bus, or a bus handle added without its port)
 
int16_t scl () const
 
int16_t sda () const
 
virtual Adapter * duplicate (const uint8_t addr) override
 Create a duplicate adapter with a different address.
 
bool releasePins ()
 Detach SDA/SCL from the I2C controller and make them GPIO.
 
bool restorePins ()
 Reattach SDA/SCL to the I2C controller as they were before releasePins()
 
Temporary API
Warning
Functionality required for a specific unit
Will be improved when integrated with M5HAL
bool begin ()
 
bool end ()
 
bool pushPin ()
 
bool popPin ()
 
- Public Member Functions inherited from m5::unit::Adapter
 Adapter ()
 Default constructor (creates Unknown type adapter)
 
 Adapter (const Adapter &)=delete
 
 Adapter (Adapter &&) noexcept=default
 
Adapter & operator= (const Adapter &)=delete
 
Adapter & operator= (Adapter &&) noexcept=default
 
Type type () const
 Gets the adapter type.
 
virtual void beginTransaction ()
 Begin a bus transaction.
 
virtual void endTransaction ()
 End a bus transaction.
 
m5::hal::error::error_t readWithTransaction (uint8_t *data, const size_t len)
 Read data within a transaction.
 
m5::hal::error::error_t writeWithTransaction (const uint8_t *data, const size_t len, const uint32_t exparam=1)
 Read data within a transaction.
 
m5::hal::error::error_t writeWithTransaction (const uint8_t reg, const uint8_t *data, const size_t len, const uint32_t exparam=1)
 Read data within a transaction.
 
m5::hal::error::error_t writeWithTransaction (const uint16_t reg, const uint8_t *data, const size_t len, const uint32_t exparam=1)
 Read data within a transaction.
 
m5::hal::error::error_t generalCall (const uint8_t *data, const size_t len)
 Send a general call on the I2C bus.
 
m5::hal::error::error_t writeDigitalRX (const bool high)
 
m5::hal::error::error_t readDigitalRX (bool &high)
 
m5::hal::error::error_t writeAnalogRX (const uint16_t v)
 
m5::hal::error::error_t readAnalogRX (uint16_t &v)
 
m5::hal::error::error_t readAnalogMilliVoltsRX (uint32_t &mv)
 
m5::hal::error::error_t pulseInRX (uint32_t &duration, const int state, const uint32_t timeout_us)
 
m5::hal::error::error_t writeDigitalTX (const bool high)
 
m5::hal::error::error_t readDigitalTX (bool &high)
 
m5::hal::error::error_t writeAnalogTX (const uint16_t v)
 
m5::hal::error::error_t readAnalogTX (uint16_t &v)
 
m5::hal::error::error_t readAnalogMilliVoltsTX (uint32_t &mv)
 
m5::hal::error::error_t pulseInTX (uint32_t &duration, const int state, const uint32_t timeout_us)
 

Protected Attributes

gpio::pin_backup_t _backupSCL {-1}
 
gpio::pin_backup_t _backupSDA {-1}
 
- Protected Attributes inherited from m5::unit::Adapter
std::unique_ptr< Impl > _impl {}
 

Additional Inherited Members

- Protected Member Functions inherited from m5::unit::Adapter
 Adapter (const Type t, Impl *impl)
 

Detailed Description

I2C access adapter.

Member Enumeration Documentation

◆ ImplType

enum class m5::unit::AdapterI2C::ImplType : uint8_t
strong

I2C implementation type.

Enumerator
Unknown 

Unknown.

TwoWire 

Arduino TwoWire.

Bus 

M5HAL Bus (including SoftwareI2C)

I2CClass 

m5::I2C_Class (m5gfx::i2c)

Member Function Documentation

◆ duplicate()

Adapter * m5::unit::AdapterI2C::duplicate ( const uint8_t )
overridevirtual

Create a duplicate adapter with a different address.

Reimplemented from m5::unit::Adapter.

◆ releasePins()

bool m5::unit::AdapterI2C::releasePins ( )

Detach SDA/SCL from the I2C controller and make them GPIO.

For sending a frame an I2C controller cannot send (e.g. a few bits, no address). SDA/SCL become open-drain GPIO outputs, left high with the internal pull-ups, and the controller is fed an idle (high) bus so it does not take the GPIO waveform as bus traffic. Drive them with the GPIO API (gpio_set_level etc.), then call restorePins()

Returns
True if successful. False (with nothing changed) if
  • SDA/SCL are unknown (sda() / scl() return -1, e.g. add(unit, bus) without the port and pins)
  • the pins are not routed through the GPIO matrix: LP I2C on its fixed pads (ESP32-C6 / C5 Wire1, ESP-IDF LP_I2C_NUM_0) and ESP32-P4 GPIO0-15 used as LP IO
  • the build is not for ESP (native / host)
Note
A software bus (M5HAL Bus) has no controller to detach, so its pins are just made GPIO
Warning
Do not access any unit on the same bus until restorePins()

◆ restorePins()

bool m5::unit::AdapterI2C::restorePins ( )

Reattach SDA/SCL to the I2C controller as they were before releasePins()

Returns
True if successful