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

📄 fat32.c

📁 基于AT89C51SND1的MP3的程序设计(包括播放mp3和录音功能)
💻 C
📖 第 1 页 / 共 5 页
字号:
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/ 
bit fat_goto_parentdir (Byte id)
{ 
Uint32 temp_cluster;
  temp_cluster = dclusters[0].cluster + 2;        /* save cluster info */
  if (temp_cluster != fat_rootclus_fat32)
  {
    fat_cache.current = fat_cache.parent;         /* goto the parent directory */
  
    /* issue the equivalent to a cd .. DOS command */
    if (fat_goto_subdir(id))
    {
      while (temp_cluster != fat_cache.current.start_cluster)
      {
        if (fat_goto_next() == KO)
          break;
      }
      if (temp_cluster == fat_cache.current.start_cluster)
        return OK;
      else
        return KO;
  
    }
    else
    {
      return KO;
    }
  }
  else
  {
    return OK; /* Nothing to do because we are in the root directory */
  }
}



/*F**************************************************************************
* NAME: fat_update_fat_sector
*----------------------------------------------------------------------------
* PARAMS: 
*   sector_number : fat sector position
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
*   update a sector of fat
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*   This function check if there is 2 fats to be updated
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/  
void fat_update_fat_sector (Uint16 sector_number)
{
  fat_up_down_load_sector(fat_ptr_fats + sector_number, DOWNLOAD);                   /* FAT 1 update */
  if (fat_2_is_present == TRUE)
  {
    fat_up_down_load_sector(fat_ptr_fats + sector_number + fat_fat_size, DOWNLOAD);  /* FAT 2 update */
  }      
}


/*F**************************************************************************
* NAME: fat_update_entry_fat
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
*   Update root entry and FAT after a writing file session (create or re-write)
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*   
*****************************************************************************/
void fat_update_entry_fat (void)
{
Byte index;
Uint16 chain_index;
Uint16 sector_number;
Byte id;
Uint16 i;
Uint16 j;
Uint32 cluster;
Uint16 temp;
Uint32 nb_cluster;

/*********************/
/* Update directory entry */
/*********************/
  fat_clear_dir_info();
  fat_dir_current_offs = (fat_current_end_entry_position - 1) * 32;
  fat_calc_cluster();
  fat_up_down_load_sector(fat_dir_current_sect, UPLOAD);
  j = ((fat_current_end_entry_position - 1) % 16) * 32 ;                /* Position of entry in the sector */

  /* Update file size */
  if (fat_cache.current.size.l <= fat_file_size.l)
    fat_cache.current.size.l = fat_file_size.l;
  fat_buf_sector[j + 28] = fat_cache.current.size.b[3];
  fat_buf_sector[j + 29] = fat_cache.current.size.b[2];
  fat_buf_sector[j + 30] = fat_cache.current.size.b[1];
  fat_buf_sector[j + 31] = fat_cache.current.size.b[0];

  ext[0] = fat_buf_sector[j + 8];
  ext[1] = fat_buf_sector[j + 9];
  ext[2] = fat_buf_sector[j + 10];
  id = fat_check_ext();

  fat_up_down_load_sector(fat_dir_current_sect, DOWNLOAD);

/********************/
/* Update fat 1 & 2 */
/********************/
  /* Calculate file size cluster */
  nb_cluster = (fat_cache.current.size.l / SECTOR_SIZE) / fat_cluster_size;
  if ((fat_cache.current.size.l % (fat_cluster_size * SECTOR_SIZE)))
  {
    nb_cluster++;
  }
  j = 1;
  index = 0;

/********************/
/* FAT32 management */
/********************/

  /* init the starting cluster value */
  cluster = fclusters[0].cluster + 2;
  /* Start at first chain cluster */
  sector_number = cluster / 128;
  /* Bufferize fat sector */
  fat_up_down_load_sector(fat_ptr_fats + sector_number, UPLOAD);
  /* i -> word fat sector position */
  i = (cluster * 4) & 0x1FF;
  chain_index = 1;

  while (j < nb_cluster)
  {
    /* Determinate the value of the next cluster */
    if (fclusters[index].number == chain_index)
    {
      /* increase index */
      index++;
      cluster = fclusters[index].cluster + 2;
      fat_buf_sector[i++] = ((Byte*)&cluster)[3];
      fat_buf_sector[i++] = ((Byte*)&cluster)[2];
      fat_buf_sector[i++] = ((Byte*)&cluster)[1];
      fat_buf_sector[i]   = ((Byte*)&cluster)[0];
      chain_index = 1;
      if ( (cluster / 128) != sector_number)
      { /* Fat change sector */
        fat_update_fat_sector(sector_number);
        sector_number = (Uint16)(cluster / 128);
        fat_up_down_load_sector(fat_ptr_fats + sector_number, UPLOAD);
      }
      i = (cluster * 4) & 0x1FF;

    }
    else
    {
      cluster++;
      fat_buf_sector[i++] = ((Byte*)&cluster)[3];
      fat_buf_sector[i++] = ((Byte*)&cluster)[2];
      fat_buf_sector[i++] = ((Byte*)&cluster)[1];
      fat_buf_sector[i++] = ((Byte*)&cluster)[0];
      chain_index++;
      if (i == SECTOR_SIZE)     /* Sector is full ? */   
      {                                                  
        fat_update_fat_sector(sector_number);            
        sector_number++;                                 
        fat_up_down_load_sector(fat_ptr_fats + sector_number, UPLOAD);   
        i = 0;                                           
      }            
    }
    j++;
  }
  /* End of file indicate by 0x0FFFFF */
  fat_buf_sector[i++] = 0xFF;
  fat_buf_sector[i++] = 0xFF;
  fat_buf_sector[i++] = 0xFF;
  fat_buf_sector[i]   = 0x0F;
  fat_update_fat_sector(sector_number);

  temp = fat_dir_list_index - 1;
  fat_seek_first();
  for (; temp != 0; temp--)
    fat_goto_next();
}


/*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;                                              /* empty file */
    }
    else
    {
      fat_fclust_byte_count = 0;                              /* byte 0 of cluster */
      fat_fchain_index = 0;                                   /* reset the allocation list variable */
      fat_fchain_nb_clust = 0;                                /* start on first contiguous cluster */
      fat_get_clusters(&fclusters, MAX_FILE_FRAGMENT_NUMBER); /* get file allocation list */
  
      fat_open_mode = READ;                                   /* seek to the beginning of the file */
      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;  
    flag_end_disk_file = FALSE;                               /* reset end disk flag  */
    fat_fchain_index = 0;                                     /* reset the allocation list variable */
    fat_fchain_nb_clust = 0;                                  /* start on first contiguous cl */
    /* get file allocation list */
    fat_get_clusters(&fclusters, MAX_FILE_FRAGMENT_NUMBER);
    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 *file_name, Byte attribute)
{
Byte temp_byte;
Byte i;
Uint16 j;
Uint16 index;
xdata Uint32 temp;
xdata Uint32 cluster;
xdata Uint32 temp_cluster;

  if (fat_no_entries_free == TRUE)                /* If last directory cluster is full */
  {
    cluster = 0;
    Hard_read_open(fat_ptr_fats);
    do
    {
      ((Byte*)&temp)[3] = Hard_read_byte();
      ((Byte*)&temp)[2] = Hard_read_byte();
      ((Byte*)&temp)[1] = Hard_read_byte();
      ((Byte*)&temp)[0] = Hard_read_byte();
      cluster++;
    }
    while (temp != 0x00000000);
    Hard_read_close();
    cluster--;                                                  /* Free cluster value */
    temp_cluster = dclusters[fat_last_dclust_index].cluster + dclusters[fat_last_dclust_index].number - 1;

    temp =  (temp_cluster + 2) / 128;                           /* End of cluster chain */
    fat_up_down_load_sector(fat_ptr_fats + temp, UPLOAD);
    j = (temp_cluster * 4) & 0x1FF;
    fat_buf_sector[j++] = ((Byte*)&cluster)[3];                 /* Update with the new cluster value */
    fat_buf_sector[j++] = ((Byte*)&cluster)[2];
    fat_buf_sector[j++] = ((Byte*)&cluster)[1];
    fat_buf_sector[j]   = ((Byte*)&cluster)[0];

    if ((cluster / 128) != temp)
    {
      fat_update_fat_sector(temp);
      temp = cluster / 128;
      fat_up_down_load_sector(fat_ptr_fats + temp, UPLOAD);
    }
    j = (cluster * 4) & 0x1FF;                                  /* Update end chain marker */
    fat_buf_sector[j++] = 0xFF;
    fat_buf_sector[j++] = 0xFF;
    fat_buf_sector[j++] = 0xFF;
    fat_buf_sector[j]   = 0x0F;
    fat_update_fat_sector(temp);                                /* Update fat sector */
    fat_last_dclust_index++;
    dclusters[fat_last_dclust_index].cluster = cluster - 2;     /* Update chain cluster value */
    dclusters[fat_last_dclust_index].number = 1;
    fat_last_dir_cluster_full = FALSE;
    fat_no_entries_free = FALSE;
    fat_total_clusters++;
  }

  fat_clear_dir_info();
  fat_dseek(0);

⌨️ 快捷键说明

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