📄 sdcmd.h
字号:
//SD/MMC 读写模块: 物理层
#ifndef SDCMD_H
#define SDCMD_H
#ifdef __cplusplus
extern "C" {
#endif
/*
*******************************************************
SD/MMC 卡的相关命令与响应宏定义
*******************************************************
*/
/* 命令响应定义*/
#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 //由于写错误而被拒绝
/* 等待类型 Wait Type */
#define SD_WAIT_READ 0x00 //读等待
#define SD_WAIT_WRITE 0x01 //写等待
#define SD_WAIT_ERASE 0x02 //擦除等待
/*
*********************************************
SD卡SPI模式下命令集
*********************************************
*/
/************* 基本命令集 *****************/
/* 复位SD 卡 */
#define CMD0 0
#define CMD0_R R1
/* 读OCR寄存器 */
#define CMD1 1
#define CMD1_R R1
/* 读CSD寄存器*/
#define CMD9 9
#define CMD9_R R1
/* 读CID寄存器*/
#define CMD10 10
#define CMD10_R R1
/* 停止读多块时的数据传输*/
#define CMD12 12
#define CMD12_R R1B
/* 读 Card_Status 寄存器*/
#define CMD13 13
#define CMD13_R R2
/************** 块读命令集 **********/
/* 设置块的长度*/
#define CMD16 16
#define CMD16_R R1
/* 读单块*/
#define CMD17 17
#define CMD17_R R1
/* 读多块,直至主机发送CMD12为止 */
#define CMD18 18
#define CMD18_R R1
/*************** 块写命令集 ***************/
/* 写单块 */
#define CMD24 24
#define CMD24_R R1
/* 写多块 */
#define CMD25 25
#define CMD25_R R1
/* 写CSD寄存器 */
#define CMD27 27
#define CMD27_R R1
/******* 写保护*******/
#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
/********** 擦除命令 *************/
/* 设置擦除块的起始地址(只用于SD卡) */
#define CMD32 32
#define CMD32_R R1
/* 设置擦除块的终止地址(只用于SD卡)*/
#define CMD33 33
#define CMD33_R R1
/* 设置擦除块的起始地址(只用于MMC卡) */
#define CMD35 35
#define CMD35_R R1
/* 设置擦除块的终止地址(只用于MMC卡)*/
#define CMD36 36
#define CMD36_R R1
/* 擦除所选择的块*/
#define CMD38 38
#define CMD38_R R1B
/*************** 锁卡命令**************/
/* 设置/复位密码或上锁/解锁卡 */
#define CMD42 42
#define CMD42_R R1B
/******************* 应用命令 *******/
/* 禁止下一个命令为应用命令 */
#define CMD55 55
#define CMD55_R R1
/* 应用命令的通用I/O */
#define CMD56 56
#define CMD56_R R1
/* 读OCR寄存器 */
#define CMD58 58
#define CMD58_R R3
/* 使能或禁止 CRC */
#define CMD59 59
#define CMD59_R R1
/************ 应用命令 *********/
/* 获取 SD Status寄存器 */
#define ACMD13 13
#define ACMD13_R R2
/* 得到已写入卡中的块的个数 */
#define ACMD22 22
#define ACMD22_R R1
/* 在写之前,设置预先擦除的块的个数 */
#define ACMD23 23
#define ACMD23_R R1
/* 读取OCR寄存器 */
#define ACMD41 41
#define ACMD41_R R1
/* 连接/断开CD/DATA[3]引脚上的上拉电阻*/
#define ACMD42 42
#define ACMD42_R R1
/* 读取SCR寄存器 */
#define ACMD51 51
#define ACMD51_R R1
extern U8 SD_TxCMD(U8 cmd, U8 *param, U8 resptype, U8 *resp); // 一个SPI命令
extern void SD_PackPara(U8 *parameter, U32 value); // 封装参数
extern U8 SD_BlockCMD(U8 cmd, U8 resptype, U32 parameter); // 块命令
extern U8 SD_ResetSD(void);
extern U8 SD_ReadCSD(U8 csdlen, U8 *recbuf);
extern U8 SD_SetBlockLen(U32 length);
// 数据流函数
extern U8 SD_ReadRegister(U32 len, U8 *recbuf);
extern U8 SD_ReadBlockData(U32 len, U8 *recbuf);
extern U8 SD_WriteBlockData(U8 bmulti, U32 len, const U8 *sendbuf);
// 其它函数
extern U8 SD_WaitBusy(U8 waittype);
extern void SD_SPIDelay(U8 value);
#ifdef __cplusplus
}
#endif
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -