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

📄 mem-mmcsd.c

📁 mmc_sd_memory_card_driver_sample_project_files.zip spi接口的sd card driver 示例
💻 C
字号:
//****************************************************************************************
//****************************************************************************************
//  Project Name:		MMC / SD CARD MEMORY CARD FAT16 & FAT 32 DRIVER
//						MMC / SD CARD DRIVER C CODE FILE
//  Copyright:			EMBEDDED-CODE.COM
//
//  Licenced To:        NONE
//  Licence Number:     NONE
//
//  IMPORTANT: These files are copyright of embedded-code.com and are subject to a licence
//  agreement.  All rights reserved.  Unauthorised use, reproduction or distribution of
//  these files may lead to prosecution.
//  Any use in violation of the licence restrictions may subject the user to criminal
//  sanctions under applicable laws, as well as to civil liability for the breach of the
//  terms and conditions of the license.
//  This software is provided in an "as is" condition. No warranties, whether express,
//  implied or statutory, including, but not limited to, implied warranties of
//  merchantability and fitness for a particular purpose apply to this software. 
//  Embedded-code.com shall not be liable in any circumstances for special, incidental
//  or consequential damages, for any reason whatsoever.
//  Please see 'www.embedded-code.com\licence' or email 'licence@embedded-code.com' for
//  full details.
//  This file contains hidden markers to identify it uniquely to the licence number it was
//  purchased under.
//  DO NOT REMOVE THIS NOTICE - IT IS A REQUIREMENT OF THE LICENCE AGREEMENT.
//****************************************************************************************
//****************************************************************************************



######## "####.#"
####### #######
######## "#########.#"

######## "#######.#"






//*******************************************************
//*******************************************************
//********** DO BACKGROUND MMC / SD CARD TASKS **********
//*******************************************************
//*******************************************************
//This function needs to be called reguarly to detect a new card being inserted so that it can be initialised ready for access.
void ffs_process (void)
{
	#### ####;
	#### ###########;
	#### ######;

	#### ######;
	#### ######;
	##### #######;
	##### ###;
	##### ###########################;
	#### ##########################;
	#### #######################;
	#### *##############;


	###### (##############)
	{
	#### ###################:
		//-------------------------------
		//----- NO CARD IS INSERTED -----
		//-------------------------------
		###### = #;
		
		########### = #;					//Flag that card not OK

		//Reset all file handlers
		### (###### = #; ###### < #############; ######++)
			########[######].#####.####.############ = #;

		//Has a card has been inserted?
		## (###################() == #)
			######;

		//A card has been inserted
		//Pause for 500mS seconds
		############## = ##;
		############## = ########################################;

		######;


    #### ########################################:
		//------------------------------------------------------------
		//----- CARD INSERTED - WAIT FOR IT TO BE FULLY INSERTED -----
		//------------------------------------------------------------
		//(To allow for users that don't insert the card in one nice quick movement)

		//Ensure card is still inserted
		## (###################() == #)
		{
			############## = ###################;
			######;
		}

		//Wait for timer to expire
		## (##############)
			######;
		
		//Initialise the card
		############## = ############################;

		//Actually exit this switch statement to run the card initialise procedure below (this is the only state that doesn't return)
		#####;

    #### ############################:
		//-----------------------------------------
		//----- CARD INSERTED AND INITIALSIED -----
		//-----------------------------------------

		//If card is still inserted then exit
		## (###################())
			######;

		//CARD HAS BEEN REMOVED
		########### = #;
		
		############## = ###################;

		######;


	#######:
		############## = ###################;
		######;
	}

	//----------------------------------------
	//----------------------------------------
	//----- INITIALISE NEW MMC / SD CARD -----
	//----------------------------------------
	//----------------------------------------
	//(The only state that exits the switch statement above is FFS_PROCESS_WAIT_FOR_CARD_RESET when it completes)

	########################;

	########### = #;					//Default to card not OK

	###### = #;								//Ensure card is de-selected

	//Send at least 74 clocks with DI high for card to enter native command mode
	### (###### = #; ###### < ##; ######++)
	{
		##############(####);
	}

	###### = #;								//Select card, telling card to go into SPI mode when it gets the RESET command

	//Send command 0 (reset)
	##############(#### + #);				//Command
	##############(#);						//All arguments zero
	##############(#);
	##############(#);
	##############(#);
	##############(####);					//Hard coded CRC

	###############################(####, ####);		//Get response


	#########################;


	//Send command 1 (do initialise) until the card has completed its initialisation and is ready
	//(This can take some time especially on larger cards - 100's of mS)
	############## = ###;
	###### = #;
	##### ((###### & ####) == #)
	{
		##############(#### + #);				//Command
		##############(#);						//All arguments zero
		##############(#);
		##############(#);
		##############(#);
		##############(####);					//Don't bother with CRC any more as not requried (dummy value)

		###### = ###############################(####, ####);	//Check for valid response
		
		//Do timeout just in case
		##(############## == #)
			#### ######################;
	}

	###### = #;									//De-select card

	##############(####);						//Output extra clock pulses in case card is still completing an operation

	###### = #;									//Select card

	//Send command 16 (block length)
	##############(#### + ##);				//Command
	##############(####);					//Data
	##############(####);
	##############(####);
	##############(####);
	##############(####);					//Dummy CRC


	## (###############################(####, ####) == #)		//Get response
		#### ######################;

	//Set the number of bytes per sector before we move on to general access
	#################### = ###;


	###### = #;							//De-select card
	##############(####);				//Send extra clock pulses in case card is still completing an operation
	###### = #; 						//Re-select card


	//---------------------------------------
	//----- READ THE MASTER BOOT RECORD -----
	//---------------------------------------
	// read sector 1 (LBA 0x00)
	#########################(#);

	//Dump the first 446 bytes of boot up executable code
	############## = &##############################[#];
	############## += ###;

	//Now at start of the partition table [0x000001be]
	//Check for Partition 1 active (0x00 = inactive, 0x80 = active) [1]
	//(We allow a value of 0x00 for partition 1 as this partition must be present and on some disks a value of 0x00 has been found)
	//if (*buffer_pointer++ != 0x80)
	//	goto init_new_ffs_card_fail
	##############++;

	//Get 'Beginning of Partition - Head' [0x000001bf]
	#### = *##############++;


	//Get 'Beginning of Partition - Cylinder + Sector' [0x000001c0]
	###### = (####)*##############++;
	###### |= (####)(*##############++) << #;

	########### = (###### >> #);				//Yes this is correct - strange bit layout in this register!
	## (###### & ######)
		########### += ######;
	## (###### & ######)
		########### += ######;

	###### = (####)(###### & ######);

	//Read the 'Type Of Partition' [0x000001c2]
	//(We accept FAT16 or FAT32)
	###### = *##############++;

	## (###### == ####)						//FAT16 (smaller than 32MB)
		############## = #;
	#### ## (###### == ####)				//FAT16 (larger than 32MB)
		############## = #;
	#### ## (###### == ####)				//FAT32 (Partition Up to 2048GB)
		############## = #;
	#### ## (###### == ####)				//FAT32 (Partition Up to 2048GB - uses 13h extensions)
		############## = #;
	#### ## (###### == ####)				//FAT16 (partition larger than 32MB, uses 13h extensions)
		############## = #;
	####
		#### ######################;

	//Get end of partition - head [0x000001c3]
	##############++;

	//Get end of partition - Cylinder & Sector [0x000001c4]
	##############++;
	##############++;

	//----- GET START ADDRESS OF PARTITION 1 -----
	//Get no of sectors between MBR and the first sector in the partition [0x000001c6]
	########################### = (#####)*##############++;
	########################### |= (#####)(*##############++) << #;
	########################### |= (#####)(*##############++) << ##;
	########################### |= (#####)(*##############++) << ##;
	//WE NOW HAVE THE START ADDRESS OF THE FIRST PARTITION (THE ONLY PARTITION WE LOOK AT)

	//Get no of sectors in the partition - could be useful when we do writing of files [0x000001ca]
	########################### = (#####)*##############++;
	########################### |= (#####)(*##############++) << #;
	########################### |= (#####)(*##############++) << ##;
	########################### |= (#####)(*##############++) << ##;



	//------------------------------------------
	//---- READ THE PARTITION 1 BOOT RECORD ----
	//------------------------------------------
	//Setup for finding the FAT1 table start address root directory start address and data area start address
	### = ###########################;

	#########################(###);
	############## = &##############################[#];

	//Dump jump code & OEM name (11 bytes)
	############## += ##;

	//Get 'Bytes Per Sector' [# + 0x000b]
	//Check value is 512
	#################### = (####)*##############++;
	#################### |= (####)(*##############++) << #;
	## (#################### != ###)
		#### ######################;

	//Get 'Sectors Per Cluster' [# + 0x000d]
	//(Restricted to powers of 2 (1, 2, 4, 8, 16, 32

⌨️ 快捷键说明

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