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