blablabla

Dependencies:   MAG3110 MMA8451Q SLCD- TSI USBDevice mbed

Committer:
Osator
Date:
Wed Apr 16 12:20:00 2014 +0000
Revision:
0:339b7abfa147
blablabla

Who changed what in which revision?

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