add pin_skip_reset_once capability

This commit is contained in:
Dan Halbert 2023-05-19 15:05:44 -04:00
parent 9e995a5e98
commit d755238005
4 changed files with 30 additions and 9 deletions

View File

@ -77,8 +77,6 @@ mp_obj_t alarm_touch_touchalarm_record_wake_alarm(void) {
const mcu_pin_obj_t *pin_obj = MP_OBJ_TO_PTR(mcu_pin_globals.map.table[i].value);
if (pin_obj->touch_channel == wake_channel) {
alarm->pin = mcu_pin_globals.map.table[i].value;
// Undo the never reset in case it was a fake deep sleep.
reset_pin_number(alarm->pin->number);
break;
}
}
@ -105,7 +103,7 @@ 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.
never_reset_pin_number(touch_alarm->pin->number);
skip_reset_once_pin_number(touch_alarm->pin->number);
touch_alarm_set = true;
}
}
@ -133,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);
@ -186,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;
@ -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;