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