Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 2 of 2

Thread: IIC onReceive is called 3 times for each onRequest weirdness

  1. #1
    Junior Member
    Join Date
    Nov 2022
    Posts
    2

    IIC onReceive is called 3 times for each onRequest weirdness

    Hi all,

    I have the weirdest IC 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

  2. #2
    Junior Member
    Join Date
    Nov 2022
    Posts
    2
    As a side note, using i2c_t3 library, the error is gone. So must be something in the wire library. And to be totally honest, I think I will just use this library.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •