Created
May 20, 2018 18:04
-
-
Save hikilaka/6102e1d0fe3f7dda33bafd93f13b2ea5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef SYSD_REGISTER_HPP | |
#define SYSD_REGISTER_HPP | |
#pragma once | |
#include <cstdint> | |
#include <type_traits> | |
#include "sysd/utility.hpp" | |
namespace sysd { | |
template <typename T> | |
using add_vptr_t = std::add_pointer_t<std::add_volatile_t<T>>; | |
template <typename T, | |
sysd::add_vptr_t<std::decay_t<T>> addr> | |
struct reg { | |
constexpr void set(T value) const noexcept { | |
(*addr) = value; | |
} | |
constexpr T& get() const noexcept { | |
return *addr; | |
} | |
template <std::uint8_t... bits> | |
constexpr void set_bits() const noexcept { | |
((*addr |= bits), ...); | |
} | |
template <std::uint8_t... bits> | |
constexpr void unset_bits() const noexcept { | |
((*addr &= ~bits), ...); | |
} | |
template <std::uint8_t... bits> | |
constexpr void toggle_bits() const noexcept { | |
((*addr ^= bits), ...); | |
} | |
}; | |
// register aliases | |
template <volatile std::uint8_t *addr> | |
using register8 = reg<std::uint8_t, addr>; | |
template <volatile std::uint_fast16_t *addr> | |
using register16 = reg<std::uint_fast16_t, addr>; | |
template <volatile std::uint_fast32_t *addr> | |
using register32 = reg<std::uint_fast32_t, addr>; | |
// register definitions | |
constexpr sysd::register8<&P1IN> port1_in{}; | |
constexpr sysd::register8<&P1OUT> port1_out{}; | |
constexpr sysd::register8<&P1DIR> port1_dir{}; | |
constexpr sysd::register16<&WDTCTL> watchdog_ctrl{}; | |
// ... continued definitions of memory-mapped registers | |
} | |
#endif /* SYSD_REGISTER_HPP */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment