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

📄 sddriver.h

📁 SD卡的SPI驱动程序
💻 H
字号:
/****************************************Copyright (c)**************************************************
**                               Guangzhou ZLG-MCU Development Co.,LTD.
**                                      graduate school
**                                 http://www.zlgmcu.com
**
**--------------File Info-------------------------------------------------------------------------------
** File name:			sddriver.h
** Last modified Date:	2005-1-6
** Last Version:		V1.0
** Descriptions:		Soft Packet of SD Card Driver: user API funciton(header file)
**
**------------------------------------------------------------------------------------------------------
** Created by:			Ming Yuan Zheng
** Created date:		2005-1-6
** Version:				V1.0
** Descriptions:		The original version
**
**------------------------------------------------------------------------------------------------------
** Modified by:			Jing.Zhang
** Modified date:		2005-12-19
** Version:				
** Descriptions:		
**
**------------------------------------------------------------------------------------------------------
** Modified by: 
** Modified date:
** Version:	
** Descriptions: 
**
********************************************************************************************************/
#ifndef __SD_DRIVER_H__
#define __SD_DRIVER_H__

/* the information structure variable of SD Card*/
typedef struct SD_STRUCT
{	
	INT32U block_num;				/* the number of blocks in a SD card */
	INT32U block_len;				/* the length of a block             */
	INT32U erase_unit;				/* the number of blocks which can be erased at one time */
	
	INT32U timeout_read;			/* Time out of read operation        */
	INT32U timeout_write;			/* Time out of write operation       */
	INT32U timeout_erase;			/* Time out of erase operation       */
	
}sd_struct;

extern sd_struct sds;				

		/**************************************************************

				User's Application functions for SD card operation
				
		**************************************************************/
		
/*  error code */
#define   SD_NO_ERR			     	0x00			// No errors
#define   SD_ERR_NO_CARD		   -0x01			// No SD card in the socket
#define   SD_ERR_USER_PARAM        -0x02			// user's parameters illegal
#define   SD_ERR_CARD_PARAM		   -0x03			// card's parameters illegal
#define	  SD_ERR_VOL_NOTSUSP       -0x04			// unsupported 3.3v voltage
#define   SD_ERR_OVER_CARDRANGE	   -0x05			// Over capability

/* error code for command */
#define   SD_ERR_CMD_RESPTYPE	   -0x10			// Command type error
#define   SD_ERR_CMD_TIMEOUT       -0x11			// response time out
#define   SD_ERR_CMD_RESP		   -0x12			// response error
			
/* error code for data flow */
#define   SD_ERR_DATA_CRC16        -0x20			// CRC16 fail
#define   SD_ERR_DATA_START_TOK	   -0x21			// read or write start data token is not correct
#define	  SD_ERR_DATA_RESP		   -0x22			// response data token is not correct

/* error code for wait operation */
#define   SD_ERR_TIMEOUT_WAIT      -0x30			// time out
#define   SD_ERR_TIMEOUT_READ      -0x31			// time out for read operation
#define	  SD_ERR_TIMEOUT_WRITE	   -0x32			// time out for write operation
#define   SD_ERR_TIMEOUT_ERASE     -0x33			// time out for erase operation
#define	  SD_ERR_TIMEOUT_WAITIDLE  -0x34			// time out for waiting for idle status

/* error code for write operation */
#define	  SD_ERR_WRITE_BLK		   -0x40			// Write block error
#define	  SD_ERR_WRITE_BLKNUMS     -0x41			// inconsistent block number
#define   SD_ERR_WRITE_PROTECT	   -0x42			// Write protect
	
		
/* initialize sd card */
extern int SD_Initialize(void);	
							
/* read a single block from sd card */
extern int SD_ReadBlock(INT32U blockaddr, INT8U *recbuf);  

/* read multi blocks from sd card */																	
extern int SD_ReadMultiBlock(INT32U blockaddr, INT32U blocknum, INT8U *recbuf); 
 
/* write a block to sd card */               
extern int SD_WriteBlock(INT32U blockaddr, const INT8U *sendbuf);  

/* write multi blocks to sd card */  
extern int SD_WriteMultiBlock(INT32U blockaddr, INT32U blocknum, const INT8U *sendbuf); 

/* Erase the block of sd card */
extern int SD_EraseBlock(INT32U startaddr, INT32U blocknum); 

/* get information of sd card */
extern int SD_GetCardInfo(void);

/*  calculate timeout from CSD register */
void SD_CalTimeout(INT8U *csdbuf);

/*  active sd to initialize process */
int SD_ActiveInit(void);

	    	    	    
/* Mask off the bits in the OCR corresponding to voltage range 3.2V to
 * 3.4V, OCR bits 20 and 21 */
#define MSK_OCR_33			 	0xC0

/* Number of tries to wait for the card to go idle during initialization */
#define SD_IDLE_WAIT_MAX     	1000

/* timeout of command */
#define SD_CMD_TIMEOUT   	 	100
	
/* 100ms correspond to SPI clock(unit: 8 clocks)*/
#define READ_TIMEOUT_100MS      (100 * SPI_CLOCK / 1000 / 8)

/* 250ms correspond to SPI clock(unit: 8 clocks)*/
#define WRITE_TIMEOUT_250MS		(250 * SPI_CLOCK / 1000 / 8)    



#define TAAC_POS 			1			//TACC
#define NSAC_POS			2			//NSAC

#define READ_BL_LEN_POS		5			//READ_BL_LEN

#define C_SIZE_POS1			6			//C_SIZE upper  2-bit
#define C_SIZE_POS2			7			//C_SIZE middle 8-bit
#define C_SIZE_POS3			8			//C_SIZE lower	2-bit

#define C_SIZE_MULT_POS1	9			//C_SIZE_MULT upper 2-bit
#define C_SIZE_MULT_POS2	10			//C_SIZE_MULT lower 1-bit	

#define SECTOR_SIZE_POS1	10			//SECTOR_SIZE upper 5-bit
#define SECTOR_SIZE_POS2	11			//SECTOR_SIZE lower 2-bit

#define R2WFACTOR_POS 		12			//R2WFACTOR_POS


#define TAAC_MSK			0x07		//TACC 
#define NSAC_MSK			0x78		//NSAC 

#define READ_BL_LEN_MSK		0x0F		//READ_BL_LEN 

#define C_SIZE_MSK1			0x03		//C_SIZE 
#define C_SIZE_MSK3			0xC0		//C_SIZE 

#define C_SIZE_MULT_MSK1 	0x03		//C_SIZE_MULT 
#define C_SIZE_MULT_MSK2 	0x80		//C_SIZE_MULT 

#define R2WFACTOR_MSK		0x1C		//R2WFACTOR 

#define SECTOR_SIZE_MSK1	0x3F		//SECTOR_SIZE lower 5 bits
#define SECTOR_SIZE_MSK2	0x80		//SECTOR_SIZE higher 1 bits

#endif /* __SD_DRIVER_H__ */

⌨️ 快捷键说明

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