|
7 | 7 | // This implementation largely follows the structure of adafruit_sdcard.py |
8 | 8 |
|
9 | 9 | #include "extmod/vfs.h" |
10 | | -#include "esp_log.h" |
11 | 10 | #include "shared-bindings/busio/SPI.h" |
12 | 11 | #include "shared-bindings/digitalio/DigitalInOut.h" |
13 | 12 | #include "shared-bindings/sdcardio/SDCard.h" |
14 | 13 | #include "shared-bindings/time/__init__.h" |
15 | 14 | #include "shared-bindings/util.h" |
16 | 15 | #include "shared-module/sdcardio/SDCard.h" |
| 16 | +#include "supervisor/shared/tick.h" |
17 | 17 |
|
18 | 18 | #include "py/mperrno.h" |
19 | 19 |
|
|
29 | 29 | // So let's allow a nice long time, but don't wait in a tight loop: allow background tasks to run. |
30 | 30 | #define CMD_TIMEOUT_MS (500) |
31 | 31 | #define TIMEOUT_MS (500) |
| 32 | +#define SPI_TIMEOUT_MS (10000) |
32 | 33 |
|
33 | 34 | #define R1_IDLE_STATE (1 << 0) |
34 | 35 | #define R1_ILLEGAL_COMMAND (1 << 2) |
@@ -59,9 +60,8 @@ static bool lock_and_configure_bus(sdcardio_sdcard_obj_t *self) { |
59 | 60 | if (common_hal_sdcardio_sdcard_deinited(self)) { |
60 | 61 | return false; |
61 | 62 | } |
62 | | - common_hal_sdcardio_check_for_deinit(self); |
63 | 63 |
|
64 | | - if (!common_hal_busio_spi_try_lock(self->bus)) { |
| 64 | + if (!common_hal_busio_spi_wait_for_lock(self->bus, SPI_TIMEOUT_MS)) { |
65 | 65 | return false; |
66 | 66 | } |
67 | 67 |
|
@@ -268,7 +268,6 @@ static mp_rom_error_text_t init_card(sdcardio_sdcard_obj_t *self) { |
268 | 268 | { |
269 | 269 | bool reached_idle_state = false; |
270 | 270 | for (int i = 0; i < 5; i++) { |
271 | | - ESP_LOGW("init_card", "loop: %d", i); |
272 | 271 | // do not call cmd with wait=true, because that will return |
273 | 272 | // prematurely if the idle state is not reached. we can't depend on |
274 | 273 | // 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 |
403 | 402 | // deinit check is in lock_and_configure_bus() |
404 | 403 | sdcardio_sdcard_obj_t *self = MP_OBJ_TO_PTR(self_in); |
405 | 404 | if (!lock_and_configure_bus(self)) { |
406 | | - return -MP_EAGAIN; |
| 405 | + return -MP_ETIMEDOUT; |
407 | 406 | } |
408 | 407 | int r = 0; |
409 | 408 | 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 |
503 | 502 | // deinit check is in lock_and_configure_bus() |
504 | 503 | sdcardio_sdcard_obj_t *self = MP_OBJ_TO_PTR(self_in); |
505 | 504 | if (!lock_and_configure_bus(self)) { |
506 | | - return -MP_EAGAIN; |
| 505 | + return -MP_ETIMEDOUT; |
507 | 506 | } |
508 | 507 |
|
509 | 508 | 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 |
538 | 537 | mp_negative_errno_t common_hal_sdcardio_sdcard_sync(sdcardio_sdcard_obj_t *self) { |
539 | 538 | // deinit check is in lock_and_configure_bus() |
540 | 539 | if (!lock_and_configure_bus(self)) { |
541 | | - return -MP_EAGAIN; |
| 540 | + return -MP_ETIMEDOUT; |
542 | 541 | } |
543 | 542 | int r = exit_cmd25(self); |
544 | 543 | extraclock_and_unlock_bus(self); |
|
0 commit comments