USB CDC (serial) and USB MSC (strage) Composite Device. http://mbed.org/users/okini3939/notebook/USB_Device/

Dependencies:   ChaNFSSD mbed ChaNFS

Committer:
okini3939
Date:
Fri Dec 23 16:37:58 2011 +0000
Revision:
2:5db90410bb90
Parent:
0:9b1d17d54055

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
okini3939 0:9b1d17d54055 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
okini3939 0:9b1d17d54055 2 *
okini3939 0:9b1d17d54055 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
okini3939 0:9b1d17d54055 4 * and associated documentation files (the "Software"), to deal in the Software without
okini3939 0:9b1d17d54055 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
okini3939 0:9b1d17d54055 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
okini3939 0:9b1d17d54055 7 * Software is furnished to do so, subject to the following conditions:
okini3939 0:9b1d17d54055 8 *
okini3939 0:9b1d17d54055 9 * The above copyright notice and this permission notice shall be included in all copies or
okini3939 0:9b1d17d54055 10 * substantial portions of the Software.
okini3939 0:9b1d17d54055 11 *
okini3939 0:9b1d17d54055 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
okini3939 0:9b1d17d54055 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
okini3939 0:9b1d17d54055 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
okini3939 0:9b1d17d54055 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
okini3939 0:9b1d17d54055 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
okini3939 0:9b1d17d54055 17 */
okini3939 0:9b1d17d54055 18
okini3939 0:9b1d17d54055 19
okini3939 0:9b1d17d54055 20 #ifndef USBMSD_H
okini3939 0:9b1d17d54055 21 #define USBMSD_H
okini3939 0:9b1d17d54055 22
okini3939 0:9b1d17d54055 23 /* These headers are included for child class. */
okini3939 0:9b1d17d54055 24 #include "USBEndpoints.h"
okini3939 0:9b1d17d54055 25 #include "USBDescriptor.h"
okini3939 0:9b1d17d54055 26 #include "USBDevice_Types.h"
okini3939 0:9b1d17d54055 27
okini3939 0:9b1d17d54055 28 #include "USBDevice.h"
okini3939 0:9b1d17d54055 29
okini3939 0:9b1d17d54055 30 /**
okini3939 0:9b1d17d54055 31 * USBMSD class: generic class in order to use all kinds of blocks storage chip
okini3939 0:9b1d17d54055 32 *
okini3939 0:9b1d17d54055 33 * Introduction
okini3939 0:9b1d17d54055 34 *
okini3939 0:9b1d17d54055 35 * The USBMSD implements the MSD protocol. It permits to access a memory chip (flash, sdcard,...)
okini3939 0:9b1d17d54055 36 * from a computer over USB. But this class doesn't work standalone, you need to subclass this class
okini3939 0:9b1d17d54055 37 * and define virtual functions which are called in USBMSD.
okini3939 0:9b1d17d54055 38 *
okini3939 0:9b1d17d54055 39 * How to use this class with your chip ?
okini3939 0:9b1d17d54055 40 *
okini3939 0:9b1d17d54055 41 * You have to inherit and define some pure virtual functions (mandatory step):
okini3939 0:9b1d17d54055 42 * - virtual int disk_read(char * data, int block): function to read a block
okini3939 0:9b1d17d54055 43 * - virtual int disk_write(const char * data, int block): function to write a block
okini3939 0:9b1d17d54055 44 * - virtual int disk_initialize(): function to initialize the memory
okini3939 0:9b1d17d54055 45 * - virtual int disk_sectors(): return the number of blocks
okini3939 0:9b1d17d54055 46 * - virtual int disk_size(): return the memory size
okini3939 0:9b1d17d54055 47 * - 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)
okini3939 0:9b1d17d54055 48 *
okini3939 0:9b1d17d54055 49 * All functions names are compatible with the fat filesystem library. So you can imagine using your own class with
okini3939 0:9b1d17d54055 50 * USBMSD and the fat filesystem library in the same program. Just be careful because there are two different parts which
okini3939 0:9b1d17d54055 51 * will access the sd card. You can do a master/slave system using the disk_status method.
okini3939 0:9b1d17d54055 52 *
okini3939 0:9b1d17d54055 53 * Once these functions defined, you can call connect() (at the end of the constructor of your class for instance)
okini3939 0:9b1d17d54055 54 * of USBMSD to connect your mass storage device. connect() will first call disk_status() to test the status of the disk.
okini3939 0:9b1d17d54055 55 * If disk_status() returns 1 (disk not initialized), then disk_initialize() is called. After this step, connect() will collect information
okini3939 0:9b1d17d54055 56 * such as the number of blocks and the memory size.
okini3939 0:9b1d17d54055 57 */
okini3939 0:9b1d17d54055 58 class USBMSD: public USBDevice {
okini3939 0:9b1d17d54055 59 public:
okini3939 0:9b1d17d54055 60
okini3939 0:9b1d17d54055 61 /**
okini3939 0:9b1d17d54055 62 * Constructor
okini3939 0:9b1d17d54055 63 *
okini3939 0:9b1d17d54055 64 * @param vendor_id Your vendor_id
okini3939 0:9b1d17d54055 65 * @param product_id Your product_id
okini3939 0:9b1d17d54055 66 * @param product_release Your preoduct_release
okini3939 0:9b1d17d54055 67 */
okini3939 0:9b1d17d54055 68 USBMSD(uint16_t vendor_id = 0x0703, uint16_t product_id = 0x0104, uint16_t product_release = 0x0001);
okini3939 0:9b1d17d54055 69
okini3939 0:9b1d17d54055 70 /**
okini3939 0:9b1d17d54055 71 * Connect the USB MSD device. Establish disk initialization before really connect the device.
okini3939 0:9b1d17d54055 72 *
okini3939 0:9b1d17d54055 73 * @returns true if successful
okini3939 0:9b1d17d54055 74 */
okini3939 0:9b1d17d54055 75 bool connect();
okini3939 0:9b1d17d54055 76
okini3939 0:9b1d17d54055 77
okini3939 0:9b1d17d54055 78 protected:
okini3939 0:9b1d17d54055 79
okini3939 0:9b1d17d54055 80 /*
okini3939 0:9b1d17d54055 81 * read a block on a storage chip
okini3939 0:9b1d17d54055 82 *
okini3939 0:9b1d17d54055 83 * @param data pointer where will be stored read data
okini3939 0:9b1d17d54055 84 * @param block block number
okini3939 0:9b1d17d54055 85 * @returns 0 if successful
okini3939 0:9b1d17d54055 86 */
okini3939 0:9b1d17d54055 87 virtual int disk_read(char * data, int block) = 0;
okini3939 0:9b1d17d54055 88
okini3939 0:9b1d17d54055 89 /*
okini3939 0:9b1d17d54055 90 * write a block on a storage chip
okini3939 0:9b1d17d54055 91 *
okini3939 0:9b1d17d54055 92 * @param data data to write
okini3939 0:9b1d17d54055 93 * @param block block number
okini3939 0:9b1d17d54055 94 * @returns 0 if successful
okini3939 0:9b1d17d54055 95 */
okini3939 0:9b1d17d54055 96 virtual int disk_write(const char * data, int block) = 0;
okini3939 0:9b1d17d54055 97
okini3939 0:9b1d17d54055 98 /*
okini3939 0:9b1d17d54055 99 * Disk initilization
okini3939 0:9b1d17d54055 100 */
okini3939 0:9b1d17d54055 101 virtual int disk_initialize() = 0;
okini3939 0:9b1d17d54055 102
okini3939 0:9b1d17d54055 103 /*
okini3939 0:9b1d17d54055 104 * Return the number of blocks
okini3939 0:9b1d17d54055 105 *
okini3939 0:9b1d17d54055 106 * @returns number of blocks
okini3939 0:9b1d17d54055 107 */
okini3939 0:9b1d17d54055 108 virtual int disk_sectors() = 0;
okini3939 0:9b1d17d54055 109
okini3939 0:9b1d17d54055 110 /*
okini3939 0:9b1d17d54055 111 * Return memory size
okini3939 0:9b1d17d54055 112 *
okini3939 0:9b1d17d54055 113 * @returns memory size
okini3939 0:9b1d17d54055 114 */
okini3939 0:9b1d17d54055 115 virtual int disk_size() = 0;
okini3939 0:9b1d17d54055 116
okini3939 0:9b1d17d54055 117
okini3939 0:9b1d17d54055 118 /*
okini3939 0:9b1d17d54055 119 * To check the status of the storage chip
okini3939 0:9b1d17d54055 120 *
okini3939 0:9b1d17d54055 121 * @returns status: 0: OK, 1: disk not initialized, 2: no medium in the drive, 4: write protected
okini3939 0:9b1d17d54055 122 */
okini3939 0:9b1d17d54055 123 virtual int disk_status() = 0;
okini3939 0:9b1d17d54055 124
okini3939 0:9b1d17d54055 125 /*
okini3939 0:9b1d17d54055 126 * Get string product descriptor
okini3939 0:9b1d17d54055 127 *
okini3939 0:9b1d17d54055 128 * @returns pointer to the string product descriptor
okini3939 0:9b1d17d54055 129 */
okini3939 0:9b1d17d54055 130 virtual uint8_t * stringIproductDesc();
okini3939 0:9b1d17d54055 131
okini3939 0:9b1d17d54055 132 /*
okini3939 0:9b1d17d54055 133 * Get string interface descriptor
okini3939 0:9b1d17d54055 134 *
okini3939 0:9b1d17d54055 135 * @returns pointer to the string interface descriptor
okini3939 0:9b1d17d54055 136 */
okini3939 0:9b1d17d54055 137 virtual uint8_t * stringIinterfaceDesc();
okini3939 0:9b1d17d54055 138
okini3939 0:9b1d17d54055 139 /*
okini3939 0:9b1d17d54055 140 * Get configuration descriptor
okini3939 0:9b1d17d54055 141 *
okini3939 0:9b1d17d54055 142 * @returns pointer to the configuration descriptor
okini3939 0:9b1d17d54055 143 */
okini3939 0:9b1d17d54055 144 virtual uint8_t * configurationDesc();
okini3939 0:9b1d17d54055 145
okini3939 0:9b1d17d54055 146 /*
okini3939 0:9b1d17d54055 147 * Callback called when a packet is received
okini3939 0:9b1d17d54055 148 */
okini3939 0:9b1d17d54055 149 virtual bool EP2_OUT_callback();
okini3939 0:9b1d17d54055 150
okini3939 0:9b1d17d54055 151 /*
okini3939 0:9b1d17d54055 152 * Callback called when a packet has been sent
okini3939 0:9b1d17d54055 153 */
okini3939 0:9b1d17d54055 154 virtual bool EP2_IN_callback();
okini3939 0:9b1d17d54055 155
okini3939 0:9b1d17d54055 156 /*
okini3939 0:9b1d17d54055 157 * Set configuration of device. Add endpoints
okini3939 0:9b1d17d54055 158 */
okini3939 0:9b1d17d54055 159 virtual bool USBCallback_setConfiguration(uint8_t configuration);
okini3939 0:9b1d17d54055 160
okini3939 0:9b1d17d54055 161 /*
okini3939 0:9b1d17d54055 162 * Callback called to process class specific requests
okini3939 0:9b1d17d54055 163 */
okini3939 0:9b1d17d54055 164 virtual bool USBCallback_request();
okini3939 0:9b1d17d54055 165
okini3939 0:9b1d17d54055 166
okini3939 0:9b1d17d54055 167 private:
okini3939 0:9b1d17d54055 168
okini3939 0:9b1d17d54055 169 // MSC Bulk-only Stage
okini3939 0:9b1d17d54055 170 enum Stage {
okini3939 0:9b1d17d54055 171 READ_CBW, // wait a CBW
okini3939 0:9b1d17d54055 172 ERROR, // error
okini3939 0:9b1d17d54055 173 PROCESS_CBW, // process a CBW request
okini3939 0:9b1d17d54055 174 SEND_CSW, // send a CSW
okini3939 0:9b1d17d54055 175 WAIT_CSW, // wait that a CSW has been effectively sent
okini3939 0:9b1d17d54055 176 };
okini3939 0:9b1d17d54055 177
okini3939 0:9b1d17d54055 178 // Bulk-only CBW
okini3939 0:9b1d17d54055 179 typedef __packed struct {
okini3939 0:9b1d17d54055 180 uint32_t Signature;
okini3939 0:9b1d17d54055 181 uint32_t Tag;
okini3939 0:9b1d17d54055 182 uint32_t DataLength;
okini3939 0:9b1d17d54055 183 uint8_t Flags;
okini3939 0:9b1d17d54055 184 uint8_t LUN;
okini3939 0:9b1d17d54055 185 uint8_t CBLength;
okini3939 0:9b1d17d54055 186 uint8_t CB[16];
okini3939 0:9b1d17d54055 187 } CBW;
okini3939 0:9b1d17d54055 188
okini3939 0:9b1d17d54055 189 // Bulk-only CSW
okini3939 0:9b1d17d54055 190 typedef __packed struct {
okini3939 0:9b1d17d54055 191 uint32_t Signature;
okini3939 0:9b1d17d54055 192 uint32_t Tag;
okini3939 0:9b1d17d54055 193 uint32_t DataResidue;
okini3939 0:9b1d17d54055 194 uint8_t Status;
okini3939 0:9b1d17d54055 195 } CSW;
okini3939 0:9b1d17d54055 196
okini3939 0:9b1d17d54055 197 //state of the bulk-only state machine
okini3939 0:9b1d17d54055 198 Stage stage;
okini3939 0:9b1d17d54055 199
okini3939 0:9b1d17d54055 200 // current CBW
okini3939 0:9b1d17d54055 201 CBW cbw;
okini3939 0:9b1d17d54055 202
okini3939 0:9b1d17d54055 203 // CSW which will be sent
okini3939 0:9b1d17d54055 204 CSW csw;
okini3939 0:9b1d17d54055 205
okini3939 0:9b1d17d54055 206 // addr where will be read or written data
okini3939 0:9b1d17d54055 207 uint32_t addr;
okini3939 0:9b1d17d54055 208
okini3939 0:9b1d17d54055 209 // length of a reading or writing
okini3939 0:9b1d17d54055 210 uint32_t length;
okini3939 0:9b1d17d54055 211
okini3939 0:9b1d17d54055 212 // memory OK (after a memoryVerify)
okini3939 0:9b1d17d54055 213 bool memOK;
okini3939 0:9b1d17d54055 214
okini3939 0:9b1d17d54055 215 // cache in RAM before writing in memory. Useful also to read a block.
okini3939 0:9b1d17d54055 216 uint8_t * page;
okini3939 0:9b1d17d54055 217
okini3939 0:9b1d17d54055 218 int BlockSize;
okini3939 0:9b1d17d54055 219 int MemorySize;
okini3939 0:9b1d17d54055 220 int BlockCount;
okini3939 0:9b1d17d54055 221
okini3939 0:9b1d17d54055 222 void CBWDecode(uint8_t * buf, uint16_t size);
okini3939 0:9b1d17d54055 223 void sendCSW (void);
okini3939 0:9b1d17d54055 224 bool inquiryRequest (void);
okini3939 0:9b1d17d54055 225 bool write (uint8_t * buf, uint16_t size);
okini3939 0:9b1d17d54055 226 bool readFormatCapacity();
okini3939 0:9b1d17d54055 227 bool readCapacity (void);
okini3939 0:9b1d17d54055 228 bool infoTransfer (void);
okini3939 0:9b1d17d54055 229 void memoryRead (void);
okini3939 0:9b1d17d54055 230 bool modeSense6 (void);
okini3939 0:9b1d17d54055 231 void testUnitReady (void);
okini3939 0:9b1d17d54055 232 bool requestSense (void);
okini3939 0:9b1d17d54055 233 void memoryVerify (uint8_t * buf, uint16_t size);
okini3939 0:9b1d17d54055 234 void memoryWrite (uint8_t * buf, uint16_t size);
okini3939 0:9b1d17d54055 235 void reset();
okini3939 0:9b1d17d54055 236 void fail();
okini3939 0:9b1d17d54055 237 };
okini3939 0:9b1d17d54055 238
okini3939 0:9b1d17d54055 239 #endif