Skip to content

Commit 9040ac8

Browse files
authored
Merge pull request #8003 from dhalbert/esp-touch-alarm-fix
Esp touch alarm fix
2 parents 3f7e6db + d755238 commit 9040ac8

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
lines changed

ports/espressif/common-hal/alarm/touch/TouchAlarm.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ void common_hal_alarm_touch_touchalarm_construct(alarm_touch_touchalarm_obj_t *s
4444
self->pin = pin;
4545
}
4646

47+
// Used for light sleep.
4748
mp_obj_t alarm_touch_touchalarm_find_triggered_alarm(const size_t n_alarms, const mp_obj_t *alarms) {
4849
for (size_t i = 0; i < n_alarms; i++) {
4950
if (mp_obj_is_type(alarms[i], &alarm_touch_touchalarm_type)) {
@@ -101,6 +102,8 @@ void alarm_touch_touchalarm_set_alarm(const bool deep_sleep, const size_t n_alar
101102
}
102103
touch_alarm = MP_OBJ_TO_PTR(alarms[i]);
103104
touch_channel_mask |= 1 << touch_alarm->pin->number;
105+
// Resetting the pin will set a pull-up, which we don't want.
106+
skip_reset_once_pin_number(touch_alarm->pin->number);
104107
touch_alarm_set = true;
105108
}
106109
}
@@ -128,8 +131,11 @@ void alarm_touch_touchalarm_set_alarm(const bool deep_sleep, const size_t n_alar
128131
// configure trigger threshold
129132
#if defined(CONFIG_IDF_TARGET_ESP32)
130133
uint16_t touch_value;
134+
// ESP32 touch_pad_read() returns a lower value when a pin is touched, not a higher value
135+
// Typical values on a Feather ESP32 V2 are 600 with a short jumper untouched,
136+
// 70 touched.
131137
touch_pad_read(touch_channel, &touch_value);
132-
touch_pad_set_thresh(touch_channel, touch_value / 10); // 10%
138+
touch_pad_set_thresh(touch_channel, touch_value / 2);
133139
#else
134140
uint32_t touch_value;
135141
touch_pad_read_benchmark(touch_channel, &touch_value);
@@ -181,8 +187,11 @@ void alarm_touch_touchalarm_prepare_for_deep_sleep(void) {
181187
// configure trigger threshold
182188
#if defined(CONFIG_IDF_TARGET_ESP32)
183189
uint16_t touch_value;
184-
touch_pad_read_filtered(touch_channel, &touch_value);
185-
touch_pad_set_thresh(touch_channel, touch_value);
190+
touch_pad_read(touch_channel, &touch_value);
191+
// ESP32 touch_pad_read() returns a lower value when a pin is touched, not a higher value
192+
// Typical values on a Feather ESP32 V2 are 600 with a short jumper untouched,
193+
// 70 touched.
194+
touch_pad_set_thresh(touch_channel, touch_value / 2);
186195
#else
187196
uint32_t touch_value;
188197
touch_pad_sleep_channel_read_smooth(touch_channel, &touch_value);

ports/espressif/common-hal/microcontroller/Pin.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "components/hal/include/hal/gpio_hal.h"
3535

3636
STATIC uint64_t _never_reset_pin_mask;
37+
STATIC uint64_t _skip_reset_once_pin_mask;
3738
STATIC uint64_t _preserved_pin_mask;
3839
STATIC uint64_t _in_use_pin_mask;
3940

@@ -112,6 +113,15 @@ void never_reset_pin_number(gpio_num_t pin_number) {
112113
_never_reset_pin_mask |= PIN_BIT(pin_number);
113114
}
114115

116+
void skip_reset_once_pin_number(gpio_num_t pin_number) {
117+
// Some CircuitPython APIs deal in uint8_t pin numbers, but NO_PIN is -1.
118+
// Also allow pin 255 to be treated as NO_PIN to avoid crashes
119+
if (pin_number == NO_PIN || pin_number == (uint8_t)NO_PIN) {
120+
return;
121+
}
122+
_skip_reset_once_pin_mask |= PIN_BIT(pin_number);
123+
}
124+
115125
void common_hal_never_reset_pin(const mcu_pin_obj_t *pin) {
116126
if (pin == NULL) {
117127
return;
@@ -131,6 +141,10 @@ STATIC bool _never_reset(gpio_num_t pin_number) {
131141
return _never_reset_pin_mask & PIN_BIT(pin_number);
132142
}
133143

144+
STATIC bool _skip_reset_once(gpio_num_t pin_number) {
145+
return _skip_reset_once_pin_mask & PIN_BIT(pin_number);
146+
}
147+
134148
STATIC bool _preserved_pin(gpio_num_t pin_number) {
135149
return _preserved_pin_mask & PIN_BIT(pin_number);
136150
}
@@ -224,12 +238,15 @@ void reset_all_pins(void) {
224238
uint32_t iomux_address = GPIO_PIN_MUX_REG[i];
225239
if (iomux_address == 0 ||
226240
_never_reset(i) ||
241+
_skip_reset_once(i) ||
227242
_preserved_pin(i)) {
228243
continue;
229244
}
230245
_reset_pin(i);
231246
}
232247
_in_use_pin_mask = _never_reset_pin_mask;
248+
// Don't continue to skip resetting these pins.
249+
_skip_reset_once_pin_mask = 0;
233250
}
234251

235252
void claim_pin_number(gpio_num_t pin_number) {

ports/espressif/common-hal/microcontroller/Pin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ extern void reset_all_pins(void);
4141
// reset_pin_number takes the pin number instead of the pointer so that objects don't
4242
// need to store a full pointer.
4343
extern void reset_pin_number(gpio_num_t pin_number);
44+
extern void skip_reset_once_pin_number(gpio_num_t pin_number);
4445
extern void claim_pin(const mcu_pin_obj_t *pin);
4546
extern void claim_pin_number(gpio_num_t pin_number);
4647
extern bool pin_number_is_free(gpio_num_t pin_number);

ports/espressif/peripherals/touch.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include "py/gc.h"
2728
#include "peripherals/touch.h"
2829

2930
static bool touch_inited = false;
@@ -43,7 +44,7 @@ void peripherals_touch_never_reset(const bool enable) {
4344
void peripherals_touch_init(const touch_pad_t touchpad) {
4445
if (!touch_inited) {
4546
touch_pad_init();
46-
touch_pad_set_fsm_mode(TOUCH_FSM_MODE_SW);
47+
touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
4748
}
4849
// touch_pad_config() must be done before touch_pad_fsm_start() the first time.
4950
// Otherwise the calibration is wrong and we get maximum raw values if there is
@@ -61,14 +62,12 @@ uint16_t peripherals_touch_read(touch_pad_t touchpad) {
6162
#if defined(CONFIG_IDF_TARGET_ESP32)
6263
uint16_t touch_value;
6364
touch_pad_read(touchpad, &touch_value);
65+
6466
// ESP32 touch_pad_read() returns a lower value when a pin is touched instead of a higher value.
6567
// Flip the values around to be consistent with TouchIn assumptions.
6668
return UINT16_MAX - touch_value;
6769
#else
6870
uint32_t touch_value;
69-
touch_pad_sw_start();
70-
while (!touch_pad_meas_is_done()) {
71-
}
7271
touch_pad_read_raw_data(touchpad, &touch_value);
7372
if (touch_value > UINT16_MAX) {
7473
return UINT16_MAX;

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