ahmedawad
Posts: 42
Joined: Mon Apr 18, 2016 11:44 am
Location: Bremen, DE

using mcp23s17 device tree

Thu Aug 17, 2017 12:02 pm

Hi,

I want to use the mcp23s17 kernel driver so I connected mcp23s17 (port expander chip ) to RPI3 SPI-1 and loaded the mcp23s17.dtbo in config.txt.

Here is what config.txt looks like:

Code: Select all

dtoverlay=spi1-3cs
dtoverlay=mcp23s17,s17-spi1-1-preset=1,s17-spi1-1-int-gpio=12  # This means that the address of the port expander is 0 and I use gpio 12 as the interrupt pin
When I reboot and check:

Code: Select all

pi@raspberrypi:~ $ cat /sys/class/gpio/gpiochip496/label
mcp23s17

pi@raspberrypi:~ $ cat /proc/interrupts

178:         16          0          0          0  pinctrl-bcm2835  12 Level     spi1.1

It looks like, the pi loads the dt layer and set the interrupt. From here, I do not know how to proceed ? How can I check that pi recognize the port expander ? , I want to use the port expander by using this dtoverlay=mcp23s17, so what sort of commands to use to communicate with this mcp23s17 interface ?

Thanks in advance

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5812
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: using mcp23s17 device tree

Thu Aug 17, 2017 12:27 pm

There is a typo in your dtoverlay line:

Code: Select all

dtoverlay=mcp23s17,s17-spi1-1-preset=1,s17-spi1-1-int-gpio=12
                              ^^^^^^
It should say "present" (and you don't need the "=1").

ahmedawad
Posts: 42
Joined: Mon Apr 18, 2016 11:44 am
Location: Bremen, DE

Re: using mcp23s17 device tree

Thu Aug 17, 2017 12:39 pm

Thanks for the reply. I removed "1" from the dtoverlay, but still the output from cat /proc/interrupts and cat /sys/class/gpio/gpiochip496/label is the same.

How to check the kernel recognize the mcp23s17 ? and please how can i communicate with it in the user space ?

when checking gpiochip496, it points to spi1.1

Code: Select all

pi@raspberrypi:~ $ ls -l /sys/class/gpio/gpiochip496/
total 0
-rwxrwx--- 1 root gpio 4096 Aug 17 12:34 base
lrwxrwxrwx 1 root gpio    0 Aug 17 12:34 device -> ../../../spi1.1
-rwxrwx--- 1 root gpio 4096 Aug 17 12:34 label
-rwxrwx--- 1 root gpio 4096 Aug 17 12:34 ngpio
drwxrwx--- 2 root gpio    0 Aug 17 12:34 power
lrwxrwxrwx 1 root gpio    0 Aug 17 12:34 subsystem -> ../../../../../../../../../class/gpio
-rwxrwx--- 1 root gpio 4096 Aug 17 12:34 uevent
pi@raspberrypi:~ $

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5812
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: using mcp23s17 device tree

Thu Aug 17, 2017 1:09 pm

If the directory "/sys/class/gpio/gpiochip496" exists and has a label saying "mcp23s17" then the Pi has recognised the GPIO expander. 496 is the first GPIO supported by the expander, and "ngpio" tells you how many GPIOs it supports, so if ngpio says 8 then it supports GPIOs 496 to 503. You then just have to export them in the usual way:

Code: Select all

$ cd /sys/class/gpio
$ echo 496 > export
$ ls gpio496
active_low  device  direction  power  subsystem  uevent  value
You can learn all about the userspace GPIO support here.

ahmedawad
Posts: 42
Joined: Mon Apr 18, 2016 11:44 am
Location: Bremen, DE

Re: using mcp23s17 device tree

Thu Aug 17, 2017 5:25 pm

Thank you so much for your help! I can now read/write to the mcp23s17 port expander using its device tree overlay. However, the interrupt does not work at all.

in config.txt I do the following,

Code: Select all

dtoverlay=mcp23s17,s17-spi1-1-present,s17-spi1-1-int-gpio=12
I understand that this configures the port expander to generate interrupts when either any value changes on its both ports (A or B) and the GPIO12 or BCM 12 on the RPI is the pin which will receive this interrupt.

In order to check how many interrupts I did get on GPIO12, I do:

Code: Select all

pi@raspberrypi:~ $ cat /proc/interrupts
:
:
178:         0          0          0          0  pinctrl-bcm2835  12 Level     spi1.1
:
:
When I apply any signal on BCM 12, the interrupt counter increases in "cat /proc/interrupts" but when I connect BCM 12 to INTA/INTB of the port expander and the port values change, the counter does not increase !!

It means that the port expander doesn"t generate the interrupt pin. Either I make something wrong or the device tree overlay doesn"t configure the port expander for generating the interrupts. ?? :)

I use this link to configure the dt overlay in config.txt https://github.com/raspberrypi/linux/bl ... verlay.dts

wavelet
Posts: 17
Joined: Mon Mar 21, 2016 7:15 pm

Re: using mcp23s17 device tree

Sat Aug 26, 2017 10:49 am

Have you set up interrupt edge selection for the input port of the mcp23s17? For example, if you want to interrupt on rising edges of gpio496, you need to execute:

Code: Select all

echo "rising" > /sys/class/gpio/gpio496/edge
The choices you have are "none", "rising", "falling", or "both".

ahmedawad
Posts: 42
Joined: Mon Apr 18, 2016 11:44 am
Location: Bremen, DE

Re: using mcp23s17 device tree

Mon Aug 28, 2017 2:37 pm

Thank you very much! That will do it.

Return to “Device Tree”