Skip to content

Commit 0454e3a

Browse files
committed
Rewrite TwoWire with using HardwareI2C
- implement proposal espressif#8818 (comment) to bring the HARDWARE interface into compliance
1 parent 14660ef commit 0454e3a

File tree

3 files changed

+97
-131
lines changed

3 files changed

+97
-131
lines changed

cores/esp32/HardwareI2C.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Copyright (c) 2016 Arduino LLC. All right reserved.
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
See the GNU Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#pragma once
20+
21+
#include <inttypes.h>
22+
#include "Stream.h"
23+
24+
class HardwareI2C : public Stream
25+
{
26+
public:
27+
virtual void begin() = 0;
28+
virtual void begin(uint8_t address) = 0;
29+
virtual void end() = 0;
30+
31+
virtual void setClock(uint32_t freq) = 0;
32+
33+
virtual void beginTransmission(uint8_t address) = 0;
34+
virtual uint8_t endTransmission(bool stopBit) = 0;
35+
virtual uint8_t endTransmission(void) = 0;
36+
37+
virtual size_t requestFrom(uint8_t address, size_t len, bool stopBit) = 0;
38+
virtual size_t requestFrom(uint8_t address, size_t len) = 0;
39+
40+
virtual void onReceive(void(*)(int)) = 0;
41+
virtual void onRequest(void(*)(void)) = 0;
42+
};

libraries/Wire/src/Wire.cpp

Lines changed: 17 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ bool TwoWire::setPins(int sdaPin, int sclPin)
145145
return !i2cIsInit(num);
146146
}
147147

148-
bool TwoWire::allocateWireBuffer(void)
148+
bool TwoWire::allocateWireBuffer()
149149
{
150150
// or both buffer can be allocated or none will be
151151
if (rxBuffer == NULL) {
@@ -167,7 +167,7 @@ bool TwoWire::allocateWireBuffer(void)
167167
return true;
168168
}
169169

170-
void TwoWire::freeWireBuffer(void)
170+
void TwoWire::freeWireBuffer()
171171
{
172172
if (rxBuffer != NULL) {
173173
free(rxBuffer);
@@ -409,7 +409,7 @@ uint16_t TwoWire::getTimeOut()
409409
return _timeOutMillis;
410410
}
411411

412-
void TwoWire::beginTransmission(uint16_t address)
412+
void TwoWire::beginTransmission(uint8_t address)
413413
{
414414
if(is_slave){
415415
log_e("Bus is in Slave Mode");
@@ -475,7 +475,7 @@ uint8_t TwoWire::endTransmission(bool sendStop)
475475
return 4;
476476
}
477477

478-
size_t TwoWire::requestFrom(uint16_t address, size_t size, bool sendStop)
478+
size_t TwoWire::requestFrom(uint8_t address, size_t size, bool sendStop)
479479
{
480480
if(is_slave){
481481
log_e("Bus is in Slave Mode");
@@ -524,6 +524,10 @@ size_t TwoWire::requestFrom(uint16_t address, size_t size, bool sendStop)
524524
return rxLength;
525525
}
526526

527+
size_t TwoWire::requestFrom(uint8_t address, size_t size){
528+
requestFrom(address, size, true);
529+
}
530+
527531
size_t TwoWire::write(uint8_t data)
528532
{
529533
if (txBuffer == NULL){
@@ -548,13 +552,13 @@ size_t TwoWire::write(const uint8_t *data, size_t quantity)
548552

549553
}
550554

551-
int TwoWire::available(void)
555+
int TwoWire::available()
552556
{
553557
int result = rxLength - rxIndex;
554558
return result;
555559
}
556560

557-
int TwoWire::read(void)
561+
int TwoWire::read()
558562
{
559563
int value = -1;
560564
if (rxBuffer == NULL){
@@ -567,7 +571,7 @@ int TwoWire::read(void)
567571
return value;
568572
}
569573

570-
int TwoWire::peek(void)
574+
int TwoWire::peek()
571575
{
572576
int value = -1;
573577
if (rxBuffer == NULL){
@@ -580,70 +584,23 @@ int TwoWire::peek(void)
580584
return value;
581585
}
582586

583-
void TwoWire::flush(void)
587+
void TwoWire::flush()
584588
{
585589
rxIndex = 0;
586590
rxLength = 0;
587591
txLength = 0;
588592
//i2cFlush(num); // cleanup
589593
}
590594

591-
// size_t TwoWire::requestFrom(uint8_t address, size_t len, bool sendStop)
592-
// {
593-
// return requestFrom(static_cast<uint16_t>(address), static_cast<size_t>(len), static_cast<bool>(sendStop));
594-
// }
595-
//
596-
// uint8_t TwoWire::requestFrom(uint8_t address, uint8_t len, uint8_t sendStop)
597-
// {
598-
// return requestFrom(static_cast<uint16_t>(address), static_cast<size_t>(len), static_cast<bool>(sendStop));
599-
// }
600-
//
601-
// uint8_t TwoWire::requestFrom(uint16_t address, uint8_t len, uint8_t sendStop)
602-
// {
603-
// return requestFrom(address, static_cast<size_t>(len), static_cast<bool>(sendStop));
604-
// }
605-
//
606-
// /* Added to match the Arduino function definition: https://github.com/arduino/ArduinoCore-API/blob/173e8eadced2ad32eeb93bcbd5c49f8d6a055ea6/api/HardwareI2C.h#L39
607-
// * See: https://github.com/arduino-libraries/ArduinoECCX08/issues/25
608-
// */
609-
// uint8_t TwoWire::requestFrom(uint16_t address, uint8_t len, bool stopBit)
610-
// {
611-
// return requestFrom((uint16_t)address, (size_t)len, stopBit);
612-
// }
613-
//
614-
// uint8_t TwoWire::requestFrom(uint8_t address, uint8_t len)
615-
// {
616-
// return requestFrom(static_cast<uint16_t>(address), static_cast<size_t>(len), true);
617-
// }
618-
//
619-
// uint8_t TwoWire::requestFrom(uint16_t address, uint8_t len)
620-
// {
621-
// return requestFrom(address, static_cast<size_t>(len), true);
622-
// }
623-
//
624-
// uint8_t TwoWire::requestFrom(int address, int len)
625-
// {
626-
// return requestFrom(static_cast<uint16_t>(address), static_cast<size_t>(len), true);
627-
// }
628-
//
629-
// uint8_t TwoWire::requestFrom(int address, int len, int sendStop)
630-
// {
631-
// return static_cast<uint8_t>(requestFrom(static_cast<uint16_t>(address), static_cast<size_t>(len), static_cast<bool>(sendStop)));
632-
// }
633-
634-
void TwoWire::beginTransmission(int address)
635-
{
636-
beginTransmission(static_cast<uint16_t>(address));
637-
}
638-
639-
void TwoWire::beginTransmission(uint8_t address)
595+
void TwoWire::onReceive( void (*function)(int) )
640596
{
641-
beginTransmission(static_cast<uint16_t>(address));
597+
user_onReceive = function;
642598
}
643599

644-
uint8_t TwoWire::endTransmission(void)
600+
// sets function called on slave read
601+
void TwoWire::onRequest( void (*function)(void) )
645602
{
646-
return endTransmission(true);
603+
user_onRequest = function;
647604
}
648605

649606
#if SOC_I2C_SUPPORT_SLAVE
@@ -688,17 +645,6 @@ void TwoWire::onRequestService(uint8_t num, void * arg)
688645
}
689646
}
690647

691-
void TwoWire::onReceive( void (*function)(int) )
692-
{
693-
user_onReceive = function;
694-
}
695-
696-
// sets function called on slave read
697-
void TwoWire::onRequest( void (*function)(void) )
698-
{
699-
user_onRequest = function;
700-
}
701-
702648
#endif /* SOC_I2C_SUPPORT_SLAVE */
703649

704650
TwoWire Wire = TwoWire(0);

libraries/Wire/src/Wire.h

Lines changed: 38 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "freertos/task.h"
3636
#include "freertos/semphr.h"
3737
#endif
38+
#include "HardwareI2C.h"
3839
#include "Stream.h"
3940

4041
// WIRE_HAS_BUFFER_SIZE means Wire has setBufferSize()
@@ -48,7 +49,7 @@
4849
typedef void(*user_onRequest)(void);
4950
typedef void(*user_onReceive)(uint8_t*, int);
5051

51-
class TwoWire: public Stream
52+
class TwoWire: public HardwareI2C
5253
{
5354
protected:
5455
uint8_t num;
@@ -79,84 +80,61 @@ class TwoWire: public Stream
7980
static void onReceiveService(uint8_t, uint8_t*, size_t, bool, void *);
8081
#endif /* SOC_I2C_SUPPORT_SLAVE */
8182
bool initPins(int sdaPin, int sclPin);
82-
bool allocateWireBuffer(void);
83-
void freeWireBuffer(void);
83+
bool allocateWireBuffer();
84+
void freeWireBuffer();
8485

8586
public:
8687
TwoWire(uint8_t bus_num);
8788
~TwoWire();
89+
90+
void begin() override final{
91+
begin(-1, -1);
92+
}
93+
94+
void begin(uint8_t address) override final
95+
{
96+
begin(address, -1, -1, 0);
97+
}
98+
99+
100+
void end() override;
101+
102+
void setClock(uint32_t freq) override;
103+
104+
void beginTransmission(uint8_t address) override;
105+
uint8_t endTransmission(bool stopBit) override;
106+
uint8_t endTransmission() override;
107+
108+
size_t requestFrom(uint8_t address, size_t len, bool stopBit) override;
109+
size_t requestFrom(uint8_t address, size_t len) override;
110+
111+
void onReceive(void(*)(int)) override;
112+
void onRequest(void(*)(void)) override;
113+
88114

89115
//call setPins() first, so that begin() can be called without arguments from libraries
90116
bool setPins(int sda, int scl);
91117

92118
bool begin(int sda, int scl, uint32_t frequency=0); // returns true, if successful init of i2c bus
93119
bool begin(uint8_t slaveAddr, int sda, int scl, uint32_t frequency);
94-
// Explicit Overload for Arduino MainStream API compatibility
95-
inline bool begin()
96-
{
97-
return begin(-1, -1, static_cast<uint32_t>(0));
98-
}
99-
inline bool begin(uint8_t addr)
100-
{
101-
return begin(addr, -1, -1, 0);
102-
}
103-
bool end();
104120

105121
size_t setBufferSize(size_t bSize);
106122

107123
void setTimeOut(uint16_t timeOutMillis); // default timeout of i2c transactions is 50ms
108124
uint16_t getTimeOut();
109125

110-
bool setClock(uint32_t);
111126
uint32_t getClock();
112127

113-
void beginTransmission(uint16_t address);
114-
void beginTransmission(uint8_t address);
115-
void beginTransmission(int address);
116-
117-
uint8_t endTransmission(bool sendStop);
118-
uint8_t endTransmission(void);
119-
120-
size_t requestFrom(uint16_t address, size_t size, bool sendStop = true);
121-
// uint8_t requestFrom(uint16_t address, uint8_t size, bool sendStop);
122-
// uint8_t requestFrom(uint16_t address, uint8_t size, uint8_t sendStop);
123-
// size_t requestFrom(uint8_t address, size_t len, bool stopBit);
124-
// uint8_t requestFrom(uint16_t address, uint8_t size);
125-
// uint8_t requestFrom(uint8_t address, uint8_t size, uint8_t sendStop);
126-
// uint8_t requestFrom(uint8_t address, uint8_t size);
127-
// uint8_t requestFrom(int address, int size, int sendStop);
128-
// uint8_t requestFrom(int address, int size);
129-
130-
size_t write(uint8_t);
131-
size_t write(const uint8_t *, size_t);
132-
int available(void);
133-
int read(void);
134-
int peek(void);
135-
void flush(void);
136-
137-
inline size_t write(const char * s)
138-
{
139-
return write((uint8_t*) s, strlen(s));
140-
}
141-
inline size_t write(unsigned long n)
142-
{
143-
return write((uint8_t)n);
144-
}
145-
inline size_t write(long n)
146-
{
147-
return write((uint8_t)n);
148-
}
149-
inline size_t write(unsigned int n)
150-
{
151-
return write((uint8_t)n);
152-
}
153-
inline size_t write(int n)
154-
{
155-
return write((uint8_t)n);
156-
}
128+
129+
130+
size_t write(uint8_t) override;
131+
size_t write(const uint8_t *, size_t) override;
132+
int available() override;
133+
int read() override;
134+
int peek() override;
135+
void flush() override;
136+
157137
#if SOC_I2C_SUPPORT_SLAVE
158-
void onReceive( void (*)(int) );
159-
void onRequest( void (*)(void) );
160138
size_t slaveWrite(const uint8_t *, size_t);
161139
#endif /* SOC_I2C_SUPPORT_SLAVE */
162140
};

0 commit comments

Comments
 (0)