Merge pull request #8003 from dhalbert/esp-touch-alarm-fix

Esp touch alarm fix
This commit is contained in:
Scott Shawcroft 2023-05-22 13:43:54 -07:00 committed by GitHub
commit 9040ac8bc6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 7 deletions

View File

@ -44,6 +44,7 @@ void common_hal_alarm_touch_touchalarm_construct(alarm_touch_touchalarm_obj_t *s
self->pin = pin;
}
// Used for light sleep.
mp_obj_t alarm_touch_touchalarm_find_triggered_alarm(const size_t n_alarms, const mp_obj_t *alarms) {
for (size_t i = 0; i < n_alarms; i++) {
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
}
touch_alarm = MP_OBJ_TO_PTR(alarms[i]);
touch_channel_mask |= 1 << touch_alarm->pin->number;
// Resetting the pin will set a pull-up, which we don't want.
skip_reset_once_pin_number(touch_alarm->pin->number);
touch_alarm_set = true;
}
}
@ -128,8 +131,11 @@ void alarm_touch_touchalarm_set_alarm(const bool deep_sleep, const size_t n_alar
// configure trigger threshold
#if defined(CONFIG_IDF_TARGET_ESP32)
uint16_t touch_value;
// ESP32 touch_pad_read() returns a lower value when a pin is touched, not a higher value
// Typical values on a Feather ESP32 V2 are 600 with a short jumper untouched,
// 70 touched.
touch_pad_read(touch_channel, &touch_value);
touch_pad_set_thresh(touch_channel, touch_value / 10); // 10%
touch_pad_set_thresh(touch_channel, touch_value / 2);
#else
uint32_t touch_value;
touch_pad_read_benchmark(touch_channel, &touch_value);
@ -181,8 +187,11 @@ void alarm_touch_touchalarm_prepare_for_deep_sleep(void) {
// configure trigger threshold
#if defined(CONFIG_IDF_TARGET_ESP32)
uint16_t touch_value;
touch_pad_read_filtered(touch_channel, &touch_value);
touch_pad_set_thresh(touch_channel, touch_value);
touch_pad_read(touch_channel, &touch_value);
// ESP32 touch_pad_read() returns a lower value when a pin is touched, not a higher value
// Typical values on a Feather ESP32 V2 are 600 with a short jumper untouched,
// 70 touched.
touch_pad_set_thresh(touch_channel, touch_value / 2);
#else
uint32_t touch_value;
touch_pad_sleep_channel_read_smooth(touch_channel, &touch_value);

View File

@ -34,6 +34,7 @@
#include "components/hal/include/hal/gpio_hal.h"
STATIC uint64_t _never_reset_pin_mask;
STATIC uint64_t _skip_reset_once_pin_mask;
STATIC uint64_t _preserved_pin_mask;
STATIC uint64_t _in_use_pin_mask;
@ -112,6 +113,15 @@ void never_reset_pin_number(gpio_num_t pin_number) {
_never_reset_pin_mask |= PIN_BIT(pin_number);
}
void skip_reset_once_pin_number(gpio_num_t pin_number) {
// Some CircuitPython APIs deal in uint8_t pin numbers, but NO_PIN is -1.
// Also allow pin 255 to be treated as NO_PIN to avoid crashes
if (pin_number == NO_PIN || pin_number == (uint8_t)NO_PIN) {
return;
}
_skip_reset_once_pin_mask |= PIN_BIT(pin_number);
}
void common_hal_never_reset_pin(const mcu_pin_obj_t *pin) {
if (pin == NULL) {
return;
@ -131,6 +141,10 @@ STATIC bool _never_reset(gpio_num_t pin_number) {
return _never_reset_pin_mask & PIN_BIT(pin_number);
}
STATIC bool _skip_reset_once(gpio_num_t pin_number) {
return _skip_reset_once_pin_mask & PIN_BIT(pin_number);
}
STATIC bool _preserved_pin(gpio_num_t pin_number) {
return _preserved_pin_mask & PIN_BIT(pin_number);
}
@ -224,12 +238,15 @@ void reset_all_pins(void) {
uint32_t iomux_address = GPIO_PIN_MUX_REG[i];
if (iomux_address == 0 ||
_never_reset(i) ||
_skip_reset_once(i) ||
_preserved_pin(i)) {
continue;
}
_reset_pin(i);
}
_in_use_pin_mask = _never_reset_pin_mask;
// Don't continue to skip resetting these pins.
_skip_reset_once_pin_mask = 0;
}
void claim_pin_number(gpio_num_t pin_number) {

View File

@ -41,6 +41,7 @@ extern void reset_all_pins(void);
// reset_pin_number takes the pin number instead of the pointer so that objects don't
// need to store a full pointer.
extern void reset_pin_number(gpio_num_t pin_number);
extern void skip_reset_once_pin_number(gpio_num_t pin_number);
extern void claim_pin(const mcu_pin_obj_t *pin);
extern void claim_pin_number(gpio_num_t pin_number);
extern bool pin_number_is_free(gpio_num_t pin_number);

View File

@ -24,6 +24,7 @@
* THE SOFTWARE.
*/
#include "py/gc.h"
#include "peripherals/touch.h"
static bool touch_inited = false;
@ -43,7 +44,7 @@ void peripherals_touch_never_reset(const bool enable) {
void peripherals_touch_init(const touch_pad_t touchpad) {
if (!touch_inited) {
touch_pad_init();
touch_pad_set_fsm_mode(TOUCH_FSM_MODE_SW);
touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
}
// touch_pad_config() must be done before touch_pad_fsm_start() the first time.
// 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) {
#if defined(CONFIG_IDF_TARGET_ESP32)
uint16_t touch_value;
touch_pad_read(touchpad, &touch_value);
// ESP32 touch_pad_read() returns a lower value when a pin is touched instead of a higher value.
// Flip the values around to be consistent with TouchIn assumptions.
return UINT16_MAX - touch_value;
#else
uint32_t touch_value;
touch_pad_sw_start();
while (!touch_pad_meas_is_done()) {
}
touch_pad_read_raw_data(touchpad, &touch_value);
if (touch_value > UINT16_MAX) {
return UINT16_MAX;