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

📄 bfsa_api.c

📁 ATMEL公司的demo程序,USB驱动程序,与识别片上flash,并进行枚举和操作.
💻 C
📖 第 1 页 / 共 4 页
字号:
          if( file_list[a].attributes == BFSA_ATTR_ARCHIVE )
          {
            TRACE_INFO( " %d/",file_list[a].date.Day);
            TRACE_INFO( "%d/",file_list[a].date.Month);
            TRACE_INFO( "%d",file_list[a].date.Years80+1980);

            TRACE_INFO( " %d:",file_list[a].time.Hours);
            TRACE_INFO( "%d:",file_list[a].time.Minutes);
            TRACE_INFO( "%d",file_list[a].time.Seconds);

            TRACE_INFO( " %d",(int)file_list[a].length);
          }
          TRACE_INFO( "\n\r");
        }
      }
    }
    else
    {
      TRACE_INFO( "Empty directory\n\r");
    }
    // Look if there is enough room on disk
    po_lock();
    readFAT();
    // offsetFAT = 0;
    for( iFatEntry = 0;
         iFatEntry < (BFSA_media.MaxCluster - BFSA_media.ClusterStart);
         iFatEntry++ )
    {
      // if( iFatEntry > sizeof(BFSA_fatBuff)  )
      // {
      //   readFAT(offsetFAT++);
      //
      // if( readFatEntry(iFatEntry+2-offsetFAT*sizeof(BFSA_fatBuff)) == FREE )
      if( readFatEntry(iFatEntry+2) == FREE )
      {
//        if( (iFatEntry + (iFatEntry / 2)) < sizeof(BFSA_fatBuff) )
//        {
          freeSpace += BFSA_media.bytesPerSector*BFSA_media.SectPerCluster;
//          if (freeSpace > dataSize)
//            break;
//        }
      }
      else
      {
        TRACE_DEBUG_L( "dbg:ReadFat:0x%X",readFatEntry(iFatEntry+2));
        TRACE_DEBUG_L( ":%X ",iFatEntry);
      }
    }
    TRACE_INFO( "     %d",(int)freeSpace/1024);
    TRACE_INFO( " Kb Free\n\r");
    po_unlock();
  }

  return( status );

}

/*****************************************************************
*
*
* S U B - R O U T I N E  : BFSA_list
*
*-----------------------------------------------------------------
*
* int BFSA_list(FILE_OBJECT* pfobj,ULONG nbFileObj,ULONG *nbFileObjFound)
*
* Object:
*   This function initializes the File System. It must be called
*   only once at start-up, before any other BFSA functions.
*
* Argument:
*       pfobj           :[OUT] pointer to FILE_OBJECTs array, allocated
*                        by caller.
*       nbFileObj       :[IN]  number of elements allocated in pfobj.
*       nbFileObjFound  :[OUT] number of elements actually written in
*                        pfobj up to nbFileObj.
*                        If nbFileObjFound>nbFileObj, there are more FILE_OBJECTs
*                        to be writen, we need pfobj with nbFileObjFound elements.
*
* Return value:
*       0 if success,
*       on error:       ERROR_READING_FAT_ENTRY
*                       MEMORY_ERROR
*                       FLASH_ACCESS_ERROR
*                       BAD_ARGUMENT
*
*****************************************************************/
ULONG BFSA_list(FILE_OBJECT* pfobj,ULONG nbFileObj,ULONG *nbFileObjFound)
{
  ULONG _status;

  if (!nbFileObjFound || (nbFileObj!=0 && !pfobj))
  {
    _status = BAD_ARGUMENT;
  }
  else
  {
    *nbFileObjFound = 0;
    // Lock and Load FAT
    po_lock();
    _status = listFile(pfobj,nbFileObj,nbFileObjFound,BFSA_current_dir.IsRoot?0:BFSA_current_dir.DirStart);
    po_unlock();
  }
  return _status;
}

/*****************************************************************
*
*
* S U B - R O U T I N E  : BFSA_readMediaInfo
*
*-----------------------------------------------------------------
*
* int BFSA_readMediaInfo(MEDIA_INFO *media)
*
* Object:
*   This function read boot sector and get detailed partition and
*   media information.
*
* Argument:
*       media           :[OUT] pointer to MEDIA_INFO data, allocated
*                        by caller.
*
* Return value:
*       0 if success,
*       on error:       NO_MEDIA_FOUND
*                       NOT_A_FAT_FILESYSTEM
*                       INTERNAL_ERROR
*                       BAD_ARGUMENT
*
*****************************************************************/
ULONG BFSA_readMediaInfo(MEDIA_INFO *media)
{
  ULONG _status = BFSA_SUCCESS;

  if (!media)
  {
    TRACE_ERROR( "Bad parameter\n\r");
    _status = BAD_ARGUMENT;
  }
  else
  {
    UCHAR _pBPB[BPB_SIZE];

    // FAT specific
    USHORT _RootEntCnt;     // Root entries count
    USHORT _FatSz16;        // FAT16 size
    ULONG _FatSz;           // FAT size
    USHORT _totSec16;       // Total sectors 16
    ULONG _totSec32;        // Total sectors 32
    ULONG _totSec;          // Total sectors
    USHORT _rsvSecCnt;      // Count of reserved sectors
    ULONG _dataSec;         // Data Sectors
    ULONG _countOfCluster;  // Count of Clusters

    // Read BPB from flash
    _status = readBPB(_pBPB, sizeof _pBPB);
    if (BFSA_FAILURE(_status))
    {
      // Can't read Flash
      TRACE_ERROR( "Can't read BPB\n\r");
      goto END;
    }

    // Populate BFSA_media from BPB: "BIOS Parameter Block"
    BFSA_media.bytesPerSector   = max(_pBPB[0x0C]*0x100 + _pBPB[0x0B],1);
    BFSA_media.SectPerCluster   = max(_pBPB[0x0D],1);

    BFSA_media.FATSectStart     = _pBPB[0x0F]*0x100 + _pBPB[0x0E];
    BFSA_media.numFAT           = _pBPB[0x10]; // Number of FAT
    BFSA_media.SectPerFAT       = _pBPB[0x17]*0x100 + _pBPB[0x16];

    BFSA_media.RootStart        = BFSA_media.FATSectStart +
                                  BFSA_media.SectPerFAT * BFSA_media.numFAT;

    // The first sector of cluster 2 (the data region of the disk) is computed using
    // the BPB fields for the volume as follows.
    // First, we determine the count of sectors occupied by the root directory:
    // RootDirSectors = ((BPB_RootEntCnt * 32) + (BPB_BytsPerSec - 1)) / BPB_BytsPerSec;
    _RootEntCnt = _pBPB[0x12]*0x100 + _pBPB[0x11]; // Root Entries
    BFSA_media.numRootSectors   = ((_RootEntCnt*32)+(BFSA_media.bytesPerSector-1))/
                                  BFSA_media.bytesPerSector;

    BFSA_media.ClusterStart     = BFSA_media.RootStart +
                                  BFSA_media.numRootSectors;

    BFSA_media.MaxCluster       = _pBPB[0x14]*0x100 + _pBPB[0x13];

    BFSA_media.mediaType        = _pBPB[0x15];  // Medium Descriptor

    if(  ((0xF0 != BFSA_media.mediaType) && (0xF8 != BFSA_media.mediaType))
    || ( BFSA_media.MaxCluster > NB_BLOCK+1024)
    || ( BFSA_media.bytesPerSector > 1024 )
    )
    {
      _status = NOT_A_FAT_FILESYSTEM;
      TRACE_DEBUG_H( "NOT FAT FS\n\r");
      BFSA_media.FATtype = BFSA_FATUNKNOWN;
      goto END;
    }

    // The start of the data region, the first sector of cluster 2, is computed as follows:
    //If(BPB_FATSz16 != 0)
    //    FATSz = BPB_FATSz16;
    //Else
    //    FATSz = BPB_FATSz32;
    //FirstDataSector = BPB_ResvdSecCnt + (BPB_NumFATs * FATSz) + RootDirSectors;

    // FAT determination
    _FatSz16 = _pBPB[0x17]*0x100 + _pBPB[0x16];  // Sect Per FAT
    if (_FatSz16!=0)
    {
      _FatSz = _FatSz16;
    }
    else
    {
      // get FAT32 size   //OK, but not for us (FAT12)
      _FatSz =  _pBPB[0x27]*0x1000000 + _pBPB[0x26]*0x10000
              + _pBPB[0x25]*0x100     + _pBPB[0x24];
    }

    // Page 14, fatgen103.doc
    // Next, we determine the count of sectors in the data region of the volume:
    //If(BPB_TotSec16 != 0)
    //    TotSec = BPB_TotSec16;
    //Else
    //    TotSec = BPB_TotSec32;
    //
    //DataSec = TotSec - (BPB_ResvdSecCnt + (BPB_NumFATs * FATSz) + RootDirSectors);

    _totSec16 = BFSA_media.MaxCluster;
    _totSec32 =  _pBPB[0x23]*0x1000000 + _pBPB[0x22]*0x10000 // Hidden Sects
               + _pBPB[0x21]*0x100     + _pBPB[0x20];
    if(_totSec16!=0)
      _totSec = _totSec16;
    else
      _totSec = _totSec32;

    // Get the total number of cluster  // FirstSectorofCluster = ((N - 2) * BPB_SecPerClus) + FirstDataSector;
    _rsvSecCnt = BFSA_media.FATSectStart;

    _dataSec = _totSec - (_rsvSecCnt + (BFSA_media.numFAT*_FatSz)+BFSA_media.numRootSectors);

    // Now we determine the count of clusters:
    _countOfCluster = _dataSec / BFSA_media.SectPerCluster;

    // Finaly determine the FAT
    if (_countOfCluster < 4085)  // The first number for FAT12 is 4085
    {
      TRACE_INFO( "FAT12\n\r");
      BFSA_media.FATtype = BFSA_FAT12;
    }
    else
    {
      if (_countOfCluster < 65525)  // The second number for FAT16 is 65525
      {
        TRACE_INFO( "FAT16\n\r");
        BFSA_media.FATtype = BFSA_FAT16;
      }
      else
      {
        // FAT 32
        TRACE_INFO( "FAT unkwn or 32\n\r");
        _status = NOT_A_FAT_FILESYSTEM;
        BFSA_media.FATtype = BFSA_FATUNKNOWN;
        goto END;
      }
    }
  }

  END:
  return _status;
}

/*****************************************************************
*
*
* S U B - R O U T I N E  : BFSA_cd
*
*-----------------------------------------------------------------
*
* int BFSA_cd(char *pucPath)
*
* Object:
*   This function changes the current directory.
*
* Argument:
*       pucPath         [IN]: pointer to path to the new directory.
*
* Return value:
*       0 if success,
*       on error:       NOT_A_VALID_DIRECTORY
*                       FILE_NOT_EXIST
*                       MEMORY_ERROR
*                       FLASH_ACCESS_ERROR
*                       BAD_ARGUMENT
*                       INTERNAL_ERROR
*
*****************************************************************/
ULONG BFSA_cd(char* path)
{
  ULONG  _status = BFSA_SUCCESS;
  char*  _pabsolutePathCurrent = BFSA_current_dir.absolutePath;
  UCHAR   _bEnd = FALSE;
  UCHAR* _pucEntry;
  UCHAR  pPucEntry[DIRECTORY_ENTRIE_SIZE];
  USHORT _firstDirCluster;
  char   _absolutePath[BFSA_MAX_PATH_LEN];    // 102 !!!!
  char*  _pt = _absolutePath;

  if (!path)
  {
    _status = BAD_ARGUMENT;
    goto END;
  }

  // copy path
  po_memcpy(_absolutePath,path,sizeof _absolutePath);
  path = _absolutePath;

  // Lock and Load FAT
  po_lock();

  if (*_pt == '\\')
  {
    // Absolute path starts from racine
    BFSA_current_dir.absolutePath[0] = '\\';
    BFSA_current_dir.absolutePath[1] = '\0';
    BFSA_current_dir.DirStart = BFSA_media.RootStart;
    BFSA_current_dir.IsRoot = TRUE;
    _pabsolutePathCurrent = BFSA_current_dir.absolutePath;
    _pt++;
  }
  else
  {
    // End of current dir absolutepath (last \ )
    while (!(*_pabsolutePathCurrent == '\\' && *(_pabsolutePathCurrent+1) =='\0') )
    {
      _pabsolutePathCurrent++;
    }
  }

  while (*_pt!='\0' && !_bEnd)
  {
    path = _pt;
    // Search the next \ or \0
    while (*_pt!='\\' && *_pt!='\0')
    {
      _pt++;
    }
    if (*_pt == '\0') _bEnd = TRUE;
    *_pt = '\0';

    // Check for . et .. file name
    if (*path == '.')
    {
      if (path[1] == '.')
      {
        // .. case
        if (path[2]=='\0')
        {
          // Nothing in ROOT
          if (BFSA_current_dir.IsRoot == TRUE)
          {
              continue;
          }

          // Get the .. entry (always the second DIRECTORY_ENTRY)
          _pucEntry = (UCHAR*)FlashBaseAdd +
                      FIRST_SECTOR_DATACLUSTER(BFSA_current_dir.DirStart)*BFSA_media.bytesPerSector;
          _pucEntry += DIRECTORY_ENTRIE_SIZE;

          AT91F_ReadMedia((unsigned int)_pucEntry, DIRECTORY_ENTRIE_SIZE, pPucEntry);

          if (pPucEntry[0]!='.' || pPucEntry[1]!='.')
          {
            // Wrong format FAT
            po_unlock();
            return INTERNAL_ERROR;
          }

          // Get the first directory Cluster
          _firstDirCluster = pPucEntry[26]+pPucEntry[27]*0x100;

          // If .. leads to ROOT
          if (_firstDirCluster==0)
          {
            BFSA_current_dir.absolutePath[0] = '\\';
            BFSA_current_dir.absolutePath[1] = '\0';
            BFSA_current_dir.DirStart = BFSA_media.RootStart;
            BFSA_current_dir.IsRoot = TRUE;
            _pabsolutePathCurrent = BFSA_current_dir.absolutePath;
            // Next directory name
            _pt++;
            continue;
          }
          else
          {
            // Update current dir
            BFSA_current_dir.DirStart = _firstDirCluster;
            BFSA_current_dir.IsRoot = FALSE;
            _pabsolutePathCurrent--;  // jump the last "\"
            // Search the preceding \ in the absolute path
            while (*_pabsolutePathCurrent!='\\')
            {
              _pabsolutePathCurrent--;
            }
            _pabsolutePathCurrent[1] = '\0';

            // Next directory name
            _pt++;
            continue;
          }
        }
        else
        {
          // invalide name
          po_unlock();

⌨️ 快捷键说明

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