Skip to content
This repository was archived by the owner on Apr 28, 2021. It is now read-only.

Commit 87d400a

Browse files
committed
clean up multi I2C GPIO expander detection
Signed-off-by: Michel-FK <[email protected]>
1 parent 44035d4 commit 87d400a

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

driver_pcal6416a.c

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,26 @@
2626
#define DEBUG_PRINTF(...)
2727
#endif
2828

29+
/****************************************************************
30+
* Structures
31+
****************************************************************/
32+
33+
typedef struct {
34+
unsigned int address;
35+
char *name;
36+
} i2c_expander_t;
37+
2938
/****************************************************************
3039
* Static variables
3140
****************************************************************/
3241
static int fd_i2c_expander;
3342
static unsigned int i2c_expander_addr;
3443
static char i2c0_sysfs_filename[] = "/dev/i2c-0";
44+
static i2c_expander_t i2c_chip[] = {
45+
{PCAL9539A_I2C_ADDR, "PCAL9539A"},
46+
{PCAL6416A_I2C_ADDR, "PCAL6416A"},
47+
{0, NULL}
48+
};
3549

3650
/****************************************************************
3751
* Static functions
@@ -41,32 +55,25 @@ static char i2c0_sysfs_filename[] = "/dev/i2c-0";
4155
* Public functions
4256
****************************************************************/
4357
bool pcal6416a_init(void) {
58+
int i;
59+
4460
if ((fd_i2c_expander = open(i2c0_sysfs_filename,O_RDWR)) < 0) {
4561
printf("Failed to open the I2C bus %s", i2c0_sysfs_filename);
4662
// ERROR HANDLING; you can check errno to see what went wrong
4763
return false;
4864
}
65+
for (i = 0, i2c_expander_addr = 0; i2c_chip[i].address; i++) {
4966

50-
i2c_expander_addr = 0;
51-
52-
/// Probing PCAL9539A chip
53-
if (ioctl(fd_i2c_expander, I2C_SLAVE_FORCE, PCAL9539A_I2C_ADDR) < 0 ||
54-
pcal6416a_read_mask_interrupts() < 0) {
55-
printf("In %s - Failed to acquire bus access and/or talk to slave PCAL9539A_I2C_ADDR 0x%02X.\n",
56-
__func__, PCAL9539A_I2C_ADDR);
57-
58-
/// Probing PCAL6416A chip
59-
if (ioctl(fd_i2c_expander, I2C_SLAVE_FORCE, PCAL6416A_I2C_ADDR) < 0 ||
67+
/// Probing I2C GPIO expander chip
68+
if (ioctl(fd_i2c_expander, I2C_SLAVE_FORCE, i2c_chip[i]) < 0 ||
6069
pcal6416a_read_mask_interrupts() < 0) {
61-
printf("In %s - Failed to acquire bus access and/or talk to slave PCAL6416A_I2C_ADDR 0x%02X.\n",
62-
__func__, PCAL6416A_I2C_ADDR);
70+
printf("In %s - Failed to acquire bus access and/or talk to slave %s at address 0x%02X.\n",
71+
__func__, i2c_chip[i].name, i2c_chip[i].address);
6372
} else {
64-
DEBUG_PRINTF("Found I2C gpio expander chip: PCAL6416A\n");
65-
i2c_expander_addr = PCAL6416A_I2C_ADDR;
73+
DEBUG_PRINTF("Found I2C gpio expander chip %s at address 0x%02X\n",
74+
i2c_chip[i].name, i2c_chip[i].address);
75+
i2c_expander_addr = i2c_chip[i].address;
6676
}
67-
} else{
68-
DEBUG_PRINTF("Found I2C gpio expander chip: PCAL9539A\n");
69-
i2c_expander_addr = PCAL9539A_I2C_ADDR;
7077
}
7178

7279
/// GPIO expander chip found?

0 commit comments

Comments
 (0)