我发现一个小技巧可以解决我的问题:
基本上它不会在 SD 卡上写入,因为缓冲区尚未满(在 XILINX 的低级 API 中)。
所以我所做的很简单:我只写一次数据。虽然文件的大小不等于或大于写入数据的大小,但我写了 0(按 32 块)
另外,另一件重要的事情:你写的数据必须是 32 字节对齐的!\ 这是我的(工作)代码:
size_t SizeOfFile(char const *path)
{
FILE *fp = fopen(path, "r");
fseek(fp, 0, SEEK_END);
size_t fsize = (size_t) ftell(fp);
fclose(fp);
return fsize;
}
void WriteFile( char const* fileName, char* buffer, size_t size )
{
size_t allignement = ( size + 32 - 1 ) & ~ ( 32 - 1 ); // We must allign by 32
char* Buffer_logger = pleb_malloc( allignement );
pleb_memset( Buffer_logger, 0, allignement );
pleb_memcpy( Buffer_logger, buffer, size );
unsigned int BytesWr;
size_t accum = 0;
result = f_open( &file, fileName, FA_CREATE_ALWAYS | FA_WRITE | FA_READ );
if ( result != 0 )
{
return;
}
sprintf( Buffer_logger, "%s", buffer );
while ( SizeOfFile( fileName ) == 0 )
{
// Open log for writing
result = f_open( &file, fileName, FA_WRITE );
if ( result != 0 )
{
return;
}
// Point to the EOF
result = f_lseek( &file, accum );
if ( result != 0 )
{
return;
}
// Write to log
result = f_write( &file, (const void*) Buffer_logger, size, &BytesWr );
if ( result != 0 )
{
return;
}
accum += accum + strlen( Buffer_logger );
//Close file.
result = f_close( &file );
if ( result != 0 )
{
return;
}
pleb_memset( Buffer_logger, 0, allignement );
size = 32;
pleb_memset( Buffer_logger, 0, size );
}
pleb_free( Buffer_logger );
PRINT( ("Data written to log Successfully\r\n") );
}