ok
USBCDCMSC.h@0:8db2bcdf4574, 2012-08-24 (annotated)
- Committer:
- sherckuith
- Date:
- Fri Aug 24 02:01:52 2012 +0000
- Revision:
- 0:8db2bcdf4574
[mbed] converted /USB_CDC_MSD_Hello/USBCDCMSC
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sherckuith | 0:8db2bcdf4574 | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
sherckuith | 0:8db2bcdf4574 | 2 | * |
sherckuith | 0:8db2bcdf4574 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
sherckuith | 0:8db2bcdf4574 | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
sherckuith | 0:8db2bcdf4574 | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
sherckuith | 0:8db2bcdf4574 | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
sherckuith | 0:8db2bcdf4574 | 7 | * Software is furnished to do so, subject to the following conditions: |
sherckuith | 0:8db2bcdf4574 | 8 | * |
sherckuith | 0:8db2bcdf4574 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
sherckuith | 0:8db2bcdf4574 | 10 | * substantial portions of the Software. |
sherckuith | 0:8db2bcdf4574 | 11 | * |
sherckuith | 0:8db2bcdf4574 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
sherckuith | 0:8db2bcdf4574 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
sherckuith | 0:8db2bcdf4574 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
sherckuith | 0:8db2bcdf4574 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
sherckuith | 0:8db2bcdf4574 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
sherckuith | 0:8db2bcdf4574 | 17 | */ |
sherckuith | 0:8db2bcdf4574 | 18 | |
sherckuith | 0:8db2bcdf4574 | 19 | #ifndef USBCDCMSC_H |
sherckuith | 0:8db2bcdf4574 | 20 | #define USBCDCMSC_H |
sherckuith | 0:8db2bcdf4574 | 21 | |
sherckuith | 0:8db2bcdf4574 | 22 | /* These headers are included for child class. */ |
sherckuith | 0:8db2bcdf4574 | 23 | #include "USBEndpoints.h" |
sherckuith | 0:8db2bcdf4574 | 24 | #include "USBDescriptor.h" |
sherckuith | 0:8db2bcdf4574 | 25 | #include "USBDevice_Types.h" |
sherckuith | 0:8db2bcdf4574 | 26 | |
sherckuith | 0:8db2bcdf4574 | 27 | #include "USBDevice.h" |
sherckuith | 0:8db2bcdf4574 | 28 | |
sherckuith | 0:8db2bcdf4574 | 29 | #include "Stream.h" |
sherckuith | 0:8db2bcdf4574 | 30 | #include "CircBuffer.h" |
sherckuith | 0:8db2bcdf4574 | 31 | |
sherckuith | 0:8db2bcdf4574 | 32 | #include "SDFileSystem.h" |
sherckuith | 0:8db2bcdf4574 | 33 | |
sherckuith | 0:8db2bcdf4574 | 34 | |
sherckuith | 0:8db2bcdf4574 | 35 | class USBCDCMSC: public USBDevice, public Stream { |
sherckuith | 0:8db2bcdf4574 | 36 | public: |
sherckuith | 0:8db2bcdf4574 | 37 | |
sherckuith | 0:8db2bcdf4574 | 38 | /* |
sherckuith | 0:8db2bcdf4574 | 39 | * Constructor |
sherckuith | 0:8db2bcdf4574 | 40 | * |
sherckuith | 0:8db2bcdf4574 | 41 | * @param vendor_id Your vendor_id |
sherckuith | 0:8db2bcdf4574 | 42 | * @param product_id Your product_id |
sherckuith | 0:8db2bcdf4574 | 43 | * @param product_release Your preoduct_release |
sherckuith | 0:8db2bcdf4574 | 44 | */ |
sherckuith | 0:8db2bcdf4574 | 45 | USBCDCMSC(SDFileSystem *sd, uint16_t vendor_id = 0x1f00, uint16_t product_id = 0x2012, uint16_t product_release = 0x0001); |
sherckuith | 0:8db2bcdf4574 | 46 | |
sherckuith | 0:8db2bcdf4574 | 47 | /** |
sherckuith | 0:8db2bcdf4574 | 48 | * Send a character. You can use puts, printf. |
sherckuith | 0:8db2bcdf4574 | 49 | * |
sherckuith | 0:8db2bcdf4574 | 50 | * @param c character to be sent |
sherckuith | 0:8db2bcdf4574 | 51 | * @returns true if there is no error, false otherwise |
sherckuith | 0:8db2bcdf4574 | 52 | */ |
sherckuith | 0:8db2bcdf4574 | 53 | virtual int _putc(int c); |
sherckuith | 0:8db2bcdf4574 | 54 | |
sherckuith | 0:8db2bcdf4574 | 55 | /** |
sherckuith | 0:8db2bcdf4574 | 56 | * Read a character: blocking |
sherckuith | 0:8db2bcdf4574 | 57 | * |
sherckuith | 0:8db2bcdf4574 | 58 | * @returns character read |
sherckuith | 0:8db2bcdf4574 | 59 | */ |
sherckuith | 0:8db2bcdf4574 | 60 | virtual int _getc(); |
sherckuith | 0:8db2bcdf4574 | 61 | |
sherckuith | 0:8db2bcdf4574 | 62 | /** |
sherckuith | 0:8db2bcdf4574 | 63 | * Check the number of bytes available. |
sherckuith | 0:8db2bcdf4574 | 64 | * |
sherckuith | 0:8db2bcdf4574 | 65 | * @returns the number of bytes available |
sherckuith | 0:8db2bcdf4574 | 66 | */ |
sherckuith | 0:8db2bcdf4574 | 67 | uint8_t available(); |
sherckuith | 0:8db2bcdf4574 | 68 | |
sherckuith | 0:8db2bcdf4574 | 69 | /** |
sherckuith | 0:8db2bcdf4574 | 70 | * Write a block of data. |
sherckuith | 0:8db2bcdf4574 | 71 | * |
sherckuith | 0:8db2bcdf4574 | 72 | * For more efficiency, a block of size 64 (maximum size of a bulk endpoint) has to be written. |
sherckuith | 0:8db2bcdf4574 | 73 | * |
sherckuith | 0:8db2bcdf4574 | 74 | * @param buf pointer on data which will be written |
sherckuith | 0:8db2bcdf4574 | 75 | * @param size size of the buffer. The maximum size of a block is limited by the size of the endpoint (64 bytes) |
sherckuith | 0:8db2bcdf4574 | 76 | * |
sherckuith | 0:8db2bcdf4574 | 77 | * @returns true if successfull |
sherckuith | 0:8db2bcdf4574 | 78 | */ |
sherckuith | 0:8db2bcdf4574 | 79 | bool writeBlock(uint8_t * buf, uint16_t size); |
sherckuith | 0:8db2bcdf4574 | 80 | |
sherckuith | 0:8db2bcdf4574 | 81 | /** |
sherckuith | 0:8db2bcdf4574 | 82 | * Attach a member function to call when a packet is received. |
sherckuith | 0:8db2bcdf4574 | 83 | * |
sherckuith | 0:8db2bcdf4574 | 84 | * @param tptr pointer to the object to call the member function on |
sherckuith | 0:8db2bcdf4574 | 85 | * @param mptr pointer to the member function to be called |
sherckuith | 0:8db2bcdf4574 | 86 | */ |
sherckuith | 0:8db2bcdf4574 | 87 | template<typename T> |
sherckuith | 0:8db2bcdf4574 | 88 | void attach(T* tptr, void (T::*mptr)(void)) { |
sherckuith | 0:8db2bcdf4574 | 89 | if((mptr != NULL) && (tptr != NULL)) { |
sherckuith | 0:8db2bcdf4574 | 90 | rx.attach(tptr, mptr); |
sherckuith | 0:8db2bcdf4574 | 91 | } |
sherckuith | 0:8db2bcdf4574 | 92 | } |
sherckuith | 0:8db2bcdf4574 | 93 | |
sherckuith | 0:8db2bcdf4574 | 94 | /** |
sherckuith | 0:8db2bcdf4574 | 95 | * Attach a callback called when a packet is received |
sherckuith | 0:8db2bcdf4574 | 96 | * |
sherckuith | 0:8db2bcdf4574 | 97 | * @param fptr function pointer |
sherckuith | 0:8db2bcdf4574 | 98 | */ |
sherckuith | 0:8db2bcdf4574 | 99 | void attach(void (*fn)(void)) { |
sherckuith | 0:8db2bcdf4574 | 100 | if(fn != NULL) { |
sherckuith | 0:8db2bcdf4574 | 101 | rx.attach(fn); |
sherckuith | 0:8db2bcdf4574 | 102 | } |
sherckuith | 0:8db2bcdf4574 | 103 | } |
sherckuith | 0:8db2bcdf4574 | 104 | |
sherckuith | 0:8db2bcdf4574 | 105 | |
sherckuith | 0:8db2bcdf4574 | 106 | /** |
sherckuith | 0:8db2bcdf4574 | 107 | * Connect the USB MSD device. Establish disk initialization before really connect the device. |
sherckuith | 0:8db2bcdf4574 | 108 | * |
sherckuith | 0:8db2bcdf4574 | 109 | * @returns true if successful |
sherckuith | 0:8db2bcdf4574 | 110 | */ |
sherckuith | 0:8db2bcdf4574 | 111 | bool connect(); |
sherckuith | 0:8db2bcdf4574 | 112 | |
sherckuith | 0:8db2bcdf4574 | 113 | protected: |
sherckuith | 0:8db2bcdf4574 | 114 | |
sherckuith | 0:8db2bcdf4574 | 115 | /* |
sherckuith | 0:8db2bcdf4574 | 116 | * Get device descriptor. Warning: this method has to store the length of the report descriptor in reportLength. |
sherckuith | 0:8db2bcdf4574 | 117 | * |
sherckuith | 0:8db2bcdf4574 | 118 | * @returns pointer to the device descriptor |
sherckuith | 0:8db2bcdf4574 | 119 | */ |
sherckuith | 0:8db2bcdf4574 | 120 | virtual uint8_t * deviceDesc(); |
sherckuith | 0:8db2bcdf4574 | 121 | |
sherckuith | 0:8db2bcdf4574 | 122 | /* |
sherckuith | 0:8db2bcdf4574 | 123 | * Get string product descriptor |
sherckuith | 0:8db2bcdf4574 | 124 | * |
sherckuith | 0:8db2bcdf4574 | 125 | * @returns pointer to the string product descriptor |
sherckuith | 0:8db2bcdf4574 | 126 | */ |
sherckuith | 0:8db2bcdf4574 | 127 | virtual uint8_t * stringIproductDesc(); |
sherckuith | 0:8db2bcdf4574 | 128 | |
sherckuith | 0:8db2bcdf4574 | 129 | /* |
sherckuith | 0:8db2bcdf4574 | 130 | * Get string interface descriptor |
sherckuith | 0:8db2bcdf4574 | 131 | * |
sherckuith | 0:8db2bcdf4574 | 132 | * @returns pointer to the string interface descriptor |
sherckuith | 0:8db2bcdf4574 | 133 | */ |
sherckuith | 0:8db2bcdf4574 | 134 | virtual uint8_t * stringIinterfaceDesc(); |
sherckuith | 0:8db2bcdf4574 | 135 | |
sherckuith | 0:8db2bcdf4574 | 136 | /* |
sherckuith | 0:8db2bcdf4574 | 137 | * Get configuration descriptor |
sherckuith | 0:8db2bcdf4574 | 138 | * |
sherckuith | 0:8db2bcdf4574 | 139 | * @returns pointer to the configuration descriptor |
sherckuith | 0:8db2bcdf4574 | 140 | */ |
sherckuith | 0:8db2bcdf4574 | 141 | virtual uint8_t * configurationDesc(); |
sherckuith | 0:8db2bcdf4574 | 142 | |
sherckuith | 0:8db2bcdf4574 | 143 | /* |
sherckuith | 0:8db2bcdf4574 | 144 | * Send a buffer |
sherckuith | 0:8db2bcdf4574 | 145 | * |
sherckuith | 0:8db2bcdf4574 | 146 | * @param endpoint endpoint which will be sent the buffer |
sherckuith | 0:8db2bcdf4574 | 147 | * @param buffer buffer to be sent |
sherckuith | 0:8db2bcdf4574 | 148 | * @param size length of the buffer |
sherckuith | 0:8db2bcdf4574 | 149 | * @returns true if successful |
sherckuith | 0:8db2bcdf4574 | 150 | */ |
sherckuith | 0:8db2bcdf4574 | 151 | bool send(uint8_t * buffer, uint16_t size); |
sherckuith | 0:8db2bcdf4574 | 152 | |
sherckuith | 0:8db2bcdf4574 | 153 | /* |
sherckuith | 0:8db2bcdf4574 | 154 | * Read a buffer from a certain endpoint. Warning: blocking |
sherckuith | 0:8db2bcdf4574 | 155 | * |
sherckuith | 0:8db2bcdf4574 | 156 | * @param endpoint endpoint to read |
sherckuith | 0:8db2bcdf4574 | 157 | * @param buffer buffer where will be stored bytes |
sherckuith | 0:8db2bcdf4574 | 158 | * @param size the number of bytes read will be stored in *size |
sherckuith | 0:8db2bcdf4574 | 159 | * @param maxSize the maximum length that can be read |
sherckuith | 0:8db2bcdf4574 | 160 | * @returns true if successful |
sherckuith | 0:8db2bcdf4574 | 161 | */ |
sherckuith | 0:8db2bcdf4574 | 162 | bool readEP(uint8_t * buffer, uint16_t * size); |
sherckuith | 0:8db2bcdf4574 | 163 | |
sherckuith | 0:8db2bcdf4574 | 164 | /* |
sherckuith | 0:8db2bcdf4574 | 165 | * Read a buffer from a certain endpoint. Warning: non blocking |
sherckuith | 0:8db2bcdf4574 | 166 | * |
sherckuith | 0:8db2bcdf4574 | 167 | * @param endpoint endpoint to read |
sherckuith | 0:8db2bcdf4574 | 168 | * @param buffer buffer where will be stored bytes |
sherckuith | 0:8db2bcdf4574 | 169 | * @param size the number of bytes read will be stored in *size |
sherckuith | 0:8db2bcdf4574 | 170 | * @param maxSize the maximum length that can be read |
sherckuith | 0:8db2bcdf4574 | 171 | * @returns true if successful |
sherckuith | 0:8db2bcdf4574 | 172 | */ |
sherckuith | 0:8db2bcdf4574 | 173 | bool readEP_NB(uint8_t * buffer, uint16_t * size); |
sherckuith | 0:8db2bcdf4574 | 174 | |
sherckuith | 0:8db2bcdf4574 | 175 | virtual bool USBCallback_request(); |
sherckuith | 0:8db2bcdf4574 | 176 | virtual bool USBCallback_setConfiguration(uint8_t configuration); |
sherckuith | 0:8db2bcdf4574 | 177 | |
sherckuith | 0:8db2bcdf4574 | 178 | virtual bool EP2_OUT_callback(); |
sherckuith | 0:8db2bcdf4574 | 179 | |
sherckuith | 0:8db2bcdf4574 | 180 | /* |
sherckuith | 0:8db2bcdf4574 | 181 | * Callback called when a packet is received |
sherckuith | 0:8db2bcdf4574 | 182 | */ |
sherckuith | 0:8db2bcdf4574 | 183 | virtual bool EP5_OUT_callback(); |
sherckuith | 0:8db2bcdf4574 | 184 | |
sherckuith | 0:8db2bcdf4574 | 185 | /* |
sherckuith | 0:8db2bcdf4574 | 186 | * Callback called when a packet has been sent |
sherckuith | 0:8db2bcdf4574 | 187 | */ |
sherckuith | 0:8db2bcdf4574 | 188 | virtual bool EP5_IN_callback(); |
sherckuith | 0:8db2bcdf4574 | 189 | |
sherckuith | 0:8db2bcdf4574 | 190 | private: |
sherckuith | 0:8db2bcdf4574 | 191 | |
sherckuith | 0:8db2bcdf4574 | 192 | FunctionPointer rx; |
sherckuith | 0:8db2bcdf4574 | 193 | CircBuffer<uint8_t> cdcbuf; |
sherckuith | 0:8db2bcdf4574 | 194 | int cdcbreak; |
sherckuith | 0:8db2bcdf4574 | 195 | |
sherckuith | 0:8db2bcdf4574 | 196 | // MSC Bulk-only Stage |
sherckuith | 0:8db2bcdf4574 | 197 | enum Stage { |
sherckuith | 0:8db2bcdf4574 | 198 | READ_CBW, // wait a CBW |
sherckuith | 0:8db2bcdf4574 | 199 | ERROR, // error |
sherckuith | 0:8db2bcdf4574 | 200 | PROCESS_CBW, // process a CBW request |
sherckuith | 0:8db2bcdf4574 | 201 | SEND_CSW, // send a CSW |
sherckuith | 0:8db2bcdf4574 | 202 | WAIT_CSW, // wait that a CSW has been effectively sent |
sherckuith | 0:8db2bcdf4574 | 203 | }; |
sherckuith | 0:8db2bcdf4574 | 204 | |
sherckuith | 0:8db2bcdf4574 | 205 | // Bulk-only CBW |
sherckuith | 0:8db2bcdf4574 | 206 | typedef __packed struct { |
sherckuith | 0:8db2bcdf4574 | 207 | uint32_t Signature; |
sherckuith | 0:8db2bcdf4574 | 208 | uint32_t Tag; |
sherckuith | 0:8db2bcdf4574 | 209 | uint32_t DataLength; |
sherckuith | 0:8db2bcdf4574 | 210 | uint8_t Flags; |
sherckuith | 0:8db2bcdf4574 | 211 | uint8_t LUN; |
sherckuith | 0:8db2bcdf4574 | 212 | uint8_t CBLength; |
sherckuith | 0:8db2bcdf4574 | 213 | uint8_t CB[16]; |
sherckuith | 0:8db2bcdf4574 | 214 | } CBW; |
sherckuith | 0:8db2bcdf4574 | 215 | |
sherckuith | 0:8db2bcdf4574 | 216 | // Bulk-only CSW |
sherckuith | 0:8db2bcdf4574 | 217 | typedef __packed struct { |
sherckuith | 0:8db2bcdf4574 | 218 | uint32_t Signature; |
sherckuith | 0:8db2bcdf4574 | 219 | uint32_t Tag; |
sherckuith | 0:8db2bcdf4574 | 220 | uint32_t DataResidue; |
sherckuith | 0:8db2bcdf4574 | 221 | uint8_t Status; |
sherckuith | 0:8db2bcdf4574 | 222 | } CSW; |
sherckuith | 0:8db2bcdf4574 | 223 | |
sherckuith | 0:8db2bcdf4574 | 224 | //state of the bulk-only state machine |
sherckuith | 0:8db2bcdf4574 | 225 | Stage stage; |
sherckuith | 0:8db2bcdf4574 | 226 | |
sherckuith | 0:8db2bcdf4574 | 227 | // current CBW |
sherckuith | 0:8db2bcdf4574 | 228 | CBW cbw; |
sherckuith | 0:8db2bcdf4574 | 229 | |
sherckuith | 0:8db2bcdf4574 | 230 | // CSW which will be sent |
sherckuith | 0:8db2bcdf4574 | 231 | CSW csw; |
sherckuith | 0:8db2bcdf4574 | 232 | |
sherckuith | 0:8db2bcdf4574 | 233 | // addr where will be read or written data |
sherckuith | 0:8db2bcdf4574 | 234 | uint32_t addr; |
sherckuith | 0:8db2bcdf4574 | 235 | |
sherckuith | 0:8db2bcdf4574 | 236 | // length of a reading or writing |
sherckuith | 0:8db2bcdf4574 | 237 | uint32_t length; |
sherckuith | 0:8db2bcdf4574 | 238 | |
sherckuith | 0:8db2bcdf4574 | 239 | // memory OK (after a memoryVerify) |
sherckuith | 0:8db2bcdf4574 | 240 | bool memOK; |
sherckuith | 0:8db2bcdf4574 | 241 | |
sherckuith | 0:8db2bcdf4574 | 242 | // cache in RAM before writing in memory. Useful also to read a block. |
sherckuith | 0:8db2bcdf4574 | 243 | uint8_t * page; |
sherckuith | 0:8db2bcdf4574 | 244 | |
sherckuith | 0:8db2bcdf4574 | 245 | int BlockSize; |
sherckuith | 0:8db2bcdf4574 | 246 | int MemorySize; |
sherckuith | 0:8db2bcdf4574 | 247 | int BlockCount; |
sherckuith | 0:8db2bcdf4574 | 248 | |
sherckuith | 0:8db2bcdf4574 | 249 | int _status; |
sherckuith | 0:8db2bcdf4574 | 250 | SDFileSystem *_sd; |
sherckuith | 0:8db2bcdf4574 | 251 | |
sherckuith | 0:8db2bcdf4574 | 252 | void CBWDecode(uint8_t * buf, uint16_t size); |
sherckuith | 0:8db2bcdf4574 | 253 | void sendCSW (void); |
sherckuith | 0:8db2bcdf4574 | 254 | bool inquiryRequest (void); |
sherckuith | 0:8db2bcdf4574 | 255 | bool msd_write (uint8_t * buf, uint16_t size); |
sherckuith | 0:8db2bcdf4574 | 256 | bool readFormatCapacity(); |
sherckuith | 0:8db2bcdf4574 | 257 | bool readCapacity (void); |
sherckuith | 0:8db2bcdf4574 | 258 | bool infoTransfer (void); |
sherckuith | 0:8db2bcdf4574 | 259 | void memoryRead (void); |
sherckuith | 0:8db2bcdf4574 | 260 | bool modeSense6 (void); |
sherckuith | 0:8db2bcdf4574 | 261 | void testUnitReady (void); |
sherckuith | 0:8db2bcdf4574 | 262 | bool requestSense (void); |
sherckuith | 0:8db2bcdf4574 | 263 | void memoryVerify (uint8_t * buf, uint16_t size); |
sherckuith | 0:8db2bcdf4574 | 264 | void memoryWrite (uint8_t * buf, uint16_t size); |
sherckuith | 0:8db2bcdf4574 | 265 | void reset(); |
sherckuith | 0:8db2bcdf4574 | 266 | void fail(); |
sherckuith | 0:8db2bcdf4574 | 267 | |
sherckuith | 0:8db2bcdf4574 | 268 | int isBreak(); |
sherckuith | 0:8db2bcdf4574 | 269 | |
sherckuith | 0:8db2bcdf4574 | 270 | int disk_initialize(); |
sherckuith | 0:8db2bcdf4574 | 271 | int disk_write(const char *buffer, int block_number); |
sherckuith | 0:8db2bcdf4574 | 272 | int disk_read(char *buffer, int block_number); |
sherckuith | 0:8db2bcdf4574 | 273 | int disk_status(); |
sherckuith | 0:8db2bcdf4574 | 274 | int disk_sectors(); |
sherckuith | 0:8db2bcdf4574 | 275 | int disk_size(); |
sherckuith | 0:8db2bcdf4574 | 276 | |
sherckuith | 0:8db2bcdf4574 | 277 | }; |
sherckuith | 0:8db2bcdf4574 | 278 | |
sherckuith | 0:8db2bcdf4574 | 279 | #endif |