Emulation of LocalFileSystem with virtual COM.
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
Revision 3:09b562c87f89, committed 2014-05-06
- Comitter:
- va009039
- Date:
- Tue May 06 16:05:57 2014 +0900
- Parent:
- 1:00c9eb8af5c2
- Parent:
- 2:97c314eae8b8
- Child:
- 4:8f6857784854
- Commit message:
- merge
Changed in this revision
--- a/USBLocalFileSystem.cpp Sun May 04 00:23:05 2014 +0000 +++ b/USBLocalFileSystem.cpp Tue May 06 16:05:57 2014 +0900 @@ -28,13 +28,6 @@ _local = new LocalStorage(_storage); } -void USBLocalFileSystem::attachEvent(void (*ptr)()) -{ - if (ptr && _storage) { - _storage->attachEvent(ptr); - } -} - void USBLocalFileSystem::remount() { if (_local) { @@ -64,4 +57,4 @@ _usb->putc(c); } - \ No newline at end of file +
--- a/USBLocalFileSystem.h Sun May 04 00:23:05 2014 +0000 +++ b/USBLocalFileSystem.h Tue May 06 16:05:57 2014 +0900 @@ -40,7 +40,6 @@ * @param name The name used to access the virtual filesystem */ USBLocalFileSystem(StorageInterface* storage, const char* name = "local"); - void attachEvent(void (*ptr)()); void remount(); /** Determine if there is a character available to read @@ -58,13 +57,17 @@ * 0 otherwise */ int writeable(); - + int getc(); void putc(int c); - + + StorageInterface* getStoage() { return _storage; } + LocalStorage* getLocal() { return _local; } + USBStorage2* getUsb() { return _usb; } + private: void init(StorageInterface* storage, const char* name); - const char* _name; + const char* _name; StorageInterface* _storage; LocalStorage* _local; USBStorage2* _usb;
--- a/src/RamDisk.cpp Sun May 04 00:23:05 2014 +0000 +++ b/src/RamDisk.cpp Tue May 06 16:05:57 2014 +0900 @@ -13,18 +13,20 @@ /* virtual */ uint32_t RamDisk::storage_sectors() { + report_sectors_count++; return _sectors; } /* virtual */ uint32_t RamDisk::storage_size() { + report_size_count++; return _sectors * 512; } /* virtual */ int RamDisk::storage_read(uint8_t * data, uint32_t block) { RAMDISK_DBG("R block=%d", block); - update(); + report_read_count++; memset(data, 0x00, 512); if (_sector_image.count(block) > 0) { uint8_t* buf = _sector_image[block]; @@ -47,7 +49,7 @@ /* virtual */ int RamDisk::storage_write(const uint8_t * data, uint32_t block) { - update(); + report_write_count++; int size = block_size(data); if (_sector_image.count(block) > 0) { uint8_t* buf = _sector_image[block];
--- a/src/SDStorage.cpp Sun May 04 00:23:05 2014 +0000 +++ b/src/SDStorage.cpp Tue May 06 16:05:57 2014 +0900 @@ -225,7 +225,7 @@ /* virtual */ int SDStorage::storage_write(const uint8_t *buffer, uint32_t block_number) { SD_DBG2("W %d", block_number); - update(); + report_write_count++; // set write address for single block (CMD24) if (_cmd(24, block_number * cdv) != 0) { return 1; @@ -238,7 +238,7 @@ /* virtual */ int SDStorage::storage_read(uint8_t *buffer, uint32_t block_number) { SD_DBG2("R %d", block_number); - update(); + report_read_count++; // set read address for single block (CMD17) if (_cmd(17, block_number * cdv) != 0) { return 1; @@ -251,7 +251,10 @@ //int SDStorage::disk_status() { return 0; } //int SDStorage::disk_sync() { return 0; } -/* virtual */ uint32_t SDStorage::storage_sectors() { return _sectors; } +/* virtual */ uint32_t SDStorage::storage_sectors() { + report_sectors_count++; + return _sectors; +} // PRIVATE FUNCTIONS @@ -479,6 +482,7 @@ /* virtual */ uint32_t SDStorage::storage_size() { + report_size_count++; return _sectors * 512; }
--- a/src/Storage.h Sun May 04 00:23:05 2014 +0000 +++ b/src/Storage.h Tue May 06 16:05:57 2014 +0900 @@ -51,23 +51,68 @@ class USBStorage2 : public USBMSD2 { public: USBStorage2(StorageInterface* storage): _storage(storage) { + init(); connect(); } + void init() { + block_flag = false; + report_block_count = 0; + report_initialize_count = 0; + report_read_count = 0; + report_write_count = 0; + report_sectors_count = 0; + report_status_count = 0; + report_size_count = 0; + } virtual int disk_read(uint8_t * data, uint64_t block) { + if (block_flag) { + report_block_count++; + return 1; + } + report_read_count++; return _storage->storage_read(data, block); } virtual int disk_write(const uint8_t * data, uint64_t block) { + if (block_flag) { + report_block_count++; + return 1; + } + report_write_count++; return _storage->storage_write(data, block); } - virtual int disk_initialize() { return 0; } + virtual int disk_initialize() { + report_initialize_count++; + return 0; + } virtual uint64_t disk_sectors() { + report_sectors_count++; return _storage->storage_sectors(); } - virtual int disk_status() { return 0; } + virtual int disk_status() { + report_status_count++; + return 0; + } virtual uint64_t disk_size() { + report_size_count++; return _storage->storage_size(); } + bool block(bool flag) { + if (block_flag != flag) { + block_flag = flag; + return true; + } + return false; + } + __IO bool block_flag; + __IO int report_block_count; + __IO int report_initialize_count; + __IO int report_read_count; + __IO int report_write_count; + __IO int report_sectors_count; + __IO int report_status_count; + __IO int report_size_count; + private: StorageInterface* _storage; };
--- a/src/StorageInterface.h Sun May 04 00:23:05 2014 +0000 +++ b/src/StorageInterface.h Tue May 06 16:05:57 2014 +0900 @@ -3,24 +3,18 @@ class StorageInterface { public: StorageInterface() { - onUpdate = NULL; + report_read_count = 0; + report_write_count = 0; + report_sectors_count = 0; + report_size_count = 0; } virtual int storage_read(uint8_t* data, uint32_t block) = 0; virtual int storage_write(const uint8_t* data, uint32_t block) = 0; virtual uint32_t storage_sectors() = 0; virtual uint32_t storage_size() = 0; - - void attachEvent(void (*ptr)()) { - if (ptr != NULL) { - onUpdate = ptr; - } - } -protected: - void (*onUpdate)(); - void update() { - if (onUpdate) { - (*onUpdate)(); - } - } + __IO int report_read_count; + __IO int report_write_count; + __IO int report_sectors_count; + __IO int report_size_count; };