[C] 纯文本查看 复制代码
/*
*********************************************************************************************************
* 函 数 名: WriteFileTest
* 功能说明: 测试文件读写速度
* 形 参:无
* 返 回 值: 无
*********************************************************************************************************
*/
static void WriteFileTest(void)
{
FRESULT result;
char path[64];
uint32_t bw;
uint32_t i,k;
uint32_t runtime1,runtime2,timelen;
uint8_t err = 0;
static uint32_t s_ucTestSn = 0;
for (i = 0; i < sizeof(g_TestBuf); i++)
{
g_TestBuf[i] = (i / 512) + '0';
}
/* 挂载文件系统 */
result = f_mount(&fs, DiskPath, 0); /* Mount a logical drive */
if (result != FR_OK)
{
printf("挂载文件系统失败 (%s)\r\n", FR_Table[result]);
}
/* 打开文件 */
sprintf(path, "%sS%05d.txt", DiskPath, s_ucTestSn++); /* 每写1次,序号递增 */
result = f_open(&file, path, FA_CREATE_ALWAYS | FA_WRITE);
/* 写一串数据 */
printf("开始写文件%s %dKB ...\r\n", path, TEST_FILE_LEN / 1024);
runtime1 = bsp_GetRunTime(); /* 读取系统运行时间 */
for (i = 0; i < TEST_FILE_LEN / BUF_SIZE; i++)
{
result = f_write(&file, g_TestBuf, sizeof(g_TestBuf), &bw);
if (result == FR_OK)
{
if (((i + 1) % 8) == 0)
{
printf(".");
}
}
else
{
err = 1;
printf("%s文件写失败\r\n", path);
break;
}
}
runtime2 = bsp_GetRunTime(); /* 读取系统运行时间 */
if (err == 0)
{
timelen = (runtime2 - runtime1);
printf("\r\n 写耗时 : %dms 平均写速度 : %dB/S (%dKB/S)\r\n",
timelen,
(TEST_FILE_LEN * 1000) / timelen,
((TEST_FILE_LEN / 1024) * 1000) / timelen);
}
f_close(&file); /* 关闭文件*/
/* 开始读文件测试 */
result = f_open(&file, path, FA_OPEN_EXISTING | FA_READ);
if (result != FR_OK)
{
printf("没有找到文件: %s\r\n", path);
return;
}
printf("开始读文件 %dKB ...\r\n", TEST_FILE_LEN / 1024);
runtime1 = bsp_GetRunTime(); /* 读取系统运行时间 */
for (i = 0; i < TEST_FILE_LEN / BUF_SIZE; i++)
{
result = f_read(&file, g_TestBuf, sizeof(g_TestBuf), &bw);
if (result == FR_OK)
{
if (((i + 1) % 8) == 0)
{
printf(".");
}
/* 比较写入的数据是否正确,此语句会导致读卡速度结果降低到 3.5MBytes/S */
for (k = 0; k < sizeof(g_TestBuf); k++)
{
if (g_TestBuf[k] != (k / 512) + '0')
{
err = 1;
printf("Speed1.txt 文件读成功,但是数据出错\r\n");
break;
}
}
if (err == 1)
{
break;
}
}
else
{
err = 1;
printf("Speed1.txt 文件读失败\r\n");
break;
}
}
runtime2 = bsp_GetRunTime(); /* 读取系统运行时间 */
if (err == 0)
{
timelen = (runtime2 - runtime1);
printf("\r\n 读耗时 : %dms 平均读速度 : %dB/S (%dKB/S)\r\n", timelen,
(TEST_FILE_LEN * 1000) / timelen, ((TEST_FILE_LEN / 1024) * 1000) / timelen);
}
/* 关闭文件*/
f_close(&file);
/* 卸载文件系统 */
f_mount(NULL, DiskPath, 0);
}