16
16
#ifndef MBED_CRC_API_H
17
17
#define MBED_CRC_API_H
18
18
19
- #include < stdint.h>
20
19
#include " drivers/TableCRC.h"
20
+ #include " hal/crc_api.h"
21
21
#include " platform/mbed_assert.h"
22
22
23
23
/* This is invalid warning from the compiler for below section of code
@@ -38,19 +38,6 @@ namespace mbed {
38
38
/* * \addtogroup drivers */
39
39
/* * @{*/
40
40
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
-
54
41
/* * CRC object provides CRC generation through hardware/software
55
42
*
56
43
* ROM polynomial tables for supported polynomials (:: crc_polynomial_t) will be used for
@@ -106,6 +93,9 @@ typedef enum crc_polynomial {
106
93
template <uint32_t polynomial=POLY_32BIT_ANSI, uint8_t width=32 >
107
94
class MbedCRC
108
95
{
96
+ public:
97
+ enum crc_mode { HARDWARE = 0 , TABLE, BITWISE };
98
+
109
99
public:
110
100
typedef uint64_t crc_data_size_t ;
111
101
@@ -178,13 +168,21 @@ class MbedCRC
178
168
*/
179
169
int32_t compute_partial (void *buffer, crc_data_size_t size, uint32_t *crc)
180
170
{
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);
187
183
}
184
+
185
+ return -1 ;
188
186
}
189
187
190
188
/* * Compute partial start, indicate start of partial computation
@@ -200,6 +198,13 @@ class MbedCRC
200
198
int32_t compute_partial_start (uint32_t *crc)
201
199
{
202
200
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
+
203
208
*crc = _initial_value;
204
209
return 0 ;
205
210
}
@@ -215,6 +220,16 @@ class MbedCRC
215
220
int32_t compute_partial_stop (uint32_t *crc)
216
221
{
217
222
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
+
218
233
uint32_t p_crc = *crc;
219
234
if ((width < 8 ) && (NULL == _crc_table)) {
220
235
p_crc = (uint32_t )(p_crc << (8 - width));
@@ -247,6 +262,7 @@ class MbedCRC
247
262
bool _reflect_data;
248
263
bool _reflect_remainder;
249
264
uint32_t *_crc_table;
265
+ crc_mode mode_;
250
266
251
267
/* * Get the current CRC data size
252
268
*
@@ -408,9 +424,17 @@ class MbedCRC
408
424
/* * Constructor init called from all specialized cases of constructor
409
425
* Note: All construtor common code should be in this function.
410
426
*/
411
- void mbed_crc_ctor (void ) const
427
+ void mbed_crc_ctor (void )
412
428
{
413
429
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
414
438
}
415
439
};
416
440
0 commit comments