⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mmc.c

📁 gps mmc logging using z8f6421 zilog microcontroller
💻 C
字号:
#include <ez8.h>
#include <stdio.h>
#include <sio.h>
#include <stdlib.h>
#include <string.h>
#include "mmc.h"
#include "spi.h"
#include "fat.h"

unsigned char CSD[16];

typedef struct{
	unsigned char byte1;
	unsigned char byte2;
	unsigned char byte3;
	unsigned char byte4;
	unsigned char byte5;
	unsigned char byte6;
}command;

void CommandMMC(unsigned char cmd_index, unsigned long cmd_argument)
//sends a 6-byte command from the MCU to the MMC
{
	command cmd;
	unsigned char CRC7;
	
	//first byte (start bit '0' + transmitter bit '1' + command index)
	cmd.byte1 = 0x40|cmd_index;
	
	//bytes 2-5 (command argument)
	cmd.byte2 = cmd_argument>>24;
	cmd.byte3 = cmd_argument>>16;
	cmd.byte4 = cmd_argument>>8;
	cmd.byte5 = cmd_argument;
	
	//CRC was not implemented in this application note
	//Although CRC is optional in SPI mode, it is a required token (i.e. a dummy CRC //must be created.  But during reset period (sending of CMD0), the card is in //MMC mode; hence, a valid CRC field is required.	
	//last byte (crc + end bit '1')
	if( cmd.byte1 == 0x40 )
		cmd.byte6 = 0x95;	
	else if( cmd.byte1 == 0x41 )
		cmd.byte6 = 0xFF;	
	else if( cmd.byte1 == 0x7B )
		cmd.byte6 = 0x01;	
	else 
		cmd.byte6 = (CRC7<<1)|0x01;

	//send command per byte
	TransferSPI(cmd.byte1);
	TransferSPI(cmd.byte2);
	TransferSPI(cmd.byte3);
	TransferSPI(cmd.byte4);
	TransferSPI(cmd.byte5);
	TransferSPI(cmd.byte6);

}

unsigned char WaitResponseMMC(void)
//waits for the card抯 response
{
	unsigned char response;
	
	do 
		response = TransferSPI(0xFF);
	while(response == 0xFF);
	return response;
}

unsigned char InitMMC(void)
//initializes the MMC to operate in SPI mode, puts it out of the idle state and temporarily disables CRC
{
	unsigned char R1;

	InitSPI();							//initialize SPI
	AssertSS();
	TransferSPI(0xFF);
	CommandMMC(CMMC_Reset,0);			//send reset command
	R1 = WaitResponseMMC();
	TransferSPI(0xFF);		

	while(R1==0x01)						//put MMC out of idle state
	{
		CommandMMC(CMMC_SendOPCond,0);
		R1 = WaitResponseMMC();
		TransferSPI(0xFF);	
	}
	
	CommandMMC(CMMC_DisableCRC,0);		//disable CRC7
	if (R1)return R1;
	R1=WaitResponseMMC();
	TransferSPI(0xFF);			
	
	DeassertSS();			
	return 0;
}

unsigned char GetDataResponseMMC(void)
//waits for the data response token sent by the card
//used after writing a block of data to the card
//returns the status bits which indicate status of write operation
{
	unsigned char dataresponse;	

	dataresponse=TransferSPI(0xFF);
	dataresponse &= 0x1F;

	return dataresponse;
}

void WaitUntilReadyMMC(void)
//waits until the card is out of busy state
//used after getting the data response token of the card when it enters a busy state
{
	unsigned char BUSY;
	
	do
	BUSY = TransferSPI(0xFF);	//zero value for BUSY indicates card is busy
	while(BUSY==0x00);
}

unsigned char WriteMMC(unsigned long StartAddress, unsigned char* data){
//writes data(single block:512 bytes) to the MMC
	
	unsigned char R1,dataresponse,err=0;
	unsigned int ctr;

	//start read
	AssertSS();	
	TransferSPI(0xFF);
	CommandMMC(CMMC_SingleBlockWrite,StartAddress);
	R1 = WaitResponseMMC();
	TransferSPI(0xFF);

	if(!R1){		
		//send data token
		TransferSPI(0xFE); //start block token
		for(ctr=0;ctr<512;ctr++)TransferSPI(data[ctr]);
		TransferSPI(0xFF);	//16-bit CRC
		TransferSPI(0xFF);	
		
		//check data response and wait until card is out of busy state
		dataresponse=GetDataResponseMMC();
		if(dataresponse!=0x05)err=1;
		WaitUntilReadyMMC();
	}
	else err=1;
	
	DeassertSS();
	return err;
}


unsigned char ReadMMC(unsigned long StartAddress,unsigned char* data)
//reads a single block:512 bytes from the MMC and saves it as data
{
	
	unsigned char R1,StartByte,err=0;
	unsigned int ctr;

	AssertSS();
	TransferSPI(0xFF);

	CommandMMC(CMMC_SingleBlockRead,StartAddress);
	R1=WaitResponseMMC();//command 17 response	

	if(!R1)
	{
		StartByte=WaitResponseMMC();
		if(StartByte==0xFE) 
			for(ctr=0;ctr<512;ctr++)data[ctr]=TransferSPI(0xFF); //return stored data
		else err=1;
	}
	else err=1;
	
	DeassertSS();
	return err;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -