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