2 years, 5 months ago.

Seeking OS5 File System demo - LocalFileSystem, SD, USB

On the LPC1768, I failed to get the local file system to write a file. And that is just the beginning as I want to read/write via USB and uSD.

Does anyone have source code example that work?

The following example prints "Writing abcde", but no file exists.

#include "mbed.h"
//#include "FATFileSystem.h"

RawSerial pc(USBTX,USBRX);
LocalFileSystem local("local");
DigitalOut led1(LED1);

int main() {
    pc.baud(460800);
    FILE * fh = fopen("/local/test.txt", "wt");
    if (fh) {
        pc.printf("Writing abcde\n");
        fprintf(fh, "abcde\n");
        fclose(fh);
    } else { 
        pc.printf("Failed to open...\n");
    }
    while (true) {
        led1 = !led1;
        wait(0.5);
    }
}

1 Answer

2 years, 5 months ago.

Hello David,

I think your code works correctly. Try to disconnect and then reconnect your LPC1768 board to the PC. When Windows asks whether to scan the MBED disk, say YES. Once the scan has been completed (and issues fixed) you should see a "test.txt" file on the MBED disk.

Accepted Answer

Hi Zoltan, of course you are correct.

When I read your note I realized, the module I was working with has firmware 141212, which doesn't update from the PC side. An older firmware, which I have in most of my modules is 16457, which has the property "LocalFileSystem disk now released to PC when mbed has no files open (used to only happen on program exit)"

That's one part of my question, and I fear the easy part. Do you know of any demo/sample for microSD or USB flash drives for mbed-os 5.X?

Update: The microSD demo project shown below by Zoltan works! Excellent.

That leaves one remaining to find - for a USB flash drive.

posted by David Smart 10 Nov 2018

Hello David,

Unfortunately I don’t have anything for USB Flash.
The example code at https://os.mbed.com/docs/v5.10/apis/fatfilesystem.html works fine for me with SD cards after modifying the start section as below, :

main.cpp

#include "mbed.h"
#include <stdio.h>
#include <errno.h>

// Block devices
//#include "SPIFBlockDevice.h"
//#include "DataFlashBlockDevice.h"
#include "SDBlockDevice.h"
//#include "HeapBlockDevice.h"

// File systems
//#include "LittleFileSystem.h"
#include "FATFileSystem.h"


// Physical block device, can be any device that supports the BlockDevice API
//SPIFBlockDevice bd(
//        MBED_CONF_SPIF_DRIVER_SPI_MOSI,
//        MBED_CONF_SPIF_DRIVER_SPI_MISO,
//        MBED_CONF_SPIF_DRIVER_SPI_CLK,
//        MBED_CONF_SPIF_DRIVER_SPI_CS);

SDBlockDevice bd(p5, p6, p7, p8);

// File system declaration
//LittleFileSystem fs("fs");
FATFileSystem fs("fs");


// Set up the button to trigger an erase
//InterruptIn irq(BUTTON1);
InterruptIn irq(p9);

void erase() {

...

To make it compile an mbed_app.json file with the following content shall be added to the project:

mbed_app.json

{
    "target_overrides": {
        "LPC1768": {
            "target.components_add": ["SD"]
        }
    }
}
posted by Zoltan Hudak 10 Nov 2018