S
S
Sencis2020-04-25 20:03:00
linux
Sencis, 2020-04-25 20:03:00

How to work with two devices on the I2C bus on Rasppberry?

For 2 days I have been unsuccessfully trying to get 2a devices to work together on the same bus, I can’t find an example of how several IIC devices for Rasppberry Pi work anywhere. There are no problems with one device.

#include <inttypes.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
#include <i2c-dev.h>


int main ()
{
//Открываю порт
if(( _HandleBNO = open("/dev/i2c-1", O_RDWR)) < 0) ;

//Открываю устройство
if( ioctl( _HandleBNO, I2C_SLAVE, _address) != 0);

 uint8_t buf [2]= { subaddress, data }; // Адрес регистра и данные 

//Запись
if(write(_HandleBNO,  &buf,  1) != 1) ;

//Чтение
 if(read(_HandleBNO, buffer, len) != len) ;

 return 0;
}


The problem is in ioctl( _HandleBNO, I2C_SLAVE, _address) because this function adds the address of the specified device to each message sent, which prevents 2m from working at once. But this function is present in all libraries and without it the entry does not work, I tried to change the value of ioctl( _HandleBNO, I2C_SLAVE, NULL) but without success. Are there any alternative ways to work with the i2c bus?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
paran0id, 2020-04-25
@paran0id

Are the devices the same or different? If they are different, then their addresses on the bus are different, you can see them using i2cdetect. If they are the same, then it is more difficult. I haven’t done it myself yet, but I dug up a guide: https://www.instructables.com/id/Raspberry-PI-Mult...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question