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

📄 fat.c

📁 这是atmel公司的89C51SND1C的mp3源程序
💻 C
📖 第 1 页 / 共 5 页
字号:
        lfn_name[i] = '\0';        /* end of name */
      }

      /* store extension */
      ext[0]= gl_buffer[8];
      ext[1]= gl_buffer[9];
      ext[2]= gl_buffer[10];

      /* standard computing for normal entry */
      entry->attributes = gl_buffer[11];
      entry->start_cluster = gl_buffer[26];
      entry->start_cluster += ((Uint16) gl_buffer[27]) << 8;
      entry->size = gl_buffer[28];
      entry->size +=  ((Uint32) gl_buffer[29]) << 8;
      entry->size +=  ((Uint32) gl_buffer[30]) << 16;
      entry->size +=  ((Uint32) gl_buffer[31]) << 24;
      /* now it's time to stop */
      exit_flag = TRUE;
    }
    else
    { /* LFN entry format */
      lfn_entry_found = TRUE;             /* a 8.3 name will follow */

      if ( (gl_buffer[0] & LFN_SEQ_MASK) <= MAX_LFN_ENTRIES)   
      {                         /* Maximum number of entries for LFN? */
        for (i = 0; i < 5; i++)
          lfn_name[i + 13*((gl_buffer[0] & LFN_SEQ_MASK) - 1)] = gl_buffer[2*i + 1];
        for (i = 0; i < 6; i++)
          lfn_name[i + 5 + 13*((gl_buffer[0] & LFN_SEQ_MASK) - 1)] = gl_buffer[2*i + 14];
        for (i = 0; i < 2; i++)
          lfn_name[i + 11 + 13*((gl_buffer[0] & LFN_SEQ_MASK) - 1)] = gl_buffer[2*i + 28];
      }
    }
  }
  Hard_read_close();                        /* close physical read */
}


/*F**************************************************************************
* NAME: fat_get_directory_chain
*----------------------------------------------------------------------------
* PARAMS:
*   id: file extension to select
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
*   Construct the file directory chain
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*   The value are relative position with the previous file.
*----------------------------------------------------------------------------
* REQUIREMENTS:
*   Maximum of 256 entries between 2 authorized file (id extension)
*   because the relative position is stored on one byte.
*   To allow more than 256 entries (-> 32768), change type of
*   fat_directory_chain[] from Byte to Uint16 (no overflow management)
*   and change MAX_DIRECTORY_GAP_FILE from 255 to 32767
*****************************************************************************/
void fat_get_directory_chain (Byte id)
{
Uint16 index;                               /* chain index */
Uint16 counter_entry;                       /* entry counter: 0...MAX_DIRECTORY_FILE */
Uint16 entry_pos;                           /* relative entry position */
Uint16 entry_pos_saved;                     /* used when the file is not the id etension */
Byte   i;

  index = 0;
  fat_last_file = 0;
  counter_entry = 0;
  entry_pos = 0;   
  fat_directory_base = fat_current_sector;
  fat_directory_pos = 0;
  
  Hard_read_open(fat_directory_base);
  
  do /* scan all entries */
  {
    if (dir_is_root == TRUE)
      for (i = 0; i < DIR_SIZE; i++) gl_buffer[i] = Hard_read_byte();
    else
      for (i = 0; i < DIR_SIZE; i++) gl_buffer[i] = fat_dgetc();
    counter_entry++;                          /* increase the # entry         */

    if ((gl_buffer[0] != FILE_DELETED) && (gl_buffer[0] != FILE_NOT_EXIST))
    { /* Existing file ? */ 
      fat_directory_chain[index] = entry_pos; /* save the relative position   */
      entry_pos_saved = entry_pos;            /* */
      entry_pos = 1;                          /* reset the relative position  */
      index++;                                /* increase the index           */

      while (gl_buffer[11] == ATTR_LFN_ENTRY) /* LFN entry ?                  */
      {                                       /* then read all the LFN entry  */
        if (dir_is_root == TRUE)
          for (i = 0; i < DIR_SIZE; i++) gl_buffer[i] = Hard_read_byte();
        else
          for (i = 0; i < DIR_SIZE; i++) gl_buffer[i] = fat_dgetc();
        counter_entry++;                      /* increase the # entry          */
        entry_pos++;                          /* increase the relative position*/
                                              /* for the next file             */
      }

      /* filter on the file type */
      fat_cache.info.attributes = gl_buffer[11];
      ext[0] = gl_buffer[8];
      ext[1] = gl_buffer[9];
      ext[2] = gl_buffer[10];
      if ( (fat_check_ext() & id) == 0)
      {                                               /* Don't valid the entry */
        index--;                                              
        entry_pos += entry_pos_saved;
      }
    }
    else   /* Not an existing file */
      entry_pos++;

    fat_last_file = index;              /* update last file number      */
    /* For sub-directory, there is no logical limit for the number of entries */
    /* In order to detect the last file, we check gl_buffer[0]                */
    /* We can put in the chain directory MAX_DIRECTORY_FILE selected file     */
    if ( (dir_is_root == FALSE) && (gl_buffer[0] == FILE_NOT_EXIST))
      index = MAX_DIRECTORY_FILE;
    /* Overflow of entry_pos */
    if (entry_pos > MAX_DIRECTORY_GAP_FILE)
      index = MAX_DIRECTORY_FILE;
    /* For Root directory, the maximum entries is 512!                        */
    if ( (dir_is_root == TRUE) && (counter_entry == MAX_DIRECTORY_FILE))
      index = MAX_DIRECTORY_FILE;
  }
  while (index < MAX_DIRECTORY_FILE);

  fat_current_sector = fat_directory_base;
  Hard_read_close();
}

/*F**************************************************************************
* NAME: fat_get_root_directory
*----------------------------------------------------------------------------
* PARAMS:
*   id: file extension to select
*
* return:
*   - OK if file available
*   - KO if file not found or file erased
*   - KO if low_level memory failed
*----------------------------------------------------------------------------
* PURPOSE:
*   Select first available file/dir in root diretory
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*   Fill all the cache information for the first time
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit fat_get_root_directory (Byte id)
{
  /* select first root dir entry */
  fat_current_sector = fat_ptr_rdir;

  fat_current_byte_counter = 0;
  dir_is_root = TRUE;

  fat_get_directory_chain(id);
  if (fat_last_file == 0) /* No file/dir */
    return KO;
  /* Open the root directory on the first entry */
  fat_file_ptr = 0;

  /* extract info from table */
  if ( fat_dseek(fat_directory_chain[fat_file_ptr] * 32) == OK)
  {
    fat_fetch_directory_info(&fat_cache.info);
    /* parent dir is also root */
    fat_cache.parent.start_cluster = 0;   
    fat_cache.parent.attributes = ATTR_DIRECTORY;
    return OK;
  }
  else
  {
    return KO;
  }
}


/*F**************************************************************************
* NAME: fat_goto_next
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
*   Fetch the next dir/file info in cache
*----------------------------------------------------------------------------
* EXAMPLE:
*   Search for the next file in current directory
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/ 
bit fat_goto_next (void)
{
  if (fat_file_ptr < (fat_last_file - 1))
  {
    fat_file_ptr++;

    if ( fat_dseek((Int16)(fat_directory_chain[fat_file_ptr] * 32)) == OK)
    {
      fat_fetch_directory_info(&fat_cache.info);
      return OK;
    }
    else
      return KO;
  }
  else
    return KO;
}


/*F**************************************************************************
* NAME: fat_goto_prev
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*   - OK: previous file found
*   - KO: - previous file not found
*         - low level error
*----------------------------------------------------------------------------
* PURPOSE:
*   Fetch the previous directory info in cache
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*   Search for the previous directory entry in current directory
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/ 
bit fat_goto_prev (void)
{
  Byte min;
  
  if (dir_is_root)
    min = 0;
  else
    min = 2;

  if (fat_file_ptr != min)            /* Check if it's the first file of the directory */
  {
    if ( fat_dseek((Int16)(fat_directory_chain[fat_file_ptr] * (-32))) == OK)
    { /* go to previous file */
      fat_file_ptr--;                             /* update the file pointer  */
      fat_fetch_directory_info(&fat_cache.info);  /* construct the file table */
      return OK;
    }
    else
      return KO;
  }
  else
    return KO;

}

/*F**************************************************************************
* NAME: fat_seek_last
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*   - OK: last file found
*   - KO: - last file not found
*         - low level error
*----------------------------------------------------------------------------
* PURPOSE:
*   Fetch the last directory info in cache
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*   
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/ 
bit fat_seek_last (void)
{
  Uint16 gl_offset;
  Uint16 i;
  
  for (i = fat_file_ptr + 1, gl_offset = 0; i < fat_last_file; fat_file_ptr++, i++)
    gl_offset += fat_directory_chain[i];

  if ( fat_dseek(gl_offset * 32) == OK)
  {
    fat_fetch_directory_info(&fat_cache.info);      
    return OK;
  }
  else
  {
    return KO;
  }
}

/*F**************************************************************************
* NAME: fat_seek_first
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*   - OK: first file found
*   - KO: - first file not found
*         - low level error
*----------------------------------------------------------------------------
* PURPOSE:
*   Fetch the first directory info in cache
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*   
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/ 
bit fat_seek_first (void)
{
  fat_directory_pos = 0;             /* Reset the global offset */

  if (dir_is_root == TRUE)           /* Root diretory ?         */
  {
    fat_file_ptr = 0;                /* Reset the file pointer  */
    if (fat_dseek((Int16)(fat_directory_chain[fat_file_ptr] * 32)) == OK)
    {
      fat_fetch_directory_info(&fat_cache.info);      
      return OK;
    }
    else
    {
      return KO;
    }
  }
  else
  {
    fat_file_ptr = 1;
    if (fat_dseek((Int16)(fat_directory_chain[fat_file_ptr] * 32)) == OK)
    {
      /* update parent info in ".." entry*/
      fat_fetch_directory_info(&fat_cache.parent);
      /* point next file */
      return fat_goto_next();
    }
    else
    {
      return KO;
    }
  }

}


/*F**************************************************************************
* NAME: fat_goto_subdir
*----------------------------------------------------------------------------
* PARAMS:
*   id: file extension to select
*
* return:   
*----------------------------------------------------------------------------
* PURPOSE:
*   Go to the subdirectory
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*   Only go to subdirectory if current file is a directory
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/ 
bit fat_goto_subdir (Byte id)
{                        

⌨️ 快捷键说明

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