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 USB_MSD.h Source File

USB_MSD.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 #include "USBMSD2.h"
00021 
00022 // MSC class specific requests
00023 #define MSC_REQUEST_RESET          0xFF
00024 #define MSC_REQUEST_GET_MAX_LUN    0xFE
00025 
00026 /**
00027  * USBMSD2 class: generic class in order to use all kinds of blocks storage chip
00028  *
00029  * Introduction
00030  *
00031  * The USBMSD implements the MSD protocol. It permits to access a memory chip (flash, sdcard,...)
00032  * from a computer over USB. But this class doesn't work standalone, you need to subclass this class
00033  * and define virtual functions which are called in USBMSD.
00034  *
00035  * How to use this class with your chip ?
00036  *
00037  * You have to inherit and define some pure virtual functions (mandatory step):
00038  *   - virtual int disk_read(char * data, int block): function to read a block
00039  *   - virtual int disk_write(const char * data, int block): function to write a block
00040  *   - virtual int disk_initialize(): function to initialize the memory
00041  *   - virtual int disk_sectors(): return the number of blocks
00042  *   - virtual int disk_size(): return the memory size
00043  *   - 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)
00044  *
00045  * All functions names are compatible with the fat filesystem library. So you can imagine using your own class with
00046  * USBMSD and the fat filesystem library in the same program. Just be careful because there are two different parts which
00047  * will access the sd card. You can do a master/slave system using the disk_status method.
00048  *
00049  * Once these functions defined, you can call connect() (at the end of the constructor of your class for instance)
00050  * of USBMSD to connect your mass storage device. connect() will first call disk_status() to test the status of the disk.
00051  * If disk_status() returns 1 (disk not initialized), then disk_initialize() is called. After this step, connect() will collect information
00052  * such as the number of blocks and the memory size.
00053  */
00054 class USB_MSD {
00055 public:
00056     USB_MSD(USBMSD2* device, USBMSD2* disk);
00057 
00058     /**
00059     * Connect the USB MSD device. Establish disk initialization before really connect the device.
00060     *
00061     * @returns true if successful
00062     */
00063     bool connect();
00064 
00065     /**
00066     * Disconnect the USB MSD device.
00067     */
00068     void disconnect();
00069     
00070     /**
00071     * Destructor
00072     */
00073     ~USB_MSD();
00074 
00075     bool Request_callback(CONTROL_TRANSFER* transfer);
00076     bool EPBULK_OUT_callback();
00077     bool EPBULK_IN_callback();
00078 private:
00079     USBMSD2* _device;
00080     DiskInterface* _disk;
00081 
00082     // MSC Bulk-only Stage
00083     enum Stage {
00084         READ_CBW,     // wait a CBW
00085         ERROR,        // error
00086         PROCESS_CBW,  // process a CBW request
00087         SEND_CSW,     // send a CSW
00088         WAIT_CSW,     // wait that a CSW has been effectively sent
00089     };
00090 
00091     // Bulk-only CBW
00092     typedef struct {
00093         uint32_t Signature;
00094         uint32_t Tag;
00095         uint32_t DataLength;
00096         uint8_t  Flags;
00097         uint8_t  LUN;
00098         uint8_t  CBLength;
00099         uint8_t  CB[16];
00100     } PACKED CBW;
00101 
00102     // Bulk-only CSW
00103     typedef struct {
00104         uint32_t Signature;
00105         uint32_t Tag;
00106         uint32_t DataResidue;
00107         uint8_t  Status;
00108     } PACKED CSW;
00109 
00110     //state of the bulk-only state machine
00111     Stage stage;
00112 
00113     // current CBW
00114     CBW cbw;
00115 
00116     // CSW which will be sent
00117     CSW csw;
00118 
00119     // addr where will be read or written data
00120     uint32_t addr;
00121 
00122     // length of a reading or writing
00123     uint32_t length;
00124 
00125     // memory OK (after a memoryVerify)
00126     bool memOK;
00127 
00128     // cache in RAM before writing in memory. Useful also to read a block.
00129     uint8_t * page;
00130 
00131     int BlockSize;
00132     uint64_t MemorySize;
00133     uint64_t BlockCount;
00134 
00135     void reset();
00136     void CBWDecode(uint8_t * buf, uint16_t size);
00137     void sendCSW (void);
00138     bool inquiryRequest (void);
00139     bool write (uint8_t * buf, uint16_t size);
00140     bool readFormatCapacity();
00141     bool readCapacity (void);
00142     bool infoTransfer (void);
00143     void memoryRead (void);
00144     bool modeSense6 (void);
00145     void testUnitReady (void);
00146     bool requestSense (void);
00147     void memoryVerify (uint8_t * buf, uint16_t size);
00148     void memoryWrite (uint8_t * buf, uint16_t size);
00149     void fail();
00150 };