Emulation of LocalFileSystem with virtual COM.

Dependencies:   USBDevice

Dependents:   KL46Z-lpc81isp lpcterm2

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBMSD2.h Source File

USBMSD2.h

00001 /* Copyright (c) 2010-2011 mbed.org, MIT License
00002 *
00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004 * and associated documentation files (the "Software"), to deal in the Software without
00005 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
00006 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
00007 * Software is furnished to do so, subject to the following conditions:
00008 *
00009 * The above copyright notice and this permission notice shall be included in all copies or
00010 * substantial portions of the Software.
00011 *
00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017 */
00018 
00019 #pragma once
00020 
00021 /* These headers are included for child class. */
00022 #include "USBDescriptor.h"
00023 #include "USBDevice_Types.h"
00024 #include "USBHID_Types.h"
00025 #include "USBDevice.h"
00026 #include "DiskInterface.h"
00027 
00028 /**
00029  * USBMSD2 class: generic class in order to use all kinds of blocks storage chip
00030  *
00031  * Introduction
00032  *
00033  * The USBMSD implements the MSD protocol. It permits to access a memory chip (flash, sdcard,...)
00034  * from a computer over USB. But this class doesn't work standalone, you need to subclass this class
00035  * and define virtual functions which are called in USBMSD.
00036  *
00037  * How to use this class with your chip ?
00038  *
00039  * You have to inherit and define some pure virtual functions (mandatory step):
00040  *   - virtual int disk_read(char * data, int block): function to read a block
00041  *   - virtual int disk_write(const char * data, int block): function to write a block
00042  *   - virtual int disk_initialize(): function to initialize the memory
00043  *   - virtual int disk_sectors(): return the number of blocks
00044  *   - virtual int disk_size(): return the memory size
00045  *   - 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)
00046  *
00047  * All functions names are compatible with the fat filesystem library. So you can imagine using your own class with
00048  * USBMSD and the fat filesystem library in the same program. Just be careful because there are two different parts which
00049  * will access the sd card. You can do a master/slave system using the disk_status method.
00050  *
00051  * Once these functions defined, you can call connect() (at the end of the constructor of your class for instance)
00052  * of USBMSD to connect your mass storage device. connect() will first call disk_status() to test the status of the disk.
00053  * If disk_status() returns 1 (disk not initialized), then disk_initialize() is called. After this step, connect() will collect information
00054  * such as the number of blocks and the memory size.
00055  */
00056 class USB_MSD;
00057 class USB_CDC;
00058 class USB_HID;
00059 
00060 class USBMSD2: public DiskInterface, public USBDevice {
00061 public:
00062     /**
00063     * Constructor
00064     *
00065     * @param vendor_id Your vendor_id
00066     * @param product_id Your product_id
00067     * @param product_release Your preoduct_release
00068     */
00069     USBMSD2(uint16_t vendor_id = 0x0d28, uint16_t product_id = 0x0204, uint16_t product_release = 0x0001);
00070 
00071     /**
00072     * Connect the USB MSD device. Establish disk initialization before really connect the device.
00073     *
00074     * @returns true if successful
00075     */
00076     bool connect();
00077 
00078     /**
00079     * Disconnect the USB MSD device.
00080     */
00081     void disconnect();
00082     
00083     /**
00084     * Destructor
00085     */
00086     ~USBMSD2();
00087     
00088     /** target to virtual COM
00089      */
00090     void putc(int c);
00091     
00092     /** virtial COM to target
00093      */
00094     int getc();
00095     int readable();
00096     int writeable();
00097 
00098 
00099     /**
00100     * Read a report: non blocking
00101     *
00102     * @param report pointer to the report to fill
00103     * @returns true if successful
00104     */
00105     bool readNB(HID_REPORT* report);
00106 
00107     /**
00108     * Send a Report. warning: blocking
00109     *
00110     * @param report Report which will be sent (a report is defined by all data and the length)
00111     * @returns true if successful
00112     */
00113     bool send(HID_REPORT* report);
00114 
00115     USB_MSD* getMSD();
00116     USB_CDC* getCDC();
00117     USB_HID* getHID();
00118 
00119 protected:
00120     /*
00121     * Get device descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
00122     *
00123     * @returns pointer to the device descriptor
00124     */
00125     virtual uint8_t * deviceDesc();
00126 
00127     /*
00128     * Get string product descriptor
00129     *
00130     * @returns pointer to the string product descriptor
00131     */
00132     virtual uint8_t * stringIproductDesc();
00133 
00134     /*
00135     * Get string interface descriptor
00136     *
00137     * @returns pointer to the string interface descriptor
00138     */
00139     virtual uint8_t * stringIinterfaceDesc();
00140 
00141     /*
00142     * Get configuration descriptor
00143     *
00144     * @returns pointer to the configuration descriptor
00145     */
00146     virtual uint8_t * configurationDesc();
00147 
00148     virtual bool EP2_OUT_callback(); // MSC Callback called when a packet is received
00149     virtual bool EP2_IN_callback();  // MSC Callback called when a packet has been sent
00150     virtual bool EP3_OUT_callback(); // CDC Callback called when a packet is received
00151     virtual bool EP5_OUT_callback(); // CDC Callback called when a packet is received
00152 
00153     /*
00154     * Set configuration of device. Add endpoints
00155     */
00156     virtual bool USBCallback_setConfiguration(uint8_t configuration);
00157 
00158     /*
00159     * Callback called to process class specific requests
00160     */
00161     virtual bool USBCallback_request();
00162 
00163     /*
00164     * Called by USBDevice on Endpoint0 request completion
00165     * if the 'notify' flag has been set to true. Warning: Called in ISR context
00166     *
00167     * In this case it is used to indicate that a HID report has
00168     * been received from the host on endpoint 0
00169     *
00170     * @param buf buffer received on endpoint 0
00171     * @param length length of this buffer
00172     */
00173     virtual void USBCallback_requestCompleted(uint8_t * buf, uint32_t length);
00174 
00175 private:
00176     USB_MSD* _msd;
00177     USB_CDC* _cdc;
00178     USB_HID* _hid;
00179 };