Skip to content

Commit d791229

Browse files
author
Deepika Bhavnani
authored
Merge pull request #107 from ARMmbed/david_init_ref_count
Add init reference count
2 parents c8ae38f + a08e5ce commit d791229

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

SDBlockDevice.cpp

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@
250250

251251
SDBlockDevice::SDBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName cs, uint64_t hz, bool crc_on)
252252
: _sectors(0), _spi(mosi, miso, sclk), _cs(cs), _is_initialized(0),
253-
_crc_on(crc_on), _crc16(0, 0, false, false)
253+
_crc_on(crc_on), _init_ref_count(0), _crc16(0, 0, false, false)
254254
{
255255
_cs = 1;
256256
_card_type = SDCARD_NONE;
@@ -363,9 +363,21 @@ int SDBlockDevice::_initialise_card()
363363

364364
int SDBlockDevice::init()
365365
{
366+
int err;
367+
366368
lock();
367369

368-
int err = _initialise_card();
370+
if (!_is_initialized) {
371+
_init_ref_count = 0;
372+
}
373+
374+
_init_ref_count++;
375+
376+
if (_init_ref_count != 1) {
377+
goto end;
378+
}
379+
380+
err = _initialise_card();
369381
_is_initialized = (err == BD_ERROR_OK);
370382
if (!_is_initialized) {
371383
debug_if(SD_DBG, "Fail to initialize card\n");
@@ -393,17 +405,33 @@ int SDBlockDevice::init()
393405
unlock();
394406
return err;
395407
}
408+
409+
end:
396410
unlock();
397411
return BD_ERROR_OK;
398412
}
399413

400414
int SDBlockDevice::deinit()
401415
{
402416
lock();
417+
418+
if (!_is_initialized) {
419+
_init_ref_count = 0;
420+
goto end;
421+
}
422+
423+
_init_ref_count--;
424+
425+
if (_init_ref_count) {
426+
goto end;
427+
}
428+
403429
_is_initialized = false;
404430
_sectors = 0;
431+
432+
end:
405433
unlock();
406-
return 0;
434+
return BD_ERROR_OK;
407435
}
408436

409437

SDBlockDevice.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ class SDBlockDevice : public BlockDevice {
226226
bool _is_initialized;
227227
bool _dbg;
228228
bool _crc_on;
229+
uint32_t _init_ref_count;
229230

230231
MbedCRC<POLY_7BIT_SD, 7> _crc7;
231232
MbedCRC<POLY_16BIT_CCITT, 16> _crc16;

0 commit comments

Comments
 (0)