中级会员
- 积分
- 365
- 金钱
- 365
- 注册时间
- 2018-3-21
- 在线时间
- 64 小时
|
10金钱
问题描述如下
1、当前fatfs文件****单独*******读写U盘和flash都没有问题,文件的导入导出测试都通过了
2、当fatfs同时挂载U盘的时候,就会出现问题
测试过程
1、独立创建了一个fatfs读写flash的工程,使用CUBEMX,测试数据读写,发现没有问题
2、在原来工程上加入flash的读写,数据就不对,追踪之后发现void MX_FATFS_Init(void)函数里只要新增了U盘的fatfs初始化,后面测试代码就不成功
void MX_FATFS_Init(void)
{
/*## FatFS: Link the USBH driver ###########################*/
retUSBH = FATFS_LinkDriver(&USBH_Driver, USBHPath);
/*## FatFS: Link the USER driver ###########################*/
retUSER = FATFS_LinkDriver(&USER_Driver, USERPath);
/* USER CODE BEGIN Init */
/* additional user code for init */
/* USER CODE END Init */
}
只有把 retUSER = FATFS_LinkDriver(&USER_Driver, USERPath); 放到后面才能正常测试
测试函数如下
typedef struct
{
TickType_t w_tick;//<写的时间
TickType_t r_tick;//<读的时间
uint8_t result ;//<读写结果
uint8_t type ;//<测试的类型
}TEST_FATFS;
static TEST_FATFS test_fatfs[20][3];
uint8_t flash_file_init(void)
{
FRESULT retSD;
unsigned char work[4096] = {0};
retSD = f_mkfs((TCHAR const*)USERPath, FM_FAT, 0, work, 4096);
retSD = f_mount(&USERFatFS, USERPath, 1);//挂载虚拟磁盘
//retSD = f_mkfs("1:/", FM_FAT, 0, work, 4096);
//retSD = f_mount(&USERFatFS, "1:/", 1);//挂载虚拟磁盘
if(retSD == FR_OK)
{
f_open(&USERFile, FIELD_DEVICE_FILENAME, FA_CREATE_ALWAYS | FA_OPEN_ALWAYS);
f_close(&USERFile);
f_open(&USERFile, GROUP_FILENAME, FA_CREATE_ALWAYS | FA_OPEN_ALWAYS);
f_close(&USERFile);
f_open(&USERFile, RULE_FILENAME, FA_CREATE_ALWAYS | FA_OPEN_ALWAYS);
f_close(&USERFile);
}
return retSD;
}
/**
* @brief 测试falsh数据
* @return
* -# FR_OK:成功
*/
void flash_test_tick(void)
{
for(uint8_t i = 0; i < 8; i++)
{
for(uint8_t j = 0; j < 3; j++)
{
read_write_fatfs(i,j);
}
}
}
void read_write_fatfs(uint8_t i,uint8_t j)
{
FRESULT result = FR_OK;
TickType_t tick;
unsigned int count = 0;
switch(i)
{
case 0: //<512字节测试
if(result == FR_OK)
{
result = f_open(&USERFile, "1.txt", FA_CREATE_ALWAYS | FA_WRITE);
if(result == FR_OK)
{
test_fatfs[i][j].type = i;
uint8_t *buf_write = MALLOC(512);
uint8_t *buf_read = MALLOC(512);
if(buf_write == NULL || buf_read == NULL)
return;
memset(buf_write,0xA5,512);
///开始写计数
tick = xTaskGetTickCount();
result = f_write(&USERFile, buf_write,512, &count);
if(result == FR_OK)
{
result = 1;
test_fatfs[i][j].w_tick = xTaskGetTickCount() - tick;
f_close(&USERFile);
}else
{
result = 2;
f_close(&USERFile);
}
//<开始读计数
tick = xTaskGetTickCount();
result = f_open(&USERFile, "1.txt", FA_OPEN_EXISTING | FA_READ);
if(result == FR_OK)
{
result = f_read(&USERFile, buf_read, 512, &count);
test_fatfs[i][j].r_tick = xTaskGetTickCount() - tick;
result = 1;
f_close(&USERFile);
}else
{
result = 2;
f_close(&USERFile);
}
///比较读写结果,释放内存
test_fatfs[i][j].result = memcmp(buf_read,buf_write,512);
FREE(buf_read);
FREE(buf_write);
}
}
break;
case 1://1K字节测试
//result = f_mount(&USERFatFS,USERPath,1);//挂载虚拟磁盘
result = f_mount(&USERFatFS,"1:/",1);//挂载虚拟磁盘
if(result == FR_OK)
{
result = f_open(&USERFile, "1.txt", FA_CREATE_ALWAYS | FA_WRITE);
if(result == FR_OK)
{
test_fatfs[i][j].type = i;
uint8_t *buf_write = MALLOC(1024);
uint8_t *buf_read = MALLOC(1024);
if(buf_write == NULL || buf_read == NULL)
return;
memset(buf_write,0xA5,1024);
///开始写计数
tick = xTaskGetTickCount();
result = f_write(&USERFile, buf_write,1024, &count);
if(result == FR_OK)
{
result = 1;
test_fatfs[i][j].w_tick = xTaskGetTickCount() - tick;
f_close(&USERFile);
}else
{
result = 2;
f_close(&USERFile);
}
//<开始读计数
tick = xTaskGetTickCount();
result = f_open(&USERFile, "1.txt", FA_OPEN_EXISTING | FA_READ);
if(result == FR_OK)
{
result = f_read(&USERFile, buf_read, 1024, &count);
test_fatfs[i][j].r_tick = xTaskGetTickCount() - tick;
result = 1;
f_close(&USERFile);
}else
{
result = 2;
f_close(&USERFile);
}
///比较读写结果,释放内存
test_fatfs[i][j].result = memcmp(buf_read,buf_write,1024);
FREE(buf_read);
FREE(buf_write);
}
}
break;
case 2://<10k数据测试
//result = f_mount(&USERFatFS,USERPath,1);//挂载虚拟磁盘
result = f_mount(&USERFatFS,"1:/",1);//挂载虚拟磁盘
if(result == FR_OK)
{
result = f_open(&USERFile, "1.txt", FA_CREATE_ALWAYS | FA_WRITE);
if(result == FR_OK)
{
test_fatfs[i][j].type = i;
uint8_t *buf_write = MALLOC(10*1024);
uint8_t *buf_read = MALLOC(10*1024);
if(buf_write == NULL || buf_read == NULL)
return;
memset(buf_write,0xA5,10*1024);
///开始写计数
tick = xTaskGetTickCount();
result = f_write(&USERFile, buf_write,10*1024, &count);
if(result == FR_OK)
{
result = 1;
test_fatfs[i][j].w_tick = xTaskGetTickCount() - tick;
f_close(&USERFile);
}else
{
result = 2;
f_close(&USERFile);
}
//<开始读计数
tick = xTaskGetTickCount();
result = f_open(&USERFile, "1.txt", FA_OPEN_EXISTING | FA_READ);
if(result == FR_OK)
{
result = f_read(&USERFile, buf_read, 10*1024, &count);
test_fatfs[i][j].r_tick = xTaskGetTickCount() - tick;
result = 1;
f_close(&USERFile);
}else
{
result = 2;
f_close(&USERFile);
}
///比较读写结果,释放内存
test_fatfs[i][j].result = memcmp(buf_read,buf_write,10*1024);
FREE(buf_read);
FREE(buf_write);
}
}
break;
case 3://<100k
//result = f_mount(&USERFatFS,USERPath,1);//挂载虚拟磁盘
result = f_mount(&USERFatFS,"1:/",1);//挂载虚拟磁盘
if(result == FR_OK)
{
result = f_open(&USERFile, "1.txt", FA_CREATE_ALWAYS | FA_WRITE);
if(result == FR_OK)
{
test_fatfs[i][j].type = i;
uint8_t *buf_write = MALLOC(100*1024);
uint8_t *buf_read = MALLOC(100*1024);
if(buf_write == NULL || buf_read == NULL)
return;
memset(buf_write,0xA5,100*1024);
///开始写计数
tick = xTaskGetTickCount();
result = f_write(&USERFile, buf_write,100*1024, &count);
if(result == FR_OK)
{
result = 1;
test_fatfs[i][j].w_tick = xTaskGetTickCount() - tick;
f_close(&USERFile);
}else
{
result = 2;
f_close(&USERFile);
}
//<开始读计数
tick = xTaskGetTickCount();
result = f_open(&USERFile, "1.txt", FA_OPEN_EXISTING | FA_READ);
if(result == FR_OK)
{
result = f_read(&USERFile, buf_read, 100*1024, &count);
test_fatfs[i][j].r_tick = xTaskGetTickCount() - tick;
result = 1;
f_close(&USERFile);
}else
{
result = 2;
f_close(&USERFile);
}
///比较读写结果,释放内存
test_fatfs[i][j].result = memcmp(buf_read,buf_write,100*1024);
FREE(buf_read);
FREE(buf_write);
}
}
break;
case 4://<1M数据读写
//result = f_mount(&USERFatFS,USERPath,1);//挂载虚拟磁盘
result = f_mount(&USERFatFS,"1:/",1);//挂载虚拟磁盘
if(result == FR_OK)
{
result = f_open(&USERFile, "1.txt", FA_CREATE_ALWAYS | FA_WRITE);
if(result == FR_OK)
{
test_fatfs[i][j].type = i;
uint8_t *buf_write = MALLOC(1000*1024);
uint8_t *buf_read = MALLOC(1000*1024);
if(buf_write == NULL || buf_read == NULL)
return;
memset(buf_write,0xA5,1000*1024);
///开始写计数
tick = xTaskGetTickCount();
result = f_write(&USERFile, buf_write,1000*1024, &count);
if(result == FR_OK)
{
result = 1;
test_fatfs[i][j].w_tick = xTaskGetTickCount() - tick;
f_close(&USERFile);
}else
{
result = 2;
f_close(&USERFile);
}
//<开始读计数
tick = xTaskGetTickCount();
result = f_open(&USERFile, "1.txt", FA_OPEN_EXISTING | FA_READ);
if(result == FR_OK)
{
result = f_read(&USERFile, buf_read, 1000*1024, &count);
test_fatfs[i][j].r_tick = xTaskGetTickCount() - tick;
result = 1;
f_close(&USERFile);
}else
{
result = 2;
f_close(&USERFile);
}
///比较读写结果,释放内存
test_fatfs[i][j].result = memcmp(buf_read,buf_write,1000*1024);
FREE(buf_read);
FREE(buf_write);
}
}
break;
case 5://<2M
//result = f_mount(&USERFatFS,USERPath,1);//挂载虚拟磁盘
result = f_mount(&USERFatFS,"1:/",1);//挂载虚拟磁盘
if(result == FR_OK)
{
result = f_open(&USERFile, "1.txt", FA_CREATE_ALWAYS | FA_WRITE);
if(result == FR_OK)
{
test_fatfs[i][j].type = i;
uint8_t *buf_write = MALLOC(2000*1024);
uint8_t *buf_read = MALLOC(2000*1024);
if(buf_write == NULL || buf_read == NULL)
return;
memset(buf_write,0xA5,2000*1024);
///开始写计数
tick = xTaskGetTickCount();
result = f_write(&USERFile, buf_write,2000*1024, &count);
if(result == FR_OK)
{
result = 1;
test_fatfs[i][j].w_tick = xTaskGetTickCount() - tick;
f_close(&USERFile);
}else
{
result = 2;
f_close(&USERFile);
}
//<开始读计数
tick = xTaskGetTickCount();
result = f_open(&USERFile, "1.txt", FA_OPEN_EXISTING | FA_READ);
if(result == FR_OK)
{
result = f_read(&USERFile, buf_read, 2000*1024, &count);
test_fatfs[i][j].r_tick = xTaskGetTickCount() - tick;
result = 1;
f_close(&USERFile);
}else
{
result = 2;
f_close(&USERFile);
}
///比较读写结果,释放内存
test_fatfs[i][j].result = memcmp(buf_read,buf_write,2000*1024);
FREE(buf_read);
FREE(buf_write);
}
}
break;
case 6://<4M
//result = f_mount(&USERFatFS,USERPath,1);//挂载虚拟磁盘
result = f_mount(&USERFatFS,"1:/",1);//挂载虚拟磁盘
if(result == FR_OK)
{
result = f_open(&USERFile, "1.txt", FA_CREATE_ALWAYS | FA_WRITE);
if(result == FR_OK)
{
test_fatfs[i][j].type = i;
uint8_t *buf_write = MALLOC(4000*1024);
uint8_t *buf_read = MALLOC(4000*1024);
if(buf_write == NULL || buf_read == NULL)
return;
memset(buf_write,0xA5,4000*1024);
///开始写计数
tick = xTaskGetTickCount();
result = f_write(&USERFile, buf_write,4000*1024, &count);
if(result == FR_OK)
{
result = 1;
test_fatfs[i][j].w_tick = xTaskGetTickCount() - tick;
f_close(&USERFile);
}else
{
result = 2;
f_close(&USERFile);
}
//<开始读计数
tick = xTaskGetTickCount();
result = f_open(&USERFile, "1.txt", FA_OPEN_EXISTING | FA_READ);
if(result == FR_OK)
{
result = f_read(&USERFile, buf_read, 4000*1024, &count);
test_fatfs[i][j].r_tick = xTaskGetTickCount() - tick;
result = 1;
f_close(&USERFile);
}else
{
result = 2;
f_close(&USERFile);
}
///比较读写结果,释放内存
test_fatfs[i][j].result = memcmp(buf_read,buf_write,4000*1024);
FREE(buf_read);
FREE(buf_write);
}
}
break;
case 7:
//result = f_mount(&USERFatFS,USERPath,1);//挂载虚拟磁盘
result = f_mount(&USERFatFS,"1:/",1);//挂载虚拟磁盘
if(result == FR_OK)
{
result = f_open(&USERFile, "1.txt", FA_CREATE_ALWAYS | FA_WRITE);
if(result == FR_OK)
{
test_fatfs[i][j].type = i;
uint8_t *buf_write = MALLOC(8000*1024);
uint8_t *buf_read = MALLOC(8000*1024);
if(buf_write == NULL || buf_read == NULL)
return;
memset(buf_write,0xA5,8000*1024);
///开始写计数
tick = xTaskGetTickCount();
result = f_write(&USERFile, buf_write,8000*1024, &count);
if(result == FR_OK)
{
result = 1;
test_fatfs[i][j].w_tick = xTaskGetTickCount() - tick;
f_close(&USERFile);
}else
{
result = 2;
f_close(&USERFile);
}
//<开始读计数
tick = xTaskGetTickCount();
result = f_open(&USERFile, "1.txt", FA_OPEN_EXISTING | FA_READ);
if(result == FR_OK)
{
result = f_read(&USERFile, buf_read, 8000*1024, &count);
test_fatfs[i][j].r_tick = xTaskGetTickCount() - tick;
result = 1;
f_close(&USERFile);
}else
{
result = 2;
f_close(&USERFile);
}
///比较读写结果,释放内存
test_fatfs[i][j].result = memcmp(buf_read,buf_write,8000*1024);
FREE(buf_read);
FREE(buf_write);
}
}
break;
}
}
只要初始化顺序不对就有问题,就很奇怪,独立测试U盘和flash没有问题
|
|