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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
use crate::crc::Crc7;
use crate::result::{Error, Result};
use crate::{const_try, lib_bitfield};
pub use super::{
BlockLength, CSD_LEN, CardCommandClass, Copied, DsrImplemented, EraseBlockEnable, FileFormat,
FileFormatGroup, NsAccessCycles, PermanentWriteProtect, ReadBlockMisalign, ReadBlockPartial,
SectorSize, TemporaryWriteProtect, TransferSpeed, VddCurrentMax, VddCurrentMin,
WriteBlockMisalign, WriteBlockPartial, WriteProtectGroupEnable, WriteProtectGroupSize,
WriteSpeedFactor,
};
pub use super::v1::c_size::{self, CapacitySize};
pub use super::v1::c_size_mult::{self, CapacitySizeMult};
pub use super::v1::taac::{self, TimeAsyncAccessCycles};
mod content_prot_app;
mod csd_structure;
mod ecc;
mod erase_grp_mult;
mod erase_grp_size;
mod spec_vers;
pub use content_prot_app::ContentProtectionApp;
pub use csd_structure::CsdStructure;
pub use ecc::Ecc;
pub use erase_grp_mult::EraseGroupMult;
pub use erase_grp_size::EraseGroupSize;
pub use spec_vers::SpecificationVersion;
#[cfg(test)]
mod tests;
lib_bitfield! {
/// Represents the Card-Specific Data (CSD) register V1.0 value returned in a `R2` response in SD mode.
///
/// This is for standard capacity SD memory cards.
pub CsdMmc(MSB0 [u8; CSD_LEN]): u16 {
/// Represents the raw `CSD_STRUCTURE` field for the [CsdMmc].
raw_csd_structure: 127, 126;
/// Represents the raw `SPEC_VERS` field for the [CsdMmc].
raw_spec_vers: 125, 122;
/// Represents the raw `TAAC` field for the [CsdMmc].
raw_taac: 119, 112;
/// Represents the raw `NSAC` field for the [CsdMmc].
raw_nsac: 111, 104;
/// Represents the raw `TRAN_SPEED` field for the [CsdMmc].
raw_tran_speed: 103, 96;
/// Represents the raw `CCC` field for the [CsdMmc].
raw_ccc: 95, 84;
/// Represents the raw `READ_BL_LEN` field for the [CsdMmc].
raw_read_bl_len: 83, 80;
/// Represents the raw `READ_BL_PARTIAL` field for the [CsdMmc].
raw_read_bl_partial: 79;
/// Represents the raw `WRITE_BLK_MISALIGN` field for the [CsdMmc].
raw_write_blk_misalign: 78;
/// Represents the raw `READ_BLK_MISALIGN` field for the [CsdMmc].
raw_read_blk_misalign: 77;
/// Represents the raw `DSR_IMPL` field for the [CsdMmc].
raw_dsr_imp: 76;
/// Represents the raw `C_SIZE` field for the [CsdMmc].
raw_c_size: 73, 62;
/// Represents the raw `VDD_R_CURR_MIN` field for the [CsdMmc].
raw_vdd_r_curr_min: 61, 59;
/// Represents the raw `VDD_R_CURR_MAX` field for the [CsdMmc].
raw_vdd_r_curr_max: 58, 56;
/// Represents the raw `VDD_W_CURR_MIN` field for the [CsdMmc].
raw_vdd_w_curr_min: 55, 53;
/// Represents the raw `VDD_W_CURR_MAX` field for the [CsdMmc].
raw_vdd_w_curr_max: 52, 50;
/// Represents the raw `C_SIZE_MULT` field for the [CsdMmc].
raw_c_size_mult: 49, 47;
/// Represents the raw `ERASE_BLK_EN` field for the [CsdMmc].
raw_erase_grp_size: 46, 42;
/// Represents the raw `SECTOR_SIZE` field for the [CsdMmc].
raw_erase_grp_mult: 41, 37;
/// Represents the raw `WR_GRP_SIZE` field for the [CsdMmc].
raw_wp_grp_size: 36, 32;
/// Represents the raw `WR_GRP_ENABLE` field for the [CsdMmc].
raw_wp_grp_enable: 31;
/// Represents the raw `DEFAULT_ECC` field for the [CsdMmc].
raw_default_ecc: 30, 29;
/// Represents the raw `R2W_FACTOR` field for the [CsdMmc].
raw_r2w_factor: 28, 26;
/// Represents the raw `WRITE_BL_LEN` field for the [CsdMmc].
raw_write_bl_len: 25, 22;
/// Represents the raw `WRITE_BL_PARTIAL` field for the [CsdMmc].
raw_write_bl_partial: 21;
/// Represents the raw `CONTENT_PROT_APP` field for the [CsdMmc].
raw_content_prot_app: 16;
/// Represents the raw `FILE_FORMAT_GRP` field for the [CsdMmc].
raw_file_format_grp: 15;
/// Represents the raw `COPY` field for the [CsdMmc].
raw_copy: 14;
/// Represents the raw `PERM_WRITE_PROTECT` field for the [CsdMmc].
raw_perm_write_protect: 13;
/// Represents the raw `TMP_WRITE_PROTECT` field for the [CsdMmc].
raw_tmp_write_protect: 12;
/// Represents the raw `RAW_FILE_FORMAT` field for the [CsdMmc].
raw_file_format: 11, 10;
/// Represents the raw `ECC` field for the [CsdMmc].
raw_ecc: 9, 8;
/// Represents the raw `CRC` field for the [CsdMmc].
raw_crc: 7, 1;
/// Represents the raw `END` field for the [CsdMmc].
raw_end: 0;
}
}
impl CsdMmc {
/// Represents the byte length of the [CsdMmc].
pub const LEN: usize = CSD_LEN;
/// Represents the default inner representation of the [CsdMmc].
#[rustfmt::skip]
pub const DEFAULT: [u8; Self::LEN] = [
0x90, 0x08, 0x00, 0x32, 0x50, 0x59, 0x80, 0x00,
0x00, 0x00, 0x00, 0x00, 0x02, 0x40, 0x00, 0x99,
];
/// Creates a new [CsdMmc].
pub const fn new() -> Self {
Self(Self::DEFAULT)
}
/// Gets the byte length of the [CsdMmc].
pub const fn len(&self) -> usize {
Self::LEN
}
/// Gets whether the [CsdMmc] is empty: always false.
pub const fn is_empty(&self) -> bool {
false
}
/// Attempts to get the `CSD_STRUCTURE` field for the [CsdMmc].
pub const fn csd_structure(&self) -> CsdStructure {
CsdStructure::from_inner(self.raw_csd_structure() as u8)
}
/// Attempts to get the `SPEC_VERS` field for the [CsdMmc].
pub const fn spec_vers(&self) -> Result<SpecificationVersion> {
SpecificationVersion::try_from_inner(self.raw_spec_vers() as u8)
}
/// Attempts to get the `TAAC` field for the [CsdMmc].
pub const fn taac(&self) -> Result<TimeAsyncAccessCycles> {
TimeAsyncAccessCycles::try_from_inner(self.raw_taac() as u8)
}
/// Gets the `NSAC` field for the [CsdMmc].
pub const fn nsac(&self) -> NsAccessCycles {
NsAccessCycles::from_bits(self.raw_nsac() as u8)
}
/// Attempts to get the `TRAN_SPEED` field for the [CsdMmc].
pub const fn tran_speed(&self) -> Result<TransferSpeed> {
TransferSpeed::try_from_bits(self.raw_tran_speed() as u8)
}
/// Gets the `CCC` field for the [CsdMmc].
pub const fn ccc(&self) -> CardCommandClass {
CardCommandClass::from_bits(self.raw_ccc())
}
/// Attempts to get the `READ_BL_LEN` field for the [CsdMmc].
pub const fn read_bl_len(&self) -> Result<BlockLength> {
BlockLength::from_raw(self.raw_read_bl_len() as u8)
}
/// Gets the `READ_BL_PARTIAL` field for the [CsdMmc].
///
/// For SD cards, this bit is always set.
///
/// Indicates partial block reads are allowed, down to a minimum one-byte read.
pub const fn read_bl_partial(&self) -> ReadBlockPartial {
ReadBlockPartial::from_inner(self.raw_read_bl_partial())
}
/// Gets the `WRITE_BLK_MISALIGN` field for the [CsdMmc].
///
/// If set, indicates crossing physical block boundaries is allowed.
/// Otherwise, crossing physical block boundaries is invalid.
pub const fn write_blk_misalign(&self) -> WriteBlockMisalign {
WriteBlockMisalign::from_inner(self.raw_write_blk_misalign())
}
/// Gets the `READ_BLK_MISALIGN` field for the [CsdMmc].
///
/// If set, indicates crossing physical block boundaries is allowed.
/// Otherwise, crossing physical block boundaries is invalid.
pub const fn read_blk_misalign(&self) -> ReadBlockMisalign {
ReadBlockMisalign::from_inner(self.raw_read_blk_misalign())
}
/// Gets the `DSR_IMP` field for the [CsdMmc].
///
/// Indicates if the configurable driver stage is integrated on the card.
///
/// If set, the `DSR` register is implemented.
/// Otherwise, the `DSR` register does not exist.
pub const fn dsr_imp(&self) -> DsrImplemented {
DsrImplemented::from_inner(self.raw_dsr_imp())
}
/// Gets the `C_SIZE` field for the [CsdMmc].
///
/// Represents one of the parameters used to calculate the card's total memory capacity.
pub const fn c_size(&self) -> CapacitySize {
CapacitySize::from_inner(self.raw_c_size())
}
/// Attempts to get the `VDD_R_CURR_MIN` field for the [CsdMmc].
pub const fn vdd_r_curr_min(&self) -> Result<VddCurrentMin> {
VddCurrentMin::from_raw(self.raw_vdd_r_curr_min() as u8)
}
/// Attempts to get the `VDD_R_CURR_MIN` field for the [CsdMmc].
pub const fn vdd_r_curr_max(&self) -> Result<VddCurrentMax> {
VddCurrentMax::from_raw(self.raw_vdd_r_curr_max() as u8)
}
/// Attempts to get the `VDD_W_CURR_MIN` field for the [CsdMmc].
pub const fn vdd_w_curr_min(&self) -> Result<VddCurrentMin> {
VddCurrentMin::from_raw(self.raw_vdd_w_curr_min() as u8)
}
/// Attempts to get the `VDD_W_CURR_MIN` field for the [CsdMmc].
pub const fn vdd_w_curr_max(&self) -> Result<VddCurrentMax> {
VddCurrentMax::from_raw(self.raw_vdd_w_curr_max() as u8)
}
/// Gets the `C_SIZE_MULT` field for the [CsdMmc].
///
/// Represents one of the parameters used to calculate the card's total memory capacity.
pub const fn c_size_mult(&self) -> CapacitySizeMult {
CapacitySizeMult::from_inner(self.raw_c_size_mult() as u8)
}
/// Gets the total memory capacity of the SD card.
///
/// The memory capacity is calculated as follows:
///
/// ```no_build,no_run
/// // C_SIZE_MULT < 8
/// MULT = 2^(C_SIZE_MULT + 2)
///
/// BLOCKNR = (C_SIZE + 1) * MULT
///
/// // READ_BL_LEN < 12
/// BLOCK_LEN = 2^READ_BL_LEN
///
/// MEMORY_CAPACITY = BLOCKNR * BLOCK_LEN
/// ```
pub const fn capacity(&self) -> Result<usize> {
match self.read_bl_len() {
Ok(r) => Ok(self
.c_size()
.size()
.saturating_mul(self.c_size_mult().mult())
.saturating_mul(r.block_len())),
Err(err) => Err(err),
}
}
/// Gets the `ERASE_GRP_SIZE` field for the [CsdMmc].
pub const fn erase_grp_size(&self) -> EraseGroupSize {
EraseGroupSize::from_inner(self.raw_erase_grp_size() as u8)
}
/// Gets the `ERASE_GRP_MULT` field for the [CsdMmc].
pub const fn erase_grp_mult(&self) -> EraseGroupMult {
EraseGroupMult::from_inner(self.raw_erase_grp_mult() as u8)
}
/// Gets the erase unit size for the [CsdMmc].
///
/// # Note
///
/// The unit size is calculated with the algorithm:
///
/// ```no_build,no_run
/// unit = (ERASE_GRP_SIZE + 1) * (ERASE_GRP_MULT + 1)
/// ```
pub const fn erase_size(&self) -> usize {
self.erase_grp_size().unit_size(self.erase_grp_mult())
}
/// Gets the `WP_GRP_SIZE` field for the [CsdMmc].
///
/// The raw value is a 7-bit binary encoded value.
pub const fn wp_grp_size(&self) -> WriteProtectGroupSize {
WriteProtectGroupSize::from_inner(self.raw_wp_grp_size() as u8)
}
/// Gets the `WP_GRP_ENABLE` field for the [CsdMmc].
///
/// If unset (`false`, `0`), no group write protection is possible.
pub const fn wp_grp_enable(&self) -> WriteProtectGroupEnable {
WriteProtectGroupEnable::from_inner(self.raw_wp_grp_enable())
}
/// Gets the `DEFAULT_ECC` field for the [CsdMmc].
pub const fn default_ecc(&self) -> Result<Ecc> {
Ecc::try_from_inner(self.raw_default_ecc() as u8)
}
/// Attempts to get the `R2W_FACTOR` field for the [CsdMmc].
pub const fn r2w_factor(&self) -> Result<WriteSpeedFactor> {
WriteSpeedFactor::try_from_inner(self.raw_r2w_factor() as u8)
}
/// Attempts to get the `WRITE_BL_LEN` field for the [CsdMmc].
pub const fn write_bl_len(&self) -> Result<BlockLength> {
BlockLength::try_from_inner(self.raw_write_bl_len() as u8)
}
/// Gets the `WRITE_BL_PARTIAL` field for the [CsdMmc].
///
/// - If unset (`false`, `0`), partial block writes are not possible.
/// - If set (`true`, `1`), partial block writes are available down to a 1-byte minimum write.
pub const fn write_bl_partial(&self) -> WriteBlockPartial {
WriteBlockPartial::from_inner(self.raw_write_bl_partial())
}
/// Gets the `CONTENT_PROT_APP` field for the [CsdMmc].
pub const fn content_prot_app(&self) -> ContentProtectionApp {
ContentProtectionApp::from_inner(self.raw_content_prot_app())
}
/// Gets the `FILE_FORMAT_GRP` field for the [CsdMmc].
///
/// - If unset (`false`, `0`), the `FILE_FORMAT` field values are defined by [FileFormat].
/// - If set (`true`, `1`), the `FILE_FORMAT` field values are reserved for future use.
pub const fn file_format_grp(&self) -> FileFormatGroup {
FileFormatGroup::from_inner(self.raw_file_format_grp())
}
/// Gets the `COPY` field for the [CsdMmc].
///
/// - If unset (`false`, `0`), the contents are the original.
/// - If set (`true`, `1`), the contents have been copied.
pub const fn copy(&self) -> Copied {
Copied::from_inner(self.raw_copy())
}
/// Gets the `PERM_WRITE_PROTECT` field for the [CsdMmc].
///
/// - If unset (`false`, `0`), permanent write protect is disabled.
/// - If set (`true`, `1`), permanent write protect is enabled.
pub const fn perm_write_protect(&self) -> PermanentWriteProtect {
PermanentWriteProtect::from_inner(self.raw_perm_write_protect())
}
/// Gets the `TMP_WRITE_PROTECT` field for the [CsdMmc].
///
/// - If unset (`false`, `0`), temporary write protect is disabled.
/// - If set (`true`, `1`), temporary write protect is enabled.
pub const fn tmp_write_protect(&self) -> TemporaryWriteProtect {
TemporaryWriteProtect::from_inner(self.raw_tmp_write_protect())
}
/// Gets the `FILE_FORMAT` field for the [CsdMmc].
///
/// - If `FILE_FORMAT_GRP` is unset (`false`, `0`), values are defined by [FileFormat].
/// - If `FILE_FORMAT_GRP` is set (`true`, `1`), values are reserved for future use.
pub const fn file_format(&self) -> Result<FileFormat> {
match self.file_format_grp() {
FileFormatGroup::FileFormat => FileFormat::try_from_inner(self.raw_file_format() as u8),
FileFormatGroup::Reserved => Ok(FileFormat::Unknown),
}
}
/// Gets the `ECC` field for the [CsdMmc].
pub const fn ecc(&self) -> Result<Ecc> {
Ecc::try_from_inner(self.raw_ecc() as u8)
}
/// Gets the `CRC` field of the [CsdMmc].
pub const fn crc(&self) -> Crc7 {
Crc7::from_bits(self.raw_crc() as u8)
}
/// Attempts to convert a byte array into a [CsdMmc].
pub const fn try_from_inner(val: [u8; Self::LEN]) -> Result<Self> {
let csd = Self(val);
const_try!(csd.spec_vers());
const_try!(csd.taac());
const_try!(csd.tran_speed());
const_try!(csd.read_bl_len());
const_try!(csd.vdd_r_curr_min());
const_try!(csd.vdd_r_curr_max());
const_try!(csd.vdd_w_curr_min());
const_try!(csd.vdd_w_curr_max());
const_try!(csd.default_ecc());
const_try!(csd.r2w_factor());
const_try!(csd.write_bl_len());
const_try!(csd.file_format());
const_try!(csd.ecc());
Ok(csd)
}
/// Attempts to convert a byte slice into a [CsdMmc].
pub const fn try_from_bytes(val: &[u8]) -> Result<Self> {
match val.len() {
len if len < Self::LEN => Err(Error::invalid_length(len, Self::LEN)),
_ => Self::try_from_inner([
val[0], val[1], val[2], val[3], val[4], val[5], val[6], val[7], val[8], val[9],
val[10], val[11], val[12], val[13], val[14], val[15],
]),
}
}
/// Converts a [CsdMmc] into an byte array.
pub const fn into_inner(self) -> [u8; Self::LEN] {
self.0
}
}
impl Default for CsdMmc {
fn default() -> Self {
Self::new()
}
}
impl TryFrom<[u8; CsdMmc::LEN]> for CsdMmc {
type Error = Error;
fn try_from(val: [u8; Self::LEN]) -> Result<Self> {
Self::try_from_inner(val)
}
}
impl TryFrom<&[u8]> for CsdMmc {
type Error = Error;
fn try_from(val: &[u8]) -> Result<Self> {
Self::try_from_bytes(val)
}
}
impl From<CsdMmc> for [u8; CsdMmc::LEN] {
fn from(val: CsdMmc) -> Self {
val.into_inner()
}
}