Skip to content

Commit da6be57

Browse files
authored
Preserve Custom SDA,SCL and Frequency when calling Wire.begin()
This change will not reset the SCL, SDA and Frequency values to their default value if `Wire.begin()` is call without any parameters. Arduino libraries that use `Wire()` usually have a call to `Wire.begin()` inside their initialization call. Without this change, any custom pin assignments are lost whenever `Wire.begin()` is called.
1 parent dd0ac4b commit da6be57

File tree

1 file changed

+34
-17
lines changed

1 file changed

+34
-17
lines changed

libraries/Wire/src/Wire.cpp

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,25 +62,37 @@ if(i2c){
6262

6363
void TwoWire::begin(int sdaPin, int sclPin, uint32_t frequency)
6464
{
65-
if(sdaPin < 0) {
66-
if(num == 0) {
67-
sdaPin = SDA;
68-
} else {
69-
return;
70-
}
65+
if(sdaPin < 0) { // default param passed
66+
if(num == 0) {
67+
if(sda==-1) sdaPin = SDA; //use Default Pin
68+
else sdaPin = sda; // reuse prior pin
69+
}
70+
else {
71+
if(sda==-1) {
72+
log_e("no Default SDA Pin for Second Peripheral");
73+
return; //no Default pin for Second Peripheral
74+
}
75+
else sdaPin = sda; // reuse prior pin
76+
}
7177
}
7278

73-
if(sclPin < 0) {
74-
if(num == 0) {
75-
sclPin = SCL;
76-
} else {
77-
return;
79+
if(sclPin < 0) { // default param passed
80+
if(num == 0) {
81+
if(scl==-1) sclPin = SCL; // use Default pin
82+
else sclPin = scl; // reuse prior pin
83+
}
84+
else {
85+
if(scl==-1){
86+
log_e("no Default SCL Pin for Second Peripheral");
87+
return; //no Default pin for Second Peripheral
7888
}
89+
else sclPin = scl; // reuse prior pin
90+
}
7991
}
80-
81-
if(!initHardware(sdaPin, sclPin, frequency)) return;
8292

83-
flush();
93+
if(!initHardware(sdaPin, sclPin, frequency)) return;
94+
95+
flush();
8496

8597
}
8698

@@ -97,8 +109,6 @@ void TwoWire::setClock(uint32_t frequency)
97109
i2cSetFrequency(i2c, frequency);
98110
}
99111

100-
/*@StickBreaker common handler for processing the queued commands
101-
*/
102112
bool TwoWire::initHardware(int sdaPin, int sclPin, uint32_t frequency){
103113

104114
i2cDetachSCL(i2c,scl); // detach pins before resetting I2C perpherial
@@ -107,7 +117,12 @@ bool TwoWire::initHardware(int sdaPin, int sclPin, uint32_t frequency){
107117
if(i2c == NULL) {
108118
return false;
109119
}
110-
120+
121+
if(frequency==0) {// don't change existing frequency
122+
frequency = i2cGetFrequency(i2c);
123+
}
124+
if(frequency==0) frequency = 100000L; // default to 100khz
125+
111126
i2cSetFrequency(i2c, frequency);
112127

113128
sda = sdaPin;
@@ -148,6 +163,8 @@ bool TwoWire::initHardware(int sdaPin, int sclPin, uint32_t frequency){
148163
return true;
149164
}
150165

166+
/*@StickBreaker common handler for processing the queued commands
167+
*/
151168
i2c_err_t TwoWire::processQueue(uint32_t * readCount){
152169
last_error=i2cProcQueue(i2c,readCount,_timeOutMillis);
153170
if(last_error==I2C_ERROR_BUSY){ // try to clear the bus

0 commit comments

Comments
 (0)