Go to the documentation of this file.
35 #include <type_traits>
44 template<addr_t BaseAddress>
75 static u8 getCRC8(
const auto &data,
u8 initValue = 0x00,
u8 polynomial = 0xD5) noexcept {
77 return calculate(data, initValue, polynomial) & 0xFF;
87 static u16 getCRC16(
const auto &data,
u16 initValue = 0x00,
u16 polynomial = 0x8005) noexcept {
89 return calculate(data, initValue, polynomial) & 0xFFFF;
99 static u32 getCRC32(
const auto &data,
u32 initValue = 0x00,
u32 polynomial = 0x04C11DB7) noexcept {
101 return calculate(data, initValue, polynomial) & 0xFFFF
'FFFF;
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
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>;
122 static inline auto Data = typename DR::template Field<0, 31>(); ///< Data register
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)
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
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
144 std::array<u8, sizeof(data)> bytes;
145 std::memcpy(bytes.data(), &data, bytes.size());
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
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
157 return Data; // Return the calculated CRC
static bool deinit()
Deinit function.
Definition: hash.hpp:64
uint16_t u16
Definition: types.h:37
uint8_t u8
Unsigned integer definitions.
Definition: types.h:36
static u16 getCRC16(const auto &data, u16 initValue=0x00, u16 polynomial=0x8005) noexcept
Hardware accelerated CRC16 caluclation.
Definition: hash.hpp:87
auto operator=(const Hash &)=delete
uint32_t u32
Definition: types.h:38
Zero-cost abstraction for accessing registers and bits/bitfields within them.
Hash(const Hash &)=delete
static u32 getCRC32(const auto &data, u32 initValue=0x00, u32 polynomial=0x04C11DB7) noexcept
Hardware accelerated CRC32 caluclation.
Definition: hash.hpp:99
CRC abstraction for Asgard.
Definition: hash.hpp:45
Frontend for the HASH abstraction.
Commonly used C++ and GNU attributes.
static bool init()
Init function.
Definition: hash.hpp:55
static u8 getCRC8(const auto &data, u8 initValue=0x00, u8 polynomial=0xD5) noexcept
Hardware accelerated CRC8 caluclation.
Definition: hash.hpp:75