0

I'm trying to make I2C communication between B-L072Z-LRWAN(Master) And Arduino(Slave).

I succeeded to send data from my master to my slave with the code below :

B-L072Z-LRWAN code :

#include "main.h"

I2C_HandleTypeDef hi2c1;
uint8_t i2cData[2];
uint8_t rec_data[1];

int main(void)
 {
  //I do not copy all the lines of code

    if(HAL_I2C_IsDeviceReady(&hi2c1,0xD0,2,10) == HAL_OK)
       {
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET);
       }
    i2cData[0] = 0x00;
    i2cData[1] = 0x7F;
    while (1)
       {   
          HAL_I2C_Master_Transmit(&hi2c1, 0xD0, i2cData, 2, 10);
       }

    //I do not copy all the lines of code
   }

Arduino Code :

 #include <Wire.h>

 uint8_t i = 1;
 uint8_t data[2];

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

void loop()
 {
   data[0] = i++;
   delay(500);
 }

// function that executes whenever data is received from master
// this function is registered as an event, see setup()

void receiveEvent(int howMany)
 {
   while (1 < Wire.available())
     { 
       // loop through all but the last
       int c = Wire.read(); // receive byte as a character
       Serial.print(c, HEX);         // print the character
     }
   int x = Wire.read();    // receive byte as an integer
   Serial.println(x);         // print the integer
  }

 void requestEvent()
   {
     Serial.println("request from master"); 
     Wire.write(data[0]); // respond with message of 6 bytes
      // as expected by master
   }

So i can send data to my slave, then i try to send data from my slave to my master, so i add this line code :

B-L072Z-LRWAN code :

  rec_data[0] = 0x04;
  while (1)
    {
      //reception data
      HAL_I2C_Master_Receive(&hi2c1, 0xD0, rec_data[0], 1, 10);
      HAL_Delay(500);
    }

I'm suppose receive what arduino send the incrementation of the value of i, but it's nor work, i continu to send data from my master but i can't send from my slave.

Maybe i I do not go wrong, can please help me ? thanks.

Kind regards,

jayen marco
  • 103
  • 2
  • 12
  • The `while (1)` loop in the first snippet never terminates. So yes, master keeps sending data continuously. It must stop at some point, and switch to receiving. – user58697 May 07 '19 at 18:27
  • add both `HAL_I2C_Master_Transmit & HAL_I2C_Master_Receive in same while(1)` with little delay.. then you will see Transmit & Receive Both – ntshetty May 08 '19 at 08:04
  • Hi user58697 , @ntshetty, thanks for your reply ! well, i add all in the while but nothing change : the rec_data[0] dosn't change his default value "4" ! here is a screenshot of my code https://imgur.com/vwPDzgJ – jayen marco May 09 '19 at 09:50

1 Answers1

0

Finnaly i found the solution, it was just to replace in the while "rec_data[0]" by "rec_data" :

 rec_data[0] = 0x04;

 while (1)
  {
    //reception data
    HAL_I2C_Master_Transmit(&hi2c1, 0xD0, i2cData, 1, 10);
    HAL_Delay(500);
    HAL_I2C_Master_Receive(&hi2c1, 0xD0, rec_data, 1, 10);
    HAL_Delay(500);
  }

thanks again guy !! ;)

jayen marco
  • 103
  • 2
  • 12