Emulation of LocalFileSystem with virtual COM.

Dependencies:   USBDevice

Dependents:   KL46Z-lpc81isp lpcterm2

#include "USBLocalFileSystem.h"

int main() {
    USBLocalFileSystem* usb_local = new USBLocalFileSystem(); // RamDisk(64KB)

    while(1) {
        usb_local->lock(true);
        usb_local->remount();
        char filename[32];
        if (usb_local->find(filename, sizeof(filename), "*.TXT")) {
            FILE* fp = fopen(filename, "r");
            if (fp) {
                int c;
                while((c = fgetc(fp)) != EOF) {
                    usb_local->putc(c);
                }
                fclose(fp);
            }
        }    
        usb_local->lock(false);

        wait_ms(1000*5);
    }
}



Sample application:

Import programKL46Z-lpc81isp

ISP example program.

Import programlpcterm2

semihost server example program

Committer:
va009039
Date:
Tue May 06 15:42:30 2014 +0900
Branch:
branch
Revision:
2:97c314eae8b8
Parent:
0:39eb4d5b97df
Child:
4:8f6857784854
add USB MSD block operation.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
va009039 0:39eb4d5b97df 1 #pragma once
va009039 0:39eb4d5b97df 2 #include "mbed.h"
va009039 0:39eb4d5b97df 3 #include "FATFileSystem.h"
va009039 0:39eb4d5b97df 4 #include "USBMSD.h"
va009039 0:39eb4d5b97df 5 #include "USBMSD2.h"
va009039 0:39eb4d5b97df 6 #include "StorageInterface.h"
va009039 0:39eb4d5b97df 7 #include "mystring.h"
va009039 0:39eb4d5b97df 8
va009039 0:39eb4d5b97df 9 class LocalStorage : public FATFileSystem {
va009039 0:39eb4d5b97df 10 public:
va009039 0:39eb4d5b97df 11 LocalStorage(StorageInterface* storage, const char* name = "local");
va009039 0:39eb4d5b97df 12 virtual int disk_read(uint8_t * buffer, uint64_t sector) {
va009039 0:39eb4d5b97df 13 return _storage->storage_read(buffer, sector);
va009039 0:39eb4d5b97df 14 }
va009039 0:39eb4d5b97df 15 virtual int disk_write(const uint8_t * buffer, uint64_t sector) {
va009039 0:39eb4d5b97df 16 return _storage->storage_write(buffer, sector);
va009039 0:39eb4d5b97df 17 }
va009039 0:39eb4d5b97df 18 virtual uint64_t disk_sectors() {
va009039 0:39eb4d5b97df 19 return _storage->storage_sectors();
va009039 0:39eb4d5b97df 20 }
va009039 0:39eb4d5b97df 21 static bool find_bin(mystring& filename);
va009039 0:39eb4d5b97df 22 private:
va009039 0:39eb4d5b97df 23 StorageInterface* _storage;
va009039 0:39eb4d5b97df 24 const char* _name;
va009039 0:39eb4d5b97df 25 };
va009039 0:39eb4d5b97df 26
va009039 0:39eb4d5b97df 27 class USBStorage : public USBMSD {
va009039 0:39eb4d5b97df 28 public:
va009039 0:39eb4d5b97df 29 USBStorage(StorageInterface* storage): _storage(storage) {
va009039 0:39eb4d5b97df 30 connect();
va009039 0:39eb4d5b97df 31 }
va009039 0:39eb4d5b97df 32 virtual int disk_read(uint8_t * data, uint64_t block) {
va009039 0:39eb4d5b97df 33 return _storage->storage_read(data, block);
va009039 0:39eb4d5b97df 34 }
va009039 0:39eb4d5b97df 35 virtual int disk_write(const uint8_t * data, uint64_t block) {
va009039 0:39eb4d5b97df 36 return _storage->storage_write(data, block);
va009039 0:39eb4d5b97df 37 }
va009039 0:39eb4d5b97df 38 virtual int disk_initialize() { return 0; }
va009039 0:39eb4d5b97df 39 virtual uint64_t disk_sectors() {
va009039 0:39eb4d5b97df 40 return _storage->storage_sectors();
va009039 0:39eb4d5b97df 41 }
va009039 0:39eb4d5b97df 42 virtual int disk_status() { return 0; }
va009039 0:39eb4d5b97df 43 virtual uint64_t disk_size() {
va009039 0:39eb4d5b97df 44 return _storage->storage_size();
va009039 0:39eb4d5b97df 45 }
va009039 0:39eb4d5b97df 46
va009039 0:39eb4d5b97df 47 private:
va009039 0:39eb4d5b97df 48 StorageInterface* _storage;
va009039 0:39eb4d5b97df 49 };
va009039 0:39eb4d5b97df 50
va009039 0:39eb4d5b97df 51 class USBStorage2 : public USBMSD2 {
va009039 0:39eb4d5b97df 52 public:
va009039 0:39eb4d5b97df 53 USBStorage2(StorageInterface* storage): _storage(storage) {
va009039 2:97c314eae8b8 54 init();
va009039 0:39eb4d5b97df 55 connect();
va009039 0:39eb4d5b97df 56 }
va009039 2:97c314eae8b8 57 void init() {
va009039 2:97c314eae8b8 58 block_flag = false;
va009039 2:97c314eae8b8 59 report_block_count = 0;
va009039 2:97c314eae8b8 60 report_initialize_count = 0;
va009039 2:97c314eae8b8 61 report_read_count = 0;
va009039 2:97c314eae8b8 62 report_write_count = 0;
va009039 2:97c314eae8b8 63 report_sectors_count = 0;
va009039 2:97c314eae8b8 64 report_status_count = 0;
va009039 2:97c314eae8b8 65 report_size_count = 0;
va009039 2:97c314eae8b8 66 }
va009039 0:39eb4d5b97df 67 virtual int disk_read(uint8_t * data, uint64_t block) {
va009039 2:97c314eae8b8 68 if (block_flag) {
va009039 2:97c314eae8b8 69 report_block_count++;
va009039 2:97c314eae8b8 70 return 1;
va009039 2:97c314eae8b8 71 }
va009039 2:97c314eae8b8 72 report_read_count++;
va009039 0:39eb4d5b97df 73 return _storage->storage_read(data, block);
va009039 0:39eb4d5b97df 74 }
va009039 0:39eb4d5b97df 75 virtual int disk_write(const uint8_t * data, uint64_t block) {
va009039 2:97c314eae8b8 76 if (block_flag) {
va009039 2:97c314eae8b8 77 report_block_count++;
va009039 2:97c314eae8b8 78 return 1;
va009039 2:97c314eae8b8 79 }
va009039 2:97c314eae8b8 80 report_write_count++;
va009039 0:39eb4d5b97df 81 return _storage->storage_write(data, block);
va009039 0:39eb4d5b97df 82 }
va009039 2:97c314eae8b8 83 virtual int disk_initialize() {
va009039 2:97c314eae8b8 84 report_initialize_count++;
va009039 2:97c314eae8b8 85 return 0;
va009039 2:97c314eae8b8 86 }
va009039 0:39eb4d5b97df 87 virtual uint64_t disk_sectors() {
va009039 2:97c314eae8b8 88 report_sectors_count++;
va009039 0:39eb4d5b97df 89 return _storage->storage_sectors();
va009039 0:39eb4d5b97df 90 }
va009039 2:97c314eae8b8 91 virtual int disk_status() {
va009039 2:97c314eae8b8 92 report_status_count++;
va009039 2:97c314eae8b8 93 return 0;
va009039 2:97c314eae8b8 94 }
va009039 0:39eb4d5b97df 95 virtual uint64_t disk_size() {
va009039 2:97c314eae8b8 96 report_size_count++;
va009039 0:39eb4d5b97df 97 return _storage->storage_size();
va009039 0:39eb4d5b97df 98 }
va009039 0:39eb4d5b97df 99
va009039 2:97c314eae8b8 100 bool block(bool flag) {
va009039 2:97c314eae8b8 101 if (block_flag != flag) {
va009039 2:97c314eae8b8 102 block_flag = flag;
va009039 2:97c314eae8b8 103 return true;
va009039 2:97c314eae8b8 104 }
va009039 2:97c314eae8b8 105 return false;
va009039 2:97c314eae8b8 106 }
va009039 2:97c314eae8b8 107 __IO bool block_flag;
va009039 2:97c314eae8b8 108 __IO int report_block_count;
va009039 2:97c314eae8b8 109 __IO int report_initialize_count;
va009039 2:97c314eae8b8 110 __IO int report_read_count;
va009039 2:97c314eae8b8 111 __IO int report_write_count;
va009039 2:97c314eae8b8 112 __IO int report_sectors_count;
va009039 2:97c314eae8b8 113 __IO int report_status_count;
va009039 2:97c314eae8b8 114 __IO int report_size_count;
va009039 2:97c314eae8b8 115
va009039 0:39eb4d5b97df 116 private:
va009039 0:39eb4d5b97df 117 StorageInterface* _storage;
va009039 0:39eb4d5b97df 118 };
va009039 0:39eb4d5b97df 119