USB-BDI
M:/USB-BDI/USB-BDI/Firmware/MPC555-USB-BDI/src/virtual_com_port.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 
00023 #include "virtual_com_port.h"
00024 #include "BDI.h"
00025 #include "usbpdi_common.h"
00026 #include "uart.h"
00027 
00029 unsigned long bitRate;
00031 unsigned char stopBit;
00033 unsigned char parityBit;
00035 unsigned char dataBits;
00037 unsigned short stopTime;
00039 unsigned char rts;
00041 unsigned char dtr;
00043 unsigned short nrOfBytesInEp8Fifo;
00044 
00045 
00052 void initVirtualComPort(void){
00053         //set default UART values
00054         bitRate = 9600;
00055         stopBit = 0 ;//0 = 1Stopbit, 1 = 1.5Stopbit, 2 = 2Stopbit
00056         parityBit = 0 ;//0 = None, 1 = Odd, 2 = Even, 3 = Mark , 4 = Space
00057         dataBits = 8;
00058         stopTime = 0; //in msec
00059         rts = 0; //RTS de-assert
00060         dtr = 0; //DTS de-assert
00061 
00062         //configure Endpoints
00063         EP1INCFG = bmVALID | bmBULK;    //activate Endpoint 1 IN and set at bulk
00064         SYNCDELAY;
00065         EP4CFG = bmVALID | bmBULK | bmIN;       // activate Endpoint 4 and set as bulk IN with 512 bytes buffer
00066         SYNCDELAY;
00067         EP8CFG = bmVALID | bmBULK ;     // activate Endpoint 8 and set as bulk OUT with 512 bytes buffer
00068         SYNCDELAY;
00069 
00070         //configure FIFO's
00071         EP8FIFOCFG = EP8FIFOCFG&0xEF;
00072 
00073         //disable Interrupts
00074         EP8FIFOIE = 0;
00075         EP4FIFOIE = 0;
00076 
00077         //reset Endpoint Fifos
00078         FIFORESET = bmNAKALL;
00079         SYNCDELAY;
00080         FIFORESET = 1;
00081         SYNCDELAY;
00082         FIFORESET = 4;
00083         SYNCDELAY;
00084         FIFORESET = 8;
00085         SYNCDELAY;
00086         EP1INBC = 0; //No Data in the FIFO
00087         EP8BCH = 0;  //No Data in the FIFO
00088         EP8BCL = 8;  //No Data in the FIFO
00089         EP4BCH = 0;  //No Data in the FIFO
00090         EP4BCL = 0;  //No Data in the FIFO
00091 }
00092 
00093 
00098 void handelPackegesOnComEp(void){
00099 
00100 }
00101 
00107 void handelPackegesOnBulkInEp(void){
00108 
00109         if(!(EP2468STAT & bmEP4FULL)){ //send message only if endpoint is not full
00110                 short uart0OutBufferLen = 0;
00111                 //disable UART0 interrupts
00112                 ES0 = 0;
00113 
00114                 uart0OutBufferLen = (UART_BUF_LEN + (uart0OutBufferEnd - uart0OutBufferStart))%UART_BUF_LEN;//calculate number of data in the UART0 Buffer
00115 
00116                 //calculate absolute value:
00117                 if (uart0OutBufferLen < 0){
00118                         uart0OutBufferLen = UART_BUF_LEN+uart0OutBufferLen;
00119                 }
00120 
00121                 if (uart0OutBufferLen > 0){ //if there are data available send them to host
00122                         int i = 0;
00123                         // copy data
00124                         for (i = 0; i < uart0OutBufferLen; i++){
00125                                 EP4FIFOBUF[i] = uart0OutBuffer[(uart0OutBufferStart + i) % UART_BUF_LEN];
00126                         }
00127                         //send data
00128                         SYNCDELAY;
00129                         EP4BCH = uart0OutBufferLen>>8;
00130                         SYNCDELAY;
00131                         EP4BCL = uart0OutBufferLen;
00132                         // adjust uart0OutBufferStart
00133                         uart0OutBufferStart = (uart0OutBufferStart + uart0OutBufferLen) % UART_BUF_LEN;
00134                 }
00135                 // enable uart0 interrupts
00136                 ES0 = 1;
00137         }
00138 
00139 }
00140 
00148 void handelPackegesOnBulkOutEp(void){
00149         short i=0;
00150         if(!(EP2468STAT & bmEP8EMPTY)){ //tests if data from the Host are available
00151                 nrOfBytesInEp8Fifo = 0;
00152                 nrOfBytesInEp8Fifo = (EP8BCH<<8) | EP8BCL;
00153                 if (nrOfBytesInEp8Fifo <= UART_BUF_LEN) { //Test if data fit into UART Buffer
00154                         int i = 0;
00155                         ES0 = 0;// disable uart0 interrupts
00156                         for (i = 0; i < nrOfBytesInEp8Fifo; i++) {// move data to UART buffer
00157                                         Uart0Insert(EP8FIFOBUF[i]);
00158                         }
00159                         Uart0TriggerSend();
00160                         // enable uart0 interrupts
00161                         ES0 = 1;
00162                 }
00163                 //skip OUT Data
00164                 SYNCDELAY;
00165                 EP8BCL = 0x80;
00166                 SYNCDELAY;
00167         }
00168 }
00169 
00175 void handelVirtualComPortPackages(void){
00176         handelPackegesOnComEp();
00177         handelPackegesOnBulkInEp();
00178         handelPackegesOnBulkOutEp();
00179 
00180 }
 All Files Functions Variables Defines