Menu

#168 Pullups not working on Pi 4

Done
None
Medium
Defect
2020-10-07
2019-06-25
limor
No

Hihi, i just tested the library on a new RasPi4 and you can toggle output on/off but pullup enabling doesnt seem to work. Probably a mmio offeset change. do you need a hand fixing?

Discussion

  • Phil

    Phil - 2019-06-25

    The process for setting pull-ups has changed slightly for the better with Pi 4 and the offsets are slightly different. I've got a local hack of RPi.GPIO that works which I'm cleaning up. Will grab a patch file for it.

     
  • Phil

    Phil - 2019-06-25

    Here's what I have so far:

    diff --git source/c_gpio.c source/c_gpio.c
    index 628011a..b5432a6 100644
    --- source/c_gpio.c
    +++ source/c_gpio.c
    @@ -43,6 +43,11 @@ SOFTWARE.
     #define PULLUPDN_OFFSET             37  // 0x0094 / 4
     #define PULLUPDNCLK_OFFSET          38  // 0x0098 / 4
    
    +#define PULLUPDN_OFFSET_2711_0      57
    +#define PULLUPDN_OFFSET_2711_1      58
    +#define PULLUPDN_OFFSET_2711_2      59
    +#define PULLUPDN_OFFSET_2711_3      60
    +
     #define PAGE_SIZE  (4*1024)
     #define BLOCK_SIZE (4*1024)
    
    @@ -208,21 +213,44 @@ void set_low_event(int gpio, int enable)
    
     void set_pullupdn(int gpio, int pud)
     {
    -    int clk_offset = PULLUPDNCLK_OFFSET + (gpio/32);
    -    int shift = (gpio%32);
    -
    -    if (pud == PUD_DOWN)
    -        *(gpio_map+PULLUPDN_OFFSET) = (*(gpio_map+PULLUPDN_OFFSET) & ~3) | PUD_DOWN;
    -    else if (pud == PUD_UP)
    -        *(gpio_map+PULLUPDN_OFFSET) = (*(gpio_map+PULLUPDN_OFFSET) & ~3) | PUD_UP;
    -    else  // pud == PUD_OFF
    +    // Check GPIO register
    +    int is2711 = *(gpio_map+PULLUPDN_OFFSET_2711_3) != 0x6770696f;
    +    if (is2711) {
    +        // Pi 4 Pull-up/down method
    +       int pullreg = PULLUPDN_OFFSET_2711_0 + (gpio >> 4);
    +       int pullshift = (gpio & 0xf) << 1;
    +       unsigned int pullbits;
    +       unsigned int pull = 0;
    +       if (pud == PUD_DOWN){
    +           pull = 2;
    +        } else
    +        if (pud == PUD_UP){
    +           pull = 1;
    +        }
    +        pullbits = *(gpio_map + pullreg);
    +        pullbits &= ~(3 << pullshift);
    +        pullbits |= (pull << pullshift);
    +        *(gpio_map + pullreg) = pullbits;
    +    }
    +    else
    +    {
    +        // Legacy Pull-up/down method
    +        int clk_offset = PULLUPDNCLK_OFFSET + (gpio/32);
    +        int shift = (gpio%32);
    +
    +        if (pud == PUD_DOWN)
    +            *(gpio_map+PULLUPDN_OFFSET) = (*(gpio_map+PULLUPDN_OFFSET) & ~3) | PUD_DOWN;
    +        else if (pud == PUD_UP)
    +            *(gpio_map+PULLUPDN_OFFSET) = (*(gpio_map+PULLUPDN_OFFSET) & ~3) | PUD_UP;
    +        else  // pud == PUD_OFF
    +            *(gpio_map+PULLUPDN_OFFSET) &= ~3;
    +
    +        short_wait();
    +        *(gpio_map+clk_offset) = 1 << shift;
    +        short_wait();
             *(gpio_map+PULLUPDN_OFFSET) &= ~3;
    -
    -    short_wait();
    -    *(gpio_map+clk_offset) = 1 << shift;
    -    short_wait();
    -    *(gpio_map+PULLUPDN_OFFSET) &= ~3;
    -    *(gpio_map+clk_offset) = 0;
    +        *(gpio_map+clk_offset) = 0;
    +    }
     }
    
     void setup_gpio(int gpio, int direction, int pud)
    

    This is largely inspired by the changes to raspi-gpio - you can see the commit for 2711 support here: https://github.com/RPi-Distro/raspi-gpio/commit/80fa7d04eafb3ea34fc6f2d32de5f1873b5fb369

     
  • Ben Croston

    Ben Croston - 2019-06-25

    Hopefully I'll get my hands on a Pi4 tomorrow to test and make a release. Thanks for the patch!

     
  • Phil

    Phil - 2019-07-02

    Did you manage to get your hands on a Pi 4? Let me know if we can assist in any way!

     
  • Ben Croston

    Ben Croston - 2019-07-08

    Alpha release is now available for testing:
    pip3 install --upgrade --pre rpi.gpio

     

    Last edit: Ben Croston 2019-07-08
  • Melissa LeBlanc-Williams

    I tested with the Alpha Release and it seems to be working great. Thanks for updating this.

     
  • kienzan

    kienzan - 2019-07-08

    Tried on my pi4 with GPIO17 pullup, working perfectly. Thank you all.

     
  • Frank Petroski

    Frank Petroski - 2019-07-11

    Tried on one of my projects using pins 23 and 24, worked fine. Also appears to have cleared up a problem in the cleanup() method. Project uses an LCD though Adafruit_GPIO, and before this fix calling the cleanup() method turned the backlight back on. Now functions the same way as the pevious version on a PI 3.

     
  • limor

    limor - 2019-07-20

    hi ben, thanks for the quick coding - is this ticket resolved and the code available for any pip installs?

     
  • Ben Croston

    Ben Croston - 2019-07-21
    • status: New --> Done
     
  • Ben Croston

    Ben Croston - 2019-07-21

    In release 0.7.0

     
  • Daniel Domit

    Daniel Domit - 2020-10-06

    Hlelo everyone, I dont know what is the problem here but my pullups are not working, can you help me?

    This is my code, but the measured voltage is not 3.3v it fluctuates all around except when i connect it to ground. Then, its 0

    Below is my code..... I run it with --------- python3 input.py 11 (in the case of pin 11)

    import RPi.GPIO as GPIO
    import time
    import sys

    pinLeft = int(sys.argv[1])
    print(pinLeft)

    GPIO.setmode(GPIO.BOARD)

    GPIO.setup(pinLeft, GPIO.IN, pull_up_down=GPIO.PUD_UP)

    while True:
    if GPIO.input(pinLeft):
    print('Input was HIGH')
    else:
    print('Input was LOW')
    time.sleep(0.05)

    GPIO.cleanup()

     
    • Ben Croston

      Ben Croston - 2020-10-06

      What is the result of:
      import RPi.GPIO as GPIO
      print(GPIO.VERSION)

       

      Last edit: Ben Croston 2020-10-06
      • Daniel Domit

        Daniel Domit - 2020-10-06

        import RPi.GPIO as GPIO
        print(GPIO.VERSION)>>> print(GPIO.VERSION)
        0.7.0

         
        • Ben Croston

          Ben Croston - 2020-10-06

          Does it work with other pins?
          Does it work after rebooting?

           

          Last edit: Ben Croston 2020-10-06
  • Daniel Domit

    Daniel Domit - 2020-10-07

    It works on some pins sometimes and on some other times, it seems to be completely random

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.