forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add touchio pullup rp2350 #10227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add touchio pullup rp2350 #10227
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a11217a
added touchio pullup for RP2350
todbot 6c1b6a4
add small note in port makefile about pull-up touchio for RP2350
todbot 232b699
Add "pull" parameter to TouchIn ports
todbot 0bd7d7e
Update shared-bindings/touchio/TouchIn.c
dhalbert 7e18bed
Merge branch 'adafruit:main' into touchio_pullup_rp2350
todbot b689d74
fix docstring
todbot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| // This file is part of the CircuitPython project: https://circuitpython.org | ||
| // | ||
| // SPDX-FileCopyrightText: Copyright (c) 2016 Scott Shawcroft for Adafruit Industries | ||
| // SPDX-FileCopyrightText: Copyright (c) 2018 Nick Moore for Adafruit Industries | ||
| // | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| #include <string.h> | ||
| #include <stdio.h> | ||
|
|
||
| #include "py/runtime.h" | ||
| #include "py/mphal.h" | ||
| #include "shared-bindings/touchio/TouchIn.h" | ||
| #include "shared-bindings/microcontroller/Pin.h" | ||
|
|
||
| // Native touchio only for RP2350, which cannot do shared-module touchio | ||
| #ifdef PICO_RP2350 | ||
|
|
||
| // This is a capacitive touch sensing routine using a single digital | ||
| // pin. The pin should be connected to the sensing pad, and to ground | ||
| // via a 1Mohm or thereabout drain resistor. When a reading is taken, | ||
| // the pin's capacitance is charged by setting it to a digital output | ||
| // 'high' for a few microseconds, and then it is changed to a high | ||
| // impedance input. We measure how long it takes to discharge through | ||
| // the resistor (around 50us), using a busy-waiting loop, and average | ||
| // over N_SAMPLES cycles to reduce the effects of noise. | ||
|
|
||
| #define N_SAMPLES 10 | ||
| #define TIMEOUT_TICKS 10000 | ||
|
|
||
| static uint16_t get_raw_reading(touchio_touchin_obj_t *self) { | ||
|
|
||
| uint16_t ticks = 0; | ||
|
|
||
| for (uint16_t i = 0; i < N_SAMPLES; i++) { | ||
| // set pad to digital output LOW for 10us to charge it | ||
|
|
||
| common_hal_digitalio_digitalinout_switch_to_output(self->digitalinout, false, DRIVE_MODE_PUSH_PULL); | ||
| mp_hal_delay_us(10); | ||
|
|
||
| // set pad back to an input and take some samples, waiting for it to go HIGH | ||
|
|
||
| common_hal_digitalio_digitalinout_switch_to_input(self->digitalinout, PULL_NONE); | ||
|
|
||
| while (!common_hal_digitalio_digitalinout_get_value(self->digitalinout)) { | ||
| if (ticks >= TIMEOUT_TICKS) { | ||
| return TIMEOUT_TICKS; | ||
| } | ||
| ticks++; | ||
| } | ||
| } | ||
| return ticks; | ||
| } | ||
|
|
||
| void common_hal_touchio_touchin_construct(touchio_touchin_obj_t *self, const mcu_pin_obj_t *pin) { | ||
| common_hal_mcu_pin_claim(pin); | ||
| self->digitalinout = mp_obj_malloc(digitalio_digitalinout_obj_t, &digitalio_digitalinout_type); | ||
|
|
||
| common_hal_digitalio_digitalinout_construct(self->digitalinout, pin); | ||
|
|
||
| uint16_t raw_reading = get_raw_reading(self); | ||
| if (raw_reading == TIMEOUT_TICKS) { | ||
| common_hal_touchio_touchin_deinit(self); | ||
| mp_raise_ValueError(MP_ERROR_TEXT("No pullup on pin; 1Mohm recommended")); | ||
| } | ||
| self->threshold = raw_reading * 1.05 + 100; | ||
| } | ||
|
|
||
| bool common_hal_touchio_touchin_deinited(touchio_touchin_obj_t *self) { | ||
| return self->digitalinout == MP_OBJ_NULL; | ||
| } | ||
|
|
||
| void common_hal_touchio_touchin_deinit(touchio_touchin_obj_t *self) { | ||
| if (common_hal_touchio_touchin_deinited(self)) { | ||
| return; | ||
| } | ||
|
|
||
| common_hal_digitalio_digitalinout_deinit(self->digitalinout); | ||
| self->digitalinout = MP_OBJ_NULL; | ||
| } | ||
|
|
||
| void touchin_reset() { | ||
| } | ||
|
|
||
| bool common_hal_touchio_touchin_get_value(touchio_touchin_obj_t *self) { | ||
| uint16_t reading = get_raw_reading(self); | ||
| return reading > self->threshold; | ||
| } | ||
|
|
||
| uint16_t common_hal_touchio_touchin_get_raw_value(touchio_touchin_obj_t *self) { | ||
| return get_raw_reading(self); | ||
| } | ||
|
|
||
| uint16_t common_hal_touchio_touchin_get_threshold(touchio_touchin_obj_t *self) { | ||
| return self->threshold; | ||
| } | ||
|
|
||
| void common_hal_touchio_touchin_set_threshold(touchio_touchin_obj_t *self, | ||
| uint16_t new_threshold) { | ||
| self->threshold = new_threshold; | ||
| } | ||
|
|
||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // This file is part of the CircuitPython project: https://circuitpython.org | ||
| // | ||
| // SPDX-FileCopyrightText: Copyright (c) 2016 Scott Shawcroft | ||
| // SPDX-FileCopyrightText: Copyright (c) 2018 Nick Moore for Adafruit Industries | ||
| // | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "common-hal/microcontroller/Pin.h" | ||
| #include "shared-bindings/digitalio/DigitalInOut.h" | ||
|
|
||
| #include "py/obj.h" | ||
|
|
||
| // Native touchio only for RP2350, which cannot do shared-module touchio | ||
| #ifdef PICO_RP2350 | ||
|
|
||
| typedef struct { | ||
| mp_obj_base_t base; | ||
| digitalio_digitalinout_obj_t *digitalinout; | ||
| uint16_t threshold; | ||
| } touchio_touchin_obj_t; | ||
|
|
||
| void touchin_reset(void); | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| // This file is part of the CircuitPython project: https://circuitpython.org | ||
| // | ||
| // SPDX-FileCopyrightText: Copyright (c) 2024 Adafruit Industries LLC | ||
| // | ||
| // SPDX-License-Identifier: MIT |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the old comment, but your implementation works the opposite way.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just listened now, after having seen the github notes. doh!
Having an optional argument to
TouchIn()is what I originally did (you may've seen my opened-and-quickly closed PR ##10223) as I realized this would impact all other ports, increase their code footprint, and require me to touch many more files to adjust thetouchioAPI for all ports.As I listen to the meeting audio, I'm conflicted. I like the simplicity of this change, but dislike the confusion it can cause for users. I like adding a
pull_dirargument but I'm worried about busting available space on SAMD21. Sigh.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I saw that PR. I was going to comment that I think it's OK, and preferred, to use
digitalio.Pull, because we use that consistently elsewhere. The argument will need to be validated to be of typePullorNone. Yes, it will grow the SAMD21 port slightly. We could#ifout the validation on SAMD21 (or anything not TOUCH_NATIVE) to save space if we needed to.SAMD21 and Espressif are the only two ports with native touchio. So there aren't that many other places to change to add the extra argument to
common-hal.Then you could add
CIRCUITPY_TOUCHIO_PULL_DIRECTIONto determine, at compile time, the two alternatives for theshared-modulecode. This would be a bool internally, since that's the easiest thing for the preprocessor. It would default to the DOWN value (false) incircuitpy_mpconfig.h, and be set to UP (true) only for RP2350.The documentation in
shared-bindingswill need to be updated.I think users will be confused a bit either way, but the extra arg makes the assumed pull more explicit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing all ports is the nature of changing a CircuitPython API. It ensures consistency. The reason is to make the up or down choice explicit so folks know to change their setup.
The benefit of adding an additional parameter would be that you can use the common touchio code still. It can support both up and down and only raise an exception on RP2350 with down.
Do we want to split out generic touchio into a new module? That wouldn't change touchio on SAMD and allow both to coexist in the future. We could do the hybrid model in CP10.