nader
Joined: 29 Aug 2004 Posts: 12
|
d12 ccs for 18f452 code |
Posted: Tue Sep 07, 2004 1:29 am |
|
|
#define PortA 0xF80 //port A
#define D12_Control_Port 0xF81 //port B usb
#define PORTC 0xF82 //port C lcd
#define D12_Data_Port 0xF83 //port D usb
#define PortE 0xF84 //port E
//------------------------------------------------------------------//
// DEFINE PDIUSBD12 CONTROLS LINES //
//------------------------------------------------------------------//
#BIT D12_INT_N = D12_Control_port.0
#BIT D12_SUSPEND = D12_Control_port.2
#BIT D12_CS_N = D12_Control_port.3
#BIT D12_A0 = D12_Control_port.4
#BIT D12_RD_N = D12_Control_port.5
#BIT D12_WR_N = D12_Control_port.6
#BIT D12_RST_N = D12_Control_port.7
#BYTE D12_DATA = D12_Data_Port
//-----------------------------------------------------------------//
#define set_tris_usb_data set_tris_d
#define set_tris_usb_control set_tris_b
//-----------------------------------------------------------------//
#bit INT0IF = 0xFF2.1
//-------------------------------ATD------------------------------------------//
#byte ADCON0 = 0xFC2
#byte ADCON1 = 0XFC1
#bit ADCON = ADCON0.0
#bit ADGO = ADCON0.2
#bit ADFM = ADCON1.7
#byte ADRESH = 0XFC4
#byte ADRESL = 0XFC3
#define ENP0_BUFFER_SIZE 16
#define ENP1_BUFFER_SIZE 16
#define MAX_BUFFER_SIZE 80
#define PROGRESS_IDLE 0
#define PROGRESS_ADDRESS 3
#define NULL 0
//----------D12 command---------------------------------------------//
#define D12_SET_ADDRESS_ENABLE 0xD0
#define D12_SET_ENDPOINT_ENABLE 0xD8
#define D12_SET_MODE 0xF3
#define D12_SET_DMA 0xFB
#define D12_READ_INTERRUPT_REGISTER 0xF4
#define D12_READ_LAST_TRANSACTION 0x40
#define D12_SET_ENDPOINT_STATUS 0x40
#define D12_READ_ENDPOINT_STATUS 0x80
#define D12_READ_BUFFER 0xF0
#define D12_WRITE_BUFFER 0xF0
#define D12_CLEAR_BUFFER 0xF2
#define D12_VALIDATE_BUFFER 0xFA
#define D12_ACK_SETUP 0xF1
#define D12_ENDPOINT_EP0_OUT 0x00
#define D12_ENDPOINT_EP0_IN 0x01
#define D12_ENDPOINT_EP1_OUT 0x02
#define D12_ENDPOINT_EP1_IN 0x03
#define D12_ENDPOINT_EP2_OUT 0x04
#define D12_ENDPOINT_EP2_IN 0x05
//---------------set mode register bit map------------------------//
#define D12_NOLAZYCLOCK 0x02
#define D12_CLOCKRUNNING 0x04
#define D12_INTERRUPTMODE 0x08
#define D12_SOFTCONNECT 0x10
#define D12_ENDP_NONISO 0x00
#define D12_ENDP_ISOOUT 0x40
#define D12_ENDP_ISOIN 0x80
#define D12_ENDP_ISOIO 0xC0
#define D12_CLOCK_12M 0x03
#define D12_CLOCK_4M 0x0b
#define D12_SETTOONE 0x40
#define D12_SOFONLY 0x80
//--------------set dma register--------------------------------//
#define D12_DMASINGLE 0x00
#define D12_BURST_4 0x01
#define D12_BURST_8 0x02
#define D12_BURST_16 0x03
#define D12_DMAENABLE 0x04
#define D12_DMA_INTOKEN 0x08
#define D12_AUTOLOAD 0x10
#define D12_NORMALPLUSSOF 0x20
#define D12_ENDP4INTENABLE 0x40
#define D12_ENDP5INTENABLE 0x80
//----------------------intrrupt register-----------------------//
#define D12_INT_ENDP0OUT 0x01
#define D12_INT_ENDP0IN 0x02
#define D12_INT_ENDP1OUT 0x04
#define D12_INT_ENDP1IN 0x08
#define D12_INT_ENDP2OUT 0x10
#define D12_INT_ENDP2IN 0x20
#define D12_INT_BUSRESET 0x40
#define D12_INT_SUSPENDCHANGE 0x80
//-----------------------request_type--------------------------------//
#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
//------------------standard device request--------------------------//
#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
//---------------------desriptor type------------------//
#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
#define D12_LAST_TRAN_SETUP 0x20
#define D12_BUFFER0FULL 0x20
#define D12_BUFFER1FULL 0x40
#define D12_FULLEMPTY 0x01
#define D12_STALL 0x02
// values for the bits returned by the USB GET_STATUS command //
#define USB_GETSTATUS_SELF_POWERED 0x01
#define USB_GETSTATUS_REMOTE_WAKEUP_ENABLED 0x02
// values for "wvalue" field of GET_DESCRIPTOR request or for //
// "bDescriptorType" field of any descriptor //
#define USB_DEVICE_DESCRIPTOR_TYPE 0x01
#define USB_CONFIGURATION_DESCRIPTOR_TYPE 0x02
#define USB_STRING_DESCRIPTOR_TYPE 0x03
#define USB_INTERFACE_DESCRIPTOR_TYPE 0x04
#define USB_ENDPOINT_DESCRIPTOR_TYPE 0x05
#define USB_POWER_DESCRIPTOR_TYPE 0x06
#define USB_ENDPOINT_DIRECTION_MASK 0x80
// test direction bit in the bEndpointAddress field of -------------
// an endpoint descriptor.
#define USB_ENDPOINT_DIRECTION_OUT(addr) (!((addr) & USB_ENDPOINT_DIRECTION_MASK))
#define USB_ENDPOINT_DIRECTION_IN(addr) ((addr) & USB_ENDPOINT_DIRECTION_MASK)
// values for "bRequest" field for Standard Device Request //
#define USB_REQUEST_GET_STATUS 0x00
#define USB_REQUEST_CLEAR_FEATURE 0x01
#define USB_REQUEST_SET_FEATURE 0x03
#define USB_REQUEST_SET_ADDRESS 0x05
#define USB_REQUEST_GET_DESCRIPTOR 0x06
#define USB_REQUEST_SET_DESCRIPTOR 0x07
#define USB_REQUEST_GET_CONFIGURATION 0x08
#define USB_REQUEST_SET_CONFIGURATION 0x09
#define USB_REQUEST_GET_INTERFACE 0x0A
#define USB_REQUEST_SET_INTERFACE 0x0B
#define USB_REQUEST_SYNC_FRAME 0x0C
// defined USB device classes----------------
#define USB_DEVICE_CLASS_RESERVED 0x00
#define USB_DEVICE_CLASS_AUDIO 0x01
#define USB_DEVICE_CLASS_COMMUNICATIONS 0x02
#define USB_DEVICE_CLASS_HUMAN_INTERFACE 0x03
#define USB_DEVICE_CLASS_MONITOR 0x04
#define USB_DEVICE_CLASS_PHYSICAL_INTERFACE 0x05
#define USB_DEVICE_CLASS_POWER 0x06
#define USB_DEVICE_CLASS_PRINTER 0x07
#define USB_DEVICE_CLASS_STORAGE 0x08
#define USB_DEVICE_CLASS_HUB 0x09
#define USB_DEVICE_CLASS_VENDOR_SPECIFIC 0xFF
// USB defined Feature selectors----------------
#define USB_FEATURE_ENDPOINT_STALL 0x0000
#define USB_FEATURE_REMOTE_WAKEUP 0x0001
#define USB_FEATURE_POWER_D0 0x0002
#define USB_FEATURE_POWER_D1 0x0003
#define USB_FEATURE_POWER_D2 0x0004
#define USB_FEATURE_POWER_D3 0x0005
void usb_init(void)
{
set_tris_usb_data(0x00); // make it out put
set_tris_usb_control(0x07); //deafult
D12_CS_N = 1; //deactive d12cs
D12_A0 = 0;
D12_WR_N = 1;
D12_RD_N = 1;
D12_RST_N = 1; //reset d12
}
//------------------------------------------------------------------//
void D12_Cmd(unsigned int8 Command)
{
D12_Data = Command;
D12_A0 = 1; //edge up
D12_WR_N = 0;
D12_CS_N = 0; //active d12cs
D12_WR_N = 1;
D12_A0 = 0; //edge dowm
D12_CS_N = 1; //off
delay_cycles(5); //500ns delay for next command
}
//------------------------------------------------------------------//
void D12_Data_Write(unsigned int8 Data)
{
D12_DATA = Data;
D12_WR_N = 0;
D12_CS_N = 0; //active d12cs
D12_WR_N = 1;
D12_CS_N = 1; //off
delay_cycles(5); //500ns delay for next command
}
//-----------------------------------------------------------------//
unsigned int8 D12_Data_Read(void)
{
unsigned int8 data;
set_tris_usb_data(0xFF); //make it input
D12_RD_N = 0;
D12_CS_N = 0; //active d12cs
data = D12_DATA; //get data
D12_RD_N = 1;
D12_CS_N = 1; //deactive d12cs
set_tris_usb_data(0xFF); //make it out put
delay_cycles(5);
return(data);
}
//----------------------------------------------------------------//
void D12_Cmd_write(unsigned char command,unsigned char *buffer,unsigned char count)
{
unsigned char i = 0;
d12_cmd(command);
while(count)
{
d12_data_write(*(buffer+i));
count--;
i++;
}
}
//----------------------------------------------------------------//
void D12_Cmd_read(unsigned char command,unsigned char *buffer,unsigned char count)
{
unsigned char i = 0;
d12_cmd(command);
while(count)
{
*(buffer+i) = d12_data_read();
count--;
i++;
}
}
//-----------------------------------------------------------------//
void usb_reconnect(void)
{
unsigned char buff[2];
//set mode disconnect
buff[0] = D12_NOLAZYCLOCK;
buff[1] = D12_SETTOONE|D12_CLOCK_4M;
D12_Cmd_write(D12_SET_MODE,buff,2);
//delay_ms(1000);
//set dma
buff[0] = 0xc0;
D12_Cmd_write(D12_SET_DMA,buff,1);
//set mode connect
buff[0] = 0x16; //soft connect
buff[1] = D12_SETTOONE|D12_CLOCK_4M;
D12_Cmd_write(D12_SET_MODE,buff,2);
}
//----------------------------------------------------------------//
void ErrorStallControlEndPoint(void)
{
unsigned char Buffer[] = { 0x01 };
D12_Cmd_write(D12_SET_ENDPOINT_STATUS + D12_ENDPOINT_EP0_IN, Buffer, 1);
D12_Cmd_write(D12_SET_ENDPOINT_STATUS + D12_ENDPOINT_EP0_OUT, Buffer, 1);
}
//----------------------------------------------------------------//
unsigned char D12ReadEndpoint(unsigned char Endpoint,unsigned char *Buffer)
{
unsigned char D12Header[2];
unsigned char BufferStatus = 0;
/* Select Endpoint */
D12_Cmd_read(Endpoint, &BufferStatus, 1);
/* Check if Buffer is Full */
if(BufferStatus & 0x01)
{
D12_Cmd_read(D12_READ_BUFFER, D12Header, 2);
if(D12Header[1]) /* Have some thing for read? */
D12_Cmd_read(D12_READ_BUFFER, Buffer, D12Header[1]);
/* Allow new packets to be accepted */
D12_Cmd_write(D12_CLEAR_BUFFER, NULL, 0);
}
return D12Header[1]; /* return the length of data read */
}
//----------------------------------------------------------------//
void D12WriteEndpoint(unsigned char Endpoint,unsigned char *Buffer,unsigned char Bytes)
{
unsigned char D12Header[2];
unsigned char BufferStatus = 0;
D12Header[0] = 0x00;
D12Header[1] = Bytes;
/* Select Endpoint */
D12_Cmd_read(Endpoint, &BufferStatus, 1);
/* Write Header */
D12_Cmd_write(D12_WRITE_BUFFER, D12Header, 2);
/* Write Packet */
if(Bytes)
D12_Cmd_write(D12_WRITE_BUFFER, Buffer, Bytes);
/* Validate Buffer */
D12_Cmd_write(D12_VALIDATE_BUFFER, NULL, 0);
}
//----------------------------------------------------------------//
void WriteBufferToEndPoint(void) //-for control endpoint send descriptor-//
{
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 */
D12WriteEndpoint(D12_ENDPOINT_EP0_IN, NULL, 0);
}
else if (BytesToSend >= ENP0_BUFFER_SIZE)
{
/* Write another 8/16 Bytes to buffer and send */
D12WriteEndpoint(D12_ENDPOINT_EP0_IN, pSendBuffer, ENP0_BUFFER_SIZE);
pSendBuffer += ENP0_BUFFER_SIZE;
BytesToSend -= ENP0_BUFFER_SIZE;
} else {
/* Buffer must have less than 8/16 bytes left */
D12WriteEndpoint(D12_ENDPOINT_EP0_IN, pSendBuffer, BytesToSend);
BytesToSend = 0;
}
}
//----------------------------------------------------------------//
void loadfromcircularbuffer(void)
{
unsigned char Buffer[2+ENP1_BUFFER_SIZE];
unsigned char count;
// Read Buffer Full Status
D12_Cmd_read(D12_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 < ENP1_BUFFER_SIZE); // Maximum Buffer Size
// Now load it into EP1_In
D12WriteEndpoint(D12_ENDPOINT_EP1_IN, Buffer, count);
}
}
}
//--------------------------------------------------------------------//
void GetDescriptor(PUSB_SETUP_REQUEST SetupPacket)
{
switch((SetupPacket->wValue & 0xFF00) >> 8)
{
case TYPE_DEVICE_DESCRIPTOR:
pSendBuffer = (unsigned char *)&DeviceDescriptor;
BytesToSend = DeviceDescriptor.bLength;
if (BytesToSend > SetupPacket->wLength)
BytesToSend = SetupPacket->wLength;
WriteBufferToEndPoint();
break;
case TYPE_CONFIGURATION_DESCRIPTOR:
pSendBuffer = (unsigned char *)&ConfigurationDescriptor;
BytesToSend = sizeof(ConfigurationDescriptor);
if (BytesToSend > SetupPacket->wLength)
BytesToSend = SetupPacket->wLength;
WriteBufferToEndPoint();
break;
case TYPE_STRING_DESCRIPTOR:
switch (SetupPacket->wValue & 0xFF)
{
case 0 : pSendBuffer = (unsigned char *)&LANGID_Descriptor;
BytesToSend = sizeof(LANGID_Descriptor);
break;
case 1 : pSendBuffer = (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 Process_EP0_OUT_Interrupt(void)
{
unsigned char Buffer[2];
USB_SETUP_REQUEST SetupPacket;
/* Check if packet received is Setup or Data - Also clears IRQ */
D12_Cmd_read(D12_READ_LAST_TRANSACTION + D12_ENDPOINT_EP0_OUT, (unsigned char *)&SetupPacket, 1);
// for debug
enp0++;
lcd_command(0x8B);
printf(lcd_write,"P0:%x",enp0);
//
IF(SetupPacket.bmRequestType & D12_LAST_TRAN_SETUP)
{
// for debug
lcd_command(0x8B);
printf(lcd_write,"welcome");
//
/* This is a setup Packet - Read Packet */
D12ReadEndpoint(D12_ENDPOINT_EP0_OUT, (char *)&SetupPacket);
/* Acknowlegde Setup Packet to EP0_OUT & Clear Buffer*/
D12_Cmd_write(D12_ACK_SETUP, NULL, 0);
D12_Cmd_write(D12_CLEAR_BUFFER, NULL, 0);
/* Acknowlegde Setup Packet to EP0_IN */
D12_Cmd_write(D12_ENDPOINT_EP0_IN, NULL, 0);
D12_Cmd_write(D12_ACK_SETUP, NULL, 0);
/* Parse bmRequestType */
switch (SetupPacket.bmRequestType & 0x7F) {
case 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;
D12WriteEndpoint(D12_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:
DeviceAddress = SetupPacket.wValue | 0x80;
D12WriteEndpoint(D12_ENDPOINT_EP0_IN, NULL, 0);
CtlTransferInProgress = PROGRESS_ADDRESS;
break;
case GET_DESCRIPTOR:
GetDescriptor(&SetupPacket);
break;
case GET_CONFIGURATION:
D12WriteEndpoint(D12_ENDPOINT_EP0_IN, &DeviceConfigured, 1);
break;
case SET_CONFIGURATION:
DeviceConfigured = SetupPacket.wValue & 0xFF;
D12WriteEndpoint(D12_ENDPOINT_EP0_IN, NULL, 0);
if(DeviceConfigured)
LCD.RC0 = 1; /* turn on configure Led */
else
LCD.RC0 = 1; /* Device Not Configured */
break;
case SET_DESCRIPTOR:
default:
/* Unsupported - Request Error - Stall */
ErrorStallControlEndPoint();
break;
}
break;
case STANDARD_INTERFACE_REQUEST:
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;
D12WriteEndpoint(D12_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 */
D12WriteEndpoint(D12_ENDPOINT_EP0_IN, NULL, 0);
else
ErrorStallControlEndPoint();
break;
case GET_INTERFACE:
if(SetupPacket.wIndex == 0)
{ /* Interface Zero */
Buffer[0] = 0; /* Alternative Setting */
D12WriteEndpoint(D12_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:
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 : D12_Cmd_write(D12_SET_ENDPOINT_STATUS + \
D12_ENDPOINT_EP1_OUT, Buffer, 1);
break;
case 0x81 : D12_Cmd_write(D12_SET_ENDPOINT_STATUS + \
D12_ENDPOINT_EP1_IN, Buffer, 1);
break;
case 0x02 : D12_Cmd_write(D12_SET_ENDPOINT_STATUS + \
D12_ENDPOINT_EP2_OUT, Buffer, 1);
break;
case 0x82 : D12_Cmd_write(D12_SET_ENDPOINT_STATUS + \
D12_ENDPOINT_EP2_IN, Buffer, 1);
break;
default : /* Invalid Endpoint - RequestError */
ErrorStallControlEndPoint();
break;
}
D12WriteEndpoint(D12_ENDPOINT_EP0_IN, NULL, 0); /* zero packet */
}
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 : D12_Cmd_read(D12_READ_ENDPOINT_STATUS + \
D12_ENDPOINT_EP1_OUT, Buffer, 1);
break;
case 0x81 : D12_Cmd_read(D12_READ_ENDPOINT_STATUS + \
D12_ENDPOINT_EP1_IN, Buffer, 1);
break;
case 0x02 : D12_Cmd_read(D12_READ_ENDPOINT_STATUS + \
D12_ENDPOINT_EP2_OUT, Buffer, 1);
break;
case 0x82 : D12_Cmd_read(D12_READ_ENDPOINT_STATUS + \
D12_ENDPOINT_EP2_IN, Buffer, 1);
break;
default : /* Invalid Endpoint - RequestError */
ErrorStallControlEndPoint();
break;
}
if(Buffer[0] & 0x80)
Buffer[0] = 0x01;
else
Buffer[0] = 0x00;
Buffer[1] = 0x00;
D12WriteEndpoint(D12_ENDPOINT_EP0_IN, Buffer, 2);
break;
default:
/* Unsupported - Request Error for Endpoint- Stall */
ErrorStallControlEndPoint();
break;
}
break;
case VENDOR_DEVICE_REQUEST:
case VENDOR_ENDPOINT_REQUEST:
switch (SetupPacket.bRequest)
{
case VENDOR_GET_ANALOG_VALUE:
set_adc_channel((SetupPacket.wIndex & 0x07));
delay_us(10);
ADGO = 1;
while(ADGO);
Buffer[0] = ADRESL;
Buffer[1] = ADRESH;
D12WriteEndpoint(D12_ENDPOINT_EP0_IN, Buffer, 2);
break;
case VENDOR_SET_RB_HIGH_NIBBLE:
lcd_command(0xD4);
printf(lcd_write,"vender:%u",(SetupPacket.wIndex >> 4) );
/* zero length packet */
D12WriteEndpoint(D12_ENDPOINT_EP0_IN, NULL, 0);
break;
default:
ErrorStallControlEndPoint();
break;
}
break;
default:
ErrorStallControlEndPoint();
break;
}
}
else
{
///* This is a Data Packet */
}
}
#include "E:\ccs\pd12 cypress\TEST.h"
#include "USB_d12_defines.c"
#include "USB_variables.c"
#include "USB_18f452_defines.c"
#include "lcd_18F452.c"
//--------------------public variable-----------------------------//
unsigned int8 c = 0,b = 0,s = 0;
unsigned int8 enp0 = 0,enp1 = 0;
unsigned char circularbuffer[MAX_BUFFER_SIZE];
unsigned char inpointer;
unsigned char outpointer;
unsigned char * pSendBuffer;
unsigned char BytesToSend;
unsigned char CtlTransferInProgress;
unsigned char DeviceAddress;
unsigned char DeviceConfigured;
//---------------------define the usb descriptors-----------------//
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 */
ENP0_BUFFER_SIZE, /* bMaxPacketSize 8/16 Bytes */
0x04B4, /* USB Thermometer Example */
0x0002,
0x0000, /* bcdDevice */
1, /* iManufacturer String Index */
0, /* iProduct String Index */
0, /* iSerialNumber String Index */
1 /* bNumberConfigurations */
};
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 */
}
};
/* LANGID String Descriptor Zero */
USB_LANGID_DESCRIPTOR LANGID_Descriptor = {
sizeof(USB_LANGID_DESCRIPTOR), /* bLength */
TYPE_STRING_DESCRIPTOR, /* bDescriptorType */
0x0409 /* LANGID US English */
};
/* ManufacturerString 1 */
USB_MANUFACTURER_DESCRIPTOR Manufacturer_Descriptor = {
sizeof(USB_MANUFACTURER_DESCRIPTOR),/* bLenght */
TYPE_STRING_DESCRIPTOR, /* bDescriptorType */
"B0e0y0o0n0d0 0L0o0g0i0c" /* ManufacturerString in UNICODE */
};
//----------------------------------------------------------------//
#include "USB_d12_functions.c"
//----------------------------------------------------------------//
void Usb_int_isr(void)
{
unsigned char IRQ[2],temp[2];
unsigned char Buffer[ENP1_BUFFER_SIZE];
unsigned char count;
unsigned char bytes;
/* Read Interrupt Register to determine source of interrupt */
D12_Cmd_read(D12_READ_INTERRUPT_REGISTER,Irq,2);
lcd_command(0x80);
/* Find the source of intrrupt */
IF(Irq != 0)
{
/* busreset */
if(IRQ[0] & D12_INT_BUSRESET)
{ // for debug
b++;
lcd_command(0x80);
printf(lcd_write,"B:%3u",b);
D12_SUSPEND = 1;
//
temp[0] = 0x80;
D12_Cmd_write(D12_SET_ADDRESS_ENABLE,temp,1);
temp[0] = 1;
D12_Cmd_write(D12_SET_ENDPOINT_ENABLE,temp,1);
}
/* suspend change */
if(IRQ[0] & D12_INT_SUSPENDCHANGE)
{ // for debug
s++;
lcd_command(0x86);
printf(lcd_write,"S:%3u",s);
//
}
/* endpoint 0 out */
if(IRQ[0] & D12_INT_ENDP0OUT)
{
Process_EP0_OUT_Interrupt();
}
/* endpoint 0 in */
if(IRQ[0] & D12_INT_ENDP0IN)
{ // for debug
enp1++;
lcd_command(0xC0);
printf(lcd_write,"P1:%3u",enp1);
//
if(CtlTransferInProgress == PROGRESS_ADDRESS)
{
D12_Cmd_write(D12_SET_ADDRESS_ENABLE,&DeviceAddress,1);
D12_Cmd_read(D12_READ_LAST_TRANSACTION + D12_ENDPOINT_EP0_IN, Buffer, 1);
CtlTransferInProgress = PROGRESS_IDLE;
}
else
{
D12_Cmd_read(D12_READ_LAST_TRANSACTION + D12_ENDPOINT_EP0_IN, Buffer, 1);
WriteBufferToEndPoint();
}
}
/* endpoint 1 out */
if(IRQ[0] & D12_INT_ENDP1OUT)
{
D12_Cmd_read(D12_READ_LAST_TRANSACTION + D12_ENDPOINT_EP1_OUT, Buffer, 1);
bytes = D12ReadEndpoint(D12_ENDPOINT_EP1_OUT, Buffer);
for(count = 0; count < bytes; count++)
{
circularbuffer[inpointer++] = Buffer[count];
if(inpointer >= MAX_BUFFER_SIZE)
inpointer = 0;
}
loadfromcircularbuffer(); //Kick Start
}
/* endpoint 1 in */
if(IRQ[0] & D12_INT_ENDP1IN)
{
D12_Cmd_read(D12_READ_LAST_TRANSACTION + D12_ENDPOINT_EP1_IN, Buffer, 1);
loadfromcircularbuffer();
}
/* endpoint 2 out */
if(IRQ[0] & D12_INT_ENDP2OUT)
{ /* clear the intrrupt flag */
D12_Cmd_read(D12_READ_LAST_TRANSACTION + D12_ENDPOINT_EP2_OUT, Buffer, 1);
Buffer[0] = 0x01; /* Stall Endpoint */
D12_Cmd_write(D12_SET_ENDPOINT_STATUS + D12_ENDPOINT_EP2_OUT, Buffer, 1);
}
/* endpoint 2 in */
if(IRQ[0] & D12_INT_ENDP2IN)
{
D12_Cmd_read(D12_READ_LAST_TRANSACTION + D12_ENDPOINT_EP2_IN, Buffer, 1);
Buffer[0] = 0x01; /* Stall Endpoint */
D12_Cmd_write(D12_SET_ENDPOINT_STATUS + D12_ENDPOINT_EP2_IN, Buffer, 1);
}
}
else
{
c++; /* number of unknown intrrupt*/
lcd_command(0xD4);
printf(lcd_write,"unknown int:%3u",c);
}
}
//-------------------------------------------------------//
void main()
{ unsigned char b[2];
inpointer = 0;
outpointer = 0;
lcd_init();
usb_init();
LCD.RC0 = 0; //turn off configure led//
setup_adc_ports(AN0_AN1_AN2_AN3_AN4);
setup_adc(ADC_CLOCK_INTERNAL);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
disable_interrupts(GLOBAL);
delay_ms(2);
usb_reconnect();
while(1) //main loop
{
if(!D12_INT_N)
Usb_int_isr();
}
} |
|