@@ -57,19 +57,14 @@ where
57
57
CS : embedded_hal:: digital:: v2:: OutputPin ,
58
58
<SPI as embedded_hal:: blocking:: spi:: Transfer < u8 > >:: Error : core:: fmt:: Debug ,
59
59
{
60
- /// Create a new SD/MMC interface using a raw SPI interface.
60
+ /// Create a new SD/MMC Card driver using a raw SPI interface.
61
+ ///
62
+ /// Uses the default options.
61
63
pub fn new ( spi : SPI , cs : CS ) -> SdCard < SPI , CS > {
62
- SdCard {
63
- inner : RefCell :: new ( SdCardInner {
64
- spi,
65
- cs,
66
- card_type : CardType :: Unknown ,
67
- options : AcquireOpts { require_crc : true } ,
68
- } ) ,
69
- }
64
+ Self :: new_with_options ( spi, cs, AcquireOpts :: default ( ) )
70
65
}
71
66
72
- /// Construct a new SD/MMC interface , using the given options.
67
+ /// Construct a new SD/MMC Card driver , using a raw SPI interface and the given options.
73
68
pub fn new_with_options ( spi : SPI , cs : CS , options : AcquireOpts ) -> SdCard < SPI , CS > {
74
69
SdCard {
75
70
inner : RefCell :: new ( SdCardInner {
@@ -105,6 +100,14 @@ where
105
100
inner. check_init ( ) ?;
106
101
inner. erase_single_block_enabled ( )
107
102
}
103
+
104
+ /// Mark the card as requiring a reset.
105
+ ///
106
+ /// The next operation will assume the card has been freshly inserted.
107
+ pub fn mark_card_uninit ( & self ) {
108
+ let mut inner = self . inner . borrow_mut ( ) ;
109
+ inner. card_type = CardType :: Unknown ;
110
+ }
108
111
}
109
112
110
113
impl < SPI , CS > BlockDevice for SdCard < SPI , CS >
@@ -120,16 +123,23 @@ where
120
123
& self ,
121
124
blocks : & mut [ Block ] ,
122
125
start_block_idx : BlockIdx ,
123
- _reason : & str ,
126
+ reason : & str ,
124
127
) -> Result < ( ) , Self :: Error > {
125
128
let mut inner = self . inner . borrow_mut ( ) ;
129
+ debug ! (
130
+ "Read {} blocks @ {} for {}" ,
131
+ blocks. len( ) ,
132
+ start_block_idx. 0 ,
133
+ reason
134
+ ) ;
126
135
inner. check_init ( ) ?;
127
- inner. read ( blocks, start_block_idx, _reason )
136
+ inner. read ( blocks, start_block_idx)
128
137
}
129
138
130
139
/// Write one or more blocks, starting at the given block index.
131
140
fn write ( & self , blocks : & [ Block ] , start_block_idx : BlockIdx ) -> Result < ( ) , Self :: Error > {
132
141
let mut inner = self . inner . borrow_mut ( ) ;
142
+ debug ! ( "Writing {} blocks @ {}" , blocks. len( ) , start_block_idx. 0 ) ;
133
143
inner. check_init ( ) ?;
134
144
inner. write ( blocks, start_block_idx)
135
145
}
@@ -138,7 +148,7 @@ where
138
148
fn num_blocks ( & self ) -> Result < BlockCount , Self :: Error > {
139
149
let mut inner = self . inner . borrow_mut ( ) ;
140
150
inner. check_init ( ) ?;
141
- inner. num_blocks ( )
151
+ inner. card_size_blocks ( )
142
152
}
143
153
}
144
154
@@ -164,12 +174,7 @@ where
164
174
<SPI as embedded_hal:: blocking:: spi:: Transfer < u8 > >:: Error : core:: fmt:: Debug ,
165
175
{
166
176
/// Read one or more blocks, starting at the given block index.
167
- fn read (
168
- & mut self ,
169
- blocks : & mut [ Block ] ,
170
- start_block_idx : BlockIdx ,
171
- _reason : & str ,
172
- ) -> Result < ( ) , Error > {
177
+ fn read ( & mut self , blocks : & mut [ Block ] , start_block_idx : BlockIdx ) -> Result < ( ) , Error > {
173
178
let start_idx = match self . card_type {
174
179
CardType :: SD1 | CardType :: SD2 => start_block_idx. 0 * 512 ,
175
180
CardType :: Sdhc => start_block_idx. 0 ,
@@ -228,16 +233,23 @@ where
228
233
}
229
234
230
235
/// Determine how many blocks this device can hold.
231
- fn num_blocks ( & mut self ) -> Result < BlockCount , Error > {
232
- let num_bytes = self . card_size_bytes ( ) ?;
233
- let num_blocks = ( num_bytes / 512 ) as u32 ;
236
+ fn card_size_blocks ( & mut self ) -> Result < BlockCount , Error > {
237
+ let num_blocks = self . with_chip_select ( |s| {
238
+ let csd = s. read_csd ( ) ?;
239
+ debug ! ( "CSD: {:?}" , csd) ;
240
+ match csd {
241
+ Csd :: V1 ( ref contents) => Ok ( contents. card_capacity_blocks ( ) ) ,
242
+ Csd :: V2 ( ref contents) => Ok ( contents. card_capacity_blocks ( ) ) ,
243
+ }
244
+ } ) ?;
234
245
Ok ( BlockCount ( num_blocks) )
235
246
}
236
247
237
248
/// Return the usable size of this SD card in bytes.
238
249
fn card_size_bytes ( & mut self ) -> Result < u64 , Error > {
239
250
self . with_chip_select ( |s| {
240
251
let csd = s. read_csd ( ) ?;
252
+ debug ! ( "CSD: {:?}" , csd) ;
241
253
match csd {
242
254
Csd :: V1 ( ref contents) => Ok ( contents. card_capacity_bytes ( ) ) ,
243
255
Csd :: V2 ( ref contents) => Ok ( contents. card_capacity_bytes ( ) ) ,
0 commit comments