Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

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  * SPDX-License-Identifier: Apache-2.0
00006  *
00007  * Licensed under the Apache License, Version 2.0 (the "License");
00008  * you may not use this file except in compliance with the License.
00009  * You may obtain a copy of the License at
00010  *
00011  *     http://www.apache.org/licenses/LICENSE-2.0
00012  *
00013  * Unless required by applicable law or agreed to in writing, software
00014  * distributed under the License is distributed on an "AS IS" BASIS,
00015  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016  * See the License for the specific language governing permissions and
00017  * limitations under the License.
00018  */
00019 #ifndef MBED_CRC_HAL_API_H
00020 #define MBED_CRC_HAL_API_H
00021 
00022 #include <stdbool.h>
00023 #include <stddef.h>
00024 #include <stdint.h>
00025 
00026 /** CRC Polynomial value
00027  *
00028  * Different polynomial values supported
00029  */
00030 typedef enum crc_polynomial {
00031     POLY_7BIT_SD     = 0x09,         ///< x7+x3+1
00032     POLY_8BIT_CCITT  = 0x07,         ///< x8+x2+x+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: x^8+x^5+x+1 -> width = 8, polynomial = 0010_0011 = 0x21 */
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 #if 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  * * Macro HAL_CRC_IS_SUPPORTED() evaluates true if platform supports hardware
00068  *   CRC for the given polynomial/width - verified by test ::crc_is_supported_test.
00069  * * Macro HAL_CRC_IS_SUPPORTED() evaluates false if platform does not support hardware
00070  *   CRC for the given polynomial/width - verified by test ::crc_is_supported_test.
00071  * * If CRC module does not support any of the following settings: initial_xor, final_xor
00072  *   reflect_in, reflect_out, then these operations must be handled by the driver
00073  *   - Verified by test ::crc_calc_single_test.
00074  * * Platform which supports hardware CRC must be able to handle at least one of the predefined
00075  *   polynomial/width configurations that can be constructed in the MbedCRC class: POLY_8BIT_CCITT,
00076  *   POLY_7BIT_SD, POLY_16BIT_CCITT, POLY_16BIT_IBM, POLY_32BIT_ANSI
00077  *   - verified by test ::crc_is_supported_test, ::crc_calc_single_test.
00078  * * Function hal_crc_compute_partial_start() configures CRC module with the given configuration
00079  *   - Verified by test ::crc_calc_single_test.
00080  * * Calling hal_crc_compute_partial_start() without finalising the
00081  *   CRC calculation overrides the current configuration - Verified by test ::crc_reconfigure_test.
00082  * * Function hal_crc_compute_partial() writes data to the CRC module - verified by test ::crc_calc_single_test.
00083  * * Function hal_crc_compute_partial() can be call multiple times in succession in order to
00084  *   provide additional data to CRC module - verified by test ::crc_calc_multi_test.
00085  * * Function hal_crc_compute_partial() does nothing if pointer to buffer is undefined or
00086  *   data length is equal to 0 - verified by test ::crc_compute_partial_invalid_param_test.
00087  * * Function hal_crc_get_result() returns the checksum result from the CRC module
00088  *   - verified by tests ::crc_calc_single_test, ::crc_calc_multi_test, ::crc_reconfigure_test.
00089  *
00090  * # Undefined behaviour
00091  *
00092  * * Calling hal_crc_compute_partial_start() function with invalid (unsupported) polynomial.
00093  * * Calling hal_crc_compute_partial() or hal_crc_get_result() functions before hal_crc_compute_partial_start().
00094  * * Calling hal_crc_get_result() function multiple times.
00095  *
00096  * # Non-functional requirements
00097  *
00098  * * CRC configuration provides the following settings:
00099  *   * polynomial - CRC Polynomial,
00100  *   * width - CRC bit width,
00101  *   * initial_xor - seed value for the computation,
00102  *   * final_xor - final xor value for the computation,
00103  *   * reflect_in - reflect bits on input,
00104  *   * reflect_out - reflect bits in final result before returning.
00105  *
00106  * # Potential bugs
00107  *
00108  * # Macros
00109  *
00110  * Platform support for particular CRC polynomials is indicated by the
00111  * macro HAL_CRC_IS_SUPPORTED(polynomial, width), which must be defined
00112  * in device.h, or a file included from there.
00113  *
00114  * The macro must evaluate to a constant boolean expression when given
00115  * constant parameters.
00116  *
00117  * The current platform must support the given polynomial with all possible
00118  * other config parameters if the macro evaluates to true. These are:
00119  * reflect in, reflect out, initial xor and final xor. If any of these settings
00120  * of these settings cannot be configured, the polynomial is not supported.
00121  *
00122  * Example:
00123  *
00124  *    #define HAL_CRC_IS_SUPPORTED(polynomial, width) \
00125  *         ((width) == 16 || ((width) == 32 && (polynomial) == POLY_32BIT_ANSI)
00126  * @{
00127  */
00128 
00129 /**
00130  * \defgroup hal_crc_tests crc hal tests
00131  * The crc HAL tests ensure driver conformance to defined behaviour.
00132  *
00133  * To run the crc hal tests use the command:
00134  *
00135  *     mbed test -t <toolchain> -m <target> -n tests-mbed_hal-crc*
00136  *
00137  */
00138 
00139 /** Initialize the hardware CRC module with the given polynomial
00140  *
00141  * After calling this function, the CRC HAL module is ready to receive data
00142  * using the hal_crc_compute_partial() function. The CRC module on the board
00143  * is configured internally with the specified configuration and is ready
00144  * to receive data.
00145  *
00146  * The platform configures itself based on the default configuration
00147  * parameters of the input polynomial.
00148  *
00149  * This function must be called before calling hal_crc_compute_partial().
00150  *
00151  * This function must be called with a valid polynomial supported by the
00152  * platform. The polynomial must be checked for support using the
00153  * HAL_CRC_IS_SUPPORTED() macro.
00154  *
00155  * Calling hal_crc_compute_partial_start() multiple times without finalizing the
00156  * CRC calculation with hal_crc_get_result() overrides the current
00157  * configuration and state, and the intermediate result of the computation is
00158  * lost.
00159  *
00160  * This function is not thread safe. A CRC calculation must not be started from
00161  * two different threads or contexts at the same time; calling this function
00162  * from two different contexts may lead to configurations being overwritten and
00163  * results being lost.
00164  *
00165  * \param config Contains CRC configuration parameters for initializing the
00166  *               hardware CRC module. For example, polynomial and initial seed
00167  *               values.
00168  */
00169 void hal_crc_compute_partial_start(const crc_mbed_config_t *config);
00170 
00171 /** Writes data to the current CRC module.
00172  *
00173  * Writes input data buffer bytes to the CRC data register. The CRC module
00174  * must interpret the data as an array of bytes.
00175  *
00176  * The final transformations are not applied to the data; the CRC module must
00177  * retain the intermediate result so that additional calls to this function
00178  * can be made, appending the additional data to the calculation.
00179  *
00180  * To obtain the final result of the CRC calculation, hal_crc_get_result() is
00181  * called to apply the final transformations to the data.
00182  *
00183  * If the function is passed an undefined pointer, or the size of the buffer is
00184  * specified to be 0, this function does nothing and returns.
00185  *
00186  * This function can be called multiple times in succession. This can be used
00187  * to calculate the CRC result of streamed data.
00188  *
00189  * This function is not thread safe. There is only one instance of the CRC
00190  * module active at a time. Calling this function from multiple contexts
00191  * appends different data to the same, single instance of the module, which causes an
00192  * erroneous value to be calculated.
00193  *
00194  * \param data Input data stream to be written into the CRC calculation
00195  * \param size Size of the data stream in bytes
00196  */
00197 void hal_crc_compute_partial(const uint8_t *data, const size_t size);
00198 
00199 /* Reads the checksum result from the CRC module.
00200  *
00201  * Reads the final checksum result for the final checksum value. The returned
00202  * value is cast as an unsigned 32-bit integer. The actual size of the returned
00203  * result depends on the polynomial used to configure the CRC module.
00204  *
00205  * Additional transformations that are used in the default configuration of the
00206  * input polynomial are applied to the result before it is returned from this
00207  * function. These transformations include: the final xor being appended to the
00208  * calculation, and the result being reflected if required.
00209  *
00210  * Calling this function multiple times is undefined. The first call to this
00211  * function returns the final result of the CRC calculation. The return
00212  * value on successive calls is undefined because the contents of the register after
00213  * accessing them is platform-specific.
00214  *
00215  * This function is not thread safe. There is only one instance of the CRC
00216  * module active at a time. Calling this function from multiple contexts may
00217  * return incorrect data or affect the current state of the module.
00218  *
00219  * \return The final CRC checksum after the reflections and final calculations
00220  *         have been applied.
00221  */
00222 uint32_t hal_crc_get_result(void);
00223 
00224 /**@}*/
00225 
00226 #ifdef __cplusplus
00227 };
00228 #endif
00229 
00230 #endif // DEVICE_CRC
00231 #endif // MBED_CRC_HAL_API_H
00232 
00233 /**@}*/