SdInfo.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* Arduino Sd2Card Library
  2. * Copyright (C) 2009 by William Greiman
  3. *
  4. * This file is part of the Arduino Sd2Card 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 Sd2Card Library. If not, see
  18. * <http://www.gnu.org/licenses/>.
  19. */
  20. #include "Marlin.h"
  21. #ifdef SDSUPPORT
  22. #ifndef SdInfo_h
  23. #define SdInfo_h
  24. #include <stdint.h>
  25. // Based on the document:
  26. //
  27. // SD Specifications
  28. // Part 1
  29. // Physical Layer
  30. // Simplified Specification
  31. // Version 3.01
  32. // May 18, 2010
  33. //
  34. // http://www.sdcard.org/developers/tech/sdcard/pls/simplified_specs
  35. //------------------------------------------------------------------------------
  36. // SD card commands
  37. /** GO_IDLE_STATE - init card in spi mode if CS low */
  38. uint8_t const CMD0 = 0X00;
  39. /** SEND_IF_COND - verify SD Memory Card interface operating condition.*/
  40. uint8_t const CMD8 = 0X08;
  41. /** SEND_CSD - read the Card Specific Data (CSD register) */
  42. uint8_t const CMD9 = 0X09;
  43. /** SEND_CID - read the card identification information (CID register) */
  44. uint8_t const CMD10 = 0X0A;
  45. /** STOP_TRANSMISSION - end multiple block read sequence */
  46. uint8_t const CMD12 = 0X0C;
  47. /** SEND_STATUS - read the card status register */
  48. uint8_t const CMD13 = 0X0D;
  49. /** READ_SINGLE_BLOCK - read a single data block from the card */
  50. uint8_t const CMD17 = 0X11;
  51. /** READ_MULTIPLE_BLOCK - read a multiple data blocks from the card */
  52. uint8_t const CMD18 = 0X12;
  53. /** WRITE_BLOCK - write a single data block to the card */
  54. uint8_t const CMD24 = 0X18;
  55. /** WRITE_MULTIPLE_BLOCK - write blocks of data until a STOP_TRANSMISSION */
  56. uint8_t const CMD25 = 0X19;
  57. /** ERASE_WR_BLK_START - sets the address of the first block to be erased */
  58. uint8_t const CMD32 = 0X20;
  59. /** ERASE_WR_BLK_END - sets the address of the last block of the continuous
  60. range to be erased*/
  61. uint8_t const CMD33 = 0X21;
  62. /** ERASE - erase all previously selected blocks */
  63. uint8_t const CMD38 = 0X26;
  64. /** APP_CMD - escape for application specific command */
  65. uint8_t const CMD55 = 0X37;
  66. /** READ_OCR - read the OCR register of a card */
  67. uint8_t const CMD58 = 0X3A;
  68. /** SET_WR_BLK_ERASE_COUNT - Set the number of write blocks to be
  69. pre-erased before writing */
  70. uint8_t const ACMD23 = 0X17;
  71. /** SD_SEND_OP_COMD - Sends host capacity support information and
  72. activates the card's initialization process */
  73. uint8_t const ACMD41 = 0X29;
  74. //------------------------------------------------------------------------------
  75. /** status for card in the ready state */
  76. uint8_t const R1_READY_STATE = 0X00;
  77. /** status for card in the idle state */
  78. uint8_t const R1_IDLE_STATE = 0X01;
  79. /** status bit for illegal command */
  80. uint8_t const R1_ILLEGAL_COMMAND = 0X04;
  81. /** start data token for read or write single block*/
  82. uint8_t const DATA_START_BLOCK = 0XFE;
  83. /** stop token for write multiple blocks*/
  84. uint8_t const STOP_TRAN_TOKEN = 0XFD;
  85. /** start data token for write multiple blocks*/
  86. uint8_t const WRITE_MULTIPLE_TOKEN = 0XFC;
  87. /** mask for data response tokens after a write block operation */
  88. uint8_t const DATA_RES_MASK = 0X1F;
  89. /** write data accepted token */
  90. uint8_t const DATA_RES_ACCEPTED = 0X05;
  91. //------------------------------------------------------------------------------
  92. /** Card IDentification (CID) register */
  93. typedef struct CID {
  94. // byte 0
  95. /** Manufacturer ID */
  96. unsigned char mid;
  97. // byte 1-2
  98. /** OEM/Application ID */
  99. char oid[2];
  100. // byte 3-7
  101. /** Product name */
  102. char pnm[5];
  103. // byte 8
  104. /** Product revision least significant digit */
  105. unsigned char prv_m : 4;
  106. /** Product revision most significant digit */
  107. unsigned char prv_n : 4;
  108. // byte 9-12
  109. /** Product serial number */
  110. uint32_t psn;
  111. // byte 13
  112. /** Manufacturing date year low digit */
  113. unsigned char mdt_year_high : 4;
  114. /** not used */
  115. unsigned char reserved : 4;
  116. // byte 14
  117. /** Manufacturing date month */
  118. unsigned char mdt_month : 4;
  119. /** Manufacturing date year low digit */
  120. unsigned char mdt_year_low :4;
  121. // byte 15
  122. /** not used always 1 */
  123. unsigned char always1 : 1;
  124. /** CRC7 checksum */
  125. unsigned char crc : 7;
  126. }cid_t;
  127. //------------------------------------------------------------------------------
  128. /** CSD for version 1.00 cards */
  129. typedef struct CSDV1 {
  130. // byte 0
  131. unsigned char reserved1 : 6;
  132. unsigned char csd_ver : 2;
  133. // byte 1
  134. unsigned char taac;
  135. // byte 2
  136. unsigned char nsac;
  137. // byte 3
  138. unsigned char tran_speed;
  139. // byte 4
  140. unsigned char ccc_high;
  141. // byte 5
  142. unsigned char read_bl_len : 4;
  143. unsigned char ccc_low : 4;
  144. // byte 6
  145. unsigned char c_size_high : 2;
  146. unsigned char reserved2 : 2;
  147. unsigned char dsr_imp : 1;
  148. unsigned char read_blk_misalign :1;
  149. unsigned char write_blk_misalign : 1;
  150. unsigned char read_bl_partial : 1;
  151. // byte 7
  152. unsigned char c_size_mid;
  153. // byte 8
  154. unsigned char vdd_r_curr_max : 3;
  155. unsigned char vdd_r_curr_min : 3;
  156. unsigned char c_size_low :2;
  157. // byte 9
  158. unsigned char c_size_mult_high : 2;
  159. unsigned char vdd_w_cur_max : 3;
  160. unsigned char vdd_w_curr_min : 3;
  161. // byte 10
  162. unsigned char sector_size_high : 6;
  163. unsigned char erase_blk_en : 1;
  164. unsigned char c_size_mult_low : 1;
  165. // byte 11
  166. unsigned char wp_grp_size : 7;
  167. unsigned char sector_size_low : 1;
  168. // byte 12
  169. unsigned char write_bl_len_high : 2;
  170. unsigned char r2w_factor : 3;
  171. unsigned char reserved3 : 2;
  172. unsigned char wp_grp_enable : 1;
  173. // byte 13
  174. unsigned char reserved4 : 5;
  175. unsigned char write_partial : 1;
  176. unsigned char write_bl_len_low : 2;
  177. // byte 14
  178. unsigned char reserved5: 2;
  179. unsigned char file_format : 2;
  180. unsigned char tmp_write_protect : 1;
  181. unsigned char perm_write_protect : 1;
  182. unsigned char copy : 1;
  183. /** Indicates the file format on the card */
  184. unsigned char file_format_grp : 1;
  185. // byte 15
  186. unsigned char always1 : 1;
  187. unsigned char crc : 7;
  188. }csd1_t;
  189. //------------------------------------------------------------------------------
  190. /** CSD for version 2.00 cards */
  191. typedef struct CSDV2 {
  192. // byte 0
  193. unsigned char reserved1 : 6;
  194. unsigned char csd_ver : 2;
  195. // byte 1
  196. /** fixed to 0X0E */
  197. unsigned char taac;
  198. // byte 2
  199. /** fixed to 0 */
  200. unsigned char nsac;
  201. // byte 3
  202. unsigned char tran_speed;
  203. // byte 4
  204. unsigned char ccc_high;
  205. // byte 5
  206. /** This field is fixed to 9h, which indicates READ_BL_LEN=512 Byte */
  207. unsigned char read_bl_len : 4;
  208. unsigned char ccc_low : 4;
  209. // byte 6
  210. /** not used */
  211. unsigned char reserved2 : 4;
  212. unsigned char dsr_imp : 1;
  213. /** fixed to 0 */
  214. unsigned char read_blk_misalign :1;
  215. /** fixed to 0 */
  216. unsigned char write_blk_misalign : 1;
  217. /** fixed to 0 - no partial read */
  218. unsigned char read_bl_partial : 1;
  219. // byte 7
  220. /** not used */
  221. unsigned char reserved3 : 2;
  222. /** high part of card size */
  223. unsigned char c_size_high : 6;
  224. // byte 8
  225. /** middle part of card size */
  226. unsigned char c_size_mid;
  227. // byte 9
  228. /** low part of card size */
  229. unsigned char c_size_low;
  230. // byte 10
  231. /** sector size is fixed at 64 KB */
  232. unsigned char sector_size_high : 6;
  233. /** fixed to 1 - erase single is supported */
  234. unsigned char erase_blk_en : 1;
  235. /** not used */
  236. unsigned char reserved4 : 1;
  237. // byte 11
  238. unsigned char wp_grp_size : 7;
  239. /** sector size is fixed at 64 KB */
  240. unsigned char sector_size_low : 1;
  241. // byte 12
  242. /** write_bl_len fixed for 512 byte blocks */
  243. unsigned char write_bl_len_high : 2;
  244. /** fixed value of 2 */
  245. unsigned char r2w_factor : 3;
  246. /** not used */
  247. unsigned char reserved5 : 2;
  248. /** fixed value of 0 - no write protect groups */
  249. unsigned char wp_grp_enable : 1;
  250. // byte 13
  251. unsigned char reserved6 : 5;
  252. /** always zero - no partial block read*/
  253. unsigned char write_partial : 1;
  254. /** write_bl_len fixed for 512 byte blocks */
  255. unsigned char write_bl_len_low : 2;
  256. // byte 14
  257. unsigned char reserved7: 2;
  258. /** Do not use always 0 */
  259. unsigned char file_format : 2;
  260. unsigned char tmp_write_protect : 1;
  261. unsigned char perm_write_protect : 1;
  262. unsigned char copy : 1;
  263. /** Do not use always 0 */
  264. unsigned char file_format_grp : 1;
  265. // byte 15
  266. /** not used always 1 */
  267. unsigned char always1 : 1;
  268. /** checksum */
  269. unsigned char crc : 7;
  270. }csd2_t;
  271. //------------------------------------------------------------------------------
  272. /** union of old and new style CSD register */
  273. union csd_t {
  274. csd1_t v1;
  275. csd2_t v2;
  276. };
  277. #endif // SdInfo_h
  278. #endif