📄 usb.txt
字号:
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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -