急!stm32f103+ch376 U盘读写文件,已折腾三四天了!

急!stm32f103+ch376 U盘读写文件,已折腾三四天了!


1:U盘新建文件返回值都是USB_INT_SUCCESS,并且CH376FileOpenPath( TarName )能打开、CH376GetFileSize()能得到正确大


小,但是大多数时候在电脑里面看不到刚刚新建的文件。


能打开、能得到正确大小,说明是创建成功的,为什么在电脑里面看不到文件?


2:无论在电脑里面能不能看到的文件,读取时,CH376ByteRead()等函数的返回值也都是USB_INT_SUCCESS,并且其RealCount参


数也是得到正确的大小,但是buf参数总是空字符串。


能打开、能得到正确大小,创建文件应该说也是成功的,RealCount参数也正确,为什么buf参数没有获取内容?


另外,获取的U盘剩余空间总是正确的。


下面附部分关键代码,其它代码都是标准代码,基本上保持不变的:

按键创建、读取等:

if(key==3)//get info and file len

{

s = CH376DiskQuery( (PUINT32)buf );

printf("Querys1=0x%02x,0x%02x ",(unsigned short)s,USB_INT_SUCCESS );

printf("free cap2 = %ld MB", *(PUINT32)buf / ( 1000000 / DEF_SECTOR_SIZE ) );

strcpy( (char *)TarName, "\\TEST.TXT" );

s =CH376FileOpenPath( TarName );

printf("Opens3=0x%02x ",(unsigned short)s );

kk=CH376GetFileSize();

s = CH376FileClose( FALSE );   

 printf("Closes4=0x%02x;%d\n",(unsigned short)s,kk );

}

if (key==4)//read file

{

strcpy( (char *)TarName, "\\TEST.TXT" );

s =CH376FileOpenPath( TarName );

printf("Opens5=0x%02x ",(unsigned short)s );

strcpy((char *)buf, "Test" );// ,Hello World!

printf("memset6:%s;%d ",(char *)buf,i);

memset((char *)buf,0,64);//

s = CH376ByteLocate( 0 );

printf("Locate7=0x%02x ",(unsigned short)s );

s = CH376ByteRead( buf, 5,&i); 

if (s!=USB_INT_SUCCESS)//0x14

{

printf("Reads8=0x%02x ",(unsigned short)s );

}

else

{

buf[63]=0;

printf("Read9:%s;%d;%d;%d;%d ",(char *)buf,i,buf[0],buf[1],buf[2]); 

}

 s = CH376FileClose( FALSE );  

 printf("Closes10=0x%02x\n",(unsigned short)s );

}

if(key==5)////create file

{

strcpy( (char *)TarName, "\\TEST.TXT" ); 

s = CH376FileCreatePath( TarName );  

printf("Opens11=0x%02x ",(unsigned short)s );

strcpy((char *)buf, "Test ,Hello World gfhh!" );

s = CH376ByteWrite( buf, strlen((const char *)buf), NULL ); 

printf("Writes12=0x%02x ",(unsigned short)s );

 s = CH376FileClose( TRUE );   

 printf("Closes13=0x%02x\n",(unsigned short)s );

}

if(key==6)//// create dir and file

{

strcpy( (char *)TarName, "\\170106\\" ); 

s = CH376FileOpenPath( TarName );  

printf("Opens14=0x%02x ",(unsigned short)s );

if ( s != USB_INT_SUCCESS && s != ERR_OPEN_DIR ){

 s = CH376DirCreatePath( TarName );

printf("Creates15=0x%02x ",(unsigned short)s );

}

s = CH376FileClose( FALSE );   

 printf("Closes16=0x%02x ",(unsigned short)s );

//if (s == ERR_OPEN_DIR ){//

 strcpy( (char *)TarName, "\\170106\\150254.TXT" ); 

 s = CH376FileCreatePath( TarName );  

 printf("Creates17=0x%02x ",(unsigned short)s );

 strcpy((char *)buf, "Test ,Hello World gfhh!" );

 s = CH376ByteWrite( buf, strlen((const char *)buf), NULL ); 

 printf("Writes18=0x%02x ",(unsigned short)s );

   s = CH376FileClose( TRUE );  

   printf("Closes19=0x%02x\n",(unsigned short)s );

//}

}

 

SPI部分:

void CH376SPI_Init(void)

{

  GPIO_InitTypeDef GPIO_InitStructure;

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE );


RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE );

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_6|GPIO_Pin_11; //PA8-CH376 CS PA7-SDI PA5-SCK

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_InitStructure);


GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7|GPIO_Pin_12;//CH376 INT

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;   

//GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_InitStructure);

}

void mDelayuS( UINT8 us )

{

while ( us -- );

}

void mDelaymS( UINT8 ms )

{

while ( ms -- ) 

{

mDelayuS( 250 );

mDelayuS( 250 );

mDelayuS( 250 );

mDelayuS( 250 );

}

}

voidCH376_PORT_INIT( void )

{

CH376_SPI_SCS_H;//CH376_SPI_SCS = 1; 

  CH376_SPI_SCK_H;// CH376_INT_WIRE=1;  

}

voidxEndCH376Cmd( void )

CH376_SPI_SCS_H;//CH376_SPI_SCS = 1; 

}

voidSpi376OutByte( UINT8 d )

{  

    UINT8i;

for ( i = 0; i < 8; i ++ ) 

{

CH376_SPI_SCK_L;//CH376_SPI_SCK = 0;

if ( d & 0x80 ) 

{

   CH376_SPI_SDI_H;//CH376_SPI_SDI = 1;

}

else 

{

   CH376_SPI_SDI_L;//CH376_SPI_SDI = 0;

}

d <<= 1;                                                  


CH376_SPI_SCK_H;//CH376_SPI_SCK = 1;                               

}

}

UINT8Spi376InByte( void )

{

    UINT8i, d;

d = 0;

for ( i = 0; i < 8; i ++ ) 

{

CH376_SPI_SCK_L;//CH376_SPI_SCK = 0;                               

d <<= 1;                                                   if ( CH376_SPI_SDO )     


                                      {

   d ++;

}

CH376_SPI_SCK_H;//CH376_SPI_SCK = 1;

}

return( d );

}



voidxWriteCH376Cmd( UINT8 mCmd )

{

CH376_SPI_SCS_H;//CH376_SPI_SCS = 1; 

mDelayuS(10 );

mDelayuS(10 );

CH376_SPI_SCS_L;//CH376_SPI_SCS = 0;    

Spi376OutByte( mCmd ); 

mDelayuS(10 ); 

mDelayuS(10 ); 

mDelayuS(10 );  

mDelayuS(10 ); 

}

voidxWriteCH376Data( UINT8 mData )

{

Spi376OutByte( mData );

mDelayuS(10 ); 

}

UINT8xReadCH376Data( void )

{

mDelayuS(10);

return( Spi376InByte( ) );

}

UINT8Query376Interrupt( void )

{

UINT8 i;

i = (CH376_INT_WIRE ? FALSE : TRUE ); 

return( i ); 

}

UINT8mInitCH376Host( void )

{

UINT8res;

mDelaymS(200);

mDelaymS(200);

mDelaymS(200);


CH376_PORT_INIT( );   

xWriteCH376Cmd( CMD11_CHECK_EXIST );   

xWriteCH376Data( 0x55 );

res = xReadCH376Data( );

//printf("res =%02x \n",(unsigned short)res);

xEndCH376Cmd( );

if ( res != 0xAA ) return( ERR_USB_UNKNOWN );  

xWriteCH376Cmd( CMD11_SET_USB_MODE ); 

xWriteCH376Data( 0x06 ); 

mDelayuS( 20 );

res = xReadCH376Data( );

//printf("res =%02x \n",(unsigned short)res);

xEndCH376Cmd( );

if ( res == CMD_RET_SUCCESS ) 

{

   return( USB_INT_SUCCESS ); 

}

else 

{

   return( ERR_USB_UNKNOWN );

}

}



求救!


1:U盘新建文件返回值都是USB_INT_SUCCESS,并且CH376FileOpenPath( TarName )能打开、CH376GetFileSize()能得到正确大


小,但是大多数时候在电脑里面看不到刚刚新建的文件。


能打开、能得到正确大小,说明是创建成功的,为什么在电脑里面看不到文件?


2:无论在电脑里面能不能看到的文件,读取时,CH376ByteRead()等函数的返回值也都是USB_INT_SUCCESS,并且其RealCount参


数也是得到正确的大小,但是buf参数总是空字符串。


能打开、能得到正确大小,创建文件应该说也是成功的,RealCount参数也正确,为什么buf参数没有获取内容?


另外,获取的U盘剩余空间总是正确的。



代码里不要定义DEF_IC_V43_U。执行一次0x01命令查看一下版本号。


最后发现 不运行CH376DiskQuery( (PUINT32)buf );就可以。


百思不得其解。为什么执行CH376DiskQuery( (PUINT32)buf );就不行呢?而返回值什么的全部正确。


我用430单片机也遇到这个问题,后来使用从WCH公司买的芯片就没出现这个问题,我出问题的芯片是从淘宝买的。


谢谢!确实是淘宝买的。请问咋样从WCH公司买?



                           

谢谢!确实是淘宝买的。请问咋样从WCH公司买?


                       

直接联系销售电话或QQ:025-52638389或2668786168


大兄弟,你的版本号是多少?我的也遇到了 看不见写入文件 的问题。

我的版本号是 42H(02H).在xp或diskgenius下格式化则能成功。。


只有登录才能回复,可以选择微信账号登录