Skip to content

Commit ae08c79

Browse files
authored
Merge pull request #10227 from todbot/touchio_pullup_rp2350
Add touchio pullup rp2350
2 parents ba483d5 + b689d74 commit ae08c79

File tree

9 files changed

+52
-27
lines changed

9 files changed

+52
-27
lines changed

locale/circuitpython.pot

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1560,6 +1560,10 @@ msgstr ""
15601560
msgid "No pulldown on pin; 1Mohm recommended"
15611561
msgstr ""
15621562

1563+
#: shared-module/touchio/TouchIn.c
1564+
msgid "No pullup on pin; 1Mohm recommended"
1565+
msgstr ""
1566+
15631567
#: py/moderrno.c
15641568
msgid "No space left on device"
15651569
msgstr ""
@@ -2582,7 +2586,8 @@ msgstr ""
25822586
msgid "bits must be 32 or less"
25832587
msgstr ""
25842588

2585-
#: shared-bindings/audiodelays/Echo.c shared-bindings/audiodelays/PitchShift.c
2589+
#: shared-bindings/audiodelays/Chorus.c shared-bindings/audiodelays/Echo.c
2590+
#: shared-bindings/audiodelays/PitchShift.c
25862591
#: shared-bindings/audiofilters/Distortion.c
25872592
#: shared-bindings/audiofilters/Filter.c shared-bindings/audiomixer/Mixer.c
25882593
msgid "bits_per_sample must be 8 or 16"

ports/atmel-samd/common-hal/touchio/TouchIn.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "py/binary.h"
1313
#include "py/mphal.h"
1414
#include "shared-bindings/microcontroller/Pin.h"
15+
#include "shared-bindings/digitalio/Pull.h"
1516
#include "shared-bindings/touchio/TouchIn.h"
1617

1718
// Native touchio only exists for SAMD21
@@ -38,7 +39,7 @@ static uint16_t get_raw_reading(touchio_touchin_obj_t *self) {
3839
}
3940

4041
void common_hal_touchio_touchin_construct(touchio_touchin_obj_t *self,
41-
const mcu_pin_obj_t *pin) {
42+
const mcu_pin_obj_t *pin, const digitalio_pull_t pull) {
4243
if (!pin->has_touch) {
4344
raise_ValueError_invalid_pin();
4445
}

ports/espressif/common-hal/touchio/TouchIn.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
#include "py/runtime.h"
1010
#include "peripherals/touch.h"
1111
#include "shared-bindings/microcontroller/Pin.h"
12+
#include "shared-bindings/digitalio/Pull.h"
1213

1314
void common_hal_touchio_touchin_construct(touchio_touchin_obj_t *self,
14-
const mcu_pin_obj_t *pin) {
15+
const mcu_pin_obj_t *pin, const digitalio_pull_t pull) {
1516
if (pin->touch_channel == NO_TOUCH_CHANNEL) {
1617
raise_ValueError_invalid_pin();
1718
}

ports/raspberrypi/mpconfigport.mk

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@ CIRCUITPY_ALARM = 0
6464
# Default PICODVI on because it doesn't require much code in RAM to talk to HSTX.
6565
CIRCUITPY_PICODVI ?= 1
6666

67-
# Our generic touchio uses a pull down and RP2350 A2 hardware doesn't work correctly.
68-
# So, turn touchio off because it doesn't work.
69-
CIRCUITPY_TOUCHIO = 0
70-
7167
# delay in ms before calling cyw43_arch_init_with_country
7268
CIRCUITPY_CYW43_INIT_DELAY ?= 0
7369

shared-bindings/touchio/TouchIn.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,30 @@
3131
//| print("touched!")"""
3232
//|
3333

34-
//| def __init__(self, pin: microcontroller.Pin) -> None:
34+
//| def __init__(self, pin: microcontroller.Pin, pull: Optional[digitalio.Pull] = None) -> None:
3535
//| """Use the TouchIn on the given pin.
3636
//|
37-
//| :param ~microcontroller.Pin pin: the pin to read from"""
37+
//| :param ~microcontroller.Pin pin: the pin to read from
38+
//| :param Optional[digitalio.Pull] pull: specify external pull resistor type. If None, assume pull-down or chip-specific implementation that does not require a pull.
39+
//| """
3840
//| ...
3941
//|
4042
static mp_obj_t touchio_touchin_make_new(const mp_obj_type_t *type,
41-
size_t n_args, size_t n_kw, const mp_obj_t *args) {
42-
// check number of arguments
43-
mp_arg_check_num(n_args, n_kw, 1, 1, false);
43+
size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
4444

45-
// 1st argument is the pin
46-
const mcu_pin_obj_t *pin = validate_obj_is_free_pin(args[0], MP_QSTR_pin);
45+
enum { ARG_pin, ARG_pull };
46+
static const mp_arg_t allowed_args[] = {
47+
{ MP_QSTR_pin, MP_ARG_OBJ | MP_ARG_REQUIRED },
48+
{ MP_QSTR_pull, MP_ARG_OBJ, {.u_obj = mp_const_none} },
49+
};
50+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
51+
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
52+
53+
const mcu_pin_obj_t *pin = validate_obj_is_free_pin(args[ARG_pin].u_obj, MP_QSTR_pin);
54+
const digitalio_pull_t pull = validate_pull(args[ARG_pull].u_obj, MP_QSTR_pull);
4755

4856
touchio_touchin_obj_t *self = mp_obj_malloc(touchio_touchin_obj_t, &touchio_touchin_type);
49-
common_hal_touchio_touchin_construct(self, pin);
57+
common_hal_touchio_touchin_construct(self, pin, pull);
5058

5159
return (mp_obj_t)self;
5260
}

shared-bindings/touchio/TouchIn.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#pragma once
88

99
#include "common-hal/microcontroller/Pin.h"
10+
#include "shared-bindings/digitalio/Pull.h"
1011

1112
#if CIRCUITPY_TOUCHIO_USE_NATIVE
1213
#include "common-hal/touchio/TouchIn.h"
@@ -16,7 +17,7 @@
1617

1718
extern const mp_obj_type_t touchio_touchin_type;
1819

19-
void common_hal_touchio_touchin_construct(touchio_touchin_obj_t *self, const mcu_pin_obj_t *pin);
20+
void common_hal_touchio_touchin_construct(touchio_touchin_obj_t *self, const mcu_pin_obj_t *pin, digitalio_pull_t pull);
2021
void common_hal_touchio_touchin_deinit(touchio_touchin_obj_t *self);
2122
bool common_hal_touchio_touchin_deinited(touchio_touchin_obj_t *self);
2223
bool common_hal_touchio_touchin_get_value(touchio_touchin_obj_t *self);

shared-bindings/touchio/__init__.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@
2828
//| For more information about working with the `touchio` module in CircuitPython,
2929
//| see `this Learn guide page <https://learn.adafruit.com/circuitpython-essentials/circuitpython-cap-touch>`_.
3030
//|
31-
//| **Limitations**: `touchio` is available on Raspberry Pi RP2040 builds,
32-
//| but not on RP2350, due to GPIO hardware limitations.
31+
//| **Limitations**: `touchio` on RP2350 must have a pull-up resistor to 3.3V
32+
//| instead of ground and set the ``pull=Pull.UP`` parameter when constructing
33+
//| a `TouchIn` object, due to GPIO hardware limitations.
3334
//|
3435
//| Example::
3536
//|
@@ -40,7 +41,8 @@
4041
//| print(touch_pin.value)
4142
//|
4243
//| This example will initialize the the device, and print the
43-
//| :py:data:`~touchio.TouchIn.value`."""
44+
//| :py:data:`~touchio.TouchIn.value`.
45+
//| """
4446

4547
static const mp_rom_map_elem_t touchio_module_globals_table[] = {
4648
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_touchio) },

shared-module/touchio/TouchIn.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,38 @@
1212
#include "py/mphal.h"
1313
#include "shared-bindings/touchio/TouchIn.h"
1414
#include "shared-bindings/microcontroller/Pin.h"
15+
#include "shared-bindings/digitalio/Pull.h"
1516

16-
// This is a capacitive touch sensing routine using a single digital
17-
// pin. The pin should be connected to the sensing pad, and to ground
17+
// This is a capacitive touch sensing routine using a single digital pin.
18+
// For pull==PULL_DOWN, the pin should be connected to the sensing pad and to ground
1819
// via a 1Mohm or thereabout drain resistor. When a reading is taken,
1920
// the pin's capacitance is charged by setting it to a digital output
2021
// 'high' for a few microseconds, and then it is changed to a high
2122
// impedance input. We measure how long it takes to discharge through
2223
// the resistor (around 50us), using a busy-waiting loop, and average
2324
// over N_SAMPLES cycles to reduce the effects of noise.
25+
// For the pull=PULL_UP case, the 1M resistor is connected to 3v3, the pin is
26+
// driven 'low' then measure how long it takes to go 'high'.
2427

2528
#define N_SAMPLES 10
2629
#define TIMEOUT_TICKS 10000
2730

2831
static uint16_t get_raw_reading(touchio_touchin_obj_t *self) {
2932

3033
uint16_t ticks = 0;
31-
34+
// state to charge pin to: if pull-down or None, pull HIGH, if pull-up, pull LOW
35+
bool pincharge = !(self->pull == PULL_UP);
3236
for (uint16_t i = 0; i < N_SAMPLES; i++) {
33-
// set pad to digital output high for 10us to charge it
37+
// set pad to digital output 'pincharge' for 10us to charge it
3438

35-
common_hal_digitalio_digitalinout_switch_to_output(self->digitalinout, true, DRIVE_MODE_PUSH_PULL);
39+
common_hal_digitalio_digitalinout_switch_to_output(self->digitalinout, pincharge, DRIVE_MODE_PUSH_PULL);
3640
mp_hal_delay_us(10);
3741

3842
// set pad back to an input and take some samples
3943

4044
common_hal_digitalio_digitalinout_switch_to_input(self->digitalinout, PULL_NONE);
4145

42-
while (common_hal_digitalio_digitalinout_get_value(self->digitalinout)) {
46+
while (common_hal_digitalio_digitalinout_get_value(self->digitalinout) == pincharge) {
4347
if (ticks >= TIMEOUT_TICKS) {
4448
return TIMEOUT_TICKS;
4549
}
@@ -49,16 +53,22 @@ static uint16_t get_raw_reading(touchio_touchin_obj_t *self) {
4953
return ticks;
5054
}
5155

52-
void common_hal_touchio_touchin_construct(touchio_touchin_obj_t *self, const mcu_pin_obj_t *pin) {
56+
void common_hal_touchio_touchin_construct(touchio_touchin_obj_t *self, const mcu_pin_obj_t *pin, const digitalio_pull_t pull) {
5357
common_hal_mcu_pin_claim(pin);
5458
self->digitalinout = mp_obj_malloc(digitalio_digitalinout_obj_t, &digitalio_digitalinout_type);
5559

5660
common_hal_digitalio_digitalinout_construct(self->digitalinout, pin);
5761

62+
self->pull = pull;
63+
5864
uint16_t raw_reading = get_raw_reading(self);
5965
if (raw_reading == TIMEOUT_TICKS) {
6066
common_hal_touchio_touchin_deinit(self);
61-
mp_raise_ValueError(MP_ERROR_TEXT("No pulldown on pin; 1Mohm recommended"));
67+
if (self->pull == PULL_UP) {
68+
mp_raise_ValueError(MP_ERROR_TEXT("No pullup on pin; 1Mohm recommended"));
69+
} else {
70+
mp_raise_ValueError(MP_ERROR_TEXT("No pulldown on pin; 1Mohm recommended"));
71+
}
6272
}
6373
self->threshold = raw_reading * 1.05 + 100;
6474
}

shared-module/touchio/TouchIn.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
typedef struct {
1616
mp_obj_base_t base;
1717
digitalio_digitalinout_obj_t *digitalinout;
18+
digitalio_pull_t pull;
1819
uint16_t threshold;
1920
} touchio_touchin_obj_t;
2021

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy