0

This is the link I am using to make Raspberry PI as an I2C slave, I see it uses the bscXfer which is OS timing independent. This is cool.

Raspberry as an I2C SLAVE

Similarly, I want to make raspberry PI as an I2C master and wants to use some I2C library which is written in C/C++ which is OS timing independent so that I don't need to add any delays when I will be communicating with slave.

I was using WiringPI library for master initialization, read and write functions. These are the API's that I have used it, wiringPiI2CSetup() wiringPiI2CRead() wiringPiI2CWrite(). To use this functions to communicate with slave, I have to add some delays. I want to use the library, so that I don't need to add any delays. Please suggest any library that you know as like pigpio.


// Master Initialization func,

fd = wiringPiI2CSetup(port_number);

// Slave Initialization func, 

gpioInitialise(); 
xfer.control = getControlBits(0x03, FALSE); // To avoid conflicts when restarting
bscXfer(&xfer);
xfer.control = getControlBits(0x03, TRUE);
Status = bscXfer(&xfer); // Should now be visible in I2C-Scanners
 
//Master Read func, 

while (number_received < number_of_bytes) {
    buffer[number_received] = wiringPiI2CRead(fd);
    number_received++;}
msleep(1200);

// Slave Read func,

do{
    bscXfer(&xfer);
    if(xfer.rxCnt > 0){
        memcpy(buffer, xfer.rxBuf, xfer.rxCnt);
        for(i = 0; i < xfer.rxCnt; i++)
             printf("data[%d] = %02x\r\n", i, buffer[i]);
    }
    msleep(100);
}
while(xfer.rxCnt == 0);
msleep(1200);

//Master Write func

while (number_sent < number_of_bytes){ 
    wiringPiI2CWrite(fd,buffer[number_sent]);
    number_sent++;
    msleep(5);
}
sleep(1);

// Slave Write func,

memcpy(xfer.txBuf, buffer,  number_of_bytes);
xfer.txCnt = number_of_bytes;
bscXfer(&xfer);
for(int i = 0; i < number_of_bytes; i++)
    printf("Write data[%d] = %02x\r\n", i, buffer[i]);
sleep(1);

7
  • Welcome -- but I think you need to clarify this: "add any delays when I will be communicating between master and slave" with an actual example, ie., a couple of lines of code from something you have written using some unsatisfactory library where you had to add delays to make it work. I2C uses a hardware based clock line. The OS does not implement the timing, period. Low level I2C calls are generally blocking. You should not have to be fussing with delays to make them work.
    – goldilocks
    Jan 13 at 15:20
  • I was using WiringPI library for master initialization, read and write functions. These are the API's that I have used it, wiringPiI2CSetup() wiringPiI2CRead() wiringPiI2CWrite() To use this functions to communicate with slave, I have to add some delays. I want to use the library, so that I don't need to add any delays. Please suggest any library that you know as like pigpio. Jan 13 at 15:55
  • Alright, although it would be better with an explicit example in code. I think I see an XY problem problem here...
    – goldilocks
    Jan 13 at 16:11
  • I have put the code in issue description, Please check and suggest me which library I need to use for master so that I don't need to add any delays for sending and receiving data. Jan 13 at 16:34
  • Can we use pigpio library for master to send and receive data over I2C lines in raspberry PI? Jan 17 at 5:05

Browse other questions tagged or ask your own question.