Skip to content

Commit

Permalink
check for SdFat::isBusy before starting a write
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertvanheusden committed Oct 21, 2024
1 parent f26f533 commit 07e283c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 33 deletions.
8 changes: 8 additions & 0 deletions microcontrollers/backend-sdcard-teensy41.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
}
3 changes: 3 additions & 0 deletions microcontrollers/backend-sdcard-teensy41.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
49 changes: 16 additions & 33 deletions microcontrollers/backend-sdcard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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();
}
3 changes: 3 additions & 0 deletions microcontrollers/backend-sdcard.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 07e283c

Please sign in to comment.