Skip to content

Commit

Permalink
Merge pull request #19784 from nandojve/avr8/implement_pm
Browse files Browse the repository at this point in the history
AVR-8: Implement Power Management
  • Loading branch information
benpicco authored Nov 30, 2023
2 parents d73ef09 + 3ac9e19 commit f73c1cf
Show file tree
Hide file tree
Showing 33 changed files with 309 additions and 63 deletions.
6 changes: 4 additions & 2 deletions boards/atmega328p-xplained-mini/Kconfig
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"
5 changes: 5 additions & 0 deletions boards/atmega328p-xplained-mini/Makefile.dep
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
USEMODULE += boards_common_atmega
USEMODULE += atmega_pcint0

ifneq (,$(filter saul_default,$(USEMODULE)))
USEMODULE += saul_gpio
endif
23 changes: 22 additions & 1 deletion boards/atmega328p-xplained-mini/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (C) 2014 Freie Universität Berlin, Hinnerk van Bruinehsen
* 2016 Laurent Navet <[email protected]>
* 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
Expand Down Expand Up @@ -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
Expand Down
53 changes: 53 additions & 0 deletions boards/atmega328p-xplained-mini/include/gpio_params.h
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
*/

#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 */
/** @} */
11 changes: 11 additions & 0 deletions cpu/atmega1281/include/periph_cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
11 changes: 11 additions & 0 deletions cpu/atmega1284p/include/periph_cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
10 changes: 10 additions & 0 deletions cpu/atmega128rfa1/include/periph_cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions cpu/atmega2560/include/periph_cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
11 changes: 11 additions & 0 deletions cpu/atmega256rfr2/include/periph_cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
* @{
Expand Down
11 changes: 11 additions & 0 deletions cpu/atmega328p/include/periph_cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
11 changes: 11 additions & 0 deletions cpu/atmega32u4/include/periph_cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
11 changes: 11 additions & 0 deletions cpu/atmega8/include/periph_cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion cpu/atmega_common/include/cpu_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down
3 changes: 3 additions & 0 deletions cpu/atmega_common/periph/adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions cpu/atmega_common/periph/i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -226,6 +227,7 @@ void i2c_acquire(i2c_t dev)
{
assert(dev < I2C_NUMOF);

pm_block(4); /* Require clkIO */
mutex_lock(&locks[dev]);
}

Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions cpu/atmega_common/periph/spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -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,
Expand Down
1 change: 0 additions & 1 deletion cpu/atxmega/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 0 additions & 3 deletions cpu/atxmega/Makefile.dep
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 0 additions & 7 deletions cpu/atxmega/Makefile.features
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,4 @@ 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
# using Kconfig module modelling
ifeq (1, $(TEST_KCONFIG))
KCONFIG_CPU_CONFIG += $(RIOTCPU)/atxmega/atxmega.config
endif
2 changes: 0 additions & 2 deletions cpu/atxmega/atxmega.config

This file was deleted.

4 changes: 4 additions & 0 deletions cpu/atxmega/include/periph_cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -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*/
/** @} */

/**
Expand Down
Loading

0 comments on commit f73c1cf

Please sign in to comment.