1 // Copyright 2015-2016 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 #ifndef ROM_CRC_H
16 #define ROM_CRC_H
17 
18 #include <stdint.h>
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 /** \defgroup uart_apis, uart configuration and communication related apis
25   * @brief uart apis
26   */
27 
28 /** @addtogroup uart_apis
29   * @{
30   */
31 
32 
33 /*           Notes about CRC APIs usage
34  * The ESP32 ROM include some CRC tables and CRC APIs to speed up CRC calculation.
35  * The CRC APIs include CRC8, CRC16, CRC32 algorithms for both little endian and big endian modes.
36  * Here are the polynomials for the algorithms:
37  * CRC-8        x8+x2+x1+1                                              0x07
38  * CRC16-CCITT  x16+x12+x5+1                                            0x1021
39  * CRC32        x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x1+1     0x04c11db7
40  *
41  * These group of CRC APIs are designed to calculate the data in buffers either continuous or not.
42  * To make it easy, we had added a `~` at the beginning and the end of the functions.
43  * To calculate non-continuous buffers, we can write the code like this:
44  *     init = ~init;
45  *     crc = crc32_le(init, buf0, length0);
46  *     crc = crc32_le(crc, buf1, length1);
47  *     crc = ~crc;
48  *
49  * However, it is not easy to select which API to use and give the correct parameters.
50  * A specific CRC algorithm will include this parameters: width, polynomials, init, refin, refout, xorout
51  *     refin and refout show the endian of the algorithm:
52  *         if both of them are true, please use the little endian API.
53  *         if both of them are false, please use the big endian API.
54  *     xorout is the value which you need to be xored to the raw result.
55  * However, these group of APIs need one '~' before and after the APIs.
56  *
57  * Here are some examples for CRC16:
58  * CRC-16/CCITT, poly = 0x1021, init = 0x0000, refin = true, refout = true, xorout = 0x0000
59  *     crc = ~crc16_le((uint16_t)~0x0000, buf, length);
60  *
61  * CRC-16/CCITT-FALSE, poly = 0x1021, init = 0xffff, refin = false, refout = false, xorout = 0x0000
62  *     crc = ~crc16_be((uint16_t)~0xffff, buf, length);
63  *
64  * CRC-16/X25, poly = 0x1021, init = 0xffff, refin = true, refout = true, xorout = 0xffff
65  *     crc = (~crc16_le((uint16_t)~(0xffff), buf, length))^0xffff;
66  *
67  * CRC-16/XMODEM, poly= 0x1021, init = 0x0000, refin = false, refout = false, xorout = 0x0000
68  *     crc = ~crc16_be((uint16_t)~0x0000, buf, length);
69  *
70  *
71  */
72 
73 /**
74   * @brief CRC32 value that is in little endian.
75   *
76   * @param  uint32_t crc : init crc value, use 0 at the first use.
77   *
78   * @param  uint8_t const *buf : buffer to start calculate crc.
79   *
80   * @param  uint32_t len : buffer length in byte.
81   *
82   * @return None
83   */
84 uint32_t crc32_le(uint32_t crc, uint8_t const *buf, uint32_t len);
85 
86 /**
87   * @brief CRC32 value that is in big endian.
88   *
89   * @param  uint32_t crc : init crc value, use 0 at the first use.
90   *
91   * @param  uint8_t const *buf : buffer to start calculate crc.
92   *
93   * @param  uint32_t len : buffer length in byte.
94   *
95   * @return None
96   */
97 uint32_t crc32_be(uint32_t crc, uint8_t const *buf, uint32_t len);
98 
99 /**
100   * @brief CRC16 value that is in little endian.
101   *
102   * @param  uint16_t crc : init crc value, use 0 at the first use.
103   *
104   * @param  uint8_t const *buf : buffer to start calculate crc.
105   *
106   * @param  uint32_t len : buffer length in byte.
107   *
108   * @return None
109   */
110 uint16_t crc16_le(uint16_t crc, uint8_t const *buf, uint32_t len);
111 
112 /**
113   * @brief CRC16 value that is in big endian.
114   *
115   * @param  uint16_t crc : init crc value, use 0 at the first use.
116   *
117   * @param  uint8_t const *buf : buffer to start calculate crc.
118   *
119   * @param  uint32_t len : buffer length in byte.
120   *
121   * @return None
122   */
123 uint16_t crc16_be(uint16_t crc, uint8_t const *buf, uint32_t len);
124 
125 /**
126   * @brief CRC8 value that is in little endian.
127   *
128   * @param  uint8_t crc : init crc value, use 0 at the first use.
129   *
130   * @param  uint8_t const *buf : buffer to start calculate crc.
131   *
132   * @param  uint32_t len : buffer length in byte.
133   *
134   * @return None
135   */
136 uint8_t crc8_le(uint8_t crc, uint8_t const *buf, uint32_t len);
137 
138 /**
139   * @brief CRC8 value that is in big endian.
140   *
141   * @param  uint32_t crc : init crc value, use 0 at the first use.
142   *
143   * @param  uint8_t const *buf : buffer to start calculate crc.
144   *
145   * @param  uint32_t len : buffer length in byte.
146   *
147   * @return None
148   */
149 uint8_t crc8_be(uint8_t crc, uint8_t const *buf, uint32_t len);
150 
151 /**
152   * @}
153   */
154 
155 #ifdef __cplusplus
156 }
157 #endif
158 
159 
160 #endif
161