📄 usb.c
字号:
}
/*-----------------------------------------------------------------------------
ROUTINE NAME : ReadEP0
INPUT/OUTPUT : data pointer, byte number
DESCRIPTION : Read data in the RAM space of the USB DMA
-----------------------------------------------------------------------------*/
void ReadEP0(Byte *DataReceived)
{ int i;
Byte ByteNumber;
if (CurrentUsbbLength < MAXPACKETSIZE)
ByteNumber = CurrentUsbbLength;
else
ByteNumber = MAXPACKETSIZE;
for (i=0; i<ByteNumber; i++)
*(DataReceived+i) = Endpoint0.Rx[i];
CurrentUsbbLength -= ByteNumber;
CurrentDescAddPointer += ByteNumber;
}
/*-----------------------------------------------------------------------------
ROUTINE NAME : SetReport
INPUT/OUTPUT : None
DESCRIPTION : Set a report received from the host
-----------------------------------------------------------------------------*/
void SetReport(void)
{
#ifdef SET_REPORT
ReadEP0(&FeatureArray[0]);
bmRequest = bmCopyRequest;
bmCopyRequest = 0;
#endif
#ifndef SET_REPORT
SetEP0RxStatus(STALL); // Not supported
#endif
}
/*-----------------------------------------------------------------------------
ROUTINE NAME : Send0LengthData
INPUT/OUTPUT : None
DESCRIPTION : Send a zero length data packet on endpoint0
-----------------------------------------------------------------------------*/
void Send0LengthData(void)
{
SetEP0TxTbc(0);
SetEP0TxStatus(VALID);
}
/*-----------------------------------------------------------------------------
ROUTINE NAME : StatusIn
INPUT/OUTPUT : None
DESCRIPTION : Status Stage of a Control Write/No-data Control Transfers
-----------------------------------------------------------------------------*/
void StatusIn(void)
{
if (UsbbRequest == SET_ADDRESS)
UsbCtrStatus |= ADDRESS2SET;
Send0LengthData(); // ACK will be sent from Host if Transaction OK
}
/*-----------------------------------------------------------------------------
ROUTINE NAME : StatusOut
INPUT/OUTPUT : None
DESCRIPTION : Status Stage of a Control Read Transfer
-----------------------------------------------------------------------------*/
void StatusOut(void)
{
if (GetEP0StatusOut()) // Status OUT Stage completed
ClearEP0StatusOut(); // Clear ST_OUT bit of ENDP0_A
else
SetEP0StatusOut(); // Status OUT expected
SetEP0RxStatus(VALID); // Set Endpoint0 Rx Valid for next reception
}
/*-----------------------------------------------------------------------------
ROUTINE NAME : SetupStage
INPUT/OUTPUT : None
DESCRIPTION : Setup Stage of a Control Transfer
-----------------------------------------------------------------------------*/
void SetupStage(void)
{
UsbbmRequestType = Endpoint0.Rx[0]; // save current USB bmRequestType
UsbbRequest = Endpoint0.Rx[1]; // save current USB bRequest
UsbwValue[0] = Endpoint0.Rx[2]; // save current USB wValue
UsbwValue[1] = Endpoint0.Rx[3];
UsbwIndex[0] = Endpoint0.Rx[4]; // save current wIndex
UsbwIndex[1] = Endpoint0.Rx[5];
if ((Endpoint0.Rx[6] | Endpoint0.Rx[7]) == 0) // test if no Data Stage
{
UsbCtrStatus = NO_DATA_STAGE;
if ((UsbbmRequestType & TYPE) == STANDARD)
{ // Standard requests
if (UsbbRequest == CLEAR_FEATURE) // Requests without data stages
ClearFeature(); // are processed here.
else
if (UsbbRequest == SET_FEATURE)
SetFeature();
else
if (UsbbRequest == SET_ADDRESS)
SetAddress();
else
if (UsbbRequest == SET_INTERFACE)
SetInterface();
else
if (UsbbRequest == SET_CONFIGURATION)
{
CurrentUsbbLength = 0x1;
SetConfiguration();
}
}
else
if ((UsbbmRequestType & TYPE) == CLASS)
{// Class requests
if (UsbbRequest == SET_IDLE)
SetIdle();
else
if (UsbbRequest == SET_PROTOCOL)
SetProtocol();
}
/*
else
if ((UsbbmRequestType & TYPE) == VENDOR)
{// Vendor requests
}
*/
StatusIn();
}
else
{
//UsbCtrStatus = DATA_STAGE; // a Data must follow the Setup Stage
if ((UsbbmRequestType & TYPE) == STANDARD)
{ // Standard requests
if ((UsbbRequest == GET_DESCRIPTOR) || (UsbbRequest == SET_DESCRIPTOR))
{
if (UsbwValue[1] == DEVICE) // Test on Descriptor Type
{
if ((Endpoint0.Rx[6] < DEVDESCSIZE)&(Endpoint0.Rx[7] ==0))
CurrentUsbbLength = Endpoint0.Rx[6];
else CurrentUsbbLength = DEVDESCSIZE; // Device Descriptor
CurrentDescAddPointer = 0;
SetEP0RxStatus(VALID); // Endpoint0 Rx VALID for next data reception
}
else
if (UsbwValue[1] == STRING)
{
if ((Endpoint0.Rx[6] == 0xff)||(Endpoint0.Rx[7] != 0x00)) // MS
{
if (UsbwValue[0] == 0x00) // Get Language ID
{
CurrentUsbbLength = 4;
CurrentDescAddPointer = 0;
}
else
{
CurrentDescAddPointer = UsbwValue[0];
if (UsbwValue[0] == INDEX_MANUFACT) //Get Manufacturer String Descriptor
CurrentUsbbLength = LENGTH_MANUFACT;
if (UsbwValue[0] == INDEX_PRODUCT ) // Get Product String Descriptor
CurrentUsbbLength = LENGTH_PRODUCT;
if (UsbwValue[0] == INDEX_SERIALNUM ) // Get SerialNumber String Descriptor
CurrentUsbbLength = LENGTH_SERIALNUM;
AdditionalString();
}
}
else
{
CurrentDescAddPointer = UsbwValue[0];
CurrentUsbbLength = Endpoint0.Rx[6];
}
SetEP0RxStatus(VALID); // Endpoint0 Rx VALID for next data reception
}
else
if (UsbwValue[1] == CONFIGURATION)
{
if ((Endpoint0.Rx[6] < CONFDESCSIZE) & (Endpoint0.Rx[7] == 0)) // Configuration Descriptor
CurrentUsbbLength = Endpoint0.Rx[6];
else
CurrentUsbbLength = CONFDESCSIZE; // Configuration+Interface+HID+Endpoint Descriptor
CurrentDescAddPointer = 0;
}
else
if (UsbwValue[1] == HID)
{
if ((Endpoint0.Rx[6] < HIDDESCSIZE) & (Endpoint0.Rx[7] == 0))
CurrentUsbbLength = Endpoint0.Rx[6];
else
CurrentUsbbLength = HIDDESCSIZE; // HID Descriptor
CurrentDescAddPointer = HIDDESCADD;
}
else
if (UsbwValue[1] == REPORT)
{
if ((Endpoint0.Rx[6] < REPORTDESCSIZE) & (Endpoint0.Rx[7] == 0))
CurrentUsbbLength = Endpoint0.Rx[6];
else
CurrentUsbbLength = REPORTDESCSIZE; // Report Descriptor
CurrentDescAddPointer = 0;
}
}
else
if (UsbbRequest == GET_CONFIGURATION)
{
CurrentUsbbLength = Endpoint0.Rx[6];
CurrentDescAddPointer = 0;
}
else
if (UsbbRequest == GET_INTERFACE)
{
CurrentUsbbLength = Endpoint0.Rx[6];
CurrentDescAddPointer = 0;
}
else
if (UsbbRequest == GET_STATUS)
{
CurrentUsbbLength = Endpoint0.Rx[6];
CurrentDescAddPointer = 0;
}
else
if (UsbbRequest == SET_DESCRIPTOR)
{
}
}
else
if ((UsbbmRequestType & TYPE) == CLASS)
{// Class requests
if ((UsbbRequest == GET_IDLE) || (UsbbRequest == GET_PROTOCOL))
CurrentUsbbLength = 1;
else
if ((UsbbRequest == GET_REPORT) || (UsbbRequest == SET_REPORT))
{
ReportIdNumber = UsbwValue[0]; // write Report ID number in ReportIdNumber
if (UsbwValue[1] == 0x1) // Input Report Type
bmRequest |= 0x2;
else
if (UsbwValue[1] == 0x2) // Output Report Type
bmRequest |= 0x1;
else
if (UsbwValue[1] == 0x3) // Feature Report Type
bmRequest |= 0x4;
}
if (UsbbRequest == GET_REPORT)
{
bmRequest |= 0x8; // Set bit 3 for GET_REPORT
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -