Hi,
I have a teensy 3.2 connected to an arduino mega via i2c, pullups 3.9 k to 5 V. The teensy is the master, the arduino the slave.
The communication is working fine with the standard i2c speed (100 kHz). However, if I change it to 400 kHz, then the onRequest handler of the arduino is never called. After many hours, I found out that if I add a tiny delay between sending data to the arduino and requesting data from the arduino, it works.
Here is a "minimum working example" that reproduces the behavior.
Teensy code:
Arduino:
While I can live with that delay, I want to understand why it does not work without.
Can someone explain that behavior?
I have a teensy 3.2 connected to an arduino mega via i2c, pullups 3.9 k to 5 V. The teensy is the master, the arduino the slave.
The communication is working fine with the standard i2c speed (100 kHz). However, if I change it to 400 kHz, then the onRequest handler of the arduino is never called. After many hours, I found out that if I add a tiny delay between sending data to the arduino and requesting data from the arduino, it works.
Here is a "minimum working example" that reproduces the behavior.
Teensy code:
Code:
#include <i2c_t3.h>
void setup() {
Wire.begin(I2C_MASTER, 0x00, I2C_PINS_18_19, I2C_PULLUP_EXT, 400000);
Wire.onReceive(receiveEvent);
}
void receiveEvent(unsigned int howMany) {
char cmd = Wire.read();
// toy code ...
}
void loop() {
Wire.beginTransmission(1);
Wire.write('S');
Wire.endTransmission();
delayMicroseconds(3); // 3 microseconds seems to be the minimum number, for 2 it stops working
Wire.requestFrom(1, 1);
delay(1000);
Wire.beginTransmission(1);
Wire.write('D');
Wire.endTransmission();
delayMicroseconds(3); // 3 microseconds seems to be the minimum number, for 2 it stops working
Wire.requestFrom(1, 1);
delay(1000);
}
Arduino:
Code:
#include <Wire.h>
#define CSPin 2
bool mode;
void setup() {
pinMode(CSPin, OUTPUT);
digitalWrite(CSPin, HIGH);
Wire.begin(1);
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);
mode = false;
}
void loop() {}
void receiveEvent(int howMany) {
char cmd = Wire.read();
if(cmd == 'S') {
mode = true;
} else {
mode = false;
}
}
void requestEvent() {
if(mode) {
digitalWrite(CSPin, LOW);
} else {
digitalWrite(CSPin, HIGH);
}
Wire.write('K');
}
While I can live with that delay, I want to understand why it does not work without.
Can someone explain that behavior?