求助:关于TF卡用Fatfs实现读写

2019-07-20 13:02发布

平台是STM32F4探索者自己移植Fatfs在探索板上使用
SPI模式 淘宝买的TF卡模块 QQ图片20170728143329.png
在串口在打印的信息如下
disk_initialize Success : 0

mount filesystem 0 Success : 0

write file test......

f_open() failed : 13

f_puts() success
f_puts() success
file size:0
f_read() fail

f_open返回值为13  There is no valid FAT volume
困惑很多天了 求大神解答疑惑 谢谢!!
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
2条回答
Divenire
2019-07-20 22:56
/*                一些初始化                */  
        SystemInit();
        LED_Init();                                                //初始化LED
        uart_init(115200);                                //初始化串口波特率为115200
        delay_init(168);                                  //初始化延时函数
        MSD0_SPI_Configuration();                //初始化SPI
       
        /*                        初始化SD卡                        */
        Res = disk_initialize(0);
        if(Res != RES_OK)
        {
                printf("disk_initialize failed : %d ",Res);
        }
        else
        {
                printf("disk_initialize Success : %d ",Res);
        }
       
        res = f_mount(0,&fs);
        if(res != FR_OK)
        {
                printf("mount filesystem 0 failed : %d ",res);
        }
        else
        {
                printf("mount filesystem 0 Success : %d ",res);
        }
      
    //写文件  
        printf("write file test...... ");
        //打开文件  如果data.txt存在,则打开;否则,创建一个新文件
    res = f_open(&file, "0:/A.txt",FA_OPEN_ALWAYS|FA_READ|FA_WRITE );  
    if(res!=FR_OK) { printf("f_open() failed : %d ",res); }  
    else { printf("f_open() Success : %d ",res); }  
       
    //将指针指向文件末  
    res = f_lseek(&file, file.fsize);     
    br = f_puts("1234567890", &file) ;  //向文件末写入字符串  
    if(br<1) { printf("f_puts() fail "); }  
    else { printf("f_puts() success "); }  
      
    res = f_lseek(&file, 2);     
    br = f_puts("--", &file) ;  //向文件末写入字符串  
    if(br<1) { printf("f_puts() fail "); }  
    else { printf("f_puts() success "); }  
      
    //读文件  
    br = file.fsize;  
    printf("file size:%d ",br);  
    res = f_read(&file, buffer, file.fsize, &br);     //一次读一个字节知道读完全部文件信息  
    if(res == FR_OK ) { printf("text:%s ", buffer); }  
    else { printf(" f_read() fail "); }  
  
    //关闭文件  
    f_close(&file);  
/*其他的一些操作*/  
        while(1)
        {
                delay_ms(200);                                           
                LED0=!LED0;
        }
        return 0;

一周热门 更多>