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
mbed library release version 165

Who changed what in which revision?

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