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

📄 fat.c

📁 MP3播放器详细设计方案
💻 C
📖 第 1 页 / 共 5 页
字号:
      }
      sector_number++;
      Hard_read_open(fat_ptr_fats + sector_number);
      for (i = 0; i < 512; i++)
        fat_buf_sector[i] = Hard_read_byte();
      Hard_read_close();
      i = 0;
    }
    cluster++;
    fat12_parity = 0;
  }
  /* FAT 1 update */
  Hard_write_open(fat_ptr_fats + sector_number);
  for (i = 0; i < 512; i++)
    Hard_write_byte(fat_buf_sector[i]);
  Hard_write_close();
  if (fat_2_is_present == TRUE)
  {
    /* FAT 2 update */
    Hard_write_open(fat_ptr_fats + sector_number + fat_fat_size);
    for (i = 0; i < 512; i++)
      Hard_write_byte(fat_buf_sector[i]);
    Hard_write_close();
  }  


  /* 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)
{
Uint16 i;
Uint16 j;
xdata Uint16 indice;
xdata Uint16 indice_min;
xdata Uint16 indice_max;
xdata Uint16 pos;
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;

  indice = 0x000;
  index = 0;

  /* determinate file index */
  indice_min = 0xFFFF;
  indice_max = 0x0000;

  if (Hard_read_open(fat_ptr_rdir) == KO)
    return KO;
  do
  {
    for (i = 0; i < DIR_SIZE; i++)
      gl_buffer[i] = Hard_read_byte();
    /* Check if it's a ?OICE???.WAV file */
    if ((gl_buffer[1] == 'O') &&
        (gl_buffer[2] == 'I') &&
        (gl_buffer[3] == 'C') &&
        (gl_buffer[4] == 'E') &&
        (gl_buffer[8] == 'W') &&
        (gl_buffer[9] == 'A') &&
        (gl_buffer[10] == 'V'))
    {
      /* Calculate actual index */
      temp =  (Uint16)(gl_buffer[5] - '0') * 100;
      temp += (Uint16)(gl_buffer[6] - '0') * 10;
      temp += (Uint16)(gl_buffer[7] - '0') * 1;
      
      if (gl_buffer[0] == FILE_DELETED) /* if file is deleted */
      {
        if (temp <= indice_min)
        {
          indice_min = temp;
          pos = index;                  /* if index is the lower, take it */
        }
      }
      else                              /* if file not deleted, then save max value */
      {
        if (temp > indice_max)
        {
          indice_max = temp;
        }
      }
    }
    index++;
  }
  while (gl_buffer[0] != FILE_NOT_EXIST);

  Hard_read_close();

  if (indice_min == 0xFFFF)             /* if we don't find an erased file */
  {
    if (fat_dir_list_last == 0x00)      /* if no wav file existing */
    {
      indice = 0x000;
    }
    else                                /* else increase the max value */
    {
      indice = indice_max + 1;
    }

    /* Find the first free entry in root */
    index = 0;
    if (Hard_read_open(fat_ptr_rdir) == KO) 
      return KO;
    do
    {
      for (i = 0; i < DIR_SIZE; i++)
        gl_buffer[i] = Hard_read_byte();
      if ((gl_buffer[0] != FILE_NOT_EXIST) && (gl_buffer[0] != FILE_DELETED))
      { 
        index++;
      }
    }
    while ((gl_buffer[0] != FILE_NOT_EXIST) && (gl_buffer[0] != FILE_DELETED));
    Hard_read_close();
  }
  else                    /* There is an erase file */
  { /*  */
    indice = indice_min;
    index = pos;
  }


  if ((dir_is_root == TRUE) && (index >= NB_ROOT_ENTRY))    /* Maximum entries in root directory */
    return KO;
  /* Construct the entry */
  gl_buffer[0] = 'V';
  gl_buffer[1] = 'O';
  gl_buffer[2] = 'I';
  gl_buffer[3] = 'C';
  gl_buffer[4] = 'E';
  gl_buffer[5] = '0' + (indice / 100);
  gl_buffer[6] = '0' + ((indice % 100) / 10);
  gl_buffer[7] = '0' + (((indice % 100) % 10));
  gl_buffer[8] = 'W';
  gl_buffer[9] = 'A';
  gl_buffer[10] = 'V';
  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 < 512; 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 < 512; 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(FALSE);            /* 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 (void)
{
  /* 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) == 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_fdelete
*------------------------------------------

⌨️ 快捷键说明

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