USB device stack - modified
Fork of USBDevice by
Embed:
(wiki syntax)
Show/hide line numbers
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 "USBDevice/USBDevice/USBEndpoints.h" 00025 #include "USBDevice/USBDevice/USBDescriptor.h" 00026 #include "USBDevice/USBDevice/USBDevice_Types.h" 00027 00028 #include "USBDevice/USBDevice/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 { 00060 public: 00061 00062 /** 00063 * Constructor 00064 * 00065 * @param vendor_id Your vendor_id 00066 * @param product_id Your product_id 00067 * @param product_release Your preoduct_release 00068 */ 00069 USBMSD( uint16_t vendor_id = 0x0703, uint16_t product_id = 0x0104, uint16_t product_release = 0x0001 ); 00070 00071 /** 00072 * Connect the USB MSD device. Establish disk initialization before really connect the device. 00073 * 00074 * @returns true if successful 00075 */ 00076 bool connect(); 00077 00078 00079 protected: 00080 00081 /* 00082 * read a block on a storage chip 00083 * 00084 * @param data pointer where will be stored read data 00085 * @param block block number 00086 * @returns 0 if successful 00087 */ 00088 virtual int disk_read( char *data, int block ) = 0; 00089 00090 /* 00091 * write a block on a storage chip 00092 * 00093 * @param data data to write 00094 * @param block block number 00095 * @returns 0 if successful 00096 */ 00097 virtual int disk_write( const char *data, int block ) = 0; 00098 00099 /* 00100 * Disk initilization 00101 */ 00102 virtual int disk_initialize() = 0; 00103 00104 /* 00105 * Return the number of blocks 00106 * 00107 * @returns number of blocks 00108 */ 00109 virtual int disk_sectors() = 0; 00110 00111 /* 00112 * Return memory size 00113 * 00114 * @returns memory size 00115 */ 00116 virtual int disk_size() = 0; 00117 00118 00119 /* 00120 * To check the status of the storage chip 00121 * 00122 * @returns status: 0: OK, 1: disk not initialized, 2: no medium in the drive, 4: write protected 00123 */ 00124 virtual int disk_status() = 0; 00125 00126 /* 00127 * Get string product descriptor 00128 * 00129 * @returns pointer to the string product descriptor 00130 */ 00131 virtual uint8_t *stringIproductDesc(); 00132 00133 /* 00134 * Get string interface descriptor 00135 * 00136 * @returns pointer to the string interface descriptor 00137 */ 00138 virtual uint8_t *stringIinterfaceDesc(); 00139 00140 /* 00141 * Get configuration descriptor 00142 * 00143 * @returns pointer to the configuration descriptor 00144 */ 00145 virtual uint8_t *configurationDesc(); 00146 00147 /* 00148 * Callback called when a packet is received 00149 */ 00150 virtual bool EP2_OUT_callback(); 00151 00152 /* 00153 * Callback called when a packet has been sent 00154 */ 00155 virtual bool EP2_IN_callback(); 00156 00157 /* 00158 * Set configuration of device. Add endpoints 00159 */ 00160 virtual bool USBCallback_setConfiguration( uint8_t configuration ); 00161 00162 /* 00163 * Callback called to process class specific requests 00164 */ 00165 virtual bool USBCallback_request(); 00166 00167 00168 private: 00169 00170 // MSC Bulk-only Stage 00171 enum Stage 00172 { 00173 READ_CBW, // wait a CBW 00174 ERROR, // error 00175 PROCESS_CBW, // process a CBW request 00176 SEND_CSW, // send a CSW 00177 WAIT_CSW, // wait that a CSW has been effectively sent 00178 }; 00179 00180 // Bulk-only CBW 00181 typedef __packed struct 00182 { 00183 uint32_t Signature; 00184 uint32_t Tag; 00185 uint32_t DataLength; 00186 uint8_t Flags; 00187 uint8_t LUN; 00188 uint8_t CBLength; 00189 uint8_t CB[16]; 00190 } CBW; 00191 00192 // Bulk-only CSW 00193 typedef __packed struct 00194 { 00195 uint32_t Signature; 00196 uint32_t Tag; 00197 uint32_t DataResidue; 00198 uint8_t Status; 00199 } CSW; 00200 00201 //state of the bulk-only state machine 00202 Stage stage; 00203 00204 // current CBW 00205 CBW cbw; 00206 00207 // CSW which will be sent 00208 CSW csw; 00209 00210 // addr where will be read or written data 00211 uint32_t addr; 00212 00213 // length of a reading or writing 00214 uint32_t length; 00215 00216 // memory OK (after a memoryVerify) 00217 bool memOK; 00218 00219 // cache in RAM before writing in memory. Useful also to read a block. 00220 uint8_t *page; 00221 00222 int BlockSize; 00223 int MemorySize; 00224 int BlockCount; 00225 00226 void CBWDecode( uint8_t *buf, uint16_t size ); 00227 void sendCSW ( void ); 00228 bool inquiryRequest ( void ); 00229 bool write ( uint8_t *buf, uint16_t size ); 00230 bool readFormatCapacity(); 00231 bool readCapacity ( void ); 00232 bool infoTransfer ( void ); 00233 void memoryRead ( void ); 00234 bool modeSense6 ( void ); 00235 void testUnitReady ( void ); 00236 bool requestSense ( void ); 00237 void memoryVerify ( uint8_t *buf, uint16_t size ); 00238 void memoryWrite ( uint8_t *buf, uint16_t size ); 00239 void reset(); 00240 void fail(); 00241 }; 00242 00243 #endif 00244
Generated on Sat Jul 16 2022 03:17:41 by 1.7.2