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
- Committer:
- va009039
- Date:
- 2014-06-21
- Revision:
- 6:528036abfb02
- Parent:
- 4:8f6857784854
File content as of revision 6:528036abfb02:
/* Copyright (c) 2010-2011 mbed.org, MIT License * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software * and associated documentation files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all copies or * substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "USB_CDC.h" #if (DEBUG2 > 3) #define CDC_DBG(...) do{fprintf(stderr,"[%s@%d] ",__PRETTY_FUNCTION__,__LINE__);fprintf(stderr,__VA_ARGS__);fprintf(stderr,"\r\n");} while(0); #define CDC_DBG_HEX(A,B) while(0); #else #define CDC_DBG(...) while(0) #define CDC_DBG_HEX(A,B) while(0) #endif static uint8_t cdc_line_coding[7]= {0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08}; #define CDC_SET_LINE_CODING 0x20 #define CDC_GET_LINE_CODING 0x21 #define CDC_SET_CONTROL_LINE_STATE 0x22 #define CDC_SEND_BREAK 0x23 #define MAX_CDC_REPORT_SIZE MAX_PACKET_SIZE_EPBULK USB_CDC::USB_CDC(USBDevice* device) : _device(device), _rx_buf(128) ,settingsChangedCallback(NULL),controlLineStateChangedCallback(NULL),sendBreakCallback(NULL) { CDC_DBG("device=%p", device); terminal_connected = false; //USBDevice::connect(); } void USB_CDC::putc(int c) { if (terminal_connected) { uint8_t buf[1]; buf[0] = c; _device->write(CDC_EPBULK_IN, buf, sizeof(buf), MAX_CDC_REPORT_SIZE); } } int USB_CDC::getc() { uint8_t c = 0; while (_rx_buf.isEmpty()); _rx_buf.dequeue(&c); return c; } int USB_CDC::readable() { return _rx_buf.available() > 0 ? 1 : 0; } int USB_CDC::writeable() { return 1; } bool USB_CDC::send(uint8_t * buffer, uint32_t size) { return _device->write(CDC_EPBULK_IN, buffer, size, MAX_CDC_REPORT_SIZE); } bool USB_CDC::readEP(uint8_t * buffer, uint32_t * size) { if (!_device->readEP(CDC_EPBULK_OUT, buffer, size, MAX_CDC_REPORT_SIZE)) return false; if (!_device->readStart(CDC_EPBULK_OUT, MAX_CDC_REPORT_SIZE)) return false; return true; } bool USB_CDC::readEP_NB(uint8_t * buffer, uint32_t * size) { if (!_device->readEP_NB(CDC_EPBULK_OUT, buffer, size, MAX_CDC_REPORT_SIZE)) return false; if (!_device->readStart(CDC_EPBULK_OUT, MAX_CDC_REPORT_SIZE)) return false; return true; } bool USB_CDC::Request_callback(CONTROL_TRANSFER* transfer) { if (transfer->setup.bmRequestType.Type == CLASS_TYPE) { switch (transfer->setup.bRequest) { case CDC_SET_LINE_CODING: // 0x20 transfer->remaining = 7; transfer->notify = true; terminal_connected = true; return true; case CDC_GET_LINE_CODING: // x021 transfer->remaining = 7; transfer->ptr = cdc_line_coding; transfer->direction = DEVICE_TO_HOST; return true; case CDC_SET_CONTROL_LINE_STATE: // 0x22 controlLineStateChanged((transfer->setup.wValue>>1) & 1, (transfer->setup.wValue) & 1); terminal_connected = false; return true; case CDC_SEND_BREAK: // 0x23 sendBreak(transfer->setup.wValue); return true; } } return false; } bool USB_CDC::RequestCompleted_callback(CONTROL_TRANSFER* transfer, uint8_t* buf, int length) { CDC_DBG("transer=%p", transfer); if (transfer->setup.bmRequestType.Type == CLASS_TYPE) { if (transfer->setup.bRequest == CDC_SET_LINE_CODING) { if (memcmp(cdc_line_coding, buf, 7)) { memcpy(cdc_line_coding, buf, 7); int baud = buf[0] + (buf[1] << 8) + (buf[2] << 16) + (buf[3] << 24); int stop = (buf[4] == 0) ? 1 : 2; int bits = buf[6]; int parity = buf[5]; lineCodingChanged(baud, bits, parity, stop); return true; } } } CDC_DBG_HEX((uint8_t*)transfer, sizeof(CONTROL_TRANSFER)); return false; } bool USB_CDC::EPBULK_OUT_callback() // virtual COM to target { uint8_t buf[MAX_CDC_REPORT_SIZE]; uint32_t size = 0; //we read the packet received and put it on the circular buffer _device->readEP(CDC_EPBULK_OUT, buf, &size, MAX_CDC_REPORT_SIZE); CDC_DBG("size=%d", size); for(int i = 0; i < size; i++) { _rx_buf.queue(buf[i]); } // We reactivate the endpoint to receive next characters _device->readStart(CDC_EPBULK_OUT, MAX_PACKET_SIZE_EPBULK); return true; } void USB_CDC::lineCodingChanged(int baud, int bits, int parity, int stop) { CDC_DBG("baud=%d,bits=%d,parity=%d,stop=%d", baud,bits, parity, stop); if (settingsChangedCallback) { settingsChangedCallback(baud, bits, parity, stop); } } void USB_CDC::controlLineStateChanged(int rts, int dtr) { CDC_DBG("rts=%d,dtr=%d", rts, dtr); if (controlLineStateChangedCallback) { controlLineStateChangedCallback(rts, dtr); } } void USB_CDC::sendBreak(uint16_t duration) { CDC_DBG("duration=%u", duration) if (sendBreakCallback) { sendBreakCallback(duration); } }