1. Martin Scharrer
  2. CRC

Commits

Martin Scharrer  committed 04ec44d

Added Xmodem version.

  • Participants
  • Parent commits 6215be5
  • Branches default

Comments (0)

Files changed (4)

File Makefile

View file
  • Ignore whitespace
 CC=gcc
 CFLAGS=-O3 -Wall -finline-functions-called-once
 
-TESTS=test_crc16_ccitt_alt
+TESTS=TEST_crc16_ccitt_alt TEST_crc16_ccitt
+
+.PHONY: ${TESTS} clean all test
 
 test: ${TESTS}
 
-${TESTS}: test_%: test_%.o %.o
-	${CC} ${CFLAGS} -o $@ $^
-	./$@
+${TESTS}: TEST_%: test_%
+	./test_$*
+
+test_%: test_%.o %.o
 
 all: test
 
+clean: 
+	${RM} *.o $(subst TESTS,TEST,test)
 
-test_%: %.o test_%.o
-
-clean: 
-	${RM} *.o crc_test
-

File crc16_ccitt.c

View file
  • Ignore whitespace
+#include "crc16_ccitt_alt.h"
+
+#define CRC16_CCITT_INIT      0x0000
+#define CRC16_CCITT_POLY      0x1021
+
+void crc16_ccitt_init(uint16_t* crc)
+{
+    *crc = CRC16_CCITT_INIT;
+}
+
+void crc16_ccitt_finish(uint16_t* crc)
+{
+
+}
+
+
+// Most Significant Bit first
+void crc16_ccitt_update(uint16_t* crc, uint8_t* data, size_t length)
+{
+    int i;
+    while (length-- > 0)
+    {
+        *crc ^= (uint16_t)*data++ << 8;
+        for (i=0; i<8; i++)
+        {
+            if ( (*crc & 0x8000) )
+            {
+                *crc = (*crc << 1) ^ CRC16_CCITT_POLY;
+            }
+            else
+            {
+                *crc <<= 1;
+            }
+        }
+    }
+}
+
+
+uint16_t crc16_ccitt(uint8_t* data, size_t length)
+{
+    uint16_t crc;
+    crc16_ccitt_init(&crc);
+    crc16_ccitt_update(&crc, data, length);
+    crc16_ccitt_finish(&crc);
+    return crc;
+}
+

File crc16_ccitt.h

View file
  • Ignore whitespace
+#include <string.h>
+#include <stdint.h>
+
+extern void crc16_ccitt_init(uint16_t* crc);
+extern void crc16_ccitt_finish(uint16_t* crc);
+extern void crc16_ccitt_update(uint16_t* crc, uint8_t* data, size_t length);
+extern uint16_t crc16_ccitt(uint8_t* data, size_t length);
+

File test_crc16_ccitt.c

View file
  • Ignore whitespace
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#include "crc16_ccitt.h"
+
+typedef struct
+{
+    uint8_t* pdata;
+    //size_t   length;
+    uint16_t correct_crc;
+} testvector_t;
+
+#define B (uint8_t*)
+#define NUM_TESTVECTORS 3
+testvector_t testvectors [NUM_TESTVECTORS] = 
+{
+    {B"123456789", 0x31C3},
+    {B"test", 0x9B06},
+    {B"This is a CRC test", 0xDCD8},
+    //{NULL, 0x0000},
+};
+
+int main(int argc, char* argv[])
+{
+    //uint8_t  stdtext[10] = "123456789";
+    uint8_t* buffer;
+    size_t   length;
+    uint16_t crc;
+    int nerrors = 0;
+    int i;
+
+    if (argc > 1)
+    {
+        buffer = (uint8_t*)argv[1];
+        length = strlen(argv[1]);
+        crc = crc16_ccitt(buffer, length);
+        printf("CRC: %s => %04X\n", buffer, crc);
+        exit (0);
+    }
+
+    for (i=0; i<NUM_TESTVECTORS; i++)
+    {
+        buffer = testvectors[i].pdata;
+        length = strlen((char*)buffer);
+        crc = crc16_ccitt(buffer, length);
+        if (crc == testvectors[i].correct_crc)
+        {
+            printf("Correct CRC: '%s' => %04X\n", buffer, crc);
+        }
+        else
+        {
+            printf("Incorrect CRC: '%s' => %04X, correct: %04X\n", buffer, crc, testvectors[i].correct_crc);
+            nerrors++;
+        }
+    }
+    exit (nerrors ? 1 : 0);
+
+    return 0;
+}
+