RAMDisk example for the USBFileSystem

Dependencies:   mbed USBFileSystem

Fork of USBFileSystem_RAMDISK_HelloWorld by Erik -

Committer:
Sissors
Date:
Tue Jul 30 18:27:18 2013 +0000
Revision:
1:e1b0157ce547
Memory leaks -_-

Who changed what in which revision?

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