From 15ffad4e39b767c49eba900e365d73ff00715fb0 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Wed, 5 Nov 2025 14:20:28 +0100 Subject: [PATCH] cpu/sam0_common: clear tamper wake on gpio_irq_disable() --- cpu/sam0_common/include/periph_cpu_common.h | 18 ++++++++++++++- cpu/sam0_common/periph/gpio.c | 8 +++++++ cpu/sam0_common/periph/rtc_rtt.c | 25 +++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/cpu/sam0_common/include/periph_cpu_common.h b/cpu/sam0_common/include/periph_cpu_common.h index 3c2ac0d736..38b33d9689 100644 --- a/cpu/sam0_common/include/periph_cpu_common.h +++ b/cpu/sam0_common/include/periph_cpu_common.h @@ -1353,10 +1353,26 @@ void rtc_tamper_init(void); * @param pin The GPIO pin to be used for tamper detection * @param flank The Flank to trigger the even * - * @return 0 on success, -1 if pin is not RTC pin + * @return 0 on success, -1 if pin is not an RTC pin */ int rtc_tamper_register(gpio_t pin, gpio_flank_t flank); +/** + * @brief (Re-)Enable Tamper Detection pin + * + * @param pin The GPIO pin to be used for tamper detection + * + * @return 0 on success, -1 if pin is not an RTC pin + */ +int rtc_tamper_pin_enable(gpio_t pin); + +/** + * @brief Disable Tamper Detection pin + * + * @param pin The GPIO pin to no longer be used for tamper detection + */ +void rtc_tamper_pin_disable(gpio_t pin); + /** * @brief Enable Tamper Detection IRQs */ diff --git a/cpu/sam0_common/periph/gpio.c b/cpu/sam0_common/periph/gpio.c index 2d977e06a7..13fc0bd26e 100644 --- a/cpu/sam0_common/periph/gpio.c +++ b/cpu/sam0_common/periph/gpio.c @@ -481,6 +481,10 @@ void gpio_irq_enable(gpio_t pin) /* enable wake from deep sleep */ _set_extwake(pin, true); + + if (IS_ACTIVE(MODULE_PERIPH_GPIO_TAMPER_WAKE)) { + rtc_tamper_pin_enable(pin); + } } void gpio_irq_disable(gpio_t pin) @@ -502,6 +506,10 @@ void gpio_irq_disable(gpio_t pin) /* disable wake from deep sleep */ _set_extwake(pin, false); + + if (IS_ACTIVE(MODULE_PERIPH_GPIO_TAMPER_WAKE)) { + rtc_tamper_pin_disable(pin); + } } #if defined(CPU_COMMON_SAML1X) diff --git a/cpu/sam0_common/periph/rtc_rtt.c b/cpu/sam0_common/periph/rtc_rtt.c index 38e10b3854..cd09a5a25d 100644 --- a/cpu/sam0_common/periph/rtc_rtt.c +++ b/cpu/sam0_common/periph/rtc_rtt.c @@ -27,6 +27,7 @@ #include #include +#include "atomic_utils.h" #include "pm_layered.h" #include "periph/rtc.h" #include "periph/rtt.h" @@ -503,6 +504,30 @@ int rtc_tamper_register(gpio_t pin, gpio_flank_t flank) return 0; } +int rtc_tamper_pin_enable(gpio_t pin) +{ + int in = _rtc_pin(pin); + + if (in < 0) { + return -1; + } + + atomic_set_bit_u32(atomic_bit_u32(&tampctr, 2 * in)); + + return 0; +} + +void rtc_tamper_pin_disable(gpio_t pin) +{ + int in = _rtc_pin(pin); + + if (in < 0) { + return; + } + + atomic_clear_bit_u32(atomic_bit_u32(&tampctr, 2 * in)); +} + void rtc_tamper_enable(void) { DEBUG("enable tamper\n");