电子说
功能:利用STM32F1的SDIO模块读取SD的容量并通过TFTLCD显示。
(1)创建sdio_sdcard.h文件,并输入以下代码。
#ifndef _SDIO_SDCARD_H_
#define _SDIO_SDCARD_H_
#include "sys.h"
/*********************************************************************************************************
SD 卡 数 据 定 义
*********************************************************************************************************/
//用户配置区
#define SDIO_INIT_CLK_DIV 0x76 //SDIO初始化频率,最大400Kh
#define SDIO_TRANSFER_CLK_DIV 0x00 //SDIO传输频率,该值太小可能会导致读写文件出错
//SDIO工作模式定义,通过SD_SetDeviceMode函数设置
#define SD_POLLING_MODE 0 //查询模式,该模式下,如果读写有问题,建议增大SDIO_TRANSFER_CLK_DIV的设置
#define SD_DMA_MODE 1 //DMA模式,该模式下,如果读写有问题,建议增大SDIO_TRANSFER_CLK_DIV的设置
//SDIO 各种错误枚举定义
typedef enum
{
//特殊错误定义
SD_CMD_CRC_FAIL = 1, //收到命令响应(CRC校验失败)
SD_DATA_CRC_FAIL = 2, //发送/接收数据块(CRC校验失败)
SD_CMD_RSP_TIMEOUT = 3, //命令响应超时
SD_DATA_TIMEOUT = 4, //数据超时
SD_TX_UNDERRUN = 5, //传输FIFO运行
SD_RX_OVERRUN = 6, //接收FIFO运行
SD_START_BIT_ERR = 7, //在宽总线模式下的所有数据信号上未检测到起始位
SD_CMD_OUT_OF_RANGE = 8, //CMD的参数超出范围
SD_ADDR_MISALIGNED = 9, //地址没有对齐
SD_BLOCK_LEN_ERR = 10, //卡不允许传输的块长度,或者传输的字节数与块长度不匹配
SD_ERASE_SEQ_ERR = 11, //擦除命令序列中发生错误
SD_BAD_ERASE_PARAM = 12, //删除组的无效选择
SD_WRITE_PROT_VIOLATION = 13, //试图对写保护块进行编程
SD_LOCK_UNLOCK_FAILED = 14, //在unlock命令中检测到序列或密码错误,或者试图访问锁定的卡
SD_COM_CRC_FAILED = 15, //前一个命令的CRC校验失败
SD_ILLEGAL_CMD = 16, //命令对卡状态无效
SD_CARD_ECC_FAILED = 17, //已应用卡内部ECC,但未能更正数据
SD_CC_ERROR = 18, //内部卡控制错误
SD_GENERAL_UNKNOWN_ERROR = 19, //通用或未知错误
SD_STREAM_READ_UNDERRUN = 20, //卡无法在流读取操作中维持数据传输
SD_STREAM_WRITE_OVERRUN = 21, //卡无法维持流模式下的数据编程
SD_CID_CSD_OVERWRITE = 22, //CID/CSD覆盖错误
SD_WP_ERASE_SKIP = 23, //只删除了部分地址空间
SD_CARD_ECC_DISABLED = 24, //命令已在不使用内部ECC的情况下执行
SD_ERASE_RESET = 25, //在执行之前清除了擦除序列,因为收到了一个超出擦除序列的命令
SD_AKE_SEQ_ERROR = 26, //身份验证顺序错误
SD_INVALID_VOLTRANGE = 27,
SD_ADDR_OUT_OF_RANGE = 28,
SD_SWITCH_ERROR = 29,
SD_SDIO_DISABLED = 30,
SD_SDIO_FUNCTION_BUSY = 31,
SD_SDIO_FUNCTION_FAILED = 32,
SD_SDIO_UNKNOWN_FUNCTION = 33,
//标准错误定义
SD_INTERNAL_ERROR,
SD_NOT_CONFIGURED,
SD_REQUEST_PENDING,
SD_REQUEST_NOT_APPLICABLE,
SD_INVALID_PARAMETER,
SD_UNSUPPORTED_FEATURE,
SD_UNSUPPORTED_HW,
SD_ERROR,
SD_OK = 0
} SD_Error;
//SD卡CSD寄存器数据
typedef struct
{
u8 CSDStruct; //CSD结构
u8 SysSpecVersion; //系统版本
u8 Reserved1; //保留
u8 TAAC; //数据读取访问时间1
u8 NSAC; //在CLK期间的数据读取访问时间2
u8 MaxBusClkFrec; //总线最高频率
u16 CardComdClasses; //卡命令类
u8 RdBlockLen; //最大读取数据块长度
u8 PartBlockRead; //允许读取的部分块
u8 WrBlockMisalign; //写入块未对准
u8 RdBlockMisalign; //读取块未对准
u8 DSRImpl; //DSR生效
u8 Reserved2; //保留
u32 DeviceSize; //驱动大小
u8 MaxRdCurrentVDDMin; //最大读取流@ VDD最小
u8 MaxRdCurrentVDDMax; //最大读取流@ VDD最大
u8 MaxWrCurrentVDDMin; //最大写入流@ VDD最小
u8 MaxWrCurrentVDDMax; //最大写入流@ VDD最大
u8 DeviceSizeMul; //驱动大小
u8 EraseGrSize; //擦除组大小
u8 EraseGrMul; //擦除组大小乘数
u8 WrProtectGrSize; //写保护组大小
u8 WrProtectGrEnable; //写保护组使能
u8 ManDeflECC; //制造商默认ECC
u8 WrSpeedFact; //写入速度因子
u8 MaxWrBlockLen; //最大写入数据长度
u8 WriteBlockPaPartial; //允许写入的部分块
u8 Reserved3; //保留
u8 ContentProtectAppli; //内容保护程序
u8 FileFormatGrouop; //文件格式组
u8 CopyFlag; //复制标志OTP
u8 PermWrProtect; //永久写保护
u8 TempWrProtect; //临时写保护
u8 FileFormat; //文件格式
u8 ECC; //ECC 代码
u8 CSD_CRC; //CSD CRC
u8 Reserved4; //总是1
} SD_CSD;
//SD卡CID寄存器数据
typedef struct
{
u8 ManufacturerID; //制造商
u16 OEM_AppliID; //OEM/应用程序ID
u32 ProdName1; //名称部分1
u8 ProdName2; //名称部分2
u8 ProdRev; //版本
u32 ProdSN; //序列号
u8 Reserved1; //保留1
u16 ManufactDate; //出厂日期
u8 CID_CRC; //CID CRC
u8 Reserved2; //总是1
} SD_CID;
//SD卡状态
typedef enum
{
SD_CARD_READY = 0x00000001,
SD_CARD_IDENTIFICATION = 0x00000002,
SD_CARD_STANDBY = 0x00000003,
SD_CARD_TRANSFER = 0x00000004,
SD_CARD_SENDING = 0x00000005,
SD_CARD_RECEIVING = 0x00000006,
SD_CARD_PROGRAMMING = 0x00000007,
SD_CARD_DISCONNECTED = 0x00000008,
SD_CARD_ERROR = 0x000000FF
}SDCardState;
//SD卡信息,包括CSD,CID等数据
typedef struct
{
SD_CSD SD_csd;
SD_CID SD_cid;
long long CardCapacity; //SD卡容量,单位:字节,最大支持2^64字节大小的卡
u32 CardBlockSize; //SD卡块大小
u16 RCA; //卡相对地址
u8 CardType; //卡类型
} SD_CardInfo;
extern SD_CardInfo SDCardInfo; //SD卡信息
//SDIO相关标志位,拷贝自:stm32f4xx_sdio.h
#define SDIO_FLAG_CCRCFAIL 0x00000001
#define SDIO_FLAG_DCRCFAIL 0x00000002
#define SDIO_FLAG_CTIMEOUT 0x00000004
#define SDIO_FLAG_DTIMEOUT 0x00000008
#define SDIO_FLAG_TXUNDERR 0x00000010
#define SDIO_FLAG_RXOVERR 0x00000020
#define SDIO_FLAG_CMDREND 0x00000040
#define SDIO_FLAG_CMDSENT 0x00000080
#define SDIO_FLAG_DATAEND 0x00000100
#define SDIO_FLAG_STBITERR 0x00000200
#define SDIO_FLAG_DBCKEND 0x00000400
#define SDIO_FLAG_CMDACT 0x00000800
#define SDIO_FLAG_TXACT 0x00001000
#define SDIO_FLAG_RXACT 0x00002000
#define SDIO_FLAG_TXFIFOHE 0x00004000
#define SDIO_FLAG_RXFIFOHF 0x00008000
#define SDIO_FLAG_TXFIFOF 0x00010000
#define SDIO_FLAG_RXFIFOF 0x00020000
#define SDIO_FLAG_TXFIFOE 0x00040000
#define SDIO_FLAG_RXFIFOE 0x00080000
#define SDIO_FLAG_TXDAVL 0x00100000
#define SDIO_FLAG_RXDAVL 0x00200000
#define SDIO_FLAG_SDIOIT 0x00400000
#define SDIO_FLAG_CEATAEND 0x00800000
//SDIO 指令集
#define SD_CMD_GO_IDLE_STATE 0
#define SD_CMD_SEND_OP_COND 1
#define SD_CMD_ALL_SEND_CID 2
#define SD_CMD_SET_REL_ADDR 3 //SD卡的SDIO_SEND_REL_ADDR
#define SD_CMD_SET_DSR 4
#define SD_CMD_SDIO_SEN_OP_COND 5
#define SD_CMD_HS_SWITCH 6
#define SD_CMD_SEL_DESEL_CARD 7
#define SD_CMD_HS_SEND_EXT_CSD 8
#define SD_CMD_SEND_CSD 9
#define SD_CMD_SEND_CID 10
#define SD_CMD_READ_DAT_UNTIL_STOP 11 //SD卡不支持
#define SD_CMD_STOP_TRANSMISSION 12
#define SD_CMD_SEND_STATUS 13
#define SD_CMD_HS_BUSTEST_READ 14
#define SD_CMD_GO_INACTIVE_STATE 15
#define SD_CMD_SET_BLOCKLEN 16
#define SD_CMD_READ_SINGLE_BLOCK 17
#define SD_CMD_READ_MULT_BLOCK 18
#define SD_CMD_HS_BUSTEST_WRITE 19
#define SD_CMD_WRITE_DAT_UNTIL_STOP 20
#define SD_CMD_SET_BLOCK_COUNT 23
#define SD_CMD_WRITE_SINGLE_BLOCK 24
#define SD_CMD_WRITE_MULT_BLOCK 25
#define SD_CMD_PROG_CID 26
#define SD_CMD_PROG_CSD 27
#define SD_CMD_SET_WRITE_PROT 28
#define SD_CMD_CLR_WRITE_PROT 29
#define SD_CMD_SEND_WRITE_PROT 30
#define SD_CMD_SD_ERASE_GRP_START 32 //设置要擦除的第一个写块的地址SD卡独有
#define SD_CMD_SD_ERASE_GRP_END 33 //设置要擦除的连续范围的最后一个写块的地址SD卡独有
#define SD_CMD_ERASE_GRP_START 35 //设置要擦除的第一个写块的地址MMC card 3.31
#define SD_CMD_ERASE_GRP_END 36 //设置要擦除的连续范围的最后一个写块的地址MMC card 3.31
#define SD_CMD_ERASE 38
#define SD_CMD_FAST_IO 39 //SD卡不支持
#define SD_CMD_GO_IRQ_STATE 40 //SD卡不支持
#define SD_CMD_LOCK_UNLOCK 42
#define SD_CMD_APP_CMD 55
#define SD_CMD_GEN_CMD 56
#define SD_CMD_NO_CMD 64
#define SD_CMD_APP_SD_SET_BUSWIDTH 6 //SD卡独有
#define SD_CMD_SD_APP_STAUS 13 //SD卡独有
#define SD_CMD_SD_APP_SEND_NUM_WRITE_BLOCKS 22 //SD卡独有
#define SD_CMD_SD_APP_OP_COND 41 //SD卡独有
#define SD_CMD_SD_APP_SET_CLR_CARD_DETECT 42 //SD卡独有
#define SD_CMD_SD_APP_SEND_SCR 51 //SD卡独有
#define SD_CMD_SDIO_RW_DIRECT 52 //SD卡IO独有
#define SD_CMD_SDIO_RW_EXTENDED 53 //SD卡IO独有
#define SD_CMD_SD_APP_GET_MKB 43 //SD卡独有
#define SD_CMD_SD_APP_GET_MID 44 //SD卡独有
#define SD_CMD_SD_APP_SET_CER_RN1 45 //SD卡独有
#define SD_CMD_SD_APP_GET_CER_RN2 46 //SD卡独有
#define SD_CMD_SD_APP_SET_CER_RES2 47 //SD卡独有
#define SD_CMD_SD_APP_GET_CER_RES1 48 //SD卡独有
#define SD_CMD_SD_APP_SECURE_READ_MULTIPLE_BLOCK 18 //SD卡独有
#define SD_CMD_SD_APP_SECURE_WRITE_MULTIPLE_BLOCK 25 //SD卡独有
#define SD_CMD_SD_APP_SECURE_ERASE 38 //SD卡独有
#define SD_CMD_SD_APP_CHANGE_SECURE_AREA 49 //SD卡独有
#define SD_CMD_SD_APP_SECURE_WRITE_MKB 48 //SD卡独有
//支持的SD卡定义
#define SDIO_STD_CAPACITY_SD_CARD_V1_1 0x00000000
#define SDIO_STD_CAPACITY_SD_CARD_V2_0 0x00000001
#define SDIO_HIGH_CAPACITY_SD_CARD 0x00000002
#define SDIO_MULTIMEDIA_CARD 0x00000003
#define SDIO_SECURE_DIGITAL_IO_CARD 0x00000004
#define SDIO_HIGH_SPEED_MULTIMEDIA_CARD 0x00000005
#define SDIO_SECURE_DIGITAL_IO_COMBO_CARD 0x00000006
#define SDIO_HIGH_CAPACITY_MMC_CARD 0x00000007
//SDIO相关参数定义
#define NULL 0
#define SDIO_STATIC_FLAGS 0x000005FF
#define SDIO_CMD0TIMEOUT 0x00010000
#define SDIO_DATATIMEOUT 0xFFFFFFFF
#define SDIO_FIFO_Address 0x40018080
//Mask for errors Card Status R1 OCR Register
#define SD_OCR_ADDR_OUT_OF_RANGE 0x80000000
#define SD_OCR_ADDR_MISALIGNED 0x40000000
#define SD_OCR_BLOCK_LEN_ERR 0x20000000
#define SD_OCR_ERASE_SEQ_ERR 0x10000000
#define SD_OCR_BAD_ERASE_PARAM 0x08000000
#define SD_OCR_WRITE_PROT_VIOLATION 0x04000000
#define SD_OCR_LOCK_UNLOCK_FAILED 0x01000000
#define SD_OCR_COM_CRC_FAILED 0x00800000
#define SD_OCR_ILLEGAL_CMD 0x00400000
#define SD_OCR_CARD_ECC_FAILED 0x00200000
#define SD_OCR_CC_ERROR 0x00100000
#define SD_OCR_GENERAL_UNKNOWN_ERROR 0x00080000
#define SD_OCR_STREAM_READ_UNDERRUN 0x00040000
#define SD_OCR_STREAM_WRITE_OVERRUN 0x00020000
#define SD_OCR_CID_CSD_OVERWRIETE 0x00010000
#define SD_OCR_WP_ERASE_SKIP 0x00008000
#define SD_OCR_CARD_ECC_DISABLED 0x00004000
#define SD_OCR_ERASE_RESET 0x00002000
#define SD_OCR_AKE_SEQ_ERROR 0x00000008
#define SD_OCR_ERRORBITS 0xFDFFE008
//Masks for R6 Response
#define SD_R6_GENERAL_UNKNOWN_ERROR 0x00002000
#define SD_R6_ILLEGAL_CMD 0x00004000
#define SD_R6_COM_CRC_FAILED 0x00008000
#define SD_VOLTAGE_WINDOW_SD 0x80100000
#define SD_HIGH_CAPACITY 0x40000000
#define SD_STD_CAPACITY 0x00000000
#define SD_CHECK_PATTERN 0x000001AA
#define SD_VOLTAGE_WINDOW_MMC 0x80FF8000
#define SD_MAX_VOLT_TRIAL 0x0000FFFF
#define SD_ALLZERO 0x00000000
#define SD_WIDE_BUS_SUPPORT 0x00040000
#define SD_SINGLE_BUS_SUPPORT 0x00010000
#define SD_CARD_LOCKED 0x02000000
#define SD_CARD_PROGRAMMING 0x00000007
#define SD_CARD_RECEIVING 0x00000006
#define SD_DATATIMEOUT 0xFFFFFFFF
#define SD_0TO7BITS 0x000000FF
#define SD_8TO15BITS 0x0000FF00
#define SD_16TO23BITS 0x00FF0000
#define SD_24TO31BITS 0xFF000000
#define SD_MAX_DATA_LENGTH 0x01FFFFFF
#define SD_HALFFIFO 0x00000008
#define SD_HALFFIFOBYTES 0x00000020
//Command Class Supported
#define SD_CCCC_LOCK_UNLOCK 0x00000080
#define SD_CCCC_WRITE_PROT 0x00000040
#define SD_CCCC_ERASE 0x00000020
//CMD8指令
#define SDIO_SEND_IF_COND 0x00000008
/*********************************************************************************************************
函 数 列 表
*********************************************************************************************************/
SD_Error SD_Init( void ) ; //初始化SD卡
u8 SD_ReadDisk( u8*buf, u32 sector, u8 cnt ) ; //读SD卡,fatfs/usb调用
u8 SD_WriteDisk( u8*buf, u32 sector, u8 cnt ) ; //写SD卡,fatfs/usb调用
#endif
全部0条评论
快来发表一下你的评论吧 !