测试了 ESP32 S3 的 I2C 从机功能, 使用的是ESP32 S3 DevKitC-1 如下:
C:\Users\YOUNAME\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.4\libraries\Wire\examples\WireSlave
#include "Wire.h"
#define I2C_DEV_ADDR 0x55
uint32_t i = 0;
void onRequest(){
Wire.print(i++);
Wire.print(" Packets.");
Serial.println("onRequest");
}
void onReceive(int len){
Serial.printf("onReceive[%d]: ", len);
while(Wire.available()){
Serial.write(Wire.read());
}
Serial.println();
}
void setup() {
Serial.begin(115200);
Serial.setDebugOutput(true);
Wire.onReceive(onReceive);
Wire.onRequest(onRequest);
Wire.begin((uint8_t)I2C_DEV_ADDR);
#if CONFIG_IDF_TARGET_ESP32
char message[64];
snprintf(message, 64, "%u Packets.", i++);
Wire.slaveWrite((uint8_t *)message, strlen(message));
#endif
}
void loop() {
}
打开 Debug 开关,运行之后提示:
Attempting to boot anyway...
entry 0x403c98d8
[ 104][I][esp32-hal-i2c-slave.c:234] i2cSlaveInit(): Initialising I2C Slave: sda=8 scl=9 freq=100000, addr=0x55
[ 104][D][esp32-hal-i2c-slave.c:486] i2c_slave_set_frequency(): Fifo thresholds: rx_fifo_full = 28, tx_fifo_empty = 4
就是说:SDA 是8 Pin, SCL 是9 Pin,特别注意默认频率是 100K.
之后 I2C 工具设置如下:
设置发送数据 aa(地址) 31 32 33 34 35 36 37
ESP32 端收到的数据为
onReceive[7]: 1234567
测试使用的工具是志明电子出品的 USB 转SPI/I2C 调试工具。