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

📄 fat.c

📁 MP3播放器详细设计方案
💻 C
📖 第 1 页 / 共 5 页
字号:
    /* End of file marked with 0xFFF */
    if (fat12_parity == 0)
    {
      fat_buf_sector[i++] = 0xFF;
      Fat_check_update(sector_number);
      fat_buf_sector[i] |= 0x0F;
    }
    else
    {
      fat_buf_sector[i++] |= 0xF0;
      Fat_check_update(sector_number);
      fat_buf_sector[i] = 0xFF;
    }
    fat_update_fat_sector(sector_number);
  }

  /* Reconstruct list file */
  temp = fat_dir_list_index;

  fat_dir_current_sect = fat_ptr_rdir;
  fat_dclust_byte_count = 0;
  dir_is_root = TRUE;

  fat_get_dir_file_list(FILE_WAV);                /* create list of entries */
  fat_dir_list_index = temp;
  for (i = 0; i <= fat_dir_list_index; i++)
    fat_dseek(fat_dir_entry_list[i] * DIR_SIZE);

  fat_get_dir_entry(&fat_cache.current);          /* update current file info */
}


/*F**************************************************************************
* NAME: fat_create_entry
*----------------------------------------------------------------------------
* PARAMS:
*   cluster: starting cluster 
*
* return:
*   - OK : new root entry created
*   - KO : no more space in file list
*----------------------------------------------------------------------------
* PURPOSE:
*   Create an entry in root directory
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*   This function creates a file DOS name 8.3 like
*     VOICE000.WAV
*   Index of file is determinated:
*     if deleted wav file like VOICExxx.WAV exist, then index = min index
*     else indice = max index read + 1
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit fat_create_entry (Uint16 cluster, char pdata *file_name)
{
Byte temp_byte;
Uint16 i;
Uint16 j;
xdata Uint16 temp;
xdata Uint16 index;
xdata Uint32 root_sector;

  /* no more place in file liste */
  if (fat_dir_list_last == (MAX_DIRECTORY_FILE - 1))
    return KO;

  /* Find the first free entry in root */
  index = 0;
  if (Hard_read_open(fat_ptr_rdir) == KO) 
    return KO;
  
  temp_byte = Hard_read_byte();
  while ((temp_byte != FILE_NOT_EXIST) && (temp_byte != FILE_DELETED))
  {
    for (i = DIR_SIZE - 1; i != 0; i--)
      Hard_read_byte();
    temp_byte = Hard_read_byte();
    index++;
  }

  Hard_read_close();

  if ((dir_is_root == TRUE) && (index >= NB_ROOT_ENTRY))    /* Maximum entries in root directory */
    return KO;
  /* 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] = 0x00;       /* 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] = 0x00;       /* High word First cluster number : 0x0000 for FAT12/16 */
  gl_buffer[21] = 0x00;
  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] = cluster;    /* Low word first cluster number */
  gl_buffer[27] = cluster >> 8;
  gl_buffer[28] = 0x00;       /* Size */
  gl_buffer[29] = 0x00;
  gl_buffer[30] = 0x00;
  gl_buffer[31] = 0x00;

  root_sector = fat_ptr_rdir + (index / 16);/* root sector address */
  if (Hard_read_open(root_sector) == KO)
    return KO;
  for (i = 0; i < SECTOR_SIZE; i++)
    fat_buf_sector[i] = Hard_read_byte();
  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];
  Hard_read_close();

  if (Hard_write_open(root_sector) == KO)
    return KO;
  for (i = 0; i < SECTOR_SIZE; i++)
    Hard_write_byte(fat_buf_sector[i]);
  Hard_write_close();
  fat_root_entry = index;

  /* Reconstruct file list */
  fat_dir_current_sect = fat_ptr_rdir;
  fat_dclust_byte_count = 0;
  dir_is_root = TRUE;

  fat_get_dir_file_list(FILE_WAV);                /* create list of entries */

  fat_dir_list_index = 0;
  temp = fat_dir_entry_list[0];
  while (temp < index)
  {    
    if ( fat_dseek(fat_dir_entry_list[fat_dir_list_index] * DIR_SIZE) == KO)
      return KO;
    fat_dir_list_index++;                         /* point on next root entry */
    temp += fat_dir_entry_list[fat_dir_list_index];
  }
  
  if (fat_dseek(fat_dir_entry_list[fat_dir_list_index] * DIR_SIZE) == KO)
    return KO;


  return OK;
}


/*F**************************************************************************
* NAME: fat_fopen
*----------------------------------------------------------------------------
* PARAMS:
*   mode: READ:   open file for read
*         WRITE:  open file for write
*
* return:
*   - OK: file opened
*   - KO: file not opened: - file is empty
*                          - low level read error
*----------------------------------------------------------------------------
* PURPOSE:
*   Open the file in read or write mode
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit fat_fopen (bit mode)
{
  if (mode == READ)
  {
    if (fat_cache.current.size.l == 0)
    {
      return KO;                            /* file empty */
    }
    else
    {
      fat_fclust_byte_count = 0;            /* byte 0 of cluster */
  
      /* reset the allocation list variable */
      fat_fchain_index = 0;
      fat_fchain_nb_clust = 0;              /* start on first contiguous cl */
      /* get file allocation list */
      fat_get_clusters(&fclusters, MAX_FILE_FRAGMENT_NUMBER);
  
      /* seek to the beginning of the file */
      fat_open_mode = READ;
      return Hard_read_open(fat_ptr_data + ((Uint32)(fclusters[0].cluster) 
                                               * fat_cluster_size));
    }
  }
  else
  {
    fat_fclust_byte_count = 0;              /* byte 0 of cluster */
    fat_file_size.l = 0;
    fat_current_file_size = 0;
    flag_end_disk_file = FALSE;
    /* reset the allocation list variable */
    fat_fchain_index = 0;
    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_fclose
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
*   Close opened file
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void fat_fclose (void)
{
  if (fat_open_mode == READ)
  {
    Hard_read_close();                      /* close reading */
  }
  else
  {
    Hard_write_close();                     /* close writing */
    fat_update_entry_fat();            /* Update entry and fat */
  }
}


/*F**************************************************************************
* NAME: fat_fcreate
*----------------------------------------------------------------------------
* PARAMS:
*    
* return:
*----------------------------------------------------------------------------
* PURPOSE:
*   Prepare creation of a wav file in the root directory
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*   This function creates first the free cluster chain from fat1 and then
*   creates an entry in root directory
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit fat_fcreate (char pdata *file_name)
{
  /* get free clusters list */
  if ( fat_set_clusters() == KO )
    return KO;

  /* create the entry in root directory */
  if ( fat_create_entry(fclusters[0].cluster + 2, file_name) == KO)
    return KO;
  
  fat_get_dir_entry(&fat_cache.current);          /* update current file info */
  /* parent dir is also root */
  fat_cache.parent.start_cluster = 0;   
  fat_cache.parent.attributes = ATTR_ROOT_DIR;    /* mark as root dir */

  /* open file in write mode */
  fat_fclust_byte_count = 0;              /* byte 0 of cluster */
  fat_file_size.l = 0;
  fat_current_file_size = 0;
  /* reset the allocation list variable */
  fat_fchain_index = 0;
  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_clear_fat
*----------------------------------------------------------------------------
* PARAMS:
*    
* return:
*----------------------------------------------------------------------------
* PURPOSE:
*   Reset FAT clusters value
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*   
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void fat_clear_fat (void)
{
Uint16 sector_number;
Uint16 i;
Uint16 cluster;
Uint16 temp;
bit parity_bit;

  if (fat_is_fat16)
  {
    /* init the starting cluster value */
    cluster = fclusters[0].cluster + 2;
    /* Start at first chain cluster */
    sector_number = cluster / 256;
    /* Bufferize fat sector */
    Fat_load_fat_sector(sector_number);

    do 
    {
      if ((cluster / 256) != sector_number)
      {
        fat_update_fat_sector(sector_number);
        sector_number =  (Uint16)(cluster / 256);
        Fat_load_fat_sector(sector_number);
      }
      i = (cluster * 2) & 0x1FF;
      cluster = fat_buf_sector[i];
      fat_buf_sector[i++] = 0;
      cluster += (Uint16)(fat_buf_sector[i] << 8);
      fat_buf_sector[i] = 0;

    }
    while (cluster != 0xFFFF);
    fat_update_fat_sector(sector_number);
  }
  else
  {
    /* init the starting cluster value */
    cluster = fclusters[0].cluster + 2;
    /* Start at first chain cluster */
    sector_number = cluster * 3 / 1024;
    /* Bufferize fat sector */
    Fat_load_fat_sector(sector_number);
    temp = sector_number;

      
    do
    {
      temp = cluster * 3 / 1024;
      if (temp != sector_number)
      {
        fat_update_fat_sector(sector_number);
        sector_number = temp;
        Fat_load_fat_sector(sector_number);
      }

      parity_bit = cluster & 0x01;

      i = (cluster * 3 / 2) & 0x1FF;
      
      if (parity_bit == 0)
      {
        cluster = fat_buf_sector[i];
        fat_buf_sector[i] = 0x00;
        i++;
        if (i == 512)
        {

⌨️ 快捷键说明

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