Skip to content

Commit e0d98bf

Browse files
author
Steven Cartmell
committed
Adapt MbedCRC class to support Hardware CRC
- Move CRC polynomial enum into HAL layer, so it's accessible from platform implementations - Add enum to CRC class to indicate which mode the CRC class should use: HARDWARE, TABLE, or BITWISE - Add calls to HAL Hardware CRC API to each of the compute functions when the class is in HARDWARE mode. - Add missing constructor call to template constructor, and remove const from delegating constructor.
1 parent bad3aa0 commit e0d98bf

File tree

2 files changed

+58
-21
lines changed

2 files changed

+58
-21
lines changed

drivers/MbedCRC.h

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
#ifndef MBED_CRC_API_H
1717
#define MBED_CRC_API_H
1818

19-
#include <stdint.h>
2019
#include "drivers/TableCRC.h"
20+
#include "hal/crc_api.h"
2121
#include "platform/mbed_assert.h"
2222

2323
/* This is invalid warning from the compiler for below section of code
@@ -38,19 +38,6 @@ namespace mbed {
3838
/** \addtogroup drivers */
3939
/** @{*/
4040

41-
/** CRC Polynomial value
42-
*
43-
* Different polynomial values supported
44-
*/
45-
typedef enum crc_polynomial {
46-
POLY_OTHER = 0,
47-
POLY_8BIT_CCITT = 0x07, // x8+x2+x+1
48-
POLY_7BIT_SD = 0x9, // x7+x3+1;
49-
POLY_16BIT_CCITT = 0x1021, // x16+x12+x5+1
50-
POLY_16BIT_IBM = 0x8005, // x16+x15+x2+1
51-
POLY_32BIT_ANSI = 0x04C11DB7, // x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x+1
52-
} crc_polynomial_t;
53-
5441
/** CRC object provides CRC generation through hardware/software
5542
*
5643
* ROM polynomial tables for supported polynomials (:: crc_polynomial_t) will be used for
@@ -106,6 +93,9 @@ typedef enum crc_polynomial {
10693
template <uint32_t polynomial=POLY_32BIT_ANSI, uint8_t width=32>
10794
class MbedCRC
10895
{
96+
public:
97+
enum crc_mode { HARDWARE = 0, TABLE, BITWISE };
98+
10999
public:
110100
typedef uint64_t crc_data_size_t;
111101

@@ -178,13 +168,21 @@ class MbedCRC
178168
*/
179169
int32_t compute_partial(void *buffer, crc_data_size_t size, uint32_t *crc)
180170
{
181-
if (NULL == _crc_table) {
182-
// Compute bitwise CRC
183-
return bitwise_compute_partial(buffer, size, crc);
184-
} else {
185-
// Table CRC
186-
return table_compute_partial(buffer, size, crc);
171+
switch (mode_)
172+
{
173+
case HARDWARE:
174+
#ifdef DEVICE_CRC
175+
hal_crc_compute_partial((uint8_t *)buffer, size);
176+
#endif // DEVICE_CRC
177+
*crc = 0;
178+
return 0;
179+
case TABLE:
180+
return table_compute_partial(buffer, size, crc);
181+
case BITWISE:
182+
return bitwise_compute_partial(buffer, size, crc);
187183
}
184+
185+
return -1;
188186
}
189187

190188
/** Compute partial start, indicate start of partial computation
@@ -200,6 +198,13 @@ class MbedCRC
200198
int32_t compute_partial_start(uint32_t *crc)
201199
{
202200
MBED_ASSERT(crc != NULL);
201+
202+
#ifdef DEVICE_CRC
203+
if (mode_ == HARDWARE) {
204+
hal_crc_compute_partial_start(polynomial);
205+
}
206+
#endif // DEVICE_CRC
207+
203208
*crc = _initial_value;
204209
return 0;
205210
}
@@ -215,6 +220,16 @@ class MbedCRC
215220
int32_t compute_partial_stop(uint32_t *crc)
216221
{
217222
MBED_ASSERT(crc != NULL);
223+
224+
if (mode_ == HARDWARE) {
225+
#ifdef DEVICE_CRC
226+
*crc = hal_crc_get_result();
227+
return 0;
228+
#else
229+
return -1;
230+
#endif
231+
}
232+
218233
uint32_t p_crc = *crc;
219234
if ((width < 8) && (NULL == _crc_table)) {
220235
p_crc = (uint32_t)(p_crc << (8 - width));
@@ -247,6 +262,7 @@ class MbedCRC
247262
bool _reflect_data;
248263
bool _reflect_remainder;
249264
uint32_t *_crc_table;
265+
crc_mode mode_;
250266

251267
/** Get the current CRC data size
252268
*
@@ -408,9 +424,17 @@ class MbedCRC
408424
/** Constructor init called from all specialized cases of constructor
409425
* Note: All construtor common code should be in this function.
410426
*/
411-
void mbed_crc_ctor(void) const
427+
void mbed_crc_ctor(void)
412428
{
413429
MBED_STATIC_ASSERT(width <= 32, "Max 32-bit CRC supported");
430+
431+
mode_ = (_crc_table == NULL) ? TABLE : BITWISE;
432+
433+
#ifdef DEVICE_CRC
434+
if (hal_crc_is_supported(polynomial)) {
435+
mode_ = HARDWARE;
436+
}
437+
#endif
414438
}
415439
};
416440

hal/crc_api.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@
55
#include <stddef.h>
66
#include <stdint.h>
77

8+
/** CRC Polynomial value
9+
*
10+
* Different polynomial values supported
11+
*/
12+
typedef enum crc_polynomial {
13+
POLY_OTHER = 0,
14+
POLY_8BIT_CCITT = 0x07, // x8+x2+x+1
15+
POLY_7BIT_SD = 0x9, // x7+x3+1;
16+
POLY_16BIT_CCITT = 0x1021, // x16+x12+x5+1
17+
POLY_16BIT_IBM = 0x8005, // x16+x15+x2+1
18+
POLY_32BIT_ANSI = 0x04C11DB7, // x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x+1
19+
} crc_polynomial_t;
20+
821
#ifdef DEVICE_CRC
922

1023
#ifdef __cplusplus

0 commit comments

Comments
 (0)