You are viewing an older revision! See the latest version

LocalFileSystem

Table of Contents

  1. Hello World!
  2. Notes

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!

#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 it is no longer accessible from the Host Computer.

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

What to do if mbed USB drive no longer appears

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:

#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);
}

All wikipages