Skip to content

Commit 7966f4a

Browse files
safocllucasssvazP-R-O-C-H-Y
authored
Fix ambiguous for TwoWire::requestFrom() methods and align API with Arduino.cc (#8817)
* Fix ambiguous for TwoWire::requestFrom() methods. * Remove TwoWire::begin(int) overload Inside the function, this overload truncated the data type to a shorter one. This could break some users' hopes. * Rewrite TwoWire with using HardwareI2C - implement proposal #8818 (comment) to bring the HARDWARE interface into compliance * Fix TwoWire::end() return type. * Fix TwoWire::setClock() return type. * Fix no return statement in the TwoWire::requestFrom. * fix(libraries/Wire): fix bad return-statement Remove non-void values of the return-statements in function returning 'void'. * style(libraries/Wire): replace tabs with spaces * refactor(libraries/Wire): use slave without support TwoWire::begin(uint8_t address) should be available without slave support by SoC? * refactor(libraries/Wire): remove unused variables Compiler reports "Wire.cpp:393:15: error: variable 'err' set but not used [-Werror=unused-but-set-variable]". * refactor(libraries/Wire): remove unused variables Compiler reports "Wire.cpp:337:15: error: variable 'err' set but not used [-Werror=unused-but-set-variable]". * fix(libraries/Wire): hide slave support elements TwoWire::user_onRequest is used only in conjunction with slave support? * refactor(libraries/Wire): remove temporary comment * fix(libraries/Wire): restore an accidentally deleted implementation TwoWire::endTransmission() was accidentally deleted from a cpp file. * refactor(libraries/Wire): return return types In TwoWire class return return types. * fix(libraries/Wire): fix return type * refactor(libraries/Wire): add return statement if slave isn't supported Co-authored-by: Jan Procházka <[email protected]> * refactor(libraries/Wire): fix indentation Co-authored-by: Jan Procházka <[email protected]> * refactor(libraries/Wire): fix indentation Co-authored-by: Jan Procházka <[email protected]> * refactor(libraries/Wire): fix indentation Co-authored-by: Jan Procházka <[email protected]> * refactor(libraries/Wire): remove unnecessary empty lines --------- Co-authored-by: Lucas Saavedra Vaz <[email protected]> Co-authored-by: Jan Procházka <[email protected]>
1 parent cceebb5 commit 7966f4a

File tree

3 files changed

+109
-137
lines changed

3 files changed

+109
-137
lines changed

cores/esp32/HardwareI2C.h

+42
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 bool begin() = 0;
28+
virtual bool begin(uint8_t address) = 0;
29+
virtual bool end() = 0;
30+
31+
virtual bool 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

+26-71
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ bool TwoWire::setPins(int sdaPin, int sclPin)
149149
return !i2cIsInit(num);
150150
}
151151

152-
bool TwoWire::allocateWireBuffer(void)
152+
bool TwoWire::allocateWireBuffer()
153153
{
154154
// or both buffer can be allocated or none will be
155155
if (rxBuffer == NULL) {
@@ -171,7 +171,7 @@ bool TwoWire::allocateWireBuffer(void)
171171
return true;
172172
}
173173

174-
void TwoWire::freeWireBuffer(void)
174+
void TwoWire::freeWireBuffer()
175175
{
176176
if (rxBuffer != NULL) {
177177
free(rxBuffer);
@@ -424,7 +424,7 @@ uint16_t TwoWire::getTimeOut()
424424
return _timeOutMillis;
425425
}
426426

427-
void TwoWire::beginTransmission(uint16_t address)
427+
void TwoWire::beginTransmission(uint8_t address)
428428
{
429429
#if SOC_I2C_SUPPORT_SLAVE
430430
if(is_slave){
@@ -492,7 +492,12 @@ uint8_t TwoWire::endTransmission(bool sendStop)
492492
return 4;
493493
}
494494

495-
size_t TwoWire::requestFrom(uint16_t address, size_t size, bool sendStop)
495+
uint8_t TwoWire::endTransmission()
496+
{
497+
return endTransmission(true);
498+
}
499+
500+
size_t TwoWire::requestFrom(uint8_t address, size_t size, bool sendStop)
496501
{
497502
#if SOC_I2C_SUPPORT_SLAVE
498503
if(is_slave){
@@ -550,6 +555,10 @@ size_t TwoWire::requestFrom(uint16_t address, size_t size, bool sendStop)
550555
return rxLength;
551556
}
552557

558+
size_t TwoWire::requestFrom(uint8_t address, size_t size){
559+
return requestFrom(address, size, true);
560+
}
561+
553562
size_t TwoWire::write(uint8_t data)
554563
{
555564
if (txBuffer == NULL){
@@ -574,13 +583,13 @@ size_t TwoWire::write(const uint8_t *data, size_t quantity)
574583

575584
}
576585

577-
int TwoWire::available(void)
586+
int TwoWire::available()
578587
{
579588
int result = rxLength - rxIndex;
580589
return result;
581590
}
582591

583-
int TwoWire::read(void)
592+
int TwoWire::read()
584593
{
585594
int value = -1;
586595
if (rxBuffer == NULL){
@@ -593,7 +602,7 @@ int TwoWire::read(void)
593602
return value;
594603
}
595604

596-
int TwoWire::peek(void)
605+
int TwoWire::peek()
597606
{
598607
int value = -1;
599608
if (rxBuffer == NULL){
@@ -606,70 +615,27 @@ int TwoWire::peek(void)
606615
return value;
607616
}
608617

609-
void TwoWire::flush(void)
618+
void TwoWire::flush()
610619
{
611620
rxIndex = 0;
612621
rxLength = 0;
613622
txLength = 0;
614623
//i2cFlush(num); // cleanup
615624
}
616625

617-
size_t TwoWire::requestFrom(uint8_t address, size_t len, bool sendStop)
618-
{
619-
return requestFrom(static_cast<uint16_t>(address), static_cast<size_t>(len), static_cast<bool>(sendStop));
620-
}
621-
622-
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t len, uint8_t sendStop)
623-
{
624-
return requestFrom(static_cast<uint16_t>(address), static_cast<size_t>(len), static_cast<bool>(sendStop));
625-
}
626-
627-
uint8_t TwoWire::requestFrom(uint16_t address, uint8_t len, uint8_t sendStop)
628-
{
629-
return requestFrom(address, static_cast<size_t>(len), static_cast<bool>(sendStop));
630-
}
631-
632-
/* Added to match the Arduino function definition: https://github.com/arduino/ArduinoCore-API/blob/173e8eadced2ad32eeb93bcbd5c49f8d6a055ea6/api/HardwareI2C.h#L39
633-
* See: https://github.com/arduino-libraries/ArduinoECCX08/issues/25
634-
*/
635-
uint8_t TwoWire::requestFrom(uint16_t address, uint8_t len, bool stopBit)
636-
{
637-
return requestFrom((uint16_t)address, (size_t)len, stopBit);
638-
}
639-
640-
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t len)
641-
{
642-
return requestFrom(static_cast<uint16_t>(address), static_cast<size_t>(len), true);
643-
}
644-
645-
uint8_t TwoWire::requestFrom(uint16_t address, uint8_t len)
646-
{
647-
return requestFrom(address, static_cast<size_t>(len), true);
648-
}
649-
650-
uint8_t TwoWire::requestFrom(int address, int len)
651-
{
652-
return requestFrom(static_cast<uint16_t>(address), static_cast<size_t>(len), true);
653-
}
654-
655-
uint8_t TwoWire::requestFrom(int address, int len, int sendStop)
656-
{
657-
return static_cast<uint8_t>(requestFrom(static_cast<uint16_t>(address), static_cast<size_t>(len), static_cast<bool>(sendStop)));
658-
}
659-
660-
void TwoWire::beginTransmission(int address)
661-
{
662-
beginTransmission(static_cast<uint16_t>(address));
663-
}
664-
665-
void TwoWire::beginTransmission(uint8_t address)
626+
void TwoWire::onReceive( void (*function)(int) )
666627
{
667-
beginTransmission(static_cast<uint16_t>(address));
628+
#if SOC_I2C_SUPPORT_SLAVE
629+
user_onReceive = function;
630+
#endif
668631
}
669632

670-
uint8_t TwoWire::endTransmission(void)
633+
// sets function called on slave read
634+
void TwoWire::onRequest( void (*function)(void) )
671635
{
672-
return endTransmission(true);
636+
#if SOC_I2C_SUPPORT_SLAVE
637+
user_onRequest = function;
638+
#endif
673639
}
674640

675641
#if SOC_I2C_SUPPORT_SLAVE
@@ -714,17 +680,6 @@ void TwoWire::onRequestService(uint8_t num, void * arg)
714680
}
715681
}
716682

717-
void TwoWire::onReceive( void (*function)(int) )
718-
{
719-
user_onReceive = function;
720-
}
721-
722-
// sets function called on slave read
723-
void TwoWire::onRequest( void (*function)(void) )
724-
{
725-
user_onRequest = function;
726-
}
727-
728683
#endif /* SOC_I2C_SUPPORT_SLAVE */
729684

730685
TwoWire Wire = TwoWire(0);

libraries/Wire/src/Wire.h

+41-66
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@
3030
#if SOC_I2C_SUPPORTED
3131

3232
#include <esp32-hal.h>
33+
#include <esp32-hal-log.h>
3334
#if !CONFIG_DISABLE_HAL_LOCKS
3435
#include "freertos/FreeRTOS.h"
3536
#include "freertos/task.h"
3637
#include "freertos/semphr.h"
3738
#endif
39+
#include "HardwareI2C.h"
3840
#include "Stream.h"
3941

4042
// WIRE_HAS_BUFFER_SIZE means Wire has setBufferSize()
@@ -50,7 +52,7 @@ typedef void(*user_onRequest)(void);
5052
typedef void(*user_onReceive)(uint8_t*, int);
5153
#endif /* SOC_I2C_SUPPORT_SLAVE */
5254

53-
class TwoWire: public Stream
55+
class TwoWire: public HardwareI2C
5456
{
5557
protected:
5658
uint8_t num;
@@ -81,12 +83,41 @@ class TwoWire: public Stream
8183
static void onReceiveService(uint8_t, uint8_t*, size_t, bool, void *);
8284
#endif /* SOC_I2C_SUPPORT_SLAVE */
8385
bool initPins(int sdaPin, int sclPin);
84-
bool allocateWireBuffer(void);
85-
void freeWireBuffer(void);
86+
bool allocateWireBuffer();
87+
void freeWireBuffer();
8688

8789
public:
8890
TwoWire(uint8_t bus_num);
8991
~TwoWire();
92+
93+
bool begin() override final
94+
{
95+
return begin(-1, -1);
96+
}
97+
98+
bool begin(uint8_t address) override final
99+
{
100+
#if SOC_I2C_SUPPORT_SLAVE
101+
return begin(address, -1, -1, 0);
102+
#else
103+
log_e("I2C slave is not supported on " CONFIG_IDF_TARGET);
104+
return false;
105+
#endif
106+
}
107+
108+
bool end() override;
109+
110+
bool setClock(uint32_t freq) override;
111+
112+
void beginTransmission(uint8_t address) override;
113+
uint8_t endTransmission(bool stopBit) override;
114+
uint8_t endTransmission() override;
115+
116+
size_t requestFrom(uint8_t address, size_t len, bool stopBit) override;
117+
size_t requestFrom(uint8_t address, size_t len) override;
118+
119+
void onReceive(void(*)(int)) override;
120+
void onRequest(void(*)(void)) override;
90121

91122
//call setPins() first, so that begin() can be called without arguments from libraries
92123
bool setPins(int sda, int scl);
@@ -95,78 +126,22 @@ class TwoWire: public Stream
95126
#if SOC_I2C_SUPPORT_SLAVE
96127
bool begin(uint8_t slaveAddr, int sda, int scl, uint32_t frequency);
97128
#endif /* SOC_I2C_SUPPORT_SLAVE */
98-
// Explicit Overload for Arduino MainStream API compatibility
99-
inline bool begin()
100-
{
101-
return begin(-1, -1, static_cast<uint32_t>(0));
102-
}
103-
#if SOC_I2C_SUPPORT_SLAVE
104-
inline bool begin(uint8_t addr)
105-
{
106-
return begin(addr, -1, -1, 0);
107-
}
108-
inline bool begin(int addr)
109-
{
110-
return begin(static_cast<uint8_t>(addr), -1, -1, 0);
111-
}
112-
#endif /* SOC_I2C_SUPPORT_SLAVE */
113-
bool end();
114129

115130
size_t setBufferSize(size_t bSize);
116131

117132
void setTimeOut(uint16_t timeOutMillis); // default timeout of i2c transactions is 50ms
118133
uint16_t getTimeOut();
119134

120-
bool setClock(uint32_t);
121135
uint32_t getClock();
122136

123-
void beginTransmission(uint16_t address);
124-
void beginTransmission(uint8_t address);
125-
void beginTransmission(int address);
126-
127-
uint8_t endTransmission(bool sendStop);
128-
uint8_t endTransmission(void);
129-
130-
size_t requestFrom(uint16_t address, size_t size, bool sendStop);
131-
uint8_t requestFrom(uint16_t address, uint8_t size, bool sendStop);
132-
uint8_t requestFrom(uint16_t address, uint8_t size, uint8_t sendStop);
133-
size_t requestFrom(uint8_t address, size_t len, bool stopBit);
134-
uint8_t requestFrom(uint16_t address, uint8_t size);
135-
uint8_t requestFrom(uint8_t address, uint8_t size, uint8_t sendStop);
136-
uint8_t requestFrom(uint8_t address, uint8_t size);
137-
uint8_t requestFrom(int address, int size, int sendStop);
138-
uint8_t requestFrom(int address, int size);
139-
140-
size_t write(uint8_t);
141-
size_t write(const uint8_t *, size_t);
142-
int available(void);
143-
int read(void);
144-
int peek(void);
145-
void flush(void);
146-
147-
inline size_t write(const char * s)
148-
{
149-
return write((uint8_t*) s, strlen(s));
150-
}
151-
inline size_t write(unsigned long n)
152-
{
153-
return write((uint8_t)n);
154-
}
155-
inline size_t write(long n)
156-
{
157-
return write((uint8_t)n);
158-
}
159-
inline size_t write(unsigned int n)
160-
{
161-
return write((uint8_t)n);
162-
}
163-
inline size_t write(int n)
164-
{
165-
return write((uint8_t)n);
166-
}
137+
size_t write(uint8_t) override;
138+
size_t write(const uint8_t *, size_t) override;
139+
int available() override;
140+
int read() override;
141+
int peek() override;
142+
void flush() override;
143+
167144
#if SOC_I2C_SUPPORT_SLAVE
168-
void onReceive( void (*)(int) );
169-
void onRequest( void (*)(void) );
170145
size_t slaveWrite(const uint8_t *, size_t);
171146
#endif /* SOC_I2C_SUPPORT_SLAVE */
172147
};

0 commit comments

Comments
 (0)