Hello,
ITS WORKING!
There was a special parameter that it was mandatory in the "make kernel_menuconfig". Something like "include /sys/class" - now it's obvious :-)
There is some unstability in the I2C bus, that I'll still need to troubleshoot (frequently the PCF address cames wrong), but that will be the easy part.
Now, in order to resume all the help I got from MBS and Karl (the GPIO patch developer), I'll post here a list of actions to get this working, so all the community could use it as a reference:
1 - Prepare the HW - Mainly based on http://code.google.com/p/fonera-i2c/wiki/FoneraHacks
1.1 - Identify GPIO lines
http://www.est.ipcb.pt/pessoais/alexand … inouts.jpg
1.2 - "Remove caps" - actualy means to take out the capacitors of each GPIO line. Not so obvious for the non-native speakers - took me a while to understand that.
1.3 - Build the circuit with the PCF8475. I followed MBS's suggestion to use a level-shifter with a mosfet (like 2N7000). Page 10 of http://ics.nxp.com/support/documents/in … n97055.pdf shows how to do it.
2. Preparing the SW
2.1 - Create the openwrt build environment
http://wiki.openwrt.org/doc/howto/buildroot.exigence
2.2 - Build the images
http://wiki.openwrt.org/doc/howto/build
Special tips:
GPIO Patch is needed. Download it from http://patchwork.openwrt.org/patch/1452/ and apply with "patch -p1 < /tmp/patchfile.diff" command.
When running "make menuconfig" search for the I2c and GPIO options (within the Kernel modules section) select them with asterisk " * ", because with "M" will only build a package to be installed after.
Run "make kernel_menuconfig" and make sure the GPIO options are selected (drivers->gpio) and the "/sys/class" - very important!
Compiling will take a couple of hours
3 - Flash the fonera with the images
http://wiki.openwrt.org/toh/fon/fonera
4 - Configure I2C/GPIO stuff
4.1 - I2C part
#echo i2c-gpio-custom bus0=0,4,3 > /etc/modules.d/58-i2c-gpio-custom
(this example configures gpio pin 3 as SCLK and pin 4 as SDA, but anything else can be choosed according to the available pins)
Reboot and probe I2C bus:
root@OpenWrt:/sys/devices/platform/i2c-gpio.0/i2c-0/0-003f/gpio/gpio251# cd
root@OpenWrt:~# i2cdetect -l
i2c-0 i2c i2c-gpio0 I2C adapter
(we have a I2C bus - Let's see what is inside)
root@OpenWrt:~# i2cdetect -y 0
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 3f
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
root@OpenWrt:~#
(when putting all the address pins at "1" in the PCF8574, its address becomes 3F. That's correct!)
At this stage it should be possible to set the pins of the PCF chip with the I2Cset command (didn't try if its possible to read - i2cget - but it should work fine as well).
However, the value to send to it is from 0 to 255, which will allways impact all the bits of the chip. If you need to change bit by bit, continue reading :-)
4.2. - I2C as GPIO
Given the address of the PCF chip (in my case 3f), issue the following command:
#echo pcf8574 0x3F > /sys/bus/i2c/devices/i2c-0/new_device
This will create a directory called "/sys/class/gpio/gpiochip0".
Now, check dmesg and search for something similar to "pcf857x 0-003f: gpios 248..255 on a pcf8574"
The 248...255 is the important part! Immagine that somehow the system attributed a "virtual independent GPIO" for each bit of the PCF8574, and their addresses are in the 248...255 range. But how can we use it? Simple. For each "virtual independent GPIO" issue the command like "echo 248 > /sys/class/gpio/export". This is the example for port 248. You need to repeat the command for each other GPIO 248, 249, 250... until 255
By issuing these commands, one directory will be created for each pin. Each directory will have a couple of files that, when changed, will change the status of the pins.
root@OpenWrt:/sys/class/gpio# ls /sys/class/gpio/
export gpio248 gpio249 gpio250 gpio251 gpiochip0 gpiochip248 unexport
root@OpenWrt:/sys/class/gpio# ls /sys/class/gpio/gpio248/
active_low device direction edge subsystem uevent value
root@OpenWrt:/sys/class/gpio#
From this point onwards it should be easy. I'll give a quick example:
Check the direction of the pins (input, output):
#cat direction
Change the direction of the pins:
#echo out > direction
or
#echo in > direction
If set to "out", you can change the status of the pin with:
#echo 1 > vaule
or
#echo 0 > vaule
If set to "in", you can read the status of the pin with:
#cat value
And that's it. With these steps you should be have it working in much less time than I did :-)
BTW, keep in mind that the chapter 4.2 needs to be re-done in every reboot of the fonera, so it should be a good idea to have implemented it in scripts.
Thanks MBS and Karl (the GPIO patch developer) for all of your tremendous support and extreme patience! ;-)
Jabss
Now the next step will be in adding 1-Wire interface to the fonera! :-D
I'll use the fonera to read the temperature via a DS18B20 (1-wire interface) and to turn on/off heaters and fans with the PCF8574 (I2C) through a power circuit.
(Last edited by jabss on 13 Nov 2011, 02:04)