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

📄 mem-mmcsd.h

📁 mmc_sd_memory_card_driver_sample_project_files.zip spi接口的sd card driver 示例
💻 H
字号:
//****************************************************************************************
//****************************************************************************************
//  Project Name:		MMC / SD CARD MEMORY CARD FAT16 & FAT 32 DRIVER
//						MMC / SD CARD DRIVER C CODE HEADER 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.
//****************************************************************************************
//****************************************************************************************




//**************************************
//**************************************
//********** DRIVER REVISIONS **********
//**************************************
//**************************************
//
//V1.02
//- First released version.
//
//V1.03
//- Comments where FFC_DI is used we're misleading as they specified DO, relating to the memory card pin, rather than DI used in the code - comments corrected.
//
//V1.04
//- 'while (!FFS_SPI_BUF_FULL)' in function ffs_write_byte had no semicolon so following return statement would occur immediately - corrected.
//
//V1.05
//- Added the following to the function ffs_process in mem-mmcsd.c just after 'ffs_bytes_per_sector = 512;' and just before '//----- READ THE MASTER BOOT RECORD -----'
//as this fixed a user reported issue where writing files to the card would cause the card to require re-formatting:-
//	FFS_CE = 1;							//De-select card
//	ffs_write_byte(0xff);				//Send extra clock pulses in case card is still completing an operation
//	FFS_CE = 0; 						//Re-select card
//
//V1.06
//- Fixed a rare issue where some cards would become corrupted due to the card not processing some commands sent to it.
//In the file mem-mmcsd.h add the following define after #define FFS_DRIVER_GEN_512_BYTE_BUFFER:-
//#define	DO_BUSY_STATE_ACCESS_DELAY	Nop(); 		//('Nop();' is a single cycle null instruction for the C18 compiler, include multiple times if required to provide time for the card to drive the DO line if it needs to)
//
//Then in the file mem-mmcsd.c search on all 3 occurrences of the following line:-
//	while (FFC_DI == 0)
//and add the this line before each:-
//	DO_BUSY_STATE_ACCESS_DELAY;
//
//- The driver was not specifically using a low SPI bus speed for initialising a new card, which some cards require.
//In the file mem-mmcsd.h add the following define after #define FFS_CD_PIN_NC:-
// //SPI BUS LOW SPEED (Max 400KHz)
// //- Adjust as required for the microcontroller and instruction clock frequency being used 
// #define	SPI_BUS_SET_TO_LOW_SPEED	SSPCON1bits.SSPM3 = 0; SSPCON1bits.SSPM2 = 0; SSPCON1bits.SSPM1 = 1; SSPCON1bits.SSPM0 = 0;
//
// //SPI BUS FULL SPEED (Max 20MHz)
// //- Adjust as required for the microcontroller and instruction clock frequency being used 
// #define	SPI_BUS_SET_TO_FULL_SPEED	SSPCON1bits.SSPM3 = 0; SSPCON1bits.SSPM2 = 0; SSPCON1bits.SSPM1 = 0; SSPCON1bits.SSPM0 = 0;
//
//Then in the function ffs_process add the following after //----- INITIALISE NEW MMC / SD CARD -----
//	SPI_BUS_SET_TO_LOW_SPEED;
//And add the following before //Send command 1 (do initialise)
//	SPI_BUS_SET_TO_FULL_SPEED;




//*****************************
//*****************************
//********** DEFINES **********
//*****************************
//*****************************
#ifndef MMCSD_C_INIT		//Do only once the first time this file is used
#define	MMCSD_C_INIT



//----------------------------------------------
//----- DEFINE TARGET COMPILER & PROCESSOR -----
//----------------------------------------------
//(ONLY 1 SHOULD BE INCLUDED, COMMENT OUT OTHERS - ALSO SET IN THE OTHER DRIVER .h FILE)
#define	FFS_USING_MICROCHIP_C18_COMPILER
//<< add other compiler types here



//----------------------
//----- IO DEFINES -----									//<<<<< CHECK FOR A NEW APPLICATION <<<<<
//----------------------

#ifdef FFS_USING_MICROCHIP_C18_COMPILER

//CONTROL PINS:-
#define	FFS_CE						LATCbits.LATC1			//Chip select pin
#define	FFC_DI						PORTCbits.RC4			//DO pin of SD card, DI pin of processor (used to check if pin is being pulled low by card)

//SPI BUS CONTROL REGISTERS
#define	FFS_SPI_BUF_FULL			SSPSTATbits.BF			//>0 when the SPI receive buffer contains a received byte, also signifying that transmit is complete
#define	FFS_SPI_TX_BYTE(data)		SSPBUF = data			//Macro to write a byte and start transmission over the SPI bus
#define	FFS_SPI_RX_BYTE_BUFFER		SSPBUF					//Register to read last received byte from

//CARD DETECT PIN (Defined like this so the pin can be connected to an input latch IC instead of directly to the processor if desired)
#define	FFS_CD_PIN_REGISTER			PORTC
#define	FFS_CD_PIN_BIT				0x04				//(0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02 or 0x01)
//#define	FFS_CD_PIN_FUNCTION							//Optional function to call to get the FFS_RESET_PIN_REGISTER.  Comment out if not requried
//#define	FFS_CD_PIN_NC								//Include if card detect pin is normally closed (breaks when card inserted) or comment out if normally open (0V common assumed)


//SPI BUS LOW SPEED (Max 400KHz)
//- Adjust as required for the microcontroller and instruction clock frequency being used
#define	SPI_BUS_SET_TO_LOW_SPEED	SSPCON1bits.SSPM3 = 0; SSPCON1bits.SSPM2 = 0; SSPCON1bits.SSPM1 = 1; SSPCON1bits.SSPM0 = 0;

//SPI BUS FULL SPEED (Max 20MHz)
//- Adjust as required for the microcontroller and instruction clock frequency being used
#define	SPI_BUS_SET_TO_FULL_SPEED	SSPCON1bits.SSPM3 = 0; SSPCON1bits.SSPM2 = 0; SSPCON1bits.SSPM1 = 0; SSPCON1bits.SSPM0 = 0;


#define	FFS_DRIVER_GEN_512_BYTE_BUFFER	ffs_general_buffer		//This general buffer is used by routines and may be the same as the buffer that
																//the application uses to read and write data from and to the card if ram is limited

#define	DO_BUSY_STATE_ACCESS_DELAY	Nop(); 				//('Nop();' is a single cycle null instruction for the C18 compiler, include multiple times if required to provide time for the card to drive the DO line if it needs to)

#endif		//#ifdef FFS_USING_MICROCHIP_C18_COMPILER







//PROCESS CARD STATE MACHINE STATES
typedef enum _FFS_PROCESS_STATE
{
    FFS_PROCESS_NO_CARD,
    FFS_PROCESS_WAIT_FOR_CARD_FULLY_INSERTED,
    FFS_PROCESS_CARD_INITIALSIED
} FFS_PROCESS_STATE;


#endif




//*******************************
//*******************************
//********** FUNCTIONS **********
//*******************************
//*******************************
#ifdef MMCSD_C
//-----------------------------------
//----- INTERNAL ONLY FUNCTIONS -----
//-----------------------------------
BYTE ffs_check_command_response_byte (BYTE mask, BYTE data_requried);


//-----------------------------------------
//----- INTERNAL & EXTERNAL FUNCTIONS -----
//-----------------------------------------
//(Also defined below as extern)
void ffs_process (void);
BYTE ffs_is_card_present (void);
void ffs_read_sector_to_buffer (DWORD sector_lba);
void ffs_write_sector_from_buffer (DWORD sector_lba);
BYTE ffs_write_byte (BYTE data);
WORD ffs_read_word (void);
BYTE ffs_read_byte (void);




#else
//------------------------------
//----- EXTERNAL FUNCTIONS -----
//------------------------------
extern void ffs_process (void);
extern BYTE ffs_is_card_present (void);
extern void ffs_read_sector_to_buffer (DWORD sector_lba);
extern void ffs_write_sector_from_buffer (DWORD sector_lba);
extern BYTE ffs_write_byte (BYTE data);
extern WORD ffs_read_word (void);
extern BYTE ffs_read_byte (void);



#endif




//****************************
//****************************
//********** MEMORY **********
//****************************
//****************************
#ifdef MMCSD_C
//--------------------------------------------
//----- INTERNAL ONLY MEMORY DEFINITIONS -----
//--------------------------------------------
BYTE sm_ffs_process = FFS_PROCESS_NO_CARD;
WORD file_system_information_sector;
BYTE ffs_no_of_heads;
BYTE ffs_no_of_sectors_per_track;
DWORD ffs_no_of_partition_sectors;





//--------------------------------------------------
//----- INTERNAL & EXTERNAL MEMORY DEFINITIONS -----
//--------------------------------------------------
//(Also defined below as extern)
WORD number_of_root_directory_sectors;				//Only used by FAT16, 0 for FAT32
BYTE ffs_buffer_needs_writing_to_card;
DWORD ffs_buffer_contains_lba = 0xffffffff;
DWORD fat1_start_sector;
DWORD root_directory_start_sector_cluster;			//Start sector for FAT16, start clustor for FAT32
DWORD data_area_start_sector;
BYTE disk_is_fat_32;
BYTE sectors_per_cluster;
DWORD last_found_free_cluster;
DWORD sectors_per_fat;
BYTE active_fat_table_flags;
DWORD read_write_directory_last_lba;
WORD read_write_directory_last_entry;


#else
//---------------------------------------
//----- EXTERNAL MEMORY DEFINITIONS -----
//---------------------------------------
extern WORD number_of_root_directory_sectors;				//Only used by FAT16, 0 for FAT32
extern BYTE ffs_buffer_needs_writing_to_card;
extern DWORD ffs_buffer_contains_lba;
extern DWORD fat1_start_sector;
extern DWORD root_directory_start_sector_cluster;
extern DWORD data_area_start_sector;
extern BYTE disk_is_fat_32;
extern BYTE sectors_per_cluster;
extern DWORD last_found_free_cluster;
extern DWORD sectors_per_fat;
extern BYTE active_fat_table_flags;
extern DWORD read_write_directory_last_lba;
extern WORD read_write_directory_last_entry;



#endif


⌨️ 快捷键说明

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