通过stm32L152给SD卡创建文本,但是数据无法写入成功

2019-03-23 16:50发布

本帖最后由 y909334873 于 2016-9-5 11:03 编辑

刚开始学习文件系统,给单片机移植了fat文件系统,出现了这样一个问题,我创建了一个文本文件(比如说是y.txt),然后给这个文本文件写入一段文本,通过电脑查看SD卡里的内容,显示这个文本文件(y.txt)显示还是0字节;
2.png
同时连接是会出现修复U盘的提示。也就是说往文本文件里写数据没有成功。
但是如果我通过电脑给这个文本文件写一句话,保存后是这样的
1.png
接下来我在通过单片机给这个文件进行写数据,那么我写的数据量不能超过这个文本文件的13字节,不超过的部分,可以通过电脑正常显示,也可以通过单片机读文件操作。超过的部分通过电脑查看,或者单片机读文件操作读是不成功看不到的

此帖出自小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
20条回答
y909334873
2019-03-25 10:06
  1. void Test_f_write(void)//写数据到文件,如果没有此文件则创建文件
  2. {
  3.     FATFS fs;            // Work area (file system object) for logical drive
  4.     FRESULT res;         // FatFs function common result code
  5.         FIL Make_file;
  6.     char file_name[20]="jj.txt";
  7.     char Storage_buffer[] ="1234567890";       
  8.         UINT bw;
  9.         //检测磁盘是否插好
  10.         if( disk_detect_OK()==FALSE ) return;
  11.          logout(" inaert_ok:>");
  12.     // Register a work area for logical drive 0
  13.     f_mount(0, &fs);

  14.         logout(" Make file Name:>");
  15. //        USART_Scanf_Name(file_name);//通过串口输入源文件路径名/dir/file.txt或者0:dir/file.txt或者0:/dir/file.txt

  16.    res = f_open(&Make_file, file_name,  FA_OPEN_ALWAYS | FA_WRITE); //可写方式打开 没有文件则创建
  17.          logout(" open_ok:>");
  18.          die(res);
  19.    res = f_lseek(&Make_file, bsize); //指针移到文件最后  
  20.    logout(" seek_ok:>");
  21.          die(res);
  22.          res = f_write(&Make_file, Storage_buffer, (sizeof (Storage_buffer))-1 , &bw); //每次需要写入的数据字节数,去掉最后的所以-1  
  23.    logout(" write_ok:>");
  24.          die(res);
  25.          //logout("文件大小=%d字节 ",Make_file.fsize);
  26.         res = f_lseek(&Make_file, Make_file.fsize); //指针移到文件最后  
  27.         //logout("文件大小=%d字节 ",Make_file.fsize);
  28.         f_close(&Make_file);//关闭文件
  29.         logout(" close_ok:>");
  30.   //logout("文件大小=%d字节 ",Make_file.fsize);
  31.         logout(" 写文件测试OK! ");

  32.         // Unregister a work area before discard it
  33.    f_mount(0, NULL);
  34. }






  35. //其中调用的f_wriite函数如下
  36. FRESULT f_write (
  37.     FIL *fp,            /* Pointer to the file object */
  38.     const void *buff,    /* Pointer to the data to be written */
  39.     UINT btw,            /* Number of bytes to write */
  40.     UINT *bw            /* Pointer to number of bytes written */
  41. )
  42. {
  43.     FRESULT res;
  44.     DWORD clst, sect;
  45.     UINT wcnt, cc;
  46.     const BYTE *wbuff = buff;
  47.    
  48.     BYTE csect;

  49.   bsize+=btw;
  50.     *bw = 0;    /* Initialize byte counter */
  51.     logout("1111111111111111111 ");
  52.     res = validate(fp->fs, fp->id);            /* Check validity */
  53.     fp->fsize =fp->fsize +btw;
  54.     logout("wenjian=%d ",fp->fsize);
  55.     //f_sync (fp);
  56.     if (res != FR_OK) LEAVE_FF(fp->fs, res);
  57.     if (fp->flag & FA__ERROR)                /* Aborted file? */
  58.         LEAVE_FF(fp->fs, FR_INT_ERR);
  59.     logout("22222222222222222222222222 ");
  60.     if (!(fp->flag & FA_WRITE))                /* Check access mode */
  61.         LEAVE_FF(fp->fs, FR_DENIED);
  62.     if ((DWORD)(fp->fsize + btw) < fp->fsize) btw = 0;    /* File size cannot reach 4GB */
  63.    logout("333333333333333333333333 ");
  64.     for ( ;  btw;                            /* Repeat until all data written */
  65.         wbuff += wcnt, fp->fptr += wcnt, *bw += wcnt, btw -= wcnt) {
  66.             logout("44444444444444444 ");
  67.         if ((fp->fptr % SS(fp->fs)) == 0)
  68.         {    /* On the sector boundary? */
  69.             
  70.             csect = (BYTE)(fp->fptr / SS(fp->fs) & (fp->fs->csize - 1));    /* Sector offset in the cluster */
  71.             if (!csect)
  72.                 {
  73.                     /* On the cluster boundary? */
  74.                 if (fp->fptr == 0) {        /* On the top of the file? */
  75.                     clst = fp->sclust;        /* Follow from the origin */
  76.                     if (clst == 0)            /* When no cluster is allocated, */
  77.                         fp->sclust = clst = create_chain(fp->fs, 0);    /* Create a new cluster chain */
  78.                 } else {                    /* Middle or end of the file */
  79. #if _USE_FASTSEEK
  80.                     if (fp->cltbl)
  81.                         clst = clmt_clust(fp, fp->fptr);    /* Get cluster# from the CLMT */
  82.                     else
  83. #endif
  84.                     
  85.                         clst = create_chain(fp->fs, fp->clust);    /* Follow or stretch cluster chain on the FAT */
  86.                 }
  87.                 if (clst == 0) break;        /* Could not allocate a new cluster (disk full) */
  88.                 if (clst == 1) ABORT(fp->fs, FR_INT_ERR);   
  89.                         
  90.                 if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
  91.                 fp->clust = clst;            /* Update current cluster */
  92.                 //fp->pad1 = 0;
  93.             }
  94.             
  95. #if _FS_TINY
  96.             if (fp->fs->winsect == fp->dsect && move_window(fp->fs, 0))    /* Write-back sector cache */
  97.                 ABORT(fp->fs, FR_DISK_ERR);
  98. #else
  99.             if (fp->flag & FA__DIRTY) {        /* Write-back sector cache */
  100.                 if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)
  101.                     ABORT(fp->fs, FR_DISK_ERR);
  102.                 fp->flag &= ~FA__DIRTY;
  103.             }
  104. #endif
  105.             sect = clust2sect(fp->fs, fp->clust);    /* Get current sector */
  106.             if (!sect) ABORT(fp->fs, FR_INT_ERR);
  107.             sect += csect;
  108.             cc = btw / SS(fp->fs);            /* When remaining bytes >= sector size, */
  109.             if (cc) {                        /* Write maximum contiguous sectors directly */
  110.                 if (csect + cc > fp->fs->csize)    /* Clip at cluster boundary */
  111.                     cc = fp->fs->csize - csect;
  112.                 if (disk_write(fp->fs->drv, wbuff, sect, (BYTE)cc) != RES_OK)
  113.                     ABORT(fp->fs, FR_DISK_ERR);
  114. #if _FS_TINY
  115.                 if (fp->fs->winsect - sect < cc) {    /* Refill sector cache if it gets invalidated by the direct write */
  116.                     mem_cpy(fp->fs->win, wbuff + ((fp->fs->winsect - sect) * SS(fp->fs)), SS(fp->fs));
  117.                     //logout("tiny");
  118.                     fp->fs->wflag = 0;
  119.                 }
  120. #else
  121.                 if (fp->dsect - sect < cc) { /* Refill sector cache if it gets invalidated by the direct write */
  122.                     mem_cpy(fp->buf, wbuff + ((fp->dsect - sect) * SS(fp->fs)), SS(fp->fs));
  123.                     //logout("no tiny");
  124.                     fp->flag &= ~FA__DIRTY;
  125.                 }
  126. #endif
  127.                 wcnt = SS(fp->fs) * cc;        /* Number of bytes transferred */
  128.                 continue;
  129.             }
  130. #if _FS_TINY
  131.             if (fp->fptr >= fp->fsize) {    /* Avoid silly cache filling at growing edge */
  132.                 if (move_window(fp->fs, 0)) ABORT(fp->fs, FR_DISK_ERR);
  133.                 fp->fs->winsect = sect;
  134.             }
  135. #else
  136.             if (fp->dsect != sect) {        /* Fill sector cache with file data */
  137.                 if (fp->fptr < fp->fsize &&
  138.                     disk_read(fp->fs->drv, fp->buf, sect, 1) != RES_OK)
  139.                         ABORT(fp->fs, FR_DISK_ERR);
  140.             }
  141. #endif
  142.             fp->dsect = sect;
  143.         }
  144.         logout("555555555555555555 ");
  145.         wcnt = SS(fp->fs) - (fp->fptr % SS(fp->fs));/* Put partial sector into file I/O buffer */
  146.         if (wcnt > btw) wcnt = btw;
  147. #if _FS_TINY
  148.         if (move_window(fp->fs, fp->dsect))    /* Move sector window */
  149.             ABORT(fp->fs, FR_DISK_ERR);
  150.         mem_cpy(&fp->fs->win[fp->fptr % SS(fp->fs)], wbuff, wcnt);    /* Fit partial sector */
  151.         fp->fs->wflag = 1;
  152. #else
  153.         mem_cpy(&fp->buf[fp->fptr % SS(fp->fs)], wbuff, wcnt);    /* Fit partial sector */
  154.         fp->flag |= FA__DIRTY;
  155. #endif
  156.     }

  157.     if (fp->fptr > fp->fsize)
  158.     fp->fsize = fp->fptr;    /* Update file size if needed */
  159.     fp->flag |= FA__WRITTEN;                        /* Set file change flag */

  160.     LEAVE_FF(fp->fs, FR_OK);
  161. }
  162. //感觉涉及的函数有点多,不知道该贴些啥。。。
复制代码

一周热门 更多>