Skip to content

Commit 853d3c4

Browse files
c1728p90xc0170
authored andcommitted
Update the USBHID classes
Update the USB classes USBHID, USBKeyboard, USBMouse and USBMouseKeyboard from the unsupported folder.
1 parent d3e11af commit 853d3c4

File tree

9 files changed

+3348
-0
lines changed

9 files changed

+3348
-0
lines changed

usb/device/USBHID/USBHID.cpp

Lines changed: 511 additions & 0 deletions
Large diffs are not rendered by default.

usb/device/USBHID/USBHID.h

Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2018-2018 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef USB_HID_H
18+
#define USB_HID_H
19+
20+
/* These headers are included for child class. */
21+
#include "USBDescriptor.h"
22+
#include "USBDevice.h"
23+
24+
#include "USBHID_Types.h"
25+
#include "AsyncOp.h"
26+
#include "LinkedList.h"
27+
28+
29+
30+
/**
31+
* USBHID example
32+
* @code
33+
* #include "mbed.h"
34+
* #include "USBHID.h"
35+
*
36+
* USBHID hid;
37+
* HID_REPORT recv;
38+
* BusOut leds(LED1,LED2,LED3,LED4);
39+
*
40+
* int main(void) {
41+
* while (1) {
42+
* hid.read(&recv);
43+
* leds = recv.data[0];
44+
* }
45+
* }
46+
* @endcode
47+
*/
48+
49+
class USBHID: public USBDevice {
50+
public:
51+
52+
/**
53+
* Basic constructor
54+
*
55+
* Construct this object optionally connecting and blocking until it is ready.
56+
*
57+
* @note Do not use this constructor in derived classes.
58+
*
59+
* @param connect_blocking true to perform a blocking connect, false to start in a disconnected state
60+
* @param output_report_length Maximum length of a sent report (up to 64 bytes)
61+
* @param input_report_length Maximum length of a received report (up to 64 bytes)
62+
* @param vendor_id Your vendor_id
63+
* @param product_id Your product_id
64+
* @param product_release Your product_release
65+
*/
66+
USBHID(bool connect_blocking = true, uint8_t output_report_length = 64, uint8_t input_report_length = 64, uint16_t vendor_id = 0x1234, uint16_t product_id = 0x0006, uint16_t product_release = 0x0001);
67+
68+
/**
69+
* Fully featured constructor
70+
*
71+
* Construct this object with the supplied USBPhy and parameters. The user
72+
* this object is responsible for calling connect() or init().
73+
*
74+
* @note Derived classes must use this constructor and call init() or
75+
* connect() themselves. Derived classes should also call deinit() in
76+
* their destructor. This ensures that no interrupts can occur when the
77+
* object is partially constructed or destroyed.
78+
*
79+
* @param phy USB phy to use
80+
* @param output_report_length Maximum length of a sent report (up to 64 bytes)
81+
* @param input_report_length Maximum length of a received report (up to 64 bytes)
82+
* @param vendor_id Your vendor_id
83+
* @param product_id Your product_id
84+
* @param product_release Your product_release
85+
*/
86+
USBHID(USBPhy *phy, uint8_t output_report_length, uint8_t input_report_length, uint16_t vendor_id, uint16_t product_id, uint16_t product_release);
87+
88+
/**
89+
* Destroy this object
90+
*
91+
* Any classes which inherit from this class must call deinit
92+
* before this destructor runs.
93+
*/
94+
virtual ~USBHID();
95+
96+
/**
97+
* Check if this class is ready
98+
*
99+
* @return true if the device is in the configured state
100+
*/
101+
bool ready();
102+
103+
/**
104+
* Block until this HID device is in the configured state
105+
*/
106+
void wait_ready();
107+
108+
/**
109+
* Send a Report. warning: blocking
110+
*
111+
* @param report Report which will be sent (a report is defined by all data and the length)
112+
* @returns true if successful
113+
*/
114+
bool send(const HID_REPORT *report);
115+
116+
117+
/**
118+
* Send a Report. warning: non blocking
119+
*
120+
* @param report Report which will be sent (a report is defined by all data and the length)
121+
* @returns true if successful
122+
*/
123+
bool send_nb(const HID_REPORT *report);
124+
125+
/**
126+
* Read a report: blocking
127+
*
128+
* @param report pointer to the report to fill
129+
* @returns true if successful
130+
*/
131+
bool read(HID_REPORT *report);
132+
133+
/**
134+
* Read a report: non blocking
135+
*
136+
* @param report pointer to the report to fill
137+
* @returns true if successful
138+
*/
139+
bool read_nb(HID_REPORT *report);
140+
141+
protected:
142+
uint16_t reportLength;
143+
uint8_t reportDescriptor[27];
144+
145+
/*
146+
* Get the Report descriptor
147+
*
148+
* @returns pointer to the report descriptor
149+
*/
150+
virtual const uint8_t *report_desc();
151+
152+
/*
153+
* Get the length of the report descriptor
154+
*
155+
* @returns the length of the report descriptor
156+
*/
157+
virtual uint16_t report_desc_length();
158+
159+
/*
160+
* Get string product descriptor
161+
*
162+
* @returns pointer to the string product descriptor
163+
*/
164+
virtual const uint8_t *string_iproduct_desc();
165+
166+
/*
167+
* Get string interface descriptor
168+
*
169+
* @returns pointer to the string interface descriptor
170+
*/
171+
virtual const uint8_t *string_iinterface_desc();
172+
173+
/*
174+
* Get configuration descriptor
175+
*
176+
* @returns pointer to the configuration descriptor
177+
*/
178+
virtual const uint8_t *configuration_desc(uint8_t index);
179+
180+
181+
/*
182+
* HID Report received by SET_REPORT request. Warning: Called in ISR context
183+
* First byte of data will be the report ID
184+
*
185+
* @param report Data and length received
186+
*/
187+
virtual void HID_callbackSetReport(HID_REPORT *report) {};
188+
189+
/**
190+
* Called when USB changes state
191+
*
192+
* @param new_state The new state of the USBDevice
193+
*
194+
* Warning: Called in ISR context
195+
*/
196+
virtual void callback_state_change(DeviceState new_state);
197+
198+
/*
199+
* This is used to handle extensions to standard requests
200+
* and class specific requests
201+
*/
202+
virtual void callback_request(const setup_packet_t *setup);
203+
204+
/*
205+
* This is used to handle extensions to standard requests
206+
* and class specific requests with a data phase
207+
*/
208+
virtual void callback_request_xfer_done(const setup_packet_t *setup, bool aborted);
209+
210+
211+
/*
212+
* Called by USBDevice layer. Set configuration of the device.
213+
* For instance, you can add all endpoints that you need on this function.
214+
*
215+
* @param configuration Number of the configuration
216+
* @returns true if class handles this request
217+
*/
218+
virtual void callback_set_configuration(uint8_t configuration);
219+
220+
/*
221+
* Called by USBDevice layer in response to set_interface.
222+
*
223+
* Upon reception of this command endpoints of any previous interface
224+
* if any must be removed with endpoint_remove and new endpoint added with
225+
* endpoint_add.
226+
*
227+
* @param configuration Number of the configuration
228+
*
229+
* Warning: Called in ISR context
230+
*/
231+
virtual void callback_set_interface(uint16_t interface, uint8_t alternate);
232+
233+
/*
234+
* Called when there is a hid report that can be read
235+
*/
236+
virtual void report_rx() {}
237+
238+
/*
239+
* Called when there is space to send a hid report
240+
*/
241+
virtual void report_tx() {}
242+
243+
protected:
244+
usb_ep_t _int_in;
245+
usb_ep_t _int_out;
246+
247+
private:
248+
void _init(uint8_t output_report_length, uint8_t input_report_length);
249+
void _send_isr(usb_ep_t endpoint);
250+
void _read_isr(usb_ep_t endpoint);
251+
252+
void _connect_wake_all();
253+
void _send_abort_all();
254+
void _read_abort_all();
255+
256+
class AsyncSend;
257+
class AsyncRead;
258+
259+
LinkedList<AsyncOp> _connect_list;
260+
LinkedList<AsyncSend> _send_list;
261+
bool _send_idle;
262+
LinkedList<AsyncRead> _read_list;
263+
bool _read_idle;
264+
265+
uint8_t _configuration_descriptor[41];
266+
HID_REPORT _input_report;
267+
HID_REPORT _output_report;
268+
uint8_t _output_length;
269+
uint8_t _input_length;
270+
271+
272+
};
273+
274+
#endif

usb/device/USBHID/USBHID_Types.h

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2018-2018 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef USBCLASS_HID_TYPES
18+
#define USBCLASS_HID_TYPES
19+
20+
#include <stdint.h>
21+
22+
/* */
23+
#define HID_VERSION_1_11 (0x0111)
24+
25+
/* HID Class */
26+
#define HID_CLASS (3)
27+
#define HID_SUBCLASS_NONE (0)
28+
#define HID_SUBCLASS_BOOT (1)
29+
#define HID_PROTOCOL_NONE (0)
30+
#define HID_PROTOCOL_KEYBOARD (1)
31+
#define HID_PROTOCOL_MOUSE (2)
32+
33+
/* Descriptors */
34+
#define HID_DESCRIPTOR (33)
35+
#define HID_DESCRIPTOR_LENGTH (0x09)
36+
#define REPORT_DESCRIPTOR (34)
37+
38+
/* Class requests */
39+
#define GET_REPORT (0x1)
40+
#define GET_IDLE (0x2)
41+
#define SET_REPORT (0x9)
42+
#define SET_IDLE (0xa)
43+
44+
/* HID Class Report Descriptor */
45+
/* Short items: size is 0, 1, 2 or 3 specifying 0, 1, 2 or 4 (four) bytes */
46+
/* of data as per HID Class standard */
47+
48+
/* Main items */
49+
#define INPUT(size) (0x80 | size)
50+
#define OUTPUT(size) (0x90 | size)
51+
#define FEATURE(size) (0xb0 | size)
52+
#define COLLECTION(size) (0xa0 | size)
53+
#define END_COLLECTION(size) (0xc0 | size)
54+
55+
/* Global items */
56+
#define USAGE_PAGE(size) (0x04 | size)
57+
#define LOGICAL_MINIMUM(size) (0x14 | size)
58+
#define LOGICAL_MAXIMUM(size) (0x24 | size)
59+
#define PHYSICAL_MINIMUM(size) (0x34 | size)
60+
#define PHYSICAL_MAXIMUM(size) (0x44 | size)
61+
#define UNIT_EXPONENT(size) (0x54 | size)
62+
#define UNIT(size) (0x64 | size)
63+
#define REPORT_SIZE(size) (0x74 | size)
64+
#define REPORT_ID(size) (0x84 | size)
65+
#define REPORT_COUNT(size) (0x94 | size)
66+
#define PUSH(size) (0xa4 | size)
67+
#define POP(size) (0xb4 | size)
68+
69+
/* Local items */
70+
#define USAGE(size) (0x08 | size)
71+
#define USAGE_MINIMUM(size) (0x18 | size)
72+
#define USAGE_MAXIMUM(size) (0x28 | size)
73+
#define DESIGNATOR_INDEX(size) (0x38 | size)
74+
#define DESIGNATOR_MINIMUM(size) (0x48 | size)
75+
#define DESIGNATOR_MAXIMUM(size) (0x58 | size)
76+
#define STRING_INDEX(size) (0x78 | size)
77+
#define STRING_MINIMUM(size) (0x88 | size)
78+
#define STRING_MAXIMUM(size) (0x98 | size)
79+
#define DELIMITER(size) (0xa8 | size)
80+
81+
/* HID Report */
82+
/* Where report IDs are used the first byte of 'data' will be the */
83+
/* report ID and 'length' will include this report ID byte. */
84+
85+
#define MAX_HID_REPORT_SIZE (64)
86+
87+
typedef struct {
88+
uint32_t length;
89+
uint8_t data[MAX_HID_REPORT_SIZE];
90+
} HID_REPORT;
91+
92+
#endif

0 commit comments

Comments
 (0)