Controlling with gnublin-gpio

At first its necessary to load the driver as shown here if you want to control the Port-Expander with the progragram gnublin-gpio:

root@gnublin:~# modprobe pca953x cnt=<Int> addr1=<I2C-Adresse>

cnt Number of connected Port-Expanders (default=1)

addr1 Address of the first Port-Expander (Hex)

addr2 Address of the second Port-Expander (Hex)

addr3, addr4 , and addr5 like addr1

If the Port-Expander is for example on the Address 0x20 the command looks like this:

root@gnublin:~# modprobe pca953x addr1=0x20

If there are two Port-Expander one one the address 0x20 and the other on 0x24 the command is this:

root@gnublin:~# modprobe pca953x cnt=2 addr1=0x20 addr2=0x24

The address depends how many Address-Jumper are placed.

i2cdetect -y 1 returns the addresses of all connected and recognised I2C-Devices. The PCA9555 Port-Expander is located between 0x20 and 0x27.

GPIO Pin as Output

To set the value of an GPIO Pin use this:

root@gnublin:~# gnublin-gpio -o 1 -p 98

This command set IO0_0 as output with the value HIGH.

To set the Value back on low type:

root@gnublin:~# gnublin-gpio -o 0 -p 98

GPIO Pin as Input

To get the actual value type:

root@gnublin:~# gnublin-gpio -i -p <pin>

The value <pin> is used for the GPIO Pin. In the following section you can find the values for <pin>.

IO Pins of the Port-Expander and the Value of GPIO-sysfs

Here you can see the table of the IO-Pins of the PCA9555 and the corresponding number in the gpio-sysfs. The control works like seen in the example of gpio98.

PCA9555 Pin

Wert für /sys/class/gpio/export

IO 0_0

98

IO 0_1

99

IO 0_2

100

IO 0_3

101

IO 0_4

102

IO 0_5

103

IO 0_6

104

IO 0_7

105

IO 1_0

106

IO 1_1

107

IO 1_2

108

IO 1_3

109

IO 1_4

110

IO 1_5

111

IO 1_6

112

IO 1_7

113

For Advanced Users

Preparation

First of all you should solder the needed pins (SCL,SDA,A0,A1,A1,VCC,GND and the used GPIO-Pins) either on the pins of the Gublin-board or on some wires to use it with a breakboard or something like that.

If you wire it up after soldering, you should know that you must connect SDA and SCL with pullup-resistors (4.7kOhm) to VCC. (I2C specification)

Here you can see how to connect the I2C-BUS:

Here you can see the pin assignment of the PCA9555 port expander:

You can configure the I2C-Address of the device, via the pins A0, A1 and A2. Depending on the level of the pins the address changes. (datasheet) If you connect all three pins to GND, the address of the PCA9555 is 0x20 for the reed access and 0x21 for the write access.

Access over GPIO-sysfs

At first the driver module have to be loaded.

root@gnublin:~# modprobe pca953x

Export Pins

To use for example the pin IO 0_0 of the PCA9555 type this command:

root@gnublin:~# echo 98 > /sys/class/gpio/export

This register the Pin IO 0_0 of the PCA9555 in the gpio-sysfs.

Using Pin as Output and changing the value

To configure the Pin as output:

root@gnublin:~# echo out > /sys/class/gpio/gpio98/direction

To set the output to the HIGH-level:

root@gnublin:~# echo 1 > /sys/class/gpio/gpio98/value

To set the level to low again:

root@gnublin:~# echo 0 > /sys/class/gpio/gpio98/value

Using Pin as Input and read the value

To set the Pin as Input:

root@gnublin:~# echo in > /sys/class/gpio/gpio98/direction

Read the value of the Input:

root@gnublin:~# cat /sys/class/gpio/gpio98/value

In this table is shown which IO correspondents with which number of the GPIO-sysfs.

I2CINTERFACE = 1 (because we use the device i2c-1)
SLAVEADDRESS = 0x20 (if A0,A1 and A2 are connected to GND)
REGISTERADDRESS = specific, depending on which Pins you want to control.
DATEN = specific, depending on which Pins you want to control.

The most important registeraddresses of the Port-Expander are 0x02,0x03,0x06 und 0x07.

Register knowledge

The register with the address 0x06 controls the direction (input/output) of all Port-0 pins.

Bit

7

6

5

4

3

2

1

0

Symbol

C0.7

C0.6

C0.5

C0.4

C0.3

C0.2

C0.1

C0.0

Default

1

1

1

1

1

1

1

1

The register with the address 0x07 controls the direction (input/output) of all Port-1 pins.

Bit

7

6

5

4

3

2

1

0

Symbol

C1.7

C1.6

C1.5

C1.4

C1.3

C1.2

C1.1

C1.0

Default

1

1

1

1

1

1

1

1

If you want to set all the pins of Port-0 as outputs, for example, you just write the value 0x00 at the registeraddress 0x06. Zero means output and one means input.

The register with the address 0x02 controls the level of all port-0 pins.

Bit

7

6

5

4

3

2

1

0

Symbol

O0.7

O0.6

O0.5

O0.4

O0.3

O0.2

O0.1

O0.0

Default

1

1

1

1

1

1

1

1

The register with the address 0x03 controls the level of all port-1 pins.

Bit

7

6

5

4

3

2

1

0

Symbol

O1.7

O1.6

O1.5

O1.4

O1.3

O1.2

O1.1

O1.0

Default

1

1

1

1

1

1

1

1

If you want to set the level of pin 0 from port-1 to high, for example, you just have to write the value 0x01 at the registeraddress 0x03. Zero means low and one means high.

Example commands

To set all the pins of port-1 as an output we use the following command:

root@gnublin:~# i2cset 1 0x20 0x06 0x00

After this command you can set the level of pin-0 from port-1 to high, with the following command:

root@gnublin:~# i2cset -y 1 0x20 0x02 0x01

If you want to set all pins from port-1 to low again, you can use the following command: