USB device stack

Dependents:   blinky_max32630fthr FTHR_USB_serial FTHR_OLED HSP_RPC_GUI_3_0_1 ... more

Fork of USBDevice by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBMSD.h Source File

USBMSD.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 
00020 #ifndef USBMSD_H
00021 #define USBMSD_H
00022 
00023 /* These headers are included for child class. */
00024 #include "USBEndpoints.h"
00025 #include "USBDescriptor.h"
00026 #include "USBDevice_Types.h"
00027 
00028 #include "USBDevice.h"
00029 
00030 /**
00031  * USBMSD class: generic class in order to use all kinds of blocks storage chip
00032  *
00033  * Introduction
00034  *
00035  * The USBMSD implements the MSD protocol. It permits to access a memory chip (flash, sdcard,...)
00036  * from a computer over USB. But this class doesn't work standalone, you need to subclass this class
00037  * and define virtual functions which are called in USBMSD.
00038  *
00039  * How to use this class with your chip ?
00040  *
00041  * You have to inherit and define some pure virtual functions (mandatory step):
00042  *   - virtual int disk_read(char * data, int block): function to read a block
00043  *   - virtual int disk_write(const char * data, int block): function to write a block
00044  *   - virtual int disk_initialize(): function to initialize the memory
00045  *   - virtual int disk_sectors(): return the number of blocks
00046  *   - virtual int disk_size(): return the memory size
00047  *   - 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)
00048  *
00049  * All functions names are compatible with the fat filesystem library. So you can imagine using your own class with
00050  * USBMSD and the fat filesystem library in the same program. Just be careful because there are two different parts which
00051  * will access the sd card. You can do a master/slave system using the disk_status method.
00052  *
00053  * Once these functions defined, you can call connect() (at the end of the constructor of your class for instance)
00054  * of USBMSD to connect your mass storage device. connect() will first call disk_status() to test the status of the disk.
00055  * If disk_status() returns 1 (disk not initialized), then disk_initialize() is called. After this step, connect() will collect information
00056  * such as the number of blocks and the memory size.
00057  */
00058 class USBMSD: public USBDevice {
00059 public:
00060 
00061     /**
00062     * Constructor
00063     *
00064     * @param vendor_id Your vendor_id
00065     * @param product_id Your product_id
00066     * @param product_release Your preoduct_release
00067     */
00068     USBMSD(uint16_t vendor_id = 0x0703, uint16_t product_id = 0x0104, uint16_t product_release = 0x0001);
00069 
00070     /**
00071     * Connect the USB MSD device. Establish disk initialization before really connect the device.
00072     *
00073     * @param blocking if not configured
00074     * @returns true if successful
00075     */
00076     bool connect(bool blocking = true);
00077 
00078     /**
00079     * Disconnect the USB MSD device.
00080     */
00081     void disconnect();
00082 
00083     /**
00084     * Destructor
00085     */
00086     ~USBMSD();
00087 
00088 protected:
00089 
00090     /*
00091     * read one or more blocks on a storage chip
00092     *
00093     * @param data pointer where will be stored read data
00094     * @param block starting block number
00095     * @param count number of blocks to read
00096     * @returns 0 if successful
00097     */
00098     virtual int disk_read(uint8_t* data, uint64_t block, uint8_t count) = 0;
00099 
00100     /*
00101     * write one or more blocks on a storage chip
00102     *
00103     * @param data data to write
00104     * @param block starting block number
00105     * @param count number of blocks to write
00106     * @returns 0 if successful
00107     */
00108     virtual int disk_write(const uint8_t* data, uint64_t block, uint8_t count) = 0;
00109 
00110     /*
00111     * Disk initilization
00112     */
00113     virtual int disk_initialize() = 0;
00114 
00115     /*
00116     * Return the number of blocks
00117     *
00118     * @returns number of blocks
00119     */
00120     virtual uint64_t disk_sectors() = 0;
00121 
00122     /*
00123     * Return memory size
00124     *
00125     * @returns memory size
00126     */
00127     virtual uint64_t disk_size() = 0;
00128 
00129 
00130     /*
00131     * To check the status of the storage chip
00132     *
00133     * @returns status: 0: OK, 1: disk not initialized, 2: no medium in the drive, 4: write protected
00134     */
00135     virtual int disk_status() = 0;
00136 
00137     /*
00138     * Get string product descriptor
00139     *
00140     * @returns pointer to the string product descriptor
00141     */
00142     virtual uint8_t * stringIproductDesc();
00143 
00144     /*
00145     * Get string interface descriptor
00146     *
00147     * @returns pointer to the string interface descriptor
00148     */
00149     virtual uint8_t * stringIinterfaceDesc();
00150 
00151     /*
00152     * Get configuration descriptor
00153     *
00154     * @returns pointer to the configuration descriptor
00155     */
00156     virtual uint8_t * configurationDesc();
00157 
00158     /*
00159     * Callback called when a packet is received
00160     */
00161     virtual bool EPBULK_OUT_callback();
00162 
00163     /*
00164     * Callback called when a packet has been sent
00165     */
00166     virtual bool EPBULK_IN_callback();
00167 
00168     /*
00169     * Set configuration of device. Add endpoints
00170     */
00171     virtual bool USBCallback_setConfiguration(uint8_t configuration);
00172 
00173     /*
00174     * Callback called to process class specific requests
00175     */
00176     virtual bool USBCallback_request();
00177 
00178 
00179 private:
00180 
00181     // MSC Bulk-only Stage
00182     enum Stage {
00183         READ_CBW,     // wait a CBW
00184         ERROR,        // error
00185         PROCESS_CBW,  // process a CBW request
00186         SEND_CSW,     // send a CSW
00187         WAIT_CSW,     // wait that a CSW has been effectively sent
00188     };
00189 
00190     // Bulk-only CBW
00191     typedef struct {
00192         uint32_t Signature;
00193         uint32_t Tag;
00194         uint32_t DataLength;
00195         uint8_t  Flags;
00196         uint8_t  LUN;
00197         uint8_t  CBLength;
00198         uint8_t  CB[16];
00199     } PACKED CBW;
00200 
00201     // Bulk-only CSW
00202     typedef struct {
00203         uint32_t Signature;
00204         uint32_t Tag;
00205         uint32_t DataResidue;
00206         uint8_t  Status;
00207     } PACKED CSW;
00208 
00209     //state of the bulk-only state machine
00210     Stage stage;
00211 
00212     // current CBW
00213     CBW cbw;
00214 
00215     // CSW which will be sent
00216     CSW csw;
00217 
00218     // addr where will be read or written data
00219     uint32_t addr;
00220 
00221     // length of a reading or writing
00222     uint32_t length;
00223 
00224     // memory OK (after a memoryVerify)
00225     bool memOK;
00226 
00227     // cache in RAM before writing in memory. Useful also to read a block.
00228     uint8_t * page;
00229 
00230     int BlockSize;
00231     uint64_t MemorySize;
00232     uint64_t BlockCount;
00233 
00234     void CBWDecode(uint8_t * buf, uint16_t size);
00235     void sendCSW (void);
00236     bool inquiryRequest (void);
00237     bool write (uint8_t * buf, uint16_t size);
00238     bool readFormatCapacity();
00239     bool readCapacity (void);
00240     bool infoTransfer (void);
00241     void memoryRead (void);
00242     bool modeSense6 (void);
00243     void testUnitReady (void);
00244     bool requestSense (void);
00245     void memoryVerify (uint8_t * buf, uint16_t size);
00246     void memoryWrite (uint8_t * buf, uint16_t size);
00247     void reset();
00248     void fail();
00249 };
00250 
00251 #endif