typedef struct ccpOdtEntry {
unsigned char *ptr;
} ccpOdtEntry_t;
typedef ccpOdtEntry_t ccpODT_t[7];
typedef struct ccpDaqList {
ccpODT_t odt[3];
unsigned short prescaler;
unsigned short cycle;
unsigned char eventChannel;
unsigned char last;
unsigned char flags;
} ccpDaqList_t;
typedef struct {
unsigned char b[8];
} ccpMsg_t;
typedef struct {
unsigned char len;
unsigned char rp;
ccpMsg_t msg[(3 * 2)];
} ccpQueue_t;
struct ccp {
unsigned char Crm[8];
unsigned char SessionStatus;
unsigned char SendStatus;
unsigned char * MTA[2];
ccpQueue_t Queue;
ccpOdtEntry_t *DaqListPtr;
ccpDaqList_t DaqList[2];
unsigned char UserSessionStatus;
unsigned short CheckSumSize;
};
extern struct ccp ccp;
extern void ccpInit(void);
extern void ccpDaq(unsigned char eventChannel);
extern void ccpCommand(unsigned char * msg);
extern unsigned char ccpSendCallBack(void);
void ccpSendCrm(void);
extern unsigned char ccpBackground(void);
extern void ccpSend(unsigned char * msg);
extern void ccpUserBackground(void);
extern unsigned char * ccpGetPointer(unsigned char addr_ext, unsigned long addr);
unsigned char ccpStationId[] = "ECU00001";
struct ccp ccp;
void ccpSendCrm(void) {
;
if (ccp.SendStatus&(0x20 | 0x10 | 0x40)) {
ccp.SendStatus |= 0x01;
}
else {
ccp.SendStatus |= 0x10;
ccpSend(ccp.Crm);
}
;
}
void ccpQueueInit(void) {
ccp.Queue.len = 0;
ccp.Queue.rp = 0;
}
unsigned char ccpQueueWrite(ccpMsg_t *msg) {
if (ccp.Queue.len >= (3 * 2)) return 0;
ccp.Queue.msg[(ccp.Queue.rp + ccp.Queue.len) % (3 * 2)] = *msg;
ccp.Queue.len++;
return 1;
}
static unsigned char ccpWriteMTA(unsigned char n, unsigned char size, unsigned char * d) {
while (size-->0) {
*(ccp.MTA[n]) = *d;
ccp.MTA[n]++;
d++;
}
return 1;
}
static void ccpReadMTA(unsigned char n, unsigned char size, unsigned char * d) {
while (size-->0) {
*d = *(ccp.MTA[n]);
d++;
ccp.MTA[n]++;
}
}
static unsigned char ccpClearDaqList(unsigned char daq) {
unsigned char * p;
unsigned char * pl;
if (daq >= 2) return 0;
p = (unsigned char *)&ccp.DaqList[daq];
pl = p + sizeof(ccpDaqList_t);
while (p<pl) *p++ = 0;
ccp.SessionStatus |= 0x02;
ccpQueueInit();
return 3;
}
static unsigned char ccpPrepareDaq(unsigned char daq, unsigned char last, unsigned char eventChannel, unsigned short prescaler) {
if (daq >= 2) return 0;
ccp.DaqList[daq].eventChannel = eventChannel;
if (prescaler == 0) prescaler = 1;
ccp.DaqList[daq].prescaler = prescaler;
ccp.DaqList[daq].cycle = 1;
ccp.DaqList[daq].last = last;
ccp.DaqList[daq].flags = 0x04;
return 1;
}
static unsigned char ccpStartDaq(unsigned char daq) {
if (daq >= 2) return 0;
ccp.DaqList[daq].flags = 0x01;
ccp.SessionStatus |= 0x80;
return 1;
}
static void ccpStartAllPreparedDaq(void) {
unsigned char q;
for (q = 0; q<2; q++) {
if (ccp.DaqList[q].flags == 0x04) ccp.DaqList[q].flags = 0x01;
}
ccp.SessionStatus |= 0x80;
}
static void ccpStopDaq(unsigned char daq) {
unsigned char i;
if (daq >= 2) return;
ccp.DaqList[daq].flags = 0;
for (i = 0; i<2; i++) {
if (ccp.DaqList[i].flags & 0x01) return;
}
ccp.SessionStatus &= ~0x80;
}
static void ccpStopAllDaq(void) {
unsigned char q;
for (q = 0; q<2; q++) ccp.DaqList[q].flags = 0;
ccp.SessionStatus &= ~0x80;
}
static int ccpSampleAndTransmitDtm(unsigned char pid, unsigned char daq, unsigned char odt) {
unsigned char dtm[8];
unsigned char * p;
unsigned char i;
ccpOdtEntry_t *e;
dtm[0] = pid;
;
e = &ccp.DaqList[daq].odt[odt][0];
for (i = 1; i<8; i++) {
p = (e++)->ptr;
if (p) dtm[i] = *p;
}
if (ccp.SendStatus&(0x20 | 0x10 | 0x40)) {
if (!ccpQueueWrite((ccpMsg_t*)dtm)) {
;
return 0;
}
}
else {
ccp.SendStatus |= 0x20;
ccpSend(dtm);
}
;
return 1;
}
void ccpDaq(unsigned char eventChannel)
{
unsigned char q, o;
unsigned char j;
if (!(ccp.SessionStatus & 0x80)) return;
;
for (o = 0, q = 0; q<2; o += 3, q++) {
if (!(ccp.DaqList[q].flags & 0x01)) continue;
if (ccp.DaqList[q].eventChannel != eventChannel) continue;
if (--ccp.DaqList[q].cycle != 0) continue;
ccp.DaqList[q].cycle = ccp.DaqList[q].prescaler;
if ((3 * 2) - ccp.Queue.len <= ccp.DaqList[q].last) {
ccp.DaqList[q].flags |= 0x80;
continue;
}
for (j = 0; j <= ccp.DaqList[q].last; j++) {
if (!ccpSampleAndTransmitDtm((o + j) | (ccp.DaqList[q].flags & 0x80), q, j)) {
ccp.DaqList[q].flags |= 0x80;
}
else {
ccp.DaqList[q].flags &= ~0x80;
}
}
}
;
}
unsigned char ccpBackground(void) {
;
ccpUserBackground();
if (ccp.CheckSumSize) {
register unsigned char n;
register unsigned char b;
if (ccp.CheckSumSize <= (256 - 1)) {
n = (unsigned char)ccp.CheckSumSize;
ccp.CheckSumSize = 0;
}
else {
n = 0;
ccp.CheckSumSize -= 256;
}
do {
b = *(ccp.MTA[(2 - 1)]);
*(unsigned short *)&ccp.Crm[4] += b;
ccp.MTA[(2 - 1)]++;
} while (--n != 0);
if (ccp.CheckSumSize) {
;
return 1;
}
ccpSendCrm();
}
;
return 0;
}
void ccpCommand(unsigned char * com) {
;
if (com[0] == 0x01 || com[0] == 0x05) {
if ((*(unsigned short *)& com[2]) == 0 || (*(unsigned short *)& com[2]) == 0) {
if (com[0] == 0x01) {
if (!(ccp.SessionStatus & 0x10)) {
ccpStopAllDaq();
ccp.SendStatus = 0;
}
ccp.SessionStatus |= 0x20;
ccp.SessionStatus &= ~0x10;
}
ccp.Crm[0] = 0xFF;
ccp.Crm[1] = 0x00;
ccp.Crm[2] = com[1];
ccp.Crm[3] = 0xFE;
*(unsigned short *)&ccp.Crm[4] = 0;
*(unsigned short *)&ccp.Crm[6] = 0;
}
else {
if (ccp.SessionStatus & 0x20) {
ccp.SessionStatus &= ~0x20;
ccp.SessionStatus |= 0x10;
}
;
return;
}
}
else if (ccp.SessionStatus & 0x20) {
ccp.Crm[0] = 0xFF;
ccp.Crm[1] = 0x00;
ccp.Crm[2] = com[1];
switch (com[0]) {
case 0x07:
{
ccp.SessionStatus &= ~0x20;
if (com[2] == 0x00) {
ccp.SessionStatus |= 0x10;
}
else {
ccpStopAllDaq();
}
}
break;
case 0x17:
{
unsigned char i;
for (i = 0; ccpStationId[i] != 0; i++);
ccp.Crm[3] = i;
ccp.Crm[4] = 0;
ccp.Crm[5] = 0x01;
ccp.Crm[6] = 0;
ccp.Crm[5] |= 0x02;
ccp.Crm[7] = 142;
ccp.MTA[0] = (unsigned char *)ccpStationId; ;
}
break;
case 0x02:
{
if (com[2] <2 - 1) {
ccp.MTA[com[2]] = ccpGetPointer(com[3], (*(unsigned long *)& com[4])); ;
}
else {
ccp.Crm[1] = 0x32;
}
}
break;
case 0x03:
{
unsigned char r;
r = ccpWriteMTA(0, com[2], &com[3]);
if (r == 2) return;
if (r == 0 || r == 3) ccp.Crm[1] = 0x33;
}
break;
case 0x23:
{
unsigned char r;
r = ccpWriteMTA(0, 6, &com[2]);
if (r == 2) return;
if (r == 0 || r == 3) ccp.Crm[1] = 0x33;
}
break;
case 0x04:
{
ccpReadMTA(0, com[2], &ccp.Crm[3]);
}
break;
case 0x0F:
{
ccp.MTA[(2 - 1)] = ccpGetPointer(com[3], (*(unsigned long *)& com[4])); ;
ccpReadMTA((2 - 1), com[2], &ccp.Crm[3]);
}
break;
case 0x14:
{
ccpStopDaq(com[2]);
ccp.Crm[3] = ccpClearDaqList(com[2]);
ccp.Crm[4] = com[2] * 3;
}
break;
case 0x15:
{
if (com[2] >= 2 || com[3] >= 3 || com[4] >7) {
ccp.Crm[1] = 0x31;
ccp.DaqListPtr = 0;
}
else {
ccp.DaqListPtr = &ccp.DaqList[com[2]].odt[com[3]][com[4]];
}
}
break;
case 0x16:
{
if (
com[2] != 1
|| ccp.DaqListPtr == 0) {
ccp.