Skip to content

Commit cbb5a59

Browse files
committed
Change Wire to Master-only I2C interface
1 parent 9dd52f1 commit cbb5a59

File tree

2 files changed

+38
-145
lines changed

2 files changed

+38
-145
lines changed

libraries/Wire/Wire.cpp

Lines changed: 21 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -38,71 +38,36 @@ extern "C" {
3838
#error Wire library is not supported on this board
3939
#endif
4040

41-
// Initialize Class Variables //////////////////////////////////////////////////
42-
43-
uint8_t TwoWire::rxBuffer[BUFFER_LENGTH];
44-
uint8_t TwoWire::rxBufferIndex = 0;
45-
uint8_t TwoWire::rxBufferLength = 0;
46-
47-
uint8_t TwoWire::txAddress = 0;
48-
uint8_t TwoWire::txBuffer[BUFFER_LENGTH];
49-
uint8_t TwoWire::txBufferIndex = 0;
50-
uint8_t TwoWire::txBufferLength = 0;
51-
52-
uint8_t TwoWire::transmitting = 0;
53-
void (*TwoWire::user_onRequest)(void);
54-
void (*TwoWire::user_onReceive)(size_t);
55-
56-
static int default_sda_pin = SDA;
57-
static int default_scl_pin = SCL;
58-
5941
// Constructors ////////////////////////////////////////////////////////////////
6042

61-
TwoWire::TwoWire() {}
43+
TwoWire::TwoWire(uint8_t rxBufferSize, uint8_t txBufferSize) :
44+
rxBuffer{new uint8_t[rxBufferSize]},
45+
txBuffer{new uint8_t[txBufferSize]}
46+
{
47+
this->txBufferSize = txBufferSize;
48+
this->rxBufferSize = rxBufferSize;
49+
}
6250

6351
// Public Methods //////////////////////////////////////////////////////////////
6452

6553
void TwoWire::begin(int sda, int scl)
6654
{
67-
default_sda_pin = sda;
68-
default_scl_pin = scl;
69-
twi_init(sda, scl);
70-
flush();
71-
}
72-
73-
void TwoWire::begin(int sda, int scl, uint8_t address)
74-
{
75-
default_sda_pin = sda;
76-
default_scl_pin = scl;
77-
twi_setAddress(address);
78-
twi_init(sda, scl);
79-
twi_attachSlaveTxEvent(onRequestService);
80-
twi_attachSlaveRxEvent(onReceiveService);
55+
twi.init(sda, scl);
8156
flush();
8257
}
8358

8459
void TwoWire::pins(int sda, int scl)
8560
{
86-
default_sda_pin = sda;
87-
default_scl_pin = scl;
8861
}
8962

9063
void TwoWire::begin(void)
9164
{
92-
begin(default_sda_pin, default_scl_pin);
93-
}
94-
95-
void TwoWire::begin(uint8_t address)
96-
{
97-
twi_setAddress(address);
98-
twi_attachSlaveTxEvent(onRequestService);
99-
twi_attachSlaveRxEvent(onReceiveService);
100-
begin();
65+
begin(SDA, SCL);
10166
}
10267

10368
uint8_t TwoWire::status()
10469
{
105-
return twi_status();
70+
return twi.status();
10671
}
10772

10873
void TwoWire::begin(int address)
@@ -112,21 +77,21 @@ void TwoWire::begin(int address)
11277

11378
void TwoWire::setClock(uint32_t frequency)
11479
{
115-
twi_setClock(frequency);
80+
twi.setClock(frequency);
11681
}
11782

11883
void TwoWire::setClockStretchLimit(uint32_t limit)
11984
{
120-
twi_setClockStretchLimit(limit);
85+
twi.setClockStretchLimit(limit);
12186
}
12287

12388
size_t TwoWire::requestFrom(uint8_t address, size_t size, bool sendStop)
12489
{
125-
if (size > BUFFER_LENGTH)
90+
if (size > rxBufferSize)
12691
{
127-
size = BUFFER_LENGTH;
92+
size = rxBufferSize;
12893
}
129-
size_t read = (twi_readFrom(address, rxBuffer, size, sendStop) == 0) ? size : 0;
94+
size_t read = (twi.readFrom(address, rxBuffer.get(), size, sendStop) == 0) ? size : 0;
13095
rxBufferIndex = 0;
13196
rxBufferLength = read;
13297
return read;
@@ -167,7 +132,7 @@ void TwoWire::beginTransmission(int address)
167132

168133
uint8_t TwoWire::endTransmission(uint8_t sendStop)
169134
{
170-
int8_t ret = twi_writeTo(txAddress, txBuffer, txBufferLength, sendStop);
135+
int8_t ret = twi.writeTo(txAddress, txBuffer.get(), txBufferLength, sendStop);
171136
txBufferIndex = 0;
172137
txBufferLength = 0;
173138
transmitting = 0;
@@ -183,7 +148,7 @@ size_t TwoWire::write(uint8_t data)
183148
{
184149
if (transmitting)
185150
{
186-
if (txBufferLength >= BUFFER_LENGTH)
151+
if (txBufferLength >= txBufferSize)
187152
{
188153
setWriteError();
189154
return 0;
@@ -194,7 +159,8 @@ size_t TwoWire::write(uint8_t data)
194159
}
195160
else
196161
{
197-
twi_transmit(&data, 1);
162+
setWriteError();
163+
return 0;
198164
}
199165
return 1;
200166
}
@@ -213,7 +179,8 @@ size_t TwoWire::write(const uint8_t *data, size_t quantity)
213179
}
214180
else
215181
{
216-
twi_transmit(data, quantity);
182+
setWriteError();
183+
return 0;
217184
}
218185
return quantity;
219186
}
@@ -261,72 +228,6 @@ void TwoWire::flush(void)
261228
txBufferLength = 0;
262229
}
263230

264-
void TwoWire::onReceiveService(uint8_t* inBytes, size_t numBytes)
265-
{
266-
// don't bother if user hasn't registered a callback
267-
if (!user_onReceive)
268-
{
269-
return;
270-
}
271-
// // don't bother if rx buffer is in use by a master requestFrom() op
272-
// // i know this drops data, but it allows for slight stupidity
273-
// // meaning, they may not have read all the master requestFrom() data yet
274-
// if(rxBufferIndex < rxBufferLength){
275-
// return;
276-
// }
277-
278-
// copy twi rx buffer into local read buffer
279-
// this enables new reads to happen in parallel
280-
for (uint8_t i = 0; i < numBytes; ++i)
281-
{
282-
rxBuffer[i] = inBytes[i];
283-
}
284-
285-
// set rx iterator vars
286-
rxBufferIndex = 0;
287-
rxBufferLength = numBytes;
288-
289-
// alert user program
290-
user_onReceive(numBytes);
291-
}
292-
293-
void TwoWire::onRequestService(void)
294-
{
295-
// don't bother if user hasn't registered a callback
296-
if (!user_onRequest)
297-
{
298-
return;
299-
}
300-
301-
// reset tx buffer iterator vars
302-
// !!! this will kill any pending pre-master sendTo() activity
303-
txBufferIndex = 0;
304-
txBufferLength = 0;
305-
306-
// alert user program
307-
user_onRequest();
308-
}
309-
310-
void TwoWire::onReceive(void (*function)(int))
311-
{
312-
// arduino api compatibility fixer:
313-
// really hope size parameter will not exceed 2^31 :)
314-
static_assert(sizeof(int) == sizeof(size_t), "something is wrong in Arduino kingdom");
315-
user_onReceive = reinterpret_cast<void(*)(size_t)>(function);
316-
}
317-
318-
void TwoWire::onReceive(void (*function)(size_t))
319-
{
320-
user_onReceive = function;
321-
twi_enableSlaveMode();
322-
}
323-
324-
void TwoWire::onRequest(void (*function)(void))
325-
{
326-
user_onRequest = function;
327-
twi_enableSlaveMode();
328-
}
329-
330231
// Preinstantiate Objects //////////////////////////////////////////////////////
331232

332233
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_TWOWIRE)

libraries/Wire/Wire.h

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,36 +26,31 @@
2626

2727
#include <inttypes.h>
2828
#include "Stream.h"
29-
30-
31-
32-
#define BUFFER_LENGTH 128
29+
#include <memory>
30+
#include <twi.h>
3331

3432
class TwoWire : public Stream
3533
{
3634
private:
37-
static uint8_t rxBuffer[];
38-
static uint8_t rxBufferIndex;
39-
static uint8_t rxBufferLength;
40-
41-
static uint8_t txAddress;
42-
static uint8_t txBuffer[];
43-
static uint8_t txBufferIndex;
44-
static uint8_t txBufferLength;
45-
46-
static uint8_t transmitting;
47-
static void (*user_onRequest)(void);
48-
static void (*user_onReceive)(size_t);
49-
static void onRequestService(void);
50-
static void onReceiveService(uint8_t*, size_t);
35+
TwiMaster twi;
36+
37+
uint8_t rxBufferSize;
38+
std::unique_ptr<uint8_t[]> rxBuffer;
39+
uint8_t rxBufferIndex;
40+
uint8_t rxBufferLength;
41+
42+
uint8_t txAddress;
43+
uint8_t txBufferSize;
44+
std::unique_ptr<uint8_t[]> txBuffer;
45+
uint8_t txBufferIndex;
46+
uint8_t txBufferLength;
47+
48+
uint8_t transmitting;
5149
public:
52-
TwoWire();
50+
TwoWire(uint8_t rxBufferSize = 128, uint8_t txBufferSize = 128);
5351
void begin(int sda, int scl);
54-
void begin(int sda, int scl, uint8_t address);
5552
void pins(int sda, int scl) __attribute__((deprecated)); // use begin(sda, scl) in new code
5653
void begin();
57-
void begin(uint8_t);
58-
void begin(int);
5954
void setClock(uint32_t);
6055
void setClockStretchLimit(uint32_t);
6156
void beginTransmission(uint8_t);
@@ -76,9 +71,6 @@ class TwoWire : public Stream
7671
virtual int read(void);
7772
virtual int peek(void);
7873
virtual void flush(void);
79-
void onReceive(void (*)(int)); // arduino api
80-
void onReceive(void (*)(size_t)); // legacy esp8266 backward compatibility
81-
void onRequest(void (*)(void));
8274

8375
using Print::write;
8476
};

0 commit comments

Comments
 (0)