ISF  2.2 rev 5
Intelligent Sensing Framework for Kinetis with Processor Expert
crc.c
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2015, Freescale Semiconductor, Inc.
4  *
5 */
6 
7 /*!
8  * @file crc.h
9  * @brief CRC source file that implements the CCITT CRC16 standard.
10  *
11  */
12 
13 #include "isf.h"
14 #include "crc.h"
15 
16 
17 
18 uint16 ccitt_crc16_cal(uint32 anumBytes, uint8 *apBuf)
19 {
20 
21  uint16 crc16 = 0xffff;
22  uint8 *p8 = (uint8*)apBuf;
23  uint8 bit;
24  uint16 xor_flag;
25 
26  while(anumBytes--)
27  {
28 
29  uint8 v;
30 
31  // Align test bit with leftmost bit of the message byte.
32  v = 0x80;
33 
34 
35  bit = 0;
36  do
37  {
38  if (crc16 & 0x8000)
39  {
40  xor_flag= 1;
41  }
42  else
43  {
44  xor_flag= 0;
45  }
46  crc16 = crc16 << 1;
47 
48 
49  if (*p8 & v)
50  {
51  // If not zero, then append the next bit of the message to the
52  // end of the CRC. The zero bit placed there by the shift above
53  // need not be changed if the next bit of the message is zero.
54  crc16 = crc16 + 1;
55  }
56 
57  if (xor_flag)
58  {
59  crc16 = crc16 ^ POLY_CRC16_GENERATOR;
60  }
61 
62  // Align test bit with next bit of the message byte.
63  v = v >> 1;
64 
65  } while(++bit < 8);
66 
67  p8++;
68  }
69 
70 
71  bit = 0;
72  do
73  {
74  if (crc16 & 0x8000)
75  {
76  xor_flag= 1;
77  }
78  else
79  {
80  xor_flag= 0;
81  }
82  crc16 = crc16 << 1;
83 
84  if (xor_flag)
85  {
86  crc16 = crc16 ^ POLY_CRC16_GENERATOR;
87  }
88  } while(++bit < 16);
89 
90 
91 
92  return crc16;
93 }
94 
unsigned char uint8
Definition: isf_types.h:76
CRC header file.
#define POLY_CRC16_GENERATOR
The standard polynomial value used for CCITT CRC 16-bit calculation.
Definition: crc.h:19
uint16 ccitt_crc16_cal(uint32 anumBytes, uint8 *apBuf)
This crc function contains the standard CCITT CRC 16-bit implementation.
Definition: crc.c:18
Main ISF header file. Contains code common to all ISF components.
unsigned short int uint16
Definition: isf_types.h:77
unsigned long int uint32
Definition: isf_types.h:78