From f51a2109b1c1a3b4a712b93c5a50d0aa2553fa6f Mon Sep 17 00:00:00 2001
From: Zaltora <lilian.marazano@gmail.com>
Date: Tue, 21 Feb 2017 20:44:03 +0100
Subject: [PATCH] CRC library (#339)

* Crc initial example
* Update example and fix submodule
---
 .gitmodules                             |   3 +
 examples/crc_example/Makefile           |   3 +
 examples/crc_example/crc_config_perso.h |  15 +++
 examples/crc_example/crc_main.c         | 142 ++++++++++++++++++++++++
 extras/crc_generic/component.mk         |   9 ++
 extras/crc_generic/crc_lib              |   1 +
 6 files changed, 173 insertions(+)
 create mode 100644 examples/crc_example/Makefile
 create mode 100644 examples/crc_example/crc_config_perso.h
 create mode 100644 examples/crc_example/crc_main.c
 create mode 100644 extras/crc_generic/component.mk
 create mode 160000 extras/crc_generic/crc_lib

diff --git a/.gitmodules b/.gitmodules
index 4e13af8..19b3794 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -25,3 +25,6 @@
 [submodule "extras/http-parser/http-parser"]
 	path = extras/http-parser/http-parser
 	url = https://github.com/nodejs/http-parser
+[submodule "extras/crc_generic/crc_lib"]
+	path = extras/crc_generic/crc_lib
+	url = https://github.com/Zaltora/crc_generic_lib.git
diff --git a/examples/crc_example/Makefile b/examples/crc_example/Makefile
new file mode 100644
index 0000000..4df8190
--- /dev/null
+++ b/examples/crc_example/Makefile
@@ -0,0 +1,3 @@
+PROGRAM=crc_example
+EXTRA_COMPONENTS = extras/crc_generic 
+include ../../common.mk
diff --git a/examples/crc_example/crc_config_perso.h b/examples/crc_example/crc_config_perso.h
new file mode 100644
index 0000000..4f4b97d
--- /dev/null
+++ b/examples/crc_example/crc_config_perso.h
@@ -0,0 +1,15 @@
+/*
+ * perso_config.h
+ *
+ *  Created on: 11 févr. 2017
+ *      Author: lilian
+ */
+
+#include "espressif/esp_common.h"
+#include "FreeRTOS.h"
+
+#define CRC_DEBUG 0
+#define CRC_4BYTE_SUPPORT 0
+/* Use the defaults for everything else */
+#include_next "crc_config.h"
+
diff --git a/examples/crc_example/crc_main.c b/examples/crc_example/crc_main.c
new file mode 100644
index 0000000..ae42e31
--- /dev/null
+++ b/examples/crc_example/crc_main.c
@@ -0,0 +1,142 @@
+/*
+ * Example code to test crc and speed
+ */
+/////////////////////////////////Lib
+#include "espressif/esp_common.h"
+#include "esp/uart.h"
+#include "FreeRTOS.h"
+#include "task.h"
+#include "esp8266.h"
+#include <esp/hwrand.h>
+
+//extras
+#include "crc_config_perso.h"
+
+#define NUMBER_COMPUTE_TEST 1000
+
+unsigned char check_data[] = { "123456789" };
+uint8_t tab_data[512];  //
+
+void crc_8bit(void *pvParameters) {
+
+    config_crc_8 customcrc ; // my crc object
+    uint8_t tabsrc[256]; // my crc look-up table
+
+    //init crc parameters  (MAXIM parameters)
+    crc_8_generic_init(&customcrc,0x31, 8, 0x00, 0x00, 1, 1, 1);
+
+    // Use table algorithm / generate table
+    crc_8_generic_select_algo(&customcrc, tabsrc, CRC_TABLE, 0);
+
+
+    //show setting of crc
+    printf("\nCRC library v1.0 written on 11/02/2017 by Zaltora\n");
+    printf("-------------------------------------------------\n");
+    printf("\n");
+    printf("Parameters:\n");
+    printf("\n");
+    printf(" polynom     :  0x%02X\n", customcrc.polynom);
+    printf(" order       :  %d\n", customcrc.order);
+    printf(" crcinit     :  0x%02X direct, 0x%x nondirect\n", customcrc.private.crcinit_direct, customcrc.private.crcinit_nondirect);
+    printf(" crcxor      :  0x%02X\n", customcrc.crcxor);
+    printf(" refin       :  %d\n", customcrc.refin);
+    printf(" refout      :  %d\n", customcrc.refout);
+    printf("\n");
+    printf("check_data   :  '%s' (%d bytes)\n", check_data, sizeof(check_data));
+    printf("\n");
+
+
+    //show table
+    printf("Lookup table generated:\n");
+    printf("\n");
+    printf("tabsrc[256]  = {");
+    for (uint16_t i = 0 ; i < 256 ; i++)
+    {
+        if(!(i%8)) printf("\n");
+        printf("0x%02X, ",tabsrc[i]);
+    }
+    printf("\n};\n\n");
+
+    printf("Check value results with all algorithms:\n");
+    printf("\n");
+
+    //try different crc algorithm
+    crc_8_generic_select_algo(&customcrc, (crc_8*)tabsrc, CRC_TABLE, 0);
+    printf("CRC_TABLE\t\t: 0x%02X\n", crc_8_generic_compute(&customcrc, check_data, sizeof(check_data)));
+    crc_8_generic_select_algo(&customcrc, (crc_8*)tabsrc, CRC_TABLE_FAST, 0);
+    printf("CRC_TABLE_FAST\t\t: 0x%02X\n", crc_8_generic_compute(&customcrc, check_data, sizeof(check_data)));
+    crc_8_generic_select_algo(&customcrc, NULL, CRC_BIT_TO_BIT, 0);
+    printf("CRC_BIT_TO_BIT\t\t: 0x%02X\n", crc_8_generic_compute(&customcrc, check_data, sizeof(check_data)));
+    crc_8_generic_select_algo(&customcrc, NULL, CRC_BIT_TO_BIT_FAST, 0);
+    printf("CRC_BIT_TO_BIT_FAST\t: 0x%02X\n", crc_8_generic_compute(&customcrc, check_data, sizeof(check_data)));
+    crc_8_generic_select_algo(&customcrc, (crc_8*)crc_8_tab_MAXIM, CRC_TABLE_FAST, 1);
+    printf("CRC_TABLE_BUILTIN\t: 0x%02X\n\n", crc_8_generic_compute(&customcrc, check_data, sizeof(check_data)));
+
+    printf("Test speed algorithms with random data:\n");
+    printf("\n");
+
+    //data to process
+    printf("%u bytes of DATA:",sizeof(tab_data));
+    for (uint16_t i = 0 ; i < sizeof(tab_data) ; i++)
+    {
+        tab_data[i] = (uint8_t)hwrand();
+        if(!(i%32)) printf("\n");
+        printf("%02X",tab_data[i]);
+    }
+    printf("\n\n");
+
+    const uint32_t cst = NUMBER_COMPUTE_TEST ;
+    char algo_txt[30] ;
+    uint32_t time = 0;
+    uint8_t select = 0;
+    uint8_t result = 0;
+    for(select = 0; select < 5 ; select++)
+    {
+        switch(select){
+        case 0:
+            crc_8_generic_select_algo(&customcrc, NULL, CRC_BIT_TO_BIT, 0);
+            sprintf(algo_txt,"CRC_BIT_TO_BIT");
+            break;
+        case 1:
+            crc_8_generic_select_algo(&customcrc, NULL, CRC_BIT_TO_BIT_FAST, 0);
+            sprintf(algo_txt,"CRC_BIT_TO_BIT_FAST");
+            break;
+        case 2:
+            crc_8_generic_select_algo(&customcrc, tabsrc, CRC_TABLE, 0);
+            sprintf(algo_txt,"CRC_TABLE");
+            break;
+        case 3:
+            crc_8_generic_select_algo(&customcrc,  tabsrc, CRC_TABLE_FAST, 0);
+            sprintf(algo_txt,"CRC_TABLE_FAST");
+            break;
+        case 4:
+            crc_8_generic_select_algo(&customcrc, (crc_8*)crc_8_tab_MAXIM, CRC_TABLE_FAST, 1);
+            sprintf(algo_txt,"CRC_TABLE_FAST_BUILTIN");
+            break;
+        }
+        vTaskDelay(1000 / portTICK_PERIOD_MS) ;
+        printf("test speed algorithm %s \n",algo_txt);
+        time = sdk_system_get_time();
+        for (uint32_t i = 0 ; i < cst ; i++)
+        {
+            result = crc_8_generic_compute(&customcrc, tab_data, sizeof(tab_data));
+        }
+        time = sdk_system_get_time()-time ;
+        printf("Speed algorithm: %.3f us\n",(float)time/(float)cst);
+        printf("Result algorithm: %02X\n\n",result);
+    }
+
+    while (1) {
+        vTaskDelay(10000 / portTICK_PERIOD_MS) ;
+    }
+}
+
+void  user_init(void)
+{
+	sdk_system_update_cpu_freq(160);
+	uart_set_baud(0, 115200);
+	printf("SDK version:%s\n", sdk_system_get_sdk_version());
+	printf("Start\n\n");
+	vTaskDelay(2000 / portTICK_PERIOD_MS) ;
+	xTaskCreate(crc_8bit, "crc_8bit", 512, NULL, 2, NULL);
+}
diff --git a/extras/crc_generic/component.mk b/extras/crc_generic/component.mk
new file mode 100644
index 0000000..59efb53
--- /dev/null
+++ b/extras/crc_generic/component.mk
@@ -0,0 +1,9 @@
+# Component makefile for extras/crc_generic
+
+# expected anyone using bmp driver includes it as 'crc_generic/crc_generic.h'
+INC_DIRS += $(crc_generic_ROOT)crc_lib/
+
+# args for passing into compile rule generation
+crc_generic_SRC_DIR =  $(crc_generic_ROOT)crc_lib/
+
+$(eval $(call component_compile_rules,crc_generic))
diff --git a/extras/crc_generic/crc_lib b/extras/crc_generic/crc_lib
new file mode 160000
index 0000000..a3b902a
--- /dev/null
+++ b/extras/crc_generic/crc_lib
@@ -0,0 +1 @@
+Subproject commit a3b902a53216b1378e5cc856ed1dfc3ffd38bc09