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:
AnnaBridge
Date:
Thu Nov 08 11:45:42 2018 +0000
Revision:
171:3a7713b1edbc
Parent:
170:e95d10626187
Child:
172:65be27845400
mbed library. Release version 164

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