#include
//#include "..\inc\option.h"
//#include "..\inc\def.h"
//#include "..\inc\44b.h"
//#include "..\inc\44blib.h"
//#include "..\inc\flash.h"
//#include "..\inc\download.h"
/
//#include "..\inc\cache.h"
//#include "..\inc\lcd240.h"
//#include "..\inc\KeyBoard16.h"
//#include "..\inc\ata.h"
#include
//following is CPU test functions include
//#include "..\inc\cputest\adc.h"
//#include "..\inc\cputest\idle.h"
//#include "..\inc\cputest\iic.h"
//#include "..\inc\cputest\iis.h"
//#include "..\inc\cputest\nwait.h"
//#include "..\inc\cputest\power.h"
//#include "..\inc\cputest\rtc.h"
//#include "..\inc\cputest\sio.h"
//#include "..\inc\cputest\stop.h"
//#include "..\inc\cputest\timer.h"
//#include "..\inc\cputest\uart.h"
#include "main.h"
#include "d12ci.h"
#include "isr.h"
#include "chap9.h"
#include "usb100.h"
#include "..\fat16\fat16.h"
#include "..\bulk\bulk_only.h"
//*******************************
extern unsigned char *downPt;
extern unsigned int fileSize;
EPPFLAGS bEPPflags;
CONTROL_XFER ControlData;
//migrated for 51ok
extern unsigned long ClockTicks;
extern unsigned char GenEpBuf[];
extern IO_REQUEST ioRequest;
extern unsigned char ioSize, ioCount;
extern unsigned char MainEpBuf[];
extern unsigned char EpBuf[];
extern U8 Flash_One_Page[];
extern U8 Bulk_In_Buf[];
extern U32 Logical_Block_Address;
extern U32 nCurrentPage,nBeginPage;
extern U8 Flash_One_Page[];
//////////////////////////////////////////////
static void (*run)(void)=(void (*)(void))DOWNLOAD_ADDRESS;
U16 EthernetMac[3];
void (*StandardDeviceRequest[])(void) =
{
get_status,
clear_feature,
reserved,
set_feature,
reserved,
set_address,
get_descriptor,
reserved,
get_configuration,
set_configuration,
get_interface,
set_interface,
reserved,
reserved,
reserved,
reserved
};
void Isr_Init(void)
{
rINTCON=0x5; ////3(reserved) 2(V 0=vector 1= non vector) 1(I,0=Enable IRQ,1=Disable IRQ) 0(FIQ Related0=enable 1=diable)
// 1,Non-vectored,0,IRQ enable,1,FIQ disable //
// 1,1,1 0x7,disable interupt.
rINTMOD=0x0; //0=IRQ mode Set all of the INT to IRQ mode//
rI_ISPC=BIT_EINT2;
rINTMSK=BIT_GLOBAL;
//pISR_FIQ,pISR_IRQ must be initialized//
pISR_EINT2=(unsigned)usb_isr;
rINTMSK=~(BIT_EINT0 |BIT_EINT2 | ~BIT_GLOBAL);
//pISR_FIQ,pISR_IRQ must be initialized//
}
void reconnect_USB(void)
{
unsigned long clk_cnt;
MCU_LED0 = MCU_LED0_ON;
MCU_LED1 = MCU_LED1_ON;
D12SUSPD = D12SUSPD_ON;
//Disconnect Usb
D12_SetMode(D12_NOLAZYCLOCK, D12_SETTOONE | D12_CLOCK_12M);
Delay(500);
//Connect USB
DISABLE;
bEPPflags.value = 0;
ENABLE;
// V2.1 enable normal+sof interrupt
D12_SetDMA(D12_ENDP4INTENABLE | D12_ENDP5INTENABLE);
// Initialize D12 configuration
//D12_SetMode(D12_NOLAZYCLOCK ,D12_SETTOONE | D12_CLOCK_12M );
D12_SetMode(D12_ENDP_NONISO |D12_NOLAZYCLOCK | D12_SOFTCONNECT|D12_CLOCKRUNNING,D12_SETTOONE | D12_CLOCK_12M );
//End of Connect USB
MCU_LED0 = MCU_LED0_OFF;
MCU_LED1 = MCU_LED1_OFF;
}
void control_handler(void)
{
unsigned char type, req;
type = ControlData.DeviceRequest.bmRequestType & USB_REQUEST_TYPE_MASK; //0x60
req = ControlData.DeviceRequest.bRequest & USB_REQUEST_MASK; //0x0F
//help_devreq(type, req); // print out device request
if (type == USB_STANDARD_REQUEST)
(*StandardDeviceRequest[req])();
else{
stall_ep0();
}
}
void single_transmit(unsigned char * buf, unsigned char len)
{
if( len D12_WriteEndpoint(1, len,buf);
}
}
void code_transmit(unsigned char * pRomData, unsigned short len)
{
ControlData.wCount = 0;
if(ControlData.wLength > len)
ControlData.wLength = len;
ControlData.pData = pRomData;
if( ControlData.wLength >= EP0_PACKET_SIZE) {
D12_WriteEndpoint(1,EP0_PACKET_SIZE, ControlData.pData);
ControlData.wCount = ControlData.wCount + EP0_PACKET_SIZE;
// Uart_Printf("len=%d,wCount=%d\n",len,ControlData.wCount);
DISABLE;
bEPPflags.bits.control_state = USB_TRANSMIT;
ENABLE;
}
else {
D12_WriteEndpoint(1, ControlData.wLength,pRomData);
ControlData.wCount = ControlData.wCount + ControlData.wLength;
// Uart_Printf("else*code_transmit,len=%d,wCount=%d\n",len,ControlData.wCount);
DISABLE;
bEPPflags.bits.control_state = USB_IDLE;
ENABLE;
}
// Uart_Printf("bEPPflags.bits.control_state=%d\n",bEPPflags.bits.control_state);
}
void stall_ep0(void)
{
D12_SetEndpointStatus(0, 1);
D12_SetEndpointStatus(1, 1);
}
void Main(void)
{
int i;
rSYSCFG=CACHECFG; // Using 8KB Cache//
Uart_Init(0,115200);
Port_Init();
Isr_Init();
Delay(0);
downPt=(unsigned char *)DOWNLOAD_ADDRESS;
Flash_Reset();
Uart_Select(0); //Select UART0//
rINTMSK=~(BIT_EINT0 |BIT_EINT2| BIT_GLOBAL);
// Format_Fat16Media();//将Flash格式化成Fat16
// for(i=0;i // Write2Flash(0,4,14,Flash_One_Page);
Uart_Printf("Hi, This is LiWei!\n");
reconnect_USB();
// D12 usb mainloop program
while( TRUE ){
if (bEPPflags.bits.setup_packet){
DISABLE;
bEPPflags.bits.setup_packet = 0;
ENABLE;
control_handler();
//D12SUSPD = 1;
D12SUSPD=D12SUSPD_ON;
} // if setup_packet
}
}