/* ATmega103 crc.c file
Author : Robert Stuart
Company : PDL Industries Ltd
Date of Creation : 2 November 1999
Tested : not yet
Function :
Performs a 16-bit CRC check on the program memory. Memory locations from
START_PROGRAM_MEMORY_ADDRESS (0x0000) to END_PROGRAM_MEMORY_ADDRESS - 1 (0xE777)
are applied to the CRC calculation. At addresses END_PROGRAM_MEMORY_ADDRESS and
END_PROGRAM_MEMORY_ADDRESS + 1 are 16-bit CRC value, therefore once subtracked
should give zero. Any non-zero answer applies a memory program fault.
*/
/* include */
#include "crc.h"
void CRCInitialise( void )
{
CRC16 = 0;
ProgramMemory = START_PROGRAM_MEMORY_ADDRESS;
/* starting address */
}
void CheckCRC( void )
{
static unsigned char i;
const unsigned char *ptr;
static unsigned int c;
if ( ProgramMemory < END_PROGRAM_MEMORY_ADDRESS )
{
if ( !CRC16 ) /* reset counter at the start */
i = 0;
if ( !i )
{
ptr = ( unsigned char * ) ProgramMemory++;
/* read 8-bits from the program memory */
c = *ptr;
c <<= 8; /* convert to 16-bit */
}
if ( ( CRC16 ^ c ) & CRC_DIVISOR ) /* adjust crc */
CRC16 = (CRC16 << 1) ^ CRC_16_MASK;
else
CRC16 <<= 1;
c <<= 1;
if ( ++i > 7 )
i = 0;
}
else if ( ProgramMemory == END_PROGRAM_MEMORY_ADDRESS )
{
/* read the last two memory locations are subtack in 16-bit from CRC value, if zero
then CRC is correct
*/
ptr = ( unsigned char * ) ProgramMemory++;
CRC16 -= *ptr;
ptr = ( unsigned char * ) ProgramMemory++;
CRC16 -= (*ptr) << 8;
if ( CRC16 )
SETBIT( SystemStatus, BIT(FAULT_CRC_CHECK) );/* flag crc fault therefore program could have been corrupted */
else
CLEARBIT( SystemStatus, BIT(FAULT_CRC_CHECK) );
}
}