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 20 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.
Jump to
Jump to file
Failed to load files.
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 void begin() = 0;
virtual void begin(uint8_t address) = 0;
virtual void end() = 0;

virtual void 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;
};
117 changes: 34 additions & 83 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 @@ -332,35 +332,34 @@ bool TwoWire::begin(int sdaPin, int sclPin, uint32_t frequency)

}

bool TwoWire::end()
void TwoWire::end()
{
esp_err_t err = ESP_OK;
#if !CONFIG_DISABLE_HAL_LOCKS
if(lock != NULL){
//acquire lock
if(xSemaphoreTake(lock, portMAX_DELAY) != pdTRUE){
log_e("could not acquire lock");
return false;
return;
}
#endif
#if SOC_I2C_SUPPORT_SLAVE
if(is_slave){
err = i2cSlaveDeinit(num);
esp_err_t err = i2cSlaveDeinit(num);
if(err == ESP_OK){
is_slave = false;
}
} else
#endif /* SOC_I2C_SUPPORT_SLAVE */
if(i2cIsInit(num)){
err = i2cDeinit(num);
i2cDeinit(num);
}
freeWireBuffer();
#if !CONFIG_DISABLE_HAL_LOCKS
//release lock
xSemaphoreGive(lock);
}
#endif
return (err == ESP_OK);
return;
}

uint32_t TwoWire::getClock()
Expand Down Expand Up @@ -388,30 +387,27 @@ uint32_t TwoWire::getClock()
return frequency;
}

bool TwoWire::setClock(uint32_t frequency)
void TwoWire::setClock(uint32_t frequency)
{
esp_err_t err = ESP_OK;
#if !CONFIG_DISABLE_HAL_LOCKS
//acquire lock
if(lock == NULL || xSemaphoreTake(lock, portMAX_DELAY) != pdTRUE){
log_e("could not acquire lock");
return false;
return;
}
#endif
#if SOC_I2C_SUPPORT_SLAVE
if(is_slave){
log_e("Bus is in Slave Mode");
err = ESP_FAIL;
} else
#endif /* SOC_I2C_SUPPORT_SLAVE */
{
err = i2cSetClock(num, frequency);
i2cSetClock(num, frequency);
}
#if !CONFIG_DISABLE_HAL_LOCKS
//release lock
xSemaphoreGive(lock);
#endif
return (err == ESP_OK);
}

void TwoWire::setTimeOut(uint16_t timeOutMillis)
Expand All @@ -424,7 +420,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 +488,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 +551,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 +579,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 +598,7 @@ int TwoWire::read(void)
return value;
}

int TwoWire::peek(void)
int TwoWire::peek()
{
int value = -1;
if (rxBuffer == NULL){
Expand All @@ -606,70 +611,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 +676,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