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

📄 fat32.c

📁 基于AT89C51SND1的MP3的程序设计(包括播放mp3和录音功能)
💻 C
📖 第 1 页 / 共 5 页
字号:

  temp_byte = fat_dgetc();          
  index = 0;
  while ((temp_byte != FILE_NOT_EXIST) && (temp_byte != FILE_DELETED))
  {
    for (i = DIR_SIZE - 1; i != 0; i--)
      fat_dgetc();
    temp_byte = fat_dgetc();
    index++;
  }
  if (temp_byte == FILE_DELETED)
  {
    fat_nb_deleted_entries--;
    if ((!fat_nb_deleted_entries) && (fat_last_dir_cluster_full == TRUE))
    {
      fat_no_entries_free = TRUE;
    }
  }
  else                              /* no deleted file entry */
  {
    fat_nb_total_entries++;         /* It's a new entry */
    if ((fat_nb_total_entries / 16) == fat_total_clusters * fat_cluster_size)
    {
      fat_last_dir_cluster_full = TRUE;
      fat_no_entries_free = TRUE;     /* Next time, add a cluster to the directory cluster chain */
    }
  }

  Hard_read_close();

  fat_get_free_clusters(1);
  cluster = fclusters[0].cluster + 2;
  /* Construct the entry */
  gl_buffer[0] = file_name[0];
  gl_buffer[1] = file_name[1];
  gl_buffer[2] = file_name[2];
  gl_buffer[3] = file_name[3];
  gl_buffer[4] = file_name[4];
  gl_buffer[5] = file_name[5];
  gl_buffer[6] = file_name[6];
  gl_buffer[7] = file_name[7];
  gl_buffer[8] = file_name[8];
  gl_buffer[9] = file_name[9];
  gl_buffer[10] = file_name[10];;
  gl_buffer[11] = attribute;                         /* Attribute : archive */
  gl_buffer[12] = 0x00;                         /* Millisecond stamp at time creation */
  gl_buffer[13] = 0x00;                         /* Time */
  gl_buffer[14] = 0x00;
  gl_buffer[15] = 0x00;                         /* Date */
  gl_buffer[16] = 0x00;
  gl_buffer[18] = 0x00;                         /* Last access date */
  gl_buffer[19] = 0x00;
  gl_buffer[20] = ((Byte*)&cluster)[1];         /* High word First cluster number*/
  gl_buffer[21] = ((Byte*)&cluster)[0];
  gl_buffer[22] = 0x00;                         /* Time of last write */
  gl_buffer[23] = 0x00;
  gl_buffer[24] = 0x00;                         /* Date of last write */
  gl_buffer[25] = 0x00;
  gl_buffer[26] = ((Byte*)&cluster)[3];         /* Low word first cluster number */
  gl_buffer[27] = ((Byte*)&cluster)[2];
  gl_buffer[28] = 0x00;                         /* Size */
  gl_buffer[29] = 0x00;
  gl_buffer[30] = 0x00;
  gl_buffer[31] = 0x00;

  fat_dir_current_sect += ((fat_dclust_byte_count % (fat_cluster_size * SECTOR_SIZE))/512);

  fat_up_down_load_sector(fat_dir_current_sect, 0);
  j = (index % 16) * DIR_SIZE ;                 /* Position of entry in the sector */
  for (i = 0; i < DIR_SIZE; i++)
    fat_buf_sector[j++] = gl_buffer[i];

  fat_up_down_load_sector(fat_dir_current_sect, DOWNLOAD);

  fat_dir_list_last++;                          /* Increase max file number */
  fat_dir_list_index = 0;
  
  fat_current_start_entry_position = index;
  fat_current_end_entry_position = index;
  fat_clear_dir_info();
  fat_dir_current_offs = index * 32;
  fat_calc_cluster();
  fat_fetch_file_info(&fat_cache.current, FETCH_NEXT);
  /* open file in write mode */
  fat_fclust_byte_count = 0;                                        /* byte 0 of cluster */
  fat_file_size.l = 0;
  fat_cache.current.size.l = 0;
  fat_fchain_index = 0;                                             /* reset the allocation list variable */
  fat_fchain_nb_clust = 0;                                          /* start on first contiguous cl */
  fat_open_mode = WRITE;
  return Hard_write_open(fat_ptr_data + ((Uint32)(fclusters[0].cluster) 
                                         * fat_cluster_size));
}


/*F**************************************************************************
* NAME: fat_refresh_dir_file_info
*----------------------------------------------------------------------------
* PARAMS:
*    
* return:
*   
*----------------------------------------------------------------------------
* PURPOSE:
*   Reconstruct the file directory list and seek to the file pointed by
*   fat_dir_list_index
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*   
*----------------------------------------------------------------------------
* REQUIREMENTS:
*   
*****************************************************************************/
void fat_refresh_dir_file_info (Byte id)
{
  fat_dir_current_sect = (((Uint32)(dclusters[0].cluster)) * fat_cluster_size)
                           + fat_ptr_data;

  fat_get_dir_file_list(id); /* Refresh file list                          */
  fat_seek_entry_record();
}

/*F**************************************************************************
* NAME: fat_clear_fat
*----------------------------------------------------------------------------
* PARAMS:
*    
* return:
*----------------------------------------------------------------------------
* PURPOSE:
*   Reset FAT clusters value
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*   
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void fat_clear_fat (Uint32 start_cluster)
{
Uint16 i;
Uint16 sector_number;
Uint32 cluster;

  /* init the starting cluster value */
  cluster = start_cluster;
  /* Start at first chain cluster */
  sector_number = cluster / 128;
  /* Bufferize fat sector */
  fat_up_down_load_sector(fat_ptr_fats + sector_number, UPLOAD);
  do 
  {
    if ((cluster / 128) != sector_number)
    {
      fat_update_fat_sector(sector_number);
      sector_number =  (Uint32)(cluster / 128);
      fat_up_down_load_sector(fat_ptr_fats + sector_number, UPLOAD);
    }
    i = (cluster * 4) & 0x1FF;
    ((Byte*)&cluster)[3] = fat_buf_sector[i];
    fat_buf_sector[i++] = 0;
    ((Byte*)&cluster)[2] = fat_buf_sector[i];
    fat_buf_sector[i++] = 0;
    ((Byte*)&cluster)[1] = fat_buf_sector[i];
    fat_buf_sector[i++] = 0;
    ((Byte*)&cluster)[0] = fat_buf_sector[i];
    fat_buf_sector[i] = 0;
  }
  while (cluster != 0x0FFFFFFF);
  fat_update_fat_sector(sector_number);
}


/*F**************************************************************************
* NAME: fat_fdelete
*----------------------------------------------------------------------------
* PARAMS:
*    
* return:
*----------------------------------------------------------------------------
* PURPOSE:
*   Delete a selected file
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*   This function works only on a root directory entry
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
Byte fat_fdelete (void)
{
Uint16 i;

  if (fat_check_ext() != FILE_DIR)
  {
    fat_clear_dir_info();
    fat_dir_current_offs = fat_current_start_entry_position * 32;
    fat_calc_cluster();
    fat_up_down_load_sector(fat_dir_current_sect, UPLOAD);
    i = (fat_current_start_entry_position % 16) * 32 ;                /* Position of entry in the sector */

    while (fat_buf_sector[i+11] == ATTR_LFN_ENTRY)
    {
      fat_buf_sector[i] = FILE_DELETED;               /* mark file as deleted */
      i += 32;
      fat_dclust_byte_count += 32;
  
      if (i == SECTOR_SIZE)
      {
        fat_up_down_load_sector(fat_dir_current_sect, DOWNLOAD);
        if  ((((Byte*)&fat_dclust_byte_count)[1] == 0x00) &&                  /* check if we are at the end of a cluster */
            ((((Byte*)&fat_dclust_byte_count)[0] & fat_cluster_mask) == 0x00))
        {
          /* extract if necessary the next cluster from the allocation list */
          if (dclusters[fat_dchain_index].number == fat_dchain_nb_clust)
          { /* new fragment */
            fat_dchain_index++;
            fat_dchain_nb_clust = 1;
            fat_dir_current_sect = (fat_ptr_data + ((Uint32)(dclusters[fat_dchain_index].cluster) * fat_cluster_size));
          }
          else
          {
            fat_dchain_nb_clust++;                          /* one more cluster read */
            fat_dir_current_sect++;                         /* Contiguous cluster    */
          }
        }
        else
        {
          fat_dir_current_sect++;
        }
        fat_up_down_load_sector(fat_dir_current_sect, UPLOAD);
        i = 0;
      }
  
    }
    fat_buf_sector[i] = FILE_DELETED;
    fat_up_down_load_sector(fat_dir_current_sect, DOWNLOAD);
    fat_clear_fat(fat_cache.current.start_cluster);         /* Clear fat cluster */

    fat_dir_list_last--;
    fat_nb_deleted_entries++;
    fat_last_dir_cluster_full = FALSE;
    fat_no_entries_free = FALSE;

    if ( ((dir_is_root == TRUE) && (fat_dir_list_last == 0)) ||     
         ((dir_is_root == FALSE) && (fat_dir_list_last == 2))      /* . and .. directory */
       )
      return DEL_RET_NO_MORE_FILE;

    if (fat_dir_list_index > fat_dir_list_last )
    {
      fat_dir_list_index = fat_dir_list_last;
    }
    return DEL_RET_OK;
  }
  else
  {
    return DEL_RET_ERROR_DIR;
  }
}


/*F**************************************************************************
* NAME: fat_get_free_clusters
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*   - OK: allocation done
*   - KO: allocation cannot be done : no free cluster
*----------------------------------------------------------------------------
* PURPOSE:
*   Prepare a list of the free clusters:
*     chain[n].cluster contains the starting cluster number of a fragment
*     chain[n].number contains the number of contiguous clusters in fragment
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*   Free cluster list is limited by the nb_frag parameter.
*   If memory is too much fragmented, created file may be limited in size.
*   Last list item always has single cluster
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit fat_get_free_clusters (bit init)
{
#define NB_MAX_SECTOR             5
#define NB_MAX_CLUSTER_AT_INIT    3000
#define NB_MAX_CLUSTER            256
static idata Uint16 fat_sector;
static bit end_disk;
bit       cluster_free;
Byte      i;
Uint16    max_cluster;
Uint16    max_sector = 0;
Uint32    cluster;
Uint32    tot_cluster;

  if (init)
  {
    cluster = 0;
    fat_sector = 0;
    max_cluster = NB_MAX_CLUSTER_AT_INIT;
    last_free_cluster = 0;
    fat_last_clust_index = 0;
    end_disk = FALSE;
  }
  else
  {
    if (!end_disk)
    {
      cluster = last_free_cluster + 1;
      fat_sector = cluster / 128;
      max_cluster = NB_MAX_CLUSTER;
    }
    else
    {
      return KO;
    }
  }
  cluster_free = FALSE;
  tot_cluster = fat_count_of_clusters + 2;
  Hard_read_open(fat_ptr_fats + fat_sector);

  for (i = (cluster % 128); i != 0; i--)            /* dummy reads */
  {
    Hard_read_long_big_endian();
  }

  do                                      /* search for first free cluster */
  {
    if (Hard_read_long_big_endian() == 0x00000000)
    {
      cluster_free = TRUE;
    }
    else
    {
      cluster++;
      if (!(cluster & 0x7F) && (!init))
      {
        max_sector++;
      }
    }
  }
  while ((cluster != tot_cluster) && (!cluster_free) && (max_sector < NB_MAX_SECTOR));
   
  if (cluster == fat_count_of_clusters)
  {
    end_disk = TRUE;
  }

  if (!cluster_free)
  {

⌨️ 快捷键说明

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