17 #ifndef MBED_CRC_API_H 18 #define MBED_CRC_API_H 20 #include "drivers/TableCRC.h" 21 #include "hal/crc_api.h" 22 #include "platform/mbed_assert.h" 23 #include "platform/SingletonPtr.h" 24 #include "platform/PlatformMutex.h" 33 #if defined ( __CC_ARM ) 34 #pragma diag_suppress 62 // Shift count is negative 35 #elif defined ( __GNUC__ ) 36 #pragma GCC diagnostic push 37 #pragma GCC diagnostic ignored "-Wshift-count-negative" 38 #elif defined (__ICCARM__) 39 #pragma diag_suppress=Pe062 // Shift count is negative 106 template <u
int32_t polynomial = POLY_32BIT_ANSI, u
int8_t w
idth = 32>
118 typedef uint64_t crc_data_size_t;
135 MbedCRC(uint32_t initial_xor, uint32_t final_xor,
bool reflect_data,
bool reflect_remainder) :
136 _initial_value(initial_xor), _final_xor(final_xor), _reflect_data(reflect_data),
137 _reflect_remainder(reflect_remainder)
156 int32_t
compute(
const void *buffer, crc_data_size_t size, uint32_t *crc)
215 status = table_compute_partial(buffer, size, crc);
218 status = bitwise_compute_partial(buffer, size, crc);
243 if (_mode == HARDWARE) {
247 config.
width = width;
257 *crc = _initial_value;
275 if (_mode == HARDWARE) {
276 *crc = hal_crc_get_result();
281 uint32_t p_crc = *crc;
282 if ((width < 8) && (NULL == _crc_table)) {
283 p_crc = (uint32_t)(p_crc << (8 - width));
287 *crc = (p_crc ^ _final_xor) & get_crc_mask();
289 *crc = (reflect_remainder(p_crc) ^ _final_xor) & get_crc_mask();
313 #if !defined(DOXYGEN_ONLY) 315 uint32_t _initial_value;
318 bool _reflect_remainder;
319 uint32_t *_crc_table;
327 if (_mode == HARDWARE) {
328 mbed_crc_mutex->
lock();
335 virtual void unlock()
338 if (_mode == HARDWARE) {
348 uint8_t get_data_size(
void)
const 350 return (width <= 8 ? 1 : (width <= 16 ? 2 : 4));
358 uint32_t get_top_bit(
void)
const 360 return (width < 8 ? (1u << 7) : (uint32_t)(1ul << (width - 1)));
367 uint32_t get_crc_mask(
void)
const 369 return (width < 8 ? ((1u << 8) - 1) : (uint32_t)((uint64_t)(1ull << width) - 1));
377 uint32_t reflect_remainder(uint32_t data)
const 379 if (_reflect_remainder) {
380 uint32_t reflection = 0x0;
381 uint8_t
const nBits = (width < 8 ? 8 : width);
383 for (uint8_t bit = 0; bit < nBits; ++bit) {
385 reflection |= (1 << ((nBits - 1) - bit));
400 uint32_t reflect_bytes(uint32_t data)
const 403 uint32_t reflection = 0x0;
405 for (uint8_t bit = 0; bit < 8; ++bit) {
407 reflection |= (1 << (7 - bit));
424 int32_t bitwise_compute_partial(
const void *buffer, crc_data_size_t size, uint32_t *crc)
const 428 const uint8_t *data =
static_cast<const uint8_t *
>(buffer);
429 uint32_t p_crc = *crc;
433 for (crc_data_size_t byte = 0; byte < size; byte++) {
434 data_byte = reflect_bytes(data[byte]);
435 for (uint8_t bit = 8; bit > 0; --bit) {
437 if ((data_byte ^ p_crc) & get_top_bit()) {
444 for (crc_data_size_t byte = 0; byte < size; byte++) {
445 p_crc ^= (reflect_bytes(data[byte]) << (width - 8));
448 for (uint8_t bit = 8; bit > 0; --bit) {
449 if (p_crc & get_top_bit()) {
450 p_crc = (p_crc << 1) ^ polynomial;
452 p_crc = (p_crc << 1);
457 *crc = p_crc & get_crc_mask();
468 int32_t table_compute_partial(
const void *buffer, crc_data_size_t size, uint32_t *crc)
const 472 const uint8_t *data =
static_cast<const uint8_t *
>(buffer);
473 uint32_t p_crc = *crc;
474 uint8_t data_byte = 0;
477 uint8_t *crc_table = (uint8_t *)_crc_table;
478 for (crc_data_size_t byte = 0; byte < size; byte++) {
479 data_byte = reflect_bytes(data[byte]) ^ p_crc;
480 p_crc = crc_table[data_byte];
482 }
else if (width <= 16) {
483 uint16_t *crc_table = (uint16_t *)_crc_table;
484 for (crc_data_size_t byte = 0; byte < size; byte++) {
485 data_byte = reflect_bytes(data[byte]) ^ (p_crc >> (width - 8));
486 p_crc = crc_table[data_byte] ^ (p_crc << 8);
489 uint32_t *crc_table = (uint32_t *)_crc_table;
491 for (crc_data_size_t i = 0; i < size; i++) {
492 p_crc = (p_crc >> 4) ^ crc_table[(p_crc ^ (data[i] >> 0)) & 0xf];
493 p_crc = (p_crc >> 4) ^ crc_table[(p_crc ^ (data[i] >> 4)) & 0xf];
496 for (crc_data_size_t byte = 0; byte < size; byte++) {
497 data_byte = reflect_bytes(data[byte]) ^ (p_crc >> (width - 8));
498 p_crc = crc_table[data_byte] ^ (p_crc << 8);
502 *crc = p_crc & get_crc_mask();
509 void mbed_crc_ctor(
void)
515 _crc_table = (uint32_t *)Table_CRC_32bit_Rev_ANSI;
521 config.
width = width;
533 switch (polynomial) {
535 _crc_table = (uint32_t *)Table_CRC_32bit_ANSI;
538 _crc_table = (uint32_t *)Table_CRC_32bit_Rev_ANSI;
541 _crc_table = (uint32_t *)Table_CRC_8bit_CCITT;
544 _crc_table = (uint32_t *)Table_CRC_7Bit_SD;
547 _crc_table = (uint32_t *)Table_CRC_16bit_CCITT;
550 _crc_table = (uint32_t *)Table_CRC_16bit_IBM;
556 _mode = (_crc_table != NULL) ? TABLE : BITWISE;
561 #if defined ( __CC_ARM ) 562 #elif defined ( __GNUC__ ) 563 #pragma GCC diagnostic pop 564 #elif defined (__ICCARM__)
int32_t compute(const void *buffer, crc_data_size_t size, uint32_t *crc)
Compute CRC for the data input Compute CRC performs the initialization, computation and collection of...
uint32_t get_polynomial(void) const
Get the current CRC polynomial.
uint32_t final_xor
Final xor value for the computation.
bool hal_crc_is_supported(const crc_mbed_config_t *config)
Determine if the current platform supports hardware CRC for given polynomial.
uint8_t get_width(void) const
Get the current CRC width.
void hal_crc_compute_partial_start(const crc_mbed_config_t *config)
Initialize the hardware CRC module with the given polynomial.
int32_t compute_partial(const void *buffer, crc_data_size_t size, uint32_t *crc)
Compute partial CRC for the data input.
uint32_t width
CRC Bit Width.
MbedCRC(uint32_t initial_xor, uint32_t final_xor, bool reflect_data, bool reflect_remainder)
Lifetime of CRC object.
int32_t compute_partial_start(uint32_t *crc)
Compute partial start, indicate start of partial computation.
CRC object provides CRC generation through hardware or software.
void hal_crc_compute_partial(const uint8_t *data, const size_t size)
Writes data to the current CRC module.
bool reflect_out
Reflect bits in final result before returning.
uint32_t polynomial
CRC Polynomial.
x31+x30+x29+x27+x26+x24+x23+x21+x20+x19+x15+x9+x8+x5
x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x+1
bool reflect_in
Reflect bits on input.
int32_t compute_partial_stop(uint32_t *crc)
Get the final CRC value of partial computation.
uint32_t initial_xor
Initial seed value for the computation.