Skip to content

Commit 17a3933

Browse files
committed
Add setPullups function to control the pullup resistors on the I2C instance.
1 parent 2a0cd23 commit 17a3933

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

libraries/Wire/src/Wire.cpp

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ typedef enum
4141
TwoWire::TwoWire(uint8_t iom_instance) : IOMaster(iom_instance)
4242
{
4343
_transmissionBegun = false;
44+
_pullups = AM_HAL_GPIO_PIN_PULLUP_1_5K; //Default
45+
_clockSpeed = AM_HAL_IOM_100KHZ;
4446
}
4547

4648
void TwoWire::begin(void)
@@ -58,7 +60,7 @@ void TwoWire::begin(void)
5860
return /*retval*/;
5961
}
6062
pincfg.uFuncSel = funcsel; // set the proper function select option for this instance/pin/type combination
61-
pincfg.ePullup = AM_HAL_GPIO_PIN_PULLUP_1_5K;
63+
pincfg.ePullup = _pullups;
6264
pincfg.eDriveStrength = AM_HAL_GPIO_PIN_DRIVESTRENGTH_12MA;
6365
pincfg.eGPOutcfg = AM_HAL_GPIO_PIN_OUTCFG_OPENDRAIN;
6466
pincfg.uIOMnum = _instance;
@@ -75,7 +77,7 @@ void TwoWire::begin(void)
7577
return /*retval*/;
7678
}
7779
pincfg.uFuncSel = funcsel;
78-
pincfg.ePullup = AM_HAL_GPIO_PIN_PULLUP_1_5K;
80+
pincfg.ePullup = _pullups;
7981
pincfg.eDriveStrength = AM_HAL_GPIO_PIN_DRIVESTRENGTH_12MA;
8082
pincfg.eGPOutcfg = AM_HAL_GPIO_PIN_OUTCFG_OPENDRAIN;
8183
pincfg.uIOMnum = _instance;
@@ -88,7 +90,7 @@ void TwoWire::begin(void)
8890

8991
memset((void *)&_config, 0x00, sizeof(am_hal_iom_config_t)); // Set the IOM configuration
9092
_config.eInterfaceMode = AM_HAL_IOM_I2C_MODE;
91-
_config.ui32ClockFreq = AM_HAL_IOM_100KHZ;
93+
_config.ui32ClockFreq = _clockSpeed;
9294

9395
initialize(); // Initialize the IOM
9496
}
@@ -103,11 +105,28 @@ void TwoWire::begin(uint8_t address, bool enableGeneralCall)
103105
void TwoWire::setClock(uint32_t baudrate)
104106
{
105107
// ToDo: disable I2C while switching, if necessary
106-
107-
_config.ui32ClockFreq = baudrate;
108+
_clockSpeed = baudrate;
109+
_config.ui32ClockFreq = _clockSpeed;
108110
initialize(); // Initialize the IOM
109111
}
110112

113+
void TwoWire::setPullups(uint32_t pullupAmount)
114+
{
115+
if (pullupAmount == 0)
116+
_pullups = AM_HAL_GPIO_PIN_PULLUP_NONE;
117+
if (pullupAmount > 0 || pullupAmount < 6)
118+
_pullups = AM_HAL_GPIO_PIN_PULLUP_1_5K;
119+
else if (pullupAmount >= 6 || pullupAmount < 12)
120+
_pullups = AM_HAL_GPIO_PIN_PULLUP_6K;
121+
else if (pullupAmount >= 12 || pullupAmount < 24)
122+
_pullups = AM_HAL_GPIO_PIN_PULLUP_12K;
123+
else if (pullupAmount >= 24)
124+
_pullups = AM_HAL_GPIO_PIN_PULLUP_24K;
125+
126+
//Reinit I2C pins with this new pullup value
127+
begin();
128+
}
129+
111130
void TwoWire::end()
112131
{
113132
// sercom->disableWIRE();

libraries/Wire/src/Wire.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class TwoWire : public Stream, public IOMaster
4343
void begin(uint8_t, bool enableGeneralCall = false);
4444
void end();
4545
void setClock(uint32_t);
46+
void setPullups(uint32_t);
4647

4748
void beginTransmission(uint8_t address);
4849
uint8_t endTransmission(bool stopBit = true);
@@ -73,6 +74,8 @@ class TwoWire : public Stream, public IOMaster
7374

7475
bool _transmissionBegun;
7576
uint8_t _transmissionAddress;
77+
uint32_t _pullups;
78+
uint32_t _clockSpeed;
7679

7780
RingBufferN<AP3_WIRE_RX_BUFFER_LEN> _rxBuffer; // RX Buffer
7881
RingBufferN<AP3_WIRE_TX_BUFFER_LEN> _txBuffer; // TX buffer

0 commit comments

Comments
 (0)