USBHostMSD

Table of Contents

  1. Hello World
  2. API
  3. Related

This content relates to a deprecated version of Mbed

Mbed 2 is now deprecated. For the latest version please see the Mbed OS documentation.

The USBHostMSD interface is used to access a USB mass storage device.

Library in Beta!

This library is in Beta. If you have any problems using the USBHost library, please send a bug report at support@mbed.org

The USB Host connector should be attached to

  • p31 (D+), p32 (D-) and GND for the LPC1768
  • add two 15k resistors tied to GND on D+ and D-

Hello World

Import program

00001 #include "mbed.h"
00002 #include "USBHostMSD.h"
00003 
00004 DigitalOut led(LED1);
00005 
00006 void msd_task(void const *) {
00007     
00008     USBHostMSD msd("usb");
00009     int i = 0;
00010     
00011     while(1) {
00012         
00013         // try to connect a MSD device
00014         while(!msd.connect()) {
00015             Thread::wait(500);
00016         }
00017         
00018         // in a loop, append a file
00019         // if the device is disconnected, we try to connect it again
00020         while(1) {
00021             
00022             // append a file
00023             FILE * fp = fopen("/usb/test1.txt", "a");
00024         
00025             if (fp != NULL) {
00026                 fprintf(fp, "Hello fun SD Card World: %d!\r\n", i++);
00027                 printf("Goodbye World!\r\n");
00028                 fclose(fp);
00029             } else {
00030                 printf("FILE == NULL\r\n");
00031             }
00032             
00033             Thread::wait(500);
00034         
00035             // if device disconnected, try to connect again
00036             if (!msd.connected())
00037                 break;
00038         }
00039             
00040     }
00041 }
00042 
00043 
00044 int main() {
00045     Thread msdTask(msd_task, NULL, osPriorityNormal, 1024 * 4);
00046     while(1) {
00047         led=!led;
00048         Thread::wait(500);
00049     }
00050 }

API

Import library

Public Member Functions

USBHostMSD (const char *rootdir)
Constructor.
bool connected ()
Check if a MSD device is connected.
bool connect ()
Try to connect to a MSD device.

Troobleshooting

If your mbed board is automatically resetted when you plug a USB device, you should consider to use an external power supply


All wikipages