The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Committer:
Anna Bridge
Date:
Fri Jun 22 15:38:59 2018 +0100
Revision:
169:a7c7b631e539
Child:
170:e95d10626187
mbed library. Release version 162

Who changed what in which revision?

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