drv_sdio.h 15 KB

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