crc.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
3  */
4 
5 #ifndef UAVCAN_TRANSPORT_CRC_HPP_INCLUDED
6 #define UAVCAN_TRANSPORT_CRC_HPP_INCLUDED
7 
8 #include <cassert>
9 #include <uavcan/std.hpp>
10 #include <uavcan/build_config.hpp>
11 
12 namespace uavcan
13 {
14 
29 {
30 #if !UAVCAN_TINY
31  static const uint16_t Table[256];
32 #endif
33 
35 
36 public:
37  enum { NumBytes = 2 };
38 
40  : value_(0xFFFFU)
41  { }
42 
43 #if UAVCAN_TINY
44  void add(uint8_t byte)
45  {
46  value_ ^= uint16_t(uint16_t(byte) << 8);
47  for (uint8_t bit = 8; bit > 0; --bit)
48  {
49  if (value_ & 0x8000U)
50  {
51  value_ = uint16_t(uint16_t(value_ << 1) ^ 0x1021U);
52  }
53  else
54  {
55  value_ = uint16_t(value_ << 1);
56  }
57  }
58  }
59 #else
60  void add(uint8_t byte)
61  {
62  value_ = uint16_t(uint16_t((value_ << 8) ^ Table[uint16_t((value_ >> 8) ^ byte) & 0xFFU]) & 0xFFFFU);
63  }
64 #endif
65 
66  void add(const uint8_t* bytes, unsigned len)
67  {
68  UAVCAN_ASSERT(bytes);
69  while (len--)
70  {
71  add(*bytes++);
72  }
73  }
74 
75  uint16_t get() const { return value_; }
76 };
77 
78 }
79 
80 #endif // UAVCAN_TRANSPORT_CRC_HPP_INCLUDED
uavcan::TransferCRC::add
void add(const uint8_t *bytes, unsigned len)
Definition: crc.hpp:66
uavcan::TransferCRC::add
void add(uint8_t byte)
Definition: crc.hpp:60
std.hpp
uavcan::TransferCRC::TransferCRC
TransferCRC()
Definition: crc.hpp:39
uavcan::TransferCRC::value_
uint16_t value_
Definition: crc.hpp:34
uavcan::uint16_t
std::uint16_t uint16_t
Definition: std.hpp:25
uavcan::uint8_t
std::uint8_t uint8_t
Definition: std.hpp:24
UAVCAN_EXPORT
#define UAVCAN_EXPORT
Definition: libuavcan/libuavcan/include/uavcan/build_config.hpp:108
build_config.hpp
uavcan
Definition: libuavcan/libuavcan/include/uavcan/build_config.hpp:204
uavcan::TransferCRC
Definition: crc.hpp:28
UAVCAN_ASSERT
#define UAVCAN_ASSERT(x)
Definition: libuavcan/libuavcan/include/uavcan/build_config.hpp:184
uavcan::TransferCRC::get
uint16_t get() const
Definition: crc.hpp:75


uavcan_communicator
Author(s):
autogenerated on Fri Dec 13 2024 03:10:02