USB-BDI
|
00001 /* USB-BDI Used for programming and debugging a MPC555 over USB 00002 * 00003 * Copyright 2005 NTB Interstate University of Applied Sciences of Technology Buchs 00004 * 00005 * This program is free software: you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License as published by 00007 * the Free Software Foundation, either version 3 of the License, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 */ 00015 00024 #include "usbpdi_common.h" 00025 #include "usbpdi_main.h" 00026 #include "delay.h" 00027 #include "TP3465regs.h" 00028 00030 volatile __bit exceptionOnTimeout; 00032 volatile unsigned char exceptionCount; 00034 unsigned short outBufIndex; // 00036 unsigned short inBufIndex; // 00037 00038 00045 void initTP3465(){ 00046 // init TP3465 00047 PD = 0xFF; // set all CS pins to inputs 00048 SKP = 0x00; // set SK polarity to normal mode 00049 MWM = 0xFF; // set 16 bit operation 00050 SKR = 0x02; // inten = 0, soi = 0, ms = 0, div = 0x2 (12Mhz/4=3Mhz) 00051 } 00052 00058 void initBDI(){ 00059 // disable (reset) timeouts 00060 exceptionOnTimeout = FALSE; 00061 // configure end points 00062 EP2CFG = bmVALID | bmBULK | bmDOUBLEBUF; // 512 double bulk OUT 00063 SYNCDELAY; 00064 EP6CFG = bmVALID | bmBULK | bmDOUBLEBUF | bmIN; // 512 double bulk IN 00065 SYNCDELAY; 00066 00067 // reset FIFOs 00068 FIFORESET = bmNAKALL; 00069 SYNCDELAY; 00070 FIFORESET = 2; 00071 SYNCDELAY; 00072 FIFORESET = 6; 00073 SYNCDELAY; 00074 00075 // since the fifos are double buffered we must write dummy byte counts twice 00076 // NOTE: ONLY write 0x80, other values don't work! 00077 // This is wrong in the TRM! 00078 EP2BCL = 0x80; // arm EP2OUT by writing byte count w/skip. Endpoint 2 Byte Count Low Registers 00079 SYNCDELAY; 00080 EP2BCL = 0x80; 00081 SYNCDELAY; 00082 } 00087 void exceptionOnTimeoutOn(){ 00088 exceptionCount = 0; 00089 exceptionOnTimeout = TRUE; 00090 } 00095 void exceptionOnTimeoutOff(){ 00096 exceptionOnTimeout = FALSE; 00097 } 00102 void BDI_get_freeze(){ 00103 usbInBuffer[inBufIndex + PACKET_DATA_OFFSET] = FREEZE; 00104 } 00112 void sendPacket(unsigned short dataLength){ 00113 EP6BCH = MSB(dataLength + PACKET_MIN_LENGTH); 00114 SYNCDELAY; 00115 EP6BCL = LSB(dataLength + PACKET_MIN_LENGTH); // arm EP6IN 00116 SYNCDELAY; 00117 inBufIndex = 0; // reset inBufIndex 00118 } 00123 void handelBDIPackages(){ 00124 __bit finished; 00125 00126 unsigned short length, outBufferCount;//, uart0OutBufferLen, i; 00127 // if there is new data in EP2FIFOBUF and EP6IN is not full then process data 00128 if(!(EP2468STAT & bmEP2EMPTY)){ // if true packages from host are available 00129 if(!(EP2468STAT & bmEP6FULL)){ //Tests if Endpoint 6 Buffer is not full if true = data can be sent to Host 00130 outBufferCount = (EP2BCH << 8) + EP2BCL; // get length of data that are send from the host 00131 outBufIndex = 0; // reset outBufIndex 00132 finished = FALSE; 00133 // process data 00134 while (!finished) { 00135 // check header bytes 00136 if ((usbOutBuffer[outBufIndex] == PACKET_HEADER)) { // Test if the rigth header code was sent if not send a error 00137 length = usbOutBuffer[outBufIndex + 3] * 0x100 + usbOutBuffer[outBufIndex + 4]; 00138 if (usbOutBuffer[outBufIndex + 5 + length] == PACKET_END) { // success test if the right length was found 00139 dispatch(length); 00140 outBufIndex += length + PACKET_MIN_LENGTH; 00141 if (outBufIndex >= outBufferCount) { //Test if all packages were worked thru else do loop again 00142 finished = TRUE; 00143 } 00144 } else { // fail send error msg 00145 initPacket(MTYPE_ERROR, STYPE_ERROR_PACKET_END, 0); 00146 // send packet 00147 sendPacket(0); 00148 finished = TRUE; 00149 } 00150 } else { // fail send a error msg 00151 initPacket(MTYPE_ERROR, STYPE_ERROR_HEADER, 0); 00152 // send packet 00153 sendPacket(0); 00154 finished = TRUE; 00155 } 00156 } 00157 // rearm for new packet 00158 EP2BCL = 0x80; // re(arm) EP2OUT 00159 SYNCDELAY; 00160 } 00161 } 00162 }