sdcard.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*!
  2. \file sdcard.h
  3. \brief the header file of SD card driver
  4. \version 2017-02-10, V1.0.0, firmware for GD32F30x
  5. \version 2018-10-10, V1.1.0, firmware for GD32F30x
  6. \version 2018-12-25, V2.0.0, firmware for GD32F30x
  7. \version 2020-09-30, V2.1.0, firmware for GD32F30x
  8. */
  9. /*
  10. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  11. Redistribution and use in source and binary forms, with or without modification,
  12. are permitted provided that the following conditions are met:
  13. 1. Redistributions of source code must retain the above copyright notice, this
  14. list of conditions and the following disclaimer.
  15. 2. Redistributions in binary form must reproduce the above copyright notice,
  16. this list of conditions and the following disclaimer in the documentation
  17. and/or other materials provided with the distribution.
  18. 3. Neither the name of the copyright holder nor the names of its contributors
  19. may be used to endorse or promote products derived from this software without
  20. specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  25. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. OF SUCH DAMAGE.
  31. */
  32. #ifndef SDCARD_H
  33. #define SDCARD_H
  34. #include "gd32f30x.h"
  35. /* SD memory card bus commands index */
  36. #define SD_CMD_GO_IDLE_STATE ((uint8_t)0) /* CMD0, GO_IDLE_STATE */
  37. #define SD_CMD_ALL_SEND_CID ((uint8_t)2) /* CMD2, ALL_SEND_CID */
  38. #define SD_CMD_SEND_RELATIVE_ADDR ((uint8_t)3) /* CMD3, SEND_RELATIVE_ADDR */
  39. #define SD_CMD_SET_DSR ((uint8_t)4) /* CMD4, SET_DSR */
  40. #define SD_CMD_SWITCH_FUNC ((uint8_t)6) /* CMD6, SWITCH_FUNC */
  41. #define SD_CMD_SELECT_DESELECT_CARD ((uint8_t)7) /* CMD7, SELECT_DESELECT_CARD */
  42. #define SD_CMD_SEND_IF_COND ((uint8_t)8) /* CMD8, SEND_IF_COND */
  43. #define SD_CMD_SEND_CSD ((uint8_t)9) /* CMD9, SEND_CSD */
  44. #define SD_CMD_SEND_CID ((uint8_t)10) /* CMD10, SEND_CID */
  45. #define SD_CMD_STOP_TRANSMISSION ((uint8_t)12) /* CMD12, STOP_TRANSMISSION */
  46. #define SD_CMD_SEND_STATUS ((uint8_t)13) /* CMD13, SEND_STATUS */
  47. #define SD_CMD_GO_INACTIVE_STATE ((uint8_t)15) /* CMD15, GO_INACTIVE_STATE */
  48. #define SD_CMD_SET_BLOCKLEN ((uint8_t)16) /* CMD16, SET_BLOCKLEN */
  49. #define SD_CMD_READ_SINGLE_BLOCK ((uint8_t)17) /* CMD17, READ_SINGLE_BLOCK */
  50. #define SD_CMD_READ_MULTIPLE_BLOCK ((uint8_t)18) /* CMD18, READ_MULTIPLE_BLOCK */
  51. #define SD_CMD_WRITE_BLOCK ((uint8_t)24) /* CMD24, WRITE_BLOCK */
  52. #define SD_CMD_WRITE_MULTIPLE_BLOCK ((uint8_t)25) /* CMD25, WRITE_MULTIPLE_BLOCK */
  53. #define SD_CMD_PROG_CSD ((uint8_t)27) /* CMD27, PROG_CSD */
  54. #define SD_CMD_SET_WRITE_PROT ((uint8_t)28) /* CMD28, SET_WRITE_PROT */
  55. #define SD_CMD_CLR_WRITE_PROT ((uint8_t)29) /* CMD29, CLR_WRITE_PROT */
  56. #define SD_CMD_SEND_WRITE_PROT ((uint8_t)30) /* CMD30, SEND_WRITE_PROT */
  57. #define SD_CMD_ERASE_WR_BLK_START ((uint8_t)32) /* CMD32, ERASE_WR_BLK_START */
  58. #define SD_CMD_ERASE_WR_BLK_END ((uint8_t)33) /* CMD33, ERASE_WR_BLK_END */
  59. #define SD_CMD_ERASE ((uint8_t)38) /* CMD38, ERASE */
  60. #define SD_CMD_LOCK_UNLOCK ((uint8_t)42) /* CMD42, LOCK_UNLOCK */
  61. #define SD_CMD_APP_CMD ((uint8_t)55) /* CMD55, APP_CMD */
  62. #define SD_CMD_GEN_CMD ((uint8_t)56) /* CMD56, GEN_CMD */
  63. /* SD memory card application specific commands index */
  64. #define SD_APPCMD_SET_BUS_WIDTH ((uint8_t)6) /* ACMD6, SET_BUS_WIDTH */
  65. #define SD_APPCMD_SD_STATUS ((uint8_t)13) /* ACMD13, SD_STATUS */
  66. #define SD_APPCMD_SEND_NUM_WR_BLOCKS ((uint8_t)22) /* ACMD22, SEND_NUM_WR_BLOCKS */
  67. #define SD_APPCMD_SET_WR_BLK_ERASE_COUNT ((uint8_t)23) /* ACMD23, SET_WR_BLK_ERASE_COUNT */
  68. #define SD_APPCMD_SD_SEND_OP_COND ((uint8_t)41) /* ACMD41, SD_SEND_OP_COND */
  69. #define SD_APPCMD_SET_CLR_CARD_DETECT ((uint8_t)42) /* ACMD42, SET_CLR_CARD_DETECT */
  70. #define SD_APPCMD_SEND_SCR ((uint8_t)51) /* ACMD51, SEND_SCR */
  71. /* card command class */
  72. #define SD_CCC_SWITCH BIT(10) /* class 10 */
  73. #define SD_CCC_IO_MODE BIT(9) /* class 9 */
  74. #define SD_CCC_APPLICATION_SPECIFIC BIT(8) /* class 8 */
  75. #define SD_CCC_LOCK_CARD BIT(7) /* class 7 */
  76. #define SD_CCC_WRITE_PROTECTION BIT(6) /* class 6 */
  77. #define SD_CCC_ERASE BIT(5) /* class 5 */
  78. #define SD_CCC_BLOCK_WRITE BIT(4) /* class 4 */
  79. #define SD_CCC_BLOCK_READ BIT(2) /* class 2 */
  80. #define SD_CCC_BASIC BIT(0) /* class 0 */
  81. /* SD card data transmission mode */
  82. #define SD_DMA_MODE ((uint32_t)0x00000000) /* DMA mode */
  83. #define SD_POLLING_MODE ((uint32_t)0x00000001) /* polling mode */
  84. /* lock unlock status */
  85. #define SD_LOCK ((uint8_t)0x05) /* lock the SD card */
  86. #define SD_UNLOCK ((uint8_t)0x02) /* unlock the SD card */
  87. /* supported memory cards types */
  88. typedef enum
  89. {
  90. SDIO_STD_CAPACITY_SD_CARD_V1_1 = 0, /* standard capacity SD card version 1.1 */
  91. SDIO_STD_CAPACITY_SD_CARD_V2_0, /* standard capacity SD card version 2.0 */
  92. SDIO_HIGH_CAPACITY_SD_CARD, /* high capacity SD card */
  93. SDIO_SECURE_DIGITAL_IO_CARD, /* secure digital IO card */
  94. SDIO_SECURE_DIGITAL_IO_COMBO_CARD, /* secure digital IO combo card */
  95. SDIO_MULTIMEDIA_CARD, /* multimedia card */
  96. SDIO_HIGH_CAPACITY_MULTIMEDIA_CARD, /* high capacity multimedia card */
  97. SDIO_HIGH_SPEED_MULTIMEDIA_CARD /* high speed multimedia card */
  98. }sdio_card_type_enum;
  99. /* card identification (CID) register */
  100. typedef struct
  101. {
  102. __IO uint8_t mid; /* manufacturer ID */
  103. __IO uint16_t oid; /* OEM/application ID */
  104. __IO uint32_t pnm0; /* product name */
  105. __IO uint8_t pnm1; /* product name */
  106. __IO uint8_t prv; /* product revision */
  107. __IO uint32_t psn; /* product serial number */
  108. __IO uint16_t mdt; /* manufacturing date */
  109. __IO uint8_t cid_crc; /* CID CRC7 checksum */
  110. }sd_cid_struct;
  111. /* CSD register (CSD version 1.0 and 2.0) */
  112. typedef struct
  113. {
  114. __IO uint8_t csd_struct; /* CSD struct */
  115. __IO uint8_t taac; /* data read access-time */
  116. __IO uint8_t nsac; /* data read access-time in CLK cycles */
  117. __IO uint8_t tran_speed; /* max. data transfer rate */
  118. __IO uint16_t ccc; /* card command classes */
  119. __IO uint8_t read_bl_len; /* max. read data block length */
  120. __IO uint8_t read_bl_partial; /* partial blocks for read allowed */
  121. __IO uint8_t write_blk_misalign; /* write block misalignment */
  122. __IO uint8_t read_blk_misalign; /* read block misalignment */
  123. __IO uint8_t dsp_imp; /* DSR implemented */
  124. __IO uint32_t c_size; /* device size, 12 bits in CSD version 1.0, 22 bits in CSD version 2.0 */
  125. __IO uint8_t vdd_r_curr_min; /* max. read current @VDD min, CSD version 1.0 */
  126. __IO uint8_t vdd_r_curr_max; /* max. read current @VDD max, CSD version 1.0 */
  127. __IO uint8_t vdd_w_curr_min; /* max. write current @VDD min, CSD version 1.0 */
  128. __IO uint8_t vdd_w_curr_max; /* max. write current @VDD max, CSD version 1.0 */
  129. __IO uint8_t c_size_mult; /* device size multiplier, CSD version 1.0 */
  130. __IO uint8_t erase_blk_en; /* erase single block enable */
  131. __IO uint8_t sector_size; /* erase sector size */
  132. __IO uint8_t wp_grp_size; /* write protect group size */
  133. __IO uint8_t wp_grp_enable; /* write protect group enable */
  134. __IO uint8_t r2w_factor; /* write speed factor */
  135. __IO uint8_t write_bl_len; /* max. write data block length */
  136. __IO uint8_t write_bl_partial; /* partial blocks for write allowed */
  137. __IO uint8_t file_format_grp; /* file format group */
  138. __IO uint8_t copy_flag; /* copy flag (OTP) */
  139. __IO uint8_t perm_write_protect; /* permanent write protection */
  140. __IO uint8_t tmp_write_protect; /* temporary write protection */
  141. __IO uint8_t file_format; /* file format */
  142. __IO uint8_t csd_crc; /* CSD CRC checksum */
  143. }sd_csd_struct;
  144. /* information of card */
  145. typedef struct
  146. {
  147. sd_cid_struct card_cid; /* CID register */
  148. sd_csd_struct card_csd; /* CSD register */
  149. sdio_card_type_enum card_type; /* card tpye */
  150. uint32_t card_capacity; /* card capacity */
  151. uint32_t card_blocksize; /* card block size */
  152. uint16_t card_rca; /* card relative card address */
  153. }sd_card_info_struct;
  154. /* SD error flags */
  155. typedef enum
  156. {
  157. SD_OUT_OF_RANGE = 0, /* command's argument was out of range */
  158. SD_ADDRESS_ERROR, /* misaligned address which did not match the block length */
  159. 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 */
  160. SD_ERASE_SEQ_ERROR, /* an error in the sequence of erase command occurs */
  161. SD_ERASE_PARAM, /* an invalid selection of write-blocks for erase occurred */
  162. SD_WP_VIOLATION, /* attempt to program a write protect block or permanent write protected card */
  163. SD_LOCK_UNLOCK_FAILED, /* sequence or password error has been detected in lock/unlock card command */
  164. SD_COM_CRC_ERROR, /* CRC check of the previous command failed */
  165. SD_ILLEGAL_COMMAND, /* command not legal for the card state */
  166. SD_CARD_ECC_FAILED, /* card internal ECC was applied but failed to correct the data */
  167. SD_CC_ERROR, /* internal card controller error */
  168. SD_GENERAL_UNKNOWN_ERROR, /* general or unknown error occurred during the operation */
  169. 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 */
  170. SD_WP_ERASE_SKIP, /* only partial address space was erased or the temporary or permanent write protected card was erased */
  171. SD_CARD_ECC_DISABLED, /* command has been executed without using internal ECC */
  172. SD_ERASE_RESET, /* erase sequence was cleared before executing because an out of erase sequence command was received */
  173. SD_AKE_SEQ_ERROR, /* error in the sequence of the authentication process */
  174. SD_CMD_CRC_ERROR, /* command response received (CRC check failed) */
  175. SD_DATA_CRC_ERROR, /* data block sent/received (CRC check failed) */
  176. SD_CMD_RESP_TIMEOUT, /* command response timeout */
  177. SD_DATA_TIMEOUT, /* data timeout */
  178. SD_TX_UNDERRUN_ERROR, /* transmit FIFO underrun error occurs */
  179. SD_RX_OVERRUN_ERROR, /* received FIFO overrun error occurs */
  180. SD_START_BIT_ERROR, /* start bit error in the bus */
  181. SD_VOLTRANGE_INVALID, /* the voltage range is invalid */
  182. SD_PARAMETER_INVALID, /* the parameter is invalid */
  183. SD_OPERATION_IMPROPER, /* the operation is improper */
  184. SD_FUNCTION_UNSUPPORTED, /* the function is unsupported */
  185. SD_ERROR, /* an error occurred */
  186. SD_OK /* no error occurred */
  187. }sd_error_enum;
  188. typedef enum
  189. {
  190. SD_NO_TRANSFER = 0, /* no data transfer is acting */
  191. SD_TRANSFER_IN_PROGRESS /* data transfer is in progress */
  192. }sd_transfer_state_enum;
  193. extern uint32_t sd_scr[2]; /* SD card SCR */
  194. /* function declarations */
  195. /* initialize the SD card and make it in standby state */
  196. sd_error_enum sd_init(void);
  197. /* initialize the card and get CID and CSD of the card */
  198. sd_error_enum sd_card_init(void);
  199. /* configure the clock and the work voltage, and get the card type */
  200. sd_error_enum sd_power_on(void);
  201. /* close the power of SDIO */
  202. sd_error_enum sd_power_off(void);
  203. /* configure the bus mode */
  204. sd_error_enum sd_bus_mode_config(uint32_t busmode);
  205. /* configure the mode of transmission */
  206. sd_error_enum sd_transfer_mode_config(uint32_t txmode);
  207. /* read a block data into a buffer from the specified address of a card */
  208. sd_error_enum sd_block_read(uint32_t *preadbuffer, uint32_t readaddr, uint16_t blocksize);
  209. /* read multiple blocks data into a buffer from the specified address of a card */
  210. sd_error_enum sd_multiblocks_read(uint32_t *preadbuffer, uint32_t readaddr, uint16_t blocksize, uint32_t blocksnumber);
  211. /* write a block data to the specified address of a card */
  212. sd_error_enum sd_block_write(uint32_t *pwritebuffer, uint32_t writeaddr, uint16_t blocksize);
  213. /* write multiple blocks data to the specified address of a card */
  214. sd_error_enum sd_multiblocks_write(uint32_t *pwritebuffer, uint32_t writeaddr, uint16_t blocksize, uint32_t blocksnumber);
  215. /* erase a continuous area of a card */
  216. sd_error_enum sd_erase(uint32_t startaddr, uint32_t endaddr);
  217. /* process all the interrupts which the corresponding flags are set */
  218. sd_error_enum sd_interrupts_process(void);
  219. /* select or deselect a card */
  220. sd_error_enum sd_card_select_deselect(uint16_t cardrca);
  221. /* get the card status whose response format R1 contains a 32-bit field */
  222. sd_error_enum sd_cardstatus_get(uint32_t *pcardstatus);
  223. /* get the SD status, the size of the SD status is one data block of 512 bit */
  224. sd_error_enum sd_sdstatus_get(uint32_t *psdstatus);
  225. /* stop an ongoing data transfer */
  226. sd_error_enum sd_transfer_stop(void);
  227. /* lock or unlock a card */
  228. sd_error_enum sd_lock_unlock(uint8_t lockstate);
  229. /* get the data transfer state */
  230. sd_transfer_state_enum sd_transfer_state_get(void);
  231. /* get SD card capacity(KB) */
  232. uint32_t sd_card_capacity_get(void);
  233. /* get the detailed information of the SD card based on received CID and CSD */
  234. sd_error_enum sd_card_information_get(sd_card_info_struct *pcardinfo);
  235. #endif /* SDCARD_H */