mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Wed Feb 20 22:31:08 2019 +0000
Revision:
189:f392fc9709a3
Parent:
188:bcfe06ba3d64
mbed library release version 165

Who changed what in which revision?

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