chap_9.c
资源名称:USB_2.0.rar [点击查看]
上传用户:ids068
上传日期:2013-04-04
资源大小:639k
文件大小:32k
源码类别:
USB编程
开发平台:
C/C++
- /****************************************Copyright (c)**************************************************
- ** 广州周立功单片机发展有限公司
- ** 研 究 所
- ** http://www.zlgmcu.com
- **--------------当前版本修订------------------------------------------------------------------------------
- ** 修改人: 刘英斌
- ** 日 期:2003-03-13
- ** 描 述:ISP1581 V1.0
- **------------------------------------------------------------------------------------------------------
- ********************************************************************************************************/
- #include "standard.h"
- #include "Kernel.h"
- #include "D14.h"
- #include "usb.h"
- extern volatile D14_CNTRL_REG xdata D14_Cntrl_Reg;
- extern void Init_D14(void);
- extern UC code Standard_Device_Descriptor[];
- extern UC code Bulk_Interface_Descriptor[];
- extern UC code Iso_Interface_Descriptor[];
- extern UC code Standard_Config_Descriptor[];
- extern UC code Standard_Manufacturer_String[];
- extern UC code Standard_Product_String[];
- extern UC code Standard_String_ID[];
- extern UC code Bulk_In_Descriptor[];
- extern UC code Bulk_Out_Descriptor[];
- extern UC code Int_In_Descriptor[];
- extern UC code Int_Out_Descriptor[];
- extern UC code Iso_In_Descriptor[];
- extern UC code Iso_Out_Descriptor[];
- extern UC code Iso_In_Zero_Descriptor[];
- extern UC code Iso_Out_Zero_Descriptor[];
- extern UC code HS_Device_Descriptor[];
- extern UC code HS_Device_Qualifier[];
- extern UC code HS_Bulk_Interface_Descriptor[];
- extern UC code HS_Bulk_In_Descriptor[];
- extern UC code HS_Bulk_Out_Descriptor[];
- extern UC code HS_Int_In_Descriptor[];
- extern UC code HS_Int_Out_Descriptor[];
- extern UC code Other_Speed_Config_Descriptor[];
- extern UI count;
- extern void Start_mSEC_Timer(Data);
- extern void Start_SEC_Timer(Data);
- extern void Check_Busy(void);
- extern void TaskFile_Update(void);
- extern UI Temp;
- extern UC Device_Config_Value;
- extern KERNEL Kernel_Flag;
- extern DMA_INT_FLAG DMA_Int_Flag;
- extern USB_INT_FLAG USB_Int_Flag;
- extern USBCBW USB_CBW;
- extern void Init_Device(void);
- extern UC code Test_Packet[];
- extern void Init_Endpoint(void);
- extern void Init_Device(void);
- extern void Set_USB(void);
- //***********************************************************************
- //* *
- //* Prototype Routine Definition *
- //* *
- //***********************************************************************
- void USB_Setup(void);
- void reserved(void);
- void Send_Device_Status(void);
- void Send_Interface_Status(void);
- void Send_Endpoint_Status(void);
- void Vendor_Specific_Command(void);
- void Send_Status(unsigned char tran_status);
- void Set_Interface_Value(void);
- void Get_Interface_Value(void);
- void Process_Synch_Frame(void);
- void get_status(void);
- void clear_feature(void);
- void set_feature(void);
- void set_address(void);
- void get_descriptor(void);
- void get_config(void);
- void set_config(void);
- void get_interface(void);
- void set_interface(void);
- void Set_Descriptor(void);
- void reserved(void);
- void ep0in_ack(void);
- void ep0out_ack(void);
- void wait_ep0tx(void);
- void wait_ep0rx(void);
- unsigned int read_endpoint(unsigned char endp,unsigned int len,unsigned char *buf);
- unsigned int write_endpoint(unsigned char endp,unsigned int len,unsigned char *buf);
- //***********************************************************************
- //* *
- //* Variable Definition *
- //* *
- //***********************************************************************
- USB_INT_FLAG USB_Int_Flag;
- USB_DEVICE_REQ idata USB_Device_Request;
- USB_DEVICE USB_Device;
- UC code *Descriptor_Ptr;
- UC idata Endpt_FIFO[MAX_BUF];
- UI FIFO_Count;
- UC VID[2],PID[2];
- FILESIZE FileSize;
- UI PIO_Count;
- UC File_Number;
- UC Current_File;
- ULI bytecount;
- //USB标准请求
- code void (*StandardDeviceRequest[])(void) =
- {
- get_status,
- clear_feature,
- reserved,
- set_feature,
- reserved,
- set_address,
- get_descriptor,
- reserved,
- get_config,
- set_config,
- get_interface,
- set_interface,
- reserved,
- reserved,
- reserved,
- reserved
- };
- //***********************************************************************
- //* *
- //* Routine Definition *
- //* *
- //***********************************************************************
- //***********************************************************************
- //* *
- //* Routine : USB Debug Mode *
- //* Input : USB Interrupt Register *
- //* Output : None *
- //* Function : Branch to respective ISR *
- //* *
- //***********************************************************************
- void USB_Debug_Mode(void)
- {
- //端点0接收到setup包处理
- if(USB_Int_Flag.BITS.EP0SETUP)
- {
- EA_DISABLE;
- USB_Setup();
- EA_ENABLE;
- }
- //挂起处理
- if(USB_Int_Flag.BITS.SUSP)
- {
- //清除挂起标志
- EA_DISABLE;
- USB_Int_Flag.BITS.SUSP = 0;
- EA_ENABLE;
- //ISP1581进入挂起状态
- D14_Cntrl_Reg.D14_MODE.BITS.GOSUSP = 1;
- D14_Cntrl_Reg.D14_MODE.BITS.GOSUSP = 0;
- }
- //挂起恢复处理
- if(USB_Int_Flag.BITS.RESUME)
- {
- //清除恢复标志
- EA_DISABLE;
- USB_Int_Flag.BITS.RESUME = 0;
- EA_ENABLE;
- //产生一个恢复信号
- D14_Cntrl_Reg.D14_MODE.BITS.SNDRSU = 1;
- D14_Cntrl_Reg.D14_MODE.BITS.SNDRSU = 0;
- }
- //高速状态
- if(USB_Int_Flag.BITS.HS_STAT)
- {
- //清除高速状态标志
- EA_DISABLE;
- USB_Int_Flag.BITS.HS_STAT = 0;
- EA_ENABLE;
- if(Kernel_Flag.BITS.HS_FS_State == FULL_SPEED)
- {
- //切换高速指示
- HS_FS_LED = HIGH_SPEED_LED;
- //设置高速模式状态
- Kernel_Flag.BITS.HS_FS_State = HIGH_SPEED;
- //重新配置高速端点
- Init_Endpoint();
- // PIO_Count = 64;
- PIO_Count = 512;
- }
- }
- }
- //***********************************************************************
- //* *
- //* Routine : *
- //* Input : None *
- //* Output : None *
- //* Function : *
- //* *
- //***********************************************************************
- //建立包处理
- void USB_Setup(void)
- {
- unsigned char type, req;
- //清除EP0SETUP中断标志
- USB_Int_Flag.BITS.EP0SETUP = 0;
- //清除总线复位标志
- Kernel_Flag.BITS.Bus_Reset = 0;
- //选择端点0SETUP缓冲区
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 0x20;
- //读取USB请求
- //读取请求类型
- USB_Device_Request.bmRequestType = D14_Cntrl_Reg.D14_DATA_PORT_LSB;
- //读取请求号
- USB_Device_Request.bRequest = D14_Cntrl_Reg.D14_DATA_PORT_LSB;
- //读取16bit请求数据
- USB_Device_Request.wValue = D14_Cntrl_Reg.D14_DATA_PORT_LSB;
- USB_Device_Request.wValue <<= 8;
- USB_Device_Request.wValue |= D14_Cntrl_Reg.D14_DATA_PORT_LSB;
- //读取16bit请求索引
- USB_Device_Request.wIndex = D14_Cntrl_Reg.D14_DATA_PORT_LSB;
- USB_Device_Request.wIndex <<= 8;
- USB_Device_Request.wIndex |= D14_Cntrl_Reg.D14_DATA_PORT_LSB;
- //读取16bit长度
- USB_Device_Request.wLength = D14_Cntrl_Reg.D14_DATA_PORT_LSB;
- USB_Device_Request.wLength <<= 8;
- USB_Device_Request.wLength |= D14_Cntrl_Reg.D14_DATA_PORT_LSB;
- //长度处理(高低8位对调)
- Temp = USB_Device_Request.wLength;
- USB_Device_Request.wLength = 0;
- USB_Device_Request.wLength = (UC) Temp;
- USB_Device_Request.wLength <<= 8;
- Temp >>= 8;
- USB_Device_Request.wLength |= Temp;
- type = USB_Device_Request.bmRequestType & 0x60;
- req = USB_Device_Request.bRequest & 0x0f;
- //厂商请求
- if (type == 0x40)
- {
- Vendor_Specific_Command();//厂商请求处理
- }
- else if (type == 0x00)
- (*StandardDeviceRequest[req])();//调用标准请求
- }
- //***********************************************************************
- //* *
- //* Routine : *
- //* Input : None *
- //* Output : None *
- //* Function : *
- //* *
- //***********************************************************************
- //厂商请求处理
- void Vendor_Specific_Command(void)
- {
- if(USB_Device_Request.bRequest == 0x0C)
- {
- switch (USB_Device_Request.wIndex)
- {
- case GET_FIRMWARE_VERSION ://获取固件版本
- if(USB_Device_Request.wLength == 1)
- {
- //select endpoint
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 1;
- //set the data length to 1 byte
- D14_Cntrl_Reg.D14_BUFFER_LENGTH_LSB = 1;
- D14_Cntrl_Reg.D14_BUFFER_LENGTH_MSB = 0;
- //sent 0x21 as data
- D14_Cntrl_Reg.D14_DATA_PORT_LSB = 0x22;//版本号
- wait_ep0tx();
- ep0out_ack();
- // ep0in_ack();
- break;
- }
- case SETUP_DMA_REQUEST :
- //Out Token Ack detected
- wait_ep0rx();
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 0;
- //
- FileSize.Size.Address[0] = D14_Cntrl_Reg.D14_DATA_PORT_LSB;
- FileSize.Size.Address[1] = D14_Cntrl_Reg.D14_DATA_PORT_LSB;
- FileSize.Size.Address[2] = D14_Cntrl_Reg.D14_DATA_PORT_LSB;
- //读取传输字节数
- Temp = D14_Cntrl_Reg.D14_DATA_PORT_LSB;
- bytecount = D14_Cntrl_Reg.D14_DATA_PORT_LSB;
- bytecount <<= 8;
- bytecount |= Temp;
- //读取传输方式
- FileSize.Size.DIR = D14_Cntrl_Reg.D14_DATA_PORT_LSB;
- ep0in_ack();
- Kernel_Flag.BITS.Transfer_Start = 1;//标志传输数据开始
- break;
- default :
- if(USB_Device_Request.bmRequestType == 0xC0)
- {
- ep0in_ack();
- break;
- }
- if(USB_Device_Request.bmRequestType == 0x40)
- {
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 0;
- D14_Cntrl_Reg.D14_CONTROL_FUNCTION.BITS.STALL = 1;
- while(!USB_Int_Flag.BITS.EP0SETUP)
- {
- //读取ISP1581中断寄存器
- USB_Int_Flag.VALUE |= D14_Cntrl_Reg.D14_INT.VALUE;
- //回写ISP1581中断寄存器
- D14_Cntrl_Reg.D14_INT.VALUE = USB_Int_Flag.VALUE;
- if(USB_Int_Flag.BITS.SUSP || USB_Int_Flag.BITS.RESET)
- return;
- }
- USB_Int_Flag.BITS.EP0TX = 0;
- break;
- }
- }
- }
- }
- //***********************************************************************
- //* *
- //* Routine : *
- //* Input : None *
- //* Output : None *
- //* Function : *
- //* *
- //***********************************************************************
- void reserved(void)
- {
- }
- //Get_Descriptor()获取描述符
- void get_descriptor(void)
- {
- switch(USB_Device_Request.wValue)
- {
- case 1://设备描述符
- //选择描述符
- if(Kernel_Flag.BITS.HS_FS_State == FULL_SPEED)
- Descriptor_Ptr = &Standard_Device_Descriptor[0]; //set pointer to device descriptor
- if(Kernel_Flag.BITS.HS_FS_State == HIGH_SPEED)
- Descriptor_Ptr = &HS_Device_Descriptor[0]; //set pointer to device descriptor
- count = *Descriptor_Ptr; //read in descriptor length
- //选择端点号
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 1;
- //写下发送字节数
- D14_Cntrl_Reg.D14_BUFFER_LENGTH_LSB = 0x12;
- D14_Cntrl_Reg.D14_BUFFER_LENGTH_MSB = 0;
- //send descriptor
- //发送描述符
- while(count != 0)
- {
- D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;
- count--;
- Descriptor_Ptr++;
- }
- wait_ep0tx();
- ep0out_ack();
- break;
- case 2 : //获取配置描述符
- USB_Int_Flag.BITS.EP0RX = 0;
- Temp = 64;
- Descriptor_Ptr = &Standard_Config_Descriptor[0]; //set pointer to configuration descriptor
- count = *Descriptor_Ptr; //get configuration Descriptor length
- //选择端点号1
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 1;
- //check if only configuration descriptor required
- //检查描述符长度,决定是只发送配置描述符还是描述符集合
- if(count >= USB_Device_Request.wLength) //Send only Configuration descriptor
- {
- //写入发送字节数
- D14_Cntrl_Reg.D14_BUFFER_LENGTH_LSB = (UC) USB_Device_Request.wLength;
- D14_Cntrl_Reg.D14_BUFFER_LENGTH_MSB = (UC) (USB_Device_Request.wLength >> 8);
- //send descriptor
- //发送描述符
- for(count = 0 ; count < USB_Device_Request.wLength ; count++)
- {
- D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;
- Descriptor_Ptr++;
- }
- wait_ep0tx();
- ep0out_ack();
- }
- else //send Configuration,Interface and Endpoint descriptor
- {
- //发送描述符集合(配置+接口+端点)
- if(USB_Device_Request.wLength >= 32)
- {
- //写入配置描述符
- while(count != 0)
- {
- D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;
- count--;
- Descriptor_Ptr++;
- }
- //选择高速还是全速接口描述符
- if(Kernel_Flag.BITS.HS_FS_State == FULL_SPEED)
- Descriptor_Ptr = &Bulk_Interface_Descriptor[0]; //set pointer to interface descriptor
- if(Kernel_Flag.BITS.HS_FS_State == HIGH_SPEED)
- Descriptor_Ptr = &HS_Bulk_Interface_Descriptor[0]; //set pointer to interface descriptor
- count = *Descriptor_Ptr; //get the descriptor count
- //写入接口描述符
- while(count != 0)
- {
- D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;
- count--;
- Descriptor_Ptr++;
- }
- //选择高速还是全速端点1IN描述符
- if(Kernel_Flag.BITS.HS_FS_State == FULL_SPEED)
- Descriptor_Ptr = &Int_In_Descriptor[0]; //set pointer to interface descriptor
- if(Kernel_Flag.BITS.HS_FS_State == HIGH_SPEED)
- Descriptor_Ptr = &HS_Int_In_Descriptor[0]; //set pointer to interface descriptor
- count = *Descriptor_Ptr; //read in descriptor length
- //写入端点1IN描述符
- while(count != 0)
- {
- D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;
- count--;
- Descriptor_Ptr++;
- }
- //选择高速还是全速端点1OUT描述符
- if(Kernel_Flag.BITS.HS_FS_State == FULL_SPEED)
- Descriptor_Ptr = &Int_Out_Descriptor[0]; //set pointer to interface descriptor
- if(Kernel_Flag.BITS.HS_FS_State == HIGH_SPEED)
- Descriptor_Ptr = &HS_Int_Out_Descriptor[0]; //set pointer to interface descriptor
- count = *Descriptor_Ptr; //read in descriptor length
- //写入端点1OUT描述符
- while(count != 0)
- {
- D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;
- count--;
- Descriptor_Ptr++;
- }
- //选择高速还是全速端点2IN描述符
- if(Kernel_Flag.BITS.HS_FS_State == FULL_SPEED)
- Descriptor_Ptr = &Bulk_In_Descriptor[0]; //set pointer to interface descriptor
- if(Kernel_Flag.BITS.HS_FS_State == HIGH_SPEED)
- Descriptor_Ptr = &HS_Bulk_In_Descriptor[0]; //set pointer to interface descriptor
- count = *Descriptor_Ptr; //read in descriptor length
- //写入端点2IN描述符
- while(count != 0)
- {
- D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;
- count--;
- Descriptor_Ptr++;
- }
- //选择高速还是全速端点2OUT描述符
- if(Kernel_Flag.BITS.HS_FS_State == FULL_SPEED)
- Descriptor_Ptr = &Bulk_Out_Descriptor[0]; //set pointer to interface descriptor
- if(Kernel_Flag.BITS.HS_FS_State == HIGH_SPEED)
- Descriptor_Ptr = &HS_Bulk_Out_Descriptor[0]; //set pointer to interface descriptor
- count = *Descriptor_Ptr; //read in descriptor length
- //写入端点2OUT描述符
- while(count != 0)
- {
- D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;
- count--;
- Descriptor_Ptr++;
- }
- }
- //使端点有效
- D14_Cntrl_Reg.D14_CONTROL_FUNCTION.BITS.VENDP = 1; //validate FIFO
- wait_ep0tx();
- ep0out_ack();
- }
- break;
- case 3: //获取字符串描述符
- Descriptor_Ptr = &Standard_String_ID[0]; //set pointer to interface descriptor
- count = *Descriptor_Ptr; //get length
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 1;
- if(USB_Device_Request.wLength < count)
- count = USB_Device_Request.wLength; //get the remaining length
- D14_Cntrl_Reg.D14_BUFFER_LENGTH_LSB = count;
- D14_Cntrl_Reg.D14_BUFFER_LENGTH_MSB = 0;
- //send string ID
- while(count != 0)
- {
- D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr; //fill up FIFO
- Descriptor_Ptr++;
- count--;
- }
- wait_ep0tx();
- ep0out_ack();
- break;
- case 4:
- reserved();
- break;
- case 5: //获取端点描述符
- Temp = 0;
- if((USB_Device_Request.wValue & 0x00ff) > 0x0001)
- {
- D14_Cntrl_Reg.D14_CONTROL_FUNCTION.BITS.STALL = 1;
- while(!USB_Int_Flag.BITS.EP0SETUP)
- {
- //读取ISP1581中断寄存器
- USB_Int_Flag.VALUE |= D14_Cntrl_Reg.D14_INT.VALUE;
- //回写ISP1581中断寄存器
- D14_Cntrl_Reg.D14_INT.VALUE = USB_Int_Flag.VALUE;
- if(USB_Int_Flag.BITS.SUSP || USB_Int_Flag.BITS.RESET)
- return;
- }
- USB_Int_Flag.BITS.EP0TX = 0;
- }
- else
- {
- Descriptor_Ptr = &Bulk_Out_Descriptor[0]; //set pointer to Bulk Out endpoint descriptor
- count = *Descriptor_Ptr; //read in descriptor length
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 1;
- if(count >= USB_Device_Request.wLength)
- {
- Temp = USB_Device_Request.wLength;
- //send endpoint descriptor
- D14_Cntrl_Reg.D14_BUFFER_LENGTH_LSB = (UC) Temp;
- D14_Cntrl_Reg.D14_BUFFER_LENGTH_MSB = (UC) (Temp >> 8);
- for(count = 0 ; count < USB_Device_Request.wLength ; count++)
- {
- D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;
- Descriptor_Ptr++;
- }
- wait_ep0tx();
- ep0out_ack();
- }
- else
- {
- USB_Device_Request.wLength -= count; //send Endpoint descriptor
- Temp += count;
- while(count != 0)
- {
- D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;
- count--;
- Descriptor_Ptr++;
- }
- Descriptor_Ptr = &Bulk_In_Descriptor[0]; //set pointer to Bulk In endpoint descriptor
- count = *Descriptor_Ptr; //read in descriptor length
- if(count >= USB_Device_Request.wLength)
- {
- Temp += USB_Device_Request.wLength;
- //send endpoint descriptor
- D14_Cntrl_Reg.D14_BUFFER_LENGTH_LSB = (UC) Temp;
- D14_Cntrl_Reg.D14_BUFFER_LENGTH_MSB = (UC) (Temp >> 8);
- for(count = 0 ; count < USB_Device_Request.wLength ; count++)
- {
- D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;
- Descriptor_Ptr++;
- }
- wait_ep0tx();
- ep0out_ack();
- }
- else
- {
- USB_Device_Request.wLength -= count; //send Endpoint descriptor
- Temp += count;
- D14_Cntrl_Reg.D14_BUFFER_LENGTH_LSB = (UC) Temp;
- D14_Cntrl_Reg.D14_BUFFER_LENGTH_MSB = (UC) (Temp >> 8);
- while(count != 0)
- {
- D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;
- count--;
- Descriptor_Ptr++;
- }
- wait_ep0tx();
- ep0out_ack();
- }
- }
- }
- break;
- case 6: //获取设备限制描述符
- Descriptor_Ptr = &HS_Device_Qualifier[0]; //set pointer to High Speed device descriptor
- count = *Descriptor_Ptr; //read in descriptor length
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 1;
- if(USB_Device_Request.wLength < count)
- {
- D14_Cntrl_Reg.D14_BUFFER_LENGTH_LSB = (UC) USB_Device_Request.wLength;
- D14_Cntrl_Reg.D14_BUFFER_LENGTH_MSB = 0;
- count = USB_Device_Request.wLength;
- }
- else
- {
- D14_Cntrl_Reg.D14_BUFFER_LENGTH_LSB = count;
- D14_Cntrl_Reg.D14_BUFFER_LENGTH_MSB = 0;
- }
- while(count != 0)
- {
- D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr; //fill up FIFO
- Descriptor_Ptr++;
- count--;
- };
- wait_ep0tx();
- ep0out_ack();
- break;
- case 7 ://获取速度配置描述符
- Temp = 64;
- Descriptor_Ptr = &Other_Speed_Config_Descriptor[0]; //set pointer to configuration descriptor
- count = *Descriptor_Ptr; //get configuration Descriptor length
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 1;
- if(count >= USB_Device_Request.wLength) //Send only Configuration descriptor
- {
- D14_Cntrl_Reg.D14_BUFFER_LENGTH_LSB = (UC) USB_Device_Request.wLength;
- D14_Cntrl_Reg.D14_BUFFER_LENGTH_MSB = (UC) (USB_Device_Request.wLength >> 8);
- for(count = 0 ; count < USB_Device_Request.wLength ; count++)
- {
- D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;
- Descriptor_Ptr++;
- }
- wait_ep0tx();
- ep0out_ack();
- }
- else //send Configuration,Interface and Endpoint descriptor
- {
- if(USB_Device_Request.wLength >= 32)
- {
- while(count != 0)
- {
- D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;
- count--;
- Descriptor_Ptr++;
- }
- Descriptor_Ptr = &Bulk_Interface_Descriptor[0]; //set pointer to interface descriptor
- count = *Descriptor_Ptr; //get the descriptor count
- while(count != 0)
- {
- D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;
- count--;
- Descriptor_Ptr++;
- }
- Descriptor_Ptr = &Int_In_Descriptor[0]; //set pointer to interface descriptor
- count = *Descriptor_Ptr; //read in descriptor length
- while(count != 0)
- {
- D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;
- count--;
- Descriptor_Ptr++;
- }
- Descriptor_Ptr = &Int_Out_Descriptor[0]; //set pointer to interface descriptor
- count = *Descriptor_Ptr; //read in descriptor length
- while(count != 0)
- {
- D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;
- count--;
- Descriptor_Ptr++;
- }
- Descriptor_Ptr = &Bulk_In_Descriptor[0]; //set pointer to interface descriptor
- count = *Descriptor_Ptr; //read in descriptor length
- while(count != 0)
- {
- D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;
- count--;
- Descriptor_Ptr++;
- }
- Descriptor_Ptr = &Bulk_Out_Descriptor[0]; //set pointer to interface descriptor
- count = *Descriptor_Ptr; //read in descriptor length
- while(count != 0)
- {
- D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;
- count--;
- Descriptor_Ptr++;
- }
- }
- D14_Cntrl_Reg.D14_CONTROL_FUNCTION.BITS.VENDP = 1; //validate FIFO
- wait_ep0tx();
- ep0out_ack();
- }
- break;
- case 8:
- break;
- default :
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 1;
- D14_Cntrl_Reg.D14_CONTROL_FUNCTION.BITS.STALL = 1;
- while(!USB_Int_Flag.BITS.EP0SETUP)
- {
- //读取ISP1581中断寄存器
- USB_Int_Flag.VALUE |= D14_Cntrl_Reg.D14_INT.VALUE;
- //回写ISP1581中断寄存器
- D14_Cntrl_Reg.D14_INT.VALUE = USB_Int_Flag.VALUE;
- if(USB_Int_Flag.BITS.SUSP || USB_Int_Flag.BITS.RESET)
- return;
- }
- USB_Int_Flag.BITS.EP0TX = 0;
- break;
- }
- }
- //Set_Address设置地址
- void set_address(void)
- {
- USB_Int_Flag.BITS.EP0RX = 0;
- D14_Cntrl_Reg.D14_ADDRESS.BITS.DEVADDR = (USB_Device_Request.wValue >> 8);//设置地址
- //状态响应
- ep0in_ack();
- }
- //Set_Config设置配置
- void set_config(void)
- {
- USB_Int_Flag.BITS.EP0RX = 0;
- if(USB_Device_Request.wValue == 0)
- {
- Device_Config_Value = 0;
- ep0in_ack();
- }
- else if (USB_Device_Request.wValue == 0x0100)
- {
- //配置设备
- Device_Config_Value = (UC) (USB_Device_Request.wValue >> 8);
- // USB_Device.BITS.State = USB_Configured;
- ep0in_ack();
- }
- else
- {
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 1;
- D14_Cntrl_Reg.D14_CONTROL_FUNCTION.BITS.STALL = 1;
- while(!USB_Int_Flag.BITS.EP0SETUP)
- {
- //读取ISP1581中断寄存器
- USB_Int_Flag.VALUE |= D14_Cntrl_Reg.D14_INT.VALUE;
- //回写ISP1581中断寄存器
- D14_Cntrl_Reg.D14_INT.VALUE = USB_Int_Flag.VALUE;
- if(USB_Int_Flag.BITS.SUSP || USB_Int_Flag.BITS.RESET)
- return;
- }
- USB_Int_Flag.BITS.EP0TX = 0;
- }
- }
- //Get_Config获取配置
- void get_config(void)
- {
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 1;
- D14_Cntrl_Reg.D14_DATA_PORT_LSB = Device_Config_Value;
- D14_Cntrl_Reg.D14_CONTROL_FUNCTION.BITS.VENDP = 1;
- wait_ep0tx();
- ep0out_ack();
- }
- //Clear_Feature特性清除
- void clear_feature(void)
- {
- unsigned char bRecipient = USB_Device_Request.bmRequestType & 0x0f;
- //端点
- if(bRecipient == 0x02 &&USB_Device_Request.wValue == 0x0000)
- {
- switch(USB_Device_Request.wIndex)
- {
- case 0x8000 :
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 1;
- break;
- case 0x0000 :
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 0;
- break;
- case 0x8100 :
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 3;
- break;
- case 0x0100 :
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 2;
- break;
- case 0x8200 :
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 5;
- break;
- case 0x0200 :
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 4;
- break;
- case 0x8300 :
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 7;
- break;
- case 0x0300 :
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 6;
- break;
- case 0x8400 :
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 9;
- break;
- case 0x0400 :
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 8;
- break;
- case 0x8500 :
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 11;
- break;
- case 0x0500 :
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 10;
- break;
- case 0x8600 :
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 13;
- break;
- case 0x0600 :
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 12;
- break;
- case 0x8700 :
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 15;
- break;
- case 0x0700 :
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 14;
- break;
- default :
- break;
- }
- D14_Cntrl_Reg.D14_CONTROL_FUNCTION.BITS.STALL = 0;
- Kernel_Flag.BITS.Endpt_Stall = 0;
- D14_Cntrl_Reg.D14_ENDPT_TYPE.BITS.ENABLE = OFF; //disable FIFO
- D14_Cntrl_Reg.D14_ENDPT_TYPE.BITS.ENABLE = ON; //disable FIFO
- ep0in_ack();
- USB_Device.BITS.Halt = 0;
- }
- else if(bRecipient == 0x00 && USB_Device_Request.wValue == 0x0100)
- { //远程唤醒
- USB_Device.BITS.Remote_Wakeup = 0;
- ep0in_ack();
- }
- else
- {
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 1;
- D14_Cntrl_Reg.D14_CONTROL_FUNCTION.BITS.STALL = 1;
- while(!USB_Int_Flag.BITS.EP0SETUP)
- {
- //读取ISP1581中断寄存器
- USB_Int_Flag.VALUE |= D14_Cntrl_Reg.D14_INT.VALUE;
- //回写ISP1581中断寄存器
- D14_Cntrl_Reg.D14_INT.VALUE = USB_Int_Flag.VALUE;
- if(USB_Int_Flag.BITS.SUSP || USB_Int_Flag.BITS.RESET)
- return;
- }
- USB_Int_Flag.BITS.EP0TX = 0;
- }
- }
- //Get_Status获取设备状态
- void get_status(void)
- {
- if(USB_Device_Request.bmRequestType == 0x80)
- {
- //端点0IN返回0x01,0x00两字节
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 1;
- D14_Cntrl_Reg.D14_BUFFER_LENGTH_LSB = 2;
- D14_Cntrl_Reg.D14_BUFFER_LENGTH_MSB = 0;
- D14_Cntrl_Reg.D14_DATA_PORT_LSB = 1;
- D14_Cntrl_Reg.D14_DATA_PORT_MSB = 0;
- //等待发送完毕
- wait_ep0tx();
- ep0out_ack();
- }
- if(USB_Device_Request.bmRequestType == 0x81)
- {
- //端点0IN返回0x00,0x00两字节
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 1;
- D14_Cntrl_Reg.D14_BUFFER_LENGTH_LSB = 2;
- D14_Cntrl_Reg.D14_BUFFER_LENGTH_MSB = 0;
- D14_Cntrl_Reg.D14_DATA_PORT_LSB = 0;
- D14_Cntrl_Reg.D14_DATA_PORT_MSB = 0;
- wait_ep0tx();
- ep0out_ack();
- }
- if(USB_Device_Request.bmRequestType == 0x82)
- {
- Temp = 0;
- Temp = USB_Device.BITS.Halt;
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 1;
- D14_Cntrl_Reg.D14_BUFFER_LENGTH_LSB = 2;
- D14_Cntrl_Reg.D14_BUFFER_LENGTH_MSB = 0;
- D14_Cntrl_Reg.D14_DATA_PORT_LSB = (UC) Temp;
- D14_Cntrl_Reg.D14_DATA_PORT_MSB = (UC) (Temp >> 8);
- wait_ep0tx();
- ep0out_ack();
- }
- }
- //Set_Feature特性设置
- //不支持测试模式
- void set_feature(void)
- {
- unsigned char bRecipient = USB_Device_Request.bmRequestType & 0x0f;
- //端点
- if(bRecipient == 0x02 && USB_Device_Request.wValue == 0x0000)
- {
- switch(USB_Device_Request.wIndex)
- {
- case 0x8000 :
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 1;
- break;
- case 0x0000 :
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 0;
- break;
- case 0x8100 :
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 3;
- break;
- case 0x0100 :
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 2;
- break;
- case 0x8200 :
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 5;
- break;
- case 0x0200 :
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 4;
- break;
- case 0x8300 :
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 7;
- break;
- case 0x0300 :
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 6;
- break;
- case 0x8400 :
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 9;
- break;
- case 0x0400 :
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 8;
- break;
- case 0x8500 :
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 11;
- break;
- case 0x0500 :
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 10;
- break;
- case 0x8600 :
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 13;
- break;
- case 0x0600 :
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 12;
- break;
- case 0x8700 :
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 15;
- break;
- case 0x0700 :
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 14;
- break;
- default :
- break;
- }
- D14_Cntrl_Reg.D14_CONTROL_FUNCTION.BITS.STALL = 1;
- Kernel_Flag.BITS.Endpt_Stall = 1;
- D14_Cntrl_Reg.D14_ENDPT_TYPE.BITS.ENABLE = OFF; //disable FIFO
- //D14_Cntrl_Reg.D14_ENDPT_TYPE.BITS.ENABLE = ON; //disable FIFO
- ep0in_ack();
- USB_Device.BITS.Halt = 0;
- }
- else if(bRecipient == 0x00 && USB_Device_Request.wValue == 0x0100)
- { //远程唤醒
- USB_Device.BITS.Remote_Wakeup = 1;
- ep0in_ack();
- }
- else
- {
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 1;
- D14_Cntrl_Reg.D14_CONTROL_FUNCTION.BITS.STALL = 1;
- while(!USB_Int_Flag.BITS.EP0SETUP)
- {
- //读取ISP1581中断寄存器
- USB_Int_Flag.VALUE |= D14_Cntrl_Reg.D14_INT.VALUE;
- //回写ISP1581中断寄存器
- D14_Cntrl_Reg.D14_INT.VALUE = USB_Int_Flag.VALUE;
- if(USB_Int_Flag.BITS.SUSP || USB_Int_Flag.BITS.RESET)
- return;
- }
- USB_Int_Flag.BITS.EP0TX = 0;
- }
- }
- //Get_Interface 获取接口状态
- void get_interface(void)
- {
- if(USB_Device_Request.wValue != 0)
- {
- D14_Cntrl_Reg.D14_CONTROL_FUNCTION.BITS.STALL = 1;
- while(!USB_Int_Flag.BITS.EP0SETUP)
- {
- //读取ISP1581中断寄存器
- USB_Int_Flag.VALUE |= D14_Cntrl_Reg.D14_INT.VALUE;
- //回写ISP1581中断寄存器
- D14_Cntrl_Reg.D14_INT.VALUE = USB_Int_Flag.VALUE;
- if(USB_Int_Flag.BITS.SUSP || USB_Int_Flag.BITS.RESET)
- return;
- }
- USB_Int_Flag.BITS.EP0TX = 0;
- }
- else
- {
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 1;
- D14_Cntrl_Reg.D14_BUFFER_LENGTH_LSB = 1;
- D14_Cntrl_Reg.D14_BUFFER_LENGTH_MSB = 0;
- if(!USB_Device.BITS.Alter_Interface)
- D14_Cntrl_Reg.D14_DATA_PORT_LSB = 0;
- else
- D14_Cntrl_Reg.D14_DATA_PORT_LSB = 1;
- wait_ep0tx();
- ep0in_ack();
- }
- }
- //Set_Interface 设置接口状态
- void set_interface(void)
- {
- if(USB_Device_Request.wIndex == 0 && USB_Device_Request.wValue == 0x0100)
- USB_Device.BITS.Alter_Interface = 1;
- if(USB_Device_Request.wIndex == 0x0100 && USB_Device_Request.wValue == 0)
- USB_Device.BITS.Alter_Interface = 0;
- if(USB_Device_Request.wIndex == 0 && USB_Device_Request.wValue == 0)
- USB_Device.BITS.Alter_Interface = 0;
- ep0in_ack();
- }
- //端点0IN状态响应
- void ep0in_ack(void)
- {
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 1;
- D14_Cntrl_Reg.D14_CONTROL_FUNCTION.BITS.STATUS = 1;
- wait_ep0tx();
- }
- //端点0IN状态响应
- void wait_ep0tx(void)
- {
- //end of status stage
- while(!USB_Int_Flag.BITS.EP0TX)
- {
- //读取ISP1581中断寄存器
- USB_Int_Flag.VALUE |= D14_Cntrl_Reg.D14_INT.VALUE;
- //回写ISP1581中断寄存器
- D14_Cntrl_Reg.D14_INT.VALUE = USB_Int_Flag.VALUE;
- if(USB_Int_Flag.BITS.SUSP || USB_Int_Flag.BITS.RESET)
- return;
- }
- USB_Int_Flag.BITS.EP0TX = 0;
- }
- //端点0OUT状态响应
- void ep0out_ack(void)
- {
- //设置状态响应
- //acknowledge status stage
- D14_Cntrl_Reg.D14_ENDPT_INDEX = 0;
- D14_Cntrl_Reg.D14_CONTROL_FUNCTION.BITS.STATUS = 1;
- // D14_Cntrl_Reg.D14_CONTROL_FUNCTION.VALUE = 2;
- wait_ep0rx();
- }
- //端点0OUT状态响应
- void wait_ep0rx(void)
- {
- while(!USB_Int_Flag.BITS.EP0RX)
- {
- //读取ISP1581中断寄存器
- USB_Int_Flag.VALUE |= D14_Cntrl_Reg.D14_INT.VALUE;
- //回写ISP1581中断寄存器
- D14_Cntrl_Reg.D14_INT.VALUE = USB_Int_Flag.VALUE;
- if(USB_Int_Flag.BITS.SUSP || USB_Int_Flag.BITS.RESET)
- return;
- }
- USB_Int_Flag.BITS.EP0RX = 0;
- }
- #pragma disable
- unsigned int read_endpoint(unsigned char endp,unsigned int len,unsigned char *buf)
- {
- unsigned int datalen;
- D14_Cntrl_Reg.D14_ENDPT_INDEX = endp;
- len = D14_Cntrl_Reg.D14_BUFFER_LENGTH_MSB;
- len <<= 8;
- len += D14_Cntrl_Reg.D14_BUFFER_LENGTH_LSB;
- datalen=len;
- while(datalen-- != 0)
- {
- *buf = D14_Cntrl_Reg.D14_DATA_PORT_LSB; //fill up FIFO
- buf++;
- }
- return len;
- }
- #pragma disable
- unsigned int write_endpoint(unsigned char endp,unsigned int len,unsigned char *buf)
- {
- unsigned int datalen;
- D14_Cntrl_Reg.D14_ENDPT_INDEX = endp;
- D14_Cntrl_Reg.D14_BUFFER_LENGTH_LSB = (unsigned char) len;
- D14_Cntrl_Reg.D14_BUFFER_LENGTH_MSB = (unsigned char) (len>>8);
- datalen=len;
- while(datalen-- != 0)
- {
- D14_Cntrl_Reg.D14_DATA_PORT_LSB = *buf; //fill up FIFO
- buf++;
- }
- return len;
- }