Skip to content

Commit 80b4676

Browse files
committed
I2C: Add explicit pinmap support
1 parent 2185e80 commit 80b4676

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

drivers/I2C.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ class I2C : private NonCopyable<I2C> {
101101
*/
102102
I2C(PinName sda, PinName scl);
103103

104+
/** Create an I2C Master interface, connected to the specified pins
105+
*
106+
* @param explicit_pinmap reference to strucure which holds static pinmap.
107+
*/
108+
I2C(const i2c_pinmap_t &explicit_pinmap);
109+
104110
/** Set the frequency of the I2C interface
105111
*
106112
* @param hz The bus frequency in hertz

drivers/I2CSlave.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ class I2CSlave {
8686
*/
8787
I2CSlave(PinName sda, PinName scl);
8888

89+
/** Create an I2C Slave interface, connected to the specified pins.
90+
*
91+
* @param explicit_pinmap reference to strucure which holds static pinmap.
92+
*/
93+
I2CSlave(const i2c_pinmap_t &explicit_pinmap);
94+
8995
/** Set the frequency of the I2C interface.
9096
*
9197
* @param hz The bus frequency in Hertz.

drivers/source/I2C.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,23 @@ I2C::I2C(PinName sda, PinName scl) :
4747
unlock();
4848
}
4949

50+
I2C::I2C(const i2c_pinmap_t &explicit_pinmap) :
51+
#if DEVICE_I2C_ASYNCH
52+
_irq(this), _usage(DMA_USAGE_NEVER), _deep_sleep_locked(false),
53+
#endif
54+
_i2c(), _hz(100000)
55+
{
56+
lock();
57+
// The init function also set the frequency to 100000
58+
_sda = explicit_pinmap.sda_pin;
59+
_scl = explicit_pinmap.scl_pin;
60+
recover(explicit_pinmap.sda_pin, explicit_pinmap.scl_pin);
61+
i2c_init_direct(&_i2c, &explicit_pinmap);
62+
// Used to avoid unnecessary frequency updates
63+
_owner = this;
64+
unlock();
65+
}
66+
5067
void I2C::frequency(int hz)
5168
{
5269
lock();

drivers/source/I2CSlave.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ I2CSlave::I2CSlave(PinName sda, PinName scl) : _i2c()
2727
i2c_slave_mode(&_i2c, 1);
2828
}
2929

30+
I2CSlave::I2CSlave(const i2c_pinmap_t &explicit_pinmap) : _i2c()
31+
{
32+
i2c_init_direct(&_i2c, &explicit_pinmap);
33+
i2c_frequency(&_i2c, 100000);
34+
i2c_slave_mode(&_i2c, 1);
35+
}
36+
3037
void I2CSlave::frequency(int hz)
3138
{
3239
i2c_frequency(&_i2c, hz);

0 commit comments

Comments
 (0)