1 /* Arduino Sd2Card Library
2 * Copyright (C) 2009 by William Greiman
4 * This file is part of the Arduino Sd2Card Library
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.
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.
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/>.
23 // Based on the document:
28 // Simplified Specification
32 // www.sdcard.org/developers/tech/sdcard/pls/Simplified_Physical_Layer_Spec.pdf
33 //------------------------------------------------------------------------------
35 /** GO_IDLE_STATE - init card in spi mode if CS low */
36 uint8_t const CMD0 = 0X00;
37 /** SEND_IF_COND - verify SD Memory Card interface operating condition.*/
38 uint8_t const CMD8 = 0X08;
39 /** SEND_CSD - read the Card Specific Data (CSD register) */
40 uint8_t const CMD9 = 0X09;
41 /** SEND_CID - read the card identification information (CID register) */
42 uint8_t const CMD10 = 0X0A;
43 /** SEND_STATUS - read the card status register */
44 uint8_t const CMD13 = 0X0D;
45 /** READ_BLOCK - read a single data block from the card */
46 uint8_t const CMD17 = 0X11;
47 /** WRITE_BLOCK - write a single data block to the card */
48 uint8_t const CMD24 = 0X18;
49 /** WRITE_MULTIPLE_BLOCK - write blocks of data until a STOP_TRANSMISSION */
50 uint8_t const CMD25 = 0X19;
51 /** ERASE_WR_BLK_START - sets the address of the first block to be erased */
52 uint8_t const CMD32 = 0X20;
53 /** ERASE_WR_BLK_END - sets the address of the last block of the continuous
55 uint8_t const CMD33 = 0X21;
56 /** ERASE - erase all previously selected blocks */
57 uint8_t const CMD38 = 0X26;
58 /** APP_CMD - escape for application specific command */
59 uint8_t const CMD55 = 0X37;
60 /** READ_OCR - read the OCR register of a card */
61 uint8_t const CMD58 = 0X3A;
62 /** SET_WR_BLK_ERASE_COUNT - Set the number of write blocks to be
63 pre-erased before writing */
64 uint8_t const ACMD23 = 0X17;
65 /** SD_SEND_OP_COMD - Sends host capacity support information and
66 activates the card's initialization process */
67 uint8_t const ACMD41 = 0X29;
68 //------------------------------------------------------------------------------
69 /** status for card in the ready state */
70 uint8_t const R1_READY_STATE = 0X00;
71 /** status for card in the idle state */
72 uint8_t const R1_IDLE_STATE = 0X01;
73 /** status bit for illegal command */
74 uint8_t const R1_ILLEGAL_COMMAND = 0X04;
75 /** start data token for read or write single block*/
76 uint8_t const DATA_START_BLOCK = 0XFE;
77 /** stop token for write multiple blocks*/
78 uint8_t const STOP_TRAN_TOKEN = 0XFD;
79 /** start data token for write multiple blocks*/
80 uint8_t const WRITE_MULTIPLE_TOKEN = 0XFC;
81 /** mask for data response tokens after a write block operation */
82 uint8_t const DATA_RES_MASK = 0X1F;
83 /** write data accepted token */
84 uint8_t const DATA_RES_ACCEPTED = 0X05;
85 //------------------------------------------------------------------------------
88 uint8_t mid; // Manufacturer ID
90 char oid[2]; // OEM/Application ID
92 char pnm[5]; // Product name
94 unsigned prv_m : 4; // Product revision n.m
97 uint32_t psn; // Product serial number
99 unsigned mdt_year_high : 4; // Manufacturing date
100 unsigned reserved : 4;
102 unsigned mdt_month : 4;
103 unsigned mdt_year_low :4;
105 unsigned always1 : 1;
108 //------------------------------------------------------------------------------
109 // CSD for version 1.00 cards
110 typedef struct CSDV1 {
112 unsigned reserved1 : 6;
113 unsigned csd_ver : 2;
123 unsigned read_bl_len : 4;
124 unsigned ccc_low : 4;
126 unsigned c_size_high : 2;
127 unsigned reserved2 : 2;
128 unsigned dsr_imp : 1;
129 unsigned read_blk_misalign :1;
130 unsigned write_blk_misalign : 1;
131 unsigned read_bl_partial : 1;
135 unsigned vdd_r_curr_max : 3;
136 unsigned vdd_r_curr_min : 3;
137 unsigned c_size_low :2;
139 unsigned c_size_mult_high : 2;
140 unsigned vdd_w_cur_max : 3;
141 unsigned vdd_w_curr_min : 3;
143 unsigned sector_size_high : 6;
144 unsigned erase_blk_en : 1;
145 unsigned c_size_mult_low : 1;
147 unsigned wp_grp_size : 7;
148 unsigned sector_size_low : 1;
150 unsigned write_bl_len_high : 2;
151 unsigned r2w_factor : 3;
152 unsigned reserved3 : 2;
153 unsigned wp_grp_enable : 1;
155 unsigned reserved4 : 5;
156 unsigned write_partial : 1;
157 unsigned write_bl_len_low : 2;
159 unsigned reserved5: 2;
160 unsigned file_format : 2;
161 unsigned tmp_write_protect : 1;
162 unsigned perm_write_protect : 1;
164 unsigned file_format_grp : 1;
166 unsigned always1 : 1;
169 //------------------------------------------------------------------------------
170 // CSD for version 2.00 cards
171 typedef struct CSDV2 {
173 unsigned reserved1 : 6;
174 unsigned csd_ver : 2;
184 unsigned read_bl_len : 4;
185 unsigned ccc_low : 4;
187 unsigned reserved2 : 4;
188 unsigned dsr_imp : 1;
189 unsigned read_blk_misalign :1;
190 unsigned write_blk_misalign : 1;
191 unsigned read_bl_partial : 1;
193 unsigned reserved3 : 2;
194 unsigned c_size_high : 6;
200 unsigned sector_size_high : 6;
201 unsigned erase_blk_en : 1;
202 unsigned reserved4 : 1;
204 unsigned wp_grp_size : 7;
205 unsigned sector_size_low : 1;
207 unsigned write_bl_len_high : 2;
208 unsigned r2w_factor : 3;
209 unsigned reserved5 : 2;
210 unsigned wp_grp_enable : 1;
212 unsigned reserved6 : 5;
213 unsigned write_partial : 1;
214 unsigned write_bl_len_low : 2;
216 unsigned reserved7: 2;
217 unsigned file_format : 2;
218 unsigned tmp_write_protect : 1;
219 unsigned perm_write_protect : 1;
221 unsigned file_format_grp : 1;
223 unsigned always1 : 1;
226 //------------------------------------------------------------------------------
227 // union of old and new style CSD register