AVR Special Function Register Infrastructure¶
This chapter introduces the AVR Special Function Register API. This API provides access to the Special Function Registers of an AVR microcontroller. These registers can be used for controlling various interfaces of an AVR controller.
Usage Example¶
The following code blocks demonstrates how to use the types described in this chapter to access a Special Function Register on an AVR microcontroller:
#include "avr/register.hpp"
struct mega328p {
~mega328p() = delete;
using pinb = pin_register<0x23, 0b11111111>;
using ddrb = ddr_register<0x24, 0b11111111>;
using portb = port_register<0x25, 0b11111111>;
using pinc = pin_register<0x26, 0b01111111>;
using ddrc = ddr_register<0x27, 0b01111111>;
using portc = port_register<0x28, 0b01111111>;
using pind = pin_register<0x29, 0b11111111>;
using ddrd = ddr_register<0x2a, 0b11111111>;
using portd = port_register<0x2b, 0b11111111>;
};
int main() {
if(mega328p::portb::port<3>::get()) {
mega328p::portb::port<7>::set();
} else {
mega328p::portb::port<7>::clear();
}
}
The machine code generated by the above C++ code demonstrates that there is no overhead incurred by the wrappers. At the same time, all accesses to bits and registers are checked at compile time
00000080 <main>:
80: 1b 9b sbis 0x03, 3 ; 3
82: 04 c0 rjmp .+8 ; 0x8c <main+0xc>
84: 1f 9a sbi 0x03, 7 ; 3
86: 90 e0 ldi r25, 0x00 ; 0
88: 80 e0 ldi r24, 0x00 ; 0
8a: 08 95 ret
8c: 1f 98 cbi 0x03, 7 ; 3
8e: fb cf rjmp .-10 ; 0x86 <main+0x6>
Base Type¶
-
template <avr::ptrdiff_t Address, avr::uint_for_size_t< 8 > Bits, avr::uint_for_size_t< Bits > ValidBits>
classspecial_function_register
¶ A safe wrapper for AVR Special Function Registers
This class provides a safe and zero-cost abstraction for the Spcial Purpose Registers found in AVR microcontrollers. All accesses to a register as well as its bits are bound checked, and violations of these bounds will cause compilation to fail.
- Since
- 1.0.0
- Template Parameters
Address
: The register’s address in memoryBase
: The address base of the registerBits
: The register’s size in BitsValidBits
: The available bits in the registerEffectiveAddress
: The actual address of the register in memory space
Subclassed by ro_io_register< Address, Bits, ValidBits >, rw_special_function_register< Address, Bits, ValidBits >
Full Register Access¶
-
static auto
get
()¶ Get the full contents of the register.
This function enables the retrieval of the full contents of the underlying register.
-
static auto
set
(value_type const value)¶ Set the value of the complete register.
Contrary to single-bit modifications via avr::bit, this functions can be used to set the value of the whole register by writing the register’s full content.
Bitwise Register Access¶
Warning
doxygentypedef: Cannot find typedef “avr::rw_special_function_register::bit” in doxygen xml output for project “AVR++” from directory: /home/docs/checkouts/readthedocs.org/user_builds/avrxx/checkouts/latest/doc/xml
Warning
doxygentypedef: Cannot find typedef “avr::ro_special_function_register::bit” in doxygen xml output for project “AVR++” from directory: /home/docs/checkouts/readthedocs.org/user_builds/avrxx/checkouts/latest/doc/xml
Warning
doxygenstruct: Cannot find class “avr::bit” in doxygen xml output for project “AVR++” from directory: /home/docs/checkouts/readthedocs.org/user_builds/avrxx/checkouts/latest/doc/xml
Warning
doxygenstruct: Cannot find class “avr::rw_bit” in doxygen xml output for project “AVR++” from directory: /home/docs/checkouts/readthedocs.org/user_builds/avrxx/checkouts/latest/doc/xml
Specialized Types and Aliases¶
Warning
doxygenstruct: Cannot find class “avr::pin_register” in doxygen xml output for project “AVR++” from directory: /home/docs/checkouts/readthedocs.org/user_builds/avrxx/checkouts/latest/doc/xml
Warning
doxygenstruct: Cannot find class “avr::ddr_register” in doxygen xml output for project “AVR++” from directory: /home/docs/checkouts/readthedocs.org/user_builds/avrxx/checkouts/latest/doc/xml
Warning
doxygenstruct: Cannot find class “avr::port_register” in doxygen xml output for project “AVR++” from directory: /home/docs/checkouts/readthedocs.org/user_builds/avrxx/checkouts/latest/doc/xml