libyggdrasil  v1.0.0
hash.hpp
Go to the documentation of this file.
1  /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * _____.___. .___ .__.__ *
3  * \__ | | ____ ____ __| _/___________ _____|__| | *
4  * / | |/ ___\ / ___\ / __ |\_ __ \__ \ / ___/ | | *
5  * \____ / /_/ > /_/ > /_/ | | | \// __ \_\___ \| | |__ *
6  * / ______\___ /\___ /\____ | |__| (____ /____ >__|____/ *
7  * \/ /_____//_____/ \/ \/ \/ *
8  * - Asgard - *
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 #include <type_traits>
36 
37 namespace bsp::asg_coproc::drv {
38 
44  template<addr_t BaseAddress>
45  struct Hash {
46  Hash() = delete;
47  Hash(const Hash&) = delete;
48  auto operator=(const Hash&) = delete;
49 
55  static bool init() {
56  return true;
57  }
58 
64  static bool deinit() {
65  return true;
66  }
67 
75  static u8 getCRC8(const auto &data, u8 initValue = 0x00, u8 polynomial = 0xD5) noexcept {
76  POLYSIZE = 0b10; // 8 Bit polynomial
77  return calculate(data, initValue, polynomial) & 0xFF;
78  }
79 
87  static u16 getCRC16(const auto &data, u16 initValue = 0x00, u16 polynomial = 0x8005) noexcept {
88  POLYSIZE = 0b01; // 16 Bit polynomial
89  return calculate(data, initValue, polynomial) & 0xFFFF;
90  }
91 
99  static u32 getCRC32(const auto &data, u32 initValue = 0x00, u32 polynomial = 0x04C11DB7) noexcept {
100  POLYSIZE = 0b00; // 32 Bit polynomial
101  return calculate(data, initValue, polynomial) & 0xFFFF'FFFF;
102  }
103 
104  private:
108  enum class RegisterMap : u8 {
109  DR = 0x00, ///< Data register
110  IDR = 0x04, ///< Independent data register
111  CR = 0x08, ///< Control register
112  INIT = 0x10, ///< Initial CRC value
113  POL = 0x14 ///< CRC polynomial
114  };
115 
116  using DR = Register<BaseAddress, RegisterMap::DR, u32>;
117  using IDR = Register<BaseAddress, RegisterMap::IDR, u32>;
118  using CR = Register<BaseAddress, RegisterMap::CR, u32>;
119  using INIT = Register<BaseAddress, RegisterMap::INIT, u32>;
120  using POL = Register<BaseAddress, RegisterMap::POL, u32>;
121 
122  static inline auto Data = typename DR::template Field<0, 31>(); ///< Data register
123 
124  static inline auto RESET = typename IDR::template Field<0, 0>(); ///< Reset bit
125  static inline auto POLYSIZE = typename IDR::template Field<3, 4>(); ///< Polynomial size (7, 8, 16 or 32 bit)
126 
127  static inline auto InitialValue = typename INIT::template Field<0, 31>(); ///< Used to write the CRC initial value.
128  static inline auto Polynomial = typename POL::template Field<0, 31>(); ///< Used to write the coefficients of the polynomial to be used for CRC calculation
129 
130 
139  static u32 calculate(const auto &data, u32 initValue, u32 polynomial) noexcept {
140  InitialValue = initValue; // Set the initial value register
141  Polynomial = polynomial; // Set the polynomial to the register
142  RESET = true; // Reset to force the CRC to update the set registers
143 
144  std::array<u8, sizeof(data)> bytes;
145  std::memcpy(bytes.data(), &data, bytes.size());
146 
147  for (u32 i = 0; i < bytes.size(); i += sizeof(u32)) {
148  if (bytes.size() >= sizeof(u32)) // Check if 4 or more bytes are left
149  Data = byteSwap(*reinterpret_cast<u32*>(&bytes[i])); // Write new data to the CRC
150  else { // When less than 4 bytes are left
151  u32 value = 0;
152  std::memcpy(&value, &bytes[i], bytes.size() - i); // Copy the left bytes to the local 32 bit variable
153  Data = byteSwap(value); // Write the last bytes to the CRC
154  }
155  }
156 
157  return Data; // Return the calculated CRC
158  }
159 
160  };
161 
162 }
bsp::asg_coproc::drv::Hash::deinit
static bool deinit()
Deinit function.
Definition: hash.hpp:64
bsp::asg_coproc::drv::Hash::Hash
Hash()=delete
u16
uint16_t u16
Definition: types.h:37
u8
uint8_t u8
Unsigned integer definitions.
Definition: types.h:36
bsp::asg_coproc::drv::Hash::getCRC16
static u16 getCRC16(const auto &data, u16 initValue=0x00, u16 polynomial=0x8005) noexcept
Hardware accelerated CRC16 caluclation.
Definition: hash.hpp:87
bsp::asg_coproc::drv
Definition: adc.hpp:32
bsp::asg_coproc::drv::Hash::operator=
auto operator=(const Hash &)=delete
u32
uint32_t u32
Definition: types.h:38
registers.hpp
Zero-cost abstraction for accessing registers and bits/bitfields within them.
bsp::asg_coproc::drv::Hash::Hash
Hash(const Hash &)=delete
bsp::asg_coproc::drv::Hash::getCRC32
static u32 getCRC32(const auto &data, u32 initValue=0x00, u32 polynomial=0x04C11DB7) noexcept
Hardware accelerated CRC32 caluclation.
Definition: hash.hpp:99
bsp::asg_coproc::drv::Hash
CRC abstraction for Asgard.
Definition: hash.hpp:45
hash.hpp
Frontend for the HASH abstraction.
attributes.hpp
Commonly used C++ and GNU attributes.
bsp::asg_coproc::drv::Hash::init
static bool init()
Init function.
Definition: hash.hpp:55
bsp::asg_coproc::drv::Hash::getCRC8
static u8 getCRC8(const auto &data, u8 initValue=0x00, u8 polynomial=0xD5) noexcept
Hardware accelerated CRC8 caluclation.
Definition: hash.hpp:75