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

📄 fat32.c

📁 基于AT89C51SND1的MP3的程序设计(包括播放mp3和录音功能)
💻 C
📖 第 1 页 / 共 5 页
字号:
  for (i = (fat_dir_current_offs / SECTOR_SIZE / fat_cluster_size) + 1; i != 0; i--)
  {
    if (dclusters[fat_dchain_index].number == fat_dchain_nb_clust)
    { /* new fragment */
      fat_dchain_index++;
      fat_dchain_nb_clust = 1;
    }
    else
    { /* no new fragment */
      fat_dchain_nb_clust++;
    }
  }
  i = fat_dir_current_offs / SECTOR_SIZE;
  fat_dir_current_sect = (((Uint32)(dclusters[fat_dchain_index].cluster + fat_dchain_nb_clust - 1) * 
                                    fat_cluster_size)
                         + fat_ptr_data + (i % fat_cluster_size));

  if ((fat_dclust_byte_count == 0)/* && (fat_dchain_index == 0)*/)     /* If we are at the beginning of a directory */
    if (fat_dchain_nb_clust == 1)
      fat_dchain_nb_clust = 0;
}


/*F**************************************************************************
* NAME: fat_clear_dir_info
*----------------------------------------------------------------------------
* PARAMS:
*   
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
*   Reset directory chain cluster value
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*   
*----------------------------------------------------------------------------
* REQUIREMENTS:
*   
*****************************************************************************/
void fat_clear_dir_info(void)
{
  fat_dchain_nb_clust = 0;    /* position of a cluster for selected chain idx */
  fat_dchain_index = 0;       /* chain index position */  
  fat_dclust_byte_count = 0;  /* byte position inside a directory cluster */
  fat_dir_current_offs = 0;   /* general offset from the start of a directory */
}

/*F**************************************************************************
* NAME: fat_up_down_load_sector
*----------------------------------------------------------------------------
* PARAMS:
*   - sector address to load/download   
*   - bit to indicate if it's a download or an upload
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
*   Download or upload a sector of 512b
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*   
*----------------------------------------------------------------------------
* REQUIREMENTS:
*   
*****************************************************************************/
void fat_up_down_load_sector(Uint32 sector, bit up_down)
{
  if (up_down == UPLOAD)
  {
    Hard_read_open(sector);     
    Hard_load_sector();
    Hard_read_close();
  }
  else
  {
    Hard_write_open(sector);
    Hard_download_sector();
    Hard_write_close();
  }
}

/*F**************************************************************************
* NAME: fat_get_dir_file_list
*----------------------------------------------------------------------------
* PARAMS:
*   id: file extension to select
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
*   Give information about the directory :
*     - total number of entries
*     - number of deleted entries
*     - number of filtered entries (filter is done by checking id value)
*     - total number of clusters used by the directory
*----------------------------------------------------------------------------
* EXAMPLE:
*
*----------------------------------------------------------------------------
* NOTE:
*   
*----------------------------------------------------------------------------
* REQUIREMENTS:
*   
*****************************************************************************/
void fat_get_dir_file_list (Byte id)
{
Byte   i;
bit   exit = FALSE;

  current_ext = id;                         /* save current extension */
  fat_last_dir_cluster_full = FALSE;        /* reset flag for full cluster */
  fat_no_entries_free = FALSE;              /* reset flag for presence of free entries inside a directory cluster */
  fat_dir_list_last = 0;                    /* last filtered directory entry */
  fat_dir_start_sect = fat_dir_current_sect;/* set fat_dir_start_sect (use by fat_dseek()) */
  fat_nb_deleted_entries = 0;               /* reset the number of entries that is marker as deleted */
  fat_nb_total_entries = 0;                 /* reset the number of total entries in the directory */
  fat_total_clusters = 0;                   /* reset the total number of clusters for a directory */

  for (i = 0; i <= fat_last_dclust_index; i++)
  {
    fat_total_clusters += dclusters[i].number;  /* calculate the total numbers of clusters */
  }

  fat_clear_dir_info();                         /* clear directory variable */
  
  Hard_read_open(fat_dir_start_sect);
  do
  {
    for (i = 0; i < DIR_SIZE; i++)
      gl_buffer[i] = fat_dgetc();

    if (gl_buffer[0] != FILE_NOT_EXIST)
    {
      if (gl_buffer[0] != FILE_DELETED)
      {
        fat_nb_total_entries++;                             /* increase total number of entries   */
        while (gl_buffer[11] == ATTR_LFN_ENTRY)
        {
          for (i = 0; i < DIR_SIZE; i++)
            gl_buffer[i] = fat_dgetc();
          fat_nb_total_entries++;                           /* increase total number of entries   */
        }
        
        fat_cache.current.attributes = gl_buffer[11];       /* filter on the file type */
        ext[0] = gl_buffer[8];
        ext[1] = gl_buffer[9];
        ext[2] = gl_buffer[10];
        if ((fat_check_ext() & current_ext) != FILE_XXX)    /* Check if file type is OK */
        {
          fat_dir_list_last++;          /* increase the number of selected entries */
        }
      }
      else
      {
        fat_nb_deleted_entries++;       /* increase number of deleted entries */
        fat_nb_total_entries++;         /* increase total number of entries   */
      }
    }
    else
    {
      exit = TRUE;                      /* "not exist" entries mark the end of directory */
    }

    if  ((((Byte*)&fat_dclust_byte_count)[1] == 0x00) &&
        ((((Byte*)&fat_dclust_byte_count)[0] & fat_cluster_mask) == 0x00) && 
        (fat_last_dclust_index == fat_dchain_index))
    {
      exit = TRUE;                          /* if the last entries of the last cluster is reached */
      fat_last_dir_cluster_full = TRUE;     /* then exit and set this flag                        */
    }
  }
  while (!exit);

  Hard_read_close();                        /* close disk */
  if (fat_last_dir_cluster_full)            /* if the last directory cluster is full        */
  {
    if (!fat_nb_deleted_entries)            /* and if there is no deleted entries detected  */
    {
      fat_no_entries_free = TRUE;           /* set this flag that there is no free entries  */
    }
  }

  fat_dir_current_sect = fat_dir_start_sect;
  fat_clear_dir_info();                     /* reset directory variable */

}


/*F**************************************************************************
* NAME: fat_fetch_file_info
*----------------------------------------------------------------------------
* PARAMS:
*   entry: directory entry structure
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
*   Get information about a directory or file entry
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*   
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void fat_fetch_file_info (fat_st_dir_entry xdata *entry, bit direction)
{
Uint16 i;
Uint16 j;
bit entry_found = FALSE;

  /* clear the name buffer */
  for (i = MAX_FILENAME_LEN; i != 0; lfn_name[--i] = '\0');

  if (direction == FETCH_PREV)
  {
    /* fetch previous file */
    fat_current_start_entry_position--;                             /* seek to the previous entry  */
    fat_dir_current_offs = (fat_current_start_entry_position) * DIR_SIZE; /* update fat_dir_current_offs */
    fat_calc_cluster();                                             /* update directory position variable */
    j = (fat_current_start_entry_position % 16) * DIR_SIZE;         /* seek to the previous entry */
    fat_up_down_load_sector(fat_dir_current_sect, UPLOAD);          /* load the directory sector */
    do
    {
      if (fat_buf_sector[j] != FILE_DELETED)                    /* if it's not a deleted entries */
      {
        entry->attributes = fat_buf_sector[j + 11];
        ext[0]= fat_buf_sector[j + 8];
        ext[1]= fat_buf_sector[j + 9];
        ext[2]= fat_buf_sector[j + 10];
        if ((fat_check_ext() & current_ext) != FILE_XXX)            /* check id of entry */ 
        {
          entry_found = TRUE;
        }
      }

      if (!entry_found )                   /* if id is not correct   */
      {
        fat_dir_current_offs -= DIR_SIZE;                           /* seek to previous entry */
        if (j == 0)                                                 /* sector change          */
        {
          fat_calc_cluster();
          fat_up_down_load_sector(fat_dir_current_sect, UPLOAD);    /* load the directory sector */
          j = SECTOR_SIZE - DIR_SIZE;
        }
        else
        {
          j -= DIR_SIZE;
        }
        fat_current_start_entry_position--;
      }
    }
    while (!entry_found);

    if (fat_current_start_entry_position != 0)
    {
      /* we find an entry. Now seek to previous entry for the process LFN */
      fat_dir_current_offs -= DIR_SIZE;                           /* seek to previous entry */
      if (j == 0)                                                 /* sector change          */
      {
        fat_calc_cluster();
        fat_up_down_load_sector(fat_dir_current_sect, UPLOAD);    /* load the directory sector */
        j = SECTOR_SIZE - DIR_SIZE;
      }
      else
      {
        j -= DIR_SIZE;
      }
      fat_current_start_entry_position--;
      if (fat_buf_sector[j + 11] != ATTR_LFN_ENTRY)
      {
        fat_dir_current_offs += DIR_SIZE;
        fat_current_start_entry_position++;
      }
      else
      {
        while ((fat_buf_sector[j + 11] == ATTR_LFN_ENTRY) && (fat_current_start_entry_position != 0))
        {
          fat_dir_current_offs -= DIR_SIZE;
          fat_current_start_entry_position--;
          if (j == 0)                                                 /* sector change          */
          {
            fat_calc_cluster();
            fat_up_down_load_sector(fat_dir_current_sect, UPLOAD);    /* load the directory sector */
            j = SECTOR_SIZE - DIR_SIZE;
          }
          else
          {
            j -= DIR_SIZE;
          }
        }
    
        if (fat_current_start_entry_position)
        {
          fat_dir_current_offs += DIR_SIZE;
          fat_current_start_entry_position++;
        }
      }

    }

    fat_current_end_entry_position = fat_current_start_entry_position;
    fat_dseek(0);
  }
  else      /* fetch next file */
  {
    fat_dseek((fat_current_end_entry_position - fat_current_start_entry_position) << 5);
  }

  do
  {
    fat_current_start_entry_position = fat_current_end_entry_position;
    for (i = 0; i < DIR_SIZE; i++)
      gl_buffer[i] = fat_dgetc();

    while (gl_buffer[0] == FILE_DELETED)                          /* find a non deleted file */
    {
      fat_current_start_entry_position++;
      fat_dir_current_offs += DIR_SIZE;
      for (i = 0; i < DIR_SIZE; i++)
        gl_buffer[i] = fat_dgetc();
    }

    fat_current_end_entry_position = fat_current_start_entry_position;

    if (gl_buffer[11] == ATTR_LFN_ENTRY)
    {
      for (j = gl_buffer[0] & 0x0F; j != 0; j--)                  /* computes lfn name */
      {
        for (i = 0; i < 5; i++)
          lfn_name[i + 13 * (j - 1)] = gl_buffer[2 * i + 1];
        for (i = 0; i < 6; i++)
          lfn_name[i + 5 + 13 * (j - 1)] = gl_buffer[2 * i + 14];
        for (i = 0; i < 2; i++)
          lfn_name[i + 11 + 13 * (j - 1)] = gl_buffer[2 * i + 28];
  
        for (i = 0; i < DIR_SIZE; i++)                            /* read the directory entry */
          gl_buffer[i] = fat_dgetc();
        fat_current_end_entry_position++;
      }
      i = 0;
      while (lfn_name[i] != '\0')                                 /* search for the end of the string */
      { 
        i++;
      }
      if (i <= 14)                                                /* append spaces for display reason (no scrolling) */
      { 
        while (i != 14)                                 
        {
          lfn_name[i++] = ' ';
        }
      }
      else
      { /* append beginning of name to ease scrolling display */
        lfn_name[i++] = ' ';

⌨️ 快捷键说明

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