• Unfortunately a soft reset doesn't do the trick @Gordon. This is what happens:

    Case 1:

    // fresh restart
     _____                 _
    |   __|___ ___ ___ _ _|_|___ ___
    |   __|_ -| . |  _| | | |   | . |
    |_____|___|  _|_| |___|_|_|_|___|
              |_| http://espruino.com
     1v70 Copyright 2014 G.Williams
    
    >var f = require("fs").readFile("test.txt");
    ="hello\n\n"
    
    // remove card
    
    >var f = require("fs").readFile("test.txt");
    ERROR: Unable to read file : DISK_ERR
    =undefined
    
    // re-insert card
    
    >var f = require("fs").readFile("test.txt");
    ERROR: Unable to read file : DISK_ERR
    =undefined
    
    >reset();
    =undefined
    
     _____                 _
    |   __|___ ___ ___ _ _|_|___ ___
    |   __|_ -| . |  _| | | |   | . |
    |_____|___|  _|_| |___|_|_|_|___|
              |_| http://espruino.com
     1v70 Copyright 2014 G.Williams
    
    >var f = require("fs").readFile("test.txt");
    ERROR: Unable to mount SD card : NOT_READY
    =undefined
    

    However if I hit the reset button on the board or cycle the power then everything comes back to life.

    Case 2:

    Does the error happen if you pull the card out and re-insert it, without doing any file operations when it is removed?

    Yes, the DISK_ERR also occurs in this case:

    >var f = require("fs").readFile("test.txt");
    ="hello\n\n"
    
    //Removed card and re-inserted without file operation
    
    >var f = require("fs").readFile("test.txt");
    ERROR: Unable to read file : DISK_ERR
    =undefined
    

    Case 3:

    I thought out another situation that could occur:

    • Fresh restart.
    • Card removed and re-inserted.
    • Attempt to read from card.

    This still works. So it seems a problem arises only once you have already performed a previous card read/write and then remove/re-insert the card, with or without doing any file operations while removed.

    If the error only happens if you read when the card is removed, I'd just explicitly check the value of the card detect pin, and not do anything when it's disconnected.

    Yes, that is what I thought I'd do. Hardware wise however, as you pointed out, this isn't possible with the Espruino board as the card detect pin is not wired up. Also, it seems from the second case illustrated above, that if you have already performed a file operation, then remove and re-insert the card without any further file operations (blocked by not detecting a card) and then attempt a file operation post re-insertion, you still have a problem.

    think something needs to be reset, but the filesystem code we have just doesn't do so yet.

    Yes, this is my suspicion too @DrAzzy. It probably has to be looked at in the C code. Not exactly my strength...yet :).

    I would have it a guess that upon detecting the card removal (assuming this hardware functionality is available), that the jswrap_fs_kill() function in jswrap_fs.c should be called.

    I don't suppose it absolutely must happen in the C code. The main thing is the handle to the FATFS needs to be renewed...I think :)

About