sd_spi.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #pragma once
  2. #include <stdint.h>
  3. #include <stdbool.h>
  4. #define __IO volatile
  5. #define SD_TIMEOUT_MS 500//(1000)
  6. #define SD_BLOCK_SIZE 512
  7. #define VERSION_APP "0.2"
  8. #define DEVELOPED " "
  9. #define GITHUB "github.com/Gl1tchub/Flipperzero-SD-SPI"
  10. typedef enum {
  11. SdSpiStatusOK,
  12. SdSpiStatusError,
  13. SdSpiStatusTimeout,
  14. } SdSpiStatus;
  15. /** R1 answer value */
  16. typedef enum {
  17. SdSpi_R1_NO_ERROR = 0x00,
  18. SdSpi_R1_IN_IDLE_STATE = 0x01,
  19. SdSpi_R1_ERASE_RESET = 0x02,
  20. SdSpi_R1_ILLEGAL_COMMAND = 0x04,
  21. SdSpi_R1_COM_CRC_ERROR = 0x08,
  22. SdSpi_R1_ERASE_SEQUENCE_ERROR = 0x10,
  23. SdSpi_R1_ADDRESS_ERROR = 0x20,
  24. SdSpi_R1_PARAMETER_ERROR = 0x40,
  25. } SdSpiR1;
  26. /** R2 answer value */
  27. typedef enum {
  28. /* R2 answer value */
  29. SdSpi_R2_NO_ERROR = 0x00,
  30. SdSpi_R2_CARD_LOCKED = 0x01,
  31. SdSpi_R2_LOCKUNLOCK_ERROR = 0x02,
  32. SdSpi_R2_ERROR = 0x04,
  33. SdSpi_R2_CC_ERROR = 0x08,
  34. SdSpi_R2_CARD_ECC_FAILED = 0x10,
  35. SdSpi_R2_WP_VIOLATION = 0x20,
  36. SdSpi_R2_ERASE_PARAM = 0x40,
  37. SdSpi_R2_OUTOFRANGE = 0x80,
  38. } SdSpiR2;
  39. /**
  40. * @brief Card Specific Data: CSD Register
  41. */
  42. typedef struct {
  43. /* Header part */
  44. uint8_t CSDStruct : 2; /* CSD structure */
  45. uint8_t Reserved1 : 6; /* Reserved */
  46. uint8_t TAAC : 8; /* Data read access-time 1 */
  47. uint8_t NSAC : 8; /* Data read access-time 2 in CLK cycles */
  48. uint8_t MaxBusClkFrec : 8; /* Max. bus clock frequency */
  49. uint16_t CardComdClasses : 12; /* Card command classes */
  50. uint8_t RdBlockLen : 4; /* Max. read data block length */
  51. uint8_t PartBlockRead : 1; /* Partial blocks for read allowed */
  52. uint8_t WrBlockMisalign : 1; /* Write block misalignment */
  53. uint8_t RdBlockMisalign : 1; /* Read block misalignment */
  54. uint8_t DSRImpl : 1; /* DSR implemented */
  55. /* v1 or v2 struct */
  56. union csd_version {
  57. struct {
  58. uint8_t Reserved1 : 2; /* Reserved */
  59. uint16_t DeviceSize : 12; /* Device Size */
  60. uint8_t MaxRdCurrentVDDMin : 3; /* Max. read current @ VDD min */
  61. uint8_t MaxRdCurrentVDDMax : 3; /* Max. read current @ VDD max */
  62. uint8_t MaxWrCurrentVDDMin : 3; /* Max. write current @ VDD min */
  63. uint8_t MaxWrCurrentVDDMax : 3; /* Max. write current @ VDD max */
  64. uint8_t DeviceSizeMul : 3; /* Device size multiplier */
  65. } v1;
  66. struct {
  67. uint8_t Reserved1 : 6; /* Reserved */
  68. uint32_t DeviceSize : 22; /* Device Size */
  69. uint8_t Reserved2 : 1; /* Reserved */
  70. } v2;
  71. } version;
  72. uint8_t EraseSingleBlockEnable : 1; /* Erase single block enable */
  73. uint8_t EraseSectorSize : 7; /* Erase group size multiplier */
  74. uint8_t WrProtectGrSize : 7; /* Write protect group size */
  75. uint8_t WrProtectGrEnable : 1; /* Write protect group enable */
  76. uint8_t Reserved2 : 2; /* Reserved */
  77. uint8_t WrSpeedFact : 3; /* Write speed factor */
  78. uint8_t MaxWrBlockLen : 4; /* Max. write data block length */
  79. uint8_t WriteBlockPartial : 1; /* Partial blocks for write allowed */
  80. uint8_t Reserved3 : 5; /* Reserved */
  81. uint8_t FileFormatGrouop : 1; /* File format group */
  82. uint8_t CopyFlag : 1; /* Copy flag (OTP) */
  83. uint8_t PermWrProtect : 1; /* Permanent write protection */
  84. uint8_t TempWrProtect : 1; /* Temporary write protection */
  85. uint8_t FileFormat : 2; /* File Format */
  86. uint8_t Reserved4 : 2; /* Reserved */
  87. uint8_t crc : 7; /* Reserved */
  88. uint8_t Reserved5 : 1; /* always 1*/
  89. } SD_CSD;
  90. /**
  91. * @brief Card Identification Data: CID Register
  92. */
  93. typedef struct {
  94. uint8_t ManufacturerID; /* ManufacturerID */
  95. char OEM_AppliID[2]; /* OEM/Application ID */
  96. char ProdName[5]; /* Product Name */
  97. uint8_t ProdRev; /* Product Revision */
  98. uint32_t ProdSN; /* Product Serial Number */
  99. uint8_t Reserved1; /* Reserved1 */
  100. uint8_t ManufactYear; /* Manufacturing Year */
  101. uint8_t ManufactMonth; /* Manufacturing Month */
  102. uint8_t CID_CRC; /* CID CRC */
  103. uint8_t Reserved2; /* always 1 */
  104. } SD_CID;
  105. /**
  106. * @brief SD Card information structure
  107. */
  108. typedef struct {
  109. SD_CSD Csd;
  110. SD_CID Cid;
  111. uint64_t CardCapacity; /*!< Card Capacity */
  112. uint32_t CardBlockSize; /*!< Card Block Size */
  113. uint32_t LogBlockNbr; /*!< Specifies the Card logical Capacity in blocks */
  114. uint32_t LogBlockSize; /*!< Specifies logical block size in bytes */
  115. } SD_CardInfo;
  116. typedef struct {
  117. uint8_t r1;
  118. uint8_t r2;
  119. uint8_t r3;
  120. uint8_t r4;
  121. uint8_t r5;
  122. } SdSpiCmdAnswer;
  123. extern SdSpiCmdAnswer cmd_answer;
  124. /**
  125. * @brief SD card max mount retry count
  126. *
  127. * @return uint8_t
  128. */
  129. uint8_t sd_max_mount_retry_count();
  130. /**
  131. * @brief Init sd card
  132. *
  133. * @param power_reset reset card power
  134. * @return SdSpiStatus
  135. */
  136. SdSpiStatus sd_init(bool power_reset);
  137. /**
  138. * @brief Get card state
  139. *
  140. * @return SdSpiStatus
  141. */
  142. SdSpiStatus sd_get_card_state(void);
  143. /**
  144. * @brief Get card info
  145. *
  146. * @param card_info
  147. * @return SdSpiStatus
  148. */
  149. SdSpiStatus sd_get_card_info(SD_CardInfo* card_info);
  150. /**
  151. * @brief Read blocks
  152. *
  153. * @param data
  154. * @param address
  155. * @param blocks
  156. * @param timeout_ms
  157. * @return SdSpiStatus
  158. */
  159. SdSpiStatus sd_read_blocks(uint32_t* data, uint32_t address, uint32_t blocks, uint32_t timeout_ms);
  160. /**
  161. * @brief Write blocks
  162. *
  163. * @param data
  164. * @param address
  165. * @param blocks
  166. * @param timeout_ms
  167. * @return SdSpiStatus
  168. */
  169. SdSpiStatus
  170. sd_write_blocks(uint32_t* data, uint32_t address, uint32_t blocks, uint32_t timeout_ms);
  171. /**
  172. * @brief Get card CSD register
  173. *
  174. * @param Cid
  175. * @return SdSpiStatus
  176. */
  177. SdSpiStatus sd_get_cid(SD_CID* cid);
  178. SdSpiStatus sd_set_pwd(char* pwd);
  179. SdSpiStatus sd_clr_pwd(char* pwd);
  180. SdSpiStatus sd_force_erase(void);