Skip to content

Commit 03e4b2d

Browse files
gekysimonqhughes
authored andcommitted
bd: Adopted the block device api in the SDBlockDevice
1 parent f096628 commit 03e4b2d

File tree

5 files changed

+371
-171
lines changed

5 files changed

+371
-171
lines changed

features/filesystem/bd/HeapBlockDevice.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ class HeapBlockDevice : public BlockDevice {
5858
*
5959
* @return 0 on success or a negative error code on failure
6060
*/
61-
virtual bd_error_t init() = 0;
61+
virtual bd_error_t init();
6262

6363
/** Deinitialize a block device
6464
*
6565
* @return 0 on success or a negative error code on failure
6666
*/
67-
virtual bd_error_t deinit() = 0;
67+
virtual bd_error_t deinit();
6868

6969
/** Read blocks from a block device
7070
*
@@ -73,7 +73,7 @@ class HeapBlockDevice : public BlockDevice {
7373
* @param size Size to read in bytes, must be a multiple of read block size
7474
* @return 0 on success, negative error code on failure
7575
*/
76-
virtual bd_error_t read(void *buffer, bd_addr_t addr, bd_size_t size) = 0;
76+
virtual bd_error_t read(void *buffer, bd_addr_t addr, bd_size_t size);
7777

7878
/** Program blocks to a block device
7979
*

features/filesystem/sd/SDFileSystem.cpp renamed to features/filesystem/sd/SDBlockDevice.cpp

Lines changed: 117 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -116,22 +116,30 @@
116116
/* If the target has no SPI support then SDCard is not supported */
117117
#ifdef DEVICE_SPI
118118

119-
#include "SDFileSystem.h"
119+
#include "SDBlockDevice.h"
120120
#include "mbed_debug.h"
121121

122122
#define SD_COMMAND_TIMEOUT 5000
123123

124124
#define SD_DBG 0
125125

126-
SDFileSystem::SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name) :
127-
FATFileSystem(name), _spi(mosi, miso, sclk), _cs(cs), _is_initialized(0) {
126+
SDBlockDevice::SDBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName cs)
127+
: _spi(mosi, miso, sclk), _cs(cs), _is_initialized(0)
128+
{
128129
_cs = 1;
129130

130131
// Set default to 100kHz for initialisation and 1MHz for data transfer
131132
_init_sck = 100000;
132133
_transfer_sck = 1000000;
133134
}
134135

136+
SDBlockDevice::~SDBlockDevice()
137+
{
138+
if (_is_initialized) {
139+
deinit();
140+
}
141+
}
142+
135143
#define R1_IDLE_STATE (1 << 0)
136144
#define R1_ERASE_RESET (1 << 1)
137145
#define R1_ILLEGAL_COMMAND (1 << 2)
@@ -150,8 +158,9 @@ SDFileSystem::SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs,
150158
#define SDCARD_V2 2
151159
#define SDCARD_V2HC 3
152160

153-
int SDFileSystem::initialise_card() {
154-
_dbg = SD_DBG;
161+
bd_error_t SDBlockDevice::_initialise_card()
162+
{
163+
_dbg = SD_DBG;
155164
// Set to SCK for initialisation, and clock card with cs = 1
156165
_spi.lock();
157166
_spi.frequency(_init_sck);
@@ -164,145 +173,183 @@ int SDFileSystem::initialise_card() {
164173
// send CMD0, should return with all zeros except IDLE STATE set (bit 0)
165174
if (_cmd(0, 0) != R1_IDLE_STATE) {
166175
debug_if(_dbg, "No disk, or could not put SD card in to SPI idle state\n");
167-
return SDCARD_FAIL;
176+
return BD_ERROR_NO_DEVICE;
168177
}
169178

170179
// send CMD8 to determine whther it is ver 2.x
171180
int r = _cmd8();
172181
if (r == R1_IDLE_STATE) {
173-
return initialise_card_v2();
182+
return _initialise_card_v2();
174183
} else if (r == (R1_IDLE_STATE | R1_ILLEGAL_COMMAND)) {
175-
return initialise_card_v1();
184+
return _initialise_card_v1();
176185
} else {
177186
debug_if(_dbg, "Not in idle state after sending CMD8 (not an SD card?)\n");
178-
return SDCARD_FAIL;
187+
return BD_ERROR_DEVICE_ERROR;
179188
}
180189
}
181190

182-
int SDFileSystem::initialise_card_v1() {
191+
bd_error_t SDBlockDevice::_initialise_card_v1()
192+
{
183193
for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
184194
_cmd(55, 0);
185195
if (_cmd(41, 0) == 0) {
186-
cdv = 512;
196+
_block_size = 512;
187197
debug_if(_dbg, "\n\rInit: SEDCARD_V1\n\r");
188-
return SDCARD_V1;
198+
return BD_ERROR_OK;
189199
}
190200
}
191201

192202
debug_if(_dbg, "Timeout waiting for v1.x card\n");
193-
return SDCARD_FAIL;
203+
return BD_ERROR_DEVICE_ERROR;
194204
}
195205

196-
int SDFileSystem::initialise_card_v2() {
206+
bd_error_t SDBlockDevice::_initialise_card_v2()
207+
{
197208
for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
198209
wait_ms(50);
199210
_cmd58();
200211
_cmd(55, 0);
201212
if (_cmd(41, 0x40000000) == 0) {
202213
_cmd58();
203214
debug_if(_dbg, "\n\rInit: SDCARD_V2\n\r");
204-
cdv = 1;
205-
return SDCARD_V2;
215+
_block_size = 1;
216+
return BD_ERROR_OK;
206217
}
207218
}
208219

209220
debug_if(_dbg, "Timeout waiting for v2.x card\n");
210-
return SDCARD_FAIL;
221+
return BD_ERROR_DEVICE_ERROR;
211222
}
212223

213-
int SDFileSystem::disk_initialize() {
214-
lock();
215-
_is_initialized = initialise_card();
216-
if (_is_initialized == 0) {
224+
bd_error_t SDBlockDevice::init()
225+
{
226+
_lock.lock();
227+
bd_error_t err = _initialise_card();
228+
_is_initialized = (err == BD_ERROR_OK);
229+
if (!_is_initialized) {
217230
debug_if(_dbg, "Fail to initialize card\n");
218-
unlock();
219-
return 1;
231+
_lock.unlock();
232+
return err;
220233
}
221234
debug_if(_dbg, "init card = %d\n", _is_initialized);
222235
_sectors = _sd_sectors();
223236

224237
// Set block length to 512 (CMD16)
225238
if (_cmd(16, 512) != 0) {
226239
debug_if(_dbg, "Set 512-byte block timed out\n");
227-
unlock();
228-
return 1;
240+
_lock.unlock();
241+
return BD_ERROR_DEVICE_ERROR;
229242
}
230243

231244
// Set SCK for data transfer
232245
_spi.frequency(_transfer_sck);
233-
unlock();
246+
_lock.unlock();
247+
return BD_ERROR_OK;
248+
}
249+
250+
bd_error_t SDBlockDevice::deinit()
251+
{
234252
return 0;
235253
}
236254

237-
int SDFileSystem::disk_write(const uint8_t* buffer, uint32_t block_number, uint32_t count) {
238-
lock();
255+
bd_error_t SDBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size)
256+
{
257+
if (!is_valid_program(addr, size)) {
258+
return BD_ERROR_PARAMETER;
259+
}
260+
261+
_lock.lock();
239262
if (!_is_initialized) {
240-
unlock();
241-
return -1;
263+
_lock.unlock();
264+
return BD_ERROR_NO_INIT;
242265
}
243-
244-
for (uint32_t b = block_number; b < block_number + count; b++) {
266+
267+
const uint8_t *buffer = static_cast<const uint8_t*>(b);
268+
while (size > 0) {
269+
bd_addr_t block = addr / 512;
245270
// set write address for single block (CMD24)
246-
if (_cmd(24, b * cdv) != 0) {
247-
unlock();
248-
return 1;
271+
if (_cmd(24, block * _block_size) != 0) {
272+
_lock.unlock();
273+
return BD_ERROR_DEVICE_ERROR;
249274
}
250-
275+
251276
// send the data block
252277
_write(buffer, 512);
253278
buffer += 512;
279+
addr += 512;
280+
size -= 512;
254281
}
255-
256-
unlock();
282+
_lock.unlock();
257283
return 0;
258284
}
259285

260-
int SDFileSystem::disk_read(uint8_t* buffer, uint32_t block_number, uint32_t count) {
261-
lock();
286+
bd_error_t SDBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size)
287+
{
288+
if (!is_valid_read(addr, size)) {
289+
return BD_ERROR_PARAMETER;
290+
}
291+
292+
_lock.lock();
262293
if (!_is_initialized) {
263-
unlock();
264-
return -1;
294+
_lock.unlock();
295+
return BD_ERROR_PARAMETER;
265296
}
266297

267-
for (uint32_t b = block_number; b < block_number + count; b++) {
298+
uint8_t *buffer = static_cast<uint8_t *>(b);
299+
while (size > 0) {
300+
bd_addr_t block = addr / 512;
268301
// set read address for single block (CMD17)
269-
if (_cmd(17, b * cdv) != 0) {
270-
unlock();
271-
return 1;
302+
if (_cmd(17, block * _block_size) != 0) {
303+
_lock.unlock();
304+
return BD_ERROR_DEVICE_ERROR;
272305
}
273306

274307
// receive the data
275308
_read(buffer, 512);
276309
buffer += 512;
310+
addr += 512;
311+
size -= 512;
277312
}
313+
_lock.unlock();
314+
return 0;
315+
}
278316

279-
unlock();
317+
bd_error_t SDBlockDevice::erase(bd_addr_t addr, bd_size_t size)
318+
{
280319
return 0;
281320
}
282321

283-
int SDFileSystem::disk_status() {
284-
lock();
285-
// FATFileSystem::disk_status() returns 0 when initialized
286-
int ret = _is_initialized ? 0 : 1;
287-
unlock();
288-
return ret;
322+
bd_size_t SDBlockDevice::get_read_size()
323+
{
324+
return 512;
325+
}
326+
327+
bd_size_t SDBlockDevice::get_program_size()
328+
{
329+
return 512;
330+
}
331+
332+
bd_size_t SDBlockDevice::get_erase_size()
333+
{
334+
return 512;
289335
}
290336

291-
int SDFileSystem::disk_sync() { return 0; }
292-
uint32_t SDFileSystem::disk_sectors() {
293-
lock();
294-
uint32_t sectors = _sectors;
295-
unlock();
296-
return sectors;
337+
bd_size_t SDBlockDevice::size()
338+
{
339+
_lock.lock();
340+
bd_size_t sectors = _sectors;
341+
_lock.unlock();
342+
return 512*sectors;
297343
}
298344

299-
void SDFileSystem::debug(bool dbg){
345+
void SDBlockDevice::debug(bool dbg)
346+
{
300347
_dbg = dbg;
301348
}
302349

303350

304351
// PRIVATE FUNCTIONS
305-
int SDFileSystem::_cmd(int cmd, int arg) {
352+
int SDBlockDevice::_cmd(int cmd, int arg) {
306353
_spi.lock();
307354
_cs = 0;
308355

@@ -329,7 +376,7 @@ int SDFileSystem::_cmd(int cmd, int arg) {
329376
_spi.unlock();
330377
return -1; // timeout
331378
}
332-
int SDFileSystem::_cmdx(int cmd, int arg) {
379+
int SDBlockDevice::_cmdx(int cmd, int arg) {
333380
_spi.lock();
334381
_cs = 0;
335382

@@ -357,7 +404,7 @@ int SDFileSystem::_cmdx(int cmd, int arg) {
357404
}
358405

359406

360-
int SDFileSystem::_cmd58() {
407+
int SDBlockDevice::_cmd58() {
361408
_spi.lock();
362409
_cs = 0;
363410
int arg = 0;
@@ -390,7 +437,7 @@ int SDFileSystem::_cmd58() {
390437
return -1; // timeout
391438
}
392439

393-
int SDFileSystem::_cmd8() {
440+
int SDBlockDevice::_cmd8() {
394441
_spi.lock();
395442
_cs = 0;
396443

@@ -422,7 +469,7 @@ int SDFileSystem::_cmd8() {
422469
return -1; // timeout
423470
}
424471

425-
int SDFileSystem::_read(uint8_t *buffer, uint32_t length) {
472+
int SDBlockDevice::_read(uint8_t *buffer, uint32_t length) {
426473
_spi.lock();
427474
_cs = 0;
428475

@@ -442,7 +489,7 @@ int SDFileSystem::_read(uint8_t *buffer, uint32_t length) {
442489
return 0;
443490
}
444491

445-
int SDFileSystem::_write(const uint8_t*buffer, uint32_t length) {
492+
int SDBlockDevice::_write(const uint8_t*buffer, uint32_t length) {
446493
_spi.lock();
447494
_cs = 0;
448495

@@ -488,7 +535,7 @@ static uint32_t ext_bits(unsigned char *data, int msb, int lsb) {
488535
return bits;
489536
}
490537

491-
uint32_t SDFileSystem::_sd_sectors() {
538+
uint32_t SDBlockDevice::_sd_sectors() {
492539
uint32_t c_size, c_size_mult, read_bl_len;
493540
uint32_t block_len, mult, blocknr, capacity;
494541
uint32_t hc_c_size;
@@ -515,7 +562,7 @@ uint32_t SDFileSystem::_sd_sectors() {
515562

516563
switch (csd_structure) {
517564
case 0:
518-
cdv = 512;
565+
_block_size = 512;
519566
c_size = ext_bits(csd, 73, 62);
520567
c_size_mult = ext_bits(csd, 49, 47);
521568
read_bl_len = ext_bits(csd, 83, 80);
@@ -525,11 +572,11 @@ uint32_t SDFileSystem::_sd_sectors() {
525572
blocknr = (c_size + 1) * mult;
526573
capacity = blocknr * block_len;
527574
blocks = capacity / 512;
528-
debug_if(_dbg, "\n\rSDCard\n\rc_size: %d \n\rcapacity: %ld \n\rsectors: %lld\n\r", c_size, capacity, blocks);
575+
debug_if(_dbg, "\n\rSDBlockDevice\n\rc_size: %d \n\rcapacity: %ld \n\rsectors: %lld\n\r", c_size, capacity, blocks);
529576
break;
530577

531578
case 1:
532-
cdv = 1;
579+
_block_size = 1;
533580
hc_c_size = ext_bits(csd, 63, 48);
534581
blocks = (hc_c_size+1)*1024;
535582
debug_if(_dbg, "\n\rSDHC Card \n\rhc_c_size: %d\n\rcapacity: %lld \n\rsectors: %lld\n\r", hc_c_size, blocks*512, blocks);

0 commit comments

Comments
 (0)