📄 usbsetup.c
字号:
/*************************************************************************/
/* */
/* Copyright Shenzhen Watertek S.&T. Co.,Ltd 2002 */
/* All Rights Reserved. */
/* */
/* THIS WORK CONTAINS TRADE SECRET AND PROPRIETARY INFORMATION WHICH IS */
/* THE PROPERTY OF MENTOR GRAPHICS CORPORATION OR ITS LICENSORS AND IS */
/* SUBJECT TO LICENSE TERMS. */
/* */
/*************************************************************************/
/*************************************************************************/
/* */
/* FILE NAME VERSION */
/* */
/* usbsetup.c S3c2410 USB device 1.0*/
/* */
/* */
/* DESCRIPTION */
/* */
/* This file contains data structure definitions , constants */
/* and manipulation facilitiesused in 2410 ep0(setup) */
/* AUTHOR */
/* Zhi-gang yang shenzhen watertek */
/* */
/* DATA STRUCTURES */
/* None */
/* */
/* FUNCTIONS */
/* */
/* PrepareEp1Fifo prepare ep1 FIFO and setup */
/* ep1 already, if token IN */
/* arrive, data send to host */
/* Ep1Handler ep1 interrupt handler */
/* PrintEpiPkt print ep1 packet,only for */
/* debug */
/* */
/* DEPENDENCIES */
/* usbmain.h */
/* usb.h */
/* usblib.h */
/* usbsetup.h */
/* option.h */
/* string.h */
/* 2410addr.h */
/* 2410usb.h */
/* 2410lib.h */
/* def.h */
/* HISTORY */
/* */
/* DATE REMARKS */
/* */
/* 2005-1-8 Created initial version 1.0 */
/* 2005-1-27 modified by yangzg */
/*************************************************************************/
#include <string.h>
#include "option.h"
#include "2410addr.h"
#include "def.h"
#include "2410usb.h"
#include "usbmain.h"
#include "usb.h"
#include "usblib.h"
#include "usbsetup.h"
// *** End point information ***
// EP0: control
// EP1: bulk in end point
// EP2: not used
// EP3: bulk out end point
// EP4: not used
// *** VERY IMPORTANT NOTE ***
// Every descriptor size of EP0 should be 8n+m(m=1~7).
// Otherwise, USB will not operate normally because the program
// doesn't prepare the case that the descriptor size is 8n+0.
// If the size of a descriptor is 8n, the 0 length packit should be sent.
// Special thanks to E.S.Choi for reminding me of this USB specification.
// ===================================================================
// All following commands will operate only in case
// - ep0_csr is valid.
// ===================================================================
//clear ep0 out packet ready
#define CLR_EP0_OUT_PKT_RDY() rEP0_CSR=( ep0_csr & (~EP0_WR_BITS)| \
EP0_SERVICED_OUT_PKT_RDY )
//clear ep0 packet 0
#define CLR_EP0_OUTPKTRDY_DATAEND() rEP0_CSR=( ep0_csr & (~EP0_WR_BITS)| \
(EP0_SERVICED_OUT_PKT_RDY|EP0_DATA_END) )
//set ep0 IN packet ready
#define SET_EP0_IN_PKT_RDY() rEP0_CSR=( ep0_csr & (~EP0_WR_BITS)| \
(EP0_IN_PKT_READY) )
//send packet 0
#define SET_EP0_INPKTRDY_DATAEND() rEP0_CSR=( ep0_csr & (~EP0_WR_BITS)| \
(EP0_IN_PKT_READY|EP0_DATA_END) )
//clear ep0 setup end tag
#define CLR_EP0_SETUP_END() rEP0_CSR=( ep0_csr & (~EP0_WR_BITS)| \
(EP0_SERVICED_SETUP_END) )
//clear ep0 sent stall tag(host ---->mcu)
#define CLR_EP0_SENT_STALL() rEP0_CSR=( ep0_csr & (~EP0_WR_BITS)& \
(~EP0_SENT_STALL) )
//flush ep0 fifo
#define FLUSH_EP0_FIFO() {while(rOUT_FIFO_CNT1_REG)rEP0_FIFO;}
U32 ep0State;
U32 ep0SubState;
extern volatile int isUsbdSetConfiguration;
volatile U8 Rwuen;
volatile U8 Configuration=1;
volatile U8 AlterSetting;
volatile U8 Selfpwr=TRUE;
volatile U8 device_status;
volatile U8 interface_status;
volatile U8 endpoint0_status;
volatile U8 endpoint1_status;
volatile U8 endpoint3_status;
struct USB_SETUP_DATA descSetup;
struct USB_DEVICE_DESCRIPTOR descDev;
struct USB_CONFIGURATION_DESCRIPTOR descConf;
struct USB_INTERFACE_DESCRIPTOR descIf;
struct USB_ENDPOINT_DESCRIPTOR descEndpt0;
struct USB_ENDPOINT_DESCRIPTOR descEndpt1;
struct USB_CONFIGURATION_SET ConfigSet;
struct USB_INTERFACE_GET InterfaceGet;
struct USB_GET_STATUS StatusGet; //={0,0,0,0,0};
static const U8 descStr0[]={
4,STRING_TYPE,LANGID_US_L,LANGID_US_H, //codes representing languages
};
static const U8 descStr1[]={ //Manufacturer
(0x14+2),STRING_TYPE,
'S',0x0,'y',0x0,'s',0x0,'t',0x0,'e',0x0,'m',0x0,' ',0x0,'M',0x0,
'C',0x0,'U',0x0,
};
static const U8 descStr2[]={ //Product
(0x2a+2),STRING_TYPE,
'S',0x0,'E',0x0,'C',0x0,' ',0x0,'S',0x0,'3',0x0,'C',0x0,'2',0x0,
'4',0x0,'1',0x0,'0',0x0,'X',0x0,' ',0x0,'T',0x0,'e',0x0,'s',0x0,
't',0x0,' ',0x0,'B',0x0,'/',0x0,'D',0x0
};
/*************************************************************************/
/* */
/* FUNCTION */
/* */
/* Ep0Handler */
/* */
/* DESCRIPTION */
/* */
/* Ep0 interrupt handler */
/* */
/* AUTHOR */
/* */
/* Zhi-gang Yang shenzhen watertek */
/* */
/* INPUTS */
/* None */
/* */
/* OUTPUTS */
/* */
/* None */
/* */
/* HISTORY */
/* */
/* DATE REMARKS */
/* */
/* 2004-12-28 Created initial version 1.0 */
/*************************************************************************/
void Ep0Handler(void)
{
static int ep0SubState;
int i;
U8 ep0_csr;
rINDEX_REG=0;
ep0_csr=rEP0_CSR;
//DATAEND interrupt(ep0_csr==0x0) will be ignored
//because ep0State==EP0_STATE_INIT when the DATAEND interrupt is issued.
if(ep0_csr & EP0_SETUP_END) //setup_end is set
{
// Host may end GET_DESCRIPTOR operation without completing the IN data stage.
// If host does that, SETUP_END bit will be set.
// OUT_PKT_RDY has to be also cleared because status stage sets OUT_PKT_RDY to 1.
CLR_EP0_SETUP_END();
if(ep0_csr & EP0_OUT_PKT_READY) //OUT_PKT_RDY is set
{
FLUSH_EP0_FIFO();
CLR_EP0_OUT_PKT_RDY();
}
ep0State=EP0_STATE_INIT;
return;
}
if(ep0_csr & EP0_SENT_STALL) //SENT_STALL is set
{
CLR_EP0_SENT_STALL();
if(ep0_csr & EP0_OUT_PKT_READY)
{
CLR_EP0_OUT_PKT_RDY();
}
ep0State=EP0_STATE_INIT;
return;
}
if((ep0_csr & EP0_OUT_PKT_READY) && (ep0State==EP0_STATE_INIT)) //deal with setup packet
{
RdPktEp0((U8 *)&descSetup,EP0_PKT_SIZE);
switch(descSetup.bRequest)
{
case GET_DESCRIPTOR: //handle GET_DESCRIPTOR request
switch(descSetup.bValueH)
{
case DEVICE_TYPE: //device descriptor
CLR_EP0_OUT_PKT_RDY();
ep0State=EP0_STATE_GD_DEV_0;
break;
case CONFIGURATION_TYPE: //configuration descriptor
CLR_EP0_OUT_PKT_RDY();
if((descSetup.bLengthL+(descSetup.bLengthH<<8))>0x9)
//bLengthH should be used for bLength=0x209 at WIN2K.
ep0State=EP0_STATE_GD_CFG_0; //for WIN98,WIN2K
else
ep0State=EP0_STATE_GD_CFG_ONLY_0; //for WIN2K
break;
case STRING_TYPE: //string descriptor
CLR_EP0_OUT_PKT_RDY();
switch(descSetup.bValueL)
{
case 0:
ep0State=EP0_STATE_GD_STR_I0;
break;
case 1:
ep0State=EP0_STATE_GD_STR_I1;
break;
case 2:
ep0State=EP0_STATE_GD_STR_I2;
break;
default:
break;
}
ep0SubState=0;
break;
case INTERFACE_TYPE: //interface descriptor
CLR_EP0_OUT_PKT_RDY();
ep0State=EP0_STATE_GD_IF_ONLY_0; //for WIN98
break;
case ENDPOINT_TYPE: //endpoint descriptor
CLR_EP0_OUT_PKT_RDY();
switch(descSetup.bValueL&0xf)
{
case 0:
ep0State=EP0_STATE_GD_EP0_ONLY_0;
break;
case 1:
ep0State=EP0_STATE_GD_EP1_ONLY_0;
break;
default:
break;
}
break;
default:
break;
}
break;
case SET_ADDRESS: //set address request
rFUNC_ADDR_REG=descSetup.bValueL | 0x80;
CLR_EP0_OUTPKTRDY_DATAEND(); //Because of no data control transfers.
ep0State=EP0_STATE_INIT;
break;
case SET_CONFIGURATION: //set configuration request
ConfigSet.ConfigurationValue=descSetup.bValueL;
CLR_EP0_OUTPKTRDY_DATAEND(); //Because of no data control transfers.
ep0State=EP0_STATE_INIT;
isUsbdSetConfiguration=1;
break;
//////////////////////// For chapter 9 test ////////////////////
case CLEAR_FEATURE: //clear configuration request
switch (descSetup.bmRequestType)
{
case DEVICE_RECIPIENT: //device speciality
if (descSetup.bValueL == 1)
Rwuen = FALSE;
break;
case ENDPOINT_RECIPIENT: //endpoint speciality
if (descSetup.bValueL == 0)
{
if((descSetup.bIndexL & 0x7f) == 0x00){
StatusGet.Endpoint0= 0;
}
if((descSetup.bIndexL & 0x8f) == 0x81){ // IN Endpoint 1
StatusGet.Endpoint1= 0;
}
if((descSetup.bIndexL & 0x8f) == 0x03){ // OUT Endpoint 3
StatusGet.Endpoint3= 0;
}
}
break;
default:
break;
}
CLR_EP0_OUTPKTRDY_DATAEND();
ep0State=EP0_STATE_INIT;
break;
case GET_CONFIGURATION: //Get configuration request
CLR_EP0_OUT_PKT_RDY();
ep0State=EP0_CONFIG_SET;
break;
case GET_INTERFACE: //Get interface request
CLR_EP0_OUT_PKT_RDY();
ep0State=EP0_INTERFACE_GET;
break;
case GET_STATUS: //Get status
switch(descSetup.bmRequestType)
{
case (0x80): //get device status
CLR_EP0_OUT_PKT_RDY();
StatusGet.Device=((U8)Rwuen<<1)|(U8)Selfpwr;
ep0State=EP0_GET_STATUS0;
break;
case (0x81): //get interface status
CLR_EP0_OUT_PKT_RDY();
StatusGet.Interface=0;
ep0State=EP0_GET_STATUS1;
break;
case (0x82): //get endpoint
CLR_EP0_OUT_PKT_RDY();
if((descSetup.bIndexL & 0x7f) == 0x00){ //get ep0 status
ep0State=EP0_GET_STATUS2;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -