CH376_RST = 1; /* 复位 */
delay_ms( 20 );
CH376_RST = 0; /* 禁止复位 */
delay_ms( 100 ); /* 延时100毫秒 */
imInitCH376Host( ) ; /* 初始化CH376 */
/* 读写U盘 */
if(CH376DiskConnect()==USB_INT_SUCCESS)/* 检查U盘是否连接,不支持SD卡 */
{
if(CH376DiskMount()==USB_INT_SUCCESS)/* 初始化磁盘并测试磁盘是否就绪 */
{
/* 获取出厂信息 */
i = CH376ReadBlock( buf ); /* 如果需要,可以读取数据块CH376_CMD_DATA.DiskMountInq,返回长度 */
if ( i == sizeof( INQUIRY_DATA ) ) /* U盘的厂商和产品信息 */
{
buf[ i ] = 0;
printf( "UdiskInfo: %s
", ((P_INQUIRY_DATA)buf) -> VendorIdStr );
}
printf( "DiskQuery: " ); /* 检查U盘或者SD卡的剩余空间 */
s = CH376DiskQuery( (PUINT32)buf ); /* 查询磁盘剩余空间信息,扇区数 */
printf("s=%02x
",(unsigned short)s );
printf( "free cap = %ld MB
", *(PUINT32)buf / ( 1000000 / DEF_SECTOR_SIZE ) );
/* 创建 */
printf( "Create file :" );
strcpy( (char *)TarName, "
\ABC.TXT" ); /* 目标文件名 */
s = CH376FileCreatePath( TarName ); /* 新建多级目录下的文件,支持多级目录路径,输入缓冲区必须在RAM中 */
printf("s=%02x
",(unsigned short)s );
/* 写入*/
printf( "Write :" );
strcpy((char *)buf, "测试成功1234567890" );
s = CH376ByteWrite( buf, strlen((const char *)buf), NULL ); /* 以字节为单位向当前位置写入数据块 */
printf("s=%02x
",(unsigned short)s );
/* 关闭文件 */
printf("close file " );
s = CH376FileClose( TRUE ); /* 关闭文件,对于字节读写建议自动更新文件长度 */
printf("s=%02x
",(unsigned short)s );
}
}
完整工程
http://www.openedv.com/posts/list/6006.htm
UINT32 NewSize;
UINT32 SIZE=0;
UINT8 FileDataBuf[2048] __attribute__((at(0X68000000)));void mFlushBufferToDisk( UINT8 force )
/* force = 0 自动刷新, force != 0 强制刷新*/
{
UINT8 s;
if ( force )
{
s = ( total + DEF_SECTOR_SIZE - 1 ) / DEF_SECTOR_SIZE;
if ( s ) {
s = CH376SecWrite( FileDataBuf, s, NULL );
//mStopIfError( s );
mDelaymS( 1 );
memcpy( FileDataBuf, & FileDataBuf[ total & ~ ( DEF_SECTOR_SIZE - 1 ) ], total & ( DEF_SECTOR_SIZE - 1 ) );
total &= DEF_SECTOR_SIZE - 1;
}
NewSize = CH376GetFileSize( );
if ( total ) NewSize -= DEF_SECTOR_SIZE - total;
CH376WriteVar32( VAR_FILE_SIZE, NewSize );
//printf( "Current file size is %ld ", NewSize );
s = CH376SecWrite( FileDataBuf, 0, NULL );
//mStopIfError( s );
s = CH376SecLocate( 0xFFFFFFFF );
//mStopIfError( s );
s = CH376SecRead( FileDataBuf, 1, NULL );
}
else if ( total >= sizeof( FileDataBuf ) - DEF_SECTOR_SIZE )
{
s = CH376SecWrite( FileDataBuf, (UINT8)( total / DEF_SECTOR_SIZE ), NULL );
//mStopIfError( s );
memcpy( FileDataBuf, & FileDataBuf[ total & ~ ( DEF_SECTOR_SIZE - 1 ) ], total & ( DEF_SECTOR_SIZE - 1 ) );
total &= DEF_SECTOR_SIZE - 1;
}
}
下面是main()中的一段
while ( CH376DiskConnect( ) != USB_INT_SUCCESS )
{
mDelaymS( 100 );
}
mDelaymS( 200 );
for ( i = 0; i < 100; i ++ )
{
mDelaymS( 50 );
s = CH376DiskMount( );
if ( s == USB_INT_SUCCESS )
break;
else if ( s == ERR_DISK_DISCON )
break;
if ( CH376GetDiskStatus( ) >= DEF_DISK_MOUNTED && i >= 5 )
break;
}
if ( s == ERR_DISK_DISCON )
{
while(1);
//continue;
}
if ( CH376GetDiskStatus( ) < DEF_DISK_MOUNTED )
{
while(1);
}
i = CH376ReadBlock( buf );
if ( i == sizeof( INQUIRY_DATA ) )
{
buf[ i ] = 0;
sprintf(str, "%s", ((P_INQUIRY_DATA)buf) -> VendorIdStr );
}
s = CH376FileOpen( "/TEST.TXT" );
if ( s == USB_INT_SUCCESS )
{
NewSize = CH376GetFileSize( );
s = CH376SecLocate( 0xFFFFFFFF );
total = NewSize & ( DEF_SECTOR_SIZE - 1 );
if ( total )
{
s = CH376SecRead( FileDataBuf, 1, NULL );
s = CH376SecLocate( 0xFFFFFFFF );
}
}
else if ( s == ERR_MISS_FILE )
{
s = CH376FileCreate( NULL );
//mStopIfError( s );
total = 0;
NewSize = 0;
}
total += sprintf((char *)FileDataBuf + total, "在本次添加数据之前,该文件已有数据的长度是%ld字节xdxa", NewSize );
mFlushBufferToDisk( 0 );
for(aa=0;aa<100;aa++)//实验用数组
{
FileDataBuf[aa]=0XAA;
total += sprintf((char *)FileDataBuf+ total, "FileDataBuf[aa]=%Xxdxa", FileDataBuf[aa]);
}//将二进制数转化成字符
mFlushBufferToDisk( 0 );
total += sprintf( (char *)FileDataBuf + total, " ********************************* " );
total += sprintf( (char *)FileDataBuf + total, "这次的数据到此结束,程序即将退出xdxa" );
mFlushBufferToDisk( 1 );
s = SetFileCreateTime( "/TEST.TXT", MAKE_FILE_DATE( 2016, 3, 3 ), MAKE_FILE_TIME( 11, 48, 20 ) );
s = CH376FileClose( FALSE );
mDelaymS( 200 );
以上是mian函数中的一部分,当首次往U盘的TXT文件中写数据时,会出现部分数据丢失;当U盘的TXT文件中已有数据再向其中写数据,会出现U盘的TXT文件中已存的数据部分丢失。请问这是为什么。还有就是只可以向U盘中写TXT文件吗?我新建doc文件之后,在电脑上打不开,说是不存在U盘中
扇区读写,会把同扇区已有的数据擦掉。这是我的理解。可以先读出来再一起写。话说我遇到写u盘写不进,有时文件都创建不了。是什么原因?
一周热门 更多>