USB composite device example program, drag-and-drop flash writer.

Dependencies:   SWD USBDevice mbed BaseDAP

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 "USBEndpoints.h"
00023 #include "USBDescriptor.h"
00024 #include "USBDevice_Types.h"
00025 #include "USBHID_Types.h"
00026 #include "USBDevice.h"
00027 #include "DiskInterface.h"
00028 
00029 /**
00030  * USBMSD2 class: generic class in order to use all kinds of blocks storage chip
00031  *
00032  * Introduction
00033  *
00034  * The USBMSD implements the MSD protocol. It permits to access a memory chip (flash, sdcard,...)
00035  * from a computer over USB. But this class doesn't work standalone, you need to subclass this class
00036  * and define virtual functions which are called in USBMSD.
00037  *
00038  * How to use this class with your chip ?
00039  *
00040  * You have to inherit and define some pure virtual functions (mandatory step):
00041  *   - virtual int disk_read(char * data, int block): function to read a block
00042  *   - virtual int disk_write(const char * data, int block): function to write a block
00043  *   - virtual int disk_initialize(): function to initialize the memory
00044  *   - virtual int disk_sectors(): return the number of blocks
00045  *   - virtual int disk_size(): return the memory size
00046  *   - 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)
00047  *
00048  * All functions names are compatible with the fat filesystem library. So you can imagine using your own class with
00049  * USBMSD and the fat filesystem library in the same program. Just be careful because there are two different parts which
00050  * will access the sd card. You can do a master/slave system using the disk_status method.
00051  *
00052  * Once these functions defined, you can call connect() (at the end of the constructor of your class for instance)
00053  * of USBMSD to connect your mass storage device. connect() will first call disk_status() to test the status of the disk.
00054  * If disk_status() returns 1 (disk not initialized), then disk_initialize() is called. After this step, connect() will collect information
00055  * such as the number of blocks and the memory size.
00056  */
00057 class USB_MSD;
00058 class USB_CDC;
00059 class USB_HID;
00060 
00061 class USBMSD2: public DiskInterface, public USBDevice {
00062 public:
00063     /**
00064     * Constructor
00065     *
00066     * @param vendor_id Your vendor_id
00067     * @param product_id Your product_id
00068     * @param product_release Your preoduct_release
00069     */
00070     USBMSD2(uint16_t vendor_id = 0x0d28, uint16_t product_id = 0x0204, uint16_t product_release = 0x0001);
00071 
00072     /**
00073     * Connect the USB MSD device. Establish disk initialization before really connect the device.
00074     *
00075     * @returns true if successful
00076     */
00077     bool connect();
00078 
00079     /**
00080     * Disconnect the USB MSD device.
00081     */
00082     void disconnect();
00083     
00084     /**
00085     * Destructor
00086     */
00087     ~USBMSD2();
00088     
00089     /** target to virtual COM
00090      */
00091     void putc(int c);
00092     
00093     /** virtial COM to target
00094      */
00095     int getc();
00096     int readable();
00097     int writeable();
00098 
00099 
00100     /**
00101     * Read a report: non blocking
00102     *
00103     * @param report pointer to the report to fill
00104     * @returns true if successful
00105     */
00106     bool readNB(HID_REPORT* report);
00107 
00108     /**
00109     * Send a Report. warning: blocking
00110     *
00111     * @param report Report which will be sent (a report is defined by all data and the length)
00112     * @returns true if successful
00113     */
00114     bool send(HID_REPORT* report);
00115 
00116 protected:
00117     /*
00118     * Get device descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
00119     *
00120     * @returns pointer to the device descriptor
00121     */
00122     virtual uint8_t * deviceDesc();
00123 
00124     /*
00125     * Get string product descriptor
00126     *
00127     * @returns pointer to the string product descriptor
00128     */
00129     virtual uint8_t * stringIproductDesc();
00130 
00131     /*
00132     * Get string interface descriptor
00133     *
00134     * @returns pointer to the string interface descriptor
00135     */
00136     virtual uint8_t * stringIinterfaceDesc();
00137 
00138     /*
00139     * Get configuration descriptor
00140     *
00141     * @returns pointer to the configuration descriptor
00142     */
00143     virtual uint8_t * configurationDesc();
00144 
00145     virtual bool EP2_OUT_callback(); // MSC Callback called when a packet is received
00146     virtual bool EP2_IN_callback();  // MSC Callback called when a packet has been sent
00147     virtual bool EP5_OUT_callback(); // CDC Callback called when a packet is received
00148 
00149     /*
00150     * Set configuration of device. Add endpoints
00151     */
00152     virtual bool USBCallback_setConfiguration(uint8_t configuration);
00153 
00154     /*
00155     * Callback called to process class specific requests
00156     */
00157     virtual bool USBCallback_request();
00158 
00159     /*
00160     * Called by USBDevice on Endpoint0 request completion
00161     * if the 'notify' flag has been set to true. Warning: Called in ISR context
00162     *
00163     * In this case it is used to indicate that a HID report has
00164     * been received from the host on endpoint 0
00165     *
00166     * @param buf buffer received on endpoint 0
00167     * @param length length of this buffer
00168     */
00169     virtual void USBCallback_requestCompleted(uint8_t * buf, uint32_t length);
00170 
00171 private:
00172     USB_MSD* _msd;
00173     USB_CDC* _cdc;
00174     USB_HID* _hid;
00175 };