@@ -67,12 +67,35 @@ void TwoWire::begin(int sdaPin, int sclPin, uint32_t frequency)
67
67
return ;
68
68
}
69
69
}
70
+
71
+ if (!initHardware (sdaPin, sclPin, frequency)) return ;
72
+
73
+ flush ();
74
+
75
+ }
76
+
77
+ void TwoWire::setTimeOut (uint16_t timeOutMillis){
78
+ _timeOutMillis = timeOutMillis;
79
+ }
80
+
81
+ uint16_t TwoWire::getTimeOut (){
82
+ return _timeOutMillis;
83
+ }
84
+
85
+ void TwoWire::setClock (uint32_t frequency)
86
+ {
87
+ i2cSetFrequency (i2c, frequency);
88
+ }
89
+
90
+ /* @StickBreaker common handler for processing the queued commands
91
+ */
92
+ bool TwoWire::initHardware (int sdaPin, int sclPin, uint32_t frequency){
70
93
71
94
i2cDetachSCL (i2c,scl); // detach pins before resetting I2C perpherial
72
95
i2cDetachSDA (i2c,sda); // else a glitch will appear on the i2c bus
73
96
i2c = i2cInit (num);// i2cInit() now performs a hardware reset
74
97
if (i2c == NULL ) {
75
- return ;
98
+ return false ;
76
99
}
77
100
78
101
i2cSetFrequency (i2c, frequency);
@@ -104,33 +127,14 @@ void TwoWire::begin(int sdaPin, int sclPin, uint32_t frequency)
104
127
105
128
i2cAttachSDA (i2c, sda);
106
129
i2cAttachSCL (i2c, scl);
130
+ return true ;
131
+ }
107
132
108
- flush ();
109
-
110
- /* This function should no longer be necessary, the
111
- i2cInitFix(i2c);
112
- */
113
- }
114
-
115
- void TwoWire::setTimeOut (uint16_t timeOutMillis){
116
- _timeOutMillis = timeOutMillis;
117
- }
118
-
119
- uint16_t TwoWire::getTimeOut (){
120
- return _timeOutMillis;
121
- }
122
-
123
- void TwoWire::setClock (uint32_t frequency)
124
- {
125
- i2cSetFrequency (i2c, frequency);
126
- }
127
- /* @StickBreaker common handler for processing the queued commands
128
- */
129
133
i2c_err_t TwoWire::processQueue (uint32_t * readCount){
130
134
last_error=i2cProcQueue (i2c,readCount,_timeOutMillis);
131
135
if (last_error==I2C_ERROR_BUSY){ // try to clear the bus
132
- begin ( sda,scl,getClock ());
133
- last_error=i2cProcQueue (i2c,readCount,_timeOutMillis);
136
+ if ( initHardware ( sda,scl,getClock ()))
137
+ last_error=i2cProcQueue (i2c,readCount,_timeOutMillis);
134
138
}
135
139
136
140
rxIndex = 0 ;
0 commit comments