You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /* Arduino SdSpiCard Library
  2. * Copyright (C) 2012 by William Greiman
  3. *
  4. * This file is part of the Arduino SdSpiCard Library
  5. *
  6. * This Library is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This Library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Arduino SdSpiCard Library. If not, see
  18. * <http://www.gnu.org/licenses/>.
  19. */
  20. #ifndef SdInfo_h
  21. #define SdInfo_h
  22. #include <stdint.h>
  23. // Based on the document:
  24. //
  25. // SD Specifications
  26. // Part 1
  27. // Physical Layer
  28. // Simplified Specification
  29. // Version 3.01
  30. // May 18, 2010
  31. //
  32. // http://www.sdcard.org/developers/tech/sdcard/pls/simplified_specs
  33. //------------------------------------------------------------------------------
  34. // SD card errors
  35. /** timeout error for command CMD0 (initialize card in SPI mode) */
  36. uint8_t const SD_CARD_ERROR_CMD0 = 0X1;
  37. /** CMD8 was not accepted - not a valid SD card*/
  38. uint8_t const SD_CARD_ERROR_CMD8 = 0X2;
  39. /** card returned an error response for CMD12 (stop multiblock read) */
  40. uint8_t const SD_CARD_ERROR_CMD12 = 0X3;
  41. /** card returned an error response for CMD17 (read block) */
  42. uint8_t const SD_CARD_ERROR_CMD17 = 0X4;
  43. /** card returned an error response for CMD18 (read multiple block) */
  44. uint8_t const SD_CARD_ERROR_CMD18 = 0X5;
  45. /** card returned an error response for CMD24 (write block) */
  46. uint8_t const SD_CARD_ERROR_CMD24 = 0X6;
  47. /** WRITE_MULTIPLE_BLOCKS command failed */
  48. uint8_t const SD_CARD_ERROR_CMD25 = 0X7;
  49. /** card returned an error response for CMD58 (read OCR) */
  50. uint8_t const SD_CARD_ERROR_CMD58 = 0X8;
  51. /** SET_WR_BLK_ERASE_COUNT failed */
  52. uint8_t const SD_CARD_ERROR_ACMD23 = 0X9;
  53. /** ACMD41 initialization process timeout */
  54. uint8_t const SD_CARD_ERROR_ACMD41 = 0XA;
  55. /** card returned a bad CSR version field */
  56. uint8_t const SD_CARD_ERROR_BAD_CSD = 0XB;
  57. /** erase block group command failed */
  58. uint8_t const SD_CARD_ERROR_ERASE = 0XC;
  59. /** card not capable of single block erase */
  60. uint8_t const SD_CARD_ERROR_ERASE_SINGLE_BLOCK = 0XD;
  61. /** Erase sequence timed out */
  62. uint8_t const SD_CARD_ERROR_ERASE_TIMEOUT = 0XE;
  63. /** card returned an error token instead of read data */
  64. uint8_t const SD_CARD_ERROR_READ = 0XF;
  65. /** read CID or CSD failed */
  66. uint8_t const SD_CARD_ERROR_READ_REG = 0X10;
  67. /** timeout while waiting for start of read data */
  68. uint8_t const SD_CARD_ERROR_READ_TIMEOUT = 0X11;
  69. /** card did not accept STOP_TRAN_TOKEN */
  70. uint8_t const SD_CARD_ERROR_STOP_TRAN = 0X12;
  71. /** card returned an error token as a response to a write operation */
  72. uint8_t const SD_CARD_ERROR_WRITE = 0X13;
  73. /** attempt to write protected block zero */
  74. uint8_t const SD_CARD_ERROR_WRITE_BLOCK_ZERO = 0X14; // REMOVE - not used
  75. /** card did not go ready for a multiple block write */
  76. uint8_t const SD_CARD_ERROR_WRITE_MULTIPLE = 0X15;
  77. /** card returned an error to a CMD13 status check after a write */
  78. uint8_t const SD_CARD_ERROR_WRITE_PROGRAMMING = 0X16;
  79. /** timeout occurred during write programming */
  80. uint8_t const SD_CARD_ERROR_WRITE_TIMEOUT = 0X17;
  81. /** incorrect rate selected */
  82. uint8_t const SD_CARD_ERROR_SCK_RATE = 0X18;
  83. /** init() not called */
  84. uint8_t const SD_CARD_ERROR_INIT_NOT_CALLED = 0X19;
  85. /** card returned an error for CMD59 (CRC_ON_OFF) */
  86. uint8_t const SD_CARD_ERROR_CMD59 = 0X1A;
  87. /** invalid read CRC */
  88. uint8_t const SD_CARD_ERROR_READ_CRC = 0X1B;
  89. /** SPI DMA error */
  90. uint8_t const SD_CARD_ERROR_SPI_DMA = 0X1C;
  91. //------------------------------------------------------------------------------
  92. // card types
  93. /** Standard capacity V1 SD card */
  94. uint8_t const SD_CARD_TYPE_SD1 = 1;
  95. /** Standard capacity V2 SD card */
  96. uint8_t const SD_CARD_TYPE_SD2 = 2;
  97. /** High Capacity SD card */
  98. uint8_t const SD_CARD_TYPE_SDHC = 3;
  99. //------------------------------------------------------------------------------
  100. // SPI divisor constants
  101. /** Set SCK to max rate of F_CPU/2. */
  102. uint8_t const SPI_FULL_SPEED = 2;
  103. /** Set SCK rate to F_CPU/3 for Due */
  104. uint8_t const SPI_DIV3_SPEED = 3;
  105. /** Set SCK rate to F_CPU/4. */
  106. uint8_t const SPI_HALF_SPEED = 4;
  107. /** Set SCK rate to F_CPU/6 for Due */
  108. uint8_t const SPI_DIV6_SPEED = 6;
  109. /** Set SCK rate to F_CPU/8. */
  110. uint8_t const SPI_QUARTER_SPEED = 8;
  111. /** Set SCK rate to F_CPU/16. */
  112. uint8_t const SPI_EIGHTH_SPEED = 16;
  113. /** Set SCK rate to F_CPU/32. */
  114. uint8_t const SPI_SIXTEENTH_SPEED = 32;
  115. //------------------------------------------------------------------------------
  116. // SD operation timeouts
  117. /** init timeout ms */
  118. uint16_t const SD_INIT_TIMEOUT = 2000;
  119. /** erase timeout ms */
  120. uint16_t const SD_ERASE_TIMEOUT = 10000;
  121. /** read timeout ms */
  122. uint16_t const SD_READ_TIMEOUT = 300;
  123. /** write time out ms */
  124. uint16_t const SD_WRITE_TIMEOUT = 600;
  125. //------------------------------------------------------------------------------
  126. // SD card commands
  127. /** GO_IDLE_STATE - init card in spi mode if CS low */
  128. uint8_t const CMD0 = 0X00;
  129. /** SEND_IF_COND - verify SD Memory Card interface operating condition.*/
  130. uint8_t const CMD8 = 0X08;
  131. /** SEND_CSD - read the Card Specific Data (CSD register) */
  132. uint8_t const CMD9 = 0X09;
  133. /** SEND_CID - read the card identification information (CID register) */
  134. uint8_t const CMD10 = 0X0A;
  135. /** STOP_TRANSMISSION - end multiple block read sequence */
  136. uint8_t const CMD12 = 0X0C;
  137. /** SEND_STATUS - read the card status register */
  138. uint8_t const CMD13 = 0X0D;
  139. /** READ_SINGLE_BLOCK - read a single data block from the card */
  140. uint8_t const CMD17 = 0X11;
  141. /** READ_MULTIPLE_BLOCK - read a multiple data blocks from the card */
  142. uint8_t const CMD18 = 0X12;
  143. /** WRITE_BLOCK - write a single data block to the card */
  144. uint8_t const CMD24 = 0X18;
  145. /** WRITE_MULTIPLE_BLOCK - write blocks of data until a STOP_TRANSMISSION */
  146. uint8_t const CMD25 = 0X19;
  147. /** ERASE_WR_BLK_START - sets the address of the first block to be erased */
  148. uint8_t const CMD32 = 0X20;
  149. /** ERASE_WR_BLK_END - sets the address of the last block of the continuous
  150. range to be erased*/
  151. uint8_t const CMD33 = 0X21;
  152. /** ERASE - erase all previously selected blocks */
  153. uint8_t const CMD38 = 0X26;
  154. /** APP_CMD - escape for application specific command */
  155. uint8_t const CMD55 = 0X37;
  156. /** READ_OCR - read the OCR register of a card */
  157. uint8_t const CMD58 = 0X3A;
  158. /** CRC_ON_OFF - enable or disable CRC checking */
  159. uint8_t const CMD59 = 0X3B;
  160. /** SET_WR_BLK_ERASE_COUNT - Set the number of write blocks to be
  161. pre-erased before writing */
  162. uint8_t const ACMD23 = 0X17;
  163. /** SD_SEND_OP_COMD - Sends host capacity support information and
  164. activates the card's initialization process */
  165. uint8_t const ACMD41 = 0X29;
  166. //==============================================================================
  167. /** status for card in the ready state */
  168. uint8_t const R1_READY_STATE = 0X00;
  169. /** status for card in the idle state */
  170. uint8_t const R1_IDLE_STATE = 0X01;
  171. /** status bit for illegal command */
  172. uint8_t const R1_ILLEGAL_COMMAND = 0X04;
  173. /** start data token for read or write single block*/
  174. uint8_t const DATA_START_BLOCK = 0XFE;
  175. /** stop token for write multiple blocks*/
  176. uint8_t const STOP_TRAN_TOKEN = 0XFD;
  177. /** start data token for write multiple blocks*/
  178. uint8_t const WRITE_MULTIPLE_TOKEN = 0XFC;
  179. /** mask for data response tokens after a write block operation */
  180. uint8_t const DATA_RES_MASK = 0X1F;
  181. /** write data accepted token */
  182. uint8_t const DATA_RES_ACCEPTED = 0X05;
  183. //==============================================================================
  184. /**
  185. * \class CID
  186. * \brief Card IDentification (CID) register.
  187. */
  188. typedef struct CID {
  189. // byte 0
  190. /** Manufacturer ID */
  191. unsigned char mid;
  192. // byte 1-2
  193. /** OEM/Application ID */
  194. char oid[2];
  195. // byte 3-7
  196. /** Product name */
  197. char pnm[5];
  198. // byte 8
  199. /** Product revision least significant digit */
  200. unsigned char prv_m : 4;
  201. /** Product revision most significant digit */
  202. unsigned char prv_n : 4;
  203. // byte 9-12
  204. /** Product serial number */
  205. uint32_t psn;
  206. // byte 13
  207. /** Manufacturing date year low digit */
  208. unsigned char mdt_year_high : 4;
  209. /** not used */
  210. unsigned char reserved : 4;
  211. // byte 14
  212. /** Manufacturing date month */
  213. unsigned char mdt_month : 4;
  214. /** Manufacturing date year low digit */
  215. unsigned char mdt_year_low : 4;
  216. // byte 15
  217. /** not used always 1 */
  218. unsigned char always1 : 1;
  219. /** CRC7 checksum */
  220. unsigned char crc : 7;
  221. } __attribute__((packed)) cid_t;
  222. //==============================================================================
  223. /**
  224. * \class CSDV1
  225. * \brief CSD register for version 1.00 cards .
  226. */
  227. typedef struct CSDV1 {
  228. // byte 0
  229. unsigned char reserved1 : 6;
  230. unsigned char csd_ver : 2;
  231. // byte 1
  232. unsigned char taac;
  233. // byte 2
  234. unsigned char nsac;
  235. // byte 3
  236. unsigned char tran_speed;
  237. // byte 4
  238. unsigned char ccc_high;
  239. // byte 5
  240. unsigned char read_bl_len : 4;
  241. unsigned char ccc_low : 4;
  242. // byte 6
  243. unsigned char c_size_high : 2;
  244. unsigned char reserved2 : 2;
  245. unsigned char dsr_imp : 1;
  246. unsigned char read_blk_misalign : 1;
  247. unsigned char write_blk_misalign : 1;
  248. unsigned char read_bl_partial : 1;
  249. // byte 7
  250. unsigned char c_size_mid;
  251. // byte 8
  252. unsigned char vdd_r_curr_max : 3;
  253. unsigned char vdd_r_curr_min : 3;
  254. unsigned char c_size_low : 2;
  255. // byte 9
  256. unsigned char c_size_mult_high : 2;
  257. unsigned char vdd_w_cur_max : 3;
  258. unsigned char vdd_w_curr_min : 3;
  259. // byte 10
  260. unsigned char sector_size_high : 6;
  261. unsigned char erase_blk_en : 1;
  262. unsigned char c_size_mult_low : 1;
  263. // byte 11
  264. unsigned char wp_grp_size : 7;
  265. unsigned char sector_size_low : 1;
  266. // byte 12
  267. unsigned char write_bl_len_high : 2;
  268. unsigned char r2w_factor : 3;
  269. unsigned char reserved3 : 2;
  270. unsigned char wp_grp_enable : 1;
  271. // byte 13
  272. unsigned char reserved4 : 5;
  273. unsigned char write_partial : 1;
  274. unsigned char write_bl_len_low : 2;
  275. // byte 14
  276. unsigned char reserved5: 2;
  277. unsigned char file_format : 2;
  278. unsigned char tmp_write_protect : 1;
  279. unsigned char perm_write_protect : 1;
  280. unsigned char copy : 1;
  281. /** Indicates the file format on the card */
  282. unsigned char file_format_grp : 1;
  283. // byte 15
  284. unsigned char always1 : 1;
  285. unsigned char crc : 7;
  286. } __attribute__((packed)) csd1_t;
  287. //==============================================================================
  288. /**
  289. * \class CSDV2
  290. * \brief CSD register for version 2.00 cards.
  291. */
  292. typedef struct CSDV2 {
  293. // byte 0
  294. unsigned char reserved1 : 6;
  295. unsigned char csd_ver : 2;
  296. // byte 1
  297. /** fixed to 0X0E */
  298. unsigned char taac;
  299. // byte 2
  300. /** fixed to 0 */
  301. unsigned char nsac;
  302. // byte 3
  303. unsigned char tran_speed;
  304. // byte 4
  305. unsigned char ccc_high;
  306. // byte 5
  307. /** This field is fixed to 9h, which indicates READ_BL_LEN=512 Byte */
  308. unsigned char read_bl_len : 4;
  309. unsigned char ccc_low : 4;
  310. // byte 6
  311. /** not used */
  312. unsigned char reserved2 : 4;
  313. unsigned char dsr_imp : 1;
  314. /** fixed to 0 */
  315. unsigned char read_blk_misalign : 1;
  316. /** fixed to 0 */
  317. unsigned char write_blk_misalign : 1;
  318. /** fixed to 0 - no partial read */
  319. unsigned char read_bl_partial : 1;
  320. // byte 7
  321. /** high part of card size */
  322. unsigned char c_size_high : 6;
  323. /** not used */
  324. unsigned char reserved3 : 2;
  325. // byte 8
  326. /** middle part of card size */
  327. unsigned char c_size_mid;
  328. // byte 9
  329. /** low part of card size */
  330. unsigned char c_size_low;
  331. // byte 10
  332. /** sector size is fixed at 64 KB */
  333. unsigned char sector_size_high : 6;
  334. /** fixed to 1 - erase single is supported */
  335. unsigned char erase_blk_en : 1;
  336. /** not used */
  337. unsigned char reserved4 : 1;
  338. // byte 11
  339. unsigned char wp_grp_size : 7;
  340. /** sector size is fixed at 64 KB */
  341. unsigned char sector_size_low : 1;
  342. // byte 12
  343. /** write_bl_len fixed for 512 byte blocks */
  344. unsigned char write_bl_len_high : 2;
  345. /** fixed value of 2 */
  346. unsigned char r2w_factor : 3;
  347. /** not used */
  348. unsigned char reserved5 : 2;
  349. /** fixed value of 0 - no write protect groups */
  350. unsigned char wp_grp_enable : 1;
  351. // byte 13
  352. unsigned char reserved6 : 5;
  353. /** always zero - no partial block read*/
  354. unsigned char write_partial : 1;
  355. /** write_bl_len fixed for 512 byte blocks */
  356. unsigned char write_bl_len_low : 2;
  357. // byte 14
  358. unsigned char reserved7: 2;
  359. /** Do not use always 0 */
  360. unsigned char file_format : 2;
  361. unsigned char tmp_write_protect : 1;
  362. unsigned char perm_write_protect : 1;
  363. unsigned char copy : 1;
  364. /** Do not use always 0 */
  365. unsigned char file_format_grp : 1;
  366. // byte 15
  367. /** not used always 1 */
  368. unsigned char always1 : 1;
  369. /** checksum */
  370. unsigned char crc : 7;
  371. } __attribute__((packed)) csd2_t;
  372. //==============================================================================
  373. /**
  374. * \class csd_t
  375. * \brief Union of old and new style CSD register.
  376. */
  377. union csd_t {
  378. csd1_t v1;
  379. csd2_t v2;
  380. };
  381. #endif // SdInfo_h