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