📄 mmc_spi_logicpd_lh79520.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 LogicPD
---------------------------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"
#include "iolh79520.h"
/*********************************************************************
*
* #define constants
*
**********************************************************************
*/
#define HW_USE_SPI 1
#define FS__MMC_DEFAULTSUPPLYVOLTAGE 3300 /* in mV, example means 3.3V */
#define SIO_FREQ 12600 /* KHz, SysFreq/4 */
/*********************************************************************
*
* #define Macros
*
**********************************************************************
*/
#define SPI_CLR_CS() GPIOPADR &= ~(1 << 2)
#define SPI_SET_CS() GPIOPADR |= (1 << 2)
/*********************************************************************
*
* Static data
*
**********************************************************************
*/
static volatile char _Dummy;
/*********************************************************************
*
* Local functions
*
**********************************************************************
*/
static void _Init(void) {
#ifdef HW_USE_SPI
IOSSPMUX |= 0x8; // Set pins as SPI
GPIOPADDR = (1 << 2) /* PA2 output */
| (1 << 1); /* PA1 output */
PERIPHCLKCTRL2 &= ~(1<<1); // enable clock for SPI
SSPCLKPRESCALE = 1; // prescaler /2 for SPI
SSCPSR = 2; // again prescaler /2 for SPI
SSPCR0 = (1 << 8) // Clock divisor
|(1 << 7) // Clock phase ... 1: SSPFRM = 0
|(1 << 6) // Polarity
|(0 << 5) // Frame format
|(0 << 4) // Frame format
|(7 << 0); // 8 bit data
SSPCR1 = (1 << 4) // enable SPI
|(0 << 3) // loopback mode
|(0 << 2) // disable receive FIFO overrun interrupt
|(0 << 1) // disable Transmit FIFO interrupt
|(0 << 1); // disable Receive FIFO interrupt
_Dummy = SSPDR;
_Dummy = SSPDR;
_Dummy = SSPDR;
_Dummy = SSPDR;
_Dummy = SSPDR;
}
/*********************************************************************
*
* FS_MMC_HW_X_BusyLedOff
*
* Description:
* FS low level function. Switches the busy LED off.
*
* Parameters:
* Unit - Unit number
*
* Return value:
* void
*/
void FS_MMC_HW_X_BusyLedOff (FS_U8 Unit) {
*((volatile unsigned char*)(0x55600000)) |= (1 << 0);
}
/*********************************************************************
*
* FS_MMC_HW_X_BusyLedOn
*
* Description:
* FS low level function. Switches the busy LED off.
*
* Parameters:
* Unit - Unit number
*
* Return value:
* void
*/
void FS_MMC_HW_X_BusyLedOn (FS_U8 Unit) {
*((volatile unsigned char*)(0x55600000)) &= ~(1 << 0);
}
/*********************************************************************
*
* FS_MMC_HW_X_EnableCS
*
* Description:
* FS low level function. Sets the card slot active using the
* chip select (CS) line.
*
* Parameters:
* Unit - Unit number
*
* 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 - Unit number
*
* 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 - Unit number
*
* 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 - Unit number
* 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) {
int Divider;
_Init();
SSPCR1 &= ~(1 << 4); // Disable SPI
Divider = SIO_FREQ / MaxFreq;
if (Divider > 255) {
Divider = 255;
}
SSPCR0 = (SSPCR0 & 0xff) | (Divider << 8);
SSPCR1 |= (1 << 4); // Enable SPI
return SIO_FREQ / (Divider + 1); /* 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 - Unit number
* 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) {
char r;
/* voltage range check */
if((Vmin <= FS__MMC_DEFAULTSUPPLYVOLTAGE) && (Vmax >= FS__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 - Unit number
*
* 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 - Unit number
* pData - Pointer to a data buffer
* NumBytes - Number of bytes
*
* Return value:
* void
*/
void FS_MMC_HW_X_Read (FS_U8 DevIndex, FS_U8 * pData, int NumBytes) {
#if 1
SSPDR = 0xff;
if (--NumBytes) {
do {
SSPDR = 0xff;
while ((SSPSR & (1 << 2)) == 0); /* Wait until we received a byte */
*pData++ = SSPDR;
} while (--NumBytes);
}
while ((SSPSR & (1 << 2)) == 0); /* Wait until we received a byte */
*pData = SSPDR;
#else
do {
SSPDR = 0xff;
while ((SSPSR & (1 << 2)) == 0); /* Wait until we received a byte */
*pData++ = SSPDR;
} while (--NumBytes);
#endif
}
/*********************************************************************
*
* 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 - Unit number
* 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) {
while ((SSPSR & (1 << 4)) != 0); /* Busy ? */
do {
SSPDR = *pData++;
while ((SSPSR & (1 << 2)) == 0); /* Wait until we received a byte */
_Dummy = SSPDR;
} while (--NumBytes);
}
#endif /* FS_USE_MMC_DRIVER */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -