dpprepaid468x60


FCS CALCULATION

FCS (Frame Check Sequence)is calculated over the entire PPP packet, not including 
the start and stop flags (0x7E). For a transmitted packet the calculation is performed 
prior to the addition of the 0x7D escape sequences to the octets lower then 0x20. 
For a received packet the calculation is performed after eliminating the 0x7D escape 
sequences and subtracting 0x20 from the octets marked by the escape sequences. 

In the example code, 
u16 buff[] is an array containing all octets in the packet with the 
FCS octets made equal to zero.
u16 fcs_start is the position of the first octet in the packet
u16 fcs_stop is the position of the last octet in the packet

/*
**************************** fcs_calc *********************
Function: fcs_calc
Description: frame check sequence (fcs) calculation.
**************************************************************
*/
typedef unsigned short u16

u16 short fcs_calc(u16 fcs_start, u16 fcs_stop, u16 buff[])
{
#define P	0x8408
#define fcsinit	0xFFFF

static u16 fcstab[256];
u16 fcs;
u16 b, v;
u16 i;
u16 MSB_fcs, LSB_fcs;

	for (b=0; ; ){
  	  v = b;
	  for (i = 8; i--; )
	     v = v & 1 ? (v >> 1) ^ P : v >> 1;
  	  fcstab[b] = v & 0xFFFF;
	  if (++b == 256)
	    break;
	}

	fcs = fcsinit;
	for (i=fcs_start; i<fcs_stop; i++)
	  fcs = (fcs >> 8) ^ fcstab[(fcs ^ buff[i]) & 0xFF];
	fcs ^= 0xFFFF;

	LSB_fcs = ((fcs >> 8) & 0xFF);
	MSB_fcs = fcs & 0xFF;

	return(MSB_fcs, LSB_fcs);
}

Previous Next Contents

mail to alex