#include "proto.h" #include /************************************************************************** USB Protocol for USB-Probe MEII **************************************************************************/ /* Define the data record types of Stage One */ typedef struct data1 { protoTime time; unsigned char flags; unsigned char pidstart; unsigned char pidend; unsigned char valstart; unsigned long data; unsigned long val[8]; unsigned long valindex; unsigned long valcount; unsigned long vallength; unsigned short crc16_calc; unsigned short crc16; unsigned char validh; } data1T,*data1P; typedef struct { int findpid; } UsbLocalData; /* Flags */ #define Fnoflag 0 #define Fpidstart 1 #define Fpidend 2 #define Fdata 4 /* PID codes */ #define RESVD 0xF0 #define USBOUT 0xE1 #define ACK 0xD2 #define DATA0 0xC3 #define PING 0xB4 #define SOF 0xA5 #define NYET 0x96 #define DATA2 0x87 #define SPLIT 0x78 #define USBIN 0x69 #define NAK 0x5A #define DATA1 0x4B #define ERR 0x3C #define SETUP 0x2D #define STALL 0x1E #define MDATA 0x0F #define ValIndexMax 0x8 /** CRC table for the CRC-16. The poly is 0x8005 (x^16 + x^15 + x^2 + 1) */ unsigned short const crc16_table[256] = { 0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241, 0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440, 0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40, 0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841, 0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40, 0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41, 0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641, 0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040, 0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240, 0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441, 0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41, 0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840, 0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41, 0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40, 0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640, 0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041, 0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240, 0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441, 0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41, 0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840, 0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41, 0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40, 0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640, 0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041, 0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241, 0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440, 0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40, 0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841, 0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40, 0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41, 0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641, 0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040 }; static unsigned short crc16_byte(unsigned short crc, const unsigned int data) { return (crc >> 8) ^ crc16_table[(crc ^ data) & 0xff]; } /************************************************************************** JTAG Protocol Level 1 **************************************************************************/ /* Stage one: Process raw trace data, generate bytes out of raw trace data */ static protoSizeT PROTOAPI process1(protoContext context, protoPtr arrayOut, protoSizeT arrayOutSize, protoPtr arrayIn, protoSizeT arrayInSize, protoPtr localdata) { #define Doff0 8 // Offset of DATA-Bus entry #define Doff1 Doff0+8 /* Strobe lines */ /* #define CLK TBits[0] */ #define PID TBits[0] #define RXACT TBits[1] #define RXERR TBits[2] #define RXVAL TBits[3] #define LSTATE0 TBits[4] #define LSTATE1 TBits[5] #define VALIDH TBits[6] #define TXRDY TBits[7] /* Data lines */ #define DAT0 TBits[Doff] #define DAT1 TBits[Doff+1] #define DAT2 TBits[Doff+2] #define DAT3 TBits[Doff+3] #define DAT4 TBits[Doff+4] #define DAT5 TBits[Doff+5] #define DAT6 TBits[Doff+6] #define DAT7 TBits[Doff+7] #define DAT8 TBits[Doff+8] #define DAT9 TBits[Doff+9] #define DAT10 TBits[Doff+10] #define DAT11 TBits[Doff+11] #define DAT12 TBits[Doff+12] #define DAT13 TBits[Doff+13] #define DAT14 TBits[Doff+14] #define DAT15 TBits[Doff+15] /* Data lines */ #define DATB0 ((TBits[Doff0+7]<<7)|(TBits[Doff0+6]<<6)|(TBits[Doff0+5]<<5)|(TBits[Doff0+4]<<4)|(TBits[Doff0+3]<<3)|(TBits[Doff0+2]<<2)|(TBits[Doff0+1]<<1)|TBits[Doff0]) #define DATB1 ((TBits[Doff1+7]<<7)|(TBits[Doff1+6]<<6)|(TBits[Doff1+5]<<5)|(TBits[Doff1+4]<<4)|(TBits[Doff1+3]<<3)|(TBits[Doff1+2]<<2)|(TBits[Doff1+1]<<1)|TBits[Doff1]) #define DATA ((DATB1<<8)|DATB0) unsigned int PIDstart, USBdata; protoSizeT indexin,indexout; data1P dataOut; /* Pointer to array which will contain the output records */ protoTime traceTime; /* timestamp of raw trace data record */ unsigned int TBits[24]; /* Bit value of the USB signals which are processed */ unsigned int RXACT_Last; dataOut=(data1P)arrayOut; /* type cast to the used output record type */ indexin = 0; indexout = 0; RXACT_Last = 1; PIDstart = 0; USBdata = 0; PROTO_ReadTrace(context, indexin, &traceTime, TBits); indexin++; while (indexin=arrayOutSize) /* If there is no free space to store more */ return PROTO_PROCESS_OUTOFMEMORY; /* records, the processing is aborted */ if ((indexin & 0xfff) == 0) /* give GUI a chance to stop us */ if (PROTO_Cancel(context)) return PROTO_PROCESS_CANCEL; if ((RXACT_Last==1)&&(RXACT==0)) { dataOut[indexout].flags = Fpidend; dataOut[indexout].time = traceTime; dataOut[indexout].data = DATA; dataOut[indexout].validh = VALIDH; indexout++; } /* if ((RXACT_Last==0)&&(RXACT==1)) PIDstart = 1; */ if (RXVAL==1) { /* if (PIDstart) */ if (PID) { dataOut[indexout].flags = Fpidstart; switch (DATA & 0xff) { case DATA0: case DATA1: case DATA2: case MDATA: USBdata = 1; break; default: USBdata = 0; break; } PIDstart = 0; } else { if (USBdata == 1) dataOut[indexout].flags = Fdata; else dataOut[indexout].flags = 0; } dataOut[indexout].time = traceTime; dataOut[indexout].data = DATA; dataOut[indexout].validh = VALIDH; indexout++; } RXACT_Last = RXACT; PROTO_ReadTrace(context, indexin, &traceTime, TBits); indexin++; } return indexout; } /* Display Stage One data */ static void PROTOAPI display1(protoContext context, protoPtr pdata, protoPtr localdata) { data1P printData; printData=(data1P)pdata; if ((printData->flags) != Fpidend) { if (printData->validh) PROTO_Printf(context, " %04X", (unsigned long) printData->data); else PROTO_Printf(context, " %02X", (unsigned long) printData->data & 0xff); } } /************************************************************************** Protocol Level 2 **************************************************************************/ /*********** Generates data displayed at layer 2 in the ListProtocol window. context: needed by higher software layers, only arrayOut: array of data of protocol level 2 generated by this function arrayOutSize: number of valid entries in arrayOut arrayIn: array of data of protocol level 1 arrayInSize: number of valid entries in arrayIn localdata: pointer to variable block global within protojtag.c; in protojtag.c only used to store "initialstate" which is a parameter read by the parser return value: size of created information array ***********/ static protoSizeT PROTOAPI process2(protoContext context, protoPtr arrayOut, protoSizeT arrayOutSize, protoPtr arrayIn, protoSizeT arrayInSize, protoPtr localdata) { data1P dataIn; data1P dataOut; int ValIndex, ValCount, USBdata; protoSizeT indexin, indexout, ValRec, FirstValRec; unsigned short crc16_calc, crc16_calc_history[3]; dataIn=(data1P)arrayIn; /* Data from Stage One */ dataOut=(data1P)arrayOut; /* Stage Two data which will be generated in this function */ ValRec = -1; ValIndex = ValIndexMax; USBdata = 0; indexout= 0; for (indexin=0;indexin=arrayOutSize) /* If there is no free space to store more */ return PROTO_PROCESS_OUTOFMEMORY; /* records, the processing is aborted */ if ((dataIn[indexin].flags) == Fpidstart) { crc16_calc = 0xffffu; // CRC16 start value for USB crc16_calc_history[0] = crc16_calc; crc16_calc_history[1] = crc16_calc; crc16_calc_history[2] = crc16_calc; dataOut[indexout].time = dataIn[indexin].time; dataOut[indexout].flags = dataIn[indexin].flags; dataOut[indexout].valstart = 0; dataOut[indexout].data = dataIn[indexin].data; dataOut[indexout].validh = dataIn[indexin].validh; ValIndex = 0; ValCount = 0; ValRec = indexout; FirstValRec = indexout; dataOut[ValRec].valcount = ValCount; switch (dataIn[indexin].data & 0xff) { case DATA0: case DATA1: case DATA2: case MDATA: USBdata = 1; break; default: USBdata = 0; break; } if (dataIn[indexin].validh) { dataOut[ValRec].valcount = ValCount; dataOut[ValRec].val[ValIndex] = (dataIn[indexin].data>>8) & 0xff; dataOut[ValRec].valindex = ValIndex; dataOut[FirstValRec].vallength= ValCount + 1; crc16_calc = crc16_byte(crc16_calc, (dataIn[indexin].data>>8) & 0xff); crc16_calc_history[ValCount % 3] = crc16_calc; dataOut[FirstValRec].crc16_calc = crc16_calc_history[(ValCount + 1) % 3]; dataOut[FirstValRec].crc16 >>= 8; dataOut[FirstValRec].crc16 |= dataIn[indexin].data & 0xff00; ValIndex++; ValCount++; } indexout++; } else { if ((ValIndex < ValIndexMax) && (dataIn[indexin].flags != Fpidend)) { dataOut[ValRec].val[ValIndex] = dataIn[indexin].data & 0xff; dataOut[ValRec].valindex = ValIndex; dataOut[FirstValRec].vallength= ValCount + 1; crc16_calc = crc16_byte(crc16_calc, dataIn[indexin].data & 0xff); crc16_calc_history[ValCount % 3] = crc16_calc; dataOut[FirstValRec].crc16_calc = crc16_calc_history[(ValCount + 1) % 3]; dataOut[FirstValRec].crc16 >>= 8; dataOut[FirstValRec].crc16 |= (dataIn[indexin].data << 8) & 0xff00; ValIndex++; ValCount++; if (dataIn[indexin].validh) { if (ValIndex < ValIndexMax) { dataOut[ValRec].val[ValIndex] = (dataIn[indexin].data>>8) & 0xff; dataOut[ValRec].valindex = ValIndex; dataOut[FirstValRec].vallength= ValCount + 1; crc16_calc = crc16_byte(crc16_calc, (dataIn[indexin].data>>8) & 0xff); crc16_calc_history[ValCount % 3] = crc16_calc; dataOut[FirstValRec].crc16_calc = crc16_calc_history[(ValCount + 1) % 3]; dataOut[FirstValRec].crc16 >>= 8; dataOut[FirstValRec].crc16 |= dataIn[indexin].data & 0xff00; ValIndex++; ValCount++; } else { if (USBdata == 1) { dataOut[indexout].time = dataIn[indexin].time; dataOut[indexout].flags = Fdata; dataOut[indexout].valstart = 1; dataOut[indexout].data = dataIn[indexin].data; dataOut[indexout].validh = dataIn[indexin].validh; ValIndex = 0; ValRec = indexout; dataOut[ValRec].valcount = ValCount; dataOut[ValRec].val[ValIndex] = (dataIn[indexin].data>>8) & 0xff; dataOut[ValRec].valindex = ValIndex; dataOut[FirstValRec].vallength= ValCount + 1; crc16_calc = crc16_byte(crc16_calc, (dataIn[indexin].data>>8) & 0xff); crc16_calc_history[ValCount % 3] = crc16_calc; dataOut[FirstValRec].crc16_calc = crc16_calc_history[(ValCount + 1) % 3]; dataOut[FirstValRec].crc16 >>= 8; dataOut[FirstValRec].crc16 |= dataIn[indexin].data & 0xff00; ValIndex++; ValCount++; indexout++; } } } } } } return indexout; } /* display Addr message*/ static void ADDR_msg (protoContext context, protoPtr pdata) { data1P printData; printData=(data1P)pdata; PROTO_Printf(context, " A:%02X EP:%1X CRC5:%02X", printData->val[0] & 0x7f , ((printData->val[0]>>7) & 0x1)|( printData->val[1]<<1) & 0xE, (printData->val[1]>>3) & 0x1F); } /* display Data message*/ static void DATA_msg (protoContext context, protoPtr pdata) { unsigned int i; data1P printData; printData=(data1P)pdata; PROTO_Printf(context, "0x%03X ", printData->valcount); for (i=0;i<=printData->valindex;i++) PROTO_Printf(context, "%02X ", printData->val[i]); } /*********** Displays one entry of layer 2 information in the ListProtocol window. context: needed by higher software layers, only pdata: points to a record entry generated by PROTO_Process2() localdata: pointer to variable block global within protojtag.c; in protojtag.c only used to store "initialstate" which is a parameter read by the parser ***********/ static void PROTOAPI display2(protoContext context, protoPtr pdata, protoPtr localdata) { data1P printData; printData=(data1P)pdata; if ((printData->flags) & Fpidstart) { switch ((printData->data) & 0xff) { case RESVD: PROTO_Printf(context, "RESVD"); break; case USBOUT: PROTO_Printf(context, "OUT "); ADDR_msg (context, printData); break; case ACK: PROTO_Printf(context, "ACK "); break; case DATA0: PROTO_Printf(context, "DATA0"); PROTO_Printf(context, " DATA-Length: 0x%03X CRC16:%04X %04X", printData->vallength - 2, printData->crc16, printData->crc16_calc ^ 0xffff); PROTO_Control(context, PROTO_CONTROL_LINEFEED); PROTO_Printf(context, " "); DATA_msg (context, printData); break; case PING: PROTO_Printf(context, "PING "); ADDR_msg (context, printData); break; case SOF: PROTO_Printf(context, "SOF Frame: %03X CRC5:%02X ", (printData->val[0] & 0xff)|(( printData->val[1]<<8) & 0x700), (printData->val[1]>>3) & 0x1F); PROTO_Control(context, PROTO_CONTROL_LINETILLEND); break; case NYET: PROTO_Printf(context, "NYET "); break; case DATA2: PROTO_Printf(context, "DATA2"); PROTO_Printf(context, " DATA-Length: 0x%03X CRC16:%04X %04X", printData->vallength - 2, printData->crc16, printData->crc16_calc ^ 0xffff); PROTO_Control(context, PROTO_CONTROL_LINEFEED); PROTO_Printf(context, " "); DATA_msg (context, printData); break; case SPLIT: PROTO_Printf(context, "SPLIT"); break; case USBIN: PROTO_Printf(context, "IN "); ADDR_msg (context, printData); break; case NAK: PROTO_Printf(context, "NAK "); break; case DATA1: PROTO_Printf(context, "DATA1"); PROTO_Printf(context, " DATA-Length: 0x%03X CRC16:%04X %04X", printData->vallength - 2, printData->crc16, printData->crc16_calc ^ 0xffff); PROTO_Control(context, PROTO_CONTROL_LINEFEED); PROTO_Printf(context, " "); DATA_msg (context, printData); break; case ERR: PROTO_Printf(context, "ERR/PRE"); break; case SETUP: PROTO_Printf(context, "SETUP "); ADDR_msg (context, printData); break; case STALL: PROTO_Printf(context, "STALL"); break; case MDATA: PROTO_Printf(context, "MDATA"); PROTO_Printf(context, " DATA-Length: 0x%03X CRC16:%04X %04X", printData->vallength - 2, printData->crc16, printData->crc16_calc ^ 0xffff); PROTO_Control(context, PROTO_CONTROL_LINEFEED); PROTO_Printf(context, " "); DATA_msg (context, printData); break; default: PROTO_Printf(context, "PID: !! PID-ERROR !! %02X", (unsigned long) printData->data); break; } } if (printData->valstart) { PROTO_Printf(context, " "); DATA_msg (context, printData); } } const char * PROTO_Chart(protoContext context, protoPtr pdata, protoPtr localdata) { data1T *printData = (data1T *) pdata; // data1P printData; static const char *pids[] = {"RESVD", "OUT", "ACK", "DATA0", "PING", "SOF", "NYET", "DATA2", "SPLIT", "IN", "NAK", "DATA1", "ERR/PRE", "SETUP", "STALL", "MDATA", "idle", "DATA"}; // printData=(data1P)pdata; if ((printData->flags)==Fpidstart) { switch ((printData->data) & 0xff) { case RESVD: return pids[0]; break; case USBOUT: return pids[1]; break; case ACK: return pids[2]; break; case DATA0: return pids[3]; break; case PING: return pids[4]; break; case SOF: return pids[5]; break; case NYET: return pids[6]; break; case DATA2: return pids[7]; break; case SPLIT: return pids[8]; break; case USBIN: return pids[9]; break; case NAK: return pids[10]; break; case DATA1: return pids[11]; break; case ERR: return pids[12]; break; case SETUP: return pids[13]; break; case STALL: return pids[14]; break; case MDATA: return pids[15]; break; default: return pids[16]; } } else { if ((printData->flags)==Fdata) return pids[17]; else return pids[16]; } } /* static int PROTO_Find(protoContext context, protoPtr pdata, protoPtr localdata) { UsbLocalData *ptr = (UsbLocalData *) localdata; data1P *data = (data1P *) pdata; return ((((data1T *) pdata)->pidstart==1) && (((data1T *) pdata)->data & 0x0f) == ((UsbLocalData *) localdata)->findpid); } */ /************************************************************************** Protocol Parser **************************************************************************/ int PROTOAPI PROTO_Init(protoContext context, int command) { UsbLocalData *ptr; ptr = (UsbLocalData *) PROTO_Alloc(context, sizeof(UsbLocalData)); /* PROTO_Parse(context, (protoPtr) 0, "", PROTO_PARSE_CHANNEL); */ PROTO_Parse(context, (protoPtr) 0, "", PROTO_PARSE_CHANNEL); PROTO_Parse(context, (protoPtr) 0, "", PROTO_PARSE_CHANNEL); PROTO_Parse(context, (protoPtr) 0, "", PROTO_PARSE_CHANNEL); PROTO_Parse(context, (protoPtr) 0, "", PROTO_PARSE_CHANNEL); PROTO_Parse(context, (protoPtr) 0, "", PROTO_PARSE_CHANNEL); PROTO_Parse(context, (protoPtr) 0, "", PROTO_PARSE_CHANNEL); PROTO_Parse(context, (protoPtr) 0, "", PROTO_PARSE_CHANNEL); PROTO_Parse(context, (protoPtr) 0, "", PROTO_PARSE_CHANNEL); PROTO_Parse(context, (protoPtr) 0, "", PROTO_PARSE_CHANNEL); PROTO_Parse(context, (protoPtr) 0, "", PROTO_PARSE_CHANNEL); PROTO_Parse(context, (protoPtr) 0, "", PROTO_PARSE_CHANNEL); PROTO_Parse(context, (protoPtr) 0, "", PROTO_PARSE_CHANNEL); PROTO_Parse(context, (protoPtr) 0, "", PROTO_PARSE_CHANNEL); PROTO_Parse(context, (protoPtr) 0, "", PROTO_PARSE_CHANNEL); PROTO_Parse(context, (protoPtr) 0, "", PROTO_PARSE_CHANNEL); PROTO_Parse(context, (protoPtr) 0, "", PROTO_PARSE_CHANNEL); PROTO_Parse(context, (protoPtr) 0, "", PROTO_PARSE_CHANNEL); PROTO_Parse(context, (protoPtr) 0, "", PROTO_PARSE_CHANNEL); PROTO_Parse(context, (protoPtr) 0, "", PROTO_PARSE_CHANNEL); PROTO_Parse(context, (protoPtr) 0, "", PROTO_PARSE_CHANNEL); PROTO_Parse(context, (protoPtr) 0, "", PROTO_PARSE_CHANNEL); PROTO_Parse(context, (protoPtr) 0, "", PROTO_PARSE_CHANNEL); PROTO_Parse(context, (protoPtr) 0, "", PROTO_PARSE_CHANNEL); PROTO_Parse(context, (protoPtr) 0, "", PROTO_PARSE_CHANNEL); /* if (command == PROTO_COMMAND_FIND) { pid = -1; PROTO_Parse(context, (protoPtr) &pid, "OUT,ACK,DATA0,PING,SOF,NYET,DATA2,SPLIT,IN,NAK,DATA1,ERR,SETUP,STALL,MDATA", PROTO_PARSE_SELECTION); ptr->findpid = pid; } */ /* Register callback functions for the implemented stages */ PROTO_RegisterProcessCallback(context, process1, (protoPtr) ptr, sizeof(data1T), 1); PROTO_RegisterDisplayCallback(context, display1, (protoPtr) ptr, 1); PROTO_RegisterProcessCallback(context, process2, (protoPtr) ptr, sizeof(data1T), 2); PROTO_RegisterDisplayCallback(context, display2, (protoPtr) ptr, 2); PROTO_RegisterChartCallback(context, PROTO_Chart, (protoPtr) ptr, 1); /* PROTO_RegisterFindCallback(context, PROTO_Find, (protoPtr) ptr, 1); */ /* Default display is: Stage Two data */ PROTO_SetDefaultLevel(context, 2); return PROTO_OK; }