micropython
micropython copied to clipboard
ports/rp2: Support reading the BOOTSEL button on the Pico board
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
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?
That's what I think, too:) I don't know how to add as a method to the rp2 module.
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.
Thank you for the easy to understand explanation. it works:) https://gist.github.com/GitHub30/cbae9fbfbca25820697b74f8e1e6ef33/revisions
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.
I have updated the PR. regards.
I wonder if this should actually be implemented more like
pyb.Switchor possibly we should make it work in machine.Pin
Would be good to think and discuss this point, how to expose the bootsel button.
- As done here, a special function
rp2.bootsel(). - A special pin
machine.Pin("BOOTSEL"), which is read-only. - A new switch/button object
rp2.Button()(orrp2.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.
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")
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.
Is there an update/estimation of when this feature will be available? :)
@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.
This was merged in 673957b643b111e5879efd05a2ddf0808ed613d0 with follow up in f80d040c038c343b0709eba537014fb52bc8115e (see #10935).