micropython icon indicating copy to clipboard operation
micropython copied to clipboard

ports/rp2: Support reading the BOOTSEL button on the Pico board

Open GitHub30 opened this issue 3 years ago • 8 comments

This patch allows the BOOTSEL button to be read. Tested with the following:

import time

def WaitForPushed():
    while rp2.bootsel_button() == 0:
        time.sleep(0.1)
    print("BOOTSEL Pushed")

def WaitForRelease():
    while rp2.bootsel_button() == 1:
        time.sleep(0.1)
    print("BOOTSEL Released")

if rp2.bootsel_button() == 1:
    print("BOOTSEL Pushed")
    WaitForRelease()
else:
    print("BOOTSEL Not Pushed")
    
while True:
    WaitForPushed()
    WaitForRelease()

Output:

BOOTSEL Not Pushed
BOOTSEL Pushed
BOOTSEL Released
BOOTSEL Pushed
BOOTSEL Released

https://github.com/micropython/micropython/issues/6852 Reference implementation: https://github.com/raspberrypi/pico-examples/blob/master/picoboard/button/button.c Demo: https://qiita.com/relu/items/df977abc4f2b6b2850d5

GitHub30 avatar Aug 29 '22 08:08 GitHub30

Shouldn't that better be added as a method to the rp2 module instead of creating a new module? Or using a "special" pin object?

robert-hh avatar Aug 29 '22 09:08 robert-hh

That's what I think, too:) I don't know how to add as a method to the rp2 module.

GitHub30 avatar Aug 29 '22 09:08 GitHub30

Just add the definitions from your bootsel_module_globals_table[] to the rp2_module_globals_table[] of modrp2.c and move the code from your file into modrp2.c. But first:

  • fix the build errors and the commit format errors.
  • wait for a comment by @dpgeorge or @jimmo about this PR.

Note that the commit requires you to have a valid e-mail address be in your github configuration, and if you want to claim a copyright, it should show a real name.

robert-hh avatar Aug 29 '22 09:08 robert-hh

Thank you for the easy to understand explanation. it works:) https://gist.github.com/GitHub30/cbae9fbfbca25820697b74f8e1e6ef33/revisions

GitHub30 avatar Aug 29 '22 16:08 GitHub30

Thanks @GitHub30 -- I agree this is a useful feature given that this board only has the one button.

Just a few notes on this PR. This code is from the forum (specifically by Hippy) and has their copyright in it. Also the style is quite different to other MicroPython code.

But what Robert said is right, this should not be its own module. Your second commit in the linked gist looks a lot better. Could you please update the PR to this. As per https://github.com/micropython/micropython/issues/6852#issuecomment-774311624 I wonder if this should actually be implemented more like pyb.Switch or possibly we should make it work in machine.Pin (like we do with the CYW43 pins)... bootsel doesn't have an actual pin number though as far as I can tell.

It's pretty subtle how this works, especially if you aren't familiar with how bootsel works. Please add some comments explaining what this code does (perhaps link to relevant documentation and schematic). To be honest, I'm not exactly sure how SS gets a pull-up given that the pull-up in the schematic (R4) is no-fit. Guessing somewhere in pico-sdk it sets an internal pull-up.

jimmo avatar Aug 30 '22 00:08 jimmo

I have updated the PR. regards.

GitHub30 avatar Aug 30 '22 08:08 GitHub30

I wonder if this should actually be implemented more like pyb.Switch or possibly we should make it work in machine.Pin

Would be good to think and discuss this point, how to expose the bootsel button.

  1. As done here, a special function rp2.bootsel().
  2. A special pin machine.Pin("BOOTSEL"), which is read-only.
  3. A new switch/button object rp2.Button() (or rp2.Switch() to match pyb, but it's a button not a switch...)

I guess option (2) would be the most general, even if it makes the Pin class more complicated. Then a button could be made out of the pin using machine.Signal.

dpgeorge avatar Aug 30 '22 14:08 dpgeorge

Would be good to think and discuss this point, how to expose the bootsel button.

My vote would be for machine.Pin("BOOTSEL") going by the precedent already set by "LED".

I also prefer machine.Pin("BOOTSEL")

GitHub30 avatar Aug 30 '22 15:08 GitHub30

This seems like an important feature, so I hope someone can resolve the minor details and get it merged!

For the delay time, instead of trying to match bootrom, why not just make the number of iterations a parameter? The bootrom has to work on everything, but a shorter delay might be fine on the Pico, and different boards might have different pull-up strengths.

pdg137 avatar Nov 14 '22 23:11 pdg137

Is there an update/estimation of when this feature will be available? :)

PaulSut avatar Jan 03 '23 15:01 PaulSut

@PaulSut see my comment on the related issue for a way to do it using inline assembly - maybe that can work for you until it's officially supported.

pdg137 avatar Jan 04 '23 18:01 pdg137

This was merged in 673957b643b111e5879efd05a2ddf0808ed613d0 with follow up in f80d040c038c343b0709eba537014fb52bc8115e (see #10935).

dpgeorge avatar Mar 09 '23 00:03 dpgeorge