elixir-circuits / circuits_gpio

Use GPIOs from Elixir
129 stars 23 forks source link

Raspberry Pi 4 model B :: GPIO Pull modes #62

Closed jsimmonds2 closed 5 years ago

jsimmonds2 commented 5 years ago

Have a simple app which uses GPIO 12 as input connected to an open-drain output on a DS3231 RTC. Have tried setting :pull_mode in the GPIO.open options according to the documentation but cannot get a :falling edge interrupt.

    {:ok, gpio_pin_12} = GPIO.open 12, :input, [{:pull_mode, :pullup}]    # A 1 Hz SQW or Alarm 1 interrupt from the DS3231
    GPIO.set_interrupts gpio_pin_12, :falling

As I don't have a scope, I'm going to try a 47k external pullup and see if that helps . . now off to order that resistor :)

mdsebald commented 5 years ago

Are you getting the initial message on the GPIO.set_interrupts call?

Then, if you disconnect the RTC and just manually short GPIO 12 to ground, are you still not getting the interrupt message?

jsimmonds2 commented 5 years ago

Are you getting the initial message on the GPIO.set_interrupts call?

Only when GPIO 12 is tied to GND and not when tied to +3V3.

Then, if you disconnect the RTC and just manually short GPIO 12 to ground, are you still not getting the interrupt message?

That is so.

Sure looks like there's a :pulldown on that GPIO. I can take that lead from GND and dab it onto the +3V3 rail and I get :falling edge interrupts, with pin value 0.

mdsebald commented 5 years ago

I don't own an RPi 4. I hope nothing has changed in the GPIOs. I'll take a closer look when I have more time this evening (US Central TZ).

mdsebald commented 5 years ago

Sorry, unable to verify if issue exists on my setup or not. Will try again tomorrow evening. I have ordered an RPi 4. I will test with that, also.

jsimmonds2 commented 5 years ago

Wow ! Thanks for digging deeper . . I was actually thinking about offering to send you an RPi4 to test with.

mdsebald commented 5 years ago

First I verified that the internal pull-up and pull-down resistors are still working as expected with Circuits.GPIO on the RPi3. I tried the same on an RPi4, and indeed, it appears the internal pull-up resistors (I only tried on GPIOs 12 and 23 so far) are not being switched in via the pullmode pullup option. I don't know what is wrong and not sure what it will take to fix it yet. Hopefully, you can get by with an external pull-up resistor for now. Just in case the pullmode option is doing something undefined on the RPi4, I would also take out that option, when opening an input pin. Thanks for bringing this to our attention.

fhunleth commented 5 years ago

Oh wow, the GPIO interface changed on the BCM2711 (RPi4)!

Here's the best of what I was able to find quickly on the new way: https://github.com/RPi-Distro/raspi-gpio/blob/master/raspi-gpio.c#L476-L492. The peripheral spec that's currently on the Raspberry Pi site doesn't seem to talk about this or I haven't found it.

Would either of you like to take a crack at updating https://github.com/elixir-circuits/circuits_gpio/blob/master/src/hal_rpi.c#L85?

mdsebald commented 5 years ago

Thanks for finding that new code. I was not able to find anything about that myself. I'll take a look at updating the code.

mdsebald commented 5 years ago

FYI: I found this code too https://github.com/raspberrypi/linux/blob/e2a6ce9ff7c94cce5c1d40d2c79a2cd0498d5f88/drivers/pinctrl/bcm/pinctrl-bcm2835.c#L933-L954

jsimmonds2 commented 5 years ago

FYI: I found this code too https://github.com/raspberrypi/linux/blob/e2a6ce9ff7c94cce5c1d40d2c79a2cd0498d5f88/drivers/pinctrl/bcm/pinctrl-bcm2835.c#L933-L954

Hi Mark. Could you edit your link to put the URL inside the parentheses rather than the brackets. :)

That reference you provided explains what I'm seeing, I think.

mdsebald commented 5 years ago

Done. Thanks!

mdsebald commented 5 years ago

I updated src/hal_rpi.c to include RPi4 pull modes code (branch: implement_rpi4_pullmodes). Tested pull-up and pull-down modes on one GPIO pin (12) on the RPi4 and it worked OK. Still need to do more testing including regression tests on other Pi models, before submitting PR.

fhunleth commented 5 years ago

Fixed in #63. Thanks @mdsebald.

jsimmonds2 commented 5 years ago

Appreciate your fixes, @mdsebald 👍 I'm now "one happy end-user", getting the ( Active Low ) Alarm interrupts from the DS3231 RTC exactly as described 🙂 . . without any external pullup resistor.

mdsebald commented 5 years ago

Thanks to @fhunleth for pushing it over the finish line too, Glad we were able to resolve this issue.