Arduino-AirMCU icon indicating copy to clipboard operation
Arduino-AirMCU copied to clipboard

I2c作为从机,被主机用requestFrom读取时异常

Open luckycheng2017 opened this issue 1 year ago • 7 comments

描述一下这个bug / Describe the bug

手里有一个ESP32和AIR001的板子,ESP32作主机,AIR001作为从机。ESP32用requestFrom方法,不能正常触发从机AIR001的onRequest注册的方法。但是两个ESP32分别作主机和从机,主机能够正常读取从机的数据。

复现步骤 / To Reproduce

从机先复位,然后主机复位,主机能找到从机的地址,但是不能读到从机的数据。

如果正常,应该是什么样 / Expected behavior

正常ESP32主机能够读取到AIR001从机发过来的数据。

截图 / Screenshots

ESP32主机的代码 ` #include <Wire.h>

byte i = 0;

uint8_t hello[] = "hello"; uint8_t aTxBuffer[] = "你好";

void setup() { Wire.setClock(100000); Wire.begin(4, 5);

Serial.begin(9600); Serial.println("\nI2C Scanner");

byte error, address; int nDevices; Serial.println("Scanning..."); nDevices = 0; for(address = 1; address < 127; address++ ) { Wire.beginTransmission(address); // Wire.write(aTxBuffer, sizeof(aTxBuffer)); error = Wire.endTransmission(); if (error == 0) { Serial.print("I2C device found at address 0x"); if (address<16) { Serial.print("0"); } Serial.println(address,HEX); nDevices++; } else if (error==4) { Serial.print("Unknow error at address 0x"); if (address<16) { Serial.print("0"); } Serial.println(address,HEX); }
} if (nDevices == 0) { Serial.println("No I2C devices found\n"); } else { Serial.println("done\n"); }

}

void loop() { byte error; byte address = 0x33; int len = Wire.requestFrom((uint16_t) address, (uint8_t) 6); if (len > 0) { Serial.printf("接收长度:%d\n", len); while (Wire.available() > 1) { char c = Wire.read(); Serial.print(c); } char x = Wire.read(); Serial.println(x); } Serial.println("");

delay(2000); }

`

AIR001从机代码 `#include <Wire.h>

#define I2C_ADDR 0x33

uint8_t hello[] = "hello"; uint8_t aTxBuffer[] = "你好";

// function that executes whenever data is received from master // this function is registered as an event, see setup() void myReceive(int howMany) { while(1 < Wire.available()) // loop through all but the last { char c = Wire.read(); // receive byte as a character Serial.println(c); // print the character } int x = Wire.read(); // receive byte as an integer Serial.println(x); // print the integer }

// function that executes whenever data is requested by master // this function is registered as an event, see setup() void myRequest() { Serial.println("收到请求"); Wire.write(0x01); // respond with message of 6 bytes // as expected by master }

void setup() { Serial.begin(9600); // start serial for output

Wire.setSDA(PF_0); Wire.setSCL(PF_1); Wire.setClock(100000); Wire.begin(I2C_ADDR); // join i2c bus with address #4 Wire.onRequest(myRequest); // register event Wire.onReceive(myReceive); // register event }

void loop() { //empty loop } `

日志 / Logs

这是主机的日志 1693141197589

系统 / System

Win10

PACK包版本 / Version

0.4.5

验证

  • [x] 检查过该问题,之前没有人提过 / Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • [X] 提供了最小可复现工程或详细的复现步骤,确保开发者可以复现 / The provided reproduction is a minimal reproducible example of the bug.
  • [X] 已经提供了完整的报错信息、日志、截图,没有经过删减。

luckycheng2017 avatar Aug 27 '23 13:08 luckycheng2017