]> git.piffa.net Git - arduino/blob - books/pdummies/Libraries/SD/utility/SdInfo.h
gio
[arduino] / books / pdummies / Libraries / SD / utility / SdInfo.h
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 #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 2.00
30 // September 25, 2006
31 //
32 // www.sdcard.org/developers/tech/sdcard/pls/Simplified_Physical_Layer_Spec.pdf
33 //------------------------------------------------------------------------------
34 // SD card commands
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
54     range to be erased*/
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 //------------------------------------------------------------------------------
86 typedef struct CID {
87   // byte 0
88   uint8_t mid;  // Manufacturer ID
89   // byte 1-2
90   char oid[2];  // OEM/Application ID
91   // byte 3-7
92   char pnm[5];  // Product name
93   // byte 8
94   unsigned prv_m : 4;  // Product revision n.m
95   unsigned prv_n : 4;
96   // byte 9-12
97   uint32_t psn;  // Product serial number
98   // byte 13
99   unsigned mdt_year_high : 4;  // Manufacturing date
100   unsigned reserved : 4;
101   // byte 14
102   unsigned mdt_month : 4;
103   unsigned mdt_year_low :4;
104   // byte 15
105   unsigned always1 : 1;
106   unsigned crc : 7;
107 }cid_t;
108 //------------------------------------------------------------------------------
109 // CSD for version 1.00 cards
110 typedef struct CSDV1 {
111   // byte 0
112   unsigned reserved1 : 6;
113   unsigned csd_ver : 2;
114   // byte 1
115   uint8_t taac;
116   // byte 2
117   uint8_t nsac;
118   // byte 3
119   uint8_t tran_speed;
120   // byte 4
121   uint8_t ccc_high;
122   // byte 5
123   unsigned read_bl_len : 4;
124   unsigned ccc_low : 4;
125   // byte 6
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;
132   // byte 7
133   uint8_t c_size_mid;
134   // byte 8
135   unsigned vdd_r_curr_max : 3;
136   unsigned vdd_r_curr_min : 3;
137   unsigned c_size_low :2;
138   // byte 9
139   unsigned c_size_mult_high : 2;
140   unsigned vdd_w_cur_max : 3;
141   unsigned vdd_w_curr_min : 3;
142   // byte 10
143   unsigned sector_size_high : 6;
144   unsigned erase_blk_en : 1;
145   unsigned c_size_mult_low : 1;
146   // byte 11
147   unsigned wp_grp_size : 7;
148   unsigned sector_size_low : 1;
149   // byte 12
150   unsigned write_bl_len_high : 2;
151   unsigned r2w_factor : 3;
152   unsigned reserved3 : 2;
153   unsigned wp_grp_enable : 1;
154   // byte 13
155   unsigned reserved4 : 5;
156   unsigned write_partial : 1;
157   unsigned write_bl_len_low : 2;
158   // byte 14
159   unsigned reserved5: 2;
160   unsigned file_format : 2;
161   unsigned tmp_write_protect : 1;
162   unsigned perm_write_protect : 1;
163   unsigned copy : 1;
164   unsigned file_format_grp : 1;
165   // byte 15
166   unsigned always1 : 1;
167   unsigned crc : 7;
168 }csd1_t;
169 //------------------------------------------------------------------------------
170 // CSD for version 2.00 cards
171 typedef struct CSDV2 {
172   // byte 0
173   unsigned reserved1 : 6;
174   unsigned csd_ver : 2;
175   // byte 1
176   uint8_t taac;
177   // byte 2
178   uint8_t nsac;
179   // byte 3
180   uint8_t tran_speed;
181   // byte 4
182   uint8_t ccc_high;
183   // byte 5
184   unsigned read_bl_len : 4;
185   unsigned ccc_low : 4;
186   // byte 6
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;
192   // byte 7
193   unsigned reserved3 : 2;
194   unsigned c_size_high : 6;
195   // byte 8
196   uint8_t c_size_mid;
197   // byte 9
198   uint8_t c_size_low;
199   // byte 10
200   unsigned sector_size_high : 6;
201   unsigned erase_blk_en : 1;
202   unsigned reserved4 : 1;
203   // byte 11
204   unsigned wp_grp_size : 7;
205   unsigned sector_size_low : 1;
206   // byte 12
207   unsigned write_bl_len_high : 2;
208   unsigned r2w_factor : 3;
209   unsigned reserved5 : 2;
210   unsigned wp_grp_enable : 1;
211   // byte 13
212   unsigned reserved6 : 5;
213   unsigned write_partial : 1;
214   unsigned write_bl_len_low : 2;
215   // byte 14
216   unsigned reserved7: 2;
217   unsigned file_format : 2;
218   unsigned tmp_write_protect : 1;
219   unsigned perm_write_protect : 1;
220   unsigned copy : 1;
221   unsigned file_format_grp : 1;
222   // byte 15
223   unsigned always1 : 1;
224   unsigned crc : 7;
225 }csd2_t;
226 //------------------------------------------------------------------------------
227 // union of old and new style CSD register
228 union csd_t {
229   csd1_t v1;
230   csd2_t v2;
231 };
232 #endif  // SdInfo_h