📄 usbfull.c
字号:
/****************************************************************************************/
/* Example Source Code for USB Enumeration using a PDIUSBD11 connected to a PIC16F87x */
/* Copyright 2002 Craig Peacock, Craig.Peacock@beyondlogic.org */
/* Version 1.2, 6th April 2002 http://www.beyondlogic.org */
/****************************************************************************************/
#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 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -