crc.h
Go to the documentation of this file.
1 //======================================================================
29 //======================================================================
30 
31 #ifndef CRC_h_
32 #define CRC_h_
33 
34 #include "sdhlibrary_settings.h"
35 
36 //----------------------------------------------------------------------
37 // System Includes - include with <>
38 //----------------------------------------------------------------------
39 
40 
41 //----------------------------------------------------------------------
42 // Project Includes - include with ""
43 //----------------------------------------------------------------------
44 
45 #include "basisdef.h"
46 
47 //----------------------------------------------------------------------
48 // Defines, enums, unions, structs
49 //----------------------------------------------------------------------
50 
52 
53 
54 typedef UInt16 tCRCValue;
55 
56 //----------------------------------------------------------------------
57 // Global variables (declarations)
58 //----------------------------------------------------------------------
59 
60 
61 //----------------------------------------------------------------------
62 // External functions (function declarations)
63 //----------------------------------------------------------------------
64 
65 
66 //----------------------------------------------------------------------
67 // Function prototypes (function declarations)
68 //----------------------------------------------------------------------
69 
70 
71 //----------------------------------------------------------------------
72 // Class declarations
73 //----------------------------------------------------------------------
74 
82 class VCC_EXPORT cCRC
83 {
84  protected:
87 
90 
93 
94  public:
96  cCRC( tCRCValue const* _crc_table, tCRCValue _initial_value )
97  {
98  crc_table = _crc_table;
99  initial_value = _initial_value;
100  current_crc = initial_value;
101  }
102 
104  tCRCValue AddByte( unsigned char byte )
105  {
106  current_crc = ( (current_crc & 0xFF00) >> 8 ) ^ crc_table[ ( current_crc & 0x00FF ) ^ (byte & 0x00FF)];
107  return current_crc;
108  }
109 
111  tCRCValue AddBytes( unsigned char* bytes, int nb_bytes )
112  {
113  for ( int i=0; i<nb_bytes; i++ )
114  current_crc = ( (current_crc & 0xFF00) >> 8 ) ^ crc_table[ ( current_crc & 0x00FF ) ^ (bytes[i] & 0x00FF)];
115  return current_crc;
116  }
117 
119  inline tCRCValue GetCRC()
120  {
121  return current_crc;
122  }
123 
125  inline UInt8 GetCRC_LB()
126  {
127  return current_crc & 0x00ff;
128  }
129 
131  inline UInt8 GetCRC_HB()
132  {
133  return (current_crc >> 8) & 0x00ff;
134  }
135 
137  inline tCRCValue Reset()
138  {
139  current_crc = initial_value;
140  return current_crc;
141  }
142 };
143 //----------------------------------------------------------------------
144 //----------------------------------------------------------------------
145 
147 class VCC_EXPORT cCRC_DSACON32m : public cCRC
148 {
149  protected:
151  static tCRCValue const crc_table_dsacon32m[256];
152 
153  public:
155  inline cCRC_DSACON32m( void )
156  : cCRC( crc_table_dsacon32m, 0xffff )
157  {
158  // nothing more to do
159  }
160 };
161 //----------------------------------------------------------------------
162 //----------------------------------------------------------------------
163 
170 class VCC_EXPORT cCRC_SDH : public cCRC_DSACON32m
171 {
172  public:
174  inline cCRC_SDH( void )
175  : cCRC_DSACON32m()
176  {
177  // nothing more to do
178  }
179 };
180 //----------------------------------------------------------------------
181 //----------------------------------------------------------------------
182 
184 
185 #endif
186 
187 
188 //======================================================================
189 /*
190  Here are some settings for the emacs/xemacs editor (and can be safely ignored):
191  (e.g. to explicitely set C++ mode for *.h header files)
192 
193  Local Variables:
194  mode:C
195  mode:ELSE
196  End:
197 */
198 //======================================================================}
cCRC::cCRC
cCRC(tCRCValue const *_crc_table, tCRCValue _initial_value)
constructor: create a new cCRC object and initialize the current value of the CRC checksum....
Definition: crc.h:96
cCRC::crc_table
tCRCValue const * crc_table
table with precalculated CRC values
Definition: crc.h:92
cCRC::AddByte
tCRCValue AddByte(unsigned char byte)
insert byte into CRC calculation and return the new current CRC checksum
Definition: crc.h:104
NAMESPACE_SDH_END
#define NAMESPACE_SDH_END
Definition: sdhlibrary_settings.h:80
cCRC::current_crc
tCRCValue current_crc
current value of the CRC checksum
Definition: crc.h:86
cCRC::GetCRC
tCRCValue GetCRC()
return the current CRC value
Definition: crc.h:119
cCRC_DSACON32m::cCRC_DSACON32m
cCRC_DSACON32m(void)
constructor to create a cCRC object suitable for checksumming the communication with a DSACON32m tact...
Definition: crc.h:155
UInt8
uint8_t UInt8
unsigned integer, size 1 Byte (8 Bit)
Definition: basisdef.h:61
tCRCValue
NAMESPACE_SDH_START typedef UInt16 tCRCValue
the data type used to calculate and exchange CRC values with DSACON32m (16 bit integer)
Definition: crc.h:54
UInt16
uint16_t UInt16
unsigned integer, size 2 Byte (16 Bit)
Definition: basisdef.h:63
cCRC_SDH::cCRC_SDH
cCRC_SDH(void)
constructor to create a cCRC object suitable for checksumming the binary communication with SDH
Definition: crc.h:174
cCRC
Cyclic Redundancy Code checker class, used for protecting communication against transmission errors.
Definition: crc.h:82
cCRC::GetCRC_LB
UInt8 GetCRC_LB()
return the low byte of the current CRC value
Definition: crc.h:125
cCRC::AddBytes
tCRCValue AddBytes(unsigned char *bytes, int nb_bytes)
insert nb_bytes from bytes into CRC calculation and return the new current CRC checksum
Definition: crc.h:111
cCRC_SDH
A derived CRC class that uses a CRC table and initial value suitable for protecing the binary communi...
Definition: crc.h:170
NAMESPACE_SDH_START
#define NAMESPACE_SDH_START
Definition: sdhlibrary_settings.h:79
cCRC::initial_value
tCRCValue initial_value
initial value of the CRC checksum
Definition: crc.h:89
basisdef.h
This file contains some basic definitions (defines, macros, datatypes)
sdhlibrary_settings.h
This file contains settings to make the SDHLibrary compile on differen systems:
cCRC::GetCRC_HB
UInt8 GetCRC_HB()
return the high byte of the current CRC value
Definition: crc.h:131
cCRC::Reset
tCRCValue Reset()
reset the current CRC value to its initial value and return it;
Definition: crc.h:137
cCRC_DSACON32m
A derived CRC class that uses a CRC table and initial value suitable for the Weiss Robotics DSACON32m...
Definition: crc.h:147


sdhlibrary_cpp
Author(s): Dirk Osswald
autogenerated on Wed Mar 2 2022 01:00:58