libyggdrasil  v1.0.0
registers.hpp
Go to the documentation of this file.
1  /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * _____.___. .___ .__.__ *
3  * \__ | | ____ ____ __| _/___________ _____|__| | *
4  * / | |/ ___\ / ___\ / __ |\_ __ \__ \ / ___/ | | *
5  * \____ / /_/ > /_/ > /_/ | | | \// __ \_\___ \| | |__ *
6  * / ______\___ /\___ /\____ | |__| (____ /____ >__|____/ *
7  * \/ /_____//_____/ \/ \/ \/ *
8  * - Common - *
9  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
10  * This software can be used by students and other personal of the *
11  * Bern University of Applied Sciences under the terms of the MIT *
12  * license. *
13  * For other persons this software is under the terms of the GNU *
14  * General Public License version 2. *
15  * *
16  * Copyright © 2021, Bern University of Applied Sciences. *
17  * All rights reserved. *
18  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
26 #pragma once
27 
28 #include <cpp/common/types.hpp>
30 
31 #include <type_traits> // Type traits
32 
33 namespace bsp {
34 
44  template<u32 BaseAddress, typename SizeType, u8 From, u8 To>
45  struct BitField {
47  static constexpr SizeType Mask = ((1ULL << u64(To - From + 1)) - 1) << From;
48 
49  BitField() = default;
50 
56  [[nodiscard]] ALWAYS_INLINE constexpr operator SizeType() const {
57  return (*reinterpret_cast<IO<SizeType>*>(BaseAddress) & Mask) >> From;
58  }
59 
67  template<typename T>
68  ALWAYS_INLINE constexpr T operator=(T value) const {
69 
70  IO<SizeType>& registerRef = *reinterpret_cast<IO<SizeType>*>(BaseAddress);
71  SizeType bitfieldValue = (registerRef & ~Mask) | ((static_cast<SizeType>(value) << From) & Mask);
72 
73  registerRef = bitfieldValue;
74 
75  return bitfieldValue;
76  }
77 
78  static_assert(From <= To, "Start bit needs to be lower than end bit");
79  };
80 
89  template<addr_t BaseAddress, auto Offset, typename SizeType = reg_t>
90  struct Register {
92  static inline auto& Value = *reinterpret_cast<IO<SizeType>*>(BaseAddress + static_cast<addr_t>(Offset));
93 
100  template<u8 From, u8 To>
101  using Field = BitField<BaseAddress + static_cast<addr_t>(Offset), SizeType, From, To>;
102 
103  static_assert((BaseAddress % sizeof(SizeType)) == 0, "MMIO Register address is not correctly aligned");
104  };
105 
106 }
bsp::Register
MMIO Register abstraction. Gives access to bitfields within the register as well as a reference to th...
Definition: registers.hpp:90
types.hpp
Commonly used type definitions and helper templates.
bsp::IO
volatile T IO
MMIO type template.
Definition: types.hpp:59
u64
uint64_t u64
Definition: types.h:39
ALWAYS_INLINE
#define ALWAYS_INLINE
Definition: attributes.h:34
bsp::Register::Value
static auto & Value
Reference to the register value.
Definition: registers.hpp:92
attributes.hpp
Commonly used C++ and GNU attributes.
bsp::BitField
Bitfield abstraction. Provides a way to read from, write to and pass around bitfields within register...
Definition: registers.hpp:45
bsp
Definition: cortex.hpp:33
bsp::BitField::BitField
BitField()=default
bsp::BitField::Mask
static constexpr SizeType Mask
Mask to access the bitfield's bits.
Definition: registers.hpp:47
bsp::addr_t
std::uint32_t addr_t
ARM32 specific types.
Definition: types.hpp:90
bsp::BitField::operator=
constexpr ALWAYS_INLINE T operator=(T value) const
Assignment operator overload.
Definition: registers.hpp:68