add touch alarm support for esp32s2

This commit is contained in:
microDev 2020-12-19 12:56:34 +05:30
parent 8eaf2b0c19
commit a60fabdffa
No known key found for this signature in database
GPG Key ID: 2C0867BE60967730
9 changed files with 151 additions and 28 deletions

View File

@ -192,6 +192,7 @@ SRC_C += \
lib/utils/sys_stdio_mphal.c \
lib/netutils/netutils.c \
peripherals/timer.c \
peripherals/touch.c \
peripherals/pcnt.c \
peripherals/pins.c \
peripherals/rmt.c \

View File

@ -162,6 +162,7 @@ void common_hal_alarm_set_deep_sleep_alarms(size_t n_alarms, const mp_obj_t *ala
}
void NORETURN alarm_enter_deep_sleep(void) {
alarm_touch_touchalarm_prepare_for_deep_sleep();
// The ESP-IDF caches the deep sleep settings and applies them before sleep.
// We don't need to worry about resetting them in the interim.
esp_deep_sleep_start();

View File

@ -25,19 +25,75 @@
*/
#include "shared-bindings/alarm/touch/TouchAlarm.h"
#include "shared-bindings/microcontroller/__init__.h"
#include "peripherals/touch.h"
#include "esp_sleep.h"
void common_hal_alarm_touch_touchalarm_construct(alarm_touch_touchalarm_obj_t *self, const mcu_pin_obj_t *pin) {
if (pin->touch_channel == TOUCH_PAD_MAX) {
mp_raise_ValueError(translate("Invalid pin"));
}
self->pin = pin;
}
mp_obj_t alarm_touch_touchalarm_get_wakeup_alarm(size_t n_alarms, const mp_obj_t *alarms) {
return mp_const_none;
// First, check to see if we match any given alarms.
for (size_t i = 0; i < n_alarms; i++) {
if (MP_OBJ_IS_TYPE(alarms[i], &alarm_touch_touchalarm_type)) {
return alarms[i];
}
}
gpio_num_t pin_number = esp_sleep_get_touchpad_wakeup_status();
alarm_touch_touchalarm_obj_t *alarm = m_new_obj(alarm_touch_touchalarm_obj_t);
alarm->base.type = &alarm_touch_touchalarm_type;
alarm->pin = NULL;
// Map the pin number back to a pin object.
for (size_t i = 0; i < mcu_pin_globals.map.used; i++) {
const mcu_pin_obj_t* pin_obj = MP_OBJ_TO_PTR(mcu_pin_globals.map.table[i].value);
if (pin_obj->number == pin_number) {
alarm->pin = mcu_pin_globals.map.table[i].value;
break;
}
}
return alarm;
}
void alarm_touch_touchalarm_set_alarm(alarm_touch_touchalarm_obj_t *self) {
static uint16_t sleep_touch_pin;
void alarm_touch_touchalarm_set_alarm(alarm_touch_touchalarm_obj_t *self) {
sleep_touch_pin |= 1 << self->pin->number;
esp_sleep_enable_touchpad_wakeup();
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
}
static void configure_sleep_touch_pin(touch_pad_t touch_channel) {
// intialize touchpad
peripherals_touch_init(touch_channel);
// configure touchpad for sleep
touch_pad_sleep_channel_enable(touch_channel, true);
touch_pad_sleep_channel_enable_proximity(touch_channel, false);
// wait for touch data to reset
mp_hal_delay_ms(10);
uint32_t touch_value;
touch_pad_sleep_channel_read_smooth(touch_channel, &touch_value);
touch_pad_sleep_set_threshold(touch_channel, touch_value * 0.1); //10%
}
void alarm_touch_touchalarm_prepare_for_deep_sleep(void) {
for (uint8_t i = 1; i <= 14; i++) {
if ((sleep_touch_pin & 1 << i) != 0) {
configure_sleep_touch_pin((touch_pad_t)i);
}
}
}
bool alarm_touch_touchalarm_woke_us_up(void) {
@ -45,5 +101,5 @@ bool alarm_touch_touchalarm_woke_us_up(void) {
}
void alarm_touch_touchalarm_reset(void) {
sleep_touch_pin = 0;
}

View File

@ -39,6 +39,7 @@ typedef struct {
mp_obj_t alarm_touch_touchalarm_get_wakeup_alarm(size_t n_alarms, const mp_obj_t *alarms);
// Check for the wake up alarm from pretend deep sleep.
void alarm_touch_touchalarm_set_alarm(alarm_touch_touchalarm_obj_t *self);
void alarm_touch_touchalarm_prepare_for_deep_sleep(void);
bool alarm_touch_touchalarm_woke_us_up(void);
void alarm_touch_touchalarm_reset(void);

View File

@ -28,15 +28,7 @@
#include "py/runtime.h"
#include "driver/touch_pad.h"
bool touch_inited = false;
void touchin_reset(void) {
if (touch_inited) {
touch_pad_deinit();
touch_inited = false;
}
}
#include "peripherals/touch.h"
static uint16_t get_raw_reading(touchio_touchin_obj_t *self) {
uint32_t touch_value;
@ -54,16 +46,10 @@ void common_hal_touchio_touchin_construct(touchio_touchin_obj_t* self,
}
claim_pin(pin);
if (!touch_inited) {
touch_pad_init();
touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
touch_pad_fsm_start();
touch_inited = true;
}
// initialize touchpad
peripherals_touch_init((touch_pad_t)pin->touch_channel);
touch_pad_config((touch_pad_t)pin->touch_channel);
// wait for "raw data" to reset
// wait for touch data to reset
mp_hal_delay_ms(10);
// Initial values for pins will vary, depending on what peripherals the pins

View File

@ -27,9 +27,8 @@
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_TOUCHIO_TOUCHIN_H
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_TOUCHIO_TOUCHIN_H
#include "common-hal/microcontroller/Pin.h"
#include "py/obj.h"
#include "common-hal/microcontroller/Pin.h"
typedef struct {
mp_obj_base_t base;
@ -37,6 +36,4 @@ typedef struct {
uint16_t threshold;
} touchio_touchin_obj_t;
void touchin_reset(void);
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_TOUCHIO_TOUCHIN_H

View File

@ -0,0 +1,46 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 microDev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "peripherals/touch.h"
static bool touch_inited = false;
void peripherals_touch_reset(void) {
if (touch_inited) {
touch_pad_deinit();
touch_inited = false;
}
}
void peripherals_touch_init(touch_pad_t touchpad) {
if (!touch_inited) {
touch_pad_init();
touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
touch_pad_fsm_start();
touch_inited = true;
}
touch_pad_config(touchpad);
}

View File

@ -0,0 +1,35 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 microDev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_ESP32S2_PERIPHERALS_TOUCH_HANDLER_H
#define MICROPY_INCLUDED_ESP32S2_PERIPHERALS_TOUCH_HANDLER_H
#include "driver/touch_pad.h"
extern void peripherals_touch_reset(void);
extern void peripherals_touch_init(touch_pad_t touchpad);
#endif // MICROPY_INCLUDED_ESP32S2_PERIPHERALS_TOUCH_HANDLER_H

View File

@ -44,7 +44,6 @@
#include "common-hal/ps2io/Ps2.h"
#include "common-hal/pulseio/PulseIn.h"
#include "common-hal/pwmio/PWMOut.h"
#include "common-hal/touchio/TouchIn.h"
#include "common-hal/watchdog/WatchDogTimer.h"
#include "common-hal/wifi/__init__.h"
#include "supervisor/memory.h"
@ -54,6 +53,7 @@
#include "peripherals/rmt.h"
#include "peripherals/pcnt.h"
#include "peripherals/timer.h"
#include "peripherals/touch.h"
#include "components/esp_rom/include/esp_rom_uart.h"
#include "components/heap/include/esp_heap_caps.h"
#include "components/xtensa/include/esp_debug_helpers.h"
@ -164,7 +164,7 @@ void reset_port(void) {
#endif
#if CIRCUITPY_TOUCHIO_USE_NATIVE
touchin_reset();
peripherals_touch_reset();
#endif
#if CIRCUITPY_WATCHDOG