📄 usb_isr.c
字号:
case SET_DESCRIPTOR:
default:
/* Unsupported - Request Error - Stall */
ErrorStallControlEndPoint();
break;
}
}
void EP0StandardInterfaceRequest( void )
{
unsigned char ucEP0DataReg1;
unsigned char ucEP0DataReg2;
unsigned char ucEP0DataReg4;
unsigned char dummy_variable_for_stack_alignment;
unsigned char Buffer[2];
ucEP0DataReg1 = IOPORT_Read( USB_SETUP_DATA1_REG );
ucEP0DataReg2 = IOPORT_Read( USB_SETUP_DATA2_REG );
ucEP0DataReg4 = IOPORT_Read( USB_SETUP_DATA4_REG );
switch (ucEP0DataReg1)
{
case GET_STATUS:
/* Get Status Request to Interface should return */
/* Zero, Zero (Reserved for future use) */
Buffer[0] = 0x00;
Buffer[1] = 0x00;
WriteEP0( Buffer, 2 );
break;
case SET_INTERFACE:
/* Device Only supports default setting, Stall may be */
/* returned in the status stage of the request */
if (ucEP0DataReg2 == 0 && ucEP0DataReg4 == 0)
{
/* Interface Zero, Alternative Setting = 0 */
WriteEP0( NULL, 0 );
}
else
{
ErrorStallControlEndPoint();
}
break;
case GET_INTERFACE:
if (ucEP0DataReg4 == 0)
{ /* Interface Zero */
Buffer[0] = 0; /* Alternative Setting */
WriteEP0( Buffer, 1 );
break;
} /* else fall through as RequestError */
case CLEAR_FEATURE:
case SET_FEATURE:
/* Interface has no defined features. Return RequestError */
default:
ErrorStallControlEndPoint();
break;
}
}
void EP0StandardEndpointRequest( void )
{
unsigned char ucEP0DataReg1;
unsigned char ucEP0DataReg2;
unsigned char ucEP0DataReg4;
unsigned char ucEP0DataReg5;
unsigned char dummy_variable_for_stack_alignment;
unsigned char Buffer[2];
ucEP0DataReg1 = IOPORT_Read( USB_SETUP_DATA1_REG );
ucEP0DataReg2 = IOPORT_Read( USB_SETUP_DATA2_REG );
ucEP0DataReg4 = IOPORT_Read( USB_SETUP_DATA4_REG );
ucEP0DataReg5 = IOPORT_Read( USB_SETUP_DATA5_REG );
switch (ucEP0DataReg1)
{
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 (ucEP0DataReg2 == ENDPOINT_HALT)
{
if (ucEP0DataReg1 == CLEAR_FEATURE)
{
Buffer[0] = 0x00;
}
else
{
Buffer[0] = 0x01;
}
switch (ucEP0DataReg4)
{
case 0x01 :
EndPoint1OUTStatus = 0x01; /* Enabled */
ConfigureStall_EP1_OUT();
break;
case 0x81 :
EndPoint1INStatus = 0x01; /* Enabled */
ConfigureStall_EP1_IN();
break;
default :
/* Invalid Endpoint - RequestError */
ErrorStallControlEndPoint();
break;
}
WriteEP0( 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 (ucEP0DataReg4)
{
case 0x01 :
Buffer[0] = EndPoint1OUTStatus;
break;
case 0x81 :
Buffer[0] = EndPoint1INStatus;
break;
default :
/* Invalid Endpoint - RequestError */
ErrorStallControlEndPoint();
break;
}
Buffer[1] = 0x00;
WriteEP0( Buffer, 2);
break;
default:
/* Unsupported - Request Error - Stall */
ErrorStallControlEndPoint();
break;
}
}
void EP0SETUPInterrupt( void )
{
irqrcvd = 0x02;
/* unsigned char dummy_variable_for_stack_alignment;
unsigned char Buffer[2];
unsigned char ucEP0DataReg0;*/
/* ucEP0DataReg0 = IOPORT_Read( USB_SETUP_DATA0_REG );
*/
/* switch (ucEP0DataReg0&0x7F)*/
/*#ifdef crap*/
switch (0x01)
{
case STANDARD_DEVICE_REQUEST:
irqrcvd |= 0x01;
EP0StandardDeviceRequest();
break;
case STANDARD_INTERFACE_REQUEST:
irqrcvd |= 0x02;
EP0StandardInterfaceRequest();
break;
case STANDARD_ENDPOINT_REQUEST:
irqrcvd |= 0x04;
EP0StandardEndpointRequest();
break;
default:
irqrcvd |= 0x08;
ErrorStallControlEndPoint();
break;
}
/* #endif*/
}
void GetDescriptor( void )
{
unsigned char ucEP0DataReg2;
unsigned char ucEP0DataReg3;
unsigned char ucEP0DataReg6;
ucEP0DataReg2 = IOPORT_Read( USB_SETUP_DATA2_REG );
ucEP0DataReg3 = IOPORT_Read( USB_SETUP_DATA3_REG );
ucEP0DataReg6 = IOPORT_Read( USB_SETUP_DATA6_REG );
switch(ucEP0DataReg3)
{
case TYPE_DEVICE_DESCRIPTOR:
/* pSendBuffer = (unsigned char *)&DeviceDescriptor;*/
pSendBuffer = DeviceDescriptor;
BytesToSend = DeviceDescriptor.bLength;
if (BytesToSend > ucEP0DataReg6)
BytesToSend = ucEP0DataReg6;
WriteBufferToEP0();
break;
case TYPE_CONFIGURATION_DESCRIPTOR:
pSendBuffer = ConfigurationDescriptor;
BytesToSend = sizeof(ConfigurationDescriptor);
if (BytesToSend > ucEP0DataReg6)
BytesToSend = ucEP0DataReg6;
WriteBufferToEP0();
break;
case TYPE_STRING_DESCRIPTOR:
switch (ucEP0DataReg2)
{
case 0 :
/* pSendBuffer = (unsigned char*)&LANGID_Descriptor;*/
pSendBuffer = LANGID_Descriptor;
BytesToSend = sizeof(LANGID_Descriptor);
break;
case 1 :
/* pSendBuffer = (unsigned char *)&ManufacturerDescriptor; */
pSendBuffer = ManufacturerDescriptor;
BytesToSend = sizeof(ManufacturerDescriptor);
break;
default : pSendBuffer = NULL;
BytesToSend = 0;
}
if (BytesToSend > ucEP0DataReg6)
BytesToSend = ucEP0DataReg6;
WriteBufferToEP0();
break;
default:
ErrorStallControlEndPoint();
break;
}
}
void ErrorStallControlEndPoint(void)
{
#asm
ld b, USB_EPI_MODE_STALL
ld c, 0xc7 ; data = 8 | USB_EPI_MPS_FORCE_TOGGLE | USB_EPI_MPS_CURRENT_TOGGLE
call SetEp0WrMode ; b->(USB_EPI_MODE_REG), c->(USB_EPI_MPS_REG)
xor a
out (USB_EPI_CNTR_LO_REG),a
jp end_ErrorStallControlEndPoint
;----------------------------------------------------------------------
; b->(USB_EPI_MODE_REG), c->(USB_EPI_MPS_REG)
SetEp0WrMode:
ld a,USB_EPI_CONTROL_WRITE
out (USB_EPI_REG),a ; EPI=Control-write
ld a,b
out (USB_EPI_MODE_REG),a ; STALL IN/OUT
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
out (USB_EPI_CNTR_HI_REG),a
ret
end_ErrorStallControlEndPoint:
#endasm
}
void WriteEP0( unsigned char *Buffer, unsigned char Bytes )
{
/*
Buffer = 0x1234;
Bytes = 2;
;Buffer = 0x1234;
LINE 710
ld hl,4 ;const
add hl,sp
ld de,4660 ;const
ex de,hl
call l_pint
;Bytes = 2;
LINE 711
ld hl,2 ;const
add hl,sp
ld (hl),#(2 % 256 % 256)
ld l,(hl)
ld h,0
*/
#asm
; a = Bytes
ld hl,2 ;const
add hl,sp
ld a,(hl)
; hl = Buffer
ld hl,4 ;const
add hl,sp
ld d, (hl)
dec hl
ld e, (hl)
ex de,hl
;----------------------------------------------------------------------
; Perform control read operation
; hl: start position
; a: size of the data should be returned
;----------------------------------------------------------------------
control_read:
call get_descriptor_length
ld c,a
ld b,0
ld a,0f7h
out (INTERNAL_MROM_SRAM_PAGE_REG),a
in a,(B1_2_MEMMAP_REG)
or B1_2_MEMMAP_B1_B2
out (B1_2_MEMMAP_REG),a
ld de,_UramXAddr
ldir
in a,(B1_2_MEMMAP_REG)
and 0xcf
or B1_2_MEMMAP_B2
out (B1_2_MEMMAP_REG),a
; ld bc,03c7h ; complete=1,stall=1
ld b, USB_EPI_MODE_COMPLETE | USB_EPI_MODE_STALL
; MPS = 8 | USB_EPI_MPS_FORCE_TOGGLE | USB_EPI_MPS_CURRENT_TOGGLE
ld c, USB_EPI_MPS_FORCE_TOGGLE | USB_EPI_MPS_CURRENT_TOGGLE | 7
call sSet_Ep0_Rd_Mode ; b->(USB_EPI_MODE_REG), c->(USB_EPI_MPS_REG)
ld a,(_EP0DataCount)
call sWait_Ep0_Send_Out
jr lsControlReadRet
lsPremature_Terminate:
lsControlReadRet:
jp ControlRead_end
;*******************************************************************************
;* Subroutine
;* b->(USB_EPI_MODE_REG), c->(USB_EPI_MPS_REG)
;*******************************************************************************
sSet_Ep0_Rd_Mode:
xor a ; a = USB_EPI_CONTROL_READ
out (USB_EPI_REG),a ; EPI=Control-read
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
out (USB_EPI_CNTR_HI_REG),a
ret
;----------------------------------------------------------------------
get_descriptor_length:
ld (_EP0DataCount),a
ld b,a
in a,(USB_SETUP_DATA7_REG)
or a
jr nz,use_actual_length
in a,(USB_SETUP_DATA6_REG)
or a
jr z,use_actual_length
cp b
jr nc,use_actual_length
ld (_EP0DataCount),a
use_actual_length:
ld a,(_EP0DataCount)
ret
;----------------------------------------------------------------------
sWait_Ep0_Send_Out:
out (USB_EPI_CNTR_LO_REG),a
Check_Control_read_BR_Set:
in a,(USB_EPI_EPSBR_REG)
bit 0,a ; USB_EPI_EPSBR_CTL_READ
jr z,Check_Control_read_BR_Set
Check_Control_read_BR_Clear:
in a,(USB_EPI_EPSBR_REG)
bit 0,a ; USB_EPI_EPSBR_CTL_READ
jr nz,Check_Control_read_BR_Clear
ld a,USB_EP_CONTROL_READ
out (USB_EP_STATUS_REG),a
ret
ControlRead_end:
#endasm
}
/*
void WriteEP1( unsigned char *Buffer, unsigned char Bytes )
{
}
*/
void WriteBufferToEP0( 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 Length packet */
WriteEP0( NULL, 0);
} else if (BytesToSend >= 8) {
/* Write another 8 Bytes to buffer and send */
WriteEP0( pSendBuffer, 8);
pSendBuffer += 8;
BytesToSend -= 8;
} else {
/* Buffer must have less than 8 bytes left */
WriteEP0( pSendBuffer, BytesToSend);
BytesToSend = 0;
}
}
#ifdef crap
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 */
WriteEP1IN( Buffer, count);
}
}
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -