The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Revision:
169:a7c7b631e539
Parent:
165:d1b4690b3f8b
Child:
170:e95d10626187
--- a/drivers/MbedCRC.h	Thu May 24 15:35:55 2018 +0100
+++ b/drivers/MbedCRC.h	Fri Jun 22 15:38:59 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
     }
 };