M480 BSP  V3.05.001
The Board Support Package for M480 Series
usbh_hid.h
Go to the documentation of this file.
1 /**************************************************************************/
9 #ifndef _USBH_HID_H_
10 #define _USBH_HID_H_
11 
12 #include "NuMicro.h"
13 #include "usb.h"
14 
15 #ifdef __cplusplus
16 extern "C"
17 {
18 #endif
19 
21 
22 #define ENABLE_ERR_MSG 1
23 #define ENABLE_DBG_MSG 0
24 
25 #if ENABLE_ERR_MSG
26 #define HID_ERRMSG printf
27 #else
28 #define HID_ERRMSG(...)
29 #endif
30 
31 #if ENABLE_DBG_MSG
32 #define HID_DBGMSG printf
33 #else
34 #define HID_DBGMSG(...)
35 #endif
36 
38 
39 
52 #define CONFIG_HID_MAX_DEV 4
53 #define CONFIG_HID_DEV_MAX_PIPE 8
55 
57 #define HID_DESCRIPTOR_TYPE 0x21
58 #define REPORT_DESCRIPTOR_TYPE 0x22
59 
60 #define HID_SUBCLASS_BOOT_DEVICE 0x01
62 #define HID_PROTOCOL_KEYBOARD 0x01
63 #define HID_PROTOCOL_MOUSE 0x02
65 /*-----------------------------------------------------------------------------------
66  * Short Item Tags
67  */
68 
69 /* Main item tag (tag & 0xFC) */
70 #define TAG_INPUT 0x80
71 #define TAG_OUTPUT 0x90
72 #define TAG_FEATURE 0xB0
73 #define TAG_COLLECTION 0xA0
74 #define TAG_END_COLLECTION 0xC0
75 
76 
77 /* Global item tag (tag & 0xFC) */
78 #define TAG_USAGE_PAGE 0x04
79 #define TAG_LOGICAL_MIN 0x14
80 #define TAG_LOGICAL_MAX 0x24
81 #define TAG_PHYSICAL_MIN 0x34
82 #define TAG_PHYSICAL_MAX 0x44
83 #define TAG_UNIT_EXPONENT 0x54
84 #define TAG_UNIT 0x64
85 #define TAG_REPORT_SIZE 0x74
86 #define TAG_REPORT_ID 0x84
87 #define TAG_REPORT_COUNT 0x94
88 #define TAG_PUSH 0xA4
89 #define TAG_POP 0xB4
90 
91 /* Local item tag (tag & 0xFC) */
92 #define TAG_USAGE 0x08
93 #define TAG_USAGE_MIN 0x18
94 #define TAG_USAGE_MAX 0x28
95 #define TAG_DESIGNATOR_INDEX 0x38
96 #define TAG_DESIGNATOR_MIN 0x48
97 #define TAG_DESIGNATOR_MAX 0x58
98 #define TAG_STRING_INDEX 0x78
99 #define TAG_STRING_MIN 0x88
100 #define TAG_STRING_MAX 0x98
101 #define TAG_DELIMITER 0xA8
102 
103 /* Collection */
104 #define COLLECT_PHYSICAL 0x00 /* group of axes */
105 #define COLLECT_APPLICATION 0x01 /* mouse, keyboard */
106 #define COLLECT_LOGICAL 0x02 /* interrelated data */
107 #define COLLECT_VENDOR 0xFF /* 0x80-0xFF Vendor-defined */
108 
109 /*-----------------------------------------------------------------------------------
110  * Usage Page
111  */
112 #define UP_GENERIC_DESKTOP 0x01
113 #define UP_SIMULATION_CONTROLS 0x02
114 #define UP_VR_CONTROLS 0x03
115 #define UP_SPORT_CONTROLS 0x04
116 #define UP_GAME_CONTROLS 0x05
117 #define UP_KEYCODE 0x07
118 #define UP_LEDS 0x08
119 #define UP_BUTTON 0x09
120 #define UP_ORDINAL 0x0A
121 #define UP_TELEPHONY 0x0B
122 #define UP_CONSUMER 0x0C
123 #define UP_DIGITIZER 0x0D
124 #define UP_PID_PAGE 0x0F
125 #define UP_UNICODE 0x10
126 #define UP_BARCODE_SCANNER 0x8C
127 
128 /* Usage ID of Generic Desktop Page */
129 #define USAGE_ID_POINTER 0x01
130 #define USAGE_ID_MOUSE 0x02
131 #define USAGE_ID_JOYSTICK 0x04
132 #define USAGE_ID_GAMEPAD 0x05
133 #define USAGE_ID_KEYBOARD 0x06
134 #define USAGE_ID_KEYPAD 0x07
135 #define USAGE_ID_X 0x30
136 #define USAGE_ID_Y 0x31
137 #define USAGE_ID_Z 0x32
138 #define USAGE_ID_WHEEL 0x38
139 
140 #define KEYCODE_CAPS_LOCK 0x39
141 #define KEYCODE_SCROLL_LOCK 0x47
142 #define KEYCODE_NUM_LOCK 0x53
143 
144 #define STATE_MASK_NUM_LOCK 0x01
145 #define STATE_MASK_CAPS_LOCK 0x02
146 #define STATE_MASK_SCROLL_LOCK 0x04
147 
149 
150 
152 #define USB_DT_HID (REQ_TYPE_CLASS_DEV | 0x01)
153 #define USB_DT_REPORT (REQ_TYPE_CLASS_DEV | 0x02)
154 
156 #define HID_REPORT_GET 0x01
157 #define HID_GET_IDLE 0x02
158 #define HID_GET_PROTOCOL 0x03
159 #define HID_REPORT_SET 0x09
160 #define HID_SET_IDLE 0x0A
161 #define HID_SET_PROTOCOL 0x0B
163 /* HID Report type */
164 #define RT_INPUT 1
165 #define RT_OUTPUT 2
166 #define RT_FEATURE 3
169  /* end of group USBH_EXPORTED_CONSTANTS */
170 
171 
176 /*
178  * HID Descriptor
179  */
180 #ifdef __ICCARM__
181 typedef struct hid_descriptor
182 {
183  __packed uint8_t bLength;
184  __packed uint8_t bDescriptorType;
185  __packed uint16_t bcdHID;
186  __packed uint8_t bCountryCode;
187  __packed uint8_t bNumDescriptors;
188  __packed uint8_t bRPDescType;
189  __packed uint16_t wDescriptorLength;
190 } DESC_HID_T;
191 #else
192 typedef struct __attribute__((__packed__)) hid_descriptor
193 {
194  uint8_t bLength;
195  uint8_t bDescriptorType;
196  uint16_t bcdHID;
197  uint8_t bCountryCode;
198  uint8_t bNumDescriptors;
199  uint8_t bRPDescType;
200  uint16_t wDescriptorLength;
201 } DESC_HID_T;
202 #endif
203 
204 typedef struct report_info
205 {
206  uint8_t type; /* TAG_INPUT, TAG_OUTPUT, or TAG_FEATURE */
207  uint8_t usage_page;
208  uint8_t app_usage;
209  uint8_t data_usage;
210  signed int usage_mim;
211  signed int usage_max;
212  signed int logical_min;
213  signed int logical_max;
214  signed int physical_min;
215  signed int physical_max;
216  signed int unit_exponent;
217  signed int unit;
218  signed int designator_index;
219  signed int designator_min;
220  signed int designator_max;
221  signed int string_index;
222  signed int string_min;
223  signed int string_max;
224  uint8_t report_id;
225  uint8_t report_size;
226  uint8_t report_count;
227  struct
228  {
229  uint32_t constant:1; /* {Data (0) | Constant (1)} */
230  uint32_t variable:1; /* {Array (0) | Variable (1)} */
231  uint32_t relative:1; /* {Absolute (0) | Relative (1)} */
232  uint32_t wrap:1; /* {No Wrap (0) | Wrap (1)} */
233  uint32_t non_linear:1; /* {Linear (0) | Non Linear (1)} */
234  uint32_t no_preferred:1; /* {Preferred State (0) | No Preferred (1)} */
235  uint32_t null_state:1; /* {No Null position (0) | Null state(1)} */
236  uint32_t is_volatile:1; /* {Non Volatile (0) | Volatile (1)} */
237  uint32_t buffered_bytes:1; /* {Bit Field (0) | Buffered Bytes (1)} */
238  uint32_t reserved:23; /* Reserved (0) */
239  } status;
240  struct report_info *next;
241 } RP_INFO_T;
242 
243 static uint8_t _designator_index, _designator_min, _designator_max;
244 static uint8_t _string_index, _string_max, _string_min;
245 
246 
247 typedef struct rp_desc_info
248 {
249  uint8_t has_report_id; /* If a Report ID tag is used anywhere in Report descriptor, all data reports for the device are preceded by a single byte ID field. */
250  uint8_t last_pressed_lock_keys; /* Recording the key-press state of last time (for keyboard) */
251  uint8_t lock_state;
252  char utr_led_idle; /* recording if the utr_led is in idle or not */
253  UTR_T *utr_led; /* UTR for LED control */
254  RP_INFO_T *report;
255 } RPD_T;
256 
258 
259 
260 /*---------------------------------------------------------------------------------------------*/
261 /* HID device */
262 /*---------------------------------------------------------------------------------------------*/
264 typedef struct usbhid_dev
265 {
266  uint16_t idVendor;
267  uint16_t idProduct;
268  uint8_t bSubClassCode;
269  uint8_t bProtocolCode;
273  UTR_T *out_utr_list;
274  void *iface;
275  uint32_t uid;
276  RPD_T rpd;
277  struct usbhid_dev *next;
278 } HID_DEV_T;
280 /*---------------------------------------------------------------------------------------------*/
281 /* HID keyboard event. */
282 /* HID driver delivers the keyboarde event to user application via callback function. */
283 /*---------------------------------------------------------------------------------------------*/
285 typedef struct usbhid_keyboard_event
286 {
287  uint8_t lock_state;
288  uint8_t modifier;
289  uint8_t keycode[6];
290  uint8_t key_cnt;
292 
293 /*---------------------------------------------------------------------------------------------*/
294 /* HID mouse event. */
295 /* HID driver delivers the mouse event to user application via callback function. */
296 /*---------------------------------------------------------------------------------------------*/
298 typedef struct usbhid_mouse_event
299 {
300  uint8_t axis_relative;
301  uint8_t wheel_relative;
302  uint8_t button_cnt;
303  uint32_t button_map;
304  signed int axis_min;
305  signed int axis_max;
306  signed int X;
307  signed int Y;
308  signed int wheel_min;
309  signed int wheel_max;
310  signed int wheel;
311  int X_bits;
312  int Y_bits;
314  uint32_t X_raw;
315  uint32_t Y_raw;
316  uint32_t wheel_raw;
317 } MOUSE_EVENT_T;
318 
319 typedef void (HID_MOUSE_FUNC)(struct usbhid_dev *hdev, MOUSE_EVENT_T *mouse);
320 typedef void (HID_KEYBOARD_FUNC)(struct usbhid_dev *hdev, KEYBOARD_EVENT_T *kbd);
322  /* end of group USBH_EXPORTED_STRUCTURES */
323 
324 
331 
333 int hid_parse_report_descriptor(HID_DEV_T *hdev, IFACE_T *iface);
334 int hid_parse_keyboard_reports(HID_DEV_T *hdev, uint8_t *data, int data_len);
335 int hid_parse_mouse_reports(HID_DEV_T *hdev, uint8_t *data, int data_len);
336 int32_t usbh_hid_set_report_non_blocking(HID_DEV_T *hdev, int rtp_typ, int rtp_id, uint8_t *data, int len);
338  /* end of group USBH_EXPORTED_FUNCTIONS */
340  /* end of group USBH_Library */
342  /* end of group Library */
344 
345 #ifdef __cplusplus
346 }
347 #endif
348 
349 #endif /* _USBH_HID_H_ */
350 
351 /*** (C) COPYRIGHT 2017 Nuvoton Technology Corp. ***/
352 
353 
354 
uint32_t uid
Definition: usbh_hid.h:275
uint16_t idVendor
Definition: usbh_hid.h:266
uint8_t bSubClassCode
Definition: usbh_hid.h:268
struct usbhid_mouse_event MOUSE_EVENT_T
uint8_t keycode[6]
Definition: usbh_hid.h:289
void() HID_KEYBOARD_FUNC(struct usbhid_dev *hdev, KEYBOARD_EVENT_T *kbd)
Definition: usbh_hid.h:320
uint8_t axis_relative
Definition: usbh_hid.h:300
void() HID_IR_FUNC(struct usbhid_dev *hdev, uint16_t ep_addr, int status, uint8_t *rdata, uint32_t data_len)
Definition: usbh_lib.h:123
NuMicro peripheral access layer header file.
void() HID_MOUSE_FUNC(struct usbhid_dev *hdev, MOUSE_EVENT_T *mouse)
Definition: usbh_hid.h:319
void * iface
Definition: usbh_hid.h:274
uint32_t X_raw
Definition: usbh_hid.h:314
void *__dso_handle __attribute__((weak))
Definition: _syscalls.c:35
signed int Y
Definition: usbh_hid.h:307
uint32_t Y_raw
Definition: usbh_hid.h:315
uint8_t button_cnt
Definition: usbh_hid.h:302
uint32_t wheel_raw
Definition: usbh_hid.h:316
void() HID_IW_FUNC(struct usbhid_dev *hdev, uint16_t ep_addr, int status, uint8_t *wbuff, uint32_t *data_len)
Definition: usbh_lib.h:124
signed int axis_max
Definition: usbh_hid.h:305
struct usbhid_dev * next
Definition: usbh_hid.h:277
uint16_t idProduct
Definition: usbh_hid.h:267
uint8_t wheel_relative
Definition: usbh_hid.h:301
signed int X
Definition: usbh_hid.h:306
UTR_T * out_utr_list
Definition: usbh_hid.h:273
HID_IR_FUNC * read_func
Definition: usbh_hid.h:270
signed int axis_min
Definition: usbh_hid.h:304
void usbh_hid_regitser_keyboard_callback(HID_KEYBOARD_FUNC *func)
Register the keyboard event callback function to HID class driver. Any keyboard reports will be sent ...
Definition: hid_core.c:755
uint8_t bProtocolCode
Definition: usbh_hid.h:269
HID_IW_FUNC * write_func
Definition: usbh_hid.h:271
void usbh_hid_regitser_mouse_callback(HID_MOUSE_FUNC *func)
Register the mouse event callback function to HID class driver. Any mouse reports will be sent to use...
Definition: hid_core.c:743
USB Host library header file.
struct usbhid_keyboard_event KEYBOARD_EVENT_T
#define CONFIG_HID_DEV_MAX_PIPE
Definition: usbh_hid.h:53
signed int wheel
Definition: usbh_hid.h:310
RPD_T rpd
Definition: usbh_hid.h:276
uint32_t button_map
Definition: usbh_hid.h:303
signed int wheel_min
Definition: usbh_hid.h:308
HIDDEN_SYMBOLS struct usbhid_dev HID_DEV_T
UTR_T * utr_list[CONFIG_HID_DEV_MAX_PIPE]
Definition: usbh_hid.h:272
signed int wheel_max
Definition: usbh_hid.h:309