Skip to content

Commit

Permalink
Add reg::io_addr() and AVR_IO_SFR_OFFSET
Browse files Browse the repository at this point in the history
+ 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.
  • Loading branch information
ricardocosme committed Sep 22, 2023
1 parent c90595c commit b4f6e51
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/avr/io/mcu/atmega328p.hpp
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 2 additions & 0 deletions include/avr/io/mcu/attiny13a.hpp
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 2 additions & 0 deletions include/avr/io/mcu/attiny85.hpp
Original file line number Diff line number Diff line change
@@ -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"
8 changes: 8 additions & 0 deletions include/avr/io/register/reg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<volatile uint8_t*>(addr); }
Expand Down

0 comments on commit b4f6e51

Please sign in to comment.