1 | // CRCcalculator.cpp : Defines the entry point for the console application.
|
2 | //
|
3 |
|
4 | #include "stdafx.h"
|
5 | #include <stdio.h>
|
6 |
|
7 | #define CODEWORD 0x10210000
|
8 |
|
9 | int main(int argc, char* argv[])
|
10 | {
|
11 | unsigned int iBit, i;
|
12 | FILE *file;
|
13 | unsigned short fileData[8192];
|
14 | unsigned int uiFileCRC=0xBEEF0000;
|
15 | size_t size;
|
16 | unsigned int totalSize=0;
|
17 |
|
18 | if (argc < 3)
|
19 | {
|
20 | argv[1] = "C:/Users/chrabru/Desktop/DSPcode.bin";
|
21 | printf("Calculating CRC of file %s\n\r", argv[1]);
|
22 |
|
23 | file = fopen(argv[1], "rb");
|
24 | if (file == NULL) {
|
25 | printf("Could not open file\n\r");
|
26 | return -1;
|
27 | }
|
28 | //printf ("File: 0x%x\n\r", file);
|
29 | while (size = fread(fileData, sizeof(unsigned char), 2048, file)) {
|
30 | totalSize += size;
|
31 | //printf("Read %d bytes\n\r", size);
|
32 | if (size & 0x01) {
|
33 | printf("Add one byte because odd file length\n\r");
|
34 | fileData[size] = 0xFF;
|
35 | size++;
|
36 | }
|
37 | for (i = 0; i < size/2; i++) {
|
38 | uiFileCRC |= (unsigned short)fileData[i];
|
39 | //printf("A = %d\n\r", fileData[i]<<8 | fileData[i+1]);
|
40 |
|
41 | for (iBit = 0; iBit < 16; iBit++) { //Do 16 times
|
42 | if (uiFileCRC & 0x80000000) { //Check value of MSBit
|
43 | uiFileCRC = (uiFileCRC <<1 )^CODEWORD; //if 1 XOR MSByte with CODEWORD after shift
|
44 | }
|
45 | else {
|
46 | uiFileCRC = uiFileCRC << 1; //if 0 only shift
|
47 | }
|
48 | }// end for iBit
|
49 | }//end for i
|
50 | }
|
51 | printf("File length %d (0x%x) bytes\n\r", totalSize, totalSize);
|
52 | printf("CRC: 0x%4x\n", uiFileCRC>>16);
|
53 | printf("Test %d", uiFileCRC);
|
54 |
|
55 | return (uiFileCRC>>16);
|
56 | }
|