11.5.2. test_crc_lfsr.cpp
#
1#include <uhd/utils/safe_main.hpp>
2#include <boost/crc.hpp>
3#include <cstring>
4
5typedef boost::crc_optimal<32, 0x04C11DB7, 0, 0, false, false> crc_32_lfsr_type;
6
7int UHD_SAFE_MAIN(int argc, char *argv[]){
8
9 // Instantiate a crc-32 object
10 crc_32_lfsr_type crc32;
11 unsigned char info[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
12
13 // CRC encode
14 crc32.process_bytes(info, 16);
15
16 // Print codeword
17 std::cout << "Information:\t";
18 for (int i=0; i<16; i++) {
19 std::cout << std::uppercase << std::hex << (unsigned short) info[i];
20 }
21 std::cout << "\nCRC:\t\t" << std::uppercase << std::hex << crc32.checksum() << std::endl;
22 // Append info and CRC to form codeword
23 unsigned char cw[20];
24 memcpy(cw, info, 16);
25 unsigned char crc_pt[4];
26 boost::uint32_t crc_val = crc32.checksum();
27 memcpy(crc_pt, &crc_val, 4);
28 // This is needed because of the byte-reversal order of int
29 memcpy(cw+16, crc_pt+3, 1);
30 memcpy(cw+17, crc_pt+2, 1);
31 memcpy(cw+18, crc_pt+1, 1);
32 memcpy(cw+19, crc_pt, 1);
33 std::cout << "Codeword:\t";
34 for (int i=0; i<20; i++) {
35 std::cout << std::uppercase << std::hex << (unsigned short) cw[i];
36 }
37 std::cout << std::endl;
38
39 // CRC decode: No error
40 std::cout << "\nNo TX error case:\n";
41 crc32.reset();
42 crc32.process_bytes(cw, 20);
43
44 // Compare calculated checksum to 0
45 if (crc32.checksum() == 0) {
46 std::cout << "CRC checked => No error\n";
47 } else {
48 std::cout << "Error detected!\n";
49 }
50
51 // CRC decode: Double error
52 std::cout << "\nDouble TX error case:\n";
53 crc32.reset();
54 cw[2] ^= 0x08;
55 cw[17] ^= 0x10;
56 crc32.process_bytes(cw, 20);
57
58 // Compare calculated checksum to 0
59 if (crc32.checksum() == 0) {
60 std::cout << "CRC checked => No error\n";
61 } else {
62 std::cout << "Error detected!\n";
63 }
64
65 return EXIT_SUCCESS;
66}
In this case, the information and parity-check byte orders are both not reflected. We may pass the whole codeword array into the class method
process_bytes()
. This is the same as the hardware linear feedback shift register implementation and the second decoding method discussed before. Hence, if the checksum is \(0\), then there is no error.
Experiment
Build and run the example above to comprare with the implementation in
test_crc.cpp
.