Support added for MCP23008 and MCP23017

453 views
Skip to first unread message

Robert Savage

unread,
Dec 3, 2012, 8:33:21 AM12/3/12
to pi...@googlegroups.com
Support has been added to Pi4J for MCP23008 and MCP23017 as GpioProviders in the 0.0.4-SNAPSHOT development version.  
These chips can be connected via I2C to the RaspberryPi to provide additional GPIO capacity (8/16 additional GPIO pins) 

Source Code:

Alex Konshin

unread,
Aug 23, 2013, 7:55:36 PM8/23/13
to pi...@googlegroups.com
Hi Robert,

I am playing with MCP23017 (IO Pi 32 Channel Port Expander) and your Pi4J library for a couple of days.
I have some feedback.

First of all, I don't like MCP23017GpioProvider constructor. Basically there is no way to specify initial values of registers. There is no way skip this initialization neither.
In my case this is really annoying. I connected MCP23017 to SainSmart 16-Channel 12V Relay Module that turns on relays on low-level input. As result during initialization of MCP23017GpioProvider all relays are switched on then immediately switched off when provisionDigitalOutputPin sets the default value.

My idea is to add a capability to describe pins *before* initializing controllers. It can be done by introducing following classes:
public abstract class AbstractPinDefinition {
  final Pin pin;
  final String name;
  protected AbstractPinDefinition ( Pin pin, String name ) { ... }
}
public class OutputPinDefinition extends AbstractPinDefinition {
  final PinState initialState; // can be null
  public OutputPinDefinition ( Pin pin, String name, PinState initialState ) { ... }
}
public class InputPinDefinition extends AbstractPinDefinition {
  final PinPullResistance pullResistance;
  public OutputPinDefinition ( Pin pin, String name, PinPullResistance pullResistance ) { ... }
}
etc...

Then add constructor:
public MCP23017GpioProvider(int busNumber, int address, GpioController gpio, AbstractPinDefinition... pins) throws IOException
This constructor should process and checks all definitions, set initial values then write them to registers. It also should register these pins (provisionPin) but without changing their modes and states.

I am an experienced Java developer and I can do it by myself. But I think that this improvement of interface makes sense for anybody who will use your library with MCP23017 and probably other port expanders.

Alex


Alex Konshin

unread,
Aug 24, 2013, 1:33:42 PM8/24/13
to pi...@googlegroups.com
I reviewed Pi4J source code and I found many problematic places with race condition.
For example, access to all fields current* in MCP23017GpioProvider should be synchronized because GpioStateMonitor is a separate thread and it updates them asynchronously.
Access to fields of GpioProviderPinCache should be synchronized too for the same reason.
Calls Device.read and Device.write must be synchronized too.
 
Reply all
Reply to author
Forward
0 new messages