⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usb.c

📁 不可不看的USB_C51源码 内有C和VC6.0两种方式的编码
💻 C
字号:
#include <intrins.h>
#include "usb.h"

void ServiceSetupPacket(void)
{
  s = RequestType;
  SendData = sb7;
  if ((sb6 & sb4 & sb3 & sb2) | (sb1 & sb0) != 0)
  {
    Invalid();
  }
  else
  {
    if (sb5 == 1)
      s = 0x03;
    s &= 0x03;

    s = _cror_ (s, 4);

    i = Request;
    if (i > 15)
    {
      Invalid();
    }
    else
    {
      i &= 0x0f;
      s |= i;
      ReplyCount = 1;
      ReplyBuffer[0] = 0;
      ReplyBuffer[1] = 0;
      SetAddr = 0;
      STALL = 0;
      IsDescriptor = 0;
      switch (s) {
      case 0x00:
        DeviceGetStatus();
        break;
      case 0x01:
        DeviceClearFeature();
        break;
      case 0x03:
        DeviceSetFeature();
        break;
      case 0x05:
        SetAddress();
        break;
      case 0x06:
        GetDescriptor();
        break;
      case 0x07:
        SetDescriptor();
        break;
      case 0x08:
        GetConfiguration();
        break;
      case 0x09:
        SetConfiguration();
        break;

      case 0x10:
        InterfaceGetStatus();
        break;
      case 0x11:
        InterfaceClearFeature();
        break;
      case 0x13:
        InterfaceSetFeature();
        break;
      case 0x16:
        GetClassDescriptor();
        break;
      case 0x17:
        SetClassDescriptor();
        break;
      case 0x1a:
        GetInterface();
        break;
      case 0x1b:
        SetInterface();
        break;
       case 0x20:
        EndpointGetStatus();
        break;
      case 0x21:
        EndpointClearFeature();
        break;
      case 0x23:
        EndpointSetFeature();
        break;
      case 0x2c:
        EndpointSyncFrame();
        break;

      case 0x31:
        GetReport();
        break;
      case 0x32:
        GetIdle();
        break;
      case 0x33:
        GetProtocol();
        break;
      case 0x34:
        GetProtocol();
        break;
      case 0x39:
        SetReport();
        break;
      case 0x3a:
        SetIdle();
        break;
      case 0x3b:
        SetProtocol();
        break;

      default:
        ;
      }
    }  
  }
}

void DeviceGetStatus(void)
{
  *ReplyBuffer = 1;
}

void DeviceClearFeature(void)
{
  Invalid();
}

void DeviceSetFeature(void)
{
  Invalid();
}

void SetAddress(void)
{
  SetAddr = 1;
}

void GetDescriptor(void)
{
  IsDescriptor = 1;
/*
  if (i != 1)
  {
  Output(i);
  delay(4000);
  }
*/
  switch (wValueHigh) {
  case 1:
    PTR = DeviceDescriptor;
    ReturnLength = *DeviceDescriptor;
    break;
  case 2:
    PTR = ConfigurationDescriptor;
    ReturnLength = ConfigLength;
    break;
  case 3:
//    PTR = ConfigurationDescriptor;
    PTR = String;
    i = wValueLow;
    while (i != 0 && *PTR != 0)
    {
      PTR += *PTR;
      i--;
    }
    if (i == 0)
    {
      ReturnLength = *PTR;
    }
    else if (*PTR == 0)
    {
      Invalid();
    }
    break;
  default:
    Invalid();
  }
}

void SetDescriptor(void)
{
  Invalid();
}

void GetConfiguration(void)
{
  *ReplyBuffer = CurrentConfiguration;
}

void SetConfiguration(void)
{
  switch (wValueLow) {
  case 0:
    Configured = 0;
    CurrentConfiguration = 0;
    break;
  case 1:
    Configured = 1;
    CurrentConfiguration = 1;
    break;
  default:
    ;
  }
}

void InterfaceGetStatus(void)
{
  ReplyCount = 2;
}

void InterfaceClearFeature(void)
{
  Invalid();
}

void InterfaceSetFeature(void)
{
  Invalid();
}

void GetClassDescriptor(void)
{
  IsDescriptor = 1;
  i = wValueHigh;
  i -= 0x21;
  if (i == 0)
  {
    PTR = HIDDescriptor;
    ReturnLength = *HIDDescriptor;
  }
  else
  {
    i--;
    if (i == 0)
    {
      PTR = ReportDescriptor;
      ReturnLength = ReportLength;
    }
    else
      Invalid();
  }
}

void SetClassDescriptor(void)
{
  Invalid();
}

void GetInterface(void)
{
  Invalid();
}

void SetInterface(void)
{
  Invalid();
}

void EndpointGetStatus(void)
{
  ReplyCount = 2;
}

void EndpointClearFeature(void)
{
  Invalid();
}

void EndpointSetFeature(void)
{
  Invalid();
}

void EndpointSyncFrame(void)
{
  Invalid();
}

void GetReport(void)
{
  if (Configured == 1)
  {
    *ReplyBuffer = 0x42;
  }
  else
    Invalid();
}

void GetIdle(void)
{
  Invalid();
}

void GetProtocol(void)
{
  Invalid();
}

void SetReport(void)
{
  if (Configured == 1)
  {
    Wait4D();
  }
  else
    Invalid();
}

void SetIdle(void)
{
  Invalid();
}

void SetProtocol(void)
{
  Invalid();
}

void Invalid(void)
{
  STALL = 1;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -