环境:STM32F103 + CH376S开发板 + USB扫码枪
问题: USB设备初始化过程失败,获取设备描述符返回0x15(USB_INT_CONNECT), 期望的是0x14(USB_INT_SUCCESS)
相关代码:
/* 等待INT中断*/
u8 waitUSBDeviceInterrupt(void) {
unsigned short i;
for ( i = 0; CH375_INT_WIRE != 0; i ++ ) { // 如果CH375的中断引脚输出高电平则等待,通过计数防止超时
delay_us(1);
if ( i == 0xF000 )
{
xWriteCH376Cmd( CMD_ABORT_NAK );
printf("Wait device timeout, send cmd abort!\n");
}
}
xWriteCH376Cmd( CMD_GET_STATUS ); /* 产生操作完成中断, 获取中断状态 */
return( xReadCH376Data() );
}
/* 从设备端获取描述符 */
unsigned char get_descr( unsigned char type ) {
xWriteCH376Cmd( CMD_GET_DESCR );
xWriteCH376Data( type ); /* 描述符类型, 只支持1(设备)或者2(配置) */
printf("write -->cmd=%x, type=%x\n",CMD_GET_DESCR, type);
return( waitUSBDeviceInterrupt() );
}
unsigned char init_scanner() {
#definep_dev_descr((PUSB_DEV_DESCR)buffer)
#definep_cfg_descr((PUSB_CFG_DESCR_LONG)buffer)
unsigned char status, len, c;
printf("init_scanner enter...\n");
status=get_descr(1); /* 获取设备描述符 *///<<<<<<<<<<<<问题点<<<<<<<<<<<<<<<<<<<<
if ( status==USB_INT_SUCCESS ) {
/*无法进入,因为get_descr(1)返回的是0x15*///<<<<<<<<<问题点<<<<<<<<<<<<<<<<<<<<
......
}
printf("get descr out, status=%#x\n",status);
return(status);
}
/* 初始化CH376 */
u8mInitCH376Host( void )
{
u8res;
//IO init
CH375_Init();
//Check if CH376 coud work
xWriteCH376Cmd( CMD_CHECK_EXIST );
printf("xWriteCH376Cmd= %#x\n", CMD_CHECK_EXIST);
xWriteCH376Data( 0x55 );
res = xReadCH376Data( );
printf("xReadCH376Data= %#x\n", res);
if ( res != 0xAA ) return( CH376_ERR );
printf("set usb mode ---> 6\n");
set_usb_mode( 6 );
printf("wait usb device online...\n");
while ( waitUSBDeviceInterrupt()!=USB_INT_CONNECT );
#define USB_RESET_FIRST 0
#if USB_RESET_FIRST
printf("reset usb device...\n");
set_usb_mode( 7 );
delay_ms(10);
set_usb_mode( 6 );
delay_ms(100);
printf("wait usb device online again...\n");
while ( waitUSBDeviceInterrupt()!=USB_INT_CONNECT );
#endif
delay_ms(200);
if ( init_scanner()!=USB_INT_SUCCESS ) while(1);
return 0;
}
调试日志:
start
xWriteCH376Cmd= 0x6
xReadCH376Data= 0xaa
set usb mode ---> 6
wait usb device online...
init_scanner enter...
write -->cmd=46, type=1
get descr out, status=0x15 //<<<<<<<<<初始化过程终止<<<<<<<<<<<<<<<<<<<<