📄 sd.c.bak
字号:
#include "stdint.h"
#include "s3c44b0x.h"
#include "system.h"
#include "sd.h"
//-----------------------------------------------------------------------------------
DSTATUS wDiskInitialize ()
{
wMMCInit();
if (wMMCReset()!=0) return STA_NOINIT;
return 0;
}
//------------------------------------------------------------------------------------
DSTATUS wDiskShutdown ()
{
// no shutdown required
return 0;
};
//------------------------------------------------------------------------------------
DSTATUS wDiskStatus ()
{
if (wMMCReset()!=0) return STA_NOINIT;
return 0;
}
//------------------------------------------------------------------------------------
DRESULT wDiskRead (
BYTE *buff, /* Data buffer to store read data */
DWORD sector, /* Sector number (LBA) */
BYTE count /* Sector count (1..255) */
)
{
DRESULT result = RES_OK;
u32 i;
for (i = 0; i< count; i++)
{
if(wMMCReadSector(sector, (u08*)buff) != 0)
{
result = RES_ERROR;
return result;
}
sector++;
buff+=512;
}
return result;
}
//-----------------------------------------------------------------------------------
DRESULT wDiskWrite (
const BYTE *buff, /* Data to be written */
DWORD sector, /* Sector number (LBA) */
BYTE count /* Sector count (1..255) */
)
{
DRESULT result = RES_OK;
u32 i;
for (i = 0; i< count; i++)
{
if(wMMCWriteSector(sector, (u08*)buff) != 0)
{
result = RES_ERROR;
return result;
}
sector++;
buff+=512;
}
return result;
}
//----------------------------------------------------------------------------------
DRESULT wDiskIOctl (BYTE, void*);
//----------------------------------------------------------------------------------
void wDiskTimerProc ()
{
}
///---------------------------------------------------------------------------------
DWORD wGetTime ()
{
return ((2006UL-1980) << 25) // Year = 2006
| (2UL << 21) // Month = Feb
| (9UL << 16) // Day = 9
| (22U << 11) // Hour = 22
| (30U << 5) // Min = 30
| (0U >> 1) // Sec = 0
;
}
//-----------------------------------------------------------------------------------
#define PORT_CS *pdatc
#define CS 11
#define MMC_GO_IDLE_STATE 0 ///< initialize card to SPI-type access
#define MMC_SEND_OP_COND 1 ///< set card operational mode
#define MMC_SEND_CSD 9 ///< get card's CSD
#define MMC_SEND_CID 10 ///< get card's CID
#define MMC_SEND_STATUS 13
#define MMC_SET_BLOCKLEN 16 ///< Set number of bytes to transfer per block
#define MMC_READ_SINGLE_BLOCK 17 ///< read a block
#define MMC_WRITE_BLOCK 24 ///< write a block
#define MMC_PROGRAM_CSD 27
#define MMC_SET_WRITE_PROT 28
#define MMC_CLR_WRITE_PROT 29
#define MMC_SEND_WRITE_PROT 30
#define MMC_TAG_SECTOR_START 32
#define MMC_TAG_SECTOR_END 33
#define MMC_UNTAG_SECTOR 34
#define MMC_TAG_ERASE_GROUP_START 35 ///< Sets beginning of erase group (mass erase)
#define MMC_TAG_ERARE_GROUP_END 36 ///< Sets end of erase group (mass erase)
#define MMC_UNTAG_ERASE_GROUP 37 ///< Untag (unset) erase group (mass erase)
#define MMC_ERASE 38 ///< Perform block/mass erase
#define MMC_CRC_ON_OFF 59 ///< Turns CRC check on/off
// R1 Response bit-defines
#define MMC_R1_BUSY 0x80 ///< R1 response: bit indicates card is busy
#define MMC_R1_PARAMETER 0x40
#define MMC_R1_ADDRESS 0x20
#define MMC_R1_ERASE_SEQ 0x10
#define MMC_R1_COM_CRC 0x08
#define MMC_R1_ILLEGAL_COM 0x04
#define MMC_R1_ERASE_RESET 0x02
#define MMC_R1_IDLE_STATE 0x01
// Data Start tokens
#define MMC_STARTBLOCK_READ 0xFE ///< when received from card, indicates that a block of data will follow
#define MMC_STARTBLOCK_WRITE 0xFE ///< when sent to card, indicates that a block of data will follow
#define MMC_STARTBLOCK_MWRITE 0xFC
// Data Stop tokens
#define MMC_STOPTRAN_WRITE 0xFD
// Data Error Token values
#define MMC_DE_MASK 0x1F
#define MMC_DE_ERROR 0x01
#define MMC_DE_CC_ERROR 0x02
#define MMC_DE_ECC_FAIL 0x04
#define MMC_DE_OUT_OF_RANGE 0x04
#define MMC_DE_CARD_LOCKED 0x04
// Data Response Token values
#define MMC_DR_MASK 0x1F
#define MMC_DR_ACCEPT 0x05
#define MMC_DR_REJECT_CRC 0x0B
#define MMC_DR_REJECT_WRITE_ERROR 0x0D
//---------------------------------------------
u08 wMMCByte(u08 x)
{
/*u32 r=0;
u32 i=0;
for (i=0; i<8; i++)
{
sck_low();
r=(r<<1);
if (x & 0x80)
mosi_high();
else
mosi_low();
r|=read_bit();
sck_high();
x=(x<<1);
}
return r;*/
*i_ispc |= (1 << 4);
*siocon = 0x00000021;
*siodat = x;
*siocon = 0x00000029;
while ((*intpnd)&(1<<4)!=0x00000010);
*siocon = 0x00000021;
*i_ispc |= (1 << 4);
return *siodat;/**/
}
//---------------------------------------------
u08 wMMCCommand(u08 command, u32 arg)
{
u08 r1;
u08 retry=0;
wMMCByte(0xff);
wMMCByte(0xff);
wMMCByte(0x40 | command);
wMMCByte((arg & 0xff000000) >> 24);
wMMCByte((arg & 0xff0000) >> 16);
wMMCByte((arg & 0xff00) >> 8);
wMMCByte((arg & 0xff));
wMMCByte(0x95);
// end command
// wait for response
// if more than 8 retries, card has timed-out
// return the received 0xFF
while((r1 = wMMCByte(0xFF)) == 0xFF)
if(retry++ > 8) break;
// return response
return r1;
}
//---------------------------------------------
//---------------------------------------------
void wSIOInit()
{
//*pconf &= ~((0xFFF)<<10);
//*pconf |= ((0x6DB)<<10);
*siocon = 0x00000020;
*sbrdr = 0x00000002;
*itvcnt = 0x00000001;
*dcntz = 0x00000003;
*siocon = 0x00000020;
*siocon = 0x00000021;
}
//--------------------------------------------
//--------------------------------------------
void wMMCInit()
{
cs_high();
*pdatf|=(1 << 5); /* preload mosi to be high */
*pconf&=~(0x07 << 10); /* mask out 3 bits for pin control */
*pconf|=(0x03 << 10); /* 1=output (3=spi) */
/* miso on pf7 */
*pupf&=~(0x01 << 7); /* 0=pullup on pf7 */
*pconf&=~(0x07 << 16); /* mask out 3 bits for pin control */
*pconf|=(0x03 << 16); /* 0=input (3=spi) */
/* sck on pf8 */
*pdatf|=(1 << 8); /* preload sck high */
*pconf&=~(0x07 << 19); /* mask out 3 bits for pin control */
*pconf|=(0x03 << 19); /* 1=output (3=spi) */
wSIOInit();
}
//--------------------------------------------
u08 wMMCReset()
{
u08 i;
u08 retry;
u08 r1=0;
retry = 0;
do
{
cs_high();
for(i=0;i<12;i++) wMMCByte(0xFF);
cs_low();
r1 = wMMCCommand(MMC_GO_IDLE_STATE, 0);
retry++;
if(retry>100) return -1;
} while(r1 != 0x01);
retry = 0;
do
{
r1 = wMMCCommand(MMC_SEND_OP_COND, 0);
retry++;
if(retry>254) return -1;
} while(r1);
r1 = wMMCCommand(MMC_CRC_ON_OFF, 0);
r1 = wMMCCommand(MMC_SET_BLOCKLEN, 512);
wMMCByte(0xFF);
wMMCByte(0xFF);
cs_high();
return 0;
}
//------------------------------------------------------
u08 wMMCReadSector(u32 sector, u08* buffer)
{
u08 r1;
u16 i;
// assert chip select
cs_low();
// issue command
r1 = wMMCCommand(MMC_READ_SINGLE_BLOCK, sector<<9);
// check for valid response
if(r1 != 0x00)
return r1;
// wait for block start
while(wMMCByte(0xFF) != MMC_STARTBLOCK_READ);
// read in data
for(i=0; i<0x200; i++)
{
*buffer = (u08) wMMCByte(0xFF);
buffer++;
}
// read 16-bit CRC
wMMCByte(0xFF);
wMMCByte(0xFF);
wMMCByte(0xFF);
// release chip select
cs_high();
// return success
return 0;
}
//--------------------------------------------------------
u08 wMMCWriteSector(u32 sector, u08* buffer)
{
u08 r1;
u16 i;
cs_low();
// issue command
r1 = wMMCCommand(MMC_WRITE_BLOCK, sector<<9);
// check for valid response
if(r1 != 0x00)
return r1;
// send dummy
wMMCByte(0xFF);
// send data start token
wMMCByte(MMC_STARTBLOCK_WRITE);
// write data
for(i=0; i<0x200; i++)
{
wMMCByte(*buffer++);
}
// write 16-bit CRC (dummy values)
wMMCByte(0xFF);
wMMCByte(0xFF);
// read data response token
r1 = wMMCByte(0xFF);
if( (r1&MMC_DR_MASK) != MMC_DR_ACCEPT)
return r1;
// wait until card not busy
while(!wMMCByte(0xFF));
cs_high();
return 0;
}
//--------------------------------------------
void wMMCDMARead(u08 *usBuffer, u32 ulLength)
{
void wSIODMAInit();
}
void wMMCDMAWrite(u08 *usBuffer, u32 ulLength)
{
}
void wSIODMAInit()
{
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -