Emulation of LocalFileSystem with virtual COM.

Dependencies:   USBDevice

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_HID.cpp

Committer:
va009039
Date:
2014-06-21
Revision:
6:528036abfb02
Parent:
0:39eb4d5b97df

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_HID.h"

#if (DEBUG2 > 3)
#define HID_DBG(...) do{fprintf(stderr,"[%s@%d] ",__PRETTY_FUNCTION__,__LINE__);fprintf(stderr,__VA_ARGS__);fprintf(stderr,"\r\n");} while(0);
#else
#define HID_DBG(...) while(0)
#endif

USB_HID::USB_HID(USBDevice* device, uint8_t output_report_length, uint8_t input_report_length) : _device(device)
{
    HID_DBG("device=%p", device);

    output_length = output_report_length;
    input_length = input_report_length;
}

bool USB_HID::send(HID_REPORT *report)
{
    return _device->write(HID_EPINT_IN, report->data, report->length, MAX_HID_REPORT_SIZE);
}

bool USB_HID::sendNB(HID_REPORT *report)
{
    return _device->writeNB(HID_EPINT_IN, report->data, report->length, MAX_HID_REPORT_SIZE);
}

bool USB_HID::read(HID_REPORT *report)
{
    uint32_t bytesRead = 0;
    bool result;
    result = _device->readEP(HID_EPINT_OUT, report->data, &bytesRead, MAX_HID_REPORT_SIZE);
    if(!_device->readStart(HID_EPINT_OUT, MAX_HID_REPORT_SIZE))
        return false;
    report->length = bytesRead;
    return result;
}

bool USB_HID::readNB(HID_REPORT *report)
{
    uint32_t bytesRead = 0;
    bool result;
    result = _device->readEP_NB(HID_EPINT_OUT, report->data, &bytesRead, MAX_HID_REPORT_SIZE);
    report->length = bytesRead;
    if(!_device->readStart(HID_EPINT_OUT, MAX_HID_REPORT_SIZE))
        return false;
    return result;
}

/* virtual */ uint8_t * USB_HID::reportDesc() {
    static uint8_t reportDescriptor[] = {
        0x06, 0x00, 0xff,
        0x09, 0x01,         // usage
        0xA1, 0x01,         // Collection 0x01
        0x15, 0x00,         // logical minimum = 0
        0x26, 0xFF, 0x00,   // logical maximum = 255
        0x75, 0x08,         // report size = 8 bits
        0x95, 0x40,         // report count
        0x09, 0x01,         // usage
        0x81, 0x02,         // Input (array)
        0x95, 0x40,         // report count
        0x09, 0x01,         // usage
        0x91, 0x02,         // Output (array)
        0x95, 0x01,         // report count
        0x09, 0x01,         // usage
        0xb1, 0x02,
        0xC0                // end collection
    };
    reportLength = sizeof(reportDescriptor);
    return reportDescriptor;
}

/* virtual */ uint16_t USB_HID::reportDescLength() {
    reportDesc();
    return reportLength;
}

bool USB_HID::Request_callback(CONTROL_TRANSFER* transfer, uint8_t* hidDescriptor)
{
    // Process additional standard requests
    if (transfer->setup.bmRequestType.Type == STANDARD_TYPE) {
        switch (transfer->setup.bRequest) {
            case GET_DESCRIPTOR:
                switch (DESCRIPTOR_TYPE(transfer->setup.wValue)) {
                    case REPORT_DESCRIPTOR:
                        if ((reportDesc() != NULL) && (reportDescLength() != 0)) {
                            transfer->remaining = reportDescLength();
                            transfer->ptr = reportDesc();
                            transfer->direction = DEVICE_TO_HOST;
                            return true;
                        }
                        break;
                    case HID_DESCRIPTOR:
                        if (hidDescriptor != NULL) {
                            transfer->remaining = HID_DESCRIPTOR_LENGTH;
                            transfer->ptr = hidDescriptor;
                            transfer->direction = DEVICE_TO_HOST;
                            return true;
                        }
                        break;
                    default:
                        break;
                }
                break;
            default:
                break;
        }
    }

    if (transfer->setup.bmRequestType.Type == CLASS_TYPE) {
        switch (transfer->setup.bRequest) {
             case SET_REPORT:
                // First byte will be used for report ID
                outputReport.data[0] = transfer->setup.wValue & 0xff;
                outputReport.length = transfer->setup.wLength + 1;

                transfer->remaining = sizeof(outputReport.data) - 1;
                transfer->ptr = &outputReport.data[1];
                transfer->direction = HOST_TO_DEVICE;
                transfer->notify = true;
                return true;
        }
    }
    return false;
}