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

📄 mmc.c

📁 移植到mega32上面的fat16代码实现了
💻 C
字号:
/*! \file mmc.c \brief MultiMedia and SD Flash Card Interface. */
//############################################################################
//OP747ARU
// File Name	: 'mmc.c'
// Title		: MultiMedia and SD Flash Card Interface
// Author		: Pascal Stang - Copyright (C) 2004
// Created		: 2004.09.22
// Revised		: 2004.09.22
// Version		: 0.1
// Target MCU	: Atmel AVR Series
// Editor Tabs	: 4
//
// NOTE: This code is currently below version 1.0, and therefore is considered
// to be lacking in some functionality or documentation, or may not be fully
// tested.  Nonetheless, you can expect most functions to work.
//
// This code is distributed under the GNU Public License
//		which can be found at http://www.gnu.org/licenses/gpl.txt
//
//############################################################################
// size 128 MB extreMEmomry(c) mmc-card: 250879 sector (not exact 128MB)


// Include Files
#include <avr/io.h>			// include I/O definitions (port names, pin names, etc)
#include <avr/signal.h>		// include "signal" names (interrupt names)
#include <avr/interrupt.h>	// include interrupt support

#include "global.h"		    // include our global settings
#include "spi.h"		    // include spi bus support
#include "rprintf.h"
#include "mmc.h"
#include "mmcconf.h"        // include project-specific hardware configuration
#include "timer.h"


// Global variables

// Functions
//############################################################################
void mmcInit(void)
//############################################################################
{
	// initialize SPI interface
	spiInit();
	// release chip select
	MMC_CS_DDR |= (1<<MMC_CS_PIN);
	MMC_CS_PORT |= (1<<MMC_CS_PIN);
}

//############################################################################
uint8_t mmcReset(void)
//############################################################################
{
	uint8_t retry;
	uint8_t r1=0;

	retry = 0;
	do
	{
		// send dummy bytes with CS high before accessing
		spiTransferByte(0xFF);
		spiTransferByte(0xFF);
		spiTransferByte(0xFF);
		spiTransferByte(0xFF);
		// resetting card, go to SPI mode
		r1 = mmcSendCommand(MMC_GO_IDLE_STATE, 0);
		//#ifdef MMC_DEBUG
		rprintf("MMC_GO_IDLE_STATE: R1=0x%x\r\n", r1);
		//#endif
		// do retry counter
		retry++;
		if(retry>10) return -1;
	} while(r1 != 0x01);

	// TODO: check card parameters for voltage compliance
	// before issuing initialize command

	retry = 0;
	do
	{
		// initializing card for operation
		r1 = mmcSendCommand(MMC_SEND_OP_COND, 0);
		#ifdef MMC_DEBUG
		rprintf("MMC_SEND_OP_COND: R1=0x%x\r\n", r1);
		#endif
		// do retry counter
		retry++;
		if(retry>100) return -1;
	} while(r1);

	// turn off CRC checking to simplify communication
	r1 = mmcSendCommand(MMC_CRC_ON_OFF, 0);
	#ifdef MMC_DEBUG
	rprintf("MMC_CRC_ON_OFF: R1=0x%x\r\n", r1);
	#endif

	// set block length to 512 bytes
	r1 = mmcSendCommand(MMC_SET_BLOCKLEN, 0x200);
	#ifdef MMC_DEBUG
	rprintf("MMC_SET_BLOCKLEN: R1=0x%x\r\n", r1);
	#endif

	// return success
	return 0;
}

//############################################################################
uint8_t mmcSendCommand(uint8_t cmd, uint32_t arg)
//############################################################################
{
	uint8_t r1;

	// assert chip select
	MMC_CS_PORT &= ~(1<<MMC_CS_PIN);
	// issue the command
	r1 = mmcCommand(cmd, arg);
	// release chip select
	MMC_CS_PORT |= (1<<MMC_CS_PIN);

	return r1;
}

//############################################################################
uint8_t mmcRead(uint32_t sector, uint8_t* buffer)
//############################################################################
{
	uint8_t r1;
	uint16_t i;

	// assert chip select
	MMC_CS_PORT &= ~(1<<MMC_CS_PIN);
	// issue command
	r1 = mmcCommand(MMC_READ_SINGLE_BLOCK, sector<<9);
	#ifdef MMC_DEBUG
		//rprintf("MMC Read Block R1=0x%x", r1);
		//rprintfNum(10,10, FALSE,' ', sector ); rprintfCRLF();
	#endif
	// check for valid response
	if(r1 != 0x00)
		return r1;
	// wait for block start
	while(spiTransferByte(0xFF) != MMC_STARTBLOCK_READ);
	// read in data
	for(i=0; i<0x200; i++)
	{
		*buffer++ = spiTransferByte(0xFF);
	}
	// read 16-bit CRC
	spiTransferByte(0xFF);
	spiTransferByte(0xFF);
	// release chip select
	MMC_CS_PORT |= (1<<MMC_CS_PIN);
	// return success
	return 0;
}

//############################################################################
uint8_t mmcWrite(uint32_t sector, uint8_t* buffer)
//############################################################################
{
	uint8_t r1;
	uint16_t i;

	// assert chip select
	MMC_CS_PORT &= ~(1<<MMC_CS_PIN);
	// issue command
	r1 = mmcCommand(MMC_WRITE_BLOCK, sector<<9);
	#ifdef MMC_DEBUG
		rprintf("MMC Write Block R1=0x%x", r1);
		rprintfNum(10,10, FALSE,' ', sector ); //rprintfCRLF();
	#endif
	// check for valid response
	if(r1 != 0x00)
		return r1;
	// send dummy
	spiTransferByte(0xFF);
	// send data start token
	spiTransferByte(MMC_STARTBLOCK_WRITE);
	// write data
	for(i=0; i<0x200; i++)
	{
		spiTransferByte(*buffer++);
	}
	// write 16-bit CRC (dummy values)
	spiTransferByte(0xFF);
	spiTransferByte(0xFF);
	// read data response token
	r1 = spiTransferByte(0xFF);
	if( (r1&MMC_DR_MASK) != MMC_DR_ACCEPT)
		return r1;
	#ifdef MMC_DEBUG
	//rprintf("Data Response Token=0x%x\r\n", r1);
	//rprintfProgStrM("Data Response Token: Data ");
	rprintfProgStrM(" Data ");
	switch(r1&0x1F){
		case 0x05: rprintfProgStrM("accepted"); break;
		case 0x0B: rprintfProgStrM("rejected due to a CRC error"); break;
		case 0x0D: rprintfProgStrM("rejected due to a Write Error"); break;
		default:  rprintfProgStrM("Unknown command"); break;
	}
	rprintfCRLF();
	#endif
	// wait until card not busy
	while(!spiTransferByte(0xFF));
	// release chip select
	MMC_CS_PORT |= (1<<MMC_CS_PIN);
	// return success

	return 0;
}

//############################################################################
uint8_t mmcCommand(uint8_t cmd, uint32_t arg)
//############################################################################
{
	uint8_t r1;
	uint8_t retry=0;
	// send command
	spiTransferByte(cmd | 0x40);
	spiTransferByte(arg>>24);
	spiTransferByte(arg>>16);
	spiTransferByte(arg>>8);
	spiTransferByte(arg);
	spiTransferByte(0x95);	// crc valid only for MMC_GO_IDLE_STATE
	// end command
	// wait for response
	// if more than 8 retries, card has timed-out
	// return the received 0xFF
	while((r1 = spiTransferByte(0xFF)) == 0xFF)
		if(retry++ > 8) break;
	// return response
	return r1;
}

⌨️ 快捷键说明

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