Skip to content

Commit 521ccca

Browse files
nashifgalak
authored andcommitted
crc: move all crc APIs into one header
Move all CRC APIs into one single header. Signed-off-by: Anas Nashif <[email protected]>
1 parent d8e3ea6 commit 521ccca

File tree

5 files changed

+174
-180
lines changed

5 files changed

+174
-180
lines changed

doc/api/misc_api.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ Miscellaneous APIs
1111
Checksum APIs
1212
*************
1313

14-
CRC16
14+
CRC
1515
=====
1616

17-
.. doxygengroup:: crc16
17+
.. doxygengroup:: crc
1818
:project: Zephyr
1919

2020
Structured Data APIs

include/crc.h

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

include/crc16.h

Lines changed: 4 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,122 +1,15 @@
11
/*
2-
* Copyright (c) 2017 Intel Corporation.
2+
* Copyright (c) 2018 Workaround GmbH.
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
6-
/** @file
7-
* @brief CRC 16 computation function
8-
*/
96

107
#ifndef ZEPHYR_INCLUDE_CRC16_H_
118
#define ZEPHYR_INCLUDE_CRC16_H_
129

13-
#include <zephyr/types.h>
14-
#include <stdbool.h>
15-
#include <stddef.h>
16-
17-
#ifdef __cplusplus
18-
extern "C" {
19-
#endif
20-
21-
/**
22-
* @defgroup checksum Checksum
23-
*/
24-
25-
/**
26-
* @defgroup crc16 CRC 16
27-
* @ingroup checksum
28-
* @{
29-
*/
10+
#include <crc.h>
3011

31-
/**
32-
* @brief Generic function for computing CRC 16
33-
*
34-
* Compute CRC 16 by passing in the address of the input, the input length
35-
* and polynomial used in addition to the initial value.
36-
*
37-
* @param src Input bytes for the computation
38-
* @param len Length of the input in bytes
39-
* @param polynomial The polynomial to use omitting the leading x^16
40-
* coefficient
41-
* @param initial_value Initial value for the CRC computation
42-
* @param pad Adds padding with zeros at the end of input bytes
43-
*
44-
* @return The computed CRC16 value
45-
*/
46-
u16_t crc16(const u8_t *src, size_t len, u16_t polynomial,
47-
u16_t initial_value, bool pad);
48-
49-
/**
50-
* @brief Compute the CRC-16/CCITT checksum of a buffer.
51-
*
52-
* See ITU-T Recommendation V.41 (November 1988). Uses 0x1021 as the
53-
* polynomial, reflects the input, and reflects the output.
54-
*
55-
* To calculate the CRC across non-contiguous blocks use the return
56-
* value from block N-1 as the seed for block N.
57-
*
58-
* For CRC-16/CCITT, use 0 as the initial seed. Other checksums in
59-
* the same family can be calculated by changing the seed and/or
60-
* XORing the final value. Examples include:
61-
*
62-
* - X-25 (used in PPP): seed=0xffff, xor=0xffff, residual=0xf0b8
63-
*
64-
* @note API changed in Zephyr 1.11.
65-
*
66-
* @param seed Value to seed the CRC with
67-
* @param src Input bytes for the computation
68-
* @param len Length of the input in bytes
69-
*
70-
* @return The computed CRC16 value
71-
*/
72-
u16_t crc16_ccitt(u16_t seed, const u8_t *src, size_t len);
73-
74-
/**
75-
* @brief Compute the CRC-16/XMODEM checksum of a buffer.
76-
*
77-
* The MSB first version of ITU-T Recommendation V.41 (November 1988).
78-
* Uses 0x1021 as the polynomial with no reflection.
79-
*
80-
* To calculate the CRC across non-contiguous blocks use the return
81-
* value from block N-1 as the seed for block N.
82-
*
83-
* For CRC-16/XMODEM, use 0 as the initial seed. Other checksums in
84-
* the same family can be calculated by changing the seed and/or
85-
* XORing the final value. Examples include:
86-
*
87-
* - CCIITT-FALSE: seed=0xffff
88-
* - GSM: seed=0, xorout=0xffff, residue=0x1d0f
89-
*
90-
* @param seed Value to seed the CRC with
91-
* @param src Input bytes for the computation
92-
* @param len Length of the input in bytes
93-
*
94-
* @return The computed CRC16 value
95-
*/
96-
u16_t crc16_itu_t(u16_t seed, const u8_t *src, size_t len);
97-
98-
/**
99-
* @brief Compute ANSI variant of CRC 16
100-
*
101-
* ANSI variant of CRC 16 is using 0x8005 as its polynomial with the initial
102-
* value set to 0xffff.
103-
*
104-
* @param src Input bytes for the computation
105-
* @param len Length of the input in bytes
106-
*
107-
* @return The computed CRC16 value
108-
*/
109-
static inline u16_t crc16_ansi(const u8_t *src, size_t len)
110-
{
111-
return crc16(src, len, 0x8005, 0xffff, true);
112-
}
113-
114-
/**
115-
* @}
116-
*/
117-
118-
#ifdef __cplusplus
119-
}
120-
#endif
12+
#warning This include file is deprecated in favor of crc.h. \
13+
Include crc.h instead.
12114

12215
#endif

include/crc32.h

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -13,44 +13,6 @@
1313
#ifndef ZEPHYR_INCLUDE_CRC32_H_
1414
#define ZEPHYR_INCLUDE_CRC32_H_
1515

16-
#include <zephyr/types.h>
17-
#include <stdbool.h>
18-
#include <stddef.h>
16+
#include <crc.h>
1917

20-
/**
21-
* @defgroup checksum Checksum
22-
*/
23-
24-
/**
25-
* @defgroup crc32 CRC 32
26-
* @ingroup checksum
27-
* @{
28-
*/
29-
30-
/**
31-
* @brief Generate IEEE conform CRC32 checksum.
32-
*
33-
* @param *data Pointer to data on which the CRC should be calculated.
34-
* @param len Data length.
35-
*
36-
* @return CRC32 value.
37-
*
38-
*/
39-
u32_t crc32_ieee(const u8_t *data, size_t len);
40-
41-
/**
42-
* @brief Update an IEEE conforming CRC32 checksum.
43-
*
44-
* @param crc CRC32 checksum that needs to be updated.
45-
* @param *data Pointer to data on which the CRC should be calculated.
46-
* @param len Data length.
47-
*
48-
* @return CRC32 value.
49-
*
50-
*/
51-
u32_t crc32_ieee_update(u32_t crc, const u8_t *data, size_t len);
52-
53-
/**
54-
* @}
55-
*/
5618
#endif

include/crc8.h

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,6 @@
88
#ifndef ZEPHYR_INCLUDE_CRC8_H_
99
#define ZEPHYR_INCLUDE_CRC8_H_
1010

11-
#include <zephyr/types.h>
12-
#include <stddef.h>
13-
14-
#ifdef __cplusplus
15-
extern "C" {
16-
#endif
17-
18-
/* Initial value expected to be used at the beginning of the crc8_ccitt
19-
* computation.
20-
*/
21-
#define CRC8_CCITT_INITIAL_VALUE 0xFF
22-
23-
/**
24-
* @brief Compute CCITT variant of CRC 8
25-
*
26-
* Normal CCITT variant of CRC 8 is using 0x07.
27-
*
28-
* @param initial_value Initial value for the CRC computation
29-
* @param buf Input bytes for the computation
30-
* @param len Length of the input in bytes
31-
*
32-
* @return The computed CRC8 value
33-
*/
34-
u8_t crc8_ccitt(u8_t initial_value, const void *buf, size_t len);
35-
36-
#ifdef __cplusplus
37-
}
38-
#endif
11+
#include <crc.h>
3912

4013
#endif

0 commit comments

Comments
 (0)