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

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?

UserRevisionLine numberNew 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 /* These headers are included for child class. */
va009039 0:39eb4d5b97df 22 #include "USBDescriptor.h"
va009039 0:39eb4d5b97df 23 #include "USBDevice_Types.h"
va009039 0:39eb4d5b97df 24 #include "USBHID_Types.h"
va009039 0:39eb4d5b97df 25 #include "USBDevice.h"
va009039 0:39eb4d5b97df 26 #include "DiskInterface.h"
va009039 0:39eb4d5b97df 27
va009039 0:39eb4d5b97df 28 /**
va009039 0:39eb4d5b97df 29 * USBMSD2 class: generic class in order to use all kinds of blocks storage chip
va009039 0:39eb4d5b97df 30 *
va009039 0:39eb4d5b97df 31 * Introduction
va009039 0:39eb4d5b97df 32 *
va009039 0:39eb4d5b97df 33 * The USBMSD implements the MSD protocol. It permits to access a memory chip (flash, sdcard,...)
va009039 0:39eb4d5b97df 34 * from a computer over USB. But this class doesn't work standalone, you need to subclass this class
va009039 0:39eb4d5b97df 35 * and define virtual functions which are called in USBMSD.
va009039 0:39eb4d5b97df 36 *
va009039 0:39eb4d5b97df 37 * How to use this class with your chip ?
va009039 0:39eb4d5b97df 38 *
va009039 0:39eb4d5b97df 39 * You have to inherit and define some pure virtual functions (mandatory step):
va009039 0:39eb4d5b97df 40 * - virtual int disk_read(char * data, int block): function to read a block
va009039 0:39eb4d5b97df 41 * - virtual int disk_write(const char * data, int block): function to write a block
va009039 0:39eb4d5b97df 42 * - virtual int disk_initialize(): function to initialize the memory
va009039 0:39eb4d5b97df 43 * - virtual int disk_sectors(): return the number of blocks
va009039 0:39eb4d5b97df 44 * - virtual int disk_size(): return the memory size
va009039 0:39eb4d5b97df 45 * - virtual int disk_status(): return the status of the storage chip (0: OK, 1: not initialized, 2: no medium in the drive, 4: write protection)
va009039 0:39eb4d5b97df 46 *
va009039 0:39eb4d5b97df 47 * All functions names are compatible with the fat filesystem library. So you can imagine using your own class with
va009039 0:39eb4d5b97df 48 * USBMSD and the fat filesystem library in the same program. Just be careful because there are two different parts which
va009039 0:39eb4d5b97df 49 * will access the sd card. You can do a master/slave system using the disk_status method.
va009039 0:39eb4d5b97df 50 *
va009039 0:39eb4d5b97df 51 * Once these functions defined, you can call connect() (at the end of the constructor of your class for instance)
va009039 0:39eb4d5b97df 52 * of USBMSD to connect your mass storage device. connect() will first call disk_status() to test the status of the disk.
va009039 0:39eb4d5b97df 53 * If disk_status() returns 1 (disk not initialized), then disk_initialize() is called. After this step, connect() will collect information
va009039 0:39eb4d5b97df 54 * such as the number of blocks and the memory size.
va009039 0:39eb4d5b97df 55 */
va009039 0:39eb4d5b97df 56 class USB_MSD;
va009039 0:39eb4d5b97df 57 class USB_CDC;
va009039 0:39eb4d5b97df 58 class USB_HID;
va009039 0:39eb4d5b97df 59
va009039 0:39eb4d5b97df 60 class USBMSD2: public DiskInterface, public USBDevice {
va009039 0:39eb4d5b97df 61 public:
va009039 0:39eb4d5b97df 62 /**
va009039 0:39eb4d5b97df 63 * Constructor
va009039 0:39eb4d5b97df 64 *
va009039 0:39eb4d5b97df 65 * @param vendor_id Your vendor_id
va009039 0:39eb4d5b97df 66 * @param product_id Your product_id
va009039 0:39eb4d5b97df 67 * @param product_release Your preoduct_release
va009039 0:39eb4d5b97df 68 */
va009039 0:39eb4d5b97df 69 USBMSD2(uint16_t vendor_id = 0x0d28, uint16_t product_id = 0x0204, uint16_t product_release = 0x0001);
va009039 0:39eb4d5b97df 70
va009039 0:39eb4d5b97df 71 /**
va009039 0:39eb4d5b97df 72 * Connect the USB MSD device. Establish disk initialization before really connect the device.
va009039 0:39eb4d5b97df 73 *
va009039 0:39eb4d5b97df 74 * @returns true if successful
va009039 0:39eb4d5b97df 75 */
va009039 0:39eb4d5b97df 76 bool connect();
va009039 0:39eb4d5b97df 77
va009039 0:39eb4d5b97df 78 /**
va009039 0:39eb4d5b97df 79 * Disconnect the USB MSD device.
va009039 0:39eb4d5b97df 80 */
va009039 0:39eb4d5b97df 81 void disconnect();
va009039 0:39eb4d5b97df 82
va009039 0:39eb4d5b97df 83 /**
va009039 0:39eb4d5b97df 84 * Destructor
va009039 0:39eb4d5b97df 85 */
va009039 0:39eb4d5b97df 86 ~USBMSD2();
va009039 0:39eb4d5b97df 87
va009039 0:39eb4d5b97df 88 /** target to virtual COM
va009039 0:39eb4d5b97df 89 */
va009039 0:39eb4d5b97df 90 void putc(int c);
va009039 0:39eb4d5b97df 91
va009039 0:39eb4d5b97df 92 /** virtial COM to target
va009039 0:39eb4d5b97df 93 */
va009039 0:39eb4d5b97df 94 int getc();
va009039 0:39eb4d5b97df 95 int readable();
va009039 0:39eb4d5b97df 96 int writeable();
va009039 0:39eb4d5b97df 97
va009039 0:39eb4d5b97df 98
va009039 0:39eb4d5b97df 99 /**
va009039 0:39eb4d5b97df 100 * Read a report: non blocking
va009039 0:39eb4d5b97df 101 *
va009039 0:39eb4d5b97df 102 * @param report pointer to the report to fill
va009039 0:39eb4d5b97df 103 * @returns true if successful
va009039 0:39eb4d5b97df 104 */
va009039 0:39eb4d5b97df 105 bool readNB(HID_REPORT* report);
va009039 0:39eb4d5b97df 106
va009039 0:39eb4d5b97df 107 /**
va009039 0:39eb4d5b97df 108 * Send a Report. warning: blocking
va009039 0:39eb4d5b97df 109 *
va009039 0:39eb4d5b97df 110 * @param report Report which will be sent (a report is defined by all data and the length)
va009039 0:39eb4d5b97df 111 * @returns true if successful
va009039 0:39eb4d5b97df 112 */
va009039 0:39eb4d5b97df 113 bool send(HID_REPORT* report);
va009039 0:39eb4d5b97df 114
va009039 4:8f6857784854 115 USB_MSD* getMSD();
va009039 4:8f6857784854 116 USB_CDC* getCDC();
va009039 4:8f6857784854 117 USB_HID* getHID();
va009039 4:8f6857784854 118
va009039 0:39eb4d5b97df 119 protected:
va009039 0:39eb4d5b97df 120 /*
va009039 0:39eb4d5b97df 121 * Get device descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
va009039 0:39eb4d5b97df 122 *
va009039 0:39eb4d5b97df 123 * @returns pointer to the device descriptor
va009039 0:39eb4d5b97df 124 */
va009039 0:39eb4d5b97df 125 virtual uint8_t * deviceDesc();
va009039 0:39eb4d5b97df 126
va009039 0:39eb4d5b97df 127 /*
va009039 0:39eb4d5b97df 128 * Get string product descriptor
va009039 0:39eb4d5b97df 129 *
va009039 0:39eb4d5b97df 130 * @returns pointer to the string product descriptor
va009039 0:39eb4d5b97df 131 */
va009039 0:39eb4d5b97df 132 virtual uint8_t * stringIproductDesc();
va009039 0:39eb4d5b97df 133
va009039 0:39eb4d5b97df 134 /*
va009039 0:39eb4d5b97df 135 * Get string interface descriptor
va009039 0:39eb4d5b97df 136 *
va009039 0:39eb4d5b97df 137 * @returns pointer to the string interface descriptor
va009039 0:39eb4d5b97df 138 */
va009039 0:39eb4d5b97df 139 virtual uint8_t * stringIinterfaceDesc();
va009039 0:39eb4d5b97df 140
va009039 0:39eb4d5b97df 141 /*
va009039 0:39eb4d5b97df 142 * Get configuration descriptor
va009039 0:39eb4d5b97df 143 *
va009039 0:39eb4d5b97df 144 * @returns pointer to the configuration descriptor
va009039 0:39eb4d5b97df 145 */
va009039 0:39eb4d5b97df 146 virtual uint8_t * configurationDesc();
va009039 0:39eb4d5b97df 147
va009039 0:39eb4d5b97df 148 virtual bool EP2_OUT_callback(); // MSC Callback called when a packet is received
va009039 0:39eb4d5b97df 149 virtual bool EP2_IN_callback(); // MSC Callback called when a packet has been sent
va009039 0:39eb4d5b97df 150 virtual bool EP3_OUT_callback(); // CDC Callback called when a packet is received
va009039 0:39eb4d5b97df 151 virtual bool EP5_OUT_callback(); // CDC Callback called when a packet is received
va009039 0:39eb4d5b97df 152
va009039 0:39eb4d5b97df 153 /*
va009039 0:39eb4d5b97df 154 * Set configuration of device. Add endpoints
va009039 0:39eb4d5b97df 155 */
va009039 0:39eb4d5b97df 156 virtual bool USBCallback_setConfiguration(uint8_t configuration);
va009039 0:39eb4d5b97df 157
va009039 0:39eb4d5b97df 158 /*
va009039 0:39eb4d5b97df 159 * Callback called to process class specific requests
va009039 0:39eb4d5b97df 160 */
va009039 0:39eb4d5b97df 161 virtual bool USBCallback_request();
va009039 0:39eb4d5b97df 162
va009039 0:39eb4d5b97df 163 /*
va009039 0:39eb4d5b97df 164 * Called by USBDevice on Endpoint0 request completion
va009039 0:39eb4d5b97df 165 * if the 'notify' flag has been set to true. Warning: Called in ISR context
va009039 0:39eb4d5b97df 166 *
va009039 0:39eb4d5b97df 167 * In this case it is used to indicate that a HID report has
va009039 0:39eb4d5b97df 168 * been received from the host on endpoint 0
va009039 0:39eb4d5b97df 169 *
va009039 0:39eb4d5b97df 170 * @param buf buffer received on endpoint 0
va009039 0:39eb4d5b97df 171 * @param length length of this buffer
va009039 0:39eb4d5b97df 172 */
va009039 0:39eb4d5b97df 173 virtual void USBCallback_requestCompleted(uint8_t * buf, uint32_t length);
va009039 0:39eb4d5b97df 174
va009039 0:39eb4d5b97df 175 private:
va009039 0:39eb4d5b97df 176 USB_MSD* _msd;
va009039 0:39eb4d5b97df 177 USB_CDC* _cdc;
va009039 0:39eb4d5b97df 178 USB_HID* _hid;
va009039 0:39eb4d5b97df 179 };