Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Committer:
kenjiArai
Date:
Tue Dec 17 23:23:45 2019 +0000
Revision:
0:5b88d5760320
Child:
1:9db0e321a9f4
mbed-os5 only for TYBLE16

Who changed what in which revision?

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