I2C: Add explicit pinmap support

pull/11892/head
Przemyslaw Stekiel 2019-08-28 13:19:10 +02:00
parent 2185e80e08
commit 80b46769aa
4 changed files with 36 additions and 0 deletions

View File

@ -101,6 +101,12 @@ public:
*/ */
I2C(PinName sda, PinName scl); 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 /** Set the frequency of the I2C interface
* *
* @param hz The bus frequency in hertz * @param hz The bus frequency in hertz

View File

@ -86,6 +86,12 @@ public:
*/ */
I2CSlave(PinName sda, PinName scl); 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. /** Set the frequency of the I2C interface.
* *
* @param hz The bus frequency in Hertz. * @param hz The bus frequency in Hertz.

View File

@ -47,6 +47,23 @@ I2C::I2C(PinName sda, PinName scl) :
unlock(); 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) void I2C::frequency(int hz)
{ {
lock(); lock();

View File

@ -27,6 +27,13 @@ I2CSlave::I2CSlave(PinName sda, PinName scl) : _i2c()
i2c_slave_mode(&_i2c, 1); 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) void I2CSlave::frequency(int hz)
{ {
i2c_frequency(&_i2c, hz); i2c_frequency(&_i2c, hz);