Skip to content

Commit 77e1fa3

Browse files
committed
SDCard: do not return until readblocks(), writeblocks(), or sync() have completed
1 parent 8808458 commit 77e1fa3

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

shared-module/sdcardio/SDCard.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
// This implementation largely follows the structure of adafruit_sdcard.py
88

99
#include "extmod/vfs.h"
10-
#include "esp_log.h"
1110
#include "shared-bindings/busio/SPI.h"
1211
#include "shared-bindings/digitalio/DigitalInOut.h"
1312
#include "shared-bindings/sdcardio/SDCard.h"
1413
#include "shared-bindings/time/__init__.h"
1514
#include "shared-bindings/util.h"
1615
#include "shared-module/sdcardio/SDCard.h"
16+
#include "supervisor/shared/tick.h"
1717

1818
#include "py/mperrno.h"
1919

@@ -29,6 +29,7 @@
2929
// So let's allow a nice long time, but don't wait in a tight loop: allow background tasks to run.
3030
#define CMD_TIMEOUT_MS (500)
3131
#define TIMEOUT_MS (500)
32+
#define SPI_TIMEOUT_MS (10000)
3233

3334
#define R1_IDLE_STATE (1 << 0)
3435
#define R1_ILLEGAL_COMMAND (1 << 2)
@@ -59,9 +60,8 @@ static bool lock_and_configure_bus(sdcardio_sdcard_obj_t *self) {
5960
if (common_hal_sdcardio_sdcard_deinited(self)) {
6061
return false;
6162
}
62-
common_hal_sdcardio_check_for_deinit(self);
6363

64-
if (!common_hal_busio_spi_try_lock(self->bus)) {
64+
if (!common_hal_busio_spi_wait_for_lock(self->bus, SPI_TIMEOUT_MS)) {
6565
return false;
6666
}
6767

@@ -268,7 +268,6 @@ static mp_rom_error_text_t init_card(sdcardio_sdcard_obj_t *self) {
268268
{
269269
bool reached_idle_state = false;
270270
for (int i = 0; i < 5; i++) {
271-
ESP_LOGW("init_card", "loop: %d", i);
272271
// do not call cmd with wait=true, because that will return
273272
// prematurely if the idle state is not reached. we can't depend on
274273
// this when the card is not yet in SPI mode
@@ -403,7 +402,7 @@ mp_uint_t sdcardio_sdcard_readblocks(mp_obj_t self_in, uint8_t *buf, uint32_t st
403402
// deinit check is in lock_and_configure_bus()
404403
sdcardio_sdcard_obj_t *self = MP_OBJ_TO_PTR(self_in);
405404
if (!lock_and_configure_bus(self)) {
406-
return -MP_EAGAIN;
405+
return -MP_ETIMEDOUT;
407406
}
408407
int r = 0;
409408
size_t buflen = 512 * nblocks;
@@ -503,7 +502,7 @@ mp_uint_t sdcardio_sdcard_writeblocks(mp_obj_t self_in, uint8_t *buf, uint32_t s
503502
// deinit check is in lock_and_configure_bus()
504503
sdcardio_sdcard_obj_t *self = MP_OBJ_TO_PTR(self_in);
505504
if (!lock_and_configure_bus(self)) {
506-
return -MP_EAGAIN;
505+
return -MP_ETIMEDOUT;
507506
}
508507

509508
if (!self->in_cmd25 || start_block != self->next_block) {
@@ -538,7 +537,7 @@ mp_uint_t sdcardio_sdcard_writeblocks(mp_obj_t self_in, uint8_t *buf, uint32_t s
538537
mp_negative_errno_t common_hal_sdcardio_sdcard_sync(sdcardio_sdcard_obj_t *self) {
539538
// deinit check is in lock_and_configure_bus()
540539
if (!lock_and_configure_bus(self)) {
541-
return -MP_EAGAIN;
540+
return -MP_ETIMEDOUT;
542541
}
543542
int r = exit_cmd25(self);
544543
extraclock_and_unlock_bus(self);

0 commit comments

Comments
 (0)