📄 mmc.c
字号:
/**************************************************************************************
//------------------ MMC/SD-Card Reading and Writing implementation -------------------
//FileName : mmc.c
//Function : Connect AVR to MMC/SD
//Created by : ZhengYanbo
//Created date : 15/08/2005
//Version : V1.2
//Last Modified: 19/08/2005
//Filesystem : Read or Write MMC without any filesystem
//CopyRight (c) 2005 ZhengYanbo
//Email: Datazyb_007@163.com
****************************************************************************************/
#include "..\inc\44b0x.h"
#include "..\inc\44b.h"
#include "..\inc\44blib.h"
#include "mmc_z.h"
#if USE_MMC
//****************************************************************************
// Port Init
void MMC_Port_Init(void)
//****************************************************************************
{
/*
//Config ports
MMC_Direction_REG.SPI_DI=0; //Set Pin MMC_DI as Input
MMC_Direction_REG.SPI_Clock=1; //Set Pin MMC_Clock as Output
MMC_Direction_REG.SPI_DO=1; //Set Pin MMC_DO as Output
MMC_Direction_REG.MMC_Chip_Select=1; //Set Pin MMC_Chip_Select as Output
//busy led port init
MMC_Direction_REG.SPI_BUSY=1; //Set spi busy led port output
MMC_BUSY_LED=1; //busy led off
MMC_CS_PIN=1; //Set MMC_Chip_Select to High,MMC/SD Invalid.
*/
SPI_CS_High;
}
//****************************************************************************
//Routine for Init MMC/SD card(SPI-MODE)
unsigned char MMC_Init(void)
//****************************************************************************
{
// unsigned char retry,temp;
unsigned char temp;
unsigned short retry;
unsigned short i;
unsigned char CMD[] = {0x40,0x00,0x00,0x00,0x00,0x95};
//MMC_Port_Init(); //Init SPI port
//用于延时
for(i=0;i<2000;i++) //Wait MMC/SD ready...
{
// #asm("nop");
;
}
Init_Flag=1; //Set the init flag
for (i=0;i<0xff;i++)
{
Write_Byte_MMC(0xff); //send 74 clock at least!!!
}
//Send Command CMD0 to MMC/SD Card
retry=0;
do
{ //retry 200 times to send CMD0 command
temp=Write_Command_MMC(CMD);
retry++;
if(retry==200)
{ //time out
return(INIT_CMD0_ERROR);//CMD0 Error!
}
}
while(temp!=1);
// Init_Flag = 0;
//Send Command CMD1 to MMC/SD-Card
CMD[0] = 0x41; //Command 1
CMD[5] = 0xFF;
// CMD[5] = 0x95;
retry=0;
do
{ //retry 100 times to send CMD1 command
temp=Write_Command_MMC(CMD);
retry++;
if(retry==1000)
{ //time out
return(INIT_CMD1_ERROR);//CMD1 Error!
}
}
while(temp!=0);
Init_Flag=0; //Init is completed,clear the flag
MMC_Disable(); //set MMC_Chip_Select to high
return(0); //All commands have been taken.
}
//****************************************************************************
//returns the :
// size of the card in MB ( ret * 1024^2) == bytes
// sector count and multiplier MB are in u08 == C_SIZE / (2^(9-C_SIZE_MULT))
// name of the media
void MMC_get_volume_info(void)
//****************************************************************************
{ unsigned char i;
VOLUME_INFO_TYPE MMC_volume_Info,*vinf;
vinf=&MMC_volume_Info; //Init the pointoer;
// read the CSD register
Read_CSD_MMC(sectorBuffer.data);
// get the C_SIZE value. bits [73:62] of data
// [73:72] == sectorBuffer.data[6] && 0x03
// [71:64] == sectorBuffer.data[7]
// [63:62] == sectorBuffer.data[8] && 0xc0
vinf->sector_count = sectorBuffer.data[6] & 0x03;
vinf->sector_count <<= 8;
vinf->sector_count += sectorBuffer.data[7];
vinf->sector_count <<= 2;
vinf->sector_count += (sectorBuffer.data[8] & 0xc0) >> 6;
// get the val for C_SIZE_MULT. bits [49:47] of sectorBuffer.data
// [49:48] == sectorBuffer.data[5] && 0x03
// [47] == sectorBuffer.data[4] && 0x80
vinf->sector_multiply = sectorBuffer.data[9] & 0x03;
vinf->sector_multiply <<= 1;
vinf->sector_multiply += (sectorBuffer.data[10] & 0x80) >> 7;
// work out the MBs
// mega bytes in u08 == C_SIZE / (2^(9-C_SIZE_MULT))
vinf->size_MB = vinf->sector_count >> (9-vinf->sector_multiply);
// get the name of the card
Read_CID_MMC(sectorBuffer.data);
vinf->name[0] = sectorBuffer.data[3];
vinf->name[1] = sectorBuffer.data[4];
vinf->name[2] = sectorBuffer.data[5];
vinf->name[3] = sectorBuffer.data[6];
vinf->name[4] = sectorBuffer.data[7];
vinf->name[5] = 0x00; //end flag
//----------------------------------------------------------
// LCDclrscr();
//Print Product name on lcd
i=0;
// writestring("Product:");
// while((vinf->name[i]!=0x00)&&(i<16)) writechar(vinf->name[i++]);
//Print Card Size(eg:128MB)
// gotoxy(1,0);
// writestring("Tot:");
// writeNumber(vinf->size_MB); writestring("MB ");
//gotoxy(2,0);
//writestring("sector_mult:"); writeNumber(vinf->sector_multiply);
//gotoxy(3,0);
//writestring("sect_cnt:"); writeNumber(vinf->sector_count);
}
//****************************************************************************
//Send a Command to MMC/SD-Card
//Return: the second byte of response register of MMC/SD-Card
unsigned char Write_Command_MMC(unsigned char *CMD)
//****************************************************************************
{
unsigned char tmp,temp1;
unsigned char retry=0;
unsigned char i;
//set MMC_Chip_Select to high (MMC/SD-Card disable)
MMC_Disable();
//send 8 Clock Impulse
Write_Byte_MMC(0xFF);
//set MMC_Chip_Select to low (MMC/SD-Card active)
MMC_Enable();
//send 6 Byte Command to MMC/SD-Card
for (i=0;i<0x06;i++)
{
Write_Byte_MMC(*CMD++);
}
//get 16 bit response
temp1 = Read_Byte_MMC(); //read the first byte,ignore it.
Uart_Printf("temp1 %x \n",temp1);
do
{ //Only last 8 bit is used here.Read it out.
tmp = Read_Byte_MMC();
retry++;
}
while((tmp==0xff)&&(retry<100));
// MMC_Disable();
return(tmp);
}
//****************************************************************************
//Routine for reading a byte from MMC/SD-Card
unsigned char Read_Byte_MMC(void)
//****************************************************************************
{
unsigned char temp=0,temp1;
unsigned char i;
unsigned short j;
//Software SPI
for (i=0; i<8; i++) //MSB First
{
// MMC_CLK_PIN=0; //Clock Impuls (Low)
SPI_CLK_Low;
if(Init_Flag)
{
for(j = 0;j<800;j++);
}
temp = (temp << 1) ; //read mmc data out pin
temp1 = SPI_DO;
temp |=temp1;
// MMC_CLK_PIN=1; //set Clock Impuls High
SPI_CLK_High;
if(Init_Flag)
{
for(j = 0;j<800;j++);
}
}
// MMC_BUSY_LED=1;
return (temp);
}
//****************************************************************************
//Routine for sending a byte to MMC/SD-Card
void Write_Byte_MMC(unsigned char value)
//****************************************************************************
{
unsigned char i;
unsigned short j;
//Software SPI
for (i=0; i<8; i++)
{ //write a byte
if (((value >> (7-i)) & 0x01)==0x01) SPI_DI_High; //Send bit by bit(MSB First)
else SPI_DI_Low;
// MMC_CLK_PIN=0; //set Clock Impuls low
SPI_CLK_Low;
if(Init_Flag)
{
for(j = 0;j<800;j++);
}
SPI_CLK_High; //set Clock Impuls High
if(Init_Flag)
{
for(j = 0;j<800;j++);
}
}//write a byte
// MMC_DI_PIN=1; //set Output High
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -