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

📄 bfsa_api.c

📁 ATMEL公司的demo程序,USB驱动程序,与识别片上flash,并进行枚举和操作.
💻 C
📖 第 1 页 / 共 4 页
字号:
//  ----------------------------------------------------------------------------
//          ATMEL Microcontroller Software Support  -  ROUSSET  -
//  ----------------------------------------------------------------------------
//  DISCLAIMER:  CONDITIONS AS PER SIGNED LIMITED LICENSE AGREEMENT (AT91
//  SOFTWARE AND USER DOCUMENTATION)
//  ALL SOFTWARE IS PROVIDED AS IS, WITH ALL FAULTS, AND WITH NO WARRANTY
//  WHATSOEVER.  ATMEL EXPRESSLY DISCLAIMS ALL WARRANTIES, EXPRESS, IMPLIED,
//  OR STATUTORY, INCLUDING WITHOUT LIMITATION ANY WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.
//  ----------------------------------------------------------------------------
// File Name           : bfsa_api.c
// Object              : bfsa_api routines
// Creation            : FB  10/feb/2005
// Modif               : JCB 15/apr/2005
// --------------------------------------------------------------------------
#include "po_types.h"
#include "fw_usb.h"
#include "po_kernel.h"
#include "trace.h"
#include "flash.h"
#include "bfsa_flash.h"
#include "bfsa_api.h"
#include "bfsa_internal.h"


// **************************************************
//        Global declaration
// **************************************************
CURRENT_DIR     BFSA_current_dir;
MEDIA_INFO      BFSA_media;


union fatBuff BFSABuffer; // BFSA_media.SectPerFAT*BFSA_media.bytesPerSector



FILE_OBJECT file_list[5];
char CurrentPath[BFSA_MAX_PATH_LEN]="\\";    // 102

/*****************************************************************
*
*
* S U B - R O U T I N E  : BFSA_init
*
*-----------------------------------------------------------------
*
* Object :
*   This function initializes the File System. It must be called
*   only once at start-up, before any other BFSA functions.
*
* Argument :            NONE
*
* Return value : 0 if success,
*       on error :      NOT_MEDIA_FOUND
*                       NOT_A_FAT_FILESYSTEM
*
*****************************************************************/
ULONG BFSA_init(void)
{
  ULONG _status = BFSA_SUCCESS;

  #ifndef NANDFLASH
    // Initialize the FLASH
    AT91F_Flash_Init();
  #else
    AT91F_NandFlash_Init();
  #endif

  // Read in MBR the location of the first partition, actualy our disk is always
  // the first partition and is located just after the MBR, so from offset 200h
  // in FLASH.
  FlashBaseAdd = FILE_MGR_BEGIN_ADDRESS+MBR_SIZE;
  TRACE_DEBUG_H( "FlashBase:0x%X\n\r",(int)FlashBaseAdd);

  // Read Media Info
  _status = BFSA_readMediaInfo(&BFSA_media);
  if(!BFSA_FAILURE(_status))
  {
     TRACE_INFO( "GetMediaInfo\n\r");
     TRACE_INFO( "FatType    : 0x%X\n\r",BFSA_media.FATtype);
     TRACE_INFO( "mediaType  : 0x%X\n\r",BFSA_media.mediaType);
     TRACE_INFO( "bytesPrSect: 0x%X\n\r",BFSA_media.bytesPerSector);
     TRACE_INFO( "SectPrClust: 0x%X\n\r",BFSA_media.SectPerCluster);
     TRACE_INFO( "FATSectStrt: 0x%X\n\r",BFSA_media.FATSectStart);
     TRACE_INFO( "SectPerFAT : 0x%X\n\r",BFSA_media.SectPerFAT);
     TRACE_INFO( "numFAT     : 0x%X\n\r",BFSA_media.numFAT);
     TRACE_INFO( "RootStart  : 0x%X\n\r",BFSA_media.RootStart);
     TRACE_INFO( "numRootSctr: 0x%X\n\r",BFSA_media.numRootSectors);
     TRACE_INFO( "ClusterStrt: 0x%X\n\r",BFSA_media.ClusterStart);
     TRACE_INFO( "MaxCluster : 0x%X\n\r",BFSA_media.MaxCluster);
     TRACE_INFO( "DiskSize   : %d Kb\n\r",(BFSA_media.MaxCluster*512)/1024);

    // Update currentdir
    BFSA_current_dir.absolutePath[0] = '\\';
    BFSA_current_dir.absolutePath[1] = '\0';
    BFSA_current_dir.DirStart = BFSA_media.RootStart;
    BFSA_current_dir.IsRoot = TRUE;
  }
  else
  {
    TRACE_INFO( "Pb format\n\r");
    _status = NOT_A_FAT_FILESYSTEM;
  }
  return _status;
} // end BFSA_init

/*****************************************************************
*
*
* S U B - R O U T I N E  : BFSA_format
*
*-----------------------------------------------------------------
*
* Object :
*
*
* Argument :
*
* Return value :
*
*****************************************************************/
ULONG BFSA_format( void )
{
  ULONG BPB_FATSz16;
  USHORT Number_of_sector;
  unsigned int value;
  ULONG _status = FLASH_ACCESS_ERROR;
  ULONG count;

  // FLASH_HD size  (NB_BLOCK_HB*256+NB_BLOCK_LB)*SIZEONESECTOR Ko
  //   512 (MBR) - 512 (MBR2) - 512*2 (FAT1) - 512*2 (FAT2) - 512x32 (RootDirectory)
  //   512x38 = 19456 = 19 Ko LOST  // 18Ko lost with AT91SAM7S64

  // Erase table BFSA_fatBuff
//  for (count=0; count < sizeof(BFSA_fatBuff); count++)
  for (count=0; count < sizeofBFSA_fatBuff; count++)
  {
    BFSABuffer.BFSA_fatBuff[count] = (unsigned char)0x00;
  }

  // ************* [0x1BE] First partition *************
    // [0x1BE][00] Boot Indicator, Not active (0x80: active)
    BFSABuffer.BFSA_fatBuff[0x1BE] = 0x00;
    // [0x1BF][01] Head partition start
    BFSABuffer.BFSA_fatBuff[0x1BF] = 0x00;
    // [0x1C0][02] Sector and cylinder of partition start
    BFSABuffer.BFSA_fatBuff[0x1C0] = 0x01; BFSABuffer.BFSA_fatBuff[0x1C1] = 0x00;
    // [0x1C2][04] Partition type 0x01 FAT12 < 10M
    BFSABuffer.BFSA_fatBuff[0x1C2] = 0x01;
    // [0x1C3][05] Head of partition end
    BFSABuffer.BFSA_fatBuff[0x1C3] = 0x00;
    // [0x1C4][06] Sector and cylinder of partition end
    Number_of_sector = NB_BLOCK-1;
    BFSABuffer.BFSA_fatBuff[0x1C4] = (UCHAR) Number_of_sector;
    BFSABuffer.BFSA_fatBuff[0x1C5] = ((UCHAR)(Number_of_sector>>8));
    // [0x1C6][08] Relative Sectors. The offset from the beginning of the disk to the beginning of the volume, counting by sectors.
    BFSABuffer.BFSA_fatBuff[0x1C6] = 0x01; BFSABuffer.BFSA_fatBuff[0x1C7] = 0x00; BFSABuffer.BFSA_fatBuff[0x1C8] = 0x00; BFSABuffer.BFSA_fatBuff[0x1C9] = 0x00;
    // FORMAT PB   BFSA_fatBuff[0x1C6] = 0x00; BFSA_fatBuff[0x1C7] = 0x00; BFSA_fatBuff[0x1C8] = 0x00; BFSA_fatBuff[0x1C9] = 0x00;
    // [0x1CA][0C] Total Sectors. The total number of sectors in the volume.
    BFSABuffer.BFSA_fatBuff[0x1CA] = (UCHAR) NB_BLOCK;
    BFSABuffer.BFSA_fatBuff[0x1CB] = ((UCHAR)(NB_BLOCK>>8));
    BFSABuffer.BFSA_fatBuff[0x1CC] = 0x00;
    BFSABuffer.BFSA_fatBuff[0x1CD] = 0x00;
  // ************* END First partition *************

  // ************* Identification code : 0xAA55 = good partition *************
    BFSABuffer.BFSA_fatBuff[0x1FE] = 0x55;
    BFSABuffer.BFSA_fatBuff[0x1FF] = 0xAA;
  // ************* End Identification code *************

  po_lock();
  value = 0x00;

  // free the FATs sector and root dir
#ifdef NANDFLASH
  for (count=0; count<64; count++)
  {
    AT91F_Flash_Write_Address((unsigned int)FILE_MGR_BEGIN_ADDRESS+(2+count)*MBR_SIZE, MBR_SIZE, (unsigned char*) &value, TRUE);
  }
#else
  #ifdef AT91SAM9265
  // Erase all HD Flash size
  if( FALSE == AT91F_Flash_Write_Address((unsigned int)FILE_MGR_BEGIN_ADDRESS, HD_SIZE, (unsigned char*) &value, TRUE) )
  {
    TRACE_ERROR( "FT_PB\n\r");
  }
  else
  #endif
#endif
  // Write Boot Sector
  if( TRUE == AT91F_Flash_Write_Address((unsigned int)FILE_MGR_BEGIN_ADDRESS, MBR_SIZE /*sizeof(BFSA_fatBuff)*/, (unsigned char*)BFSABuffer.BFSA_fatBuff, FALSE ) )
  {
    // Erase table BFSA_fatBuff
    for (count=0; count < sizeofBFSA_fatBuff; count++)
    {
      BFSABuffer.BFSA_fatBuff[count] = (unsigned char)0x00;
    }


    // Jump to boot routine BS_jmpBoot
    BFSABuffer.BFSA_fatBuff[0x00] = 0xEB;  // jmpBoot[0] = 0xEB, jmpBoot[1] = 0x??, jmpBoot[2] = 0x90
    BFSABuffer.BFSA_fatBuff[0x01] = 0x3C;
    BFSABuffer.BFSA_fatBuff[0x02] = 0x90;
    // Manufacturer name and version number // BS_OEMName "MSWIN4.1"
    //BFSA_fatBuff[0x03] = 'M'; BFSA_fatBuff[0x04] = 'S'; BFSA_fatBuff[0x05] = 'W'; BFSA_fatBuff[0x06] = 'I';
    //BFSA_fatBuff[0x07] = 'N'; BFSA_fatBuff[0x08] = '4'; BFSA_fatBuff[0x09] = '.'; BFSA_fatBuff[0x0A] = '1';

    // ************* "BIOS Parameter Block" (BPB) *************
      // BPB_BytsPerSec: Count of bytes per sector (512, 1024, 2048 or 4096)
      BFSABuffer.BFSA_fatBuff[0x0B] = 0x00; BFSABuffer.BFSA_fatBuff[0x0C] = 0x02;
      // BPB_SecPerClus: Number of sectors per allocation unit (1, 2, 4, 8, 16, 32, 64, and 128)
#if defined(NANDFLASH)
      BFSABuffer.BFSA_fatBuff[0x0D] = 0x20;
#else
      BFSABuffer.BFSA_fatBuff[0x0D] = 0x01;
#endif
      // BPB_RsvdSecCnt: 0x01 Number of reserved sectors in the Reserved region of the volume starting at the first sector of the volume
      BFSABuffer.BFSA_fatBuff[0x0E] = 0x01; BFSABuffer.BFSA_fatBuff[0x0F] = 0x00;
      // BPB_NumFATs   : The count of FAT data structures on the volume
      BFSABuffer.BFSA_fatBuff[0x10] = 0x02;

      // BPB_RootEntCnt: For FAT12 and FAT16 volumes, this field contains the count
      // of 32-byte directory entries in the root directory (224 = 0x00E0) (512=0x0200)
#if defined(NANDFLASH)
      BFSABuffer.BFSA_fatBuff[0x11] = 0xE0; BFSABuffer.BFSA_fatBuff[0x12] = 0x01;
#else
      BFSABuffer.BFSA_fatBuff[0x11] = 0x00; BFSABuffer.BFSA_fatBuff[0x12] = 0x02;   // 15 Ko lost
      //BFSA_fatBuff[0x11] = 0xE0; BFSA_fatBuff[0x12] = 0x00; //  7 Ko lost, not format by windows
#endif

      // BPB_TotSec16: Number of sector (cluster) in volume
      BFSABuffer.BFSA_fatBuff[0x13] = (UCHAR) NB_BLOCK;
      BFSABuffer.BFSA_fatBuff[0x14] = ((UCHAR)(NB_BLOCK>>8));

      // BPB_Media 0xF0 for removable, 0xF8 for non removable media
      BFSABuffer.BFSA_fatBuff[0x15] = 0xF0;

      // BPB_FATSz16 This field is the FAT12/FAT16 16-bit count of sectors occupied by ONE FAT.
      // On FAT32 volumes this field must be 0, and BPB_FATSz32 contains the FAT size count.
#if defined(NANDFLASH)
      BFSABuffer.BFSA_fatBuff[0x16] = 0x10; BFSABuffer.BFSA_fatBuff[0x17] = 0x00;
#else
      BFSABuffer.BFSA_fatBuff[0x16] = 0x02; BFSABuffer.BFSA_fatBuff[0x17] = 0x00;  // 47 Ko more for 256Ko Flash
  #ifdef AT91SAM7S32
      BFSABuffer.BFSA_fatBuff[0x16] = 0x01; BFSABuffer.BFSA_fatBuff[0x17] = 0x00;  //  1 Ko more
  #endif
  #ifdef AT91SAM7S64
      BFSABuffer.BFSA_fatBuff[0x16] = 0x01; BFSABuffer.BFSA_fatBuff[0x17] = 0x00;  //  1 Ko more
  #endif
#endif
      BPB_FATSz16 = BFSABuffer.BFSA_fatBuff[0x16] + BFSABuffer.BFSA_fatBuff[0x17]*0x100;

      // BPB_SecPerTrk : Sectors per track for interrupt 0x13
      BFSABuffer.BFSA_fatBuff[0x18] = 0x01; BFSABuffer.BFSA_fatBuff[0x19] = 0x00;
      // BPB_NumHeads  : Number of heads for interrupt 0x13
      BFSABuffer.BFSA_fatBuff[0x1A] = 0x01; BFSABuffer.BFSA_fatBuff[0x1B] = 0x00;
      // BPB_HiddSec : 0x00 Count of hidden sectors preceding the partition that contains this FAT volume
      BFSABuffer.BFSA_fatBuff[0x1C] = 0x00; BFSABuffer.BFSA_fatBuff[0x1D] = 0x00; BFSABuffer.BFSA_fatBuff[0x1E] = 0x00; BFSABuffer.BFSA_fatBuff[0x1F] = 0x00;
      // BPB_TotSec32 : This field is the new 32-bit total count of sectors on the volume.
      BFSABuffer.BFSA_fatBuff[0x20] = 0x00; BFSABuffer.BFSA_fatBuff[0x21] = 0x00;
      BFSABuffer.BFSA_fatBuff[0x22] = 0x00; BFSABuffer.BFSA_fatBuff[0x23] = 0x00;
    // ************* END "BIOS Parameter Block" (BPB) *************

    // ************* "extended BIOS Parameter Block" (EBPB) *************
      BFSABuffer.BFSA_fatBuff[0x24] = 0x00; // BS_DrvNum : Int 0x13 drive number BootIndicator (0x00: floppy, 0x80: hard drive)
      BFSABuffer.BFSA_fatBuff[0x25] = 0x00; // BS_Reserved1: not used (only for winNT, control by chkdsk)
      BFSABuffer.BFSA_fatBuff[0x26] = 0x29; // BS_BootSig: Extended boot signature (0x29)
      // BFSA_fatBuff[0x27] = ; BFSA_fatBuff[0x28] = ; BFSA_fatBuff[0x29] = ; BFSA_fatBuff[0x2A] = ;  // BS_VolID: Volume serial number
      // BS_VolLab : Volume label. This field matches the 11-byte volume label recorded in the root directory.
      // The setting for this field when there is no volume label is the string "NO NAME    ".
      BFSABuffer.BFSA_fatBuff[0x2B] = 'N';
      BFSABuffer.BFSA_fatBuff[0x2C] = 'O';
      BFSABuffer.BFSA_fatBuff[0x2D] = ' ';
      BFSABuffer.BFSA_fatBuff[0x2E] = 'N';
      BFSABuffer.BFSA_fatBuff[0x2F] = 'A';
      BFSABuffer.BFSA_fatBuff[0x30] = 'M';
      BFSABuffer.BFSA_fatBuff[0x31] = 'E';
      BFSABuffer.BFSA_fatBuff[0x32] = 0x20;  // space
      BFSABuffer.BFSA_fatBuff[0x33] = 0x20;  // space
      BFSABuffer.BFSA_fatBuff[0x34] = 0x20;  // space
      BFSABuffer.BFSA_fatBuff[0x35] = 0x20;  // space
      // BS_FilSysType : One of the strings "FAT12   ", "FAT16   ", or "FAT     ".
      BFSABuffer.BFSA_fatBuff[0x36] = 'F';
      BFSABuffer.BFSA_fatBuff[0x37] = 'A';
      BFSABuffer.BFSA_fatBuff[0x38] = 'T';
      BFSABuffer.BFSA_fatBuff[0x39] = '1';
      BFSABuffer.BFSA_fatBuff[0x3A] = '2';
      BFSABuffer.BFSA_fatBuff[0x3B] = 0x20;  // space
      BFSABuffer.BFSA_fatBuff[0x3C] = 0x20;  // space
      BFSABuffer.BFSA_fatBuff[0x3D] = 0x20;  // space
    // ************* END "extended BIOS Parameter Block" (EBPB) *************

    // ************* Identification code : 0xAA55 = good partition *************
      BFSABuffer.BFSA_fatBuff[0x1FE] = 0x55;  // 0x1FE
      BFSABuffer.BFSA_fatBuff[0x1FF] = 0xAA;  // 0x1FF
    // ************* End Identification code *************

    // Write MBR
    if( TRUE == AT91F_Flash_Write_Address((unsigned int)FILE_MGR_BEGIN_ADDRESS+MBR_SIZE, MBR_SIZE/*sizeof(BFSA_fatBuff)*/, (unsigned char*)BFSABuffer.BFSA_fatBuff, FALSE ) )
    {
      // Erase only need HD Flash size
      if( TRUE == AT91F_Flash_Write_Address((unsigned int)FILE_MGR_BEGIN_ADDRESS+2*MBR_SIZE,
                                      (int)(BPB_FATSz16+BFSABuffer.BFSA_fatBuff[0x10])*SIZEONESECTOR+(BFSABuffer.BFSA_fatBuff[0x12]*0x100+BFSABuffer.BFSA_fatBuff[0x11])*32,
                                      (unsigned char*) &value, TRUE) )
      {
        // Write FAT
        // Set the FAT[0] to 0xFF0 and FAT[1] to 0xFFF
        // FF8-FFE   may be used to mark end of a file chain
        BFSABuffer.BFSA_fatBuff[0] = 0xF8;
        BFSABuffer.BFSA_fatBuff[1] = 0xFF;
        BFSABuffer.BFSA_fatBuff[2] = 0xFF;
        BFSABuffer.BFSA_fatBuff[3] = 0x00;
        if( TRUE == AT91F_Flash_Write_Address((unsigned int)FILE_MGR_BEGIN_ADDRESS+2*MBR_SIZE, 4, (unsigned char*)BFSABuffer.BFSA_fatBuff, FALSE) )
        {
          // Depend of the number and size of FAT  (4 if 2 sector/fat, 3 si 1 sector/fat)
          if( TRUE == AT91F_Flash_Write_Address((unsigned int)FILE_MGR_BEGIN_ADDRESS+(BPB_FATSz16+2)*MBR_SIZE, 4, (unsigned char*)BFSABuffer.BFSA_fatBuff, FALSE) )
          {
            // Label name of volume
            BFSABuffer.BFSA_fatBuff[0x00] = 'A';
            BFSABuffer.BFSA_fatBuff[0x01] = 'T';
            BFSABuffer.BFSA_fatBuff[0x02] = 'M';
            BFSABuffer.BFSA_fatBuff[0x03] = 'E';
            BFSABuffer.BFSA_fatBuff[0x04] = 'L';
            BFSABuffer.BFSA_fatBuff[0x05] = 0x20;
            BFSABuffer.BFSA_fatBuff[0x06] = 0x20;
            BFSABuffer.BFSA_fatBuff[0x07] = 0x20;
            BFSABuffer.BFSA_fatBuff[0x08] = 0x20;
            BFSABuffer.BFSA_fatBuff[0x09] = 0x20;
            BFSABuffer.BFSA_fatBuff[0x0A] = 0x20;
            BFSABuffer.BFSA_fatBuff[0x0B] = 0x08;
            BFSABuffer.BFSA_fatBuff[0x0C] = 0x00;
            BFSABuffer.BFSA_fatBuff[0x0D] = 0x00;
            if( TRUE == AT91F_Flash_Write_Address((unsigned int)FILE_MGR_BEGIN_ADDRESS+0x800, 14, (unsigned char*)BFSABuffer.BFSA_fatBuff, FALSE) )
            {
              // Format completed
              _status = BFSA_SUCCESS;
            }
          }
        }
      }
    }
  }
  po_unlock();
  return _status;
}


/*****************************************************************
*
*
* S U B - R O U T I N E  : BFSA_DIR
*
*-----------------------------------------------------------------
*
* Object :
*
*
* Argument :
*
* Return value :
*
*****************************************************************/
ULONG BFSA_dir( void )
{
  ULONG  _ulNbRead = 0xFF;
  int    a;
  ULONG  status = BAD_ARGUMENT;
  USHORT iFatEntry;
  ULONG  freeSpace = 0;

  status = BFSA_list(file_list,sizeof(file_list)/sizeof(file_list[0]),&_ulNbRead);
  if(!BFSA_FAILURE(status))
  {
    if (_ulNbRead)
    {
      ULONG _max = min(_ulNbRead,sizeof(file_list)/sizeof(file_list[0]));

      status = BFSA_path(CurrentPath);
      if (!BFSA_FAILURE(status))
      {
        int i;
        char compteur=0;

        TRACE_INFO( "Directory %s\n\r",CurrentPath);

        TRACE_INFO( " %i files in dir, pri",(int)_ulNbRead);
        TRACE_INFO( "nt %d firsts:\n\r",(char)_max);
        for (a=0; a<_max; a++)
        {
          TRACE_INFO( "   ");
          if( file_list[a].attributes == BFSA_ATTR_DIRECTORY )
          {
            TRACE_INFO( "<");
          }
          TRACE_INFO( "%s",file_list[a].fileName);
          for( i=0; i<8; i++)
            if( (UCHAR)file_list[a].fileName[i] == 0x00)
              compteur++;
          if (*file_list[a].extension!='\0')
          {
            TRACE_INFO( ".%s",file_list[a].extension);
            for( i=0; i<3; i++)
              if( (UCHAR)file_list[a].extension[i] == 0x00)
                compteur++;
          }
          else
          {
            compteur+=4;
          }
          if( file_list[a].attributes == BFSA_ATTR_DIRECTORY )
          {
            TRACE_INFO( ">");
          }
          while( compteur > 0 )
          {
            TRACE_INFO( " ");
            compteur--;
          }

⌨️ 快捷键说明

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