USB device stack, fixes for USBSerial & KL25Z
Fork of USBDevice by
USBMSD.h
00001 /* Copyright (c) 2010-2011 mbed.org, MIT License 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00004 * and associated documentation files (the "Software"), to deal in the Software without 00005 * restriction, including without limitation the rights to use, copy, modify, merge, publish, 00006 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the 00007 * Software is furnished to do so, subject to the following conditions: 00008 * 00009 * The above copyright notice and this permission notice shall be included in all copies or 00010 * substantial portions of the Software. 00011 * 00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 */ 00018 00019 00020 #ifndef USBMSD_H 00021 #define USBMSD_H 00022 00023 /* These headers are included for child class. */ 00024 #include "USBEndpoints.h" 00025 #include "USBDescriptor.h" 00026 #include "USBDevice_Types.h" 00027 00028 #include "USBDevice.h" 00029 00030 /** 00031 * USBMSD class: generic class in order to use all kinds of blocks storage chip 00032 * 00033 * Introduction 00034 * 00035 * The USBMSD implements the MSD protocol. It permits to access a memory chip (flash, sdcard,...) 00036 * from a computer over USB. But this class doesn't work standalone, you need to subclass this class 00037 * and define virtual functions which are called in USBMSD. 00038 * 00039 * How to use this class with your chip ? 00040 * 00041 * You have to inherit and define some pure virtual functions (mandatory step): 00042 * - virtual int disk_read(char * data, int block): function to read a block 00043 * - virtual int disk_write(const char * data, int block): function to write a block 00044 * - virtual int disk_initialize(): function to initialize the memory 00045 * - virtual int disk_sectors(): return the number of blocks 00046 * - virtual int disk_size(): return the memory size 00047 * - 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) 00048 * 00049 * All functions names are compatible with the fat filesystem library. So you can imagine using your own class with 00050 * USBMSD and the fat filesystem library in the same program. Just be careful because there are two different parts which 00051 * will access the sd card. You can do a master/slave system using the disk_status method. 00052 * 00053 * Once these functions defined, you can call connect() (at the end of the constructor of your class for instance) 00054 * of USBMSD to connect your mass storage device. connect() will first call disk_status() to test the status of the disk. 00055 * If disk_status() returns 1 (disk not initialized), then disk_initialize() is called. After this step, connect() will collect information 00056 * such as the number of blocks and the memory size. 00057 */ 00058 class USBMSD: public USBDevice { 00059 public: 00060 00061 /** 00062 * Constructor 00063 * 00064 * @param vendor_id Your vendor_id 00065 * @param product_id Your product_id 00066 * @param product_release Your preoduct_release 00067 */ 00068 USBMSD(uint16_t vendor_id = 0x0703, uint16_t product_id = 0x0104, uint16_t product_release = 0x0001); 00069 00070 /** 00071 * Connect the USB MSD device. Establish disk initialization before really connect the device. 00072 * 00073 * @param blocking if not configured 00074 * @returns true if successful 00075 */ 00076 bool connect(bool blocking = true); 00077 00078 /** 00079 * Disconnect the USB MSD device. 00080 */ 00081 void disconnect(); 00082 00083 /** 00084 * Destructor 00085 */ 00086 ~USBMSD(); 00087 00088 protected: 00089 00090 /* 00091 * read a block on a storage chip 00092 * 00093 * @param data pointer where will be stored read data 00094 * @param block block number 00095 * @returns 0 if successful 00096 */ 00097 virtual int disk_read(uint8_t * data, uint64_t block) = 0; 00098 00099 /* 00100 * write a block on a storage chip 00101 * 00102 * @param data data to write 00103 * @param block block number 00104 * @returns 0 if successful 00105 */ 00106 virtual int disk_write(const uint8_t * data, uint64_t block) = 0; 00107 00108 /* 00109 * Disk initilization 00110 */ 00111 virtual int disk_initialize() = 0; 00112 00113 /* 00114 * Return the number of blocks 00115 * 00116 * @returns number of blocks 00117 */ 00118 virtual uint64_t disk_sectors() = 0; 00119 00120 /* 00121 * Return memory size 00122 * 00123 * @returns memory size 00124 */ 00125 virtual uint64_t disk_size() = 0; 00126 00127 00128 /* 00129 * To check the status of the storage chip 00130 * 00131 * @returns status: 0: OK, 1: disk not initialized, 2: no medium in the drive, 4: write protected 00132 */ 00133 virtual int disk_status() = 0; 00134 00135 /* 00136 * Get string product descriptor 00137 * 00138 * @returns pointer to the string product descriptor 00139 */ 00140 virtual uint8_t * stringIproductDesc(); 00141 00142 /* 00143 * Get string interface descriptor 00144 * 00145 * @returns pointer to the string interface descriptor 00146 */ 00147 virtual uint8_t * stringIinterfaceDesc(); 00148 00149 /* 00150 * Get configuration descriptor 00151 * 00152 * @returns pointer to the configuration descriptor 00153 */ 00154 virtual uint8_t * configurationDesc(); 00155 00156 /* 00157 * Callback called when a packet is received 00158 */ 00159 virtual bool EP2_OUT_callback(); 00160 00161 /* 00162 * Callback called when a packet has been sent 00163 */ 00164 virtual bool EP2_IN_callback(); 00165 00166 /* 00167 * Set configuration of device. Add endpoints 00168 */ 00169 virtual bool USBCallback_setConfiguration(uint8_t configuration); 00170 00171 /* 00172 * Callback called to process class specific requests 00173 */ 00174 virtual bool USBCallback_request(); 00175 00176 00177 private: 00178 00179 // MSC Bulk-only Stage 00180 enum Stage { 00181 READ_CBW, // wait a CBW 00182 ERROR, // error 00183 PROCESS_CBW, // process a CBW request 00184 SEND_CSW, // send a CSW 00185 WAIT_CSW, // wait that a CSW has been effectively sent 00186 }; 00187 00188 // Bulk-only CBW 00189 typedef struct { 00190 uint32_t Signature; 00191 uint32_t Tag; 00192 uint32_t DataLength; 00193 uint8_t Flags; 00194 uint8_t LUN; 00195 uint8_t CBLength; 00196 uint8_t CB[16]; 00197 } PACKED CBW; 00198 00199 // Bulk-only CSW 00200 typedef struct { 00201 uint32_t Signature; 00202 uint32_t Tag; 00203 uint32_t DataResidue; 00204 uint8_t Status; 00205 } PACKED CSW; 00206 00207 //state of the bulk-only state machine 00208 Stage stage; 00209 00210 // current CBW 00211 CBW cbw; 00212 00213 // CSW which will be sent 00214 CSW csw; 00215 00216 // addr where will be read or written data 00217 uint32_t addr; 00218 00219 // length of a reading or writing 00220 uint32_t length; 00221 00222 // memory OK (after a memoryVerify) 00223 bool memOK; 00224 00225 // cache in RAM before writing in memory. Useful also to read a block. 00226 uint8_t * page; 00227 00228 int BlockSize; 00229 uint64_t MemorySize; 00230 uint64_t BlockCount; 00231 00232 void CBWDecode(uint8_t * buf, uint16_t size); 00233 void sendCSW (void); 00234 bool inquiryRequest (void); 00235 bool write (uint8_t * buf, uint16_t size); 00236 bool readFormatCapacity(); 00237 bool readCapacity (void); 00238 bool infoTransfer (void); 00239 void memoryRead (void); 00240 bool modeSense6 (void); 00241 void testUnitReady (void); 00242 bool requestSense (void); 00243 void memoryVerify (uint8_t * buf, uint16_t size); 00244 void memoryWrite (uint8_t * buf, uint16_t size); 00245 void reset(); 00246 void fail(); 00247 }; 00248 00249 #endif
Generated on Wed Jul 13 2022 17:00:18 by 1.7.2