USB device stack, with KL25Z fixes for USB 3.0 hosts and sleep/resume interrupt handling
Dependents: frdm_Slider_Keyboard idd_hw2_figlax_PanType idd_hw2_appachu_finger_chording idd_hw3_AngieWangAntonioDeLimaFernandesDanielLim_BladeSymphony ... more
Fork of USBDevice by
USBDevice.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 USBDEVICE_H 00020 #define USBDEVICE_H 00021 00022 #include "mbed.h" 00023 #include "USBDevice_Types.h" 00024 #include "USBHAL.h" 00025 00026 class USBDevice: public USBHAL 00027 { 00028 public: 00029 USBDevice(uint16_t vendor_id, uint16_t product_id, uint16_t product_release); 00030 00031 /* 00032 * Check if the device is configured 00033 * 00034 * @returns true if configured, false otherwise 00035 */ 00036 bool configured(void); 00037 00038 /* 00039 * Connect a device 00040 * 00041 * @param blocking: block if not configured 00042 */ 00043 void connect(bool blocking = true); 00044 00045 /* 00046 * Disconnect a device 00047 */ 00048 void disconnect(void); 00049 00050 /* 00051 * Add an endpoint 00052 * 00053 * @param endpoint endpoint which will be added 00054 * @param maxPacket Maximum size of a packet which can be sent for this endpoint 00055 * @returns true if successful, false otherwise 00056 */ 00057 bool addEndpoint(uint8_t endpoint, uint32_t maxPacket); 00058 00059 /* 00060 * Start a reading on a certain endpoint. 00061 * You can access the result of the reading by USBDevice_read 00062 * 00063 * @param endpoint endpoint which will be read 00064 * @param maxSize the maximum length that can be read 00065 * @return true if successful 00066 */ 00067 bool readStart(uint8_t endpoint, uint32_t maxSize); 00068 00069 /* 00070 * Read a certain endpoint. Before calling this function, USBUSBDevice_readStart 00071 * must be called. 00072 * 00073 * Warning: blocking 00074 * 00075 * @param endpoint endpoint which will be read 00076 * @param buffer buffer will be filled with the data received 00077 * @param size the number of bytes read will be stored in *size 00078 * @param maxSize the maximum length that can be read 00079 * @returns true if successful 00080 */ 00081 bool readEP(uint8_t endpoint, uint8_t * buffer, uint32_t * size, uint32_t maxSize); 00082 00083 /* 00084 * Read a certain endpoint. 00085 * 00086 * Warning: non blocking 00087 * 00088 * @param endpoint endpoint which will be read 00089 * @param buffer buffer will be filled with the data received (if data are available) 00090 * @param size the number of bytes read will be stored in *size 00091 * @param maxSize the maximum length that can be read 00092 * @returns true if successful 00093 */ 00094 bool readEP_NB(uint8_t endpoint, uint8_t * buffer, uint32_t * size, uint32_t maxSize); 00095 00096 /* 00097 * Write a certain endpoint. 00098 * 00099 * Warning: blocking 00100 * 00101 * @param endpoint endpoint to write 00102 * @param buffer data contained in buffer will be write 00103 * @param size the number of bytes to write 00104 * @param maxSize the maximum length that can be written on this endpoint 00105 */ 00106 bool write(uint8_t endpoint, uint8_t * buffer, uint32_t size, uint32_t maxSize); 00107 00108 00109 /* 00110 * Write a certain endpoint. 00111 * 00112 * Warning: non blocking 00113 * 00114 * @param endpoint endpoint to write 00115 * @param buffer data contained in buffer will be write 00116 * @param size the number of bytes to write 00117 * @param maxSize the maximum length that can be written on this endpoint 00118 */ 00119 bool writeNB(uint8_t endpoint, uint8_t * buffer, uint32_t size, uint32_t maxSize); 00120 00121 /** 00122 * Write a certain endpoint. Blocks until the report has been sent successfully, 00123 * or until the timeout expires, whichever comes first. 00124 * 00125 * @param endpoint endpoint to write 00126 * @param buffer data contained in buffer will be write 00127 * @param size the number of bytes to write 00128 * @param maxSize the maximum length that can be written on this endpoint 00129 * @param timeout_ms timeout in milliseconds 00130 * @returns true if successful, false on error or timeout 00131 */ 00132 bool writeTO(uint8_t endpoint, uint8_t * buffer, uint32_t size, uint32_t maxSize, int timeout_ms); 00133 00134 /* 00135 * Called by USBDevice layer on bus reset. Warning: Called in ISR context 00136 * 00137 * May be used to reset state 00138 */ 00139 virtual void USBCallback_busReset(void) {}; 00140 00141 /* 00142 * Called by USBDevice on Endpoint0 request. Warning: Called in ISR context 00143 * This is used to handle extensions to standard requests 00144 * and class specific requests 00145 * 00146 * @returns true if class handles this request 00147 */ 00148 virtual bool USBCallback_request() { return false; }; 00149 00150 /* 00151 * Called by USBDevice on Endpoint0 request completion 00152 * if the 'notify' flag has been set to true. Warning: Called in ISR context 00153 * 00154 * In this case it is used to indicate that a HID report has 00155 * been received from the host on endpoint 0 00156 * 00157 * @param buf buffer received on endpoint 0 00158 * @param length length of this buffer 00159 */ 00160 virtual void USBCallback_requestCompleted(uint8_t * buf, uint32_t length) {}; 00161 00162 /* 00163 * Called by USBDevice layer. Set configuration of the device. 00164 * For instance, you can add all endpoints that you need on this function. 00165 * 00166 * @param configuration Number of the configuration 00167 */ 00168 virtual bool USBCallback_setConfiguration(uint8_t configuration) { return false; }; 00169 00170 /* 00171 * Called by USBDevice layer. Set interface/alternate of the device. 00172 * 00173 * @param interface Number of the interface to be configured 00174 * @param alternate Number of the alternate to be configured 00175 * @returns true if class handles this request 00176 */ 00177 virtual bool USBCallback_setInterface(uint16_t interface, uint8_t alternate) { return false; } 00178 virtual bool USBCallback_getInterface(uint16_t interface, uint8_t *alternate) { return false; } 00179 00180 /* 00181 * Get device descriptor. Warning: this method has to store the length of the report descriptor in reportLength. 00182 * 00183 * @returns pointer to the device descriptor 00184 */ 00185 virtual const uint8_t *deviceDesc(); 00186 00187 /* 00188 * Get configuration descriptor 00189 * 00190 * @returns pointer to the configuration descriptor 00191 */ 00192 virtual const uint8_t *configurationDesc(){return NULL;}; 00193 00194 /* 00195 * Get string lang id descriptor 00196 * 00197 * @return pointer to the string lang id descriptor 00198 */ 00199 virtual const uint8_t *stringLangidDesc(); 00200 00201 /* 00202 * Get string manufacturer descriptor 00203 * 00204 * @returns pointer to the string manufacturer descriptor 00205 */ 00206 virtual const uint8_t *stringImanufacturerDesc(); 00207 00208 /* 00209 * Get string product descriptor 00210 * 00211 * @returns pointer to the string product descriptor 00212 */ 00213 virtual const uint8_t *stringIproductDesc(); 00214 00215 /* 00216 * Get string serial descriptor 00217 * 00218 * @returns pointer to the string serial descriptor 00219 */ 00220 virtual const uint8_t *stringIserialDesc(); 00221 00222 /* 00223 * Get string configuration descriptor 00224 * 00225 * @returns pointer to the string configuration descriptor 00226 */ 00227 virtual const uint8_t *stringIConfigurationDesc(); 00228 00229 /* 00230 * Get string interface descriptor 00231 * 00232 * @returns pointer to the string interface descriptor 00233 */ 00234 virtual const uint8_t *stringIinterfaceDesc(); 00235 00236 00237 protected: 00238 virtual void busReset(void); 00239 virtual void EP0setupCallback(void); 00240 virtual void EP0out(void); 00241 virtual void EP0in(void); 00242 virtual void setDeviceState(DEVICE_STATE state) { device.state = state; } 00243 virtual void connectStateChanged(unsigned int connected); 00244 virtual void suspendStateChanged(unsigned int suspended); 00245 virtual void sleepStateChanged(unsigned int sleep); 00246 const uint8_t *findDescriptor(uint8_t descriptorType, int idx); 00247 CONTROL_TRANSFER *getTransferPtr(void); 00248 00249 uint16_t VENDOR_ID; 00250 uint16_t PRODUCT_ID; 00251 uint16_t PRODUCT_RELEASE; 00252 00253 private: 00254 bool addRateFeedbackEndpoint(uint8_t endpoint, uint32_t maxPacket); 00255 bool requestGetDescriptor(void); 00256 bool controlOut(void); 00257 bool controlIn(void); 00258 bool requestSetAddress(void); 00259 bool requestSetConfiguration(void); 00260 bool requestSetFeature(void); 00261 bool requestClearFeature(void); 00262 bool requestGetStatus(void); 00263 bool requestSetup(void); 00264 bool controlSetup(void); 00265 void decodeSetupPacket(uint8_t *data, SETUP_PACKET *packet); 00266 bool requestGetConfiguration(void); 00267 virtual bool requestGetInterface(void); 00268 virtual bool requestSetInterface(void); 00269 00270 CONTROL_TRANSFER transfer; 00271 USB_DEVICE device; 00272 }; 00273 00274 00275 #endif
Generated on Tue Jul 12 2022 18:05:21 by 1.7.2