From cba06d1ef63d821ac4bb8f08f45ee5964f95e36f Mon Sep 17 00:00:00 2001 From: safocl Date: Mon, 30 Oct 2023 10:34:42 +0400 Subject: [PATCH 01/21] Fix ambiguous for TwoWire::requestFrom() methods. --- libraries/Wire/src/Wire.cpp | 84 ++++++++++++++++++------------------- libraries/Wire/src/Wire.h | 18 ++++---- 2 files changed, 51 insertions(+), 51 deletions(-) diff --git a/libraries/Wire/src/Wire.cpp b/libraries/Wire/src/Wire.cpp index 2568e3c9413..2bcd9bc2112 100644 --- a/libraries/Wire/src/Wire.cpp +++ b/libraries/Wire/src/Wire.cpp @@ -588,48 +588,48 @@ void TwoWire::flush(void) //i2cFlush(num); // cleanup } -size_t TwoWire::requestFrom(uint8_t address, size_t len, bool sendStop) -{ - return requestFrom(static_cast(address), static_cast(len), static_cast(sendStop)); -} - -uint8_t TwoWire::requestFrom(uint8_t address, uint8_t len, uint8_t sendStop) -{ - return requestFrom(static_cast(address), static_cast(len), static_cast(sendStop)); -} - -uint8_t TwoWire::requestFrom(uint16_t address, uint8_t len, uint8_t sendStop) -{ - return requestFrom(address, static_cast(len), static_cast(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(address), static_cast(len), true); -} - -uint8_t TwoWire::requestFrom(uint16_t address, uint8_t len) -{ - return requestFrom(address, static_cast(len), true); -} - -uint8_t TwoWire::requestFrom(int address, int len) -{ - return requestFrom(static_cast(address), static_cast(len), true); -} - -uint8_t TwoWire::requestFrom(int address, int len, int sendStop) -{ - return static_cast(requestFrom(static_cast(address), static_cast(len), static_cast(sendStop))); -} +// size_t TwoWire::requestFrom(uint8_t address, size_t len, bool sendStop) +// { +// return requestFrom(static_cast(address), static_cast(len), static_cast(sendStop)); +// } +// +// uint8_t TwoWire::requestFrom(uint8_t address, uint8_t len, uint8_t sendStop) +// { +// return requestFrom(static_cast(address), static_cast(len), static_cast(sendStop)); +// } +// +// uint8_t TwoWire::requestFrom(uint16_t address, uint8_t len, uint8_t sendStop) +// { +// return requestFrom(address, static_cast(len), static_cast(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(address), static_cast(len), true); +// } +// +// uint8_t TwoWire::requestFrom(uint16_t address, uint8_t len) +// { +// return requestFrom(address, static_cast(len), true); +// } +// +// uint8_t TwoWire::requestFrom(int address, int len) +// { +// return requestFrom(static_cast(address), static_cast(len), true); +// } +// +// uint8_t TwoWire::requestFrom(int address, int len, int sendStop) +// { +// return static_cast(requestFrom(static_cast(address), static_cast(len), static_cast(sendStop))); +// } void TwoWire::beginTransmission(int address) { diff --git a/libraries/Wire/src/Wire.h b/libraries/Wire/src/Wire.h index a4cdb117443..a966a9340eb 100644 --- a/libraries/Wire/src/Wire.h +++ b/libraries/Wire/src/Wire.h @@ -121,15 +121,15 @@ class TwoWire: public Stream 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 requestFrom(uint16_t address, size_t size, bool sendStop = true); + // 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); From 14660effb37bb527a489ad1444757dacef95cbbe Mon Sep 17 00:00:00 2001 From: safocl Date: Mon, 30 Oct 2023 12:24:36 +0400 Subject: [PATCH 02/21] Remove TwoWire::begin(int) overload Inside the function, this overload truncated the data type to a shorter one. This could break some users' hopes. --- libraries/Wire/src/Wire.h | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/libraries/Wire/src/Wire.h b/libraries/Wire/src/Wire.h index a966a9340eb..5af1e7ff939 100644 --- a/libraries/Wire/src/Wire.h +++ b/libraries/Wire/src/Wire.h @@ -100,11 +100,7 @@ class TwoWire: public Stream { return begin(addr, -1, -1, 0); } - inline bool begin(int addr) - { - return begin(static_cast(addr), -1, -1, 0); - } - bool end(); + bool end(); size_t setBufferSize(size_t bSize); From 0454e3a97dff938f94d7efa34636f29f16486a9f Mon Sep 17 00:00:00 2001 From: safocl Date: Sat, 4 Nov 2023 06:01:30 +0400 Subject: [PATCH 03/21] Rewrite TwoWire with using HardwareI2C - implement proposal https://github.com/espressif/arduino-esp32/pull/8818#issuecomment-1792191815 to bring the HARDWARE interface into compliance --- cores/esp32/HardwareI2C.h | 42 ++++++++++++++++ libraries/Wire/src/Wire.cpp | 88 +++++++-------------------------- libraries/Wire/src/Wire.h | 98 ++++++++++++++----------------------- 3 files changed, 97 insertions(+), 131 deletions(-) create mode 100644 cores/esp32/HardwareI2C.h diff --git a/cores/esp32/HardwareI2C.h b/cores/esp32/HardwareI2C.h new file mode 100644 index 00000000000..2db796af1bb --- /dev/null +++ b/cores/esp32/HardwareI2C.h @@ -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 +#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; +}; diff --git a/libraries/Wire/src/Wire.cpp b/libraries/Wire/src/Wire.cpp index 2bcd9bc2112..4230e307d1a 100644 --- a/libraries/Wire/src/Wire.cpp +++ b/libraries/Wire/src/Wire.cpp @@ -145,7 +145,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) { @@ -167,7 +167,7 @@ bool TwoWire::allocateWireBuffer(void) return true; } -void TwoWire::freeWireBuffer(void) +void TwoWire::freeWireBuffer() { if (rxBuffer != NULL) { free(rxBuffer); @@ -409,7 +409,7 @@ uint16_t TwoWire::getTimeOut() return _timeOutMillis; } -void TwoWire::beginTransmission(uint16_t address) +void TwoWire::beginTransmission(uint8_t address) { if(is_slave){ log_e("Bus is in Slave Mode"); @@ -475,7 +475,7 @@ uint8_t TwoWire::endTransmission(bool sendStop) return 4; } -size_t TwoWire::requestFrom(uint16_t address, size_t size, bool sendStop) +size_t TwoWire::requestFrom(uint8_t address, size_t size, bool sendStop) { if(is_slave){ log_e("Bus is in Slave Mode"); @@ -524,6 +524,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){ + requestFrom(address, size, true); +} + size_t TwoWire::write(uint8_t data) { if (txBuffer == NULL){ @@ -548,13 +552,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){ @@ -567,7 +571,7 @@ int TwoWire::read(void) return value; } -int TwoWire::peek(void) +int TwoWire::peek() { int value = -1; if (rxBuffer == NULL){ @@ -580,7 +584,7 @@ int TwoWire::peek(void) return value; } -void TwoWire::flush(void) +void TwoWire::flush() { rxIndex = 0; rxLength = 0; @@ -588,62 +592,15 @@ void TwoWire::flush(void) //i2cFlush(num); // cleanup } -// size_t TwoWire::requestFrom(uint8_t address, size_t len, bool sendStop) -// { -// return requestFrom(static_cast(address), static_cast(len), static_cast(sendStop)); -// } -// -// uint8_t TwoWire::requestFrom(uint8_t address, uint8_t len, uint8_t sendStop) -// { -// return requestFrom(static_cast(address), static_cast(len), static_cast(sendStop)); -// } -// -// uint8_t TwoWire::requestFrom(uint16_t address, uint8_t len, uint8_t sendStop) -// { -// return requestFrom(address, static_cast(len), static_cast(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(address), static_cast(len), true); -// } -// -// uint8_t TwoWire::requestFrom(uint16_t address, uint8_t len) -// { -// return requestFrom(address, static_cast(len), true); -// } -// -// uint8_t TwoWire::requestFrom(int address, int len) -// { -// return requestFrom(static_cast(address), static_cast(len), true); -// } -// -// uint8_t TwoWire::requestFrom(int address, int len, int sendStop) -// { -// return static_cast(requestFrom(static_cast(address), static_cast(len), static_cast(sendStop))); -// } - -void TwoWire::beginTransmission(int address) -{ - beginTransmission(static_cast(address)); -} - -void TwoWire::beginTransmission(uint8_t address) +void TwoWire::onReceive( void (*function)(int) ) { - beginTransmission(static_cast(address)); + user_onReceive = function; } -uint8_t TwoWire::endTransmission(void) +// sets function called on slave read +void TwoWire::onRequest( void (*function)(void) ) { - return endTransmission(true); + user_onRequest = function; } #if SOC_I2C_SUPPORT_SLAVE @@ -688,17 +645,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); diff --git a/libraries/Wire/src/Wire.h b/libraries/Wire/src/Wire.h index 5af1e7ff939..2e893e9d756 100644 --- a/libraries/Wire/src/Wire.h +++ b/libraries/Wire/src/Wire.h @@ -35,6 +35,7 @@ #include "freertos/task.h" #include "freertos/semphr.h" #endif +#include "HardwareI2C.h" #include "Stream.h" // WIRE_HAS_BUFFER_SIZE means Wire has setBufferSize() @@ -48,7 +49,7 @@ typedef void(*user_onRequest)(void); typedef void(*user_onReceive)(uint8_t*, int); -class TwoWire: public Stream +class TwoWire: public HardwareI2C { protected: uint8_t num; @@ -79,84 +80,61 @@ 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(); + + void begin() override final{ + begin(-1, -1); + } + + void begin(uint8_t address) override final + { + begin(address, -1, -1, 0); + } + + + void end() override; + + void 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); bool begin(int sda, int scl, uint32_t frequency=0); // returns true, if successful init of i2c bus bool begin(uint8_t slaveAddr, int sda, int scl, uint32_t frequency); - // Explicit Overload for Arduino MainStream API compatibility - inline bool begin() - { - return begin(-1, -1, static_cast(0)); - } - inline bool begin(uint8_t addr) - { - return begin(addr, -1, -1, 0); - } - 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 = true); - // 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); - } + + + 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 */ }; From b36ff08bd1d4f4a064cc3faad01a36f1cf94953b Mon Sep 17 00:00:00 2001 From: safocl Date: Tue, 28 Nov 2023 17:24:22 +0400 Subject: [PATCH 04/21] Fix TwoWire::end() return type. --- libraries/Wire/src/Wire.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Wire/src/Wire.cpp b/libraries/Wire/src/Wire.cpp index 9fd552bae3e..0d011db5d1a 100644 --- a/libraries/Wire/src/Wire.cpp +++ b/libraries/Wire/src/Wire.cpp @@ -332,7 +332,7 @@ 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 From f294609bb7a6f508297b34021d847ea25613fbae Mon Sep 17 00:00:00 2001 From: safocl Date: Tue, 28 Nov 2023 17:30:26 +0400 Subject: [PATCH 05/21] Fix TwoWire::setClock() return type. --- libraries/Wire/src/Wire.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/libraries/Wire/src/Wire.cpp b/libraries/Wire/src/Wire.cpp index 0d011db5d1a..60a9c106495 100644 --- a/libraries/Wire/src/Wire.cpp +++ b/libraries/Wire/src/Wire.cpp @@ -388,7 +388,7 @@ 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 @@ -411,7 +411,6 @@ bool TwoWire::setClock(uint32_t frequency) //release lock xSemaphoreGive(lock); #endif - return (err == ESP_OK); } void TwoWire::setTimeOut(uint16_t timeOutMillis) From a1e2e69e07da3183738c38e821ac5576427fdecd Mon Sep 17 00:00:00 2001 From: safocl Date: Tue, 28 Nov 2023 17:35:18 +0400 Subject: [PATCH 06/21] Fix no return statement in the TwoWire::requestFrom. --- libraries/Wire/src/Wire.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Wire/src/Wire.cpp b/libraries/Wire/src/Wire.cpp index 60a9c106495..6c61826ef16 100644 --- a/libraries/Wire/src/Wire.cpp +++ b/libraries/Wire/src/Wire.cpp @@ -545,7 +545,7 @@ size_t TwoWire::requestFrom(uint8_t address, size_t size, bool sendStop) } size_t TwoWire::requestFrom(uint8_t address, size_t size){ - requestFrom(address, size, true); + return requestFrom(address, size, true); } size_t TwoWire::write(uint8_t data) From fca0f8579c22d6c04c15d105c1f4f77ebc22717d Mon Sep 17 00:00:00 2001 From: safocl Date: Thu, 18 Jan 2024 04:46:24 +0400 Subject: [PATCH 07/21] fix(libraries/Wire): fix bad return-statement Remove non-void values of the return-statements in function returning 'void'. --- libraries/Wire/src/Wire.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries/Wire/src/Wire.cpp b/libraries/Wire/src/Wire.cpp index f13426ceeac..83845f6cc62 100644 --- a/libraries/Wire/src/Wire.cpp +++ b/libraries/Wire/src/Wire.cpp @@ -340,7 +340,7 @@ void TwoWire::end() //acquire lock if(xSemaphoreTake(lock, portMAX_DELAY) != pdTRUE){ log_e("could not acquire lock"); - return false; + return; } #endif #if SOC_I2C_SUPPORT_SLAVE @@ -360,7 +360,7 @@ void TwoWire::end() xSemaphoreGive(lock); } #endif - return (err == ESP_OK); + return; } uint32_t TwoWire::getClock() @@ -395,7 +395,7 @@ void TwoWire::setClock(uint32_t frequency) //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 From 135ad6a95d93aff3f242be89206c82c1e93bf626 Mon Sep 17 00:00:00 2001 From: safocl Date: Thu, 18 Jan 2024 04:59:12 +0400 Subject: [PATCH 08/21] style(libraries/Wire): replace tabs with spaces --- libraries/Wire/src/Wire.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libraries/Wire/src/Wire.h b/libraries/Wire/src/Wire.h index 1d0196acc28..6e09f5e1013 100644 --- a/libraries/Wire/src/Wire.h +++ b/libraries/Wire/src/Wire.h @@ -89,12 +89,13 @@ class TwoWire: public HardwareI2C TwoWire(uint8_t bus_num); ~TwoWire(); - void begin() override final{ + void begin() override final + { begin(-1, -1); } void begin(uint8_t address) override final - { + { begin(address, -1, -1, 0); } From 3cf20e08fdce7136c4e695fb07e22c803064e4a6 Mon Sep 17 00:00:00 2001 From: safocl Date: Thu, 18 Jan 2024 05:19:17 +0400 Subject: [PATCH 09/21] refactor(libraries/Wire): use slave without support TwoWire::begin(uint8_t address) should be available without slave support by SoC? --- libraries/Wire/src/Wire.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/libraries/Wire/src/Wire.h b/libraries/Wire/src/Wire.h index 6e09f5e1013..0d0f1af33de 100644 --- a/libraries/Wire/src/Wire.h +++ b/libraries/Wire/src/Wire.h @@ -30,6 +30,7 @@ #if SOC_I2C_SUPPORTED #include +#include #if !CONFIG_DISABLE_HAL_LOCKS #include "freertos/FreeRTOS.h" #include "freertos/task.h" @@ -94,9 +95,16 @@ class TwoWire: public HardwareI2C begin(-1, -1); } + /** + * Fixme: it is necessary to determine whether it is possible to use without SOC_I2C_SUPPORT_SLAVE? !!! + */ void begin(uint8_t address) override final { +#if SOC_I2C_SUPPORT_SLAVE begin(address, -1, -1, 0); +#else + log_e("I2C slave is not supported by you SoC!!!"); +#endif } From 7a351dd0d0bafe3721592d155fc3e07793e93f9f Mon Sep 17 00:00:00 2001 From: safocl Date: Thu, 18 Jan 2024 17:01:03 +0400 Subject: [PATCH 10/21] refactor(libraries/Wire): remove unused variables Compiler reports "Wire.cpp:393:15: error: variable 'err' set but not used [-Werror=unused-but-set-variable]". --- libraries/Wire/src/Wire.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/libraries/Wire/src/Wire.cpp b/libraries/Wire/src/Wire.cpp index 83845f6cc62..a19c376e75b 100644 --- a/libraries/Wire/src/Wire.cpp +++ b/libraries/Wire/src/Wire.cpp @@ -390,7 +390,6 @@ uint32_t TwoWire::getClock() 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){ @@ -401,11 +400,10 @@ void TwoWire::setClock(uint32_t frequency) #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 From 011a2dac95f34f34dabc012929546de34bb60e5e Mon Sep 17 00:00:00 2001 From: safocl Date: Thu, 18 Jan 2024 17:11:08 +0400 Subject: [PATCH 11/21] refactor(libraries/Wire): remove unused variables Compiler reports "Wire.cpp:337:15: error: variable 'err' set but not used [-Werror=unused-but-set-variable]". --- libraries/Wire/src/Wire.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/libraries/Wire/src/Wire.cpp b/libraries/Wire/src/Wire.cpp index a19c376e75b..160a1141a2d 100644 --- a/libraries/Wire/src/Wire.cpp +++ b/libraries/Wire/src/Wire.cpp @@ -334,7 +334,6 @@ bool TwoWire::begin(int sdaPin, int sclPin, uint32_t frequency) void TwoWire::end() { - esp_err_t err = ESP_OK; #if !CONFIG_DISABLE_HAL_LOCKS if(lock != NULL){ //acquire lock @@ -345,14 +344,14 @@ void TwoWire::end() #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 From a6115aed87637492c6f799110f969ccd07340433 Mon Sep 17 00:00:00 2001 From: safocl Date: Thu, 18 Jan 2024 17:15:51 +0400 Subject: [PATCH 12/21] fix(libraries/Wire): hide slave support elements TwoWire::user_onRequest is used only in conjunction with slave support? --- libraries/Wire/src/Wire.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libraries/Wire/src/Wire.cpp b/libraries/Wire/src/Wire.cpp index 160a1141a2d..0430f29d644 100644 --- a/libraries/Wire/src/Wire.cpp +++ b/libraries/Wire/src/Wire.cpp @@ -616,13 +616,17 @@ void TwoWire::flush() void TwoWire::onReceive( void (*function)(int) ) { +#if SOC_I2C_SUPPORT_SLAVE user_onReceive = function; +#endif } // sets function called on slave read void TwoWire::onRequest( void (*function)(void) ) { +#if SOC_I2C_SUPPORT_SLAVE user_onRequest = function; +#endif } #if SOC_I2C_SUPPORT_SLAVE From 1b9f656aea7c00a9a74c672902275a0864520388 Mon Sep 17 00:00:00 2001 From: safocl Date: Fri, 19 Jan 2024 23:54:25 +0400 Subject: [PATCH 13/21] refactor(libraries/Wire): remove temporary comment --- libraries/Wire/src/Wire.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/libraries/Wire/src/Wire.h b/libraries/Wire/src/Wire.h index 0d0f1af33de..4a9903582a5 100644 --- a/libraries/Wire/src/Wire.h +++ b/libraries/Wire/src/Wire.h @@ -95,9 +95,6 @@ class TwoWire: public HardwareI2C begin(-1, -1); } - /** - * Fixme: it is necessary to determine whether it is possible to use without SOC_I2C_SUPPORT_SLAVE? !!! - */ void begin(uint8_t address) override final { #if SOC_I2C_SUPPORT_SLAVE From 75f8ee95e804828755f0a4a2f626bd51a027053b Mon Sep 17 00:00:00 2001 From: safocl Date: Sat, 20 Jan 2024 08:40:40 +0400 Subject: [PATCH 14/21] fix(libraries/Wire): restore an accidentally deleted implementation TwoWire::endTransmission() was accidentally deleted from a cpp file. --- libraries/Wire/src/Wire.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/libraries/Wire/src/Wire.cpp b/libraries/Wire/src/Wire.cpp index 0430f29d644..7baae0bb312 100644 --- a/libraries/Wire/src/Wire.cpp +++ b/libraries/Wire/src/Wire.cpp @@ -488,6 +488,11 @@ uint8_t TwoWire::endTransmission(bool sendStop) return 4; } +uint8_t TwoWire::endTransmission() +{ + return endTransmission(true); +} + size_t TwoWire::requestFrom(uint8_t address, size_t size, bool sendStop) { #if SOC_I2C_SUPPORT_SLAVE From 07154af8136abe83880feb6f831c2e282b0201c3 Mon Sep 17 00:00:00 2001 From: safocl Date: Sun, 28 Jan 2024 21:57:15 +0400 Subject: [PATCH 15/21] refactor(libraries/Wire): return return types In TwoWire class return return types. --- cores/esp32/HardwareI2C.h | 8 ++++---- libraries/Wire/src/Wire.cpp | 18 +++++++++++------- libraries/Wire/src/Wire.h | 12 ++++++------ 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/cores/esp32/HardwareI2C.h b/cores/esp32/HardwareI2C.h index 2db796af1bb..830c24a80d6 100644 --- a/cores/esp32/HardwareI2C.h +++ b/cores/esp32/HardwareI2C.h @@ -24,11 +24,11 @@ class HardwareI2C : public Stream { public: - virtual void begin() = 0; - virtual void begin(uint8_t address) = 0; - virtual void end() = 0; + virtual bool begin() = 0; + virtual bool begin(uint8_t address) = 0; + virtual bool end() = 0; - virtual void setClock(uint32_t freq) = 0; + virtual bool setClock(uint32_t freq) = 0; virtual void beginTransmission(uint8_t address) = 0; virtual uint8_t endTransmission(bool stopBit) = 0; diff --git a/libraries/Wire/src/Wire.cpp b/libraries/Wire/src/Wire.cpp index 7baae0bb312..1ca2d786a76 100644 --- a/libraries/Wire/src/Wire.cpp +++ b/libraries/Wire/src/Wire.cpp @@ -332,26 +332,27 @@ bool TwoWire::begin(int sdaPin, int sclPin, uint32_t frequency) } -void TwoWire::end() +bool 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; + return false; } #endif #if SOC_I2C_SUPPORT_SLAVE if(is_slave){ - esp_err_t err = i2cSlaveDeinit(num); + err = i2cSlaveDeinit(num); if(err == ESP_OK){ is_slave = false; } } else #endif /* SOC_I2C_SUPPORT_SLAVE */ if(i2cIsInit(num)){ - i2cDeinit(num); + err = i2cDeinit(num); } freeWireBuffer(); #if !CONFIG_DISABLE_HAL_LOCKS @@ -359,7 +360,7 @@ void TwoWire::end() xSemaphoreGive(lock); } #endif - return; + return (err == ESP_OK); } uint32_t TwoWire::getClock() @@ -389,25 +390,28 @@ uint32_t TwoWire::getClock() 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; + return false; } #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 */ { - i2cSetClock(num, frequency); + err = i2cSetClock(num, frequency); } #if !CONFIG_DISABLE_HAL_LOCKS //release lock xSemaphoreGive(lock); #endif + return (err == ESP_OK); } void TwoWire::setTimeOut(uint16_t timeOutMillis) diff --git a/libraries/Wire/src/Wire.h b/libraries/Wire/src/Wire.h index 4a9903582a5..b6ba77844cf 100644 --- a/libraries/Wire/src/Wire.h +++ b/libraries/Wire/src/Wire.h @@ -90,24 +90,24 @@ class TwoWire: public HardwareI2C TwoWire(uint8_t bus_num); ~TwoWire(); - void begin() override final + bool begin() override final { - begin(-1, -1); + return begin(-1, -1); } - void begin(uint8_t address) override final + bool begin(uint8_t address) override final { #if SOC_I2C_SUPPORT_SLAVE - begin(address, -1, -1, 0); + return begin(address, -1, -1, 0); #else log_e("I2C slave is not supported by you SoC!!!"); #endif } - void end() override; + bool end() override; - void setClock(uint32_t freq) override; + bool setClock(uint32_t freq) override; void beginTransmission(uint8_t address) override; uint8_t endTransmission(bool stopBit) override; From 69a5890f441cb8654dccb07796544bd6b3f30a40 Mon Sep 17 00:00:00 2001 From: safocl Date: Sun, 28 Jan 2024 22:06:27 +0400 Subject: [PATCH 16/21] fix(libraries/Wire): fix return type --- libraries/Wire/src/Wire.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Wire/src/Wire.cpp b/libraries/Wire/src/Wire.cpp index 1ca2d786a76..859adcb32ed 100644 --- a/libraries/Wire/src/Wire.cpp +++ b/libraries/Wire/src/Wire.cpp @@ -388,7 +388,7 @@ uint32_t TwoWire::getClock() return frequency; } -void TwoWire::setClock(uint32_t frequency) +bool TwoWire::setClock(uint32_t frequency) { esp_err_t err = ESP_OK; #if !CONFIG_DISABLE_HAL_LOCKS From 35c0568f5165a3f5998444ac2696ba8bb5d26dcd Mon Sep 17 00:00:00 2001 From: safocl Date: Mon, 29 Jan 2024 15:23:17 +0400 Subject: [PATCH 17/21] refactor(libraries/Wire): add return statement if slave isn't supported MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jan Procházka <90197375+P-R-O-C-H-Y@users.noreply.github.com> --- libraries/Wire/src/Wire.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libraries/Wire/src/Wire.h b/libraries/Wire/src/Wire.h index b6ba77844cf..d53eb126dbe 100644 --- a/libraries/Wire/src/Wire.h +++ b/libraries/Wire/src/Wire.h @@ -100,7 +100,8 @@ class TwoWire: public HardwareI2C #if SOC_I2C_SUPPORT_SLAVE return begin(address, -1, -1, 0); #else - log_e("I2C slave is not supported by you SoC!!!"); + log_e("I2C slave is not supported on " CONFIG_IDF_TARGET); + return false; #endif } From eacea66d5e8ac708f884cf3dc78702aa61553e23 Mon Sep 17 00:00:00 2001 From: safocl Date: Mon, 29 Jan 2024 16:49:00 +0400 Subject: [PATCH 18/21] refactor(libraries/Wire): fix indentation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jan Procházka <90197375+P-R-O-C-H-Y@users.noreply.github.com> --- libraries/Wire/src/Wire.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Wire/src/Wire.cpp b/libraries/Wire/src/Wire.cpp index 859adcb32ed..ee049313e34 100644 --- a/libraries/Wire/src/Wire.cpp +++ b/libraries/Wire/src/Wire.cpp @@ -556,7 +556,7 @@ size_t TwoWire::requestFrom(uint8_t address, size_t size, bool sendStop) } size_t TwoWire::requestFrom(uint8_t address, size_t size){ - return requestFrom(address, size, true); + return requestFrom(address, size, true); } size_t TwoWire::write(uint8_t data) From b1ca983322aec413375147c3f350e2a357519a30 Mon Sep 17 00:00:00 2001 From: safocl Date: Mon, 29 Jan 2024 16:49:14 +0400 Subject: [PATCH 19/21] refactor(libraries/Wire): fix indentation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jan Procházka <90197375+P-R-O-C-H-Y@users.noreply.github.com> --- libraries/Wire/src/Wire.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Wire/src/Wire.cpp b/libraries/Wire/src/Wire.cpp index ee049313e34..8003acf8453 100644 --- a/libraries/Wire/src/Wire.cpp +++ b/libraries/Wire/src/Wire.cpp @@ -626,7 +626,7 @@ void TwoWire::flush() void TwoWire::onReceive( void (*function)(int) ) { #if SOC_I2C_SUPPORT_SLAVE - user_onReceive = function; + user_onReceive = function; #endif } From bca28876a28dd73c54e36740f3e614f94249a100 Mon Sep 17 00:00:00 2001 From: safocl Date: Mon, 29 Jan 2024 16:49:30 +0400 Subject: [PATCH 20/21] refactor(libraries/Wire): fix indentation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jan Procházka <90197375+P-R-O-C-H-Y@users.noreply.github.com> --- libraries/Wire/src/Wire.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Wire/src/Wire.cpp b/libraries/Wire/src/Wire.cpp index 8003acf8453..ac8c353575f 100644 --- a/libraries/Wire/src/Wire.cpp +++ b/libraries/Wire/src/Wire.cpp @@ -634,7 +634,7 @@ void TwoWire::onReceive( void (*function)(int) ) void TwoWire::onRequest( void (*function)(void) ) { #if SOC_I2C_SUPPORT_SLAVE - user_onRequest = function; + user_onRequest = function; #endif } From 311330e243cde78ab829b9c294cae05221e3c30a Mon Sep 17 00:00:00 2001 From: safocl Date: Mon, 29 Jan 2024 16:53:20 +0400 Subject: [PATCH 21/21] refactor(libraries/Wire): remove unnecessary empty lines --- libraries/Wire/src/Wire.h | 4 ---- 1 file changed, 4 deletions(-) diff --git a/libraries/Wire/src/Wire.h b/libraries/Wire/src/Wire.h index d53eb126dbe..2a5e9b4215d 100644 --- a/libraries/Wire/src/Wire.h +++ b/libraries/Wire/src/Wire.h @@ -105,7 +105,6 @@ class TwoWire: public HardwareI2C #endif } - bool end() override; bool setClock(uint32_t freq) override; @@ -119,7 +118,6 @@ class TwoWire: public HardwareI2C 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); @@ -136,8 +134,6 @@ class TwoWire: public HardwareI2C uint32_t getClock(); - - size_t write(uint8_t) override; size_t write(const uint8_t *, size_t) override; int available() override;