📄 usb_isr.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 <stdio.h>
#include <string.h>
#include <drivers/ioport.h>
#include <drivers/usb.h>
#include <usbfull.h>
#include <ATJ2085_Ports.h>
void EP0SETUPInterrupt( void );
USB_DEVICE_DESCRIPTOR DeviceDescriptor = {
USB_DEVICE_DESCRIPTOR_SIZE, /* 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) */
0x0000, /* bcdDevice */
1, /* iManufacturer String Index */
0, /* iProduct String Index */
0, /* iSerialNumber String Index */
1 /* bNumberConfigurations */
};
#ifdef oldone
USB_CONFIG_DATA ConfigurationDescriptor = {
{ /* configuration descriptor */
USB_CONFIGURATION_DESCRIPTOR_SIZE, /* bLength */
TYPE_CONFIGURATION_DESCRIPTOR, /* bDescriptorType */
USB_CONFIG_DATA_SIZE, /* wTotalLength */
1, /* bNumInterfaces */
1, /* bConfigurationValue */
0, /* iConfiguration String Index */
0x80, /* bmAttributes Bus Powered, No Remote Wakeup */
0x32 /* bMaxPower, 100mA */
},
{ /* interface descriptor */
USB_INTERFACE_DESCRIPTOR_SIZE, /* 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 */
USB_ENDPOINT_DESCRIPTOR_SIZE, /* bLength */
TYPE_ENDPOINT_DESCRIPTOR, /* bDescriptorType */
0x01, /* bEndpoint Address EP1 OUT */
0x02, /* bmAttributes - Interrupt */
0x0008, /* wMaxPacketSize */
0x00 /* bInterval */
},
{ /* endpoint descriptor */
USB_ENDPOINT_DESCRIPTOR_SIZE, /* bLength */
TYPE_ENDPOINT_DESCRIPTOR, /* bDescriptorType */
0x81, /* bEndpoint Address EP1 IN */
0x02, /* bmAttributes - Interrupt */
0x0008, /* wMaxPacketSize */
0x00 /* bInterval */
}
};
#endif
#define SIZEOF_CONFIG_DESCRIPTOR 32
static unsigned char ConfigurationDescriptor[]=
{ 0x09, TYPE_CONFIGURATION_DESCRIPTOR, SIZEOF_CONFIG_DESCRIPTOR, 0x00, 0x01, 0x01, 0x00, 0x80, 0x32,
0x09, TYPE_INTERFACE_DESCRIPTOR, 0x00, 0x00, 0x02, 0x08, 0x05, 0x50, 0x00,
0x07, TYPE_ENDPOINT_DESCRIPTOR, 0x01, 0x02, 0x40, 0x00, 0x00,
0x07, TYPE_ENDPOINT_DESCRIPTOR, 0x82, 0x02, 0x40, 0x00, 0x00 };
LANGID_DESCRIPTOR LANGID_Descriptor = { /* LANGID String Descriptor Zero */
LANGID_DESCRIPTOR_SIZE, /* bLength */
TYPE_STRING_DESCRIPTOR, /* bDescriptorType */
0x0409 /* LANGID US English */
};
MANUFACTURER_DESCRIPTOR ManufacturerDescriptor = { /* ManufacturerString 1 */
MANUFACTURER_DESCRIPTOR_SIZE, /* bLength */
TYPE_STRING_DESCRIPTOR, /* bDescriptorType */
"B\0e\0y\0o\0n\0d\0 l\0o\0g\0i\0c\0" /* ManufacturerString in UNICODE */
};
#define MAX_BUFFER_SIZE 80
unsigned char irqrcvd = 0xFF;
unsigned char circularbuffer[MAX_BUFFER_SIZE];
unsigned char inpointer;
unsigned char outpointer;
unsigned char *pSendBuffer;
unsigned char BytesToSend;
unsigned char CtlTransferInProgress;
unsigned char USBDeviceAddress = 0xAA;
unsigned char DeviceConfigured;
static unsigned char IPM_IDM_FB_Keeper;
static unsigned char ACK_Request_Sense_command = 0;
static unsigned char First_inquiry_command_flag = 0;
static unsigned char ConfigValue = 0;
static unsigned char AlternateSetting = 0;
static unsigned char USBPowerConnected = 0;
static unsigned char EP0DataCount;
unsigned char UramXAddr[30];
static unsigned int SPKeeper;
static unsigned int DeviceStatus = 0;
static unsigned int InterfaceStatus = 0;
static unsigned int EndPoint1OUTStatus = 0;
static unsigned int EndPoint1INStatus = 0;
#define PROGRESS_IDLE 0
#define PROGRESS_ADDRESS 3
#ifdef NOPE
void D11GetIRQ(void)
{
if (Irq & USB_EP_CONTROL_READ)
{
if (CtlTransferInProgress == PROGRESS_ADDRESS)
{
D11CmdDataWrite(D11_SET_ADDRESS_ENABLE,&USBDeviceAddress,1);
D11CmdDataRead(D11_READ_LAST_TRANSACTION + D11_ENDPOINT_EP0_IN, Buffer, 1);
IOPORT_Set( USB_DEVADDR_REG, USB_DEVADDR_ENABLE + USBDeviceAddress );
CtlTransferInProgress = PROGRESS_IDLE;
}
else
{
D11CmdDataRead(D11_READ_LAST_TRANSACTION + D11_ENDPOINT_EP0_IN, Buffer, 1);
WriteBufferToEP0();
}
}
if (Irq & USB_EP_ENDPOINT1_OUT)
{
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 & USB_EP_ENDPOINT1_IN)
{
D11CmdDataRead(D11_READ_LAST_TRANSACTION + D11_ENDPOINT_EP1_IN, Buffer, 1);
loadfromcircularbuffer();
}
}
#endif
void ConfigureStall_EP1_OUT( void )
{
#asm
ConfigureStall_EP1_OUT:
ld bc,01bfh ; Stall EP1_OUT
call SetEp1OutMode ; b->(USB_EPI_MODE_REG), c->(USB_EPI_MPS_REG)
xor a
out (USB_EPI_CNTR_HI_REG),a
ld a,64
out (USB_EPI_CNTR_LO_REG),a ;Stall
jp ConfigureStall_EP1_OUT_end
;----------------------------------------------------------------------
; b->(USB_EPI_MODE_REG), c->(USB_EPI_MPS_REG)
SetEp1OutMode:
ld a,USB_EPI_ENDPOINT1_OUT
out (USB_EPI_REG),a ; EPI=EP1_OUT
ld a,b
out (USB_EPI_MODE_REG),a
ld a,c
out (USB_EPI_MPS_REG),a
xor a
out (USB_EPI_START_ADDR_HI_REG),a
out (USB_EPI_START_ADDR_LO_REG),a
ld a,USB_EP_ENDPOINT1_OUT
out (USB_EP_STATUS_REG),a
ret
ConfigureStall_EP1_OUT_end:
#endasm
}
void ConfigureStall_EP1_IN( void )
{
#asm
ConfigureStall_EP1_IN:
ld bc,01bfh ; Stall EP1_IN
call SetEp1InMode ; b->(USB_EPI_MODE_REG), c->(USB_EPI_MPS_REG)
xor a
out (USB_EPI_CNTR_HI_REG),a
ld a,64
out (USB_EPI_CNTR_LO_REG),a ; Stall
jp ConfigureStall_EP1_IN_end
;----------------------------------------------------------------------
; b->(USB_EPI_MODE_REG), c->(USB_EPI_MPS_REG)
SetEp1InMode:
ld a,USB_EPI_ENDPOINT1_IN
out (USB_EPI_REG),a ; EPI=EP1_IN
ld a,b ; Stall EP1_OUT
out (USB_EPI_MODE_REG),a
ld a,c
out (USB_EPI_MPS_REG),a
xor a
out (USB_EPI_START_ADDR_HI_REG),a
out (USB_EPI_START_ADDR_LO_REG),a
ld a,USB_EP_ENDPOINT1_IN
out (USB_EP_STATUS_REG),a
ret
ConfigureStall_EP1_IN_end:
#endasm
}
void USBResetISR( void )
{
#asm
;----------------------------------------------------------------------
; USB Reset interrupt service routine
xor a
ld (_ACK_Request_Sense_command),a
ld (_First_inquiry_command_flag),a
ld (_ConfigValue),a
ld (_AlternateSetting),a
ld hl,0
ld (_DeviceStatus),hl
ld (_InterfaceStatus),hl
ld (_EndPoint1OUTStatus),hl
ld (_EndPoint1INStatus),hl
ld a,USB_DEVADDR_ENABLE ; enable USB SIE with address 0
out (USB_DEVADDR_REG),a
USBResetISR_ClearBR:
ld a, USB_EPI_CONTROL_READ
out (USB_EPI_REG),a ; EPI=Control_read
out (USB_EPI_CNTR_HI_REG),a
ld a, USB_EPI_CONTROL_WRITE
out (USB_EPI_REG),a ; EPI=Control_write
out (USB_EPI_CNTR_HI_REG),a
ld a, USB_EPI_ENDPOINT1_IN
out (USB_EPI_REG),a ; EPI=EP1_IN
out (USB_EPI_CNTR_HI_REG),a
ld a, USB_EPI_ENDPOINT1_OUT
out (USB_EPI_REG),a ; EPI=EP1_OUT
out (USB_EPI_CNTR_HI_REG),a
ld a, USB_EPI_ENDPOINT2_IN
out (USB_EPI_REG),a ; EPI=EP2_IN
out (USB_EPI_CNTR_HI_REG),a
ld a, USB_EPI_ENDPOINT2_OUT
out (USB_EPI_REG),a ; EPI=EP2_OUT
out (USB_EPI_CNTR_HI_REG),a
ld a, USB_EPI_ENDPOINT3_IN
out (USB_EPI_REG),a ; EPI=EP3_IN
out (USB_EPI_CNTR_HI_REG),a
ld a, USB_EPI_ENDPOINT3_OUT
out (USB_EPI_REG),a ; EPI=EP3_OUT
out (USB_EPI_CNTR_HI_REG),a
in a,(USB_EPI_EPSBR_REG)
cp 0
jr nz, USBResetISR_ClearBR
ld a,0xff ; Clear all pending interrupts
out (USB_INT_STATUS_REG),a
out (USB_EP_STATUS_REG),a
out (USB_EPI_EPSNS_REG),a
out (USB_EPI_EPSST_REG),a
#endasm
}
void USBConnectISR( void )
{
#asm
in a, (USB_STATUS_CONTROL_REG)
bit 6, a ; USB_STATUS_POWER
jr nz, USBConnectISR_Connected
xor a
ld (_USBPowerConnected),a
jp USBConnectISR_Exit
USBConnectISR_Connected:
ld a,0x01
ld (_USBPowerConnected),a
USBConnectISR_Exit:
#endasm
}
void USB_ISREP_ControlRead( void )
{
irqrcvd = 0x04;
#ifdef crap
if (CtlTransferInProgress == PROGRESS_ADDRESS)
{
#asm
ld a,(_USBDeviceAddress)
or USB_DEVADDR_ENABLE
out (USB_DEVADDR_REG),a ; Enable device with address
#endasm
/* IOPORT_Set( USB_DEVADDR_REG, USB_DEVADDR_ENABLE + USBDeviceAddress );*/
CtlTransferInProgress = PROGRESS_IDLE;
}
else
{
WriteBufferToEP0();
}
#endif
}
void USB_ISR( void )
{
#asm
in a,(INTERNAL_MROM_SRAM_PAGE_REG)
ld (_IPM_IDM_FB_Keeper),a
ld (_SPKeeper),sp
in a,(USB_INT_STATUS_REG)
USB_ISR_Reset:
bit 7,a ;USB_INT_BUS_RESET
jp z, USB_ISR_EP0Setup
call _USBResetISR
ld a,USB_INT_BUS_RESET
out (USB_INT_STATUS_REG),a
jp USB_ISR_End
USB_ISR_EP0Setup:
bit 5,a ;USB_INT_EP0_SETUP
jp z, USB_ISR_Connect
call _EP0SETUPInterrupt
ld a,USB_INT_EP0_SETUP
out (USB_INT_STATUS_REG),a
jp USB_ISR_End
USB_ISR_Connect:
bit 6,a ;USB_INT_BUS_CON_DISC
jp z, USB_ISR_Wakeup
call _USBConnectISR
in a,(USB_INT_STATUS_REG)
or USB_INT_BUS_CON_DISC
out (USB_INT_STATUS_REG),a
jp USB_ISR_End
USB_ISR_Wakeup:
bit 4,a ;USB_INT_WAKEUP_IRQ
jp z, USB_ISR_EndPoint
ld a,USB_INT_WAKEUP_IRQ ; Clear the interrupt
out (USB_INT_STATUS_REG),a
jp USB_ISR_End
USB_ISR_EndPoint:
in a,(USB_EP_STATUS_REG)
bit 0,a ; USB_EP_CONTROL_READ
jp z, USB_ISR_EndPoint3
call _USB_ISREP_ControlRead
jp USB_ISR_End
USB_ISR_EndPoint3:
; in a,(USB_EP_STATUS_REG)
; bit 3,a ;USB_EP_ENDPOINT1_OUT
; jp nz, EP1_OUT_ISR
; ld a,0ffh ; All Irqs
; out (USB_EP_STATUS_REG),a
USB_ISR_End:
ld sp,(_SPKeeper)
ld a,(_IPM_IDM_FB_Keeper)
out (INTERNAL_MROM_SRAM_PAGE_REG),a
ld a,0ffh ; All Irqs
out (USB_EP_STATUS_REG),a
ld a, 0xff
out (USB_INT_STATUS_REG), a
in a, (USB_IRQSTATUS_REG)
out (USB_IRQSTATUS_REG), a
#endasm
}
void EP0StandardDeviceRequest( void )
{
unsigned char ucEP0DataReg1;
unsigned char ucEP0DataReg2;
unsigned char Buffer[2];
ucEP0DataReg1 = IOPORT_Read( USB_SETUP_DATA1_REG );
ucEP0DataReg2 = IOPORT_Read( USB_SETUP_DATA2_REG );
switch (ucEP0DataReg1)
{
case GET_STATUS:
/* Get Status Request to Device should return */
/* Remote Wakeup and Self Powered Status */
Buffer[0] = 0x01;
Buffer[1] = 0x00;
WriteEP0( Buffer,2 );
break;
case CLEAR_FEATURE:
case SET_FEATURE:
/* We don't support DEVICE_REMOTE_WAKEUP or TEST_MODE */
ErrorStallControlEndPoint();
break;
case SET_ADDRESS:
USBDeviceAddress = ucEP0DataReg2;
WriteEP0( NULL, 0 );
CtlTransferInProgress = PROGRESS_ADDRESS;
break;
case GET_DESCRIPTOR:
GetDescriptor();
break;
case GET_CONFIGURATION:
WriteEP0( &DeviceConfigured, 1 );
break;
case SET_CONFIGURATION:
DeviceConfigured = ucEP0DataReg2;
WriteEP0( NULL, 0 );
break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -