📄 sd_spi.h
字号:
#ifndef __SD_SPI_H__
#define __SD_SPI_H__
#include "define.h"
//typedef unsigned char uint8;
//typedef unsigned int uint16;
//typedef unsigned long uint32;
// 命令响应长度
#define R1 1
#define R1B 2
#define R2 3
#define R3 4
// 错误码(R1和R2高字节)
#define MSK_IDLE 0x01
#define MSK_ERASE_RST 0x02
#define MSK_ILL_CMD 0x04
#define MSK_CRC_ERR 0x08
#define MSK_ERASE_SEQ_ERR 0x10
#define MSK_ADDR_ERR 0x20
#define MSK_PARAM_ERR 0x40
// 错误码(R2低字节)
#define MSK_TOK_ERROR 0x01
#define MSK_TOK_CC_ERROR 0x02
#define MSK_TOK_ECC_FAILED 0x04
#define MSK_TOK_CC_OUTOFRANGE 0x08
#define MSK_TOK_CC_LOCKED 0x10
// 数据令牌
#define SD_TOK_READ_STARTBLOCK 0xFE
#define SD_TOK_WRITE_STARTBLOCK 0xFE
#define SD_TOK_READ_STARTBLOCK_M 0xFE
#define SD_TOK_WRITE_STARTBLOCK_M 0xFC
#define SD_TOK_STOP_MULTI 0xFD
// 数据响应令牌
#define SD_RESP_DATA_MSK 0x0F // 数据响应掩码
#define SD_RESP_DATA_ACCETPTED 0x05 // 数据被接受
#define SD_RESP_DATA_REJECT_CRC 0x0B // 由于CRC错误而被拒绝
#define SD_RESP_DATA_REJECT_WRITE 0x0D // 由于写错误而被拒绝
// 等待类型
#define SD_WAIT_READ 0x00 // 读等待
#define SD_WAIT_WRITE 0x01 // 写等待
#define SD_WAIT_ERASE 0x02 // 擦除等待
#define SD_READREG_TIMEOUT 30 // 读寄存器超时次数
/******************************** 基本命令集 Basic command set **************************/
/* 复位SD 卡 Reset cards to idle state */
#define CMD0 0
#define CMD0_R R1
#define CMD1 1
#define CMD1_R R1
/* 读CSD寄存器 Card sends the CSD */
#define CMD9 9
#define CMD9_R R1
/* 读CID寄存器 Card sends CID */
#define CMD10 10
#define CMD10_R R1
/* 停止读多块时的数据传输 Stop a multiple block (stream) read/write operation */
#define CMD12 12
#define CMD12_R R1B
/* 读 Card_Status 寄存器 Get the addressed card's status register */
#define CMD13 13
#define CMD13_R R2
/***************************** 块读命令集 Block read commands **************************/
/* 设置块的长度 Set the block length */
#define CMD16 16
#define CMD16_R R1
/* 读单块 Read a single block */
#define CMD17 17
#define CMD17_R R1
/* 读多块,直至主机发送CMD12为止 Read multiple blocks until a CMD12 */
#define CMD18 18
#define CMD18_R R1
/***************************** 块写命令集 Block write commands *************************/
/* 写单块 Write a block of the size selected with CMD16 */
#define CMD24 24
#define CMD24_R R1
/* 写多块 Multiple block write until a CMD12 */
#define CMD25 25
#define CMD25_R R1
/* 写CSD寄存器 Program the programmable bits of the CSD */
#define CMD27 27
#define CMD27_R R1
/***************************** 写保护 Write protection *****************************/
/* Set the write protection bit of the addressed group */
#define CMD28 28
#define CMD28_R R1B
/* Clear the write protection bit of the addressed group */
#define CMD29 29
#define CMD29_R R1B
/* Ask the card for the status of the write protection bits */
#define CMD30 30
#define CMD30_R R1
/***************************** 擦除命令 Erase commands *******************************/
/* 设置擦除块的起始地址 Set the address of the first write block to be erased */
#define CMD32 32
#define CMD32_R R1
/* 设置擦除块的终止地址 Set the address of the last write block to be erased */
#define CMD33 33
#define CMD33_R R1
/* 擦除所选择的块 Erase the selected write blocks */
#define CMD38 38
#define CMD38_R R1B
/***************************** 锁卡命令 Lock Card commands ***************************/
/* 设置/复位密码或上锁/解锁卡 Set/reset the password or lock/unlock the card */
#define CMD42 42
#define CMD42_R R1B
/* Commands from 42 to 54, not defined here */
/***************************** 应用命令 Application-specific commands ****************/
/* 禁止下一个命令为应用命令 Flag that the next command is application-specific */
#define CMD55 55
#define CMD55_R R1
/* 应用命令的通用I/O General purpose I/O for application-specific commands */
#define CMD56 56
#define CMD56_R R1
/* 读OCR寄存器 Read the OCR (SPI mode only) */
#define CMD58 58
#define CMD58_R R3
/* 使能或禁止 CRC Turn CRC on or off */
#define CMD59 59
#define CMD59_R R1
/***************************** 应用命令 Application-specific commands ***************/
/* 获取 SD Status寄存器 Get the SD card's status */
#define ACMD13 13
#define ACMD13_R R2
/* 得到已写入卡中的块的个数 Get the number of written write blocks (Minus errors ) */
#define ACMD22 22
#define ACMD22_R R1
/* 在写之前,设置预先擦除的块的个数 Set the number of write blocks to be pre-erased before writing */
#define ACMD23 23
#define ACMD23_R R1
#define ACMD41 41
#define ACMD41_R R1
/* 连接/断开CD/DATA[3]引脚上的上拉电阻 Connect or disconnect the 50kOhm internal pull-up on CD/DAT[3] */
#define ACMD42 42
#define ACMD42_R R1
/* 读取SCR寄存器 Get the SD configuration register */
#define ACMD51 51
#define ACMD51_R R1
/*
*************************************************************
用户API函数: 读, 写, 擦 SD卡 API函数及错误码
*************************************************************
*/
/* 错误码 error code */
#define SD_NO_ERR 0x00 // 函数执行成功
#define SD_ERR_NO_CARD 0x01 // SD卡没有完全插入到卡座中
#define SD_ERR_USER_PARAM 0x02 // 用户使用API函数时,入口参数有错误
#define SD_ERR_CARD_PARAM 0x03 // 卡中参数有错误(与本模块不兼容)
#define SD_ERR_VOL_NOTSUSP 0x04 // 卡不支持3.3V供电
#define SD_ERR_OVER_CARDRANGE 0x05 // 操作超出卡容量范围
/* SD命令可能返回的错误码 */
#define SD_ERR_CMD_RESPTYPE 0x10 // 命令类型错误
#define SD_ERR_CMD_TIMEOUT 0x11 // SD命令响应超时
#define SD_ERR_CMD_RESP 0x12 // SD命令响应错误
/* 数据流错误码 */
#define SD_ERR_DATA_CRC16 0x20 // 数据流CRC16校验不通过
#define SD_ERR_DATA_START_TOK 0x21 // 读单块或多块时,数据开始令牌不正确
#define SD_ERR_DATA_RESP 0x22 // 写单块或多块时,SD卡数据响应令牌不正确
/* 等待错误码 */
#define SD_ERR_TIMEOUT_WAIT 0x30 // 写或擦操作时,发生超时错误
#define SD_ERR_TIMEOUT_READ 0x31 // 读操作超时错误
#define SD_ERR_TIMEOUT_WRITE 0x32 // 写操作超时错误
#define SD_ERR_TIMEOUT_ERASE 0x33 // 擦除操作超时错误
#define SD_ERR_TIMEOUT_WAITIDLE 0x34 // 初始化SD卡时,等待SD卡进入空闲状态超时错误
/* 写操作可能返回的错误码 */
#define SD_ERR_WRITE_BLK 0x40 // 写块数据错误
#define SD_ERR_WRITE_BLKNUMS 0x41 // 写多块时,想要写入的块与正确写入的块数不一致
#define SD_ERR_WRITE_PROTECT 0x42 // 卡外壳的写保护开关打在写保护位置
uint8 sd_send_cmd(uint8 cmd,uint8 *param,uint8 resptype,uint8 *resp); //发送命令
void sd_pack_param(uint8 *parameter,uint32 value); //将32位数据转化为4个字节
uint8 sd_block_cmd(uint8 cmd,uint8 restype,uint32 parameter); //块命令
uint8 sd_reset(void); //复位SD卡
uint8 sd_check(void); //发出CMD1,查询卡的状态
uint8 sd_readcard_status(uint8*buffer); //读card status寄存器
uint8 sd_set_blocklen(uint32 length); //设置块长度
uint8 sd_crc(uint8 enable); //使能禁止CRC
uint8 sd_read_single_block(uint32 blockaddr); //读单块命令
uint8 sd_read_multiple_block(uint32 blockaddr); //读多块命令,直到收到停止命令
uint8 sd_stop_transmission(void); //停止发送
uint8 sd_write_single_block(uint32 blockaddr); //写单块命令
uint8 sd_write_multiple_block_cmd(uint8 blockaddr); //写多块命令
uint8 erase_startblock(uint32 startblock); //设置块擦除起始地址
uint8 erase_endblock(uint32 endblock); //设置块擦除结尾地址
uint8 erase_selectedblock(void); //擦除选中的块
/************************************数据传输*****************************************/
uint8 sd_read(uint32 len,char* recbuf); //读SD卡,len为数据长度,recbuf为数据缓冲区
uint8 sd_write(uint8 bmulti,uint32 len,char* sendbuf); //写SD卡,len为长度,sendbuf为发送缓冲,
void sd_sendmulti_stop(void); //发送停止数据传输令牌
void sd_delay(uint8 value); //延时函数
uint8 sd_init(void); //SD卡初始化进入SPI模式
uint8 sd_read_csd(void);
uint8 sd_read_cid(void);
uint8 sd_read_ocr(void);
uint8 sd_en_acmd(void);
uint8 sd_activates(void);
uint8 sd_clear_protect(void);
uint8 sd_set_protect(void);
uint8 sd_check_protect(void);
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -