目录
SDIO相关结构体
SDIO初始化结构体
SDIO命令初始化结构体
SDIO数据初始化结构体
硬件连接
实验源码
SDIO相关结构体
标准库函数对SDIO外设建立了三个初始化结构体,分别为SDIO初始化结构体SDIO_InitTypeDef, SDIO命令初始化结构体SDIO_CmdInitTypeDef和SDIO数据初始化结构体SDIO_DatalnitTypeDef。
初始化结构体成员用于设置SDIO工作环境参数,并由SDIO相应初始化配置函数或功能函数调用,这些参数将会被写入到SDI。相应的寄存器,达到配置SDIO工作环境的目的。
SDIO命令初始化结构体用于设置命令相关内容,比如命令号、命令参数、响应类型等等。
SDIO数据初始化结构体用于配置数据发送和接收参数,比如传输超时、数据长度、传输模式等等。
SDIO初始化结构体
SDIO初始化结构体用于配置SDIO基本工作环境,比如时钟分频、时钟沿、数据宽度等等。
typedef struct
{
uint32_t SDIo_ClockEdge;//时钟沿
uint32_t SDIo_ClockBypass;//旁路时钟
uint32_t SDio_clockPowerSave;//节能模式
uint32_t SDIo_BusWide;//数据宽度
uint32_t SDio HardwareFlowControl;// 硬件流控制
uint8_t SDio_ClockDiv;//时钟分频
} SDIO_InitTypeDef;
SDIO_ClockEdge:主时钟SDIOCLK产生CLK引脚时钟有效沿选择,可选上升沿或下降沿,它设定SDIO时钟控制寄存器(SDIO_CLKCR)的NEGEDGE位的值,一般选择设置为高电平。
SDIO_ClockBypass:时钟分频旁路使用,可选使能或禁用,它设定SDIO_CLKCR寄存器的BYPASS位。如果使能旁路,SDIOCLK直接驱动CLK线输出时钟;如果禁用,使用SDIO_CLKCR寄存器的CLKDIV位值分频SDIOCLK,然后输出到CLK线。一般选择禁用时钟分频旁路。
SDIO_ClockPowerSave:节能模式选择,可选使能或禁用,它设定SDIO_CLKCR寄存器的PWRSAV位的值。如果使能节能模式,CLK线只有在总线激活时才有时钟输出;如果禁用节能模式,始终使能CLK线输出时钟。
SDIO_BusWide:数据线宽度选择,可选1位数据总线、4位数据总线或8为数据总线,系统默认使用1位数据总线,操作SD卡时在数据传输模式下一般选择4位数据总线。它设定SDIO_CLKCR寄存器的WIDBUS位的值。
SDIO_HardwareFlowControl:硬件流控制选择,可选使能或禁用,它设定SDIO_CLKCR寄存器的HWFC_EN位的值。硬件流控制功能可以避免FIFO发送上溢和下溢错误。
SDIO_ClockDiv:时钟分频系数,它设定SDIO_CLKCR寄存器的CLKDIV位的值,设置SDIOCLK与CLK线输出时钟分频系数:CLK线时钟频率=SDIOCLK/([CLKDIV+2])。
SDIO命令初始化结构体
SDIO命令初始化结构体用于设置命令相关内容,比如命令号、命令参数、响应类型等等。
typedef struct
{
uint32_t SDIo_Argument;//命令参数
uint32_t SDIo_CmdIndex;//命令号
uint32_t SDIo_Response;//响应类型
uint32_t SDIo_Wait;//等待使能
uint32_t SDIO_CPSM;//命令路径状态机
} SDIo_CmdInitTypeDef;
SDIO_Argument:作为命令的一部分发送到卡的命令参数,它设定SDIO参数寄存器(SDIO_ARG)的值。
SDIO_CmdIndex:命令号选择,它设定SDIO命令寄存器(SDIO_CMD)的CMDINDEX位的值。
SDIO_Response:响应类型,SDIO定义两个响应类型:长响应和短响应。根据命令号选择对应的响应类型。SDIO定义了四个32位的SDIO响应寄存器(SDIO_RESPx,x=1..4),短响应只用到SDIO_RESP1。
SDIO_Wait:等待类型选择,有三种状态可选,一种是无等待状态,超时检测功能启动;一种是等待中断,另外一种是等待传输完成。它设定SDIO_CMD寄存器的WAITPEND位和WAITINT位的值。
SDIO_CPSM:命令路径状态机控制,可选使能或禁用CPSM。它设定SDIO_CMD寄存器的CPSMEN位的值。
SDIO数据初始化结构体
SDIO数据初始化结构体用于配置数据发送和接收参数,比如传输超时、数据长度、传输模式等等。它被SDIO_DataConfig函数使用。
typedef struct
{
uint32_t SDIo_DataTimeOut;//数据传输超时
uint32_t SDIoDataLength;//数据长度
uint32_t SDIoDataBlockSize;//数据块大小
uint32_t SDIoTransferDir;//数据传输方向
uint32_t SDIo_TransferMode;//数据传输模式
uint32_t SDIO DPSM;// 数据路径状态机
}SDIO_DataInitTypeDef;
SDIO_DataTimeOut:设置数据传输以卡总线时钟周期表示的超时周期,它设定SDIO数据定时器寄存器(SDIO_DTIMER)的值。在DPSM进入Wait_R或繁忙状态后开始递减,直到0还处于以上两种状态则将超时状态标志置1.
SDIO_DataLength:设置传输数据长度,它设定SDIO数据长度寄存器(SDIO_DLEN)的值。
SDIO_DataBlockSize:设置数据块大小,有多种尺寸可选,不同命令要求的数据块可能不同。它设定SDIO数据控制寄存器(SDIO_DCTRL)寄存器的DBLOCKSIZE位的值。
SDIO_TransferDir:数据传输方向,可选从主机到卡的写操作,或从卡到主机2的读操作。它设定SDIO_DCTRL寄存器的DTDIR位的值。
SDIO_TransferDir:数据传输方向,可选从主机到卡的写操作,或从卡到主机2的读操作。它设定SDIO_DCTRL寄存器的DTDIR位的值。
SDIO_DPSM:数据路径状态机控制,可选使能或禁用DPSM。它设定SDIO_DCTRL寄存器的DTEN位的值。要实现数据传输都必须使能SDIO_DPSM.
硬件连接
实验源码
/**
******************************************************************************
* @file : user_rcc_config.c
* @brief : V1.00
******************************************************************************
* @attention
*
******************************************************************************
*/
/* Include 包含---------------------------------------------------------------*/
#include "user_rcc_config.h"
/* Typedef 类型----------------------------------------------------------------*/
/* Define 定义----------------------------------------------------------------*/
/* Macro 宏------------------------------------------------------------------*/
/* Variables 变量--------------------------------------------------------------*/
/* Constants 常量--------------------------------------------------------------*/
/* Function 函数--------------------------------------------------------------*/
/*!
\brief RCC配置
\param[in] none
\param[out] none
\retval none
*/
void Rcc_config(void)
{
/*使能GPIOA时钟*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
/*使能GPIOC时钟*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
/*使能GPIOD时钟*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);
/*使能UART1时钟*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
/*使能SDIO时钟*/
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_SDIO|RCC_AHBPeriph_DMA2,ENABLE);
}
/************************************************************** END OF FILE ****/
/**
******************************************************************************
* @file : user_gpio.c
* @brief : V1.00
******************************************************************************
* @attention
*
******************************************************************************
*/
/* Include 包含---------------------------------------------------------------*/
#include "user_gpio.h"
/* Typedef 类型----------------------------------------------------------------*/
/* Define 定义----------------------------------------------------------------*/
/* Macro 宏------------------------------------------------------------------*/
/* Variables 变量--------------------------------------------------------------*/
/* Constants 常量--------------------------------------------------------------*/
/* Function 函数--------------------------------------------------------------*/
/*!
\brief GPIO初始化函数
\param[in] none
\param[out] none
\retval none
*/
void Gpio_Init(void)
{
/*GPIO结构体*/
GPIO_InitTypeDef GPIO_InitTypeDefstruct;
/*UART1发送引脚配置*/
GPIO_InitTypeDefstruct.GPIO_Mode = GPIO_Mode_AF_PP;//推挽复用输出
GPIO_InitTypeDefstruct.GPIO_Pin = GPIO_Pin_9;
GPIO_InitTypeDefstruct.GPIO_Speed = GPIO_Speed_10MHz;
/*写入结构体到GPIOA*/
GPIO_Init(GPIOA,&GPIO_InitTypeDefstruct);
/*UART1接收引脚配置*/
GPIO_InitTypeDefstruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_InitTypeDefstruct.GPIO_Pin = GPIO_Pin_10;
GPIO_InitTypeDefstruct.GPIO_Speed = GPIO_Speed_10MHz;
/*写入结构体到GPIOA*/
GPIO_Init(GPIOA,&GPIO_InitTypeDefstruct);
/*SDIO引脚初始 8~11数据线 12时钟线*/
GPIO_InitTypeDefstruct.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12; //复用输出
GPIO_InitTypeDefstruct.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitTypeDefstruct.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
/*写入结构体GPIOC*/
GPIO_Init(GPIOC, &GPIO_InitTypeDefstruct);
/*SDIO引脚初始化 命令线*/
GPIO_InitTypeDefstruct.GPIO_Pin = GPIO_Pin_2; //复用输出
GPIO_InitTypeDefstruct.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitTypeDefstruct.GPIO_Speed = GPIO_Speed_50MHz;//IO口速度为50MHz
/*写入结构体GPIOD*/
GPIO_Init(GPIOD, &GPIO_InitTypeDefstruct);
/*SDIO选中引脚*/
GPIO_InitTypeDefstruct.GPIO_Pin = GPIO_Pin_7; //上拉输入
GPIO_InitTypeDefstruct.GPIO_Mode = GPIO_Mode_IPU; //复用推挽输出
GPIO_InitTypeDefstruct.GPIO_Speed = GPIO_Speed_50MHz;//IO口速度为50MHz
/*写入结构体GPIOD*/
GPIO_Init(GPIOD, &GPIO_InitTypeDefstruct);
}
/************************************************************** END OF FILE ****/
/**
******************************************************************************
* @file : user_uart.c
* @brief : V1.00
******************************************************************************
* @attention
*
******************************************************************************
*/
/* Include 包含---------------------------------------------------------------*/
#include "user_uart.h"
/* Typedef 类型----------------------------------------------------------------*/
/* Define 定义----------------------------------------------------------------*/
/* Macro 宏------------------------------------------------------------------*/
/* Variables 变量--------------------------------------------------------------*/
extern uint16_t USART_RX_STA;
extern uint8_t USART_RX_BUF[200];
/* Constants 常量--------------------------------------------------------------*/
/* Function 函数--------------------------------------------------------------*/
#if 1
#pragma import(__use_no_semihosting)
/*实现Printf代码*/
struct __FILE
{
int handle;
};
FILE __stdout;
void _sys_exit(int x)
{
x = x;
}
//重定义fputc函数
int fputc(int ch, FILE *f)
{
while((USART1->SR&0X40)==0);//循环发送,直到发送完毕
USART1->DR = (u8) ch;
return ch;
}
#endif
/*!
\brief UART1初始化
\param[in] none
\param[out] none
\retval none
*/
void Uart1_Init(u32 bound)
{
/*UART结构体*/
USART_InitTypeDef USART_InitTypeDefstruct;
/*UART结构体配置*/
USART_InitTypeDefstruct.USART_BaudRate = bound; //波特率
USART_InitTypeDefstruct.USART_HardwareFlowControl =USART_HardwareFlowControl_None; //不使用硬件流
USART_InitTypeDefstruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//发送接收使能
USART_InitTypeDefstruct.USART_Parity = USART_Parity_No; //不使用奇偶校验
USART_InitTypeDefstruct.USART_StopBits = USART_StopBits_1; //1个停止位
USART_InitTypeDefstruct.USART_WordLength = USART_WordLength_8b; //8个数据位
/*写入USART1*/
USART_Init(USART1,&USART_InitTypeDefstruct);
/*使能串口1*/
USART_Cmd(USART1,ENABLE);
}
/*!
\brief UART1中断服务函数
\param[in] none
\param[out] none
\retval none
*/
void USART1_IRQHandler(void)
{
}
/************************************************************** END OF FILE ****/
/**
******************************************************************************
* @file : user_sdio.h
* @brief : V1.00
******************************************************************************
* @attention
*
******************************************************************************
*/
/* Define to prevent recursive incluson---------------------------------------*/
#ifndef _USER_SDIO_H__
#define _USER_SDIO_H__
/* Include 包含---------------------------------------------------------------*/
#include "stm32f10x.h"
/* Typedef 类型----------------------------------------------------------------*/
/*SDIO 各种错误枚举定义*/
typedef enum
{
//特殊错误定义
SD_CMD_CRC_FAIL = (1), /*!< Command response received (but CRC check failed) */
SD_DATA_CRC_FAIL = (2), /*!< Data bock sent/received (CRC check Failed) */
SD_CMD_RSP_TIMEOUT = (3), /*!< Command response timeout */
SD_DATA_TIMEOUT = (4), /*!< Data time out */
SD_TX_UNDERRUN = (5), /*!< Transmit FIFO under-run */
SD_RX_OVERRUN = (6), /*!< Receive FIFO over-run */
SD_START_BIT_ERR = (7), /*!< Start bit not detected on all data signals in widE bus mode */
SD_CMD_OUT_OF_RANGE = (8), /*!< CMD's argument was out of range.*/
SD_ADDR_MISALIGNED = (9), /*!< Misaligned address */
SD_BLOCK_LEN_ERR = (10), /*!< Transferred block length is not allowed for the card or the number of transferred bytes does not match the block length */
SD_ERASE_SEQ_ERR = (11), /*!< An error in the sequence of erase command occurs.*/
SD_BAD_ERASE_PARAM = (12), /*!< An Invalid selection for erase groups */
SD_WRITE_PROT_VIOLATION = (13), /*!< Attempt to program a write protect block */
SD_LOCK_UNLOCK_FAILED = (14), /*!< Sequence or password error has been detected in unlock command or if there was an attempt to access a locked card */
SD_COM_CRC_FAILED = (15), /*!< CRC check of the previous command failed */
SD_ILLEGAL_CMD = (16), /*!< Command is not legal for the card state */
SD_CARD_ECC_FAILED = (17), /*!< Card internal ECC was applied but failed to correct the data */
SD_CC_ERROR = (18), /*!< Internal card controller error */
SD_GENERAL_UNKNOWN_ERROR = (19), /*!< General or Unknown error */
SD_STREAM_READ_UNDERRUN = (20), /*!< The card could not sustain data transfer in stream read operation. */
SD_STREAM_WRITE_OVERRUN = (21), /*!< The card could not sustain data programming in stream mode */
SD_CID_CSD_OVERWRITE = (22), /*!< CID/CSD overwrite error */
SD_WP_ERASE_SKIP = (23), /*!< only partial address space was erased */
SD_CARD_ECC_DISABLED = (24), /*!< Command has been executed without using internal ECC */
SD_ERASE_RESET = (25), /*!< Erase sequence was cleared before executing because an out of erase sequence command was received */
SD_AKE_SEQ_ERROR = (26), /*!< Error in sequence of authentication. */
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
{
uint8_t CSDStruct; /*!< CSD structure */
uint8_t SysSpecVersion; /*!< System specification version */
uint8_t Reserved1; /*!< Reserved */
uint8_t TAAC; /*!< Data read access-time 1 */
uint8_t NSAC; /*!< Data read access-time 2 in CLK cycles */
uint8_t MaxBusClkFrec; /*!< Max. bus clock frequency */
uint16_t CardComdClasses; /*!< Card command classes */
uint8_t RdBlockLen; /*!< Max. read data block length */
uint8_t PartBlockRead; /*!< Partial blocks for read allowed */
uint8_t WrBlockMisalign; /*!< Write block misalignment */
uint8_t RdBlockMisalign; /*!< Read block misalignment */
uint8_t DSRImpl; /*!< DSR implemented */
uint8_t Reserved2; /*!< Reserved */
uint32_t DeviceSize; /*!< Device Size */
uint8_t MaxRdCurrentVDDMin; /*!< Max. read current @ VDD min */
uint8_t MaxRdCurrentVDDMax; /*!< Max. read current @ VDD max */
uint8_t MaxWrCurrentVDDMin; /*!< Max. write current @ VDD min */
uint8_t MaxWrCurrentVDDMax; /*!< Max. write current @ VDD max */
uint8_t DeviceSizeMul; /*!< Device size multiplier */
uint8_t EraseGrSize; /*!< Erase group size */
uint8_t EraseGrMul; /*!< Erase group size multiplier */
uint8_t WrProtectGrSize; /*!< Write protect group size */
uint8_t WrProtectGrEnable; /*!< Write protect group enable */
uint8_t ManDeflECC; /*!< Manufacturer default ECC */
uint8_t WrSpeedFact; /*!< Write speed factor */
uint8_t MaxWrBlockLen; /*!< Max. write data block length */
uint8_t WriteBlockPaPartial; /*!< Partial blocks for write allowed */
uint8_t Reserved3; /*!< Reserded */
uint8_t ContentProtectAppli; /*!< Content protection application */
uint8_t FileFormatGrouop; /*!< File format group */
uint8_t CopyFlag; /*!< Copy flag (OTP) */
uint8_t PermWrProtect; /*!< Permanent write protection */
uint8_t TempWrProtect; /*!< Temporary write protection */
uint8_t FileFormat; /*!< File Format */
uint8_t ECC; /*!< ECC code */
uint8_t CSD_CRC; /*!< CSD CRC */
uint8_t Reserved4; /*!< always 1*/
} SD_CSD;
/*SD卡CID寄存器数据*/
typedef struct
{
uint8_t ManufacturerID; /*!< ManufacturerID */
uint16_t OEM_AppliID; /*!< OEM/Application ID */
uint32_t ProdName1; /*!< Product Name part1 */
uint8_t ProdName2; /*!< Product Name part2*/
uint8_t ProdRev; /*!< Product Revision */
uint32_t ProdSN; /*!< Product Serial Number */
uint8_t Reserved1; /*!< Reserved1 */
uint16_t ManufactDate; /*!< Manufacturing Date */
uint8_t CID_CRC; /*!< CID CRC */
uint8_t Reserved2; /*!< always 1 */
} SD_CID;
/*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;
/* Define 定义----------------------------------------------------------------*/
/*SDIO工作模式定义,通过SD_SetDeviceMode函数设置*/
#define SD_POLLING_MODE 0 //查询模式,该模式下,如果读写有问题,建议增大SDIO_TRANSFER_CLK_DIV的设置.
#define SD_DMA_MODE 1 //DMA模式,该模式下,如果读写有问题,建议增大SDIO_TRANSFER_CLK_DIV的设置.
/*SDIO时钟计算公式:SDIO_CK时钟=SDIOCLK/[clkdiv+2];其中,SDIOCLK一般为72Mhz
使用DMA模式的时候,传输速率可以到24Mhz,不过如果你的卡不是高速卡,可能也会出错
出错就请降低时钟,使用查询模式的话,推荐SDIO_TRANSFER_CLK_DIV设置为3或者更大*/
#define SDIO_INIT_CLK_DIV 0xB2 //SDIO初始化频率,最大400Kh
#define SDIO_TRANSFER_CLK_DIV 0x04 //SDIO传输频率,该值太小可能会导致读写文件出错
/*Mask for errors Card Status R1 (OCR Register)*/
#define SD_OCR_ADDR_OUT_OF_RANGE ((u32)0x80000000)
#define SD_OCR_ADDR_MISALIGNED ((u32)0x40000000)
#define SD_OCR_BLOCK_LEN_ERR ((u32)0x20000000)
#define SD_OCR_ERASE_SEQ_ERR ((u32)0x10000000)
#define SD_OCR_BAD_ERASE_PARAM ((u32)0x08000000)
#define SD_OCR_WRITE_PROT_VIOLATION ((u32)0x04000000)
#define SD_OCR_LOCK_UNLOCK_FAILED ((u32)0x01000000)
#define SD_OCR_COM_CRC_FAILED ((u32)0x00800000)
#define SD_OCR_ILLEGAL_CMD ((u32)0x00400000)
#define SD_OCR_CARD_ECC_FAILED ((u32)0x00200000)
#define SD_OCR_CC_ERROR ((u32)0x00100000)
#define SD_OCR_GENERAL_UNKNOWN_ERROR ((u32)0x00080000)
#define SD_OCR_STREAM_READ_UNDERRUN ((u32)0x00040000)
#define SD_OCR_STREAM_WRITE_OVERRUN ((u32)0x00020000)
#define SD_OCR_CID_CSD_OVERWRIETE ((u32)0x00010000)
#define SD_OCR_WP_ERASE_SKIP ((u32)0x00008000)
#define SD_OCR_CARD_ECC_DISABLED ((u32)0x00004000)
#define SD_OCR_ERASE_RESET ((u32)0x00002000)
#define SD_OCR_AKE_SEQ_ERROR ((u32)0x00000008)
#define SD_OCR_ERRORBITS ((u32)0xFDFFE008)
#define SD_WIDE_BUS_SUPPORT ((u32)0x00040000)
#define SD_SINGLE_BUS_SUPPORT ((u32)0x00010000)
#define SD_CARD_LOCKED ((u32)0x02000000)
#define SD_CARD_PROGRAMMING ((u32)0x00000007)
#define SD_CARD_RECEIVING ((u32)0x00000006)
#define SD_DATATIMEOUT ((u32)0xFFFFFFFF)
#define SD_0TO7BITS ((u32)0x000000FF)
#define SD_8TO15BITS ((u32)0x0000FF00)
#define SD_16TO23BITS ((u32)0x00FF0000)
#define SD_24TO31BITS ((u32)0xFF000000)
#define SD_MAX_DATA_LENGTH ((u32)0x01FFFFFF)
//CMD8指令
#define SDIO_SEND_IF_COND ((u32)0x00000008)
#define SD_HALFFIFO ((u32)0x00000008)
#define SD_HALFFIFOBYTES ((u32)0x00000020)
//Masks for R6 Response
#define SD_R6_GENERAL_UNKNOWN_ERROR ((u32)0x00002000)
#define SD_R6_ILLEGAL_CMD ((u32)0x00004000)
#define SD_R6_COM_CRC_FAILED ((u32)0x00008000)
/*支持的SD卡定义*/
#define SDIO_STD_CAPACITY_SD_CARD_V1_1 ((uint32_t)0x00000000)
#define SDIO_STD_CAPACITY_SD_CARD_V2_0 ((uint32_t)0x00000001)
#define SDIO_HIGH_CAPACITY_SD_CARD ((uint32_t)0x00000002)
#define SDIO_MULTIMEDIA_CARD ((uint32_t)0x00000003)
#define SDIO_SECURE_DIGITAL_IO_CARD ((uint32_t)0x00000004)
#define SDIO_HIGH_SPEED_MULTIMEDIA_CARD ((uint32_t)0x00000005)
#define SDIO_SECURE_DIGITAL_IO_COMBO_CARD ((uint32_t)0x00000006)
#define SDIO_HIGH_CAPACITY_MMC_CARD ((uint32_t)0x00000007)
//SDIO相关参数定义
#define NULL 0
#define SDIO_STATIC_FLAGS ((u32)0x000005FF)
#define SDIO_CMD0TIMEOUT ((u32)0x00010000)
#define SDIO_DATATIMEOUT ((u32)0xFFFFFFFF)
#define SDIO_FIFO_Address ((u32)0x40018080)
/*SD卡电压SD卡容量*/
#define SD_VOLTAGE_WINDOW_SD ((u32)0x80100000)
#define SD_HIGH_CAPACITY ((u32)0x40000000)
#define SD_STD_CAPACITY ((u32)0x00000000)
#define SD_CHECK_PATTERN ((u32)0x000001AA)
#define SD_VOLTAGE_WINDOW_MMC ((u32)0x80FF8000)
#define SD_MAX_VOLT_TRIAL ((u32)0x0000FFFF)
#define SD_ALLZERO ((u32)0x00000000)
/*SDIO 指令集*/
#define SD_CMD_GO_IDLE_STATE ((u8)0)
#define SD_CMD_SEND_OP_COND ((u8)1)
#define SD_CMD_ALL_SEND_CID ((u8)2)
#define SD_CMD_SET_REL_ADDR ((u8)3) /*!< SDIO_SEND_REL_ADDR for SD Card */
#define SD_CMD_SET_DSR ((u8)4)
#define SD_CMD_SDIO_SEN_OP_COND ((u8)5)
#define SD_CMD_HS_SWITCH ((u8)6)
#define SD_CMD_SEL_DESEL_CARD ((u8)7)
#define SD_CMD_HS_SEND_EXT_CSD ((u8)8)
#define SD_CMD_SEND_CSD ((u8)9)
#define SD_CMD_SEND_CID ((u8)10)
#define SD_CMD_READ_DAT_UNTIL_STOP ((u8)11) /*!< SD Card doesn't support it */
#define SD_CMD_STOP_TRANSMISSION ((u8)12)
#define SD_CMD_SEND_STATUS ((u8)13)
#define SD_CMD_HS_BUSTEST_READ ((u8)14)
#define SD_CMD_GO_INACTIVE_STATE ((u8)15)
#define SD_CMD_SET_BLOCKLEN ((u8)16)
#define SD_CMD_READ_SINGLE_BLOCK ((u8)17)
#define SD_CMD_READ_MULT_BLOCK ((u8)18)
#define SD_CMD_HS_BUSTEST_WRITE ((u8)19)
#define SD_CMD_WRITE_DAT_UNTIL_STOP ((u8)20)
#define SD_CMD_SET_BLOCK_COUNT ((u8)23)
#define SD_CMD_WRITE_SINGLE_BLOCK ((u8)24)
#define SD_CMD_WRITE_MULT_BLOCK ((u8)25)
#define SD_CMD_PROG_CID ((u8)26)
#define SD_CMD_PROG_CSD ((u8)27)
#define SD_CMD_SET_WRITE_PROT ((u8)28)
#define SD_CMD_CLR_WRITE_PROT ((u8)29)
#define SD_CMD_SEND_WRITE_PROT ((u8)30)
#define SD_CMD_SD_ERASE_GRP_START ((u8)32) /*!< To set the address of the first write
block to be erased. (For SD card only) */
#define SD_CMD_SD_ERASE_GRP_END ((u8)33) /*!< To set the address of the last write block of the
continuous range to be erased. (For SD card only) */
#define SD_CMD_ERASE_GRP_START ((u8)35) /*!< To set the address of the first write block to be erased.
(For MMC card only spec 3.31) */
#define SD_CMD_ERASE_GRP_END ((u8)36) /*!< To set the address of the last write block of the
continuous range to be erased. (For MMC card only spec 3.31) */
#define SD_CMD_ERASE ((u8)38)
#define SD_CMD_FAST_IO ((u8)39) /*!< SD Card doesn't support it */
#define SD_CMD_GO_IRQ_STATE ((u8)40) /*!< SD Card doesn't support it */
#define SD_CMD_LOCK_UNLOCK ((u8)42)
#define SD_CMD_APP_CMD ((u8)55)
#define SD_CMD_GEN_CMD ((u8)56)
#define SD_CMD_NO_CMD ((u8)64)
/**
* @brief Following commands are SD Card Specific commands.
* SDIO_APP_CMD :CMD55 should be sent before sending these commands.
*/
#define SD_CMD_APP_SD_SET_BUSWIDTH ((u8)6) /*!< For SD Card only */
#define SD_CMD_SD_APP_STAUS ((u8)13) /*!< For SD Card only */
#define SD_CMD_SD_APP_SEND_NUM_WRITE_BLOCKS ((u8)22) /*!< For SD Card only */
#define SD_CMD_SD_APP_OP_COND ((u8)41) /*!< For SD Card only */
#define SD_CMD_SD_APP_SET_CLR_CARD_DETECT ((u8)42) /*!< For SD Card only */
#define SD_CMD_SD_APP_SEND_SCR ((u8)51) /*!< For SD Card only */
#define SD_CMD_SDIO_RW_DIRECT ((u8)52) /*!< For SD I/O Card only */
#define SD_CMD_SDIO_RW_EXTENDED ((u8)53) /*!< For SD I/O Card only */
/**
* @brief Following commands are SD Card Specific security commands.
* SDIO_APP_CMD should be sent before sending these commands.
*/
#define SD_CMD_SD_APP_GET_MKB ((u8)43) /*!< For SD Card only */
#define SD_CMD_SD_APP_GET_MID ((u8)44) /*!< For SD Card only */
#define SD_CMD_SD_APP_SET_CER_RN1 ((u8)45) /*!< For SD Card only */
#define SD_CMD_SD_APP_GET_CER_RN2 ((u8)46) /*!< For SD Card only */
#define SD_CMD_SD_APP_SET_CER_RES2 ((u8)47) /*!< For SD Card only */
#define SD_CMD_SD_APP_GET_CER_RES1 ((u8)48) /*!< For SD Card only */
#define SD_CMD_SD_APP_SECURE_READ_MULTIPLE_BLOCK ((u8)18) /*!< For SD Card only */
#define SD_CMD_SD_APP_SECURE_WRITE_MULTIPLE_BLOCK ((u8)25) /*!< For SD Card only */
#define SD_CMD_SD_APP_SECURE_ERASE ((u8)38) /*!< For SD Card only */
#define SD_CMD_SD_APP_CHANGE_SECURE_AREA ((u8)49) /*!< For SD Card only */
#define SD_CMD_SD_APP_SECURE_WRITE_MKB ((u8)48) /*!< For SD Card only */
/* Macro 宏------------------------------------------------------------------*/
/* Variables 变量--------------------------------------------------------------*/
/* Constants 常量--------------------------------------------------------------*/
/* Function 函数--------------------------------------------------------------*/
uint8_t SD_WriteDisk(u8*buf,u32 sector,u8 cnt);
uint8_t SD_ReadDisk(u8*buf,u32 sector,u8 cnt);
SD_Error Sdio_Init(void);
#endif
/************************************************************** END OF FILE ****/
/**
******************************************************************************
* @file : user_sdio.c
* @brief : V1.00
******************************************************************************
* @attention
*
******************************************************************************
*/
/* Include 包含---------------------------------------------------------------*/
#include "user_sdio.h"
#include "string.h"
#include "user_sys.h"
#include "user_uart.h"
#include "user_delay.h"
/* Typedef 类型----------------------------------------------------------------*/
/*SDIO初始化结构体*/
SDIO_InitTypeDef SDIO_InitStructure;
/*SDIO命令结构体*/
SDIO_CmdInitTypeDef SDIO_CmdInitStructure;
/*SDIO数据配置结构体*/
SDIO_DataInitTypeDef SDIO_DataInitStructure;/* Define 定义----------------------------------------------------------------*/
/* Macro 宏------------------------------------------------------------------*/
/* Variables 变量--------------------------------------------------------------*/
/*SD卡类型(默认为1.x卡)*/
static uint8_t CardType=SDIO_STD_CAPACITY_SD_CARD_V1_1;
/*SD卡CSD,CID以及相对地址(RCA)数据*/
static uint32_t CSD_Tab[4],CID_Tab[4],RCA=0;
/*SD卡信息*/
SD_CardInfo SDCardInfo;
/*工作模式,注意,工作模式必须通过SD_SetDeviceMode,
后才算数.这里只是定义一个默认的模式(SD_DMA_MODE)*/
static uint8_t DeviceMode=SD_DMA_MODE;
/*SD_ReadDisk/SD_WriteDisk函数专用buf,
当这两个函数的数据缓存区地址不是4字节对齐的时候,
需要用到该数组,确保数据缓存区地址是4字节对齐的.*/
uint8_t SDIO_DATA_BUFFER[512];
/*传输结束标志,DMA读写时使用*/
volatile u8 TransferEnd=0;
/*数据传输错误标志,DMA读写时使用*/
volatile SD_Error TransferError=SD_OK;
/*是否发送停止传输标志位,DMA多块读写的时候用到 */
static u8 StopCondition=0;
/*读多块用*/
__align(4) u32 *tempbuff;
/* Constants 常量--------------------------------------------------------------*/
/* Function 函数--------------------------------------------------------------*/
/*!
\brief SD卡错误类型
\param[in] none
\param[in] none
\retval 错误类型
*/
SD_Error SD_CmdError(void)
{
/*SD卡状态*/
SD_Error ErrorStatus=SD_OK;
/*超时时间*/
uint32_t timeout=10000;
while(timeout--)
{
/*获取错误类型*/
if(SDIO_GetFlagStatus(SDIO_FLAG_CMDSENT) != RESET)
{
break;
}
}
/*命令响应超时错误*/
if(timeout==0)
{
return SD_CMD_RSP_TIMEOUT;
}
/*清楚所有标志位*/
SDIO_ClearFlag(0x5FF);
/*返回错误类型*/
return ErrorStatus;
}
/*!
\brief R7响应判断
\param[in] none
\param[in] none
\retval 错误类型
*/
SD_Error SD_Cmd_R7_Error(void)
{
/*SD卡状态*/
SD_Error ErrorStatus=SD_OK;
uint32_t status;
uint32_t timeout=10000;
while(timeout--)
{
status=SDIO->STA;
/*CRC错误/命令响应超时/已经收到响应(CRC校验成功)*/
if(status&((1<<0)|(1<<2)|(1<<6)))
{
break;
}
}
/*检测超时*/
if((timeout==0)||(status&(1<<2)))
{
/*当前卡不是2.0兼容卡,或者不支持设定的电压范围*/
ErrorStatus=SD_CMD_RSP_TIMEOUT;
/*清除命令响应超时标志*/
SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT);
/*返回错误类型*/
return ErrorStatus;
}
/*成功接收到响应*/
if(status&1<<6)
{
ErrorStatus=SD_OK;
/*清除响应标志*/
SDIO_ClearFlag(SDIO_FLAG_CMDREND);
}
/*返回状态*/
return ErrorStatus;
}
/*!
\brief R6响应判断
\param[in] 命令
\param[in] 参数
\retval 错误类型
*/
SD_Error SD_Cmd_R6_Error(u8 cmd,u16*prca)
{
/*错误类型*/
SD_Error ErrorStatus=SD_OK;
/*状态*/
uint32_t status;
/*临时变量*/
uint32_t rspr1;
while(1)
{
/*CRC错误/命令响应超时/已经收到响应(CRC校验成功)*/
status=SDIO->STA;
if(status&((1<<0)|(1<<2)|(1<<6)))
{
break;
}
}
/*响应超时判断*/
if(SDIO_GetFlagStatus(SDIO_FLAG_CTIMEOUT) != RESET)
{
/*清除命令响应超时标志*/
SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT);
/*返回超时错误*/
return SD_CMD_RSP_TIMEOUT;
}
/*CRC错误判断*/
if(SDIO_GetFlagStatus(SDIO_FLAG_CCRCFAIL) != RESET) //CRC错误
{
/*清除响应标志*/
SDIO_ClearFlag(SDIO_FLAG_CCRCFAIL);
/*返回CRC错误*/
return SD_CMD_CRC_FAIL;
}
/*判断是否响应cmd命令*/
if(SDIO->RESPCMD!=cmd)
{
printf("SDIO->RESPCMD:%x %x\r\n",SDIO->RESPCMD,cmd);
delay_ms(1000);
/*命令不合法错误*/
return SD_ILLEGAL_CMD;
}
/*清除所有标记*/
SDIO_ClearFlag(0x05FF);
/*得到响应*/
rspr1=SDIO->RESP1;
/*判断是否有未知错误,和命令不合法错误,CRC错误*/
if(SD_ALLZERO==(rspr1&(SD_R6_GENERAL_UNKNOWN_ERROR|SD_R6_ILLEGAL_CMD|SD_R6_COM_CRC_FAILED)))
{
/*右移16位得到,rca*/
*prca=(u16)(rspr1>>16);
return ErrorStatus;
}
/*未知错误*/
if(rspr1&SD_R6_GENERAL_UNKNOWN_ERROR)
{
/*返回未知错误*/
return SD_GENERAL_UNKNOWN_ERROR;
}
/*命令不合法*/
if(rspr1&SD_R6_ILLEGAL_CMD)
{
/*命令不合法错误*/
return SD_ILLEGAL_CMD;
}
/*CRC错误*/
if(rspr1&SD_R6_COM_CRC_FAILED)
{ /*返回CRC错误*/
return SD_COM_CRC_FAILED;
}
/*返回错误类型*/
return ErrorStatus;
}
/*!
\brief R3响应判断
\param[in] none
\param[in] none
\retval 错误类型
*/
SD_Error SD_Cmd_R3_Error(void)
{
/*错误状态*/
uint32_t status;
/*超时时间*/
uint32_t timeout=10000;
while(timeout--)
{
status=SDIO->STA;
/*CRC错误/命令响应超时/已经收到响应(CRC校验成功)*/
if(status&((1<<0)|(1<<2)|(1<<6)))
{
break;
}
}
/*响应超时*/
if(SDIO_GetFlagStatus(SDIO_FLAG_CTIMEOUT) != RESET)
{
/*清除命令响应超时标志*/
SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT);
/*返回超时错误*/
return SD_CMD_RSP_TIMEOUT;
}
/*清除所有标记*/
SDIO_ClearFlag(0x5FFF);
/*返回OK*/
return SD_OK;
}
/*!
\brief R2响应判断
\param[in] none
\param[in] none
\retval 错误类型
*/
SD_Error SD_Cmd_R2_Error(void)
{
SD_Error ErrorStatus=SD_OK;
u32 status;
u32 timeout=0xFFFFFFFF;
while(timeout--)
{
status=SDIO->STA;
/*CRC错误/命令响应超时/已经收到响应(CRC校验成功)*/
if(status&((1<<0)|(1<<2)|(1<<6)))
{
break;
}
}
/*响应超时*/
if((timeout==0)||(status&(1<<2)))
{
/*超时错误*/
ErrorStatus=SD_CMD_RSP_TIMEOUT;
/*清除命令响应超时标志*/
SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT);
return ErrorStatus;
}
/*CRC错误*/
if(SDIO_GetFlagStatus(SDIO_FLAG_CCRCFAIL) != RESET) //CRC错误
{
/*清除标志位*/
ErrorStatus=SD_CMD_CRC_FAIL;
SDIO_ClearFlag(SDIO_FLAG_CCRCFAIL); //清除响应标志
}
/*清除所有标注位*/
SDIO_ClearFlag(0x5FF);
/*返回错误类型*/
return ErrorStatus;
}
/*!
\brief R1响应判断
\param[in] 命令
\param[in] none
\retval 错误类型
*/
SD_Error SD_Cmd_R1_Error(uint8_t cmd)
{
/*错误状态*/
uint32_t status;
/*超时时间*/
uint32_t timeout=10000;
while(timeout--)
{
status=SDIO->STA;
/*CRC错误/命令响应超时/已经收到响应(CRC校验成功)*/
if(status&((1<<0)|(1<<2)|(1<<6)))
{
break;
}
}
/*判断是否超时*/
if(SDIO_GetFlagStatus(SDIO_FLAG_CTIMEOUT) != RESET)
{
/*清除命令响应超时标志*/
SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT);
/*返回超时错误*/
return SD_CMD_RSP_TIMEOUT;
}
/*判断是不是CRC错误*/
if(SDIO_GetFlagStatus(SDIO_FLAG_CCRCFAIL) != RESET)
{
/*清除标志*/
SDIO_ClearFlag(SDIO_FLAG_CCRCFAIL);
/*返回CRC错误*/
return SD_CMD_CRC_FAIL;
}
/*命令不匹配*/
if(SDIO->RESPCMD!=cmd)
{
/*返回命令不匹配错误*/
return SD_ILLEGAL_CMD;
}
/*清除标志*/
SDIO->ICR=0X5FF;
/*返回卡响*/
return (SD_Error)(SDIO->RESP1&SD_OCR_ERRORBITS);
}
/*!
\brief 设置SD卡上电是SDIO状态
\param[in] none
\param[in] none
\retval 错误类型
*/
SD_Error SD_PowerON(void)
{
/*SD卡容量类型*/
uint32_t SDType=SD_STD_CAPACITY;
uint8_t i=0;
uint32_t response=0,count=0,validvoltage=0;
/*SD卡状态*/
SD_Error ErrorStatus=SD_OK;
/*初始化时的时钟不能大于400KHz,且一根数据线*/
SDIO_InitStructure.SDIO_ClockDiv = SDIO_INIT_CLK_DIV; //HCLK = 72MHz, SDIOCLK = 72MHz, SDIO_CK = HCLK/(178 + 2) = 400 KHz
SDIO_InitStructure.SDIO_ClockEdge = SDIO_ClockEdge_Rising;//主时钟SDIOCLK产生CLK引脚时钟有效沿选择 上升沿
SDIO_InitStructure.SDIO_ClockBypass = SDIO_ClockBypass_Disable; //不使用旁路模式,直接用HCLK进行分频得到SDIO_CK
SDIO_InitStructure.SDIO_ClockPowerSave = SDIO_ClockPowerSave_Disable; //节能模式关
SDIO_InitStructure.SDIO_BusWide = SDIO_BusWide_1b; //1位数据线
SDIO_InitStructure.SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_Disable;//硬件流
/*初始化SDIO*/
SDIO_Init(&SDIO_InitStructure);
/*给上电*/
SDIO_SetPowerState(SDIO_PowerState_ON);
/*使能时钟*/
SDIO_ClockCmd(ENABLE);
/*尝试发送74次CMD0命令进入卡识别模式的空闲状态*/
for(i=0;i<74;i++)
{
SDIO_CmdInitStructure.SDIO_Argument = 0; //命令参数无
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_GO_IDLE_STATE; //CMD0
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_No; //无响应
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; //不等待
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; //使能命令状态机开始发送命令之前等待数据传输结束。
SDIO_SendCommand(&SDIO_CmdInitStructure); //写命令进命令寄存器
/*获取错误类型*/
ErrorStatus=SD_CmdError();
/*SD_OK状态跳出for循环*/
if(ErrorStatus==SD_OK)
{
break;
}
}
/*尝试74次后返回错误,无错误则不成立*/
if(ErrorStatus)
{
return ErrorStatus;//返回错误状态
}
/*发送CMD8,检测SD接口特性*/
SDIO_CmdInitStructure.SDIO_Argument = SD_CHECK_PATTERN;//参数检查模式
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_HS_SEND_EXT_CSD;//CMD8
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; //R7是短响应
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;//不等待
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;//使能命令状态机开始发送命令之前等待数据传输结束。
SDIO_SendCommand(&SDIO_CmdInitStructure); //使能命令状态机开始发送命令之前等待数据传输结束。
/*等待R7响应*/
ErrorStatus = SD_Cmd_R7_Error();
/*R7响应正常*/
if(ErrorStatus==SD_OK)
{
/*预判断为2.0卡,高容量卡,后面在判断实际的*/
CardType=SDIO_STD_CAPACITY_SD_CARD_V2_0;//SD 2.0卡
SDType=SD_HIGH_CAPACITY;//高容量卡
}
/*发送CMD55,如果是SD卡他会响应,如果是不会响应则是MMC卡*/
SDIO_CmdInitStructure.SDIO_Argument = 0x00;//无参数
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;//R1是短响应
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;//不等待
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;//使能命令状态机开始发送命令之前等待数据传输结束。
SDIO_SendCommand(&SDIO_CmdInitStructure); //使能命令状态机开始发送命令之前等待数据传输结束。
/*等待R1响应*/
ErrorStatus=SD_Cmd_R1_Error(SD_CMD_APP_CMD);
/*SD卡2.0 V1.1否则为MMC*/
if(ErrorStatus==SD_OK)
{
/*发送ACMD41 SD_APP_OP_COND,参数为:0x80100000 确定供电范围和容量*/
while((!validvoltage)&&(count<SD_MAX_VOLT_TRIAL))
{
/*发送扩展指令前先发CMD55*/
SDIO_CmdInitStructure.SDIO_Argument = 0x00;//无参数
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;//CMD55
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;//短响应
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;//不等待
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;//使能命令状态机开始发送命令之前等待数据传输结束。
SDIO_SendCommand(&SDIO_CmdInitStructure);//使能命令状态机开始发送命令之前等待数据传输结束。
/*等待R1响应*/
ErrorStatus=SD_Cmd_R1_Error(SD_CMD_APP_CMD);
/*响应错误*/
if(ErrorStatus!=SD_OK)
{
return ErrorStatus;
}
/*acmd41,命令参数由支持的电压范围及HCS位组成,HCS位置一来区分卡是SDSc还是sdhc*/
SDIO_CmdInitStructure.SDIO_Argument = SD_VOLTAGE_WINDOW_SD | SDType;//参数
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_APP_OP_COND;//CMD41
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; //R3是短响应
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;//不等待
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;//使能命令状态机开始发送命令之前等待数据传输结束。
SDIO_SendCommand(&SDIO_CmdInitStructure);//使能命令状态机开始发送命令之前等待数据传输结束。
/*等待R3响应*/
ErrorStatus=SD_Cmd_R3_Error();
/*响应错误判断*/
if(ErrorStatus!=SD_OK)
{
return ErrorStatus;
}
/*得到响应*/
response=SDIO->RESP1;
/*判断SD卡上电是否完成*/
validvoltage=(((response>>31)==1)?1:0);
/*次数加1*/
count++;
}
/*检查电压是否支持*/
if(count>=SD_MAX_VOLT_TRIAL)
{
/*返回电压错误*/
ErrorStatus=SD_INVALID_VOLTRANGE;
return ErrorStatus;
}
/*检查是否是高容量的SD卡*/
if(response&=SD_HIGH_CAPACITY)
{ /*上面预计V2.0类型是高容量,这里是确定*/
CardType=SDIO_HIGH_CAPACITY_SD_CARD;
}
}
else
/*不是SD卡,是MMC卡*/
{
/*MMC卡,发送CMD1 SDIO_SEND_OP_COND,参数为:0x80FF8000*/
while((!validvoltage)&&(count<SD_MAX_VOLT_TRIAL))
{
SDIO_CmdInitStructure.SDIO_Argument = SD_VOLTAGE_WINDOW_MMC;//参数
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_OP_COND;//CMD1
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;//R3是短响应
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;//不等待
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;//使能命令状态机开始发送命令之前等待数据传输结束。
SDIO_SendCommand(&SDIO_CmdInitStructure);//使能命令状态机开始发送命令之前等待数据传输结束。
/*等待R3响应*/
ErrorStatus=SD_Cmd_R3_Error();
/*响应错误*/
if(ErrorStatus!=SD_OK)
{ /*返回错误类型*/
return ErrorStatus;
}
/*得到响应*/
response=SDIO->RESP1;
/*检查是否上电完成*/
validvoltage=(((response>>31)==1)?1:0);
/*次数加1*/
count++;
}
/*判断是否支持电压*/
if(count>=SD_MAX_VOLT_TRIAL)
{
/*返回电压错误*/
ErrorStatus=SD_INVALID_VOLTRANGE;
return ErrorStatus;
}
/*卡类型是多媒体卡*/
CardType=SDIO_MULTIMEDIA_CARD;
}
/*返回类型*/
return(ErrorStatus);
}
/*!
\brief 初始化卡,并进入就绪状态
\param[in] none
\param[in] none
\retval 错误类型
*/
SD_Error SD_InitializeCards(void)
{
/*SD卡默认是ok状态*/
SD_Error ErrorStatus=SD_OK;
/*R6参数1*/
uint16_t rca = 0x01;
/*检查电源状态,确保为上电状态*/
if(SDIO_GetPowerState()==0)
{
return SD_REQUEST_NOT_APPLICABLE;
}
/*非SECURE_DIGITAL_IO_CARD*/
if(SDIO_SECURE_DIGITAL_IO_CARD!=CardType)
{
/*读取ID命令*/
SDIO_CmdInitStructure.SDIO_Argument = 0x0;//无参数
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_ALL_SEND_CID;//CMD2
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Long;//R2是长响应
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;//不等待
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;//使能命令状态机开始发送命令之前等待数据传输结束。
SDIO_SendCommand(&SDIO_CmdInitStructure);//使能命令状态机开始发送命令之前等待数据传输结束。
/*等待R2响应*/
ErrorStatus = SD_Cmd_R2_Error();
/*响应错误*/
if(ErrorStatus!=SD_OK)
{
return ErrorStatus;
}
/*保存ID*/
CID_Tab[0]=SDIO->RESP1;
CID_Tab[1]=SDIO->RESP2;
CID_Tab[2]=SDIO->RESP3;
CID_Tab[3]=SDIO->RESP4;
}
/*判断只要不是MMC卡就发CMD3指令请求RCA值*/
if((SDIO_STD_CAPACITY_SD_CARD_V1_1==CardType)||(SDIO_STD_CAPACITY_SD_CARD_V2_0==CardType)
||(SDIO_SECURE_DIGITAL_IO_COMBO_CARD==CardType)
||(SDIO_HIGH_CAPACITY_SD_CARD==CardType))
{
/*发送CMD3*/
SDIO_CmdInitStructure.SDIO_Argument = 0x00;//无参数
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_REL_ADDR;//CMD3
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; //R6短响应
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;//不等待
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;//使能命令状态机开始发送命令之前等待数据传输结束。
SDIO_SendCommand(&SDIO_CmdInitStructure);//使能命令状态机开始发送命令之前等待数据传输结束。
/*等待R6响应*/
ErrorStatus = SD_Cmd_R6_Error(SD_CMD_SET_REL_ADDR,&rca);
/*返回错误响应*/
if(ErrorStatus!=SD_OK)
{
/*返回响应错误*/
return ErrorStatus;
}
}
/*MMC卡指令请求RCA值*/
if (SDIO_MULTIMEDIA_CARD==CardType)
{
/*发送CMD3*/
SDIO_CmdInitStructure.SDIO_Argument = (u32)(rca<<16);//参数
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_REL_ADDR;//CMD3
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;//R6
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;//不等待
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;//使能命令状态机开始发送命令之前等待数据传输结束。
SDIO_SendCommand(&SDIO_CmdInitStructure);//使能命令状态机开始发送命令之前等待数据传输结束。
/*等待R6响应*/
ErrorStatus=SD_Cmd_R6_Error(SD_CMD_SET_REL_ADDR,&rca);
/*返回错误响应*/
if(ErrorStatus!=SD_OK)
{
/*返回响应错误*/
return ErrorStatus;
}
}
/*非SECURE_DIGITAL_IO_CARD*/
if(SDIO_SECURE_DIGITAL_IO_CARD!=CardType)
{
/*SD卡RCA值*/
RCA = rca;
/*发送参数为RCA,命令为CMD9获取CSD卡的操作条件信息*/
SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)(rca << 16);//参数
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_CSD;//CMD9
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Long;//R2长响应
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;//不等待
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;//使能命令状态机开始发送命令之前等待数据传输结束。
SDIO_SendCommand(&SDIO_CmdInitStructure);//使能命令状态机开始发送命令之前等待数据传输结束。
/*等待R2响应*/
ErrorStatus=SD_Cmd_R2_Error();
/*响应错误*/
if(ErrorStatus!=SD_OK)
{
/*返回错误响应*/
return ErrorStatus;
}
/*获取CSD卡的操作条件*/
CSD_Tab[0]=SDIO->RESP1;
CSD_Tab[1]=SDIO->RESP2;
CSD_Tab[2]=SDIO->RESP3;
CSD_Tab[3]=SDIO->RESP4;
}
return SD_OK;//卡初始化成功
}
/*!
\brief 解析卡信息函数
\param[in] none
\param[in] none
\retval 错误类型
*/
SD_Error SD_GetCardInfo(SD_CardInfo *cardinfo)
{
SD_Error errorstatus=SD_OK;
u8 tmp=0;
cardinfo->CardType=(u8)CardType; //卡类型
cardinfo->RCA=(u16)RCA; //卡RCA值
tmp=(u8)((CSD_Tab[0]&0xFF000000)>>24);
cardinfo->SD_csd.CSDStruct=(tmp&0xC0)>>6; //CSD结构
cardinfo->SD_csd.SysSpecVersion=(tmp&0x3C)>>2; //2.0协议还没定义这部分(为保留),应该是后续协议定义的
cardinfo->SD_csd.Reserved1=tmp&0x03; //2个保留位
tmp=(u8)((CSD_Tab[0]&0x00FF0000)>>16); //第1个字节
cardinfo->SD_csd.TAAC=tmp; //数据读时间1
tmp=(u8)((CSD_Tab[0]&0x0000FF00)>>8); //第2个字节
cardinfo->SD_csd.NSAC=tmp; //数据读时间2
tmp=(u8)(CSD_Tab[0]&0x000000FF); //第3个字节
cardinfo->SD_csd.MaxBusClkFrec=tmp; //传输速度
tmp=(u8)((CSD_Tab[1]&0xFF000000)>>24); //第4个字节
cardinfo->SD_csd.CardComdClasses=tmp<<4; //卡指令类高四位
tmp=(u8)((CSD_Tab[1]&0x00FF0000)>>16); //第5个字节
cardinfo->SD_csd.CardComdClasses|=(tmp&0xF0)>>4;//卡指令类低四位
cardinfo->SD_csd.RdBlockLen=tmp&0x0F; //最大读取数据长度
tmp=(u8)((CSD_Tab[1]&0x0000FF00)>>8); //第6个字节
cardinfo->SD_csd.PartBlockRead=(tmp&0x80)>>7; //允许分块读
cardinfo->SD_csd.WrBlockMisalign=(tmp&0x40)>>6; //写块错位
cardinfo->SD_csd.RdBlockMisalign=(tmp&0x20)>>5; //读块错位
cardinfo->SD_csd.DSRImpl=(tmp&0x10)>>4;
cardinfo->SD_csd.Reserved2=0; //保留
if((CardType==SDIO_STD_CAPACITY_SD_CARD_V1_1)||(CardType==SDIO_STD_CAPACITY_SD_CARD_V2_0)||(SDIO_MULTIMEDIA_CARD==CardType))//标准1.1/2.0卡/MMC卡
{
cardinfo->SD_csd.DeviceSize=(tmp&0x03)<<10; //C_SIZE(12位)
tmp=(u8)(CSD_Tab[1]&0x000000FF); //第7个字节
cardinfo->SD_csd.DeviceSize|=(tmp)<<2;
tmp=(u8)((CSD_Tab[2]&0xFF000000)>>24); //第8个字节
cardinfo->SD_csd.DeviceSize|=(tmp&0xC0)>>6;
cardinfo->SD_csd.MaxRdCurrentVDDMin=(tmp&0x38)>>3;
cardinfo->SD_csd.MaxRdCurrentVDDMax=(tmp&0x07);
tmp=(u8)((CSD_Tab[2]&0x00FF0000)>>16); //第9个字节
cardinfo->SD_csd.MaxWrCurrentVDDMin=(tmp&0xE0)>>5;
cardinfo->SD_csd.MaxWrCurrentVDDMax=(tmp&0x1C)>>2;
cardinfo->SD_csd.DeviceSizeMul=(tmp&0x03)<<1;//C_SIZE_MULT
tmp=(u8)((CSD_Tab[2]&0x0000FF00)>>8); //第10个字节
cardinfo->SD_csd.DeviceSizeMul|=(tmp&0x80)>>7;
cardinfo->CardCapacity=(cardinfo->SD_csd.DeviceSize+1);//计算卡容量
cardinfo->CardCapacity*=(1<<(cardinfo->SD_csd.DeviceSizeMul+2));
cardinfo->CardBlockSize=1<<(cardinfo->SD_csd.RdBlockLen);//块大小
cardinfo->CardCapacity*=cardinfo->CardBlockSize;
}else if(CardType==SDIO_HIGH_CAPACITY_SD_CARD) //高容量卡
{
tmp=(u8)(CSD_Tab[1]&0x000000FF); //第7个字节
cardinfo->SD_csd.DeviceSize=(tmp&0x3F)<<16;//C_SIZE
tmp=(u8)((CSD_Tab[2]&0xFF000000)>>24); //第8个字节
cardinfo->SD_csd.DeviceSize|=(tmp<<8);
tmp=(u8)((CSD_Tab[2]&0x00FF0000)>>16); //第9个字节
cardinfo->SD_csd.DeviceSize|=(tmp);
tmp=(u8)((CSD_Tab[2]&0x0000FF00)>>8); //第10个字节
cardinfo->CardCapacity=(long long)(cardinfo->SD_csd.DeviceSize+1)*512*1024;//计算卡容量
cardinfo->CardBlockSize=512; //块大小固定为512字节
}
cardinfo->SD_csd.EraseGrSize=(tmp&0x40)>>6;
cardinfo->SD_csd.EraseGrMul=(tmp&0x3F)<<1;
tmp=(u8)(CSD_Tab[2]&0x000000FF); //第11个字节
cardinfo->SD_csd.EraseGrMul|=(tmp&0x80)>>7;
cardinfo->SD_csd.WrProtectGrSize=(tmp&0x7F);
tmp=(u8)((CSD_Tab[3]&0xFF000000)>>24); //第12个字节
cardinfo->SD_csd.WrProtectGrEnable=(tmp&0x80)>>7;
cardinfo->SD_csd.ManDeflECC=(tmp&0x60)>>5;
cardinfo->SD_csd.WrSpeedFact=(tmp&0x1C)>>2;
cardinfo->SD_csd.MaxWrBlockLen=(tmp&0x03)<<2;
tmp=(u8)((CSD_Tab[3]&0x00FF0000)>>16); //第13个字节
cardinfo->SD_csd.MaxWrBlockLen|=(tmp&0xC0)>>6;
cardinfo->SD_csd.WriteBlockPaPartial=(tmp&0x20)>>5;
cardinfo->SD_csd.Reserved3=0;
cardinfo->SD_csd.ContentProtectAppli=(tmp&0x01);
tmp=(u8)((CSD_Tab[3]&0x0000FF00)>>8); //第14个字节
cardinfo->SD_csd.FileFormatGrouop=(tmp&0x80)>>7;
cardinfo->SD_csd.CopyFlag=(tmp&0x40)>>6;
cardinfo->SD_csd.PermWrProtect=(tmp&0x20)>>5;
cardinfo->SD_csd.TempWrProtect=(tmp&0x10)>>4;
cardinfo->SD_csd.FileFormat=(tmp&0x0C)>>2;
cardinfo->SD_csd.ECC=(tmp&0x03);
tmp=(u8)(CSD_Tab[3]&0x000000FF); //第15个字节
cardinfo->SD_csd.CSD_CRC=(tmp&0xFE)>>1;
cardinfo->SD_csd.Reserved4=1;
tmp=(u8)((CID_Tab[0]&0xFF000000)>>24); //第0个字节
cardinfo->SD_cid.ManufacturerID=tmp;
tmp=(u8)((CID_Tab[0]&0x00FF0000)>>16); //第1个字节
cardinfo->SD_cid.OEM_AppliID=tmp<<8;
tmp=(u8)((CID_Tab[0]&0x000000FF00)>>8); //第2个字节
cardinfo->SD_cid.OEM_AppliID|=tmp;
tmp=(u8)(CID_Tab[0]&0x000000FF); //第3个字节
cardinfo->SD_cid.ProdName1=tmp<<24;
tmp=(u8)((CID_Tab[1]&0xFF000000)>>24); //第4个字节
cardinfo->SD_cid.ProdName1|=tmp<<16;
tmp=(u8)((CID_Tab[1]&0x00FF0000)>>16); //第5个字节
cardinfo->SD_cid.ProdName1|=tmp<<8;
tmp=(u8)((CID_Tab[1]&0x0000FF00)>>8); //第6个字节
cardinfo->SD_cid.ProdName1|=tmp;
tmp=(u8)(CID_Tab[1]&0x000000FF); //第7个字节
cardinfo->SD_cid.ProdName2=tmp;
tmp=(u8)((CID_Tab[2]&0xFF000000)>>24); //第8个字节
cardinfo->SD_cid.ProdRev=tmp;
tmp=(u8)((CID_Tab[2]&0x00FF0000)>>16); //第9个字节
cardinfo->SD_cid.ProdSN=tmp<<24;
tmp=(u8)((CID_Tab[2]&0x0000FF00)>>8); //第10个字节
cardinfo->SD_cid.ProdSN|=tmp<<16;
tmp=(u8)(CID_Tab[2]&0x000000FF); //第11个字节
cardinfo->SD_cid.ProdSN|=tmp<<8;
tmp=(u8)((CID_Tab[3]&0xFF000000)>>24); //第12个字节
cardinfo->SD_cid.ProdSN|=tmp;
tmp=(u8)((CID_Tab[3]&0x00FF0000)>>16); //第13个字节
cardinfo->SD_cid.Reserved1|=(tmp&0xF0)>>4;
cardinfo->SD_cid.ManufactDate=(tmp&0x0F)<<8;
tmp=(u8)((CID_Tab[3]&0x0000FF00)>>8); //第14个字节
cardinfo->SD_cid.ManufactDate|=tmp;
tmp=(u8)(CID_Tab[3]&0x000000FF); //第15个字节
cardinfo->SD_cid.CID_CRC=(tmp&0xFE)>>1;
cardinfo->SD_cid.Reserved2=1;
return errorstatus;
}
/*!
\brief 选卡CMD7
\param[in] 卡的RCA地址
\param[in] none
\retval 错误状态
*/
SD_Error SD_SelectDeselect(u32 addr)
{
/*发送CMD7选卡命令*/
SDIO_CmdInitStructure.SDIO_Argument = addr;//发送CMD7,选择卡,短响应
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEL_DESEL_CARD;//CMD7
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;//R1短响应
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;//不等待
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;//使能命令状态机开始发送命令之前等待数据传输结束。
SDIO_SendCommand(&SDIO_CmdInitStructure);//使能命令状态机开始发送命令之前等待数据传输结束。
/*返回R1响应类型*/
return SD_Cmd_R1_Error(SD_CMD_SEL_DESEL_CARD);
}
/*!
\brief 查找SD卡的SCR寄存器值
\param[in] 卡相对地址
\param[in] 数据缓存区(存储SCR内容)
\retval 错误状态
*/
SD_Error FindSCR(u16 rca,u32 *pscr)
{
u32 index = 0;
SD_Error ErrorStatus = SD_OK;
u32 tempscr[2]={0,0};
/*设置快命令的长度CMD16*/
SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)8; //设置快 Size为8字节
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN; //CMD16
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; //R1短响应
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;//不等待
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;//使能命令状态机开始发送命令之前等待数据传输结束。
SDIO_SendCommand(&SDIO_CmdInitStructure);//使能命令状态机开始发送命令之前等待数据传输结束。
/*等待R1响应*/
ErrorStatus=SD_Cmd_R1_Error(SD_CMD_SET_BLOCKLEN);
if(ErrorStatus!=SD_OK)
{
/*返回错误状态*/
return ErrorStatus;
}
/*发送CMD55特定指令*/
SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16; //参数卡的相对地址
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;//CMD55
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;//R1短响应
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;//不等待
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;//使能命令状态机开始发送命令之前等待数据传输结束。
SDIO_SendCommand(&SDIO_CmdInitStructure);//使能命令状态机开始发送命令之前等待数据传输结束。
/*等待R1响应*/
ErrorStatus=SD_Cmd_R1_Error(SD_CMD_APP_CMD);
if(ErrorStatus!=SD_OK)
{
/*返回错误状态*/
return ErrorStatus;
}
/*SDIO发送配置*/
SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;//数据超时时间
SDIO_DataInitStructure.SDIO_DataLength = 8; //8个字节长度,block为8字节,SD卡到SDIO.
SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_8b ; //块大小8byte
SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToSDIO;//SDIO到SDIO
SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;//传送模式
SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable;//使能发送数据状态机
SDIO_DataConfig(&SDIO_DataInitStructure);//发送数据格式配置
/*SDIO发送数据读取存储器SCR值*/
SDIO_CmdInitStructure.SDIO_Argument = 0x0;//参数0
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_APP_SEND_SCR;//ACMD51
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;//R1短响应
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;//不等待
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;//使能命令状态机开始发送命令之前等待数据传输结束。
SDIO_SendCommand(&SDIO_CmdInitStructure);//使能命令状态机开始发送命令之前等待数据传输结束。
/*等待R1响应*/
ErrorStatus=SD_Cmd_R1_Error(SD_CMD_SD_APP_SEND_SCR);
if(ErrorStatus!=SD_OK)
{
/*返回错误状态*/
return ErrorStatus;
}
/*检查错误*/
while(!(SDIO->STA&(SDIO_FLAG_RXOVERR|SDIO_FLAG_DCRCFAIL|SDIO_FLAG_DTIMEOUT|SDIO_FLAG_DBCKEND|SDIO_FLAG_STBITERR)))
{
if(SDIO_GetFlagStatus(SDIO_FLAG_RXDAVL) != RESET)//接收FIFO数据可用
{
*(tempscr+index)=SDIO_ReadData(); //读取FIFO内容
index++;
if(index>=2)break;
}
}
if(SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET) //数据超时错误
{
SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT); //清错误标志
return SD_DATA_TIMEOUT;
}else if(SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET) //数据块CRC错误
{
SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL); //清错误标志
return SD_DATA_CRC_FAIL;
}else if(SDIO_GetFlagStatus(SDIO_FLAG_RXOVERR) != RESET) //接收fifo上溢错误
{
SDIO_ClearFlag(SDIO_FLAG_RXOVERR); //清错误标志
return SD_RX_OVERRUN;
}else if(SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET) //接收起始位错误
{
SDIO_ClearFlag(SDIO_FLAG_STBITERR);//清错误标志
return SD_START_BIT_ERR;
}
SDIO_ClearFlag(0x05FF);//清除所有标记
//把数据顺序按8位为单位倒过来.
*(pscr+1)=((tempscr[0]&SD_0TO7BITS)<<24)|((tempscr[0]&SD_8TO15BITS)<<8)|((tempscr[0]&SD_16TO23BITS)>>8)|((tempscr[0]&SD_24TO31BITS)>>24);
*(pscr)=((tempscr[1]&SD_0TO7BITS)<<24)|((tempscr[1]&SD_8TO15BITS)<<8)|((tempscr[1]&SD_16TO23BITS)>>8)|((tempscr[1]&SD_24TO31BITS)>>24);
return ErrorStatus;
}
/*!
\brief SDIO使能宽总线模式
\param[in] 0,不使能;1,使能;
\param[in] none
\retval 错误状态
*/
SD_Error SDEnWideBus(u8 enx)
{
SD_Error ErrorStatus = SD_OK;
u32 scr[2]={0,0};
u8 arg=0X00;
if(enx)arg=0X02;
else arg=0X00;
if(SDIO->RESP1&SD_CARD_LOCKED)
{
/*SD卡处于上锁状态*/
return SD_LOCK_UNLOCK_FAILED;
}
/*得到SCR寄存器数据*/
ErrorStatus=FindSCR(RCA,scr);
/*判断错误类型*/
if(ErrorStatus!=SD_OK)
{
/*返回错误类型*/
return ErrorStatus;
}
/*支持宽总线*/
if((scr[1]&SD_WIDE_BUS_SUPPORT)!=SD_ALLZERO)
{
/*发送CMD55参数是RCA*/
SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16;//参数
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;//CMD55
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;//R1短响应
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;//不等待
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;//使能命令状态机开始发送命令之前等待数据传输结束。
SDIO_SendCommand(&SDIO_CmdInitStructure);//使能命令状态机开始发送命令之前等待数据传输结束。
/*等待R1响应*/
ErrorStatus=SD_Cmd_R1_Error(SD_CMD_APP_CMD);
/*错误类型判断*/
if(ErrorStatus!=SD_OK)
{
/*返回错误类型*/
return ErrorStatus;
}
/*发送CMD6参数是02是4总线模式*/
SDIO_CmdInitStructure.SDIO_Argument = arg;//参数
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_SD_SET_BUSWIDTH;//CMD6
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;//R1短响应
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;//不等待
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;//使能命令状态机开始发送命令之前等待数据传输结束。
SDIO_SendCommand(&SDIO_CmdInitStructure);//使能命令状态机开始发送命令之前等待数据传输结束。
/*等待R1响应*/
ErrorStatus=SD_Cmd_R1_Error(SD_CMD_APP_SD_SET_BUSWIDTH);
/*返回错误类型*/
return ErrorStatus;
}else{
/*不支持宽总线设置*/
return SD_REQUEST_NOT_APPLICABLE;
}
}
/*!
\brief 设置SDIO总线宽度
\param[in] 位宽模式0是1位数据宽度;1是4位数据宽度;2是8位数据宽度
\param[in] none
\retval 错误状态
*/
SD_Error SD_EnableWideBusOperation(u32 wmode)
{
SD_Error ErrorStatus=SD_OK;
if(SDIO_MULTIMEDIA_CARD==CardType)
{
/*MMC卡不支持*/
return SD_UNSUPPORTED_FEATURE;
}
else if((SDIO_STD_CAPACITY_SD_CARD_V1_1==CardType)||(SDIO_STD_CAPACITY_SD_CARD_V2_0==CardType)||(SDIO_HIGH_CAPACITY_SD_CARD==CardType))
{ /*SD卡1.0或2.0或高容量*/
if(wmode>=2)
{ /*不支持8位模式*/
return SD_UNSUPPORTED_FEATURE;
}
else
{
ErrorStatus=SDEnWideBus(wmode);
if(SD_OK==ErrorStatus)
{
SDIO->CLKCR&=~(3<<11); //清除之前的位宽设置
SDIO->CLKCR|=(u16)wmode<<11;//1位或4位总线宽度
SDIO->CLKCR|=0<<14; //不开启硬件流控制
}
}
}
return ErrorStatus;
}
/*!
\brief 设置SDIO时钟
\param[in] 时钟分频系数
\param[in] none
\retval none
*/
void SDIO_Clock_Set(uint8_t clkdiv)
{
uint32_t tmpreg=SDIO->CLKCR;
tmpreg&=0XFFFFFF00;
tmpreg|=clkdiv;
SDIO->CLKCR=tmpreg;
}
/*!
\brief 设置SDIO时钟
\param[in] 时钟分频系数
\param[in] none
\retval none
*/
SD_Error SD_SetDeviceMode(u32 Mode)
{
SD_Error ErrorStatus = SD_OK;
if((Mode==SD_DMA_MODE)||(Mode==SD_POLLING_MODE))
{
DeviceMode=Mode;
}
else
{
ErrorStatus=SD_INVALID_PARAMETER;
}
return ErrorStatus;
}
/*!
\brief SDIO初始化
\param[in] none
\param[in] none
\retval none
*/
SD_Error Sdio_Init(void)
{
/*SD卡默认是ok状态*/
SD_Error ErrorStatus=SD_OK;
/*分频*/
uint8_t clkdiv=0;
/*SDIO外设寄存器设置为默认值*/
SDIO_DeInit();
/*SD卡上电*/
ErrorStatus = SD_PowerON();
/*初始化卡*/
if(ErrorStatus==SD_OK)
{ /*返回初始错误类型*/
ErrorStatus=SD_InitializeCards();
}
/*解析SD卡信息*/
if(ErrorStatus==SD_OK)
{ /*获取卡信息*/
ErrorStatus=SD_GetCardInfo(&SDCardInfo);
}
/*选中SD卡*/
if(ErrorStatus==SD_OK)
{ /*选中SD卡发送CMD7参数为RCA*/
ErrorStatus=SD_SelectDeselect((u32)(SDCardInfo.RCA<<16));
}
/*设置数据线数量*/
if(ErrorStatus==SD_OK)
{
/*4位宽度,如果是MMC卡,则不能用4位模式*/
ErrorStatus=SD_EnableWideBusOperation(1);
}
/*配置时钟SD卡错误状态是OK或是MMC卡*/
if((ErrorStatus==SD_OK)||(SDIO_MULTIMEDIA_CARD==CardType))
{
if(SDCardInfo.CardType==SDIO_STD_CAPACITY_SD_CARD_V1_1||SDCardInfo.CardType==SDIO_STD_CAPACITY_SD_CARD_V2_0)
{
/*V1.1/V2.0卡,设置最高72/12=6Mhz*/
clkdiv=SDIO_TRANSFER_CLK_DIV+6;
}else
{ /*SDHC等其他卡,设置最高72/6=12Mhz*/
clkdiv=SDIO_TRANSFER_CLK_DIV;
}
/*设置时钟频率,SDIO时钟计算公式:
SDIO_CK时钟=SDIOCLK/[clkdiv+2];
其中,SDIOCLK固定为48Mhz */
SDIO_Clock_Set(clkdiv);
/*设置查询模式*/
ErrorStatus=SD_SetDeviceMode(SD_POLLING_MODE);
}
/*返回错误状态*/
return ErrorStatus;
}
/*!
\brief 得到字节数以2位底数
\param[in] 字节数.
\param[in] none
\param[in] none
\retval 以2为底的指数值
*/
uint8_t convert_from_bytes_to_power_of_two(u16 NumberOfBytes)
{
u8 count=0;
while(NumberOfBytes!=1)
{
NumberOfBytes>>=1;
count++;
}
return count;
}
/*!
\brief 配置SDIO DMA
\param[in] 存储器地址
\param[in] 传输数据量
\param[in] 方向DMA_DIR_PeripheralDST ,存储器-->SDIO(写数据);
DMA_DIR_PeripheralSRC,SDIO-->存储器(读数据);
\retval none
*/
void SD_DMA_Config(u32*mbuf,u32 bufsize,u32 DMA_DIR)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE); //使能DMA2时钟
DMA_DeInit(DMA2_Channel4); //将DMA2的通道4寄存器重设为缺省值
DMA_Cmd(DMA2_Channel4, DISABLE ); //关闭DMA2 通道4
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&SDIO->FIFO; //DMA外设基地址
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)mbuf; //DMA内存基地址
DMA_InitStructure.DMA_DIR = DMA_DIR; //数据传输方向,从内存读取发送到外设
DMA_InitStructure.DMA_BufferSize = bufsize/4; //DMA通道的DMA缓存的大小
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设地址寄存器不变
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //内存地址寄存器递增
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word; //数据宽度为32位
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word; //数据宽度为32位
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //工作在正常缓存模式
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //DMA通道 x拥有高优先级
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //DMA通道x没有设置为内存到内存传输
DMA_Init(DMA2_Channel4, &DMA_InitStructure); //根据DMA_InitStruct中指定的参数初始化DMA的通道USART1_Tx_DMA_Channel所标识的寄存器
DMA_Cmd(DMA2_Channel4, DISABLE ); //开启DMA2 通道4
}
/*!
\brief 读取一个块
\param[in] 读数据缓存区(必须4字节对齐!!)
\param[in] 读取地址
\param[in] 块大小
\retval none
*/
SD_Error SD_ReadBlock(u8 *buf,long long addr,u16 blksize)
{
SD_Error ErrorStatus=SD_OK;
uint8_t power;
/*转换为u32指针 */
uint32_t count=0,*tempbuff=(u32*)buf;
uint32_t timeout=0xFFFFFFFF;
if(NULL==buf)
{
return SD_INVALID_PARAMETER;
}
/*//数据控制寄存器清零(关DMA) */
SDIO->DCTRL=0x0;
/*大容量卡*/
if(CardType==SDIO_HIGH_CAPACITY_SD_CARD)
{
blksize=512;
addr>>=9;
}
SDIO_DataInitStructure.SDIO_DataBlockSize= SDIO_DataBlockSize_1b ;//清除DPSM状态机配置
SDIO_DataInitStructure.SDIO_DataLength= 0 ;//数据长度
SDIO_DataInitStructure.SDIO_DataTimeOut=SD_DATATIMEOUT;//超时时间
SDIO_DataInitStructure.SDIO_DPSM=SDIO_DPSM_Enable;//使能发送数据状态机
SDIO_DataInitStructure.SDIO_TransferDir=SDIO_TransferDir_ToCard;//方向SDIO到卡
SDIO_DataInitStructure.SDIO_TransferMode=SDIO_TransferMode_Block;//传送模式
SDIO_DataConfig(&SDIO_DataInitStructure);//发送数据
/*卡锁住了*/
if(SDIO->RESP1&SD_CARD_LOCKED)
{
return SD_LOCK_UNLOCK_FAILED;
}
if((blksize>0)&&(blksize<=2048)&&((blksize&(blksize-1))==0))
{
power=convert_from_bytes_to_power_of_two(blksize);
/*发送CMD16+设置数据长度为blksize,短响应*/
SDIO_CmdInitStructure.SDIO_Argument = blksize;
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
SDIO_SendCommand(&SDIO_CmdInitStructure);
/*等待R1响应*/
ErrorStatus=SD_Cmd_R1_Error(SD_CMD_SET_BLOCKLEN);
/*响应错误*/
if(ErrorStatus!=SD_OK)
{
return ErrorStatus;
}
}else
{
return SD_INVALID_PARAMETER;
}
/*SDIO数据配置*/
SDIO_DataInitStructure.SDIO_DataBlockSize= power<<4 ;//清除DPSM状态机配置
SDIO_DataInitStructure.SDIO_DataLength= blksize ;
SDIO_DataInitStructure.SDIO_DataTimeOut=SD_DATATIMEOUT ;
SDIO_DataInitStructure.SDIO_DPSM=SDIO_DPSM_Enable;
SDIO_DataInitStructure.SDIO_TransferDir=SDIO_TransferDir_ToSDIO;
SDIO_DataInitStructure.SDIO_TransferMode=SDIO_TransferMode_Block;
SDIO_DataConfig(&SDIO_DataInitStructure);
/*发送CMD17+从addr地址出读取数据,短响应 */
SDIO_CmdInitStructure.SDIO_Argument = addr;
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_READ_SINGLE_BLOCK;
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
SDIO_SendCommand(&SDIO_CmdInitStructure);
/*等待R1响应*/
ErrorStatus=SD_Cmd_R1_Error(SD_CMD_READ_SINGLE_BLOCK);
/*响应错误*/
if(ErrorStatus!=SD_OK)
{
return ErrorStatus;
}
/*查询模式,轮询数据*/
if(DeviceMode==SD_POLLING_MODE)
{
INTX_DISABLE();//关闭总中断(POLLING模式,严禁中断打断SDIO读写操作!!!)
while(!(SDIO->STA&((1<<5)|(1<<1)|(1<<3)|(1<<10)|(1<<9))))//无上溢/CRC/超时/完成(标志)/起始位错误
{
if(SDIO_GetFlagStatus(SDIO_FLAG_RXFIFOHF) != RESET) //接收区半满,表示至少存了8个字
{
for(count=0;count<8;count++) //循环读取数据
{
*(tempbuff+count)=SDIO->FIFO;
}
tempbuff+=8;
timeout=0X7FFFFF; //读数据溢出时间
}else //处理超时
{
if(timeout==0)return SD_DATA_TIMEOUT;
timeout--;
}
}
if(SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET) //数据超时错误
{
SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT); //清错误标志
return SD_DATA_TIMEOUT;
}else if(SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET) //数据块CRC错误
{
SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL); //清错误标志
return SD_DATA_CRC_FAIL;
}else if(SDIO_GetFlagStatus(SDIO_FLAG_RXOVERR) != RESET) //接收fifo上溢错误
{
SDIO_ClearFlag(SDIO_FLAG_RXOVERR); //清错误标志
return SD_RX_OVERRUN;
}else if(SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET) //接收起始位错误
{
SDIO_ClearFlag(SDIO_FLAG_STBITERR);//清错误标志
return SD_START_BIT_ERR;
}
while(SDIO_GetFlagStatus(SDIO_FLAG_RXDAVL) != RESET) //FIFO里面,还存在可用数据
{
*tempbuff=SDIO_ReadData(); //循环读取数据
tempbuff++;
}
INTX_ENABLE();//开启总中断
SDIO_ClearFlag(0x05FF);//清除所有标记
}else if(DeviceMode==SD_DMA_MODE)
{
SD_DMA_Config((u32*)buf,blksize,DMA_DIR_PeripheralSRC);
TransferError=SD_OK;
StopCondition=0; //单块读,不需要发送停止传输指令
TransferEnd=0; //传输结束标置位,在中断服务置1
SDIO->MASK|=(1<<1)|(1<<3)|(1<<8)|(1<<5)|(1<<9); //配置需要的中断
SDIO_DMACmd(ENABLE);
while(((DMA2->ISR&0X2000)==RESET)&&(TransferEnd==0)&&(TransferError==SD_OK)&&timeout)timeout--;//等待传输完成
if(timeout==0)return SD_DATA_TIMEOUT;//超时
if(TransferError!=SD_OK)ErrorStatus=TransferError;
}
return ErrorStatus;
}
/*!
\brief SD卡读取多个块
\param[in] 读数据缓存区
\param[in] 块大小
\param[in] 要读取的块数
\retval 错误状态
*/
SD_Error SD_ReadMultiBlocks(u8 *buf,long long addr,u16 blksize,u32 nblks)
{
SD_Error ErrorStatus=SD_OK;
u8 power;
u32 count=0;
u32 timeout=0xFFFFFFFF;
tempbuff=(u32*)buf;//转换为u32指针
SDIO->DCTRL=0x0; //数据控制寄存器清零(关DMA)
if(CardType==SDIO_HIGH_CAPACITY_SD_CARD)//大容量卡
{
blksize=512;
addr>>=9;
}
SDIO_DataInitStructure.SDIO_DataBlockSize= 0; ;//清除DPSM状态机配置
SDIO_DataInitStructure.SDIO_DataLength= 0 ;
SDIO_DataInitStructure.SDIO_DataTimeOut=SD_DATATIMEOUT ;
SDIO_DataInitStructure.SDIO_DPSM=SDIO_DPSM_Enable;
SDIO_DataInitStructure.SDIO_TransferDir=SDIO_TransferDir_ToCard;
SDIO_DataInitStructure.SDIO_TransferMode=SDIO_TransferMode_Block;
SDIO_DataConfig(&SDIO_DataInitStructure);
if(SDIO->RESP1&SD_CARD_LOCKED)return SD_LOCK_UNLOCK_FAILED;//卡锁了
if((blksize>0)&&(blksize<=2048)&&((blksize&(blksize-1))==0))
{
power=convert_from_bytes_to_power_of_two(blksize);
SDIO_CmdInitStructure.SDIO_Argument = blksize;//发送CMD16+设置数据长度为blksize,短响应
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
SDIO_SendCommand(&SDIO_CmdInitStructure);
ErrorStatus=SD_Cmd_R1_Error(SD_CMD_SET_BLOCKLEN); //等待R1响应
if(ErrorStatus!=SD_OK)return ErrorStatus; //响应错误
}else return SD_INVALID_PARAMETER;
if(nblks>1) //多块读
{
if(nblks*blksize>SD_MAX_DATA_LENGTH)return SD_INVALID_PARAMETER;//判断是否超过最大接收长度
SDIO_DataInitStructure.SDIO_DataBlockSize= power<<4; ;//nblks*blksize,512块大小,卡到控制器
SDIO_DataInitStructure.SDIO_DataLength= nblks*blksize ;
SDIO_DataInitStructure.SDIO_DataTimeOut=SD_DATATIMEOUT ;
SDIO_DataInitStructure.SDIO_DPSM=SDIO_DPSM_Enable;
SDIO_DataInitStructure.SDIO_TransferDir=SDIO_TransferDir_ToSDIO;
SDIO_DataInitStructure.SDIO_TransferMode=SDIO_TransferMode_Block;
SDIO_DataConfig(&SDIO_DataInitStructure);
SDIO_CmdInitStructure.SDIO_Argument = addr;//发送CMD18+从addr地址出读取数据,短响应
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_READ_MULT_BLOCK;
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
SDIO_SendCommand(&SDIO_CmdInitStructure);
ErrorStatus=SD_Cmd_R1_Error(SD_CMD_READ_MULT_BLOCK);//等待R1响应
if(ErrorStatus!=SD_OK)return ErrorStatus; //响应错误
if(DeviceMode==SD_POLLING_MODE)
{
INTX_DISABLE();//关闭总中断(POLLING模式,严禁中断打断SDIO读写操作!!!)
while(!(SDIO->STA&((1<<5)|(1<<1)|(1<<3)|(1<<8)|(1<<9))))//无上溢/CRC/超时/完成(标志)/起始位错误
{
if(SDIO_GetFlagStatus(SDIO_FLAG_RXFIFOHF) != RESET) //接收区半满,表示至少存了8个字
{
for(count=0;count<8;count++) //循环读取数据
{
*(tempbuff+count)=SDIO->FIFO;
}
tempbuff+=8;
timeout=0X7FFFFF; //读数据溢出时间
}else //处理超时
{
if(timeout==0)return SD_DATA_TIMEOUT;
timeout--;
}
}
if(SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET) //数据超时错误
{
SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT); //清错误标志
return SD_DATA_TIMEOUT;
}else if(SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET) //数据块CRC错误
{
SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL); //清错误标志
return SD_DATA_CRC_FAIL;
}else if(SDIO_GetFlagStatus(SDIO_FLAG_RXOVERR) != RESET) //接收fifo上溢错误
{
SDIO_ClearFlag(SDIO_FLAG_RXOVERR); //清错误标志
return SD_RX_OVERRUN;
}else if(SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET) //接收起始位错误
{
SDIO_ClearFlag(SDIO_FLAG_STBITERR);//清错误标志
return SD_START_BIT_ERR;
}
while(SDIO_GetFlagStatus(SDIO_FLAG_RXDAVL) != RESET) //FIFO里面,还存在可用数据
{
*tempbuff=SDIO_ReadData(); //循环读取数据
tempbuff++;
}
if(SDIO_GetFlagStatus(SDIO_FLAG_DATAEND) != RESET) //接收结束
{
if((SDIO_STD_CAPACITY_SD_CARD_V1_1==CardType)||(SDIO_STD_CAPACITY_SD_CARD_V2_0==CardType)||(SDIO_HIGH_CAPACITY_SD_CARD==CardType))
{
SDIO_CmdInitStructure.SDIO_Argument = 0;//发送CMD12+结束传输
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_STOP_TRANSMISSION;
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
SDIO_SendCommand(&SDIO_CmdInitStructure);
ErrorStatus=SD_Cmd_R1_Error(SD_CMD_STOP_TRANSMISSION);//等待R1响应
if(ErrorStatus!=SD_OK)return ErrorStatus;
}
}
INTX_ENABLE();//开启总中断
SDIO_ClearFlag(0x05FF);//清除所有标记
}else if(DeviceMode==SD_DMA_MODE)
{
SD_DMA_Config((u32*)buf,nblks*blksize,DMA_DIR_PeripheralSRC);
TransferError=SD_OK;
StopCondition=1; //多块读,需要发送停止传输指令
TransferEnd=0; //传输结束标置位,在中断服务置1
SDIO->MASK|=(1<<1)|(1<<3)|(1<<8)|(1<<5)|(1<<9); //配置需要的中断
SDIO->DCTRL|=1<<3; //SDIO DMA使能
while(((DMA2->ISR&0X2000)==RESET)&&timeout)timeout--;//等待传输完成
if(timeout==0)return SD_DATA_TIMEOUT;//超时
while((TransferEnd==0)&&(TransferError==SD_OK));
if(TransferError!=SD_OK)ErrorStatus=TransferError;
}
}
return ErrorStatus;
}
/*!
\brief 读SD卡
\param[in] buf:读数据缓存区
\param[in] sector:扇区地址
\param[in] cnt:扇区个数
\retval 错误状态;0,正常;其他,错误代码;
*/
uint8_t SD_ReadDisk(u8*buf,u32 sector,u8 cnt)
{
u8 sta=SD_OK;
long long lsector=sector;
u8 n;
lsector<<=9;
if((u32)buf%4!=0)
{
/*单个sector的读操作*/
for(n=0;n<cnt;n++)
{
sta=SD_ReadBlock(SDIO_DATA_BUFFER,lsector+512*n,512);
memcpy(buf,SDIO_DATA_BUFFER,512);
buf+=512;
}
}else
{
if(cnt==1)
{ /*单个sector的读操作*/
sta=SD_ReadBlock(buf,lsector,512);
}
else
{ //多个sector
sta=SD_ReadMultiBlocks(buf,lsector,512,cnt);
}
}
return sta;
}
/*!
\brief 检查卡是否正在执行写操作
\param[in] 当前状态
\retval 错误状态
*/
SD_Error IsCardProgramming(u8 *pstatus)
{
vu32 respR1 = 0, status = 0;
SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16; //卡相对地址参数
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_STATUS;//发送CMD13
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
SDIO_SendCommand(&SDIO_CmdInitStructure);
status=SDIO->STA;
while(!(status&((1<<0)|(1<<6)|(1<<2))))status=SDIO->STA;//等待操作完成
if(SDIO_GetFlagStatus(SDIO_FLAG_CCRCFAIL) != RESET) //CRC检测失败
{
SDIO_ClearFlag(SDIO_FLAG_CCRCFAIL); //清除错误标记
return SD_CMD_CRC_FAIL;
}
if(SDIO_GetFlagStatus(SDIO_FLAG_CTIMEOUT) != RESET) //命令超时
{
SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT); //清除错误标记
return SD_CMD_RSP_TIMEOUT;
}
if(SDIO->RESPCMD!=SD_CMD_SEND_STATUS)return SD_ILLEGAL_CMD;
SDIO_ClearFlag(0x05FF);//清除所有标记
respR1=SDIO->RESP1;
*pstatus=(u8)((respR1>>9)&0x0000000F);
return SD_OK;
}
/*!
\brief SD卡写多个块
\param[in] buf:数据缓存区
\param[in] addr:写地址
\param[in] blksize:块大小
\param[in] nblks:要写入的块数
\retval 错误状态
*/
SD_Error SD_WriteMultiBlocks(u8 *buf,long long addr,u16 blksize,u32 nblks)
{
SD_Error errorstatus = SD_OK;
u8 power = 0, cardstate = 0;
u32 timeout=0,bytestransferred=0;
u32 count = 0, restwords = 0;
u32 tlen=nblks*blksize; //总长度(字节)
u32 *tempbuff = (u32*)buf;
if(buf==NULL)return SD_INVALID_PARAMETER; //参数错误
SDIO->DCTRL=0x0; //数据控制寄存器清零(关DMA)
SDIO_DataInitStructure.SDIO_DataBlockSize= 0; ; //清除DPSM状态机配置
SDIO_DataInitStructure.SDIO_DataLength= 0 ;
SDIO_DataInitStructure.SDIO_DataTimeOut=SD_DATATIMEOUT ;
SDIO_DataInitStructure.SDIO_DPSM=SDIO_DPSM_Enable;
SDIO_DataInitStructure.SDIO_TransferDir=SDIO_TransferDir_ToCard;
SDIO_DataInitStructure.SDIO_TransferMode=SDIO_TransferMode_Block;
SDIO_DataConfig(&SDIO_DataInitStructure);
if(SDIO->RESP1&SD_CARD_LOCKED)return SD_LOCK_UNLOCK_FAILED;//卡锁了
if(CardType==SDIO_HIGH_CAPACITY_SD_CARD)//大容量卡
{
blksize=512;
addr>>=9;
}
if((blksize>0)&&(blksize<=2048)&&((blksize&(blksize-1))==0))
{
power=convert_from_bytes_to_power_of_two(blksize);
SDIO_CmdInitStructure.SDIO_Argument = blksize; //发送CMD16+设置数据长度为blksize,短响应
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
SDIO_SendCommand(&SDIO_CmdInitStructure);
errorstatus=SD_Cmd_R1_Error(SD_CMD_SET_BLOCKLEN); //等待R1响应
if(errorstatus!=SD_OK)return errorstatus; //响应错误
}else return SD_INVALID_PARAMETER;
if(nblks>1)
{
if(nblks*blksize>SD_MAX_DATA_LENGTH)return SD_INVALID_PARAMETER;
if((SDIO_STD_CAPACITY_SD_CARD_V1_1==CardType)||(SDIO_STD_CAPACITY_SD_CARD_V2_0==CardType)||(SDIO_HIGH_CAPACITY_SD_CARD==CardType))
{
//提高性能
SDIO_CmdInitStructure.SDIO_Argument = (u32)RCA<<16; //发送ACMD55,短响应
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
SDIO_SendCommand(&SDIO_CmdInitStructure);
errorstatus=SD_Cmd_R1_Error(SD_CMD_APP_CMD); //等待R1响应
if(errorstatus!=SD_OK)return errorstatus;
SDIO_CmdInitStructure.SDIO_Argument =nblks; //发送CMD23,设置块数量,短响应
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCK_COUNT;
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
SDIO_SendCommand(&SDIO_CmdInitStructure);
errorstatus=SD_Cmd_R1_Error(SD_CMD_SET_BLOCK_COUNT);//等待R1响应
if(errorstatus!=SD_OK)return errorstatus;
}
SDIO_CmdInitStructure.SDIO_Argument =addr; //发送CMD25,多块写指令,短响应
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_WRITE_MULT_BLOCK;
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
SDIO_SendCommand(&SDIO_CmdInitStructure);
errorstatus=SD_Cmd_R1_Error(SD_CMD_WRITE_MULT_BLOCK); //等待R1响应
if(errorstatus!=SD_OK)return errorstatus;
SDIO_DataInitStructure.SDIO_DataBlockSize= power<<4; ; //blksize, 控制器到卡
SDIO_DataInitStructure.SDIO_DataLength= nblks*blksize ;
SDIO_DataInitStructure.SDIO_DataTimeOut=SD_DATATIMEOUT ;
SDIO_DataInitStructure.SDIO_DPSM=SDIO_DPSM_Enable;
SDIO_DataInitStructure.SDIO_TransferDir=SDIO_TransferDir_ToCard;
SDIO_DataInitStructure.SDIO_TransferMode=SDIO_TransferMode_Block;
SDIO_DataConfig(&SDIO_DataInitStructure);
if(DeviceMode==SD_POLLING_MODE)
{
timeout=0xFFFFFFFF;
INTX_DISABLE();//关闭总中断(POLLING模式,严禁中断打断SDIO读写操作!!!)
while(!(SDIO->STA&((1<<4)|(1<<1)|(1<<8)|(1<<3)|(1<<9))))//下溢/CRC/数据结束/超时/起始位错误
{
if(SDIO->STA&(1<<14)) //发送区半空,表示至少存了8字(32字节)
{
if((tlen-bytestransferred)<SD_HALFFIFOBYTES)//不够32字节了
{
restwords=((tlen-bytestransferred)%4==0)?((tlen-bytestransferred)/4):((tlen-bytestransferred)/4+1);
for(count=0;count<restwords;count++,tempbuff++,bytestransferred+=4)
{
SDIO_WriteData(*tempbuff);
}
}else //发送区半空,可以发送至少8字(32字节)数据
{
for(count=0;count<SD_HALFFIFO;count++)
{
SDIO_WriteData(*(tempbuff+count));
}
tempbuff+=SD_HALFFIFO;
bytestransferred+=SD_HALFFIFOBYTES;
}
timeout=0X3FFFFFFF; //写数据溢出时间
}else
{
if(timeout==0)return SD_DATA_TIMEOUT;
timeout--;
}
}
if(SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET) //数据超时错误
{
SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT); //清错误标志
return SD_DATA_TIMEOUT;
}else if(SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET) //数据块CRC错误
{
SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL); //清错误标志
return SD_DATA_CRC_FAIL;
}else if(SDIO_GetFlagStatus(SDIO_FLAG_TXUNDERR) != RESET) //接收fifo下溢错误
{
SDIO_ClearFlag(SDIO_FLAG_TXUNDERR); //清错误标志
return SD_TX_UNDERRUN;
}else if(SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET) //接收起始位错误
{
SDIO_ClearFlag(SDIO_FLAG_STBITERR);//清错误标志
return SD_START_BIT_ERR;
}
if(SDIO_GetFlagStatus(SDIO_FLAG_DATAEND) != RESET) //发送结束
{
if((SDIO_STD_CAPACITY_SD_CARD_V1_1==CardType)||(SDIO_STD_CAPACITY_SD_CARD_V2_0==CardType)||(SDIO_HIGH_CAPACITY_SD_CARD==CardType))
{
SDIO_CmdInitStructure.SDIO_Argument =0;//发送CMD12+结束传输
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_STOP_TRANSMISSION;
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
SDIO_SendCommand(&SDIO_CmdInitStructure);
errorstatus=SD_Cmd_R1_Error(SD_CMD_STOP_TRANSMISSION);//等待R1响应
if(errorstatus!=SD_OK)return errorstatus;
}
}
INTX_ENABLE();//开启总中断
SDIO_ClearFlag(0x05FF);//清除所有标记
}else if(DeviceMode==SD_DMA_MODE)
{
SD_DMA_Config((u32*)buf,nblks*blksize,DMA_DIR_PeripheralDST);//SDIO DMA配置
TransferError=SD_OK;
StopCondition=1; //多块写,需要发送停止传输指令
TransferEnd=0; //传输结束标置位,在中断服务置1
SDIO->MASK|=(1<<1)|(1<<3)|(1<<8)|(1<<4)|(1<<9); //配置产生数据接收完成中断
SDIO->DCTRL|=1<<3; //SDIO DMA使能.
timeout=0xFFFFFFFF;
while(((DMA2->ISR&0X2000)==RESET)&&timeout)timeout--;//等待传输完成
if(timeout==0) //超时
{
Sdio_Init(); //重新初始化SD卡,可以解决写入死机的问题
return SD_DATA_TIMEOUT; //超时
}
timeout=0xFFFFFFFF;
while((TransferEnd==0)&&(TransferError==SD_OK)&&timeout)timeout--;
if(timeout==0)return SD_DATA_TIMEOUT; //超时
if(TransferError!=SD_OK)return TransferError;
}
}
SDIO_ClearFlag(0x05FF);//清除所有标记
errorstatus=IsCardProgramming(&cardstate);
while((errorstatus==SD_OK)&&((cardstate==SD_CARD_PROGRAMMING)||(cardstate==SD_CARD_RECEIVING)))
{
errorstatus=IsCardProgramming(&cardstate);
}
return errorstatus;
}
/*!
\brief SD卡写1个块
\param[in] buf:数据缓存区
\param[in] sector:扇区地址
\param[in] cnt:扇区个数
\retval 错误状态
*/
SD_Error SD_WriteBlock(u8 *buf,long long addr, u16 blksize)
{
SD_Error ErrorStatus = SD_OK;
u8 power=0,cardstate=0;
u32 timeout=0,bytestransferred=0;
u32 cardstatus=0,count=0,restwords=0;
u32 tlen=blksize; //总长度(字节)
u32*tempbuff=(u32*)buf;
if(buf==NULL)return SD_INVALID_PARAMETER;//参数错误
SDIO->DCTRL=0x0; //数据控制寄存器清零(关DMA)
SDIO_DataInitStructure.SDIO_DataBlockSize= 0; ;//清除DPSM状态机配置
SDIO_DataInitStructure.SDIO_DataLength= 0 ;
SDIO_DataInitStructure.SDIO_DataTimeOut=SD_DATATIMEOUT ;
SDIO_DataInitStructure.SDIO_DPSM=SDIO_DPSM_Enable;
SDIO_DataInitStructure.SDIO_TransferDir=SDIO_TransferDir_ToCard;
SDIO_DataInitStructure.SDIO_TransferMode=SDIO_TransferMode_Block;
SDIO_DataConfig(&SDIO_DataInitStructure);
if(SDIO->RESP1&SD_CARD_LOCKED)return SD_LOCK_UNLOCK_FAILED;//卡锁了
if(CardType==SDIO_HIGH_CAPACITY_SD_CARD) //大容量卡
{
blksize=512;
addr>>=9;
}
if((blksize>0)&&(blksize<=2048)&&((blksize&(blksize-1))==0))
{
power=convert_from_bytes_to_power_of_two(blksize);
SDIO_CmdInitStructure.SDIO_Argument = blksize;//发送CMD16+设置数据长度为blksize,短响应
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
SDIO_SendCommand(&SDIO_CmdInitStructure);
ErrorStatus=SD_Cmd_R1_Error(SD_CMD_SET_BLOCKLEN); //等待R1响应
if(ErrorStatus!=SD_OK)return ErrorStatus; //响应错误
}else return SD_INVALID_PARAMETER;
SDIO_CmdInitStructure.SDIO_Argument = (u32)RCA<<16;//发送CMD13,查询卡的状态,短响应
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_STATUS;
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
SDIO_SendCommand(&SDIO_CmdInitStructure);
ErrorStatus=SD_Cmd_R1_Error(SD_CMD_SEND_STATUS); //等待R1响应
if(ErrorStatus!=SD_OK)return ErrorStatus;
cardstatus=SDIO->RESP1;
timeout=SD_DATATIMEOUT;
while(((cardstatus&0x00000100)==0)&&(timeout>0)) //检查READY_FOR_DATA位是否置位
{
timeout--;
SDIO_CmdInitStructure.SDIO_Argument = (u32)RCA<<16;//发送CMD13,查询卡的状态,短响应
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_STATUS;
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
SDIO_SendCommand(&SDIO_CmdInitStructure);
ErrorStatus=SD_Cmd_R1_Error(SD_CMD_SEND_STATUS); //等待R1响应
if(ErrorStatus!=SD_OK)return ErrorStatus;
cardstatus=SDIO->RESP1;
}
if(timeout==0)return SD_ERROR;
SDIO_CmdInitStructure.SDIO_Argument = addr;//发送CMD24,写单块指令,短响应
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_WRITE_SINGLE_BLOCK;
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
SDIO_SendCommand(&SDIO_CmdInitStructure);
ErrorStatus=SD_Cmd_R1_Error(SD_CMD_WRITE_SINGLE_BLOCK);//等待R1响应
if(ErrorStatus!=SD_OK)return ErrorStatus;
StopCondition=0; //单块写,不需要发送停止传输指令
SDIO_DataInitStructure.SDIO_DataBlockSize= power<<4; ; //blksize, 控制器到卡
SDIO_DataInitStructure.SDIO_DataLength= blksize ;
SDIO_DataInitStructure.SDIO_DataTimeOut=SD_DATATIMEOUT ;
SDIO_DataInitStructure.SDIO_DPSM=SDIO_DPSM_Enable;
SDIO_DataInitStructure.SDIO_TransferDir=SDIO_TransferDir_ToCard;
SDIO_DataInitStructure.SDIO_TransferMode=SDIO_TransferMode_Block;
SDIO_DataConfig(&SDIO_DataInitStructure);
timeout=0xFFFFFFFF;
if (DeviceMode == SD_POLLING_MODE)
{
INTX_DISABLE();//关闭总中断(POLLING模式,严禁中断打断SDIO读写操作!!!)
while(!(SDIO->STA&((1<<10)|(1<<4)|(1<<1)|(1<<3)|(1<<9))))//数据块发送成功/下溢/CRC/超时/起始位错误
{
if(SDIO_GetFlagStatus(SDIO_FLAG_TXFIFOHE) != RESET) //发送区半空,表示至少存了8个字
{
if((tlen-bytestransferred)<SD_HALFFIFOBYTES)//不够32字节了
{
restwords=((tlen-bytestransferred)%4==0)?((tlen-bytestransferred)/4):((tlen-bytestransferred)/4+1);
for(count=0;count<restwords;count++,tempbuff++,bytestransferred+=4)
{
SDIO_WriteData(*tempbuff);
}
}else
{
for(count=0;count<8;count++)
{
SDIO_WriteData(*(tempbuff+count));
}
tempbuff+=8;
bytestransferred+=32;
}
timeout=0X3FFFFFFF; //写数据溢出时间
}else
{
if(timeout==0)return SD_DATA_TIMEOUT;
timeout--;
}
}
if(SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET) //数据超时错误
{
SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT); //清错误标志
return SD_DATA_TIMEOUT;
}else if(SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET) //数据块CRC错误
{
SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL); //清错误标志
return SD_DATA_CRC_FAIL;
}else if(SDIO_GetFlagStatus(SDIO_FLAG_TXUNDERR) != RESET) //接收fifo下溢错误
{
SDIO_ClearFlag(SDIO_FLAG_TXUNDERR); //清错误标志
return SD_TX_UNDERRUN;
}else if(SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET) //接收起始位错误
{
SDIO_ClearFlag(SDIO_FLAG_STBITERR);//清错误标志
return SD_START_BIT_ERR;
}
INTX_ENABLE();//开启总中断
SDIO->ICR=0X5FF; //清除所有标记
}else if(DeviceMode==SD_DMA_MODE)
{
SD_DMA_Config((u32*)buf,blksize,DMA_DIR_PeripheralDST);//SDIO DMA配置
TransferError=SD_OK;
StopCondition=0; //单块写,不需要发送停止传输指令
TransferEnd=0; //传输结束标置位,在中断服务置1
SDIO->MASK|=(1<<1)|(1<<3)|(1<<8)|(1<<4)|(1<<9); //配置产生数据接收完成中断
SDIO->DCTRL|=1<<3; //SDIO DMA使能.
while(((DMA2->ISR&0X2000)==RESET)&&timeout)timeout--;//等待传输完成
if(timeout==0)
{
Sdio_Init(); //重新初始化SD卡,可以解决写入死机的问题
return SD_DATA_TIMEOUT; //超时
}
timeout=0xFFFFFFFF;
while((TransferEnd==0)&&(TransferError==SD_OK)&&timeout)timeout--;
if(timeout==0)return SD_DATA_TIMEOUT; //超时
if(TransferError!=SD_OK)return TransferError;
}
SDIO_ClearFlag(0x05FF);//清除所有标记
ErrorStatus=IsCardProgramming(&cardstate);
while((ErrorStatus==SD_OK)&&((cardstate==SD_CARD_PROGRAMMING)||(cardstate==SD_CARD_RECEIVING)))
{
ErrorStatus=IsCardProgramming(&cardstate);
}
return ErrorStatus;
}
/*!
\brief 写SD卡
\param[in] buf:写数据缓存区
\param[in] sector:扇区地址
\param[in] cnt:扇区个数
\retval 错误状态;0,正常;其他,错误代码;
*/
uint8_t SD_WriteDisk(u8*buf,u32 sector,u8 cnt)
{
u8 sta=SD_OK;
u8 n;
long long lsector=sector;
lsector<<=9;
if((u32)buf%4!=0)
{
for(n=0;n<cnt;n++)
{
memcpy(SDIO_DATA_BUFFER,buf,512);
sta=SD_WriteBlock(SDIO_DATA_BUFFER,lsector+512*n,512);//单个sector的写操作
buf+=512;
}
}else
{
if(cnt==1)sta=SD_WriteBlock(buf,lsector,512); //单个sector的写操作
else sta=SD_WriteMultiBlocks(buf,lsector,512,cnt); //多个sector
}
return sta;
}
/************************************************************** END OF FILE ****/
/**
******************************************************************************
* @file : user_mian.h
* @brief : V1.00
******************************************************************************
* @attention
*
******************************************************************************
*/
/* Include 包含---------------------------------------------------------------*/
#include "stm32f10x.h"
#include <stdbool.h>
#include "user_gpio.h"
#include "user_delay.h"
#include "user_rcc_config.h"
#include "user_uart.h"
#include "user_sdio.h"
/* Typedef 类型----------------------------------------------------------------*/
/* Define 定义----------------------------------------------------------------*/
/* Macro 宏------------------------------------------------------------------*/
/* Variables 变量--------------------------------------------------------------*/
extern SD_CardInfo SDCardInfo; //SD卡信息
extern uint8_t SDIO_DATA_BUFFER[512];
uint8_t SDIO_DATA_BUFFER1[512];
/* Constants 常量--------------------------------------------------------------*/
/* Function 函数--------------------------------------------------------------*/
int main(void)
{
uint32_t sd_size;
uint8_t *buf=SDIO_DATA_BUFFER1;
/*配置系统中断分组为2位抢占2位响应*/
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
/*延时函数初始化*/
delay_init();
/*RCC配置*/
Rcc_config();
/*GPIO初始化*/
Gpio_Init();
/*USART1初始化*/
Uart1_Init(9600);
/*初始化SD卡*/
while(Sdio_Init())
{
}
/*死循环*/
while(1)
{
switch(SDCardInfo.CardType)
{
case SDIO_STD_CAPACITY_SD_CARD_V1_1:printf("Card Type:SDSC V1.1\r\n");break;
case SDIO_STD_CAPACITY_SD_CARD_V2_0:printf("Card Type:SDSC V2.0\r\n");break;
case SDIO_HIGH_CAPACITY_SD_CARD:printf("Card Type:SDHC V2.0\r\n");break;
case SDIO_MULTIMEDIA_CARD:printf("Card Type:MMC Card\r\n");break;
}
printf("Card ManufacturerID:%d\r\n",SDCardInfo.SD_cid.ManufacturerID); //制造商ID
printf("Card RCA:%d\r\n",SDCardInfo.RCA); //卡相对地址
printf("Card Capacity:%d MB\r\n",(u32)(SDCardInfo.CardCapacity>>20)); //显示容量
printf("Card BlockSize:%d\r\n\r\n",SDCardInfo.CardBlockSize); //显示块大小
for(sd_size=0;sd_size<512;sd_size++)
{
SDIO_DATA_BUFFER[sd_size] = sd_size;
}
/*写扇区0*/
if(SD_WriteDisk(SDIO_DATA_BUFFER,0,1) ==0)
{
/*读取扇区0的内容*/
if(SD_ReadDisk(buf,0,1)==0)
{
/*打印0扇区数据*/
for(sd_size=0;sd_size<512;sd_size++)
{
printf("%x ",buf[sd_size]);
}
printf("\r\n\r\n\r\n\r\n");
}
}
delay_ms(10000);
}
}
/************************************************************** END OF FILE ****/