Unverified Commit 76782f2c authored by Me No Dev's avatar Me No Dev Committed by GitHub

Fully guard I2C Slave in preparation for C2 support (#8882)

parent 82e5fe8a
......@@ -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"
......@@ -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()
......@@ -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;
......@@ -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();
......@@ -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
......@@ -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
......@@ -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...");
......@@ -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;
......@@ -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;
......
......@@ -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
{
......@@ -71,8 +73,8 @@ protected:
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 *);
......@@ -90,12 +92,15 @@ public:
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);
......@@ -104,6 +109,7 @@ public:
{
return begin(static_cast<uint8_t>(addr), -1, -1, 0);
}
#endif /* SOC_I2C_SUPPORT_SLAVE */
bool end();
size_t setBufferSize(size_t bSize);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment