Recent changes
Order
tag order
RTOS
Help
mbed NXP LPC1768
Firmware
Homepage
From the mbed microcontroller Handbook.  

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.

Hello World!

Code

#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.

Here is an example program that shows the behaviour clearly:

Code

#include "mbed.h"

LocalFileSystem local("local");

int main() {
	printf("Hello World!\n");
	
	wait(5.0);
	
	printf("Opening File...\n"); // Drive should be marked as removed
	FILE *fp = fopen("/local/test.txt", "w");
	if(!fp) {
		fprintf(stderr, "File /local/test.txt could not be opened!\n");
		exit(1);
	}
	
	wait(5.0);
	
	printf("Writing Data...\n");	
	fprintf(fp, "Hello World!");
	
	wait(5.0);

	printf("Closing File...\n");
	fclose(fp);

        // Drive should be restored. this is the same as just returning from main

        wait(5);
}

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.




calendar Page history
Last modified 11 Jan 2011, by   user Simon Ford   tag No tags | 15 comments  

15 comments on LocalFileSystem:

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

3 weeks, 5 days ago

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?

3 weeks, 5 days ago

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.

Please login to post comments.