Genadi Zawidowski / mbed-dev

Dependents:   IGLOO_board

Fork of mbed-dev by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers crc_api.h Source File

crc_api.h

00001 /** \addtogroup hal */
00002 /** @{*/
00003 /* mbed Microcontroller Library
00004  * Copyright (c) 2018 ARM Limited
00005  *
00006  * Licensed under the Apache License, Version 2.0 (the "License");
00007  * you may not use this file except in compliance with the License.
00008  * You may obtain a copy of the License at
00009  *
00010  *     http://www.apache.org/licenses/LICENSE-2.0
00011  *
00012  * Unless required by applicable law or agreed to in writing, software
00013  * distributed under the License is distributed on an "AS IS" BASIS,
00014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  * See the License for the specific language governing permissions and
00016  * limitations under the License.
00017  */
00018 #ifndef MBED_CRC_HAL_API_H
00019 #define MBED_CRC_HAL_API_H
00020 
00021 #include <stdbool.h>
00022 #include <stddef.h>
00023 #include <stdint.h>
00024 
00025 /** CRC Polynomial value
00026  *
00027  * Different polynomial values supported
00028  */
00029 typedef enum crc_polynomial {
00030     POLY_OTHER       = 0,
00031     POLY_8BIT_CCITT  = 0x07,       // x8+x2+x+1
00032     POLY_7BIT_SD     = 0x9,        // x7+x3+1;
00033     POLY_16BIT_CCITT = 0x1021,     // x16+x12+x5+1
00034     POLY_16BIT_IBM   = 0x8005,     // x16+x15+x2+1
00035     POLY_32BIT_ANSI  = 0x04C11DB7, // x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x+1
00036 } crc_polynomial_t;
00037 
00038 typedef struct crc_mbed_config {
00039     /** CRC Polynomial. Example polynomial: 0x21 = 0010_0011 = x^5+x+1 */
00040     uint32_t polynomial;
00041     /** CRC Bit Width */
00042     uint32_t width;
00043     /** Initial seed value for the computation. */
00044     uint32_t initial_xor;
00045     /** Final xor value for the computation. */
00046     uint32_t final_xor;
00047     /** Reflect bits on input. */
00048     bool reflect_in;
00049     /** Reflect bits in final result before returning. */
00050     bool reflect_out;
00051 } crc_mbed_config_t;
00052 
00053 #ifdef DEVICE_CRC
00054 
00055 #ifdef __cplusplus
00056 extern "C" {
00057 #endif
00058 
00059 /**
00060  * \defgroup hal_crc Hardware CRC
00061  *
00062  * The Hardware CRC HAL API provides a low-level interface to the Hardware CRC
00063  * module of a target platform.
00064  *
00065  * # Defined behaviour
00066  *
00067  * * Function hal_crc_is_supported() returns true if platform supports hardware
00068  *   CRC for the given polynomial/width - verified by test ::crc_is_supported_test.
00069  * * Function hal_crc_is_supported() returns false if platform does not support hardware
00070  *   CRC for the given polynomial/width - verified by test ::crc_is_supported_test.
00071  * * Function hal_crc_is_supported() returns false if given pointer to configuration
00072  *   structure is undefined (NULL) - verified by test ::crc_is_supported_invalid_param_test.
00073  * * If CRC module does not support one of the following settings: initial_xor, final_xor
00074  *   reflect_in, reflect_out, then these operations should be handled by the driver
00075  *   - Verified by test ::crc_calc_single_test.
00076  * * Platform which supports hardware CRC must be able to handle at least one of the predefined
00077  *   polynomial/width configurations that can be constructed in the MbedCRC class: POLY_8BIT_CCITT,
00078  *   POLY_7BIT_SD, POLY_16BIT_CCITT, POLY_16BIT_IBM, POLY_32BIT_ANSI
00079  *   - verified by test ::crc_is_supported_test, ::crc_calc_single_test.
00080  * * Function hal_crc_compute_partial_start() configures CRC module with the given configuration
00081  *   - Verified by test ::crc_calc_single_test.
00082  * * Calling hal_crc_compute_partial_start() without finalising the
00083  *   CRC calculation overrides the current configuration - Verified by test ::crc_reconfigure_test.
00084  * * Function hal_crc_compute_partial() writes data to the CRC module - verified by test ::crc_calc_single_test.
00085  * * Function hal_crc_compute_partial() can be call multiple times in succession in order to
00086  *   provide additional data to CRC module - verified by test ::crc_calc_multi_test.
00087  * * Function hal_crc_compute_partial() does nothing if pointer to buffer is undefined or
00088  *   data length is equal to 0 - verified by test ::crc_compute_partial_invalid_param_test.
00089  * * Function hal_crc_get_result() returns the checksum result from the CRC module
00090  *   - verified by tests ::crc_calc_single_test, ::crc_calc_multi_test, ::crc_reconfigure_test.
00091  *
00092  * # Undefined behaviour
00093  *
00094  * * Calling hal_crc_compute_partial_start() function with invalid (unsupported) polynomial.
00095  * * Calling hal_crc_compute_partial() or hal_crc_get_result() functions before hal_crc_compute_partial_start().
00096  * * Calling hal_crc_get_result() function multiple times.
00097  *
00098  * # Non-functional requirements
00099  *
00100  * * CRC configuration provides the following settings:
00101  *   * polynomial - CRC Polynomial,
00102  *   * width - CRC bit width,
00103  *   * initial_xor - seed value for the computation,
00104  *   * final_xor - final xor value for the computation,
00105  *   * reflect_in - reflect bits on input,
00106  *   * reflect_out - reflect bits in final result before returning.
00107  *
00108  * # Potential bugs
00109  *
00110  * @{
00111  */
00112 
00113 /**
00114  * \defgroup hal_crc_tests crc hal tests
00115  * The crc HAL tests ensure driver conformance to defined behaviour.
00116  *
00117  * To run the crc hal tests use the command:
00118  *
00119  *     mbed test -t <toolchain> -m <target> -n tests-mbed_hal-crc*
00120  *
00121  */
00122 
00123 /** Determine if the current platform supports hardware CRC for given polynomial
00124  *
00125  * The purpose of this function is to inform the CRC Platform API whether the
00126  * current platform has a hardware CRC module and that it can support the
00127  * requested polynomial.
00128  *
00129  * Supported polynomials are restricted to the named polynomials that can be
00130  * constructed in the MbedCRC class, POLY_8BIT_CCITT, POLY_7BIT_SD,
00131  * POLY_16BIT_CCITT, POLY_16BIT_IBM and POLY_32BIT_ANSI.
00132  *
00133  * The current platform must support the given polynomials default parameters
00134  * in order to return a true response. These include: reflect in, reflect out,
00135  * initial xor and final xor. For example, POLY_32BIT_ANSI requires an initial
00136  * and final xor of 0xFFFFFFFF, and reflection of both input and output. If any
00137  * of these settings cannot be configured, the polynomial is not supported.
00138  *
00139  * This function is thread safe; it safe to call from multiple contexts if
00140  * required.
00141  *
00142  * \param config Contains CRC configuration parameters for initializing the
00143  *               hardware CRC module. For example, polynomial and initial seed
00144  *               values.
00145  *
00146  * \return  True if running if the polynomial is supported, false if not.
00147  */
00148 bool hal_crc_is_supported(const crc_mbed_config_t* config);
00149 
00150 /** Initialize the hardware CRC module with the given polynomial
00151  *
00152  * After calling this function, the CRC HAL module is ready to receive data
00153  * using the hal_crc_compute_partial() function. The CRC module on the board
00154  * is configured internally with the specified configuration and is ready
00155  * to receive data.
00156  *
00157  * The platform configures itself based on the default configuration
00158  * parameters of the input polynomial.
00159  *
00160  * This function must be called before calling hal_crc_compute_partial().
00161  *
00162  * This function must be called with a valid polynomial supported by the
00163  * platform. The polynomial must be checked for support using the
00164  * hal_crc_is_supported() function.
00165  *
00166  * Calling hal_crc_compute_partial_start() multiple times without finalizing the
00167  * CRC calculation with hal_crc_get_result() overrides the current
00168  * configuration and state, and the intermediate result of the computation is
00169  * lost.
00170  *
00171  * This function is not thread safe. A CRC calculation must not be started from
00172  * two different threads or contexts at the same time; calling this function
00173  * from two different contexts may lead to configurations being overwritten and
00174  * results being lost.
00175  *
00176  * \param config Contains CRC configuration parameters for initializing the
00177  *               hardware CRC module. For example, polynomial and initial seed
00178  *               values.
00179  */
00180 void hal_crc_compute_partial_start(const crc_mbed_config_t* config);
00181 
00182 /** Writes data to the current CRC module.
00183  *
00184  * Writes input data buffer bytes to the CRC data register. The CRC module
00185  * must interpret the data as an array of bytes.
00186  *
00187  * The final transformations are not applied to the data; the CRC module must
00188  * retain the intermediate result so that additional calls to this function
00189  * can be made, appending the additional data to the calculation.
00190  *
00191  * To obtain the final result of the CRC calculation, hal_crc_get_result() is
00192  * called to apply the final transformations to the data.
00193  *
00194  * If the function is passed an undefined pointer, or the size of the buffer is
00195  * specified to be 0, this function does nothing and returns.
00196  *
00197  * This function can be called multiple times in succession. This can be used
00198  * to calculate the CRC result of streamed data.
00199  *
00200  * This function is not thread safe. There is only one instance of the CRC
00201  * module active at a time. Calling this function from multiple contexts
00202  * appends different data to the same, single instance of the module, which causes an
00203  * erroneous value to be calculated.
00204  *
00205  * \param data Input data stream to be written into the CRC calculation
00206  * \param size Size of the data stream in bytes
00207  */
00208 void hal_crc_compute_partial(const uint8_t *data, const size_t size);
00209 
00210 /* Reads the checksum result from the CRC module.
00211  *
00212  * Reads the final checksum result for the final checksum value. The returned
00213  * value is cast as an unsigned 32-bit integer. The actual size of the returned
00214  * result depends on the polynomial used to configure the CRC module.
00215  *
00216  * Additional transformations that are used in the default configuration of the
00217  * input polynomial are applied to the result before it is returned from this
00218  * function. These transformations include: the final xor being appended to the
00219  * calculation, and the result being reflected if required.
00220  *
00221  * Calling this function multiple times is undefined. The first call to this
00222  * function returns the final result of the CRC calculation. The return
00223  * value on successive calls is undefined because the contents of the register after
00224  * accessing them is platform-specific.
00225  *
00226  * This function is not thread safe. There is only one instance of the CRC
00227  * module active at a time. Calling this function from multiple contexts may
00228  * return incorrect data or affect the current state of the module.
00229  *
00230  * \return The final CRC checksum after the reflections and final calculations
00231  *         have been applied.
00232  */
00233 uint32_t hal_crc_get_result(void);
00234 
00235 /**@}*/
00236 
00237 #ifdef __cplusplus
00238 };
00239 #endif
00240 
00241 #endif // DEVICE_CRC
00242 #endif // MBED_CRC_HAL_API_H
00243 
00244 /**@}*/