8000 added touchio pullup for RP2350 · tannewt/circuitpython@a11217a · GitHub
[go: up one dir, main page]

Skip to content

Commit a11217a

Browse files
committed
added touchio pullup for RP2350
1 parent ad73d0b commit a11217a

File tree

5 files changed

+142
-2
lines changed

5 files changed

+142
-2
lines changed

locale/circuitpython.pot

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1555,6 +1555,10 @@ msgstr ""
15551555
msgid "No pulldown on pin; 1Mohm recommended"
15561556
msgstr ""
15571557

1558+
#: ports/raspberrypi/common-hal/touchio/TouchIn.c
1559+
msgid "No pullup on pin; 1Mohm recommended"
1560+
msgstr ""
1561+
15581562
#: py/moderrno.c
15591563
msgid "No space left on device"
15601564
msgstr ""
@@ -2577,7 +2581,8 @@ msgstr ""
25772581
msgid "bits must be 32 or less"
25782582
msgstr ""
25792583

2580-
#: shared-bindings/audiodelays/Echo.c shared-bindings/audiodelays/PitchShift.c
2584+
#: shared-bindings/audiodelays/Chorus.c shared-bindings/audiodelays/Echo.c
2585+
#: shared-bindings/audiodelays/PitchShift.c
25812586
#: shared-bindings/audiofilters/Distortion.c
25822587
#: shared-bindings/audiofilters/Filter.c shared-bindings/audiomixer/Mixer.c
25832588
msgid "bits_per_sample must be 8 or 16"
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// This file is part of the CircuitPython project: https://circuitpython.org
2+
//
3+
// SPDX-FileCopyrightText: Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
4+
// SPDX-FileCopyrightText: Copyright (c) 2018 Nick Moore for Adafruit Industries
5+
//
6+
// SPDX-License-Identifier: MIT
7+
8+
#include <string.h>
9+
#include <stdio.h>
10+
11+
#include "py/runtime.h"
12+
#include "py/mphal.h"
13+
#include "shared-bindings/touchio/TouchIn.h"
14+
#include "shared-bindings/microcontroller/Pin.h"
15+
16+
// Native touchio only for RP2350, which cannot do shared-module touchio
17+
#ifdef PICO_RP2350
18+
19+
// This is a capacitive touch sensing routine using a single digital
20+
// pin. The pin should be connected to the sensing pad, and to ground
21+
// via a 1Mohm or thereabout drain resistor. When a reading is taken,
22+
// the pin's capacitance is charged by setting it to a digital output
23+
// 'high' for a few microseconds, and then it is changed to a high
24+
// impedance input. We measure how long it takes to discharge through
25+
// the resistor (around 50us), using a busy-waiting loop, and average
26+
// over N_SAMPLES cycles to reduce the effects of noise.
27+
28+
#define N_SAMPLES 10
29+
#define TIMEOUT_TICKS 10000
30+
31+
static uint16_t get_raw_reading(touchio_touchin_obj_t *self) {
32+
33+
uint16_t ticks = 0;
34+
35+
for (uint16_t i = 0; i < N_SAMPLES; i++) {
36+
// set pad to digital output LOW for 10us to charge it
37+
38+
common_hal_digitalio_digitalinout_switch_to_output(self->digitalinout, false, DRIVE_MODE_PUSH_PULL);
39+
mp_hal_delay_us(10);
40+
41+
// set pad back to an input and take some samples, waiting for it to go HIGH
42+
43+
common_hal_digitalio_digitalinout_switch_to_input(self->digitalinout, PULL_NONE);
44+
45+
while (!common_hal_digitalio_digitalinout_get_value(self->digitalinout)) {
46+
if (ticks >= TIMEOUT_TICKS) {
47+
return TIMEOUT_TICKS;
48+
}
49+
ticks++;
50+
}
51+
}
52+
return ticks;
53+
}
54+
55+
void common_hal_touchio_touchin_construct(touchio_touchin_obj_t *self, const mcu_pin_obj_t *pin) {
56+
common_hal_mcu_pin_claim(pin);
57+
self->digitalinout = mp_obj_malloc(digitalio_digitalinout_obj_t, &digitalio_digitalinout_type);
58+
59+
common_hal_digitalio_digitalinout_construct(self->digitalinout, pin);
60+
61+
uint16_t raw_reading = get_raw_reading(self);
62+
if (raw_reading == TIMEOUT_TICKS) {
63+
common_hal_touchio_touchin_deinit(self);
64+
mp_raise_ValueError(MP_ERROR_TEXT("No pullup on pin; 1Mohm recommended"));
65+
}
66+
self->threshold = raw_reading * 1.05 + 100;
67+
}
68+
69+
bool common_hal_touchio_touchin_deinited(touchio_touchin_obj_t *self) {
70+
return self->digitalinout == MP_OBJ_NULL;
71+
}
72+
73+
void common_hal_touchio_touchin_deinit(touchio_touchin_obj_t *self) {
74+
if (common_hal_touchio_touchin_deinited(self)) {
75+
return;
76+
}
77+
78+
common_hal_digitalio_digitalinout_deinit(self->digitalinout);
79+
self->digitalinout = MP_OBJ_NULL;
80+
}
81+
82+
void touchin_reset() {
83+
}
84+
85+
bool common_hal_touchio_touchin_get_value(touchio_touchin_obj_t *self) {
86+
uint16_t reading = get_raw_reading(self);
87+
return reading > self->threshold;
88+
}
89+
90+
uint16_t common_hal_touchio_touchin_get_raw_value(touchio_touchin_obj_t *self) {
91+
return get_raw_reading(self);
92+
}
93+
94+
uint16_t common_hal_touchio_touchin_get_threshold(touchio_touchin_obj_t *self) {
95+
return self->threshold;
96+
}
97+
98+
void common_hal_touchio_touchin_set_threshold(touchio_touchin_obj_t *self,
99+
uint16_t new_threshold) {
100+
self->threshold = new_threshold;
101+
}
102+
103+
#endif
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// This file is part of the CircuitPython project: https://circuitpython.org
2+
//
3+
// SPDX-FileCopyrightText: Copyright (c) 2016 Scott Shawcroft
4+
// SPDX-FileCopyrightText: Copyright (c) 2018 Nick Moore for Adafruit Industries
5+
//
6+
// SPDX-License-Identifier: MIT
7+
8+
#pragma once
9+
10+
#include "common-hal/microcontroller/Pin.h"
11+
#include "shared-bindings/digitalio/DigitalInOut.h"
12+
13+
#include "py/obj.h"
14+
15+
// Native touchio only for RP2350, which cannot do shared-module touchio
16+
#ifdef PICO_RP2350
17+
18+
typedef struct {
19+
mp_obj_base_t base;
20+
digitalio_digitalinout_obj_t *digitalinout;
21+
uint16_t threshold;
22+
} touchio_touchin_obj_t;
23+
24+
void touchin_reset(void);
25+
26+
#endif
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// This file is part of the CircuitPython project: https://circuitpython.org
2+
//
3+
// SPDX-FileCopyrightText: Copyright (c) 2024 Adafruit Industries LLC
4+
//
5+
// SPDX-License-Identifier: MIT

ports/raspberrypi/mpconfigport.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ CIRCUITPY_PICODVI ?= 1
6666

6767
# Our generic touchio uses a pull down and RP2350 A2 hardware doesn't work correctly.
6868
# So, turn touchio off because it doesn't work.
69-
CIRCUITPY_TOUCHIO = 0
69+
CIRCUITPY_TOUCHIO = 1
70+
CIRCUITPY_TOUCHIO_USE_NATIVE ?= 1
7071

7172
# delay in ms before calling cyw43_arch_init_with_country
7273
CIRCUITPY_CYW43_INIT_DELAY ?= 0

0 commit comments

Comments
 (0)
0