mirror of https://github.com/ARMmbed/mbed-os.git
Add CRC configuration options to HAL API
parent
167d3f9a1e
commit
e1ca2b32fc
|
@ -201,7 +201,14 @@ public:
|
|||
|
||||
#ifdef DEVICE_CRC
|
||||
if (_mode == HARDWARE) {
|
||||
hal_crc_compute_partial_start(polynomial);
|
||||
crc_mbed_config_t config;
|
||||
config.polynomial = polynomial;
|
||||
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);
|
||||
}
|
||||
#endif // DEVICE_CRC
|
||||
|
||||
|
@ -431,7 +438,14 @@ private:
|
|||
_mode = (_crc_table == NULL) ? TABLE : BITWISE;
|
||||
|
||||
#ifdef DEVICE_CRC
|
||||
if (hal_crc_is_supported(polynomial)) {
|
||||
crc_mbed_config_t config;
|
||||
config.polynomial = polynomial;
|
||||
config.initial_xor = _initial_value;
|
||||
config.final_xor = _final_xor;
|
||||
config.reflect_in = _reflect_data;
|
||||
config.reflect_out = _reflect_remainder;
|
||||
|
||||
if (hal_crc_is_supported(&config)) {
|
||||
_mode = HARDWARE;
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -35,6 +35,19 @@ typedef enum crc_polynomial {
|
|||
POLY_32BIT_ANSI = 0x04C11DB7, // x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x+1
|
||||
} crc_polynomial_t;
|
||||
|
||||
typedef struct crc_mbed_config {
|
||||
/// CRC Polynomial. Example polynomial: 0x21 = 0010_0011 = x^5+x+1
|
||||
uint32_t polynomial;
|
||||
/// Initial seed value for the computation.
|
||||
uint32_t initial_xor;
|
||||
/// Final xor value for the computation.
|
||||
uint32_t final_xor;
|
||||
/// Reflect bits on input.
|
||||
bool reflect_in;
|
||||
/// Reflect bits in final result before returning.
|
||||
bool reflect_out;
|
||||
} crc_mbed_config_t;
|
||||
|
||||
#ifdef DEVICE_CRC
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
@ -70,7 +83,7 @@ extern "C" {
|
|||
*
|
||||
* \return True if running if the polynomial is supported, false if not.
|
||||
*/
|
||||
bool hal_crc_is_supported(const uint32_t polynomial);
|
||||
bool hal_crc_is_supported(const crc_mbed_config_t* polynomial);
|
||||
|
||||
/** Initialise the hardware CRC module with the given polynomial
|
||||
*
|
||||
|
@ -100,7 +113,7 @@ bool hal_crc_is_supported(const uint32_t polynomial);
|
|||
*
|
||||
* \param polynomial CRC Polynomial. Example polynomial: 0x1021 = x^12+x^5+1
|
||||
*/
|
||||
void hal_crc_compute_partial_start(const uint32_t polynomial);
|
||||
void hal_crc_compute_partial_start(const crc_mbed_config_t* polynomial);
|
||||
|
||||
/** Writes data to the current CRC module.
|
||||
*
|
||||
|
|
|
@ -7,77 +7,34 @@
|
|||
|
||||
static crc_bits_t width;
|
||||
|
||||
bool hal_crc_is_supported(const uint32_t polynomial)
|
||||
bool hal_crc_is_supported(const crc_mbed_config_t* config)
|
||||
{
|
||||
switch (polynomial)
|
||||
{
|
||||
case POLY_16BIT_CCITT:
|
||||
case POLY_16BIT_IBM:
|
||||
case POLY_32BIT_ANSI:
|
||||
return true;
|
||||
case POLY_8BIT_CCITT:
|
||||
case POLY_7BIT_SD:
|
||||
return false;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
if (config == NULL)
|
||||
return false;
|
||||
|
||||
if ((config->polynomial & 0x80008000) == 0)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void hal_crc_compute_partial_start(const uint32_t polynomial)
|
||||
void hal_crc_compute_partial_start(const crc_mbed_config_t* config)
|
||||
{
|
||||
crc_config_t config;
|
||||
if (config == NULL)
|
||||
return;
|
||||
|
||||
switch (polynomial)
|
||||
{
|
||||
case POLY_32BIT_ANSI:
|
||||
{
|
||||
width = kCrcBits32;
|
||||
width = ((config->polynomial & 0xFFFF0000U) != 0) ? kCrcBits32 : kCrcBits16;
|
||||
|
||||
config.polynomial = polynomial;
|
||||
config.seed = 0xFFFFFFFFU;
|
||||
config.reflectIn = true;
|
||||
config.reflectOut = true;
|
||||
config.complementChecksum = true;
|
||||
config.crcBits = width;
|
||||
config.crcResult = kCrcFinalChecksum;
|
||||
crc_config_t platform_config;
|
||||
platform_config.polynomial = config->polynomial;
|
||||
platform_config.seed = config->initial_xor;
|
||||
platform_config.reflectIn = config->reflect_in;
|
||||
platform_config.reflectOut = config->reflect_out;
|
||||
platform_config.complementChecksum = true;
|
||||
platform_config.crcBits = width;
|
||||
platform_config.crcResult = kCrcFinalChecksum;
|
||||
|
||||
break;
|
||||
}
|
||||
case POLY_16BIT_IBM:
|
||||
{
|
||||
width = kCrcBits16;
|
||||
|
||||
config.polynomial = polynomial;
|
||||
config.seed = 0;
|
||||
config.reflectIn = true;
|
||||
config.reflectOut = true;
|
||||
config.complementChecksum = false;
|
||||
config.crcBits = width;
|
||||
config.crcResult = kCrcFinalChecksum;
|
||||
|
||||
break;
|
||||
}
|
||||
case POLY_16BIT_CCITT:
|
||||
{
|
||||
width = kCrcBits16;
|
||||
|
||||
config.polynomial = polynomial;
|
||||
config.seed = 0xFFFFFFFFU;
|
||||
config.reflectIn = false;
|
||||
config.reflectOut = false;
|
||||
config.complementChecksum = false;
|
||||
config.crcBits = width;
|
||||
config.crcResult = kCrcFinalChecksum;
|
||||
|
||||
break;
|
||||
}
|
||||
default:
|
||||
MBED_ASSERT("Configuring Mbed CRC with unsupported polynomial");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
CRC_Init(CRC0, &config);
|
||||
CRC_Init(CRC0, &platform_config);
|
||||
}
|
||||
|
||||
void hal_crc_compute_partial(const uint8_t *data, const size_t size)
|
||||
|
|
Loading…
Reference in New Issue