SD卡的读写功能
0赞
发表于 1/11/2012 2:17:44 PM
阅读(2816)
#include#include #include "dm_types.h" #include "dm_mmc.h" #include "dm_bf5xx.h" /**************************************************************************** * 名称 : SPI_TransferByte * 功能 : 将数据value发出 * 入口参数 :value * 出口参数 :incoming 返回读取的数据 ****************************************************************************/ uint8_t SPI_TransferByte(uint8_t value) { uint8_t incoming=0; while((*pSPI_STAT & TXS)); *pSPI_TDBR = value; while(*pSPI_STAT & RXS) incoming = *pSPI_RDBR; return(incoming); } /**************************************************************************** * 名称 : MMC_Init * 功能 : 初始化MMC Card * 入口参数 :无 * 出口参数 :1 ****************************************************************************/ uint8_t MMC_Init(void) { uint8_t retry,temp; uint8_t i; for (i=0;i<10;i++) { SPI_TransferByte(0xff); //send 74 clock at least!!! } SPI_TransferByte(MMC_RESET); SPI_TransferByte(0x00); SPI_TransferByte(0x00); SPI_TransferByte(0x00); SPI_TransferByte(0x00); SPI_TransferByte(0x95); SPI_TransferByte(0xff); SPI_TransferByte(0xff); retry=0; do{ temp=Write_Command_MMC(MMC_INIT,0); retry++; if(retry==100) { printf("MMC_INIT is ERROR!\r\n"); return(INIT_CMD1_ERROR); } }while(temp!=0); printf("MMC_INIT is OK!\r\n"); return (1); } /**************************************************************************** * 名称 : Write_Command_MMC * 功能 : 向MCC卡写命令 ,将命令字cmd送入地址address,返回状态值tmp * 入口参数 :cmd,address * 出口参数 :tmp ****************************************************************************/ uint8_t Write_Command_MMC(uint32_t cmd, uint32_t address) { uint8_t tmp; uint8_t retry=0; SPI_TransferByte(cmd); SPI_TransferByte(address>>24); SPI_TransferByte(address>>16); SPI_TransferByte(address>>8); SPI_TransferByte(address); SPI_TransferByte(0xFF); do{ tmp = SPI_TransferByte(0xFF); retry++; }while((tmp==0xff)&&(retry<8)); return(tmp); } /**************************************************************************** * 名称 : MMC_read_sector * 功能 : 读取MMC卡扇区数据,指定要读的扇区号和将数据传入的buff地址 * 入口参数 :addrBuffer * 出口参数 :1 ****************************************************************************/ uint8_t MMC_read_sector(uint32_t addr,uint8_t *Buffer) { uint8_t temp; uint32_t i; SPI_TransferByte(0xff); temp = Write_Command_MMC(MMC_READ_BLOCK,addr<<9); if(temp != 0x00) { return(READ_BLOCK_ERROR); } while(SPI_TransferByte(0xff) != 0xfe); for(i=0;i<512;i++) { *Buffer++ = SPI_TransferByte(0xff); } SPI_TransferByte(0xff); SPI_TransferByte(0xff); return (1); } /**************************************************************************** * 名称 : MMC_Write_sector * 功能 : 写MMC卡扇区,将BUF的数据写入扇区号sector * 入口参数 :sector,buf * 出口参数 :0 ****************************************************************************/ uint8_t MMC_Write_sector(uint32_t sector, uint8_t *buf) { uint8_t *p, by; uint32_t i,flag; uint32_t startadr; p = buf; startadr = sector*(uint32_t)BYTE_PER_SEC; Write_Command_MMC (MMC_WRITE_BLOCK,startadr); SPI_TransferByte(0xFF); SPI_TransferByte(0xFF); SPI_TransferByte(0xFE); for(i=0; i