📄 usb.c
字号:
#include "head.h"
#include "sh7045.h"
//declare Function ////////////////////////////////////////////////////////////
void getstatus(void);
void setaddress(void);
void getdescriptor(void);
void get_desc(void);
void setconfiguration(void);
void delayus(uint num);
void delayms(uint num);
uchar read_9604(uint addr); //Read from 9604
void write_9604(uint addr,uchar wdata);//Write to 9604
void bitset(uint addr,uchar bitval);//Set 1 to 9604 registor's bit eg:bitset(MCNCTRL,0X20); 0X02=>00100000B
void bitclr(uint addr,uchar bitval);//Clear bit of 9604 registor eg:bitclr(MCNCTRL,0X20); 0X02=>11011111B
void TXEN0_PID(void); //Exchange the PID
interrupt void IntUSB(void);
//void usb_interrupt(void); //interrupt 0
//void t0(void); //T0
//void t1(void); //T1
void usb_alt(void);
void outnak_0(void);
void rx_0(void);
void tx_0(void);
void rx_3(void);
void tx_3(void);
void FLUSHTX0(void);
void FLUSHRX0(void);
void FLUSHTX3(void);
void FLUSHRX3(void);
void usb_icont(int no);
//define variable and array/////////////////////////////////////////////////
uchar GETDESC; //whole!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
uchar TGL0PID; //whole
uchar usbenumerating; //whole
uchar setconfigurationfinish; //whole
int originalindex; //whole
int desc_idx; //whole
uchar event;
uchar txstat;
uchar desc_sze; //whole
uchar desc_typ; //whole
uchar desc_dta;
uchar usb_cfg; //whole
uchar address; //whole
uchar bagman;
uchar multiple30ms; //whole
long t1count;
uchar iii;
uchar jjj;
uchar req_buf[8];
const uchar DEV_DESC[]=
{
0X12, //Length of Device Descriptor
0X01, //DEVICE
0X10,0X01, //USB1.1*
0X00,
0X00,
0X00,
0X08, //bMaxPacketSize(0)
0x00,0x04, //National's vendor ID
0x5B,0xC3, //National's product ID
0x40,0x01, //National's revision ID
0, //index of manuf. string
0,//4, //index of prod. string
0,//90, //index of ser. # string
0X01, //number of configs.
};
const uchar CONF_DESC[]=
{
0X09, //Length of Configration Descriptor
0X02, //CONFIGURATION
0x29,0x00, //Total Length 41
0X01, //Number Of Support Interface
0XFF, //Identifier
0, //index of config. string
0XC0, //self powered
0XFA, //Maxpower500 mA
/********************/
9, //Length of Interface Descriptor
0X04, //INTERFACE
1, //Interface number
0,
2, //Number of Endpoint
3, //HID Class
0,
0,
0, //index of interface
/******************/
0X09, //Length of HID Descriptor
0X21, //HID
0x10,0x01, //USB1.1 (BCD)
0x00,
0X01, //Support Descriptors of HID
0X22, //REPORT
0X1C,0X00, //Length of HID Descriptor 28
//endpoint 5
0X07, //Length of Endpoint Descriptor
0X05, //ENDPOINT
0x85, //IN Endpoint 5
0x03, //INTERRUPT)
0x40,0x00, //max packet size (64)
0x03, //access time 2ms
//endpoint 6
0X07,
0X05,
0x06,
0x03,
0x40,0x00,
0x03,
};
const uchar REPORT_DESC[]=
{
0X06,0X00,0XFF, /*USAGE_PAGE (Vendor Defined Page 1)*/
0X09,0X01, /*USAGE (Vendor Usage 1)*/
0XA1,0X01, /*COLLECTION (Application)*/
0X09,0X01, /*USAGE (Vendor Usage 1)*/
0X15,0X00, /*LOGICAL_MINIMUM (0)*/
0X25,0X7F, /*LOGICAL_MAXIMUM (127)*/
0X75,0X08, /*REPORT_SIZE (8)*/
0X95,0X40, /*REPORT_COUNT (64)*/
0X81,0X02, /*INPUT (Data,Var,Abs)*/
0X09,0X02, /*USAGE (Vendor Usage 2)*/
0X75,0X08, /*REPORT_SIZE (8)*/
0X95,0X40, /*REPORT_COUNT (64)*/
0X91,0X02, /*OUTPUT (Data,Var,Abs)*/
0XC0, /*END_COLLECTION*/
};
const uchar STRING_DESC[]=
{4,3,9,4,/*ID(English) */
/*004 Product string"Laser Parameter Testing Instrument Sec5000"*/
86,3, 76,0, 97,0, 115,0, 101,0, 114,0, 32,0, 80,0, /*Laser P*/
97,0, 114,0, 97,0, 109,0, 101,0, 116,0, 101,0, /*aramete*/
114,0, 32,0, 84,0, 101,0, 115,0, 116,0, 105,0, /*r Testi*/
110,0, 103,0, 32,0, 73,0, 110,0, 115,0, 116,0, /*ng Inst*/
114,0, 117,0, 109,0, 101,0, 110,0, 116,0, 32,0, /*rument */
83,0, 101,0, 99,0, 53,0, 48,0, 48,0, 48,0, /*Sec5000*/
/*90 Series Number"ZJ80000008"*/
34,3, 90,0, 74,0, 56,0, 48,0, 48,0, 48,0, 48,0, /*ZJ80000*/
48,0, 48,0, 48,0, 48,0, 48,0, 48,0, 48,0, /*0000000*/
48,0, 56,0, /*08*/
};
//usb_job/////////////////////////////////////////////////////////////
void usb_job(void)
{
/*if(usbenumerating)
{
ET1=0;
TR0=1;
}
else
{
ET1=1;
TR1=1;
}*/
}
//init9604////////////////////////////////////////////////////////////////
void init9604(void)
{
GETDESC=0;
TGL0PID=0;
usbenumerating=0;
setconfigurationfinish=0;
originalindex=0X00;
desc_idx=0X00;
event=0X00;
txstat=0X00;
desc_sze=0X00;
desc_typ=0X00;
desc_dta=0X00;
usb_cfg=0X00;
address=0X00;
////bagman=0X00;
multiple30ms=0X1E;
t1count=0;
//EA=0;
usb_icont(0);
usb_cfg=0;
GETDESC=0;
//P16=0; //enable 9604,remove reset
write_9604(MCNTRL,SRST);
delayus(10);
//write_9604(MCNTRL,VGE+NAT+0X40);
write_9604(MCNTRL,VGE+0XC0);
write_9604(CCONF,0X80);
write_9604(FAR,AD_EN);
write_9604(EPC0,0X00);
write_9604(NAKMSK,0X10);
write_9604(FWMSK,0X00);
write_9604(TXMSK,0X09);
write_9604(ALTMSK,0XD0);
write_9604(RXMSK,0X09);
write_9604(MAMSK,INTR+RX_EV+NAK+TX_EV+ALT+WARN);
write_9604(TXC0,0X00); //forbid transfer
write_9604(RXC0,0X09); //allow receive endpoint 0
write_9604(RXC3,0X0d); //allow receive endpoint 6
write_9604(NFSR,NFS_OPERATIONAL);
//write_9604(MCNTRL,0X4C);
write_9604(MCNTRL,VGE+NAT+0XC0);
usb_icont(3);
// EA=1;
}
//interrupt//////////////////////////////////////////////////////////
/*INT0*/
//void usb_interrupt(void) interrupt 0 using 1
interrupt void IntUSB(void)
{
uchar hostevent;
uchar subevent;
hostevent=0;
subevent=0;
usb_icont(0);
if(desc_typ==34)
iii++;
//EA=0;
hostevent=read_9604(MAEV);
if(hostevent&TX_EV)
{
subevent=read_9604(TXEV);
if(subevent&TXFIFO0)
tx_0();
if(subevent&TXFIFO3)
tx_3();
}
else if(hostevent&RX_EV)
{
subevent=read_9604(RXEV);
if(subevent&RXFIFO0)
rx_0();
if(subevent&RXFIFO3)
rx_3();
}
else if(hostevent&NAK)
{
subevent=read_9604(NAKEV);
if(subevent&OUTNAK0)
outnak_0();
}
else if(hostevent&ALT)
usb_alt();
usb_icont(3);
//EA=1;
}
//event/////////////////////////////////////////////////////////////////////////
/*rx0*/
void rx_0(void)
{
uchar rxstat;
uchar count;
//uchar bagman;
rxstat=0;
count=0;
bitclr(RXC0,RX_EN);
rxstat=read_9604(RXS0); /*gain status of endpoint 0*/
if(rxstat&RX_SETUP) /*SETUP packet*/
{
for(count=0;count<8;count++)
{
bagman = read_9604(RXD0);
req_buf[count] = bagman;
} /*send requre to req_buf[]*/
FLUSHRX0();
FLUSHTX0();
switch (req_buf[0]&0x60)
{
case 0x00: /*standar requre*/
switch (req_buf[1])
{
case GET_STATUS:
getstatus();
break;
case SET_ADDRESS:
setaddress();
break;
case GET_DESCRIPTOR:
getdescriptor();
if(desc_typ==34)
iii++;
break;
case GET_CONFIGURATION:
write_9604(TXD0,usb_cfg);
break;
case SET_CONFIGURATION:
setconfiguration();
break;
case GET_INTERFACE:
write_9604(TXD0,CONF_DESC[12]);
break;
default:
bitset(EPC0,STALL);
break; /*other standar requre*/
}
break;
case 0x20: /*Class requre*/
switch (req_buf[1])
{
default:
bitset(EPC0,STALL);
break; /*other Class requre*/
}
break;
default: bitset(EPC0,STALL);
break;
}
//if(desc_typ==34)
// write_9604(TXC0,TX_EN);
//else
write_9604(TXC0,TX_TOGGLE+TX_EN); /*enable and DATA1*/
//TX_EN can not be in GET function
TGL0PID=0; /*next PID*/
if(setconfigurationfinish) /*enumerate finish?*/
{
setconfigurationfinish=0;
usbenumerating=0;
}
}
else /*OUT Packet*/
{
if(desc_typ==34)
iii++;
if(GETDESC)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -