@@ -50,6 +50,16 @@ TwoWire::TwoWire(uint8_t bus_num)
50
50
,_dump(false )
51
51
{}
52
52
53
+ TwoWire::~TwoWire (){
54
+ flush ();
55
+ i2cDetachSCL (i2c,scl); // detach pins before resetting I2C perpherial
56
+ i2cDetachSDA (i2c,sda); // else a glitch will appear on the i2c bus
57
+ if (i2c){
58
+ i2cReleaseAll (i2c);
59
+ i2c=NULL ;
60
+ }
61
+ }
62
+
53
63
void TwoWire::begin (int sdaPin, int sclPin, uint32_t frequency)
54
64
{
55
65
if (sdaPin < 0 ) {
@@ -102,6 +112,9 @@ bool TwoWire::initHardware(int sdaPin, int sclPin, uint32_t frequency){
102
112
103
113
sda = sdaPin;
104
114
scl = sclPin;
115
+
116
+ // 03/15/2018 What about MultiMaster? How can I be polite and still catch glitches?
117
+
105
118
// 03/10/2018 test I2C bus before attach.
106
119
// if the bus is not 'clear' try the recommended recovery sequence, START, 9 Clocks, STOP
107
120
digitalWrite (sda,HIGH);
@@ -110,7 +123,7 @@ bool TwoWire::initHardware(int sdaPin, int sclPin, uint32_t frequency){
110
123
pinMode (scl,PULLUP|OPEN_DRAIN|OUTPUT|INPUT);
111
124
112
125
if (!digitalRead (sda)||!digitalRead (scl)){ // bus in busy state
113
- // Serial.printf ("invalid state sda=%d, scl=%d\n",digitalRead(sda),digitalRead(scl));
126
+ log_e (" invalid state sda=%d, scl=%d\n " ,digitalRead (sda),digitalRead (scl));
114
127
digitalWrite (sda,HIGH);
115
128
digitalWrite (scl,HIGH);
116
129
delayMicroseconds (5 );
@@ -128,7 +141,7 @@ bool TwoWire::initHardware(int sdaPin, int sclPin, uint32_t frequency){
128
141
i2cAttachSCL (i2c, scl);
129
142
130
143
if (!digitalRead (sda)||!digitalRead (scl)){ // bus in busy state
131
- // Serial.println ("Bus Invalid State, TwoWire() Can't init");
144
+ log_e (" Bus Invalid State, TwoWire() Can't init" );
132
145
return false ; // bus is busy
133
146
}
134
147
@@ -199,7 +212,7 @@ uint16_t TwoWire::requestFrom(uint16_t address, uint8_t * readBuff, uint16_t siz
199
212
*/
200
213
i2c_err_t TwoWire::writeTransmission (uint16_t address, uint8_t *buff, uint16_t size, bool sendStop){
201
214
// will destroy any partially created beginTransaction()
202
-
215
+ log_i ( " i2c=%p " ,i2c);
203
216
last_error=i2cAddQueueWrite (i2c,address,buff,size,sendStop,NULL );
204
217
205
218
if (last_error==I2C_ERROR_OK){ // queued
@@ -466,12 +479,5 @@ void TwoWire::flush(void)
466
479
i2cFreeQueue (i2c); // cleanup
467
480
}
468
481
469
- void TwoWire::reset (void )
470
- {
471
- i2cReleaseISR (i2c); // remove ISR from Interrupt chain,Delete EventGroup,Free Heap memory
472
- i2cReset ( i2c );
473
- i2c = NULL ;
474
- begin ( sda, scl );
475
- }
476
482
477
483
TwoWire Wire = TwoWire(0 );
0 commit comments