Page 2 of 2

Re: I2C master/slave help

Posted: Sat May 02, 2020 9:30 pm
by mnf
Can you look at the data on the scope - one end is getting it wrong. But is it send, receive (or both)?

Re: I2C master/slave help

Posted: Sun May 03, 2020 8:40 am
by fotios
Thanks a lot again
I got a new screenshot of the scope. Everything looks OK. From the left, the first byte is the Slave ID = 0x38<<1 = 0x70, the second byte is the DataH=20d, and the third byte is the DataL=150d. The black triangles point the 9th bit=ACK. Additionally, I use 2 blue LEDs mounted on the STM32 application board, to show if the ACK is received each time by the master and both are ON. This time I added in the FCF two extra calculations to set again the ACK=1 after each ACK=0 at the end of each byte transmission.
However, the Argon still receives correctly only the last Data byte transmitted (i.e. 150) while the first Data byte is shown on Serial Monitor as 255d.
Capture-1.jpg
Capture-1.jpg (97.8 KiB) Viewed 6887 times

Re: I2C master/slave help

Posted: Sun May 03, 2020 10:21 am
by fotios
Hi Martin
OK, solved.
The error was in the expression while(1 < Wire.available()) { // loop through all but the last
It might be while(2 < Wire.available()) { // loop through all but the last to receive two bytes :lol: :lol:
So, I suppose for 3 bytes it should be 3 and so on.

A big thank to you again

Re: I2C master/slave help

Posted: Sun May 03, 2020 10:54 am
by mnf
Good spot.

Wire.available() returns number of bytes available - so use a variable (i=0) and use while( i < wire.available()){data = wire.read(); i+=1;}

It's okay loading to named variables for one or two bytes but for more you'll want to use an array..

Sorry - this from my phone. I'll tidy the code later!

Martin

Re: I2C master/slave help

Posted: Sun May 03, 2020 6:22 pm
by fotios
For anyone interested, here is the correct I2C Slave Wire (Arduino) code for UINT16 type (2 bytes) number reception:

Code: Select all

void receiveEvent(int howMany) {
  while(2 < Wire.available()) { // loop through all but the last
    char c = Wire.read();       // receive byte as a character
    Serial.print(c);            // print the character
  }
  uint8_t STdataH = Wire.read();
  uint8_t STdataL = Wire.read();
  uint16_t STdataRX = (STdataH << 8) + STdataL;

  Serial.println(STdataRX);
}

void setup() {
  Wire.begin(0x38);                // join i2c bus with address 0x38
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           // start serial for output
}

void loop() {
  delay(100);
}
And here is the I2C Master FCF for UINT type (2 bytes) number transmission:
I2C_STM32.fcfx
(14.77 KiB) Downloaded 267 times

Re: I2C master/slave help

Posted: Sun May 03, 2020 7:17 pm
by fotios
mnf wrote:Good spot.
Do you believe that I was able to find the bug? :lol: I'm not so smart, and so I asked in Particle community forum :roll: and got the fix.
Simply, the members of it, are not so polite like all of you here in Matrix forums.
The reply I got nearly was : "Hey man, are you serious? You expect to receive 2 bytes with (1 < Wire.available())?" :lol:
So, I usually avoid Particle forums unless is absolutely necessary :wink:
Your solution

Code: Select all

uint16_t STdataRX = (STdataH << 8) + STdataL;
is also the correct, I also had bug on it and thanks again.
All the best
Fotis

Re: I2C master/slave help

Posted: Sun May 03, 2020 8:49 pm
by mnf
Yes - some sites are less than friendly... Always seems a shame - everyone needs to learn something sometime.....


Martin