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

📄 mmc_drv.c

📁 AT91SAM7X256微处理器
💻 C
字号:

/***********************************************************************
*                          Micrium, Inc.
*                      949 Crestview Circle
*                     Weston,  FL 33327-1848
*
*                            uC/FS
*
*             (c) Copyright 2001 - 2006, Micrium, Inc.
*                      All rights reserved.
*
***********************************************************************

----------------------------------------------------------------------
File        : generic_drv.c
Purpose     : Skeleton device driver
----------------------------------------------------------------------

This is an example driver that should help implementing any kind of
data storage devices into the filesystem. The device you may use does
not need to provide any special functions other than reading data from
a given sector address.

---------------------------END-OF-HEADER------------------------------
*/
//由于ucfs 没有 mmc和 sdc的驱动文件。所以,本驱动主要参照教学版示例
//(可在ucfs官方网站申请,无源码,仅有lib库)generic_drv.c和IDE驱动改写而来
//其底层接口祥见文件 mmc.c和 mmc.h 。可用于mmc和 sdc

/*
********************************************************************
*
*       #include Section
*
**********************************************************************
*/

#include "fs_dev.h"
#include "fs_lbl.h"
//#include "FS_ConfDefaults.h"        /* FS Configuration */
#include "fs_port.h"
#include <includes.h>

/*********************************************************************
*  Wether this driver is being compiled into the FS or not can be
*  switched on/off in the global configuration file "fs_conf.h".
*  Refer the corresponding #define in the section "Device Driver Support"
*/

//#if FS_USE_GENDEV_DRIVER /* only compiled if needed */
#if  FS_USE_MMC_DRIVER      //because no mmc driver ,so modify it

#include "fs_api.h"   /* Standard includes that always must be included. */
#include "fs_clib.h"
#include "includes.h"
#include "mmc.h"        //包含读写mmc_sdc的基本函数库


unsigned char *mmc_buff ;

/*********************************************************************
*
*       #defines configurable
*
**********************************************************************/

static FS_u32   _FS_mmc_logicalstart[FS_MMC_MAXUNIT];     /* start of partition */
static char     _FS_mmc_mbrbuffer[0x200];                 /* buffer for reading MBR */
static char     _FS_mmc_diskchange[FS_MMC_MAXUNIT];       /* signal flag for driver */
static char     _FS_mmc_busycnt[FS_MMC_MAXUNIT];          /* counter for BSY LED on/off */
/*********************************************************************
*
*  Sets the maximum number of devices that this driver can address.
*/
#define FS_GENDEV_MAX_DEVICE 1

/*********************************************************************
*  If the media size always remains the same, you can define a
*  constant size here.
*/
#define FS_GENDEV_NUMSEC            250000  //

/*********************************************************************
*
*  If the bytes per sector size does not change, you can define a
*  constant size here.
*/
#define FS_GENDEV_BYTESPERSECTOR     512

/*********************************************************************
*
*       Static data
*
**********************************************************************
*/

/*********************************************************************
*  To make sure you initialize the device only once, use a static
*  variable for that purpose.
*/
static int _GenDev_Status[FS_GENDEV_MAX_DEVICE];

/*********************************************************************
*
*       Static code
*
**********************************************************************
*/

/*********************************************************************
*
*       _GENDEV_DevStatus
*
*  Description:
*    FS driver function. Gets status of the device. This function is also
*    used to initialize the device and to detect a media change.
*
*  Parameters:
*    Unit                  - Device number.
*
*  Return value:
*    ==1 (FS_LBL_MEDIA_CHANGED) - The media of the device has changed.
*    ==0                       - Device okay and ready for operation.
*  <0                          - An error has occured.
*/

static int _GENDEV_DevStatus(FS_u32 Unit)
{
 // return _GenDev_Status[Unit];
  static int init;
  int x;
  char a;

  if (!init) {
    /*
       The function is called the first time. For each unit,
       the flag for 'diskchange' is set. That makes sure, that
       FS_LBL_MEDIACHANGED is returned, if there is already a
       media in the reader.
    */
    for (init = 0; init <FS_GENDEV_MAX_DEVICE; init++) {
      _GenDev_Status[init] = 1;
    }
    init = 1;
  }
  if (Unit >= FS_GENDEV_MAX_DEVICE)
   {
    return -1;  /* No valid unit number */
   }
  a = disk_initialize();  //初始化一张卡 ,
  if (a!=0x0)            //如果不能初始化一张卡
   {
    return -1;            /* No card in reader */
   }
  /* When you get here, then there is a card in the reader */
  a = _GenDev_Status[Unit];  /* Check if the media has changed */
  if (a){
    /*
       A diskchange took place. The following code reads the MBR of the
       card to get its partition information.
    */
    _GenDev_Status[Unit] = 0;  /* Reset 'diskchange' flag */
    disk_initialize();         //由于工作再单卡状态,其实不用选择

    x = disk_read
       (
	mmc_buff,		/* Data buffer to store read data */
	0,		        /* Sector number (LBA) */
	1			/* Sector count (1..255) */
       )   ;
    if (x != 0)
     {
      return -1;
     }
    /* Calculate start sector of the first partition */
    _FS_mmc_logicalstart[Unit]  = _FS_mmc_mbrbuffer[0x1c6];
    _FS_mmc_logicalstart[Unit] += (0x100UL * _FS_mmc_mbrbuffer[0x1c7]);
    _FS_mmc_logicalstart[Unit] += (0x10000UL * _FS_mmc_mbrbuffer[0x1c8]);
    _FS_mmc_logicalstart[Unit] += (0x1000000UL * _FS_mmc_mbrbuffer[0x1c9]);
    return FS_LBL_MEDIACHANGED;
  }
  return 0;
}

/*********************************************************************
*
*       _GENDEV_DevRead
*
*  Description:
*    FS driver function. Read a sector from the medium.
*
*  Parameters:
*    Unit            - Device number.
*    Sector          - Sector to be read from the device.
*    pBuffer         - Pointer to buffer for storing the data.
*
*    Return value:
*    ==0             - Sector has been read and copied to pBuffer.
*    <0              - An error has occured.
*/
//here ,i see the parameter that unit as number of the read sectors

static int _GENDEV_DevRead(FS_u32 Unit, FS_u32 Sector, void *pBuffer)
 {
//    FS_USE_PARA(Unit);
//    FS_USE_PARA(pBuffer);
    /*
     *  The next test checks if the given sector address is within a valid range.
     *  Depending on your device, you can set a constant value or calculate
     *  this value from media descriptor of your hardware.
     */
    if (Sector >= FS_GENDEV_NUMSEC)
     {
      return -1;  /* Out of sector range */
     }
    /*
     *  Place the code that reads from your media here. The number of bytes
     *  that has to be read equal to the sector size. This value is defined
     *  as FS_GENDEV_BYTESPERSECTOR. Below you can see the parameter that are used to
     *  transfer a sector from the file system to the storage device.

       Sector                     - Sector Number.
       *pBuffer                   - Pointer to the buffer were sector data is stored.
       FS_GENDEV_BYTESPERSECTOR   - Number of bytes that must be transferred.

    */
   // return (disk_read(pBuffer,Sector,Unit)) ;//读数据正确返回 0 ,1,2,3,4为各种错误
    return (disk_read(pBuffer,Sector,1)) ;
}

/*********************************************************************
*
*       _GENDEV_DevWrite
*
*  Description:
*  FS driver function. Write sector to the RAM disk.
*
*  Parameters:
*    Unit        - Device number.
*    Sector          - Sector to be written to the device.
*    pBuffer         - Pointer to data to be stored.
*
*  Return value:
*    ==0             - Sector has been written to the device.
*    <0              - An error has occured.
*/
static int _GENDEV_DevWrite(FS_u32 Unit, FS_u32 Sector, void *pBuffer) {
 // FS_USE_PARA(Unit);
 // FS_USE_PARA(pBuffer);
  /*
     The next test checks if the given sector address is within a valid range.
     Depending on your device, you can set a constant value or calculate
     this value from media descriptor of your hardware.
  */
  if (Sector >= FS_GENDEV_NUMSEC) {
    return -1;  /* Out of sector range */
  }

  /*
     Place the code that writes data onto your media here. The number of bytes
     that has to be read equal to the sector size. This value is defined
     as FS_SEC_SIZE. Below you can see the parameter that are used to
     transfer a sector from the storage device to the file system.

     Sector        - Sector Number.
     *pBuffer      - Pointer to the buffer were sector data is stored.
     FS_SEC_SIZE   - Number of bytes that must be transferred.

  */
  // return value : 0 : ok ;   1,2,3,4 : kinds of errors
  return (disk_write((unsigned char*)pBuffer,Sector,1))  ;
  /* If you would like to use a read only device, just return here with zero. */
}


/*********************************************************************
*
*             _GENDEV_DevIoCtl
*
*  Description:
*    FS driver function. Execute device command.
*
*  Parameters:
*    Unit    - Device number.
*    Cmd         - Command to be executed.
*    Aux         - Parameter depending on command.
*    pBuffer     - Pointer to a buffer used for the command.
*
*  Return value:
*    This function is used to execute device specific commands.
*    In the file fs_api.h you can find I/O commands that are currently
*    implemented. If the higher layers don't know the command, they
*    send it to the next lower. Your driver does not have to
*    implement one of these commands. Only if automatic formatting
*    is used or user routines need to get the size of the medium,
*    FS_CMD_GET_DEVINFO must be implemented.
*/
static int _GENDEV_DevIoCtl(FS_u32 Unit, FS_i32 Cmd, FS_i32 Aux, void *pBuffer) {
 // FS__device_type *pInfo;

  /*
    #define GET_SECTORS		1
    #define PUT_IDLE_STATE	2
    #define MMC_GET_CSD		10
    #define MMC_GET_CID		11
    #define MMC_GET_OCR		12
    #define ATA_GET_REV		20
    #define ATA_GET_MODEL	21
    #define ATA_GET_SN		22
  */
/*  if(disk_ioctl ( Cmd,pBuffer )!=0 )     //执行操作SD卡的命令(命令类型不同,一般用不上)
   {
     pBuffer='X' ;
     pBuffer='0' ;
   }
  */
 // FS_DISKFREE_T ********************** pInfo maybe are the type of structure
 //  FS_USE_PARA(Unit);
  /*
     FS_DEV_INFO is a simple structure defined in fs_dev.h that is filled with
     the geometrical values of the device.
  */
//  FS_USE_PARA(Aux);
  /* This check makes sure, the device number is within valid range */



  switch (Cmd) { /* Filter out the command that you want to response to */
  case FS_CMD_GET_DEVINFO:
    if (pBuffer == 0) { /* avoid writing into non existing buffers */
      return -1;
    }
    //pInfo = (FS__device_type*)pBuffer; /* The parameter pBuffer contains the pointer to the structure */
    /*
       The number of hidden sectors is used the reserve a certain amount of sectors for
       operating system specific data and the partition table. If you do not need or
       have a partition table or special system boot code on the drive, the number
       of hidden sectors is zero.
    */
   // pInfo->NumHeads        = 0;  /* Relevant only for mechanical drives   */
   // pInfo->SectorsPerTrack = 2;  /* Relevant only for mechanical drives   */
//    pInfo->NumSectors      = FS_GENDEV_NUMSEC; /* Number of total sectors  */
//    pInfo->BytesPerSector  = FS_GENDEV_BYTESPERSECTOR;
    /* Now all necessary information is complete. */
    break;
  }
  return 0;  /* Return with zero if no problems have occurred. */
}


/*********************************************************************
*
*       _GENDEV_InitDevice
*
*  Description:
*  Initializes the device drivers internal variables and hardware.
*
*  Parameters:
*    Unit        - Device number.
*
*  Return value:
*    ==0             - Sector has been written to the device.
*    <0              - An error has occured.
*/
static int _GENDEV_InitDevice(FS_u32 Unit) {
  int i;
//  FS_USE_PARA(Unit);
  for (i = 0; i < FS_GENDEV_MAX_DEVICE; i++) {
    _GenDev_Status[Unit] = FS_MEDIA_ISNOTPRESENT;
  }
  return 0;
}

/*********************************************************************
*
*       _GENDEV_InitMedium
*
*  Description:
*  Initializes the medium we want access to, if necessary.
*
*  Parameters:
*    Unit        - Device number.
*
*  Return value:
*    None.
*/
static int _GENDEV_InitMedium(FS_u32 Unit) {
  _GenDev_Status[Unit] = FS_MEDIA_ISPRESENT;
  return 0 ;
}

/*********************************************************************
*
*       Public data
*
**********************************************************************
*/

/* This structure contains pointers to the functions that all drivers
   of the file system must provide. */
const FS__device_type FS__mmcdevice_driver=
 {
  "mmc",
  _GENDEV_DevStatus,//_GENDEV_InitDevice,//_GENDEV_InitMedium,
  _GENDEV_DevRead,
  _GENDEV_DevWrite,
  _GENDEV_DevIoCtl,
};



#endif /* FS_USE_MMC_DRIVER */

/*************************** End of file ****************************/

⌨️ 快捷键说明

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