-
Notifications
You must be signed in to change notification settings - Fork 0
/
SdInfo.h
386 lines (378 loc) · 15 KB
/
SdInfo.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
* SdInfo.hpp
*
* Created on: Mar 20, 2023
* Author: dylan
*/
#ifndef SDINFO_H_
#define SDINFO_H_
// SD card errors
// See the SD Specification for command info.
enum {
SD_CARD_ERROR_NONE, // "No error"
SD_CARD_ERROR_CMD0, // "Card reset failed"
SD_CARD_ERROR_CMD2, // "SDIO read CID"
SD_CARD_ERROR_CMD3, // "SDIO publish RCA"
SD_CARD_ERROR_CMD6, // "Switch card function"
SD_CARD_ERROR_CMD7, // "SDIO card select"
SD_CARD_ERROR_CMD8, // "Send and check interface settings"
SD_CARD_ERROR_CMD9, // "Read CSD data"
SD_CARD_ERROR_CMD10, // "Read CID data"
SD_CARD_ERROR_CMD12, // "Stop multiple block read"
SD_CARD_ERROR_CMD13, // "Read card status"
SD_CARD_ERROR_CMD17, // "Read single block"
SD_CARD_ERROR_CMD18, // "Read multiple blocks"
SD_CARD_ERROR_CMD24, // "Write single block"
SD_CARD_ERROR_CMD25, // "Write multiple blocks"
SD_CARD_ERROR_CMD32, // "Set first erase block"
SD_CARD_ERROR_CMD33, // "Set last erase block"
SD_CARD_ERROR_CMD38, // "Erase selected blocks"
SD_CARD_ERROR_CMD58, // "Read OCR register"
SD_CARD_ERROR_CMD59, // "Set CRC mode"
SD_CARD_ERROR_ACMD6, // "Set SDIO bus width"
SD_CARD_ERROR_ACMD13, // "Read extended status"
SD_CARD_ERROR_ACMD23, // "Set pre-erased count"
SD_CARD_ERROR_ACMD41, // "Activate card initialization"
SD_CARD_ERROR_ACMD51, // "Read SCR data"
SD_CARD_ERROR_READ_TOKEN, // "Bad read data token"
SD_CARD_ERROR_READ_CRC, // "Read CRC error"
SD_CARD_ERROR_READ_FIFO, // "SDIO fifo read timeout"
SD_CARD_ERROR_READ_REG, // "Read CID or CSD failed."
SD_CARD_ERROR_READ_START, // "Bad readStart argument"
SD_CARD_ERROR_READ_TIMEOUT, // "Read data timeout"
SD_CARD_ERROR_STOP_TRAN, // "Multiple block stop failed"
SD_CARD_ERROR_TRANSFER_COMPLETE, // "SDIO transfer complete"
SD_CARD_ERROR_WRITE_DATA, // "Write data not accepted"
SD_CARD_ERROR_WRITE_FIFO, // "SDIO fifo write timeout"
SD_CARD_ERROR_WRITE_START, // "Bad writeStart argument"
SD_CARD_ERROR_WRITE_PROGRAMMING, // "Flash programming"
SD_CARD_ERROR_WRITE_TIMEOUT, // "Write timeout"
SD_CARD_ERROR_DMA, // "DMA transfer failed"
SD_CARD_ERROR_ERASE, // "Card did not accept erase commands"
SD_CARD_ERROR_ERASE_SINGLE_SECTOR, // "Card does not support erase"
SD_CARD_ERROR_ERASE_TIMEOUT, // "Erase command timeout"
SD_CARD_ERROR_INIT_NOT_CALLED, // "Card has not been initialized"
SD_CARD_ERROR_INVALID_CARD_CONFIG, // "Invalid card config"
SD_CARD_ERROR_FUNCTION_NOT_SUPPORTED, // "Unsupported SDIO command"
SD_CARD_ERROR_UNKNOWN
};
//void printSdErrorSymbol(print_t* pr, uint8_t code);
//void printSdErrorText(print_t* pr, uint8_t code);
//------------------------------------------------------------------------------
// card types
/** Standard capacity V1 SD card */
const uint8_t SD_CARD_TYPE_SD1 = 1;
/** Standard capacity V2 SD card */
const uint8_t SD_CARD_TYPE_SD2 = 2;
/** High Capacity SD card */
const uint8_t SD_CARD_TYPE_SDHC = 3;
//------------------------------------------------------------------------------
// SD operation timeouts
/** CMD0 retry count */
const uint8_t SD_CMD0_RETRY = 10;
/** command timeout ms */
const uint16_t SD_CMD_TIMEOUT = 300;
/** erase timeout ms */
const uint16_t SD_ERASE_TIMEOUT = 10000;
/** init timeout ms */
const uint16_t SD_INIT_TIMEOUT = 2000;
/** read timeout ms */
const uint16_t SD_READ_TIMEOUT = 300;
/** write time out ms */
const uint16_t SD_WRITE_TIMEOUT = 600;
//------------------------------------------------------------------------------
// SD card commands
/** GO_IDLE_STATE - init card in spi mode if CS low */
const uint8_t CMD0 = 0X00;
/** ALL_SEND_CID - Asks any card to send the CID. */
const uint8_t CMD2 = 0X02;
/** SEND_RELATIVE_ADDR - Ask the card to publish a new RCA. */
const uint8_t CMD3 = 0X03;
/** SWITCH_FUNC - Switch Function Command */
const uint8_t CMD6 = 0X06;
/** SELECT/DESELECT_CARD - toggles between the stand-by and transfer states. */
const uint8_t CMD7 = 0X07;
/** SEND_IF_COND - verify SD Memory Card interface operating condition.*/
const uint8_t CMD8 = 0X08;
/** SEND_CSD - read the Card Specific Data (CSD register) */
const uint8_t CMD9 = 0X09;
/** SEND_CID - read the card identification information (CID register) */
const uint8_t CMD10 = 0X0A;
/** VOLTAGE_SWITCH -Switch to 1.8V bus signaling level. */
const uint8_t CMD11 = 0X0B;
/** STOP_TRANSMISSION - end multiple sector read sequence */
const uint8_t CMD12 = 0X0C;
/** SEND_STATUS - read the card status register */
const uint8_t CMD13 = 0X0D;
/** READ_SINGLE_SECTOR - read a single data sector from the card */
const uint8_t CMD17 = 0X11;
/** READ_MULTIPLE_SECTOR - read multiple data sectors from the card */
const uint8_t CMD18 = 0X12;
/** WRITE_SECTOR - write a single data sector to the card */
const uint8_t CMD24 = 0X18;
/** WRITE_MULTIPLE_SECTOR - write sectors of data until a STOP_TRANSMISSION */
const uint8_t CMD25 = 0X19;
/** ERASE_WR_BLK_START - sets the address of the first sector to be erased */
const uint8_t CMD32 = 0X20;
/** ERASE_WR_BLK_END - sets the address of the last sector of the continuous
range to be erased*/
const uint8_t CMD33 = 0X21;
/** ERASE - erase all previously selected sectors */
const uint8_t CMD38 = 0X26;
/** APP_CMD - escape for application specific command */
const uint8_t CMD55 = 0X37;
/** READ_OCR - read the OCR register of a card */
const uint8_t CMD58 = 0X3A;
/** CRC_ON_OFF - enable or disable CRC checking */
const uint8_t CMD59 = 0X3B;
/** SET_BUS_WIDTH - Defines the data bus width for data transfer. */
const uint8_t ACMD6 = 0X06;
/** SD_STATUS - Send the SD Status. */
const uint8_t ACMD13 = 0X0D;
/** SET_WR_BLK_ERASE_COUNT - Set the number of write sectors to be
pre-erased before writing */
const uint8_t ACMD23 = 0X17;
/** SD_SEND_OP_COMD - Sends host capacity support information and
activates the card's initialization process */
const uint8_t ACMD41 = 0X29;
/** Reads the SD Configuration Register (SCR). */
const uint8_t ACMD51 = 0X33;
//==============================================================================
// CARD_STATUS
/** The command's argument was out of the allowed range for this card. */
const uint32_t CARD_STATUS_OUT_OF_RANGE = 1UL << 31;
/** A misaligned address which did not match the sector length. */
const uint32_t CARD_STATUS_ADDRESS_ERROR = 1UL << 30;
/** The transferred sector length is not allowed for this card. */
const uint32_t CARD_STATUS_SECTOR_LEN_ERROR = 1UL << 29;
/** An error in the sequence of erase commands occurred. */
const uint32_t CARD_STATUS_ERASE_SEQ_ERROR = 1UL <<28;
/** An invalid selection of write-sectors for erase occurred. */
const uint32_t CARD_STATUS_ERASE_PARAM = 1UL << 27;
/** Set when the host attempts to write to a protected sector. */
const uint32_t CARD_STATUS_WP_VIOLATION = 1UL << 26;
/** When set, signals that the card is locked by the host. */
const uint32_t CARD_STATUS_CARD_IS_LOCKED = 1UL << 25;
/** Set when a sequence or password error has been detected. */
const uint32_t CARD_STATUS_LOCK_UNLOCK_FAILED = 1UL << 24;
/** The CRC check of the previous command failed. */
const uint32_t CARD_STATUS_COM_CRC_ERROR = 1UL << 23;
/** Command not legal for the card state. */
const uint32_t CARD_STATUS_ILLEGAL_COMMAND = 1UL << 22;
/** Card internal ECC was applied but failed to correct the data. */
const uint32_t CARD_STATUS_CARD_ECC_FAILED = 1UL << 21;
/** Internal card controller error */
const uint32_t CARD_STATUS_CC_ERROR = 1UL << 20;
/** A general or an unknown error occurred during the operation. */
const uint32_t CARD_STATUS_ERROR = 1UL << 19;
// bits 19, 18, and 17 reserved.
/** Permanent WP set or attempt to change read only values of CSD. */
const uint32_t CARD_STATUS_CSD_OVERWRITE = 1UL <<16;
/** partial address space was erased due to write protect. */
const uint32_t CARD_STATUS_WP_ERASE_SKIP = 1UL << 15;
/** The command has been executed without using the internal ECC. */
const uint32_t CARD_STATUS_CARD_ECC_DISABLED = 1UL << 14;
/** out of erase sequence command was received. */
const uint32_t CARD_STATUS_ERASE_RESET = 1UL << 13;
/** The state of the card when receiving the command.
* 0 = idle
* 1 = ready
* 2 = ident
* 3 = stby
* 4 = tran
* 5 = data
* 6 = rcv
* 7 = prg
* 8 = dis
* 9-14 = reserved
* 15 = reserved for I/O mode
*/
const uint32_t CARD_STATUS_CURRENT_STATE = 0XF << 9;
/** Shift for current state. */
const uint32_t CARD_STATUS_CURRENT_STATE_SHIFT = 9;
/** Corresponds to buffer empty signaling on the bus. */
const uint32_t CARD_STATUS_READY_FOR_DATA = 1UL << 8;
// bit 7 reserved.
/** Extension Functions may set this bit to get host to deal with events. */
const uint32_t CARD_STATUS_FX_EVENT = 1UL << 6;
/** The card will expect ACMD, or the command has been interpreted as ACMD */
const uint32_t CARD_STATUS_APP_CMD = 1UL << 5;
// bit 4 reserved.
/** Error in the sequence of the authentication process. */
const uint32_t CARD_STATUS_AKE_SEQ_ERROR = 1UL << 3;
// bits 2,1, and 0 reserved for manufacturer test mode.
//==============================================================================
/** status for card in the ready state */
const uint8_t R1_READY_STATE = 0X00;
/** status for card in the idle state */
const uint8_t R1_IDLE_STATE = 0X01;
/** status bit for illegal command */
const uint8_t R1_ILLEGAL_COMMAND = 0X04;
/** start data token for read or write single sector*/
const uint8_t DATA_START_SECTOR = 0XFE;
/** stop token for write multiple sectors*/
const uint8_t STOP_TRAN_TOKEN = 0XFD;
/** start data token for write multiple sectors*/
const uint8_t WRITE_MULTIPLE_TOKEN = 0XFC;
/** mask for data response tokens after a write sector operation */
const uint8_t DATA_RES_MASK = 0X1F;
/** write data accepted token */
const uint8_t DATA_RES_ACCEPTED = 0X05;
//==============================================================================
/**
* \class CID
* \brief Card IDentification (CID) register.
*/
//typedef struct CID {
// // byte 0
// /** Manufacturer ID */
// uint8_t mid;
// // byte 1-2
// /** OEM/Application ID. */
// char oid[2];
// // byte 3-7
// /** Product name. */
// char pnm[5];
// // byte 8
// /** Product revision - n.m two 4-bit nibbles. */
// uint8_t prv;
// // byte 9-12
// /** Product serial 32-bit number Big Endian format. */
// uint8_t psn8[4];
// // byte 13-14
// /** Manufacturing date big endian - four nibbles RYYM Reserved Year Month. */
// uint8_t mdt[2];
// // byte 15
// /** CRC7 bits 1-7 checksum, bit 0 always 1 */
// uint8_t crc;
// // Extract big endian fields.
// /** \return major revision number. */
// int prvN() const {return prv >> 4;}
// /** \return minor revision number. */
// int prvM() const {return prv & 0XF;}
// /** \return Manufacturing Year. */
// int mdtYear() const {return 2000 + ((mdt[0] & 0XF) << 4) + (mdt[1] >> 4);}
// /** \return Manufacturing Month. */
// int mdtMonth() const {return mdt[1] & 0XF;}
// /** \return Product Serial Number. */
// uint32_t psn() const {
// return (uint32_t)psn8[0] << 24 |
// (uint32_t)psn8[1] << 16 |
// (uint32_t)psn8[2] << 8 |
// (uint32_t)psn8[3];
// }
//} __attribute__((packed)) cid_t;
//==============================================================================
/**
* \class CSD
* \brief Union of old and new style CSD register.
*/
//typedef struct CSD {
// /** union of all CSD versions */
// uint8_t csd[16];
// // Extract big endian fields.
// /** \return Capacity in sectors */
// uint32_t capacity() const {
// uint32_t c_size;
// uint8_t ver = csd[0] >> 6;
// if (ver == 0) {
// c_size = (uint32_t)(csd[6] & 3) << 10;
// c_size |= (uint32_t)csd[7] << 2 | csd[8] >> 6;
// uint8_t c_size_mult = (csd[9] & 3) << 1 | csd[10] >> 7;
// uint8_t read_bl_len = csd[5] & 15;
// return (c_size + 1) << (c_size_mult + read_bl_len + 2 - 9);
// } else if (ver == 1) {
// c_size = (uint32_t)(csd[7] & 63) << 16;
// c_size |= (uint32_t)csd[8] << 8;
// c_size |= csd[9];
// return (c_size + 1) << 10;
// } else {
// return 0;
// }
// }
// /** \return true if erase granularity is single block. */
// bool eraseSingleBlock() const {return csd[10] & 0X40;}
// /** \return erase size in 512 byte blocks if eraseSingleBlock is false. */
// int eraseSize() const {return ((csd[10] & 0X3F) << 1 | csd[11] >> 7) + 1;}
// /** \return true if the contents is copied or true if original. */
// bool copy() const {return csd[14] & 0X40;}
// /** \return true if the entire card is permanently write protected. */
// bool permWriteProtect() const {return csd[14] & 0X20;}
// /** \return true if the entire card is temporarily write protected. */
// bool tempWriteProtect() const {return csd[14] & 0X10;}
//} csd_t;
////==============================================================================
///**
// * \class SCR
// * \brief SCR register.
// */
//typedef struct SCR {
// /** Bytes 0-3 SD Association, bytes 4-7 reserved for manufacturer. */
// uint8_t scr[8];
// /** \return SCR_STRUCTURE field - must be zero.*/
// uint8_t srcStructure() {return scr[0] >> 4;}
// /** \return SD_SPEC field 0 - v1.0 or V1.01, 1 - 1.10, 2 - V2.00 or greater */
// uint8_t sdSpec() {return scr[0] & 0XF;}
// /** \return false if all zero, true if all one. */
// bool dataAfterErase() {return 0X80 & scr[1];}
// /** \return CPRM Security Version. */
// uint8_t sdSecurity() {return (scr[1] >> 4) & 0X7;}
// /** \return 0101b. */
// uint8_t sdBusWidths() {return scr[1] & 0XF;}
// /** \return true if V3.0 or greater. */
// bool sdSpec3() {return scr[2] & 0X80;}
// /** \return if true and sdSpecX is zero V4.xx. */
// bool sdSpec4() {return scr[2] & 0X4;}
// /** \return nonzero for version 5 or greater if sdSpec == 2,
// sdSpec3 == true. Version is return plus four.*/
// uint8_t sdSpecX() {return (scr[2] & 0X3) << 2 | scr[3] >> 6;}
// /** \return bit map for support CMD58/59, CMD48/49, CMD23, and CMD20 */
// uint8_t cmdSupport() {return scr[3] &0XF;}
// /** \return SD spec version */
// int16_t sdSpecVer() {
// if (sdSpec() > 2) {
// return -1;
// } else if (sdSpec() < 2) {
// return sdSpec() ? 110 : 101;
// } else if (!sdSpec3()) {
// return 200;
// } else if (!sdSpec4() && !sdSpecX()) {
// return 300;
// }
// return 400 + 100*sdSpecX();
// }
//} scr_t;
////==============================================================================
//// fields are big endian
//typedef struct SdStatus {
// //
// uint8_t busWidthSecureMode;
// uint8_t reserved1;
// // byte 2
// uint8_t sdCardType[2];
// // byte 4
// uint8_t sizeOfProtectedArea[4];
// // byte 8
// uint8_t speedClass;
// // byte 9
// uint8_t performanceMove;
// // byte 10
// uint8_t auSize;
// // byte 11
// uint8_t eraseSize[2];
// // byte 13
// uint8_t eraseTimeoutOffset;
// // byte 14
// uint8_t uhsSpeedAuSize;
// // byte 15
// uint8_t videoSpeed;
// // byte 16
// uint8_t vscAuSize[2];
// // byte 18
// uint8_t susAddr[3];
// // byte 21
// uint8_t reserved2[3];
// // byte 24
// uint8_t reservedManufacturer[40];
//} /*__attribute__((packed)) ??? */ SdStatus_t;
#endif /* SDINFO_H_ */