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

📄 main.c

📁 This zip describes MCI communication with an MMC and SDCard Device. Includes main.html file for help
💻 C
字号:
//*----------------------------------------------------------------------------
//*         ATMEL Microcontroller Software Support  -  ROUSSET  -
//*----------------------------------------------------------------------------
//* The software is delivered "AS IS" without warranty or condition of any
//* kind, either express, implied or statutory. This includes without
//* limitation any warranty or condition with respect to merchantability or
//* fitness for any particular purpose, or against the infringements of
//* intellectual property rights of others.
//*----------------------------------------------------------------------------
//* File Name           : main.c
//* Object              : main application written in C
//* Creation            : FB   21/11/2002
//*
//*----------------------------------------------------------------------------
#include <stdio.h>

#include "AT91C_MCI_Device.h"

#define AT91C_MCI_TIMEOUT			1000000   /* For AT91F_MCIDeviceWaitReady */
#define BUFFER_SIZE_MCI_DEVICE		512
#define MASTER_CLOCK				60000000
#define FALSE						-1
#define TRUE						1

//* External Functions
extern void 					AT91F_DBGU_Printk(char *);
extern void 					AT91F_ASM_MCI_Handler(void);
extern void 					AT91F_MCI_Device_Handler(AT91PS_MciDevice,unsigned int);
extern AT91S_MCIDeviceStatus 	AT91F_MCI_SDCard_Init (AT91PS_MciDevice);
extern AT91S_MCIDeviceStatus	AT91F_MCI_MMC_Init(AT91PS_MciDevice);
extern AT91S_MCIDeviceStatus 	AT91F_MCI_SetBlocklength(AT91PS_MciDevice,unsigned int);
extern AT91S_MCIDeviceStatus 	AT91F_MCI_MMC_SelectCard(AT91PS_MciDevice,unsigned int);
extern AT91S_MCIDeviceStatus 	AT91F_MCI_ReadBlock(AT91PS_MciDevice,int,unsigned int *,int);
extern AT91S_MCIDeviceStatus 	AT91F_MCI_WriteBlock(AT91PS_MciDevice,int,unsigned int *,int);
//* Global Variables
AT91S_MciDeviceFeatures			MCI_Device_Features;
AT91S_MciDeviceDesc				MCI_Device_Desc;
AT91S_MciDevice					MCI_Device;
char							Buffer[BUFFER_SIZE_MCI_DEVICE];

//*----------------------------------------------------------------------------
//* \fn    AT91F_MCIDeviceWaitReady
//* \brief Wait for MCI Device ready
//*----------------------------------------------------------------------------
void AT91F_MCIDeviceWaitReady(unsigned int timeout)
{
	volatile int status;
	
	do
	{
		status = AT91C_BASE_MCI->MCI_SR;
		timeout--;
	}
	while( !(status & AT91C_MCI_NOTBUSY)  && (timeout>0) );	
}

//*----------------------------------------------------------------------------
//* \fn    AT91F_Test
//* \brief Test Functions
//*----------------------------------------------------------------------------
int AT91F_Test(void)
{
	int i;
	unsigned int Max_Read_DataBlock_Length;
		
	Max_Read_DataBlock_Length = MCI_Device.pMCI_DeviceFeatures->Max_Read_DataBlock_Length;
	
	//* ReadBlock & WriteBlock Test -> Entire Block

	//* Wait MCI Device Ready
	AT91F_MCIDeviceWaitReady(AT91C_MCI_TIMEOUT);

	//* Read Block 1
	for(i=0;i<BUFFER_SIZE_MCI_DEVICE;i++) 	Buffer[i] = 0x00;	
	AT91F_MCI_ReadBlock(&MCI_Device,(1*Max_Read_DataBlock_Length),(unsigned int*) Buffer,Max_Read_DataBlock_Length);

	//* Wait end of Read
	AT91F_MCIDeviceWaitReady(AT91C_MCI_TIMEOUT);

	//* Write Page 1
    sprintf(Buffer,"\n\rThis sentence is written in your device... Congratulations\n\r");
	AT91F_MCI_WriteBlock(&MCI_Device,(1*Max_Read_DataBlock_Length),(unsigned int*) Buffer,Max_Read_DataBlock_Length);

	//* Wait end of Write
	AT91F_MCIDeviceWaitReady(AT91C_MCI_TIMEOUT);

	//* Read Block 1
	for(i=0;i<BUFFER_SIZE_MCI_DEVICE;i++) 	Buffer[i] = 0x00;	
	AT91F_MCI_ReadBlock(&MCI_Device,(1*Max_Read_DataBlock_Length),(unsigned int*) Buffer,Max_Read_DataBlock_Length);

	//* Wait end of Read
	AT91F_MCIDeviceWaitReady(AT91C_MCI_TIMEOUT);

	//* End Of Test
	AT91F_DBGU_Printk("\n\rTests Completed: !!!\n\r");
	AT91F_DBGU_Printk(Buffer);

	return TRUE;
}

//*----------------------------------------------------------------------------
//* \fn    AT91F_CfgDevice
//* \brief This function is used to initialise MMC or SDCard Features
//*----------------------------------------------------------------------------
void AT91F_CfgDevice(void)
{
	// Init Device Structure

	MCI_Device_Features.Relative_Card_Address 		= 0;
	MCI_Device_Features.Card_Inserted 				= AT91C_CARD_REMOVED;
	MCI_Device_Features.Max_Read_DataBlock_Length	= 0;
	MCI_Device_Features.Max_Write_DataBlock_Length 	= 0;
	MCI_Device_Features.Read_Partial 				= 0;
	MCI_Device_Features.Write_Partial 				= 0;
	MCI_Device_Features.Erase_Block_Enable 			= 0;
	MCI_Device_Features.Sector_Size 				= 0;
	MCI_Device_Features.Memory_Capacity 			= 0;
	
	MCI_Device_Desc.state							= AT91C_MCI_IDLE;
	MCI_Device_Desc.SDCard_bus_width				= AT91C_MCI_SCDBUS;
	
	// Init AT91S_DataFlash Global Structure, by default AT45DB choosen !!!
	MCI_Device.pMCI_DeviceDesc 		= &MCI_Device_Desc;
	MCI_Device.pMCI_DeviceFeatures 	= &MCI_Device_Features;

}

//*----------------------------------------------------------------------------
//* \fn    AT91F_Test_MMC
//* \brief Configure MCI for MMC and complete MMC init, then jump to Test Functions
//*----------------------------------------------------------------------------
int AT91F_Test_MMC(void)
{
	//////////////////////////////////////////////////////////
	//* For MMC Init
	//////////////////////////////////////////////////////////
	
	AT91F_MCI_Configure(AT91C_BASE_MCI,
						AT91C_MCI_DTOR_1MEGA_CYCLES,
						0x834A,						// 400kHz for MCK = 60MHz
						AT91C_MCI_MMC_SLOTA);

	//* Wait MCI Device Ready
	AT91F_MCIDeviceWaitReady(AT91C_MCI_TIMEOUT);

	if(AT91F_MCI_MMC_Init(&MCI_Device) != AT91C_INIT_OK)
		return FALSE;

	// Select MMC Card n

⌨️ 快捷键说明

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