Skip to content

Commit e1ca2b3

Browse files
author
Steven Cartmell
committed
Add CRC configuration options to HAL API
1 parent 167d3f9 commit e1ca2b3

File tree

3 files changed

+52
-68
lines changed

3 files changed

+52
-68
lines changed

drivers/MbedCRC.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,14 @@ class MbedCRC
201201

202202
#ifdef DEVICE_CRC
203203
if (_mode == HARDWARE) {
204-
hal_crc_compute_partial_start(polynomial);
204+
crc_mbed_config_t config;
205+
config.polynomial = polynomial;
206+
config.initial_xor = _initial_value;
207+
config.final_xor = _final_xor;
208+
config.reflect_in = _reflect_data;
209+
config.reflect_out = _reflect_remainder;
210+
211+
hal_crc_compute_partial_start(&config);
205212
}
206213
#endif // DEVICE_CRC
207214

@@ -431,7 +438,14 @@ class MbedCRC
431438
_mode = (_crc_table == NULL) ? TABLE : BITWISE;
432439

433440
#ifdef DEVICE_CRC
434-
if (hal_crc_is_supported(polynomial)) {
441+
crc_mbed_config_t config;
442+
config.polynomial = polynomial;
443+
config.initial_xor = _initial_value;
444+
config.final_xor = _final_xor;
445+
config.reflect_in = _reflect_data;
446+
config.reflect_out = _reflect_remainder;
447+
448+
if (hal_crc_is_supported(&config)) {
435449
_mode = HARDWARE;
436450
}
437451
#endif

hal/crc_api.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ typedef enum crc_polynomial {
3535
POLY_32BIT_ANSI = 0x04C11DB7, // x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x+1
3636
} crc_polynomial_t;
3737

38+
typedef struct crc_mbed_config {
39+
/// CRC Polynomial. Example polynomial: 0x21 = 0010_0011 = x^5+x+1
40+
uint32_t polynomial;
41+
/// Initial seed value for the computation.
42+
uint32_t initial_xor;
43+
/// Final xor value for the computation.
44+
uint32_t final_xor;
45+
/// Reflect bits on input.
46+
bool reflect_in;
47+
/// Reflect bits in final result before returning.
48+
bool reflect_out;
49+
} crc_mbed_config_t;
50+
3851
#ifdef DEVICE_CRC
3952

4053
#ifdef __cplusplus
@@ -70,7 +83,7 @@ extern "C" {
7083
*
7184
* \return True if running if the polynomial is supported, false if not.
7285
*/
73-
bool hal_crc_is_supported(const uint32_t polynomial);
86+
bool hal_crc_is_supported(const crc_mbed_config_t* polynomial);
7487

7588
/** Initialise the hardware CRC module with the given polynomial
7689
*
@@ -100,7 +113,7 @@ bool hal_crc_is_supported(const uint32_t polynomial);
100113
*
101114
* \param polynomial CRC Polynomial. Example polynomial: 0x1021 = x^12+x^5+1
102115
*/
103-
void hal_crc_compute_partial_start(const uint32_t polynomial);
116+
void hal_crc_compute_partial_start(const crc_mbed_config_t* polynomial);
104117

105118
/** Writes data to the current CRC module.
106119
*

targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/mbed_crc_api.c

Lines changed: 21 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -7,77 +7,34 @@
77

88
static crc_bits_t width;
99

10-
bool hal_crc_is_supported(const uint32_t polynomial)
10+
bool hal_crc_is_supported(const crc_mbed_config_t* config)
1111
{
12-
switch (polynomial)
13-
{
14-
case POLY_16BIT_CCITT:
15-
case POLY_16BIT_IBM:
16-
case POLY_32BIT_ANSI:
17-
return true;
18-
case POLY_8BIT_CCITT:
19-
case POLY_7BIT_SD:
20-
return false;
21-
default:
22-
return false;
23-
}
24-
}
25-
26-
void hal_crc_compute_partial_start(const uint32_t polynomial)
27-
{
28-
crc_config_t config;
29-
30-
switch (polynomial)
31-
{
32-
case POLY_32BIT_ANSI:
33-
{
34-
width = kCrcBits32;
12+
if (config == NULL)
13+
return false;
3514

36-
config.polynomial = polynomial;
37-
config.seed = 0xFFFFFFFFU;
38-
config.reflectIn = true;
39-
config.reflectOut = true;
40-
config.complementChecksum = true;
41-
config.crcBits = width;
42-
config.crcResult = kCrcFinalChecksum;
15+
if ((config->polynomial & 0x80008000) == 0)
16+
return false;
4317

44-
break;
45-
}
46-
case POLY_16BIT_IBM:
47-
{
48-
width = kCrcBits16;
49-
50-
config.polynomial = polynomial;
51-
config.seed = 0;
52-
config.reflectIn = true;
53-
config.reflectOut = true;
54-
config.complementChecksum = false;
55-
config.crcBits = width;
56-
config.crcResult = kCrcFinalChecksum;
57-
58-
break;
59-
}
60-
case POLY_16BIT_CCITT:
61-
{
62-
width = kCrcBits16;
18+
return true;
19+
}
6320

64-
config.polynomial = polynomial;
65-
config.seed = 0xFFFFFFFFU;
66-
config.reflectIn = false;
67-
config.reflectOut = false;
68-
config.complementChecksum = false;
69-
config.crcBits = width;
70-
config.crcResult = kCrcFinalChecksum;
21+
void hal_crc_compute_partial_start(const crc_mbed_config_t* config)
22+
{
23+
if (config == NULL)
24+
return;
7125

72-
break;
73-
}
74-
default:
75-
MBED_ASSERT("Configuring Mbed CRC with unsupported polynomial");
26+
width = ((config->polynomial & 0xFFFF0000U) != 0) ? kCrcBits32 : kCrcBits16;
7627

77-
return;
78-
}
28+
crc_config_t platform_config;
29+
platform_config.polynomial = config->polynomial;
30+
platform_config.seed = config->initial_xor;
31+
platform_config.reflectIn = config->reflect_in;
32+
platform_config.reflectOut = config->reflect_out;
33+
platform_config.complementChecksum = true;
34+
platform_config.crcBits = width;
35+
platform_config.crcResult = kCrcFinalChecksum;
7936

80-
CRC_Init(CRC0, &config);
37+
CRC_Init(CRC0, &platform_config);
8138
}
8239

8340
void hal_crc_compute_partial(const uint8_t *data, const size_t size)

0 commit comments

Comments
 (0)