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

📄 usb.txt

📁 全套USB开发源码!有需要的快
💻 TXT
📖 第 1 页 / 共 3 页
字号:
头文件:

typedef struct {
 unsigned char bmRequestType;
 unsigned char bRequest;
 unsigned short wValue;
 unsigned short wIndex;
 unsigned short wLength;
} USB_SETUP_REQUEST, *PUSB_SETUP_REQUEST;

typedef struct {
  unsigned char bLength;
  unsigned char bDescriptorType;
  unsigned short bcdUSB;
  unsigned char bDeviceClass;
  unsigned char bDeviceSubClass;
  unsigned char bDeviceProtocol;
  unsigned char bMaxPacketSize0;
  unsigned short idVendor;
  unsigned short idProduct;
  unsigned short bcdDevice;
  unsigned char iManufacturer;
  unsigned char iProduct;
  unsigned char iSerialNumber;
  unsigned char bNumConfigurations;
} USB_DEVICE_DESCRIPTOR, *PUSB_DEVICE_DESCRIPTOR;

typedef struct {
  unsigned char bLength;
  unsigned char bDescriptorType;
  unsigned char bEndpointAddress;
  unsigned char bmAttributes;
  unsigned short wMaxPacketSize;
  unsigned char bInterval;
} USB_ENDPOINT_DESCRIPTOR, *PUSB_ENDPOINT_DESCRIPTOR;

typedef struct {
  unsigned char bLength;
  unsigned char bDescriptorType;
  unsigned short wTotalLength;
  unsigned char bNumInterfaces;
  unsigned char bConfigurationValue;
  unsigned char iConfiguration;
  unsigned char bmAttributes;
  unsigned char MaxPower;
} USB_CONFIGURATION_DESCRIPTOR, *PUSB_CONFIGURATION_DESCRIPTOR;

typedef struct {
  unsigned char bLength;
  unsigned char bDescriptorType;
  unsigned char bInterfaceNumber;
  unsigned char bAlternateSetting;
  unsigned char bNumEndpoints;
  unsigned char bInterfaceClass;
  unsigned char bInterfaceSubClass;
  unsigned char bInterfaceProtocol;
  unsigned char iInterface;
} USB_INTERFACE_DESCRIPTOR, *PUSB_INTERFACE_DESCRIPTOR;

typedef struct {
  unsigned char bLength;
  unsigned char bDescriptorType;
  unsigned short wHIDClassSpecComp;
  unsigned char bCountry;
  unsigned char bNumDescriptors;
  unsigned char b1stDescType;
  unsigned short w1stDescLength;
} USB_HID_DESCRIPTOR, *PUSB_HID_DESCRIPTOR;

typedef struct {
 USB_CONFIGURATION_DESCRIPTOR ConfigDescriptor;
 USB_INTERFACE_DESCRIPTOR InterfaceDescriptor;
   USB_ENDPOINT_DESCRIPTOR EndpointDescriptor0;
 USB_ENDPOINT_DESCRIPTOR EndpointDescriptor1;
} USB_CONFIG_DATA, *PUSB_CONFIG_DATA;

typedef struct {
 unsigned char bLenght;
       unsigned char bDescriptorType;
       char     bString[24];
} MANUFACTURER_DESCRIPTOR, *PMANUFACTURER_DESCRIPTOR;

typedef struct {
 unsigned char bLenght;
       unsigned char bDescriptorType;
       unsigned short wLANGID0;
} LANGID_DESCRIPTOR, *PLANGID_DESCRIPTOR;

void I2C_Write(unsigned char byDevId, const unsigned char * pbyData, unsigned char byCount);
void I2C_Read(unsigned char byDevId, unsigned char pbyData[], unsigned char byCount);
void I2C_Init(void);
void I2C_Start(void);
void I2C_Stop(void);
unsigned char I2C_Read_Byte(unsigned char Ack);
void I2C_Write_Byte(unsigned char byte);
void USB_Init(void);
void USB_Disconnect(void);
void D11CmdDataWrite(unsigned char Command, const unsigned char *Buffer, unsigned char Count);
void D11CmdDataRead(unsigned char Command, unsigned char Buffer[], unsigned char Count);
unsigned char D11ReadEndpoint(unsigned char Endpoint, unsigned char *Buffer);
void D11WriteEndpoint(unsigned char Endpoint, const unsigned char *Buffer, unsigned char Bytes);
void WriteBufferToEndPoint(void);
void loadfromcircularbuffer(void);
void GetDescriptor(PUSB_SETUP_REQUEST SetupPacket);
void D11GetIRQ(void);
void InitUART(void);
void putch(unsigned char byte);
void puthex(unsigned char byte);
void Process_EP0_OUT_Interrupt(void);
void ErrorStallControlEndPoint(void);

#define D11_SET_HUB_ADDRESS   0xD0
#define D11_SET_ADDRESS_ENABLE  0xD1
#define D11_SET_ENDPOINT_ENABLE   0xD8
#define D11_SET_MODE   0xF3
#define D11_READ_INTERRUPT_REGISTER  0xF4
#define D11_READ_LAST_TRANSACTION 0x40
#define D11_SET_ENDPOINT_STATUS  0x40
#define D11_READ_ENDPOINT_STATUS 0x80
#define D11_READ_BUFFER   0xF0
#define D11_WRITE_BUFFER  0xF0
#define D11_CLEAR_BUFFER  0xF2
#define D11_VALIDATE_BUFFER  0xFA
#define D11_ACK_SETUP   0xF1

#define D11_ENDPOINT_EP0_OUT   0x02
#define D11_ENDPOINT_EP0_IN   0x03
#define D11_ENDPOINT_EP1_OUT   0x05
#define D11_ENDPOINT_EP1_IN   0x04
#define D11_ENDPOINT_EP2_OUT   0x06
#define D11_ENDPOINT_EP2_IN   0x07
#define D11_ENDPOINT_EP3_OUT   0x08
#define D11_ENDPOINT_EP3_IN   0x09

#define D11_CMD_ADDR        0x36
#define D11_DATA_ADDR_WRITE  0x34
#define D11_DATA_ADDR_READ  0x35

#define D11_INT_BUS_RESET  0x4000
#define D11_INT_EP0_OUT   0x0004
#define D11_INT_EP0_IN   0x0008
#define D11_INT_EP1_OUT   0x0020
#define D11_INT_EP1_IN   0x0010
#define D11_INT_EP2_OUT   0x0040
#define D11_INT_EP2_IN   0x0080
#define D11_INT_EP3_OUT   0x0100
#define D11_INT_EP3_IN   0x0200

#define D11_LAST_TRAN_SETUP  0x20

#define STANDARD_DEVICE_REQUEST  0x00
#define STANDARD_INTERFACE_REQUEST 0x01
#define STANDARD_ENDPOINT_REQUEST 0x02
#define VENDOR_DEVICE_REQUEST  0x40
#define VENDOR_ENDPOINT_REQUEST  0x42

#define GET_STATUS     0
#define CLEAR_FEATURE       1
#define SET_FEATURE                  3
#define SET_ADDRESS                  5
#define GET_DESCRIPTOR               6
#define SET_DESCRIPTOR               7
#define GET_CONFIGURATION            8
#define SET_CONFIGURATION            9
#define GET_INTERFACE                10
#define SET_INTERFACE                11
#define SYNCH_FRAME                  12
#define VENDOR_GET_ANALOG_VALUE  1
#define VENDOR_SET_RB_HIGH_NIBBLE 2

#define ENDPOINT_HALT   0

#define TYPE_DEVICE_DESCRIPTOR          1
#define TYPE_CONFIGURATION_DESCRIPTOR   2
#define TYPE_STRING_DESCRIPTOR          3
#define TYPE_INTERFACE_DESCRIPTOR       4
#define TYPE_ENDPOINT_DESCRIPTOR        5
#define TYPE_HID_DESCRIPTOR  0x21

#define USB_ENDPOINT_TYPE_CONTROL 0x00
#define USB_ENDPOINT_TYPE_ISOCHRONOUS 0x01
#define USB_ENDPOINT_TYPE_BULK  0x02
#define USB_ENDPOINT_TYPE_INTERRUPT 0x03


 

C 文件:

#include <pic.h>
#include <stdio.h>
#include <string.h>
#include "usbfull.h"

const USB_DEVICE_DESCRIPTOR DeviceDescriptor = {
    sizeof(USB_DEVICE_DESCRIPTOR), /* bLength */
    TYPE_DEVICE_DESCRIPTOR,        /* bDescriptorType */
    0x0110,                        /* bcdUSB USB Version 1.1 */
    0,                             /* bDeviceClass */
    0,                             /* bDeviceSubclass */
    0,                             /* bDeviceProtocol */
    8,                             /* bMaxPacketSize 8 Bytes */
//    0x04B4,                        /* idVendor (Cypress Semi) */
//    0x0002,                        /* idProduct (USB Thermometer Example) */
    0x045E,                        /* Intel 82930 USB Bulk IO Test Board */
    0x930A,
    0x0000,                        /* bcdDevice */
    1,                             /* iManufacturer String Index */
    0,                             /* iProduct String Index */
    0,                             /* iSerialNumber String Index */
    1                              /* bNumberConfigurations */
};

const USB_CONFIG_DATA ConfigurationDescriptor = {
    {                              /* configuration descriptor */
    sizeof(USB_CONFIGURATION_DESCRIPTOR), /* bLength */
    TYPE_CONFIGURATION_DESCRIPTOR, /* bDescriptorType */
    sizeof(USB_CONFIG_DATA),       /* wTotalLength */
    1,                             /* bNumInterfaces */
    1,                             /* bConfigurationValue */
    0,                             /* iConfiguration String Index */
    0x80,                          /* bmAttributes Bus Powered, No Remote Wakeup */
    0x32                           /* bMaxPower, 100mA */ 
    },
    {                              /* interface descriptor */
    sizeof(USB_INTERFACE_DESCRIPTOR), /* bLength */
    TYPE_INTERFACE_DESCRIPTOR,     /* bDescriptorType */
    0,                             /* bInterface Number */
    0,                             /* bAlternateSetting */
    2,                             /* bNumEndpoints */
    0xFF,                          /* bInterfaceClass (Vendor specific) */
    0xFF,                          /* bInterfaceSubClass */
    0xFF,                          /* bInterfaceProtocol */
    0                              /* iInterface String Index */
    },
    {                              /* endpoint descriptor */
    sizeof(USB_ENDPOINT_DESCRIPTOR),  /* bLength */
    TYPE_ENDPOINT_DESCRIPTOR,      /* bDescriptorType */
    0x01,                          /* bEndpoint Address EP1 OUT */
    0x02,                          /* bmAttributes - Interrupt */
    0x0008,                        /* wMaxPacketSize */
    0x00                           /* bInterval */
    },
    {                              /* endpoint descriptor */
    sizeof(USB_ENDPOINT_DESCRIPTOR),  /* bLength */
    TYPE_ENDPOINT_DESCRIPTOR,      /* bDescriptorType */
    0x81,                          /* bEndpoint Address EP1 IN */
    0x02,                          /* bmAttributes - Interrupt */
    0x0008,                        /* wMaxPacketSize */
    0x00                           /* bInterval */
    }
};

const LANGID_DESCRIPTOR LANGID_Descriptor = { /* LANGID String Descriptor Zero */
    sizeof(LANGID_DESCRIPTOR),          /* bLength */
    TYPE_STRING_DESCRIPTOR,             /* bDescriptorType */
    0x0409                              /* LANGID US English */
};

const MANUFACTURER_DESCRIPTOR Manufacturer_Descriptor = { /* ManufacturerString 1 */
    sizeof(MANUFACTURER_DESCRIPTOR),                      /* bLenght */
    TYPE_STRING_DESCRIPTOR,                               /* bDescriptorType */
    "B\0e\0y\0o\0n\0d\0 \0L\0o\0g\0i\0c\0"                /* ManufacturerString in UNICODE */
};

#define MAX_BUFFER_SIZE 80

bank1 unsigned char circularbuffer[MAX_BUFFER_SIZE];
unsigned char inpointer;
unsigned char outpointer;

const unsigned char * pSendBuffer;
unsigned char BytesToSend;
unsigned char CtlTransferInProgress;
unsigned char DeviceAddress;
unsigned char DeviceConfigured;

#define PROGRESS_IDLE             0
#define PROGRESS_ADDRESS          3

void main (void)
{
    TRISB = 0x03;   /* Int & Suspend Inputs */
    RB3 = 1;        /* Device Not Configured (LED) */
    RB2 = 0;        /* Reset PDIUSBD11 */

    inpointer = 0;
    outpointer = 0;

    InitUART();
    printf("Initialising\n\r");
    I2C_Init(); 

    RB2 = 1;        /* Bring PDIUSBD11 out of reset */

    ADCON1 = 0x80;  /* ADC Control - All 8 Channels Enabled, */

⌨️ 快捷键说明

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