?? usb.c
字號:
#include "usb.h" // Definitions for USB Data Types & Constants
#include "usbdesc.c" // const USB Descriptor Data
#define b_EP0STALL 6
#define b_DPPULLEN 2
#pragma DATA_SEG MY_ZEROPAGE
//variable declare
unsigned char USB_Status=0;
unsigned char Status=0;
unsigned char EP1234_Status=0;
unsigned char SetupSize;
unsigned char const *SetupDataPtr;
unsigned char usb_out[8]; //usb output data buffercde
extern BYTE macCurrentChannel;
extern BYTE macWarncode;
#pragma DATA_SEG DEFAULT
unsigned char IdleRate_Table=0,Protocol_Table=1;
unsigned char UsbIdleFlag=0;
static BYTE In_PacketKb[8]={0,0,0,0,0,0,0,0} ;
static BYTE In_PacketMs[MOUSELENGTH]={0,0,0,0,0,0} ;
static BYTE In_PacketDev[MAXDEVLENGTH] ;
static BYTE Out_Length=0;
static BYTE In_Packet[8],Inlength ;
setup_buffer SetupBuffer;
BYTE SendUsbjkData(unsigned char *buffer,unsigned char length);
//;**************************************************************************
//;
//; for 48MHZ CGMCLK, 8MHZ bus, 4MHz reference clk
//;
//;**************************************************************************
void PLL_Start(void) {
__asm {
;R=1, N=06, P=1, E=2, L=96
clra
sta PCTL ;PTCL
sta PMSH ;PMSH; N high
lda #6
sta PMSL ;PMSL; N low
lda #96
sta PMRS ;PMRS; L
lda #1
sta PMDS ;PMDS; RDS
lda #$80
sta PBWC ;PBWC; AUTO
lda #$06
sta PCTL ;PLLON, E=2 and P=1
lda #$26
sta PCTL ;PLLON, E=2 and P=1
AUTO_LP:
lda PBWC
cbeqa #$80, AUTO_LP ;LOCK?
; jsr Delay_3ms_PLL ;constant delay
lda #$36
sta PCTL ;BCS
rts
}
}
//;**************************************************************************
//;
//; USB Initialisation
//;
//;**************************************************************************
void USB_EN(void) {
__asm{
; clr USB_Status ;Init Software flag
; clr Status ;Init Software flag
; clr EP1234_Status ;Init Software flag
bset b_DPPULLEN, POCR2 ;DP pullup enable
mov #$54, UINTFCR ;EP1=INTF0
;EP2=INTF1
;EP3=INTF2
;EP4=INTF3
mov #$20, UEP12BPR ;EP1=$1000
;EP2=$1010
mov #$64, UEP34BPR ;EP3=$1020
;EP4=$1030
;--- BULK OUT Testing ---
; mov #$84, UEP1CSR ;16byte buffer
; ;BULK OUT EP
; mov #$84, UEP2CSR ;16byte buffer
; ;BULK OUT EP
; mov #$84, UEP3CSR ;16byte buffer
; ;BULK OUT EP
; mov #$84, UEP4CSR ;16byte buffer
; ;BULK OUT EP
;------------------------
;--- BULK IN Testing ---
; mov #$94, UEP1CSR ;16byte buffer
; ;BULK IN EP
; mov #$94, UEP2CSR ;16byte buffer
; ;BULK IN EP
; mov #$94, UEP3CSR ;16byte buffer
; ;BULK IN EP
; mov #$94, UEP4CSR ;16byte buffer
; ;BULK IN EP
;------------------------
;--- Interrupt IN Testing ---
mov #$d4, UEP1CSR ;16byte buffer
;INTERRUPT IN EP
mov #$d4, UEP2CSR ;16byte buffer
;INTERRUPT IN EP
mov #$d4, UEP3CSR ;16byte buffer
;INTERRUPT in EP mov #$d4, UEP4CSR ;16byte buffer
mov #$d4, UEP4CSR ;16byte buffer
;INTERRUPT IN EP
;----------------------------
mov #$0f, USIMR ;b0,SUSPNDIEN=1
;b1,RESUMEFIEN=1
;b2,USBRSTIEN=1
;b3,CONFIG_CHGIEN=1
;b4,SOFIEN=0
;b5,SETUPIEN=0
mov #$fe, USBCR ;b0,RESUME=0
;b1,TC0IEN=1
;b2,TC1IEN=1
;b3,TC2IEN=1
;b4,TC3IEN=1
;b5,TC4IEN=1
;b6,USBCLKEN=1
;b7,USEN=1
;--- IN Testing
; ldx #EP1_Size
;Next_Byte1:
; lda DataIN_1-1, x
; sta EP1_Base-1, x
; dbnzx Next_Byte1
; mov #EP1_Size, UEP1DSR
; bset b_DVALID, UEP1CSR
;--------------
;--- IN Testing
; ldx #EP2_Size
;Next_Byte2:
; lda DataIN_2-1, x
; sta EP2_Base-1, x
; dbnzx Next_Byte2
; mov #EP2_Size, UEP2DSR
; bset b_DVALID, UEP2CSR
;--------------
;--- IN Testing
; ldx #EP3_Size
;Next_Byte3:
; lda DataIN_3-1, x
; sta EP3_Base-1, x
; dbnzx Next_Byte3
; mov #EP3_Size, UEP3DSR
; bset b_DVALID, UEP3CSR
;--------------
;--- IN Testing
; ldx #EP4_Size
;Next_Byte4:
; lda DataIN_4-1, x
; sta EP4_Base-1, x
; dbnzx Next_Byte4
; mov #EP4_Size, UEP4DSR
; bset b_DVALID, UEP4CSR
;--------------
}
UsbIdleFlag = 1;
USB_Status = 2;
}
//-------------------------------------------------------------------
// Delay n ms with 8MHz bus
//-------------------------------------------------------------------
void Wait_x_us_8MHz_Bus(byte Delay) {
__asm {
lda Delay
deca ;compensate for the overhead
deca ;compensate for the overhead
loop:
cmp 1,SP ;(5) @8MHz
dbnza loop ;(3) @8MHz
}
}
void forceSTALL(void)
{
__asm
{
bset b_EP0STALL, USIMR
ldx #5
W_AGAIN: dbnzx W_AGAIN ;Delay 2.1us @ 8MHz
bset b_EP0STALL, USIMR
}
}
void SynchFrame(void)
{
forceSTALL();
UEP0CSR=0;
}
//;**************************************************************************
//;
//; USB Processor subroutine
//;
//;**************************************************************************
void getDescriptor()
{
byte n;
byte *dest;
switch(SetupBuffer.wValue.hi) {
case DT_DEVICE: // 1 Get Device Descriptor
SetupDataPtr = (byte *)&DeviceDesc;
SetupSize = DeviceDesc.bLength;
break;
case DT_CONFIGURATION: // 2 Get Configuration Descriptor
SetupDataPtr = (byte *)&ConfigDesc;
SetupSize = ConfigDesc.hid_configuration_descriptor.wTotalLength.lo;
break;
case DT_STRING: // 3 Get String Descriptor
// ### Table Index Boundary should be checked
SetupDataPtr = StringDescTable[SetupBuffer.wValue.lo];
SetupSize = *SetupDataPtr;
break;
case DT_HID: // 0x21 Get HID Descriptor
/*
if(SetupBuffer.wIndex.lo==0)
{
SetupDataPtr = (byte *)&ConfigDesc.mouse_hid_descriptor;
SetupSize = ConfigDesc.mouse_hid_descriptor.bLength;
}
else if(SetupBuffer.wIndex.lo==1)
{
SetupDataPtr = (byte *)&ConfigDesc.keyboard_hid_descriptor;
SetupSize = ConfigDesc.keyboard_hid_descriptor.bLength;
}
else if(SetupBuffer.wIndex.lo==2)
{
SetupDataPtr = (byte *)&ConfigDesc.hid_hid_descriptor;
SetupSize = ConfigDesc.hid_hid_descriptor.bLength;
}
*/
switch(SetupBuffer.wIndex.lo)
{
case 0:
SetupDataPtr = (byte *)&ConfigDesc.mouse_hid_descriptor;
SetupSize = ConfigDesc.mouse_hid_descriptor.bLength;
break;
case 1:
SetupDataPtr = (byte *)&ConfigDesc.keyboard_hid_descriptor;
SetupSize = ConfigDesc.keyboard_hid_descriptor.bLength;
break;
case 2:
SetupDataPtr = (byte *)&ConfigDesc.hid_hid_descriptor;
SetupSize = ConfigDesc.hid_hid_descriptor.bLength;
break;
case 3:
SetupDataPtr = (byte *)&ConfigDesc.joystick_hid_descriptor;
SetupSize = ConfigDesc.joystick_hid_descriptor.bLength;
break;
case 4:
SetupDataPtr = (byte *)&ConfigDesc.joystick_hid_descriptor1;
SetupSize = ConfigDesc.joystick_hid_descriptor1.bLength;
break;
default:
forceSTALL();
break;
}
break;
case DT_REPORT: // 0x22 Get Report Descriptor
/*
if(SetupBuffer.wIndex.lo==0)
{
SetupDataPtr = (byte *)&HidReportDesc0;
SetupSize = ConfigDesc.mouse_hid_descriptor.wDescriptorLength.lo;
}
else if(SetupBuffer.wIndex.lo==1)
{
SetupDataPtr = (byte *)&HidReportDesc1;
SetupSize = ConfigDesc.keyboard_hid_descriptor.wDescriptorLength.lo;
}
else if(SetupBuffer.wIndex.lo==2)
{
SetupDataPtr = (byte *)&HidReportDesc2;
SetupSize = ConfigDesc.hid_hid_descriptor.wDescriptorLength.lo;
}
*/
switch(SetupBuffer.wIndex.lo)
{
case 0:
SetupDataPtr = (byte *)&HidReportDesc0;
SetupSize = ConfigDesc.mouse_hid_descriptor.wDescriptorLength.lo;
break;
case 1:
SetupDataPtr = (byte *)&HidReportDesc1;
SetupSize = ConfigDesc.keyboard_hid_descriptor.wDescriptorLength.lo;
break;
case 2:
SetupDataPtr = (byte *)&HidReportDesc2;
SetupSize = ConfigDesc.hid_hid_descriptor.wDescriptorLength.lo;
break;
case 3:
SetupDataPtr = (byte *)&HidReportDesc3;
SetupSize = sizeof(HidReportDesc3);//ConfigDesc.joystick_hid_descriptor.wDescriptorLength.lo;
break;
case 4:
SetupDataPtr = (byte *)&HidReportDesc4;
SetupSize = ConfigDesc.joystick_hid_descriptor1.wDescriptorLength.lo;
break;
default:
forceSTALL();
break;
}
break;
default:
forceSTALL();
break;
}
if( SetupBuffer.wValue.hi == DT_DEVICE ||
SetupBuffer.wValue.hi == DT_CONFIGURATION ||
SetupBuffer.wValue.hi == DT_STRING ||
SetupBuffer.wValue.hi == DT_HID ||
SetupBuffer.wValue.hi == DT_REPORT ){
// check if requested Length is less than Descriptor Length
if((SetupBuffer.wLength.lo < SetupSize) && (SetupBuffer.wLength.hi == 0))
SetupSize = SetupBuffer.wLength.lo;
// copy (up to) 8 Bytes to EP0 Data Registers
n = 0;
dest = (byte *)&UE0D0;
while(SetupSize!=0 && n<8)
{
*dest = *SetupDataPtr;
dest++;
SetupDataPtr++;
SetupSize--;
n++;
}
UEP0CSR= UEP0CSR_DVALID_IN_MASK+
(n<<4);
// check if this is the last DATA packet to send
if(n < 8) SetupBuffer.bRequest = REQUEST_COMPLETE;
}
}
BYTE handleOUT()
{
byte i,n;
// byte buf[9]={7,0x7f,0x7f,0x7f,0,0,0x0f,0,0};
byte *dest;
//halWait(2);
if((SetupBuffer.bmRequestType & 0x60) == 0) //Standard Device Request
{
return 0;
}
else
{ // feature report or vendor request.maximum length is 8 bytes
if(UEP0CSR_DVALID_OUT==0)
{
return 0;
}
n=UEP0CSR>>4;
if(n<2)
return 0;
if(n>Out_Length||n>8)//
{
return 0;
}
//LED^=1 ;
dest =(unsigned char *)&UE0D0;
for(i=0;i<n;i++)
{
In_Packet[i]=*dest;
dest++;
}
Inlength = n;
APPUsbReceived();
UEP0CSR=UEP0CSR_DVALID_IN_MASK;
//UEP0CSR_DVALID_IN=1;
Out_Length = 0;
// SendUsbjkData(buf,9);
}
return 1;
}
void USB_Processor(void)
{
unsigned char n;
byte *dest;
Status=0; //;Initially Software flags
USBSR_SETUP=0; //;Clear SETUP flag
SetupBuffer = *(setup_buffer *)(&UE0D0);
// now we will check the Request Type
if((SetupBuffer.bmRequestType & 0x60) == 0)
{ // Standard Request Decoder:
switch(SetupBuffer.bRequest)
{
case GET_DESCRIPTOR: // 6
getDescriptor();
break;
case SYNCH_FRAME: //12
SynchFrame();
break;
default:
forceSTALL();
break;
}
}
else if((SetupBuffer.bmRequestType & 0x60) == 0x20) // class requre
{
switch(SetupBuffer.bRequest)
{
case 10: //SET_IDLE
IdleRate_Table=SetupBuffer.wValue.hi;
UEP0CSR=UEP0CSR_DVALID_IN_MASK;//Set DValid and Data count
SetupBuffer.bRequest = REQUEST_COMPLETE;
break;
case 11: //Set_Proto
Protocol_Table=SetupBuffer.wValue.hi;
UEP0CSR=UEP0CSR_DVALID_IN_MASK;//Set DValid and Data count
SetupBuffer.bRequest = REQUEST_COMPLETE;
break;
case 2: //Get_Idle
UE0D0=IdleRate_Table;
UEP0CSR= UEP0CSR_DSIZE0_OUT_DSIZE0_IN_MASK+
UEP0CSR_DVALID_IN_MASK;
SetupBuffer.bRequest = REQUEST_COMPLETE;
break;
case 3: //Get_Proto
UE0D0=Protocol_Table;
UEP0CSR= UEP0CSR_DSIZE0_OUT_DSIZE0_IN_MASK+
UEP0CSR_DVALID_IN_MASK;
SetupBuffer.bRequest = REQUEST_COMPLETE;
break;
case 9: //Set_Report
SetupBuffer.bRequest = REQUEST_COMPLETE;
Out_Length = SetupBuffer.wLength.lo ;
UEP0CSR=UEP0CSR_DVALID_IN_MASK;
//LED^=1 ;
//UEP0CSR_DVALID_OUT=0;
// SetupBuffer.bRequest = REQUEST_COMPLETE;
break;
case 1: //Get_Report
SetupBuffer.bRequest = REQUEST_COMPLETE;
usb_out[0]=0x06;
usb_out[1]=macInfo.panId;
usb_out[2]=macCurrentChannel;
usb_out[3]=macWarncode;
SetupDataPtr = (unsigned char *)usb_out;
SetupSize = 8;//DeviceDesc.bLength;
n = 0;
dest = (byte *)&UE0D0;
while(SetupSize!=0 && n<8)
{
*dest = *SetupDataPtr;
dest++;
SetupDataPtr++;
SetupSize--;
n++;
}
UEP0CSR= UEP0CSR_DVALID_IN_MASK+
(n<<4);
break;
default:
forceSTALL();
break;
}
}
else if((SetupBuffer.bmRequestType & 0x60) == 0x40) //Vendor Request
{
;
// forceSTALL();
}
else
{
forceSTALL(); // Non-Standard Requests will not be handled!
}
}
void handleIN() {
byte n;
byte *dest;
if((SetupBuffer.bmRequestType & 0x60) == 0) //Standard Device Request
{
switch(SetupBuffer.bRequest) {
case GET_DESCRIPTOR:
// copy (up to) 8 Bytes to EP0 Data Registers
n = 0;
dest = (byte *)&UE0D0;
while(SetupSize!=0 && n<8) {
*dest = *SetupDataPtr;
dest++;
SetupDataPtr++;
SetupSize--;
n++;
}
// prepare to send n Bytes at next IN Transaction
UEP0CSR=UEP0CSR_DVALID_IN_MASK+(n<<4);
// check if this is the last DATA packet to send
if(n < 8) SetupBuffer.bRequest = REQUEST_COMPLETE;
break;
case REQUEST_COMPLETE:
// Request is finished - just clear the TXD0F Flag (see above)
// and do not re-enable EP0 Transmitter, since there is no more
// data to send
break;
default:
forceSTALL();
break;
}
}
else if((SetupBuffer.bmRequestType & 0x60) == 0x20) // class requre
{
if(SetupBuffer.bRequest==0x09)//set report
{
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -