Suggestion: re-write SDFileSystem

26 Jun 2016

Hi everyone,

Ik did some more testing en after a export to the LPCXpresso IDE logging is not locked after 1368 appends. So the possible problem is the Mbed compiler. I only have make a import and I changed nothing to the LPCXpresso IDE compiler settings.

26 june 2016 ;

For sure its the online Mbed compiler, after export to LPCXpresso I did every try to lock up my logging. 2/3 time a second a write en remove random the SD card run the program for a few days and everything works rock stable.....

03 Apr 2017

Hello, Neil. I use your SDFileSystem with LPCxpress1549 and it works great. I have one suggestion to your code.

Your code uses mbed Timer API for wait or time out in some place like below.

SDFileSystem.cpp

inline bool SDFileSystem::waitReady(int timeout) // timeout (ms)
{
    char resp;
    //Keep sending dummy clocks with DI held high until the card releases the DO line
    m_Timer.start();
    do {
        resp = m_Spi.write(0xFF);
    } while (resp == 0x00 && m_Timer.read_ms() < timeout);
    m_Timer.stop();
    m_Timer.reset();
    //Return success/failure
    return (resp > 0x00);
}

read_us code in mbed Timer API is

Timer.cpp

int Timer::read_us() {
    core_util_critical_section_enter();
    int time = _time + slicetime();
    core_util_critical_section_exit();
    return time;
}

I think "core_util_critical_section_enter()" is not good in some codes with crucial interrupt timing. Because it call _disable_irq();

mbed_critical.c

void core_util_critical_section_enter(void)
{
    bool interrupts_disabled = !core_util_are_interrupts_enabled();
    __disable_irq();

    /* Save the interrupt disabled state as it was prior to any nested critical section lock use */
    if (!interrupt_enable_counter) {
        critical_interrupts_disabled = interrupts_disabled;
    }

    /* If the interrupt_enable_counter overflows or we are in a nested critical section and interrupts
       are enabled, then something has gone badly wrong thus assert an error.
    */
    MBED_ASSERT(interrupt_enable_counter < UINT32_MAX); 
// FIXME
#ifndef   FEATURE_UVISOR
    if (interrupt_enable_counter > 0) {
        MBED_ASSERT(interrupts_disabled);
    }
#else
#warning "core_util_critical_section_enter needs fixing to work from unprivileged code"
#endif /* FEATURE_UVISOR */
    interrupt_enable_counter++;
}

So I changed your code without Timer API like this.

SDFileSystem.cpp

inline bool SDFileSystem::waitReady(int timeout) // timeout (ms)
{
    char resp;
    uint32_t start = us_ticker_read();
    //Keep sending dummy clocks with DI held high until the card releases the DO line
    //m_Timer.start();
    do {
        resp = m_Spi.write(0xFF);
    //} while (resp == 0x00 && m_Timer.read_ms() < timeout);
    //m_Timer.stop();
    //m_Timer.reset();
    } while (resp == 0x00 && ((us_ticker_read() - start) < (uint32_t)(timeout*1000)) ); // ms to us
    //Return success/failure
    return (resp > 0x00);
}

I use LPCxpresso1549 and us_ticker_read(); doesn't use _disable_irq(); , only use 1MHz SCT3 timer. I really don't know us_ticker_read(); is good solution or not in any environment.

Thank you.

12 Apr 2017

Hi guys... does anyone know if sd file system can support two cards at once? I am trying to initialize two sd objects sd and sd2, and the program compiles fine. When I go to run it on my nucleo, it says it cannot create the second card. Anyone run into this problem or have suggestions?

26 Apr 2017

Hello... I'm trying to use this with the mbed-rtos, and it is just not happening.

It can see the card - sd.card_type() and sd.disk_sectors() return the correct values.

sd.opendir always returns NULL. As does fopen.

I tried moving all the sd operations to a separate thread with a large (8K - 4x default) stack. Still no joy.

Where should I start looking? <edit>

OK, I think I found something:

The example provided works with mbed versions 137 and lower The example provided does NOT work with versions 138, 139 and 140. I see comments about 'interrupt in' being deprecated (on the cs line), this may or may not be part of the problem.

Near as I can tell mbed-rtos is not part of the problem.

Bottom line: It (SDFileSystem) does NOT work with the latest mbed release.

02 Jul 2018

Neil. Thank you for your work on the SD card library, which is a great help.

I tried your Hello World program (https://os.mbed.com/users/neilt6/code/SDFileSystem_HelloWorld/) and got the following output for a SanDisk 32GB MicroSD card:

Press the button to perform tests: 
Mounting SD card...success!
.Card type: SDHC
.Sectors: 62333952
.Capacity: 30436.5MB
Testing 4096B write performance...done!
.Result: 374.50KB/s
Testing 4096B read performance...done!
.Result: 564.65KB/s

It made no difference to the performance whether or not I connected a 47K resistor between 3.3V and MISO. Please may I ask what is the value of connecting this pull-up resistor? Thanks for your advice.