Skip to content

Commit

Permalink
Merge pull request #9418 from dhalbert/sdcardio-deinit-check
Browse files Browse the repository at this point in the history
shared-module/sdcardio/SDCard.c: check for deinit more thoroughly
  • Loading branch information
dhalbert authored Jul 9, 2024
2 parents 079a5f5 + 48dd9e3 commit 685101f
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions shared-module/sdcardio/SDCard.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,15 @@
#define TOKEN_STOP_TRAN (0xFD)
#define TOKEN_DATA (0xFE)

static void common_hal_sdcardio_check_for_deinit(sdcardio_sdcard_obj_t *self) {
if (!self->bus) {
raise_deinited_error();
}
}

static bool lock_and_configure_bus(sdcardio_sdcard_obj_t *self) {
common_hal_sdcardio_check_for_deinit(self);

if (!common_hal_busio_spi_try_lock(self->bus)) {
return false;
}
Expand Down Expand Up @@ -316,12 +324,6 @@ void common_hal_sdcardio_sdcard_deinit(sdcardio_sdcard_obj_t *self) {
common_hal_digitalio_digitalinout_deinit(&self->cs);
}

static void common_hal_sdcardio_check_for_deinit(sdcardio_sdcard_obj_t *self) {
if (!self->bus) {
raise_deinited_error();
}
}

int common_hal_sdcardio_sdcard_get_blockcount(sdcardio_sdcard_obj_t *self) {
common_hal_sdcardio_check_for_deinit(self);
return self->sectors;
Expand All @@ -341,6 +343,7 @@ static int readinto(sdcardio_sdcard_obj_t *self, void *buf, size_t size) {
}

mp_uint_t sdcardio_sdcard_readblocks(mp_obj_t self_in, uint8_t *buf, uint32_t start_block, uint32_t nblocks) {
// deinit check is in lock_and_configure_bus()
sdcardio_sdcard_obj_t *self = MP_OBJ_TO_PTR(self_in);
if (!lock_and_configure_bus(self)) {
return MP_EAGAIN;
Expand Down Expand Up @@ -380,7 +383,6 @@ mp_uint_t sdcardio_sdcard_readblocks(mp_obj_t self_in, uint8_t *buf, uint32_t st
}

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

mp_uint_t sdcardio_sdcard_writeblocks(mp_obj_t self_in, uint8_t *buf, uint32_t start_block, uint32_t nblocks) {
// deinit check is in lock_and_configure_bus()
sdcardio_sdcard_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_sdcardio_check_for_deinit(self);

if (!lock_and_configure_bus(self)) {
return MP_EAGAIN;
}
Expand Down Expand Up @@ -471,15 +472,15 @@ mp_uint_t sdcardio_sdcard_writeblocks(mp_obj_t self_in, uint8_t *buf, uint32_t s
}

int common_hal_sdcardio_sdcard_sync(sdcardio_sdcard_obj_t *self) {
common_hal_sdcardio_check_for_deinit(self);
// deinit check is in lock_and_configure_bus()
lock_and_configure_bus(self);
int r = exit_cmd25(self);
extraclock_and_unlock_bus(self);
return r;
}

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

0 comments on commit 685101f

Please sign in to comment.