//#include"usb_drv.h"
#include "config.h"
#include "soucehead.h"
#include "USB.h"
#define CFG_NUM 32
volatile char GenEpBuf;
//EPPFLAGS bEPPflags;/* Control endpoint TX/RX buffers */
//extern CONTROL_XFER ControlData;/* ISR static vars */
//unsigned char idata GenEpBuf[EP1_PACKET_SIZE];
//IO_REQUEST idata ioRequest;
//unsigned long ClockTicks = 0;
extern void Flash_page_read(unsigned long int flash_page_addr);
extern void Flash_page_program(unsigned long int flash_page_addr);
extern unsigned char Data_buffer[0x200];
extern void Screen_clear(void);
int x=0,y=0;
extern void Delay100ns(long int dly);
extern void Delayms(long int dly);
extern void Screen_clear();
extern void Display_hex(unsigned char hex,unsigned int window_start_x,unsigned int window_start_y,unsigned long int color,char times_x,char times_y);
extern volatile unsigned char USBA0L;
extern volatile unsigned char USBA0H;
extern unsigned int LCD_x_size,LCD_y_size;
extern void Display_string(char *string,unsigned int window_start_x,unsigned int window_start_y,unsigned long int color,char times_x,char times_y);
extern void Flash_block_erase(unsigned int flash_block_addr);
extern void Flash_page_program(unsigned long int flash_page_addr);
unsigned char inquirydata[36]=
{0,0x80,02,02,
0x1f,0,0,0,
0x6b,0x70,0x6d,0x20,
0x20,0x20,0x20,0x20,
0x6b,0x70,0x6d,0x20,
0x20,0x20,0x20,0x20,
0x20,0x20,0x20,0x20,
0x20,0x20,0x20,0x20,
0x20,0x20,0x20,0x20,
};
unsigned char csw[64],cbw[31];
const char cap[8]={0,0x01,0xff,0xff,0x00,0x00,0x02,0x00};
const char fcap[12]={0,0,0,8,0,2,0,0,2,0,2,0};
const char mode[12]={0x0b,0,0,8,0,2,0,0,0,0,2,0};
/*
inquirydata[0]=0x00;inquirydata[1]=0x80;inquirydata[2]=0x02;inquirydata[3]=0x02;
inquirydata[4]=0x1f;inquirydata[5]=inquirydata[6]=inquirydata[7]=0;
inquirydata[8]=0x6B;inquirydata[9]=0x70;inquirydata[10]=0x6D;inquirydata[11]=0x20;
inquirydata[12]=0x20;inquirydata[13]=0x20;inquirydata[14]=0x20;inquirydata[15]=0x20;
inquirydata[16]=0x6B;inquirydata[17]=0x70;inquirydata[18]=0x6D;inquirydata[19]=0x20;
inquirydata[20]=0x20;inquirydata[21]=0x20;inquirydata[22]=0x20;inquirydata[23]=0x20;
inquirydata[24]=0x20;inquirydata[25]=0x20;inquirydata[26]=0x20;inquirydata[27]=0x20;
inquirydata[28]=0x20;inquirydata[29]=0x20;inquirydata[30]=0x20;inquirydata[31]=0x20;
inquirydata[32]=0x20;inquirydata[33]=0x20;inquirydata[34]=0x20;inquirydata[35]=0x20; */
extern volatile unsigned char receive_buffer[0x8000];
unsigned long int count=0,count1=0;
unsigned long int block_addr,block_addr_old;
unsigned long int trans_start_page,flash_page_start;
unsigned int trans_num;
unsigned char USB;
void Read_Flash_PAG0()
{
Flash_page_read(0);
/*unsigned int i;
for(i=0;i<512;i++)
Data_buffer[i]=i&0xff;
*/
}
void Outport_command(unsigned char val)
{
USBA0H=val;
Delay100ns(5);
}
void Outport_data(unsigned char val)
{
USBA0L=val;
Delay100ns(5);
}
unsigned char Inport_data(void)
{
unsigned char c;
c=USBA0L;
Delay100ns(5);
return c;
}
void D12_SetAddressEnable(unsigned char bAddress, unsigned char bEnable)
{
if(bEPPflags.bits.in_isr == 0) DISABLE;
Outport_command( 0xD0);
if(bEnable) bAddress |= 0x80;
Outport_data( bAddress);
if(bEPPflags.bits.in_isr == 0) ENABLE;
}
void D12_SetEndpointEnable(unsigned char bEnable)
{
if(bEPPflags.bits.in_isr == 0) DISABLE;
Outport_command( 0xD8);
if(bEnable) Outport_data( 1);
else Outport_data( 0);
if(bEPPflags.bits.in_isr == 0) ENABLE;
}
void D12_SetMode(unsigned char bConfig, unsigned char bClkDiv)
{
if(bEPPflags.bits.in_isr == 0) DISABLE;
Outport_command( 0xF3);
Outport_data( bConfig);
Outport_data( bClkDiv);
if(bEPPflags.bits.in_isr == 0) ENABLE;
}
void D12_SetDMA(unsigned char bMode)
{
if(bEPPflags.bits.in_isr == 0) DISABLE;
Outport_command( 0xFB);
Outport_data( bMode);
if(bEPPflags.bits.in_isr == 0) ENABLE;
}
unsigned short D12_ReadInterruptRegister(void)
{
unsigned char b1;
unsigned int j;
Outport_command( 0xF4);
b1 = Inport_data();Delay100ns(1);
j = Inport_data();
j <<= 8;
j += b1;
return j;
}
unsigned char D12_SelectEndpoint(unsigned char bEndp)
{
unsigned char c;
if(bEPPflags.bits.in_isr == 0) DISABLE;
Outport_command( bEndp);
c = Inport_data();
if(bEPPflags.bits.in_isr == 0) ENABLE;
return c;
}
unsigned char D12_ReadLastTransactionStatus(unsigned char bEndp)
{
Outport_command( 0x40 + bEndp);
return Inport_data();
}
unsigned char D12_ReadEndpointStatus(unsigned char bEndp)
{
unsigned char c;
if(bEPPflags.bits.in_isr == 0) DISABLE;
Outport_command( 0x80 + bEndp);
c = Inport_data();
if(bEPPflags.bits.in_isr == 0) ENABLE;
return c;
}
void D12_SetEndpointStatus(unsigned char bEndp, unsigned char bStalled)
{
if(bEPPflags.bits.in_isr == 0) DISABLE;
Outport_command( 0x40 + bEndp);
Outport_data( bStalled);
if(bEPPflags.bits.in_isr == 0) ENABLE;
}
void D12_SendResume(void)
{
Outport_command( 0xF6);
}
unsigned short D12_ReadCurrentFrameNumber(void)
{
unsigned short i,j;
if(bEPPflags.bits.in_isr == 0) DISABLE;
Outport_command( 0xF5);
i= Inport_data();
j = Inport_data();
i += (j<<8);
if(bEPPflags.bits.in_isr == 0) ENABLE;
return i;
}
unsigned short D12_ReadChipID(void)
{
unsigned short i,j;
if(bEPPflags.bits.in_isr == 0) DISABLE;
Outport_command( 0xFD);
i=Inport_data();
j=Inport_data();
i += (j<<8);
if(bEPPflags.bits.in_isr == 0)ENABLE;
return i;
}
unsigned char D12_ReadEndpoint(unsigned char endp, unsigned char * buf, unsigned char len)
{
unsigned char i, j;
if(bEPPflags.bits.in_isr == 0) DISABLE;
Outport_command( endp);
if((Inport_data() & D12_FULLEMPTY) == 0)
{
if(bEPPflags.bits.in_isr == 0) ENABLE;
return 0;
}
Outport_command( 0xF0);
j = Inport_data();
j = Inport_data();
if(j > len) j = len;
for(i=0; i<j; i++)
*(buf+i) = Inport_data();
Outport_command( 0xF2);
if(bEPPflags.bits.in_isr == 0) ENABLE;
return j;
}
// D12_ReadMainEndpoint() added by V2_2 to support double-buffering_
// Caller should assume maxium 128 bytes of returned data_
unsigned char D12_ReadMainEndpoint(unsigned char * buf)
{
unsigned char i, j, k = 0, bDblBuf = 1;
if(bEPPflags.bits.in_isr == 0) DISABLE;
Outport_command( 0x84);
if( (Inport_data() & 0x60) == 0x60) bDblBuf = 2;
while(bDblBuf)
{
Outport_command( 4);
if((Inport_data() & D12_FULLEMPTY) == 0) break;
Outport_command( 0xF0);
j = Inport_data();
j = Inport_data();
for(i=0; i<j; i++)
*(buf+i+k) = Inport_data();
k += j;
Outport_command( 0xF2);
bDblBuf --;
}
if(bEPPflags.bits.in_isr == 0) ENABLE;
return k;
}
unsigned char D12_WriteEndpoint(unsigned char endp, unsigned char * buf, unsigned char len)
{
unsigned char i;
if(bEPPflags.bits.in_isr == 0) DISABLE;
Outport_command(endp);
Inport_data();
Outport_command( 0xF0);
Outport_data( 0);
Outport_data( len);
for(i=0; i<len; i++)
Outport_data( *(buf+i));
Outport_command( 0xFA);
if(bEPPflags.bits.in_isr == 0)
ENABLE;
return len;
}
void D12_AcknowledgeEndpoint(unsigned char endp)
{
Outport_command( endp);
Outport_command( 0xF1);
if(endp == 0) Outport_command( 0xF2);
}
const char DeviceDescr[] =//USB_DEVICE_DESCRIPTOR
{
0x12,
0x01,
0x10,0x01,
0x00 ,
0, 0,
0x10,
0x71,0x04,//SWAP(0x0471),
0x28,0x02,//SWAP(0x0222),
0x00,0x01,//SWAP(0x0100),
0, 0, 0,
1
};
const char ConfigDescr[] =//USB_CONFIGURATION_DESCRIPTOR
{
9,
02,
CFG_NUM,0,
1,
1,
0,
0x80,
0x32,
9,
0x04,
0,
0,
2,
8,
0x06,
0x50,
0,
7,
0x05,
0x81,
2,
64,0,
01,
6,
0x05,
0x02,
2,
64,0,
1,
};
void suspend_change(void)
{
}
void stall_ep0(void)
{
D12_SetEndpointStatus(0, 1);
D12_SetEndpointStatus(1, 1);
}
void single_transmit(unsigned char * buf, unsigned char len)
{
if( len <= E