Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot handle multiple I2C devices on RPi 4B #402

Closed
poirierlouis opened this issue Nov 4, 2020 · 2 comments
Closed

Cannot handle multiple I2C devices on RPi 4B #402

poirierlouis opened this issue Nov 4, 2020 · 2 comments

Comments

@poirierlouis
Copy link

poirierlouis commented Nov 4, 2020

Context
Raspberry Pi : v4 Model B
pigpio C library : v78
Use multiple MCP23017 devices with I2C protocol.

Problem
When initializing multiple devices, first device with handle 0 (returned by i2cOpen) can be used without problem. I can write with i2cWriteByteData.
But using another device with handle 1, I can't write anymore as the library returns a PI_BAD_HANDLE error code when using i2cWriteByteData. While the call to i2cOpen for second device, returned a valid handle 1.

Example

#include "pigpio.h"

#define MCP23017_ADDRESS 0x20
#define MCP23017_IODIRA 0x00
#define MCP23017_IODIRB 0x01

#define I2C_BUS 1
#define I2C_FLAGS 0

int mcp[2];

if (gpioInitialise() == PI_INIT_FAILED) { return; }

mcp[0] = i2cOpen(I2C_BUS, MCP23017_ADDRESS | 0b000, I2C_FLAGS);
i2cWriteByteData(mcp[0], MCP23017_IODIRA, 0xFF);
i2cWriteByteData(mcp[0], MCP23017_IODIRB, 0xFF);

mcp[1] = i2cOpen(I2C_BUS, MCP23017_ADDRESS | 0b001, I2C_FLAGS);
i2cWriteByteData(mcp[1], MCP23017_IODIRA, 0xFF); // return PI_BAD_HANDLE
i2cWriteByteData(mcp[1], MCP23017_IODIRB, 0xFF);

Solution
After analyzing the code in pigpio.c. There is a mistake in the i2cOpen function :

pigpio/pigpio.c

Line 4119 in cc17196

i2cInfo[i].state = PI_I2C_OPENED;
.

You can fix it by replacing i with slot. I guess i is no longer in sync with slot after the loop. After changing it on local, I no longer have this issue.

@guymcswain
Copy link
Collaborator

Thanks for finding this. It'll be in the next release before the year end and in the develop branch sooner.

@guymcswain
Copy link
Collaborator

Fixed in v79

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants