Skip to content

Commit 557a58b

Browse files
authored
Merge pull request #3591 from microDev1/TouchIO-S2
ESP32S2: Support for native TouchIO
2 parents 73162bd + ef97ed6 commit 557a58b

File tree

6 files changed

+192
-44
lines changed

6 files changed

+192
-44
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
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 "shared-bindings/touchio/TouchIn.h"
28+
#include "py/runtime.h"
29+
30+
#include "driver/touch_pad.h"
31+
32+
static uint16_t get_raw_reading(touchio_touchin_obj_t *self) {
33+
uint32_t touch_value;
34+
touch_pad_read_raw_data((touch_pad_t)self->pin->touch_channel, &touch_value);
35+
if (touch_value > UINT16_MAX) {
36+
return UINT16_MAX;
37+
}
38+
return touch_value;
39+
}
40+
41+
void common_hal_touchio_touchin_construct(touchio_touchin_obj_t* self,
42+
const mcu_pin_obj_t *pin) {
43+
if (pin->touch_channel == TOUCH_PAD_MAX) {
44+
mp_raise_ValueError(translate("Invalid pin"));
45+
}
46+
claim_pin(pin);
47+
48+
touch_pad_init();
49+
touch_pad_config((touch_pad_t)pin->touch_channel);
50+
51+
touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
52+
touch_pad_fsm_start();
53+
54+
// wait for "raw data" to reset
55+
mp_hal_delay_ms(10);
56+
57+
// Initial values for pins will vary, depending on what peripherals the pins
58+
// share on-chip.
59+
60+
// Set a "touched" threshold not too far above the initial value.
61+
// For simple finger touch, the values may vary as much as a factor of two,
62+
// but for touches using fruit or other objects, the difference is much less.
63+
64+
self->pin = pin;
65+
self->threshold = get_raw_reading(self) + 100;
66+
}
67+
68+
bool common_hal_touchio_touchin_deinited(touchio_touchin_obj_t* self) {
69+
return self->pin == NULL;
70+
}
71+
72+
void common_hal_touchio_touchin_deinit(touchio_touchin_obj_t* self) {
73+
if (common_hal_touchio_touchin_deinited(self)) {
74+
return;
75+
}
76+
touch_pad_deinit();
77+
reset_pin_number(self->pin->touch_channel);
78+
self->pin = NULL;
79+
}
80+
81+
bool common_hal_touchio_touchin_get_value(touchio_touchin_obj_t *self) {
82+
uint16_t reading = get_raw_reading(self);
83+
return reading > self->threshold;
84+
}
85+
86+
uint16_t common_hal_touchio_touchin_get_raw_value(touchio_touchin_obj_t *self) {
87+
return get_raw_reading(self);
88+
}
89+
90+
uint16_t common_hal_touchio_touchin_get_threshold(touchio_touchin_obj_t *self) {
91+
return self->threshold;
92+
}
93+
94+
void common_hal_touchio_touchin_set_threshold(touchio_touchin_obj_t *self, uint16_t new_threshold) {
95+
self->threshold = new_threshold;
96+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Scott Shawcroft
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_COMMON_HAL_TOUCHIO_TOUCHIN_H
28+
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_TOUCHIO_TOUCHIN_H
29+
30+
#include "common-hal/microcontroller/Pin.h"
31+
32+
#include "py/obj.h"
33+
34+
typedef struct {
35+
mp_obj_base_t base;
36+
const mcu_pin_obj_t * pin;
37+
uint16_t threshold;
38+
} touchio_touchin_obj_t;
39+
40+
void touchin_reset(void);
41+
42+
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_TOUCHIO_TOUCHIN_H
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// No touchio module functions.

ports/esp32s2/mpconfigport.mk

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@ CIRCUITPY_USB_MIDI = 0
2727
CIRCUITPY_WIFI = 1
2828
CIRCUITPY_ESPIDF = 1
2929

30+
ifndef CIRCUITPY_TOUCHIO_USE_NATIVE
31+
CIRCUITPY_TOUCHIO_USE_NATIVE = 1
32+
endif
33+
3034
CIRCUITPY_MODULE ?= none

ports/esp32s2/peripherals/pins.c

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -29,55 +29,58 @@
2929
#define NO_ADC 0
3030
#define NO_ADC_CHANNEL ADC_CHANNEL_MAX
3131

32+
#define NO_TOUCH_CHANNEL TOUCH_PAD_MAX
33+
3234
// This macro is used to simplify pin definition in boards/<board>/pins.c
33-
#define PIN(p_name, p_number, p_adc_index, p_adc_channel) \
35+
#define PIN(p_name, p_number, p_adc_index, p_adc_channel, p_touch_channel) \
3436
const mcu_pin_obj_t pin_## p_name = { \
3537
PIN_PREFIX_VALUES \
3638
.number = p_number, \
3739
.adc_index = p_adc_index, \
3840
.adc_channel = p_adc_channel, \
41+
.touch_channel = p_touch_channel, \
3942
}
4043

41-
PIN(GPIO0, 0, NO_ADC, NO_ADC_CHANNEL);
42-
PIN(GPIO1, 1, ADC_UNIT_1, ADC_CHANNEL_0);
43-
PIN(GPIO2, 2, ADC_UNIT_1, ADC_CHANNEL_1);
44-
PIN(GPIO3, 3, ADC_UNIT_1, ADC_CHANNEL_2);
45-
PIN(GPIO4, 4, ADC_UNIT_1, ADC_CHANNEL_3);
46-
PIN(GPIO5, 5, ADC_UNIT_1, ADC_CHANNEL_4);
47-
PIN(GPIO6, 6, ADC_UNIT_1, ADC_CHANNEL_5);
48-
PIN(GPIO7, 7, ADC_UNIT_1, ADC_CHANNEL_6);
49-
PIN(GPIO8, 8, ADC_UNIT_1, ADC_CHANNEL_7);
50-
PIN(GPIO9, 9, ADC_UNIT_1, ADC_CHANNEL_8);
51-
PIN(GPIO10, 10, ADC_UNIT_1, ADC_CHANNEL_9);
52-
PIN(GPIO11, 11, ADC_UNIT_2, ADC_CHANNEL_0);
53-
PIN(GPIO12, 12, ADC_UNIT_2, ADC_CHANNEL_1);
54-
PIN(GPIO13, 13, ADC_UNIT_2, ADC_CHANNEL_2);
55-
PIN(GPIO14, 14, ADC_UNIT_2, ADC_CHANNEL_3);
56-
PIN(GPIO15, 15, ADC_UNIT_2, ADC_CHANNEL_4);
57-
PIN(GPIO16, 16, ADC_UNIT_2, ADC_CHANNEL_5);
58-
PIN(GPIO17, 17, ADC_UNIT_2, ADC_CHANNEL_6);
59-
PIN(GPIO18, 18, ADC_UNIT_2, ADC_CHANNEL_7);
60-
PIN(GPIO19, 19, ADC_UNIT_2, ADC_CHANNEL_8);
61-
PIN(GPIO20, 20, ADC_UNIT_2, ADC_CHANNEL_9);
62-
PIN(GPIO21, 21, NO_ADC, NO_ADC_CHANNEL);
63-
PIN(GPIO26, 26, NO_ADC, NO_ADC_CHANNEL);
64-
PIN(GPIO27, 27, NO_ADC, NO_ADC_CHANNEL);
65-
PIN(GPIO28, 28, NO_ADC, NO_ADC_CHANNEL);
66-
PIN(GPIO29, 29, NO_ADC, NO_ADC_CHANNEL);
67-
PIN(GPIO30, 30, NO_ADC, NO_ADC_CHANNEL);
68-
PIN(GPIO31, 31, NO_ADC, NO_ADC_CHANNEL);
69-
PIN(GPIO32, 32, NO_ADC, NO_ADC_CHANNEL);
70-
PIN(GPIO33, 33, NO_ADC, NO_ADC_CHANNEL);
71-
PIN(GPIO34, 34, NO_ADC, NO_ADC_CHANNEL);
72-
PIN(GPIO35, 35, NO_ADC, NO_ADC_CHANNEL);
73-
PIN(GPIO36, 36, NO_ADC, NO_ADC_CHANNEL);
74-
PIN(GPIO37, 37, NO_ADC, NO_ADC_CHANNEL);
75-
PIN(GPIO38, 38, NO_ADC, NO_ADC_CHANNEL);
76-
PIN(GPIO39, 39, NO_ADC, NO_ADC_CHANNEL);
77-
PIN(GPIO40, 40, NO_ADC, NO_ADC_CHANNEL);
78-
PIN(GPIO41, 41, NO_ADC, NO_ADC_CHANNEL);
79-
PIN(GPIO42, 42, NO_ADC, NO_ADC_CHANNEL);
80-
PIN(GPIO43, 43, NO_ADC, NO_ADC_CHANNEL);
81-
PIN(GPIO44, 44, NO_ADC, NO_ADC_CHANNEL);
82-
PIN(GPIO45, 45, NO_ADC, NO_ADC_CHANNEL);
83-
PIN(GPIO46, 46, NO_ADC, NO_ADC_CHANNEL);
44+
PIN(GPIO0, 0, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL);
45+
PIN(GPIO1, 1, ADC_UNIT_1, ADC_CHANNEL_0, TOUCH_PAD_NUM1);
46+
PIN(GPIO2, 2, ADC_UNIT_1, ADC_CHANNEL_1, TOUCH_PAD_NUM2);
47+
PIN(GPIO3, 3, ADC_UNIT_1, ADC_CHANNEL_2, TOUCH_PAD_NUM3);
48+
PIN(GPIO4, 4, ADC_UNIT_1, ADC_CHANNEL_3, TOUCH_PAD_NUM4);
49+
PIN(GPIO5, 5, ADC_UNIT_1, ADC_CHANNEL_4, TOUCH_PAD_NUM5);
50+
PIN(GPIO6, 6, ADC_UNIT_1, ADC_CHANNEL_5, TOUCH_PAD_NUM6);
51+
PIN(GPIO7, 7, ADC_UNIT_1, ADC_CHANNEL_6, TOUCH_PAD_NUM7);
52+
PIN(GPIO8, 8, ADC_UNIT_1, ADC_CHANNEL_7, TOUCH_PAD_NUM8);
53+
PIN(GPIO9, 9, ADC_UNIT_1, ADC_CHANNEL_8, TOUCH_PAD_NUM9);
54+
PIN(GPIO10, 10, ADC_UNIT_1, ADC_CHANNEL_9, TOUCH_PAD_NUM10);
55+
PIN(GPIO11, 11, ADC_UNIT_2, ADC_CHANNEL_0, TOUCH_PAD_NUM11);
56+
PIN(GPIO12, 12, ADC_UNIT_2, ADC_CHANNEL_1, TOUCH_PAD_NUM12);
57+
PIN(GPIO13, 13, ADC_UNIT_2, ADC_CHANNEL_2, TOUCH_PAD_NUM13);
58+
PIN(GPIO14, 14, ADC_UNIT_2, ADC_CHANNEL_3, TOUCH_PAD_NUM14);
59+
PIN(GPIO15, 15, ADC_UNIT_2, ADC_CHANNEL_4, NO_TOUCH_CHANNEL);
60+
PIN(GPIO16, 16, ADC_UNIT_2, ADC_CHANNEL_5, NO_TOUCH_CHANNEL);
61+
PIN(GPIO17, 17, ADC_UNIT_2, ADC_CHANNEL_6, NO_TOUCH_CHANNEL);
62+
PIN(GPIO18, 18, ADC_UNIT_2, ADC_CHANNEL_7, NO_TOUCH_CHANNEL);
63+
PIN(GPIO19, 19, ADC_UNIT_2, ADC_CHANNEL_8, NO_TOUCH_CHANNEL);
64+
PIN(GPIO20, 20, ADC_UNIT_2, ADC_CHANNEL_9, NO_TOUCH_CHANNEL);
65+
PIN(GPIO21, 21, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL);
66+
PIN(GPIO26, 26, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL);
67+
PIN(GPIO27, 27, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL);
68+
PIN(GPIO28, 28, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL);
69+
PIN(GPIO29, 29, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL);
70+
PIN(GPIO30, 30, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL);
71+
PIN(GPIO31, 31, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL);
72+
PIN(GPIO32, 32, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL);
73+
PIN(GPIO33, 33, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL);
74+
PIN(GPIO34, 34, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL);
75+
PIN(GPIO35, 35, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL);
76+
PIN(GPIO36, 36, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL);
77+
PIN(GPIO37, 37, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL);
78+
PIN(GPIO38, 38, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL);
79+
PIN(GPIO39, 39, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL);
80+
PIN(GPIO40, 40, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL);
81+
PIN(GPIO41, 41, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL);
82+
PIN(GPIO42, 42, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL);
83+
PIN(GPIO43, 43, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL);
84+
PIN(GPIO44, 44, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL);
85+
PIN(GPIO45, 45, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL);
86+
PIN(GPIO46, 46, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL);

ports/esp32s2/peripherals/pins.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@
3737

3838
#include "components/hal/include/hal/gpio_types.h"
3939
#include "components/hal/include/hal/adc_types.h"
40+
#include "components/hal/include/hal/touch_sensor_types.h"
4041

4142
typedef struct {
4243
PIN_PREFIX_FIELDS
4344
gpio_num_t number;
4445
uint8_t adc_index:2;
4546
uint8_t adc_channel:6;
47+
uint8_t touch_channel;
4648
} mcu_pin_obj_t;
4749

4850
extern const mcu_pin_obj_t pin_GPIO0;

0 commit comments

Comments
 (0)