diff --git a/drivers/I2C.h b/drivers/I2C.h index 4fb06a3d26..e6190ee485 100644 --- a/drivers/I2C.h +++ b/drivers/I2C.h @@ -101,6 +101,12 @@ public: */ I2C(PinName sda, PinName scl); + /** Create an I2C Master interface, connected to the specified pins + * + * @param explicit_pinmap reference to strucure which holds static pinmap. + */ + I2C(const i2c_pinmap_t &explicit_pinmap); + /** Set the frequency of the I2C interface * * @param hz The bus frequency in hertz diff --git a/drivers/I2CSlave.h b/drivers/I2CSlave.h index 000d480f74..c4f255a717 100644 --- a/drivers/I2CSlave.h +++ b/drivers/I2CSlave.h @@ -86,6 +86,12 @@ public: */ I2CSlave(PinName sda, PinName scl); + /** Create an I2C Slave interface, connected to the specified pins. + * + * @param explicit_pinmap reference to strucure which holds static pinmap. + */ + I2CSlave(const i2c_pinmap_t &explicit_pinmap); + /** Set the frequency of the I2C interface. * * @param hz The bus frequency in Hertz. diff --git a/drivers/source/I2C.cpp b/drivers/source/I2C.cpp index 7857d22c6d..8691f66a1f 100644 --- a/drivers/source/I2C.cpp +++ b/drivers/source/I2C.cpp @@ -47,6 +47,23 @@ I2C::I2C(PinName sda, PinName scl) : unlock(); } +I2C::I2C(const i2c_pinmap_t &explicit_pinmap) : +#if DEVICE_I2C_ASYNCH + _irq(this), _usage(DMA_USAGE_NEVER), _deep_sleep_locked(false), +#endif + _i2c(), _hz(100000) +{ + lock(); + // The init function also set the frequency to 100000 + _sda = explicit_pinmap.sda_pin; + _scl = explicit_pinmap.scl_pin; + recover(explicit_pinmap.sda_pin, explicit_pinmap.scl_pin); + i2c_init_direct(&_i2c, &explicit_pinmap); + // Used to avoid unnecessary frequency updates + _owner = this; + unlock(); +} + void I2C::frequency(int hz) { lock(); diff --git a/drivers/source/I2CSlave.cpp b/drivers/source/I2CSlave.cpp index 489926c146..d4619df91a 100644 --- a/drivers/source/I2CSlave.cpp +++ b/drivers/source/I2CSlave.cpp @@ -27,6 +27,13 @@ I2CSlave::I2CSlave(PinName sda, PinName scl) : _i2c() i2c_slave_mode(&_i2c, 1); } +I2CSlave::I2CSlave(const i2c_pinmap_t &explicit_pinmap) : _i2c() +{ + i2c_init_direct(&_i2c, &explicit_pinmap); + i2c_frequency(&_i2c, 100000); + i2c_slave_mode(&_i2c, 1); +} + void I2CSlave::frequency(int hz) { i2c_frequency(&_i2c, hz);