Mistake on this page?
Report an issue in GitHub or email us
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  */
30 typedef 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 
38 typedef 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. */
48  bool reflect_in;
49  /** Reflect bits in final result before returning. */
52 
53 #if DEVICE_CRC
54 
55 #ifdef __cplusplus
56 extern "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  * #define HAL_CRC_IS_SUPPORTED(polynomial, width) \
125  * ((width) == 16 || ((width) == 32 && (polynomial) == POLY_32BIT_ANSI)
126  * @{
127  */
128 
129 /**
130  * \defgroup hal_crc_tests crc hal tests
131  * The crc HAL tests ensure driver conformance to defined behaviour.
132  *
133  * To run the crc hal tests use the command:
134  *
135  * mbed test -t <toolchain> -m <target> -n tests-mbed_hal-crc*
136  *
137  */
138 
139 /** Initialize the hardware CRC module with the given polynomial
140  *
141  * After calling this function, the CRC HAL module is ready to receive data
142  * using the hal_crc_compute_partial() function. The CRC module on the board
143  * is configured internally with the specified configuration and is ready
144  * to receive data.
145  *
146  * The platform configures itself based on the default configuration
147  * parameters of the input polynomial.
148  *
149  * This function must be called before calling hal_crc_compute_partial().
150  *
151  * This function must be called with a valid polynomial supported by the
152  * platform. The polynomial must be checked for support using the
153  * HAL_CRC_IS_SUPPORTED() macro.
154  *
155  * Calling hal_crc_compute_partial_start() multiple times without finalizing the
156  * CRC calculation with hal_crc_get_result() overrides the current
157  * configuration and state, and the intermediate result of the computation is
158  * lost.
159  *
160  * This function is not thread safe. A CRC calculation must not be started from
161  * two different threads or contexts at the same time; calling this function
162  * from two different contexts may lead to configurations being overwritten and
163  * results being lost.
164  *
165  * \param config Contains CRC configuration parameters for initializing the
166  * hardware CRC module. For example, polynomial and initial seed
167  * values.
168  */
170 
171 /** Writes data to the current CRC module.
172  *
173  * Writes input data buffer bytes to the CRC data register. The CRC module
174  * must interpret the data as an array of bytes.
175  *
176  * The final transformations are not applied to the data; the CRC module must
177  * retain the intermediate result so that additional calls to this function
178  * can be made, appending the additional data to the calculation.
179  *
180  * To obtain the final result of the CRC calculation, hal_crc_get_result() is
181  * called to apply the final transformations to the data.
182  *
183  * If the function is passed an undefined pointer, or the size of the buffer is
184  * specified to be 0, this function does nothing and returns.
185  *
186  * This function can be called multiple times in succession. This can be used
187  * to calculate the CRC result of streamed data.
188  *
189  * This function is not thread safe. There is only one instance of the CRC
190  * module active at a time. Calling this function from multiple contexts
191  * appends different data to the same, single instance of the module, which causes an
192  * erroneous value to be calculated.
193  *
194  * \param data Input data stream to be written into the CRC calculation
195  * \param size Size of the data stream in bytes
196  */
197 void hal_crc_compute_partial(const uint8_t *data, const size_t size);
198 
199 /* Reads the checksum result from the CRC module.
200  *
201  * Reads the final checksum result for the final checksum value. The returned
202  * value is cast as an unsigned 32-bit integer. The actual size of the returned
203  * result depends on the polynomial used to configure the CRC module.
204  *
205  * Additional transformations that are used in the default configuration of the
206  * input polynomial are applied to the result before it is returned from this
207  * function. These transformations include: the final xor being appended to the
208  * calculation, and the result being reflected if required.
209  *
210  * Calling this function multiple times is undefined. The first call to this
211  * function returns the final result of the CRC calculation. The return
212  * value on successive calls is undefined because the contents of the register after
213  * accessing them is platform-specific.
214  *
215  * This function is not thread safe. There is only one instance of the CRC
216  * module active at a time. Calling this function from multiple contexts may
217  * return incorrect data or affect the current state of the module.
218  *
219  * \return The final CRC checksum after the reflections and final calculations
220  * have been applied.
221  */
222 uint32_t hal_crc_get_result(void);
223 
224 /**@}*/
225 
226 #ifdef __cplusplus
227 }
228 #endif
229 
230 #endif // DEVICE_CRC
231 #endif // MBED_CRC_HAL_API_H
232 
233 /**@}*/
x16+x15+x2+1
Definition: crc_api.h:34
enum crc_polynomial crc_polynomial_t
CRC Polynomial value.
x7+x3+1
Definition: crc_api.h:31
uint32_t final_xor
Final xor value for the computation.
Definition: crc_api.h:46
void hal_crc_compute_partial_start(const crc_mbed_config_t *config)
Initialize the hardware CRC module with the given polynomial.
crc_polynomial
CRC Polynomial value.
Definition: crc_api.h:30
uint32_t width
CRC Bit Width.
Definition: crc_api.h:42
x16+x12+x5+1
Definition: crc_api.h:33
x8+x2+x+1
Definition: crc_api.h:32
void hal_crc_compute_partial(const uint8_t *data, const size_t size)
Writes data to the current CRC module.
bool reflect_out
Reflect bits in final result before returning.
Definition: crc_api.h:50
uint32_t polynomial
CRC Polynomial.
Definition: crc_api.h:40
x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x+1
Definition: crc_api.h:35
bool reflect_in
Reflect bits on input.
Definition: crc_api.h:48
uint32_t initial_xor
Initial seed value for the computation.
Definition: crc_api.h:44
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.