Skip to content

Commit

Permalink
i2c bus update
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandruradovici committed Jan 12, 2015
1 parent d002d47 commit c5eeeca
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/libraries/Wire.cpp
Expand Up @@ -131,11 +131,12 @@ uint8_t TwoWire::requestFrom(int address, int quantity, int sendStop)

void TwoWire::beginTransmission(uint8_t address)
{
if (i2c_fd < 0 || adapter_nr < 0)
if (i2c_fd < 0)
return;
/* set slave address via ioctl in case we need to perform terminating
* write operation
*/
// printf ("i2c_fd: %d\n", i2c_fd);
i2c_setslave(i2c_fd, address);
// save address of target and empty buffer
txAddress = address;
Expand Down Expand Up @@ -274,7 +275,7 @@ static void Wire1_Init(void)
if ((adapter_nr = i2c_getadapter(I2C1)) < 0) {
return;
}
if ((i2c_fd = i2c_openadapter) < 0) {
if ((i2c_fd = i2c_openadapter(adapter_nr)) < 0) {
return;
}
}
Expand Down
14 changes: 12 additions & 2 deletions src/wiring/i2c.c
Expand Up @@ -20,6 +20,13 @@ static int i2c_addresses[MAX_I2C_PINS];

pthread_mutex_t locki2c;

void i2c_init ()
{
int i;
for (i=0; i<MAX_I2C_PINS; i++) i2c_buses[i]=-1;
}


int getI2CId ()
{
int i;
Expand Down Expand Up @@ -73,11 +80,14 @@ int i2c_openadapter(uint8_t i2c_bus)
#endif
}
char filepath[32];
// printf ("i2c_bus: %d\n", i2c_bus);
snprintf(filepath, 32, "/dev/i2c-%u", i2c_bus);
if ((i2c_buses[id] = open(filepath, O_RDWR)) < 1) {
if ((i2c_buses[id] = open(filepath, O_RDWR)) < 0) {
fprintf(stderr, "Failed to open requested i2c port %s\n", filepath);
perror ("bus");
}
//perror ("set");

// printf ("id: %d\n", id);
return id;
}

Expand Down
3 changes: 3 additions & 0 deletions src/wiring/raspberrypi.c
Expand Up @@ -20,6 +20,8 @@ static struct serial_bus *serial_buses[MAX_SERIAL_BUSES];
#define SPI_DEFAULT_FREQ 4000000
#define SERIAL_DEFAULT_SPEED 9600

void i2c_init ();

unsigned long pulseIn(uint8_t pin, uint8_t state)
{
printf ("pulseIn is not implemented for Raspberry Pi");
Expand Down Expand Up @@ -52,6 +54,7 @@ int wiringSetup ()
}
for (i=0; i<MAX_SPI_BUSES; i++) spi_buses[i]=-1;
for (i=0; i<MAX_SERIAL_BUSES; i++) serial_buses[i]=NULL;
i2c_init ();
}

pthread_mutex_t lockspi;
Expand Down

0 comments on commit c5eeeca

Please sign in to comment.