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

Status
Not open for further replies.

johnnyfp

Well-known member
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?
 
Try declaring these three volatile:
Code:
volatile uint8_t opcode3=0;

volatile bool wire3Received=false;
volatile bool wire3Requested=false;

Pete
 
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.
 
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.

p2-1.jpg

p2-2.jpg

Using i2c_t3 library fixed the problem.
 
Status
Not open for further replies.
Back
Top