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