Skip to content

Commit a60fabd

Browse files
committed
add touch alarm support for esp32s2
1 parent 8eaf2b0 commit a60fabd

File tree

9 files changed

+150
-27
lines changed

9 files changed

+150
-27
lines changed

ports/esp32s2/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ SRC_C += \
192192
lib/utils/sys_stdio_mphal.c \
193193
lib/netutils/netutils.c \
194194
peripherals/timer.c \
195+
peripherals/touch.c \
195196
peripherals/pcnt.c \
196197
peripherals/pins.c \
197198
peripherals/rmt.c \

ports/esp32s2/common-hal/alarm/__init__.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ void common_hal_alarm_set_deep_sleep_alarms(size_t n_alarms, const mp_obj_t *ala
162162
}
163163

164164
void NORETURN alarm_enter_deep_sleep(void) {
165+
alarm_touch_touchalarm_prepare_for_deep_sleep();
165166
// The ESP-IDF caches the deep sleep settings and applies them before sleep.
166167
// We don't need to worry about resetting them in the interim.
167168
esp_deep_sleep_start();

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

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,81 @@
2525
*/
2626

2727
#include "shared-bindings/alarm/touch/TouchAlarm.h"
28+
#include "shared-bindings/microcontroller/__init__.h"
29+
30+
#include "peripherals/touch.h"
2831

2932
#include "esp_sleep.h"
3033

3134
void common_hal_alarm_touch_touchalarm_construct(alarm_touch_touchalarm_obj_t *self, const mcu_pin_obj_t *pin) {
32-
35+
if (pin->touch_channel == TOUCH_PAD_MAX) {
36+
mp_raise_ValueError(translate("Invalid pin"));
37+
}
38+
self->pin = pin;
3339
}
3440

3541
mp_obj_t alarm_touch_touchalarm_get_wakeup_alarm(size_t n_alarms, const mp_obj_t *alarms) {
36-
return mp_const_none;
42+
// First, check to see if we match any given alarms.
43+
for (size_t i = 0; i < n_alarms; i++) {
44+
if (MP_OBJ_IS_TYPE(alarms[i], &alarm_touch_touchalarm_type)) {
45+
return alarms[i];
46+
}
47+
}
48+
49+
gpio_num_t pin_number = esp_sleep_get_touchpad_wakeup_status();
50+
51+
alarm_touch_touchalarm_obj_t *alarm = m_new_obj(alarm_touch_touchalarm_obj_t);
52+
alarm->base.type = &alarm_touch_touchalarm_type;
53+
alarm->pin = NULL;
54+
55+
// Map the pin number back to a pin object.
56+
for (size_t i = 0; i < mcu_pin_globals.map.used; i++) {
57+
const mcu_pin_obj_t* pin_obj = MP_OBJ_TO_PTR(mcu_pin_globals.map.table[i].value);
58+
if (pin_obj->number == pin_number) {
59+
alarm->pin = mcu_pin_globals.map.table[i].value;
60+
break;
61+
}
62+
}
63+
64+
return alarm;
3765
}
3866

67+
static uint16_t sleep_touch_pin;
68+
3969
void alarm_touch_touchalarm_set_alarm(alarm_touch_touchalarm_obj_t *self) {
70+
sleep_touch_pin |= 1 << self->pin->number;
71+
esp_sleep_enable_touchpad_wakeup();
72+
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
73+
}
74+
75+
static void configure_sleep_touch_pin(touch_pad_t touch_channel) {
76+
// intialize touchpad
77+
peripherals_touch_init(touch_channel);
78+
79+
// configure touchpad for sleep
80+
touch_pad_sleep_channel_enable(touch_channel, true);
81+
touch_pad_sleep_channel_enable_proximity(touch_channel, false);
82+
83+
// wait for touch data to reset
84+
mp_hal_delay_ms(10);
4085

86+
uint32_t touch_value;
87+
touch_pad_sleep_channel_read_smooth(touch_channel, &touch_value);
88+
touch_pad_sleep_set_threshold(touch_channel, touch_value * 0.1); //10%
89+
}
90+
91+
void alarm_touch_touchalarm_prepare_for_deep_sleep(void) {
92+
for (uint8_t i = 1; i <= 14; i++) {
93+
if ((sleep_touch_pin & 1 << i) != 0) {
94+
configure_sleep_touch_pin((touch_pad_t)i);
95+
}
96+
}
4197
}
4298

4399
bool alarm_touch_touchalarm_woke_us_up(void) {
44100
return false;
45101
}
46102

47103
void alarm_touch_touchalarm_reset(void) {
48-
104+
sleep_touch_pin = 0;
49105
}

ports/esp32s2/common-hal/alarm/touch/TouchAlarm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ typedef struct {
3939
mp_obj_t alarm_touch_touchalarm_get_wakeup_alarm(size_t n_alarms, const mp_obj_t *alarms);
4040
// Check for the wake up alarm from pretend deep sleep.
4141
void alarm_touch_touchalarm_set_alarm(alarm_touch_touchalarm_obj_t *self);
42+
void alarm_touch_touchalarm_prepare_for_deep_sleep(void);
4243
bool alarm_touch_touchalarm_woke_us_up(void);
4344
void alarm_touch_touchalarm_reset(void);
4445

ports/esp32s2/common-hal/touchio/TouchIn.c

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,7 @@
2828

2929
#include "py/runtime.h"
3030
#include "driver/touch_pad.h"
31-
32-
bool touch_inited = false;
33-
34-
void touchin_reset(void) {
35-
if (touch_inited) {
36-
touch_pad_deinit();
37-
touch_inited = false;
38-
}
39-
}
31+
#include "peripherals/touch.h"
4032

4133
static uint16_t get_raw_reading(touchio_touchin_obj_t *self) {
4234
uint32_t touch_value;
@@ -54,16 +46,10 @@ void common_hal_touchio_touchin_construct(touchio_touchin_obj_t* self,
5446
}
5547
claim_pin(pin);
5648

57-
if (!touch_inited) {
58-
touch_pad_init();
59-
touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
60-
touch_pad_fsm_start();
61-
touch_inited = true;
62-
}
63-
64-
touch_pad_config((touch_pad_t)pin->touch_channel);
49+
// initialize touchpad
50+
peripherals_touch_init((touch_pad_t)pin->touch_channel);
6551

66-
// wait for "raw data" to reset
52+
// wait for touch data to reset
6753
mp_hal_delay_ms(10);
6854

6955
// Initial values for pins will vary, depending on what peripherals the pins

ports/esp32s2/common-hal/touchio/TouchIn.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,13 @@
2727
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_TOUCHIO_TOUCHIN_H
2828
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_TOUCHIO_TOUCHIN_H
2929

30-
#include "common-hal/microcontroller/Pin.h"
31-
3230
#include "py/obj.h"
31+
#include "common-hal/microcontroller/Pin.h"
3332

3433
typedef struct {
3534
mp_obj_base_t base;
3635
const mcu_pin_obj_t * pin;
3736
uint16_t threshold;
3837
} touchio_touchin_obj_t;
3938

40-
void touchin_reset(void);
41-
4239
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_TOUCHIO_TOUCHIN_H

ports/esp32s2/peripherals/touch.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 microDev
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "peripherals/touch.h"
28+
29+
static bool touch_inited = false;
30+
31+
void peripherals_touch_reset(void) {
32+
if (touch_inited) {
33+
touch_pad_deinit();
34+
touch_inited = false;
35+
}
36+
}
37+
38+
void peripherals_touch_init(touch_pad_t touchpad) {
39+
if (!touch_inited) {
40+
touch_pad_init();
41+
touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
42+
touch_pad_fsm_start();
43+
touch_inited = true;
44+
}
45+
touch_pad_config(touchpad);
46+
}

ports/esp32s2/peripherals/touch.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 microDev
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_ESP32S2_PERIPHERALS_TOUCH_HANDLER_H
28+
#define MICROPY_INCLUDED_ESP32S2_PERIPHERALS_TOUCH_HANDLER_H
29+
30+
#include "driver/touch_pad.h"
31+
32+
extern void peripherals_touch_reset(void);
33+
extern void peripherals_touch_init(touch_pad_t touchpad);
34+
35+
#endif // MICROPY_INCLUDED_ESP32S2_PERIPHERALS_TOUCH_HANDLER_H

ports/esp32s2/supervisor/port.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
#include "common-hal/ps2io/Ps2.h"
4545
#include "common-hal/pulseio/PulseIn.h"
4646
#include "common-hal/pwmio/PWMOut.h"
47-
#include "common-hal/touchio/TouchIn.h"
4847
#include "common-hal/watchdog/WatchDogTimer.h"
4948
#include "common-hal/wifi/__init__.h"
5049
#include "supervisor/memory.h"
@@ -54,6 +53,7 @@
5453
#include "peripherals/rmt.h"
5554
#include "peripherals/pcnt.h"
5655
#include "peripherals/timer.h"
56+
#include "peripherals/touch.h"
5757
#include "components/esp_rom/include/esp_rom_uart.h"
5858
#include "components/heap/include/esp_heap_caps.h"
5959
#include "components/xtensa/include/esp_debug_helpers.h"
@@ -164,7 +164,7 @@ void reset_port(void) {
164164
#endif
165165

166166
#if CIRCUITPY_TOUCHIO_USE_NATIVE
167-
touchin_reset();
167+
peripherals_touch_reset();
168168
#endif
169169

170170
#if CIRCUITPY_WATCHDOG

0 commit comments

Comments
 (0)