Skip to content

Commit c571e1b

Browse files
authored
Create Destructor for Wire Class.
This destructor allows reassigning either hardware peripheral to the default Wire() objects. sequence: Wire.~TwoWire(); Wire = TwoWire(peripherial); // peripheral is either 0 or 1 Wire.begin(sda,scl); // sda, scl are the pins to use, when using peripheral 1, YOU MUST specifiy the pins, there ARE NO DEFAULT VALUES for peripheral 1.
1 parent a0db0cc commit c571e1b

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

libraries/Wire/src/Wire.cpp

+16-10
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ TwoWire::TwoWire(uint8_t bus_num)
5050
,_dump(false)
5151
{}
5252

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+
5363
void TwoWire::begin(int sdaPin, int sclPin, uint32_t frequency)
5464
{
5565
if(sdaPin < 0) {
@@ -102,6 +112,9 @@ bool TwoWire::initHardware(int sdaPin, int sclPin, uint32_t frequency){
102112

103113
sda = sdaPin;
104114
scl = sclPin;
115+
116+
// 03/15/2018 What about MultiMaster? How can I be polite and still catch glitches?
117+
105118
// 03/10/2018 test I2C bus before attach.
106119
// if the bus is not 'clear' try the recommended recovery sequence, START, 9 Clocks, STOP
107120
digitalWrite(sda,HIGH);
@@ -110,7 +123,7 @@ bool TwoWire::initHardware(int sdaPin, int sclPin, uint32_t frequency){
110123
pinMode(scl,PULLUP|OPEN_DRAIN|OUTPUT|INPUT);
111124

112125
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));
114127
digitalWrite(sda,HIGH);
115128
digitalWrite(scl,HIGH);
116129
delayMicroseconds(5);
@@ -128,7 +141,7 @@ bool TwoWire::initHardware(int sdaPin, int sclPin, uint32_t frequency){
128141
i2cAttachSCL(i2c, scl);
129142

130143
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");
132145
return false; // bus is busy
133146
}
134147

@@ -199,7 +212,7 @@ uint16_t TwoWire::requestFrom(uint16_t address, uint8_t * readBuff, uint16_t siz
199212
*/
200213
i2c_err_t TwoWire::writeTransmission(uint16_t address, uint8_t *buff, uint16_t size, bool sendStop){
201214
// will destroy any partially created beginTransaction()
202-
215+
log_i("i2c=%p",i2c);
203216
last_error=i2cAddQueueWrite(i2c,address,buff,size,sendStop,NULL);
204217

205218
if(last_error==I2C_ERROR_OK){ //queued
@@ -466,12 +479,5 @@ void TwoWire::flush(void)
466479
i2cFreeQueue(i2c); // cleanup
467480
}
468481

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-
}
476482

477483
TwoWire Wire = TwoWire(0);

libraries/Wire/src/Wire.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include "freertos/queue.h"
3131
#include "Stream.h"
3232

33-
#define STICKBREAKER
33+
#define STICKBREAKER V0.2.1
3434
#define I2C_BUFFER_LENGTH 128
3535
typedef void(*user_onRequest)(void);
3636
typedef void(*user_onReceive)(uint8_t*, int);
@@ -69,6 +69,7 @@ class TwoWire: public Stream
6969

7070
public:
7171
TwoWire(uint8_t bus_num);
72+
~TwoWire();
7273
void begin(int sda=-1, int scl=-1, uint32_t frequency=100000);
7374
void setClock(uint32_t);
7475
void beginTransmission(uint16_t);
@@ -113,8 +114,6 @@ class TwoWire: public Stream
113114
int peek(void);
114115
void flush(void);
115116

116-
void reset(void);
117-
118117
inline size_t write(const char * s)
119118
{
120119
return write((uint8_t*) s, strlen(s));
@@ -139,4 +138,8 @@ class TwoWire: public Stream
139138

140139
extern TwoWire Wire;
141140

141+
142+
/*
143+
V0.2.1 15MAR2018 Hardware reset, Glitch prevention, adding destructor for second i2c testing
144+
*/
142145
#endif

0 commit comments

Comments
 (0)