📄 main.c
字号:
#endif /* description */
/* PRM file method test routines */
enum CheckType {
CheckSumAdd= '+', /* Add */
CheckSumXor= '^', /* XOR */
CheckSumCRC_CCITT= 'c', /* CRC CCITT */
CheckSumCRC_8= '8', /* CRC8 with default poly */
CheckSumCRC_16= '6', /* CRC16 with default poly */
CheckSumCRC_32= '2', /* CRC32 with default poly */
CheckSumCRC_CCITT_P= 'C', /* precomputed CRC CCITT */
CheckSumCRC_8_P= '9', /* precomputed CRC8 with default poly */
CheckSumCRC_16_P= '7', /* precomputed CRC16 with default poly */
CheckSumCRC_32_P= '3', /* precomputed CRC32 with default poly */
CheckSumCRC_8_spec= 's', /* CRC8 with non default poly and special init value */
};
struct CheckSums {
unsigned char size;
enum CheckType type;
void* start;
void* end;
void* cmp;
} checkSums[]= {
/* 0 */ {4, CheckSumAdd, (void*)0xE020, (void*)0xFF00, (void*)0xE000},
/* 1 */ {2, CheckSumAdd, (void*)0xE020, (void*)0xFF00, (void*)0xE004},
/* 2 */ {1, CheckSumAdd, (void*)0xE020, (void*)0xFF00, (void*)0xE006},
/* 3 */ {4, CheckSumXor, (void*)0xE020, (void*)0xFF00, (void*)0xE008},
/* 4 */ {2, CheckSumXor, (void*)0xE020, (void*)0xFF00, (void*)0xE00C},
/* 5 */ {1, CheckSumXor, (void*)0xE020, (void*)0xFF00, (void*)0xE00E},
/* 6 */ {2, CheckSumCRC_CCITT, (void*)0xE020, (void*)0xFF00, (void*)0xE010},
/* 7 */ {2, CheckSumCRC_CCITT_P, (void*)0xE020, (void*)0xFF00, (void*)0xE010},
/* 8 */ {1, CheckSumCRC_8, (void*)0xE020, (void*)0xFF00, (void*)0xE018},
/* 9 */ {1, CheckSumCRC_8_P, (void*)0xE020, (void*)0xFF00, (void*)0xE018},
/* 10 */ {2, CheckSumCRC_16, (void*)0xE020, (void*)0xFF00, (void*)0xE012},
/* 11 */ {2, CheckSumCRC_16_P, (void*)0xE020, (void*)0xFF00, (void*)0xE012},
/* 12 */ {4, CheckSumCRC_32, (void*)0xE020, (void*)0xFF00, (void*)0xE014},
/* 13 */ {4, CheckSumCRC_32_P, (void*)0xE020, (void*)0xFF00, (void*)0xE014},
/* 14 */ {1, CheckSumCRC_8_spec, (void*)0xE020, (void*)0xFF00, (void*)0xE019},
};
/* here we store some timings to compare the algorithms */
unsigned long simplePrmAddTime;
unsigned long simplePrmCRCTime;
unsigned long prmTimes[sizeof(checkSums)/sizeof(checkSums[0])];
unsigned long allAutoTime;
/* Timings do only work with the optional Timer component loaded in the simulator. */
/* Timer component: Every write to its address range at 0x210 does update the read values with the current time (in cycles) */
/* If the Timer component is not available, all the measured times will be 0. */
volatile unsigned long currentTime@0x210; /* Do not move: memory actually implemented by Timer component */
unsigned long GetTime(unsigned long* pLastTime) {
unsigned long time;
currentTime=0; /* Timer component: set currentTime to the current time */
time= currentTime-*pLastTime;
*pLastTime= currentTime;
return time;
}
#define CHECKSUM_FIRST_ADDR 0xE020
#define CHECKSUM_LAST_ADDR 0xFEFF
#define CHECKSUM_STORAGE_BYTE_ADD (*(unsigned char*)0xE006)
#define CHECKSUM_STORAGE_CRC_CCITT (*(unsigned short*)0xE010)
static int IsSimplePrmCheckSumOK(void) {
unsigned char* ptr= (unsigned char*)CHECKSUM_FIRST_ADDR;
unsigned char checkSum=0;
while (ptr != (unsigned char*)CHECKSUM_LAST_ADDR+1) {
checkSum += *ptr;
ptr++;
}
return (checkSum & 0xff) == CHECKSUM_STORAGE_BYTE_ADD;
}
static int IsSimplePrmCRCCheckSumOK(void) {
return _Checksum_CheckAreaCRC_CCITT((const void*)CHECKSUM_FIRST_ADDR, CHECKSUM_LAST_ADDR+1-CHECKSUM_FIRST_ADDR) == CHECKSUM_STORAGE_CRC_CCITT;
}
static int AreAllPrmChecksSumsOK(void) {
int i;
for (i=0; i < sizeof(checkSums)/sizeof(checkSums[0]); i++) {
unsigned long checkSumVal;
unsigned long expectedChecksum;
const void* start= (const void*)checkSums[i].start;
unsigned int len= (const unsigned char*)checkSums[i].end - (const unsigned char*)start;
enum CheckType checkSumType= checkSums[i].type;
unsigned char checkSize = checkSums[i].size;
unsigned long time=0;
/* read the expected checksum */
if (checkSize == 1) {
expectedChecksum= *(unsigned char*)checkSums[i].cmp;
} else if (checkSize == 2) {
expectedChecksum= *(unsigned short*)checkSums[i].cmp;
} else {
/* (checkSize == 4) */
expectedChecksum= *(unsigned long*)checkSums[i].cmp;
}
(void)GetTime(&time); /* init timer */
/* calculate the actual checksum */
switch (checkSumType) {
case CheckSumCRC_CCITT: checkSumVal= _Checksum_CheckAreaCRC_CCITT(start, len); break;
case CheckSumCRC_CCITT_P: checkSumVal= _Checksum_CheckAreaCRC_CCITT_PreCalc(start, len); break;
case CheckSumCRC_8: checkSumVal= _Checksum_CheckAreaCRC8(start, len, DEFAULT_CRC8_CHECKSUM, -1); break;
case CheckSumCRC_8_P: checkSumVal= _Checksum_CheckAreaCRC8_PreCalc(start, len); break;
case CheckSumCRC_16: checkSumVal= _Checksum_CheckAreaCRC16(start, len, DEFAULT_CRC16_CHECKSUM, -1); break;
case CheckSumCRC_16_P: checkSumVal= _Checksum_CheckAreaCRC16_PreCalc(start, len); break;
case CheckSumCRC_32: checkSumVal= _Checksum_CheckAreaCRC32(start, len, DEFAULT_CRC32_CHECKSUM, -1); break;
case CheckSumCRC_32_P: checkSumVal= _Checksum_CheckAreaCRC32_PreCalc(start, len); break;
case CheckSumCRC_8_spec: checkSumVal= _Checksum_CheckAreaCRC8(start, len, 0x81, 0x01); break;
case CheckSumAdd:
if (checkSize == 1) {
checkSumVal= _Checksum_CheckAreaByteAdd(start, len);
} else if (checkSize == 2) {
checkSumVal= _Checksum_CheckAreaWordAdd(start, len);
} else {
/* (checkSize == 4) */
checkSumVal= _Checksum_CheckAreaLongAdd(start, len);
}
break;
case CheckSumXor:
if (checkSize == 1) {
checkSumVal= _Checksum_CheckAreaByteXor(start, len);
} else if (checkSize == 2) {
checkSumVal= _Checksum_CheckAreaWordXor(start, len);
} else {
/* (checkSize == 4) */
checkSumVal= _Checksum_CheckAreaLongXor(start, len);
}
break;
}
prmTimes[i]= GetTime(&time); /* get time */
if (checkSumVal != expectedChecksum) {
return FALSE;
}
}
return TRUE;
}
/* main test application */
int isSimpleAddChecksumOK; /* simple checksum computed by added the values in some memory area */
int isSimpleCRCChecksumOK; /* simple checksum computed by computing a CRC in a memory area */
int areAllPrmChecksumsOK; /* are all the checksums computed because of the prm entry ok. Uses the array checkSums above to find them all */
int areAutomaticChecksumsOK; /* are all the checksums automatically generated by the linker togehter with position/size info correct? */
int areAutomaticChecksumsOkAfterModi; /* test if checksums do fail (test only works when the simulator is configured accordinly) */
int cnt; /* counts the loops */
#define ADDITIONAL_CONST_AREA 0x0220 /* actually its RAM */
const char Data[] @ ADDITIONAL_CONST_AREA ="Hallo World. This is to test the checksum stuff a little bit more";
const char Data1[] @ (ADDITIONAL_CONST_AREA+101) ="OddSized";
const char Data2[] @ (ADDITIONAL_CONST_AREA+130) ="OddSized";
void main(void) {
for (;;) {
unsigned long time=0;
(void)GetTime(&time); /* init timer */
isSimpleAddChecksumOK= IsSimplePrmCheckSumOK();
simplePrmAddTime= GetTime(&time);
isSimpleCRCChecksumOK= IsSimplePrmCRCCheckSumOK();
simplePrmCRCTime= GetTime(&time);
areAllPrmChecksumsOK= AreAllPrmChecksSumsOK();
(void)GetTime(&time); /* init timer */
areAutomaticChecksumsOK= _Checksum_Check(_startupData.checkSum, _startupData.nofCheckSums);
allAutoTime= GetTime(&time);
*((char*)&Data[0])= 'B'; /* can we write here? */
/* if the debugger stops here, we probably cannot write. In the simulator, you the user, could help? */
areAutomaticChecksumsOkAfterModi = _Checksum_Check(_startupData.checkSum, _startupData.nofCheckSums);
areAutomaticChecksumsOkAfterModi ^= (Data[0] != 'H'); /* depending on the memory configuration, we can or cannot write into RAM. If we can, a failure is actually OK. Otherwise not */
*((char*)&Data[0])= 'H'; /* undo the change for the next round */
cnt++;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -