From b4f6e51f115a7c7191ffa58f0c04df29a4051a2b Mon Sep 17 00:00:00 2001 From: Ricardo Cosme Date: Fri, 22 Sep 2023 13:19:01 -0300 Subject: [PATCH] Add reg::io_addr() and AVR_IO_SFR_OFFSET + Add a method to return the register address inside the I/O space. + Define a macro for each supported MCU to indicate the memory offset to map an I/O register into data memory space. --- include/avr/io/mcu/atmega328p.hpp | 2 ++ include/avr/io/mcu/attiny13a.hpp | 2 ++ include/avr/io/mcu/attiny85.hpp | 2 ++ include/avr/io/register/reg.hpp | 8 ++++++++ 4 files changed, 14 insertions(+) diff --git a/include/avr/io/mcu/atmega328p.hpp b/include/avr/io/mcu/atmega328p.hpp index fb2531b..ff4598b 100644 --- a/include/avr/io/mcu/atmega328p.hpp +++ b/include/avr/io/mcu/atmega328p.hpp @@ -1,4 +1,6 @@ #pragma once +#define AVR_IO_SFR_OFFSET 0x20 + #include "avr/io/mcu/atmega328p/pins.hpp" #include "avr/io/mcu/atmega328p/regs.hpp" diff --git a/include/avr/io/mcu/attiny13a.hpp b/include/avr/io/mcu/attiny13a.hpp index 886e5d2..7df611b 100644 --- a/include/avr/io/mcu/attiny13a.hpp +++ b/include/avr/io/mcu/attiny13a.hpp @@ -1,4 +1,6 @@ #pragma once +#define AVR_IO_SFR_OFFSET 0x20 + #include "avr/io/mcu/attiny13a/pins.hpp" #include "avr/io/mcu/attiny13a/regs.hpp" diff --git a/include/avr/io/mcu/attiny85.hpp b/include/avr/io/mcu/attiny85.hpp index 5b8fb40..2a97eb0 100644 --- a/include/avr/io/mcu/attiny85.hpp +++ b/include/avr/io/mcu/attiny85.hpp @@ -1,4 +1,6 @@ #pragma once +#define AVR_IO_SFR_OFFSET 0x20 + #include "avr/io/mcu/attiny85/pins.hpp" #include "avr/io/mcu/attiny85/regs.hpp" diff --git a/include/avr/io/register/reg.hpp b/include/avr/io/register/reg.hpp index 776f4ad..8d619d1 100644 --- a/include/avr/io/register/reg.hpp +++ b/include/avr/io/register/reg.hpp @@ -20,6 +20,14 @@ struct reg : detail::reg_base { /** Memory address of the register. */ static constexpr uint8_t addr = address; + /** Return the register address inside the I/O space. */ + static constexpr uint8_t io_addr() { + static_assert(addr < 0x60, + "data memory address greater than or equal to 0x60 doesn't correspond" \ + " to a register with an address on I/O space."); + return addr - AVR_IO_SFR_OFFSET; + } + /** Returns a reference to read/write. */ static volatile uint8_t& ref() noexcept { return *reinterpret_cast(addr); }