Hi all,
I have the weirdest I²C trouble with my setup, in short, every time the master is sending a "Wire.requestFrom()", the slave not only runs the onRequest function but also the onReceive function. From the code, the slave receives three bytes of data.
My setup:
master: Teensy 4.1
slave: TeensyLC
Pullups: 4k7 on SDA/SCL
VSCode & PlatformIO
Wire Library Version 1.0 (as PlatformIO tells me)
I have the weirdest I²C trouble with my setup, in short, every time the master is sending a "Wire.requestFrom()", the slave not only runs the onRequest function but also the onReceive function. From the code, the slave receives three bytes of data.
My setup:
master: Teensy 4.1
slave: TeensyLC
Pullups: 4k7 on SDA/SCL
VSCode & PlatformIO
Wire Library Version 1.0 (as PlatformIO tells me)
Code:
//Master
#include <Wire.h> //I2C communication
//##### SETUP
void setup(){
Wire.begin(); //start I2C
}
//##### LOOP
void loop(){
Wire.requestFrom(0x08,8,1); //request 8 bytes
while(Wire.available()){
Wire.read();
}
delay(3000);
uint8_t send_status = 1; //send
while(send_status != 0){
Wire.beginTransmission(0x08);
Wire.write(1);
send_status = Wire.endTransmission();
}
delay(3000);
}
Code:
//Slave
#include <Wire.h>
volatile uint8_t bytes_in = 0;
uint8_t mybuffer[8] = {0,1,2,3,4,5,6,7};
//##### SETUP
void setup(){
//I2C Setup
Wire.begin(0x08); //join I2C Bus at address 8 (0-7 is reserved)
Wire.onRequest(sendData); //what to do when being talked to
Wire.onReceive(receiveEvent); //what to do with data received
}
//##### LOOP
void loop(){
if(bytes_in){
Serial.println(bytes_in);
bytes_in = 0;
}
}
//##### FUNCTIONS
//send data on request
void sendData(){
Wire.write(mybuffer,8);
}
//receive data
void receiveEvent(int bytes_incoming){
while(Wire.available()){
Wire.read();
bytes_in += 1;
}
}
Code:
--- Terminal on /dev/ttyACM1 | 9600 8-N-1
--- Available filters and text transformations: colorize, debug, default, direct, hexlify, log2file, nocontrol, printable, send_on_enter, time
--- More details at https://bit.ly/pio-monitor-filters
--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H
1
3
1
3
1
3