Skip to content

Commit eb0027b

Browse files
projectgusdpgeorge
authored andcommitted
esp32: Use hardware version for touchpad macro defines.
ESP32 has hardware V1 and S2/S3 has V2, and future chips may have different versions. This should still compile to the same binary before and after. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <[email protected]>
1 parent 03bc561 commit eb0027b

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

ports/esp32/machine_touchpad.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@
2929
#include "modmachine.h"
3030
#include "driver/gpio.h"
3131

32-
#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
32+
#if SOC_TOUCH_SENSOR_SUPPORTED
3333

34-
#if CONFIG_IDF_TARGET_ESP32
34+
#if SOC_TOUCH_VERSION_1 // ESP32 only
3535
#include "driver/touch_pad.h"
36-
#elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
36+
#elif SOC_TOUCH_VERSION_2 // All other SoCs with touch, to date
3737
#include "driver/touch_sensor.h"
38+
#else
39+
#error "Unknown touch hardware version"
3840
#endif
3941

4042
typedef struct _mtp_obj_t {
@@ -70,6 +72,8 @@ static const mtp_obj_t touchpad_obj[] = {
7072
{{&machine_touchpad_type}, GPIO_NUM_12, TOUCH_PAD_NUM12},
7173
{{&machine_touchpad_type}, GPIO_NUM_13, TOUCH_PAD_NUM13},
7274
{{&machine_touchpad_type}, GPIO_NUM_14, TOUCH_PAD_NUM14},
75+
#else
76+
#error "Please add GPIO mapping for this SoC"
7377
#endif
7478
};
7579

@@ -92,14 +96,14 @@ static mp_obj_t mtp_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_
9296
if (!initialized) {
9397
touch_pad_init();
9498
touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
95-
#if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
99+
#if TOUCH_HW_VER == 2
96100
touch_pad_fsm_start();
97101
#endif
98102
initialized = 1;
99103
}
100-
#if CONFIG_IDF_TARGET_ESP32
104+
#if SOC_TOUCH_VERSION_1
101105
esp_err_t err = touch_pad_config(self->touchpad_id, 0);
102-
#elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
106+
#elif SOC_TOUCH_VERSION_2
103107
esp_err_t err = touch_pad_config(self->touchpad_id);
104108
#endif
105109
if (err == ESP_OK) {
@@ -110,10 +114,10 @@ static mp_obj_t mtp_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_
110114

111115
static mp_obj_t mtp_config(mp_obj_t self_in, mp_obj_t value_in) {
112116
mtp_obj_t *self = self_in;
113-
#if CONFIG_IDF_TARGET_ESP32
117+
#if SOC_TOUCH_VERSION_1
114118
uint16_t value = mp_obj_get_int(value_in);
115119
esp_err_t err = touch_pad_config(self->touchpad_id, value);
116-
#elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
120+
#elif SOC_TOUCH_VERSION_2
117121
esp_err_t err = touch_pad_config(self->touchpad_id);
118122
#endif
119123
if (err == ESP_OK) {
@@ -125,10 +129,10 @@ MP_DEFINE_CONST_FUN_OBJ_2(mtp_config_obj, mtp_config);
125129

126130
static mp_obj_t mtp_read(mp_obj_t self_in) {
127131
mtp_obj_t *self = self_in;
128-
#if CONFIG_IDF_TARGET_ESP32
132+
#if SOC_TOUCH_VERSION_1
129133
uint16_t value;
130134
esp_err_t err = touch_pad_read(self->touchpad_id, &value);
131-
#elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
135+
#elif SOC_TOUCH_VERSION_2
132136
uint32_t value;
133137
esp_err_t err = touch_pad_read_raw_data(self->touchpad_id, &value);
134138
#endif
@@ -155,4 +159,4 @@ MP_DEFINE_CONST_OBJ_TYPE(
155159
locals_dict, &mtp_locals_dict
156160
);
157161

158-
#endif
162+
#endif // SOC_TOUCH_SENSOR_SUPPORTED

0 commit comments

Comments
 (0)