📄 usb.c
字号:
#include <string.h>
#include "..\inc\usb\def9603.h"
#include "..\inc\usb\defusb.h" /*include USB defines*/
#include "..\inc\usb\bitops.h"
#include "..\inc\usb\macro.h"
#include "..\inc\usb\desc.h"
#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\usb.h"
#include "..\inc\file.h"
U8 BIOS_BIN[] ={'B','I','O','S',' ',' ',' ',' ','B','I','N'};
U8 File2Sdram=0;
extern unsigned char *downPt;
//extern U32 current_block;
/*store vendor command and its data */
U8 cmd_buf[64], cmd_sze;
/*Contain data transfered to host */
U8 tx_buf[16*1024];
/*Contain data output by host */
U8 rx_buf[16*1024];
U8 USB_DATA[64];
U8 usb_status = WaitForComm;
int n_size; //used for transmit
int fsize; //the current file size in byte
int n_block;//n个 Block/Cluster
int total;
U8 desc_typ,desc_idx;
U16 desc_sze;
U8 rcount3; /*cnt of bytes in RX FIFO3*/
U8 usb_cfg; /*usb config. setting */
U8 evnt,rxstat,txstat; /*usb status temp storage */
U8 suscntr; /*suspend timeout counter */
U8 setaddr; /*SET_ADDRESS state var. */
/*USB related buffers and variables *****************************/
typedef union{
struct
{
U8 High;
U8 Low;
}ByteValue;
U16 WordValue;
}UByteWord; /*16bit 无符号数联合体*/
typedef struct { /*定义Setup Data的数据结构*/
U8 bmRequestType;
U8 bRequest;
UByteWord wValue;
UByteWord wIndex;
UByteWord wLength;
}SetupData_Struct;
SetupData_Struct SetupData; /*Setup Data数据存放于data区*/
U8 status;
#define UPLOAD BIT1 /*set when upload datas */
#define DOWNLOAD BIT2 /*set when download datas */
#define READROOT BIT3 /*set when host listening */
#define READMEM BIT4 /*set for multi-packet ops*/
#define GETDESC BIT5 /*set for get_descr. req. */
#define SENDCMD BIT6 /*set for send_cmd. req. */
#define READSTA BIT7 /*set when doing usb cmd */
/*sequence is underway */
U8 stalld;
#define stalld_bit0 BIT0
#define stalld_bit1 BIT1
#define stalld_bit2 BIT2
#define stalld_bit3 BIT3
#define stalld_bit4 BIT4
#define stalld_bit5 BIT5
#define stalld_bit6 BIT6
#define stalld_bit7 BIT7
U8 dtapid; /* PID related status */
#define TGL0PID BIT0 /* tracks NEXT data PID for endpoint0*/
#define TGL1PID BIT1 /* tracks NEXT data PID for endpoint1*/
#define TGL2PID BIT2 /* tracks NEXT data PID for endpoint2*/
#define TGL3PID BIT3 /* tracks NEXT data PID for endpoint3*/
/******************************/
/* 读USBN9603指定寄存器中的数据 */
/*****************************/
unsigned char read_usb(unsigned char adr)
{
*( (volatile U8 *) (IO_9603 + 0x2) ) = adr; /*write to address reg */
return(*( (volatile U8 *) IO_9603 )); /*return the reg data */
}
/*********************************/
/* 将数据写到USBN9603的指定寄存器中 */
/*********************************/
void write_usb(unsigned char adr,unsigned char dat)
{
*( (volatile U8 *) (IO_9603 + 0x2) ) = adr; /*write to address reg */
*( (volatile U8 *) IO_9603 ) = dat; /*write the reg data */
}
/********************************/
/* 置USBN9603指定寄存器中某个位 */
/********************************/
void bitset(unsigned char adr,unsigned char mask)
{
write_usb(adr,(read_usb(adr)|(mask)));
}
/**********************************/
/* 清USBN9603指定寄存器中的某个位 */
/**********************************/
void bitclr(unsigned char adr,unsigned char mask)
{
write_usb(adr,(read_usb(adr)&(~mask)));
}
void deep_sleep(void)
{}
/**********************************************************************/
/* This subroutine loads a byte from a descriptor or report into the */
/* endpoint 0 fifo. */
/**********************************************************************/
void mlti_pkt(void)
{
unsigned char pckt_dta;
if(desc_sze>0)
{
if(TSTBIT(status,GETDESC))
{
switch (desc_typ) /*descriptor **************/
{
case DEVICE:
pckt_dta=DEV_DESC[desc_idx];
break;
case CONFIGURATION:
pckt_dta=CFG_DESC[desc_idx];
break;
case XSTRING:
pckt_dta=STR_DATA[desc_idx];
break;
case HID:
pckt_dta=CFG_DESC[desc_idx];
break;
case HIDREPORT:
pckt_dta=ReportDescriptor[desc_idx];
break;
default: /*UNDEFINED */
pckt_dta=0;
break;
}
}
else
{
switch (desc_typ) /*report ******************/
{
default: /*UNDEFINED */
pckt_dta=0;
break;
}
}
write_usb(TXD0,pckt_dta); /*send data to the FIFO */
desc_sze--; /*decriment size */
desc_idx++; /*increment index */
}
else if(cmd_sze < 64) //send status
{
if(TSTBIT(status,READSTA))
{
pckt_dta = cmd_buf[cmd_sze];
write_usb(TXD0,pckt_dta); /*send data to the FIFO */
cmd_sze++;
}
}
}
/************************/
/* 处理 GET_STATUS 请求 */
/************************/
/*以下是Get Status的叙述:
This request returns the status for the specified recipient.
---------------------------------------------------------------
|bmRequestType| bRequest |wValue| wIndex |wLength| Data |
|=============+==========+======+=========+=======+===========|
| 10000000B |GET_STATUS| Zero | Zero | Two |Device, |
| 10000001B | | |Interface| |Interface, |
| 10000010B | | | Endpoint| |or Endpoint|
| | | | | |Status |
---------------------------------------------------------------
The Recipient bits of the bmRequestType field specify the
desired recipient.The data return is the current status of the
specified recipient.
If wValue or wLength are not as specified above, or if wIndex
is non-zero for a device status request,then the behavior of the
device is not specified.
If an interface or an endpoint is specified that does not exist
then the device responds with a Request Error.
A GetStatus() request to a device return the information shown in following table:
-----------------------------------------------
| D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0 |
|-----------------------------+------+--------|
| Reserved(Reset to zero) |Remote| Self |
| |Wakeup|Powered |
|=============================+======+========|
|D15 |D14 |D13 |D12 |D11 |D10 | D9 | D8 |
|---------------------------------------------|
| Reserved(Reset to zero) |
-----------------------------------------------
The Self Powered field indicates whether the device is currently self-powered.If D0 is
reset to zero,the device is bus-powered.If D0 is set to one,the device is self-powered.The
Self Power field may not be changed by the SetFeature() or ClearFeature() requests.
The Remote Wakeup field indicates whether the device is currently enable to request remote
wakeup.The default mode for devices that support remote wakeup is disable.If D1 is reset to zero,
the ability of the device to signal remote wakeup is disable.If D1 is set to one,the ability of
the device is enable.The Remote Wakeup field can be modified by the SetFeature() and
ClearFeaturn() requests using the DEVICE_REMOTE_WAKEUP feature selector.This field is reset to
zero when the device is reset.
A GetStatus() request to an interface return the information shown in following table:
-----------------------------------------
| D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0 |
|-----------------------------+----+----|
| Reserved(Reset to zero) |
|=======================================|
|D15 |D14 |D13 |D12 |D11 |D10 | D9 | D8 |
|---------------------------------------|
| Reserved(Reset to zero) |
-----------------------------------------
A GetStatus() request to an endpoint return the information shown in following table:
-----------------------------------------
| D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0 |
|----------------------------------+----|
| Reserved(Reset to zero) |Halt|
|=======================================|
|D15 |D14 |D13 |D12 |D11 |D10 | D9 | D8 |
|---------------------------------------|
| Reserved(Reset to zero) |
-----------------------------------------
The Halt feature is requested to be implemented for all interrupt and bulk endpoint types.
If the endpoint is current halted,then the Halt feature is set to one.Otherwise,the Halt feature
is reset to zero.The Halt feature may optionally be set with the SetFeature(ENDPOINT_HALT) request.
When set by the SetFeature() request,the endpoint exhibits the same stall behavior as if the field
had been set by a hardware condition.If the condition causing a halt has been removed,clearing
the Halt feature via a ClearFeature(ENDPOINT_HALT) request results in the endpoint no longer returning
a STALL.For endpoint using data toggle,regardless of whether an endpoint has the Halt feature set,a
ClearFeature(ENDPOINT_HALT) request always results in the data toggle being reinitialized to DATA0.
The Halt feature is reset to zero after either a SetConfiguration() or SetInterface() request
even if the requested configuration or interface is the same as the current configuration or interface.
It is neither required nor recommended that the Halt feature be implemented for the Default
Control Pipe.However,devices may set the Halt feature of the Default Control Pipe in order to reflect
a function error condition.If the feature is set to one,the device will return STALL in the Data and
Status stages of each standard request to the pipe except GetStatus(), SetFeature(),and ClearFeature()
requests.The device need not return STALL for class-specific requests.
*/
void getstatus(void)//OK
{
switch (SetupData.bmRequestType&0x03) /*find request target */
{
case 0: /*DEVICE */
write_usb(TXD0,0); /*first byte is reserved */
break;
case 1: /*INTERFACE */
write_usb(TXD0,0); /*first byte is reserved */
break;
case 2: /*ENDPOINT */
switch (SetupData.wIndex.ByteValue.Low&0x0F) /*find specific endpoint */
{ /* (strip off dir. bit) */
case 0: EPSTATUS(BIT0);
case 1: EPSTATUS(BIT1);
case 2: EPSTATUS(BIT2);
case 3: EPSTATUS(BIT3);
case 4: EPSTATUS(BIT4);
case 5: EPSTATUS(BIT5);
case 6: EPSTATUS(BIT6);
default:break;
}
break;
default: /*UNDEFINED */
write_usb(TXD0,0); /*first byte is reserved */
break;
}
write_usb(TXD0,0); /*second byte is reserved */
}
/***************************/
/* 处理 CLEAR_FEATURE 请求 */
/***************************/
/*以下是Clear Feature的叙述:
This request is used to clear or disable a specific feature.
Feature selector values in wValue must be appropriate to the
recipient.Only device feature selector values may be used when
the recipient is a device, only interface feature selector values
may be used when the recipient ia an interface,and only endpoint
feature selector value may be use when the reciptipient is an
endpoint.
Refer to following table for a definition of which feature
selector values are defined for which recipientd.
--------------------------------------
|Feature Selector |Recipient|Value|
|====================+=========+=====|
|DEVICE_REMOTE_WAKEUP|Device | 1 |
|--------------------+---------+-----|
|ENDPOINT_HALT |Endpoint | 0 |
--------------------------------------
A ClearFeature() request that references a feature that cannot
be clear, that does not exit,or that refernces an interface or
endpoint that does not exist will cause the device to respond with
a Request Error.
*/
void clrfeature(void)//OK
{
switch (SetupData.bmRequestType&0x03) /*find request target */
{
case 0: /*DEVICE */
break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -