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

📄 mmc_x_hw.c

📁 嵌入式文件系统 ucfs.包含全部源代码
💻 C
字号:
/*
**********************************************************************
*                          Micrium, Inc.
*                      949 Crestview Circle
*                     Weston,  FL 33327-1848
*
*                            uC/FS
*
*             (c) Copyright 2001 - 2003, Micrium, Inc.
*                      All rights reserved.
*
***********************************************************************

----------------------------------------------------------------------
File        : mmc_x_hw.c
Purpose     : Sample MMC hardware layer for accessing MMC/SD 
              via port banging.
---------------------------END-OF-HEADER------------------------------
*/

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

#include "fs_api.h"
#include "fs_conf.h"

#if FS_USE_MMC_DRIVER

#include "mmc.h"
#include "mmc_x_hw.h"


/*********************************************************************
*
*             #define Macros
*
**********************************************************************
*/

/*********************************************************************
*
*       Configurable macros
*
*   Please setup these macros according your hardware
*
*/

#define SPI_CS_PORT            P1 
#define SPI_CLK_PORT           P2
#define SPI_DATAOUT_PORT       P2
#define SPI_DATAIN_PORT        P4

#define SPI_CS_PIN            1 
#define SPI_CLK_PIN           3
#define SPI_DATAOUT_PIN       7
#define SPI_DATAIN_PIN        2



/*********************************************************************
*
*             #define Macros
*
*/
#define SPI_CLR_CS()          SPI_CS_PORT      &= ~(1 << SPI_CS_PIN)
#define SPI_SET_CS()          SPI_CS_PORT      |=  (1 << SPI_CS_PIN)
#define SPI_CLR_CLK()         SPI_CLK_PORT     &= ~(1 << SPI_CLK_PIN)
#define SPI_SET_CLK()         SPI_CLK_PORT     |=  (1 << SPI_CLK_PIN)
#define SPI_CLR_DATAOUT()     SPI_DATAOUT_PORT &= ~(1 << SPI_DATAOUT_PIN)
#define SPI_SET_DATAOUT()     SPI_DATAOUT_PORT |=  (1 << SPI_DATAOUT_PIN)
#define SPI_DATAIN()          (SPI_DATAIN_PORT &   (1 << SPI_DATAIN_PIN))
#define SPI_DELAY()           { int i=10L; while(i-- != 0); }
#define SPI_SETUP_PINS()


#define MMC_DEFAULTSUPPLYVOLTAGE  3300 /* in mV, example means 3.3V */
#define MMC_MAXFREQUENCY           400 /* 400 KHz                   */

/*********************************************************************
*
*       Static data
*
**********************************************************************
*/
static volatile int _MaxFreq;


/*********************************************************************
*
*             Local functions
*
**********************************************************************
*/


static void _Init(void) {
  _MaxFreq = MMC_MAXFREQUENCY;
  SPI_SETUP_PINS();
}

/*********************************************************************
*
*             FS_MMC_HW_X_BusyLedOff
*
*  Description:
*    FS low level function. Switches the busy LED off.
*
*  Parameters:
*    Unit      - Device Index
*
*  Return value:
*    void
*/
void FS_MMC_HW_X_BusyLedOff (FS_U8 Unit) {
}

/*********************************************************************
*
*             FS_MMC_HW_X_BusyLedOn
*
*  Description:
*    FS low level function. Switches the busy LED off.
*
*  Parameters:
*    Unit      - Device Index
*
*  Return value:
*    void
*/

void FS_MMC_HW_X_BusyLedOn(FS_U8 Unit) {
}

/*********************************************************************
*
*             FS_MMC_HW_X_EnableCS
*
*  Description:
*    FS low level function. Sets the card slot active using the
*    chip select (CS) line.
*
*  Parameters:
*    Unit      - Device Index
*
*  Return value:
*    void
*/

void FS_MMC_HW_X_EnableCS   (FS_U8 Unit) {
  SPI_CLR_CS();
}

/*********************************************************************
*
*             FS_MMC_HW_X_EnableCS
*
*  Description:
*    FS low level function. Sets the card slot inactive using the
*    chip select (CS) line.
*
*  Parameters:
*    Unit      - Device Index
*
*  Return value:
*    void
*/

void FS_MMC_HW_X_DisableCS(FS_U8 Unit) {
  SPI_SET_CS();
}

/*********************************************************************
*
*             FS_MMC_HW_X_IsWriteProtected
*
*  Description:
*    FS low level function. Returns the state of the physical write
*    protection of the SD cards.
*
*  Parameters:
*    Unit      - Device Index
*
*  Return value:
*    1                - the card is write protected
*    ==0              - the card is not write protected
*/

char FS_MMC_HW_X_IsWriteProtected(FS_U8 Unit) {
  /* If the card slot has no write switch detector, return 0 */
  return 0;
}

/*********************************************************************
*
*             FS_MMC_HW_X_SetMaxSpeed
*
*  Description:
*    FS low level function. Sets the SPI interface to a maximum frequency.
*    Make sure that you set the frequency lower or equal but never higher
*    than the given value. Recommended startup frequency is 100kHz - 400kHz.
*
*  Parameters:
*    Unit       - Device Index
*    MaxFreq           - SPI clock frequency in kHz
*
*  Return value:
*    max. frequency    - the maximum frequency set in kHz
*    ==0               - the frequency could not be set
*/

FS_U16 FS_MMC_HW_X_SetMaxSpeed(FS_U8 Unit, FS_U16 MaxFreq) {
  _Init();
  return MMC_MAXFREQUENCY;    /* We are not faster than this */
}

/*********************************************************************
*
*             FS_MMC_HW_X_SetVoltage
*
*  Description:
*    FS low level function. Be sure that your card slot si within the given
*    voltage range. Return 1 if your slot can support the required voltage,
*    and if not, return 0;
*
*  Parameters:
*    Unit      - Device Index
*    MaxFreq          - SPI clock frequency in kHz
*
*  Return value:
*    1                - the card slot supports the voltage range
*    ==0              - the card slot does not support the voltage range
*/

char FS_MMC_HW_X_SetVoltage(FS_U8 Unit, FS_U16 Vmin, FS_U16 Vmax) {
  /* voltage range check */
  char r;
  if((Vmin <= MMC_DEFAULTSUPPLYVOLTAGE) && (Vmax >= MMC_DEFAULTSUPPLYVOLTAGE)) {
    r = 1;
  } else {
    r = 0;
  }
  return r;
}

/*********************************************************************
*
*             FS_MMC_HW_X_IsPresent
*
*  Description:
*    Returns the state of the media. If you do not know the state, return
*    FS_MEDIA_STATEUNKNOWN and the higher layer will try to figure out if
*    a media is present.
*
*  Parameters:
*    Unit                 - Device Index
*
*  Return value:
*    FS_MEDIA_STATEUNKNOWN       - the state of the media is unkown
*    FS_MEDIA_ISNOTPRESENT       - no card is present
*    FS_MEDIA_ISPRESENT          - a card is present
*/

char FS_MMC_HW_X_IsPresent(FS_U8 Unit) {
  return FS_MEDIA_STATEUNKNOWN;
}

/*********************************************************************
*
*             FS_MMC_HW_X_Read
*
*  Description:
*    FS low level function. Reads a specified number of bytes from MMC
*    card to buffer.
*
*  Parameters:
*    Unit      - Device Index
*    pData            - Pointer to a data buffer
*    NumBytes         - Number of bytes
*
*  Return value:
*    void
*/

void FS_MMC_HW_X_Read (FS_U8 Unit, FS_U8 * pData, int NumBytes) {
  FS_U8 bpos;
  FS_U8 c;
  SPI_SET_DATAOUT();
  if (_MaxFreq < 100) {
    /* Slow version */
    do {
      c = 0;
      bpos = 8; /* get 8 bits */
      do {
        SPI_CLR_CLK();
        SPI_DELAY();
        c <<= 1;
        if (SPI_DATAIN()) {
          c |= 1;
        }
        SPI_SET_CLK();
        SPI_DELAY();
      } while (--bpos);
      *pData++ = c;
    } while (--NumBytes);
  } else {
    /* Faster version */
    do {
      c = 0;
      bpos = 8; /* get 8 bits */
      do {
        SPI_CLR_CLK();
        c <<= 1;
        if (SPI_DATAIN()) {
          c |= 1;
        }
        SPI_SET_CLK();
      } while (--bpos);
      *pData++ = c;
    } while (--NumBytes);
  }
}

/*********************************************************************
*
*             FS_MMC_HW_X_Write
*
*  Description:
*    FS low level function. Writes a specified number of bytes from
*    data buffer to the MMC/SD card.
*
*  Parameters:
*    Unit      - Device Index
*    pData            - Pointer to a data buffer
*    NumBytes         - Number of bytes
*
*  Return value:
*    void
*/

void FS_MMC_HW_X_Write(FS_U8 Unit, const FS_U8 * pData, int NumBytes) {
  int i;
  FS_U8 mask;
  FS_U8 data;
  for (i = 0; i < NumBytes; i++) {
    data = pData[i];
    mask = 0x80;
    while (mask) {
      if (data & mask) {
        SPI_SET_DATAOUT();
      } else {
        SPI_CLR_DATAOUT();
      }
      SPI_CLR_CLK();
      SPI_DELAY();
      SPI_SET_CLK();
      SPI_DELAY();
      mask >>= 1;
    }
  }
  SPI_SET_DATAOUT(); /* default state of data line is high */
}

#endif /* FS_USE_MMC_DRIVER */

⌨️ 快捷键说明

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