SdInfo.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. /** Toshiba FlashAir: iSDIO */
  65. uint8_t const CMD48 = 0x30;
  66. /** Toshiba FlashAir: iSDIO */
  67. uint8_t const CMD49 = 0x31;
  68. /** APP_CMD - escape for application specific command */
  69. uint8_t const CMD55 = 0X37;
  70. /** READ_OCR - read the OCR register of a card */
  71. uint8_t const CMD58 = 0X3A;
  72. /** SET_WR_BLK_ERASE_COUNT - Set the number of write blocks to be
  73. pre-erased before writing */
  74. uint8_t const ACMD23 = 0X17;
  75. /** SD_SEND_OP_COMD - Sends host capacity support information and
  76. activates the card's initialization process */
  77. uint8_t const ACMD41 = 0X29;
  78. //------------------------------------------------------------------------------
  79. /** status for card in the ready state */
  80. uint8_t const R1_READY_STATE = 0X00;
  81. /** status for card in the idle state */
  82. uint8_t const R1_IDLE_STATE = 0X01;
  83. /** status bit for illegal command */
  84. uint8_t const R1_ILLEGAL_COMMAND = 0X04;
  85. /** start data token for read or write single block*/
  86. uint8_t const DATA_START_BLOCK = 0XFE;
  87. /** stop token for write multiple blocks*/
  88. uint8_t const STOP_TRAN_TOKEN = 0XFD;
  89. /** start data token for write multiple blocks*/
  90. uint8_t const WRITE_MULTIPLE_TOKEN = 0XFC;
  91. /** mask for data response tokens after a write block operation */
  92. uint8_t const DATA_RES_MASK = 0X1F;
  93. /** write data accepted token */
  94. uint8_t const DATA_RES_ACCEPTED = 0X05;
  95. //------------------------------------------------------------------------------
  96. /** Card IDentification (CID) register */
  97. typedef struct CID {
  98. // byte 0
  99. /** Manufacturer ID */
  100. unsigned char mid;
  101. // byte 1-2
  102. /** OEM/Application ID */
  103. char oid[2];
  104. // byte 3-7
  105. /** Product name */
  106. char pnm[5];
  107. // byte 8
  108. /** Product revision least significant digit */
  109. unsigned char prv_m : 4;
  110. /** Product revision most significant digit */
  111. unsigned char prv_n : 4;
  112. // byte 9-12
  113. /** Product serial number */
  114. uint32_t psn;
  115. // byte 13
  116. /** Manufacturing date year low digit */
  117. unsigned char mdt_year_high : 4;
  118. /** not used */
  119. unsigned char reserved : 4;
  120. // byte 14
  121. /** Manufacturing date month */
  122. unsigned char mdt_month : 4;
  123. /** Manufacturing date year low digit */
  124. unsigned char mdt_year_low :4;
  125. // byte 15
  126. /** not used always 1 */
  127. unsigned char always1 : 1;
  128. /** CRC7 checksum */
  129. unsigned char crc : 7;
  130. }cid_t;
  131. //------------------------------------------------------------------------------
  132. /** CSD for version 1.00 cards */
  133. typedef struct CSDV1 {
  134. // byte 0
  135. unsigned char reserved1 : 6;
  136. unsigned char csd_ver : 2;
  137. // byte 1
  138. unsigned char taac;
  139. // byte 2
  140. unsigned char nsac;
  141. // byte 3
  142. unsigned char tran_speed;
  143. // byte 4
  144. unsigned char ccc_high;
  145. // byte 5
  146. unsigned char read_bl_len : 4;
  147. unsigned char ccc_low : 4;
  148. // byte 6
  149. unsigned char c_size_high : 2;
  150. unsigned char reserved2 : 2;
  151. unsigned char dsr_imp : 1;
  152. unsigned char read_blk_misalign :1;
  153. unsigned char write_blk_misalign : 1;
  154. unsigned char read_bl_partial : 1;
  155. // byte 7
  156. unsigned char c_size_mid;
  157. // byte 8
  158. unsigned char vdd_r_curr_max : 3;
  159. unsigned char vdd_r_curr_min : 3;
  160. unsigned char c_size_low :2;
  161. // byte 9
  162. unsigned char c_size_mult_high : 2;
  163. unsigned char vdd_w_cur_max : 3;
  164. unsigned char vdd_w_curr_min : 3;
  165. // byte 10
  166. unsigned char sector_size_high : 6;
  167. unsigned char erase_blk_en : 1;
  168. unsigned char c_size_mult_low : 1;
  169. // byte 11
  170. unsigned char wp_grp_size : 7;
  171. unsigned char sector_size_low : 1;
  172. // byte 12
  173. unsigned char write_bl_len_high : 2;
  174. unsigned char r2w_factor : 3;
  175. unsigned char reserved3 : 2;
  176. unsigned char wp_grp_enable : 1;
  177. // byte 13
  178. unsigned char reserved4 : 5;
  179. unsigned char write_partial : 1;
  180. unsigned char write_bl_len_low : 2;
  181. // byte 14
  182. unsigned char reserved5: 2;
  183. unsigned char file_format : 2;
  184. unsigned char tmp_write_protect : 1;
  185. unsigned char perm_write_protect : 1;
  186. unsigned char copy : 1;
  187. /** Indicates the file format on the card */
  188. unsigned char file_format_grp : 1;
  189. // byte 15
  190. unsigned char always1 : 1;
  191. unsigned char crc : 7;
  192. }csd1_t;
  193. //------------------------------------------------------------------------------
  194. /** CSD for version 2.00 cards */
  195. typedef struct CSDV2 {
  196. // byte 0
  197. unsigned char reserved1 : 6;
  198. unsigned char csd_ver : 2;
  199. // byte 1
  200. /** fixed to 0X0E */
  201. unsigned char taac;
  202. // byte 2
  203. /** fixed to 0 */
  204. unsigned char nsac;
  205. // byte 3
  206. unsigned char tran_speed;
  207. // byte 4
  208. unsigned char ccc_high;
  209. // byte 5
  210. /** This field is fixed to 9h, which indicates READ_BL_LEN=512 Byte */
  211. unsigned char read_bl_len : 4;
  212. unsigned char ccc_low : 4;
  213. // byte 6
  214. /** not used */
  215. unsigned char reserved2 : 4;
  216. unsigned char dsr_imp : 1;
  217. /** fixed to 0 */
  218. unsigned char read_blk_misalign :1;
  219. /** fixed to 0 */
  220. unsigned char write_blk_misalign : 1;
  221. /** fixed to 0 - no partial read */
  222. unsigned char read_bl_partial : 1;
  223. // byte 7
  224. /** not used */
  225. unsigned char reserved3 : 2;
  226. /** high part of card size */
  227. unsigned char c_size_high : 6;
  228. // byte 8
  229. /** middle part of card size */
  230. unsigned char c_size_mid;
  231. // byte 9
  232. /** low part of card size */
  233. unsigned char c_size_low;
  234. // byte 10
  235. /** sector size is fixed at 64 KB */
  236. unsigned char sector_size_high : 6;
  237. /** fixed to 1 - erase single is supported */
  238. unsigned char erase_blk_en : 1;
  239. /** not used */
  240. unsigned char reserved4 : 1;
  241. // byte 11
  242. unsigned char wp_grp_size : 7;
  243. /** sector size is fixed at 64 KB */
  244. unsigned char sector_size_low : 1;
  245. // byte 12
  246. /** write_bl_len fixed for 512 byte blocks */
  247. unsigned char write_bl_len_high : 2;
  248. /** fixed value of 2 */
  249. unsigned char r2w_factor : 3;
  250. /** not used */
  251. unsigned char reserved5 : 2;
  252. /** fixed value of 0 - no write protect groups */
  253. unsigned char wp_grp_enable : 1;
  254. // byte 13
  255. unsigned char reserved6 : 5;
  256. /** always zero - no partial block read*/
  257. unsigned char write_partial : 1;
  258. /** write_bl_len fixed for 512 byte blocks */
  259. unsigned char write_bl_len_low : 2;
  260. // byte 14
  261. unsigned char reserved7: 2;
  262. /** Do not use always 0 */
  263. unsigned char file_format : 2;
  264. unsigned char tmp_write_protect : 1;
  265. unsigned char perm_write_protect : 1;
  266. unsigned char copy : 1;
  267. /** Do not use always 0 */
  268. unsigned char file_format_grp : 1;
  269. // byte 15
  270. /** not used always 1 */
  271. unsigned char always1 : 1;
  272. /** checksum */
  273. unsigned char crc : 7;
  274. }csd2_t;
  275. //------------------------------------------------------------------------------
  276. /** union of old and new style CSD register */
  277. union csd_t {
  278. csd1_t v1;
  279. csd2_t v2;
  280. };
  281. #endif // SdInfo_h
  282. #endif