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

📄 mmc_drv.c

📁 TFT LCD(240*320
💻 C
字号:
/*
**********************************************************************
*                          Micrium, Inc.
*                      949 Crestview Circle
*                     Weston,  FL 33327-1848
*
*                            uC/FS
*
*             (c) Copyright 2001 - 2003, Micrium, Inc.
*                      All rights reserved.
*
***********************************************************************

----------------------------------------------------------------------
File        : mmc_drv.c
Purpose     : File system generic MMC/SD driver
---------------------------END-OF-HEADER------------------------------
*/

/*********************************************************************
*
*             #include Section
*
**********************************************************************
*/
#include "fs_api.h"
#include "fs_clib.h"
#include "fs_dev.h"

#if FS_USE_MMC_DRIVER
#include "mmc_X_hw.h"
#include "fs_lbl.h"

/*********************************************************************
*
*             Local Variables        
*
**********************************************************************
*/

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 */
/*********************************************************************
*
*             Local functions
*
**********************************************************************
*/

/*********************************************************************
*
*             _FS_MMC_DevStatus
*
  Description:
  FS driver function. Get status of the media.

  Parameters:
  Unit        - Unit number.
 
  Return value:
  ==1 (FS_LBL_MEDIACHANGED) - The media of the device has changed.
  ==0                       - Device okay and ready for operation.
  <0                        - An error has occured.
*/

static int _FS_MMC_DevStatus(FS_u32 Unit) {
    static int init;
    int x;
    char a;
    FS_MMC_HW_X_BusyLedOn(1);
    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_MMC_MAXUNIT; init++) {
           _FS_mmc_diskchange[init] = 1;
       }
       init = 1;
    }
    if (Unit >= FS_MMC_MAXUNIT) {
       return -1;  /* No valid unit number */
    }
    a=FS_MMC_HW_X_IsPresent(Unit);/* Check if a card is present */
    if (!a) {
      return -1;  /* No card in reader */
    }
    /* When you get here, then there is a card in the reader */
    a = _FS_mmc_diskchange[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.
       */
       _FS_mmc_diskchange[Unit] = 0;  /* Reset 'diskchange' flag */
       FS_MMC_Init(Unit);
       x = FS_MMC_Read(Unit, 0, (FS_u8*)&_FS_mmc_mbrbuffer[0]);
       if (x != 1) {
           return -1;
       }
       /* Calculate start sector of the first partition */
       _FS_mmc_logicalstart[Unit]=0;
       //_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;
    }
    FS_MMC_HW_X_BusyLedOff(1);
    return 0;
}
/*********************************************************************
*
*             _FS_MMC_DevRead
*
  Description:
  FS driver function. Read a sector from the media.

  Parameters:
  Unit        - Unit 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.
*/

static int _FS_MMC_DevRead(FS_u32 Unit, FS_u32 Sector, void *pBuffer){
  int x;
  if (Unit >= FS_MMC_MAXUNIT) {
     return -1;  /* No valid unit number */
  }
  FS_MMC_HW_X_BusyLedOn(1);
  x = FS_MMC_Read(Unit,Sector+_FS_mmc_logicalstart[Unit],(FS_u8*)pBuffer);
  if(x != 1) {
     x = -1;
  }    
  else x=0;
  FS_MMC_HW_X_BusyLedOff(1);
  return x;
}
/*********************************************************************
*
*             _FS_MMC_DevWrite
*
  Description:
  FS driver function. Write sector to the media.

  Parameters:
  Unit        - Unit 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 _FS_MMC_DevWrite(FS_u32 Unit, FS_u32 Sector, void *pBuffer){

  int x;
  
  if (Unit >= FS_MMC_MAXUNIT) {
    return -1;  /* No valid unit number */
  }
  FS_MMC_HW_X_BusyLedOn(1);
  x = FS_MMC_Write(Unit,Sector+_FS_mmc_logicalstart[Unit],(FS_u8*)pBuffer);
  if (x != 1) {
    x = -1;
  }
  else x=0;
  FS_MMC_HW_X_BusyLedOff(1);
  return x;
}
/*********************************************************************
*
*             _FS_MMC_DevIoCtl
*
  Description:
  FS driver function. Execute device command.

  Parameters:
  Unit        - Unit number.
  Cmd         - Command to be executed.
  Aux         - Parameter depending on command.
  pBuffer     - Pointer to a buffer used for the command.
 
  Return value:
  Command specific. In general a negative value means an error.
*/

static int _FS_MMC_DevIoCtl(FS_u32 Unit, FS_i32 Cmd, FS_i32 Aux, void *pBuffer){
   FS_u32 *info;
   int x;

   FS_MMC_HW_X_BusyLedOn(1);
   Aux = Aux;  /* Get rid of compiler warning */
   if (Unit >= FS_MMC_MAXUNIT) {
      return -1;  /* No valid unit number */
   }
   switch(Cmd){
     case FS_CMD_INC_BUSYCNT:
       _FS_mmc_busycnt[Unit]++;
       if(_FS_mmc_busycnt[Unit] > 0){
          FS_MMC_HW_X_BusyLedOn(Unit);
       }
       break;
     case FS_CMD_DEC_BUSYCNT:
       _FS_mmc_busycnt[Unit]--;
       if(_FS_mmc_busycnt[Unit] <= 0){
          _FS_mmc_busycnt[Unit] = 0;
          FS_MMC_HW_X_BusyLedOff(Unit);
       }
       break;
     case FS_CMD_CHK_DSKCHANGE:
       //a = FS_MMC_HW_X_DetectStatus(Unit);
       //if(a){
       //   _FS_mmc_diskchange[Unit] = 1;
       //}    
       break;
     case FS_CMD_GET_DEVINFO:
       if (!pBuffer) {
         return -1;
       }
       info = pBuffer;
       FS_MMC_Init(Unit);
       x = FS_MMC_Read(Unit,0,(FS_u8*)&_FS_mmc_mbrbuffer[0]);
       if (x != 1) {
          return -1;
       }
       /* hidden */
       *info = _FS_mmc_mbrbuffer[0x1c6];
       *info += (0x100UL * _FS_mmc_mbrbuffer[0x1c7]);
       *info += (0x10000UL * _FS_mmc_mbrbuffer[0x1c8]);
       *info += (0x1000000UL * _FS_mmc_mbrbuffer[0x1c9]);
       info++;
       /* head */
       *info = _FS_mmc_mbrbuffer[0x1c3]; 
       info++;
       /* sec per track */
       *info = _FS_mmc_mbrbuffer[0x1c4]; 
       info++;
       /* size */
       *info = _FS_mmc_mbrbuffer[0x1ca];
       *info += (0x100UL * _FS_mmc_mbrbuffer[0x1cb]);
       *info += (0x10000UL * _FS_mmc_mbrbuffer[0x1cc]);
       *info += (0x1000000UL * _FS_mmc_mbrbuffer[0x1cd]);
       break;
     default:
       break;
   }
   FS_MMC_HW_X_BusyLedOff(1);
   return 0; 
}
/*********************************************************************
*
*             Global variables
*
**********************************************************************
*/
const FS__device_type FS__mmcdevice_driver = {
  "MMC device",
  _FS_MMC_DevStatus,
  _FS_MMC_DevRead,
  _FS_MMC_DevWrite,
  _FS_MMC_DevIoCtl
};

#endif /* FS_USE_MMC_DRIVER */

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

⌨️ 快捷键说明

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