re-enabling serial
[clinton/Smoothieware.git] / gcc4mbed / samples / MSTest / USBDevice / USBDevice / USBDevice.h
1 /* USBDevice.h */
2 /* Generic USB device */
3 /* Copyright (c) 2011 ARM Limited. All rights reserved. */
4
5 #ifndef USBDEVICE_H
6 #define USBDEVICE_H
7
8 #include "mbed.h"
9 #include "USBDevice_Types.h"
10 #include "USBBusInterface.h"
11
12
13
14 class USBDevice: public USBHAL
15 {
16 public:
17 USBDevice(uint16_t vendor_id, uint16_t product_id, uint16_t product_release);
18
19 /*
20 * Check if the device is configured
21 *
22 * @returns true if configured, false otherwise
23 */
24 bool configured(void);
25
26 /*
27 * Connect a device
28 */
29 void connect(void);
30
31 /*
32 * Disconnect a device
33 */
34 void disconnect(void);
35
36 /*
37 * Add an endpoint
38 *
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
42 */
43 bool addEndpoint(uint8_t endpoint, uint32_t maxPacket);
44
45 /*
46 * Start a reading on a certain endpoint.
47 * You can access the result of the reading by USBDevice_read
48 *
49 * @param endpoint endpoint which will be read
50 * @param maxSize the maximum length that can be read
51 * @return true if successful
52 */
53 bool readStart(uint8_t endpoint, uint16_t maxSize);
54
55 /*
56 * Read a certain endpoint. Before calling this function, USBUSBDevice_readStart
57 * must be called.
58 *
59 * Warning: blocking
60 *
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
66 */
67 bool readEP(uint8_t endpoint, uint8_t * buffer, uint16_t * size, uint16_t maxSize);
68
69 /*
70 * Read a certain endpoint.
71 *
72 * Warning: non blocking
73 *
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
79 */
80 bool readEP_NB(uint8_t endpoint, uint8_t * buffer, uint16_t * size, uint16_t maxSize);
81
82 /*
83 * Write a certain endpoint.
84 *
85 * Warning: blocking
86 *
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
91 */
92 bool write(uint8_t endpoint, uint8_t * buffer, uint16_t size, uint16_t maxSize);
93
94
95 /*
96 * Write a certain endpoint.
97 *
98 * Warning: non blocking
99 *
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
104 */
105 bool writeNB(uint8_t endpoint, uint8_t * buffer, uint16_t size, uint16_t maxSize);
106
107
108 /*
109 * Called by USBDevice layer on bus reset. Warning: Called in ISR context
110 *
111 * May be used to reset state
112 */
113 virtual void USBCallback_busReset(void) {};
114
115 /*
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
119 *
120 * @returns true if class handles this request
121 */
122 virtual bool USBCallback_request() { return false; };
123
124 /*
125 * Called by USBDevice on Endpoint0 request completion
126 * if the 'notify' flag has been set to true. Warning: Called in ISR context
127 *
128 * In this case it is used to indicate that a HID report has
129 * been received from the host on endpoint 0
130 *
131 * @param buf buffer received on endpoint 0
132 * @param length length of this buffer
133 */
134 virtual void USBCallback_requestCompleted(uint8_t * buf, uint16_t length) {};
135
136 /*
137 * Called by USBDevice layer. Set configuration of the device.
138 * For instance, you can add all endpoints that you need on this function.
139 *
140 * @param configuration Number of the configuration
141 */
142 virtual bool USBCallback_setConfiguration(uint8_t configuration) { return false; };
143
144 /*
145 * Called by USBDevice layer. Set interface/alternate of the device.
146 *
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
150 */
151 virtual bool USBCallback_setInterface(uint16_t interface, uint8_t alternate) { return false; };
152
153 /*
154 * Get device descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
155 *
156 * @returns pointer to the device descriptor
157 */
158 virtual uint8_t * deviceDesc();
159
160 /*
161 * Get configuration descriptor
162 *
163 * @returns pointer to the configuration descriptor
164 */
165 virtual uint8_t * configurationDesc(){return NULL;};
166
167 /*
168 * Get string lang id descriptor
169 *
170 * @return pointer to the string lang id descriptor
171 */
172 virtual uint8_t * stringLangidDesc();
173
174 /*
175 * Get string manufacturer descriptor
176 *
177 * @returns pointer to the string manufacturer descriptor
178 */
179 virtual uint8_t * stringImanufacturerDesc();
180
181 /*
182 * Get string product descriptor
183 *
184 * @returns pointer to the string product descriptor
185 */
186 virtual uint8_t * stringIproductDesc();
187
188 /*
189 * Get string serial descriptor
190 *
191 * @returns pointer to the string serial descriptor
192 */
193 virtual uint8_t * stringIserialDesc();
194
195 /*
196 * Get string configuration descriptor
197 *
198 * @returns pointer to the string configuration descriptor
199 */
200 virtual uint8_t * stringIConfigurationDesc();
201
202 /*
203 * Get string interface descriptor
204 *
205 * @returns pointer to the string interface descriptor
206 */
207 virtual uint8_t * stringIinterfaceDesc();
208
209 /*
210 * Get the length of the report descriptor
211 *
212 * @returns length of the report descriptor
213 */
214 virtual uint16_t reportDescLength() { return 0; };
215
216
217
218 protected:
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);
227
228 uint16_t VENDOR_ID;
229 uint16_t PRODUCT_ID;
230 uint16_t PRODUCT_RELEASE;
231
232 private:
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);
248
249 CONTROL_TRANSFER transfer;
250 USB_DEVICE device;
251
252 uint16_t currentInterface;
253 uint8_t currentAlternate;
254 };
255
256
257 #endif