zoukankan      html  css  js  c++  java
  • 使用FATFS文件系统读写SD卡

    定义相关变量

    /* USER CODE BEGIN PV */
    /* Private variables ---------------------------------------------------------*/
    #include "stdio.h"
    FATFS fs;                 // Work area (file system object) for logical drive
    FIL fil;                  // file objects
     
    uint32_t byteswritten;                /* File write counts */
    uint32_t bytesread;                   /* File read counts */
    uint8_t wtext[] = "This is STM32 working with FatFs"; /* File write buffer */
    uint8_t rtext[100];                     /* File read buffers */
    char filename[] = "STM32cube.txt";
    /* USER CODE END PV */
    

    进行读写

    /* USER CODE BEGIN 2 */
        printf("
     ****** FatFs Example ******
    
    ");
     
        /*##-1- Register the file system object to the FatFs module ##############*/
        retSD = f_mount(&fs, "", 0);
        if(retSD)
        {
            printf(" mount error : %d 
    ",retSD);
            Error_Handler();
        }
        else
            printf(" mount sucess!!! 
    ");
         
        /*##-2- Create and Open new text file objects with write access ######*/
        retSD = f_open(&fil, filename, FA_CREATE_ALWAYS | FA_WRITE);
        if(retSD)
            printf(" open file error : %d
    ",retSD);
        else
            printf(" open file sucess!!! 
    ");
         
        /*##-3- Write data to the text files ###############################*/
        retSD = f_write(&fil, wtext, sizeof(wtext), (void *)&byteswritten);
        if(retSD)
            printf(" write file error : %d
    ",retSD);
        else
        {
            printf(" write file sucess!!! 
    ");
            printf(" write Data : %s
    ",wtext);
        }
         
        /*##-4- Close the open text files ################################*/
        retSD = f_close(&fil);
        if(retSD)
            printf(" close error : %d
    ",retSD);
        else
            printf(" close sucess!!! 
    ");
         
        /*##-5- Open the text files object with read access ##############*/
        retSD = f_open(&fil, filename, FA_READ);
        if(retSD)
            printf(" open file error : %d
    ",retSD);
        else
            printf(" open file sucess!!! 
    ");
         
        /*##-6- Read data from the text files ##########################*/
        retSD = f_read(&fil, rtext, sizeof(rtext), (UINT*)&bytesread);
        if(retSD)
            printf(" read error!!! %d
    ",retSD);
        else
        {
            printf(" read sucess!!! 
    ");
            printf(" read Data : %s
    ",rtext);
        }
         
        /*##-7- Close the open text files ############################*/
        retSD = f_close(&fil);
        if(retSD)  
            printf(" close error!!! %d
    ",retSD);
        else
            printf(" close sucess!!! 
    ");
         
        /*##-8- Compare read data with the expected data ############*/
        if(bytesread == byteswritten)
        { 
            printf(" FatFs is working well!!!
    ");
        }
      /* USER CODE END 2 */
    

    完整的代码工程链接:https://pan.baidu.com/s/11ptg47VsVxnL_2rfkcFXdg
    提取码:tciq

  • 相关阅读:
    linux虚拟机系统的复制或克隆后续问题解决!
    Linux命令大全
    Linux 介绍快速浏览
    Linux软件管理和安装
    Kali linux 2016.2(Rolling)安装之后的常用配置
    使用ttXactAdmin、ttSQLCmdCacheInfo、ttSQLCmdQueryPlan获取SQL相关具体信息[TimesTen运维]
    MongoDB--Getting Started with Java Driver
    OpenCV 通过 MFC 的 Picture Control 控件操作图像
    HTML学习笔记之中的一个(input文件选择框的封装)
    hdu 5282 Senior's String 两次dp
  • 原文地址:https://www.cnblogs.com/mengydz/p/13092609.html
Copyright © 2011-2022 走看看