Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

304 rindas
9.7KB

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