请问:我用CH375B来操作鼠标,将列程作了一点改动,CH375的通讯 采用并口方式,获得的描述符数据全部发送到PC机进行观察,能得到设备、配置、接口、端口、报表的描述符,但是我操作鼠标时却没有任何数据,是怎么回事情啊,是不是还有什么需要设置啊??? // 用CH375操作HID设备,比如键盘,鼠标 #include #include #include #define TEST_LOW_SPEED 1 //unsigned char volatile xdata CH375_CMD_PORT _at_ 0xBDF1; /* CH375命令端口的I/O地址 */ //unsigned char volatile xdata CH375_DAT_PORT _at_ 0xBCF0; /* CH375数据端口的I/O地址 */ sbit CH375_INT_WIRE = P3^2; /* P3.2, INT0, 连接CH375的INT#引脚,用于查询中断状态 */ //sbit P1_0=P1^0; //sbit P1_2=P1^2; sbit CH375_A0 = P1^0; sbit CH375_RD = P1^1; sbit CH375_CS = P1^3; sbit CH375_WR = P1^2; sfr CH375_D0_D7 =0xa0; #define TRUE 1 #define FALSE 0 unsigned char endp_int; //中断端点号 unsigned char num_interfaces; //接口数 unsigned char config_value; //配置值 unsigned char report_descr_len=0;//REPORT描述符长度 bit flag_config_2=0; //第二次获取描述符标志位 bit flag_interface_2=0; //多个接口标志位 unsigned char endp6_mode=0x80, endp7_mode=0x80;//同步标志位初值 unsigned char status=0xff; //全局状态 unsigned char xdata data_buf[96];//描述符缓冲区可以适当减小 union _REQUEST //请求包结构 { struct { unsigned char bmRequestType; unsigned char bRequest; unsigned int wValue; unsigned int wIndex; unsigned int wLength; }Req; unsigned char Req_buf[8]; }Request; unsigned char report_cou=0; //REPORT描述符长度计数 bit flag_output=0; //串口输出标志位 unsigned char xdata data_in[100];//串口输出缓冲区
void delay2us( ) { unsigned char i; #ifdef TEST_LOW_SPEED for ( i = 20; i != 0; i -- ); #else for ( i = 2; i != 0; i -- ); #endif } void delay1us( ) { unsigned char i; #ifdef TEST_LOW_SPEED for ( i = 10; i != 0; i -- ); #else for ( i = 1; i != 0; i -- ); #endif } void delayms(unsigned char delay) { unsigned char i; do { for(i=0;i!=250;i++){;} for(i=0;i!=250;i++){;} for(i=0;i!=150;i++){;} }while(--delay); } void mInitSTDIO( ) { SCON = 0x50; PCON = 0x80; TMOD = 0x21; TH1 = 0xfa; /* 11.0592MHz晶振, 9600bps */ TR1 = 1; //TI = 1; EX0=1; EA=0; }
void CH375_WR_CMD_PORT( unsigned char cmd ) { CH375_D0_D7 = cmd; CH375_A0 = 1; CH375_RD = 1; CH375_CS = 0; CH375_WR = 0; delay2us(); CH375_WR = 1; CH375_CS = 1; CH375_A0 = 0; delay1us(); CH375_D0_D7 = 0xff; return; }
void CH375_WR_DAT_PORT( unsigned char dat ) { CH375_D0_D7 = dat; CH375_CS = 0; CH375_WR = 0; delay2us(); CH375_WR = 1; CH375_CS = 1; delay1us(); CH375_D0_D7 = 0xff; return; }
unsigned char CH375_RD_DAT_PORT( void ) { unsigned char dat; delay2us(); CH375_D0_D7 = 0xff; CH375_CS =0; CH375_RD = 0; CH375_CS = 0; dat = CH375_D0_D7; CH375_RD = 1; CH375_CS =1; CH375_D0_D7 = 0xff; delay1us(); return( dat ); }
unsigned char set_usb_mode( unsigned char mode ) { /* 设置CH37X的工作模式 */ unsigned char i; CH375_WR_CMD_PORT( CMD_SET_USB_MODE ); CH375_WR_DAT_PORT( mode ); endp7_mode=0x80; /* 主机端复位USB数据同步标志 */ endp6_mode=endp7_mode; for( i=0; i!=100; i++ ) { /* 等待设置模式操作完成,不超过30uS */ if ( CH375_RD_DAT_PORT()==CMD_RET_SUCCESS ) return( TRUE ); /* 成功 */ } return( FALSE ); /* CH375出错,例如芯片型号错或者处于串口方式或者不支持 */ }
void set_freq(void) { CH375_WR_CMD_PORT(0x0b); //该命令获取USB 存储设备的最大逻辑单元号 /* 切换使375B进入低速模式 */ CH375_WR_DAT_PORT(0x17); CH375_WR_DAT_PORT(0xd8); }
/* 数据同步 */ /* USB的数据同步通过切换DATA0和DATA1实现: 在设备端, CH372/CH375可以自动切换; 在主机端, 必须由SET_ENDP6和SET_ENDP7命令控制CH375切换DATA0与DATA1. 主机端的程序处理方法是为SET_ENDP6和SET_ENDP7分别提供一个全局变量, 初始值均为80H, 每执行一次成功事务后将位6取反, 每执行一次失败事务后将其复位为80H. */
void toggle_recv() { // 主机接收成功后,切换DATA0和DATA1实现数据同步 ,设置USB端点2/主机端点的接收器 CH375_WR_CMD_PORT( CMD_SET_ENDP6 ); CH375_WR_DAT_PORT( endp6_mode ); endp6_mode^=0x40; delay2us(); } void toggle_send() { // 主机发送成功后,切换DATA0和DATA1实现数据同步 ,设置USB端点2/主机端点的发送器。 CH375_WR_CMD_PORT( CMD_SET_ENDP7 ); CH375_WR_DAT_PORT( endp7_mode ); //实现同步方式的办法是切换位7与位6。 endp7_mode^=0x40; delay2us(); } //void clr_stall6() { /* 主机接收失败后,复位设备端的数据同步到DATA0 */ // CH375_WR_CMD_PORT( CMD_CLR_STALL ); // CH375_WR_DAT_PORT( 2 | 0x80 ); /* 如果设备端不是CH37X芯片,那么需要修改端点号 */ // endp6_mode=0x80; // status=0xff; //}
//void clr_stall7() { /* 主机发送失败后,复位设备端的数据同步到DATA0 */ // CH375_WR_CMD_PORT( CMD_CLR_STALL ); // CH375_WR_DAT_PORT( 2 ); /* 如果设备端不是CH37X芯片,那么需要修改端点号 */ // endp7_mode=0x80; // status=0xff; //}
unsigned char rd_usb_data( unsigned char *buf ) { /* 从CH37X读出数据块 */ unsigned char i, len; CH375_WR_CMD_PORT( CMD_RD_USB_DATA ); /* 从CH375的端点缓冲区读取接收到的数据 */ len=CH375_RD_DAT_PORT(); /* 后续数据长度 */ for ( i=0; i!=len; i++ ) *buf++=CH375_RD_DAT_PORT(); return( len ); }
void wr_usb_data( unsigned char len, unsigned char *buf ) { /* 向CH37X写入数据块 */ CH375_WR_CMD_PORT( CMD_WR_USB_DATA7 ); /* 向CH375的端点缓冲区写入准备发送的数据 */ CH375_WR_DAT_PORT( len ); /* 后续数据长度, len不能大于64 */ while( len-- ) CH375_WR_DAT_PORT( *buf++ ); }
void issue_token( unsigned char endp_and_pid ) { //主机执行USB事务,发出令牌 CH375_WR_CMD_PORT( CMD_ISSUE_TOKEN ); CH375_WR_DAT_PORT( endp_and_pid ); // 高4位目的端点号, 低4位令牌PID status=0xff; }
void intt0() interrupt 0 { unsigned char len_temp,i; EX0=0; SBUF=0x55; //产生中断发送串口数据 while(TI!=1) { } TI=0; CH375_WR_CMD_PORT( CMD_GET_STATUS ); /* 产生操作完成中断, 获取中断状态 */ status=CH375_RD_DAT_PORT(); if(status!=USB_INT_SUCCESS)//&&((status&0xf0)==0x20)) { CH375_WR_CMD_PORT( CMD_CLR_STALL ); //主机方式: 控制传输-清除端点错误 CH375_WR_DAT_PORT(1); /* 如果设备端不是CH37X芯片,那么需要修改端点号 */ endp6_mode=0x80; endp7_mode=0x80; toggle_recv(); issue_token(( endp_int << 4 ) | DEF_USB_PID_IN);//发送从中断端点读数据的令牌 } else { len_temp=rd_usb_data(data_buf); //键盘中断端点数据长度一般为8字节,鼠标为4字节 for(i=0;i!=len_temp;i++) data_in[i]=data_buf[i]; flag_output=1; toggle_recv(); issue_token(( endp_int << 4 ) | DEF_USB_PID_IN);//发送从中断端点读数据的令牌 } EX0=1; }
unsigned char wait_interrupt() { /* 主机端等待操作完成, 返回操作状态 */ while( CH375_INT_WIRE ); /* 查询等待CH375操作完成中断(INT#低电平) */ CH375_WR_CMD_PORT( CMD_GET_STATUS ); /* 产生操作完成中断, 获取中断状态 */ return( CH375_RD_DAT_PORT() ); }
void set_retry(unsigned char num) { CH375_WR_CMD_PORT( CMD_SET_RETRY); CH375_WR_DAT_PORT( 0x25); CH375_WR_DAT_PORT( num); delay2us(); }
unsigned char set_config_ex() { Request.Req.bmRequestType=0x00; Request.Req.bRequest=0x09; config_value=config_value; Request.Req.wValue=0x0000|((unsigned int)config_value<<8); Request.Req.wIndex=0x0000; Request.Req.wLength=0x0000; endp7_mode=0x80; toggle_send(); wr_usb_data(8,Request.Req_buf);/* SETUP数据总是8字节 */ issue_token(( 0 << 4 ) | DEF_USB_PID_SETUP); status=wait_interrupt(); if(status==USB_INT_SUCCESS)/* SETUP阶段操作成功 */ { endp6_mode=0xc0; toggle_recv(); issue_token(( 0 << 4 ) | DEF_USB_PID_IN); status=wait_interrupt(); if(status==USB_INT_SUCCESS)/* 状态阶段操作成功 */ { if(rd_usb_data(data_buf)!=0) return(0); } else return(0); } else return(0); return(1) ; }
unsigned char set_idle() { Request.Req.bmRequestType=0x21; //主机至设备,类,接口!! 数据包格式 Request.Req.bRequest=0x0a; //获得接口 Request.Req.wValue=0x0000; Request.Req.wIndex=0x0000; if(