USB-BDI
M:/USB-BDI/USB-BDI/Firmware/MPC555-USB-BDI/src/dispatch.c
Go to the documentation of this file.
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 "dispatch.h"
00027 #include "BDI_555.h"
00028 #include "uart.h"
00030 unsigned short uart0InBufferLen;
00032 unsigned short doNothing;
00033 
00041 void initPacket(unsigned char mainType, unsigned char subType, unsigned short dataLength)
00042 {
00043         usbInBuffer[inBufIndex] = PACKET_HEADER;                                        // header
00044         usbInBuffer[inBufIndex + 1] = mainType;                                         // type field
00045         usbInBuffer[inBufIndex + 2] = subType;
00046         usbInBuffer[inBufIndex + 3] = MSB(dataLength);                          // length
00047         usbInBuffer[inBufIndex + 4] = LSB(dataLength);
00048         usbInBuffer[inBufIndex + PACKET_DATA_OFFSET + dataLength] = PACKET_END; // packet end
00049 }
00050 
00056 void dispatch(unsigned short dataLength)
00057 {
00058         unsigned char error;
00059         unsigned short index;
00060         switch (usbOutBuffer[outBufIndex + 1]) {
00061                 case MTYPE_BDI_555:
00062                 switch (usbOutBuffer[outBufIndex + 2]) {
00063                                 case STYPE_BDI_555_35IN:
00064                                         BDI_555_35();
00065                                         // send packet
00066                                         initPacket(MTYPE_BDI_555, STYPE_BDI_555_35OUT, BDI_DATA35_LENGTH);
00067                                         sendPacket(BDI_DATA35_LENGTH);
00068                                         break;
00069                                 case STYPE_BDI_555_10IN:
00070                                         BDI_555_10();
00071                                         // send packet
00072                                         initPacket(MTYPE_BDI_555, STYPE_BDI_555_10OUT, BDI_DATA10_LENGTH);
00073                                         sendPacket(BDI_DATA10_LENGTH);
00074                                         break;
00075                                 case STYPE_BDI_555_HARD_RESET:
00076                                         BDI_555_hard_reset();
00077                                         // send packet
00078                                         initPacket(MTYPE_BDI_555, STYPE_BDI_HARD_RESET_SUCCESS, 0);
00079                                         sendPacket(0);
00080                                         break;
00081                                 case STYPE_BDI_GET_FREEZE:
00082                                         BDI_get_freeze();
00083                                         // send packet
00084                                         initPacket(MTYPE_BDI_555, STYPE_BDI_FREEZE_RESULT, 1);
00085                                         sendPacket(1);
00086                                         break;
00087                                 case STYPE_BDI_555_FD_DATA:
00088                                         error = FALSE; index = 0;
00089                                         // check if invalid length
00090                                         if (dataLength < BDI_DATA35_LENGTH) {
00091                                                 // send packet
00092                                                 initPacket(MTYPE_BDI_555, STYPE_BDI_ERROR_FD_LENGTH, 0);
00093                                                 sendPacket(0);
00094                                                 break;
00095                                         }
00096                                         while((index < dataLength) && !error) {
00097                                                 // the inBuffer gets overwritten (if no error and not finished)
00098                                                 BDI_555_35();
00099                                                 // check if we received an error condition (first two bits)
00100 
00101                                                 if ((usbInBuffer[inBufIndex + PACKET_DATA_OFFSET] & BDI_35STATUS_MASK) != BDI_35STATUS_NULL) {
00102                                                         error = TRUE;
00103                                                 }
00104                                                 index += BDI_DATA35_LENGTH;
00105                                                 outBufIndex += BDI_DATA35_LENGTH;
00106                                         }
00107                                         // send success or error
00108                                         if (error) {
00109                                                 initPacket(MTYPE_BDI_555, STYPE_BDI_ERROR_FD_GENERAL, BDI_DATA35_LENGTH);
00110                                                 sendPacket(BDI_DATA35_LENGTH);
00111                                         } else {
00112                                                 initPacket(MTYPE_BDI_555, STYPE_BDI_SUCCESS_FD, 0);
00113                                                 sendPacket(0);
00114                                         }
00115                                         break;
00116                                 default:        // ERROR: Unknown STYPE_BDI
00117                                         initPacket(MTYPE_BDI_555, STYPE_BDI_UNKNOWN_STYPE, 0);
00118                                         sendPacket(0);
00119                                         break;
00120                         }
00121                         break;
00122                 case MTYPE_UART:
00123                         //this part isn't handled anymore but shoudn't send an error
00124                         doNothing = 0;
00125                 default:        // ERROR: Unknown MTYPE
00126                         initPacket(MTYPE_ERROR, STYPE_ERROR_UNKNOWN_MTYPE, 0);
00127                         sendPacket(0);
00128                         break;
00129         }
00130 }
 All Files Functions Variables Defines