?? usb.c
字號:
/****************************************************************************
【文 件 名 稱】usb.c
【功 能 描 述】深圳英蓓特S3EV40開發板USB程序代碼
【程 序 版 本】1.0
【創建人及創建日期】
【修改人及修改日期】
****************************************************************************/
#include "reg9603.h"
#include "usb9603.h"
#include "44b.h"
#include "44blib.h"
#include "option.h"
#define U32 unsigned int
#define U16 unsigned short
#define S32 int
#define S16 short int
#define U8 unsigned char
#define S8 char
#define TRUE 1
#define FALSE 0
static volatile int COMfront[3];
static volatile int COMtail[3];
static U8 COMbuf[3][68];
#define USB_DEFAULT 0 /* No descriptor read*/
#define USB_DESCRIPTOR_READ 1 /* Descriptor read, Addr not assigned*/
#define USB_ADDRESS_ASSIGNED 2 /* Descriptor Read, Addr assigned*/
#define USB_CONFIGURED 3
/*GLOBAL STATUS VALUES*/
#define STD_COMMAND 0x00
#define SETUP_COMMAND_PHASE 0x40
#define FUNCTION_ERROR 0x7F /* Used when we are stalling the function EP0 */
#define HUB_ERROR 0xFF /* Used when we are stalling the HUB EP0 */
/*標準設備請求 bRequest Values*/
#define GET_STATUS 0
#define CLEAR_FEATURE 1
#define SET_FEATURE 3
#define SET_ADDRESS 5
#define GET_DESCRIPTOR 6
#define SET_DESCRIPTOR 7
#define GET_CONFIGURATION 8
#define SET_CONFIGURATION 9
#define GET_INTERFACE 10
#define SET_INTERFACE 11
#define SYNCH_FRAME 12
#define REQ_DONE 0xFF /*private code: request done*/
/*Descriptor Types*/
#define DEVICE 1
#define CONFIGURATION 2
#define XSTRING 3
#define INTERFACE 4
#define ENDPOINT 5
/*Recipient Selectors/Masks*/
#define RECIPIENT_MASK 0x1F
#define DEVICE_RECIPIENT 0
#define INTERFACE_RECIPIENT 1
#define ENDPOINT_RECIPIENT 2
#define OTHER_RECIPIENT 3
/*Feature Selectors*/
#define DEVICE_REMOTE_WAKEUP 0x01
#define ENDPOINT_STALL 0x00
/* Flush and disable the USB TX0 **************************************/
#define FLUSHTX0 {write_usb(TXC0,FLUSH);}
/* Flush and disable the USB TX1 **************************************/
#define FLUSHTX1 {write_usb(TXC1,FLUSH);}
/* Flush and disable the USB TX2 **************************************/
#define FLUSHTX2 {write_usb(TXC2,FLUSH);}
/* Flush and disable the USB TX3 **************************************/
#define FLUSHTX3 {write_usb(TXC3,FLUSH);}
/* Flush and disable the USB RX0 **************************************/
#define FLUSHRX0 {write_usb(RXC0,FLUSH);}
/* Flush and disable the USB RX1 **************************************/
#define FLUSHRX1 {write_usb(RXC1,FLUSH);}
/* Flush and disable the USB RX2 **************************************/
#define FLUSHRX2 {write_usb(RXC2,FLUSH);}
/* Flush and disable the USB RX3 **************************************/
#define FLUSHRX3 {write_usb(RXC3,FLUSH);}
/* enable TX0, using the appropriate DATA PID *************************/
#define TXEN0_PID \
{ if(dtapid_TGL0PID) write_usb(TXC0,TX_TOGL+TX_EN+TX_LAST); /*DATA1*/\
else write_usb(TXC0,TX_EN); /*DATA0*/\
dtapid_TGL0PID=!dtapid_TGL0PID;}
/* enable TX1, using the appropriate DATA PID, but not toggling it ****/
#define TXEN1_PID_NO_TGL \
{ if(dtapid_TGL1PID) write_usb(TXC1,TX_TOGL+TX_LAST+TX_EN); \
else write_usb(TXC1,TX_LAST+TX_EN); /*DATA1*/\
}
/* enable TX2, using the appropriate DATA PID, but not toggling it ****/
#define TXEN2_PID_NO_TGL \
{ if(dtapid_TGL1PID) write_usb(TXC2,TX_TOGL+TX_LAST+TX_EN); \
else write_usb(TXC2,TX_LAST+TX_EN); /*DATA2*/\
}
/* enable TX3, using the appropriate DATA PID, but not toggling it ****/
#define TXEN3_PID_NO_TGL \
{ if(dtapid_TGL3PID) write_usb(TXC3,TX_TOGL+TX_LAST+TX_EN); \
else write_usb(TXC3,TX_LAST+TX_EN); /*DATA3*/\
}
/* enable TX1, using the appropriate DATA PID *************************/
#define TXEN1_PID \
{ TXEN1_PID_NO_TGL; \
dtapid_TGL1PID=!dtapid_TGL1PID;}
/* enable TX2, using the appropriate DATA PID *************************/
#define TXEN2_PID \
{ TXEN2_PID_NO_TGL; \
dtapid_TGL2PID=!dtapid_TGL2PID;}
/* enable TX3, using the appropriate DATA PID *************************/
#define TXEN3_PID \
{ TXEN3_PID_NO_TGL; \
dtapid_TGL3PID=!dtapid_TGL3PID;}
U8 evnt,usb_cfg,usb_buf[8],n;
U8 desc_typ,desc_idx=0,desc_sze,buf,txstat,rxstat;
int status_GETDESC,dtapid_TGL0PID,dtapid_TGL1PID,dtapid_TGL3PID;
int stall0,stall1,stall2,stall3,stall4,stall5,stall6;
int Flag;
#define DEV_DESC_SIZE 18
static U8 DEV_DESC[]={18, /*length of this desc. */
0x01, /*DEVICE descriptor */
0x00,0x01, /*spec rev level (BCD) */
0x00, /*device class */
0x00, /*device subclass */
0x00, /*device protocol */
0x08, /*max packet size */
0x00,0x04, /*vendor ID 賣主ID 0X0400代表國半公司*/
/*the VID can be modified as your wish*/
0x07,0x00, /*product ID */
0x63,0x00, /*revision ID */
0, /*index of manuf. string */
0, /*index of prod. string */
0, /*index of ser. # string */
0x01 /*number of configs. */
};
#define CFG_DESC_SIZE 60
static U8 CFG_DESC[] = {0x09, /*length of this desc. */
0x02, /*CONFIGURATION descriptor*/
0x3c,0x00, /*total length returned */
0x01, /*number of interfaces */
0x01, /*number of this config */
0x00, /*index of config. string */
0x40, /*attr.: self powered,remote wakeup*/
0x50, /*max power (100 mA) */
0x09, /*length of this desc. */
0x04, /*INTERFACE descriptor */
0x00, /*interface number */
0x00, /*alternate setting */
0x06, /*# of endpoints */
0x00, /*interface class */
0x00, /*interface subclass */
0x00, /*interface protocol */
0x00, /*index of intf. string */
/*The WDM driver for this board references the endpoints by 'pipe' */
/*number: 0, 1, 2 et al in order below: */
/*Pipe 0 */
0x07, /*length of this desc. */
0x05, /*ENDPOINT descriptor */
0x81, /*address (IN) */
0x02, /*attributes (bulk) */
0x20,0x00, /*max packet size (32) */
0x01, /*interval (ms) */
/*Pipe 1 */
0x07, /*length of this desc. */
0x05, /*ENDPOINT descriptor */
0x02, /*address (OUT) */
0x02, /*attributes (BULK) */
0x20,0x00, /*max packet size (32) */
0x01, /*interval (ms) */
/*Pipe 2 */
0x07, /*length of this desc. */
0x05, /*ENDPOINT descriptor */
0x83, /*address (IN) */
0x02, /*attributes (bulk) */
0x20,0x00, /*max packet size (32) */
0x01, /*interval (ms) */
/*Pipe 3 */
0x07, /*length of this desc. */
0x05, /*ENDPOINT descriptor */
0x04, /*address (OUT) */
0x02, /*attributes (BULK) */
0x20,0x00, /*max packet size (32) */
0x01, /*interval (ms) */
/*Pipe 4 */
0x07, /*length of this desc. */
0x05, /*ENDPOINT descriptor */
0x85, /*address (IN) */
0x02, /*attributes (BULK) */
0x40,0x00, /*max packet size (64) */
0x01, /*interval (ms) */
/*Pipe 5 */
0x07, /*length of this desc. */
0x05, /*ENDPOINT descriptor */
0x06, /*address (OUT) */
0x02, /*attributes (BULK) */
0x40,0x00, /*max packet size (64) */
0x01}; /*interval (ms) */
/***************************************************************************
【功能說明】處理USB中斷,配置設備,傳輸數據
****************************************************************************/
void Eint1Isr(void)
{
rI_ISPC = BIT_EINT1; /* 清中斷 */
evnt=read_usb(MAEV); /*check the events */
//Uart_Printf(0,"\n evnt=%x",evnt);
if(evnt&RX_EV)
{
evnt=read_usb(RXEV); /*check the RX events */
if (evnt&RXFIFO0)
rxevent_0();/*endpoint 0 */
else if(evnt&RXFIFO1)
rxevent_1();/*endpoint 2 */
else if(evnt&RXFIFO2)
rxevent_2();/*endpoint 4 */
else if(evnt&RXFIFO3)
rxevent_3();/*endpoint 6 */
}
else if(evnt&TX_EV)
{
evnt=read_usb(TXEV); /*check the TX events */
if (evnt&TXFIFO0)
txevent_0();/*endpoint 0 */
else if(evnt&TXFIFO1)
txevent_1();/*endpoint 1 */
else if(evnt&TXFIFO2)
txevent_2();/*endpoint 3 */
else if(evnt&TXFIFO3)
txevent_3();/*endpoint 5 */
}
else if(evnt&ALT)
usb_alt(); /*alternate event? */
/*NAKs can come so fast and furious (especially with OHCI hosts)*/
/*that they MUST have a lower priority than the other events. If*/
/*they did not, the other events could get starved out. */
else if(evnt&NAK)
{
evnt=read_usb(NAKEV); /*check the NAK events */
if (evnt&NAK_O0) nak0(); /*endpoint 0 */
//else if (evnt&NAK_O1) onak1(); /*endpoint 2 */
//else if (evnt&NAK_O2) onak2(); /*endpoint 4 */
//else if (evnt&NAK_I3) inak3(); /*endpoint 5 */
}
}
/***************************************************************************
【功能說明】中斷初始化
****************************************************************************/
void Isr_Init(void)
{
rINTMOD &= ~BIT_EINT1; // EINT1 interrupt
rINTMSK &= ~BIT_GLOBAL; //GLOBAL valid
pISR_EINT1 = (int)Eint1Isr;
rPCONG = rPCONG | (3 << 2); //GPG1設為中斷引腳
rPUPG = rPUPG & 0xFD; //GPG1上拉電阻有效
rEXTINT=0x00; //低電平觸發
rINTMSK=rINTMSK|BIT_EINT1; //關外部中斷
}
/***************************************************************************
【功能說明】初始化USBN9603
****************************************************************************/
void Init_9603(void)
{
status_GETDESC=0;
usb_cfg=0;
/*give a soft reset, then set ints to push pull, active hi or lo*/
write_usb(MCNTRL,SRST);
while(read_usb(MCNTRL) & SRST);
write_usb(MCNTRL,VGE+INT_H_P);
/*initialize the clock generator ********************************/
write_usb(CCONF,CODIS+0x0c);
/*set default address, enable EP0 only **************************/
write_usb(FAR,AD_EN+0); //FAR=FUNCTION ADDRESS REGISTER
write_usb(EPC0,0x00);
/*set up interrupt masks ****************************************/
write_usb(NAKMSK,NAK_O0); /*NAK evnts*/
write_usb(TXMSK,TXFIFO0+TXFIFO1+TXFIFO2+TXFIFO3); /*TX events*/
write_usb(RXMSK,RXFIFO0+RXFIFO1+RXFIFO2+RXFIFO3); /*RX events*/
write_usb(ALTMSK,SD3+RESET_A); /*ALT evnts*/
write_usb(MAMSK,INTR_E+RX_EV+NAK+TX_EV+ALT);
/*enable the receiver and go operational ************************/
FLUSHTX0; /*flush TX0 and disable */
write_usb(RXC0,RX_EN); /*enable the receiver */
write_usb(NFSR,OPR_ST); /*go operational */
write_usb(MCNTRL,VGE+INT_L_P+NAT); /*set NODE ATTACH */
delay(100);
}
/***************************************************************************
【功能說明】This subroutine handles RX events for FIFO0 (endpoint 0)
****************************************************************************/
void rxevent_0(void)
{
rxstat=read_usb(RXS0); /*get receiver status */
/*is this a setup packet? ***************************************/
if(rxstat&SETUP_R)
{
/*read data payload into buffer then flush/disble the RX*********/
for(desc_idx=0;desc_idx<8;desc_idx++)
usb_buf[desc_idx]=read_usb(RXD0);
FLUSHRX0; /*make sure the RX is off */
FLUSHTX0; /*make sure the TX is off */
bitclr(EPC0,STALL); /*turn off stall */
if((usb_buf[0]&0x60)==0x00) /*standard request */
switch(usb_buf[1]) /*decode request code */
{
case CLEAR_FEATURE: //1
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -