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

📄 sdmmc.h

📁 SD卡驱动
💻 H
字号:
/******************************************************************************
 *
 *       PIC18 C18 Secure Digital and Multimedia Cards Interface
 *
 ******************************************************************************
 * FileName:        sdmmc.h
 * Dependencies:    generic.h
 *					_fat.def
 *					_FATDefs.h
 * Processor:       PIC18
 * Compiler:        C18
 * Company:         Microchip Technology, Inc.
 *
 * Software License Agreement
 *
 * The software supplied herewith by Microchip Technology Incorporated
 * (the 揅ompany? for its PICmicro?Microcontroller is intended and
 * supplied to you, the Company抯 customer, for use solely and
 * exclusively on Microchip PICmicro Microcontroller products. The
 * software is owned by the Company and/or its supplier, and is
 * protected under applicable copyright laws. All rights are reserved.
 * Any use in violation of the foregoing 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 this
 * license.
 *
 * THIS SOFTWARE IS PROVIDED IN AN 揂S 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. THE COMPANY SHALL NOT,
 * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
 *
*****************************************************************************/

#ifndef SDMMC_H
#define	SDMMC_H

#include "generic.h"
#include "_fat.def"
#include "_FATDefs.h"

#define FALSE	0
#define TRUE	!FALSE


#if defined PIC24F256GB106
	//cs is defined as out;
	#define CS_HIGH				LATBbits.LATB8 = 1
	#define CS_LOW              LATBbits.LATB8 = 0   
	#define CS_DDR				TRISBbits.TRISB8 = 0

#else if defined PIC24FJ128GA006
	
	#define CS_HIGH				LATFbits.LATF1 = 1
	#define CS_LOW              LATFbits.LATF1 = 0   
	#define CS_DDR				TRISFbits.TRISF1 = 0

	//SCK
	#define SCK_DDR  TRISFbits.TRISF6 
	#define SCK_LOW  LATFbits.LATF6 = 0
	#define SCK_HIGH LATFbits.LATF6 = 1
	//SD0
	#define SDO_DDR  TRISFbits.TRISF3
	#define SDO_HIGH LATFbits.LATF3 = 0
	#define SDO_LOW  LATFbits.LATF3 = 1
	//SDI
	#define SDI_DDR  TRISFbits.TRISF2
	#define SDI_HIGH LATFbits.LATF2 = 0
	#define SDI_LOW  LATFbits.LATF2 = 1
#endif
#define FO_MBR          0L  // master boot record sector LBA
//#define SPICON					SPI1CON1
//#define SPISTAT					SPI1STAT
//#define SPIBUF					SPI1BUF
//#define SPISTAT_RBF				SPI1STATbits.SPIRBF
//---------------------------------------------------------------------------------
typedef struct SD_STRUCT
{	
	long block_num;				/* 卡中块的数量 */
	int block_len;				/* 卡的块长度(单位:字节) */
	int erase_unit;				/* 一次可擦除的块个数 */
	
	int timeout_read;				/* 读块超时时间(单位: 8 SPI clock) */
	int timeout_write;			/* 写块超时时间(单位: 8 SPI clock) */
	int timeout_erase;			/* 擦块超时时间(单位: 8 SPI clock) */
	
}sd_struct;

/* CSD中一些域的字节位置(高字节在前) */
#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


//CSD中一些域的掩码
#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 高2位掩码
#define C_SIZE_MSK3			0xC0		//C_SIZE 低2位掩码

#define C_SIZE_MULT_MSK1 	0x03		//C_SIZE_MULT 的高2位掩码
#define C_SIZE_MULT_MSK2 	0x80		//C_SIZE_MULT 的低2位掩码

#define R2WFACTOR_MSK		0x1C		//R2WFACTOR 掩码
//------------------------------------------------------------------------
#define SECTOR_SIZE_MSK1	0x3F		//SECTOR_SIZE 的高5位
#define SECTOR_SIZE_MSK2	0x80		//SECTOR_SIZE 的低2位
#define MEDIA_SECTOR_SIZE 		512
#define SECTOR_SHIFT_BITS		9

#define INPUT	1
#define OUTPUT	0

#define low(num) (num & 0xFF)
#define high(num) ((num >> 8) & 0xFF)
#define upper(num) ((num >> 16) & 0xFF)

#define CLKSPERINSTRUCTION (BYTE) 2
#define TMR1PRESCALER	(BYTE)    8
#define TMR1OVERHEAD	(BYTE)    5	
#define MILLISECDELAY   (WORD)((SYSTEM_CLOCK/CLKSPERINSTRUCTION/TMR1PRESCALER/(WORD)1000) - TMR1OVERHEAD)
#define B115K26MHZ      0x0C        // = 115.2K baud @26MHz

BYTE MediaInit(void);
#define MediaIsPresent()			(!SD_CD)
#define MediaIsWriteProtected()		(SD_WE)
BYTE MediaDetect(void);
BYTE MediaInitialize(void);
BYTE SECTORread(DWORD sector_addr, BYTE* buffer);
BYTE SECTORwrite(DWORD sector_addr, BYTE* buffer);
extern BYTE gblSDMMCBuf[];
extern DISK glbDiskData;
BYTE MediaDetect(void);
BYTE WriteProtectState(void);
void ShutdownMedia(void);

#endif

⌨️ 快捷键说明

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