EepromSecureData
CRC-controlled EEPROM memory storage
CrcIButton.h
1 /*
2  * Copyright (C) 2023 epsilonrt <epsilonrt@gmail.com>
3  * SPDX-License-Identifier: LGPL-2.1-or-later
4  */
5 #include <stdint.h>
6 #include <stddef.h>
7 #pragma once
8 
14 template<class T>
15 class CrcIButton {
16  public:
20  const static uint8_t InitValue = 0x5A;
21 
25  CrcIButton () : m_ucCrc (InitValue) {}
26 
30  void reset () {
31  m_ucCrc = InitValue;
32  }
33 
38  uint8_t value () const {
39  return m_ucCrc;
40  }
41 
47  void update (const T &value) {
48  const uint8_t *buf = reinterpret_cast<const uint8_t *> (&value);
49  size_t len = sizeof (T);
50  while (len--) {
51  m_ucCrc = CrcIButtonUpdate (m_ucCrc, *buf++);
52  }
53  }
54  private:
55  uint8_t m_ucCrc;
63  static inline uint8_t CrcIButtonUpdate (uint8_t crc, uint8_t data) {
64  unsigned int i;
65 
66  crc = crc ^ data;
67  for (i = 0; i < 8; i++) {
68  if (crc & 0x01) {
69  crc = (crc >> 1) ^ 0x8C;
70  }
71  else {
72  crc >>= 1;
73  }
74  }
75  return crc;
76  }
77 };
iButton CRC template class
Definition: CrcIButton.h:15
void update(const T &value)
Update CRC with a T value.
Definition: CrcIButton.h:47
static const uint8_t InitValue
Initial value of CRC.
Definition: CrcIButton.h:20
CrcIButton()
Construct a new Crc I Button object.
Definition: CrcIButton.h:25
void reset()
Reset CRC value.
Definition: CrcIButton.h:30
uint8_t value() const
Get the value of CRC.
Definition: CrcIButton.h:38