2 /* Generic USB device */
3 /* Copyright (c) 2011 ARM Limited. All rights reserved. */
9 #include "USBDevice_Types.h"
10 #include "USBBusInterface.h"
14 class USBDevice
: public USBHAL
17 USBDevice(uint16_t vendor_id
, uint16_t product_id
, uint16_t product_release
);
20 * Check if the device is configured
22 * @returns true if configured, false otherwise
24 bool configured(void);
34 void disconnect(void);
39 * @param endpoint endpoint which will be added
40 * @param maxPacket Maximum size of a packet which can be sent for this endpoint
41 * @returns true if successful, false otherwise
43 bool addEndpoint(uint8_t endpoint
, uint32_t maxPacket
);
46 * Start a reading on a certain endpoint.
47 * You can access the result of the reading by USBDevice_read
49 * @param endpoint endpoint which will be read
50 * @param maxSize the maximum length that can be read
51 * @return true if successful
53 bool readStart(uint8_t endpoint
, uint16_t maxSize
);
56 * Read a certain endpoint. Before calling this function, USBUSBDevice_readStart
61 * @param endpoint endpoint which will be read
62 * @param buffer buffer will be filled with the data received
63 * @param size the number of bytes read will be stored in *size
64 * @param maxSize the maximum length that can be read
65 * @returns true if successful
67 bool readEP(uint8_t endpoint
, uint8_t * buffer
, uint16_t * size
, uint16_t maxSize
);
70 * Read a certain endpoint.
72 * Warning: non blocking
74 * @param endpoint endpoint which will be read
75 * @param buffer buffer will be filled with the data received (if data are available)
76 * @param size the number of bytes read will be stored in *size
77 * @param maxSize the maximum length that can be read
78 * @returns true if successful
80 bool readEP_NB(uint8_t endpoint
, uint8_t * buffer
, uint16_t * size
, uint16_t maxSize
);
83 * Write a certain endpoint.
87 * @param endpoint endpoint to write
88 * @param buffer data contained in buffer will be write
89 * @param size the number of bytes to write
90 * @param maxSize the maximum length that can be written on this endpoint
92 bool write(uint8_t endpoint
, uint8_t * buffer
, uint16_t size
, uint16_t maxSize
);
96 * Write a certain endpoint.
98 * Warning: non blocking
100 * @param endpoint endpoint to write
101 * @param buffer data contained in buffer will be write
102 * @param size the number of bytes to write
103 * @param maxSize the maximum length that can be written on this endpoint
105 bool writeNB(uint8_t endpoint
, uint8_t * buffer
, uint16_t size
, uint16_t maxSize
);
109 * Called by USBDevice layer on bus reset. Warning: Called in ISR context
111 * May be used to reset state
113 virtual void USBCallback_busReset(void) {};
116 * Called by USBDevice on Endpoint0 request. Warning: Called in ISR context
117 * This is used to handle extensions to standard requests
118 * and class specific requests
120 * @returns true if class handles this request
122 virtual bool USBCallback_request() { return false; };
125 * Called by USBDevice on Endpoint0 request completion
126 * if the 'notify' flag has been set to true. Warning: Called in ISR context
128 * In this case it is used to indicate that a HID report has
129 * been received from the host on endpoint 0
131 * @param buf buffer received on endpoint 0
132 * @param length length of this buffer
134 virtual void USBCallback_requestCompleted(uint8_t * buf
, uint16_t length
) {};
137 * Called by USBDevice layer. Set configuration of the device.
138 * For instance, you can add all endpoints that you need on this function.
140 * @param configuration Number of the configuration
142 virtual bool USBCallback_setConfiguration(uint8_t configuration
) { return false; };
145 * Called by USBDevice layer. Set interface/alternate of the device.
147 * @param interface Number of the interface to be configured
148 * @param alternate Number of the alternate to be configured
149 * @returns true if class handles this request
151 virtual bool USBCallback_setInterface(uint16_t interface
, uint8_t alternate
) { return false; };
154 * Get device descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
156 * @returns pointer to the device descriptor
158 virtual uint8_t * deviceDesc();
161 * Get configuration descriptor
163 * @returns pointer to the configuration descriptor
165 virtual uint8_t * configurationDesc(){return NULL
;};
168 * Get string lang id descriptor
170 * @return pointer to the string lang id descriptor
172 virtual uint8_t * stringLangidDesc();
175 * Get string manufacturer descriptor
177 * @returns pointer to the string manufacturer descriptor
179 virtual uint8_t * stringImanufacturerDesc();
182 * Get string product descriptor
184 * @returns pointer to the string product descriptor
186 virtual uint8_t * stringIproductDesc();
189 * Get string serial descriptor
191 * @returns pointer to the string serial descriptor
193 virtual uint8_t * stringIserialDesc();
196 * Get string configuration descriptor
198 * @returns pointer to the string configuration descriptor
200 virtual uint8_t * stringIConfigurationDesc();
203 * Get string interface descriptor
205 * @returns pointer to the string interface descriptor
207 virtual uint8_t * stringIinterfaceDesc();
210 * Get the length of the report descriptor
212 * @returns length of the report descriptor
214 virtual uint16_t reportDescLength() { return 0; };
219 virtual void busReset(void);
220 virtual void EP0setupCallback(void);
221 virtual void EP0out(void);
222 virtual void EP0in(void);
223 virtual void connectStateChanged(unsigned int connected
);
224 virtual void suspendStateChanged(unsigned int suspended
);
225 uint8_t * findDescriptor(uint8_t descriptorType
);
226 CONTROL_TRANSFER
* getTransferPtr(void);
230 uint16_t PRODUCT_RELEASE
;
233 bool addRateFeedbackEndpoint(uint8_t endpoint
, uint32_t maxPacket
);
234 bool requestGetDescriptor(void);
235 bool controlOut(void);
236 bool controlIn(void);
237 bool requestSetAddress(void);
238 bool requestSetConfiguration(void);
239 bool requestSetFeature(void);
240 bool requestClearFeature(void);
241 bool requestGetStatus(void);
242 bool requestSetup(void);
243 bool controlSetup(void);
244 void decodeSetupPacket(uint8_t *data
, SETUP_PACKET
*packet
);
245 bool requestGetConfiguration(void);
246 bool requestGetInterface(void);
247 bool requestSetInterface(void);
249 CONTROL_TRANSFER transfer
;
252 uint16_t currentInterface
;
253 uint8_t currentAlternate
;