How to add a 40xGPIO PCA9698?

611 views
Skip to first unread message

Andre

unread,
Mar 19, 2014, 4:45:37 PM3/19/14
to web...@googlegroups.com
Hello,

since several hours I am trying to add a new 40xGPIO device PCA9698.

I knew the code for the communication with this device, but I have a problem to understand adding a new device to webiopi.

Where I have to add the python script for the new device?

Many thanks in advance for any help!

I am using webiopi 0.7.

Andre

unread,
Mar 19, 2014, 6:55:28 PM3/19/14
to web...@googlegroups.com
Okay, I have done the next step. I hope the right one :-)

I changed the config python file "pcf8574.py" in the folder
"/usr/local/lib/python2.7/dist-packages/WebIOPi-0.7.0-py2.7-linux-armv6l.egg/webiopi/devices/digital".

I set the device adress to default 0x42.

But what about the next step? Can anyone help me please?

Here are some more information about the device. It is a very cheap ready to use evaluation board with 40 GPIOs.

Datasheet can be found here:

Andreas Riegg

unread,
Mar 20, 2014, 12:27:51 PM3/20/14
to web...@googlegroups.com
Hi,

short answer, this won't work! The PCF8574 has a very simple (almost trivial) I2C protocol and no configuration registers, no special commends etc.

The device you mention has a complexity comparable to the MCP23xxx series expanders with specific command registers, output direction registers, banks etc. This is much more work, just compare the amount of driver code needed for the PCF8574 with that for the MCP23xxx chips.

So, to get a driver for it, forget about PCF8574. You have to implement this in the way like the MCP23xxx drivers are coded. You may be able to implement your driver as a subclass of one of the MCPxxx classes, but as your device is from another vendor there is a high chance, that the commands to configure and read/write the MCP23xxx devices do not work for the PCA one.

So, you will have to create a new PCA9698.py in the digital subfolder, take the MCP23xxx.py as a kind of learning template and implement all methods needed to be overloaded (those with the __ at the beginning / those that produce a NotImplementedError in class GPIOPort). And you have to add the new driver file with a correct entry in __init__.py of the digital subfolder. Also, don't change the code in the directory you mention above, do it in the original
source directory that was created when untaring the downloaded installation file and call ./setup.sh to install and test your work.

This all can be done if you are experienced enough with Python and I2C drivers protocol, but this is nothing for a beginner. And, to be honest, neither me (or Eric) will have currently the spare time to explain you more details about driver development
or debug your code if something goes wrong or does not work repeatedly.

Alternatively you can ask
Eric if he would write a driver for the device if you make a donation to him. Or just use an expansion card that uses chips that are suppoprted already ....

Andreas

Andre

unread,
Mar 22, 2014, 9:15:15 AM3/22/14
to web...@googlegroups.com
Hi Andreas,

thank You for your response and your explanation. I agree with You that writing a driver is nothing for a beginner. But I am ambitiously :-)
In a previous project I have successfully connected the PCA9698 with an ARM7 evaluation board.

I tested the connection with "i2cdetect" and here are the results:
The adress of my PCA9698-board is 0x42, ironically I have to set the adress to the half value (0x21)

Declare pins as output
sudo i2cset -y 1 0x21 0x98 0 0 0 0 0 i

0x98 is the command-register to declare all the five 8-bit ports to type outputs (value "0")


Set the outputs
sudo i2cset -y 1 0x21 0x88 0 0 0 0 0 i

0x88 is the value-register. Here we set all the five 8-bit ports to the value "0". Possible is 0-255 or rather 0x00-0xff.


Read the state of the outputs
sudo i2cget -y 1 0x21 1

Only access a bank of a 8-bit output, here it is bank 1


But even with this knowledge I am not able to write/change a driver similar to the PCF8574 or the MCP23XXX.
Do You have the next idea or is the target still far away.
I also ask Eric if he is so friendly to write a driver. Of course I give him a donation.

Also I found a driver for the PCA9698. But it is written in C. Maybe the code will help You or Eric.

Thanks in advance

Andre

Andreas Riegg

unread,
Mar 22, 2014, 11:50:36 AM3/22/14
to web...@googlegroups.com
Hi,

the slave address in WebIOPi is like with the most drivers like the i2c.... commands you use above the 7 bit address, the LSB ist the R/W bit. So 0x21 is correct and thats also the one you have to use within the WebIOPi driver.

You are on the right way using the registers, but as I mentioned, take the MCP23xxx.py as example and adapt it to the PCA. Mainly you have to look for the right registers and bank addressing modes and commands.

What you can do:

Take the Class MCP23XXX, use the spec of the chip and try to understand how the methods of MCP23XXX work. Then transfer this knowledge to the PCA and its registers/commands. The base code to do I2C calls exists already in the I2C Python class, see class MCP23XX what it inherits. One tip: If you don't understand the code of MCP23XXX and MCP23XX and GPIOPort methods starting with __xxxx within some hours, forget about implementing a driver for the PCA! If you understand the (bit plumbing) code of the three classes within one hour or two you should be able to implement a driver with one or two days work including testing.

Andreas
Reply all
Reply to author
Forward
0 new messages