library

Dependents:   USB_CDC_MSD_Hello

Committer:
sherckuith
Date:
Fri Aug 24 02:01:51 2012 +0000
Revision:
0:d5bb9a9c3e24
[mbed] converted /USB_CDC_MSD_Hello/USBDevice

Who changed what in which revision?

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