?? mmc.c
字號:
/*-----------------------------------------------------------------------*/
/* MMC/SD (in SPI mode) control module (C)ChaN, 2007 */
/*-----------------------------------------------------------------------*/
/* Only rcvr_spi(), xmit_spi(), disk_timerproc() and some macros are */
/* platform dependent. */
/*-----------------------------------------------------------------------*/
#include <machine.h>
#include "iodefine.h"
#include "diskio.h"
/* MMC/SD command (in SPI) */
#define CMD0 (0x40+0) /* GO_IDLE_STATE */
#define CMD1 (0x40+1) /* SEND_OP_COND */
#define CMD8 (0x40+8) /* SEND_IF_COND */
#define CMD9 (0x40+9) /* SEND_CSD */
#define CMD10 (0x40+10) /* SEND_CID */
#define CMD12 (0x40+12) /* STOP_TRANSMISSION */
#define CMD16 (0x40+16) /* SET_BLOCKLEN */
#define CMD17 (0x40+17) /* READ_SINGLE_BLOCK */
#define CMD18 (0x40+18) /* READ_MULTIPLE_BLOCK */
#define CMD23 (0x40+23) /* SET_BLOCK_COUNT */
#define CMD24 (0x40+24) /* WRITE_BLOCK */
#define CMD25 (0x40+25) /* WRITE_MULTIPLE_BLOCK */
#define CMD41 (0x40+41) /* SEND_OP_COND (ACMD) */
#define CMD55 (0x40+55) /* APP_CMD */
#define CMD58 (0x40+58) /* READ_OCR */
/* Control signals (Platform dependent) */
#define MMCLK IO.PDR5.BIT.B0 /* MMC SCLK */
#define MMDI IO.PDR5.BIT.B1 /* MMC DI */
#define MMDO IO.PDR5.BIT.B2 /* MMC DO */
#define MMCS IO.PDR5.BIT.B3 /* MMC CS */
#define SELECT() MMCS = 0 /* MMC CS = L */
#define DESELECT() MMCS = 1 /* MMC CS = H */
#define SOCKPORT IO.PDR5.BYTE /* Socket control port */
#define SOCKWP 0x10 /* Write protect switch (PB5) */
#define SOCKINS 0x20 /* Card detect switch (PB4) */
#define CARDPWR 0x80 /* Card power (PE7) */
/*--------------------------------------------------------------------------
Module Private Functions
---------------------------------------------------------------------------*/
static volatile
DSTATUS Stat = STA_NOINIT; /* Disk status */
static volatile
BYTE Timer1, Timer2; /* 100Hz decrement timer */
static
BYTE CardType; /* b0:MMC, b1:SDC, b2:Block addressing */
/*-----------------------------------------------------------------------*/
/* Transmit a byte to MMC via SPI (Platform dependent) */
/*-----------------------------------------------------------------------*/
static
void xmit_spi (BYTE dat)
{
MMDI = (dat & 0x80) ? 1 : 0; dat <<= 1;
MMCLK = 1; MMCLK = 0;
MMDI = (dat & 0x80) ? 1 : 0; dat <<= 1;
MMCLK = 1; MMCLK = 0;
MMDI = (dat & 0x80) ? 1 : 0; dat <<= 1;
MMCLK = 1; MMCLK = 0;
MMDI = (dat & 0x80) ? 1 : 0; dat <<= 1;
MMCLK = 1; MMCLK = 0;
MMDI = (dat & 0x80) ? 1 : 0; dat <<= 1;
MMCLK = 1; MMCLK = 0;
MMDI = (dat & 0x80) ? 1 : 0; dat <<= 1;
MMCLK = 1; MMCLK = 0;
MMDI = (dat & 0x80) ? 1 : 0; dat <<= 1;
MMCLK = 1; MMCLK = 0;
MMDI = (dat & 0x80) ? 1 : 0;
MMCLK = 1; MMCLK = 0;
}
/*-----------------------------------------------------------------------*/
/* Receive a byte from MMC via SPI (Platform dependent) */
/*-----------------------------------------------------------------------*/
static
BYTE rcvr_spi (void)
{
BYTE r;
MMDI = 1;
r = 0; if (MMDO) r++;
MMCLK = 1; MMCLK = 0;
r <<= 1; if (MMDO) r++;
MMCLK = 1; MMCLK = 0;
r <<= 1; if (MMDO) r++;
MMCLK = 1; MMCLK = 0;
r <<= 1; if (MMDO) r++;
MMCLK = 1; MMCLK = 0;
r <<= 1; if (MMDO) r++;
MMCLK = 1; MMCLK = 0;
r <<= 1; if (MMDO) r++;
MMCLK = 1; MMCLK = 0;
r <<= 1; if (MMDO) r++;
MMCLK = 1; MMCLK = 0;
r <<= 1; if (MMDO) r++;
MMCLK = 1; MMCLK = 0;
return r;
}
/*-----------------------------------------------------------------------*/
/* Wait for card ready */
/*-----------------------------------------------------------------------*/
static
BYTE wait_ready (void)
{
BYTE res;
Timer2 = 50; /* Wait for ready in timeout of 500ms */
rcvr_spi();
do
res = rcvr_spi();
while ((res != 0xFF) && Timer2);
return res;
}
/*-----------------------------------------------------------------------*/
/* Power Control (Platform dependent) */
/*-----------------------------------------------------------------------*/
/* When the target system does not support socket power control, there */
/* is nothing to do in these functions and chk_power always returns 1. */
static
void power_on (void)
{
IO.PUCR5.BYTE = 0x30; /* Enable pull-up for socket contacts */
IO.PDR5.BYTE = 0x00; /* Power ON */
IO.PCR5 = 0x80;
for (Timer1 = 2; Timer1; ); /* Wait for 20ms */
IO.PDR5.BYTE = 0x0E; /* Enable Driver */
IO.PCR5 = 0x8B;
for (Timer1 = 1; Timer1; ); /* Wait for 10ms */
}
static
void power_off (void)
{
SELECT(); /* Wait for card ready */
wait_ready();
DESELECT();
rcvr_spi();
IO.PDR5.BYTE = 0x00; /* Disable driver */
IO.PCR5 = 0x80;
IO.PDR5.BYTE = 0x80; /* Power OFF */
Stat |= STA_NOINIT; /* Set STA_NOINIT */
}
/*-----------------------------------------------------------------------*/
/* Receive a data packet from MMC */
/*-----------------------------------------------------------------------*/
static
BOOL rcvr_datablock (
BYTE *buff, /* Data buffer to store received data */
UINT btr /* Byte count (must be even number) */
)
{
BYTE token;
Timer1 = 10;
do { /* Wait for data packet in timeout of 100ms */
token = rcvr_spi();
} while ((token == 0xFF) && Timer1);
if(token != 0xFE) return FALSE; /* If not valid data token, retutn with error */
do { /* Receive the data block into buffer */
*buff++ = rcvr_spi();
*buff++ = rcvr_spi();
} while (btr -= 2);
rcvr_spi(); /* Discard CRC */
rcvr_spi();
return TRUE; /* Return with success */
}
/*-----------------------------------------------------------------------*/
/* Send a data packet to MMC */
/*-----------------------------------------------------------------------*/
#if _READONLY == 0
static
BOOL xmit_datablock (
const BYTE *buff, /* 512 byte data block to be transmitted */
BYTE token /* Data/Stop token */
)
{
BYTE resp, wc;
if (wait_ready() != 0xFF) return FALSE;
xmit_spi(token); /* Xmit data token */
if (token != 0xFD) { /* Is data token */
wc = 0;
do { /* Xmit the 512 byte data block to MMC */
xmit_spi(*buff++);
xmit_spi(*buff++);
} while (--wc);
xmit_spi(0xFF); /* CRC (Dummy) */
xmit_spi(0xFF);
resp = rcvr_spi(); /* Reveive data response */
if ((resp & 0x1F) != 0x05) /* If not accepted, return with error */
return FALSE;
}
return TRUE;
}
#endif /* _READONLY */
/*-----------------------------------------------------------------------*/
/* Send a command packet to MMC */
/*-----------------------------------------------------------------------*/
static
BYTE send_cmd (
BYTE cmd, /* Command byte */
DWORD arg /* Argument */
)
{
BYTE n, res;
if (wait_ready() != 0xFF) return 0xFF;
/* Send command packet */
xmit_spi(cmd); /* Command */
xmit_spi((BYTE)(arg >> 24)); /* Argument[31..24] */
xmit_spi((BYTE)(arg >> 16)); /* Argument[23..16] */
xmit_spi((BYTE)(arg >> 8)); /* Argument[15..8] */
xmit_spi((BYTE)arg); /* Argument[7..0] */
n = 0;
if (cmd == CMD0) n = 0x95; /* CRC for CMD0(0) */
if (cmd == CMD8) n = 0x87; /* CRC for CMD8(0x1AA) */
xmit_spi(n);
/* Receive command response */
if (cmd == CMD12) rcvr_spi(); /* Skip a stuff byte when stop reading */
n = 10; /* Wait for a valid response in timeout of 10 attempts */
do
res = rcvr_spi();
while ((res & 0x80) && --n);
return res; /* Return with the response value */
}
/*--------------------------------------------------------------------------
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -