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
USBMSD2/USB_CDC.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 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
va009039 | 0:39eb4d5b97df | 2 | * |
va009039 | 0:39eb4d5b97df | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
va009039 | 0:39eb4d5b97df | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
va009039 | 0:39eb4d5b97df | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
va009039 | 0:39eb4d5b97df | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
va009039 | 0:39eb4d5b97df | 7 | * Software is furnished to do so, subject to the following conditions: |
va009039 | 0:39eb4d5b97df | 8 | * |
va009039 | 0:39eb4d5b97df | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
va009039 | 0:39eb4d5b97df | 10 | * substantial portions of the Software. |
va009039 | 0:39eb4d5b97df | 11 | * |
va009039 | 0:39eb4d5b97df | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
va009039 | 0:39eb4d5b97df | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
va009039 | 0:39eb4d5b97df | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
va009039 | 0:39eb4d5b97df | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
va009039 | 0:39eb4d5b97df | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
va009039 | 0:39eb4d5b97df | 17 | */ |
va009039 | 0:39eb4d5b97df | 18 | |
va009039 | 0:39eb4d5b97df | 19 | #pragma once |
va009039 | 0:39eb4d5b97df | 20 | |
va009039 | 0:39eb4d5b97df | 21 | #include "USBDevice.h" |
va009039 | 0:39eb4d5b97df | 22 | #include "CircBuffer.h" |
va009039 | 0:39eb4d5b97df | 23 | |
va009039 | 0:39eb4d5b97df | 24 | #if defined(TARGET_LPC1768)||defined(TARGET_LPC4088) |
va009039 | 0:39eb4d5b97df | 25 | #define CDC_EPINT_IN EP1IN |
va009039 | 0:39eb4d5b97df | 26 | #define CDC_EPBULK_IN EP5IN |
va009039 | 0:39eb4d5b97df | 27 | #define CDC_EPBULK_OUT EP5OUT |
va009039 | 6:528036abfb02 | 28 | #elif defined(TARGET_LPC1347)||defined(TARGET_LPC11U24)||defined(TARGET_LPC11U35_401)||defined(TARGET_LPC11U35_501)||defined(TARGET_LPC1549)||defined(TARGET_LPC11U68) |
va009039 | 0:39eb4d5b97df | 29 | #define CDC_EPINT_IN EP1IN |
va009039 | 0:39eb4d5b97df | 30 | #define CDC_EPBULK_IN EP3IN |
va009039 | 0:39eb4d5b97df | 31 | #define CDC_EPBULK_OUT EP3OUT |
va009039 | 0:39eb4d5b97df | 32 | #elif defined(TARGET_KL25Z)||defined(TARGET_KL46Z) |
va009039 | 0:39eb4d5b97df | 33 | #define CDC_EPINT_IN EP1IN |
va009039 | 0:39eb4d5b97df | 34 | #define CDC_EPBULK_IN EP5IN |
va009039 | 0:39eb4d5b97df | 35 | #define CDC_EPBULK_OUT EP5OUT |
va009039 | 0:39eb4d5b97df | 36 | #else |
va009039 | 0:39eb4d5b97df | 37 | #error "target type error" |
va009039 | 0:39eb4d5b97df | 38 | #endif |
va009039 | 0:39eb4d5b97df | 39 | |
va009039 | 0:39eb4d5b97df | 40 | class USB_CDC { |
va009039 | 0:39eb4d5b97df | 41 | public: |
va009039 | 0:39eb4d5b97df | 42 | USB_CDC(USBDevice* device); |
va009039 | 0:39eb4d5b97df | 43 | |
va009039 | 0:39eb4d5b97df | 44 | /** target to virtual COM |
va009039 | 0:39eb4d5b97df | 45 | */ |
va009039 | 0:39eb4d5b97df | 46 | void putc(int c); |
va009039 | 0:39eb4d5b97df | 47 | |
va009039 | 0:39eb4d5b97df | 48 | /** virtial COM to target |
va009039 | 0:39eb4d5b97df | 49 | */ |
va009039 | 0:39eb4d5b97df | 50 | int getc(); |
va009039 | 0:39eb4d5b97df | 51 | |
va009039 | 0:39eb4d5b97df | 52 | int readable(); |
va009039 | 0:39eb4d5b97df | 53 | |
va009039 | 0:39eb4d5b97df | 54 | int writeable(); |
va009039 | 0:39eb4d5b97df | 55 | |
va009039 | 0:39eb4d5b97df | 56 | /* |
va009039 | 0:39eb4d5b97df | 57 | * Send a buffer |
va009039 | 0:39eb4d5b97df | 58 | * |
va009039 | 0:39eb4d5b97df | 59 | * @param endpoint endpoint which will be sent the buffer |
va009039 | 0:39eb4d5b97df | 60 | * @param buffer buffer to be sent |
va009039 | 0:39eb4d5b97df | 61 | * @param size length of the buffer |
va009039 | 0:39eb4d5b97df | 62 | * @returns true if successful |
va009039 | 0:39eb4d5b97df | 63 | */ |
va009039 | 0:39eb4d5b97df | 64 | bool send(uint8_t * buffer, uint32_t size); |
va009039 | 0:39eb4d5b97df | 65 | |
va009039 | 0:39eb4d5b97df | 66 | /* |
va009039 | 0:39eb4d5b97df | 67 | * Read a buffer from a certain endpoint. Warning: blocking |
va009039 | 0:39eb4d5b97df | 68 | * |
va009039 | 0:39eb4d5b97df | 69 | * @param endpoint endpoint to read |
va009039 | 0:39eb4d5b97df | 70 | * @param buffer buffer where will be stored bytes |
va009039 | 0:39eb4d5b97df | 71 | * @param size the number of bytes read will be stored in *size |
va009039 | 0:39eb4d5b97df | 72 | * @param maxSize the maximum length that can be read |
va009039 | 0:39eb4d5b97df | 73 | * @returns true if successful |
va009039 | 0:39eb4d5b97df | 74 | */ |
va009039 | 0:39eb4d5b97df | 75 | bool readEP(uint8_t * buffer, uint32_t * size); |
va009039 | 0:39eb4d5b97df | 76 | |
va009039 | 0:39eb4d5b97df | 77 | /* |
va009039 | 0:39eb4d5b97df | 78 | * Read a buffer from a certain endpoint. Warning: non blocking |
va009039 | 0:39eb4d5b97df | 79 | * |
va009039 | 0:39eb4d5b97df | 80 | * @param endpoint endpoint to read |
va009039 | 0:39eb4d5b97df | 81 | * @param buffer buffer where will be stored bytes |
va009039 | 0:39eb4d5b97df | 82 | * @param size the number of bytes read will be stored in *size |
va009039 | 0:39eb4d5b97df | 83 | * @param maxSize the maximum length that can be read |
va009039 | 0:39eb4d5b97df | 84 | * @returns true if successful |
va009039 | 0:39eb4d5b97df | 85 | */ |
va009039 | 0:39eb4d5b97df | 86 | bool readEP_NB(uint8_t * buffer, uint32_t * size); |
va009039 | 0:39eb4d5b97df | 87 | |
va009039 | 0:39eb4d5b97df | 88 | bool Request_callback(CONTROL_TRANSFER* transfer); |
va009039 | 0:39eb4d5b97df | 89 | bool RequestCompleted_callback(CONTROL_TRANSFER* transfer, uint8_t* buf, int length); |
va009039 | 0:39eb4d5b97df | 90 | bool EPBULK_OUT_callback(); |
va009039 |
4:8f6857784854 | 91 | |
va009039 |
4:8f6857784854 | 92 | /** |
va009039 |
4:8f6857784854 | 93 | * Attach a callback to call when serial's settings are changed. |
va009039 |
4:8f6857784854 | 94 | * |
va009039 |
4:8f6857784854 | 95 | * @param fptr function pointer |
va009039 |
4:8f6857784854 | 96 | */ |
va009039 |
4:8f6857784854 | 97 | void attach(void (*fptr)(int baud, int bits, int parity, int stop)) { |
va009039 |
4:8f6857784854 | 98 | settingsChangedCallback = fptr; |
va009039 |
4:8f6857784854 | 99 | } |
va009039 |
4:8f6857784854 | 100 | |
va009039 |
4:8f6857784854 | 101 | void attachControlLineStateChanged(void (*fptr)(int rts, int dtr)) { |
va009039 |
4:8f6857784854 | 102 | controlLineStateChangedCallback = fptr; |
va009039 |
4:8f6857784854 | 103 | } |
va009039 |
4:8f6857784854 | 104 | |
va009039 |
4:8f6857784854 | 105 | void attachSendBreak(void (*fptr)(uint16_t duration)) { |
va009039 |
4:8f6857784854 | 106 | sendBreakCallback = fptr; |
va009039 |
4:8f6857784854 | 107 | } |
va009039 | 0:39eb4d5b97df | 108 | |
va009039 |
4:8f6857784854 | 109 | protected: |
va009039 |
4:8f6857784854 | 110 | void lineCodingChanged(int baud, int bits, int parity, int stop); |
va009039 |
4:8f6857784854 | 111 | void controlLineStateChanged(int rts, int dtr); |
va009039 |
4:8f6857784854 | 112 | void sendBreak(uint16_t duration); |
va009039 |
4:8f6857784854 | 113 | |
va009039 | 0:39eb4d5b97df | 114 | private: |
va009039 | 0:39eb4d5b97df | 115 | USBDevice* _device; |
va009039 | 0:39eb4d5b97df | 116 | CircBuffer<uint8_t> _rx_buf; |
va009039 |
4:8f6857784854 | 117 | void (*settingsChangedCallback)(int baud, int bits, int parity, int stop); |
va009039 |
4:8f6857784854 | 118 | void (*controlLineStateChangedCallback)(int rts, int dtr); |
va009039 |
4:8f6857784854 | 119 | void (*sendBreakCallback)(uint16_t duration); |
va009039 | 0:39eb4d5b97df | 120 | volatile bool terminal_connected; |
va009039 | 0:39eb4d5b97df | 121 | }; |
va009039 |
4:8f6857784854 | 122 |