diff --git a/microcontrollers/backend-sdcard-teensy41.cpp b/microcontrollers/backend-sdcard-teensy41.cpp index e0d77b3..021e397 100644 --- a/microcontrollers/backend-sdcard-teensy41.cpp +++ b/microcontrollers/backend-sdcard-teensy41.cpp @@ -104,6 +104,7 @@ bool backend_sdcard_teensy41::write(const uint64_t block_nr, const uint32_t n_bl bool rc = false; for(int i=0; i<5; i++) { // 5 is arbitrarily chosen + wait_for_card(); arm_dcache_flush(data, n_bytes_to_write); size_t bytes_written = file.write(data, n_bytes_to_write); rc = bytes_written == n_bytes_to_write; @@ -220,6 +221,7 @@ backend::cmpwrite_result_t backend_sdcard_teensy41::cmpwrite(const uint64_t bloc break; } + wait_for_card(); arm_dcache_flush(&data_write[i * block_size], block_size); ssize_t rc2 = file.write(&data_write[i * block_size], block_size); if (rc2 != block_size) { @@ -241,3 +243,9 @@ backend::cmpwrite_result_t backend_sdcard_teensy41::cmpwrite(const uint64_t bloc return result; } + +void backend_sdcard_teensy41::wait_for_card() +{ + while(file.isBusy()) + yield(); +} diff --git a/microcontrollers/backend-sdcard-teensy41.h b/microcontrollers/backend-sdcard-teensy41.h index c3be61b..4ef836d 100644 --- a/microcontrollers/backend-sdcard-teensy41.h +++ b/microcontrollers/backend-sdcard-teensy41.h @@ -12,6 +12,9 @@ class backend_sdcard_teensy41 : public backend const int led_read { -1 }; const int led_write { -1 }; + // wait until it is no longer busy with something and available for writes + void wait_for_card(); + public: backend_sdcard_teensy41(const int led_read, const int led_write); virtual ~backend_sdcard_teensy41(); diff --git a/microcontrollers/backend-sdcard.cpp b/microcontrollers/backend-sdcard.cpp index 7e7e885..3896d8d 100644 --- a/microcontrollers/backend-sdcard.cpp +++ b/microcontrollers/backend-sdcard.cpp @@ -175,27 +175,15 @@ bool backend_sdcard::write(const uint64_t block_nr, const uint32_t n_blocks, con size_t n_bytes_to_write = n_blocks * iscsi_block_size; bytes_written += n_bytes_to_write; - bool rc = false; - for(int k=2; k>=0; k--) { // 2 is arbitrarily chosen - for(int i=0; i<5; i++) { // 5 is arbitrarily chosen - size_t bytes_written = file.write(data, n_bytes_to_write); - rc = bytes_written == n_bytes_to_write; - if (rc) - goto ok; - Serial.printf("Wrote %zu bytes instead of %zu\r\n", bytes_written, n_bytes_to_write); - delay((i + 1) * 100); // 100ms is arbitrarily chosen - Serial.printf("Retrying write of %" PRIu32 " blocks starting at block number % " PRIu64 "\r\n", n_blocks, block_nr); - } + wait_for_card(); + size_t bytes_written = file.write(data, n_bytes_to_write); + bool rc = bytes_written == n_bytes_to_write; + if (!rc) + Serial.printf("Wrote %zu bytes instead of %zu\r\n", bytes_written, n_bytes_to_write); - if (k) - reinit(true); - } -ok: #if defined(RP2040W) mutex_exit(&serial_access_lock); #endif - if (!rc) - DOLOG(logging::ll_error, "backend_sdcard::write", "-", "Cannot write: %d", file.getError()); write_led(led_write, LOW); @@ -248,26 +236,14 @@ bool backend_sdcard::read(const uint64_t block_nr, const uint32_t n_blocks, uint bytes_read += n_bytes_to_read; bool rc = false; - for(int k=2; k>=0; k--) { // 2 is arbitrarily chosen - for(int i=0; i<5; i++) { // 5 is arbitrarily chosen - size_t bytes_read = file.read(data, n_bytes_to_read); - rc = bytes_read == n_bytes_to_read; - if (rc) - goto ok; - Serial.printf("Read %zu bytes instead of %zu\r\n", bytes_read, n_bytes_to_read); - delay((i + 1) * 100); // 100ms is arbitrarily chosen - Serial.printf("Retrying read of %" PRIu32 " blocks starting at block number % " PRIu64 "\r\n", n_blocks, block_nr); - } + size_t bytes_read = file.read(data, n_bytes_to_read); + rc = bytes_read == n_bytes_to_read; + if (!rc) + Serial.printf("Read %zu bytes instead of %zu\r\n", bytes_read, n_bytes_to_read); - if (k) - reinit(true); - } -ok: #if defined(RP2040W) mutex_exit(&serial_access_lock); #endif - if (!rc) - DOLOG(logging::ll_error, "backend_sdcard::read", "-", "Cannot read: %d", file.getError()); write_led(led_read, LOW); ts_last_acces = get_micros(); return rc; @@ -318,6 +294,7 @@ backend::cmpwrite_result_t backend_sdcard::cmpwrite(const uint64_t block_nr, con break; } + wait_for_card(); ssize_t rc2 = file.write(&data_write[i * block_size], block_size); if (rc2 != block_size) { result = cmpwrite_result_t::CWR_WRITE_ERROR; @@ -341,3 +318,9 @@ backend::cmpwrite_result_t backend_sdcard::cmpwrite(const uint64_t block_nr, con return result; } + +void backend_sdcard::wait_for_card() +{ + while(sd.card()->isBusy()) + yield(); +} diff --git a/microcontrollers/backend-sdcard.h b/microcontrollers/backend-sdcard.h index ca84606..2b95d3a 100644 --- a/microcontrollers/backend-sdcard.h +++ b/microcontrollers/backend-sdcard.h @@ -26,6 +26,9 @@ class backend_sdcard : public backend bool reinit(const bool close_first); + // wait until it is no longer busy with something and available for writes + void wait_for_card(); + public: backend_sdcard(const int led_read, const int led_write, const int pin_SD_MISO, const int pin_SD_MOSI, const int pin_SD_SCLK, const int pin_SD_CS); virtual ~backend_sdcard();