From 6edeb9ebd982c47f18075c32c7b2d1edbb9e67c4 Mon Sep 17 00:00:00 2001 From: Gustavo Henrique Nihei Date: Tue, 23 Feb 2021 20:51:22 -0300 Subject: [PATCH] risc-v/esp32c3: Free CPU interrupt if irq_attach fails --- arch/risc-v/src/esp32c3/esp32c3_wdt.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/arch/risc-v/src/esp32c3/esp32c3_wdt.c b/arch/risc-v/src/esp32c3/esp32c3_wdt.c index 8c9f4f2b0e5..1b6d9ca0a9b 100644 --- a/arch/risc-v/src/esp32c3/esp32c3_wdt.c +++ b/arch/risc-v/src/esp32c3/esp32c3_wdt.c @@ -678,7 +678,7 @@ static int32_t esp32c3_wdt_setisr(struct esp32c3_wdt_dev_s *dev, { if (wdt->cpuint != -ENOMEM) { - /* Disable the provided CPU Interrupt to configure it. */ + /* Disable the provided CPU interrupt to configure it. */ up_disable_irq(wdt->cpuint); } @@ -695,12 +695,18 @@ static int32_t esp32c3_wdt_setisr(struct esp32c3_wdt_dev_s *dev, /* Attach and enable the IRQ. */ ret = irq_attach(wdt->irq, handler, arg); - if (ret == OK) + if (ret != OK) { - /* Enable the CPU Interrupt that is linked to the WDT. */ + /* Failed to attach IRQ, so CPU interrupt must be freed. */ - up_enable_irq(wdt->cpuint); + esp32c3_free_cpuint(wdt->periph); + wdt->cpuint = -ENOMEM; + return ret; } + + /* Enable the CPU interrupt that is linked to the WDT. */ + + up_enable_irq(wdt->cpuint); } return ret;