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.cpp@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 | #include "USB_CDC.h" |
va009039 | 0:39eb4d5b97df | 20 | |
va009039 | 0:39eb4d5b97df | 21 | #if (DEBUG2 > 3) |
va009039 | 0:39eb4d5b97df | 22 | #define CDC_DBG(...) do{fprintf(stderr,"[%s@%d] ",__PRETTY_FUNCTION__,__LINE__);fprintf(stderr,__VA_ARGS__);fprintf(stderr,"\r\n");} while(0); |
va009039 | 0:39eb4d5b97df | 23 | #define CDC_DBG_HEX(A,B) while(0); |
va009039 | 0:39eb4d5b97df | 24 | #else |
va009039 | 0:39eb4d5b97df | 25 | #define CDC_DBG(...) while(0) |
va009039 | 0:39eb4d5b97df | 26 | #define CDC_DBG_HEX(A,B) while(0) |
va009039 | 0:39eb4d5b97df | 27 | #endif |
va009039 | 0:39eb4d5b97df | 28 | |
va009039 |
4:8f6857784854 | 29 | static uint8_t cdc_line_coding[7]= {0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08}; |
va009039 |
4:8f6857784854 | 30 | |
va009039 | 0:39eb4d5b97df | 31 | #define CDC_SET_LINE_CODING 0x20 |
va009039 | 0:39eb4d5b97df | 32 | #define CDC_GET_LINE_CODING 0x21 |
va009039 | 0:39eb4d5b97df | 33 | #define CDC_SET_CONTROL_LINE_STATE 0x22 |
va009039 | 0:39eb4d5b97df | 34 | #define CDC_SEND_BREAK 0x23 |
va009039 | 0:39eb4d5b97df | 35 | |
va009039 | 0:39eb4d5b97df | 36 | #define MAX_CDC_REPORT_SIZE MAX_PACKET_SIZE_EPBULK |
va009039 | 0:39eb4d5b97df | 37 | |
va009039 | 0:39eb4d5b97df | 38 | USB_CDC::USB_CDC(USBDevice* device) : _device(device), _rx_buf(128) |
va009039 |
4:8f6857784854 | 39 | ,settingsChangedCallback(NULL),controlLineStateChangedCallback(NULL),sendBreakCallback(NULL) |
va009039 | 0:39eb4d5b97df | 40 | { |
va009039 | 0:39eb4d5b97df | 41 | CDC_DBG("device=%p", device); |
va009039 | 0:39eb4d5b97df | 42 | |
va009039 | 0:39eb4d5b97df | 43 | terminal_connected = false; |
va009039 | 0:39eb4d5b97df | 44 | //USBDevice::connect(); |
va009039 | 0:39eb4d5b97df | 45 | } |
va009039 | 0:39eb4d5b97df | 46 | |
va009039 | 0:39eb4d5b97df | 47 | void USB_CDC::putc(int c) |
va009039 | 0:39eb4d5b97df | 48 | { |
va009039 | 0:39eb4d5b97df | 49 | if (terminal_connected) { |
va009039 | 0:39eb4d5b97df | 50 | uint8_t buf[1]; |
va009039 | 0:39eb4d5b97df | 51 | buf[0] = c; |
va009039 | 0:39eb4d5b97df | 52 | _device->write(CDC_EPBULK_IN, buf, sizeof(buf), MAX_CDC_REPORT_SIZE); |
va009039 | 0:39eb4d5b97df | 53 | } |
va009039 | 0:39eb4d5b97df | 54 | } |
va009039 | 0:39eb4d5b97df | 55 | |
va009039 | 0:39eb4d5b97df | 56 | int USB_CDC::getc() |
va009039 | 0:39eb4d5b97df | 57 | { |
va009039 | 0:39eb4d5b97df | 58 | uint8_t c = 0; |
va009039 | 0:39eb4d5b97df | 59 | while (_rx_buf.isEmpty()); |
va009039 | 0:39eb4d5b97df | 60 | _rx_buf.dequeue(&c); |
va009039 | 0:39eb4d5b97df | 61 | return c; |
va009039 | 0:39eb4d5b97df | 62 | } |
va009039 | 0:39eb4d5b97df | 63 | |
va009039 | 0:39eb4d5b97df | 64 | int USB_CDC::readable() |
va009039 | 0:39eb4d5b97df | 65 | { |
va009039 | 0:39eb4d5b97df | 66 | return _rx_buf.available() > 0 ? 1 : 0; |
va009039 | 0:39eb4d5b97df | 67 | } |
va009039 | 0:39eb4d5b97df | 68 | |
va009039 | 0:39eb4d5b97df | 69 | int USB_CDC::writeable() |
va009039 | 0:39eb4d5b97df | 70 | { |
va009039 | 0:39eb4d5b97df | 71 | return 1; |
va009039 | 0:39eb4d5b97df | 72 | } |
va009039 | 0:39eb4d5b97df | 73 | |
va009039 | 0:39eb4d5b97df | 74 | bool USB_CDC::send(uint8_t * buffer, uint32_t size) { |
va009039 | 0:39eb4d5b97df | 75 | return _device->write(CDC_EPBULK_IN, buffer, size, MAX_CDC_REPORT_SIZE); |
va009039 | 0:39eb4d5b97df | 76 | } |
va009039 | 0:39eb4d5b97df | 77 | |
va009039 | 0:39eb4d5b97df | 78 | bool USB_CDC::readEP(uint8_t * buffer, uint32_t * size) { |
va009039 | 0:39eb4d5b97df | 79 | if (!_device->readEP(CDC_EPBULK_OUT, buffer, size, MAX_CDC_REPORT_SIZE)) |
va009039 | 0:39eb4d5b97df | 80 | return false; |
va009039 | 0:39eb4d5b97df | 81 | if (!_device->readStart(CDC_EPBULK_OUT, MAX_CDC_REPORT_SIZE)) |
va009039 | 0:39eb4d5b97df | 82 | return false; |
va009039 | 0:39eb4d5b97df | 83 | return true; |
va009039 | 0:39eb4d5b97df | 84 | } |
va009039 | 0:39eb4d5b97df | 85 | |
va009039 | 0:39eb4d5b97df | 86 | bool USB_CDC::readEP_NB(uint8_t * buffer, uint32_t * size) { |
va009039 | 0:39eb4d5b97df | 87 | if (!_device->readEP_NB(CDC_EPBULK_OUT, buffer, size, MAX_CDC_REPORT_SIZE)) |
va009039 | 0:39eb4d5b97df | 88 | return false; |
va009039 | 0:39eb4d5b97df | 89 | if (!_device->readStart(CDC_EPBULK_OUT, MAX_CDC_REPORT_SIZE)) |
va009039 | 0:39eb4d5b97df | 90 | return false; |
va009039 | 0:39eb4d5b97df | 91 | return true; |
va009039 | 0:39eb4d5b97df | 92 | } |
va009039 | 0:39eb4d5b97df | 93 | |
va009039 | 0:39eb4d5b97df | 94 | bool USB_CDC::Request_callback(CONTROL_TRANSFER* transfer) |
va009039 | 0:39eb4d5b97df | 95 | { |
va009039 | 0:39eb4d5b97df | 96 | if (transfer->setup.bmRequestType.Type == CLASS_TYPE) { |
va009039 | 0:39eb4d5b97df | 97 | switch (transfer->setup.bRequest) { |
va009039 | 0:39eb4d5b97df | 98 | case CDC_SET_LINE_CODING: // 0x20 |
va009039 | 0:39eb4d5b97df | 99 | transfer->remaining = 7; |
va009039 | 0:39eb4d5b97df | 100 | transfer->notify = true; |
va009039 | 0:39eb4d5b97df | 101 | terminal_connected = true; |
va009039 | 0:39eb4d5b97df | 102 | return true; |
va009039 | 0:39eb4d5b97df | 103 | |
va009039 | 0:39eb4d5b97df | 104 | case CDC_GET_LINE_CODING: // x021 |
va009039 | 0:39eb4d5b97df | 105 | transfer->remaining = 7; |
va009039 | 0:39eb4d5b97df | 106 | transfer->ptr = cdc_line_coding; |
va009039 | 0:39eb4d5b97df | 107 | transfer->direction = DEVICE_TO_HOST; |
va009039 | 0:39eb4d5b97df | 108 | return true; |
va009039 | 0:39eb4d5b97df | 109 | |
va009039 | 0:39eb4d5b97df | 110 | case CDC_SET_CONTROL_LINE_STATE: // 0x22 |
va009039 |
4:8f6857784854 | 111 | controlLineStateChanged((transfer->setup.wValue>>1) & 1, (transfer->setup.wValue) & 1); |
va009039 | 0:39eb4d5b97df | 112 | terminal_connected = false; |
va009039 | 0:39eb4d5b97df | 113 | return true; |
va009039 | 0:39eb4d5b97df | 114 | |
va009039 | 0:39eb4d5b97df | 115 | case CDC_SEND_BREAK: // 0x23 |
va009039 |
4:8f6857784854 | 116 | sendBreak(transfer->setup.wValue); |
va009039 | 0:39eb4d5b97df | 117 | return true; |
va009039 | 0:39eb4d5b97df | 118 | } |
va009039 | 0:39eb4d5b97df | 119 | } |
va009039 | 0:39eb4d5b97df | 120 | return false; |
va009039 | 0:39eb4d5b97df | 121 | } |
va009039 | 0:39eb4d5b97df | 122 | |
va009039 | 0:39eb4d5b97df | 123 | bool USB_CDC::RequestCompleted_callback(CONTROL_TRANSFER* transfer, uint8_t* buf, int length) |
va009039 | 0:39eb4d5b97df | 124 | { |
va009039 | 0:39eb4d5b97df | 125 | CDC_DBG("transer=%p", transfer); |
va009039 | 0:39eb4d5b97df | 126 | if (transfer->setup.bmRequestType.Type == CLASS_TYPE) { |
va009039 |
4:8f6857784854 | 127 | if (transfer->setup.bRequest == CDC_SET_LINE_CODING) { |
va009039 |
4:8f6857784854 | 128 | if (memcmp(cdc_line_coding, buf, 7)) { |
va009039 |
4:8f6857784854 | 129 | memcpy(cdc_line_coding, buf, 7); |
va009039 |
4:8f6857784854 | 130 | |
va009039 |
4:8f6857784854 | 131 | int baud = buf[0] + (buf[1] << 8) |
va009039 |
4:8f6857784854 | 132 | + (buf[2] << 16) + (buf[3] << 24); |
va009039 |
4:8f6857784854 | 133 | int stop = (buf[4] == 0) ? 1 : 2; |
va009039 |
4:8f6857784854 | 134 | int bits = buf[6]; |
va009039 |
4:8f6857784854 | 135 | int parity = buf[5]; |
va009039 |
4:8f6857784854 | 136 | lineCodingChanged(baud, bits, parity, stop); |
va009039 | 0:39eb4d5b97df | 137 | return true; |
va009039 |
4:8f6857784854 | 138 | } |
va009039 | 0:39eb4d5b97df | 139 | } |
va009039 | 0:39eb4d5b97df | 140 | } |
va009039 | 0:39eb4d5b97df | 141 | CDC_DBG_HEX((uint8_t*)transfer, sizeof(CONTROL_TRANSFER)); |
va009039 | 0:39eb4d5b97df | 142 | return false; |
va009039 | 0:39eb4d5b97df | 143 | } |
va009039 | 0:39eb4d5b97df | 144 | |
va009039 | 0:39eb4d5b97df | 145 | bool USB_CDC::EPBULK_OUT_callback() // virtual COM to target |
va009039 | 0:39eb4d5b97df | 146 | { |
va009039 | 0:39eb4d5b97df | 147 | uint8_t buf[MAX_CDC_REPORT_SIZE]; |
va009039 | 0:39eb4d5b97df | 148 | uint32_t size = 0; |
va009039 | 0:39eb4d5b97df | 149 | //we read the packet received and put it on the circular buffer |
va009039 | 0:39eb4d5b97df | 150 | _device->readEP(CDC_EPBULK_OUT, buf, &size, MAX_CDC_REPORT_SIZE); |
va009039 | 0:39eb4d5b97df | 151 | CDC_DBG("size=%d", size); |
va009039 | 0:39eb4d5b97df | 152 | for(int i = 0; i < size; i++) { |
va009039 | 0:39eb4d5b97df | 153 | _rx_buf.queue(buf[i]); |
va009039 | 0:39eb4d5b97df | 154 | } |
va009039 | 0:39eb4d5b97df | 155 | |
va009039 | 0:39eb4d5b97df | 156 | // We reactivate the endpoint to receive next characters |
va009039 | 0:39eb4d5b97df | 157 | _device->readStart(CDC_EPBULK_OUT, MAX_PACKET_SIZE_EPBULK); |
va009039 | 0:39eb4d5b97df | 158 | return true; |
va009039 | 0:39eb4d5b97df | 159 | } |
va009039 |
4:8f6857784854 | 160 | |
va009039 |
4:8f6857784854 | 161 | void USB_CDC::lineCodingChanged(int baud, int bits, int parity, int stop) |
va009039 |
4:8f6857784854 | 162 | { |
va009039 |
4:8f6857784854 | 163 | CDC_DBG("baud=%d,bits=%d,parity=%d,stop=%d", baud,bits, parity, stop); |
va009039 |
4:8f6857784854 | 164 | if (settingsChangedCallback) { |
va009039 |
4:8f6857784854 | 165 | settingsChangedCallback(baud, bits, parity, stop); |
va009039 |
4:8f6857784854 | 166 | } |
va009039 |
4:8f6857784854 | 167 | } |
va009039 |
4:8f6857784854 | 168 | |
va009039 |
4:8f6857784854 | 169 | void USB_CDC::controlLineStateChanged(int rts, int dtr) |
va009039 |
4:8f6857784854 | 170 | { |
va009039 |
4:8f6857784854 | 171 | CDC_DBG("rts=%d,dtr=%d", rts, dtr); |
va009039 |
4:8f6857784854 | 172 | if (controlLineStateChangedCallback) { |
va009039 |
4:8f6857784854 | 173 | controlLineStateChangedCallback(rts, dtr); |
va009039 |
4:8f6857784854 | 174 | } |
va009039 |
4:8f6857784854 | 175 | } |
va009039 |
4:8f6857784854 | 176 | |
va009039 |
4:8f6857784854 | 177 | void USB_CDC::sendBreak(uint16_t duration) |
va009039 |
4:8f6857784854 | 178 | { |
va009039 |
4:8f6857784854 | 179 | CDC_DBG("duration=%u", duration) |
va009039 |
4:8f6857784854 | 180 | if (sendBreakCallback) { |
va009039 |
4:8f6857784854 | 181 | sendBreakCallback(duration); |
va009039 |
4:8f6857784854 | 182 | } |
va009039 |
4:8f6857784854 | 183 | } |
va009039 |
4:8f6857784854 | 184 |