From ccbeb38275ea20a4218a109cdeac69046c34107c Mon Sep 17 00:00:00 2001 From: Gerson Fernando Budke Date: Thu, 20 Oct 2022 18:50:22 +0200 Subject: [PATCH 1/7] cpu/avr8: Enable PM periph to all SoC This refactor the current xmega PM peripheral to avr8 common and extend PM to cpus families. Signed-off-by: Gerson Fernando Budke --- cpu/atmega1281/include/periph_cpu.h | 11 ++++ cpu/atmega1284p/include/periph_cpu.h | 11 ++++ cpu/atmega128rfa1/include/periph_cpu.h | 10 ++++ cpu/atmega2560/include/periph_cpu.h | 11 ++++ cpu/atmega256rfr2/include/periph_cpu.h | 11 ++++ cpu/atmega328p/include/periph_cpu.h | 11 ++++ cpu/atmega32u4/include/periph_cpu.h | 11 ++++ cpu/atmega8/include/periph_cpu.h | 11 ++++ cpu/atxmega/Kconfig | 1 - cpu/atxmega/Makefile.dep | 3 - cpu/atxmega/Makefile.features | 1 - cpu/atxmega/atxmega.config | 2 - cpu/atxmega/include/periph_cpu.h | 4 ++ cpu/atxmega/periph/pm.c | 40 ------------- cpu/avr8_common/Kconfig | 1 + cpu/avr8_common/Makefile | 2 +- cpu/avr8_common/Makefile.dep | 5 +- cpu/avr8_common/Makefile.features | 1 + cpu/avr8_common/avr8_common.config | 2 + cpu/avr8_common/include/cpu.h | 9 +++ cpu/avr8_common/periph/Kconfig | 7 +++ cpu/avr8_common/periph/Makefile | 3 + cpu/avr8_common/periph/pm.c | 83 ++++++++++++++++++++++++++ 23 files changed, 202 insertions(+), 49 deletions(-) delete mode 100644 cpu/atxmega/atxmega.config create mode 100644 cpu/avr8_common/avr8_common.config create mode 100644 cpu/avr8_common/periph/Kconfig create mode 100644 cpu/avr8_common/periph/Makefile create mode 100644 cpu/avr8_common/periph/pm.c diff --git a/cpu/atmega1281/include/periph_cpu.h b/cpu/atmega1281/include/periph_cpu.h index cec8221dbe098..eab61b47dde51 100644 --- a/cpu/atmega1281/include/periph_cpu.h +++ b/cpu/atmega1281/include/periph_cpu.h @@ -29,6 +29,17 @@ extern "C" { #include "periph_cpu_common.h" +/** + * @name Power management configuration + * @{ + */ +#define PM_NUM_MODES (5) +#define AVR8_PM_SLEEP_MODE_0 SLEEP_MODE_PWR_DOWN /**< Power Down */ +#define AVR8_PM_SLEEP_MODE_1 SLEEP_MODE_PWR_SAVE /**< Power Save */ +#define AVR8_PM_SLEEP_MODE_2 SLEEP_MODE_STANDBY /**< Standby */ +#define AVR8_PM_SLEEP_MODE_3 SLEEP_MODE_ADC /**< Sleep ADC low noise */ +/** @} */ + /** * @brief Available ports on the ATmega1281 family */ diff --git a/cpu/atmega1284p/include/periph_cpu.h b/cpu/atmega1284p/include/periph_cpu.h index b2f292a6240af..cf6f3a76d16d1 100644 --- a/cpu/atmega1284p/include/periph_cpu.h +++ b/cpu/atmega1284p/include/periph_cpu.h @@ -29,6 +29,17 @@ extern "C" { #endif +/** + * @name Power management configuration + * @{ + */ +#define PM_NUM_MODES (5) +#define AVR8_PM_SLEEP_MODE_0 SLEEP_MODE_PWR_DOWN /**< Power Down */ +#define AVR8_PM_SLEEP_MODE_1 SLEEP_MODE_PWR_SAVE /**< Power Save */ +#define AVR8_PM_SLEEP_MODE_2 SLEEP_MODE_STANDBY /**< Standby */ +#define AVR8_PM_SLEEP_MODE_3 SLEEP_MODE_ADC /**< Sleep ADC low noise */ +/** @} */ + /** * @brief Define a CPU specific GPIO pin generator macro */ diff --git a/cpu/atmega128rfa1/include/periph_cpu.h b/cpu/atmega128rfa1/include/periph_cpu.h index 349b04c0714f4..4e3c4f709c3f1 100644 --- a/cpu/atmega128rfa1/include/periph_cpu.h +++ b/cpu/atmega128rfa1/include/periph_cpu.h @@ -26,6 +26,16 @@ #ifdef __cplusplus extern "C" { #endif +/** + * @name Power management configuration + * @{ + */ +#define PM_NUM_MODES (5) +#define AVR8_PM_SLEEP_MODE_0 SLEEP_MODE_PWR_DOWN /**< Power Down */ +#define AVR8_PM_SLEEP_MODE_1 SLEEP_MODE_PWR_SAVE /**< Power Save */ +#define AVR8_PM_SLEEP_MODE_2 SLEEP_MODE_STANDBY /**< Standby */ +#define AVR8_PM_SLEEP_MODE_3 SLEEP_MODE_ADC /**< Sleep ADC low noise */ +/** @} */ /** * @name Available ports on the ATmega128rfa1 MCU diff --git a/cpu/atmega2560/include/periph_cpu.h b/cpu/atmega2560/include/periph_cpu.h index 7a4d546a5208a..df606bfbd9014 100644 --- a/cpu/atmega2560/include/periph_cpu.h +++ b/cpu/atmega2560/include/periph_cpu.h @@ -27,6 +27,17 @@ extern "C" { #endif +/** + * @name Power management configuration + * @{ + */ +#define PM_NUM_MODES (5) +#define AVR8_PM_SLEEP_MODE_0 SLEEP_MODE_PWR_DOWN /**< Power Down */ +#define AVR8_PM_SLEEP_MODE_1 SLEEP_MODE_PWR_SAVE /**< Power Save */ +#define AVR8_PM_SLEEP_MODE_2 SLEEP_MODE_STANDBY /**< Standby */ +#define AVR8_PM_SLEEP_MODE_3 SLEEP_MODE_ADC /**< Sleep ADC low noise */ +/** @} */ + /** * @brief Available ports on the ATmega2560 family */ diff --git a/cpu/atmega256rfr2/include/periph_cpu.h b/cpu/atmega256rfr2/include/periph_cpu.h index 496ffe901b405..6e42eba9206d1 100644 --- a/cpu/atmega256rfr2/include/periph_cpu.h +++ b/cpu/atmega256rfr2/include/periph_cpu.h @@ -27,6 +27,17 @@ extern "C" { #endif +/** + * @name Power management configuration + * @{ + */ +#define PM_NUM_MODES (5) +#define AVR8_PM_SLEEP_MODE_0 SLEEP_MODE_PWR_DOWN /**< Power Down */ +#define AVR8_PM_SLEEP_MODE_1 SLEEP_MODE_PWR_SAVE /**< Power Save */ +#define AVR8_PM_SLEEP_MODE_2 SLEEP_MODE_STANDBY /**< Standby */ +#define AVR8_PM_SLEEP_MODE_3 SLEEP_MODE_ADC /**< Sleep ADC low noise */ +/** @} */ + /** * @name Available ports on the ATmega256rfr family * @{ diff --git a/cpu/atmega328p/include/periph_cpu.h b/cpu/atmega328p/include/periph_cpu.h index b9c2423d0c0bf..d53e5496a0f29 100644 --- a/cpu/atmega328p/include/periph_cpu.h +++ b/cpu/atmega328p/include/periph_cpu.h @@ -27,6 +27,17 @@ extern "C" { #endif +/** + * @name Power management configuration + * @{ + */ +#define PM_NUM_MODES (5) +#define AVR8_PM_SLEEP_MODE_0 SLEEP_MODE_PWR_DOWN /**< Power Down */ +#define AVR8_PM_SLEEP_MODE_1 SLEEP_MODE_PWR_SAVE /**< Power Save */ +#define AVR8_PM_SLEEP_MODE_2 SLEEP_MODE_STANDBY /**< Standby */ +#define AVR8_PM_SLEEP_MODE_3 SLEEP_MODE_ADC /**< Sleep ADC low noise */ +/** @} */ + /** * @brief Define a CPU specific GPIO pin generator macro */ diff --git a/cpu/atmega32u4/include/periph_cpu.h b/cpu/atmega32u4/include/periph_cpu.h index 8429e00e2c50a..be2d71df4dcfd 100644 --- a/cpu/atmega32u4/include/periph_cpu.h +++ b/cpu/atmega32u4/include/periph_cpu.h @@ -26,6 +26,17 @@ extern "C" { #endif +/** + * @name Power management configuration + * @{ + */ +#define PM_NUM_MODES (5) +#define AVR8_PM_SLEEP_MODE_0 SLEEP_MODE_PWR_DOWN /**< Power Down */ +#define AVR8_PM_SLEEP_MODE_1 SLEEP_MODE_PWR_SAVE /**< Power Save */ +#define AVR8_PM_SLEEP_MODE_2 SLEEP_MODE_STANDBY /**< Standby */ +#define AVR8_PM_SLEEP_MODE_3 SLEEP_MODE_ADC /**< Sleep ADC low noise */ +/** @} */ + /** * @brief Available ports on the ATmega32u4 family */ diff --git a/cpu/atmega8/include/periph_cpu.h b/cpu/atmega8/include/periph_cpu.h index d8666c83ed56d..32b6c51ebf26e 100644 --- a/cpu/atmega8/include/periph_cpu.h +++ b/cpu/atmega8/include/periph_cpu.h @@ -29,6 +29,17 @@ extern "C" { #endif +/** + * @name Power management configuration + * @{ + */ +#define PM_NUM_MODES (5) +#define AVR8_PM_SLEEP_MODE_0 SLEEP_MODE_PWR_DOWN /**< Power Down */ +#define AVR8_PM_SLEEP_MODE_1 SLEEP_MODE_PWR_SAVE /**< Power Save */ +#define AVR8_PM_SLEEP_MODE_2 SLEEP_MODE_STANDBY /**< Standby */ +#define AVR8_PM_SLEEP_MODE_3 SLEEP_MODE_ADC /**< Sleep ADC low noise */ +/** @} */ + /** * @brief Define a CPU specific GPIO pin generator macro */ diff --git a/cpu/atxmega/Kconfig b/cpu/atxmega/Kconfig index 09d699cbe42b5..19495de1d05b9 100644 --- a/cpu/atxmega/Kconfig +++ b/cpu/atxmega/Kconfig @@ -19,7 +19,6 @@ config CPU_COMMON_ATXMEGA select HAS_PERIPH_GPIO select HAS_PERIPH_GPIO_IRQ select HAS_PERIPH_NVM - select HAS_PERIPH_PM select HAS_PERIPH_TIMER select HAS_PERIPH_TIMER_PERIODIC diff --git a/cpu/atxmega/Makefile.dep b/cpu/atxmega/Makefile.dep index 43da1e7f66f3f..55c0238cbf0bb 100644 --- a/cpu/atxmega/Makefile.dep +++ b/cpu/atxmega/Makefile.dep @@ -1,9 +1,6 @@ # peripheral drivers are linked into the final binary USEMODULE += atxmega_periph -# All ATxmega based CPUs provide PM -USEMODULE += pm_layered - ifneq (,$(filter periph_cpuid,$(USEMODULE))) USEMODULE += periph_nvm endif diff --git a/cpu/atxmega/Makefile.features b/cpu/atxmega/Makefile.features index 7908685a4f807..d566d9ce38b85 100644 --- a/cpu/atxmega/Makefile.features +++ b/cpu/atxmega/Makefile.features @@ -11,7 +11,6 @@ FEATURES_PROVIDED += cpu_core_atxmega FEATURES_PROVIDED += periph_cpuid FEATURES_PROVIDED += periph_gpio periph_gpio_irq FEATURES_PROVIDED += periph_nvm -FEATURES_PROVIDED += periph_pm FEATURES_PROVIDED += periph_timer periph_timer_periodic # Add atxmega configurations. This configuration enables modules that are only available when diff --git a/cpu/atxmega/atxmega.config b/cpu/atxmega/atxmega.config deleted file mode 100644 index 3360051f8bebd..0000000000000 --- a/cpu/atxmega/atxmega.config +++ /dev/null @@ -1,2 +0,0 @@ -# All ATxmega based CPUs provide PM -CONFIG_MODULE_PM_LAYERED=y diff --git a/cpu/atxmega/include/periph_cpu.h b/cpu/atxmega/include/periph_cpu.h index 440f09266a9b0..72219bae38c59 100644 --- a/cpu/atxmega/include/periph_cpu.h +++ b/cpu/atxmega/include/periph_cpu.h @@ -96,6 +96,10 @@ enum { * @{ */ #define PM_NUM_MODES (5) +#define AVR8_PM_SLEEP_MODE_0 SLEEP_MODE_PWR_DOWN /**< Power Down */ +#define AVR8_PM_SLEEP_MODE_1 SLEEP_MODE_PWR_SAVE /**< Power Save */ +#define AVR8_PM_SLEEP_MODE_2 SLEEP_MODE_STANDBY /**< Standby */ +#define AVR8_PM_SLEEP_MODE_3 SLEEP_MODE_EXT_STANDBY /**< Extended Standby*/ /** @} */ /** diff --git a/cpu/atxmega/periph/pm.c b/cpu/atxmega/periph/pm.c index e002715cde0c7..af072b0071636 100644 --- a/cpu/atxmega/periph/pm.c +++ b/cpu/atxmega/periph/pm.c @@ -19,8 +19,6 @@ * @} */ -#include - #include "cpu_pm.h" #include "irq.h" #include "periph/pm.h" @@ -73,44 +71,6 @@ void pm_reboot(void) while (1) {} } -/* - * DEBUG may affect this routine. - * - * --- Do NOT add DEBUG macro here --- - * - */ -void pm_set(unsigned mode) -{ - unsigned irq_state = irq_disable(); - - if (avr8_is_uart_tx_pending() && mode < 4) { - irq_restore(irq_state); - return; - } - - switch (mode) { - case 0: - set_sleep_mode(SLEEP_SMODE_PDOWN_gc); - break; - case 1: - set_sleep_mode(SLEEP_SMODE_PSAVE_gc); - break; - case 2: - set_sleep_mode(SLEEP_SMODE_STDBY_gc); - break; - case 3: - set_sleep_mode(SLEEP_SMODE_ESTDBY_gc); - break; - default: - set_sleep_mode(SLEEP_SMODE_IDLE_gc); - } - sleep_enable(); - sei(); - sleep_cpu(); - sleep_disable(); - irq_restore(irq_state); -} - void pm_periph_enable(pwr_reduction_t pwr) { uint8_t mask = _device_mask(pwr); diff --git a/cpu/avr8_common/Kconfig b/cpu/avr8_common/Kconfig index 54bd222d82ac8..03a0f40b5a9fe 100644 --- a/cpu/avr8_common/Kconfig +++ b/cpu/avr8_common/Kconfig @@ -10,6 +10,7 @@ config CPU_ARCH_AVR8 bool select HAS_ARCH_8BIT select HAS_ARCH_AVR8 + select HAS_PERIPH_PM select MODULE_MALLOC_THREAD_SAFE if TEST_KCONFIG select MODULE_TINY_STRERROR_AS_STRERROR if TEST_KCONFIG diff --git a/cpu/avr8_common/Makefile b/cpu/avr8_common/Makefile index 4b36b1f63ac46..3920b1bb4c5bc 100644 --- a/cpu/avr8_common/Makefile +++ b/cpu/avr8_common/Makefile @@ -2,6 +2,6 @@ MODULE = avr8_common # add a list of subdirectories, that should also be build -DIRS = avr_libc_extra +DIRS = periph avr_libc_extra include $(RIOTBASE)/Makefile.base diff --git a/cpu/avr8_common/Makefile.dep b/cpu/avr8_common/Makefile.dep index 80c7f8d197252..a1a228d518c08 100644 --- a/cpu/avr8_common/Makefile.dep +++ b/cpu/avr8_common/Makefile.dep @@ -2,7 +2,10 @@ USEMODULE += avr_libc_extra # tell the build system to build the avr8 common files -USEMODULE += avr8_common +USEMODULE += avr8_common avr8_common_periph + +# All avr8 CPUs provide PM +USEMODULE += pm_layered # The AVR-libc provides no thread safe malloc implementation and has no hooks # to inject. Use malloc_thread_safe to link calls to malloc to safe wrappers diff --git a/cpu/avr8_common/Makefile.features b/cpu/avr8_common/Makefile.features index 1c5acda5cd1db..c9f934217d040 100644 --- a/cpu/avr8_common/Makefile.features +++ b/cpu/avr8_common/Makefile.features @@ -1,3 +1,4 @@ FEATURES_PROVIDED += arch_8bit FEATURES_PROVIDED += arch_avr8 FEATURES_PROVIDED += cpp +FEATURES_PROVIDED += periph_pm diff --git a/cpu/avr8_common/avr8_common.config b/cpu/avr8_common/avr8_common.config new file mode 100644 index 0000000000000..2bc22c6261f4d --- /dev/null +++ b/cpu/avr8_common/avr8_common.config @@ -0,0 +1,2 @@ +# All AVR-8 based CPUs provide PM +CONFIG_MODULE_PM_LAYERED=y diff --git a/cpu/avr8_common/include/cpu.h b/cpu/avr8_common/include/cpu.h index 5cfeca21d0906..6e183661e74f3 100644 --- a/cpu/avr8_common/include/cpu.h +++ b/cpu/avr8_common/include/cpu.h @@ -48,6 +48,15 @@ extern "C" { #endif +/** + * @name BOD monitoring when CPU is on sleep + * @{ + */ +#ifndef AVR8_PM_DISABLE_BOD_ON_SLEEP +#define AVR8_PM_DISABLE_BOD_ON_SLEEP 0 /**< BOD is active on sleep */ +#endif +/** @} */ + /** * @name Use shared I2C functions * @{ diff --git a/cpu/avr8_common/periph/Kconfig b/cpu/avr8_common/periph/Kconfig new file mode 100644 index 0000000000000..17750d59e9b6d --- /dev/null +++ b/cpu/avr8_common/periph/Kconfig @@ -0,0 +1,7 @@ +config MODULE_AVR8_COMMON_PERIPH + bool + depends on TEST_KCONFIG + depends on CPU_COMMON_ATXMEGA + default y + help + AVR8 common peripheral drivers. diff --git a/cpu/avr8_common/periph/Makefile b/cpu/avr8_common/periph/Makefile new file mode 100644 index 0000000000000..ce4c195b0e3b0 --- /dev/null +++ b/cpu/avr8_common/periph/Makefile @@ -0,0 +1,3 @@ +MODULE = avr8_common_periph + +include $(RIOTMAKE)/periph.mk diff --git a/cpu/avr8_common/periph/pm.c b/cpu/avr8_common/periph/pm.c new file mode 100644 index 0000000000000..1d31fd0833100 --- /dev/null +++ b/cpu/avr8_common/periph/pm.c @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2022-2023 Gerson Fernando Budke + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup cpu_avr8_common + * @ingroup cpu_avr8_common_periph + * @{ + * + * @file + * @brief Low-level PM driver implementation + * + * @author Gerson Fernando Budke + * + * @} + */ + +#include + +#include "irq.h" +#include "periph_cpu.h" + +#define ENABLE_DEBUG 0 +#include "debug.h" + +/** + * @note: The pm_set assumes that interrupts are disable and there is no reason + * to save SREG here. + * + * @note: DEBUG affects this routine. + */ +void pm_set(unsigned mode) +{ + DEBUG("pm_set(%d)\n", mode); + + if (avr8_is_uart_tx_pending()) { + mode = PM_NUM_MODES - 1; + } + + switch (mode) { + case 0: + set_sleep_mode(AVR8_PM_SLEEP_MODE_0); + break; +#if PM_NUM_MODES > 1 + case 1: + set_sleep_mode(AVR8_PM_SLEEP_MODE_1); + break; +#endif +#if PM_NUM_MODES > 2 + case 2: + set_sleep_mode(AVR8_PM_SLEEP_MODE_2); + break; +#endif +#if PM_NUM_MODES > 3 + case 3: + set_sleep_mode(AVR8_PM_SLEEP_MODE_3); + break; +#endif + default: + set_sleep_mode(SLEEP_MODE_IDLE); + break; + } + + DEBUG("mode selected: %d\n", mode); + +/* + * Critical Section to correct enable SLEEP instruction on RIOT-OS + */ +#if (AVR8_PM_DISABLE_BOD_ON_SLEEP == 1) +#ifdef sleep_bod_disable + sleep_bod_disable(); +#endif +#endif + sleep_enable(); + sei(); + sleep_cpu(); + cli(); + sleep_disable(); +} From 42dbc2fd1772d66291eb5a10bb99415cc5d3bd94 Mon Sep 17 00:00:00 2001 From: Gerson Fernando Budke Date: Sat, 1 Jul 2023 21:03:48 +0200 Subject: [PATCH 2/7] cpu/atxmega: Fix PM states on peripherals Fix the required PM state on i2c and spi peripherals. Signed-off-by: Gerson Fernando Budke --- cpu/atxmega/periph/i2c.c | 4 ++-- cpu/atxmega/periph/spi.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cpu/atxmega/periph/i2c.c b/cpu/atxmega/periph/i2c.c index 5dd2cc7fe3b93..1855c387cc855 100644 --- a/cpu/atxmega/periph/i2c.c +++ b/cpu/atxmega/periph/i2c.c @@ -105,7 +105,7 @@ void i2c_acquire(i2c_t i2c) { assert((unsigned)i2c < I2C_NUMOF); DEBUG("acquire\n"); - pm_block(3); + pm_block(4); /* Require clkPer */ mutex_lock(&i2c_ctx[i2c].locks); pm_periph_enable(i2c_config[i2c].pwr); @@ -122,7 +122,7 @@ void i2c_release(i2c_t i2c) dev(i2c)->MASTER.CTRLA = 0; pm_periph_disable(i2c_config[i2c].pwr); mutex_unlock(&i2c_ctx[i2c].locks); - pm_unblock(3); + pm_unblock(4); DEBUG("release\n"); } diff --git a/cpu/atxmega/periph/spi.c b/cpu/atxmega/periph/spi.c index 690bf73717d24..bd942fb25daf1 100644 --- a/cpu/atxmega/periph/spi.c +++ b/cpu/atxmega/periph/spi.c @@ -94,7 +94,7 @@ void spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk) DEBUG("acquire\n"); - pm_block(3); + pm_block(4); /* Require clkPer */ mutex_lock(&locks[bus]); pm_periph_enable(spi_config[bus].pwr); @@ -113,7 +113,7 @@ void spi_release(spi_t bus) dev(bus)->CTRL &= ~SPI_ENABLE_bm; pm_periph_disable(spi_config[bus].pwr); mutex_unlock(&locks[bus]); - pm_unblock(3); + pm_unblock(4); DEBUG("release\n"); } From ef4c724c763690acfd73e40048ce0f4ec52501c6 Mon Sep 17 00:00:00 2001 From: Gerson Fernando Budke Date: Thu, 20 Oct 2022 19:35:17 +0200 Subject: [PATCH 3/7] cpu/atmega_common: Add PM on peripherals Add PM blocks to adc/i2c/spi peripherals. Signed-off-by: Gerson Fernando Budke --- cpu/atmega_common/periph/adc.c | 3 +++ cpu/atmega_common/periph/i2c.c | 3 +++ cpu/atmega_common/periph/spi.c | 3 +++ 3 files changed, 9 insertions(+) diff --git a/cpu/atmega_common/periph/adc.c b/cpu/atmega_common/periph/adc.c index 52271feaf5ebe..28de07f9a914c 100644 --- a/cpu/atmega_common/periph/adc.c +++ b/cpu/atmega_common/periph/adc.c @@ -25,6 +25,7 @@ #include "cpu.h" #include "mutex.h" #include "periph/adc.h" +#include "periph/pm.h" #include "periph_conf.h" #define ADC_MAX_CLK (200000U) @@ -33,6 +34,7 @@ static mutex_t lock = MUTEX_INIT; static inline void _prep(void) { + pm_block(3); /* Require clkADC */ mutex_lock(&lock); /* Enable ADC */ ADCSRA |= (1 << ADEN); @@ -43,6 +45,7 @@ static inline void _done(void) /* Disable ADC */ ADCSRA &= ~(1 << ADEN); mutex_unlock(&lock); + pm_unblock(3); } int adc_init(adc_t line) diff --git a/cpu/atmega_common/periph/i2c.c b/cpu/atmega_common/periph/i2c.c index 118eb0964896a..d7425688edc7b 100644 --- a/cpu/atmega_common/periph/i2c.c +++ b/cpu/atmega_common/periph/i2c.c @@ -31,6 +31,7 @@ #include "cpu.h" #include "mutex.h" #include "periph/i2c.h" +#include "periph/pm.h" #include "periph_conf.h" #define ENABLE_DEBUG 0 @@ -226,6 +227,7 @@ void i2c_acquire(i2c_t dev) { assert(dev < I2C_NUMOF); + pm_block(4); /* Require clkIO */ mutex_lock(&locks[dev]); } @@ -234,6 +236,7 @@ void i2c_release(i2c_t dev) assert(dev < I2C_NUMOF); mutex_unlock(&locks[dev]); + pm_unblock(4); } static void i2c_poweron(i2c_t dev) diff --git a/cpu/atmega_common/periph/spi.c b/cpu/atmega_common/periph/spi.c index 4231f4929baae..7dc3370996738 100644 --- a/cpu/atmega_common/periph/spi.c +++ b/cpu/atmega_common/periph/spi.c @@ -31,6 +31,7 @@ #include "cpu.h" #include "mutex.h" #include "periph/spi.h" +#include "periph/pm.h" /** * @brief Extract BR0, BR1 and SPI2X bits from speed value @@ -89,6 +90,7 @@ void spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk) assert(bus == SPI_DEV(0)); /* lock the bus and power on the SPI peripheral */ + pm_block(4); /* Require clkIO */ mutex_lock(&lock); #ifdef PRSPI power_spi_enable(); @@ -112,6 +114,7 @@ void spi_release(spi_t bus) power_spi_disable(); #endif mutex_unlock(&lock); + pm_unblock(4); } void spi_transfer_bytes(spi_t bus, spi_cs_t cs, bool cont, From 22bcf1d9ce30cf2287f2572038d614f227ff241b Mon Sep 17 00:00:00 2001 From: Gerson Fernando Budke Date: Sat, 1 Jul 2023 20:54:55 +0200 Subject: [PATCH 4/7] cpu/atmega_common: Increase stack a little bit This is necessary to allow run the thread_duel example. Signed-off-by: Gerson Fernando Budke --- cpu/atmega_common/include/cpu_conf.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpu/atmega_common/include/cpu_conf.h b/cpu/atmega_common/include/cpu_conf.h index 8cb9dd85608cb..13cea613fb123 100644 --- a/cpu/atmega_common/include/cpu_conf.h +++ b/cpu/atmega_common/include/cpu_conf.h @@ -32,7 +32,7 @@ extern "C" { #endif #ifndef THREAD_EXTRA_STACKSIZE_PRINTF -#define THREAD_EXTRA_STACKSIZE_PRINTF (128) +#define THREAD_EXTRA_STACKSIZE_PRINTF (132) #endif /** From 70cfe4230cf36fa3b7eee170ad1d1060cdd14478 Mon Sep 17 00:00:00 2001 From: Gerson Fernando Budke Date: Sun, 2 Jul 2023 11:54:27 +0200 Subject: [PATCH 5/7] boards/atmega328p-xplained-mini: Add BTN0 and LED0 The board have one user button and a user led but are not enabled. This add necessary support to use the button and the led. It include the configs to use with SAUL and button interrupt. Signed-off-by: Gerson Fernando Budke --- boards/atmega328p-xplained-mini/Kconfig | 6 ++- boards/atmega328p-xplained-mini/Makefile.dep | 5 ++ .../atmega328p-xplained-mini/include/board.h | 23 +++++++- .../include/gpio_params.h | 53 +++++++++++++++++++ 4 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 boards/atmega328p-xplained-mini/include/gpio_params.h diff --git a/boards/atmega328p-xplained-mini/Kconfig b/boards/atmega328p-xplained-mini/Kconfig index ca8ba2ab14fa9..7142eef135534 100644 --- a/boards/atmega328p-xplained-mini/Kconfig +++ b/boards/atmega328p-xplained-mini/Kconfig @@ -1,5 +1,5 @@ # Copyright (c) 2020 HAW Hamburg -# Copyright (c) 2021 Gerson Fernando Budke +# Copyright (c) 2021-2023 Gerson Fernando Budke # # This file is subject to the terms and conditions of the GNU Lesser # General Public License v2.1. See the file LICENSE in the top level @@ -21,7 +21,9 @@ config BOARD_ATMEGA328P_XPLAINED_MINI select HAS_PERIPH_SPI select HAS_PERIPH_TIMER select HAS_PERIPH_UART - # Various other features (if any) + + select HAVE_SAUL_GPIO select MODULE_BOARDS_COMMON_ATMEGA if TEST_KCONFIG + select MODULE_ATMEGA_PCINT0 if TEST_KCONFIG source "$(RIOTBOARD)/common/atmega/Kconfig" diff --git a/boards/atmega328p-xplained-mini/Makefile.dep b/boards/atmega328p-xplained-mini/Makefile.dep index 3d1c295b9b210..a1b7040e408e3 100644 --- a/boards/atmega328p-xplained-mini/Makefile.dep +++ b/boards/atmega328p-xplained-mini/Makefile.dep @@ -1 +1,6 @@ USEMODULE += boards_common_atmega +USEMODULE += atmega_pcint0 + +ifneq (,$(filter saul_default,$(USEMODULE))) + USEMODULE += saul_gpio +endif diff --git a/boards/atmega328p-xplained-mini/include/board.h b/boards/atmega328p-xplained-mini/include/board.h index 9ae5b246a24bd..1848fd0c16d95 100644 --- a/boards/atmega328p-xplained-mini/include/board.h +++ b/boards/atmega328p-xplained-mini/include/board.h @@ -2,7 +2,7 @@ * Copyright (C) 2014 Freie Universität Berlin, Hinnerk van Bruinehsen * 2016 Laurent Navet * 2019 Otto-von-Guericke-Universität Magdeburg - * 2021 Gerson Fernando Budke + * 2021-2123 Gerson Fernando Budke * * This file is subject to the terms and conditions of the GNU Lesser * General Public License v2.1. See the file LICENSE in the top level @@ -55,6 +55,27 @@ extern "C" { #define XTIMER_BACKOFF (40) /** @} */ +/** + * @name Macros for controlling the on-board LED + * @{ + */ +#define LED0_PIN GPIO_PIN(PORT_B, 5) +#define LED0_MODE GPIO_OUT +#define LED0_ENABLE_PORT DDRB |= LED0_PIN +#define LED0_ON PORTB |= LED0_PIN +#define LED0_OFF PORTB &= ~LED0_PIN +#define LED0_TOGGLE PORTB ^= LED0_PIN +/** @} */ + +/** + * @name Button pin configuration + * @{ + */ +#define BTN0_PIN GPIO_PIN(PORT_B, 7) +#define BTN0_MODE GPIO_IN_PU +#define BTN0_INT_FLANK GPIO_FALLING +/** @} */ + #ifdef __cplusplus } #endif diff --git a/boards/atmega328p-xplained-mini/include/gpio_params.h b/boards/atmega328p-xplained-mini/include/gpio_params.h new file mode 100644 index 0000000000000..f2dedc9b1d9b0 --- /dev/null +++ b/boards/atmega328p-xplained-mini/include/gpio_params.h @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2023 Gerson Fernando Budke + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup boards_atmega328p_xplained_mini + * @{ + * + * @file + * @brief Configuration of SAUL mapped GPIO pins + * + * @author Gerson Fernando Budke + */ + +#ifndef GPIO_PARAMS_H +#define GPIO_PARAMS_H + +#include "board.h" +#include "saul/periph.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief GPIO configuration + */ +static const saul_gpio_params_t saul_gpio_params[] = +{ + { + .name = "BTN0 (SW0)", + .pin = BTN0_PIN, + .mode = BTN0_MODE, + .flags = SAUL_GPIO_INVERTED, + }, + { + .name = "LED0 (Yellow)", + .pin = LED0_PIN, + .mode = LED0_MODE, + .flags = (SAUL_GPIO_INIT_CLEAR), + }, +}; + +#ifdef __cplusplus +} +#endif + +#endif /* GPIO_PARAMS_H */ +/** @} */ From d4530e9ffb44f8e4bf78e6f99883d92e54615712 Mon Sep 17 00:00:00 2001 From: Gerson Fernando Budke Date: Thu, 6 Jul 2023 18:18:07 +0200 Subject: [PATCH 6/7] cpu/avr8_common: Fix PM and Common peripherals build Co-authored-by: Marian Buschsieweke Signed-off-by: Gerson Fernando Budke --- cpu/avr8_common/Kconfig | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/cpu/avr8_common/Kconfig b/cpu/avr8_common/Kconfig index 03a0f40b5a9fe..7bd1c5f4672ec 100644 --- a/cpu/avr8_common/Kconfig +++ b/cpu/avr8_common/Kconfig @@ -12,6 +12,8 @@ config CPU_ARCH_AVR8 select HAS_ARCH_AVR8 select HAS_PERIPH_PM + select MODULE_AVR8_COMMON_PERIPH if TEST_KCONFIG + select MODULE_PM_LAYERED if TEST_KCONFIG select MODULE_MALLOC_THREAD_SAFE if TEST_KCONFIG select MODULE_TINY_STRERROR_AS_STRERROR if TEST_KCONFIG # static C++ constructors need guards for thread safe initialization @@ -41,6 +43,13 @@ config MODULE_AVR8_COMMON help AVR-8 common code. + config MODULE_AVR8_COMMON_PERIPH + bool + depends on TEST_KCONFIG + default y + help + Common peripheral drivers used across different AVR-8 MCU families. + # the atmel port uses stdio_uart by default choice STDIO_IMPLEMENTATION default MODULE_STDIO_UART From 3ac9e19a61ffbec8a59148bc726a206759a478db Mon Sep 17 00:00:00 2001 From: Gerson Fernando Budke Date: Wed, 29 Nov 2023 18:52:00 +0100 Subject: [PATCH 7/7] cpu/atxmega: Drop unnecessary test config Co-authored-by: MrKevinWeiss Signed-off-by: Gerson Fernando Budke --- cpu/atxmega/Makefile.features | 6 ------ 1 file changed, 6 deletions(-) diff --git a/cpu/atxmega/Makefile.features b/cpu/atxmega/Makefile.features index d566d9ce38b85..7395748241293 100644 --- a/cpu/atxmega/Makefile.features +++ b/cpu/atxmega/Makefile.features @@ -12,9 +12,3 @@ FEATURES_PROVIDED += periph_cpuid FEATURES_PROVIDED += periph_gpio periph_gpio_irq FEATURES_PROVIDED += periph_nvm FEATURES_PROVIDED += periph_timer periph_timer_periodic - -# Add atxmega configurations. This configuration enables modules that are only available when -# using Kconfig module modelling -ifeq (1, $(TEST_KCONFIG)) - KCONFIG_CPU_CONFIG += $(RIOTCPU)/atxmega/atxmega.config -endif