Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ambiguous for TwoWire::requestFrom() methods and align API with Arduino.cc #8817

Merged
merged 27 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
cba06d1
Fix ambiguous for TwoWire::requestFrom() methods.
safocl Oct 30, 2023
14660ef
Remove TwoWire::begin(int) overload
safocl Oct 30, 2023
0454e3a
Rewrite TwoWire with using HardwareI2C
safocl Nov 4, 2023
c7a3b6e
Merge branch 'master' into TwoWireInheritFromHardwareI2C
safocl Nov 28, 2023
b36ff08
Fix TwoWire::end() return type.
safocl Nov 28, 2023
f294609
Fix TwoWire::setClock() return type.
safocl Nov 28, 2023
a1e2e69
Fix no return statement in the TwoWire::requestFrom.
safocl Nov 28, 2023
70a5316
Merge branch 'master' into TwoWireInheritFromHardwareI2C
lucasssvaz Jan 17, 2024
b2ad191
Merge branch 'espressif:master' into TwoWireInheritFromHardwareI2C
safocl Jan 17, 2024
fca0f85
fix(libraries/Wire): fix bad return-statement
safocl Jan 18, 2024
135ad6a
style(libraries/Wire): replace tabs with spaces
safocl Jan 18, 2024
3cf20e0
refactor(libraries/Wire): use slave without support
safocl Jan 18, 2024
e56d262
Merge branch 'espressif:master' into TwoWireInheritFromHardwareI2C
safocl Jan 18, 2024
7a351dd
refactor(libraries/Wire): remove unused variables
safocl Jan 18, 2024
011a2da
refactor(libraries/Wire): remove unused variables
safocl Jan 18, 2024
a6115ae
fix(libraries/Wire): hide slave support elements
safocl Jan 18, 2024
a787d49
Merge branch 'espressif:master' into TwoWireInheritFromHardwareI2C
safocl Jan 19, 2024
1b9f656
refactor(libraries/Wire): remove temporary comment
safocl Jan 19, 2024
75f8ee9
fix(libraries/Wire): restore an accidentally deleted implementation
safocl Jan 20, 2024
5d3c8c7
Merge branch 'espressif:master' into TwoWireInheritFromHardwareI2C
safocl Jan 28, 2024
07154af
refactor(libraries/Wire): return return types
safocl Jan 28, 2024
69a5890
fix(libraries/Wire): fix return type
safocl Jan 28, 2024
35c0568
refactor(libraries/Wire): add return statement if slave isn't supported
safocl Jan 29, 2024
eacea66
refactor(libraries/Wire): fix indentation
safocl Jan 29, 2024
b1ca983
refactor(libraries/Wire): fix indentation
safocl Jan 29, 2024
bca2887
refactor(libraries/Wire): fix indentation
safocl Jan 29, 2024
311330e
refactor(libraries/Wire): remove unnecessary empty lines
safocl Jan 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions cores/esp32/HardwareI2C.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Copyright (c) 2016 Arduino LLC. All right reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#pragma once

#include <inttypes.h>
#include "Stream.h"

class HardwareI2C : public Stream
{
public:
virtual bool begin() = 0;
virtual bool begin(uint8_t address) = 0;
virtual bool end() = 0;

virtual bool setClock(uint32_t freq) = 0;

virtual void beginTransmission(uint8_t address) = 0;
virtual uint8_t endTransmission(bool stopBit) = 0;
virtual uint8_t endTransmission(void) = 0;

virtual size_t requestFrom(uint8_t address, size_t len, bool stopBit) = 0;
virtual size_t requestFrom(uint8_t address, size_t len) = 0;

virtual void onReceive(void(*)(int)) = 0;
virtual void onRequest(void(*)(void)) = 0;
};
97 changes: 26 additions & 71 deletions libraries/Wire/src/Wire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ bool TwoWire::setPins(int sdaPin, int sclPin)
return !i2cIsInit(num);
}

bool TwoWire::allocateWireBuffer(void)
bool TwoWire::allocateWireBuffer()
{
// or both buffer can be allocated or none will be
if (rxBuffer == NULL) {
Expand All @@ -171,7 +171,7 @@ bool TwoWire::allocateWireBuffer(void)
return true;
}

void TwoWire::freeWireBuffer(void)
void TwoWire::freeWireBuffer()
{
if (rxBuffer != NULL) {
free(rxBuffer);
Expand Down Expand Up @@ -424,7 +424,7 @@ uint16_t TwoWire::getTimeOut()
return _timeOutMillis;
}

void TwoWire::beginTransmission(uint16_t address)
void TwoWire::beginTransmission(uint8_t address)
{
#if SOC_I2C_SUPPORT_SLAVE
if(is_slave){
Expand Down Expand Up @@ -492,7 +492,12 @@ uint8_t TwoWire::endTransmission(bool sendStop)
return 4;
}

size_t TwoWire::requestFrom(uint16_t address, size_t size, bool sendStop)
uint8_t TwoWire::endTransmission()
{
return endTransmission(true);
}

size_t TwoWire::requestFrom(uint8_t address, size_t size, bool sendStop)
{
#if SOC_I2C_SUPPORT_SLAVE
if(is_slave){
Expand Down Expand Up @@ -550,6 +555,10 @@ size_t TwoWire::requestFrom(uint16_t address, size_t size, bool sendStop)
return rxLength;
}

size_t TwoWire::requestFrom(uint8_t address, size_t size){
return requestFrom(address, size, true);
}
safocl marked this conversation as resolved.
Show resolved Hide resolved

size_t TwoWire::write(uint8_t data)
{
if (txBuffer == NULL){
Expand All @@ -574,13 +583,13 @@ size_t TwoWire::write(const uint8_t *data, size_t quantity)

}

int TwoWire::available(void)
int TwoWire::available()
{
int result = rxLength - rxIndex;
return result;
}

int TwoWire::read(void)
int TwoWire::read()
{
int value = -1;
if (rxBuffer == NULL){
Expand All @@ -593,7 +602,7 @@ int TwoWire::read(void)
return value;
}

int TwoWire::peek(void)
int TwoWire::peek()
{
int value = -1;
if (rxBuffer == NULL){
Expand All @@ -606,70 +615,27 @@ int TwoWire::peek(void)
return value;
}

void TwoWire::flush(void)
void TwoWire::flush()
{
rxIndex = 0;
rxLength = 0;
txLength = 0;
//i2cFlush(num); // cleanup
}

size_t TwoWire::requestFrom(uint8_t address, size_t len, bool sendStop)
{
return requestFrom(static_cast<uint16_t>(address), static_cast<size_t>(len), static_cast<bool>(sendStop));
}

uint8_t TwoWire::requestFrom(uint8_t address, uint8_t len, uint8_t sendStop)
{
return requestFrom(static_cast<uint16_t>(address), static_cast<size_t>(len), static_cast<bool>(sendStop));
}

uint8_t TwoWire::requestFrom(uint16_t address, uint8_t len, uint8_t sendStop)
{
return requestFrom(address, static_cast<size_t>(len), static_cast<bool>(sendStop));
}

/* Added to match the Arduino function definition: https://github.com/arduino/ArduinoCore-API/blob/173e8eadced2ad32eeb93bcbd5c49f8d6a055ea6/api/HardwareI2C.h#L39
* See: https://github.com/arduino-libraries/ArduinoECCX08/issues/25
*/
uint8_t TwoWire::requestFrom(uint16_t address, uint8_t len, bool stopBit)
{
return requestFrom((uint16_t)address, (size_t)len, stopBit);
}

uint8_t TwoWire::requestFrom(uint8_t address, uint8_t len)
{
return requestFrom(static_cast<uint16_t>(address), static_cast<size_t>(len), true);
}

uint8_t TwoWire::requestFrom(uint16_t address, uint8_t len)
{
return requestFrom(address, static_cast<size_t>(len), true);
}

uint8_t TwoWire::requestFrom(int address, int len)
{
return requestFrom(static_cast<uint16_t>(address), static_cast<size_t>(len), true);
}

uint8_t TwoWire::requestFrom(int address, int len, int sendStop)
{
return static_cast<uint8_t>(requestFrom(static_cast<uint16_t>(address), static_cast<size_t>(len), static_cast<bool>(sendStop)));
}

void TwoWire::beginTransmission(int address)
{
beginTransmission(static_cast<uint16_t>(address));
}

void TwoWire::beginTransmission(uint8_t address)
void TwoWire::onReceive( void (*function)(int) )
{
beginTransmission(static_cast<uint16_t>(address));
#if SOC_I2C_SUPPORT_SLAVE
user_onReceive = function;
#endif
safocl marked this conversation as resolved.
Show resolved Hide resolved
}

uint8_t TwoWire::endTransmission(void)
// sets function called on slave read
void TwoWire::onRequest( void (*function)(void) )
{
return endTransmission(true);
#if SOC_I2C_SUPPORT_SLAVE
user_onRequest = function;
#endif
safocl marked this conversation as resolved.
Show resolved Hide resolved
}

#if SOC_I2C_SUPPORT_SLAVE
Expand Down Expand Up @@ -714,17 +680,6 @@ void TwoWire::onRequestService(uint8_t num, void * arg)
}
}

void TwoWire::onReceive( void (*function)(int) )
{
user_onReceive = function;
}

// sets function called on slave read
void TwoWire::onRequest( void (*function)(void) )
{
user_onRequest = function;
}

#endif /* SOC_I2C_SUPPORT_SLAVE */

TwoWire Wire = TwoWire(0);
Expand Down
111 changes: 45 additions & 66 deletions libraries/Wire/src/Wire.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@
#if SOC_I2C_SUPPORTED

#include <esp32-hal.h>
#include <esp32-hal-log.h>
#if !CONFIG_DISABLE_HAL_LOCKS
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#endif
#include "HardwareI2C.h"
#include "Stream.h"

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

class TwoWire: public Stream
class TwoWire: public HardwareI2C
{
protected:
uint8_t num;
Expand Down Expand Up @@ -81,12 +83,43 @@ class TwoWire: public Stream
static void onReceiveService(uint8_t, uint8_t*, size_t, bool, void *);
#endif /* SOC_I2C_SUPPORT_SLAVE */
bool initPins(int sdaPin, int sclPin);
bool allocateWireBuffer(void);
void freeWireBuffer(void);
bool allocateWireBuffer();
void freeWireBuffer();

public:
TwoWire(uint8_t bus_num);
~TwoWire();

bool begin() override final
{
return begin(-1, -1);
}

bool begin(uint8_t address) override final
{
#if SOC_I2C_SUPPORT_SLAVE
return begin(address, -1, -1, 0);
#else
log_e("I2C slave is not supported on " CONFIG_IDF_TARGET);
return false;
#endif
}


bool end() override;

bool setClock(uint32_t freq) override;

void beginTransmission(uint8_t address) override;
uint8_t endTransmission(bool stopBit) override;
uint8_t endTransmission() override;

size_t requestFrom(uint8_t address, size_t len, bool stopBit) override;
size_t requestFrom(uint8_t address, size_t len) override;

void onReceive(void(*)(int)) override;
void onRequest(void(*)(void)) override;


//call setPins() first, so that begin() can be called without arguments from libraries
bool setPins(int sda, int scl);
Expand All @@ -95,78 +128,24 @@ class TwoWire: public Stream
#if SOC_I2C_SUPPORT_SLAVE
bool begin(uint8_t slaveAddr, int sda, int scl, uint32_t frequency);
#endif /* SOC_I2C_SUPPORT_SLAVE */
// Explicit Overload for Arduino MainStream API compatibility
inline bool begin()
{
return begin(-1, -1, static_cast<uint32_t>(0));
}
#if SOC_I2C_SUPPORT_SLAVE
inline bool begin(uint8_t addr)
{
return begin(addr, -1, -1, 0);
}
inline bool begin(int addr)
{
return begin(static_cast<uint8_t>(addr), -1, -1, 0);
}
#endif /* SOC_I2C_SUPPORT_SLAVE */
bool end();

size_t setBufferSize(size_t bSize);

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

bool setClock(uint32_t);
uint32_t getClock();

void beginTransmission(uint16_t address);
void beginTransmission(uint8_t address);
void beginTransmission(int address);

uint8_t endTransmission(bool sendStop);
uint8_t endTransmission(void);

size_t requestFrom(uint16_t address, size_t size, bool sendStop);
uint8_t requestFrom(uint16_t address, uint8_t size, bool sendStop);
uint8_t requestFrom(uint16_t address, uint8_t size, uint8_t sendStop);
size_t requestFrom(uint8_t address, size_t len, bool stopBit);
uint8_t requestFrom(uint16_t address, uint8_t size);
uint8_t requestFrom(uint8_t address, uint8_t size, uint8_t sendStop);
uint8_t requestFrom(uint8_t address, uint8_t size);
uint8_t requestFrom(int address, int size, int sendStop);
uint8_t requestFrom(int address, int size);

size_t write(uint8_t);
size_t write(const uint8_t *, size_t);
int available(void);
int read(void);
int peek(void);
void flush(void);

inline size_t write(const char * s)
{
return write((uint8_t*) s, strlen(s));
}
inline size_t write(unsigned long n)
{
return write((uint8_t)n);
}
inline size_t write(long n)
{
return write((uint8_t)n);
}
inline size_t write(unsigned int n)
{
return write((uint8_t)n);
}
inline size_t write(int n)
{
return write((uint8_t)n);
}


safocl marked this conversation as resolved.
Show resolved Hide resolved
size_t write(uint8_t) override;
size_t write(const uint8_t *, size_t) override;
int available() override;
int read() override;
int peek() override;
void flush() override;

#if SOC_I2C_SUPPORT_SLAVE
void onReceive( void (*)(int) );
void onRequest( void (*)(void) );
size_t slaveWrite(const uint8_t *, size_t);
#endif /* SOC_I2C_SUPPORT_SLAVE */
};
Expand Down
Loading