使用STM32串口操作CH375B做USB主机,一直初始化不成功,返回0x51.主要函数如下:
void CH375_WR_CMD_PORT( unsigned char cmd ) {
short cmd_data=0;
delay2us();
cmd_data=cmd+0x0100;
USART_SendData( USART2, cmd_data);
while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET);
delay2us();
}
void CH375_WR_DAT_PORT( unsigned char dat ) {
USART_SendData( USART2, dat);
while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET);
// delay1us();
}
unsigned char CH375_RD_DAT_PORT() {
delay_us(1);
while(USART_GetITStatus (USART2, USART_IT_RXNE) == SET){}
return( USART_ReceiveData(USART2));
}
unsigned char wait_interrupt() {
char i=0;
while( KEY2 !=0 ){}
CH375_WR_CMD_PORT( CMD_GET_STATUS );
i=CH375_RD_DAT_PORT();
return( i );
}
初始化函数如下:
void CH375_init() {
unsigned char i=0,RD_Data=0,k=0;
delay_ms(500);
uart2_init(9600);
for( k=100; k!=0; k-- )
{
CH375_WR_CMD_PORT( CMD_CHECK_EXIST );
i = 0x5A;
CH375_WR_DAT_PORT( i );
i = ~i;
RD_Data = CH375_RD_DAT_PORT(); 此处返回值正确
printf("%d \n",RD_Data);
if ( RD_Data != i )
{
for ( i=5; i!=0; i-- )
{
CH375_WR_CMD_PORT( CMD_RESET_ALL );
}
delay_ms(50);
}
else break;
}
do
{
delay_ms(10);
i=set_usb_mode( 6 ); 此处返回值正确,设置成功
printf("%d ok \r\n",i);
}while(i==0);
while(1)
{
while ( wait_interrupt()!=USB_INT_CONNECT );
此处一直通不过,wait_interrupt()一直返回的是0x51,只有当USB设备拔出的瞬间才会返回0x15;
电路图如下,其中V3和Vcc有短路,只使用了INT#、TXD、RXD三个脚。USB的D+和D-也没有接错。
请麻烦检查下哪里有问题,万分感谢!