英锐恩单片机论坛,Microchip单片机,模拟器件,接口电路,麦肯单片机,单片机应用交流

 找回密码
 立即注册
搜索
电子烟方案单片机单片机开发深圳单片机开发
单片机方案国产单片机8位单片机电子烟方案开发
查看: 3074|回复: 0
打印 上一主题 下一主题

87X配PDIUSBD11的C源码

[复制链接]
跳转到指定楼层
1#
发表于 2009-4-26 12:20:44 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式




头文件:
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, */
                    /* supporting upgrade to 16F877 */
    USB_Init();
    printf("PDIUSBD11 Ready for connection\n\r");
    while(1)
if (!RB0) D11GetIRQ(); /* If IRQ is Low, PDIUSBD11 has an Interrupt Condition */
   
}   
void USB_Init(void)
{
    unsigned char Buffer[2];
    /* Disable Hub Function in PDIUSBD11 */
    Buffer[0] = 0x00;
    D11CmdDataWrite(D11_SET_HUB_ADDRESS, Buffer, 1);
    /* Set Address to zero (default) and enable function */
    Buffer[0] = 0x80;
    D11CmdDataWrite(D11_SET_ADDRESS_ENABLE, Buffer, 1);
    /* Enable function generic endpoints */
    Buffer[0] = 0x02;
    D11CmdDataWrite(D11_SET_ENDPOINT_ENABLE, Buffer, 1);
    /* Set Mode - Enable SoftConnect */
    Buffer[0] = 0x97; /* Embedded Function, SoftConnect, Clk Run, No LazyClk, Remote Wakeup */
    Buffer[1] = 0x0B; /* CLKOut = 4MHz */
    D11CmdDataWrite(D11_SET_MODE, Buffer, 2);
}
void D11GetIRQ(void)
{
    unsigned short Irq;
    unsigned char Buffer[8];
    unsigned char count;
    unsigned char bytes;
    /* Read Interrupt Register to determine source of interrupt */
    D11CmdDataRead(D11_READ_INTERRUPT_REGISTER, (unsigned char *)&Irq, 2);
        // if (Irq) printf("Irq = 0x%X: ",Irq);
        if (Irq & D11_INT_BUS_RESET) {
            printf("Bus Reset\n\r");
            USB_Init();
        }
        if (Irq & D11_INT_EP0_OUT) {
            printf("EP0_Out: ");
            Process_EP0_OUT_Interrupt();
        }
        if (Irq & D11_INT_EP0_IN) {
            printf("EP0_In: \n\r");
            if (CtlTransferInProgress == PROGRESS_ADDRESS) {
                D11CmdDataWrite(D11_SET_ADDRESS_ENABLE,&DeviceAddress,1);
                D11CmdDataRead(D11_READ_LAST_TRANSACTION + D11_ENDPOINT_EP0_IN, Buffer, 1);
                CtlTransferInProgress = PROGRESS_IDLE;
                }
            else {
                D11CmdDataRead(D11_READ_LAST_TRANSACTION + D11_ENDPOINT_EP0_IN, Buffer, 1);
                WriteBufferToEndPoint();
                }
        }
        if (Irq & D11_INT_EP1_OUT) {
            printf("EP1_OUT\n\r");
            D11CmdDataRead(D11_READ_LAST_TRANSACTION + D11_ENDPOINT_EP1_OUT, Buffer, 1);
            bytes = D11ReadEndpoint(D11_ENDPOINT_EP1_OUT, Buffer);
            for (count = 0; count < bytes; count++) {
               circularbuffer[inpointer++] = Buffer[count];
               if (inpointer >= MAX_BUFFER_SIZE) inpointer = 0;
              }
            loadfromcircularbuffer(); //Kick Start
        }
     
        if (Irq & D11_INT_EP1_IN) {
            printf("EP1_IN\n\r");
            D11CmdDataRead(D11_READ_LAST_TRANSACTION + D11_ENDPOINT_EP1_IN, Buffer, 1);
            loadfromcircularbuffer();
        }
        if (Irq & D11_INT_EP2_OUT) {
            printf("EP2_OUT\n\r");
            D11CmdDataRead(D11_READ_LAST_TRANSACTION + D11_ENDPOINT_EP2_OUT, Buffer, 1);
            Buffer[0] = 0x01; /* Stall Endpoint */
            D11CmdDataWrite(D11_SET_ENDPOINT_STATUS + D11_ENDPOINT_EP2_OUT, Buffer, 1);
        }
        if (Irq & D11_INT_EP2_IN) {
            printf("EP2_IN\n\r");
            D11CmdDataRead(D11_READ_LAST_TRANSACTION + D11_ENDPOINT_EP2_IN, Buffer, 1);
            Buffer[0] = 0x01; /* Stall Endpoint */
            D11CmdDataWrite(D11_SET_ENDPOINT_STATUS + D11_ENDPOINT_EP2_IN, Buffer, 1);
        }
        if (Irq & D11_INT_EP3_OUT) {
            printf("EP3_OUT\n\r");
            D11CmdDataRead(D11_READ_LAST_TRANSACTION + D11_ENDPOINT_EP3_OUT, Buffer, 1);
            Buffer[0] = 0x01; /* Stall Endpoint */
            D11CmdDataWrite(D11_SET_ENDPOINT_STATUS + D11_ENDPOINT_EP3_OUT, Buffer, 1);
        }
        if (Irq & D11_INT_EP3_IN) {
            printf("EP3_IN\n\r");
            D11CmdDataRead(D11_READ_LAST_TRANSACTION + D11_ENDPOINT_EP3_IN, Buffer, 1);
            Buffer[0] = 0x01; /* Stall Endpoint */
            D11CmdDataWrite(D11_SET_ENDPOINT_STATUS + D11_ENDPOINT_EP3_IN, Buffer, 1);
        }
}
void Process_EP0_OUT_Interrupt(void)
{
    unsigned long a;
    unsigned char Buffer[2];
    USB_SETUP_REQUEST SetupPacket;
    /* Check if packet received is Setup or Data - Also clears IRQ */
    D11CmdDataRead(D11_READ_LAST_TRANSACTION + D11_ENDPOINT_EP0_OUT, (char *)&SetupPacket, 1);
    if (SetupPacket.bmRequestType & D11_LAST_TRAN_SETUP) {
        /* This is a setup Packet - Read Packet */
        D11ReadEndpoint(D11_ENDPOINT_EP0_OUT, (char *)&SetupPacket);
        /* Acknowlegde Setup Packet to EP0_OUT & Clear Buffer*/
        D11CmdDataWrite(D11_ACK_SETUP, NULL, 0);
        D11CmdDataWrite(D11_CLEAR_BUFFER, NULL, 0);
        /* Acknowlegde Setup Packet to EP0_IN */
        D11CmdDataWrite(D11_ENDPOINT_EP0_IN, NULL, 0);
        D11CmdDataWrite(D11_ACK_SETUP, NULL, 0);
        /* Parse bmRequestType */
        switch (SetupPacket.bmRequestType & 0x7F) {
            case STANDARD_DEVICE_REQUEST:
                printf("Standard Device Request ");
                switch (SetupPacket.bRequest) {
                    case GET_STATUS:
                        /* Get Status Request to Device should return */
                        /* Remote Wakeup and Self Powered Status */
                        Buffer[0] = 0x01;
                        Buffer[1] = 0x00;
                        D11WriteEndpoint(D11_ENDPOINT_EP0_IN, Buffer, 2);
                        break;
                    case CLEAR_FEATURE:
                    case SET_FEATURE:
                        /* We don't support DEVICE_REMOTE_WAKEUP or TEST_MODE */
                        ErrorStallControlEndPoint();
                        break;
                    case SET_ADDRESS:
                        printf("Set Address\n\r");
                        DeviceAddress = SetupPacket.wValue | 0x80;
                        D11WriteEndpoint(D11_ENDPOINT_EP0_IN, NULL, 0);
                        CtlTransferInProgress = PROGRESS_ADDRESS;
                        break;
                    case GET_DESCRIPTOR:
                        GetDescriptor(&SetupPacket);
                        break;
  
                    case GET_CONFIGURATION:
                        D11WriteEndpoint(D11_ENDPOINT_EP0_IN, &DeviceConfigured, 1);
                        break;
                    case SET_CONFIGURATION:
                        DeviceConfigured = SetupPacket.wValue & 0xFF;
                        D11WriteEndpoint(D11_ENDPOINT_EP0_IN, NULL, 0);
                        if (DeviceConfigured) {
                            RB3 = 0;
                            printf("\n\r *** Device Configured *** \n\r");
                            }
                        else {
                            RB3 = 1; /* Device Not Configured */
                            printf("\n\r ** Device Not Configured *** \n\r");
                            }
                        break;
                    case SET_DESCRIPTOR:
                    default:
                        /* Unsupported - Request Error - Stall */
                        ErrorStallControlEndPoint();
                        break;
                }
                break;
            case STANDARD_INTERFACE_REQUEST:
                printf("Standard Interface Request\n\r");
                switch (SetupPacket.bRequest) {
                        
                    case GET_STATUS:
                        /* Get Status Request to Interface should return */
                        /* Zero, Zero (Reserved for future use) */
                        Buffer[0] = 0x00;
                        Buffer[1] = 0x00;
                        D11WriteEndpoint(D11_ENDPOINT_EP0_IN, Buffer, 2);
                        break;
                    case SET_INTERFACE:
                        /* Device Only supports default setting, Stall may be */
                        /* returned in the status stage of the request */
                        if (SetupPacket.wIndex == 0 && SetupPacket.wValue == 0)  
                            /* Interface Zero, Alternative Setting = 0 */
                            D11WriteEndpoint(D11_ENDPOINT_EP0_IN, NULL, 0);     
                        else ErrorStallControlEndPoint();
                        break;
                    case GET_INTERFACE:
                        if (SetupPacket.wIndex == 0) { /* Interface Zero */
                            Buffer[0] = 0; /* Alternative Setting */
                            D11WriteEndpoint(D11_ENDPOINT_EP0_IN, Buffer, 1);
                            break;
   } /* else fall through as RequestError */
                    //case CLEAR_FEATURE:
                    //case SET_FEATURE:
                        /* Interface has no defined features. Return RequestError */
                    default:
                        ErrorStallControlEndPoint();
                        break;
                }
                break;
            case STANDARD_ENDPOINT_REQUEST:
                printf("Standard Endpoint Request\n\r");
                switch (SetupPacket.bRequest) {
                    case CLEAR_FEATURE:
                    case SET_FEATURE:
                        /* Halt(Stall) feature required to be implemented on all Interrupt and */
                        /* Bulk Endpoints. It is not required nor recommended on the Default Pipe */
                          
                        if (SetupPacket.wValue == ENDPOINT_HALT)
                        {
                            if (SetupPacket.bRequest == CLEAR_FEATURE) Buffer[0] = 0x00;
                            else                                       Buffer[0] = 0x01;
                            switch (SetupPacket.wIndex & 0xFF) {
                                case 0x01 : D11CmdDataWrite(D11_SET_ENDPOINT_STATUS + \
                                              D11_ENDPOINT_EP1_OUT, Buffer, 1);
                                            break;
                                case 0x81 : D11CmdDataWrite(D11_SET_ENDPOINT_STATUS + \
                                              D11_ENDPOINT_EP1_IN, Buffer, 1);
                                            break;
                                case 0x02 : D11CmdDataWrite(D11_SET_ENDPOINT_STATUS + \
                                              D11_ENDPOINT_EP2_OUT, Buffer, 1);
                                            break;
                                case 0x82 : D11CmdDataWrite(D11_SET_ENDPOINT_STATUS + \
                                              D11_ENDPOINT_EP2_IN, Buffer, 1);
                                            break;
                                case 0x03 : D11CmdDataWrite(D11_SET_ENDPOINT_STATUS + \
                                              D11_ENDPOINT_EP3_OUT, Buffer, 1);
                                            break;
                                case 0x83 : D11CmdDataWrite(D11_SET_ENDPOINT_STATUS + \
                                              D11_ENDPOINT_EP3_IN, Buffer, 1);
                                            break;
                                default   : /* Invalid Endpoint - RequestError */
                                            ErrorStallControlEndPoint();
                                            break;
                            }
                            D11WriteEndpoint(D11_ENDPOINT_EP0_IN, NULL, 0);
                        } else {
                            /* No other Features for Endpoint - Request Error */
                            ErrorStallControlEndPoint();
                        }
                        break;
                    case GET_STATUS:
                        /* Get Status Request to Endpoint should return */
                        /* Halt Status in D0 for Interrupt and Bulk */
                        switch (SetupPacket.wIndex & 0xFF) {
                            case 0x01 : D11CmdDataRead(D11_READ_ENDPOINT_STATUS + \
                                          D11_ENDPOINT_EP1_OUT, Buffer, 1);
                                        break;
                            case 0x81 : D11CmdDataRead(D11_READ_ENDPOINT_STATUS + \
                                          D11_ENDPOINT_EP1_IN, Buffer, 1);
                                        break;
                            case 0x02 : D11CmdDataRead(D11_READ_ENDPOINT_STATUS + \
                                          D11_ENDPOINT_EP2_OUT, Buffer, 1);
                                        break;
                            case 0x82 : D11CmdDataRead(D11_READ_ENDPOINT_STATUS + \
                                          D11_ENDPOINT_EP2_IN, Buffer, 1);
                                        break;
                            case 0x03 : D11CmdDataRead(D11_READ_ENDPOINT_STATUS + \
                                          D11_ENDPOINT_EP3_OUT, Buffer, 1);
                                        break;
                            case 0x83 : D11CmdDataRead(D11_READ_ENDPOINT_STATUS + \
                                          D11_ENDPOINT_EP3_IN, Buffer, 1);
                                        break;
                            default   : /* Invalid Endpoint - RequestError */
                                        ErrorStallControlEndPoint();
                                        break;
                        }
                        if (Buffer[0] & 0x08) Buffer[0] = 0x01;
                        else                  Buffer[0] = 0x00;
                        Buffer[1] = 0x00;
                        D11WriteEndpoint(D11_ENDPOINT_EP0_IN, Buffer, 2);
                        break;
                    
                    default:
                        /* Unsupported - Request Error - Stall */
                        ErrorStallControlEndPoint();
                        break;
                }
                break;
            case VENDOR_DEVICE_REQUEST:
            case VENDOR_ENDPOINT_REQUEST:
                printf("Vendor Device bRequest = 0x%X, wValue = 0x%X, wIndex = 0x%X\n\r", \
                    SetupPacket.bRequest, SetupPacket.wValue, SetupPacket.wIndex);
                switch (SetupPacket.bRequest) {
                    case VENDOR_GET_ANALOG_VALUE:
                        printf("Get Analog Value, Channel %x :",SetupPacket.wIndex & 0x07);
                        ADCON0 = 0xC1 | (SetupPacket.wIndex & 0x07) << 3;            
                        /* Wait Acquistion time of Sample and Hold */
                        for (a = 0; a <= 255; a++);
                        ADGO = 1;
                        while(ADGO);
                        Buffer[0] = ADRESL;
                        Buffer[1] = ADRESH;
                        a = (Buffer[1] << 8) + Buffer[0];
                        a = (a * 500) / 1024;
                        printf(" Value = %d.%02d\n\r",(unsigned int)a/100,(unsigned int)a%100);
                        D11WriteEndpoint(D11_ENDPOINT_EP0_IN, Buffer, 2);
                        break;
                    case VENDOR_SET_RB_HIGH_NIBBLE:
                        printf("Write High Nibble of PORTB\n\r");
                        PORTB = (PORTB & 0x0F) | (SetupPacket.wIndex & 0xF0);
                        D11WriteEndpoint(D11_ENDPOINT_EP0_IN, NULL, 0);
                        break;
                    default:
                        ErrorStallControlEndPoint();
                        break;   
                }
                break;
            default:
                printf("UnSupported Request Type 0x%X\n\r",SetupPacket.bmRequestType);
                ErrorStallControlEndPoint();
                break;
        }
    } else {
    printf("Data Packet?\n\r");
    /* This is a Data Packet */
    }
}
void GetDescriptor(PUSB_SETUP_REQUEST SetupPacket)
{
    switch((SetupPacket->wValue & 0xFF00) >> 8) {
        case TYPE_DEVICE_DESCRIPTOR:
            printf("\n\rDevice Descriptor: Bytes Asked For %d, Size of Descriptor %d\n\r", \
                    SetupPacket->wLength,DeviceDescriptor.bLength);
            pSendBuffer = (const unsigned char *)&DeviceDescriptor;
            BytesToSend = DeviceDescriptor.bLength;
            if (BytesToSend > SetupPacket->wLength)
                BytesToSend = SetupPacket->wLength;
            WriteBufferToEndPoint();
            break;
        case TYPE_CONFIGURATION_DESCRIPTOR:
            printf("\n\rConfiguration Descriptor: Bytes Asked For %d, Size of Descriptor %d\n\r", \
                    SetupPacket->wLength, sizeof(ConfigurationDescriptor));
            pSendBuffer = (const unsigned char *)&ConfigurationDescriptor;
            BytesToSend = sizeof(ConfigurationDescriptor);
            if (BytesToSend > SetupPacket->wLength)
                BytesToSend = SetupPacket->wLength;
            WriteBufferToEndPoint();
            break;
        case TYPE_STRING_DESCRIPTOR:
            printf("\n\rString Descriptor: LANGID = 0x%04x, Index %d\n\r", \
                SetupPacket->wIndex, SetupPacket->wValue & 0xFF);
            switch (SetupPacket->wValue & 0xFF){
                case 0 :  pSendBuffer = (const unsigned char *)&LANGID_Descriptor;
                          BytesToSend = sizeof(LANGID_Descriptor);
                          break;
                case 1 :  pSendBuffer = (const unsigned char *)&Manufacturer_Descriptor;
                          BytesToSend = sizeof(Manufacturer_Descriptor);
                          break;
                default : pSendBuffer = NULL;
                          BytesToSend = 0;
            }
            if (BytesToSend > SetupPacket->wLength)
                BytesToSend = SetupPacket->wLength;
            WriteBufferToEndPoint();
            break;
       default:
            ErrorStallControlEndPoint();
            break;
    }
}
void ErrorStallControlEndPoint(void)
{
    unsigned char Buffer[] = { 0x01 };
    /* 9.2.7 RequestError - return STALL PID in response to next DATA Stage Transaction */
    D11CmdDataWrite(D11_SET_ENDPOINT_STATUS + D11_ENDPOINT_EP0_IN, Buffer, 1);
    /* or in the status stage of the message. */
    D11CmdDataWrite(D11_SET_ENDPOINT_STATUS + D11_ENDPOINT_EP0_OUT, Buffer, 1);
}
unsigned char D11ReadEndpoint(unsigned char Endpoint, unsigned char *Buffer)
{
    unsigned char D11Header[2];
    unsigned char BufferStatus = 0;
    /* Select Endpoint */
    D11CmdDataRead(Endpoint, &BufferStatus, 1);
    /* Check if Buffer is Full */
    if(BufferStatus & 0x01)
    {
        /* Read dummy header - D11 buffer pointer is incremented on each read */
        /* and is only reset by a Select Endpoint Command */
        D11CmdDataRead(D11_READ_BUFFER, D11Header, 2);
        if(D11Header[1]) D11CmdDataRead(D11_READ_BUFFER, Buffer, D11Header[1]);
        /* Allow new packets to be accepted */
        D11CmdDataWrite(D11_CLEAR_BUFFER, NULL, 0);
    }
    return D11Header[1];
}
void D11WriteEndpoint(unsigned char Endpoint, const unsigned char *Buffer, unsigned char Bytes)
{
    unsigned char D11Header[2];
    unsigned char BufferStatus = 0;
    D11Header[0] = 0x00;
    D11Header[1] = Bytes;
    /* Select Endpoint */
    D11CmdDataRead(Endpoint, &BufferStatus, 1);
    /* Write Header */
    D11CmdDataWrite(D11_WRITE_BUFFER, D11Header, 2);
    /* Write Packet */
    if (Bytes) D11CmdDataWrite(D11_WRITE_BUFFER, Buffer, Bytes);
    /* Validate Buffer */
    D11CmdDataWrite(D11_VALIDATE_BUFFER, NULL, 0);
}
void WriteBufferToEndPoint(void)
{
    if (BytesToSend == 0) {
        /* If BytesToSend is Zero and we get called again, assume buffer is smaller */
        /* than Setup Request Size and indicate end by sending Zero Lenght packet */
        D11WriteEndpoint(D11_ENDPOINT_EP0_IN, NULL, 0);
    } else if (BytesToSend >= 8) {
        /* Write another 8 Bytes to buffer and send */
        D11WriteEndpoint(D11_ENDPOINT_EP0_IN, pSendBuffer, 8);
        pSendBuffer += 8;
        BytesToSend -= 8;
    } else {
        /* Buffer must have less than 8 bytes left */
        D11WriteEndpoint(D11_ENDPOINT_EP0_IN, pSendBuffer, BytesToSend);
        BytesToSend = 0;
    }
}
void loadfromcircularbuffer(void)
{
   unsigned char Buffer[10];
   unsigned char count;
   // Read Buffer Full Status
   D11CmdDataRead(D11_ENDPOINT_EP1_IN, Buffer, 1);
   if (Buffer[0] == 0){
      // Buffer Empty
      if (inpointer != outpointer){
         // We have bytes to send
         count = 0;
         do {
             Buffer[count++] = circularbuffer[outpointer++];
             if (outpointer >= MAX_BUFFER_SIZE) outpointer = 0;
             if (outpointer == inpointer) break; // No more data
            } while (count < 8); // Maximum Buffer Size
         // Now load it into EP1_In
         D11WriteEndpoint(D11_ENDPOINT_EP1_IN, Buffer, count);
         }
   }
}
void D11CmdDataWrite(unsigned char Command, const unsigned char *Buffer, unsigned char Count)
{
    I2C_Write(D11_CMD_ADDR, &Command, 1);
    if(Count) I2C_Write(D11_DATA_ADDR_WRITE, Buffer, Count);
}
void D11CmdDataRead(unsigned char Command, unsigned char Buffer[], unsigned char Count)
{
    I2C_Write(D11_CMD_ADDR, &Command, 1);
    if(Count) I2C_Read(D11_DATA_ADDR_READ, Buffer, Count);
}
void I2C_Read(unsigned char byDevId, unsigned char pbyData[], unsigned char byCount)
{
    unsigned char byRetVal = 0;
    I2C_Start();            
    I2C_Write_Byte(byDevId);
    while(byCount) {
        pbyData[byRetVal] = I2C_Read_Byte(byCount);
        byCount--;
        byRetVal++;
    }
    I2C_Stop();
}
void I2C_Write(unsigned char byDevId, const unsigned char * pbyData, unsigned char byCount)
{
    unsigned char byRetVal = 0;
    I2C_Start();
    I2C_Write_Byte(byDevId);
    while(byCount) {
        I2C_Write_Byte( *(pbyData+byRetVal));
        byCount--;
        byRetVal++;
        }
    I2C_Stop();
}
void I2C_Init(void)
{
    SSPCON = 0x08;    /* I2C Master Mode, Clock = FOSC / (4*(SSPADD+1)) */
                      /* Clk = 20Mhz / (4 * (4+1)) = 1Mhz */
    SSPADD = 0x04;    /* Load Baud Rate Generator -> Bank 2 */
    STAT_SMP = 1;     /* Slew Rate Control Disabled (1Mhz) -> Bank 2 */
    SSPEN = 1;        /* Enable SSP */
}
void I2C_Start(void)
{
    SEN = 1;          /* Generate Start Condition */
    while(SEN);       /* Wait for Start Condition to Finish */
}
void I2C_Stop(void)
{
    PEN = 1;          /* Generate Stop Condition */
    while(PEN);       /* Wait for Stop Condition to Finish */
}
void I2C_Write_Byte(unsigned char byte)
{
    SSPIF = 0;        /* Clear SPP Interrupt Flag */
    SSPBUF = byte;
    while(STAT_BF);   /* Wait for Buffer to Empty */
    while(!SSPIF);    /* SSP Interrupt Occured, ACK has been Read */
}
unsigned char I2C_Read_Byte(unsigned char Ack)
{
    unsigned char byte;
    SSPIF = 0;        /* Clear SPP Interrupt Flag */
    RCEN = 1;         /* Enable Receive Mode */
    while(!SSPIF);    /* SSP Interrupt Occured */
    byte = SSPBUF;
    if (Ack != 1) ACKDT=0;
    else          ACKDT=1;
    ACKEN=1;          /* send acknowledge sequence */
    while(ACKEN);
    return(byte);
}
void InitUART(void)
{
    SPBRG = 10;       /* 115200BPS @ 20MHz */
    BRGH = 1;         /* High Speed Mode */   
    TXEN = 1;         /* Enable Transmit */
    SPEN = 1;         /* Enable Serial Port */
}
void putch(unsigned char byte)
{
    while(!TRMT);     /* Wait for TX Buffer Empty */
    TXREG = byte;
}

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|公司首页|Microchip单片机,模拟器件,接口电路,麦肯单片机,单片机应用交流 ( 粤ICP备09008620号 )

GMT+8, 2024-5-2 09:34 , Processed in 0.065382 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表