Skip to content

Commit

Permalink
Fully guard I2C Slave in preparation for C2 support (espressif#8882)
Browse files Browse the repository at this point in the history
  • Loading branch information
me-no-dev authored Nov 13, 2023
1 parent 82e5fe8 commit 76782f2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
Empty file.
27 changes: 24 additions & 3 deletions libraries/Wire/src/Wire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ extern "C" {
}

#include "esp32-hal-i2c.h"
#if SOC_I2C_SUPPORT_SLAVE
#include "esp32-hal-i2c-slave.h"
#endif /* SOC_I2C_SUPPORT_SLAVE */
#include "Wire.h"
#include "Arduino.h"

Expand All @@ -53,9 +55,11 @@ TwoWire::TwoWire(uint8_t bus_num)
,nonStopTask(NULL)
,lock(NULL)
#endif
#if SOC_I2C_SUPPORT_SLAVE
,is_slave(false)
,user_onRequest(NULL)
,user_onReceive(NULL)
#endif /* SOC_I2C_SUPPORT_SLAVE */
{}

TwoWire::~TwoWire()
Expand Down Expand Up @@ -297,10 +301,12 @@ bool TwoWire::begin(int sdaPin, int sclPin, uint32_t frequency)
return false;
}
#endif
#if SOC_I2C_SUPPORT_SLAVE
if(is_slave){
log_e("Bus already started in Slave Mode.");
goto end;
}
#endif /* SOC_I2C_SUPPORT_SLAVE */
if(i2cIsInit(num)){
log_w("Bus already started in Master Mode.");
started = true;
Expand Down Expand Up @@ -337,12 +343,15 @@ bool TwoWire::end()
return false;
}
#endif
#if SOC_I2C_SUPPORT_SLAVE
if(is_slave){
err = i2cSlaveDeinit(num);
if(err == ESP_OK){
is_slave = false;
}
} else if(i2cIsInit(num)){
} else
#endif /* SOC_I2C_SUPPORT_SLAVE */
if(i2cIsInit(num)){
err = i2cDeinit(num);
}
freeWireBuffer();
Expand All @@ -363,9 +372,12 @@ uint32_t TwoWire::getClock()
log_e("could not acquire lock");
} else {
#endif
#if SOC_I2C_SUPPORT_SLAVE
if(is_slave){
log_e("Bus is in Slave Mode");
} else {
} else
#endif /* SOC_I2C_SUPPORT_SLAVE */
{
i2cGetClock(num, &frequency);
}
#if !CONFIG_DISABLE_HAL_LOCKS
Expand All @@ -386,10 +398,13 @@ bool TwoWire::setClock(uint32_t frequency)
return false;
}
#endif
#if SOC_I2C_SUPPORT_SLAVE
if(is_slave){
log_e("Bus is in Slave Mode");
err = ESP_FAIL;
} else {
} else
#endif /* SOC_I2C_SUPPORT_SLAVE */
{
err = i2cSetClock(num, frequency);
}
#if !CONFIG_DISABLE_HAL_LOCKS
Expand All @@ -411,10 +426,12 @@ uint16_t TwoWire::getTimeOut()

void TwoWire::beginTransmission(uint16_t address)
{
#if SOC_I2C_SUPPORT_SLAVE
if(is_slave){
log_e("Bus is in Slave Mode");
return;
}
#endif /* SOC_I2C_SUPPORT_SLAVE */
#if !CONFIG_DISABLE_HAL_LOCKS
if(nonStop && nonStopTask == xTaskGetCurrentTaskHandle()){
log_e("Unfinished Repeated Start transaction! Expected requestFrom, not beginTransmission! Clearing...");
Expand Down Expand Up @@ -444,10 +461,12 @@ endTransmission() returns:
*/
uint8_t TwoWire::endTransmission(bool sendStop)
{
#if SOC_I2C_SUPPORT_SLAVE
if(is_slave){
log_e("Bus is in Slave Mode");
return 4;
}
#endif /* SOC_I2C_SUPPORT_SLAVE */
if (txBuffer == NULL){
log_e("NULL TX buffer pointer");
return 4;
Expand Down Expand Up @@ -477,10 +496,12 @@ uint8_t TwoWire::endTransmission(bool sendStop)

size_t TwoWire::requestFrom(uint16_t address, size_t size, bool sendStop)
{
#if SOC_I2C_SUPPORT_SLAVE
if(is_slave){
log_e("Bus is in Slave Mode");
return 0;
}
#endif /* SOC_I2C_SUPPORT_SLAVE */
if (rxBuffer == NULL || txBuffer == NULL){
log_e("NULL buffer pointer");
return 0;
Expand Down
8 changes: 7 additions & 1 deletion libraries/Wire/src/Wire.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@
#ifndef I2C_BUFFER_LENGTH
#define I2C_BUFFER_LENGTH 128 // Default size, if none is set using Wire::setBuffersize(size_t)
#endif
#if SOC_I2C_SUPPORT_SLAVE
typedef void(*user_onRequest)(void);
typedef void(*user_onReceive)(uint8_t*, int);
#endif /* SOC_I2C_SUPPORT_SLAVE */

class TwoWire: public Stream
{
Expand All @@ -71,8 +73,8 @@ class TwoWire: public Stream
SemaphoreHandle_t lock;
#endif
private:
bool is_slave;
#if SOC_I2C_SUPPORT_SLAVE
bool is_slave;
void (*user_onRequest)(void);
void (*user_onReceive)(int);
static void onRequestService(uint8_t, void *);
Expand All @@ -90,12 +92,15 @@ class TwoWire: public Stream
bool setPins(int sda, int scl);

bool begin(int sda, int scl, uint32_t frequency=0); // returns true, if successful init of i2c bus
#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);
Expand All @@ -104,6 +109,7 @@ class TwoWire: public Stream
{
return begin(static_cast<uint8_t>(addr), -1, -1, 0);
}
#endif /* SOC_I2C_SUPPORT_SLAVE */
bool end();

size_t setBufferSize(size_t bSize);
Expand Down

0 comments on commit 76782f2

Please sign in to comment.