Unable to write in LocalfileSystem

hello I am using lpc1768 with mbed 2 + rtos and I try to migrate it to MBED-0S 5.
all my code is compilng wth MBED-OS V5.15 (I am getting it from online compiler “blinky” and it seems the latest version 5.14.2 regarding commit of blinky code)
but I am facing problem when trying to ready my parameter file in LocalFileSystem
for that I got a snippet code on bug report on same pb and try to solve the situation but unsucessfully

***here is the sample code tested witl latest firmware 141212 and previous one 21164 ****
*** with same result ***

int hits;   // hit counter

void write(){
    FILE *fp=NULL;
    fp= fopen("/local/out.txt", "a");    // use the append to ADD data to the out.txt file
    fprintf(fp,"test %d \r\n", hits);   // add test + hit count to the out.txt file
    fflush (fp);
    //free(fp);  //  mbed-os  reported but not working

int main()
    while(1){   // keep looping round wating for button press

    	if (hits<200)
            ThisThread::sleep_for(1000);  // small delay for debounce
            hits++;         // increase hit counter

thanks a lot for your help

******* RESULTS ****
led1 is blinking rapidly and lowly all the time
led2-3-4 are light on
file is create “out.txt” on file system with 1 line “test 0”

serial port is getting report

  • MbedOS Fault Handler ++

                        FaultType: HardFault
                                                    R0   : 00000001
                                                                   R1   : 10003A1C

    R2 : 00000017
    R3 : 74007478
    R4 : 00000001
    R5 : 10003A1C
    R6 : 10003A4C
    R7 : 00000000
    R8 : 000028D1
    R9 : 00000000
    R10 : 00000000
    R11 : 00000000
    R12 : 01010101
    SP : 10003A18
    LR : 000035DD
    PC : 000035E6
    xPSR : 01000000
    PSP : 100039F8
    MSP : 10007FA0
    CPUID: 412FC230
    HFSR : 80000000
    MMFSR: 00000000
    BFSR : 00000000
    UFSR : 00000000
    DFSR : 0000000A
    AFSR : 00000000
    Mode : Thread
    Priv : Privileged
    Stack: PSP

    – MbedOS Fault Handler –

                            ++ MbedOS Error Info ++
                                                   Error Status: 0x80FF013D Code: 317 Module: 255
                 Error Message: Fault exception
                                               Location: 0x234F
                                                               Error Value: 0x35E6

    Current Thread: main Id: 0x10002628 Entry: 0x1E17 StackSize: 0x1000 StackMem: 0x10002AD8 SP: 0x10007F3C
    For more info, visit: https://mbed.com/s/error?error=0x80FF013D&tgt=LPC1768
    – MbedOS Error Info –

Same problem here!

Apparently the sleep_for breaks the LocalfileSystem.

Does anyone have found the solution?

Have you tried to put the routines containing sleep_for into it’s own Thread?
For some reason, sleep_for sometimes behave unexpectedly when it’s used on the only (main) Thread.

1 Like

Thanks @ardwar for your suggestion, it works perfectly! One possible solution is to put routines with sleep_for into threads and control the main loop timing using a Timer .

My test code:

#include "mbed.h"
#include "platform/mbed_thread.h"

// Blinking rate in milliseconds
#define BLINKING_RATE_MS  500
#define SAVE_DELAY        1000

DigitalOut led(LED1);
DigitalOut led2(LED2);

int counter,last_time;
LocalFileSystem local("local");               // Create the local filesystem under the name "local"
Serial pc(USBTX, USBRX,115200);
Thread led2_thread;
Timer t;

void append_to_file(int n){
    pc.printf("Writing %d to file\r\n",n);
    FILE *fp = fopen("/local/test.txt", "a");  // Open "test.txt" on the local file system for writing (append)
    fprintf(fp, "Write number: %d\r\n",n);
void led2_loop(void){
    while (true) {
        led2 = !led2;
int main() {
    pc.printf("\r\n\nStarting LocalFileSystem Test\r\n");
    while (true) {
        if(t.read_ms()-last_time > SAVE_DELAY){
            led = !led;

Just for record, my final application uses the Ethernet Interface library, the connect method also breaks the LocalFileSystem. Running the ethernet into it’s own Thread can solve this issue.