PX4 Firmware
PX4 Autopilot Software http://px4.io
crc.c
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2015 PX4 Development Team. All rights reserved.
4  * Author: Ben Dyer <ben_dyer@mac.com>
5  * Pavel Kirienko <pavel.kirienko@zubax.com>
6  * David Sidrane <david_s5@nscdg.com>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  * 3. Neither the name PX4 nor the names of its contributors may be
19  * used to endorse or promote products derived from this software
20  * without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  ****************************************************************************/
36 
37 /****************************************************************************
38  * Included Files
39  ****************************************************************************/
40 
41 #include <stdint.h>
42 #include <stdlib.h>
43 #include "crc.h"
44 
45 /****************************************************************************
46  * Pre-processor Definitions
47  ****************************************************************************/
48 
49 /****************************************************************************
50  * Private Types
51  ****************************************************************************/
52 
53 /****************************************************************************
54  * Private Function Prototypes
55  ****************************************************************************/
56 
57 /****************************************************************************
58  * Private Data
59  ****************************************************************************/
60 
61 /****************************************************************************
62  * Public Data
63  ****************************************************************************/
64 
65 /****************************************************************************
66  * Private Functions
67  ****************************************************************************/
68 
69 /****************************************************************************
70  * Public Functions
71  ****************************************************************************/
72 
73 /****************************************************************************
74  * Name: crc16_add
75  *
76  * Description:
77  * Use to calculates a CRC-16-CCITT using the polynomial of
78  * 0x1021 by adding a value successive values.
79  *
80  * Input Parameters:
81  * crc - The running total of the crc 16
82  * value - The value to add
83  *
84  * Returned Value:
85  * The current crc16 with the value processed.
86  *
87  ****************************************************************************/
88 
89 uint16_t crc16_add(uint16_t crc, uint8_t value)
90 {
91  uint32_t i;
92  const uint16_t poly = 0x1021u;
93  crc ^= (uint16_t)((uint16_t) value << 8u);
94 
95  for (i = 0; i < 8; i++) {
96  if (crc & (1u << 15u)) {
97  crc = (uint16_t)((crc << 1u) ^ poly);
98 
99  } else {
100  crc = (uint16_t)(crc << 1u);
101  }
102  }
103 
104  return crc;
105 }
106 
107 /****************************************************************************
108  * Name: crc16_signature
109  *
110  * Description:
111  * Calculates a CRC-16-CCITT using the crc16_add
112  * function
113  *
114  * Input Parameters:
115  * initial - The Initial value to uses as the crc's starting point
116  * length - The number of bytes to add to the crc
117  * bytes - A pointer to any array of length bytes
118  *
119  * Returned Value:
120  * The crc16 of the array of bytes
121  *
122  ****************************************************************************/
123 
124 uint16_t crc16_signature(uint16_t initial, size_t length, const uint8_t *bytes)
125 {
126  size_t i;
127 
128  for (i = 0u; i < length; i++) {
129  initial = crc16_add(initial, bytes[i]);
130  }
131 
132  return initial ^ CRC16_OUTPUT_XOR;
133 }
134 
135 /****************************************************************************
136  * Name: crc64_add_word
137  *
138  * Description:
139  * Calculates a CRC-64-WE using the polynomial of 0x42F0E1EBA9EA3693
140  * See http://reveng.sourceforge.net/crc-catalogue/17plus.htm#crc.cat-bits.64
141  * Check: 0x62EC59E3F1A4F00A
142  *
143  * Input Parameters:
144  * crc - The running total of the crc 64
145  * value - The value to add
146  *
147  * Returned Value:
148  * The current crc64 with the value processed.
149  *
150  ****************************************************************************/
151 __EXPORT
152 uint64_t crc64_add_word(uint64_t crc, uint32_t value)
153 {
154  uint32_t i, j;
155  uint8_t byte;
156  const uint64_t poly = 0x42F0E1EBA9EA3693ull;
157 
158  for (j = 0; j < 4; j++) {
159  byte = ((uint8_t *) &value)[j];
160  crc ^= (uint64_t) byte << 56u;
161 
162  for (i = 0; i < 8; i++) {
163  if (crc & (1ull << 63u)) {
164  crc = (uint64_t)(crc << 1u) ^ poly;
165 
166  } else {
167  crc = (uint64_t)(crc << 1u);
168  }
169  }
170  }
171 
172  return crc;
173 }
__EXPORT uint64_t crc64_add_word(uint64_t crc, uint32_t value)
Definition: crc.c:152
Definition: I2C.hpp:51
uint16_t crc16_signature(uint16_t initial, size_t length, const uint8_t *bytes)
Definition: crc.c:124
#define CRC16_OUTPUT_XOR
Definition: crc.h:46
uint16_t crc16_add(uint16_t crc, uint8_t value)
Definition: crc.c:89