1 /* 2 * Copyright (c) 2006-2022, RT-Thread Development Team 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 * 6 * Change Logs: 7 * Date Author Notes 8 * 2022-07-20 jiezhi320 the first version 9 */ 10 11 #ifndef __SDCARD_H_ 12 #define __SDCARD_H_ 13 14 #include <board.h> 15 16 #if defined SOC_SERIES_GD32F10x 17 #include "gd32f10x_sdio.h" 18 #include "gd32f10x_dma.h" 19 #elif defined SOC_SERIES_GD32F20x 20 #include "gd32f20x_sdio.h" 21 #include "gd32f20x_dma.h" 22 #elif defined SOC_SERIES_GD32F30x 23 #include "gd32f30x_sdio.h" 24 #include "gd32f30x_dma.h" 25 #elif defined SOC_SERIES_GD32F4xx 26 #include "gd32f4xx_sdio.h" 27 #include "gd32f4xx_dma.h" 28 #endif 29 30 #define SDIO_PERI_CLOCK RCU_SDIO 31 #define SDIO_GPIO_CLK RCU_GPIOC 32 #define SDIO_GPIO_CMD RCU_GPIOD 33 #define SDIO_GPIO_D0 RCU_GPIOC 34 #define SDIO_GPIO_D1 RCU_GPIOC 35 #define SDIO_GPIO_D2 RCU_GPIOC 36 #define SDIO_GPIO_D3 RCU_GPIOC 37 38 #define SDIO_CLK_PORT GPIOC 39 #define SDIO_CLK_PIN GPIO_PIN_12 40 #define SDIO_CMD_PORT GPIOD 41 #define SDIO_CMD_PIN GPIO_PIN_2 42 #define SDIO_D0_PORT GPIOC 43 #define SDIO_D0_PIN GPIO_PIN_8 44 #define SDIO_D1_PORT GPIOC 45 #define SDIO_D1_PIN GPIO_PIN_9 46 #define SDIO_D2_PORT GPIOC 47 #define SDIO_D2_PIN GPIO_PIN_10 48 #define SDIO_D3_PORT GPIOC 49 #define SDIO_D3_PIN GPIO_PIN_11 50 51 #define SDIO_DMA DMA1 52 #define SDIO_DMA_CLOCK RCU_DMA1 53 #define SDIO_DMA_CHANNEL DMA_CH3 54 #define SDIO_DMA_IRQ DMA1_Channel3_IRQn 55 #define SDIO_DMA_IRQ_HANDLER DMA1_Channel3_IRQHandler 56 57 58 /* SD memory card bus commands index */ 59 #define SD_CMD_GO_IDLE_STATE ((uint8_t)0) /* CMD0, GO_IDLE_STATE */ 60 #define SD_CMD_ALL_SEND_CID ((uint8_t)2) /* CMD2, ALL_SEND_CID */ 61 #define SD_CMD_SEND_RELATIVE_ADDR ((uint8_t)3) /* CMD3, SEND_RELATIVE_ADDR */ 62 #define SD_CMD_SET_DSR ((uint8_t)4) /* CMD4, SET_DSR */ 63 #define SD_CMD_SWITCH_FUNC ((uint8_t)6) /* CMD6, SWITCH_FUNC */ 64 #define SD_CMD_SELECT_DESELECT_CARD ((uint8_t)7) /* CMD7, SELECT_DESELECT_CARD */ 65 #define SD_CMD_SEND_IF_COND ((uint8_t)8) /* CMD8, SEND_IF_COND */ 66 #define SD_CMD_SEND_CSD ((uint8_t)9) /* CMD9, SEND_CSD */ 67 #define SD_CMD_SEND_CID ((uint8_t)10) /* CMD10, SEND_CID */ 68 #define SD_CMD_STOP_TRANSMISSION ((uint8_t)12) /* CMD12, STOP_TRANSMISSION */ 69 #define SD_CMD_SEND_STATUS ((uint8_t)13) /* CMD13, SEND_STATUS */ 70 #define SD_CMD_GO_INACTIVE_STATE ((uint8_t)15) /* CMD15, GO_INACTIVE_STATE */ 71 #define SD_CMD_SET_BLOCKLEN ((uint8_t)16) /* CMD16, SET_BLOCKLEN */ 72 #define SD_CMD_READ_SINGLE_BLOCK ((uint8_t)17) /* CMD17, READ_SINGLE_BLOCK */ 73 #define SD_CMD_READ_MULTIPLE_BLOCK ((uint8_t)18) /* CMD18, READ_MULTIPLE_BLOCK */ 74 #define SD_CMD_WRITE_BLOCK ((uint8_t)24) /* CMD24, WRITE_BLOCK */ 75 #define SD_CMD_WRITE_MULTIPLE_BLOCK ((uint8_t)25) /* CMD25, WRITE_MULTIPLE_BLOCK */ 76 #define SD_CMD_PROG_CSD ((uint8_t)27) /* CMD27, PROG_CSD */ 77 #define SD_CMD_SET_WRITE_PROT ((uint8_t)28) /* CMD28, SET_WRITE_PROT */ 78 #define SD_CMD_CLR_WRITE_PROT ((uint8_t)29) /* CMD29, CLR_WRITE_PROT */ 79 #define SD_CMD_SEND_WRITE_PROT ((uint8_t)30) /* CMD30, SEND_WRITE_PROT */ 80 #define SD_CMD_ERASE_WR_BLK_START ((uint8_t)32) /* CMD32, ERASE_WR_BLK_START */ 81 #define SD_CMD_ERASE_WR_BLK_END ((uint8_t)33) /* CMD33, ERASE_WR_BLK_END */ 82 #define SD_CMD_ERASE ((uint8_t)38) /* CMD38, ERASE */ 83 #define SD_CMD_LOCK_UNLOCK ((uint8_t)42) /* CMD42, LOCK_UNLOCK */ 84 #define SD_CMD_APP_CMD ((uint8_t)55) /* CMD55, APP_CMD */ 85 #define SD_CMD_GEN_CMD ((uint8_t)56) /* CMD56, GEN_CMD */ 86 87 /* SD memory card application specific commands index */ 88 #define SD_APPCMD_SET_BUS_WIDTH ((uint8_t)6) /* ACMD6, SET_BUS_WIDTH */ 89 #define SD_APPCMD_SD_STATUS ((uint8_t)13) /* ACMD13, SD_STATUS */ 90 #define SD_APPCMD_SEND_NUM_WR_BLOCKS ((uint8_t)22) /* ACMD22, SEND_NUM_WR_BLOCKS */ 91 #define SD_APPCMD_SET_WR_BLK_ERASE_COUNT ((uint8_t)23) /* ACMD23, SET_WR_BLK_ERASE_COUNT */ 92 #define SD_APPCMD_SD_SEND_OP_COND ((uint8_t)41) /* ACMD41, SD_SEND_OP_COND */ 93 #define SD_APPCMD_SET_CLR_CARD_DETECT ((uint8_t)42) /* ACMD42, SET_CLR_CARD_DETECT */ 94 #define SD_APPCMD_SEND_SCR ((uint8_t)51) /* ACMD51, SEND_SCR */ 95 96 /* card command class */ 97 #define SD_CCC_SWITCH BIT(10) /* class 10 */ 98 #define SD_CCC_IO_MODE BIT(9) /* class 9 */ 99 #define SD_CCC_APPLICATION_SPECIFIC BIT(8) /* class 8 */ 100 #define SD_CCC_LOCK_CARD BIT(7) /* class 7 */ 101 #define SD_CCC_WRITE_PROTECTION BIT(6) /* class 6 */ 102 #define SD_CCC_ERASE BIT(5) /* class 5 */ 103 #define SD_CCC_BLOCK_WRITE BIT(4) /* class 4 */ 104 #define SD_CCC_BLOCK_READ BIT(2) /* class 2 */ 105 #define SD_CCC_BASIC BIT(0) /* class 0 */ 106 107 /* SD card data transmission mode */ 108 #define SD_DMA_MODE ((uint32_t)0x00000000) /* DMA mode */ 109 #define SD_POLLING_MODE ((uint32_t)0x00000001) /* polling mode */ 110 111 /* lock unlock status */ 112 #define SD_LOCK ((uint8_t)0x05) /* lock the SD card */ 113 #define SD_UNLOCK ((uint8_t)0x02) /* unlock the SD card */ 114 115 /* supported memory cards types */ 116 typedef enum 117 { 118 SDIO_STD_CAPACITY_SD_CARD_V1_1 = 0, /* standard capacity SD card version 1.1 */ 119 SDIO_STD_CAPACITY_SD_CARD_V2_0, /* standard capacity SD card version 2.0 */ 120 SDIO_HIGH_CAPACITY_SD_CARD, /* high capacity SD card */ 121 SDIO_SECURE_DIGITAL_IO_CARD, /* secure digital IO card */ 122 SDIO_SECURE_DIGITAL_IO_COMBO_CARD, /* secure digital IO combo card */ 123 SDIO_MULTIMEDIA_CARD, /* multimedia card */ 124 SDIO_HIGH_CAPACITY_MULTIMEDIA_CARD, /* high capacity multimedia card */ 125 SDIO_HIGH_SPEED_MULTIMEDIA_CARD /* high speed multimedia card */ 126 }sdio_card_type_enum; 127 128 /* card identification (CID) register */ 129 typedef struct 130 { 131 __IO uint8_t mid; /* manufacturer ID */ 132 __IO uint16_t oid; /* OEM/application ID */ 133 __IO uint32_t pnm0; /* product name */ 134 __IO uint8_t pnm1; /* product name */ 135 __IO uint8_t prv; /* product revision */ 136 __IO uint32_t psn; /* product serial number */ 137 __IO uint16_t mdt; /* manufacturing date */ 138 __IO uint8_t cid_crc; /* CID CRC7 checksum */ 139 }sd_cid_struct; 140 141 /* CSD register (CSD version 1.0 and 2.0) */ 142 typedef struct 143 { 144 __IO uint8_t csd_struct; /* CSD struct */ 145 __IO uint8_t taac; /* data read access-time */ 146 __IO uint8_t nsac; /* data read access-time in CLK cycles */ 147 __IO uint8_t tran_speed; /* max. data transfer rate */ 148 __IO uint16_t ccc; /* card command classes */ 149 __IO uint8_t read_bl_len; /* max. read data block length */ 150 __IO uint8_t read_bl_partial; /* partial blocks for read allowed */ 151 __IO uint8_t write_blk_misalign; /* write block misalignment */ 152 __IO uint8_t read_blk_misalign; /* read block misalignment */ 153 __IO uint8_t dsp_imp; /* DSR implemented */ 154 __IO uint32_t c_size; /* device size, 12 bits in CSD version 1.0, 22 bits in CSD version 2.0 */ 155 __IO uint8_t vdd_r_curr_min; /* max. read current @VDD min, CSD version 1.0 */ 156 __IO uint8_t vdd_r_curr_max; /* max. read current @VDD max, CSD version 1.0 */ 157 __IO uint8_t vdd_w_curr_min; /* max. write current @VDD min, CSD version 1.0 */ 158 __IO uint8_t vdd_w_curr_max; /* max. write current @VDD max, CSD version 1.0 */ 159 __IO uint8_t c_size_mult; /* device size multiplier, CSD version 1.0 */ 160 __IO uint8_t erase_blk_en; /* erase single block enable */ 161 __IO uint8_t sector_size; /* erase sector size */ 162 __IO uint8_t wp_grp_size; /* write protect group size */ 163 __IO uint8_t wp_grp_enable; /* write protect group enable */ 164 __IO uint8_t r2w_factor; /* write speed factor */ 165 __IO uint8_t write_bl_len; /* max. write data block length */ 166 __IO uint8_t write_bl_partial; /* partial blocks for write allowed */ 167 __IO uint8_t file_format_grp; /* file format group */ 168 __IO uint8_t copy_flag; /* copy flag (OTP) */ 169 __IO uint8_t perm_write_protect; /* permanent write protection */ 170 __IO uint8_t tmp_write_protect; /* temporary write protection */ 171 __IO uint8_t file_format; /* file format */ 172 __IO uint8_t csd_crc; /* CSD CRC checksum */ 173 }sd_csd_struct; 174 175 /* information of card */ 176 typedef struct 177 { 178 sd_cid_struct card_cid; /* CID register */ 179 sd_csd_struct card_csd; /* CSD register */ 180 sdio_card_type_enum card_type; /* card tpye */ 181 uint32_t card_capacity; /* card capacity */ 182 uint32_t card_blocksize; /* card block size */ 183 uint16_t card_rca; /* card relative card address */ 184 }sd_card_info_struct; 185 186 /* SD error flags */ 187 typedef enum 188 { 189 SD_OUT_OF_RANGE = 0, /* command's argument was out of range */ 190 SD_ADDRESS_ERROR, /* misaligned address which did not match the block length */ 191 SD_BLOCK_LEN_ERROR, /* transferred block length is not allowed for the card or the number of transferred bytes does not match the block length */ 192 SD_ERASE_SEQ_ERROR, /* an error in the sequence of erase command occurs */ 193 SD_ERASE_PARAM, /* an invalid selection of write-blocks for erase occurred */ 194 SD_WP_VIOLATION, /* attempt to program a write protect block or permanent write protected card */ 195 SD_LOCK_UNLOCK_FAILED, /* sequence or password error has been detected in lock/unlock card command */ 196 SD_COM_CRC_ERROR, /* CRC check of the previous command failed */ 197 SD_ILLEGAL_COMMAND, /* command not legal for the card state */ 198 SD_CARD_ECC_FAILED, /* card internal ECC was applied but failed to correct the data */ 199 SD_CC_ERROR, /* internal card controller error */ 200 SD_GENERAL_UNKNOWN_ERROR, /* general or unknown error occurred during the operation */ 201 SD_CSD_OVERWRITE, /* read only section of the CSD does not match the card content or an attempt to reverse the copy or permanent WP bits was made */ 202 SD_WP_ERASE_SKIP, /* only partial address space was erased or the temporary or permanent write protected card was erased */ 203 SD_CARD_ECC_DISABLED, /* command has been executed without using internal ECC */ 204 SD_ERASE_RESET, /* erase sequence was cleared before executing because an out of erase sequence command was received */ 205 SD_AKE_SEQ_ERROR, /* error in the sequence of the authentication process */ 206 207 SD_CMD_CRC_ERROR, /* command response received (CRC check failed) */ 208 SD_DATA_CRC_ERROR, /* data block sent/received (CRC check failed) */ 209 SD_CMD_RESP_TIMEOUT, /* command response timeout */ 210 SD_DATA_TIMEOUT, /* data timeout */ 211 SD_TX_UNDERRUN_ERROR, /* transmit FIFO underrun error occurs */ 212 SD_RX_OVERRUN_ERROR, /* received FIFO overrun error occurs */ 213 SD_START_BIT_ERROR, /* start bit error in the bus */ 214 215 SD_VOLTRANGE_INVALID, /* the voltage range is invalid */ 216 SD_PARAMETER_INVALID, /* the parameter is invalid */ 217 SD_OPERATION_IMPROPER, /* the operation is improper */ 218 SD_FUNCTION_UNSUPPORTED, /* the function is unsupported */ 219 SD_ERROR, /* an error occurred */ 220 SD_OK /* no error occurred */ 221 }sd_error_enum; 222 223 typedef enum 224 { 225 SD_NO_TRANSFER = 0, /* no data transfer is acting */ 226 SD_TRANSFER_IN_PROGRESS /* data transfer is in progress */ 227 }sd_transfer_state_enum; 228 229 230 /* function declarations */ 231 /* initialize the SD card and make it in standby state */ 232 sd_error_enum sd_init(void); 233 /* initialize the card and get CID and CSD of the card */ 234 sd_error_enum sd_card_init(void); 235 /* configure the clock and the work voltage, and get the card type */ 236 sd_error_enum sd_power_on(void); 237 /* close the power of SDIO */ 238 sd_error_enum sd_power_off(void); 239 240 /* configure the bus mode */ 241 sd_error_enum sd_bus_mode_config(uint32_t busmode); 242 /* configure the mode of transmission */ 243 sd_error_enum sd_transfer_mode_config(uint32_t txmode); 244 245 /* read a block data into a buffer from the specified address of a card */ 246 sd_error_enum sd_block_read(uint32_t *preadbuffer, uint32_t readaddr, uint16_t blocksize); 247 /* read multiple blocks data into a buffer from the specified address of a card */ 248 sd_error_enum sd_multiblocks_read(uint32_t *preadbuffer, uint32_t readaddr, uint16_t blocksize, uint32_t blocksnumber); 249 /* write a block data to the specified address of a card */ 250 sd_error_enum sd_block_write(uint32_t *pwritebuffer, uint32_t writeaddr, uint16_t blocksize); 251 /* write multiple blocks data to the specified address of a card */ 252 sd_error_enum sd_multiblocks_write(uint32_t *pwritebuffer, uint32_t writeaddr, uint16_t blocksize, uint32_t blocksnumber); 253 /* erase a continuous area of a card */ 254 sd_error_enum sd_erase(uint32_t startaddr, uint32_t endaddr); 255 /* process all the interrupts which the corresponding flags are set */ 256 sd_error_enum sd_interrupts_process(void); 257 258 /* select or deselect a card */ 259 sd_error_enum sd_card_select_deselect(uint16_t cardrca); 260 /* get the card status whose response format R1 contains a 32-bit field */ 261 sd_error_enum sd_cardstatus_get(uint32_t *pcardstatus); 262 /* get the SD status, the size of the SD status is one data block of 512 bit */ 263 sd_error_enum sd_sdstatus_get(uint32_t *psdstatus); 264 /* stop an ongoing data transfer */ 265 sd_error_enum sd_transfer_stop(void); 266 /* lock or unlock a card */ 267 sd_error_enum sd_lock_unlock(uint8_t lockstate); 268 269 /* get the data transfer state */ 270 sd_transfer_state_enum sd_transfer_state_get(void); 271 /* get SD card capacity(KB) */ 272 uint32_t sd_card_capacity_get(void); 273 /* get the detailed information of the SD card based on received CID and CSD */ 274 sd_error_enum sd_card_information_get(sd_card_info_struct *pcardinfo); 275 276 #endif /* __SDCARD_H_ */ 277 278 279