Skip to content

Commit 76782f2

Browse files
authored
Fully guard I2C Slave in preparation for C2 support (#8882)
1 parent 82e5fe8 commit 76782f2

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

Diff for: libraries/Wire/examples/WireSlave/.skip.esp32c2

Whitespace-only changes.

Diff for: libraries/Wire/src/Wire.cpp

+24-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ extern "C" {
3232
}
3333

3434
#include "esp32-hal-i2c.h"
35+
#if SOC_I2C_SUPPORT_SLAVE
3536
#include "esp32-hal-i2c-slave.h"
37+
#endif /* SOC_I2C_SUPPORT_SLAVE */
3638
#include "Wire.h"
3739
#include "Arduino.h"
3840

@@ -53,9 +55,11 @@ TwoWire::TwoWire(uint8_t bus_num)
5355
,nonStopTask(NULL)
5456
,lock(NULL)
5557
#endif
58+
#if SOC_I2C_SUPPORT_SLAVE
5659
,is_slave(false)
5760
,user_onRequest(NULL)
5861
,user_onReceive(NULL)
62+
#endif /* SOC_I2C_SUPPORT_SLAVE */
5963
{}
6064

6165
TwoWire::~TwoWire()
@@ -297,10 +301,12 @@ bool TwoWire::begin(int sdaPin, int sclPin, uint32_t frequency)
297301
return false;
298302
}
299303
#endif
304+
#if SOC_I2C_SUPPORT_SLAVE
300305
if(is_slave){
301306
log_e("Bus already started in Slave Mode.");
302307
goto end;
303308
}
309+
#endif /* SOC_I2C_SUPPORT_SLAVE */
304310
if(i2cIsInit(num)){
305311
log_w("Bus already started in Master Mode.");
306312
started = true;
@@ -337,12 +343,15 @@ bool TwoWire::end()
337343
return false;
338344
}
339345
#endif
346+
#if SOC_I2C_SUPPORT_SLAVE
340347
if(is_slave){
341348
err = i2cSlaveDeinit(num);
342349
if(err == ESP_OK){
343350
is_slave = false;
344351
}
345-
} else if(i2cIsInit(num)){
352+
} else
353+
#endif /* SOC_I2C_SUPPORT_SLAVE */
354+
if(i2cIsInit(num)){
346355
err = i2cDeinit(num);
347356
}
348357
freeWireBuffer();
@@ -363,9 +372,12 @@ uint32_t TwoWire::getClock()
363372
log_e("could not acquire lock");
364373
} else {
365374
#endif
375+
#if SOC_I2C_SUPPORT_SLAVE
366376
if(is_slave){
367377
log_e("Bus is in Slave Mode");
368-
} else {
378+
} else
379+
#endif /* SOC_I2C_SUPPORT_SLAVE */
380+
{
369381
i2cGetClock(num, &frequency);
370382
}
371383
#if !CONFIG_DISABLE_HAL_LOCKS
@@ -386,10 +398,13 @@ bool TwoWire::setClock(uint32_t frequency)
386398
return false;
387399
}
388400
#endif
401+
#if SOC_I2C_SUPPORT_SLAVE
389402
if(is_slave){
390403
log_e("Bus is in Slave Mode");
391404
err = ESP_FAIL;
392-
} else {
405+
} else
406+
#endif /* SOC_I2C_SUPPORT_SLAVE */
407+
{
393408
err = i2cSetClock(num, frequency);
394409
}
395410
#if !CONFIG_DISABLE_HAL_LOCKS
@@ -411,10 +426,12 @@ uint16_t TwoWire::getTimeOut()
411426

412427
void TwoWire::beginTransmission(uint16_t address)
413428
{
429+
#if SOC_I2C_SUPPORT_SLAVE
414430
if(is_slave){
415431
log_e("Bus is in Slave Mode");
416432
return;
417433
}
434+
#endif /* SOC_I2C_SUPPORT_SLAVE */
418435
#if !CONFIG_DISABLE_HAL_LOCKS
419436
if(nonStop && nonStopTask == xTaskGetCurrentTaskHandle()){
420437
log_e("Unfinished Repeated Start transaction! Expected requestFrom, not beginTransmission! Clearing...");
@@ -444,10 +461,12 @@ endTransmission() returns:
444461
*/
445462
uint8_t TwoWire::endTransmission(bool sendStop)
446463
{
464+
#if SOC_I2C_SUPPORT_SLAVE
447465
if(is_slave){
448466
log_e("Bus is in Slave Mode");
449467
return 4;
450468
}
469+
#endif /* SOC_I2C_SUPPORT_SLAVE */
451470
if (txBuffer == NULL){
452471
log_e("NULL TX buffer pointer");
453472
return 4;
@@ -477,10 +496,12 @@ uint8_t TwoWire::endTransmission(bool sendStop)
477496

478497
size_t TwoWire::requestFrom(uint16_t address, size_t size, bool sendStop)
479498
{
499+
#if SOC_I2C_SUPPORT_SLAVE
480500
if(is_slave){
481501
log_e("Bus is in Slave Mode");
482502
return 0;
483503
}
504+
#endif /* SOC_I2C_SUPPORT_SLAVE */
484505
if (rxBuffer == NULL || txBuffer == NULL){
485506
log_e("NULL buffer pointer");
486507
return 0;

Diff for: libraries/Wire/src/Wire.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@
4545
#ifndef I2C_BUFFER_LENGTH
4646
#define I2C_BUFFER_LENGTH 128 // Default size, if none is set using Wire::setBuffersize(size_t)
4747
#endif
48+
#if SOC_I2C_SUPPORT_SLAVE
4849
typedef void(*user_onRequest)(void);
4950
typedef void(*user_onReceive)(uint8_t*, int);
51+
#endif /* SOC_I2C_SUPPORT_SLAVE */
5052

5153
class TwoWire: public Stream
5254
{
@@ -71,8 +73,8 @@ class TwoWire: public Stream
7173
SemaphoreHandle_t lock;
7274
#endif
7375
private:
74-
bool is_slave;
7576
#if SOC_I2C_SUPPORT_SLAVE
77+
bool is_slave;
7678
void (*user_onRequest)(void);
7779
void (*user_onReceive)(int);
7880
static void onRequestService(uint8_t, void *);
@@ -90,12 +92,15 @@ class TwoWire: public Stream
9092
bool setPins(int sda, int scl);
9193

9294
bool begin(int sda, int scl, uint32_t frequency=0); // returns true, if successful init of i2c bus
95+
#if SOC_I2C_SUPPORT_SLAVE
9396
bool begin(uint8_t slaveAddr, int sda, int scl, uint32_t frequency);
97+
#endif /* SOC_I2C_SUPPORT_SLAVE */
9498
// Explicit Overload for Arduino MainStream API compatibility
9599
inline bool begin()
96100
{
97101
return begin(-1, -1, static_cast<uint32_t>(0));
98102
}
103+
#if SOC_I2C_SUPPORT_SLAVE
99104
inline bool begin(uint8_t addr)
100105
{
101106
return begin(addr, -1, -1, 0);
@@ -104,6 +109,7 @@ class TwoWire: public Stream
104109
{
105110
return begin(static_cast<uint8_t>(addr), -1, -1, 0);
106111
}
112+
#endif /* SOC_I2C_SUPPORT_SLAVE */
107113
bool end();
108114

109115
size_t setBufferSize(size_t bSize);

0 commit comments

Comments
 (0)