forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrc.h
200 lines (182 loc) · 5.33 KB
/
crc.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* Copyright (c) 2018 Workaround GmbH.
* Copyright (c) 2017 Intel Corporation.
* Copyright (c) 2017 Nordic Semiconductor ASA
* Copyright (c) 2015 Runtime Inc
* Copyright (c) 2018 Google LLC.
*
* SPDX-License-Identifier: Apache-2.0
*/
/** @file
* @brief CRC computation function
*/
#ifndef ZEPHYR_INCLUDE_SYS_CRC_H_
#define ZEPHYR_INCLUDE_SYS_CRC_H_
#include <zephyr/types.h>
#include <stdbool.h>
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
/* Initial value expected to be used at the beginning of the crc8_ccitt
* computation.
*/
#define CRC8_CCITT_INITIAL_VALUE 0xFF
/**
* @defgroup checksum Checksum
*/
/**
* @defgroup crc CRC
* @ingroup checksum
* @{
*/
/**
* @brief Generic function for computing CRC 16
*
* Compute CRC 16 by passing in the address of the input, the input length
* and polynomial used in addition to the initial value.
*
* @param src Input bytes for the computation
* @param len Length of the input in bytes
* @param polynomial The polynomial to use omitting the leading x^16
* coefficient
* @param initial_value Initial value for the CRC computation
* @param pad Adds padding with zeros at the end of input bytes
*
* @return The computed CRC16 value
*/
u16_t crc16(const u8_t *src, size_t len, u16_t polynomial,
u16_t initial_value, bool pad);
/**
* @brief Generic function for computing CRC 8
*
* Compute CRC 8 by passing in the address of the input, the input length
* and polynomial used in addition to the initial value.
*
* @param src Input bytes for the computation
* @param len Length of the input in bytes
* @param polynomial The polynomial to use omitting the leading x^8
* coefficient
* @param initial_value Initial value for the CRC computation
* @param reversed Should we use reflected/reversed values or not
*
* @return The computed CRC8 value
*/
u8_t crc8(const u8_t *src, size_t len, u8_t polynomial, u8_t initial_value,
bool reversed);
/**
* @brief Compute the CRC-16/CCITT checksum of a buffer.
*
* See ITU-T Recommendation V.41 (November 1988). Uses 0x1021 as the
* polynomial, reflects the input, and reflects the output.
*
* To calculate the CRC across non-contiguous blocks use the return
* value from block N-1 as the seed for block N.
*
* For CRC-16/CCITT, use 0 as the initial seed. Other checksums in
* the same family can be calculated by changing the seed and/or
* XORing the final value. Examples include:
*
* - X-25 (used in PPP): seed=0xffff, xor=0xffff, residual=0xf0b8
*
* @note API changed in Zephyr 1.11.
*
* @param seed Value to seed the CRC with
* @param src Input bytes for the computation
* @param len Length of the input in bytes
*
* @return The computed CRC16 value
*/
u16_t crc16_ccitt(u16_t seed, const u8_t *src, size_t len);
/**
* @brief Compute the CRC-16/XMODEM checksum of a buffer.
*
* The MSB first version of ITU-T Recommendation V.41 (November 1988).
* Uses 0x1021 as the polynomial with no reflection.
*
* To calculate the CRC across non-contiguous blocks use the return
* value from block N-1 as the seed for block N.
*
* For CRC-16/XMODEM, use 0 as the initial seed. Other checksums in
* the same family can be calculated by changing the seed and/or
* XORing the final value. Examples include:
*
* - CCIITT-FALSE: seed=0xffff
* - GSM: seed=0, xorout=0xffff, residue=0x1d0f
*
* @param seed Value to seed the CRC with
* @param src Input bytes for the computation
* @param len Length of the input in bytes
*
* @return The computed CRC16 value
*/
u16_t crc16_itu_t(u16_t seed, const u8_t *src, size_t len);
/**
* @brief Compute ANSI variant of CRC 16
*
* ANSI variant of CRC 16 is using 0x8005 as its polynomial with the initial
* value set to 0xffff.
*
* @param src Input bytes for the computation
* @param len Length of the input in bytes
*
* @return The computed CRC16 value
*/
static inline u16_t crc16_ansi(const u8_t *src, size_t len)
{
return crc16(src, len, 0x8005, 0xffff, true);
}
/**
* @brief Generate IEEE conform CRC32 checksum.
*
* @param *data Pointer to data on which the CRC should be calculated.
* @param len Data length.
*
* @return CRC32 value.
*
*/
u32_t crc32_ieee(const u8_t *data, size_t len);
/**
* @brief Update an IEEE conforming CRC32 checksum.
*
* @param crc CRC32 checksum that needs to be updated.
* @param *data Pointer to data on which the CRC should be calculated.
* @param len Data length.
*
* @return CRC32 value.
*
*/
u32_t crc32_ieee_update(u32_t crc, const u8_t *data, size_t len);
/**
* @brief Compute CCITT variant of CRC 8
*
* Normal CCITT variant of CRC 8 is using 0x07.
*
* @param initial_value Initial value for the CRC computation
* @param buf Input bytes for the computation
* @param len Length of the input in bytes
*
* @return The computed CRC8 value
*/
u8_t crc8_ccitt(u8_t initial_value, const void *buf, size_t len);
/**
* @brief Compute the CRC-7 checksum of a buffer.
*
* See JESD84-A441. Used by the MMC protocol. Uses 0x09 as the
* polynomial with no reflection. The CRC is left
* justified, so bit 7 of the result is bit 6 of the CRC.
*
* @param seed Value to seed the CRC with
* @param src Input bytes for the computation
* @param len Length of the input in bytes
*
* @return The computed CRC7 value
*/
u8_t crc7_be(u8_t seed, const u8_t *src, size_t len);
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif