Add CRC configuration options to HAL API

pull/6708/head
Steven Cartmell 2018-04-30 17:40:03 +01:00
parent 167d3f9a1e
commit e1ca2b32fc
3 changed files with 52 additions and 68 deletions

View File

@ -201,7 +201,14 @@ public:
#ifdef DEVICE_CRC #ifdef DEVICE_CRC
if (_mode == HARDWARE) { 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 #endif // DEVICE_CRC
@ -431,7 +438,14 @@ private:
_mode = (_crc_table == NULL) ? TABLE : BITWISE; _mode = (_crc_table == NULL) ? TABLE : BITWISE;
#ifdef DEVICE_CRC #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; _mode = HARDWARE;
} }
#endif #endif

View File

@ -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 POLY_32BIT_ANSI = 0x04C11DB7, // x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x+1
} crc_polynomial_t; } 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 DEVICE_CRC
#ifdef __cplusplus #ifdef __cplusplus
@ -70,7 +83,7 @@ extern "C" {
* *
* \return True if running if the polynomial is supported, false if not. * \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 /** 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 * \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. /** Writes data to the current CRC module.
* *

View File

@ -7,77 +7,34 @@
static crc_bits_t width; 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) if (config == NULL)
{ return false;
case POLY_16BIT_CCITT:
case POLY_16BIT_IBM: if ((config->polynomial & 0x80008000) == 0)
case POLY_32BIT_ANSI: return false;
return true;
case POLY_8BIT_CCITT: return true;
case POLY_7BIT_SD:
return false;
default:
return false;
}
} }
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) width = ((config->polynomial & 0xFFFF0000U) != 0) ? kCrcBits32 : kCrcBits16;
{
case POLY_32BIT_ANSI:
{
width = kCrcBits32;
config.polynomial = polynomial; crc_config_t platform_config;
config.seed = 0xFFFFFFFFU; platform_config.polynomial = config->polynomial;
config.reflectIn = true; platform_config.seed = config->initial_xor;
config.reflectOut = true; platform_config.reflectIn = config->reflect_in;
config.complementChecksum = true; platform_config.reflectOut = config->reflect_out;
config.crcBits = width; platform_config.complementChecksum = true;
config.crcResult = kCrcFinalChecksum; platform_config.crcBits = width;
platform_config.crcResult = kCrcFinalChecksum;
break; CRC_Init(CRC0, &platform_config);
}
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);
} }
void hal_crc_compute_partial(const uint8_t *data, const size_t size) void hal_crc_compute_partial(const uint8_t *data, const size_t size)