#ifndef __SDCARD_H
#define __SDCARD_H

#include "sys.h"

#define 	_USE_WRITE 1

#ifndef 	SDCARD_CS_PIN
#define   SDCARD_CS_PIN   P26        //片选脚
#endif

#ifndef 	SDCARD_CS_PIN_mode
#define		SDCARD_CS_PIN_mode	P2M1&=~0x40;P2M0|=0x40;     //P26推挽输出
#endif 

//SD卡的命令
#define     CMD0            (0x40+0)     	/* GO_IDLE_STATE */
#define     CMD1            (0x40+1)     	/* SEND_OP_COND */
#define     CMD8            (0x40+8)     	/* SEND_IF_COND */
#define     CMD9            (0x40+9)     	/* SEND_CSD */
#define     CMD10           (0x40+10)    	/* SEND_CID */
#define     CMD12           (0x40+12)    	/* STOP_TRANSMISSION */
#define     CMD16           (0x40+16)    	/* SET_BLOCKLEN */
#define     CMD17           (0x40+17)    	/* READ_SINGLE_BLOCK */
#define     CMD18           (0x40+18)    	/* READ_MULTIPLE_BLOCK */
#define     CMD23           (0x40+23)    	/* SET_BLOCK_COUNT */
#define     CMD24           (0x40+24)    	/* WRITE_BLOCK */
#define     CMD25           (0x40+25)    	/* WRITE_MULTIPLE_BLOCK */
#define     CMD41           (0x40+41)    	/* SEND_OP_COND (ACMD) */
#define     CMD55           (0x40+55)    	/* APP_CMD */
#define     CMD58           (0x40+58)    	/* READ_OCR */
#define     ACMD41          (0xC0+41)      

/* MMC card type flags (MMC_GET_TYPE) */
#define     CT_MMC		    0x01		/* MMC ver 3 */
#define     CT_SD1		    0x02		/* SD ver 1 */
#define     CT_SD2		    0x04		/* SD ver 2 */
#define     CT_SDC		    0x06		/* SD */
#define     CT_BLOCK	    0x08		/* Block addressing */

#define     STA_NOINIT		0x01	/* Drive not initialized */
#define     STA_NODISK		0x02	/* No medium in the drive */

/*-----------------------------------------------------------------------*/
/* diskio.h                                                                 */
/*-----------------------------------------------------------------------*/
/* Status of Disk Functions */
typedef  uint8	DSTATUS;

/* Results of Disk Functions */
typedef enum {
	RES_OK = 0,		/* 0: Function succeeded */
	RES_ERROR,		/* 1: Disk error */
	RES_NOTRDY,		/* 2: Not ready */
	RES_PARERR		/* 3: Invalid parameter */
} DRESULT;

DSTATUS disk_initialize ();
DRESULT disk_readp (uint8* buff, uint32 sector, uint16 offser, uint16 count);
DRESULT disk_writep (const uint8* buff, uint32 sc);

#define STA_NOINIT		0x01	/* Drive not initialized */
#define STA_NODISK		0x02	/* No medium in the drive */


#define	    SELECTING       SDCARD_CS_PIN	/* CS status (true:CS low) */

#endif //   sdcard.h

