📄 c ams standard cyclic redundancy check - crc16 -status baseline.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0046)http://ams.cern.ch/AMS/Dataformats/node26.html -->
<!--Converted with LaTeX2HTML 96.1-g (July 19, 1996) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds --><HTML><HEAD><TITLE>C AMS Standard Cyclic Redundancy Check - CRC16 -Status: Baseline</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META
content="C AMS Standard Cyclic Redundancy Check - CRC16 -Status: Baseline "
name=description>
<META content=Dataformats name=keywords>
<META content=document name=resource-type>
<META content=global name=distribution><LINK
href="C AMS Standard Cyclic Redundancy Check - CRC16 -Status Baseline.files/Dataformats.css"
rel=STYLESHEET>
<META content="MSHTML 6.00.2800.1106" name=GENERATOR></HEAD>
<BODY lang=EN><A href="http://ams.cern.ch/AMS/Dataformats/node27.html"
name=tex2html438><IMG height=24 alt=next src="" width=37 align=bottom></A> <A
href="http://ams.cern.ch/AMS/Dataformats/Dataformats.html" name=tex2html436><IMG
height=24 alt=up src="" width=26 align=bottom></A> <A
href="http://ams.cern.ch/AMS/Dataformats/node25.html" name=tex2html430><IMG
height=24 alt=previous src="" width=63 align=bottom></A> <A
href="http://ams.cern.ch/AMS/Dataformats/node2.html" name=tex2html440><IMG
height=24 alt=contents src="" width=65 align=bottom></A> <BR><B>Next:</B> <A
href="http://ams.cern.ch/AMS/Dataformats/node27.html" name=tex2html439>About
this document </A><B>Up:</B> <A
href="http://ams.cern.ch/AMS/Dataformats/Dataformats.html" name=tex2html437>AMS
Data Formats</A> <B>Previous:</B> <A
href="http://ams.cern.ch/AMS/Dataformats/node25.html" name=tex2html431>B Time
Formats -Status: </A><BR>
<P>
<H1><A name=SECTION002600000000000000000>C AMS Standard Cyclic Redundancy Check
- CRC16 -<I>Status: Baseline</I> </A></H1>
<P><A name=ssCRC16> </A> In several places a Cyclic Redundancy Check (CRC)
is used for error detection in data transport and storage. Except where
indicated, we standardize this to be the CRC calculation described below, which
is based on 16 bit words passed as indicated and referred to as <B>CRC16</B>.
<P><PRE>/*
Coding for Error Detection in AMS Data Transport -- CRC16
Draft - early Sept 97, V. Koutsenko
Encoding/decoding procedure that is described
in Ref.1 Page D-2 will be used for error detection in AMS data.
Essentials:
"The encoding procedure accepts an (n-16)-bit data block and generates
a systematic binary (n,n-16) block code by appending a 16-bit Frame Check
Sequence (FCS) as the final 16 bits of the codeblock."
FCS(x) = [x**16 * M(x) + x**(n-16) * L(x)] modulo G(x)
where
M(x) is the (n-16)-bit data to be encoded expressed as a polynomial
with binary coefficients,
L(x) = x**15 + x**14 + ... + x**2 + x + 1 (all "1" polynomial),
G(x) = x**16 + x**12 + x**5 + 1 is the generator polynomial,
All addition operators are the modulo 2 additions (Exclusive OR).
The error detection syndrome S(x) will be zero if no error is detected
S(x) = [x**16 * C'(x) + x**n * L(x)] modulo G(x)
where
C'(x) is the received block in polynomial form.
Big Endian bits and bytes order.
References:
1. "Telemetry Concept and Rationale", CCSDS 100.0-G-1, Green Book, Consultative
Committee for Space Data Systems, December 1987, Annex D, "Telemetry Transfer
Frame Error Detection Encoding/Decoding Guideline".
2. "Advanced Orbiting Systems, Network and Data Links: Summary of Concept,
Rationale, and Performance", CCSDS 700.0-G-3, Green Book, Consultative
Committee for Space Data Systems, November 1992, Appendix C, "Procedures
for Verifying CCSDS Encoder Implementations", Page C-11.
*/
/* The FCS calculation using simulated shift register with feedback */
unsigned short const g=0x1021; /* x16 + x12 + x5 + 1 generator polynomial */
unsigned short fcs_calc_in_shift_register(unsigned char *data, unsigned short count)
{
unsigned short fcs=0xFFFF; /* initial FCS value */
unsigned short d, i, k;
for (i=0; i<count; i++)
{
d = *data++ << 8;
for (k=0; k<8; k++)
{
if ((fcs ^ d) & 0x8000)
fcs = (fcs << 1) ^ g;
else
fcs = (fcs << 1);
d <<= 1;
}
}
return(fcs);
}
/* The faster FCS calculation procedure uses 256 x 16-bit Look Up Table: */
unsigned short fcs_look_up_table[256];
unsigned short fcs_calc_by_look_up_table(unsigned char *data, unsigned short count)
{
unsigned short fcs=0xFFFF; /* initial FCS value */
unsigned char idx;
unsigned short i, t;
for (i=0; i<count; i++)
{
t = fcs;
fcs = fcs_look_up_table[(t>>8) ^ *data++];
fcs = fcs ^ (t<<8);
}
return(fcs);
}
/* FCS Look Up Table Calculation */
void fcs_look_up_table_calc(unsigned short *table)
{
unsigned short d, i, k, fcs;
for (i=0; i<256; i++)
{
fcs=0x0000;
d = i<<8;
for (k=0; k<8; k++)
{
if ((fcs ^ d) & 0x8000)
fcs = (fcs << 1) ^ g;
else
fcs = (fcs << 1);
d <<= 1;
}
table[i] = fcs;
}
}
/* FCS calculation program example */
#include <stdio.h>
unsigned char frame[128]={0x80};
void main(void)
{
FILE *out_file;
unsigned short i, j, d, fcs;
unsigned short a16, feedback;
/* Calculate FCS Look Up Table */
fcs_look_up_table_calc( fcs_look_up_table );
/* make table.h contains FSC Look Up Table */
out_file = fopen("table.h", "w");
if(out_file==NULL) {printf("can't open file\n"); return;}
fprintf(out_file,"/*\n unsigned short fcs_look_up_table[256]={\n");
for (i=0; i<256; i++)
{
if (i%8 == 0) fprintf(out_file,"\n");
fprintf(out_file,"%04X ",fcs_look_up_table[i]);
}
fprintf(out_file,"};\n*/\n");
fclose(out_file);
printf("Compare Shift Register and Look Up Table FCS Calculations\n\n");
/* Encode 126 data bytes */
fcs = fcs_calc_in_shift_register(frame, 126);
printf(" shift register look up table\n");
printf(" frame check sequence: ");
for (j=0x8000; j; j>>=1) if (fcs & j) printf("1"); else printf("0");
printf(" ");
fcs = fcs_calc_by_look_up_table(frame, 126);
for (j=0x8000; j; j>>=1) if (fcs & j) printf("1"); else printf("0");
printf ("\n");
/* Append FCS to frame */
frame[126] = fcs>>8; /* most significant FCS byte first */
frame[127] = fcs; /* least significant FCS byte last */
/* Decode - Calculate error detection syndrome */
fcs = fcs_calc_in_shift_register(frame, 128);
printf("error detection syndrome: ");
for (j=0x8000; j; j>>=1) if (fcs & j) printf("1"); else printf("0");
printf(" ");
fcs = fcs_calc_by_look_up_table(frame, 128);
for (j=0x8000; j; j>>=1) if (fcs & j) printf("1"); else printf("0");
printf("\n");
printf("\n");
printf("Testing the CRC Polynomial ref.2 Page C-11\n\n");
printf("STEP INFO A(16) FEEDBACK CONTENTS 16,15,...,2,1\n");
d = 0x8000;
fcs = 0xFFFF;
for (i=1; i<21; i++)
{
a16 = fcs>>15;
if (feedback=((d ^ fcs)>>15))
fcs = (fcs<<1) ^ g;
else
fcs = fcs<<1;
printf("%2d %d %d %d ", i, d>>15, a16, feedback);
for (j=0x8000; j; j>>=1) if (fcs & j) printf("1"); else printf("0");
printf("\n");
if (i%10==0) printf("\n");
d<<=1;
}
}</PRE>
<P><BR>
<HR>
<P>
<ADDRESS><I>Mike Capell <BR>Thu Oct 30 23:29:55 MET 1997</I>
</ADDRESS></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -