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@7:6494da2a5c60, 2011-11-14 (annotated)
- Committer:
- samux
- Date:
- Mon Nov 14 12:08:32 2011 +0000
- Revision:
- 7:6494da2a5c60
- Parent:
- USBDevice/USBMSD/USBMSD.h@6:126c4d980196
- Child:
- 8:534fd41d8cc7
will try to use ChaNFSSD
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux |
2:27a7e7f8d399 | 1 | /* USBMSD.h */ |
samux |
2:27a7e7f8d399 | 2 | /* USB mass storage device example */ |
samux |
2:27a7e7f8d399 | 3 | /* Copyright (c) 2011 ARM Limited. All rights reserved. */ |
samux |
2:27a7e7f8d399 | 4 | |
samux |
2:27a7e7f8d399 | 5 | /* |
samux |
2:27a7e7f8d399 | 6 | * Guide to adapt this class to your storage chip: |
samux |
2:27a7e7f8d399 | 7 | * |
samux |
2:27a7e7f8d399 | 8 | */ |
samux |
2:27a7e7f8d399 | 9 | |
samux |
2:27a7e7f8d399 | 10 | #ifndef USBMSD_H |
samux |
2:27a7e7f8d399 | 11 | #define USBMSD_H |
samux |
2:27a7e7f8d399 | 12 | |
samux |
2:27a7e7f8d399 | 13 | /* These headers are included for child class. */ |
samux |
2:27a7e7f8d399 | 14 | #include "USBEndpoints.h" |
samux |
2:27a7e7f8d399 | 15 | #include "USBDescriptor.h" |
samux |
2:27a7e7f8d399 | 16 | #include "USBDevice_Types.h" |
samux |
2:27a7e7f8d399 | 17 | |
samux |
2:27a7e7f8d399 | 18 | #include "USBDevice.h" |
samux |
2:27a7e7f8d399 | 19 | |
samux |
2:27a7e7f8d399 | 20 | #define DEFAULT_CONFIGURATION (1) |
samux |
2:27a7e7f8d399 | 21 | |
samux |
2:27a7e7f8d399 | 22 | |
samux |
2:27a7e7f8d399 | 23 | // MSC Bulk-only Stage |
samux |
2:27a7e7f8d399 | 24 | enum Stage { |
samux |
5:8afbc15d6892 | 25 | READ_CBW, // wait a CBW |
samux |
6:126c4d980196 | 26 | ERROR, // error |
samux |
5:8afbc15d6892 | 27 | PROCESS_CBW, // process a CBW request |
samux |
5:8afbc15d6892 | 28 | SEND_CSW, // send a CSW |
samux |
5:8afbc15d6892 | 29 | WAIT_CSW, // wait that a CSW has been effectively sent |
samux |
2:27a7e7f8d399 | 30 | }; |
samux |
2:27a7e7f8d399 | 31 | |
samux |
2:27a7e7f8d399 | 32 | // Bulk-only CBW |
samux |
2:27a7e7f8d399 | 33 | typedef __packed struct { |
samux |
2:27a7e7f8d399 | 34 | uint32_t Signature; |
samux |
2:27a7e7f8d399 | 35 | uint32_t Tag; |
samux |
2:27a7e7f8d399 | 36 | uint32_t DataLength; |
samux |
2:27a7e7f8d399 | 37 | uint8_t Flags; |
samux |
2:27a7e7f8d399 | 38 | uint8_t LUN; |
samux |
2:27a7e7f8d399 | 39 | uint8_t CBLength; |
samux |
2:27a7e7f8d399 | 40 | uint8_t CB[16]; |
samux |
2:27a7e7f8d399 | 41 | } CBW; |
samux |
2:27a7e7f8d399 | 42 | |
samux |
2:27a7e7f8d399 | 43 | // Bulk-only CSW |
samux |
2:27a7e7f8d399 | 44 | typedef __packed struct { |
samux |
2:27a7e7f8d399 | 45 | uint32_t Signature; |
samux |
2:27a7e7f8d399 | 46 | uint32_t Tag; |
samux |
2:27a7e7f8d399 | 47 | uint32_t DataResidue; |
samux |
2:27a7e7f8d399 | 48 | uint8_t Status; |
samux |
2:27a7e7f8d399 | 49 | } CSW; |
samux |
2:27a7e7f8d399 | 50 | |
samux |
3:0ffb2eee9e06 | 51 | |
samux |
2:27a7e7f8d399 | 52 | class USBMSD: public USBDevice { |
samux |
2:27a7e7f8d399 | 53 | public: |
samux |
2:27a7e7f8d399 | 54 | |
samux |
2:27a7e7f8d399 | 55 | /** |
samux |
2:27a7e7f8d399 | 56 | * Constructor |
samux |
2:27a7e7f8d399 | 57 | * |
samux |
2:27a7e7f8d399 | 58 | * @param vendor_id Your vendor_id |
samux |
2:27a7e7f8d399 | 59 | * @param product_id Your product_id |
samux |
2:27a7e7f8d399 | 60 | * @param product_release Your preoduct_release |
samux |
2:27a7e7f8d399 | 61 | */ |
samux |
2:27a7e7f8d399 | 62 | USBMSD(uint16_t vendor_id = 0x0703, uint16_t product_id = 0x0104, uint16_t product_release = 0x0001); |
samux |
2:27a7e7f8d399 | 63 | |
samux |
3:0ffb2eee9e06 | 64 | /* |
samux |
3:0ffb2eee9e06 | 65 | * read a block on a storage chip |
samux |
3:0ffb2eee9e06 | 66 | * |
samux |
3:0ffb2eee9e06 | 67 | * @param data pointer where will be stored read data |
samux |
3:0ffb2eee9e06 | 68 | * @param block block number |
samux |
3:0ffb2eee9e06 | 69 | * @returns 0 if successful |
samux |
3:0ffb2eee9e06 | 70 | */ |
samux |
3:0ffb2eee9e06 | 71 | virtual int blockRead(uint8_t * data, uint16_t block){return 1;}; |
samux |
3:0ffb2eee9e06 | 72 | |
samux |
3:0ffb2eee9e06 | 73 | /* |
samux |
3:0ffb2eee9e06 | 74 | * write a block on a storage chip |
samux |
3:0ffb2eee9e06 | 75 | * |
samux |
3:0ffb2eee9e06 | 76 | * @param data data to write |
samux |
3:0ffb2eee9e06 | 77 | * @param block block number |
samux |
3:0ffb2eee9e06 | 78 | * @returns 0 if successful |
samux |
3:0ffb2eee9e06 | 79 | */ |
samux |
3:0ffb2eee9e06 | 80 | virtual int blockWrite(uint8_t * data, uint16_t block){return 1;}; |
samux |
3:0ffb2eee9e06 | 81 | |
samux |
6:126c4d980196 | 82 | /* |
samux |
6:126c4d980196 | 83 | * Disk initilization |
samux |
6:126c4d980196 | 84 | */ |
samux |
6:126c4d980196 | 85 | virtual int diskInit(){return -1;}; |
samux |
6:126c4d980196 | 86 | |
samux |
6:126c4d980196 | 87 | /* |
samux |
6:126c4d980196 | 88 | * Return block size |
samux |
6:126c4d980196 | 89 | * |
samux |
6:126c4d980196 | 90 | * @returns size of a block |
samux |
6:126c4d980196 | 91 | */ |
samux |
6:126c4d980196 | 92 | virtual uint16_t blockSize(){return 0;}; |
samux |
6:126c4d980196 | 93 | |
samux |
6:126c4d980196 | 94 | /* |
samux |
6:126c4d980196 | 95 | * Return memory size |
samux |
6:126c4d980196 | 96 | * |
samux |
6:126c4d980196 | 97 | * @returns memory size |
samux |
6:126c4d980196 | 98 | */ |
samux |
6:126c4d980196 | 99 | virtual uint32_t memorySize(){return 0;}; |
samux |
6:126c4d980196 | 100 | |
samux |
6:126c4d980196 | 101 | /* |
samux |
6:126c4d980196 | 102 | * Connect the USB MSD device. Establish disk initialization before really connect the device. |
samux |
6:126c4d980196 | 103 | * |
samux |
6:126c4d980196 | 104 | * @returns |
samux |
6:126c4d980196 | 105 | */ |
samux |
6:126c4d980196 | 106 | bool connect(); |
samux |
6:126c4d980196 | 107 | |
samux |
4:980e6470dcce | 108 | |
samux |
3:0ffb2eee9e06 | 109 | protected: |
samux |
2:27a7e7f8d399 | 110 | |
samux |
2:27a7e7f8d399 | 111 | |
samux |
2:27a7e7f8d399 | 112 | /* |
samux |
2:27a7e7f8d399 | 113 | * Get number of logical unit - 1 (here 0) |
samux |
2:27a7e7f8d399 | 114 | * |
samux |
2:27a7e7f8d399 | 115 | * @returns Pointer containing the number of logical unit - 1 |
samux |
2:27a7e7f8d399 | 116 | */ |
samux |
2:27a7e7f8d399 | 117 | uint8_t * getMaxLUN(); |
samux |
2:27a7e7f8d399 | 118 | |
samux |
2:27a7e7f8d399 | 119 | /* |
samux |
2:27a7e7f8d399 | 120 | * Get string product descriptor |
samux |
2:27a7e7f8d399 | 121 | * |
samux |
2:27a7e7f8d399 | 122 | * @returns pointer to the string product descriptor |
samux |
2:27a7e7f8d399 | 123 | */ |
samux |
2:27a7e7f8d399 | 124 | virtual uint8_t * stringIproductDesc(); |
samux |
2:27a7e7f8d399 | 125 | |
samux |
2:27a7e7f8d399 | 126 | /* |
samux |
2:27a7e7f8d399 | 127 | * Get string interface descriptor |
samux |
2:27a7e7f8d399 | 128 | * |
samux |
2:27a7e7f8d399 | 129 | * @returns pointer to the string interface descriptor |
samux |
2:27a7e7f8d399 | 130 | */ |
samux |
2:27a7e7f8d399 | 131 | virtual uint8_t * stringIinterfaceDesc(); |
samux |
2:27a7e7f8d399 | 132 | |
samux |
2:27a7e7f8d399 | 133 | /* |
samux |
2:27a7e7f8d399 | 134 | * Get configuration descriptor |
samux |
2:27a7e7f8d399 | 135 | * |
samux |
2:27a7e7f8d399 | 136 | * @returns pointer to the configuration descriptor |
samux |
2:27a7e7f8d399 | 137 | */ |
samux |
2:27a7e7f8d399 | 138 | virtual uint8_t * configurationDesc(); |
samux |
2:27a7e7f8d399 | 139 | |
samux |
2:27a7e7f8d399 | 140 | /* |
samux |
2:27a7e7f8d399 | 141 | * Callback called when a packet is received |
samux |
2:27a7e7f8d399 | 142 | */ |
samux |
2:27a7e7f8d399 | 143 | virtual bool EP2_OUT_callback(); |
samux |
2:27a7e7f8d399 | 144 | |
samux |
2:27a7e7f8d399 | 145 | /* |
samux |
2:27a7e7f8d399 | 146 | * Callback called when a packet has been sent |
samux |
2:27a7e7f8d399 | 147 | */ |
samux |
2:27a7e7f8d399 | 148 | virtual bool EP2_IN_callback(); |
samux |
2:27a7e7f8d399 | 149 | |
samux |
2:27a7e7f8d399 | 150 | /* |
samux |
2:27a7e7f8d399 | 151 | * Set configuration of device. Add endpoints |
samux |
2:27a7e7f8d399 | 152 | */ |
samux |
2:27a7e7f8d399 | 153 | virtual bool USBCallback_setConfiguration(uint8_t configuration); |
samux |
2:27a7e7f8d399 | 154 | |
samux |
2:27a7e7f8d399 | 155 | /* |
samux |
2:27a7e7f8d399 | 156 | * Callback called to process class specific requests |
samux |
2:27a7e7f8d399 | 157 | */ |
samux |
2:27a7e7f8d399 | 158 | virtual bool USBCallback_request(); |
samux |
2:27a7e7f8d399 | 159 | |
samux |
2:27a7e7f8d399 | 160 | |
samux |
2:27a7e7f8d399 | 161 | private: |
samux |
2:27a7e7f8d399 | 162 | //state of the bulk-only state machine |
samux |
2:27a7e7f8d399 | 163 | Stage stage; |
samux |
2:27a7e7f8d399 | 164 | |
samux |
2:27a7e7f8d399 | 165 | // current CBW |
samux |
2:27a7e7f8d399 | 166 | CBW cbw; |
samux |
2:27a7e7f8d399 | 167 | |
samux |
2:27a7e7f8d399 | 168 | // CSW which will be sent |
samux |
2:27a7e7f8d399 | 169 | CSW csw; |
samux |
2:27a7e7f8d399 | 170 | |
samux |
2:27a7e7f8d399 | 171 | // addr where will be read or written data |
samux |
2:27a7e7f8d399 | 172 | uint32_t addr; |
samux |
2:27a7e7f8d399 | 173 | |
samux |
2:27a7e7f8d399 | 174 | // length of a reading or writing |
samux |
2:27a7e7f8d399 | 175 | uint32_t length; |
samux |
2:27a7e7f8d399 | 176 | |
samux |
2:27a7e7f8d399 | 177 | // memory OK (after a memoryVerify) |
samux |
2:27a7e7f8d399 | 178 | bool memOK; |
samux |
2:27a7e7f8d399 | 179 | |
samux |
2:27a7e7f8d399 | 180 | // cache in RAM before writing in memory. Useful also to read a block. |
samux |
6:126c4d980196 | 181 | uint8_t * page; |
samux |
6:126c4d980196 | 182 | |
samux |
6:126c4d980196 | 183 | uint16_t BlockSize; |
samux |
6:126c4d980196 | 184 | uint32_t MemorySize; |
samux |
6:126c4d980196 | 185 | uint16_t BlockCount; |
samux |
2:27a7e7f8d399 | 186 | |
samux |
2:27a7e7f8d399 | 187 | void CBWDecode(uint8_t * buf, uint16_t size); |
samux |
2:27a7e7f8d399 | 188 | void sendCSW (void); |
samux |
2:27a7e7f8d399 | 189 | bool inquiryRequest (void); |
samux |
2:27a7e7f8d399 | 190 | bool write (uint8_t * buf, uint16_t size); |
samux |
2:27a7e7f8d399 | 191 | bool readFormatCapacity(); |
samux |
2:27a7e7f8d399 | 192 | bool readCapacity (void); |
samux |
2:27a7e7f8d399 | 193 | bool infoTransfer (void); |
samux |
2:27a7e7f8d399 | 194 | void memoryRead (void); |
samux |
2:27a7e7f8d399 | 195 | bool modeSense6 (void); |
samux |
2:27a7e7f8d399 | 196 | void testUnitReady (void); |
samux |
2:27a7e7f8d399 | 197 | bool requestSense (void); |
samux |
2:27a7e7f8d399 | 198 | void memoryVerify (uint8_t * buf, uint16_t size); |
samux |
2:27a7e7f8d399 | 199 | void memoryWrite (uint8_t * buf, uint16_t size); |
samux |
2:27a7e7f8d399 | 200 | void reset(); |
samux |
2:27a7e7f8d399 | 201 | void fail(); |
samux |
2:27a7e7f8d399 | 202 | }; |
samux |
2:27a7e7f8d399 | 203 | |
samux |
2:27a7e7f8d399 | 204 | #endif |