libyggdrasil  v1.0.0
rng.hpp
Go to the documentation of this file.
1  /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * _____.___. .___ .__.__ *
3  * \__ | | ____ ____ __| _/___________ _____|__| | *
4  * / | |/ ___\ / ___\ / __ |\_ __ \__ \ / ___/ | | *
5  * \____ / /_/ > /_/ > /_/ | | | \// __ \_\___ \| | |__ *
6  * / ______\___ /\___ /\____ | |__| (____ /____ >__|____/ *
7  * \/ /_____//_____/ \/ \/ \/ *
8  * - Midgard - *
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 
29 
30 #include <cpp/common/registers.hpp>
32 
33 #include <array>
34 #include <cmath>
35 
36 namespace bsp::mid::drv {
37 
44  template<addr_t BaseAddress>
45  struct Random {
46  Random() = delete;
47  Random(const Random&) = delete;
48  auto operator=(const Random&) = delete;
49 
55  static bool init() {
56  return true;
57  }
58 
64  static bool deinit() {
65  return true;
66  }
67 
73  template<typename T>
74  [[nodiscard]] static T get() noexcept {
75  T data;
76 
77  RNGEN = true; // Enable rng
78  for (u32 offset = 0; offset < sizeof(T); offset += sizeof(u32)) { // Always get 4 baytes of random data
79  while (!DRDY); // Wait for the rng to finish
80 
81  u32 rng = RNGDATA; // Get random data (4 bytes)
82  std::memcpy(&data, &rng, std::min(u32(sizeof(u32)), sizeof(T) - offset)); // Fill up 4 bytes to the Type T
83  }
84  RNGEN = false; // Disable rng
85 
86  return data; // Return random data
87  }
88 
89  private:
90  enum class RegisterMap : u8 {
91  CR = 0x00,
92  SR = 0x04,
93  DR = 0x08,
94 
95  };
96 
100 
101  static inline auto RNGEN = typename CR::template Field<2, 2>();
102  static inline auto DRDY = typename SR::template Field<0, 0>();
103  static inline auto RNGDATA = typename DR::template Field<0, 31>();
104  };
105 
106 }
bsp::mid::drv::Random::get
static T get() noexcept
Get random values seeded by true entropy.
Definition: rng.hpp:74
bsp::Register
MMIO Register abstraction. Gives access to bitfields within the register as well as a reference to th...
Definition: registers.hpp:90
rng.hpp
Frontend for the RNG abstraction.
u8
uint8_t u8
Unsigned integer definitions.
Definition: types.h:36
u32
uint32_t u32
Definition: types.h:38
bsp::mid::drv::Random
RNG abstraction.
Definition: rng.hpp:45
RegisterMap
RegisterMap
Register map.
Definition: hash.cpp:42
bsp::mid::drv::Random::Random
Random(const Random &)=delete
registers.hpp
Zero-cost abstraction for accessing registers and bits/bitfields within them.
bsp::mid::drv::Random::Random
Random()=delete
bsp::mid::drv::Random::deinit
static bool deinit()
Deinit function.
Definition: rng.hpp:64
attributes.hpp
Commonly used C++ and GNU attributes.
bsp::mid::drv::Random::operator=
auto operator=(const Random &)=delete
bsp::mid::drv::Random::init
static bool init()
Init function.
Definition: rng.hpp:55
bsp::mid::drv
Definition: adc.hpp:32