Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

WDT reset with SD card #1255

Closed
svaygame opened this issue Dec 19, 2015 · 9 comments · Fixed by earlephilhower/ESP8266SdFat#3
Closed

WDT reset with SD card #1255

svaygame opened this issue Dec 19, 2015 · 9 comments · Fixed by earlephilhower/ESP8266SdFat#3

Comments

@svaygame
Copy link

svaygame commented Dec 19, 2015

When creating a new file , need a lot of time for search the FAT for free clusters.
If there are a large number of files in SD card, might lead WDT reset

Trigger WDT timeout at here:
https://github.com/esp8266/Arduino/blob/master/libraries/SD/src/utility/SdVolume.cpp : line 58

  // search the FAT for free clusters
  for (uint32_t n = 0;; n++, endCluster++) {
    // can't find space checked all clusters
    if (n >= clusterCount_) return false;
    /// if( n %1024 == 0)delay(0); // Add dealy for WDT

    // past end - start from beginning of FAT
    if (endCluster > fatEnd) {
      bgnCluster = endCluster = 2;
    }
    uint32_t f;
    if (!fatGet(endCluster, &f)) return false;

    if (f != 0) {
      // cluster in use try next cluster as bgnCluster
      bgnCluster = endCluster + 1;
    } else if ((endCluster - bgnCluster + 1) == count) {
      // done - found space
      /// Serial.printf("result:%d , %d\n" , n , bgnCluster); // result: 371012 , 371014
      break;
    }

  }

It found space at 371012 time.

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@me-no-dev
Copy link
Collaborator

did delay(0); fix the issue?
if so can you try using optimistic_yield(10000); without the "if" statement.

@igrr igrr added this to the 2.2.0 milestone Feb 29, 2016
@igrr igrr modified the milestones: 2.2.0, 2.3.0 Apr 18, 2016
@igrr igrr modified the milestones: 2.3.0, 2.4.0 Jun 3, 2016
@devyte
Copy link
Collaborator

devyte commented Oct 19, 2017

The referenced code is still in latest git.

@devyte
Copy link
Collaborator

devyte commented Nov 20, 2017

Adding yield or delay means the function can't be used in async code anymore.

@igrr I'm not sure what can be done here, and I don't have the hardware to test. Any ideas?

@igrr igrr modified the milestones: 2.4.0, 2.5.0 Dec 31, 2017
@igrr
Copy link
Member

igrr commented Dec 31, 2017

Don't have hardware to test this at the moment either, moving to 2.5.0

@d-a-v d-a-v removed the level: easy label Oct 12, 2018
@devyte
Copy link
Collaborator

devyte commented Dec 1, 2018

Note: if the code here must yield, the correct way is with a delay(0) as originally proposed. That will yield when in CONT, and do nothing when in SYS. In contrast, optimistic_yield() only skips yield when called too soon after loop() entry.

@devyte
Copy link
Collaborator

devyte commented Dec 1, 2018

This requires investigation that won't fit into 2.5.0, mostly due to lack of hardware for testing. Pushing milestone back.

@devyte devyte modified the milestones: 2.5.0, 2.6.0 Dec 1, 2018
@devyte
Copy link
Collaborator

devyte commented Mar 1, 2019

Assigning to @earlephilhower in view of the new SDFS implementation.

@earlephilhower
Copy link
Collaborator

The code paths are modified and in a different file, but they're generally the same. There are two methods in FatVolume.cpp which can iterate over all clusters on a card that need attention:

bool FatVolume::allocateCluster(uint32_t current, uint32_t* next)
bool FatVolume::allocContiguous(uint32_t count, uint32_t* firstCluster)

Wackypedia lists FAT32 potentially having 268,435,445 clusters, so iterating over them all might take a wee bit of time. A practical example of a 16GB card (formatted w/Linux VFAT default options) shows just under 500,000 clusters (30441472 sectors / 64 (sectors/cluster)).

As a simple, brute-force way of handling the WDT I propose calling delay(0) every 5,000 clusters iterated over in these methods.

earlephilhower added a commit to earlephilhower/ESP8266SdFat that referenced this issue Jul 21, 2019
FAT filesystems may have up to ~270 million clusters, and the SdFat
library needs to perform a linear scan on them to find free clusters.
WDT timeouts will occur on systems that have periodic yield
requirements when large scans are needed.

Add in a OS yield (no-op unless needed by the specific uController)
every 5,000 cluster entries.

Fixes esp8266/Arduino#1255
@earlephilhower
Copy link
Collaborator

See earlephilhower/ESP8266SdFat#3 for the proposed solution. That repo needs to be merged, then a submodule update done here on the core to pull it in.

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants