UoS³ Flight Computer Firmware
 All Data Structures Files Functions Groups Pages
crc.c
1 #include "../../firmware.h"
2 #include "../../test.h"
3 
4 /* Test inputs */
5 const uint8_t crc_test_1[] = {
6  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
7  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
8  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
9  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
10  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
11 };
12 const uint8_t crc_test_2[] = {
13  0x7F, 0x9C, 0x2B, 0xA4, 0xE8, 0x8F, 0x82, 0x7D,
14  0x61, 0x60, 0x45, 0x50, 0x76, 0x05, 0x85, 0x3E,
15  0x13, 0x1A, 0xB8, 0xD2, 0xB5, 0x94, 0x94, 0x6B,
16  0x9C, 0x81, 0x33, 0x3F, 0x9B, 0xB6, 0xE0, 0xCE,
17  0xF4, 0x3E, 0xA7, 0xA3, 0x36, 0x9D, 0x4B, 0xAA
18 };
19 const uint8_t crc_test_3[] = {'1','2','3','4','5','6','7','8','9'};
20 
21 
22 // CRC-8
23 
24 const uint8_t crc8_result_1 = 0x00;
25 
26 const uint8_t crc8_result_2 = 0x18;
27 
28 const uint8_t crc8_result_3 = 0xF4;
29 
30 // CRC-16-CCITT-FALSE
31 
32 const uint16_t crc16_result_1 = 0x570D;
33 
34 const uint16_t crc16_result_2 = 0x5264;
35 
36 const uint16_t crc16_result_3 = 0x29B1;
37 
38 // CRC-32-CCITT
39 
40 const uint32_t crc32_result_1 = 0x1F877C1E;
41 
42 const uint32_t crc32_result_2 = 0xF62AE45F;
43 
44 const uint32_t crc32_result_3 = 0xCBF43926;
45 
46 bool test_crc(void)
47 {
48  uint8_t crc8_result;
49  uint16_t crc16_result;
50  uint32_t crc32_result;
51 
52  /* CRC-8 Tests */
53 
54  crc8_result = Util_crc8(crc_test_1, sizeof(crc_test_1));
55  if(crc8_result != crc8_result_1)
56  {
57  if(TEST_VERBOSE)
58  {
59  Debug_print("CRC-8 Test 1: Expected 0x%02x, Got 0x%02x\r\n"
60  , crc8_result_1
61  , crc8_result
62  );
63  }
64  return false;
65  }
66 
67  crc8_result = Util_crc8(crc_test_2, sizeof(crc_test_2));
68  if(crc8_result != crc8_result_2)
69  {
70  if(TEST_VERBOSE)
71  {
72  Debug_print("CRC-8 Test 2: Expected 0x%02x, Got 0x%02x\r\n"
73  , crc8_result_2
74  , crc8_result
75  );
76  }
77  return false;
78  }
79 
80  crc8_result = Util_crc8(crc_test_3, sizeof(crc_test_3));
81  if(crc8_result != crc8_result_3)
82  {
83  if(TEST_VERBOSE)
84  {
85  Debug_print("CRC-8 Test 3: Expected 0x%02x, Got 0x%02x\r\n"
86  , crc8_result_3
87  , crc8_result
88  );
89  }
90  return false;
91  }
92 
93  /* CRC-16-CCITT-FALSE Tests */
94 
95  crc16_result = Util_crc16(crc_test_1, sizeof(crc_test_1));
96  if(crc16_result != crc16_result_1)
97  {
98  if(TEST_VERBOSE)
99  {
100  Debug_print("CRC-16-CCITT-FALSE Test 1: Expected 0x%04x, Got 0x%04x\r\n"
101  , crc16_result_1
102  , crc16_result
103  );
104  }
105  return false;
106  }
107 
108  crc16_result = Util_crc16(crc_test_2, sizeof(crc_test_2));
109  if(crc16_result != crc16_result_2)
110  {
111  if(TEST_VERBOSE)
112  {
113  Debug_print("CRC-16-CCITT-FALSE Test 2: Expected 0x%04x, Got 0x%04x\r\n"
114  , crc16_result_2
115  , crc16_result
116  );
117  }
118  return false;
119  }
120 
121  crc16_result = Util_crc16(crc_test_3, sizeof(crc_test_3));
122  if(crc16_result != crc16_result_3)
123  {
124  if(TEST_VERBOSE)
125  {
126  Debug_print("CRC-16-CCITT-FALSE Test 3: Expected 0x%04x, Got 0x%04x\r\n"
127  , crc16_result_3
128  , crc16_result
129  );
130  }
131  return false;
132  }
133 
134  /* CRC-32 Tests */
135 
136  crc32_result = Util_crc32(crc_test_1, sizeof(crc_test_1));
137  if(crc32_result != crc32_result_1)
138  {
139  if(TEST_VERBOSE)
140  {
141  Debug_print("CRC-32 Test 1: Expected 0x%08x, Got 0x%08x\r\n"
142  , crc32_result_1
143  , crc32_result
144  );
145  }
146  return false;
147  }
148 
149  crc32_result = Util_crc32(crc_test_2, sizeof(crc_test_2));
150  if(crc32_result != crc32_result_2)
151  {
152  if(TEST_VERBOSE)
153  {
154  Debug_print("CRC-32 Test 2: Expected 0x%08x, Got 0x%08x\r\n"
155  , crc32_result_2
156  , crc32_result
157  );
158  }
159  return false;
160  }
161 
162  crc32_result = Util_crc32(crc_test_3, sizeof(crc_test_3));
163  if(crc32_result != crc32_result_3)
164  {
165  if(TEST_VERBOSE)
166  {
167  Debug_print("CRC-32 Test 3: Expected 0x%08x, Got 0x%08x\r\n"
168  , crc32_result_3
169  , crc32_result
170  );
171  }
172  return false;
173  }
174 
175  return true;
176 }
void Debug_print(char *fmt,...)
Definition: debug.c:16
uint16_t Util_crc16(const uint8_t *buf, uint32_t size)
Definition: crc.c:100
uint32_t Util_crc32(const uint8_t *buf, uint32_t size)
Definition: crc.c:112
uint8_t Util_crc8(const uint8_t *buf, uint32_t size)
Definition: crc.c:88