-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo_device_hid_keyboard_rtos.c
More file actions
669 lines (559 loc) · 28.7 KB
/
demo_device_hid_keyboard_rtos.c
File metadata and controls
669 lines (559 loc) · 28.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
/***************************************************************************
* Copyright (c) 2025-present Eclipse ThreadX Contributors
*
* This program and the accompanying materials are made available under the
* terms of the MIT License which is available at
* https://opensource.org/licenses/MIT.
*
*
* SPDX-License-Identifier: MIT
**************************************************************************/
/**************************************************************************/
/**************************************************************************/
/** */
/** Overview */
/** */
/** This example works as a USB HID device. It will appear as a USB */
/** keyboard device on PC. This application demo is running in rtos */
/** mode. */
/** */
/** Note */
/** */
/** This demonstration is not optimized, to optimize application user */
/** should configure related class flag in ux_user.h and adjust */
/** UX_DEVICE_MEMORY_STACK_SIZE */
/** */
/** */
/** AUTHOR */
/** */
/** Mohamed AYED */
/** */
/**************************************************************************/
/**************************************************************************/
#include "ux_api.h"
#include "ux_device_class_hid.h"
#ifndef UX_DEVICE_SIDE_ONLY
#error UX_DEVICE_SIDE_ONLY must be defined
#endif
#if (UX_DEVICE_CLASS_HID_EVENT_BUFFER_LENGTH < 8)
#error HID keyboard event buffer length must be more then 8.
#endif
/* Defined the keyboard will act as boot device. */
/* define DEMO_HID_BOOT_DEVICE */
/**************************************************/
/** Define constants */
/**************************************************/
#define UX_DEVICE_MEMORY_STACK_SIZE (7*1024)
#define UX_DEMO_THREAD_STACK_SIZE (1*1024)
#ifdef DEMO_HID_BOOT_DEVICE
#define UX_DEMO_HID_SUBCLASS 0x01
#else /* DEMO_HID_BOOT_DEVICE */
#define UX_DEMO_HID_SUBCLASS 0x00
#endif
#define UX_HID_NUM_LOCK_MASK 0x01
#define UX_HID_CAPS_LOCK_MASK 0x02
/**************************************************/
/** Demo descriptor define constants */
/**************************************************/
#define UX_DEMO_DEVICE_VID 0x090A
#define UX_DEMO_DEVICE_PID 0x4036
#define UX_DEMO_MAX_EP0_FS_SIZE 0x08U
#define UX_DEMO_MAX_EP0_HS_SIZE 0x40U
#define UX_DEMO_HID_CONFIG_DESC_SIZE 0x22U
#define UX_DEMO_BCD_USB 0x0200
#define UX_DEMO_BCD_HID 0x0110
#define UX_DEMO_HID_ENDPOINT_ADDRESS 0x81
#define UX_DEMO_HID_ENDPOINT_FS_SIZE 0x08
#define UX_DEMO_HID_ENDPOINT_FS_BINTERVAL 0x08
#define UX_DEMO_HID_ENDPOINT_HS_SIZE 0x08
#define UX_DEMO_HID_ENDPOINT_HS_BINTERVAL 0x08
/************************************************************/
/** Demo device class demo callbacks function prototypes */
/************************************************************/
VOID ux_demo_device_hid_instance_activate(VOID *hid_instance);
VOID ux_demo_device_hid_instance_deactivate(VOID *hid_instance);
UINT ux_demo_device_hid_callback(UX_SLAVE_CLASS_HID *hid_instance, UX_SLAVE_CLASS_HID_EVENT *hid_event);
UINT ux_demo_device_hid_get_callback(UX_SLAVE_CLASS_HID *hid_instance, UX_SLAVE_CLASS_HID_EVENT *hid_event);
#ifndef DEMO_TEST
/************************************************************/
/** usbx application initialization with RTOS */
/************************************************************/
VOID tx_application_define(VOID *first_unused_memory);
#endif /* DEMO_TEST */
/************************************************************/
/** usbx device hid keyboard instance */
/************************************************************/
UX_SLAVE_CLASS_HID *hid_keyboard;
/************************************************************/
/** Thread object */
/************************************************************/
static UX_THREAD ux_hid_thread;
static ULONG ux_hid_thread_stack[UX_DEMO_THREAD_STACK_SIZE / sizeof(ULONG)];
static VOID ux_demo_device_hid_thread_entry(ULONG thread_input);
/************************************************************/
/** usbx demo callback prototype */
/************************************************************/
static VOID ux_demo_error_callback(UINT system_level, UINT system_context, UINT error_code);
/************************************************************/
/** Demo function prototypes */
/************************************************************/
UINT ux_demo_device_hid_init(VOID);
UINT ux_demo_device_hid_uninit(VOID);
UINT ux_demo_device_hid_keyboard_send_character(UX_SLAVE_CLASS_HID *device_hid);
/************************************************************/
/** Demo variables */
/************************************************************/
static CHAR ux_system_memory_pool[UX_DEVICE_MEMORY_STACK_SIZE];
ULONG num_lock_flag = UX_FALSE;
ULONG caps_lock_flag = UX_FALSE;
/************************************************************/
/** usbx demo extern function prototypes */
/************************************************************/
#ifndef EXTERNAL_MAIN
extern int board_setup(void);
#endif /* EXTERNAL_MAIN */
#ifndef EXTERNAL_DCD_INITIALIZE
extern int usb_device_dcd_initialize(void *param);
#endif /* EXTERNAL_DCD_INITIALIZE */
/************************************************************/
/** HID Report descriptor */
/************************************************************/
#define UX_HID_KEYBOARD_REPORT_LENGTH (sizeof(hid_keyboard_report)/sizeof(hid_keyboard_report[0]))
UCHAR hid_keyboard_report[] = {
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x06, // USAGE (Keyboard)
0xa1, 0x01, // COLLECTION (Application)
/* Modifier Keys (Shift, Ctrl, Alt, GUI) */
0x05, 0x07, // USAGE_PAGE (Key Codes)
0x19, 0xE0, // USAGE_MINIMUM (Left Control)
0x29, 0xE7, // USAGE_MAXIMUM (Right GUI)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x75, 0x01, // REPORT_SIZE (1)
0x95, 0x08, // REPORT_COUNT (8)
0x81, 0x02, // INPUT (Data, Variable, Absolute)
/* Reserved byte */
0x75, 0x08, // REPORT_SIZE (8)
0x95, 0x01, // REPORT_COUNT (1)
0x81, 0x01, // INPUT (Constant)
/* Key Array (6-Key Rollover) */
0x05, 0x07, // USAGE_PAGE (Key Codes)
0x19, 0x00, // USAGE_MINIMUM (0)
0x29, 0x65, // USAGE_MAXIMUM (101)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x65, // LOGICAL_MAXIMUM (101)
0x75, 0x08, // REPORT_SIZE (8)
0x95, 0x06, // REPORT_COUNT (6)
0x81, 0x00, // INPUT (Data, Array) - Key array
/* LED Output (Caps Lock, Num Lock, etc.) */
0x05, 0x08, // Usage Page (LEDs)
0x19, 0x01, // Usage Minimum (Num Lock)
0x29, 0x05, // Usage Maximum (Kana)
0x15, 0x00, // Logical Minimum (0)
0x25, 0x01, // Logical Maximum (1)
0x75, 0x01, // Report Size (1)
0x95, 0x05, // Report Count (5)
0x91, 0x02, // OUTPUT (Data, Variable, Absolute)
/* Padding to make LED section byte-aligned */
0x75, 0x03, // Report Size (3)
0x95, 0x01, // Report Count (1)
0x91, 0x01, // OUTPUT (Constant, Array, Absolute)
0xc0 // End Collection
};
/************************************************************/
/** USB descriptors */
/** - framework full speed */
/** - framework high speed */
/** - framework string */
/** - framework language id */
/************************************************************/
#define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED_LENGTH sizeof(ux_demo_device_framework_full_speed)
#define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED_LENGTH sizeof(ux_demo_device_framework_high_speed)
#define DEVICE_FRAMEWORK_STRING_LENGTH sizeof(ux_demo_device_framework_string)
#define DEVICE_FRAMEWORK_LANGUAGE_ID_LENGTH sizeof(ux_demo_device_framework_language_id)
UCHAR ux_demo_device_framework_full_speed[] = {
/* Device descriptor */
0x12, /* bLength */
0x01, /* bDescriptorType */
UX_W0(UX_DEMO_BCD_USB), UX_W1(UX_DEMO_BCD_USB), /* bcdUSB */
0x00, /* bDeviceClass : 0x00 : Interface-defined */
0x00, /* bDeviceSubClass : 0x00 : Reset */
0x00, /* bDeviceProtocol : 0x00 : Reset */
UX_DEMO_MAX_EP0_FS_SIZE, /* bMaxPacketSize0 */
UX_W0(UX_DEMO_DEVICE_VID), UX_W1(UX_DEMO_DEVICE_VID), /* idVendor */
UX_W0(UX_DEMO_DEVICE_PID), UX_W1(UX_DEMO_DEVICE_PID), /* idProduct */
UX_W0(0x200), UX_W1(0x200), /* bcdDevice */
0x01, /* iManufacturer */
0x02, /* iProduct */
0x03, /* iSerialNumber */
0x01, /* bNumConfigurations */
/* Configuration Descriptor, total 34 */
0x09, /* bLength */
0x02, /* bDescriptorType */
UX_W0(UX_DEMO_HID_CONFIG_DESC_SIZE), /* wTotalLength */
UX_W1(UX_DEMO_HID_CONFIG_DESC_SIZE),
0x01, /* bNumInterfaces */
0x01, /* bConfigurationValue */
0x04, /* iConfiguration */
0xC0, /* bmAttributes */
/* D6 : 0x1 : Self-powered */
/* D5, Remote Wakeup : 0x0 : Not supported */
0x32, /* bMaxPower : 50 : 100mA */
/* Interface descriptor */
0x09, /* bLength */
0x04, /* bDescriptorType */
0x00, /* bInterfaceNumber */
0x00, /* bAlternateSetting */
0x01, /* bNumEndpoints */
0x03, /* bInterfaceClass : 0x03 : HID */
UX_DEMO_HID_SUBCLASS, /* bInterfaceSubClass : Boot/non-boot Subclass */
0x01, /* bInterfaceProtocol : 0x00 : Undefined */
0x06, /* iInterface */
/* HID Descriptor */
0x09, /* bLength : 9 */
0x21, /* bDescriptorType : 0x21 : HID descriptor */
0x10, 0x01, /* bcdHID : 0x0110 */
0x21, /* bCountryCode : 33 : US */
0x01, /* bNumDescriptors */
0x22, /* bReportDescriptorType1 : 0x22 : Report descriptor */
UX_W0(UX_HID_KEYBOARD_REPORT_LENGTH), /* wDescriptorLength1 */
UX_W1(UX_HID_KEYBOARD_REPORT_LENGTH),
/* Endpoint Descriptor */
0x07, /* bLength */
0x05, /* bDescriptorType */
UX_DEMO_HID_ENDPOINT_ADDRESS, /* bEndpointAddress */
/* D7, Direction : 0x01 */
/* D3..0, Endpoint number */
0x03, /* bmAttributes */
/* D1..0, Transfer Type : 0x3 : Interrupt */
/* D3..2, Synchronization Type : 0x0 : No Synchronization */
/* D5..4, Usage Type : 0x0 : Data endpoint */
UX_W0(UX_DEMO_HID_ENDPOINT_FS_SIZE), /* wMaxPacketSize */
UX_W1(UX_DEMO_HID_ENDPOINT_FS_SIZE), /* D10..0, Max Packet Size */
/* D12..11, Additional transactions : 0x00 */
UX_DEMO_HID_ENDPOINT_FS_BINTERVAL, /* bInterval : 8ms / x128 (FS 128ms/HS 16ms) */
};
UCHAR ux_demo_device_framework_high_speed[] = {
/* Device descriptor */
0x12, /* bLength */
0x01, /* bDescriptorType */
UX_W0(UX_DEMO_BCD_USB), UX_W1(UX_DEMO_BCD_USB), /* bcdUSB */
0x00, /* bDeviceClass : 0x00 : Interface-defined */
0x00, /* bDeviceSubClass : 0x00 : Reset */
0x00, /* bDeviceProtocol : 0x00 : Reset */
UX_DEMO_MAX_EP0_HS_SIZE, /* bMaxPacketSize0 */
UX_W0(UX_DEMO_DEVICE_VID), UX_W1(UX_DEMO_DEVICE_VID), /* idVendor */
UX_W0(UX_DEMO_DEVICE_PID), UX_W1(UX_DEMO_DEVICE_PID), /* idProduct */
UX_W0(0x200), UX_W1(0x200), /* bcdDevice */
0x01, /* iManufacturer */
0x02, /* iProduct */
0x03, /* iSerialNumber */
0x01, /* bNumConfigurations */
/* Device qualifier descriptor */
0x0A, /* bLength */
0x06, /* bDescriptorType */
UX_W0(UX_DEMO_BCD_USB), UX_W1(UX_DEMO_BCD_USB), /* bcdUSB */
0x00, /* bDeviceClass : 0x00 : Interface-defined */
0x00, /* bDeviceSubClass : 0x00 : Reset */
0x00, /* bDeviceProtocol : 0x00 : Reset */
UX_DEMO_MAX_EP0_HS_SIZE, /* bMaxPacketSize0 */
0x01, /* bNumConfigurations */
0x00, /* bReserved */
/* Configuration descriptor */
0x09, /* bLength */
0x02, /* bDescriptorType */
UX_W0(UX_DEMO_HID_CONFIG_DESC_SIZE), /* wTotalLength */
UX_W1(UX_DEMO_HID_CONFIG_DESC_SIZE),
0x01, /* bNumInterfaces */
0x01, /* bConfigurationValue */
0x05, /* iConfiguration */
0xC0, /* bmAttributes */
/* D6 : 0x1 : Self-powered */
/* D5, Remote Wakeup : 0x0 : Not supported */
0x19, /* bMaxPower : 50 : 100mA */
/* Interface descriptor */
0x09, /* bLength */
0x04, /* bDescriptorType */
0x00, /* bInterfaceNumber */
0x00, /* bAlternateSetting */
0x01, /* bNumEndpoints */
0x03, /* bInterfaceClass : 0x03 : HID */
UX_DEMO_HID_SUBCLASS, /* bInterfaceSubClass : Boot/non-boot Subclass */
0x01, /* bInterfaceProtocol : 0x00 : Undefined */
0x06, /* iInterface */
/* HID Descriptor */
0x09, /* bLength : 9 */
0x21, /* bDescriptorType : 0x21 : HID descriptor */
UX_W0(UX_DEMO_BCD_HID), UX_W1(UX_DEMO_BCD_HID), /* bcdHID : 0x0110 */
0x21, /* bCountryCode : 33 : US */
0x01, /* bNumDescriptors */
0x22, /* bReportDescriptorType1 : 0x22 : Report descriptor */
UX_W0(UX_HID_KEYBOARD_REPORT_LENGTH), /* wDescriptorLength1 */
UX_W1(UX_HID_KEYBOARD_REPORT_LENGTH),
/* Endpoint Descriptor (Interrupt In) */
0x07, /* bLength */
0x05, /* bDescriptorType */
UX_DEMO_HID_ENDPOINT_ADDRESS, /* bEndpointAddress */
/* D7, Direction : 0x01 */
/* D3..0, Endpoint number */
0x03, /* bmAttributes */
/* D1..0, Transfer Type : 0x3 : Interrupt */
/* D3..2, Synchronization Type : 0x0 : No Synchronization */
/* D5..4, Usage Type : 0x0 : Data endpoint */
UX_W0(UX_DEMO_HID_ENDPOINT_HS_SIZE), /* wMaxPacketSize */
UX_W1(UX_DEMO_HID_ENDPOINT_HS_SIZE), /* D10..0, Max Packet Size */
/* D12..11, Additional transactions */
UX_DEMO_HID_ENDPOINT_HS_BINTERVAL, /* bInterval : 8ms / x128 (FS 128ms/HS 16ms) */
};
/* String Device Framework :
Byte 0 and 1 : Word containing the language ID : 0x0904 for US
Byte 2 : Byte containing the index of the descriptor
Byte 3 : Byte containing the length of the descriptor string
*/
UCHAR ux_demo_device_framework_string[] = {
/* iManufacturer string descriptor : Index 1 */
0x09, 0x04, 0x01, 12,
'U', 'S', 'B', 'X', ' ', 'e', 'c', 'l', 'i', 'p', 's', 'e',
/* iProduct string descriptor : Index 2 */
0x09, 0x04, 0x02, 17,
'H', 'I', 'D', ' ', 'K', 'e', 'y', 'b', 'o', 'a', 'r', 'd', ' ', 'D', 'e', 'm', 'o',
/* iSerialNumber Number string descriptor : Index 3 */
0x09, 0x04, 0x03, 13,
'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1',
/* iConfiguration string descriptor : Index 4 */
0x09, 0x04, 0x04, 10,
'F', 'U', 'L', 'L', ' ', 'S', 'P', 'E', 'E', 'D',
/* iConfiguration string descriptor : Index 5 */
0x09, 0x04, 0x05, 10,
'H', 'I', 'G', 'H', ' ', 'S', 'P', 'E', 'E', 'D',
/* iInterface string descriptor : Index 6 */
0x09, 0x04, 0x06, 8,
'K', 'e', 'y', 'b', 'o', 'a', 'r', 'd'
};
/* Multiple languages are supported on the device, to add a language besides english,
the unicode language code must be appended to the ux_demo_device_framework_language_id
array and the length adjusted accordingly.
*/
UCHAR ux_demo_device_framework_language_id[] = {
/* English. */
0x09, 0x04
};
#ifndef EXTERNAL_MAIN
int main(void)
{
/* Initialize the board. */
board_setup();
/* Enter the ThreadX kernel. */
tx_kernel_enter();
}
#endif /* EXTERNAL_MAIN */
#ifndef DEMO_TEST
VOID tx_application_define(VOID *first_unused_memory)
{
UX_PARAMETER_NOT_USED(first_unused_memory);
ux_demo_device_hid_init();
}
#endif /* DEMO_TEST */
/********************************************************************/
/** ux_demo_device_hid_init */
/********************************************************************/
UINT ux_demo_device_hid_init(VOID)
{
CHAR *memory_pointer;
UINT status;
UX_SLAVE_CLASS_HID_PARAMETER hid_keyboard_parameter;
/* Use static memory block. */
memory_pointer = ux_system_memory_pool;
/* Initialize USBX Memory. */
status = ux_system_initialize(memory_pointer, UX_DEVICE_MEMORY_STACK_SIZE, UX_NULL, 0);
if(status != UX_SUCCESS)
return status;
/* Install the device portion of USBX. */
status = ux_device_stack_initialize(ux_demo_device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED_LENGTH,
ux_demo_device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED_LENGTH,
ux_demo_device_framework_string, DEVICE_FRAMEWORK_STRING_LENGTH,
ux_demo_device_framework_language_id, DEVICE_FRAMEWORK_LANGUAGE_ID_LENGTH,
UX_NULL);
if(status != UX_SUCCESS)
return status;
/* Initialize the hid keyboard class parameters for the device. */
hid_keyboard_parameter.ux_slave_class_hid_instance_activate = ux_demo_device_hid_instance_activate;
hid_keyboard_parameter.ux_slave_class_hid_instance_deactivate = ux_demo_device_hid_instance_deactivate;
hid_keyboard_parameter.ux_device_class_hid_parameter_report_address = hid_keyboard_report;
hid_keyboard_parameter.ux_device_class_hid_parameter_report_length = UX_HID_KEYBOARD_REPORT_LENGTH;
hid_keyboard_parameter.ux_device_class_hid_parameter_report_id = UX_FALSE;
hid_keyboard_parameter.ux_device_class_hid_parameter_callback = ux_demo_device_hid_callback;
hid_keyboard_parameter.ux_device_class_hid_parameter_get_callback = ux_demo_device_hid_get_callback;
/* Initialize the device hid class. The class is connected with interface 0 on configuration 1. */
status = ux_device_stack_class_register(_ux_system_slave_class_hid_name, ux_device_class_hid_entry,
1, 0, (VOID *)&hid_keyboard_parameter);
if(status != UX_SUCCESS)
return status;
/* Create the main demo thread. */
status = ux_utility_thread_create(&ux_hid_thread, "hid_usbx_app_thread_entry",
ux_demo_device_hid_thread_entry, 0, ux_hid_thread_stack,
UX_DEMO_THREAD_STACK_SIZE, 20, 20, 1, UX_AUTO_START);
if(status != UX_SUCCESS)
return status;
/* Register error callback. */
ux_utility_error_callback_register(ux_demo_error_callback);
return status;
}
/********************************************************************/
/** ux_demo_device_hid_uninit */
/********************************************************************/
UINT ux_demo_device_hid_uninit(VOID)
{
UINT status;
/* Uninitialize USBX Memory. */
status = ux_device_stack_uninitialize();
if(status != UX_SUCCESS)
return status;
/* Uninitialize the device hid class. */
status = ux_device_stack_class_unregister(_ux_system_slave_class_hid_name, ux_device_class_hid_entry);
if(status != UX_SUCCESS)
return status;
/* Delete the main demo thread. */
status = ux_utility_thread_delete(&ux_hid_thread);
if(status != UX_SUCCESS)
return status;
return status;
}
/********************************************************************/
/** ux_demo_device_hid_instance_activate */
/********************************************************************/
VOID ux_demo_device_hid_instance_activate(VOID *hid_instance)
{
if (hid_keyboard == UX_NULL)
hid_keyboard = (UX_SLAVE_CLASS_HID*) hid_instance;
}
/********************************************************************/
/** ux_demo_device_hid_instance_deactivate */
/********************************************************************/
VOID ux_demo_device_hid_instance_deactivate(VOID *hid_instance)
{
if (hid_instance == (VOID *)hid_keyboard)
hid_keyboard = UX_NULL;
}
/********************************************************************/
/** ux_demo_device_hid_callback */
/********************************************************************/
UINT ux_demo_device_hid_callback(UX_SLAVE_CLASS_HID *hid_instance, UX_SLAVE_CLASS_HID_EVENT *hid_event)
{
UX_PARAMETER_NOT_USED(hid_instance);
/* There was an event. Analyze it. Is it NUM LOCK ? */
if ((hid_event -> ux_device_class_hid_event_buffer[0] & UX_HID_NUM_LOCK_MASK) &&
(hid_event -> ux_device_class_hid_event_report_type == UX_DEVICE_CLASS_HID_REPORT_TYPE_OUTPUT))
/* Set the Num lock flag. */
num_lock_flag = UX_TRUE;
else
/* Reset the Num lock flag. */
num_lock_flag = UX_FALSE;
/* There was an event. Analyze it. Is it CAPS LOCK ? */
if ((hid_event -> ux_device_class_hid_event_buffer[0] & UX_HID_CAPS_LOCK_MASK) &&
(hid_event -> ux_device_class_hid_event_report_type == UX_DEVICE_CLASS_HID_REPORT_TYPE_OUTPUT))
/* Set the Caps lock flag. */
caps_lock_flag = UX_TRUE;
else
/* Reset the Caps lock flag. */
caps_lock_flag = UX_FALSE;
return UX_SUCCESS;
}
/********************************************************************/
/** ux_demo_device_hid_get_callback */
/********************************************************************/
UINT ux_demo_device_hid_get_callback(UX_SLAVE_CLASS_HID *hid_instance, UX_SLAVE_CLASS_HID_EVENT *hid_event)
{
UX_PARAMETER_NOT_USED(hid_instance);
UX_PARAMETER_NOT_USED(hid_event);
return UX_SUCCESS;
}
/********************************************************************/
/** ux_demo_device_hid_thread_entry: hid demo thread */
/********************************************************************/
static VOID ux_demo_device_hid_thread_entry(ULONG thread_input)
{
UX_PARAMETER_NOT_USED(thread_input);
#ifndef EXTERNAL_DCD_INITIALIZE
/* Register the USB device controllers available in this system */
usb_device_dcd_initialize(UX_NULL);
#else /* EXTERNAL_DCD_INITIALIZE */
/* Register the USB device simulator controllers for testing */
ux_dcd_sim_slave_initialize();
#endif /* EXTERNAL_DCD_INITIALIZE */
while (1)
{
/* Check if the device state already configured. */
if ((UX_SLAVE_DEVICE_CHECK_STATE(UX_DEVICE_CONFIGURED)) && (hid_keyboard != UX_NULL))
{
ux_demo_device_hid_keyboard_send_character(hid_keyboard);
}
else
{
/* Sleep thread for 10ms. */
ux_utility_delay_ms(MS_TO_TICK(10));
}
}
}
/********************************************************************/
/** ux_demo_device_hid_keyboard_send_character: */
/** Send keyboard character */
/********************************************************************/
UINT ux_demo_device_hid_keyboard_send_character(UX_SLAVE_CLASS_HID *device_hid)
{
UINT status;
UX_SLAVE_CLASS_HID_EVENT device_hid_event;
static UCHAR key = 4;
/* Reset the HID event structure. */
ux_utility_memory_set(&device_hid_event, 0, sizeof(UX_SLAVE_CLASS_HID_EVENT));
if (key != 0)
{
/* Sleep thread for 20ms. */
ux_utility_delay_ms(MS_TO_TICK(20));
/* Then insert a key into the keyboard event. Length is fixed to 8. */
device_hid_event.ux_device_class_hid_event_report_id = 0;
device_hid_event.ux_device_class_hid_event_report_type = UX_DEVICE_CLASS_HID_REPORT_TYPE_INPUT;
device_hid_event.ux_device_class_hid_event_length = 8;
device_hid_event.ux_device_class_hid_event_buffer[0] = 0; /* 0x02: Left Shift modifier */
device_hid_event.ux_device_class_hid_event_buffer[1] = 0;
device_hid_event.ux_device_class_hid_event_buffer[2] = key; /* key */
device_hid_event.ux_device_class_hid_event_buffer[3] = 0;
device_hid_event.ux_device_class_hid_event_buffer[4] = 0;
device_hid_event.ux_device_class_hid_event_buffer[5] = 0;
device_hid_event.ux_device_class_hid_event_buffer[6] = 0;
device_hid_event.ux_device_class_hid_event_buffer[7] = 0;
/* Set the keyboard event. */
status = ux_device_class_hid_event_set(hid_keyboard, &device_hid_event);
if (status != UX_SUCCESS)
return status;
/* Next event has the key depressed. */
device_hid_event.ux_device_class_hid_event_buffer[2] = 0; /* 0x28: ENTER key */
/* Set the keyboard event. */
status = ux_device_class_hid_event_set(hid_keyboard, &device_hid_event);
if (status != UX_SUCCESS)
return status;
/* Are we at the end of alphabet ? */
if (key != (0x04 + 25))
/* Next key. */
key++;
else
key = 0;
}
else
{
/* Sleep thread for 10ms. */
ux_utility_delay_ms(MS_TO_TICK(10));
}
return status;
}
/********************************************************************/
/** ux_demo_error_callback: error callback */
/********************************************************************/
static VOID ux_demo_error_callback(UINT system_level, UINT system_context, UINT error_code)
{
/*
* Refer to ux_api.h. For example,
* UX_SYSTEM_LEVEL_INTERRUPT, UX_SYSTEM_CONTEXT_DCD, UX_DEVICE_HANDLE_UNKNOWN
*/
printf("USBX error: system level(%d), context(%d), error code(0x%x)\r\n", system_level, system_context, error_code);
}