Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
USBCDCMSC.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 #ifndef USBCDCMSC_H 00020 #define USBCDCMSC_H 00021 00022 /* These headers are included for child class. */ 00023 #include "USBEndpoints.h" 00024 #include "USBDescriptor.h" 00025 #include "USBDevice_Types.h" 00026 00027 #include "USBDevice.h" 00028 00029 #include "Stream.h" 00030 #include "CircBuffer.h" 00031 00032 #include "SDFileSystem.h" 00033 00034 00035 class USBCDCMSC: public USBDevice, public Stream { 00036 public: 00037 00038 /* 00039 * Constructor 00040 * 00041 * @param vendor_id Your vendor_id 00042 * @param product_id Your product_id 00043 * @param product_release Your preoduct_release 00044 */ 00045 USBCDCMSC(SDFileSystem *sd, uint16_t vendor_id = 0x1f00, uint16_t product_id = 0x2012, uint16_t product_release = 0x0001); 00046 00047 /** 00048 * Send a character. You can use puts, printf. 00049 * 00050 * @param c character to be sent 00051 * @returns true if there is no error, false otherwise 00052 */ 00053 virtual int _putc(int c); 00054 00055 /** 00056 * Read a character: blocking 00057 * 00058 * @returns character read 00059 */ 00060 virtual int _getc(); 00061 00062 /** 00063 * Check the number of bytes available. 00064 * 00065 * @returns the number of bytes available 00066 */ 00067 uint8_t available(); 00068 00069 /** 00070 * Write a block of data. 00071 * 00072 * For more efficiency, a block of size 64 (maximum size of a bulk endpoint) has to be written. 00073 * 00074 * @param buf pointer on data which will be written 00075 * @param size size of the buffer. The maximum size of a block is limited by the size of the endpoint (64 bytes) 00076 * 00077 * @returns true if successfull 00078 */ 00079 bool writeBlock(uint8_t * buf, uint16_t size); 00080 00081 /** 00082 * Attach a member function to call when a packet is received. 00083 * 00084 * @param tptr pointer to the object to call the member function on 00085 * @param mptr pointer to the member function to be called 00086 */ 00087 template<typename T> 00088 void attach(T* tptr, void (T::*mptr)(void)) { 00089 if((mptr != NULL) && (tptr != NULL)) { 00090 rx.attach(tptr, mptr); 00091 } 00092 } 00093 00094 /** 00095 * Attach a callback called when a packet is received 00096 * 00097 * @param fptr function pointer 00098 */ 00099 void attach(void (*fn)(void)) { 00100 if(fn != NULL) { 00101 rx.attach(fn); 00102 } 00103 } 00104 00105 00106 /** 00107 * Connect the USB MSD device. Establish disk initialization before really connect the device. 00108 * 00109 * @returns true if successful 00110 */ 00111 bool connect(); 00112 00113 protected: 00114 00115 /* 00116 * Get device descriptor. Warning: this method has to store the length of the report descriptor in reportLength. 00117 * 00118 * @returns pointer to the device descriptor 00119 */ 00120 virtual uint8_t * deviceDesc(); 00121 00122 /* 00123 * Get string product descriptor 00124 * 00125 * @returns pointer to the string product descriptor 00126 */ 00127 virtual uint8_t * stringIproductDesc(); 00128 00129 /* 00130 * Get string interface descriptor 00131 * 00132 * @returns pointer to the string interface descriptor 00133 */ 00134 virtual uint8_t * stringIinterfaceDesc(); 00135 00136 /* 00137 * Get configuration descriptor 00138 * 00139 * @returns pointer to the configuration descriptor 00140 */ 00141 virtual uint8_t * configurationDesc(); 00142 00143 /* 00144 * Send a buffer 00145 * 00146 * @param endpoint endpoint which will be sent the buffer 00147 * @param buffer buffer to be sent 00148 * @param size length of the buffer 00149 * @returns true if successful 00150 */ 00151 bool send(uint8_t * buffer, uint16_t size); 00152 00153 /* 00154 * Read a buffer from a certain endpoint. Warning: blocking 00155 * 00156 * @param endpoint endpoint to read 00157 * @param buffer buffer where will be stored bytes 00158 * @param size the number of bytes read will be stored in *size 00159 * @param maxSize the maximum length that can be read 00160 * @returns true if successful 00161 */ 00162 bool readEP(uint8_t * buffer, uint16_t * size); 00163 00164 /* 00165 * Read a buffer from a certain endpoint. Warning: non blocking 00166 * 00167 * @param endpoint endpoint to read 00168 * @param buffer buffer where will be stored bytes 00169 * @param size the number of bytes read will be stored in *size 00170 * @param maxSize the maximum length that can be read 00171 * @returns true if successful 00172 */ 00173 bool readEP_NB(uint8_t * buffer, uint16_t * size); 00174 00175 virtual bool USBCallback_request(); 00176 virtual bool USBCallback_setConfiguration(uint8_t configuration); 00177 00178 virtual bool EP2_OUT_callback(); 00179 00180 /* 00181 * Callback called when a packet is received 00182 */ 00183 virtual bool EP5_OUT_callback(); 00184 00185 /* 00186 * Callback called when a packet has been sent 00187 */ 00188 virtual bool EP5_IN_callback(); 00189 00190 private: 00191 00192 FunctionPointer rx; 00193 CircBuffer<uint8_t> cdcbuf; 00194 int cdcbreak; 00195 00196 // MSC Bulk-only Stage 00197 enum Stage { 00198 READ_CBW, // wait a CBW 00199 ERROR, // error 00200 PROCESS_CBW, // process a CBW request 00201 SEND_CSW, // send a CSW 00202 WAIT_CSW, // wait that a CSW has been effectively sent 00203 }; 00204 00205 // Bulk-only CBW 00206 typedef __packed struct { 00207 uint32_t Signature; 00208 uint32_t Tag; 00209 uint32_t DataLength; 00210 uint8_t Flags; 00211 uint8_t LUN; 00212 uint8_t CBLength; 00213 uint8_t CB[16]; 00214 } CBW; 00215 00216 // Bulk-only CSW 00217 typedef __packed struct { 00218 uint32_t Signature; 00219 uint32_t Tag; 00220 uint32_t DataResidue; 00221 uint8_t Status; 00222 } CSW; 00223 00224 //state of the bulk-only state machine 00225 Stage stage; 00226 00227 // current CBW 00228 CBW cbw; 00229 00230 // CSW which will be sent 00231 CSW csw; 00232 00233 // addr where will be read or written data 00234 uint32_t addr; 00235 00236 // length of a reading or writing 00237 uint32_t length; 00238 00239 // memory OK (after a memoryVerify) 00240 bool memOK; 00241 00242 // cache in RAM before writing in memory. Useful also to read a block. 00243 uint8_t * page; 00244 00245 int BlockSize; 00246 int MemorySize; 00247 int BlockCount; 00248 00249 int _status; 00250 SDFileSystem *_sd; 00251 00252 void CBWDecode(uint8_t * buf, uint16_t size); 00253 void sendCSW (void); 00254 bool inquiryRequest (void); 00255 bool msd_write (uint8_t * buf, uint16_t size); 00256 bool readFormatCapacity(); 00257 bool readCapacity (void); 00258 bool infoTransfer (void); 00259 void memoryRead (void); 00260 bool modeSense6 (void); 00261 void testUnitReady (void); 00262 bool requestSense (void); 00263 void memoryVerify (uint8_t * buf, uint16_t size); 00264 void memoryWrite (uint8_t * buf, uint16_t size); 00265 void reset(); 00266 void fail(); 00267 00268 int isBreak(); 00269 00270 int disk_initialize(); 00271 int disk_write(const char *buffer, int block_number); 00272 int disk_read(char *buffer, int block_number); 00273 int disk_status(); 00274 int disk_sectors(); 00275 int disk_size(); 00276 00277 }; 00278 00279 #endif
Generated on Fri Jul 15 2022 02:30:55 by
1.7.2