Mbed OS Reference
Loading...
Searching...
No Matches
crc_api.h
1/** \addtogroup hal */
2/** @{*/
3/* mbed Microcontroller Library
4 * Copyright (c) 2018 ARM Limited
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19#ifndef MBED_CRC_HAL_API_H
20#define MBED_CRC_HAL_API_H
21
22#include <stdbool.h>
23#include <stddef.h>
24#include <stdint.h>
25
26/** CRC Polynomial value
27 *
28 * Different polynomial values supported
29 */
30typedef enum crc_polynomial {
31 POLY_7BIT_SD = 0x09, ///< x7+x3+1
32 POLY_8BIT_CCITT = 0x07, ///< x8+x2+x+1
33 POLY_16BIT_CCITT = 0x1021, ///< x16+x12+x5+1
34 POLY_16BIT_IBM = 0x8005, ///< x16+x15+x2+1
35 POLY_32BIT_ANSI = 0x04C11DB7 ///< x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x+1
37
38typedef struct crc_mbed_config {
39 /** CRC Polynomial. Example polynomial: x^8+x^5+x+1 -> width = 8, polynomial = 0010_0011 = 0x21 */
40 uint32_t polynomial;
41 /** CRC Bit Width */
42 uint32_t width;
43 /** Initial seed value for the computation. */
44 uint32_t initial_xor;
45 /** Final xor value for the computation. */
46 uint32_t final_xor;
47 /** Reflect bits on input. */
49 /** Reflect bits in final result before returning. */
52
53#if DEVICE_CRC
54
55#ifdef __cplusplus
56extern "C" {
57#endif
58
59/**
60 * \defgroup hal_crc Hardware CRC
61 *
62 * The Hardware CRC HAL API provides a low-level interface to the Hardware CRC
63 * module of a target platform.
64 *
65 * # Defined behaviour
66 *
67 * * Macro HAL_CRC_IS_SUPPORTED() evaluates true if platform supports hardware
68 * CRC for the given polynomial/width - verified by test crc_is_supported_test.
69 * * Macro HAL_CRC_IS_SUPPORTED() evaluates false if platform does not support hardware
70 * CRC for the given polynomial/width - verified by test crc_is_supported_test.
71 * * If CRC module does not support any of the following settings: initial_xor, final_xor
72 * reflect_in, reflect_out, then these operations must be handled by the driver
73 * - Verified by test crc_calc_single_test.
74 * * Platform which supports hardware CRC must be able to handle at least one of the predefined
75 * polynomial/width configurations that can be constructed in the MbedCRC class: POLY_8BIT_CCITT,
76 * POLY_7BIT_SD, POLY_16BIT_CCITT, POLY_16BIT_IBM, POLY_32BIT_ANSI
77 * - verified by test crc_is_supported_test, crc_calc_single_test.
78 * * Function hal_crc_compute_partial_start() configures CRC module with the given configuration
79 * - Verified by test crc_calc_single_test.
80 * * Calling hal_crc_compute_partial_start() without finalising the
81 * CRC calculation overrides the current configuration - Verified by test crc_reconfigure_test.
82 * * Function hal_crc_compute_partial() writes data to the CRC module - verified by test crc_calc_single_test.
83 * * Function hal_crc_compute_partial() can be call multiple times in succession in order to
84 * provide additional data to CRC module - verified by test crc_calc_multi_test.
85 * * Function hal_crc_compute_partial() does nothing if pointer to buffer is undefined or
86 * data length is equal to 0 - verified by test crc_compute_partial_invalid_param_test.
87 * * Function hal_crc_get_result() returns the checksum result from the CRC module
88 * - verified by tests crc_calc_single_test, crc_calc_multi_test, crc_reconfigure_test.
89 *
90 * # Undefined behaviour
91 *
92 * * Calling hal_crc_compute_partial_start() function with invalid (unsupported) polynomial.
93 * * Calling hal_crc_compute_partial() or hal_crc_get_result() functions before hal_crc_compute_partial_start().
94 * * Calling hal_crc_get_result() function multiple times.
95 *
96 * # Non-functional requirements
97 *
98 * * CRC configuration provides the following settings:
99 * * polynomial - CRC Polynomial,
100 * * width - CRC bit width,
101 * * initial_xor - seed value for the computation,
102 * * final_xor - final xor value for the computation,
103 * * reflect_in - reflect bits on input,
104 * * reflect_out - reflect bits in final result before returning.
105 *
106 * # Potential bugs
107 *
108 * # Macros
109 *
110 * Platform support for particular CRC polynomials is indicated by the
111 * macro HAL_CRC_IS_SUPPORTED(polynomial, width), which must be defined
112 * in device.h, or a file included from there.
113 *
114 * The macro must evaluate to a constant boolean expression when given
115 * constant parameters.
116 *
117 * The current platform must support the given polynomial with all possible
118 * other config parameters if the macro evaluates to true. These are:
119 * reflect in, reflect out, initial xor and final xor. If any of these settings
120 * of these settings cannot be configured, the polynomial is not supported.
121 *
122 * Example:
123 *
124 * @code
125 * #define HAL_CRC_IS_SUPPORTED(polynomial, width) \
126 * ((width) == 16 || ((width) == 32 && (polynomial) == POLY_32BIT_ANSI)
127 * @endcode
128 *
129 * @{
130 */
131
132/**
133 * \defgroup hal_crc_tests crc hal tests
134 * The crc HAL tests ensure driver conformance to defined behaviour.
135 *
136 * To run the crc hal tests use the command:
137 *
138 * mbed test -t <toolchain> -m <target> -n tests-mbed_hal-crc*
139 *
140 */
141
142/** Initialize the hardware CRC module with the given polynomial
143 *
144 * After calling this function, the CRC HAL module is ready to receive data
145 * using the hal_crc_compute_partial() function. The CRC module on the board
146 * is configured internally with the specified configuration and is ready
147 * to receive data.
148 *
149 * The platform configures itself based on the default configuration
150 * parameters of the input polynomial.
151 *
152 * This function must be called before calling hal_crc_compute_partial().
153 *
154 * This function must be called with a valid polynomial supported by the
155 * platform. The polynomial must be checked for support using the
156 * HAL_CRC_IS_SUPPORTED() macro.
157 *
158 * Calling hal_crc_compute_partial_start() multiple times without finalizing the
159 * CRC calculation with hal_crc_get_result() overrides the current
160 * configuration and state, and the intermediate result of the computation is
161 * lost.
162 *
163 * This function is not thread safe. A CRC calculation must not be started from
164 * two different threads or contexts at the same time; calling this function
165 * from two different contexts may lead to configurations being overwritten and
166 * results being lost.
167 *
168 * \param config Contains CRC configuration parameters for initializing the
169 * hardware CRC module. For example, polynomial and initial seed
170 * values.
171 */
173
174/** Writes data to the current CRC module.
175 *
176 * Writes input data buffer bytes to the CRC data register. The CRC module
177 * must interpret the data as an array of bytes.
178 *
179 * The final transformations are not applied to the data; the CRC module must
180 * retain the intermediate result so that additional calls to this function
181 * can be made, appending the additional data to the calculation.
182 *
183 * To obtain the final result of the CRC calculation, hal_crc_get_result() is
184 * called to apply the final transformations to the data.
185 *
186 * If the function is passed an undefined pointer, or the size of the buffer is
187 * specified to be 0, this function does nothing and returns.
188 *
189 * This function can be called multiple times in succession. This can be used
190 * to calculate the CRC result of streamed data.
191 *
192 * This function is not thread safe. There is only one instance of the CRC
193 * module active at a time. Calling this function from multiple contexts
194 * appends different data to the same, single instance of the module, which causes an
195 * erroneous value to be calculated.
196 *
197 * \param data Input data stream to be written into the CRC calculation
198 * \param size Size of the data stream in bytes
199 */
200void hal_crc_compute_partial(const uint8_t *data, const size_t size);
201
202/* Reads the checksum result from the CRC module.
203 *
204 * Reads the final checksum result for the final checksum value. The returned
205 * value is cast as an unsigned 32-bit integer. The actual size of the returned
206 * result depends on the polynomial used to configure the CRC module.
207 *
208 * Additional transformations that are used in the default configuration of the
209 * input polynomial are applied to the result before it is returned from this
210 * function. These transformations include: the final xor being appended to the
211 * calculation, and the result being reflected if required.
212 *
213 * Calling this function multiple times is undefined. The first call to this
214 * function returns the final result of the CRC calculation. The return
215 * value on successive calls is undefined because the contents of the register after
216 * accessing them is platform-specific.
217 *
218 * This function is not thread safe. There is only one instance of the CRC
219 * module active at a time. Calling this function from multiple contexts may
220 * return incorrect data or affect the current state of the module.
221 *
222 * \return The final CRC checksum after the reflections and final calculations
223 * have been applied.
224 */
225uint32_t hal_crc_get_result(void);
226
227/**@}*/
228
229#ifdef __cplusplus
230}
231#endif
232
233#endif // DEVICE_CRC
234#endif // MBED_CRC_HAL_API_H
235
236/**@}*/
void hal_crc_compute_partial(const uint8_t *data, const size_t size)
Writes data to the current CRC module.
void hal_crc_compute_partial_start(const crc_mbed_config_t *config)
Initialize the hardware CRC module with the given polynomial.
enum crc_polynomial crc_polynomial_t
CRC Polynomial value.
crc_polynomial
CRC Polynomial value.
Definition: crc_api.h:30
@ POLY_8BIT_CCITT
x8+x2+x+1
Definition: crc_api.h:32
@ POLY_7BIT_SD
x7+x3+1
Definition: crc_api.h:31
@ POLY_16BIT_IBM
x16+x15+x2+1
Definition: crc_api.h:34
@ POLY_16BIT_CCITT
x16+x12+x5+1
Definition: crc_api.h:33
@ POLY_32BIT_ANSI
x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x+1
Definition: crc_api.h:35
uint32_t initial_xor
Initial seed value for the computation.
Definition: crc_api.h:44
uint32_t width
CRC Bit Width.
Definition: crc_api.h:42
bool reflect_in
Reflect bits on input.
Definition: crc_api.h:48
uint32_t polynomial
CRC Polynomial.
Definition: crc_api.h:40
bool reflect_out
Reflect bits in final result before returning.
Definition: crc_api.h:50
uint32_t final_xor
Final xor value for the computation.
Definition: crc_api.h:46