From b72cafb169b0cd1aa6f0c837a77b34c534e3ea98 Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Mon, 27 Jun 2022 15:58:36 +0200 Subject: [PATCH] cpu/atmega_common: Fix atmega_port_addr() In 04ab5a74f3d7f205aace713701fadb7da4065242 a bug was introduced in the calculation of the GPIO port address by refactoring code. This fixes the issue by extracting the GPIO port first from the pin. --- cpu/atmega_common/include/atmega_gpio.h | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/cpu/atmega_common/include/atmega_gpio.h b/cpu/atmega_common/include/atmega_gpio.h index c57e4a4735..c837a536b4 100644 --- a/cpu/atmega_common/include/atmega_gpio.h +++ b/cpu/atmega_common/include/atmega_gpio.h @@ -24,6 +24,7 @@ #ifndef ATMEGA_GPIO_H #define ATMEGA_GPIO_H +#include #include #include @@ -55,11 +56,11 @@ static inline uint8_t atmega_port_num(gpio_t pin) } /** - * @brief Generate the PORTx address of the give pin. + * @brief Generate the PINx address of the given pin. */ -static inline uint16_t atmega_port_addr(gpio_t pin) +static inline uint16_t atmega_pin_addr(gpio_t pin) { - return (uintptr_t)(&atmega_gpio_port(pin)->port); + return (uintptr_t)atmega_gpio_port(atmega_port_num(pin)); } /** @@ -67,15 +68,15 @@ static inline uint16_t atmega_port_addr(gpio_t pin) */ static inline uint16_t atmega_ddr_addr(gpio_t pin) { - return (atmega_port_addr(pin) - 0x01); + return atmega_pin_addr(pin) + offsetof(atmega_gpio_port_t, ddr); } /** - * @brief Generate the PINx address of the given pin. + * @brief Generate the PORTx address of the give pin. */ -static inline uint16_t atmega_pin_addr(gpio_t pin) +static inline uint16_t atmega_port_addr(gpio_t pin) { - return (atmega_port_addr(pin) - 0x02); + return atmega_pin_addr(pin) + offsetof(atmega_gpio_port_t, port); } #ifdef __cplusplus