mbed library sources. Supersedes mbed-src.
Fork of mbed-dev by
Diff: drivers/MbedCRC.h
- Revision:
- 186:707f6e361f3e
- Parent:
- 184:08ed48f1de7f
--- a/drivers/MbedCRC.h Thu Apr 19 17:12:19 2018 +0100 +++ b/drivers/MbedCRC.h Fri Jun 22 16:45:37 2018 +0100 @@ -16,8 +16,8 @@ #ifndef MBED_CRC_API_H #define MBED_CRC_API_H -#include <stdint.h> #include "drivers/TableCRC.h" +#include "hal/crc_api.h" #include "platform/mbed_assert.h" /* This is invalid warning from the compiler for below section of code @@ -38,19 +38,6 @@ /** \addtogroup drivers */ /** @{*/ -/** CRC Polynomial value - * - * Different polynomial values supported - */ -typedef enum crc_polynomial { - POLY_OTHER = 0, - POLY_8BIT_CCITT = 0x07, // x8+x2+x+1 - POLY_7BIT_SD = 0x9, // x7+x3+1; - POLY_16BIT_CCITT = 0x1021, // x16+x12+x5+1 - POLY_16BIT_IBM = 0x8005, // x16+x15+x2+1 - POLY_32BIT_ANSI = 0x04C11DB7, // x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x+1 -} crc_polynomial_t; - /** CRC object provides CRC generation through hardware/software * * ROM polynomial tables for supported polynomials (:: crc_polynomial_t) will be used for @@ -107,6 +94,9 @@ class MbedCRC { public: + enum CrcMode { HARDWARE = 0, TABLE, BITWISE }; + +public: typedef uint64_t crc_data_size_t; /** Lifetime of CRC object @@ -178,13 +168,21 @@ */ int32_t compute_partial(void *buffer, crc_data_size_t size, uint32_t *crc) { - if (NULL == _crc_table) { - // Compute bitwise CRC - return bitwise_compute_partial(buffer, size, crc); - } else { - // Table CRC - return table_compute_partial(buffer, size, crc); + switch (_mode) + { + case HARDWARE: +#ifdef DEVICE_CRC + hal_crc_compute_partial((uint8_t *)buffer, size); +#endif // DEVICE_CRC + *crc = 0; + return 0; + case TABLE: + return table_compute_partial(buffer, size, crc); + case BITWISE: + return bitwise_compute_partial(buffer, size, crc); } + + return -1; } /** Compute partial start, indicate start of partial computation @@ -200,6 +198,21 @@ int32_t compute_partial_start(uint32_t *crc) { MBED_ASSERT(crc != NULL); + +#ifdef DEVICE_CRC + if (_mode == HARDWARE) { + 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; + + hal_crc_compute_partial_start(&config); + } +#endif // DEVICE_CRC + *crc = _initial_value; return 0; } @@ -215,6 +228,16 @@ int32_t compute_partial_stop(uint32_t *crc) { MBED_ASSERT(crc != NULL); + + if (_mode == HARDWARE) { +#ifdef DEVICE_CRC + *crc = hal_crc_get_result(); + return 0; +#else + return -1; +#endif + } + uint32_t p_crc = *crc; if ((width < 8) && (NULL == _crc_table)) { p_crc = (uint32_t)(p_crc << (8 - width)); @@ -247,6 +270,7 @@ bool _reflect_data; bool _reflect_remainder; uint32_t *_crc_table; + CrcMode _mode; /** Get the current CRC data size * @@ -408,9 +432,25 @@ /** Constructor init called from all specialized cases of constructor * Note: All construtor common code should be in this function. */ - void mbed_crc_ctor(void) const + void mbed_crc_ctor(void) { MBED_STATIC_ASSERT(width <= 32, "Max 32-bit CRC supported"); + + _mode = (_crc_table != NULL) ? TABLE : BITWISE; + +#ifdef DEVICE_CRC + 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; + + if (hal_crc_is_supported(&config)) { + _mode = HARDWARE; + } +#endif } };