Skip to content

Commit 6554704

Browse files
wmrsouzakartben
authored andcommitted
drivers: touch/rtc/wdt: esp32: fix conflict among device drivers
allows use of touch_sensor, rtc_counter, and wdt simultaneously by enabling ESP_INTR_FLAG_SHARED when calling esp_intr_alloc() Signed-off-by: Marcio Ribeiro <[email protected]>
1 parent f5946a5 commit 6554704

File tree

4 files changed

+46
-48
lines changed

4 files changed

+46
-48
lines changed

drivers/counter/counter_esp32_rtc.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,17 @@ static int counter_esp32_init(const struct device *dev)
5757
{
5858
const struct counter_esp32_config *cfg = dev->config;
5959
struct counter_esp32_data *data = dev->data;
60+
int ret, flags;
6061

6162
/* RTC_SLOW_CLK is the default clk source */
6263
clock_control_get_rate(cfg->clock_dev,
6364
(clock_control_subsys_t)ESP32_CLOCK_CONTROL_SUBSYS_RTC_SLOW,
6465
&data->clk_src_freq);
6566

66-
int ret = esp_intr_alloc(cfg->irq_source,
67-
ESP_PRIO_TO_FLAGS(cfg->irq_priority) |
68-
ESP_INT_FLAGS_CHECK(cfg->irq_flags),
69-
(ESP32_COUNTER_RTC_ISR_HANDLER)counter_esp32_isr,
70-
(void *)dev,
71-
NULL);
67+
flags = ESP_PRIO_TO_FLAGS(cfg->irq_priority) | ESP_INT_FLAGS_CHECK(cfg->irq_flags) |
68+
ESP_INTR_FLAG_SHARED;
69+
ret = esp_intr_alloc(cfg->irq_source, flags,
70+
(ESP32_COUNTER_RTC_ISR_HANDLER)counter_esp32_isr, (void *)dev, NULL);
7271

7372
if (ret != 0) {
7473
LOG_ERR("could not allocate interrupt (err %d)", ret);
@@ -228,6 +227,11 @@ static void counter_esp32_isr(void *arg)
228227
const struct device *dev = (const struct device *)arg;
229228
struct counter_esp32_data *data = dev->data;
230229
uint32_t now;
230+
uint32_t status = REG_READ(RTC_CNTL_INT_ST_REG);
231+
232+
if (!(status & RTC_CNTL_MAIN_TIMER_INT_ST_M)) {
233+
return;
234+
}
231235

232236
counter_esp32_cancel_alarm(dev, 0);
233237
counter_esp32_get_value(dev, &now);

drivers/input/input_esp32_touch_sensor.c

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222

2323
LOG_MODULE_REGISTER(espressif_esp32_touch, CONFIG_INPUT_LOG_LEVEL);
2424

25-
BUILD_ASSERT(!IS_ENABLED(CONFIG_COUNTER_RTC_ESP32),
26-
"Conflict detected: COUNTER_RTC_ESP32 enabled");
27-
2825
#define ESP32_SCAN_DONE_MAX_COUNT 5
2926

3027
#if defined(CONFIG_SOC_SERIES_ESP32)
@@ -75,7 +72,6 @@ struct esp32_touch_sensor_channel_data {
7572
};
7673

7774
struct esp32_touch_sensor_data {
78-
uint32_t rtc_intr_msk;
7975
};
8076

8177
static void esp32_touch_sensor_interrupt_cb(void *arg)
@@ -140,36 +136,18 @@ static void esp32_touch_sensor_interrupt_cb(void *arg)
140136
}
141137
}
142138

143-
static void esp32_rtc_isr(void *arg)
139+
static void esp32_touch_rtc_isr(void *arg)
144140
{
145141
uint32_t status = REG_READ(RTC_CNTL_INT_ST_REG);
146142

147-
if (arg != NULL) {
148-
const struct device *dev = arg;
149-
struct esp32_touch_sensor_data *dev_data = dev->data;
150-
151-
if (dev_data->rtc_intr_msk & status) {
152-
esp32_touch_sensor_interrupt_cb(arg);
153-
}
143+
if (!(status & ESP32_RTC_INTR_MSK)) {
144+
return;
154145
}
155146

147+
esp32_touch_sensor_interrupt_cb(arg);
156148
REG_WRITE(RTC_CNTL_INT_CLR_REG, status);
157149
}
158150

159-
static esp_err_t esp32_rtc_isr_install(intr_handler_t intr_handler, const void *handler_arg)
160-
{
161-
esp_err_t err;
162-
163-
REG_WRITE(RTC_CNTL_INT_ENA_REG, 0);
164-
REG_WRITE(RTC_CNTL_INT_CLR_REG, UINT32_MAX);
165-
166-
err = esp_intr_alloc(DT_IRQ_BY_IDX(DT_NODELABEL(touch), 0, irq),
167-
ESP_PRIO_TO_FLAGS(DT_IRQ_BY_IDX(DT_NODELABEL(touch), 0, priority)) |
168-
ESP_INT_FLAGS_CHECK(DT_IRQ_BY_IDX(DT_NODELABEL(touch), 0, flags)),
169-
intr_handler, (void *)handler_arg, NULL);
170-
171-
return err;
172-
}
173151

174152
/**
175153
* Handle debounced touch sensor touch state.
@@ -198,7 +176,8 @@ static void esp32_touch_sensor_change_deferred(struct k_work *work)
198176

199177
static int esp32_touch_sensor_init(const struct device *dev)
200178
{
201-
struct esp32_touch_sensor_data *dev_data = dev->data;
179+
esp_err_t err, flags;
180+
202181
const struct esp32_touch_sensor_config *dev_cfg = dev->config;
203182
const int num_channels = dev_cfg->num_channels;
204183

@@ -293,8 +272,16 @@ static int esp32_touch_sensor_init(const struct device *dev)
293272
touch_hal_timeout_set_threshold(SOC_TOUCH_PAD_THRESHOLD_MAX);
294273
#endif /* defined(CONFIG_SOC_SERIES_ESP32) */
295274

296-
dev_data->rtc_intr_msk = ESP32_RTC_INTR_MSK;
297-
esp32_rtc_isr_install(&esp32_rtc_isr, dev);
275+
flags = ESP_PRIO_TO_FLAGS(DT_IRQ_BY_IDX(DT_NODELABEL(touch), 0, priority)) |
276+
ESP_INT_FLAGS_CHECK(DT_IRQ_BY_IDX(DT_NODELABEL(touch), 0, flags)) |
277+
ESP_INTR_FLAG_SHARED;
278+
err = esp_intr_alloc(DT_IRQ_BY_IDX(DT_NODELABEL(touch), 0, irq), flags, esp32_touch_rtc_isr,
279+
(void *)dev, NULL);
280+
if (err) {
281+
LOG_ERR("Failed to register ISR\n");
282+
return -EFAULT;
283+
}
284+
298285
#if defined(CONFIG_SOC_SERIES_ESP32)
299286
touch_hal_intr_enable();
300287
#elif defined(CONFIG_SOC_SERIES_ESP32S2) || defined(CONFIG_SOC_SERIES_ESP32S3)

drivers/watchdog/wdt_esp32.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ static int wdt_esp32_init(const struct device *dev)
158158
{
159159
const struct wdt_esp32_config *const config = dev->config;
160160
struct wdt_esp32_data *data = dev->data;
161+
int ret, flags;
161162

162163
if (!device_is_ready(config->clock_dev)) {
163164
LOG_ERR("clock control device not ready");
@@ -168,12 +169,10 @@ static int wdt_esp32_init(const struct device *dev)
168169

169170
wdt_hal_init(&data->hal, config->wdt_inst, MWDT_TICK_PRESCALER, true);
170171

171-
int ret = esp_intr_alloc(config->irq_source,
172-
ESP_PRIO_TO_FLAGS(config->irq_priority) |
173-
ESP_INT_FLAGS_CHECK(config->irq_flags),
174-
(ISR_HANDLER)wdt_esp32_isr,
175-
(void *)dev,
176-
NULL);
172+
flags = ESP_PRIO_TO_FLAGS(config->irq_priority) | ESP_INT_FLAGS_CHECK(config->irq_flags) |
173+
ESP_INTR_FLAG_SHARED;
174+
ret = esp_intr_alloc(config->irq_source, flags, (ISR_HANDLER)wdt_esp32_isr, (void *)dev,
175+
NULL);
177176

178177
if (ret != 0) {
179178
LOG_ERR("could not allocate interrupt (err %d)", ret);
@@ -217,6 +216,11 @@ static void wdt_esp32_isr(void *arg)
217216
{
218217
const struct device *dev = (const struct device *)arg;
219218
struct wdt_esp32_data *data = dev->data;
219+
uint32_t status = REG_READ(RTC_CNTL_INT_ST_REG);
220+
221+
if (!(status & RTC_CNTL_WDT_INT_ST)) {
222+
return;
223+
}
220224

221225
if (data->callback) {
222226
data->callback(dev, 0);

drivers/watchdog/xt_wdt_esp32.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ static void esp32_xt_wdt_isr(void *arg)
103103
struct esp32_clock_config clk_cfg = {0};
104104
uint32_t status = REG_READ(RTC_CNTL_INT_ST_REG);
105105

106+
if (!(status & RTC_CNTL_XTAL32K_DEAD_INT_ST)) {
107+
return;
108+
}
109+
106110
REG_WRITE(RTC_CNTL_INT_CLR_REG, status);
107111

108112
clk_cfg.rtc.rtc_slow_clock_src = ESP32_RTC_SLOW_CLK_SRC_RC_SLOW;
@@ -123,16 +127,15 @@ static int esp32_xt_wdt_init(const struct device *dev)
123127
xt_wdt_hal_config_t xt_wdt_hal_config = {
124128
.timeout = ESP32_XT_WDT_MAX_TIMEOUT,
125129
};
130+
int err, flags = 0;
126131

127132
xt_wdt_hal_init(&data->hal, &xt_wdt_hal_config);
128-
xt_wdt_hal_enable_backup_clk(&data->hal,
129-
ESP32_RTC_SLOW_CLK_SRC_RC_SLOW_FREQ/1000);
130-
131-
int err = esp_intr_alloc(cfg->irq_source,
132-
ESP_PRIO_TO_FLAGS(cfg->irq_priority) |
133-
ESP_INT_FLAGS_CHECK(cfg->irq_flags),
134-
(ISR_HANDLER)esp32_xt_wdt_isr, (void *)dev, NULL);
133+
xt_wdt_hal_enable_backup_clk(&data->hal, ESP32_RTC_SLOW_CLK_SRC_RC_SLOW_FREQ/1000);
135134

135+
flags = ESP_PRIO_TO_FLAGS(cfg->irq_priority) | ESP_INT_FLAGS_CHECK(cfg->irq_flags) |
136+
ESP_INTR_FLAG_SHARED;
137+
err = esp_intr_alloc(cfg->irq_source, flags, (ISR_HANDLER)esp32_xt_wdt_isr, (void *)dev,
138+
NULL);
136139
if (err) {
137140
LOG_ERR("Failed to register ISR\n");
138141
return -EFAULT;

0 commit comments

Comments
 (0)