2017-11-22 21:41:37 +00:00
|
|
|
/* mbed Microcontroller Library
|
2018-02-01 18:01:17 +00:00
|
|
|
* Copyright (c) 2018 ARM Limited
|
2018-11-09 11:22:52 +00:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2017-11-22 21:41:37 +00:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
#ifndef MBED_CRC_API_H
|
|
|
|
#define MBED_CRC_API_H
|
|
|
|
|
2019-11-26 13:45:37 +00:00
|
|
|
#include "drivers/internal/TableCRC.h"
|
2018-04-24 15:00:17 +00:00
|
|
|
#include "hal/crc_api.h"
|
2018-02-01 18:01:17 +00:00
|
|
|
#include "platform/mbed_assert.h"
|
2018-08-13 19:54:56 +00:00
|
|
|
#include "platform/SingletonPtr.h"
|
|
|
|
#include "platform/PlatformMutex.h"
|
2017-11-22 21:41:37 +00:00
|
|
|
|
2019-11-26 13:45:37 +00:00
|
|
|
/* This is an invalid warning from the compiler for the below section of code
|
|
|
|
if ((width < 8) && (NULL == _crc_table)) {
|
|
|
|
p_crc = (uint32_t)(p_crc << (8 - width));
|
|
|
|
}
|
|
|
|
Compiler warns of the shift operation with width as it is width=(std::uint8_t),
|
|
|
|
but we check for ( width < 8) before performing shift, so it should not be an issue.
|
|
|
|
*/
|
|
|
|
#if defined ( __CC_ARM )
|
|
|
|
#pragma diag_suppress 62 // Shift count is negative
|
|
|
|
#elif defined ( __GNUC__ )
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wshift-count-negative"
|
|
|
|
#elif defined (__ICCARM__)
|
|
|
|
#pragma diag_suppress=Pe062 // Shift count is negative
|
|
|
|
#endif
|
2017-11-22 21:41:37 +00:00
|
|
|
|
|
|
|
namespace mbed {
|
2019-07-11 15:23:39 +00:00
|
|
|
/** \addtogroup drivers-public-api */
|
2017-11-22 21:41:37 +00:00
|
|
|
/** @{*/
|
2019-07-11 15:23:39 +00:00
|
|
|
/**
|
|
|
|
* \defgroup drivers_MbedCRC MbedCRC class
|
|
|
|
* @{
|
|
|
|
*/
|
2017-11-22 21:41:37 +00:00
|
|
|
|
2019-06-17 09:21:16 +00:00
|
|
|
extern SingletonPtr<PlatformMutex> mbed_crc_mutex;
|
|
|
|
|
2019-06-17 11:14:27 +00:00
|
|
|
/** CRC object provides CRC generation through hardware or software
|
2018-02-01 18:01:17 +00:00
|
|
|
*
|
2019-06-17 13:20:55 +00:00
|
|
|
* CRC sums can be generated using three different methods: hardware, software ROM tables
|
2019-11-26 13:45:37 +00:00
|
|
|
* and bitwise computation. The mode used is selected automatically based on required
|
2019-06-17 11:14:27 +00:00
|
|
|
* polynomial and hardware capabilities. Any polynomial in standard form (`x^3 + x + 1`)
|
|
|
|
* can be used for computation, but custom ones can affect the performance.
|
|
|
|
*
|
2019-06-17 13:20:55 +00:00
|
|
|
* First choice is the hardware mode. The supported polynomials are hardware specific, and
|
|
|
|
* you need to consult your MCU manual to discover them. Next, ROM polynomial tables
|
|
|
|
* are tried (you can find list of supported polynomials here ::crc_polynomial). If the selected
|
|
|
|
* configuration is supported, it will accelerate the software computations. If ROM tables
|
|
|
|
* are not available for the selected polynomial, then CRC is computed at run time bit by bit
|
2019-06-17 11:14:27 +00:00
|
|
|
* for all data input.
|
2018-08-13 19:54:56 +00:00
|
|
|
* @note Synchronization level: Thread safe
|
2017-11-22 21:41:37 +00:00
|
|
|
*
|
|
|
|
* @tparam polynomial CRC polynomial value in hex
|
2019-11-26 13:45:37 +00:00
|
|
|
* @tparam width CRC polynomial width
|
2018-02-01 18:01:17 +00:00
|
|
|
*
|
|
|
|
* Example: Compute CRC data
|
|
|
|
* @code
|
|
|
|
*
|
|
|
|
* #include "mbed.h"
|
|
|
|
*
|
|
|
|
* int main() {
|
|
|
|
* MbedCRC<POLY_32BIT_ANSI, 32> ct;
|
|
|
|
*
|
|
|
|
* char test[] = "123456789";
|
|
|
|
* uint32_t crc = 0;
|
|
|
|
*
|
|
|
|
* printf("\nPolynomial = 0x%lx Width = %d \n", ct.get_polynomial(), ct.get_width());
|
|
|
|
*
|
|
|
|
* ct.compute((void *)test, strlen((const char*)test), &crc);
|
|
|
|
*
|
|
|
|
* printf("The CRC of data \"123456789\" is : 0x%lx\n", crc);
|
|
|
|
* return 0;
|
|
|
|
* }
|
|
|
|
* @endcode
|
|
|
|
* Example: Compute CRC with data available in parts
|
|
|
|
* @code
|
|
|
|
*
|
|
|
|
* #include "mbed.h"
|
|
|
|
* int main() {
|
|
|
|
* MbedCRC<POLY_32BIT_ANSI, 32> ct;
|
|
|
|
*
|
|
|
|
* char test[] = "123456789";
|
|
|
|
* uint32_t crc = 0;
|
|
|
|
*
|
|
|
|
* printf("\nPolynomial = 0x%lx Width = %d \n", ct.get_polynomial(), ct.get_width());
|
|
|
|
* ct.compute_partial_start(&crc);
|
|
|
|
* ct.compute_partial((void *)&test, 4, &crc);
|
|
|
|
* ct.compute_partial((void *)&test[4], 5, &crc);
|
|
|
|
* ct.compute_partial_stop(&crc);
|
|
|
|
* printf("The CRC of data \"123456789\" is : 0x%lx\n", crc);
|
|
|
|
* return 0;
|
|
|
|
* }
|
|
|
|
* @endcode
|
2017-11-22 21:41:37 +00:00
|
|
|
*/
|
2019-11-26 13:45:37 +00:00
|
|
|
template <uint32_t polynomial = POLY_32BIT_ANSI, uint8_t width = 32>
|
|
|
|
class MbedCRC {
|
2018-08-13 19:54:56 +00:00
|
|
|
|
2018-04-24 15:00:17 +00:00
|
|
|
public:
|
2018-10-09 19:59:56 +00:00
|
|
|
enum CrcMode {
|
2018-12-19 23:16:42 +00:00
|
|
|
#if DEVICE_CRC
|
2019-11-26 13:45:37 +00:00
|
|
|
HARDWARE = 0,
|
2018-08-08 18:56:13 +00:00
|
|
|
#endif
|
2019-11-26 13:45:37 +00:00
|
|
|
TABLE = 1,
|
|
|
|
BITWISE
|
2018-08-08 18:56:13 +00:00
|
|
|
};
|
2018-04-24 15:00:17 +00:00
|
|
|
|
2019-11-26 13:45:37 +00:00
|
|
|
typedef uint64_t crc_data_size_t;
|
2017-11-22 21:41:37 +00:00
|
|
|
|
|
|
|
/** Lifetime of CRC object
|
|
|
|
*
|
2019-11-26 13:45:37 +00:00
|
|
|
* @param initial_xor Inital value/seed to Xor
|
2018-02-01 18:01:17 +00:00
|
|
|
* @param final_xor Final Xor value
|
2017-11-22 21:41:37 +00:00
|
|
|
* @param reflect_data
|
|
|
|
* @param reflect_remainder
|
2018-08-13 19:54:56 +00:00
|
|
|
* @note Default constructor without any arguments is valid only for supported CRC polynomials. :: crc_polynomial_t
|
2018-02-01 18:01:17 +00:00
|
|
|
* MbedCRC <POLY_7BIT_SD, 7> ct; --- Valid POLY_7BIT_SD
|
|
|
|
* MbedCRC <0x1021, 16> ct; --- Valid POLY_16BIT_CCITT
|
|
|
|
* MbedCRC <POLY_16BIT_CCITT, 32> ct; --- Invalid, compilation error
|
2018-10-26 23:35:34 +00:00
|
|
|
* MbedCRC <POLY_16BIT_CCITT, 32> ct (i,f,rd,rr) Constructor can be used for not supported polynomials
|
2018-02-01 18:01:17 +00:00
|
|
|
* MbedCRC<POLY_16BIT_CCITT, 16> sd(0, 0, false, false); Constructor can also be used for supported
|
2019-11-26 13:45:37 +00:00
|
|
|
* polynomials with different intial/final/reflect values
|
2018-02-01 18:01:17 +00:00
|
|
|
*
|
2017-11-22 21:41:37 +00:00
|
|
|
*/
|
2018-03-23 19:55:07 +00:00
|
|
|
MbedCRC(uint32_t initial_xor, uint32_t final_xor, bool reflect_data, bool reflect_remainder) :
|
2019-11-26 13:45:37 +00:00
|
|
|
_initial_value(initial_xor), _final_xor(final_xor), _reflect_data(reflect_data),
|
|
|
|
_reflect_remainder(reflect_remainder)
|
2018-03-23 19:55:07 +00:00
|
|
|
{
|
2019-11-26 13:45:37 +00:00
|
|
|
mbed_crc_ctor();
|
2018-03-23 19:55:07 +00:00
|
|
|
}
|
2017-11-22 21:41:37 +00:00
|
|
|
MbedCRC();
|
2019-11-26 13:45:37 +00:00
|
|
|
virtual ~MbedCRC()
|
2017-11-22 21:41:37 +00:00
|
|
|
{
|
2019-11-26 13:45:37 +00:00
|
|
|
// Do nothing
|
2017-11-22 21:41:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Compute CRC for the data input
|
2018-08-13 19:54:56 +00:00
|
|
|
* Compute CRC performs the initialization, computation and collection of
|
|
|
|
* final CRC.
|
2017-11-22 21:41:37 +00:00
|
|
|
*
|
|
|
|
* @param buffer Data bytes
|
|
|
|
* @param size Size of data
|
|
|
|
* @param crc CRC is the output value
|
|
|
|
* @return 0 on success, negative error code on failure
|
|
|
|
*/
|
2019-05-28 14:58:19 +00:00
|
|
|
int32_t compute(const void *buffer, crc_data_size_t size, uint32_t *crc)
|
2017-11-22 21:41:37 +00:00
|
|
|
{
|
2019-11-26 13:45:37 +00:00
|
|
|
MBED_ASSERT(crc != NULL);
|
|
|
|
int32_t status = 0;
|
2018-08-13 19:54:56 +00:00
|
|
|
|
|
|
|
status = compute_partial_start(crc);
|
|
|
|
if (0 != status) {
|
2019-11-26 13:45:37 +00:00
|
|
|
unlock();
|
2017-11-22 21:41:37 +00:00
|
|
|
return status;
|
|
|
|
}
|
2018-08-13 19:54:56 +00:00
|
|
|
|
|
|
|
status = compute_partial(buffer, size, crc);
|
|
|
|
if (0 != status) {
|
2019-11-26 13:45:37 +00:00
|
|
|
unlock();
|
2017-11-22 21:41:37 +00:00
|
|
|
return status;
|
|
|
|
}
|
2018-08-13 19:54:56 +00:00
|
|
|
|
|
|
|
status = compute_partial_stop(crc);
|
2019-11-26 13:45:37 +00:00
|
|
|
if (0 != status) {
|
|
|
|
*crc = 0;
|
|
|
|
}
|
|
|
|
|
2018-08-13 19:54:56 +00:00
|
|
|
return status;
|
2019-11-26 13:45:37 +00:00
|
|
|
|
2017-11-22 21:41:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Compute partial CRC for the data input.
|
|
|
|
*
|
|
|
|
* CRC data if not available fully, CRC can be computed in parts with available data.
|
2018-08-13 19:54:56 +00:00
|
|
|
*
|
2018-10-26 07:45:05 +00:00
|
|
|
* In case of hardware, intermediate values and states are saved by hardware. Mutex
|
2018-08-13 19:54:56 +00:00
|
|
|
* locking is used to serialize access to hardware CRC.
|
|
|
|
*
|
|
|
|
* In case of software CRC, previous CRC output should be passed as argument to the
|
|
|
|
* current compute_partial call. Please note the intermediate CRC value is maintained by
|
|
|
|
* application and not the driver.
|
|
|
|
*
|
|
|
|
* @pre: Call `compute_partial_start` to start the partial CRC calculation.
|
|
|
|
* @post: Call `compute_partial_stop` to get the final CRC value.
|
2017-11-22 21:41:37 +00:00
|
|
|
*
|
|
|
|
* @param buffer Data bytes
|
|
|
|
* @param size Size of data
|
|
|
|
* @param crc CRC value is intermediate CRC value filled by API.
|
|
|
|
* @return 0 on success or a negative error code on failure
|
2018-08-13 19:54:56 +00:00
|
|
|
* @note: CRC as output in compute_partial is not final CRC value, call `compute_partial_stop`
|
2017-11-22 21:41:37 +00:00
|
|
|
* to get final correct CRC value.
|
|
|
|
*/
|
2019-05-28 14:58:19 +00:00
|
|
|
int32_t compute_partial(const void *buffer, crc_data_size_t size, uint32_t *crc)
|
2017-11-22 21:41:37 +00:00
|
|
|
{
|
2019-11-26 13:45:37 +00:00
|
|
|
int32_t status = 0;
|
|
|
|
|
|
|
|
switch (_mode) {
|
|
|
|
#if DEVICE_CRC
|
|
|
|
case HARDWARE:
|
|
|
|
hal_crc_compute_partial(static_cast<const uint8_t *>(buffer), size);
|
|
|
|
*crc = 0;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case TABLE:
|
|
|
|
status = table_compute_partial(buffer, size, crc);
|
|
|
|
break;
|
|
|
|
case BITWISE:
|
|
|
|
status = bitwise_compute_partial(buffer, size, crc);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
status = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
2017-11-22 21:41:37 +00:00
|
|
|
}
|
|
|
|
|
2018-10-26 07:45:05 +00:00
|
|
|
/** Compute partial start, indicate start of partial computation.
|
2017-11-22 21:41:37 +00:00
|
|
|
*
|
|
|
|
* This API should be called before performing any partial computation
|
|
|
|
* with compute_partial API.
|
|
|
|
*
|
|
|
|
* @param crc Initial CRC value set by the API
|
|
|
|
* @return 0 on success or a negative in case of failure
|
|
|
|
* @note: CRC is an out parameter and must be reused with compute_partial
|
2018-08-13 19:54:56 +00:00
|
|
|
* and `compute_partial_stop` without any modifications in application.
|
2017-11-22 21:41:37 +00:00
|
|
|
*/
|
|
|
|
int32_t compute_partial_start(uint32_t *crc)
|
|
|
|
{
|
2019-11-26 13:45:37 +00:00
|
|
|
MBED_ASSERT(crc != NULL);
|
|
|
|
|
2018-12-19 23:16:42 +00:00
|
|
|
#if DEVICE_CRC
|
2019-11-26 13:45:37 +00:00
|
|
|
if (_mode == HARDWARE) {
|
2018-08-13 19:54:56 +00:00
|
|
|
lock();
|
2018-04-30 16:40:03 +00:00
|
|
|
crc_mbed_config_t config;
|
|
|
|
config.polynomial = polynomial;
|
2018-05-04 10:43:39 +00:00
|
|
|
config.width = width;
|
2018-04-30 16:40:03 +00:00
|
|
|
config.initial_xor = _initial_value;
|
|
|
|
config.final_xor = _final_xor;
|
|
|
|
config.reflect_in = _reflect_data;
|
|
|
|
config.reflect_out = _reflect_remainder;
|
|
|
|
|
|
|
|
hal_crc_compute_partial_start(&config);
|
2018-04-24 15:00:17 +00:00
|
|
|
}
|
2018-08-13 19:54:56 +00:00
|
|
|
#endif
|
2018-04-24 15:00:17 +00:00
|
|
|
|
2017-11-22 21:41:37 +00:00
|
|
|
*crc = _initial_value;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Get the final CRC value of partial computation.
|
|
|
|
*
|
|
|
|
* CRC value available in partial computation is not correct CRC, as some
|
|
|
|
* algorithms require remainder to be reflected and final value to be XORed
|
|
|
|
* This API is used to perform final computation to get correct CRC value.
|
|
|
|
*
|
|
|
|
* @param crc CRC result
|
2018-10-26 07:45:38 +00:00
|
|
|
* @return 0 on success or a negative in case of failure.
|
2017-11-22 21:41:37 +00:00
|
|
|
*/
|
|
|
|
int32_t compute_partial_stop(uint32_t *crc)
|
|
|
|
{
|
2019-11-26 13:45:37 +00:00
|
|
|
MBED_ASSERT(crc != NULL);
|
|
|
|
|
2018-12-19 23:16:42 +00:00
|
|
|
#if DEVICE_CRC
|
2019-11-26 13:45:37 +00:00
|
|
|
if (_mode == HARDWARE) {
|
2018-04-24 15:00:17 +00:00
|
|
|
*crc = hal_crc_get_result();
|
2018-08-13 19:54:56 +00:00
|
|
|
unlock();
|
2018-04-24 15:00:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2018-08-08 18:56:13 +00:00
|
|
|
#endif
|
2019-11-26 13:45:37 +00:00
|
|
|
uint32_t p_crc = *crc;
|
|
|
|
if ((width < 8) && (NULL == _crc_table)) {
|
|
|
|
p_crc = (uint32_t)(p_crc << (8 - width));
|
2018-08-08 18:56:42 +00:00
|
|
|
}
|
2019-11-26 13:45:37 +00:00
|
|
|
// Optimized algorithm for 32BitANSI does not need additional reflect_remainder
|
|
|
|
if ((TABLE == _mode) && (POLY_32BIT_REV_ANSI == polynomial)) {
|
|
|
|
*crc = (p_crc ^ _final_xor) & get_crc_mask();
|
|
|
|
} else {
|
|
|
|
*crc = (reflect_remainder(p_crc) ^ _final_xor) & get_crc_mask();
|
|
|
|
}
|
|
|
|
unlock();
|
2017-11-22 21:41:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-11-26 13:45:37 +00:00
|
|
|
/** Get the current CRC polynomial.
|
2017-11-22 21:41:37 +00:00
|
|
|
*
|
2019-11-26 13:45:37 +00:00
|
|
|
* @return Polynomial value
|
2017-11-22 21:41:37 +00:00
|
|
|
*/
|
2019-11-26 13:45:37 +00:00
|
|
|
uint32_t get_polynomial(void) const
|
2017-11-22 21:41:37 +00:00
|
|
|
{
|
2019-11-26 13:45:37 +00:00
|
|
|
return polynomial;
|
2017-11-22 21:41:37 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 13:45:37 +00:00
|
|
|
/** Get the current CRC width
|
2017-11-22 21:41:37 +00:00
|
|
|
*
|
2019-11-26 13:45:37 +00:00
|
|
|
* @return CRC width
|
2017-11-22 21:41:37 +00:00
|
|
|
*/
|
2019-11-26 13:45:37 +00:00
|
|
|
uint8_t get_width(void) const
|
2017-11-22 21:41:37 +00:00
|
|
|
{
|
2019-11-26 13:45:37 +00:00
|
|
|
return width;
|
2017-11-22 21:41:37 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 13:45:37 +00:00
|
|
|
#if !defined(DOXYGEN_ONLY)
|
|
|
|
private:
|
|
|
|
uint32_t _initial_value;
|
|
|
|
uint32_t _final_xor;
|
|
|
|
bool _reflect_data;
|
|
|
|
bool _reflect_remainder;
|
|
|
|
uint32_t *_crc_table;
|
|
|
|
CrcMode _mode;
|
2017-11-22 21:41:37 +00:00
|
|
|
|
2018-10-26 07:45:05 +00:00
|
|
|
/** Acquire exclusive access to CRC hardware/software.
|
2018-08-13 19:54:56 +00:00
|
|
|
*/
|
2019-11-26 13:45:37 +00:00
|
|
|
void lock()
|
2018-08-13 19:54:56 +00:00
|
|
|
{
|
2018-12-19 23:16:42 +00:00
|
|
|
#if DEVICE_CRC
|
2019-11-26 13:45:37 +00:00
|
|
|
if (_mode == HARDWARE) {
|
2018-08-13 19:54:56 +00:00
|
|
|
mbed_crc_mutex->lock();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-10-26 07:45:05 +00:00
|
|
|
/** Release exclusive access to CRC hardware/software.
|
2018-08-13 19:54:56 +00:00
|
|
|
*/
|
2019-11-26 13:45:37 +00:00
|
|
|
virtual void unlock()
|
2018-08-13 19:54:56 +00:00
|
|
|
{
|
2018-12-19 23:16:42 +00:00
|
|
|
#if DEVICE_CRC
|
2019-11-26 13:45:37 +00:00
|
|
|
if (_mode == HARDWARE) {
|
2018-08-13 19:54:56 +00:00
|
|
|
mbed_crc_mutex->unlock();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-11-26 13:45:37 +00:00
|
|
|
/** Get the current CRC data size.
|
2017-11-22 21:41:37 +00:00
|
|
|
*
|
2019-11-26 13:45:37 +00:00
|
|
|
* @return CRC data size in bytes
|
2017-11-22 21:41:37 +00:00
|
|
|
*/
|
2019-11-26 13:45:37 +00:00
|
|
|
uint8_t get_data_size(void) const
|
2017-11-22 21:41:37 +00:00
|
|
|
{
|
2019-11-26 13:45:37 +00:00
|
|
|
return (width <= 8 ? 1 : (width <= 16 ? 2 : 4));
|
2017-11-22 21:41:37 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 13:45:37 +00:00
|
|
|
/** Get the top bit of current CRC.
|
2017-11-22 21:41:37 +00:00
|
|
|
*
|
2019-11-26 13:45:37 +00:00
|
|
|
* @return Top bit is set high for respective data width of current CRC
|
|
|
|
* Top bit for CRC width less then 8 bits will be set as 8th bit.
|
2017-11-22 21:41:37 +00:00
|
|
|
*/
|
2019-11-26 13:45:37 +00:00
|
|
|
uint32_t get_top_bit(void) const
|
2017-11-22 21:41:37 +00:00
|
|
|
{
|
2019-11-26 13:45:37 +00:00
|
|
|
return (width < 8 ? (1u << 7) : (uint32_t)(1ul << (width - 1)));
|
2017-11-22 21:41:37 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 13:45:37 +00:00
|
|
|
/** Get the CRC data mask.
|
Revise MbedCRC template
* Use compile-time detection of hardware CRC capability, so unneeded
code and tables do not go into the image.
* Add global JSON config option to allow choice between no tables,
16-entry tables or 256-entry tables for software CRC. Default set
to 16-entry, reducing ROM size from previous 256-entry.
* Allow manual override in template parameter to force software or
bitwise CRC for a particular instance.
* Micro-optimisations, particularly use of `RBIT` instruction and
optimising bitwise computation using inline assembler.
Incompatible changes:
* Remove special-case "POLY_32BIT_REV_ANSI" - users can use standard
POLY_32BIT_ANSI, which now uses the same 16-entry tables by default,
or can use hardware acceleration, which was disabled for
POLY_32BIT_REV_ANSI. MbedCRC<POLY_32BIT_ANSI, 32, CrcMode::TABLE> can
be used to force software like POLY_32BIT_REV_ANSI.
* The precomputed table for POLY_16BIT_IBM had errors - this has been
corrected, but software CRC results will be different from the previous
software calculation.
* < 8-bit CRC results are no longer are shifted up in the output value,
but placed in the lowest bits, like other sizes. This means that code
performing the SD command CRC will now need to use `(crc << 1) | 1`,
rather than `crc | 1`.
2019-09-24 13:49:04 +00:00
|
|
|
*
|
2019-11-26 13:45:37 +00:00
|
|
|
* @return CRC data mask is generated based on current CRC width
|
Revise MbedCRC template
* Use compile-time detection of hardware CRC capability, so unneeded
code and tables do not go into the image.
* Add global JSON config option to allow choice between no tables,
16-entry tables or 256-entry tables for software CRC. Default set
to 16-entry, reducing ROM size from previous 256-entry.
* Allow manual override in template parameter to force software or
bitwise CRC for a particular instance.
* Micro-optimisations, particularly use of `RBIT` instruction and
optimising bitwise computation using inline assembler.
Incompatible changes:
* Remove special-case "POLY_32BIT_REV_ANSI" - users can use standard
POLY_32BIT_ANSI, which now uses the same 16-entry tables by default,
or can use hardware acceleration, which was disabled for
POLY_32BIT_REV_ANSI. MbedCRC<POLY_32BIT_ANSI, 32, CrcMode::TABLE> can
be used to force software like POLY_32BIT_REV_ANSI.
* The precomputed table for POLY_16BIT_IBM had errors - this has been
corrected, but software CRC results will be different from the previous
software calculation.
* < 8-bit CRC results are no longer are shifted up in the output value,
but placed in the lowest bits, like other sizes. This means that code
performing the SD command CRC will now need to use `(crc << 1) | 1`,
rather than `crc | 1`.
2019-09-24 13:49:04 +00:00
|
|
|
*/
|
2019-11-26 13:45:37 +00:00
|
|
|
uint32_t get_crc_mask(void) const
|
Revise MbedCRC template
* Use compile-time detection of hardware CRC capability, so unneeded
code and tables do not go into the image.
* Add global JSON config option to allow choice between no tables,
16-entry tables or 256-entry tables for software CRC. Default set
to 16-entry, reducing ROM size from previous 256-entry.
* Allow manual override in template parameter to force software or
bitwise CRC for a particular instance.
* Micro-optimisations, particularly use of `RBIT` instruction and
optimising bitwise computation using inline assembler.
Incompatible changes:
* Remove special-case "POLY_32BIT_REV_ANSI" - users can use standard
POLY_32BIT_ANSI, which now uses the same 16-entry tables by default,
or can use hardware acceleration, which was disabled for
POLY_32BIT_REV_ANSI. MbedCRC<POLY_32BIT_ANSI, 32, CrcMode::TABLE> can
be used to force software like POLY_32BIT_REV_ANSI.
* The precomputed table for POLY_16BIT_IBM had errors - this has been
corrected, but software CRC results will be different from the previous
software calculation.
* < 8-bit CRC results are no longer are shifted up in the output value,
but placed in the lowest bits, like other sizes. This means that code
performing the SD command CRC will now need to use `(crc << 1) | 1`,
rather than `crc | 1`.
2019-09-24 13:49:04 +00:00
|
|
|
{
|
2019-11-26 13:45:37 +00:00
|
|
|
return (width < 8 ? ((1u << 8) - 1) : (uint32_t)((uint64_t)(1ull << width) - 1));
|
Revise MbedCRC template
* Use compile-time detection of hardware CRC capability, so unneeded
code and tables do not go into the image.
* Add global JSON config option to allow choice between no tables,
16-entry tables or 256-entry tables for software CRC. Default set
to 16-entry, reducing ROM size from previous 256-entry.
* Allow manual override in template parameter to force software or
bitwise CRC for a particular instance.
* Micro-optimisations, particularly use of `RBIT` instruction and
optimising bitwise computation using inline assembler.
Incompatible changes:
* Remove special-case "POLY_32BIT_REV_ANSI" - users can use standard
POLY_32BIT_ANSI, which now uses the same 16-entry tables by default,
or can use hardware acceleration, which was disabled for
POLY_32BIT_REV_ANSI. MbedCRC<POLY_32BIT_ANSI, 32, CrcMode::TABLE> can
be used to force software like POLY_32BIT_REV_ANSI.
* The precomputed table for POLY_16BIT_IBM had errors - this has been
corrected, but software CRC results will be different from the previous
software calculation.
* < 8-bit CRC results are no longer are shifted up in the output value,
but placed in the lowest bits, like other sizes. This means that code
performing the SD command CRC will now need to use `(crc << 1) | 1`,
rather than `crc | 1`.
2019-09-24 13:49:04 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 13:45:37 +00:00
|
|
|
/** Final value of CRC is reflected.
|
Revise MbedCRC template
* Use compile-time detection of hardware CRC capability, so unneeded
code and tables do not go into the image.
* Add global JSON config option to allow choice between no tables,
16-entry tables or 256-entry tables for software CRC. Default set
to 16-entry, reducing ROM size from previous 256-entry.
* Allow manual override in template parameter to force software or
bitwise CRC for a particular instance.
* Micro-optimisations, particularly use of `RBIT` instruction and
optimising bitwise computation using inline assembler.
Incompatible changes:
* Remove special-case "POLY_32BIT_REV_ANSI" - users can use standard
POLY_32BIT_ANSI, which now uses the same 16-entry tables by default,
or can use hardware acceleration, which was disabled for
POLY_32BIT_REV_ANSI. MbedCRC<POLY_32BIT_ANSI, 32, CrcMode::TABLE> can
be used to force software like POLY_32BIT_REV_ANSI.
* The precomputed table for POLY_16BIT_IBM had errors - this has been
corrected, but software CRC results will be different from the previous
software calculation.
* < 8-bit CRC results are no longer are shifted up in the output value,
but placed in the lowest bits, like other sizes. This means that code
performing the SD command CRC will now need to use `(crc << 1) | 1`,
rather than `crc | 1`.
2019-09-24 13:49:04 +00:00
|
|
|
*
|
2019-11-26 13:45:37 +00:00
|
|
|
* @param data final crc value, which should be reflected
|
|
|
|
* @return Reflected CRC value
|
Revise MbedCRC template
* Use compile-time detection of hardware CRC capability, so unneeded
code and tables do not go into the image.
* Add global JSON config option to allow choice between no tables,
16-entry tables or 256-entry tables for software CRC. Default set
to 16-entry, reducing ROM size from previous 256-entry.
* Allow manual override in template parameter to force software or
bitwise CRC for a particular instance.
* Micro-optimisations, particularly use of `RBIT` instruction and
optimising bitwise computation using inline assembler.
Incompatible changes:
* Remove special-case "POLY_32BIT_REV_ANSI" - users can use standard
POLY_32BIT_ANSI, which now uses the same 16-entry tables by default,
or can use hardware acceleration, which was disabled for
POLY_32BIT_REV_ANSI. MbedCRC<POLY_32BIT_ANSI, 32, CrcMode::TABLE> can
be used to force software like POLY_32BIT_REV_ANSI.
* The precomputed table for POLY_16BIT_IBM had errors - this has been
corrected, but software CRC results will be different from the previous
software calculation.
* < 8-bit CRC results are no longer are shifted up in the output value,
but placed in the lowest bits, like other sizes. This means that code
performing the SD command CRC will now need to use `(crc << 1) | 1`,
rather than `crc | 1`.
2019-09-24 13:49:04 +00:00
|
|
|
*/
|
2019-11-26 13:45:37 +00:00
|
|
|
uint32_t reflect_remainder(uint32_t data) const
|
Revise MbedCRC template
* Use compile-time detection of hardware CRC capability, so unneeded
code and tables do not go into the image.
* Add global JSON config option to allow choice between no tables,
16-entry tables or 256-entry tables for software CRC. Default set
to 16-entry, reducing ROM size from previous 256-entry.
* Allow manual override in template parameter to force software or
bitwise CRC for a particular instance.
* Micro-optimisations, particularly use of `RBIT` instruction and
optimising bitwise computation using inline assembler.
Incompatible changes:
* Remove special-case "POLY_32BIT_REV_ANSI" - users can use standard
POLY_32BIT_ANSI, which now uses the same 16-entry tables by default,
or can use hardware acceleration, which was disabled for
POLY_32BIT_REV_ANSI. MbedCRC<POLY_32BIT_ANSI, 32, CrcMode::TABLE> can
be used to force software like POLY_32BIT_REV_ANSI.
* The precomputed table for POLY_16BIT_IBM had errors - this has been
corrected, but software CRC results will be different from the previous
software calculation.
* < 8-bit CRC results are no longer are shifted up in the output value,
but placed in the lowest bits, like other sizes. This means that code
performing the SD command CRC will now need to use `(crc << 1) | 1`,
rather than `crc | 1`.
2019-09-24 13:49:04 +00:00
|
|
|
{
|
2019-11-26 13:45:37 +00:00
|
|
|
if (_reflect_remainder) {
|
|
|
|
uint32_t reflection = 0x0;
|
|
|
|
uint8_t const nBits = (width < 8 ? 8 : width);
|
Revise MbedCRC template
* Use compile-time detection of hardware CRC capability, so unneeded
code and tables do not go into the image.
* Add global JSON config option to allow choice between no tables,
16-entry tables or 256-entry tables for software CRC. Default set
to 16-entry, reducing ROM size from previous 256-entry.
* Allow manual override in template parameter to force software or
bitwise CRC for a particular instance.
* Micro-optimisations, particularly use of `RBIT` instruction and
optimising bitwise computation using inline assembler.
Incompatible changes:
* Remove special-case "POLY_32BIT_REV_ANSI" - users can use standard
POLY_32BIT_ANSI, which now uses the same 16-entry tables by default,
or can use hardware acceleration, which was disabled for
POLY_32BIT_REV_ANSI. MbedCRC<POLY_32BIT_ANSI, 32, CrcMode::TABLE> can
be used to force software like POLY_32BIT_REV_ANSI.
* The precomputed table for POLY_16BIT_IBM had errors - this has been
corrected, but software CRC results will be different from the previous
software calculation.
* < 8-bit CRC results are no longer are shifted up in the output value,
but placed in the lowest bits, like other sizes. This means that code
performing the SD command CRC will now need to use `(crc << 1) | 1`,
rather than `crc | 1`.
2019-09-24 13:49:04 +00:00
|
|
|
|
2019-11-26 13:45:37 +00:00
|
|
|
for (uint8_t bit = 0; bit < nBits; ++bit) {
|
|
|
|
if (data & 0x01) {
|
|
|
|
reflection |= (1 << ((nBits - 1) - bit));
|
|
|
|
}
|
|
|
|
data = (data >> 1);
|
|
|
|
}
|
|
|
|
return (reflection);
|
2017-11-22 21:41:37 +00:00
|
|
|
} else {
|
2019-11-26 13:45:37 +00:00
|
|
|
return data;
|
2017-11-22 21:41:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-26 13:45:37 +00:00
|
|
|
/** Data bytes are reflected.
|
2017-11-22 21:41:37 +00:00
|
|
|
*
|
2019-11-26 13:45:37 +00:00
|
|
|
* @param data value to be reflected
|
|
|
|
* @return Reflected data value
|
2017-11-22 21:41:37 +00:00
|
|
|
*/
|
2019-11-26 13:45:37 +00:00
|
|
|
uint32_t reflect_bytes(uint32_t data) const
|
2017-11-22 21:41:37 +00:00
|
|
|
{
|
2019-11-26 13:45:37 +00:00
|
|
|
if (_reflect_data) {
|
|
|
|
uint32_t reflection = 0x0;
|
|
|
|
|
|
|
|
for (uint8_t bit = 0; bit < 8; ++bit) {
|
|
|
|
if (data & 0x01) {
|
|
|
|
reflection |= (1 << (7 - bit));
|
|
|
|
}
|
|
|
|
data = (data >> 1);
|
|
|
|
}
|
|
|
|
return (reflection);
|
2017-11-22 21:41:37 +00:00
|
|
|
} else {
|
2019-11-26 13:45:37 +00:00
|
|
|
return data;
|
2017-11-22 21:41:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-26 07:45:05 +00:00
|
|
|
/** Bitwise CRC computation.
|
2017-11-22 21:41:37 +00:00
|
|
|
*
|
|
|
|
* @param buffer data buffer
|
|
|
|
* @param size size of the data
|
|
|
|
* @param crc CRC value is filled in, but the value is not the final
|
|
|
|
* @return 0 on success or a negative error code on failure
|
|
|
|
*/
|
2019-11-26 13:45:37 +00:00
|
|
|
int32_t bitwise_compute_partial(const void *buffer, crc_data_size_t size, uint32_t *crc) const
|
2017-11-22 21:41:37 +00:00
|
|
|
{
|
2019-11-26 13:45:37 +00:00
|
|
|
MBED_ASSERT(crc != NULL);
|
2017-11-22 21:41:37 +00:00
|
|
|
|
2019-11-26 13:45:37 +00:00
|
|
|
const uint8_t *data = static_cast<const uint8_t *>(buffer);
|
|
|
|
uint32_t p_crc = *crc;
|
Revise MbedCRC template
* Use compile-time detection of hardware CRC capability, so unneeded
code and tables do not go into the image.
* Add global JSON config option to allow choice between no tables,
16-entry tables or 256-entry tables for software CRC. Default set
to 16-entry, reducing ROM size from previous 256-entry.
* Allow manual override in template parameter to force software or
bitwise CRC for a particular instance.
* Micro-optimisations, particularly use of `RBIT` instruction and
optimising bitwise computation using inline assembler.
Incompatible changes:
* Remove special-case "POLY_32BIT_REV_ANSI" - users can use standard
POLY_32BIT_ANSI, which now uses the same 16-entry tables by default,
or can use hardware acceleration, which was disabled for
POLY_32BIT_REV_ANSI. MbedCRC<POLY_32BIT_ANSI, 32, CrcMode::TABLE> can
be used to force software like POLY_32BIT_REV_ANSI.
* The precomputed table for POLY_16BIT_IBM had errors - this has been
corrected, but software CRC results will be different from the previous
software calculation.
* < 8-bit CRC results are no longer are shifted up in the output value,
but placed in the lowest bits, like other sizes. This means that code
performing the SD command CRC will now need to use `(crc << 1) | 1`,
rather than `crc | 1`.
2019-09-24 13:49:04 +00:00
|
|
|
|
2019-11-26 13:45:37 +00:00
|
|
|
if (width < 8) {
|
|
|
|
uint8_t data_byte;
|
|
|
|
for (crc_data_size_t byte = 0; byte < size; byte++) {
|
|
|
|
data_byte = reflect_bytes(data[byte]);
|
|
|
|
for (uint8_t bit = 8; bit > 0; --bit) {
|
|
|
|
p_crc <<= 1;
|
|
|
|
if ((data_byte ^ p_crc) & get_top_bit()) {
|
|
|
|
p_crc ^= polynomial;
|
|
|
|
}
|
|
|
|
data_byte <<= 1;
|
2017-11-22 21:41:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (crc_data_size_t byte = 0; byte < size; byte++) {
|
2019-11-26 13:45:37 +00:00
|
|
|
p_crc ^= (reflect_bytes(data[byte]) << (width - 8));
|
2017-11-22 21:41:37 +00:00
|
|
|
|
|
|
|
// Perform modulo-2 division, a bit at a time
|
2019-11-26 13:45:37 +00:00
|
|
|
for (uint8_t bit = 8; bit > 0; --bit) {
|
|
|
|
if (p_crc & get_top_bit()) {
|
|
|
|
p_crc = (p_crc << 1) ^ polynomial;
|
|
|
|
} else {
|
|
|
|
p_crc = (p_crc << 1);
|
|
|
|
}
|
2017-11-22 21:41:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-26 13:45:37 +00:00
|
|
|
*crc = p_crc & get_crc_mask();
|
2017-11-22 21:41:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-26 07:45:05 +00:00
|
|
|
/** CRC computation using ROM tables.
|
2018-05-24 15:58:14 +00:00
|
|
|
*
|
|
|
|
* @param buffer data buffer
|
|
|
|
* @param size size of the data
|
|
|
|
* @param crc CRC value is filled in, but the value is not the final
|
|
|
|
* @return 0 on success or a negative error code on failure
|
|
|
|
*/
|
2019-11-26 13:45:37 +00:00
|
|
|
int32_t table_compute_partial(const void *buffer, crc_data_size_t size, uint32_t *crc) const
|
2017-11-22 21:41:37 +00:00
|
|
|
{
|
2019-11-26 13:45:37 +00:00
|
|
|
MBED_ASSERT(crc != NULL);
|
|
|
|
|
|
|
|
const uint8_t *data = static_cast<const uint8_t *>(buffer);
|
|
|
|
uint32_t p_crc = *crc;
|
|
|
|
uint8_t data_byte = 0;
|
|
|
|
|
|
|
|
if (width <= 8) {
|
|
|
|
uint8_t *crc_table = (uint8_t *)_crc_table;
|
|
|
|
for (crc_data_size_t byte = 0; byte < size; byte++) {
|
|
|
|
data_byte = reflect_bytes(data[byte]) ^ p_crc;
|
|
|
|
p_crc = crc_table[data_byte];
|
|
|
|
}
|
|
|
|
} else if (width <= 16) {
|
|
|
|
uint16_t *crc_table = (uint16_t *)_crc_table;
|
|
|
|
for (crc_data_size_t byte = 0; byte < size; byte++) {
|
|
|
|
data_byte = reflect_bytes(data[byte]) ^ (p_crc >> (width - 8));
|
|
|
|
p_crc = crc_table[data_byte] ^ (p_crc << 8);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
uint32_t *crc_table = (uint32_t *)_crc_table;
|
|
|
|
if (POLY_32BIT_REV_ANSI == polynomial) {
|
|
|
|
for (crc_data_size_t i = 0; i < size; i++) {
|
|
|
|
p_crc = (p_crc >> 4) ^ crc_table[(p_crc ^ (data[i] >> 0)) & 0xf];
|
|
|
|
p_crc = (p_crc >> 4) ^ crc_table[(p_crc ^ (data[i] >> 4)) & 0xf];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (crc_data_size_t byte = 0; byte < size; byte++) {
|
|
|
|
data_byte = reflect_bytes(data[byte]) ^ (p_crc >> (width - 8));
|
|
|
|
p_crc = crc_table[data_byte] ^ (p_crc << 8);
|
|
|
|
}
|
2017-11-22 21:41:37 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-26 13:45:37 +00:00
|
|
|
*crc = p_crc & get_crc_mask();
|
2017-11-22 21:41:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-11-26 13:45:37 +00:00
|
|
|
/** Constructor init called from all specialized cases of constructor.
|
|
|
|
* Note: All constructor common code should be in this function.
|
2017-11-22 21:41:37 +00:00
|
|
|
*/
|
2019-11-26 13:45:37 +00:00
|
|
|
void mbed_crc_ctor(void)
|
2017-11-22 21:41:37 +00:00
|
|
|
{
|
2019-11-26 13:45:37 +00:00
|
|
|
MBED_STATIC_ASSERT(width <= 32, "Max 32-bit CRC supported");
|
Revise MbedCRC template
* Use compile-time detection of hardware CRC capability, so unneeded
code and tables do not go into the image.
* Add global JSON config option to allow choice between no tables,
16-entry tables or 256-entry tables for software CRC. Default set
to 16-entry, reducing ROM size from previous 256-entry.
* Allow manual override in template parameter to force software or
bitwise CRC for a particular instance.
* Micro-optimisations, particularly use of `RBIT` instruction and
optimising bitwise computation using inline assembler.
Incompatible changes:
* Remove special-case "POLY_32BIT_REV_ANSI" - users can use standard
POLY_32BIT_ANSI, which now uses the same 16-entry tables by default,
or can use hardware acceleration, which was disabled for
POLY_32BIT_REV_ANSI. MbedCRC<POLY_32BIT_ANSI, 32, CrcMode::TABLE> can
be used to force software like POLY_32BIT_REV_ANSI.
* The precomputed table for POLY_16BIT_IBM had errors - this has been
corrected, but software CRC results will be different from the previous
software calculation.
* < 8-bit CRC results are no longer are shifted up in the output value,
but placed in the lowest bits, like other sizes. This means that code
performing the SD command CRC will now need to use `(crc << 1) | 1`,
rather than `crc | 1`.
2019-09-24 13:49:04 +00:00
|
|
|
|
2019-11-26 13:45:37 +00:00
|
|
|
#if DEVICE_CRC
|
|
|
|
if (POLY_32BIT_REV_ANSI != polynomial) {
|
|
|
|
crc_mbed_config_t config;
|
|
|
|
config.polynomial = polynomial;
|
|
|
|
config.width = width;
|
|
|
|
config.initial_xor = _initial_value;
|
|
|
|
config.final_xor = _final_xor;
|
|
|
|
config.reflect_in = _reflect_data;
|
|
|
|
config.reflect_out = _reflect_remainder;
|
Revise MbedCRC template
* Use compile-time detection of hardware CRC capability, so unneeded
code and tables do not go into the image.
* Add global JSON config option to allow choice between no tables,
16-entry tables or 256-entry tables for software CRC. Default set
to 16-entry, reducing ROM size from previous 256-entry.
* Allow manual override in template parameter to force software or
bitwise CRC for a particular instance.
* Micro-optimisations, particularly use of `RBIT` instruction and
optimising bitwise computation using inline assembler.
Incompatible changes:
* Remove special-case "POLY_32BIT_REV_ANSI" - users can use standard
POLY_32BIT_ANSI, which now uses the same 16-entry tables by default,
or can use hardware acceleration, which was disabled for
POLY_32BIT_REV_ANSI. MbedCRC<POLY_32BIT_ANSI, 32, CrcMode::TABLE> can
be used to force software like POLY_32BIT_REV_ANSI.
* The precomputed table for POLY_16BIT_IBM had errors - this has been
corrected, but software CRC results will be different from the previous
software calculation.
* < 8-bit CRC results are no longer are shifted up in the output value,
but placed in the lowest bits, like other sizes. This means that code
performing the SD command CRC will now need to use `(crc << 1) | 1`,
rather than `crc | 1`.
2019-09-24 13:49:04 +00:00
|
|
|
|
2019-11-26 13:45:37 +00:00
|
|
|
if (hal_crc_is_supported(&config)) {
|
|
|
|
_mode = HARDWARE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
Revise MbedCRC template
* Use compile-time detection of hardware CRC capability, so unneeded
code and tables do not go into the image.
* Add global JSON config option to allow choice between no tables,
16-entry tables or 256-entry tables for software CRC. Default set
to 16-entry, reducing ROM size from previous 256-entry.
* Allow manual override in template parameter to force software or
bitwise CRC for a particular instance.
* Micro-optimisations, particularly use of `RBIT` instruction and
optimising bitwise computation using inline assembler.
Incompatible changes:
* Remove special-case "POLY_32BIT_REV_ANSI" - users can use standard
POLY_32BIT_ANSI, which now uses the same 16-entry tables by default,
or can use hardware acceleration, which was disabled for
POLY_32BIT_REV_ANSI. MbedCRC<POLY_32BIT_ANSI, 32, CrcMode::TABLE> can
be used to force software like POLY_32BIT_REV_ANSI.
* The precomputed table for POLY_16BIT_IBM had errors - this has been
corrected, but software CRC results will be different from the previous
software calculation.
* < 8-bit CRC results are no longer are shifted up in the output value,
but placed in the lowest bits, like other sizes. This means that code
performing the SD command CRC will now need to use `(crc << 1) | 1`,
rather than `crc | 1`.
2019-09-24 13:49:04 +00:00
|
|
|
|
2019-11-26 13:45:37 +00:00
|
|
|
switch (polynomial) {
|
|
|
|
case POLY_32BIT_ANSI:
|
|
|
|
_crc_table = (uint32_t *)Table_CRC_32bit_ANSI;
|
|
|
|
break;
|
|
|
|
case POLY_32BIT_REV_ANSI:
|
|
|
|
_crc_table = (uint32_t *)Table_CRC_32bit_Rev_ANSI;
|
|
|
|
break;
|
|
|
|
case POLY_8BIT_CCITT:
|
|
|
|
_crc_table = (uint32_t *)Table_CRC_8bit_CCITT;
|
|
|
|
break;
|
|
|
|
case POLY_7BIT_SD:
|
|
|
|
_crc_table = (uint32_t *)Table_CRC_7Bit_SD;
|
|
|
|
break;
|
|
|
|
case POLY_16BIT_CCITT:
|
|
|
|
_crc_table = (uint32_t *)Table_CRC_16bit_CCITT;
|
|
|
|
break;
|
|
|
|
case POLY_16BIT_IBM:
|
|
|
|
_crc_table = (uint32_t *)Table_CRC_16bit_IBM;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
_crc_table = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_mode = (_crc_table != NULL) ? TABLE : BITWISE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
};
|
Revise MbedCRC template
* Use compile-time detection of hardware CRC capability, so unneeded
code and tables do not go into the image.
* Add global JSON config option to allow choice between no tables,
16-entry tables or 256-entry tables for software CRC. Default set
to 16-entry, reducing ROM size from previous 256-entry.
* Allow manual override in template parameter to force software or
bitwise CRC for a particular instance.
* Micro-optimisations, particularly use of `RBIT` instruction and
optimising bitwise computation using inline assembler.
Incompatible changes:
* Remove special-case "POLY_32BIT_REV_ANSI" - users can use standard
POLY_32BIT_ANSI, which now uses the same 16-entry tables by default,
or can use hardware acceleration, which was disabled for
POLY_32BIT_REV_ANSI. MbedCRC<POLY_32BIT_ANSI, 32, CrcMode::TABLE> can
be used to force software like POLY_32BIT_REV_ANSI.
* The precomputed table for POLY_16BIT_IBM had errors - this has been
corrected, but software CRC results will be different from the previous
software calculation.
* < 8-bit CRC results are no longer are shifted up in the output value,
but placed in the lowest bits, like other sizes. This means that code
performing the SD command CRC will now need to use `(crc << 1) | 1`,
rather than `crc | 1`.
2019-09-24 13:49:04 +00:00
|
|
|
|
2019-11-26 13:45:37 +00:00
|
|
|
#if defined ( __CC_ARM )
|
|
|
|
#elif defined ( __GNUC__ )
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#elif defined (__ICCARM__)
|
|
|
|
#endif
|
2017-11-22 21:41:37 +00:00
|
|
|
|
|
|
|
/** @}*/
|
2019-07-11 15:23:39 +00:00
|
|
|
/** @}*/
|
|
|
|
|
2017-11-22 21:41:37 +00:00
|
|
|
} // namespace mbed
|
|
|
|
|
|
|
|
#endif
|