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
USBLocalFileSystem.h@6:528036abfb02, 2014-06-21 (annotated)
- Committer:
- va009039
- Date:
- Sat Jun 21 22:39:59 2014 +0000
- Revision:
- 6:528036abfb02
- Parent:
- 4:8f6857784854
add LPC11U68
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 | 0:39eb4d5b97df | 1 | #pragma once |
va009039 | 0:39eb4d5b97df | 2 | #include "Storage.h" |
va009039 | 0:39eb4d5b97df | 3 | |
va009039 | 1:00c9eb8af5c2 | 4 | /** Access the LocalFileSystem using RamDisk(64KB) |
va009039 | 1:00c9eb8af5c2 | 5 | * |
va009039 | 1:00c9eb8af5c2 | 6 | * @code |
va009039 | 1:00c9eb8af5c2 | 7 | * #include "USBLocalFileSystem.h" |
va009039 | 1:00c9eb8af5c2 | 8 | * |
va009039 | 1:00c9eb8af5c2 | 9 | * int main() { |
va009039 | 1:00c9eb8af5c2 | 10 | * USBLocalFileSystem* usb_local = new USBLocalFileSystem(); // RamDisk(64KB) |
va009039 | 1:00c9eb8af5c2 | 11 | * |
va009039 | 1:00c9eb8af5c2 | 12 | * FILE *fp = fopen("/local/mbed.txt", "a"); |
va009039 | 1:00c9eb8af5c2 | 13 | * fprintf(fp, "Hello World!\n"); |
va009039 | 1:00c9eb8af5c2 | 14 | * fclose(fp); |
va009039 | 1:00c9eb8af5c2 | 15 | * } |
va009039 | 1:00c9eb8af5c2 | 16 | * @endcode |
va009039 | 1:00c9eb8af5c2 | 17 | */ |
va009039 | 0:39eb4d5b97df | 18 | class USBLocalFileSystem { |
va009039 | 0:39eb4d5b97df | 19 | public: |
va009039 | 1:00c9eb8af5c2 | 20 | |
va009039 | 1:00c9eb8af5c2 | 21 | /** Create the Local File System using RamDisk(64KB) |
va009039 | 1:00c9eb8af5c2 | 22 | * |
va009039 | 1:00c9eb8af5c2 | 23 | * @param name The name used to access the virtual filesystem |
va009039 | 1:00c9eb8af5c2 | 24 | */ |
va009039 | 0:39eb4d5b97df | 25 | USBLocalFileSystem(const char* name = "local"); |
va009039 | 1:00c9eb8af5c2 | 26 | |
va009039 | 1:00c9eb8af5c2 | 27 | /** Create the Local File System for accessing an SD Card using SPI |
va009039 | 1:00c9eb8af5c2 | 28 | * |
va009039 | 1:00c9eb8af5c2 | 29 | * @param mosi SPI mosi pin connected to SD Card |
va009039 | 1:00c9eb8af5c2 | 30 | * @param miso SPI miso pin conencted to SD Card |
va009039 | 1:00c9eb8af5c2 | 31 | * @param sclk SPI sclk pin connected to SD Card |
va009039 | 1:00c9eb8af5c2 | 32 | * @param cs DigitalOut pin used as SD Card chip select |
va009039 | 1:00c9eb8af5c2 | 33 | * @param name The name used to access the virtual filesystem |
va009039 | 1:00c9eb8af5c2 | 34 | */ |
va009039 | 0:39eb4d5b97df | 35 | USBLocalFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name = "local"); |
va009039 | 1:00c9eb8af5c2 | 36 | |
va009039 | 1:00c9eb8af5c2 | 37 | /** Create the Local File System using StorageInterface |
va009039 | 1:00c9eb8af5c2 | 38 | * |
va009039 | 1:00c9eb8af5c2 | 39 | * @param storage StorageInterface |
va009039 | 1:00c9eb8af5c2 | 40 | * @param name The name used to access the virtual filesystem |
va009039 | 1:00c9eb8af5c2 | 41 | */ |
va009039 | 0:39eb4d5b97df | 42 | USBLocalFileSystem(StorageInterface* storage, const char* name = "local"); |
va009039 |
4:8f6857784854 | 43 | |
va009039 |
4:8f6857784854 | 44 | void remount(); // remount local storage |
va009039 |
4:8f6857784854 | 45 | int lock(bool f); |
va009039 |
4:8f6857784854 | 46 | bool find(char* name, size_t size, const char* pat); |
va009039 | 1:00c9eb8af5c2 | 47 | |
va009039 | 1:00c9eb8af5c2 | 48 | /** Determine if there is a character available to read |
va009039 | 1:00c9eb8af5c2 | 49 | * |
va009039 | 1:00c9eb8af5c2 | 50 | * @returns |
va009039 | 1:00c9eb8af5c2 | 51 | * 1 if there is a character available to read, |
va009039 | 1:00c9eb8af5c2 | 52 | * 0 otherwise |
va009039 | 1:00c9eb8af5c2 | 53 | */ |
va009039 | 1:00c9eb8af5c2 | 54 | int readable(); |
va009039 | 1:00c9eb8af5c2 | 55 | |
va009039 | 1:00c9eb8af5c2 | 56 | /** Determine if there is space available to write a character |
va009039 | 1:00c9eb8af5c2 | 57 | * |
va009039 | 1:00c9eb8af5c2 | 58 | * @returns |
va009039 | 1:00c9eb8af5c2 | 59 | * 1 if there is space to write a character, |
va009039 | 1:00c9eb8af5c2 | 60 | * 0 otherwise |
va009039 | 1:00c9eb8af5c2 | 61 | */ |
va009039 | 1:00c9eb8af5c2 | 62 | int writeable(); |
va009039 |
2:97c314eae8b8 | 63 | |
va009039 |
4:8f6857784854 | 64 | /** Read a char from the serial port |
va009039 |
4:8f6857784854 | 65 | * |
va009039 |
4:8f6857784854 | 66 | * @returns The char read from the serial port |
va009039 |
4:8f6857784854 | 67 | */ |
va009039 | 0:39eb4d5b97df | 68 | int getc(); |
va009039 |
4:8f6857784854 | 69 | |
va009039 |
4:8f6857784854 | 70 | /** Write a char to the serial port |
va009039 |
4:8f6857784854 | 71 | * |
va009039 |
4:8f6857784854 | 72 | * @param c The char to write |
va009039 |
4:8f6857784854 | 73 | * |
va009039 |
4:8f6857784854 | 74 | * @returns The written char or -1 if an error occured |
va009039 |
4:8f6857784854 | 75 | */ |
va009039 |
4:8f6857784854 | 76 | int putc(int c); |
va009039 |
4:8f6857784854 | 77 | |
va009039 |
4:8f6857784854 | 78 | /** Write a string to the serial port |
va009039 |
4:8f6857784854 | 79 | * |
va009039 |
4:8f6857784854 | 80 | * @param str The string to write |
va009039 |
4:8f6857784854 | 81 | * |
va009039 |
4:8f6857784854 | 82 | * @returns 0 if the write succeeds, EOF for error |
va009039 |
4:8f6857784854 | 83 | */ |
va009039 |
4:8f6857784854 | 84 | int puts(const char* str); |
va009039 |
4:8f6857784854 | 85 | |
va009039 |
4:8f6857784854 | 86 | void attachSendBreak(void (*fptr)(uint16_t duration)); |
va009039 |
4:8f6857784854 | 87 | void attachControlLineState(void (*fptr)(int dts, int dtr)); |
va009039 |
4:8f6857784854 | 88 | void attachSettingChanged(void (*fptr)(int baud, int bits, int parity, int stop)); |
va009039 |
2:97c314eae8b8 | 89 | |
va009039 |
2:97c314eae8b8 | 90 | StorageInterface* getStoage() { return _storage; } |
va009039 |
2:97c314eae8b8 | 91 | LocalStorage* getLocal() { return _local; } |
va009039 |
2:97c314eae8b8 | 92 | USBStorage2* getUsb() { return _usb; } |
va009039 |
2:97c314eae8b8 | 93 | |
va009039 | 0:39eb4d5b97df | 94 | private: |
va009039 | 0:39eb4d5b97df | 95 | void init(StorageInterface* storage, const char* name); |
va009039 |
2:97c314eae8b8 | 96 | const char* _name; |
va009039 | 0:39eb4d5b97df | 97 | StorageInterface* _storage; |
va009039 | 0:39eb4d5b97df | 98 | LocalStorage* _local; |
va009039 | 0:39eb4d5b97df | 99 | USBStorage2* _usb; |
va009039 | 0:39eb4d5b97df | 100 | }; |