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

Thread: I2C Slave on T3.6 receiving all messages after first match

  1. #1
    Senior Member johnnyfp's Avatar
    Join Date
    Jan 2014
    Location
    New Zealand
    Posts
    263

    I2C Slave on T3.6 receiving all messages after first match

    Hi,

    I've got a very simple Master slave setup where the T3.6 would ignore any packets not addressed to it (as it should) and would fire the onRequest callback when a packet is addressed to it.
    However after the first addressed message, the T3.6 would then fire the onRequest callback for any packets on the I2C bus regardless of the address used.

    This is the code I used on the T3.6. I have tried this on all 4 Wire channels on the T3.6 with the same effect (I have enabled the Wire3 in the library).
    Code:
    #include <Wire.h>
    
    uint32_t timeNow=0;
    uint32_t tickTime=0;
    boolean ledDone=false;
    int indistate=0;
    
    uint8_t opcode3=0;
    
    bool wire3Received=false;
    bool wire3Requested=false;
    
    void wire3ReceiveHandlerFunction(int bytesReceived){
      wire3Received=true;
      while(1<Wire3.available()) Wire3.read();
      opcode3 = Wire3.read();  
    }
    
    void printReceived(int port,int code){
      Serial.printf("SDA%d received 0x%02x\r\n",port,code);
    }
    
    void wire3RequestHandlerFunction(){
      wire3Requested=true;
      Wire3.write(1);
      Wire3.write(3);
      Wire3.write(3);
      opcode3=0;
    }
    
    void printRequest(int port) {
      Serial.printf("SDA%d Request called\r\n",port);
    }
    
    void setup() {
      Serial.begin(115200);
      pinMode(LED_BUILTIN,OUTPUT);
      Wire3.begin(0x6D);
      Wire3.onReceive(wire3ReceiveHandlerFunction);
      Wire3.onRequest(wire3RequestHandlerFunction);
      timeNow=millis();
    }
    
    void loop() {
    
      if (wire3Received) {
        printReceived(3,opcode3);
        wire3Received=false;
      }
      if (wire3Requested) {
        printRequest(3);
        wire3Requested=false;
      }
      
      if (millis()-timeNow>250) {
        tickTime++;
        timeNow=millis();
      }
      if (tickTime%2==1) {
        if (!ledDone) {
          digitalWrite(LED_BUILTIN,HIGH);
          ledDone=true;
        }
      } else {
        if (ledDone) {
          digitalWrite(LED_BUILTIN,LOW);
          ledDone=false;
        }
      }
      
      
    }
    Then on the master all I'm doing is the following.
    Code:
    Wire.begin();
      Wire.beginTransmission(0x6D);
      Wire.write(0x77);
      Wire.endTransmission();
      Wire.requestFrom(0x6D,3);
      Wire.read();
      Wire.read();
      Wire.read();
      delay(1000);
      Wire.beginTransmission(0x50);
      Wire.write(0x50);
      Wire.write(0x18);
      Wire.endTransmission();
      Wire.requestFrom(0x50,4);
      Wire.read();
      Wire.read();
      Wire.read();
      Wire.read();
    and the console would output
    Code:
    SDA3 received 0x77
    SDA3 Request called
    SDA3 received 0x77 <--Not expected
    SDA3 received 0x77 <--Not expected and appeared when the Wire request to Addr 0x50 was called.
    Any ideas why this is happening?

  2. #2
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,366
    Try declaring these three volatile:
    Code:
    volatile uint8_t opcode3=0;
    
    volatile bool wire3Received=false;
    volatile bool wire3Requested=false;
    Pete

  3. #3
    Senior Member johnnyfp's Avatar
    Join Date
    Jan 2014
    Location
    New Zealand
    Posts
    263
    Still the same issue. I'm wondering if a flag has to be cleared on the I2C bus?

  4. #4
    Senior Member johnnyfp's Avatar
    Join Date
    Jan 2014
    Location
    New Zealand
    Posts
    263
    Ok Answering my own question.

    Basically seems the Wire.h library is not Teensy3.6 friendly when using slave mode. Changing it to the i2c_t3 library fixes the issue.

  5. #5
    Junior Member
    Join Date
    Sep 2019
    Posts
    13
    I made the observation that Teensy 3.6 slave mode shows wrong number of bytes received. In a test transferring two bytes from master to a Teensy 3.6 slave the number of received bytes are wrong by two one after the other.
    The picture of the LA shows that on the line the right number is transmitted. It seems to be something on the Teensy 3.6 slave side. It may disturb the transmission depending on the software.

    Click image for larger version. 

Name:	p2-1.jpg 
Views:	4 
Size:	120.0 KB 
ID:	19979

    Click image for larger version. 

Name:	p2-2.jpg 
Views:	4 
Size:	78.7 KB 
ID:	19980

    Using i2c_t3 library fixed the problem.

Posting Permissions

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