Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

491 lines
17KB

  1. /**
  2. * Copyright (c) 2011-2019 Bill Greiman
  3. * This file is part of the SdFat library for SD memory cards.
  4. *
  5. * MIT License
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included
  15. * in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #ifndef SdCardInfo_h
  26. #define SdCardInfo_h
  27. #include <stdint.h>
  28. #include "../common/SysCall.h"
  29. // Based on the document:
  30. //
  31. // SD Specifications
  32. // Part 1
  33. // Physical Layer
  34. // Simplified Specification
  35. // Version 5.00
  36. // Aug 10, 2016
  37. //
  38. // https://www.sdcard.org/downloads/pls/
  39. //------------------------------------------------------------------------------
  40. // SD card errors
  41. // See the SD Specification for command info.
  42. #define SD_ERROR_CODE_LIST\
  43. SD_CARD_ERROR(NONE, "No error")\
  44. SD_CARD_ERROR(CMD0, "Card reset failed")\
  45. SD_CARD_ERROR(CMD2, "SDIO read CID")\
  46. SD_CARD_ERROR(CMD3, "SDIO publish RCA")\
  47. SD_CARD_ERROR(CMD6, "Switch card function")\
  48. SD_CARD_ERROR(CMD7, "SDIO card select")\
  49. SD_CARD_ERROR(CMD8, "Send and check interface settings")\
  50. SD_CARD_ERROR(CMD9, "Read CSD data")\
  51. SD_CARD_ERROR(CMD10, "Read CID data")\
  52. SD_CARD_ERROR(CMD12, "Stop multiple block read")\
  53. SD_CARD_ERROR(CMD13, "Read card status")\
  54. SD_CARD_ERROR(CMD17, "Read single block")\
  55. SD_CARD_ERROR(CMD18, "Read multiple blocks")\
  56. SD_CARD_ERROR(CMD24, "Write single block")\
  57. SD_CARD_ERROR(CMD25, "Write multiple blocks")\
  58. SD_CARD_ERROR(CMD32, "Set first erase block")\
  59. SD_CARD_ERROR(CMD33, "Set last erase block")\
  60. SD_CARD_ERROR(CMD38, "Erase selected blocks")\
  61. SD_CARD_ERROR(CMD58, "Read OCR register")\
  62. SD_CARD_ERROR(CMD59, "Set CRC mode")\
  63. SD_CARD_ERROR(ACMD6, "Set SDIO bus width")\
  64. SD_CARD_ERROR(ACMD13, "Read extended status")\
  65. SD_CARD_ERROR(ACMD23, "Set pre-erased count")\
  66. SD_CARD_ERROR(ACMD41, "Activate card initialization")\
  67. SD_CARD_ERROR(READ_TOKEN, "Bad read data token")\
  68. SD_CARD_ERROR(READ_CRC, "Read CRC error")\
  69. SD_CARD_ERROR(READ_FIFO, "SDIO fifo read timeout")\
  70. SD_CARD_ERROR(READ_REG, "Read CID or CSD failed.")\
  71. SD_CARD_ERROR(READ_START, "Bad readStart argument")\
  72. SD_CARD_ERROR(READ_TIMEOUT, "Read data timeout")\
  73. SD_CARD_ERROR(STOP_TRAN, "Multiple block stop failed")\
  74. SD_CARD_ERROR(WRITE_DATA, "Write data not accepted")\
  75. SD_CARD_ERROR(WRITE_FIFO, "SDIO fifo write timeout")\
  76. SD_CARD_ERROR(WRITE_START, "Bad writeStart argument")\
  77. SD_CARD_ERROR(WRITE_PROGRAMMING, "Flash programming")\
  78. SD_CARD_ERROR(WRITE_TIMEOUT, "Write timeout")\
  79. SD_CARD_ERROR(DMA, "DMA transfer failed")\
  80. SD_CARD_ERROR(ERASE, "Card did not accept erase commands")\
  81. SD_CARD_ERROR(ERASE_SINGLE_SECTOR, "Card does not support erase")\
  82. SD_CARD_ERROR(ERASE_TIMEOUT, "Erase command timeout")\
  83. SD_CARD_ERROR(INIT_NOT_CALLED, "Card has not been initialized")\
  84. SD_CARD_ERROR(INVALID_CARD_CONFIG, "Invalid card config")\
  85. SD_CARD_ERROR(FUNCTION_NOT_SUPPORTED, "Unsupported SDIO command")
  86. enum {
  87. #define SD_CARD_ERROR(e, m) SD_CARD_ERROR_##e,
  88. SD_ERROR_CODE_LIST
  89. #undef SD_CARD_ERROR
  90. SD_CARD_ERROR_UNKNOWN
  91. };
  92. void printSdErrorSymbol(print_t* pr, uint8_t code);
  93. void printSdErrorText(print_t* pr, uint8_t code);
  94. //------------------------------------------------------------------------------
  95. // card types
  96. /** Standard capacity V1 SD card */
  97. const uint8_t SD_CARD_TYPE_SD1 = 1;
  98. /** Standard capacity V2 SD card */
  99. const uint8_t SD_CARD_TYPE_SD2 = 2;
  100. /** High Capacity SD card */
  101. const uint8_t SD_CARD_TYPE_SDHC = 3;
  102. //------------------------------------------------------------------------------
  103. // SD operation timeouts
  104. /** CMD0 retry count */
  105. const uint8_t SD_CMD0_RETRY = 10;
  106. /** command timeout ms */
  107. const uint16_t SD_CMD_TIMEOUT = 300;
  108. /** erase timeout ms */
  109. const uint16_t SD_ERASE_TIMEOUT = 10000;
  110. /** init timeout ms */
  111. const uint16_t SD_INIT_TIMEOUT = 2000;
  112. /** read timeout ms */
  113. const uint16_t SD_READ_TIMEOUT = 300;
  114. /** write time out ms */
  115. const uint16_t SD_WRITE_TIMEOUT = 600;
  116. //------------------------------------------------------------------------------
  117. // SD card commands
  118. /** GO_IDLE_STATE - init card in spi mode if CS low */
  119. const uint8_t CMD0 = 0X00;
  120. /** ALL_SEND_CID - Asks any card to send the CID. */
  121. const uint8_t CMD2 = 0X02;
  122. /** SEND_RELATIVE_ADDR - Ask the card to publish a new RCA. */
  123. const uint8_t CMD3 = 0X03;
  124. /** SWITCH_FUNC - Switch Function Command */
  125. const uint8_t CMD6 = 0X06;
  126. /** SELECT/DESELECT_CARD - toggles between the stand-by and transfer states. */
  127. const uint8_t CMD7 = 0X07;
  128. /** SEND_IF_COND - verify SD Memory Card interface operating condition.*/
  129. const uint8_t CMD8 = 0X08;
  130. /** SEND_CSD - read the Card Specific Data (CSD register) */
  131. const uint8_t CMD9 = 0X09;
  132. /** SEND_CID - read the card identification information (CID register) */
  133. const uint8_t CMD10 = 0X0A;
  134. /** VOLTAGE_SWITCH -Switch to 1.8V bus signaling level. */
  135. const uint8_t CMD11 = 0X0B;
  136. /** STOP_TRANSMISSION - end multiple sector read sequence */
  137. const uint8_t CMD12 = 0X0C;
  138. /** SEND_STATUS - read the card status register */
  139. const uint8_t CMD13 = 0X0D;
  140. /** READ_SINGLE_SECTOR - read a single data sector from the card */
  141. const uint8_t CMD17 = 0X11;
  142. /** READ_MULTIPLE_SECTOR - read multiple data sectors from the card */
  143. const uint8_t CMD18 = 0X12;
  144. /** WRITE_SECTOR - write a single data sector to the card */
  145. const uint8_t CMD24 = 0X18;
  146. /** WRITE_MULTIPLE_SECTOR - write sectors of data until a STOP_TRANSMISSION */
  147. const uint8_t CMD25 = 0X19;
  148. /** ERASE_WR_BLK_START - sets the address of the first sector to be erased */
  149. const uint8_t CMD32 = 0X20;
  150. /** ERASE_WR_BLK_END - sets the address of the last sector of the continuous
  151. range to be erased*/
  152. const uint8_t CMD33 = 0X21;
  153. /** ERASE - erase all previously selected sectors */
  154. const uint8_t CMD38 = 0X26;
  155. /** APP_CMD - escape for application specific command */
  156. const uint8_t CMD55 = 0X37;
  157. /** READ_OCR - read the OCR register of a card */
  158. const uint8_t CMD58 = 0X3A;
  159. /** CRC_ON_OFF - enable or disable CRC checking */
  160. const uint8_t CMD59 = 0X3B;
  161. /** SET_BUS_WIDTH - Defines the data bus width for data transfer. */
  162. const uint8_t ACMD6 = 0X06;
  163. /** SD_STATUS - Send the SD Status. */
  164. const uint8_t ACMD13 = 0X0D;
  165. /** SET_WR_BLK_ERASE_COUNT - Set the number of write sectors to be
  166. pre-erased before writing */
  167. const uint8_t ACMD23 = 0X17;
  168. /** SD_SEND_OP_COMD - Sends host capacity support information and
  169. activates the card's initialization process */
  170. const uint8_t ACMD41 = 0X29;
  171. //==============================================================================
  172. // CARD_STATUS
  173. /** The command's argument was out of the allowed range for this card. */
  174. const uint32_t CARD_STATUS_OUT_OF_RANGE = 1UL << 31;
  175. /** A misaligned address which did not match the sector length. */
  176. const uint32_t CARD_STATUS_ADDRESS_ERROR = 1UL << 30;
  177. /** The transferred sector length is not allowed for this card. */
  178. const uint32_t CARD_STATUS_SECTOR_LEN_ERROR = 1UL << 29;
  179. /** An error in the sequence of erase commands occurred. */
  180. const uint32_t CARD_STATUS_ERASE_SEQ_ERROR = 1UL <<28;
  181. /** An invalid selection of write-sectors for erase occurred. */
  182. const uint32_t CARD_STATUS_ERASE_PARAM = 1UL << 27;
  183. /** Set when the host attempts to write to a protected sector. */
  184. const uint32_t CARD_STATUS_WP_VIOLATION = 1UL << 26;
  185. /** When set, signals that the card is locked by the host. */
  186. const uint32_t CARD_STATUS_CARD_IS_LOCKED = 1UL << 25;
  187. /** Set when a sequence or password error has been detected. */
  188. const uint32_t CARD_STATUS_LOCK_UNLOCK_FAILED = 1UL << 24;
  189. /** The CRC check of the previous command failed. */
  190. const uint32_t CARD_STATUS_COM_CRC_ERROR = 1UL << 23;
  191. /** Command not legal for the card state. */
  192. const uint32_t CARD_STATUS_ILLEGAL_COMMAND = 1UL << 22;
  193. /** Card internal ECC was applied but failed to correct the data. */
  194. const uint32_t CARD_STATUS_CARD_ECC_FAILED = 1UL << 21;
  195. /** Internal card controller error */
  196. const uint32_t CARD_STATUS_CC_ERROR = 1UL << 20;
  197. /** A general or an unknown error occurred during the operation. */
  198. const uint32_t CARD_STATUS_ERROR = 1UL << 19;
  199. // bits 19, 18, and 17 reserved.
  200. /** Permanent WP set or attempt to change read only values of CSD. */
  201. const uint32_t CARD_STATUS_CSD_OVERWRITE = 1UL <<16;
  202. /** partial address space was erased due to write protect. */
  203. const uint32_t CARD_STATUS_WP_ERASE_SKIP = 1UL << 15;
  204. /** The command has been executed without using the internal ECC. */
  205. const uint32_t CARD_STATUS_CARD_ECC_DISABLED = 1UL << 14;
  206. /** out of erase sequence command was received. */
  207. const uint32_t CARD_STATUS_ERASE_RESET = 1UL << 13;
  208. /** The state of the card when receiving the command.
  209. * 0 = idle
  210. * 1 = ready
  211. * 2 = ident
  212. * 3 = stby
  213. * 4 = tran
  214. * 5 = data
  215. * 6 = rcv
  216. * 7 = prg
  217. * 8 = dis
  218. * 9-14 = reserved
  219. * 15 = reserved for I/O mode
  220. */
  221. const uint32_t CARD_STATUS_CURRENT_STATE = 0XF << 9;
  222. /** Shift for current state. */
  223. const uint32_t CARD_STATUS_CURRENT_STATE_SHIFT = 9;
  224. /** Corresponds to buffer empty signaling on the bus. */
  225. const uint32_t CARD_STATUS_READY_FOR_DATA = 1UL << 8;
  226. // bit 7 reserved.
  227. /** Extension Functions may set this bit to get host to deal with events. */
  228. const uint32_t CARD_STATUS_FX_EVENT = 1UL << 6;
  229. /** The card will expect ACMD, or the command has been interpreted as ACMD */
  230. const uint32_t CARD_STATUS_APP_CMD = 1UL << 5;
  231. // bit 4 reserved.
  232. /** Error in the sequence of the authentication process. */
  233. const uint32_t CARD_STATUS_AKE_SEQ_ERROR = 1UL << 3;
  234. // bits 2,1, and 0 reserved for manufacturer test mode.
  235. //==============================================================================
  236. /** status for card in the ready state */
  237. const uint8_t R1_READY_STATE = 0X00;
  238. /** status for card in the idle state */
  239. const uint8_t R1_IDLE_STATE = 0X01;
  240. /** status bit for illegal command */
  241. const uint8_t R1_ILLEGAL_COMMAND = 0X04;
  242. /** start data token for read or write single sector*/
  243. const uint8_t DATA_START_SECTOR = 0XFE;
  244. /** stop token for write multiple sectors*/
  245. const uint8_t STOP_TRAN_TOKEN = 0XFD;
  246. /** start data token for write multiple sectors*/
  247. const uint8_t WRITE_MULTIPLE_TOKEN = 0XFC;
  248. /** mask for data response tokens after a write sector operation */
  249. const uint8_t DATA_RES_MASK = 0X1F;
  250. /** write data accepted token */
  251. const uint8_t DATA_RES_ACCEPTED = 0X05;
  252. //==============================================================================
  253. /**
  254. * \class CID
  255. * \brief Card IDentification (CID) register.
  256. */
  257. typedef struct CID {
  258. // byte 0
  259. /** Manufacturer ID */
  260. unsigned char mid;
  261. // byte 1-2
  262. /** OEM/Application ID */
  263. char oid[2];
  264. // byte 3-7
  265. /** Product name */
  266. char pnm[5];
  267. // byte 8
  268. /** Product revision least significant digit */
  269. unsigned char prv_m : 4;
  270. /** Product revision most significant digit */
  271. unsigned char prv_n : 4;
  272. // byte 9-12
  273. /** Product serial number */
  274. uint32_t psn;
  275. // byte 13
  276. /** Manufacturing date year high digit */
  277. unsigned char mdt_year_high : 4;
  278. /** not used */
  279. unsigned char reserved : 4;
  280. // byte 14
  281. /** Manufacturing date month */
  282. unsigned char mdt_month : 4;
  283. /** Manufacturing date year low digit */
  284. unsigned char mdt_year_low : 4;
  285. // byte 15
  286. /** not used always 1 */
  287. unsigned char always1 : 1;
  288. /** CRC7 checksum */
  289. unsigned char crc : 7;
  290. } __attribute__((packed)) cid_t;
  291. //==============================================================================
  292. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  293. /**
  294. * \class CSDV1
  295. * \brief CSD register for version 1.00 cards .
  296. */
  297. typedef struct CSDV1 {
  298. // byte 0
  299. unsigned char reserved1 : 6;
  300. unsigned char csd_ver : 2;
  301. // byte 1
  302. unsigned char taac;
  303. // byte 2
  304. unsigned char nsac;
  305. // byte 3
  306. unsigned char tran_speed;
  307. // byte 4
  308. unsigned char ccc_high;
  309. // byte 5
  310. unsigned char read_bl_len : 4;
  311. unsigned char ccc_low : 4;
  312. // byte 6
  313. unsigned char c_size_high : 2;
  314. unsigned char reserved2 : 2;
  315. unsigned char dsr_imp : 1;
  316. unsigned char read_blk_misalign : 1;
  317. unsigned char write_blk_misalign : 1;
  318. unsigned char read_bl_partial : 1;
  319. // byte 7
  320. unsigned char c_size_mid;
  321. // byte 8
  322. unsigned char vdd_r_curr_max : 3;
  323. unsigned char vdd_r_curr_min : 3;
  324. unsigned char c_size_low : 2;
  325. // byte 9
  326. unsigned char c_size_mult_high : 2;
  327. unsigned char vdd_w_cur_max : 3;
  328. unsigned char vdd_w_curr_min : 3;
  329. // byte 10
  330. unsigned char sector_size_high : 6;
  331. unsigned char erase_blk_en : 1;
  332. unsigned char c_size_mult_low : 1;
  333. // byte 11
  334. unsigned char wp_grp_size : 7;
  335. unsigned char sector_size_low : 1;
  336. // byte 12
  337. unsigned char write_bl_len_high : 2;
  338. unsigned char r2w_factor : 3;
  339. unsigned char reserved3 : 2;
  340. unsigned char wp_grp_enable : 1;
  341. // byte 13
  342. unsigned char reserved4 : 5;
  343. unsigned char write_partial : 1;
  344. unsigned char write_bl_len_low : 2;
  345. // byte 14
  346. unsigned char reserved5: 2;
  347. unsigned char file_format : 2;
  348. unsigned char tmp_write_protect : 1;
  349. unsigned char perm_write_protect : 1;
  350. unsigned char copy : 1;
  351. /** Indicates the file format on the card */
  352. unsigned char file_format_grp : 1;
  353. // byte 15
  354. unsigned char always1 : 1;
  355. unsigned char crc : 7;
  356. } __attribute__((packed)) csd1_t;
  357. //==============================================================================
  358. /**
  359. * \class CSDV2
  360. * \brief CSD register for version 2.00 cards.
  361. */
  362. typedef struct CSDV2 {
  363. // byte 0
  364. unsigned char reserved1 : 6;
  365. unsigned char csd_ver : 2;
  366. // byte 1
  367. /** fixed to 0X0E */
  368. unsigned char taac;
  369. // byte 2
  370. /** fixed to 0 */
  371. unsigned char nsac;
  372. // byte 3
  373. unsigned char tran_speed;
  374. // byte 4
  375. unsigned char ccc_high;
  376. // byte 5
  377. /** This field is fixed to 9h, which indicates READ_BL_LEN=512 Byte */
  378. unsigned char read_bl_len : 4;
  379. unsigned char ccc_low : 4;
  380. // byte 6
  381. /** not used */
  382. unsigned char reserved2 : 4;
  383. unsigned char dsr_imp : 1;
  384. /** fixed to 0 */
  385. unsigned char read_blk_misalign : 1;
  386. /** fixed to 0 */
  387. unsigned char write_blk_misalign : 1;
  388. /** fixed to 0 - no partial read */
  389. unsigned char read_bl_partial : 1;
  390. // byte 7
  391. /** high part of card size */
  392. unsigned char c_size_high : 6;
  393. /** not used */
  394. unsigned char reserved3 : 2;
  395. // byte 8
  396. /** middle part of card size */
  397. unsigned char c_size_mid;
  398. // byte 9
  399. /** low part of card size */
  400. unsigned char c_size_low;
  401. // byte 10
  402. /** sector size is fixed at 64 KB */
  403. unsigned char sector_size_high : 6;
  404. /** fixed to 1 - erase single is supported */
  405. unsigned char erase_blk_en : 1;
  406. /** not used */
  407. unsigned char reserved4 : 1;
  408. // byte 11
  409. unsigned char wp_grp_size : 7;
  410. /** sector size is fixed at 64 KB */
  411. unsigned char sector_size_low : 1;
  412. // byte 12
  413. /** write_bl_len fixed for 512 byte sectors */
  414. unsigned char write_bl_len_high : 2;
  415. /** fixed value of 2 */
  416. unsigned char r2w_factor : 3;
  417. /** not used */
  418. unsigned char reserved5 : 2;
  419. /** fixed value of 0 - no write protect groups */
  420. unsigned char wp_grp_enable : 1;
  421. // byte 13
  422. unsigned char reserved6 : 5;
  423. /** always zero - no partial sector read*/
  424. unsigned char write_partial : 1;
  425. /** write_bl_len fixed for 512 byte sectors */
  426. unsigned char write_bl_len_low : 2;
  427. // byte 14
  428. unsigned char reserved7: 2;
  429. /** Do not use always 0 */
  430. unsigned char file_format : 2;
  431. unsigned char tmp_write_protect : 1;
  432. unsigned char perm_write_protect : 1;
  433. unsigned char copy : 1;
  434. /** Do not use always 0 */
  435. unsigned char file_format_grp : 1;
  436. // byte 15
  437. /** not used always 1 */
  438. unsigned char always1 : 1;
  439. /** checksum */
  440. unsigned char crc : 7;
  441. } __attribute__((packed)) csd2_t;
  442. //==============================================================================
  443. /**
  444. * \class csd_t
  445. * \brief Union of old and new style CSD register.
  446. */
  447. union csd_t {
  448. csd1_t v1;
  449. csd2_t v2;
  450. };
  451. //-----------------------------------------------------------------------------
  452. inline uint32_t sdCardCapacity(csd_t* csd) {
  453. if (csd->v1.csd_ver == 0) {
  454. uint8_t read_bl_len = csd->v1.read_bl_len;
  455. uint16_t c_size = (csd->v1.c_size_high << 10)
  456. | (csd->v1.c_size_mid << 2) | csd->v1.c_size_low;
  457. uint8_t c_size_mult = (csd->v1.c_size_mult_high << 1)
  458. | csd->v1.c_size_mult_low;
  459. return (uint32_t)(c_size + 1) << (c_size_mult + read_bl_len - 7);
  460. } else if (csd->v2.csd_ver == 1) {
  461. return (((uint32_t)csd->v2.c_size_high << 16) +
  462. ((uint16_t)csd->v2.c_size_mid << 8) + csd->v2.c_size_low + 1) << 10;
  463. } else {
  464. return 0;
  465. }
  466. }
  467. //-----------------------------------------------------------------------------
  468. // fields are big endian
  469. typedef struct SdStatus {
  470. uint8_t busWidthSecureMode;
  471. uint8_t reserved1;
  472. uint8_t sdCardType[2];
  473. uint8_t sizeOfProtectedArea[4];
  474. uint8_t speedClass;
  475. uint8_t performanceMove;
  476. uint8_t auSize;
  477. uint8_t eraseSize[2];
  478. uint8_t eraseTimeoutOffset;
  479. uint8_t uhsSpeedAuSize;
  480. uint8_t videoSpeed;
  481. uint8_t vscAuSize[2];
  482. uint8_t susAddr[3];
  483. uint8_t reserved2[3];
  484. uint8_t reservedManufacturer[40];
  485. } SdStatus_t;
  486. #endif // DOXYGEN_SHOULD_SKIP_THIS
  487. #endif // SdCardInfo_h