Michael Swan / fwdcrc16library

Files at this revision

API Documentation at this revision

Comitter:
Mike951
Date:
Sun Mar 17 20:12:29 2013 +0000
Commit message:
No changes

Changed in this revision

crc16.cpp Show annotated file Show diff for this revision Revisions of this file
crc16.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 99467adee231 crc16.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/crc16.cpp	Sun Mar 17 20:12:29 2013 +0000
@@ -0,0 +1,45 @@
+#include "crc16.h"
+
+/*
+CRC16 generation algorithm taken from J.G. Harston at
+http://mdfs.net/Info/Comp/Comms/CRC16.htm
+*/
+
+// Refer to crc16.h for overall function details
+
+#define poly 0x11021 // Define polynomial generating CRC tag
+
+int     crc16 (char* dataIn, int numberOfBytes) {
+    if (numberOfBytes > 16)     // Keep numberOfBytes under 16
+        return 0;
+    int i, j;
+    int crc = 0;                // Initialize CRC to zero
+    
+    for (j = numberOfBytes ; j > 0; j--) {  // Execute numberOfBytes times
+        crc = crc ^ (*dataIn++ << 8);   // Get byte and XOR with crc
+        for (i = 0; i < 8; i++) {
+            crc = crc << 1;             // Rotate
+            if (crc & 0x10000)          // If bit 15 is set
+                crc ^= poly;            // XOR with polynomial            
+        }
+    }
+    return crc;             // Return CRC tag       
+}
+
+void    crc16_attach (char* dataIn, int numberOfBytes) {
+    int crc = crc16(dataIn, numberOfBytes);             // Generate CRC tag from first numberOfBytes elements of DataIn
+    dataIn[numberOfBytes] = ((crc >> 8) & 0xFF);        // Store 8 MSbits as the next element in dataIn
+    dataIn[numberOfBytes + 1] = (crc & 0xFF);           // Store the 8 LSbits is the last element in dataIn
+}
+
+bool    crc16_match (char* dataIn, int numberOfBytes) {
+    int crcData = crc16(dataIn, numberOfBytes);         // Generate CRC tag from the first numberOfBytes elements of dataIn
+    int a,b;
+    a = dataIn[numberOfBytes];                          // Extract the last two 8 bit elements of dataIn as integers
+    b = dataIn[numberOfBytes + 1];
+    int crcExpect = (a << 8) + b;                       // Recombine into a single 16 bit element
+    if (crcData == crcExpect)                           // Compare the generated with the expected
+        return true;                                    // If they match, data is intact
+    else
+        return false;                                   // Otherwise, data is not intact
+}            
diff -r 000000000000 -r 99467adee231 crc16.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/crc16.h	Sun Mar 17 20:12:29 2013 +0000
@@ -0,0 +1,44 @@
+#ifndef CRC16_H_
+#define CRC16_H_
+
+/* General Layout Design taken from Erik van Wijk's crc8 library found at
+http://mbed.org/users/evwijk/code/crc8/
+*/
+ 
+/* Takes in a character array with a given number of bytes. Returns the
+16 bit CRC tag as a 32 bit integer. numberOfBytes exceeds 16, function returns 0.
+*/ 
+
+int     crc16 (char* dataIn, int numberOfBytes);
+
+/* Takes in a character array with at least (numberOfBytes + 2) entries. A 16-bit
+CRC tag is created from the first numberOfBytes elements of dataIn. The 16-bit tag is then
+broken into two 8 bit tags. The most significant is stored in dataIn at (numberOfBytes+1) 
+and the least significant is stored in dataIn at (numberOfBytes+2)
+
+*******************************************************************************
+Note: numberOfBytes indicates the number of data elements to generate the CRC.
+dataIn must be at least of length numberOfBytes + 2 for the two bytes of CRC
+*******************************************************************************
+*/
+
+void    crc16_attach (char* dataIn, int numberOfBytes);
+
+/* Takes in a character array of length (numberOfBytes + 2). A 16-bit CRC is created from the first
+numberOfBytes elements of dataIn. The elements at numberOfBytes + 1 and numberOfBytes + 2 are assumed
+to be a CRC tag created in the form described above and are reassembled into a single integer.
+
+The calculated CRC and the input CRC are compared
+
+If they match, funtion is true.
+If the two CRC tags to not match, function is false.
+
+*******************************************************************************
+Note: numberOfBytes indicates the number of data elements to generate the CRC.
+dataIn must be at least of length numberOfBytes + 2 for the two bytes of CRC
+*******************************************************************************
+*/
+bool    crc16_match (char* dataIn, int numberOfBytes);
+
+ 
+#endif
\ No newline at end of file