本帖最后由 DX3906 于 2023-12-14 11:34 编辑
我在at32f435上用fatfs周期写16KB数据到sd卡,偶尔会出现写文件失败和打开文件失败,有时候重新上电几次之后又能长时间稳定写入,fatfs报错是FR_DISK_ERR,但是sdio驱动和fatfs都是直接用的官方的,换了几张卡都有一定几率出现
想问下各位大佬这可能是哪些方面的问题呢?
这个是我的测试代码,在freertos下跑的,只有这一个任务
[C] 纯文本查看 复制代码 void log_store_init(void)
{
FRESULT ret;
ret = f_mount(NULL, "1:", 1);
ret = f_mount(&fs, "1:", 1);
if(ret){
DEBUG_PRINTF("fs mount err:%d.\r\n", ret);
if(ret == FR_NO_FILESYSTEM){
DEBUG_PRINTF("create fatfs..\r\n");
BYTE *work = (BYTE *)pvPortMalloc(FF_MAX_SS);
ret = f_mkfs("1:", 0, work, sizeof(work));
if(ret){
DEBUG_PRINTF("creates fatfs err:%d.\r\n", ret);
return;
}
else{
DEBUG_PRINTF("creates fatfs ok.\r\n");
}
ret = f_mount(NULL, "1:", 1);
ret = f_mount(&fs, "1:", 1);
if(ret){
DEBUG_PRINTF("fs mount err:%d.\r\n", ret);
return;
}
else{
DEBUG_PRINTF("fs mount ok.\r\n");
}
}
else{
return;
}
}
else{
DEBUG_PRINTF("fs mount ok.\r\n");
}
}
char filename[] = "1:/test_data0000.bin";
void write_sd_test(void)
{
FIL file0;
FRESULT ret;
uint32_t len = 0;
static uint64_t total_len = 0;
static uint16_t file_count = 0;
if(file_count >= 940)
{
file_count = 0;
}
char *filename = (char *)pvPortMalloc(22);
sprintf(filename, "1:/test_data%04d.bin", file_count);
if(filename == NULL)
{
DEBUG_PRINTF("firename malloc failed.\r\n");
return;
}
ret = f_open(&file0, filename, FA_WRITE | FA_CREATE_ALWAYS | FA_OPEN_ALWAYS);
if(ret){
DEBUG_PRINTF("open file err:%d.\r\n", ret);
}
else{
DEBUG_PRINTF("open file ok. %s\r\n", filename);
uint8_t *buf = (uint8_t *)pvPortMalloc(16384);
if(buf == NULL)
{
DEBUG_PRINTF("buf malloc failed.\r\n");
if(filename != NULL)
vPortFree(filename);
f_close(&file0);
return;
}
else
{
for(uint32_t i = 0; i < 500; i++)
{
for(uint16_t j = 0; j < 16384; j++)
{
buf[j] = rand();
}
f_lseek(&file0, len);
ret = f_write(&file0, (uint8_t*)buf, 16384, &bytes_written);
if(ret){
DEBUG_PRINTF("write file err:%d.\r\n", ret);
f_close(&file0);
ret = f_mount(NULL, "1:", 1);
ret = f_mount(&fs, "1:", 1);
if(ret){
DEBUG_PRINTF("fs mount err:%d.\r\n", ret);
return;
}
else{
DEBUG_PRINTF("fs mount ok.\r\n");
}
break;
}
else{
f_sync(&file0);
len += bytes_written;
total_len += bytes_written;
DEBUG_PRINTF("write file ok, byte:%u, total:%lld\r\n", bytes_written, total_len);
}
vTaskDelay(1);
}
f_close(&file0);
file_count++;
}
if(buf != NULL)
vPortFree(buf);
}
if(filename != NULL)
vPortFree(filename);
}
static void log_task(void* parameter)
{
portBASE_TYPE uxHighWaterMark;
__IO size_t xFreeStackSpace;
log_store_init();
while(1)
{
xFreeStackSpace = xPortGetFreeHeapSize();
DEBUG_PRINTF("free heap size: %d\r\n", xFreeStackSpace);
write_sd_test();
DEBUG_PRINTF("%s runing\r\n", LOG_TASK_NAME);
vTaskDelay(1);
}
}
|