怎么在pc端sd卡id修改教程sd卡TMP_WRITE_PROTECT csd寄存器,来去除写保护?

SD卡命令格式解析_文档库 文档库最新最全的文档下载 当前位置: & SD卡命令格式解析 SD卡命令格式解析 SD卡的命令格式及解析Linux驱动开发 23:28:38 阅读500 评论0 字号:大中小 订阅1.SD卡的命令格式:SD卡的指令由6字节(Byte)组成,如下:Byte1:0 1 x x x x x x(命令号,由指令标志定义,如CMD39为进制0x27,那么完整的CMD39第一字节为,即0x27+0x40)Byte2-5:Command Arguments,命令参数,有些命令没有参数Byte6:前7位为CRC(Cyclic Redundacy Check,循环冗余校验)校验位,最后一位为停止位02.SD卡的命令SD卡命令共分为12类,分别为class0到class11,不同的SDd卡,主控根据其功能,支持不同的命令集 如下:Class0 :(卡的识别、初始化等基本命令集)CMD0:复位SD 卡.CMD1:读OCR寄存器.CMD9:读CSD寄存器.CMD10:读CID寄存器.CMD12:停止读多块时的数据传输CMD13:读 Card_Status 寄存器Class2 (读卡命令集):CMD16:设置块的长度CMD17:读单块.CMD18:读多块,直至主机发送CMD12为止 .Class4(写卡命令集) :CMD24:写单块.CMD25:写多块.CMD27:写CSD寄存器 .Class5 (擦除卡命令集):CMD32:设置擦除块的起始地址.CMD33:设置擦除块的终止地址.CMD38: 擦除所选择的块.Class6(写保护命令集):CMD28:设置写保护块的地址.CMD29:擦除写保护块的地址.CMD30: Ask the card for the status of the write protection bitsclass7:卡的锁定,解锁功能命令集class8:申请特定命令集 。class10 -11 :保留有关sd卡驱动和fat fs的实现用了3个文件来实现。sdboot.c为sd的驱动(可理解为pdd)层,主要实现一些对sd控制器的配置以及一些基本sd命令的实现和对sd卡的操作。sdmmc.c实现了从sd卡读取nk并跳到内存去运行的代码(基本可以理解为sd驱动的mdd层)。sdfat.c文件就是实现fat fs的。mdd层通过fatfs来对pdd层操作以实现读取文件。在整个过程中遇到了很多问题,现在列举如下:1)sd卡初始化问题配置gpio有关sd的功能:SDCMD, SDDAT[3:0]。使能CLKCON中的SDI位。时钟以及计算公式:SDIPRE = PCLK/(CLK)-1;INICLK=300000;SDCLK=; MMCCLK= cmd0-cmd55-cmd41-cmd2-cmd3-cmd7-cmd6-cmd172)对sd卡操作问题SD卡包括:一个标识寄存器CID,一个相应地址寄存器RCA,一个其他参数寄存器CSD。对sd卡的操作是驱动通过sd controller来发相应的命令以达到读写等操作的:发送命令通过SDICmdCon[7:0]的除了开始2bit:CmdIndex放置要发送的命令号;SDICmdCon[8]开始发送命令来完成的。检测卡的插入,直接用中断引脚的电平来判断。判断插入的卡是否是sd卡,用命令cmd55和cmd41,因为mmc卡对cmd55不做回应。命令9就是获取sd卡中csd寄存器的值的,该值包括很多sd卡 Word文档免费下载:&&|&& &&|&& &&|&& 23:11:17 && 阅读:2928&& SD卡命令共分为12类,分别为class0到class11, 不同的SDd卡,主控根据其功能,支持不同的命令集 如下: Class0 :(卡的识别、初始化等基本命令集) &CMD0:复位SD 卡. &CMD1:读OCR寄存器. &CMD9:读CSD寄存器. &CMD10:读CID寄存器. &CMD12:停止读多块时的数据传输 &CMD13:读 Card_Status 寄存器 Class2 (读卡命令集): CMD16:设置块的长度 CMD17:读单块. CMD18:读多块,直至主机发送CMD12为止 . Class4(写卡命令集) : CMD24:写单块. CMD25:写多块. CMD27:写CSD寄存器 . Class5 (擦除卡命令集): CMD32:设置擦除块的起始地址. CMD33:设置擦除块的终止地址. CMD38: 擦除所选择的块. Class6(写保护命令集): CMD28:设置写保护块的地址. CMD29:擦除写保护块的地址. CMD30: Ask the card for the status of the write protection bits class7:卡的锁定,解锁功能命令集&& class8:申请特定命令集&。& class10 -11 :保留 其中 class1,&&& class3,class9:SPI模式不支持&&&& & ****************************************************************************** & * @file&&& SDIO/sdcard.c & * @author& MCD Application Team & * @version V3.1.2 & * @date&&& 09/28/2009 & * @brief&& This file provides all the SD Card driver firmware functions. & ****************************************************************************** & * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS & * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO S***E & * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY & * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING & * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE & * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. & * &h2&¢er&& COPYRIGHT 2009 STMicroelectronics&/center&&/h2& /* Includes ------------------------------------------------------------------*/ #include &sdcard.h& /** @addtogroup STM32F10x_StdPeriph_Examples /** @addtogroup SDIO_Example /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ #define NULL 0 #define SDIO_STATIC_FLAGS&&&&&&&&&&&&&& ((uint32_t)0x000005FF) #define SDIO_CMD0TIMEOUT&&&&&&&&&&&&&&& ((uint32_t)0x) #define SDIO_FIFO_Address&&&&&&&&&&&&&& ((uint32_t)0x) /* Mask for errors Card Status R1 (OCR Register) */ #define SD_OCR_ADDR_OUT_OF_RANGE&&&&&&& ((uint32_t)0x) #define SD_OCR_ADDR_MISALIGNED&&&&&&&&& ((uint32_t)0x) #define SD_OCR_BLOCK_LEN_ERR&&&&&&&&&&& ((uint32_t)0x) #define SD_OCR_ERASE_SEQ_ERR&&&&&&&&&&& ((uint32_t)0x) #define SD_OCR_BAD_ERASE_PARAM&&&&&&&&& ((uint32_t)0x) #define SD_OCR_WRITE_PROT_VIOLATION&&&& ((uint32_t)0x) #define SD_OCR_LOCK_UNLOCK_FAILED&&&&&& ((uint32_t)0x) #define SD_OCR_COM_CRC_FAILED&&&&&&&&&& ((uint32_t)0x) #define SD_OCR_ILLEGAL_CMD&&&&&&&&&&&&& ((uint32_t)0x) #define SD_OCR_CARD_ECC_FAILED&&&&&&&&& ((uint32_t)0x) #define SD_OCR_CC_ERROR&&&&&&&&&&&&&&&& ((uint32_t)0x) #define SD_OCR_GENERAL_UNKNOWN_ERROR&&& ((uint32_t)0x) #define SD_OCR_STREAM_READ_UNDERRUN&&&& ((uint32_t)0x) #define SD_OCR_STREAM_WRITE_OVERRUN&&&& ((uint32_t)0x) #define SD_OCR_CID_CSD_OVERWRIETE&&&&&& ((uint32_t)0x) #define SD_OCR_WP_ERASE_SKIP&&&&&&&&&&& ((uint32_t)0x) #define SD_OCR_CARD_ECC_DISABLED&&&&&&& ((uint32_t)0x) #define SD_OCR_ERASE_RESET&&&&&&&&&&&&& ((uint32_t)0x) #define SD_OCR_AKE_SEQ_ERROR&&&&&&&&&&& ((uint32_t)0x) #define SD_OCR_ERRORBITS&&&&&&&&&&&&&&& ((uint32_t)0xFDFFE008) /* Masks for R6 Response */ #define SD_R6_GENERAL_UNKNOWN_ERROR&&&& ((uint32_t)0x) #define SD_R6_ILLEGAL_CMD&&&&&&&&&&&&&& ((uint32_t)0x) #define SD_R6_COM_CRC_FAILED&&&&&&&&&&& ((uint32_t)0x) #define SD_VOLTAGE_WINDOW_SD&&&&&&&&&&& ((uint32_t)0x) #define SD_HIGH_CAPACITY&&&&&&&&&&&&&&& ((uint32_t)0x) #define SD_STD_CAPACITY&&&&&&&&&&&&&&&& ((uint32_t)0x) #define SD_CHECK_PATTERN&&&&&&&&&&&&&&& ((uint32_t)0x000001AA) #define SD_MAX_VOLT_TRIAL&&&&&&&&&&&&&& ((uint32_t)0x0000FFFF) #define SD_ALLZERO&&&&&&&&&&&&&&&&&&&&& ((uint32_t)0x) #define SD_WIDE_BUS_SUPPORT&&&&&&&&&&&& ((uint32_t)0x) #define SD_SINGLE_BUS_SUPPORT&&&&&&&&&& ((uint32_t)0x) #define SD_CARD_LOCKED&&&&&&&&&&&&&&&&& ((uint32_t)0x) #define SD_CARD_PROGRAMMING&&&&&&&&&&&& ((uint32_t)0x) #define SD_CARD_RECEIVING&&&&&&&&&&&&&& ((uint32_t)0x) #define SD_DATATIMEOUT&&&&&&&&&&&&&&&&& ((uint32_t)0x000FFFFF) #define SD_0TO7BITS&&&&&&&&&&&&&&&&&&&& ((uint32_t)0x000000FF) #define SD_8TO15BITS&&&&&&&&&&&&&&&&&&& ((uint32_t)0x0000FF00) #define SD_16TO23BITS&&&&&&&&&&&&&&&&&& ((uint32_t)0x00FF0000) #define SD_24TO31BITS&&&&&&&&&&&&&&&&&& ((uint32_t)0xFF000000) #define SD_MAX_DATA_LENGTH&&&&&&&&&&&&& ((uint32_t)0x01FFFFFF) #define SD_HALFFIFO&&&&&&&&&&&&&&&&&&&& ((uint32_t)0x) #define SD_HALFFIFOBYTES&&&&&&&&&&&&&&& ((uint32_t)0x) /* Command Class Supported */ #define SD_CCCC_LOCK_UNLOCK&&&&&&&&&&&& ((uint32_t)0x) #define SD_CCCC_WRITE_PROT&&&&&&&&&&&&& ((uint32_t)0x) #define SD_CCCC_ERASE&&&&&&&&&&&&&&&&&& ((uint32_t)0x) /* Following commands are SD Card Specific commands. && SDIO_APP_CMD should be sent before sending these commands. */ #define SDIO_SEND_IF_COND&&&&&&&&&&&&&& ((uint32_t)0x) #define SDIO_INIT_CLK_DIV&&&&&&&&&&&&&&&&& ((uint8_t)0xB2) #define SDIO_TRANSFER_CLK_DIV&&&&&&&&&&&&& ((uint8_t)0x1) /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ static uint32_t CardType =& SDIO_STD_CAPACITY_SD_CARD_V1_1; //static uint32_t CardType =& SDIO_SECURE_DIGITAL_IO_COMBO_CARD; static uint32_t CSD_Tab[4], CID_Tab[4], RCA = 0; static uint32_t DeviceMode = SD_POLLING_MODE; static uint32_t TotalNumberOfBytes = 0, StopCondition = 0; uint32_t *SrcBuffer, *DestB volatile SD_Error TransferError = SD_OK; __IO uint32_t TransferEnd = 0; __IO uint32_t NumberOfBytes = 0; SDIO_InitTypeDef SDIO_InitS SDIO_CmdInitTypeDef SDIO_CmdInitS SDIO_DataInitTypeDef SDIO_DataInitS /* Private function prototypes -----------------------------------------------*/ static SD_Error CmdError(void); static SD_Error CmdResp1Error(uint8_t cmd); static SD_Error CmdResp7Error(void); static SD_Error CmdResp3Error(void); static SD_Error CmdResp2Error(void); static SD_Error CmdResp6Error(uint8_t cmd, uint16_t *prca); static SD_Error SDEnWideBus(FunctionalState NewState); static SD_Error IsCardProgramming(uint8_t *pstatus); static SD_Error FindSCR(uint16_t rca, uint32_t *pscr); static uint8_t convert_from_bytes_to_power_of_two(uint16_t NumberOfBytes); static void GPIO_Configuration(void); static void DMA_TxConfiguration(uint32_t *BufferSRC, uint32_t BufferSize); static void DMA_RxConfiguration(uint32_t *BufferDST, uint32_t BufferSize); /* Private functions ---------------------------------------------------------*/ & * @brief& Initializes the SD Card and put it into StandBy State (Ready & *&& for data transfer). & * @param& None & * @retval SD_Error: SD Card Error code. SD_Error SD_Init(void) & SD_Error errorstatus = SD_OK; & /* Configure SDIO interface GPIO */ & GPIO_Configuration(); & /* Enable the SDIO AHB Clock */ & RCC_AHBPeriphClockCmd(RCC_AHBPeriph_SDIO, ENABLE); & /* Enable the DMA2 Clock */ & RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE); & SDIO_DeInit(); & errorstatus = SD_PowerON();//对SD卡做一系列的检测 & if (errorstatus != SD_OK) &&& /* CMD Response TimeOut (wait for CMDSENT flag) */ &&& return(errorstatus); & errorstatus = SD_InitializeCards();//初始化SD卡 & if (errorstatus != SD_OK) &&& /* CMD Response TimeOut (wait for CMDSENT flag) */ &&& return(errorstatus); & /* Configure the SDIO peripheral */ & /* HCLK = 72 MHz, SDIOCLK = 72 MHz, SDIO_CK = HCLK/(2 + 1) = 24 MHz */& & SDIO_InitStructure.SDIO_ClockDiv = SDIO_TRANSFER_CLK_DIV; & SDIO_InitStructure.SDIO_ClockEdge = SDIO_ClockEdge_R & SDIO_InitStructure.SDIO_ClockBypass = SDIO_ClockBypass_D & SDIO_InitStructure.SDIO_ClockPowerSave = SDIO_ClockPowerSave_D & SDIO_InitStructure.SDIO_BusWide = SDIO_BusWide_1b; & SDIO_InitStructure.SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_D & SDIO_Init(&SDIO_InitStructure); & return(errorstatus); & * @brief& Enquires cards about their operating voltage and configures & *&& clock controls. 简短的任何查询,卡对他们的工作电压和配置 &&&&&& 时钟控制。 & * @param& None & * @retval SD_Error: SD Card Error code. SD_Error SD_PowerON(void) & SD_Error errorstatus = SD_OK; & uint32_t response = 0, count = 0; & bool validvoltage = FALSE; & uint32_t SDType = SD_STD_CAPACITY; & /* Power ON Sequence -------------------------------------------------------*/ & /* Configure the SDIO peripheral 配置的SDIO外设*/ & 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_R//时钟脉冲边沿 & SDIO_InitStructure.SDIO_ClockBypass = SDIO_ClockBypass_D//旁路时钟 & SDIO_InitStructure.SDIO_ClockPowerSave = SDIO_ClockPowerSave_D//时钟节电 & SDIO_InitStructure.SDIO_BusWide = SDIO_BusWide_1b;//总线宽度 & SDIO_InitStructure.SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_D//硬件流控制 & SDIO_Init(&SDIO_InitStructure);//即用上面的值初始化华SDIO & /* Set Power State to ON */ & SDIO_SetPowerState(SDIO_PowerState_ON);//打开电源 & /* Enable SDIO Clock */ & SDIO_ClockCmd(ENABLE); //使能SDIO时钟 & /* CMD0: GO_IDLE_STATE -------------------------------------------------------*/ & /* No CMD response required */ & SDIO_CmdInitStructure.SDIO_Argument = 0x0; & SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_GO_IDLE_STATE; & SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_No; & SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; & SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_E & SDIO_SendCommand(&SDIO_CmdInitStructure);//根据SDIO_CmdInitStructure中的参数发送命令 & errorstatus = CmdError();//查询命令发送的情况 & if (errorstatus != SD_OK) &&& /* CMD Response TimeOut (wait for CMDSENT flag) CMD相应超时 */ &&& return(errorstatus); & /* CMD8: SEND_IF_COND --------------------------------------------------------*/ & /* Send CMD8 to verify SD card interface operating condition 发送CMD8核实SD卡接口,操作条件*/ & /* Argument: - [31:12]: Reserved (shall be set to '0') &&&&&&&&&&&&&& - [11:8]: Supply Voltage (VHS) 0x1 (Range: 2.7-3.6 V) &&&&&&&&&&&&&& - [7:0]: Check Pattern (recommended 0xAA) */ & /* CMD Response: R7 */ & SDIO_CmdInitStructure.SDIO_Argument = SD_CHECK_PATTERN; & SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_SEND_IF_COND; & SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_S & SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; & SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_E & SDIO_SendCommand(&SDIO_CmdInitStructure);& //发送命令 & errorstatus = CmdResp7Error();//错误信息检测 & if (errorstatus == SD_OK) &&& CardType = SDIO_STD_CAPACITY_SD_CARD_V2_0; /* SD Card 2.0 */ &&& SDType = SD_HIGH_CAPACITY; &&& /* CMD55 */ &&& SDIO_CmdInitStructure.SDIO_Argument = 0x00; &&& SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_APP_CMD; &&& SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_S &&& SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; &&& SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_E &&& SDIO_SendCommand(&SDIO_CmdInitStructure); &&& errorstatus = CmdResp1Error(SDIO_APP_CMD); & /* CMD55 */ & SDIO_CmdInitStructure.SDIO_Argument = 0x00; & SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_APP_CMD; & SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_S & SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; & SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_E & SDIO_SendCommand(&SDIO_CmdInitStructure); & errorstatus = CmdResp1Error(SDIO_APP_CMD); & /* If errorstatus is Command TimeOut, it is a MMC card */ & /* If errorstatus is SD_OK it is a SD card: SD card 2.0 (voltage range mismatch) &&&& or SD card 1.x */ & if (errorstatus == SD_OK) &&& /* SD CARD */ &&& /* Send ACMD41 SD_APP_OP_COND with Argument 0x */ &&& while ((!validvoltage) && (count & SD_MAX_VOLT_TRIAL)) &&&&& /* SEND CMD55 APP_CMD with RCA as 0 */ &&&&& SDIO_CmdInitStructure.SDIO_Argument = 0x00; &&&&& SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_APP_CMD; &&&&& SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_S &&&&& SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; &&&&& SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_E &&&&& SDIO_SendCommand(&SDIO_CmdInitStructure); &&&&& errorstatus = CmdResp1Error(SDIO_APP_CMD); &&&&& if (errorstatus != SD_OK) &&&&&&& return(errorstatus); &&&&& SDIO_CmdInitStructure.SDIO_Argument = SD_VOLTAGE_WINDOW_SD | SDT &&&&& SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_SD_APP_OP_COND; &&&&& SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_S &&&&& SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; &&&&& SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_E &&&&& SDIO_SendCommand(&SDIO_CmdInitStructure); &&&&& errorstatus = CmdResp3Error(); &&&&& if (errorstatus != SD_OK) &&&&&&& return(errorstatus); &&&&& response = SDIO_GetResponse(SDIO_RESP1); &&&&& validvoltage = (bool) (((response && 31) == 1) ? 1 : 0); &&&&& count++; &&& if (count &= SD_MAX_VOLT_TRIAL) &&&&& errorstatus = SD_INVALID_VOLTRANGE; &&&&& return(errorstatus); &&& if (response &= SD_HIGH_CAPACITY) &&&&& CardType = SDIO_HIGH_CAPACITY_SD_CARD; & }/* else MMC Card */ & return(errorstatus); & * @brief& Turns the SDIO output signals off. & * @param& None & * @retval SD_Error: SD Card Error code. SD_Error SD_PowerOFF(void) & SD_Error errorstatus = SD_OK; & /* Set Power State to OFF */ & SDIO_SetPowerState(SDIO_PowerState_OFF); & return(errorstatus); & * @brief& Intialises all cards or single card as the case may be. & *&& Card(s) come into standby state. & * @param& None & * @retval SD_Error: SD Card Error code. SD_Error SD_InitializeCards(void) & SD_Error errorstatus = SD_OK; & uint16_t rca = 0x01; & if (SDIO_GetPowerState() == SDIO_PowerState_OFF) &&& errorstatus = SD_REQUEST_NOT_APPLICABLE; &&& return(errorstatus); & if (SDIO_SECURE_DIGITAL_IO_CARD != CardType) &&& /* Send CMD2 ALL_SEND_CID */ &&& SDIO_CmdInitStructure.SDIO_Argument = 0x0; &&& SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_ALL_SEND_CID; &&& SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_L &&& SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; &&& SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_E &&& SDIO_SendCommand(&SDIO_CmdInitStructure); &&& errorstatus = CmdResp2Error(); &&& if (SD_OK != errorstatus) &&&&& return(errorstatus); &&& CID_Tab[0] = SDIO_GetResponse(SDIO_RESP1); &&& CID_Tab[1] = SDIO_GetResponse(SDIO_RESP2); &&& CID_Tab[2] = SDIO_GetResponse(SDIO_RESP3); &&& CID_Tab[3] = SDIO_GetResponse(SDIO_RESP4); & 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)) &&& /* Send CMD3 SET_REL_ADDR with argument 0 */ &&& /* SD Card publishes its RCA. */ &&& SDIO_CmdInitStructure.SDIO_Argument = 0x00; &&& SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_SET_REL_ADDR; &&& SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_S &&& SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; &&& SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_E &&& SDIO_SendCommand(&SDIO_CmdInitStructure); &&& errorstatus = CmdResp6Error(SDIO_SET_REL_ADDR, &rca); &&& if (SD_OK != errorstatus) &&&&& return(errorstatus); & if (SDIO_SECURE_DIGITAL_IO_CARD != CardType) &&& /* Send CMD9 SEND_CSD with argument as card's RCA */ &&& SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)(rca && 16); &&& SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_SEND_CSD; &&& SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_L &&& SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; &&& SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_E &&& SDIO_SendCommand(&SDIO_CmdInitStructure); &&& errorstatus = CmdResp2Error(); &&& if (SD_OK != errorstatus) &&&&& return(errorstatus); &&& CSD_Tab[0] = SDIO_GetResponse(SDIO_RESP1); &&& CSD_Tab[1] = SDIO_GetResponse(SDIO_RESP2); &&& CSD_Tab[2] = SDIO_GetResponse(SDIO_RESP3); &&& CSD_Tab[3] = SDIO_GetResponse(SDIO_RESP4); & errorstatus = SD_OK; /* All cards get intialized */ & return(errorstatus); & * @brief& Returns information about specific card. & * @param& cardinfo : pointer to a SD_CardInfo structure & *&& that contains all SD card information. & * @retval SD_Error: SD Card Error code. SD_Error SD_GetCardInfo(SD_CardInfo *cardinfo) & SD_Error errorstatus = SD_OK; & uint8_t tmp = 0; & cardinfo-&CardType = (uint8_t)CardT & cardinfo-&RCA = (uint16_t)RCA; & /* Byte 0 */ & tmp = (uint8_t)((CSD_Tab[0] & 0xFF000000) && 24); & cardinfo-&SD_csd.CSDStruct = (tmp & 0xC0) && 6; & cardinfo-&SD_csd.SysSpecVersion = (tmp & 0x3C) && 2; & cardinfo-&SD_csd.Reserved1 = tmp & 0x03; & /* Byte 1 */ & tmp = (uint8_t)((CSD_Tab[0] & 0x00FF0000) && 16); & cardinfo-&SD_csd.TAAC = & /* Byte 2 */ & tmp = (uint8_t)((CSD_Tab[0] & 0x0000FF00) && 8); & cardinfo-&SD_csd.NSAC = & /* Byte 3 */ & tmp = (uint8_t)(CSD_Tab[0] & 0x000000FF); & cardinfo-&SD_csd.MaxBusClkFrec = & /* Byte 4 */ & tmp = (uint8_t)((CSD_Tab[1] & 0xFF000000) && 24); & cardinfo-&SD_csd.CardComdClasses = tmp && 4; & /* Byte 5 */ & tmp = (uint8_t)((CSD_Tab[1] & 0x00FF0000) && 16); & cardinfo-&SD_csd.CardComdClasses |= (tmp & 0xF0) && 4; & cardinfo-&SD_csd.RdBlockLen = tmp & 0x0F; & /* Byte 6 */ & tmp = (uint8_t)((CSD_Tab[1] & 0x0000FF00) && 8); & 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; /* Reserved */ & if ((CardType == SDIO_STD_CAPACITY_SD_CARD_V1_1) || (CardType == SDIO_STD_CAPACITY_SD_CARD_V2_0)) &&& cardinfo-&SD_csd.DeviceSize = (tmp & 0x03) && 10; &&& /* Byte 7 */ &&& tmp = (uint8_t)(CSD_Tab[1] & 0x000000FF); &&& cardinfo-&SD_csd.DeviceSize |= (tmp) && 2; &&& /* Byte 8 */ &&& tmp = (uint8_t)((CSD_Tab[2] & 0xFF000000) && 24); &&& cardinfo-&SD_csd.DeviceSize |= (tmp & 0xC0) && 6; &&& cardinfo-&SD_csd.MaxRdCurrentVDDMin = (tmp & 0x38) && 3; &&& cardinfo-&SD_csd.MaxRdCurrentVDDMax = (tmp & 0x07); &&& /* Byte 9 */ &&& tmp = (uint8_t)((CSD_Tab[2] & 0x00FF0000) && 16); &&& cardinfo-&SD_csd.MaxWrCurrentVDDMin = (tmp & 0xE0) && 5; &&& cardinfo-&SD_csd.MaxWrCurrentVDDMax = (tmp & 0x1C) && 2; &&& cardinfo-&SD_csd.DeviceSizeMul = (tmp & 0x03) && 1; &&& /* Byte 10 */ &&& tmp = (uint8_t)((CSD_Tab[2] & 0x0000FF00) && 8); &&& 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-&CardBlockS & else if (CardType == SDIO_HIGH_CAPACITY_SD_CARD) &&& /* Byte 7 */ &&& tmp = (uint8_t)(CSD_Tab[1] & 0x000000FF); &&& cardinfo-&SD_csd.DeviceSize = (tmp & 0x3F) && 16; &&& /* Byte 8 */ &&& tmp = (uint8_t)((CSD_Tab[2] & 0xFF000000) && 24); &&& cardinfo-&SD_csd.DeviceSize |= (tmp && 8); &&& /* Byte 9 */ &&& tmp = (uint8_t)((CSD_Tab[2] & 0x00FF0000) && 16); &&& cardinfo-&SD_csd.DeviceSize |= (tmp); &&& /* Byte 10 */ &&& tmp = (uint8_t)((CSD_Tab[2] & 0x0000FF00) && 8); &&& cardinfo-&CardCapacity = (cardinfo-&SD_csd.DeviceSize + 1) * 512 * 1024; &&& cardinfo-&CardBlockSize = 512;&&& & cardinfo-&SD_csd.EraseGrSize = (tmp & 0x40) && 6; & cardinfo-&SD_csd.EraseGrMul = (tmp & 0x3F) && 1; & /* Byte 11 */ & tmp = (uint8_t)(CSD_Tab[2] & 0x000000FF); & cardinfo-&SD_csd.EraseGrMul |= (tmp & 0x80) && 7; & cardinfo-&SD_csd.WrProtectGrSize = (tmp & 0x7F); & /* Byte 12 */ & tmp = (uint8_t)((CSD_Tab[3] & 0xFF000000) && 24); & 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; & /* Byte 13 */ & tmp = (uint8_t)((CSD_Tab[3] & 0x00FF0000) && 16); & 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); & /* Byte 14 */ & tmp = (uint8_t)((CSD_Tab[3] & 0x0000FF00) && 8); & 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); & /* Byte 15 */ & tmp = (uint8_t)(CSD_Tab[3] & 0x000000FF); & cardinfo-&SD_csd.CSD_CRC = (tmp & 0xFE) && 1; & cardinfo-&SD_csd.Reserved4 = 1; & /* Byte 0 */ & tmp = (uint8_t)((CID_Tab[0] & 0xFF000000) && 24); & cardinfo-&SD_cid.ManufacturerID = & /* Byte 1 */ & tmp = (uint8_t)((CID_Tab[0] & 0x00FF0000) && 16); & cardinfo-&SD_cid.OEM_AppliID = tmp && 8; & /* Byte 2 */ & tmp = (uint8_t)((CID_Tab[0] & 0x) && 8); & cardinfo-&SD_cid.OEM_AppliID |= & /* Byte 3 */ & tmp = (uint8_t)(CID_Tab[0] & 0x000000FF); & cardinfo-&SD_cid.ProdName1 = tmp && 24; & /* Byte 4 */ & tmp = (uint8_t)((CID_Tab[1] & 0xFF000000) && 24); & cardinfo-&SD_cid.ProdName1 |= tmp && 16; & /* Byte 5 */ & tmp = (uint8_t)((CID_Tab[1] & 0x00FF0000) && 16); & cardinfo-&SD_cid.ProdName1 |= tmp && 8; & /* Byte 6 */ & tmp = (uint8_t)((CID_Tab[1] & 0x0000FF00) && 8); & cardinfo-&SD_cid.ProdName1 |= & /* Byte 7 */ & tmp = (uint8_t)(CID_Tab[1] & 0x000000FF); & cardinfo-&SD_cid.ProdName2 = & /* Byte 8 */ & tmp = (uint8_t)((CID_Tab[2] & 0xFF000000) && 24); & cardinfo-&SD_cid.ProdRev = & /* Byte 9 */ & tmp = (uint8_t)((CID_Tab[2] & 0x00FF0000) && 16); & cardinfo-&SD_cid.ProdSN = tmp && 24; & /* Byte 10 */ & tmp = (uint8_t)((CID_Tab[2] & 0x0000FF00) && 8); & cardinfo-&SD_cid.ProdSN |= tmp && 16; & /* Byte 11 */ & tmp = (uint8_t)(CID_Tab[2] & 0x000000FF); & cardinfo-&SD_cid.ProdSN |= tmp && 8; & /* Byte 12 */ & tmp = (uint8_t)((CID_Tab[3] & 0xFF000000) && 24); & cardinfo-&SD_cid.ProdSN |= & /* Byte 13 */ & tmp = (uint8_t)((CID_Tab[3] & 0x00FF0000) && 16); & cardinfo-&SD_cid.Reserved1 |= (tmp & 0xF0) && 4; & cardinfo-&SD_cid.ManufactDate = (tmp & 0x0F) && 8; & /* Byte 14 */ & tmp = (uint8_t)((CID_Tab[3] & 0x0000FF00) && 8); & cardinfo-&SD_cid.ManufactDate |= & /* Byte 15 */ & tmp = (uint8_t)(CID_Tab[3] & 0x000000FF); & cardinfo-&SD_cid.CID_CRC = (tmp & 0xFE) && 1; & cardinfo-&SD_cid.Reserved2 = 1; & return(errorstatus); & * @brief& Enables wide bus opeartion for the requeseted card if & *&& supported by card. & * @param& WideMode: Specifies the SD card wide bus mode. & *&& This parameter can be one of the following values: & *&&&& @arg SDIO_BusWide_8b: 8-bit data transfer (Only for MMC) & *&&&& @arg SDIO_BusWide_4b: 4-bit data transfer & *&&&& @arg SDIO_BusWide_1b: 1-bit data transfer & * @retval SD_Error: SD Card Error code. SD_Error SD_EnableWideBusOperation(uint32_t WideMode) & SD_Error errorstatus = SD_OK; & /* MMC Card doesn't support this feature */ & if (SDIO_MULTIMEDIA_CARD == CardType) &&& errorstatus = SD_UNSUPPORTED_FEATURE; &&& return(errorstatus); & else if ((SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType) || (SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType) || (SDIO_HIGH_CAPACITY_SD_CARD == CardType)) &&& if (SDIO_BusWide_8b == WideMode) &&&&& errorstatus = SD_UNSUPPORTED_FEATURE; &&&&& return(errorstatus); &&& else if (SDIO_BusWide_4b == WideMode) &&&&& errorstatus = SDEnWideBus(ENABLE); &&&&& if (SD_OK == errorstatus) &&&&&&& /* Configure the SDIO peripheral */ &&&&&&& SDIO_InitStructure.SDIO_ClockDiv = SDIO_TRANSFER_CLK_DIV; &&&&&&& SDIO_InitStructure.SDIO_ClockEdge = SDIO_ClockEdge_R &&&&&&& SDIO_InitStructure.SDIO_ClockBypass = SDIO_ClockBypass_D &&&&&&& SDIO_InitStructure.SDIO_ClockPowerSave = SDIO_ClockPowerSave_D &&&&&&& SDIO_InitStructure.SDIO_BusWide = SDIO_BusWide_4b; &&&&&&& SDIO_InitStructure.SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_D &&&&&&& SDIO_Init(&SDIO_InitStructure); &&&&& errorstatus = SDEnWideBus(DISABLE); &&&&& if (SD_OK == errorstatus) &&&&&&& /* Configure the SDIO peripheral */ &&&&&&& SDIO_InitStructure.SDIO_ClockDiv = SDIO_TRANSFER_CLK_DIV; &&&&&&& SDIO_InitStructure.SDIO_ClockEdge = SDIO_ClockEdge_R &&&&&&& SDIO_InitStructure.SDIO_ClockBypass = SDIO_ClockBypass_D &&&&&&& SDIO_InitStructure.SDIO_ClockPowerSave = SDIO_ClockPowerSave_D &&&&&&& SDIO_InitStructure.SDIO_BusWide = SDIO_BusWide_1b; &&&&&&& SDIO_InitStructure.SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_D &&&&&&& SDIO_Init(&SDIO_InitStructure); & return(errorstatus); & * @brief& Sets device mode whether to operate in Polling, Interrupt or & *&& DMA mode. & * @param& Mode: Specifies the Data Transfer mode. & *&& This parameter can be one of the following values: & *&&&& @arg SD_DMA_MODE: Data transfer using DMA. & *&&&& @arg SD_INTERRUPT_MODE: Data transfer using interrupts. & *&&&& @arg SD_POLLING_MODE: Data transfer using flags. & * @retval SD_Error: SD Card Error code. SD_Error SD_SetDeviceMode(uint32_t Mode) & SD_Error errorstatus = SD_OK; & if ((Mode == SD_DMA_MODE) || (Mode == SD_INTERRUPT_MODE) || (Mode == SD_POLLING_MODE)) &&& DeviceMode = M &&& errorstatus = SD_INVALID_PARAMETER; & return(errorstatus); & * @brief& Selects od Deselects the corresponding card. & * @param& addr: Address of the Card to be selected. & * @retval SD_Error: SD Card Error code. SD_Error SD_SelectDeselect(uint32_t addr) & SD_Error errorstatus = SD_OK; & /* Send CMD7 SDIO_SEL_DESEL_CARD */ & SDIO_CmdInitStructure.SDIO_Argument =& & SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_SEL_DESEL_CARD; & SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_S & SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; & SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_E & SDIO_SendCommand(&SDIO_CmdInitStructure); & errorstatus = CmdResp1Error(SDIO_SEL_DESEL_CARD); & return(errorstatus); & * @brief& Allows to read one block from a specified address in a card. & * @param& addr: Address from where data are to be read. & * @param& readbuff: pointer to the buffer that will contain the & *&& received data & * @param& BlockSize: the SD card Data block size. & * @retval SD_Error: SD Card Error code. SD_Error SD_ReadBlock(uint32_t addr, uint32_t *readbuff, uint16_t BlockSize) & SD_Error errorstatus = SD_OK; & uint32_t count = 0, *tempbuff = & uint8_t power = 0; & if (NULL == readbuff) &&& errorstatus = SD_INVALID_PARAMETER; &&& return(errorstatus); & TransferError = SD_OK; & TransferEnd = 0; & TotalNumberOfBytes = 0; & /* Clear all DPSM configuration */ & SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT; & SDIO_DataInitStructure.SDIO_DataLength = 0; & SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_1b; & SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToC & SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_B & SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_D & SDIO_DataConfig(&SDIO_DataInitStructure); & SDIO_DMACmd(DISABLE); & if (SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED) &&& errorstatus = SD_LOCK_UNLOCK_FAILED; &&& return(errorstatus); & if (CardType == SDIO_HIGH_CAPACITY_SD_CARD) &&& BlockSize = 512; &&& addr /= 512; & if ((BlockSize & 0) && (BlockSize &= 2048) && ((BlockSize & (BlockSize - 1)) == 0)) &&& power = convert_from_bytes_to_power_of_two(BlockSize); &&& /* Set Block Size for Card */ &&& SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) BlockS &&& SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_SET_BLOCKLEN; &&& SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_S &&& SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; &&& SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_E &&& SDIO_SendCommand(&SDIO_CmdInitStructure); &&& errorstatus = CmdResp1Error(SDIO_SET_BLOCKLEN); &&& if (SD_OK != errorstatus) &&&&& return(errorstatus); &&& errorstatus = SD_INVALID_PARAMETER; &&& return(errorstatus); & SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT; & SDIO_DataInitStructure.SDIO_DataLength = BlockS & SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t) power && 4; & SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToSDIO; & SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_B & SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_E & SDIO_DataConfig(&SDIO_DataInitStructure); & TotalNumberOfBytes = BlockS & StopCondition = 0; & DestBuffer = & /* Send CMD17 READ_SINGLE_BLOCK */ & SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) & SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_READ_SINGLE_BLOCK; & SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_S & SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; & //SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_P & SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_E & SDIO_SendCommand(&SDIO_CmdInitStructure); & errorstatus = CmdResp1Error(SDIO_READ_SINGLE_BLOCK); & if (errorstatus != SD_OK) &&& return(errorstatus); & /* In case of single block transfer, no need of stop transfer at all.*/ & if (DeviceMode == SD_POLLING_MODE) &&& /* Polling mode */ &&& while (!(SDIO-&STA &(SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR))) &&&&& if (SDIO_GetFlagStatus(SDIO_FLAG_RXFIFOHF) != RESET) &&&&&&& for (count = 0; count & 8; count++) &&&&&&&&& *(tempbuff + count) = SDIO_ReadData(); &&&&&&& tempbuff += 8; &&& if (SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET) &&&&& SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT); &&&&& errorstatus = SD_DATA_TIMEOUT; &&&&& return(errorstatus); &&& else if (SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET) &&&&& SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL); &&&&& errorstatus = SD_DATA_CRC_FAIL; &&&&& return(errorstatus); &&& else if (SDIO_GetFlagStatus(SDIO_FLAG_RXOVERR) != RESET) &&&&& SDIO_ClearFlag(SDIO_FLAG_RXOVERR); &&&&& errorstatus = SD_RX_OVERRUN; &&&&& return(errorstatus); &&& else if (SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET) &&&&& SDIO_ClearFlag(SDIO_FLAG_STBITERR); &&&&& errorstatus = SD_START_BIT_ERR; &&&&& return(errorstatus); &&& while (SDIO_GetFlagStatus(SDIO_FLAG_RXD***L) != RESET) &&&&& *tempbuff = SDIO_ReadData(); &&&&& tempbuff++; &&& /* Clear all the static flags */ &&& SDIO_ClearFlag(SDIO_STATIC_FLAGS); & else if (DeviceMode == SD_INTERRUPT_MODE) &&& SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_RXOVERR | SDIO_IT_RXFIFOHF | SDIO_IT_STBITERR, ENABLE); &&& while ((TransferEnd == 0) && (TransferError == SD_OK)) &&& if (TransferError != SD_OK) &&&&& return(TransferError); & else if (DeviceMode == SD_DMA_MODE) &&& SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_RXOVERR | SDIO_IT_STBITERR, ENABLE); &&& SDIO_DMACmd(ENABLE); &&& DMA_RxConfiguration(readbuff, BlockSize); &&& while (DMA_GetFlagStatus(DMA2_FLAG_TC4) == RESET) & return(errorstatus); & * @brief& Allows to read blocks from a specified address& in a card. & * @param& addr: Address from where data are to be read. & * @param& readbuff: pointer to the buffer that will contain the & *&& received data. & * @param& BlockSize: the SD card Data block size. & * @param& NumberOfBlocks: number of blocks to be read. & * @retval SD_Error: SD Card Error code. SD_Error SD_ReadMultiBlocks(uint32_t addr, uint32_t *readbuff, uint16_t BlockSize, uint32_t NumberOfBlocks) & SD_Error errorstatus = SD_OK; & uint32_t count = 0, *tempbuff = & uint8_t power = 0; & if (NULL == readbuff) &&& errorstatus = SD_INVALID_PARAMETER; &&& return(errorstatus); & TransferError = SD_OK; & TransferEnd = 0; & TotalNumberOfBytes = 0; & /* Clear all DPSM configuration */ & SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT; & SDIO_DataInitStructure.SDIO_DataLength = 0; & SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_1b; & SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToC & SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_B & SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_D & SDIO_DataConfig(&SDIO_DataInitStructure); & SDIO_DMACmd(DISABLE); & if (SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED) &&& errorstatus = SD_LOCK_UNLOCK_FAILED; &&& return(errorstatus); & if (CardType == SDIO_HIGH_CAPACITY_SD_CARD) &&& BlockSize = 512; &&& addr /= 512; & if ((BlockSize & 0) && (BlockSize &= 2048) && (0 == (BlockSize & (BlockSize - 1)))) &&& power = convert_from_bytes_to_power_of_two(BlockSize); &&& /* Set Block Size for Card */ &&& SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) BlockS &&& SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_SET_BLOCKLEN; &&& SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_S &&& SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; &&& SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_E &&& SDIO_SendCommand(&SDIO_CmdInitStructure); &&& errorstatus = CmdResp1Error(SDIO_SET_BLOCKLEN); &&& if (SD_OK != errorstatus) &&&&& return(errorstatus); &&& errorstatus = SD_INVALID_PARAMETER; &&& return(errorstatus); & if (NumberOfBlocks & 1) &&& /* Common to all modes */ &&& if (NumberOfBlocks * BlockSize & SD_MAX_DATA_LENGTH) &&&&& errorstatus = SD_INVALID_PARAMETER; &&&&& return(errorstatus); &&& TotalNumberOfBytes = NumberOfBlocks * BlockS &&& StopCondition = 1; &&& DestBuffer = &&& SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT; &&& SDIO_DataInitStructure.SDIO_DataLength = NumberOfBlocks * BlockS &&& SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t) power && 4; &&& SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToSDIO; &&& SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_B &&& SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_E &&& SDIO_DataConfig(&SDIO_DataInitStructure); &&& /* Send CMD18 READ_MULT_BLOCK with argument data address */ &&& SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) &&& SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_READ_MULT_BLOCK; &&& SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_S &&& SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; &&& SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_E &&& SDIO_SendCommand(&SDIO_CmdInitStructure); &&& errorstatus = CmdResp1Error(SDIO_READ_MULT_BLOCK); &&& if (errorstatus != SD_OK) &&&&& return(errorstatus); &&& if (DeviceMode == SD_POLLING_MODE) &&&&& /* Polling mode */ &&&&& while (!(SDIO-&STA &(SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DATAEND | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_STBITERR))) &&&&&&& if (SDIO_GetFlagStatus(SDIO_FLAG_RXFIFOHF) != RESET) &&&&&&&&& for (count = 0; count & SD_HALFFIFO; count++) &&&&&&&&& { &&&&&&&&&&& *(tempbuff + count) = SDIO_ReadData(); &&&&&&&&& } &&&&&&&&& tempbuff += SD_HALFFIFO; &&&&& if (SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET) &&&&&&& SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT); &&&&&&& errorstatus = SD_DATA_TIMEOUT; &&&&&&& return(errorstatus); &&&&& else if (SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET) &&&&&&& SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL); &&&&&&& errorstatus = SD_DATA_CRC_FAIL; &&&&&&& return(errorstatus); &&&&& else if (SDIO_GetFlagStatus(SDIO_FLAG_RXOVERR) != RESET) &&&&&&& SDIO_ClearFlag(SDIO_FLAG_RXOVERR); &&&&&&& errorstatus = SD_RX_OVERRUN; &&&&&&& return(errorstatus); &&&&& else if (SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET) &&&&&&& SDIO_ClearFlag(SDIO_FLAG_STBITERR); &&&&&&& errorstatus = SD_START_BIT_ERR; &&&&&&& return(errorstatus); &&&&& while (SDIO_GetFlagStatus(SDIO_FLAG_RXD***L) != RESET) &&&&&&& *tempbuff = SDIO_ReadData(); &&&&&&& tempbuff++; &&&&& if (SDIO_GetFlagStatus(SDIO_FLAG_DATAEND) != RESET) &&&&&&& /* In Case Of SD-CARD Send Command STOP_TRANSMISSION */ &&&&&&& if ((SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType) || (SDIO_HIGH_CAPACITY_SD_CARD == CardType) || (SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType)) &&&&&&&&& /* Send CMD12 STOP_TRANSMISSION */ &&&&&&&&& SDIO_CmdInitStructure.SDIO_Argument = 0x0; &&&&&&&&& SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_STOP_TRANSMISSION; &&&&&&&&& SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_S &&&&&&&&& SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; &&&&&&&&& SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_E &&&&&&&&& SDIO_SendCommand(&SDIO_CmdInitStructure); &&&&&&&&& errorstatus = CmdResp1Error(SDIO_STOP_TRANSMISSION); &&&&&&&&& if (errorstatus != SD_OK) &&&&&&&&& { &&&&&&&&&&& return(errorstatus); &&&&&&&&& } &&&&& /* Clear all the static flags */ &&&&& SDIO_ClearFlag(SDIO_STATIC_FLAGS); &&& else if (DeviceMode == SD_INTERRUPT_MODE) &&&&& SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_RXOVERR | SDIO_IT_RXFIFOHF | SDIO_IT_STBITERR, ENABLE); &&&&& while ((TransferEnd == 0) && (TransferError == SD_OK)) &&&&& if (TransferError != SD_OK) &&&&&&& return(TransferError); &&& else if (DeviceMode == SD_DMA_MODE) &&&&& SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_RXOVERR | SDIO_IT_STBITERR, ENABLE); &&&&& SDIO_DMACmd(ENABLE); &&&&& DMA_RxConfiguration(readbuff, (NumberOfBlocks * BlockSize)); &&&&& while (DMA_GetFlagStatus(DMA2_FLAG_TC4) == RESET) &&&&& while ((TransferEnd == 0) && (TransferError == SD_OK)) &&&&& if (TransferError != SD_OK) &&&&&&& return(TransferError); & return(errorstatus); & * @brief& Allows to write one block starting from a specified address & *&& in a card. & * @param& addr: Address from where data are to be read. & * @param& writebuff: pointer to the buffer that contain the data to be & *&& transferred. & * @param& BlockSize: the SD card Data block size. & * @retval SD_Error: SD Card Error code. SD_Error SD_WriteBlock(uint32_t addr, uint32_t *writebuff, uint16_t BlockSize) & SD_Error errorstatus = SD_OK; & uint8_t& power = 0, cardstate = 0; & uint32_t timeout = 0, bytestransferred = 0; & uint32_t cardstatus = 0, count = 0, restwords = 0; & uint32_t *tempbuff = & if (writebuff == NULL) &&& errorstatus = SD_INVALID_PARAMETER; &&& return(errorstatus); & TransferError = SD_OK; & TransferEnd = 0; & TotalNumberOfBytes = 0; & SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT; & SDIO_DataInitStructure.SDIO_DataLength = 0; & SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_1b; & SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToC & SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_B & SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_D & SDIO_DataConfig(&SDIO_DataInitStructure); & SDIO_DMACmd(DISABLE); & if (SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED) &&& errorstatus = SD_LOCK_UNLOCK_FAILED; &&& return(errorstatus); & if (CardType == SDIO_HIGH_CAPACITY_SD_CARD) &&& BlockSize = 512; &&& addr /= 512; & /* Set the block size, both on controller and card */ & if ((BlockSize & 0) && (BlockSize &= 2048) && ((BlockSize & (BlockSize - 1)) == 0)) &&& power = convert_from_bytes_to_power_of_two(BlockSize); &&& SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) BlockS &&& SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_SET_BLOCKLEN; &&& SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_S &&& SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; &&& SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_E &&& SDIO_SendCommand(&SDIO_CmdInitStructure); &&& errorstatus = CmdResp1Error(SDIO_SET_BLOCKLEN); &&& if (errorstatus != SD_OK) &&&&& return(errorstatus); &&& errorstatus = SD_INVALID_PARAMETER; &&& return(errorstatus); & /* Wait till card is ready for data Added */ & SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) (RCA && 16); & SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_SEND_STATUS;

我要回帖

更多关于 sd卡id修改工具 的文章

 

随机推荐