📄 sd.h
字号:
//*****************************************************************************
//
// File Name : 'sd.h'
// Title : SD Flash Card Interface
// Author : Andy Zhu - Copyright (C) 2008
// Created : 2008.07.20
// Revised : 2008.07.20
// Version : 0.1
// Target MCU :
//
// Description : This library offers some simple function which can be used
// to read and write data on a MultiMedia or SecureDigital (SD) Flash
// Card. Although MM and SD Cards are designed to operate with their own
// special bus wiring and protocols, both types of cards also provide a
// simple SPI-like interface mode which is exceptionally useful when
// attempting to use the cards in embedded systems.
//
// To work with this library, the card must be wired to the SPI port of
// the Atmel microcontroller as described below.
// _________________
// / 1 2 3 4 5 6 78 | <- view of MMC/SD card looking at contacts
// / 9 | Pins 8 and 9 are present only on SD cards
// | MMC/SD Card |
// /\/\/\/\/\/\/\/\/\/
//
// 1 - CS (chip select) - wire to any available I/O pin(*)
// 2 - DIN (data in, host->card) - wire to SPI MOSI pin
// 3 - VSS (ground) - wire to ground
// 4 - VDD (power, 3.3V only?) - wire to power (MIGHT BE 3.3V ONLY!)
// 5 - SCLK (data clock) - wire to SPI SCLK pin
// 6 - VSS (ground) - wire to ground
// 7 - DOUT (data out, card->host) - wire to SPI MISO pin
//*****************************************************************************
#ifndef __sd_h__
#define __sd_h__
// SD card chip select pin defines
#define SD_CS_PORT PORTB
#define SD_CS_DDR DDRB
#define SD_CS_PIN PB6
#define ERROR 0xff
#define SD_CS_SELECT() (SD_CS_PORT&=~(1<<SD_CS_PIN))
#define SD_CS_RELEASE() (SD_CS_PORT|=(1<<SD_CS_PIN))
//SD commands
#define SD_GO_IDLE_STATE 0
#define SD_SEND_OP_COND 1
#define SD_SEND_CSD 9
#define SD_SEND_CID 10
#define SD_SEND_STATUS 13
#define SD_SET_BLOCKLEN 16
#define SD_READ_SINGLE_BLOCK 17
#define SD_WRITE_BLOCK 24
#define SD_PROGRAM_CSD 27
#define SD_SET_WRITE_PROT 28
#define SD_CLR_WRITE_PROT 29
#define SD_SEND_WRITE_PROT 30
#define SD_TAG_SECTOR_START 32
#define SD_TAG_SECTOR_END 33
#define SD_UNTAG_SECTOR 34
#define SD_TAG_ERASE_GROUP_START 35
#define SD_TAG_ERARE_GROUP_END 36
#define SD_UNTAG_ERASE_GROUP 37
#define SD_ERASE 38
#define SD_APP_CMD 55
#define SD_READ_OCR 58
#define SD_CRC_ON_OFF 59
#define SD_SEND_SCR_APP 51
// R1 Response bit-defines
#define SD_R1_BUSY 0x80
#define SD_R1_PARAMETER_ERR 0x40
#define SD_R1_ADDRESS_ERR 0x20
#define SD_R1_ERASE_SEQ_ERR 0x10
#define SD_R1_COM_CRC_ERR 0x08
#define SD_R1_ILLEGAL_COM 0x04
#define SD_R1_ERASE_RESET 0x02
#define SD_R1_IDLE_STATE 0x01
//-----------------------------------------
typedef struct
{
unsigned char OCR_3;
unsigned char OCR_2;
unsigned char OCR_1;
unsigned char OCR_0;
}OCR_Reg; //一般此四个值依次为0x80,0xff,0x80,0x00 代表Operatin Voltage 2.7V–3.6V
typedef struct
{
unsigned char MID; //Manufacturer ID
unsigned int OID; //OEM/Application ID
unsigned char PNM[5]; //Product Name
unsigned char PRV; //Product Revision
unsigned char PSN[4]; //Serial Numbe
unsigned int MDT; //Manufacture Date Code
unsigned char CID_CRC7; //CRC7 checksum
}CID_Reg;
typedef struct
{
unsigned char CSD_STRUCTURE; //CSD structure
unsigned char TAAC; //data read access-time-1
unsigned char NSAC; //data read access-time-2 in CLK cycles
unsigned char TRAN_SPEED; //max. data transfer rate
unsigned int CCC; //card command classes
unsigned char READ_BL_LEN; //max. read data block length
unsigned char READ_BL_PARTIAL; //partial blocks for read allowed
unsigned char WRITE_BLK_MISALIGN; //write block misalignment
unsigned char READ_BLK_MISALIGN; //read block misalignment
unsigned char DSR_IMP; //DSR implemented
unsigned int C_SIZE; //device size
unsigned char VDD_R_CURR_MIN; //max. read current @VDD min
unsigned char VDD_R_CURR_MAX; //max. read current @VDD max
unsigned char VDD_W_CURR_MIN; //max. write current @VDD min
unsigned char VDD_W_CURR_MAX; //max. write current @VDD max
unsigned char C_SIZE_MULT; //device size multiplier
unsigned char ERASE_BLK_EN; //erase single block enable
unsigned char SECTOR_SIZE ; //erase sector size
unsigned char WP_GRP_SIZE; //write protect group size
unsigned char WP_GRP_ENABLE; //write protect group enable
unsigned char R2W_FACTOR; //write speed factor Binary MLC
unsigned char WRITE_BL_LEN; //max. write data block length
unsigned char WRITE_BL_PARTIAL; //partial blocks for write allowed
unsigned char FILE_FORMAT_GRP; //File format group
unsigned char COPY; //copy flag (OTP)
unsigned char PERM_WRITE_PROTECT; //permanent write protection
unsigned char TMP_WRITE_PROTECT; //temporary write protection
unsigned char FILE_FORMAT; //File format
unsigned char CSD_CRC7; //CRC
}CSD_Reg;
typedef struct
{
unsigned char SCR_STRUCTURE; //SCR Structure
unsigned char SD_SPEC; //SD Card—Spec. Version
unsigned char DATA_STAT_AFTER_ERASE; //data_status_after erases
unsigned char SD_SECURITY; //SD Security Support
unsigned char SD_BUS_WIDTHS; //DAT Bus widths supported
}SCR_Reg;
//CID_Reg SD_CID_infor;
typedef struct
{
//unsigned long int BLOCKNR;
//unsigned int MULT;
//unsigned int BLOCK_LEN;
unsigned long int Capacity; //SD卡总的容量(单位/字节)
unsigned int size_MB; //SD卡的容量(单位/兆)
//unsigned int sector_count; //SD卡总的扇区数
}VOLUME_INFO_TYPE;
//------------------------------------------
CID_Reg SD_CID_infor;
CSD_Reg SD_CSD_infor;
OCR_Reg SD_OCR_infor;
SCR_Reg SD_SCR_infor;
VOLUME_INFO_TYPE SD_CAPACITY_infor;
//------------------------------------------
void SD_Init(void); //
uint8_t SD_Reset(void);
uint8_t SD_SendCommand(uint8_t cmd, uint32_t arg);
uint8_t SD_Command(uint8_t cmd, uint32_t arg);
uint8_t Read_SD_Reg(uint8_t *buffer,uint8_t CMD,uint8_t length);
uint8_t SD_ReadSingleBlock(uint32_t sector, uint8_t *buffer);
//uint8_t SD_card_infor_read(uint8_t cmd,uint8_t *pt,uint8_t length);
//uint8_t READ_CID_Reg(CID_Reg *buf);
void Get_SD_card_CID_Infor(unsigned char *tag_buf);
void Get_SD_card_CSD_Infor(unsigned char *tag_buf);
void Get_SD_card_OCR_Infor(unsigned char *tag_buf);
void Get_SD_card_SCR_Infor(unsigned char *tag_buf);
void Get_SD_card_Volume_Infor(void);
//-------------------------------------------
#endif //----------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -