【GD32E503评测】07 测评SDIO总线驱动SD卡(二)
[复制链接]
根据上一篇的贴子,我们对SD卡这个外设有了更深入的了解。
这篇重点讲解移植的过程,遇到其他MCU如何移植,重在方法。
以下是通用接口,移植不需要修改的。需要修改的是SDIO的驱动部分,要把设备和驱动分开。
- #ifndef SDCARD_H
- #define SDCARD_H
-
- #include "gd32e50x.h"
- #include "gd32e50x_sdio.h"
- #include "gd32e50x_dma.h"
- #include <stddef.h>
- #include <stdio.h>
-
-
- #define SD_R1_OUT_OF_RANGE BIT(31) /* command's argument was out of the allowed range */
- #define SD_R1_ADDRESS_ERROR BIT(30) /* misaligned address which did not match the block length */
- #define SD_R1_BLOCK_LEN_ERROR BIT(29) /* transferred block length is not allowed */
- #define SD_R1_ERASE_SEQ_ERROR BIT(28) /* an error in the sequence of erase commands occurred */
- #define SD_R1_ERASE_PARAM BIT(27) /* an invalid selection of write-blocks for erase occurred */
- #define SD_R1_WP_VIOLATION BIT(26) /* the host attempts to write to a protected block or to the temporary or permanent write protected card */
- #define SD_R1_CARD_IS_LOCKED BIT(25) /* the card is locked by the host */
- #define SD_R1_LOCK_UNLOCK_FAILED BIT(24) /* a sequence or password error has been detected in lock/unlock card command */
- #define SD_R1_COM_CRC_ERROR BIT(23) /* CRC check of the previous command failed */
- #define SD_R1_ILLEGAL_COMMAND BIT(22) /* command not legal for the card state */
- #define SD_R1_CARD_ECC_FAILED BIT(21) /* card internal ECC was applied but failed to correct the data */
- #define SD_R1_CC_ERROR BIT(20) /* internal card controller error */
- #define SD_R1_GENERAL_UNKNOWN_ERROR BIT(19) /* a general or an unknown error occurred during the operation */
- #define SD_R1_CSD_OVERWRITE BIT(16) /* read only section of the CSD does not match or attempt to reverse the copy or permanent WP bits */
- #define SD_R1_WP_ERASE_SKIP BIT(15) /* partial address space was erased */
- #define SD_R1_CARD_ECC_DISABLED BIT(14) /* command has been executed without using the internal ECC */
- #define SD_R1_ERASE_RESET BIT(13) /* an erase sequence was cleared before executing */
- #define SD_R1_READY_FOR_DATA BIT(8) /* correspond to buffer empty signaling on the bus */
- #define SD_R1_APP_CMD BIT(5) /* card will expect ACMD */
- #define SD_R1_AKE_SEQ_ERROR BIT(3) /* error in the sequence of the authentication process */
- #define SD_R1_ERROR_BITS (uint32_t)0xFDF9E008 /* all the R1 error bits */
-
-
- #define SD_R6_COM_CRC_ERROR BIT(15) /* CRC check of the previous command failed */
- #define SD_R6_ILLEGAL_COMMAND BIT(14) /* command not legal for the card state */
- #define SD_R6_GENERAL_UNKNOWN_ERROR BIT(13) /* a general or an unknown error occurred during the operation */
-
-
- #define SD_CARDSTATE_IDLE ((uint8_t)0x00) /* card is in idle state */
- #define SD_CARDSTATE_READY ((uint8_t)0x01) /* card is in ready state */
- #define SD_CARDSTATE_IDENTIFICAT ((uint8_t)0x02) /* card is in identificat state */
- #define SD_CARDSTATE_STANDBY ((uint8_t)0x03) /* card is in standby state */
- #define SD_CARDSTATE_TRANSFER ((uint8_t)0x04) /* card is in transfer state */
- #define SD_CARDSTATE_DATA ((uint8_t)0x05) /* card is in data sending state */
- #define SD_CARDSTATE_RECEIVING ((uint8_t)0x06) /* card is in receiving state */
- #define SD_CARDSTATE_PROGRAMMING ((uint8_t)0x07) /* card is in programming state */
- #define SD_CARDSTATE_DISCONNECT ((uint8_t)0x08) /* card is in disconnect state */
- #define SD_CARDSTATE_LOCKED ((uint32_t)0x02000000) /* card is in locked state */
-
- #define SD_CHECK_PATTERN ((uint32_t)0x000001AA) /* check pattern for CMD8 */
- #define SD_VOLTAGE_WINDOW ((uint32_t)0x80100000) /* host 3.3V request in ACMD41 */
-
-
- #define SD_HIGH_CAPACITY ((uint32_t)0x40000000) /* high capacity SD memory card */
- #define SD_STD_CAPACITY ((uint32_t)0x00000000) /* standard capacity SD memory card */
-
-
- #define SD_BUS_WIDTH_4BIT ((uint32_t)0x00040000) /* 4-bit width bus mode */
- #define SD_BUS_WIDTH_1BIT ((uint32_t)0x00010000) /* 1-bit width bus mode */
-
-
- #define SD_MASK_0_7BITS ((uint32_t)0x000000FF) /* mask [7:0] bits */
- #define SD_MASK_8_15BITS ((uint32_t)0x0000FF00) /* mask [15:8] bits */
- #define SD_MASK_16_23BITS ((uint32_t)0x00FF0000) /* mask [23:16] bits */
- #define SD_MASK_24_31BITS ((uint32_t)0xFF000000) /* mask [31:24] bits */
-
- #define SDIO_FIFO_ADDR ((uint32_t)SDIO + 0x80U) /* address of SDIO_FIFO */
- #define SD_FIFOHALF_WORDS ((uint32_t)0x00000008) /* words of FIFO half full/empty */
- #define SD_FIFOHALF_BYTES ((uint32_t)0x00000020) /* bytes of FIFO half full/empty */
-
- #define SD_DATATIMEOUT ((uint32_t)0xFFFFFFFF) /* DSM data timeout */
- #define SD_MAX_VOLT_VALIDATION ((uint32_t)0x0000FFFF) /* the maximum times of voltage validation */
- #define SD_MAX_DATA_LENGTH ((uint32_t)0x01FFFFFF) /* the maximum length of data */
- #define SD_ALLZERO ((uint32_t)0x00000000) /* all zero */
- #define SD_RCA_SHIFT ((uint8_t)0x10) /* RCA shift bits */
- #define SD_CLK_DIV_INIT ((uint16_t)0x01C0) /* SD clock division in initilization phase */
- #define SD_CLK_DIV_TRANS ((uint16_t)0x0011) /* SD clock division in transmission phase */
-
- #define SDIO_MASK_INTC_FLAGS ((uint32_t)0x00C007FF) /* mask flags of SDIO_INTC */
-
-
- #define SD_CMD_GO_IDLE_STATE ((uint8_t)0) /* CMD0, GO_IDLE_STATE */
- #define SD_CMD_ALL_SEND_CID ((uint8_t)2) /* CMD2, ALL_SEND_CID */
- #define SD_CMD_SEND_RELATIVE_ADDR ((uint8_t)3) /* CMD3, SEND_RELATIVE_ADDR */
- #define SD_CMD_SET_DSR ((uint8_t)4) /* CMD4, SET_DSR */
- #define SD_CMD_SWITCH_FUNC ((uint8_t)6) /* CMD6, SWITCH_FUNC */
- #define SD_CMD_SELECT_DESELECT_CARD ((uint8_t)7) /* CMD7, SELECT_DESELECT_CARD */
- #define SD_CMD_SEND_IF_COND ((uint8_t)8) /* CMD8, SEND_IF_COND */
- #define SD_CMD_SEND_CSD ((uint8_t)9) /* CMD9, SEND_CSD */
- #define SD_CMD_SEND_CID ((uint8_t)10) /* CMD10, SEND_CID */
- #define SD_CMD_STOP_TRANSMISSION ((uint8_t)12) /* CMD12, STOP_TRANSMISSION */
- #define SD_CMD_SEND_STATUS ((uint8_t)13) /* CMD13, SEND_STATUS */
- #define SD_CMD_GO_INACTIVE_STATE ((uint8_t)15) /* CMD15, GO_INACTIVE_STATE */
- #define SD_CMD_SET_BLOCKLEN ((uint8_t)16) /* CMD16, SET_BLOCKLEN */
- #define SD_CMD_READ_SINGLE_BLOCK ((uint8_t)17) /* CMD17, READ_SINGLE_BLOCK */
- #define SD_CMD_READ_MULTIPLE_BLOCK ((uint8_t)18) /* CMD18, READ_MULTIPLE_BLOCK */
- #define SD_CMD_WRITE_BLOCK ((uint8_t)24) /* CMD24, WRITE_BLOCK */
- #define SD_CMD_WRITE_MULTIPLE_BLOCK ((uint8_t)25) /* CMD25, WRITE_MULTIPLE_BLOCK */
- #define SD_CMD_PROG_CSD ((uint8_t)27) /* CMD27, PROG_CSD */
- #define SD_CMD_SET_WRITE_PROT ((uint8_t)28) /* CMD28, SET_WRITE_PROT */
- #define SD_CMD_CLR_WRITE_PROT ((uint8_t)29) /* CMD29, CLR_WRITE_PROT */
- #define SD_CMD_SEND_WRITE_PROT ((uint8_t)30) /* CMD30, SEND_WRITE_PROT */
- #define SD_CMD_ERASE_WR_BLK_START ((uint8_t)32) /* CMD32, ERASE_WR_BLK_START */
- #define SD_CMD_ERASE_WR_BLK_END ((uint8_t)33) /* CMD33, ERASE_WR_BLK_END */
- #define SD_CMD_ERASE ((uint8_t)38) /* CMD38, ERASE */
- #define SD_CMD_LOCK_UNLOCK ((uint8_t)42) /* CMD42, LOCK_UNLOCK */
- #define SD_CMD_APP_CMD ((uint8_t)55) /* CMD55, APP_CMD */
- #define SD_CMD_GEN_CMD ((uint8_t)56) /* CMD56, GEN_CMD */
-
-
- #define SD_APPCMD_SET_BUS_WIDTH ((uint8_t)6) /* ACMD6, SET_BUS_WIDTH */
- #define SD_APPCMD_SD_STATUS ((uint8_t)13) /* ACMD13, SD_STATUS */
- #define SD_APPCMD_SEND_NUM_WR_BLOCKS ((uint8_t)22) /* ACMD22, SEND_NUM_WR_BLOCKS */
- #define SD_APPCMD_SET_WR_BLK_ERASE_COUNT ((uint8_t)23) /* ACMD23, SET_WR_BLK_ERASE_COUNT */
- #define SD_APPCMD_SD_SEND_OP_COND ((uint8_t)41) /* ACMD41, SD_SEND_OP_COND */
- #define SD_APPCMD_SET_CLR_CARD_DETECT ((uint8_t)42) /* ACMD42, SET_CLR_CARD_DETECT */
- #define SD_APPCMD_SEND_SCR ((uint8_t)51) /* ACMD51, SEND_SCR */
-
-
- #define SD_CCC_SWITCH BIT(10) /* class 10 */
- #define SD_CCC_IO_MODE BIT(9) /* class 9 */
- #define SD_CCC_APPLICATION_SPECIFIC BIT(8) /* class 8 */
- #define SD_CCC_LOCK_CARD BIT(7) /* class 7 */
- #define SD_CCC_WRITE_PROTECTION BIT(6) /* class 6 */
- #define SD_CCC_ERASE BIT(5) /* class 5 */
- #define SD_CCC_BLOCK_WRITE BIT(4) /* class 4 */
- #define SD_CCC_BLOCK_READ BIT(2) /* class 2 */
- #define SD_CCC_BASIC BIT(0) /* class 0 */
-
-
- #define SD_DMA_MODE ((uint32_t)0x00000000) /* DMA mode */
- #define SD_POLLING_MODE ((uint32_t)0x00000001) /* polling mode */
-
-
- #define SD_LOCK ((uint8_t)0x05) /* lock the SD card */
- #define SD_UNLOCK ((uint8_t)0x02) /* unlock the SD card */
-
-
- typedef enum
- {
- SDIO_STD_CAPACITY_SD_CARD_V1_1 = 0,
- SDIO_STD_CAPACITY_SD_CARD_V2_0,
- SDIO_HIGH_CAPACITY_SD_CARD,
- SDIO_SECURE_DIGITAL_IO_CARD,
- SDIO_SECURE_DIGITAL_IO_COMBO_CARD,
- SDIO_MULTIMEDIA_CARD,
- SDIO_HIGH_CAPACITY_MULTIMEDIA_CARD,
- SDIO_HIGH_SPEED_MULTIMEDIA_CARD
- }sdio_card_type_enum;
-
-
- typedef struct
- {
- __IO uint8_t mid;
- __IO uint16_t oid;
- __IO uint32_t pnm0;
- __IO uint8_t pnm1;
- __IO uint8_t prv;
- __IO uint32_t psn;
- __IO uint16_t mdt;
- __IO uint8_t cid_crc;
- }sd_cid_struct;
-
-
- typedef struct
- {
- __IO uint8_t csd_struct;
- __IO uint8_t taac;
- __IO uint8_t nsac;
- __IO uint8_t tran_speed;
- __IO uint16_t ccc;
- __IO uint8_t read_bl_len;
- __IO uint8_t read_bl_partial;
- __IO uint8_t write_blk_misalign;
- __IO uint8_t read_blk_misalign;
- __IO uint8_t dsp_imp;
- __IO uint32_t c_size;
- __IO uint8_t vdd_r_curr_min;
- __IO uint8_t vdd_r_curr_max;
- __IO uint8_t vdd_w_curr_min;
- __IO uint8_t vdd_w_curr_max;
- __IO uint8_t c_size_mult;
- __IO uint8_t erase_blk_en;
- __IO uint8_t sector_size;
- __IO uint8_t wp_grp_size;
- __IO uint8_t wp_grp_enable;
- __IO uint8_t r2w_factor;
- __IO uint8_t write_bl_len;
- __IO uint8_t write_bl_partial;
- __IO uint8_t file_format_grp;
- __IO uint8_t copy_flag;
- __IO uint8_t perm_write_protect;
- __IO uint8_t tmp_write_protect;
- __IO uint8_t file_format;
- __IO uint8_t csd_crc;
- }sd_csd_struct;
-
-
- typedef struct
- {
- sd_cid_struct card_cid;
- sd_csd_struct card_csd;
- sdio_card_type_enum card_type;
- uint32_t card_capacity;
- uint32_t card_blocksize;
- uint16_t card_rca;
- }sd_card_info_struct;
-
-
- typedef enum
- {
- SD_OUT_OF_RANGE = 0,
- SD_ADDRESS_ERROR,
- SD_BLOCK_LEN_ERROR,
- SD_ERASE_SEQ_ERROR,
- SD_ERASE_PARAM,
- SD_WP_VIOLATION,
- SD_LOCK_UNLOCK_FAILED,
- SD_COM_CRC_ERROR,
- SD_ILLEGAL_COMMAND,
- SD_CARD_ECC_FAILED,
- SD_CC_ERROR,
- SD_GENERAL_UNKNOWN_ERROR,
- SD_CSD_OVERWRITE,
- SD_WP_ERASE_SKIP,
- SD_CARD_ECC_DISABLED,
- SD_ERASE_RESET,
- SD_AKE_SEQ_ERROR,
-
- SD_CMD_CRC_ERROR,
- SD_DATA_CRC_ERROR,
- SD_CMD_RESP_TIMEOUT,
- SD_DATA_TIMEOUT,
- SD_TX_UNDERRUN_ERROR,
- SD_RX_OVERRUN_ERROR,
- SD_START_BIT_ERROR,
-
- SD_VOLTRANGE_INVALID,
- SD_PARAMETER_INVALID,
- SD_OPERATION_IMPROPER,
- SD_FUNCTION_UNSUPPORTED,
- SD_ERROR,
- SD_OK
- }sd_error_enum;
-
- typedef enum
- {
- SD_NO_TRANSFER = 0,
- SD_TRANSFER_IN_PROGRESS
- }sd_transfer_state_enum;
-
- extern uint32_t sd_scr[2];
-
-
-
-
- sd_error_enum sd_card_init(void);
-
- sd_error_enum sd_power_on(void);
-
- sd_error_enum sd_power_off(void);
-
-
- sd_error_enum sd_bus_mode_config(uint32_t busmode);
-
- sd_error_enum sd_transfer_mode_config(uint32_t txmode);
-
-
- sd_error_enum sd_block_read(uint32_t *preadbuffer, uint32_t readaddr, uint16_t blocksize);
-
- sd_error_enum sd_multiblocks_read(uint32_t *preadbuffer, uint32_t readaddr, uint16_t blocksize, uint32_t blocksnumber);
-
- sd_error_enum sd_block_write(uint32_t *pwritebuffer, uint32_t writeaddr, uint16_t blocksize);
-
- sd_error_enum sd_multiblocks_write(uint32_t *pwritebuffer, uint32_t writeaddr, uint16_t blocksize, uint32_t blocksnumber);
-
- sd_error_enum sd_erase(uint32_t startaddr, uint32_t endaddr);
-
- sd_error_enum sd_interrupts_process(void);
-
-
- sd_error_enum sd_card_select_deselect(uint16_t cardrca);
-
- sd_error_enum sd_cardstatus_get(uint32_t *pcardstatus);
-
- sd_error_enum sd_sdstatus_get(uint32_t *psdstatus);
-
- sd_error_enum sd_transfer_stop(void);
-
- sd_error_enum sd_lock_unlock(uint8_t lockstate);
-
-
- sd_transfer_state_enum sd_transfer_state_get(void);
-
- uint32_t sd_card_capacity_get(void);
-
- sd_error_enum sd_card_information_get(sd_card_info_struct *pcardinfo);
-
- sd_error_enum BSP_SDCard_init(void);
- void BSP_SDCard_info_get(void);
- void BSP_SDCard_test(void);
-
2. 这里利用官方给的demo,自己再添砖加瓦,做了些修改,增加一个初始化接口。
- sd_error_enum BSP_SDCard_init(void)
- {
- int i = 0;
- sd_error_enum sd_error;
-
- for(i=0; i<10; i++)
- {
- sd_error = SDcard_init();
- if(sd_error == SD_OK)
- {
- break;
- }
- }
- if(sd_error == SD_OK)
- {
- printf("\r\n Card init success![%d]\r\n",i);
- }
- else
- {
- printf("\r\n Card init failed!\r\n");
- }
- return sd_error;
- }
这里倒是很奇怪的,有时候一次初始化不能成功,需要要循环几次~ 不知道有没有小伙伴遇到同样的。
这些驱动整理进去,就可以看到效果了!
代码见附件:
|