pkliem
Posts: 4
Joined: Fri Jun 01, 2018 11:29 am

Can not use some GPIOs with matrix_keypad - why?

Fri Jun 01, 2018 1:19 pm

Hi,

i converted an old Atari Portfolio with a Raspi Zero w. To use the keyboard I try to use the matrix keypad module. I followed the tutorial I found here: http://blog.gegg.us/2017/08/a-matrix-ke ... one-right/ .

I am on a freshly compiled kernel (4.14.43+ #1 Mon May 28 17:31:10 CEST 2018 armv6l GNU/Linux). Basically the keyboard works fine (at least most of the keys) with my dts file. However I need 16 IOs and can only figure out 14 working one (so two rows are missing). Some IOs are just not not working. Those I tried and can not use are
GPIO14 (uart0 tx)
GPIO15 (uart rx)
GPIO7 (spi cs1)
GPIO4 (gp clk0)
GPIO5 (gpclk1)
GPIO6 (gpclk2)

I disabled everything (SPI, I2C, UART) in config.txt.

What am I missing? When I check the state of the IOs using raspi-gpio get all the IOs the IOs in question are (I assume correctly, as the non functioning ones are rows) set as input.

I would love to use GPIO5/GPIO6 for the missing ones.

I included my dts below, but I think I am missing some unmapping for the IOs I need to do. Maybe it is simple but I just can't figure that one out.

Thanks for any hints

Peter

My dts is currently as follows

Code: Select all

/dts-v1/;
    /plugin/;
    / {
           compatible = "brcm,bcm2835", "brcm,bcm2708", "brcm,bcm2709";

           [email protected] {
              target-path = "/";
              __overlay__ {
                 keypad: PFMATRIX {
                    compatible = "gpio-matrix-keypad";
                    debounce-delay-ms = <10>;
                    col-scan-delay-us = <10>;

                    row-gpios = <&gpio 23 0    // 1
                                 &gpio 17 0    // 2
                                 &gpio 27 0    // 3
                                 &gpio 22 0    // 4
                                 &gpio 15 0    // 5
                                 &gpio 7 0    // 6
                                 &gpio 13 0    // 7
                                 &gpio 19 0>;   // 8

                    col-gpios = <&gpio 26 0    // 9
                                 &gpio 21 0    // 10
                                 &gpio 20 0    // 11
                                 &gpio 16 0    // 12
                                 &gpio 12 0    // 13
                                 &gpio 25 0    // 14
                                 &gpio 24 0    // 15
                                 &gpio 18 0>;  // 16

                    linux,keymap = <
                                    // col0 row0 KEY_ESC
                                    0x00000001
                                    // col0 row1 KEY_X
                                    0x0100002d
        /*
       			.... a lot of mappings
        */
        
        
                                    >;

                 };
              };
           };
      };

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

Re: Can not use some GPIOs with matrix_keypad - why?

Fri Jun 01, 2018 2:20 pm

Your problem is likely to be the pull on those pins interfering with with detection mechanism. By default, GPIOs 0-8 pull high, and 9-27 pull low. This may explain why 4-7 are problematic. The dt-blob (not Device Tree, but used by the firmware for pin setup) changes the pulls on 14 and 15 so that there is no pull on 14, and 15 pulls high.

I find it odd that the Linux GPIO interface does not recognise pulls as a property to change - for that you have to delve into pinctrl. I suggest you define a group of pins for use by your PFMATRIX node that sets the pulls to low - it would look something like this:

Code: Select all

/dts-v1/;
    /plugin/;
    / {
           compatible = "brcm,bcm2835", "brcm,bcm2708", "brcm,bcm2709";
           
           [email protected] {
              target-path = "/";
              __overlay__ {
                 keypad: PFMATRIX {
                    compatible = "gpio-matrix-keypad";
                    debounce-delay-ms = <10>;
                    col-scan-delay-us = <10>;
                    pinctrl-names = "default";
                    pinctrl-0 = <&keymatrix_pins>;

                    row-gpios = <&gpio 23 0    // 1
                                 &gpio 17 0    // 2
                                 &gpio 27 0    // 3
                                 &gpio 22 0    // 4
                                 &gpio 15 0    // 5
                                 &gpio 7 0    // 6
                                 &gpio 13 0    // 7
                                 &gpio 19 0>;   // 8

                    col-gpios = <&gpio 26 0    // 9
                                 &gpio 21 0    // 10
                                 &gpio 20 0    // 11
                                 &gpio 16 0    // 12
                                 &gpio 12 0    // 13
                                 &gpio 25 0    // 14
                                 &gpio 24 0    // 15
                                 &gpio 18 0>;  // 16

                    linux,keymap = <
                                    // col0 row0 KEY_ESC
                                    0x00000001
                                    // col0 row1 KEY_X
                                    0x0100002d
        /*
       			.... a lot of mappings
        */
        
        
                                    >;

                 };
              };
           };

           [email protected] {
              target = <&gpio>;
              __overlay__ {
                  keymatrix_pins: keymatrix_pins {
		      brcm,pins = </* all your GPIO numbers - 5 6 7 8 etc. */>;
		      brcm,function = <0>; /* input */
		      brcm,pull = <1>; /* down */
                  };
              };
           };
      };

pkliem
Posts: 4
Joined: Fri Jun 01, 2018 11:29 am

Re: Can not use some GPIOs with matrix_keypad - why?

Fri Jun 01, 2018 9:26 pm

Thanks very much! You hit the point I guess.

I switched to GPIO5 and GPIO6 for the missing ones.

What works is to directly set the pull downs using raspi-gpio:

Code: Select all

sudo raspi-gpio set 5 ip pd
sudo raspi-gpio set 6 ip pd
What does not work is an additional fragment. I added the following to my dts:

Code: Select all

[email protected] {
              target = <&gpio>;
              __overlay__ {
                  keymatrix_pins: keymatrix_pins {
		      brcm,pins = <5 6>;
		      brcm,function = <0>; /* input */
		      brcm,pull = <1>; /* down */
                  };
              };
           };

compiled with

Code: Select all

./scripts/dtc/dtc -W no-unit_address_vs_reg -I dts -O dtb -o pfmatrix.dtbo pfmatrix.dts
and copied to /boot/overlays.

Any idea?

However using raspi-gpio I can solve the issue. Again: thanks for leading me on to the right track.

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

Re: Can not use some GPIOs with matrix_keypad - why?

Fri Jun 01, 2018 10:03 pm

You need to include "[email protected]" in the dtc command line, otherwise it won't generate the necessary symbols.

pkliem
Posts: 4
Joined: Fri Jun 01, 2018 11:29 am

Re: Can not use some GPIOs with matrix_keypad - why?

Mon Jun 04, 2018 8:37 pm

just adding the "[email protected]" to my command did not do the trick. After some poking around I ended up with the following fragment which seems to work

Code: Select all

[email protected] {
        target = <&gpio>;
        __overlay__ {
            pinctrl-names = "default";
            pinctrl-0 = <&my_pins>;

            my_pins: my_pins {
                brcm,pins = <5 6>;     /* gpio no. */
                brcm,function = <0 0>; /* 0:in, 1:out */
                brcm,pull = <1 1>;     /* 2:up 1:down 0:none */
            };
        };
    };
I consider this solved! Thanks again for your help!

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

Re: Can not use some GPIOs with matrix_keypad - why?

Mon Jun 04, 2018 9:09 pm

Ah - I think you must have missed the reference to the pins node that I added to the first fragment:

Code: Select all

                    pinctrl-names = "default";
                    pinctrl-0 = <&keymatrix_pins>;
Your solution will work, but only until you apply some other overlay that uses the same trick of getting the gpio node to activate your pinctrl group.

pkliem
Posts: 4
Joined: Fri Jun 01, 2018 11:29 am

Re: Can not use some GPIOs with matrix_keypad - why?

Mon Jun 04, 2018 11:03 pm

You are right. I actually missed the reference. I will try this again, but I guess that will work like you suggested ...

Return to “Device Tree”