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