Skip to content

Commit 685101f

Browse files
authored
Merge pull request #9418 from dhalbert/sdcardio-deinit-check
shared-module/sdcardio/SDCard.c: check for deinit more thoroughly
2 parents 079a5f5 + 48dd9e3 commit 685101f

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

shared-module/sdcardio/SDCard.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,15 @@
3232
#define TOKEN_STOP_TRAN (0xFD)
3333
#define TOKEN_DATA (0xFE)
3434

35+
static void common_hal_sdcardio_check_for_deinit(sdcardio_sdcard_obj_t *self) {
36+
if (!self->bus) {
37+
raise_deinited_error();
38+
}
39+
}
40+
3541
static bool lock_and_configure_bus(sdcardio_sdcard_obj_t *self) {
42+
common_hal_sdcardio_check_for_deinit(self);
43+
3644
if (!common_hal_busio_spi_try_lock(self->bus)) {
3745
return false;
3846
}
@@ -316,12 +324,6 @@ void common_hal_sdcardio_sdcard_deinit(sdcardio_sdcard_obj_t *self) {
316324
common_hal_digitalio_digitalinout_deinit(&self->cs);
317325
}
318326

319-
static void common_hal_sdcardio_check_for_deinit(sdcardio_sdcard_obj_t *self) {
320-
if (!self->bus) {
321-
raise_deinited_error();
322-
}
323-
}
324-
325327
int common_hal_sdcardio_sdcard_get_blockcount(sdcardio_sdcard_obj_t *self) {
326328
common_hal_sdcardio_check_for_deinit(self);
327329
return self->sectors;
@@ -341,6 +343,7 @@ static int readinto(sdcardio_sdcard_obj_t *self, void *buf, size_t size) {
341343
}
342344

343345
mp_uint_t sdcardio_sdcard_readblocks(mp_obj_t self_in, uint8_t *buf, uint32_t start_block, uint32_t nblocks) {
346+
// deinit check is in lock_and_configure_bus()
344347
sdcardio_sdcard_obj_t *self = MP_OBJ_TO_PTR(self_in);
345348
if (!lock_and_configure_bus(self)) {
346349
return MP_EAGAIN;
@@ -380,7 +383,6 @@ mp_uint_t sdcardio_sdcard_readblocks(mp_obj_t self_in, uint8_t *buf, uint32_t st
380383
}
381384

382385
int common_hal_sdcardio_sdcard_readblocks(sdcardio_sdcard_obj_t *self, uint32_t start_block, mp_buffer_info_t *buf) {
383-
common_hal_sdcardio_check_for_deinit(self);
384386
if (buf->len % 512 != 0) {
385387
mp_raise_ValueError(MP_ERROR_TEXT("Buffer length must be a multiple of 512"));
386388
}
@@ -434,9 +436,8 @@ static int _write(sdcardio_sdcard_obj_t *self, uint8_t token, void *buf, size_t
434436
}
435437

436438
mp_uint_t sdcardio_sdcard_writeblocks(mp_obj_t self_in, uint8_t *buf, uint32_t start_block, uint32_t nblocks) {
439+
// deinit check is in lock_and_configure_bus()
437440
sdcardio_sdcard_obj_t *self = MP_OBJ_TO_PTR(self_in);
438-
common_hal_sdcardio_check_for_deinit(self);
439-
440441
if (!lock_and_configure_bus(self)) {
441442
return MP_EAGAIN;
442443
}
@@ -471,15 +472,15 @@ mp_uint_t sdcardio_sdcard_writeblocks(mp_obj_t self_in, uint8_t *buf, uint32_t s
471472
}
472473

473474
int common_hal_sdcardio_sdcard_sync(sdcardio_sdcard_obj_t *self) {
474-
common_hal_sdcardio_check_for_deinit(self);
475+
// deinit check is in lock_and_configure_bus()
475476
lock_and_configure_bus(self);
476477
int r = exit_cmd25(self);
477478
extraclock_and_unlock_bus(self);
478479
return r;
479480
}
480481

481482
int common_hal_sdcardio_sdcard_writeblocks(sdcardio_sdcard_obj_t *self, uint32_t start_block, mp_buffer_info_t *buf) {
482-
common_hal_sdcardio_check_for_deinit(self);
483+
// deinit check is in lock_and_configure_bus()
483484
if (buf->len % 512 != 0) {
484485
mp_raise_ValueError(MP_ERROR_TEXT("Buffer length must be a multiple of 512"));
485486
}

0 commit comments

Comments
 (0)