📄 ccpsim.c
字号:
void ccpUserBackground( void ) {
}
//#pragma argsused
CCP_BYTE ccpDisableNormalOperation( CCP_MTABYTEPTR a, CCP_WORD s )
{
return 1;
}
#if defined(CCP_SEED_KEY)
// ccpGetSeed() ----------------------------------------------------------------
//
// Parameter: PL_CAL, PL_PGM, PL_DAQ
// Return: 32 bit value with seed
//
// Call:
// ccpCommand(), CC_GET_SEED
// ccpUnlock() -----------------------------------------------------------------
//
// Parameter: Key mit 0-6 Byte
// Return: PL_CAL, PL_PGM, PL_DAQ or 0
//
// Call:
// ccpCommand(), CC_UNLOCK
#ifndef random
static CCP_DWORD RndCnt = 0x4fe9d61;
static CCP_DWORD random(CCP_DWORD max) {
RndCnt += max + RndCnt/3;
return RndCnt % (max+1);
}
#endif
CCP_BYTE ccpResourceMask = 0;
CCP_DWORD ccpLastSeed = 0;
CCP_DWORD ccpGetSeed( CCP_BYTE resourceMask )
{
ccpResourceMask = resourceMask;
/* Generate a seed */
/* Example: */
/* Optimum is a number which never appears twice */
ccpLastSeed = random(0xFFFFFFFF);
return ccpLastSeed;
}
CCP_BYTE ccpUnlock( CCP_BYTE *key )
{
CCP_BYTE res;
/* Check the key */
/* Example: */
ccpLastSeed = (ccpLastSeed>>5) | (ccpLastSeed<<23);
ccpLastSeed *= 7;
ccpLastSeed ^= 0x26031961;
if ( *(CCP_DWORD*)key != ccpLastSeed ) {
return 0;
}
/* Reset resource protection bit */
res = ccpResourceMask;
ccpResourceMask = 0;
return res;
}
#endif
///////////////////////////////////////////////////////////////////////
// ccpReset()
// Send a COLD_START_REQUEST event message
//---------------------------------------------------------------------
void ccpReset(void) {
#ifdef CCP_RESET_MESSAGE
unsigned char msg[8];
msg[0] = 0xFE;
msg[1] = CRC_COLD_START_REQUEST;
msg[2] = 0;
ccpSend(msg);
#endif
}
///////////////////////////////////////////////////////////////////////
// main()
//---------------------------------------------------------------------
//
// help
static void usage( void ) {
printf(
"\n"
"Usage:\n"
" " APP_NAME " [options]\n"
"\n"
" Options:\n"
" -tx Set screen output verbosity to x (Default 1)\n"
" -bx Set CAN bitrate to x (Default not initialized)\n"
" -v Use a virtual channel (Default CANcardX)\n"
" CANape will be automatically configured to virtual channels\n"
" -ac2 Use a CANac2 channel\n"
" -pci Use a CANac2-PCI channel\n"
" -pari Use a CANpari channel\n"
" -cardx Use a CANcardX channel\n"
" -1 Use channel 1 (Default)\n"
" -2 Use channel 2\n"
" -crox Set CRO identifier to x (Default 1639)\n"
" -dtox Set DTO identifier to x (Default 2017)\n"
" -x Use extended ids (Default standard)\n"
" -addr Set slave address to x (Default 57)\n"
" -init Init flash to default\n"
"\n"
" Keyboard commands:\n"
" 1-4 Set screen output verbosity 1..4\n"
" T Trigger event channel 1\n"
" ESC Exit\n"
"\n"
);
exit(0);
}
int handleKey( int key ) {
switch (key) {
// Help
case 'h':
case 'H':
usage();
break;
// Send a COLD_START_REQUEST
case 'r':
case 'R':
ccpReset();
break;
// Trigger a DAQ event
case 't':
case 'T': ccpDaq(1);
break;
// Debuglevel
case '0':
case '1':
case '2':
case '3':
case '4':
gDebugLevel = key-'0';
break;
// ESC
case 27:
return 1;
//break;
default:
break;
}
return 0;
}
// thread running ccpDaq()
//#pragma argsused
CCP_DWORD WINAPI thread( PVOID par ) {
int i;
i = 0;
threadRunning = 1;
while (threadRunning) {
/* CCP driver background processing */
ccpBackground();
/* ECU Simulation */
ecuCyclic();
/* Event Channel 10 is cyclic 10 ms */
ccpDaq(10);
/* Event Channel 11 is cyclic 100 ms */
if (++i>=10) {
i=0;
ccpDaq(11);
}
/* Delay */
Sleep(10);
}
return 0;
}
int main( int argc, char *argv[])
{
int i;
unsigned int u;
printf(
APP_NAME " ECU simulator using CCP\n"
"Vector Informatik GmbH, 1997-2001\n\n"
"Build " __DATE__ " " __TIME__ "\n"
#ifndef __BORLANDC__
"Microsoft Version\n"
#endif
);
// Get default hardware configuration from VCANCONF
{
int type, index, channel;
if (VSUCCESS==ncdGetApplConfig("CCPsim",0,&type,&index,&channel)) {
hwType = type;
hwChannel = channel;
hwIndex = index;
}
}
// Parse commandline
for (i=1; i<argc; i++) {
char c;
if (strcmp(argv[i], "-h") == 0) {
usage();
} else if (strcmp(argv[i], "-v") == 0) {
hwType = HWTYPE_VIRTUAL;
printf("Use a virtual channel\n");
} else if (strcmp(argv[i], "-cardx") == 0) {
hwType = HWTYPE_CANCARDX;
printf("Use a CANcardX channel\n");
} else if (strcmp(argv[i], "-ac2") == 0) {
hwType = HWTYPE_CANAC2;
printf("Use a CANac2 channel\n");
} else if (strcmp(argv[i], "-pci") == 0) {
hwType = HWTYPE_CANAC2PCI;
printf("Use a CANac2-PCI channel\n");
} else if (strcmp(argv[i], "-pari") == 0) {
hwType = HWTYPE_CANPARI;
printf("Use a CANpari channel\n");
} else if (strcmp(argv[i], "-x") == 0) {
gDtoId |= 0x80000000UL;
gCroId |= 0x80000000UL;
printf("Extended Ids\n");
} else if (strcmp(argv[i], "-1") == 0) {
hwChannel = 0;
printf("Use channel 1\n");
} else if (strcmp(argv[i], "-2") == 0) {
hwChannel = 1;
printf("Use channel 2\n");
} else if (strcmp(argv[i], "-init") == 0) {
remove("CCPSIM.DAT");
printf("Init flash\n");
} else if (sscanf(argv[i], "-b%u", &u) == 1) {
if (u>5000&&u<=1000000) gBitRate = u;
printf("Initialize CAN hardware to %u BPS\n",gBitRate);
} else if (sscanf(argv[i], "-cro%u", &u) == 1) {
CCP_CRO_ID = u;
printf("CCP_CRO_ID = %u\n",CCP_CRO_ID);
} else if (sscanf(argv[i], "-dto%u", &u) == 1) {
CCP_DTO_ID = u;
printf("CCP_DTO_ID = %u\n",CCP_DTO_ID);
} else if (sscanf(argv[i], "-addr%u", &u) == 1) {
CCP_STATION_ADDR = u;
printf("CCP_STATION_ADDR = %u\n",CCP_STATION_ADDR);
} else if (sscanf(argv[i], "-t%c", &c) == 1) {
gDebugLevel = c-'0';
printf("Set screen output verbosity to %u\n",gDebugLevel);
} else {
usage();
}
}
printf("\n");
// Set default hardware configuration in VCANCONF
ncdSetApplConfig("CCPsim",0,hwType,hwIndex,hwChannel);
// Initialize Address -> Pointer
{
extern CCP_BYTE flashSignatur;
if (gDebugLevel) {
printf("\nRequired CANape Calibration RAM Settings:\n");
//printf(" dataSeg = 0x%08X\n",dataSeg);
//printf(" CALRAM_START = 0x%08X\n",(CCP_BYTE*)&CALRAM_START);
printf(" CALRAM_SECTOR_COUNT = 1\n");
printf(" CALRAM_SECTOR_ADDR_1 = 0x%08X\n",(CCP_BYTE*)&CALRAM_START-dataSeg);
printf(" CALRAM_SECTOR_SIZE_1 = 0x%08X\n",(CCP_BYTE*)&CALRAM_LAST-(CCP_BYTE*)&CALRAM_START+1);
printf("\nRequired CANape FLASH Settings:\n");
printf(" FLASH_SECTOR_COUNT = 1\n");
printf(" FLASH_SECTOR_ADDR_1 = 0x%08X\n",(CCP_BYTE*)&CALRAM_START-dataSeg);
printf(" FLASH_SECTOR_SIZE_1 = 0x%08X\n",(CCP_BYTE*)&CALRAM_LAST-(CCP_BYTE*)&CALRAM_START+1);
printf(" FLASH_SIGN = 0x%08X\n",(CCP_BYTE*)&flashSignatur-dataSeg);
printf(" FLASH_SIZE = 32\n\n");
}
}
// Initialize the flash emulation feature
#ifdef CCP_PROGRAM
ccpInitFlash();
#endif
// Initialize CCP driver
ccpInit();
// Initialize ECU simulation
ecuInit();
// Create the DAQ thread
threadHandle = CreateThread(0,0x1000,thread,0,0,&threadId);
SetThreadPriority(threadHandle,THREAD_PRIORITY_TIME_CRITICAL);
// Mainloop
canMain();
// Stop DAQ thread
if (threadHandle) {
threadRunning = 0;
WaitForSingleObject(threadHandle,5000);
}
// Write Flash to disk
#ifdef CCP_PROGRAM
ccpWriteFlash();
#endif
return 0;
}
void canMain( void ) {
int end,i;
Vstatus vErr;
Vevent *pEvent;
HANDLE h;
#ifdef CCP_LATENCY_TEST
unsigned int txRqTime;
#endif
// Initialize the CAN driver
vErr = InitDriver(hwType,hwIndex,hwChannel);
if (vErr) goto error;
// Create a synchronisation object
h = CreateEvent(NULL, FALSE, FALSE, NULL);
vErr = ncdSetNotification(gPortHandle, (void*)&h, 1);
if (vErr) goto ncdError;
// channel on bus
vErr = ncdActivateChannel(gPortHandle,gChannelMask);
if (vErr) goto ncdError;
// RESET message
ccpReset();
// Mainloop
end = 0;
while (!end) {
// Wait for receive event
WaitForSingleObject(h,500);
// Receive
for (;;) { // read all events
vErr = ncdReceive1(gPortHandle,&pEvent);
if (vErr&&vErr!=VERR_QUEUE_IS_EMPTY) goto ncdError;
if (vErr==VERR_QUEUE_IS_EMPTY) break;
// check for the ccp cro message
if (pEvent->tag==V_RECEIVE_MSG) {
if (pEvent->tagData.msg.flags & MSGFLAG_ERROR_FRAME) {
printf("Error Frame!\n");
}
else if (pEvent->tagData.msg.id==CCP_DTO_ID) {
#ifdef CCP_LATENCY_TEST
if (pEvent->tagData.msg.flags&MSGFLAG_TXRQ) {
txRqTime = pEvent->timeStamp;
}
#endif
if (pEvent->tagData.msg.flags&MSGFLAG_TX) {
#ifdef CCP_LATENCY_TEST
ccpLatencyTime = pEvent->timeStamp-txRqTime;
#endif
/* Notify the CCP driver */
if (!ccpSendCallBack()) {
}
}
}
if (pEvent->tagData.msg.id==CCP_CRO_ID) {
// Screen output
if (gDebugLevel>=2) {
printf("RX: ");
for (i=0;i<pEvent->tagData.msg.dlc;i++) printf(" %02X",pEvent->tagData.msg.data[i]);
printf("\n");
}
ccpCommand(&pEvent->tagData.msg.data[0]);
}
}
} // for
// Check keyboard
if (kbhit()) {
int c = getch();
if (handleKey(c)) {
end = 1;
}
}
} // while(!end);
error:
// RESET message
ccpReset();
ncdDeactivateChannel(gPortHandle, gChannelMask);
CleanUp();
return;
ncdError:
printf("ERROR: %s\n",ncdGetErrorString(vErr));
goto error;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -