急!stm32f103+ch376 U盤讀寫文件,已折騰三四天了!

急!stm32f103+ch376 U盤讀寫文件,已折騰三四天了!


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


小,但是大多數(shù)時候在電腦里面看不到剛剛新建的文件。


能打開、能得到正確大小,說明是創(chuàng)建成功的,為什么在電腦里面看不到文件?


2:無論在電腦里面能不能看到的文件,讀取時,CH376ByteRead()等函數(shù)的返回值也都是USB_INT_SUCCESS,并且其RealCount參


數(shù)也是得到正確的大小,但是buf參數(shù)總是空字符串。


能打開、能得到正確大小,創(chuàng)建文件應(yīng)該說也是成功的,RealCount參數(shù)也正確,為什么buf參數(shù)沒有獲取內(nèi)容?


另外,獲取的U盤剩余空間總是正確的。


下面附部分關(guān)鍵代碼,其它代碼都是標(biāo)準(zhǔn)代碼,基本上保持不變的:

按鍵創(chuàng)建、讀取等:

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()能得到正確大


小,但是大多數(shù)時候在電腦里面看不到剛剛新建的文件。


能打開、能得到正確大小,說明是創(chuàng)建成功的,為什么在電腦里面看不到文件?


2:無論在電腦里面能不能看到的文件,讀取時,CH376ByteRead()等函數(shù)的返回值也都是USB_INT_SUCCESS,并且其RealCount參


數(shù)也是得到正確的大小,但是buf參數(shù)總是空字符串。


能打開、能得到正確大小,創(chuàng)建文件應(yīng)該說也是成功的,RealCount參數(shù)也正確,為什么buf參數(shù)沒有獲取內(nèi)容?


另外,獲取的U盤剩余空間總是正確的。



代碼里不要定義DEF_IC_V43_U。執(zhí)行一次0x01命令查看一下版本號。


最后發(fā)現(xiàn) 不運行CH376DiskQuery( (PUINT32)buf );就可以。


百思不得其解。為什么執(zhí)行CH376DiskQuery( (PUINT32)buf );就不行呢?而返回值什么的全部正確。


我用430單片機也遇到這個問題,后來使用從WCH公司買的芯片就沒出現(xiàn)這個問題,我出問題的芯片是從淘寶買的。


謝謝!確實是淘寶買的。請問咋樣從WCH公司買?



? ? ? ? ? ? ? ? ? ? ? ? ? ?

謝謝!確實是淘寶買的。請問咋樣從WCH公司買?


? ? ? ? ? ? ? ? ? ? ? ?

直接聯(lián)系銷售電話或QQ:025-52638389或2668786168


大兄弟,你的版本號是多少?我的也遇到了 看不見寫入文件 的問題。

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


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

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