CH374S讀取鍵盤數(shù)據(jù)問題求助,盼復(fù)

用STM32+CH374S驅(qū)動(dòng)USB鍵盤,出現(xiàn)一個(gè)非常奇怪的問題。剛燒寫完程序(不斷電)進(jìn)去的可以正常讀取鍵盤數(shù)據(jù),也非常穩(wěn)定。但如果斷一次電再去讀取鍵盤時(shí)發(fā)現(xiàn)就無法獲取到數(shù)據(jù)了。

對比一下啟動(dòng)后調(diào)試信息,燒寫后未斷電重啟,endp_in_addr = 01;如果斷電后再重啟發(fā)現(xiàn)endp_in_addr = 04。不知道這是什么情況,請版主指教,非常感謝!

?

int?UsbKey(void)
{??
??? UINT8?? i, s,close_bit_flag=0;
?//由于報(bào)告描述符大小不定,緩沖區(qū)的大小最好定義大些
??? UINT8?? buf[80];?
?// 等待CH374復(fù)位完成?????????
??? mDelaymS( 50 );?
?// CH374接口初始化
??? CH374_PORT_INIT( );???
?// 初始化USB主機(jī)
??? Init374Host( );?
?//設(shè)置主機(jī)空閑
??? HostSetBusFree( );
?// 由于USB設(shè)備剛插入尚未穩(wěn)定,故等待USB設(shè)備數(shù)百毫秒,消除插拔抖動(dòng)
??? if(ch374_flag == 1)
??? {?
??? ?ch374_flag = 0;
??? ?//while(1)
??????? //{??
??????? //??? if ( Query374Interrupt( ) ) HostDetectInterrupt( );?
??????? //??? if ( Query374DeviceIn( ) ) break;??????
??????? //}??
????? ?mDelaymS( 10 );????????????????????????????????????????
??????? if ( Query374Interrupt( ) ) HostDetectInterrupt( );????? // 如果有USB主機(jī)中斷則處理
??
??????? LCDprintf( "Reset Device\n" );??
??????? HostSetBusReset( );????????????????????????????????????? // USB總線復(fù)位
??????? for ( i = 0; i < 100; i ++ )???????????????????????????? // 等待USB設(shè)備復(fù)位后重新連接
??????? {??
??????????? if ( Query374DeviceIn( ) ) break;??????????????????? // 有USB設(shè)備
??????????? mDelaymS( 1 );??
??????? }??
??????? if ( Query374Interrupt( ) ) HostDetectInterrupt( );????? // 如果有USB主機(jī)中斷則處理
??????? if ( Query374DeviceIn( ) )?????????????????????????????? // 有USB設(shè)備
??????? {????
??????????? if ( Query374DevFullSpeed( ) )??
??????????? {??
??????????????? LCDprintf( "Start Full-Speed Device\n" );??
??????????????? HostSetFullSpeed( );??????????????????????????? // 檢測到全速USB設(shè)備
??????????????? LOW_SPEED_BIT=0;?
??????????? }??
??????????? else???
??????????? {??
??????????????? LCDprintf( "Start Low-Speed Device\n" );??
??????????????? HostSetLowSpeed( );??????????????????????????? // 檢測到低速USB設(shè)備
??????????????? LOW_SPEED_BIT=1;??
??????????? }??
??????? }??
??????? else???
??????? {??
??????????? return -1;??????????????????????????????????????????? // 設(shè)備已經(jīng)斷開,繼續(xù)等待
??????? }??
??????? mDelaymS( 50 );??
??????? close_bit_flag=0;??
??????? LCDprintf( "GetDeviceDescr: " );??
??????? s = GetDeviceDescr( buf );???????????????????????????? // 獲取設(shè)備描述符
??????? LCDprintf("device_status=%x \n",(unsigned short)s);??
??????? if ( s != USB_INT_SUCCESS )??
??????? {??
??????????? goto WaitDeviceOut;??????????????????????????????? // 終止操作,等待USB設(shè)備拔出
??????? }??
??????? for ( i = 0; i < ( (PUSB_SETUP_REQ)SetupGetDevDescr ) -> wLengthL; i ++ ) LCDprintf( "%x ", (UINT16)( buf[i] ) );??
??????? LCDprintf( "\n" );??
??????? s = SetUsbAddress( 0x02 );???????????????????????????? // 設(shè)置USB設(shè)備地址
??????? LCDprintf("address_status=%x\n",(unsigned short)s);??
??????? if ( s != USB_INT_SUCCESS )???
??????? {??
??????????? goto WaitDeviceOut;???????????????????????????????? // 終止操作,等待USB設(shè)備拔出
??????? }??
??????? s = GetConfigDescr( buf );???????????????????????????? // 獲取配置描述符
??????? LCDprintf("config_status=%x\n",(unsigned short)s);??
??????? if ( s != USB_INT_SUCCESS )???
??????? {??
??????????? goto WaitDeviceOut;??????????????????????????????? // ????,??USB??????
??????? }??
??
?// 分析配置描述符,獲取端點(diǎn)數(shù)據(jù)/各端點(diǎn)地址/各端點(diǎn)大小等,更新變量endp_addr和endp_size等
???s = SetUsbConfig( ( (PUSB_CFG_DESCR)buf ) -> bConfigurationValue );?? // 設(shè)置USB設(shè)備配置
??????? if ( s != USB_INT_SUCCESS )???
??????? {??
//????????? LCDprintf( "ERROR = %x\n", (UINT16)s );??
??????????? goto WaitDeviceOut;????????????????????????????? // 終止操作,等待USB設(shè)備拔出
??????? }??
??????? else??? LCDprintf( "SetUsbConfig_success\n" );??
??
?//-------------------------以下進(jìn)行HID類的簡單操作-----------------------------------------------------------
??????? LCDprintf("Set_Idle\n");??
??????? s=Set_Idle( );??????????????????????????????????????? //設(shè)置IDLE,這個(gè)步驟是按照HID類的協(xié)議來做的
??????? if(s!=USB_INT_SUCCESS)??
??????? {??
??????????? LCDprintf("Set_Idle_Err=%x\n",(unsigned short)s);??
??????????? if(s&0x0f==USB_INT_RET_STALL)? goto next_operate1; //返回STALL可能本身不支持
??????? }??
??????? else LCDprintf("Set_idle success\n");??
next_operate1:??
??????? LCDprintf("Get_Hid_Des\n");??
??????? s=Get_Hid_Des(buf);????????????????????????????????? // 獲取報(bào)表描述符描述符
??????? if(s==USB_INT_SUCCESS)??
??????? {??
??????????? LCDprintf("HID_Desc: ");??
??????????? for(i=0;i!=hid_des_leng;i++)? LCDprintf("%x ",(unsigned short)buf[i]);??
??????????? LCDprintf("\n");??
??????? }??
??????? else??
??????? {??
??????????? goto WaitDeviceOut;???????????????????????????? //出錯(cuò)退出
??????? }??
??
??????? LCDprintf("Set_Report \n");???????????????????????????
??//對于鍵盤發(fā)Set_Report來點(diǎn)亮燈,對于鼠標(biāo)則不需要這一步
??????? buf[0]=0x01;??
??????? s=Set_Report(buf);???????????????????????????????? //設(shè)置報(bào)表
??????? if(s==USB_INT_SUCCESS)?????
??????? {??
???????????? LCDprintf("Set_Report success\n");??
??????? }??
??????? else??
??????? {???
????????????? LCDprintf("Set_Report Err=%x\n",(unsigned short)s);?????? //設(shè)置報(bào)告出錯(cuò)
????????????? if(s&0x0f==USB_INT_RET_STALL)?
???? ?goto next_operate2;????? //返回STALL可能本身不支持?
??????? }??
next_operate2:??
??// 下面開始讀取數(shù)據(jù)
??//實(shí)際在讀取數(shù)據(jù)的時(shí)候,
??//要先發(fā)送中斷端點(diǎn)的令牌來讀取數(shù)據(jù),
??//接著才能獲取到數(shù)據(jù)
??//LCD_Clear(0x001F);
????tog1=FALSE; ???????? //開始取DATA0?
????while(1)??
????{?
?????s=Interrupt_Data_Trans(buf);??
??//???printf("s:%x \n",(unsigned short)s);??
?????if(s==USB_INT_SUCCESS)?
?????{?
??????for(i=0;i!=8;i++)?LCDprintf("%02x ",(unsigned short)buf[i]);??
??????LCDprintf("\n");?
?????}?
?????else if(s==USB_INT_DISCONNECT)???? //? 這個(gè)是為了知道設(shè)備拔出產(chǎn)生的中斷狀態(tài)?
?????{?
??????close_bit_flag=1;?
??????break;??????
?????}?
????}?

??
??????? return 0;
??
WaitDeviceOut:?????????????????????????????????????? //? 這個(gè)是為了知道設(shè)備拔出產(chǎn)生的中斷狀態(tài)
??????? LCDprintf( "Wait Device Out\n" );??
??usbrst_flag = 1;
??????? if(close_bit_flag==0){??
??????????? while ( 1 )??
??????????? {??
???????????????? if ( Query374Interrupt( ) ) HostDetectInterrupt( );? // 如果有USB主機(jī)中斷則處理
???????????????? if ( Query374DeviceIn( ) == FALSE ) break;?????????? // 沒有USB設(shè)備
??????????? }??
??????? }??
??????? mDelaymS( 100 );???????????????????????????????????????????? // 等待設(shè)備完全斷開,消除插拔抖動(dòng)
??return -2;??????
?}
?else
??? ?return -3;??????
}

?

你好:
endp_in_addr 的數(shù)值來自你獲取的完整“配置描述符”中的端點(diǎn)描述符,你自己檢查一下,這個(gè)變量被賦值的情況,什么時(shí)候發(fā)生的變化就能找到問題了。


只有登錄才能回復(fù),可以選擇微信賬號登錄

国产91精品新入口,国产成人综合网在线播放,九热这里只有精品,本道在线观看,美女视频a美女视频,韩国美女激情视频,日本美女pvp视频