⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tff.lst

📁 SdCard_V2.1TinyFatFs.rar是单片机实现SD卡功能的FAT文件系统
💻 LST
📖 第 1 页 / 共 5 页
字号:
 467   2                      if (!move_window(scan->sect)) return NULL;
 468   2                      dptr = &(fs->win[(scan->index & 15) * 32]);             /* Pointer to the directory entry */
 469   2                      c = *dptr;
 470   2                      if ((c == 0) || (c == 0xE5)) return dptr;               /* Found an empty entry! */
 471   2              } while (next_dir_entry(scan));                                         /* Next directory pointer */
 472   1              /* Reached to end of the directory table */
 473   1      
 474   1              /* Abort when static table or could not stretch dynamic table */
 475   1              if ((!clust) || !(clust = create_chain(scan->clust))) return NULL;
 476   1              if (!move_window(0)) return 0;
 477   1      
 478   1              fs->winsect = sector = clust2sect(clust);                       /* Cleanup the expanded table */
 479   1              memset(fs->win, 0, 512);
 480   1              for (n = fs->sects_clust; n; n--) {
 481   2                      if (disk_write(fs->win, sector, 1) != RES_OK) return NULL;
 482   2                      sector++;
 483   2              }
 484   1              fs->winflag = 1;
 485   1              return fs->win;
 486   1      }
 487          #endif /* _FS_READONLY */
 488          
C51 COMPILER V7.02b   TFF                                                                  03/24/2008 15:52:29 PAGE 9   

 489          
 490          
 491          /*-----------------------------------------*/
 492          /* Make Sure that the File System is Valid */
 493          
 494          static
 495          FRESULT check_mounted ()
 496          {
 497   1              FATFS *fs = FatFs;
 498   1      
 499   1      
 500   1              if (!fs) return FR_NOT_ENABLED;         /* Has the FatFs been enabled? */
 501   1      
 502   1              if (disk_status() & STA_NOINIT) {       /* The drive has not been initialized */
 503   2                      if (fs->files)                                  /* Drive was uninitialized with any file left opend */
 504   2                              return FR_INCORRECT_DISK_CHANGE;
 505   2                      else
 506   2                              return f_mountdrv();            /* Initialize file system and return resulut */
 507   2              } else {                                                        /* The drive has been initialized */
 508   2                      if (!fs->fs_type)                               /* But the file system has not been initialized */
 509   2                              return f_mountdrv();            /* Initialize file system and return resulut */
 510   2              }
 511   1              return FR_OK;                                           /* File system is valid */
 512   1      }
 513          
 514          
 515          
 516          
 517          
 518          /*--------------------------------------------------------------------------*/
 519          /* Public Funciotns                                                         */
 520          /*--------------------------------------------------------------------------*/
 521          
 522          
 523          /*--------------------------------------------------------*/
 524          /* Load File System Information and Initialize the Module */
 525          
 526          FRESULT f_mountdrv ()
 527          {
 528   1              BYTE fat;
 529   1              DWORD sect, fatend, maxsect;
 530   1              FATFS *fs = &fatfsentity;
 531   1              FatFs = fs;
 532   1      
 533   1              if (!fs) return FR_NOT_ENABLED;
 534   1      
 535   1              /* Initialize file system object */
 536   1              memset(fs, 0, sizeof(FATFS));
 537   1      
 538   1              /* Initialize disk drive */
 539   1              //if (disk_initialize() & STA_NOINIT)   return FR_NOT_READY;
 540   1      
 541   1              /* Search FAT partition */
 542   1              fat = check_fs(sect = 0);               /* Check sector 0 as an SFD format */
 543   1              if (!fat) {                                             /* Not a FAT boot record, it will be an FDISK format */
 544   2                      /* Check first pri-partition listed in the partition table */
 545   2                      if (fs->win[0x1C2]) {                                   /* Is a partition existing? */
 546   3                              sect = LD_DWORD(&(fs->win[0x1C6]));     /* Partition offset in LBA */
 547   3                              fat = check_fs(sect);                           /* Check the partition */
 548   3                      }
 549   2              }
 550   1              if (!fat) return FR_NO_FILESYSTEM;      /* No FAT patition */
C51 COMPILER V7.02b   TFF                                                                  03/24/2008 15:52:29 PAGE 10  

 551   1      
 552   1              /* Initialize file system object */
 553   1              fs->fs_type = fat;                                                              /* FAT type */
 554   1              fs->sects_fat = LD_WORD(&(fs->win[0x16]));              /* Sectors per FAT */
 555   1              fs->sects_clust = fs->win[0x0D];                                /* Sectors per cluster */
 556   1              fs->n_fats = fs->win[0x10];                                             /* Number of FAT copies */
 557   1              fs->fatbase = sect + LD_WORD(&(fs->win[0x0E])); /* FAT start sector (physical) */
 558   1              fs->n_rootdir = LD_WORD(&(fs->win[0x11]));              /* Nmuber of root directory entries */
 559   1      
 560   1              fatend = fs->sects_fat * fs->n_fats + fs->fatbase;
 561   1              fs->dirbase = fatend;                                                   /* Directory start sector (physical) */
 562   1              fs->database = fs->n_rootdir / 16 + fatend;             /* Data start sector (physical) */
 563   1      
 564   1              maxsect = LD_DWORD(&(fs->win[0x20]));                   /* Calculate maximum cluster number */
 565   1              if (!maxsect) maxsect = LD_WORD(&(fs->win[0x13]));
 566   1              fs->max_clust = (maxsect - fs->database + sect) / fs->sects_clust + 2;
 567   1      
 568   1              fs->files = 0;
 569   1              return FR_OK;
 570   1      }
 571          
 572          
 573          /*-----------------------------*/
 574          /* Get Number of Free Clusters */
 575          #if 0
              FRESULT f_getfree (
                      DWORD *nclust           /* Pointer to the double word to return number of free clusters */
              )
              {
                      DWORD n, sect;
                      WORD clust;
                      BYTE f, *p;
                      FRESULT res;
                      FATFS *fs = FatFs;
              
              
                      if ((res = check_mounted()) != FR_OK) return res;
              
                      /* Count number of free clusters */
                      n = 0;
                      if (fs->fs_type == FS_FAT12) {
                              clust = 2;
                              do {
                                      if (get_cluster(clust) == 0) n++;
                              } while (++clust < fs->max_clust);
                      } else {
                              clust = fs->max_clust;
                              sect = fs->fatbase;
                              f = 0; p = 0;
                              do {
                                      if (!f) {
                                              if (!move_window(sect++)) return FR_RW_ERROR;
                                              p = fs->win;
                                      }
                                      if (LD_WORD(p) == 0) n++;
                                      p += 2; f += 1;
                              } while (--clust);
                      }
              
                      *nclust = n;
                      return FR_OK;
              }
C51 COMPILER V7.02b   TFF                                                                  03/24/2008 15:52:29 PAGE 11  

              
              #endif
 615          
 616          /*---------------------*/
 617          /* Open or Create File */
 618          
 619          FRESULT f_open (
 620                  FIL *fp,                        /* Pointer to the buffer of new file object to create */
 621                  const char *path,       /* Pointer to the file path */
 622                  BYTE mode                       /* Access mode and file open mode flags */
 623          )
 624          {
 625   1              FRESULT res;
 626   1              BYTE *dir;
 627   1              DIR dirscan;
 628   1              char fn[8+3+1];
 629   1              FATFS *fs = FatFs;
 630   1      
 631   1      
 632   1              if ((res = check_mounted()) != FR_OK) return res;
 633   1      #ifndef _FS_READONLY
 634   1              if ((mode & (FA_WRITE|FA_CREATE_ALWAYS|FA_OPEN_ALWAYS)) && (disk_status() & STA_PROTECT))
 635   1                      return FR_WRITE_PROTECTED;
 636   1      #endif
 637   1      
 638   1              res = trace_path(&dirscan, fn, path, &dir);     /* Trace the file path */
 639   1      
 640   1      #ifndef _FS_READONLY
 641   1              /* Create or Open a File */
 642   1              if (mode & (FA_CREATE_ALWAYS|FA_OPEN_ALWAYS)) {
 643   2                      DWORD dw;
 644   2                      if (res != FR_OK) {             /* No file, create new */
 645   3                              mode |= FA_CREATE_ALWAYS;
 646   3                              //if (res != FR_NO_FILE) return res;
 647   3                              dir = reserve_direntry(&dirscan);       /* Reserve a directory entry */
 648   3                              if (dir == NULL) return FR_DENIED;
 649   3                              memcpy(dir, fn, 8+3);           /* Initialize the new entry */
 650   3                              *(dir+12) = fn[11];
 651   3                              memset(dir+13, 0, 32-13);
 652   3                      } else {                                /* File already exists */
 653   3                              if ((dir == NULL) || (*(dir+11) & (AM_RDO|AM_DIR)))     /* Could not overwrite (R/O or DIR) */
 654   3                                      return FR_DENIED;
 655   3                              if (mode & FA_CREATE_ALWAYS) {  /* Resize it to zero */
 656   4                                      dw = fs->winsect;                       /* Remove the cluster chain */
 657   4                                      if (!remove_chain(LD_WORD(dir+26)) || !move_window(dw) )
 658   4                                              return FR_RW_ERROR;
 659   4                                      ST_WORD(dir+26, 0);                             /* cluster = 0 */
 660   4                                      ST_DWORD(dir+28, 0);                    /* size = 0 */
 661   4                              }
 662   3                      }
 663   2                      if (mode & FA_CREATE_ALWAYS) {
 664   3                              *(dir+11) = AM_ARC;
 665   3                      //      dw = get_fattime();
 666   3                      //      ST_DWORD(dir+14, dw);   /* Created time */
 667   3                      //      ST_DWORD(dir+22, dw);   /* Updated time */
 668   3                              fs->winflag = 1;
 669   3                      }
 670   2              }
 671   1              /* Open a File */
 672   1              else {
 673   2      #endif /* _FS_READONLY */
 674   2                      if (res != FR_OK) return res;           /* Trace failed */
C51 COMPILER V7.02b   TFF                                                                  03/24/2008 15:52:29 PAGE 12  

 675   2                      if ((dir == NULL) || (*(dir+11) & AM_DIR))      /* It is a directory */
 676   2                              return FR_NO_FILE;
 677   2      #ifndef _FS_READONLY
 678   2                      if ((mode & FA_WRITE) && (*(dir+11) & AM_RDO)) /* R/O violation */
 679   2                              return FR_DENIED;
 680   2              }
 681   1      #endif
 682   1      
 683   1      #ifdef _FS_READONLY
                      fp->flag = mode & FA_READ;
              #else
 686   1              fp->flag = mode & (FA_WRITE|FA_READ);
 687   1              fp->dir_sect = fs->winsect;                     /* Pointer to the directory entry */
 688   1              fp->dir_ptr = dir;
 689   1      #endif
 690   1              fp->org_clust = LD_WORD(dir+26);        /* File start cluster */
 691   1              fp->fsize = LD_DWORD(dir+28);           /* File size */
 692   1              fp->fptr = 0;                                           /* File ptr */
 693   1              fp->sect_clust = 1;                                     /* Sector counter */
 694   1              fs->files++;
 695   1              return FR_OK;
 696   1      }
 697          
 698          
 699          
 700          /*-----------*/
 701          /* Read File */
 702          
 703          FRESULT f_read (

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -