?? mmc.c
字號:
/*-----------------------------------------------------------------------*/
/* MMC/SDC (in SPI mode) control module (C)ChaN, 2007 */
/*-----------------------------------------------------------------------*/
#include "v850es.h"
#include "diskio.h"
#define USE_DMA3
/* 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 SELECT() P0 &= ~(1<<6) /* MMC CS = L */
#define DESELECT() P0 |= (1<<6) /* MMC CS = H */
#define SOCKPORT P3L /* Socket contact port */
#define SOCKWP 0x04 /* Write protect switch (bit2) */
#define SOCKINS 0x08 /* Card detect switch (bit3) */
/*--------------------------------------------------------------------------
Module Private Functions
---------------------------------------------------------------------------*/
static volatile
DSTATUS Stat = STA_NOINIT; /* Disk status */
static volatile
UINT Timer1, Timer2; /* 1000Hz decrement timer */
static
UINT CardType; /* b0:MMC, b1:SDC, b2:Block addressing */
/*-----------------------------------------------------------------------*/
/* Transmit a byte to MMC via SPI (Platform dependent) */
/*-----------------------------------------------------------------------*/
#define xmit_spi(dat) CB0TXL=(dat); do; while(CB0TSF)
/*-----------------------------------------------------------------------*/
/* Receive a byte from MMC via SPI (Platform dependent) */
/*-----------------------------------------------------------------------*/
static
BYTE rcvr_spi (void)
{
CB0TXL = 0xFF;
do; while (CB0TSF);
return CB0RXL;
}
/* Alternative macro to receive data fast */
#define rcvr_spi_m(dst) CB0TXL=0xFF; do; while(CB0TSF); *(dst)=CB0RXL
/*-----------------------------------------------------------------------*/
/* Wait for card ready */
/*-----------------------------------------------------------------------*/
static
BYTE wait_ready (void)
{
BYTE res;
Timer2 = 500; /* 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)
{
P4 = 0x03; /* Attach CSIB0 unit to I/O pin */
PM4 = 0x02;
PF4 = 0x00;
PMC4 = 0x07;
PFC4 = 0x00;
CB0CTL0 = 0; /* Disable CSIB0 */
CB0CTL1 = 0x18; /* Select fclk=fxx/2, SPI-0 */
CB0CTL2 = 0; /* Select 8 bit mode */
CB0CTL0 = 0xE0; /* Enable CSIB0 with single R/W, MSB first */
}
static
void power_off (void)
{
SELECT(); /* Wait for card ready */
wait_ready();
DESELECT();
rcvr_spi();
CB1CTL0 = 0; /* Disable CSIB1 */
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 = 100;
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 */
#ifdef USE_DMA3 /* (Platform dependent in DMA mode) */
PMC4.1 = 0; /* Initialize CSIB0 to R/O mode */
CB0CTL0 = 0;
CB0CTL0 = 0xA1;
DDA3H = (((DWORD)buff >> 16) | 0x8000) & 0x83FF;/* Destination address (data buffer) */
DDA3L = (WORD)buff;
DSA3H = 0x03FF; DSA3L = 0xFD04; /* Source address (CB0RXL) */
DADC3 = 0x0080; /* Src fixed, Dst increment */
DBC3 = btr - 1; /* DMA count */
DTFR3 = 0x1E; /* Triggered by INTCB0R */
DCHC3 = 0x01; /* Enable DMA */
token = CB0RXL; /* Start DMA xfer */
do; while (!TC3); /* Wait for end of xfer */
do; while (CB0TSF);
CB0CTL0 = 0; /* Re-initialize CSIB0 to R/W mode */
CB0CTL0 = 0xE0;
PMC4.1 = 1;
#else
do { /* Receive the data block into buffer */
rcvr_spi_m(buff++);
rcvr_spi_m(buff++);
} while (btr -= 2);
#endif
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;
UINT bc = 512;
if (wait_ready() != 0xFF) return FALSE;
xmit_spi(token); /* Xmit data token */
if (token != 0xFD) { /* Is data token */
#ifdef USE_DMA3 /* (Platform dependent in DMA mode) */
DSA3H = (((DWORD)(buff+1) >> 16) | 0x8000) & 0x83FF;/* Source address (data buffer + 1) */
DSA3L = (WORD)(buff+1);
DDA3H = 0x03FF; DDA3L = 0xFD06; /* Destination address (CB0TXL) */
DADC3 = 0x0020; /* Src increment, Dst fixed */
DBC3 = bc - 2; /* DMA count */
DTFR3 = 0x1E; /* Triggered by INTCB0R */
DCHC3 = 0x01; /* Enable DMA */
CB0TXL = *buff; /* Start DMA xfer */
do; while (!TC3); /* Wait for end of xfer */
do; while (CB0TSF);
#else
do { /* Xmit the 512 byte data block to MMC */
xmit_spi(*buff++);
xmit_spi(*buff++);
} while (bc -= 2);
#endif
xmit_spi(0xFF); /* CRC (Dummy) */
xmit_spi(0xFF);
resp = rcvr_spi(); /* Receive 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 */
}
/*--------------------------------------------------------------------------
Public Functions
---------------------------------------------------------------------------*/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -