#ifndef __SDMMC_SDCARD_H #define __SDMMC_SDCARD_H #include "sys_type.h" ////////////////////////////////////////////////////////////////////////////////// //本程序只供学习使用,未经作者许可,不得用于其它任何用途 //ALIENTEK STM32H7开发板 //SDMMC 驱动代码 (仅提供查询模式驱动代码) //正点原子@ALIENTEK //技术论坛:www.openedv.com //创建日期:2018/7/31 //版本:V1.0 //版权所有,盗版必究。 //Copyright(C) 广州市星翼电子科技有限公司 2014-2024 //All rights reserved //******************************************************************************** //升级说明 //无 ////////////////////////////////////////////////////////////////////////////////// //用户配置区 //SDMMC时钟计算公式:SDMMC_CK时钟=sdmmc_ker_ck/[2*clkdiv];其中,sdmmc_ker_ck来自pll1_q_ck,为200Mhz //如果出现驱动错误,请尝试将SDMMC_TRANSFER_CLK_DIV频率降低 //#define SDMMC_INIT_CLK_DIV 0xFA //SDMMC初始化频率,200M/(250*2)=400Khz,最大400Kh #define SDMMC_TRANSFER_CLK_DIV 0x04 //SDMMC传输频率,该值太小可能会导致读写文件出错 //SD卡操作 各种错误枚举定义 typedef enum { //特殊错误定义 SD_CMD_CRC_FAIL = (1), /*!< Command response received (but CRC check failed) */ SD_DATA_CRC_FAIL = (2), /*!< Data block sent/received (CRC check failed) */ SD_CMD_RSP_TIMEOUT = (3), /*!< Command response timeout */ SD_DATA_TIMEOUT = (4), /*!< Data timeout */ SD_TX_UNDERRUN = (5), /*!< Transmit FIFO underrun */ SD_RX_OVERRUN = (6), /*!< Receive FIFO overrun */ SD_START_BIT_ERR = (7), /*!< Start bit not detected on all data signals in wide bus mode */ SD_CMD_OUT_OF_RANGE = (8), /*!< Command'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_SDMMC_DISABLED = (30), SD_SDMMC_FUNCTION_BUSY = (31), SD_SDMMC_FUNCTION_FAILED = (32), SD_SDMMC_UNKNOWN_FUNCTION = (33), //标准错误定义 SD_INTERNAL_ERROR = (34), SD_NOT_CONFIGURED = (35), SD_REQUEST_PENDING = (36), SD_REQUEST_NOT_APPLICABLE = (37), SD_INVALID_PARAMETER = (38), SD_UNSUPPORTED_FEATURE = (39), SD_UNSUPPORTED_HW = (40), SD_ERROR = (41), SD_OK = (0) } SD_Error; //SD卡CSD寄存器数据 typedef struct { u8 CSDStruct; /*!< CSD structure */ u8 SysSpecVersion; /*!< System specification version */ u8 Reserved1; /*!< Reserved */ u8 TAAC; /*!< Data read access-time 1 */ u8 NSAC; /*!< Data read access-time 2 in CLK cycles */ u8 MaxBusClkFrec; /*!< Max. bus clock frequency */ u16 CardComdClasses; /*!< Card command classes */ u8 RdBlockLen; /*!< Max. read data block length */ u8 PartBlockRead; /*!< Partial blocks for read allowed */ u8 WrBlockMisalign; /*!< Write block misalignment */ u8 RdBlockMisalign; /*!< Read block misalignment */ u8 DSRImpl; /*!< DSR implemented */ u8 Reserved2; /*!< Reserved */ u32 DeviceSize; /*!< Device Size */ u8 MaxRdCurrentVDDMin; /*!< Max. read current @ VDD min */ u8 MaxRdCurrentVDDMax; /*!< Max. read current @ VDD max */ u8 MaxWrCurrentVDDMin; /*!< Max. write current @ VDD min */ u8 MaxWrCurrentVDDMax; /*!< Max. write current @ VDD max */ u8 DeviceSizeMul; /*!< Device size multiplier */ u8 EraseGrSize; /*!< Erase group size */ u8 EraseGrMul; /*!< Erase group size multiplier */ u8 WrProtectGrSize; /*!< Write protect group size */ u8 WrProtectGrEnable; /*!< Write protect group enable */ u8 ManDeflECC; /*!< Manufacturer default ECC */ u8 WrSpeedFact; /*!< Write speed factor */ u8 MaxWrBlockLen; /*!< Max. write data block length */ u8 WriteBlockPaPartial; /*!< Partial blocks for write allowed */ u8 Reserved3; /*!< Reserded */ u8 ContentProtectAppli; /*!< Content protection application */ u8 FileFormatGrouop; /*!< File format group */ u8 CopyFlag; /*!< Copy flag (OTP) */ u8 PermWrProtect; /*!< Permanent write protection */ u8 TempWrProtect; /*!< Temporary write protection */ u8 FileFormat; /*!< File Format */ u8 ECC; /*!< ECC code */ u8 CSD_CRC; /*!< CSD CRC */ u8 Reserved4; /*!< always 1*/ } SD_CSD; //SD卡CID寄存器数据 typedef struct { u8 ManufacturerID; /*!< ManufacturerID */ u16 OEM_AppliID; /*!< OEM/Application ID */ u32 ProdName1; /*!< Product Name part1 */ u8 ProdName2; /*!< Product Name part2*/ u8 ProdRev; /*!< Product Revision */ u32 ProdSN; /*!< Product Serial Number */ u8 Reserved1; /*!< Reserved1 */ u16 ManufactDate; /*!< Manufacturing Date */ u8 CID_CRC; /*!< CID CRC */ u8 Reserved2; /*!< always 1 */ } SD_CID; //SD卡状态 typedef enum { SD_CARD_READY = ((uint32_t)0x00000001), SD_CARD_IDENTIFICATION = ((uint32_t)0x00000002), SD_CARD_STANDBY = ((uint32_t)0x00000003), SD_CARD_TRANSFER = ((uint32_t)0x00000004), SD_CARD_SENDING = ((uint32_t)0x00000005), SD_CARD_RECEIVING = ((uint32_t)0x00000006), SD_CARD_PROGRAMMING = ((uint32_t)0x00000007), SD_CARD_DISCONNECTED = ((uint32_t)0x00000008), SD_CARD_ERROR = ((uint32_t)0x000000FF) }SDCardState; //SD卡信息,包括CSD,CID等数据 typedef struct { SD_CSD SD_csd; SD_CID SD_cid; long long CardCapacity; //SD卡容量,单位:字节,最大支持2^64字节大小的卡. u32 CardBlockSize; //SD卡块大小 u16 RCA; //卡相对地址 u8 CardType; //卡类型 } SD_CardInfo; extern SD_CardInfo SDCardInfo;//SD卡信息 //////////////////////////////////////////////////////////////////////////////////////////////////// //SDMMC卡 指令集 //拷贝自:stm32f7xx_hal_sd.h #define SD_CMD_GO_IDLE_STATE ((uint8_t)0U) /*!< Resets the SD memory card. */ #define SD_CMD_SEND_OP_COND ((uint8_t)1U) /*!< Sends host capacity support information and activates the card's initialization process. */ #define SD_CMD_ALL_SEND_CID ((uint8_t)2U) /*!< Asks any card connected to the host to send the CID numbers on the CMD line. */ #define SD_CMD_SET_REL_ADDR ((uint8_t)3U) /*!< Asks the card to publish a new relative address (RCA). */ #define SD_CMD_SET_DSR ((uint8_t)4U) /*!< Programs the DSR of all cards. */ #define SD_CMD_SDMMC_SEN_OP_COND ((uint8_t)5U) /*!< Sends host capacity support information (HCS) and asks the accessed card to send its operating condition register (OCR) content in the response on the CMD line. */ #define SD_CMD_HS_SWITCH ((uint8_t)6U) /*!< Checks switchable function (mode 0) and switch card function (mode 1). */ #define SD_CMD_SEL_DESEL_CARD ((uint8_t)7U) /*!< Selects the card by its own relative address and gets deselected by any other address */ #define SD_CMD_HS_SEND_EXT_CSD ((uint8_t)8U) /*!< Sends SD Memory Card interface condition, which includes host supply voltage information and asks the card whether card supports voltage. */ #define SD_CMD_SEND_CSD ((uint8_t)9U) /*!< Addressed card sends its card specific data (CSD) on the CMD line. */ #define SD_CMD_SEND_CID ((uint8_t)10U) /*!< Addressed card sends its card identification (CID) on the CMD line. */ #define SD_CMD_READ_DAT_UNTIL_STOP ((uint8_t)11U) /*!< SD card doesn't support it. */ #define SD_CMD_STOP_TRANSMISSION ((uint8_t)12U) /*!< Forces the card to stop transmission. */ #define SD_CMD_SEND_STATUS ((uint8_t)13U) /*!< Addressed card sends its status register. */ #define SD_CMD_HS_BUSTEST_READ ((uint8_t)14U) #define SD_CMD_GO_INACTIVE_STATE ((uint8_t)15U) /*!< Sends an addressed card into the inactive state. */ #define SD_CMD_SET_BLOCKLEN ((uint8_t)16U) /*!< Sets the block length (in bytes for SDSC) for all following block commands (read, write, lock). Default block length is fixed to 512 Bytes. Not effective for SDHS and SDXC. */ #define SD_CMD_READ_SINGLE_BLOCK ((uint8_t)17U) /*!< Reads single block of size selected by SET_BLOCKLEN in case of SDSC, and a block of fixed 512 bytes in case of SDHC and SDXC. */ #define SD_CMD_READ_MULT_BLOCK ((uint8_t)18U) /*!< Continuously transfers data blocks from card to host until interrupted by STOP_TRANSMISSION command. */ #define SD_CMD_HS_BUSTEST_WRITE ((uint8_t)19U) /*!< 64 bytes tuning pattern is sent for SDR50 and SDR104. */ #define SD_CMD_WRITE_DAT_UNTIL_STOP ((uint8_t)20U) /*!< Speed class control command. */ #define SD_CMD_SET_BLOCK_COUNT ((uint8_t)23U) /*!< Specify block count for CMD18 and CMD25. */ #define SD_CMD_WRITE_SINGLE_BLOCK ((uint8_t)24U) /*!< Writes single block of size selected by SET_BLOCKLEN in case of SDSC, and a block of fixed 512 bytes in case of SDHC and SDXC. */ #define SD_CMD_WRITE_MULT_BLOCK ((uint8_t)25U) /*!< Continuously writes blocks of data until a STOP_TRANSMISSION follows. */ #define SD_CMD_PROG_CID ((uint8_t)26U) /*!< Reserved for manufacturers. */ #define SD_CMD_PROG_CSD ((uint8_t)27U) /*!< Programming of the programmable bits of the CSD. */ #define SD_CMD_SET_WRITE_PROT ((uint8_t)28U) /*!< Sets the write protection bit of the addressed group. */ #define SD_CMD_CLR_WRITE_PROT ((uint8_t)29U) /*!< Clears the write protection bit of the addressed group. */ #define SD_CMD_SEND_WRITE_PROT ((uint8_t)30U) /*!< Asks the card to send the status of the write protection bits. */ #define SD_CMD_SD_ERASE_GRP_START ((uint8_t)32U) /*!< Sets the address of the first write block to be erased. (For SD card only). */ #define SD_CMD_SD_ERASE_GRP_END ((uint8_t)33U) /*!< Sets the address of the last write block of the continuous range to be erased. */ #define SD_CMD_ERASE_GRP_START ((uint8_t)35U) /*!< Sets the address of the first write block to be erased. Reserved for each command system set by switch function command (CMD6). */ #define SD_CMD_ERASE_GRP_END ((uint8_t)36U) /*!< Sets the address of the last write block of the continuous range to be erased. Reserved for each command system set by switch function command (CMD6). */ #define SD_CMD_ERASE ((uint8_t)38U) /*!< Reserved for SD security applications. */ #define SD_CMD_FAST_IO ((uint8_t)39U) /*!< SD card doesn't support it (Reserved). */ #define SD_CMD_GO_IRQ_STATE ((uint8_t)40U) /*!< SD card doesn't support it (Reserved). */ #define SD_CMD_LOCK_UNLOCK ((uint8_t)42U) /*!< Sets/resets the password or lock/unlock the card. The size of the data block is set by the SET_BLOCK_LEN command. */ #define SD_CMD_APP_CMD ((uint8_t)55U) /*!< Indicates to the card that the next command is an application specific command rather than a standard command. */ #define SD_CMD_GEN_CMD ((uint8_t)56U) /*!< Used either to transfer a data block to the card or to get a data block from the card for general purpose/application specific commands. */ #define SD_CMD_NO_CMD ((uint8_t)64U) /** * @brief Following commands are SD Card Specific commands. * SDMMC_APP_CMD should be sent before sending these commands. */ #define SD_CMD_APP_SD_SET_BUSWIDTH ((uint8_t)6U) /*!< (ACMD6) Defines the data bus width to be used for data transfer. The allowed data bus widths are given in SCR register. */ #define SD_CMD_SD_APP_STATUS ((uint8_t)13U) /*!< (ACMD13) Sends the SD status. */ #define SD_CMD_SD_APP_SEND_NUM_WRITE_BLOCKS ((uint8_t)22U) /*!< (ACMD22) Sends the number of the written (without errors) write blocks. Responds with 32bit+CRC data block. */ #define SD_CMD_SD_APP_OP_COND ((uint8_t)41U) /*!< (ACMD41) Sends host capacity support information (HCS) and asks the accessed card to send its operating condition register (OCR) content in the response on the CMD line. */ #define SD_CMD_SD_APP_SET_CLR_CARD_DETECT ((uint8_t)42U) /*!< (ACMD42) Connects/Disconnects the 50 KOhm pull-up resistor on CD/DAT3 (pin 1) of the card. */ #define SD_CMD_SD_APP_SEND_SCR ((uint8_t)51U) /*!< Reads the SD Configuration Register (SCR). */ #define SD_CMD_SDMMC_RW_DIRECT ((uint8_t)52U) /*!< For SD I/O card only, reserved for security specification. */ #define SD_CMD_SDMMC_RW_EXTENDED ((uint8_t)53U) /*!< For SD I/O card only, reserved for security specification. */ /** * @brief Following commands are SD Card Specific security commands. * SD_CMD_APP_CMD should be sent before sending these commands. */ #define SD_CMD_SD_APP_GET_MKB ((uint8_t)43U) /*!< For SD card only */ #define SD_CMD_SD_APP_GET_MID ((uint8_t)44U) /*!< For SD card only */ #define SD_CMD_SD_APP_SET_CER_RN1 ((uint8_t)45U) /*!< For SD card only */ #define SD_CMD_SD_APP_GET_CER_RN2 ((uint8_t)46U) /*!< For SD card only */ #define SD_CMD_SD_APP_SET_CER_RES2 ((uint8_t)47U) /*!< For SD card only */ #define SD_CMD_SD_APP_GET_CER_RES1 ((uint8_t)48U) /*!< For SD card only */ #define SD_CMD_SD_APP_SECURE_READ_MULTIPLE_BLOCK ((uint8_t)18U) /*!< For SD card only */ #define SD_CMD_SD_APP_SECURE_WRITE_MULTIPLE_BLOCK ((uint8_t)25U) /*!< For SD card only */ #define SD_CMD_SD_APP_SECURE_ERASE ((uint8_t)38U) /*!< For SD card only */ #define SD_CMD_SD_APP_CHANGE_SECURE_AREA ((uint8_t)49U) /*!< For SD card only */ #define SD_CMD_SD_APP_SECURE_WRITE_MKB ((uint8_t)48U) /*!< For SD card only */ //CMD8指令 #define SD_SDMMC_SEND_IF_COND ((uint32_t)SD_CMD_HS_SEND_EXT_CSD) //////////////////////////////////////////////////////////////////////////////////////////////////// //支持的SD卡定义 #define STD_CAPACITY_SD_CARD_V1_1 ((uint32_t)0x00000000U) #define STD_CAPACITY_SD_CARD_V2_0 ((uint32_t)0x00000001U) #define HIGH_CAPACITY_SD_CARD ((uint32_t)0x00000002U) #define MULTIMEDIA_CARD ((uint32_t)0x00000003U) #define SECURE_DIGITAL_IO_CARD ((uint32_t)0x00000004U) #define HIGH_SPEED_MULTIMEDIA_CARD ((uint32_t)0x00000005U) #define SECURE_DIGITAL_IO_COMBO_CARD ((uint32_t)0x00000006U) #define HIGH_CAPACITY_MMC_CARD ((uint32_t)0x00000007U) //SDMMC相关参数定义 #define NULL 0 //#define SDMMC_STATIC_FLAGS ((u32)0x000205FF) #define SDMMC_CMD0TIMEOUT ((u32)0x00010000) //#define SDMMC_DATATIMEOUT ((u32)0xFFFFFFFF) //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) //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) #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) #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) #define SD_HALFFIFO ((u32)0x00000008) #define SD_HALFFIFOBYTES ((u32)0x00000020) //Command Class Supported #define SD_CCCC_LOCK_UNLOCK ((u32)0x00000080) #define SD_CCCC_WRITE_PROT ((u32)0x00000040) #define SD_CCCC_ERASE ((u32)0x00000020) //////////////////////////////////////////////////////////////////////////////////////////////////// //相关函数定义 SD_Error SD_Init(void); void SDMMC_Clock_Set(u16 clkdiv); void SDMMC_Send_Cmd(u8 cmdindex,u8 waitrsp,u32 arg); void SDMMC_Send_Data_Cfg(u32 datatimeout,u32 datalen,u8 blksize,u8 dir); SD_Error SD_PowerON(void); SD_Error SD_PowerOFF(void); SD_Error SD_InitializeCards(void); SD_Error SD_GetCardInfo(SD_CardInfo *cardinfo); SD_Error SD_EnableWideBusOperation(u32 wmode); SD_Error SD_SelectDeselect(u32 addr); SD_Error SD_SendStatus(uint32_t *pcardstatus); SDCardState SD_GetState(void); SD_Error SD_ReadBlocks(u8 *buf,long long addr,u16 blksize,u32 nblks); SD_Error SD_WriteBlocks(u8 *buf,long long addr,u16 blksize,u32 nblks); SD_Error CmdError(void); SD_Error CmdResp7Error(void); SD_Error CmdResp1Error(u8 cmd); SD_Error CmdResp3Error(void); SD_Error CmdResp2Error(void); SD_Error CmdResp6Error(u8 cmd,u16*prca); SD_Error SDEnWideBus(u8 enx); SD_Error IsCardProgramming(u8 *pstatus); SD_Error FindSCR(u16 rca,u32 *pscr); u8 SD_ReadDisk(u8*buf,u32 sector,u32 cnt); //读SD卡,fatfs/usb调用 u8 SD_WriteDisk(u8*buf,u32 sector,u32 cnt); //写SD卡,fatfs/usb调用 #endif