📄 chap_9.c
字号:
//***********************************************************************
// *
// P H I L I P S P R O P R I E T A R Y *
// *
// COPYRIGHT (c) 1999 BY PHILIPS SINGAPORE (APIC). *
// -- ALL RIGHTS RESERVED -- *
// *
// File Name : USBDebug.c *
// Author : Albert Goh *
// Created : 3 March 2000 *
// *
//***********************************************************************
//***********************************************************************
// *
// USBDebug has the ability to test the USB core in Bus enumeration or *
// in CBI/bulk mode. *
// *
//***********************************************************************
//***********************************************************************
// *
// Module History *
// ************** *
// *
// Date Version Author Changes *
// ==== ======= ====== ======= *
// 030300 0.1 Albert Created *
// *
//***********************************************************************
//***********************************************************************
//* *
//* Include Files Definition *
//* *
//***********************************************************************
#include "standard.h"
#include "Kernel.h"
#include "D14.h"
#include "usb.h"
#include "viic_c51.h"
extern D14_CNTRL_REG volatile 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);
extern void ZLG7289_SendBuf(UC *buf);
//***********************************************************************
//* *
//* Prototype Routine Definition *
//* *
//***********************************************************************
unsigned int read_endpoint(unsigned char endp,unsigned int len,unsigned char *buf);
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);
//***********************************************************************
//* *
//* Variable Definition *
//* *
//***********************************************************************
USB_INT_FLAG USB_Int_Flag;
USB_DEVICE_REQ idata USB_Device_Request;
USB_DEVICE USB_Device;
UC code disp_buf0[8]={0,0,0,0,0,0,0,0}; //LED初始化值
UC code disp_buf1[8]={1,1,1,1,1,1,1,1}; //LED初始化值
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,bytecouter;
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 = 0x21;//版本号
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;
case GET_SET_EEPROM_REQUEST ://对EEPROM操作的请求
//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;
bytecouter=(UC)bytecount;
//读取传输方式
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)
{
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -