Skip to content

Commit d755238

Browse files
committed
add pin_skip_reset_once capability
1 parent 9e995a5 commit d755238

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ mp_obj_t alarm_touch_touchalarm_record_wake_alarm(void) {
7777
const mcu_pin_obj_t *pin_obj = MP_OBJ_TO_PTR(mcu_pin_globals.map.table[i].value);
7878
if (pin_obj->touch_channel == wake_channel) {
7979
alarm->pin = mcu_pin_globals.map.table[i].value;
80-
// Undo the never reset in case it was a fake deep sleep.
81-
reset_pin_number(alarm->pin->number);
8280
break;
8381
}
8482
}
@@ -105,7 +103,7 @@ void alarm_touch_touchalarm_set_alarm(const bool deep_sleep, const size_t n_alar
105103
touch_alarm = MP_OBJ_TO_PTR(alarms[i]);
106104
touch_channel_mask |= 1 << touch_alarm->pin->number;
107105
// Resetting the pin will set a pull-up, which we don't want.
108-
never_reset_pin_number(touch_alarm->pin->number);
106+
skip_reset_once_pin_number(touch_alarm->pin->number);
109107
touch_alarm_set = true;
110108
}
111109
}
@@ -133,8 +131,11 @@ void alarm_touch_touchalarm_set_alarm(const bool deep_sleep, const size_t n_alar
133131
// configure trigger threshold
134132
#if defined(CONFIG_IDF_TARGET_ESP32)
135133
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.
136137
touch_pad_read(touch_channel, &touch_value);
137-
touch_pad_set_thresh(touch_channel, touch_value / 10); // 10%
138+
touch_pad_set_thresh(touch_channel, touch_value / 2);
138139
#else
139140
uint32_t touch_value;
140141
touch_pad_read_benchmark(touch_channel, &touch_value);
@@ -186,8 +187,11 @@ void alarm_touch_touchalarm_prepare_for_deep_sleep(void) {
186187
// configure trigger threshold
187188
#if defined(CONFIG_IDF_TARGET_ESP32)
188189
uint16_t touch_value;
189-
touch_pad_read_filtered(touch_channel, &touch_value);
190-
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);
191195
#else
192196
uint32_t touch_value;
193197
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: 2 additions & 3 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;
@@ -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)