Date post: | 03-Jul-2015 |
Category: |
Engineering |
Upload: | ajay-singh |
View: | 188 times |
Download: | 5 times |
Cyclic Redundancy Check (CRC) implementation in Software
using C
Physics Department Indian Institute of Technology Delhi
Ajay Singh (2014JOP2558)Manoj Falaswal (2014JOP2489)
Contents:-
1. Why Error Detection And Correction?2. Type Of Errors3. Error Detection Techniques
Parity Check Two Dimensional Parity Check Checksum Cyclic Redundancy Check
4. CRC Implementation Modulo-2 Arithmetic Digital circuit for polynomial division Implementation and Code for CRC in C Performance of CRC
Why error detection and correction
• Because of attenuation, distortion, noise andinterference, error during transmission areinevitable, leading to corruption transmittedbits.
• Longer the frame size and higher theprobability of single bit error ,lower is theprobability receiving a frame without error.
Types of error
• Single-bit error: only one bit get corruptedCommon in parallel transmission.
• Burst errormore than one bit get corruptedVery common in serial transmission of dataOccurs when the duration of noise is larger thanthe duration of one bit.
Error detection techniques
• Use of redundancy: additional bits are added to facilitate detection and correction of error.
• Popular techniques:
Parity Check
Two Dimensional Parity Check
Checksum
Cyclic Redundancy Check
• CRC is one of the most powerful and commonlyused error detecting technique.
Cyclic redundancy check (CRC)
• Basic approach: a m-bit block of bit sequence,the sender generate an n-bit sequence, knownas frame check sequence (FCS) , so that theresulting frame, consisting of m+n bits, isexactly divisible by same pre determinednumber.
• The receiver divides the incoming frame bythat number and, if there is no remainder,assumes there are no error.
Cyclic redundancy check (CRC)
Data 000….0
Divisor
CRC
Data CRC
m n
(n+1) bits
N-bits
Divisor
(n+1) bits
CRCReminder
Data 000….0
m n
zeroRejected Accept
YN
Sender Receiver
Data: 1010Divisor: 1011
1001 Quotient
1 0 1 0 0 0 01 0 1 1
0 0 1 00 0 0 0
0 1 0 0 0 0 0 0
1 0 0 01 0 1 1
0 1 1 Reminder
1 0 1 1
Modulo-2 Arithmetic
Data to be sent:1 0 1 0 0 1 1Data CRC
Received Data: 1010011Divisor: 1011
1001 Quotient1 0 1 0 0 1 11 0 1 1
0 0 1 00 0 0 0
0 1 0 1 0 0 0 0
1 0 1 11 0 1 1
0 0 0 Reminder
1 0 1 1
Modulo-2 Arithmetic
No error
Polynomials
• All the value can be expressed as polynomial of a dummy variable X. P=11001 => X4+X3+1
• CRC process can be expressed as:
Xn*M(X)/P(X)=Q(X)+R(X)/P(X)
• Commonly used divisor polynomial:
(1) CRC-16 = X16+X15+X2+1
(2) CRC-CCITT = X16+X12+X5+1
Digital circuit for polynomial division
• The CRC process can be vary easilyimplemented in hardware using LinearFeedback shift register (LFSR).
• The LFSR divides a message polynomial bysuitably chosen divisor polynomial; thereminder constitutes the FCS.
Implementation
C2 C1 C0 ++
clock
Input 1 0 1 0 0 0 0
Initial C2 C1 C0 C2(+)C0 C2(+)input input0 0 0 0 1 1
Step 1 0 0 1 1 0 0
Step 2 0 1 0 0 1 1
Step 3 1 0 1 0 1 0
Step 4 0 0 1 1 0 0
Step 5 0 1 0 0 0 0
Step 6 1 0 0 1 1 0
Step 7 0 1 1 1 0 -
Data to be sent:1010011
Code for CRC in C
#include <stdio.h>#include <string.h>void main(){int i,j,keylen,msglen;char input[100], key[30],temp[30],quot[100],rem[30],key1[30];clrscr();printf("Enter Data: ");gets(input);printf("Enter Key: ");gets(key);keylen=strlen(key);msglen=strlen(input);strcpy(key1,key);for(i=0;i<keylen-1;i++){input[msglen+i]='0';}
1
for(i=0;i<keylen;i++)temp[i]=input[i];for(i=0;i<msglen;i++){quot[i]=temp[0];if(quot[i]=='0')for(j=0;j<keylen;j++)key[j]='0';elsefor(j=0;j<keylen;j++)key[j]=key1[j];for(j=keylen-1;j>0;j--){if(temp[j]==key[j])rem[j-1]='0';elserem[j-1]='1';}
rem[keylen-1]=input[i+keylen];strcpy(temp,rem);}strcpy(rem,temp);printf("\nQuotient is ");for(i=0;i<msglen;i++)printf("%c",quot[i]);printf("\nRemainder is ");for(i=0;i<keylen-1;i++)printf("%c",rem[i]);printf("\nFinal data is: ");for(i=0;i<msglen;i++)printf("%c",input[i]);for(i=0;i<keylen-1;i++)printf("%c",rem[i]);}
#include<stdio.h>#include<string.h>#define N strlen(g)
char t[28],cs[28],g[]="1011";int a,e,c;
void xor(){for(c = 1;c < N; c++)cs[c] = (( cs[c] == g[c])?'0':'1');
}
void crc(){for(e=0;e<N;e++)
cs[e]=t[e];do{
if(cs[0]=='1')xor();
for(c=0;c<N-1;c++)cs[c]=cs[c+1];
cs[c]=t[e++];}
while(e<=a+N-1);}int main()
{printf("\nEnter data : ");scanf("%s",t);printf("\n-------------------------------------
---");printf("\nGeneratng polynomial :
%s",g);a=strlen(t);for(e=a;e<a+N-1;e++)
t[e]='0';printf("\n-------------------------------------
---");printf("\nModified data is : %s",t);
2
printf("\n----------------------------------------");
crc();printf("\nCRC is : %s",cs);for(e=a;e<a+N-1;e++)
t[e]=cs[e-a];printf("\n--------------------------------
--------");printf("\nFinal codeword is :
%s",t);printf("\n--------------------------------
--------");printf("\nTest error detection
0(yes) 1(no)? : ");scanf("%d",&e);if(e==0){
do{printf("\nEnter the position
where error is to be inserted : ");
scanf("%d",&e);}while(e==0 || e>a+N-1);t[e-1]=(t[e-1]=='0')?'1':'0';
printf("\n----------------------------------------");
printf("\nErroneous data : %s\n",t);
}crc();for(e=0;(e<N-1) &&
(cs[e]!='1');e++);if(e<N-1)
printf("\nErrordetected\n\n");
elseprintf("\nNo error
detected\n\n");printf("\n---------------------------
-------------\n");return 0;
}
Performance of CRC
• CRC can detect all single bit errors
• CRC can detect all double-bit error(three 1’s)
• CRC can detect any odd number of error (X+1)
• CRC can detect all burst error of less than the
degree of the polynomial.
• CRC detects most of the larger burst errorswith a high probability.
• For example CRC-12 detects 99.97% of error
Reference Links:
http://en.wikipedia.org/wiki/Computation_of_cyclic_redundancy_checkshttp://www.barrgroup.com/Embedded-Systems/How-To/CRC-Calculation-C-Codehttp://stackoverflow.com/questions/18974220/how-to-implement-crc-using-c-languagehttp://www.ccodechamp.com/c-program-to-implement-cyclic-redundancy-check-crc/http://getprogramcode.com/2013/03/c-program-to-implement-crc-cyclic-redundancy-code/http://www.barrgroup.com/Embedded-Systems/How-To/CRC-Calculation-C-Code
!