LocalFileSystem

A filesystem for accessing the local mbed Microcontroller USB disk drive.

This allows programs to read and write files on the same disk drive that is used to program the mbed Microcontroller. Once created, the standard C file access functions are used to open, read and write files.

Warning

As the FRDM-KL25Z does not have external flash to store files, the LocalFileSystem is not available for this board

Hello World!

» Import this program

#include "mbed.h"
 
LocalFileSystem local("local");               // Create the local filesystem under the name "local"
 
int main() {
    FILE *fp = fopen("/local/out.txt", "w");  // Open "out.txt" on the local file system for writing
    fprintf(fp, "Hello World!");
    fclose(fp);
}

Notes

If the microcontroller program opens a file on the local drive, it will be marked as “removed” on the Host computer. This means the PC will often display a message such as "insert a disk into drive" if you try to access it at this time; this is normal, and stops both the mbed and the PC trying to access the disk at the same time.

Warning

The USB drive will only re-appear when all file handles are closed in your program, or the microcontroller program exits.

If a running program on the mbed does not exit or does not release it's open file handles, you will no longer be able to see the USB drive when you plug the mbed into your PC. To allow you to see the drive again (and load a new program), use the following procedure:

  1. Unplug the microcontroller
  2. Hold the reset button down
  3. While still holding the button, plug in the microcontroller. The mbed USB drive should appear.
  4. Keep holding the button until the new program is saved onto the USB drive.

Implementation Details

The LocalFileSystem actually accesses the mbed USB disk by making "semihosting" calls to the mbed Interface, which does the actual accesses on behalf of your program. This means the "filesystem" in this case is running on the interface. The underlying mechanism is the interface acting like a debugger, and the "semihost" calls are effectively breakpoints that the interface spots and does the requested operation.

Warning

The LocalFilesystem has a few restrictions:

  • Only 8.3 filenames are supported
  • Sub-directories are not supported
  • fseek is not supported for files opened for writing ("w")
  • File access calls (fread, fwrite) will block, including interrupts, as semihosting is effectively a debug breakpoint

Other filesystems (such as ones to talk to an SD card, or a USB FLASH drive) run on the target itself, so don't talk to the mbed Interface at all and these restrictions don't necessarily apply.




1 related question:

20 comments:

11 Jan 2011

Is it possible to NOT have the mbed marked as removed when accessing a file? I realize in most situations that is not ideal but when testing code repeatedly it can be quite irritating to have the mbed removed. I also know that the file system can get pretty messed up if something goes wrong (I've done it once already). Is it part of the library that marks it removed or is it part of the mbed that I can't change?

13 Jan 2011

Does the LocalFileSystem work without a connected PC? I have a program which runs when my mbed is connected to a PC; but not when I use an external power supply. I know that the example program runs without PC, so there is something in my program causing this.I know it's not printf, and the only other thing I can think of is the file system.

14 Jan 2011

Hi Hendrick,

The LocalFileSystem will run fine when not connected to a PC. Very useful for quick data loggers or coffin files.

The main difference is the 5v VU output won't be powered, as that is the USB rail.

Simon

14 Jan 2011

With external power supply I meant 'USB power supply' (e.g. like the onefrom Apple. But you are right - the LocalFS works fine.

I just did a test with my laboratory power supply. When I supply 5 volt to Vin, my LCD display won't power up properly (it's connected to Vu). When I connect it directly to Vin, everything works fine. (Whats strange: when powering via PC, Vu is 4.2V, but it's 5V when using an external supply. It might be that the mis-behaviour is due to problems wiht the voltage levels, and might depend on the load of the mbed).

01 Feb 2011

Does this support appending to existing files by using "w+"? So far when I've tried it the mbed locks up.

Thanks! Larry

01 Feb 2011

'A' append works.

03 May 2011

Is it possible to get fread and fwrite to work on the same file? (The file is opened using "r+b")

My code works fine on Windows. I tried using fflush but it still doesn't work.

Thanks

22 Sep 2011

If need determine what files are inside a directory, how could do? Know of any example that can be exported to mbed?

thanks

Edit: Así xD

printf("Open directory on /sd\n"); DIR *d = opendir("/sd"); struct dirent *p; while((p = readdir(d)) != NULL) { printf("%s\n", p->d_name); } closedir(d); printf("Directory closed\n");

07 Oct 2011

Hi Simon

Your symptoms described above - was these symptoms running a mbed on Windows? I'm having problems on Linux using the recovery procedure on this page. I have a simple test where I open a local file, read data in the file, and then close the file. But thereafter I can't reprogram the mbed without power cycling and other resets.

13 Oct 2011

How much can the LocalFileSystem hold?

03 Nov 2011

user Patrick Grady wrote:

How much can the LocalFileSystem hold?

We couldn't go more than 4KB. We have been trying to write log files but a log file can't be larger than 4KB.. Sad news

12 Nov 2011

How can I use pins 31 and 32 which are listed as D+ and D- to connect my usb memory stick so I can write to it. There doesn't seem to b any mention of these pins in the handbook

02 Mar 2012

Unfortunately `stat` does not appear to be supported for getting file size. So for mbed you have to use the more ugly and improper ftell/fseek method https://www.securecoding.cert.org/confluence/display/seccode/FIO19-C.+Do+not+use+fseek%28%29+and+ftell%28%29+to+compute+the+size+of+a+file

28 Apr 2012

user Deniz Akkaya wrote:

user Patrick Grady wrote:

How much can the LocalFileSystem hold?

We couldn't go more than 4KB. We have been trying to write log files but a log file can't be larger than 4KB.. Sad news

It says the mbed has 512KB of flash of which some would be taken for the code. Is there any way I can calculate the remaining memory so that I can accommodate a stream of data from the Serial-IN in a .txt file on the LocalFileSystem?

28 Apr 2012

The LocalFileSystem doesn't use the internal 512KB of FLASH on the LPC part but instead uses a FLASH specific part that is connected to the interface chip. This part supports around 2MB of storage. You should be able to see how much space is free by mounting the mbed device on your PC and using it to query the amount of space left available. By default things like the "Recycle Bin" on Windows and "Trash" on OS X will use up space even for deleted files and will need to be emptied to free up the maximum amount of space.

I don't think there is a 4KB size limit on files in the LocalFileSystem.

22 Jun 2012

Adam, thanks for clarifying the 512k LPC Flash versus the LocalFileSystem memory size. I found Windows XP reported around 2MB and was concerned if I could exceed 512K, but all is now clear thanks. I am writing data in CSV format of 8kB without any issues.

13 Jul 2012

Will that work with LpcXpresso 1769 board?

21 Aug 2012

Hi folks, can anyone tell me if the local file system onthe lpc11u24 works when the mbed is powered from a 3V battery on VB. I'm guessing that the 'magic' chip is not powered in this condition and that the local file system is therefore unavailable. Is this right ? In fact cann anyone point me to a desription of what is powered when running in this manner ? is there a step up converter that generates 3.3V out ?

- Chris

25 Aug 2012

Hi Chris the following link should help http://mbed.org/media/uploads/chris/mbed-010.2-schematic.pdf See page 2 for the general power supply circuits.

I had always thought that VB was for power down modes only and Vin should be for operational power.

- Kevin

22 Jan 2013

Hi there, I'm running a little battery volrtage logger and keep finding that the mbed hangs after about an hour which it turns out coincides with havbing written 4.5kb to the log file. I'm using lpc11u24 and writing one data every minute. the program opens the local file in mode 'a' to append a line to it, then writes a line of ascii data before closing the file. for battery logging tio test battery if(seconds % 60 == 0 && SW1==0) log once per minute { bp=fopen("/local/battlog.dat","a"); append to file not erase v5=V5V; read battery voltage from adc fprintf(bp,"%d\t%e\t%e\t%e\t%e\t%e\n",seconds,Vb,v5,ppo1,ppo2,ppo3); fflush(bp); fclose(bp); }

I'm doing fflush on the iostream before closing the file but it makes no difference. I'm quite worried about thsi problem - is there something fundamentally wrong with my method ? It seems curious that I'm not the only one who's noticed a 4kb limit - is there anything we should be doing differently here ?

Chris

user Deniz Akkaya wrote:

user Patrick Grady wrote:

How much can the LocalFileSystem hold?

We couldn't go more than 4KB. We have been trying to write log files but a log file can't be larger than 4KB.. Sad news

Posting comments for this page has been disabled