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

Feature Request: MCP23S17 control #36

Open
yz88 opened this issue Nov 27, 2017 · 7 comments
Open

Feature Request: MCP23S17 control #36

yz88 opened this issue Nov 27, 2017 · 7 comments

Comments

@yz88
Copy link

yz88 commented Nov 27, 2017

Hi,

first of all, really nice module, works great. Thank you for this.

Is it possible to controll a MCP23S17 via homebridge-gpio-wpi2?
WiringPi gpio command use an extension module for this: gpio -x mcp23s17:base:spi:devId …

@rsg98
Copy link
Owner

rsg98 commented Nov 29, 2017

I haven’t included that module in the back end rsg98/node-wiring-pi - but not sure that would help here, as this plugin also uses the SysFS GPIO interface to do some bits and pieces.

However, there does look to be device tree support for your expansion board:

https://github.com/raspberrypi/firmware/blob/master/boot/overlays/README

In theory, if you set that up, they should just get mapped as “normal” GPIO pins and therefore be controllable by this plugin without any further changes. Let us know how you get on!

@yz88
Copy link
Author

yz88 commented Dec 1, 2017

I used the DT overlay to map the port expander as a 'normal' GPIO pin. After that I was able to control the gpio496 via sysfs from bash shell.

Unfortunately I was not able o control the gpio496 from homebridge app correctly. Every push on the button in the homebridge app triggers an ON-state in the app for a few seconds, and after that the button falls back to OFF-state. But the GPIO pin state does not change.

Do you have any idea?

root@minibian:~# cat /proc/cpuinfo
processor       : 0
model name      : ARMv6-compatible processor rev 7 (v6l)
BogoMIPS        : 697.95
Features        : half thumb fastmult vfp edsp java tls
CPU implementer : 0x41
CPU architecture: 7
CPU variant     : 0x0
CPU part        : 0xb76
CPU revision    : 7

Hardware        : BCM2835
Revision        : 0002
Serial          : 00000000a150079c
root@minibian:~# cat /proc/device-tree/model
Raspberry Pi Model B Rev 1
root@minibian:~#
root@minibian:~# uname -a
Linux minibian 4.9.35+ #1014 Fri Jun 30 14:34:49 BST 2017 armv6l GNU/Linux


root@minibian:~# ls -la /boot/
total 21170
drwxr-xr-x  5 root root   16384 Jan  1  1970 .
drwxr-xr-x 23 root root    4096 Nov 26 14:04 ..
-rwxr-xr-x  1 root root   15660 Nov 26 14:27 bcm2708-rpi-0-w.dtb
-rwxr-xr-x  1 root root   15197 Nov 26 14:27 bcm2708-rpi-b.dtb
-rwxr-xr-x  1 root root   15456 Nov 26 14:27 bcm2708-rpi-b-plus.dtb
-rwxr-xr-x  1 root root   14916 Nov 26 14:27 bcm2708-rpi-cm.dtb
-rwxr-xr-x  1 root root   16523 Nov 26 14:27 bcm2709-rpi-2-b.dtb
-rwxr-xr-x  1 root root   17624 Nov 26 14:27 bcm2710-rpi-3-b.dtb
-rwxr-xr-x  1 root root   16380 Nov 26 14:27 bcm2710-rpi-cm3.dtb
-rwxr-xr-x  1 root root   50248 Nov 26 14:33 bootcode.bin
-rwxr-xr-x  1 root root     158 Nov 10  2015 cmdline.txt
-rwxr-xr-x  1 root root     121 Nov  2 00:01 config.txt
-rwxr-xr-x  1 root root   18693 Nov 26 14:27 COPYING.linux
-rwxr-xr-x  1 root root    2583 Nov 26 14:33 fixup_cd.dat
-rwxr-xr-x  1 root root    6674 Nov 26 14:33 fixup.dat
-rwxr-xr-x  1 root root    9813 Nov 26 14:33 fixup_db.dat
-rwxr-xr-x  1 root root    9813 Nov 26 14:33 fixup_x.dat
-rwxr-xr-x  1 root root 4579248 Nov 26 14:27 kernel7.img
-rwxr-xr-x  1 root root 4379032 Nov 26 14:27 kernel.img
-rwxr-xr-x  1 root root    1494 Nov 26 14:33 LICENCE.broadcom
drwxr-xr-x  2 root root   10240 Nov 26 14:33 overlays
-rwxr-xr-x  1 root root  659492 Nov 26 14:33 start_cd.elf
-rwxr-xr-x  1 root root 4993604 Nov 26 14:33 start_db.elf
-rwxr-xr-x  1 root root 2855556 Nov 26 14:33 start.elf
-rwxr-xr-x  1 root root 3939492 Nov 26 14:33 start_x.elf
drwxr-xr-x  2 root root    2048 Aug 12  2016 System Volume Information
drwxr-xr-x  4 root root    2048 Mar 11  2016 .Trash-999
root@minibian:~# ls -la /boot/overlays/ | grep mcp
-rwxr-xr-x 1 root root  1329 Nov 26 14:30 mcp23017.dtbo
-rwxr-xr-x 1 root root 15229 Nov 26 14:30 mcp23s17.dtbo
-rwxr-xr-x 1 root root  1833 Nov 26 14:30 mcp2515-can0.dtbo
-rwxr-xr-x 1 root root  1833 Nov 26 14:30 mcp2515-can1.dtbo
-rwxr-xr-x 1 root root  4237 Nov 26 14:30 mcp3008.dtbo
root@minibian:~# more /boot/config.txt
gpu_mem=16
arm_freq=800
core_freq=250
sdram_freq=400
over_voltage=0
dtparam=spi=on
dtparam=i2c_arm=on
#dtoverlay=w1-gpio
root@minibian:~#
root@minibian:~#
root@minibian:~# ls -la /sys/class/gpio/
total 0
drwxr-xr-x  2 root root    0 Dec  1 20:54 .
drwxr-xr-x 47 root root    0 Jan  1  1970 ..
--w-------  1 root root 4096 Nov 26 21:04 export
lrwxrwxrwx  1 root root    0 Nov 26 21:04 gpiochip0 -> ../../devices/platform/soc/20200000.gpio/gpio/gpiochip0
--w-------  1 root root 4096 Nov 26 21:04 unexport
root@minibian:~#
root@minibian:~# vi /boot/config.txt
root@minibian:~# more /boot/config.txt
gpu_mem=16
arm_freq=800
core_freq=250
sdram_freq=400
over_voltage=0
dtparam=spi=on
dtparam=i2c_arm=on
#dtoverlay=w1-gpio
dtoverlay=mcp23s17,s17-spi0-0-present=1
root@minibian:~#
root@minibian:~#
root@minibian:~#
root@minibian:~#
root@minibian:~# reboot

root@minibian:~#
root@minibian:~# ls -la /sys/class/gpio/
total 0
drwxr-xr-x  2 root root    0 Dec  1 21:39 .
drwxr-xr-x 47 root root    0 Jan  1  1970 ..
--w-------  1 root root 4096 Dec  1 21:38 export
lrwxrwxrwx  1 root root    0 Dec  1 21:38 gpiochip0 -> ../../devices/platform/soc/20200000.gpio/gpio/gpiochip0
lrwxrwxrwx  1 root root    0 Dec  1 21:39 gpiochip496 -> ../../devices/platform/soc/20204000.spi/spi_master/spi0/spi0.0/gpio/gpiochip496
--w-------  1 root root 4096 Dec  1 21:38 unexport
root@minibian:~#
root@minibian:~#
root@minibian:~#
root@minibian:~#
root@minibian:~# echo 496 > /sys/class/gpio/export
root@minibian:~# echo out > /sys/class/gpio/gpio496/direction
root@minibian:~# echo 1 > /sys/class/gpio/gpio496/value
root@minibian:~# echo 0 > /sys/class/gpio/gpio496/value
root@minibian:~#
root@minibian:~# echo 497 > /sys/class/gpio/export
root@minibian:~# echo out > /sys/class/gpio/gpio497/direction
root@minibian:~# echo 0 > /sys/class/gpio/gpio497/value
root@minibian:~# echo 1 > /sys/class/gpio/gpio497/value
root@minibian:~#
root@minibian:~# ls -la /sys/class/gpio/
total 0
drwxr-xr-x  2 root root    0 Dec  1 21:40 .
drwxr-xr-x 47 root root    0 Dec  1 21:39 ..
--w-------  1 root root 4096 Dec  1 21:40 export
lrwxrwxrwx  1 root root    0 Dec  1 21:40 gpio496 -> ../../devices/platform/soc/20204000.spi/spi_master/spi0/spi0.0/gpiochip1/gpio/gpio496
lrwxrwxrwx  1 root root    0 Dec  1 21:40 gpio497 -> ../../devices/platform/soc/20204000.spi/spi_master/spi0/spi0.0/gpiochip1/gpio/gpio497
lrwxrwxrwx  1 root root    0 Dec  1 21:39 gpiochip0 -> ../../devices/platform/soc/20200000.gpio/gpio/gpiochip0
lrwxrwxrwx  1 root root    0 Dec  1 21:39 gpiochip496 -> ../../devices/platform/soc/20204000.spi/spi_master/spi0/spi0.0/gpio/gpiochip496
--w-------  1 root root 4096 Dec  1 21:39 unexport
root@minibian:~#
root@minibian:~# more .homebridge/config.json
{
    "bridge": {
        "name": "Minibian",
        "username": "00:13:EF:90:0C:0A",
        "port": 51826,
        "pin": "031-45-155"
    },
    "platforms": [{
          "platform" : "WiringPiPlatform",
          "name" : "Pi GPIO (WiringPi)",
          "overrideCache" : "true",
          "autoExport" : "true",
          "gpiopins" : [{
                "name" : "GPIO0_BCM17",
                "pin"  : 17,
                "enabled" : "true",
                "mode" : "out",
                "pull" : "down",
                "inverted" : "false",
                "duration" : 3000,
                "polling" : "true"
            },{
                "name" : "GPIO2_BCM21",
                "pin"  : 21,
                "enabled" : "true",
                "mode" : "out",
                "pull" : "down",
                "inverted" : "false",
                "duration" : 0,
                "polling" : "true"
            },{
                "name" : "GPIO496",
                "pin"  : 496,
                "enabled" : "true",
                "mode" : "out",
                "pull" : "down",
                "inverted" : "false",
                "duration" : 0,
                "polling" : "true"
          }]
    }]
}

root@minibian:~#
root@minibian:~# homebridge
*** WARNING *** The program 'node' uses the Apple Bonjour compatibility layer of Avahi.
*** WARNING *** Please fix your application to use the native API of Avahi!
*** WARNING *** For more information see <http://0pointer.de/avahi-compat?s=libdns_sd&e=node>
*** WARNING *** The program 'node' called 'DNSServiceRegister()' which is not supported (or only supported partially) in the Apple Bonjour compatibility layer of Avahi.
*** WARNING *** Please fix your application to use the native API of Avahi!
*** WARNING *** For more information see <http://0pointer.de/avahi-compat?s=libdns_sd&e=node&f=DNSServiceRegister>
[12/1/2017, 9:45:32 PM] Loaded plugin: homebridge-gpio-wpi2
[12/1/2017, 9:45:33 PM] Registering platform 'homebridge-gpio-wpi2.WiringPiPlatform'
[12/1/2017, 9:45:33 PM] ---
[12/1/2017, 9:45:33 PM] Loaded config.json with 0 accessories and 1 platforms.
[12/1/2017, 9:45:33 PM] ---
[12/1/2017, 9:45:33 PM] Loading 1 platforms...
[12/1/2017, 9:45:33 PM] [Pi GPIO (WiringPi)] Initializing WiringPiPlatform platform...
[12/1/2017, 9:45:33 PM] [Pi GPIO (WiringPi)] WORK IN PROGRESS... Report issues on https://github.com/rsg98/homebridge-gpio-wpi2
[12/1/2017, 9:45:33 PM] [Pi GPIO (WiringPi)] Exporting GPIO Pins from config file...
[12/1/2017, 9:45:33 PM] [Pi GPIO (WiringPi)] Exporting and configuring: GPIO0_BCM17
[12/1/2017, 9:45:33 PM] [Pi GPIO (WiringPi)] Exporting and configuring: GPIO2_BCM21
[12/1/2017, 9:45:33 PM] [Pi GPIO (WiringPi)] Exporting and configuring: GPIO496
[12/1/2017, 9:45:33 PM] [Pi GPIO (WiringPi)] homebridge API version: 2.2
Setup Payload:
X-HM://0023ISYWZM4GD
Scan this code with your HomeKit app on your iOS device to pair with Homebridge:

Or enter this code with your HomeKit app on your iOS device to pair with Homebridge:

    ┌────────────┐
    │ 031-45-155 │
    └────────────┘

[12/1/2017, 9:45:34 PM] [Pi GPIO (WiringPi)] Loading cached GPIO pins complete
[12/1/2017, 9:45:34 PM] [Pi GPIO (WiringPi)] New GPIO from config.json: GPIO0_BCM17 (17)
[12/1/2017, 9:45:34 PM] [Pi GPIO (WiringPi)] GPIO0_BCM17 Configure GPIO Pin bfea6d23-7b8d-4270-971f-471356163801
[12/1/2017, 9:45:34 PM] [Pi GPIO (WiringPi)] Turning off pin #17
[12/1/2017, 9:45:34 PM] [Pi GPIO (WiringPi)] New GPIO from config.json: GPIO2_BCM21 (21)
[12/1/2017, 9:45:34 PM] [Pi GPIO (WiringPi)] GPIO2_BCM21 Configure GPIO Pin 90e6ac77-8139-4962-a37b-2dc4fed3443a
[12/1/2017, 9:45:34 PM] [Pi GPIO (WiringPi)] Turning off pin #21
[12/1/2017, 9:45:34 PM] [Pi GPIO (WiringPi)] New GPIO from config.json: GPIO496 (496)
[12/1/2017, 9:45:34 PM] [Pi GPIO (WiringPi)] GPIO496 Configure GPIO Pin eece8690-fbcb-4247-841c-a3faacd6f07c
[12/1/2017, 9:45:34 PM] [Pi GPIO (WiringPi)] Turning off pin #496
[12/1/2017, 9:45:34 PM] Homebridge is running on port 51826.

[12/1/2017, 9:48:44 PM] [Pi GPIO (WiringPi)] GPIO0_BCM17 Identify!!!
[12/1/2017, 9:48:50 PM] [Pi GPIO (WiringPi)] GPIO2_BCM21 Identify!!!
[12/1/2017, 9:48:53 PM] [Pi GPIO (WiringPi)] GPIO496 Identify!!!

[12/1/2017, 9:49:24 PM] [Pi GPIO (WiringPi)] Turning on pin #17
[12/1/2017, 9:49:27 PM] [Pi GPIO (WiringPi)] Timer expired 3000ms
[12/1/2017, 9:49:27 PM] [Pi GPIO (WiringPi)] Turning off pin #17
[12/1/2017, 9:49:38 PM] [Pi GPIO (WiringPi)] Turning on pin #21
[12/1/2017, 9:49:44 PM] [Pi GPIO (WiringPi)] Turning off pin #21
[12/1/2017, 9:49:45 PM] [Pi GPIO (WiringPi)] Turning on pin #17
[12/1/2017, 9:49:48 PM] [Pi GPIO (WiringPi)] Timer expired 3000ms
[12/1/2017, 9:49:48 PM] [Pi GPIO (WiringPi)] Turning off pin #17
[12/1/2017, 9:49:52 PM] [Pi GPIO (WiringPi)] Turning on pin #496
[12/1/2017, 9:49:59 PM] [Pi GPIO (WiringPi)] Turning on pin #496
[12/1/2017, 9:50:02 PM] [Pi GPIO (WiringPi)] Turning on pin #496
[12/1/2017, 9:50:03 PM] [Pi GPIO (WiringPi)] Turning on pin #496
[12/1/2017, 9:50:04 PM] [Pi GPIO (WiringPi)] Turning on pin #496
[12/1/2017, 9:50:06 PM] [Pi GPIO (WiringPi)] Turning on pin #496
[12/1/2017, 9:50:09 PM] [Pi GPIO (WiringPi)] Turning on pin #496
[12/1/2017, 9:50:12 PM] [Pi GPIO (WiringPi)] Turning on pin #496
[12/1/2017, 9:50:13 PM] [Pi GPIO (WiringPi)] Turning on pin #496
[12/1/2017, 9:50:13 PM] [Pi GPIO (WiringPi)] Turning on pin #496
```

@rsg98
Copy link
Owner

rsg98 commented Dec 4, 2017

Hmmm... didn't realise it would get allocated such a high range of pin numbers. I think (although I need to do more checks) that WiringPi has a built in limit to those it considers "built in" rather than extensions - which means we may need to add support for that module to the backend node-wiring-pi and figure out a way of addressing properly... will have a further dig around the WiringPi code.

@yz88
Copy link
Author

yz88 commented Dec 9, 2017

I did some basic test, and its possible to get the pin up and down with javascript fs.writeFile()

var fs = require("fs");
var path = require("path");

var sysFsPath = "/sys/class/gpio";
var pinNumber = 496;
var direction = "out";
var value = "0";

fs.writeFile(sysFsPath + "/export", pinNumber, "utf8")
fs.writeFile(sysFsPath + "/gpio" + pinNumber + "/direction", direction, "utf8")
fs.writeFile(sysFsPath + "/gpio" + pinNumber + "/value", value, "utf8")

@yz88
Copy link
Author

yz88 commented Dec 9, 2017

I was not able to control gpio 496 via wiringpi gpio utility

root@minibian:~/INTE-node# gpio -v
gpio version: 2.44
Copyright (c) 2012-2017 Gordon Henderson
This is free software with ABSOLUTELY NO WARRANTY.
For details type: gpio -warranty

Raspberry Pi Details:
  Type: Model B, Revision: 00, Memory: 256MB, Maker: Egoman
  * Device tree is enabled.
  *--> Raspberry Pi Model B Rev 1
  * This Raspberry Pi supports user-level GPIO access.
root@minibian:~/INTE-node#
root@minibian:~/INTE-node# cat /sys/class/gpio/gpio496/direction
out
root@minibian:~/INTE-node# cat /sys/class/gpio/gpio496/value
1
root@minibian:~/INTE-node# gpio -g write 496 0
root@minibian:~/INTE-node# cat /sys/class/gpio/gpio496/value
1
root@minibian:~/INTE-node#

@rsg98
Copy link
Owner

rsg98 commented Dec 10, 2017

Thanks for the tests. Can you check whether you can still control it via the gpio -x mcp23s17:base:spi:devId interface when you've got device tree configured?

I can look at adding the module support back into node-wiring-pi and then figuring out an option for getting this module to control it - but it would (currently) require both device tree and the WiringPi extension.

@yz88
Copy link
Author

yz88 commented Dec 11, 2017

with the device tree configured, I'm not able to control the port expander via gpio -x mcp23s17:base:spi:devId

root@minibian:~# more /boot/config.txt
gpu_mem=16
arm_freq=800
core_freq=250
sdram_freq=400
over_voltage=0
dtparam=spi=on
dtparam=i2c_arm=on
#dtoverlay=w1-gpio
dtoverlay=mcp23s17,s17-spi0-0-present=1
root@minibian:~# lsmod | grep mcp
gpio_mcp23s08          11742  0
root@minibian:~#
root@minibian:~#  gpio -x mcp23s17:65:0:0x00 mode 65 out
Unable to open SPI device: No such file or directory
root@minibian:~#

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