mirror of https://github.com/ARMmbed/mbed-os.git
commit
d791229918
|
|
@ -250,7 +250,7 @@
|
||||||
|
|
||||||
SDBlockDevice::SDBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName cs, uint64_t hz, bool crc_on)
|
SDBlockDevice::SDBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName cs, uint64_t hz, bool crc_on)
|
||||||
: _sectors(0), _spi(mosi, miso, sclk), _cs(cs), _is_initialized(0),
|
: _sectors(0), _spi(mosi, miso, sclk), _cs(cs), _is_initialized(0),
|
||||||
_crc_on(crc_on), _crc16(0, 0, false, false)
|
_crc_on(crc_on), _init_ref_count(0), _crc16(0, 0, false, false)
|
||||||
{
|
{
|
||||||
_cs = 1;
|
_cs = 1;
|
||||||
_card_type = SDCARD_NONE;
|
_card_type = SDCARD_NONE;
|
||||||
|
|
@ -363,9 +363,21 @@ int SDBlockDevice::_initialise_card()
|
||||||
|
|
||||||
int SDBlockDevice::init()
|
int SDBlockDevice::init()
|
||||||
{
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
lock();
|
lock();
|
||||||
|
|
||||||
int err = _initialise_card();
|
if (!_is_initialized) {
|
||||||
|
_init_ref_count = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
_init_ref_count++;
|
||||||
|
|
||||||
|
if (_init_ref_count != 1) {
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
|
||||||
|
err = _initialise_card();
|
||||||
_is_initialized = (err == BD_ERROR_OK);
|
_is_initialized = (err == BD_ERROR_OK);
|
||||||
if (!_is_initialized) {
|
if (!_is_initialized) {
|
||||||
debug_if(SD_DBG, "Fail to initialize card\n");
|
debug_if(SD_DBG, "Fail to initialize card\n");
|
||||||
|
|
@ -393,6 +405,8 @@ int SDBlockDevice::init()
|
||||||
unlock();
|
unlock();
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
end:
|
||||||
unlock();
|
unlock();
|
||||||
return BD_ERROR_OK;
|
return BD_ERROR_OK;
|
||||||
}
|
}
|
||||||
|
|
@ -400,10 +414,24 @@ int SDBlockDevice::init()
|
||||||
int SDBlockDevice::deinit()
|
int SDBlockDevice::deinit()
|
||||||
{
|
{
|
||||||
lock();
|
lock();
|
||||||
|
|
||||||
|
if (!_is_initialized) {
|
||||||
|
_init_ref_count = 0;
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
|
||||||
|
_init_ref_count--;
|
||||||
|
|
||||||
|
if (_init_ref_count) {
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
|
||||||
_is_initialized = false;
|
_is_initialized = false;
|
||||||
_sectors = 0;
|
_sectors = 0;
|
||||||
|
|
||||||
|
end:
|
||||||
unlock();
|
unlock();
|
||||||
return 0;
|
return BD_ERROR_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -226,6 +226,7 @@ private:
|
||||||
bool _is_initialized;
|
bool _is_initialized;
|
||||||
bool _dbg;
|
bool _dbg;
|
||||||
bool _crc_on;
|
bool _crc_on;
|
||||||
|
uint32_t _init_ref_count;
|
||||||
|
|
||||||
MbedCRC<POLY_7BIT_SD, 7> _crc7;
|
MbedCRC<POLY_7BIT_SD, 7> _crc7;
|
||||||
MbedCRC<POLY_16BIT_CCITT, 16> _crc16;
|
MbedCRC<POLY_16BIT_CCITT, 16> _crc16;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue