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