1 // Copyright 2010-2020 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 #include <stdint.h>
22 
23 /**           Notes about CRC API
24  * The ESP32 ROM include some CRC tables and CRC APIs to speed up CRC calculation.
25  * The CRC APIs include CRC8, CRC16, CRC32 algorithms for both little endian and big endian modes.
26  * Here are the polynomials for the algorithms:
27  * CRC-8        x8+x2+x1+1                                              0x07
28  * CRC16-CCITT  x16+x12+x5+1                                            0x1021
29  * CRC32        x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x1+1     0x04c11db7
30  *
31  * These group of CRC APIs are designed to calculate the data in buffers either continuous or not.
32  * To make it easy, we had added a `~` at the beginning and the end of the functions.
33  * To calculate non-continuous buffers, we can write the code like this:
34  *     init = ~init;
35  *     crc = crc32_le(init, buf0, length0);
36  *     crc = crc32_le(crc, buf1, length1);
37  *     crc = ~crc;
38  *
39  * However, it is not easy to select which API to use and give the correct parameters.
40  * A specific CRC algorithm will include this parameters: width, polynomials, init, refin, refout, xorout
41  *     refin and refout show the endian of the algorithm:
42  *         if both of them are true, please use the little endian API.
43  *         if both of them are false, please use the big endian API.
44  *     xorout is the value which you need to be xored to the raw result.
45  * However, these group of APIs need one '~' before and after the APIs.
46  *
47  * Here are some examples for CRC16:
48  * CRC-16/CCITT, poly = 0x1021, init = 0x0000, refin = true, refout = true, xorout = 0x0000
49  *     crc = ~crc16_le((uint16_t)~0x0000, buf, length);
50  *
51  * CRC-16/CCITT-FALSE, poly = 0x1021, init = 0xffff, refin = false, refout = false, xorout = 0x0000
52  *     crc = ~crc16_be((uint16_t)~0xffff, buf, length);
53  *
54  * CRC-16/X25, poly = 0x1021, init = 0xffff, refin = true, refout = true, xorout = 0xffff
55  *     crc = (~crc16_le((uint16_t)~(0xffff), buf, length))^0xffff;
56  *
57  * CRC-16/XMODEM, poly= 0x1021, init = 0x0000, refin = false, refout = false, xorout = 0x0000
58  *     crc = ~crc16_be((uint16_t)~0x0000, buf, length);
59  *
60  */
61 
62 /**
63  * @brief CRC32 value in little endian.
64  *
65  * @param crc: Initial CRC value (result of last calculation or 0 for the first time)
66  * @param buf: Data buffer that used to calculate the CRC value
67  * @param len: Length of the data buffer
68  * @return CRC32 value
69  */
70 uint32_t esp_rom_crc32_le(uint32_t crc, uint8_t const *buf, uint32_t len);
71 
72 /**
73  * @brief CRC32 value in big endian.
74  *
75  * @param crc: Initial CRC value (result of last calculation or 0 for the first time)
76  * @param buf: Data buffer that used to calculate the CRC value
77  * @param len: Length of the data buffer
78  * @return CRC32 value
79  */
80 uint32_t esp_rom_crc32_be(uint32_t crc, uint8_t const *buf, uint32_t len);
81 
82 /**
83  * @brief CRC16 value in little endian.
84  *
85  * @param crc: Initial CRC value (result of last calculation or 0 for the first time)
86  * @param buf: Data buffer that used to calculate the CRC value
87  * @param len: Length of the data buffer
88  * @return CRC16 value
89  */
90 uint16_t esp_rom_crc16_le(uint16_t crc, uint8_t const *buf, uint32_t len);
91 
92 /**
93  * @brief CRC16 value in big endian.
94  *
95  * @param crc: Initial CRC value (result of last calculation or 0 for the first time)
96  * @param buf: Data buffer that used to calculate the CRC value
97  * @param len: Length of the data buffer
98  * @return CRC16 value
99  */
100 uint16_t esp_rom_crc16_be(uint16_t crc, uint8_t const *buf, uint32_t len);
101 
102 /**
103  * @brief CRC8 value in little endian.
104  *
105  * @param crc: Initial CRC value (result of last calculation or 0 for the first time)
106  * @param buf: Data buffer that used to calculate the CRC value
107  * @param len: Length of the data buffer
108  * @return CRC8 value
109  */
110 uint8_t esp_rom_crc8_le(uint8_t crc, uint8_t const *buf, uint32_t len);
111 
112 /**
113  * @brief CRC8 value in big endian.
114  *
115  * @param crc: Initial CRC value (result of last calculation or 0 for the first time)
116  * @param buf: Data buffer that used to calculate the CRC value
117  * @param len: Length of the data buffer
118  * @return CRC8 value
119  */
120 uint8_t esp_rom_crc8_be(uint8_t crc, uint8_t const *buf, uint32_t len);
121 
122 #ifdef __cplusplus
123 }
124 #endif
125