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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
|
//*****************************************************************************
//
// usbdhidkeyb.c - USB HID Keyboard device class driver.
//
// Copyright (c) 2008-2012 Texas Instruments Incorporated. All rights reserved.
// Software License Agreement
//
// Texas Instruments (TI) is supplying this software for use solely and
// exclusively on TI's microcontroller products. The software is owned by
// TI and/or its suppliers, and is protected under applicable copyright
// laws. You may not combine this software with "viral" open-source
// software in order to form a larger program.
//
// THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
// NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
// NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES, FOR ANY REASON WHATSOEVER.
//
// This is part of revision 9453 of the Stellaris USB Library.
//
//*****************************************************************************
#include "inc/hw_types.h"
#include "driverlib/debug.h"
#include "driverlib/usb.h"
#include "usblib/usblib.h"
#include "usblib/device/usbdevice.h"
#include "usblib/usbhid.h"
#include "usblib/device/usbdhid.h"
#include "usblib/device/usbdhidkeyb.h"
//*****************************************************************************
//
//! \addtogroup hid_keyboard_device_class_api
//! @{
//
//*****************************************************************************
//*****************************************************************************
//
// The following is the HID report structure definition that is passed back
// to the host.
//
//*****************************************************************************
static const unsigned char g_pucKeybReportDescriptor[]=
{
UsagePage(USB_HID_GENERIC_DESKTOP),
Usage(USB_HID_KEYBOARD),
Collection(USB_HID_APPLICATION),
//
// Modifier keys.
// 8 - 1 bit values indicating the modifier keys (ctrl, shift...)
//
ReportSize(1),
ReportCount(8),
UsagePage(USB_HID_USAGE_KEYCODES),
UsageMinimum(224),
UsageMaximum(231),
LogicalMinimum(0),
LogicalMaximum(1),
Input(USB_HID_INPUT_DATA | USB_HID_INPUT_VARIABLE | USB_HID_INPUT_ABS),
//
// One byte of rsvd data required by HID spec.
//
ReportCount(1),
ReportSize(8),
Input(USB_HID_INPUT_CONSTANT),
//
// Keyboard LEDs.
// 5 - 1 bit values.
//
ReportCount(5),
ReportSize(1),
UsagePage(USB_HID_USAGE_LEDS),
UsageMinimum(1),
UsageMaximum(5),
Output(USB_HID_OUTPUT_DATA | USB_HID_OUTPUT_VARIABLE |
USB_HID_OUTPUT_ABS),
//
// 1 - 3 bit value to pad out to a full byte.
//
ReportCount(1),
ReportSize(3),
Output(USB_HID_OUTPUT_CONSTANT), //LED report padding
//
// The Key buffer.
// 6 - 8 bit values to store the current key state.
//
ReportCount(6),
ReportSize(8),
LogicalMinimum(0),
LogicalMaximum(101),
UsagePage(USB_HID_USAGE_KEYCODES),
UsageMinimum (0),
UsageMaximum (101),
Input(USB_HID_INPUT_DATA | USB_HID_INPUT_ARRAY),
EndCollection
};
//*****************************************************************************
//
// The HID class descriptor table. For the keyboard class, we have only a
// single report descriptor.
//
//*****************************************************************************
static const unsigned char * const g_pKeybClassDescriptors[] =
{
g_pucKeybReportDescriptor
};
//*****************************************************************************
//
// The HID descriptor for the keyboard device.
//
//*****************************************************************************
static const tHIDDescriptor g_sKeybHIDDescriptor =
{
9, // bLength
USB_HID_DTYPE_HID, // bDescriptorType
0x111, // bcdHID (version 1.11 compliant)
0, // bCountryCode (not localized)
1, // bNumDescriptors
{
{
USB_HID_DTYPE_REPORT, // Report descriptor
sizeof(g_pucKeybReportDescriptor) // Size of report descriptor
}
}
};
//*****************************************************************************
//
// Forward references for keyboard device callback functions.
//
//*****************************************************************************
static unsigned long HIDKeyboardRxHandler(void *pvCBData,
unsigned long ulEvent,
unsigned long ulMsgData,
void *pvMsgData);
static unsigned long HIDKeyboardTxHandler(void *pvCBData,
unsigned long ulEvent,
unsigned long ulMsgData,
void *pvMsgData);
//*****************************************************************************
//
// Main HID device class event handler function.
//
// \param pvCBData is the event callback pointer provided during USBDHIDInit().
// This is a pointer to our HID device structure (&g_sHIDKeybDevice).
// \param ulEvent identifies the event we are being called back for.
// \param ulMsgData is an event-specific value.
// \param pvMsgData is an event-specific pointer.
//
// This function is called by the HID device class driver to inform the
// application of particular asynchronous events related to operation of the
// keyboard HID device.
//
// \return Returns a value which is event-specific.
//
//*****************************************************************************
static unsigned long
HIDKeyboardRxHandler(void *pvCBData, unsigned long ulEvent,
unsigned long ulMsgData, void *pvMsgData)
{
tHIDKeyboardInstance *psInst;
tUSBDHIDKeyboardDevice *psDevice;
//
// Make sure we didn't get a NULL pointer.
//
ASSERT(pvCBData);
//
// Get a pointer to our instance data
//
psDevice = (tUSBDHIDKeyboardDevice *)pvCBData;
psInst = psDevice->psPrivateHIDKbdData;
//
// Which event were we sent?
//
switch (ulEvent)
{
//
// The host has connected to us and configured the device.
//
case USB_EVENT_CONNECTED:
{
psInst->ucUSBConfigured = true;
//
// Pass the information on to the client.
//
psDevice->pfnCallback(psDevice->pvCBData, USB_EVENT_CONNECTED,
0, (void *)0);
break;
}
//
// The host has disconnected from us.
//
case USB_EVENT_DISCONNECTED:
{
psInst->ucUSBConfigured = false;
//
// Pass the information on to the client.
//
psDevice->pfnCallback(psDevice->pvCBData, USB_EVENT_DISCONNECTED,
0, (void *)0);
break;
}
//
// The host is polling us for a particular report and the HID driver
// is asking for the latest version to transmit.
//
case USBD_HID_EVENT_IDLE_TIMEOUT:
case USBD_HID_EVENT_GET_REPORT:
{
//
// We only support a single input report so we don't need to check
// the ulMsgValue parameter in this case. Set the report pointer
// in *pvMsgData and return the length of the report in bytes.
//
*(unsigned char **)pvMsgData = psInst->pucReport;
return(KEYB_IN_REPORT_SIZE);
}
//
// The device class driver has completed sending a report to the
// host in response to a Get_Report request.
//
case USBD_HID_EVENT_REPORT_SENT:
{
//
// We have nothing to do here.
//
break;
}
//
// This event is sent in response to a host Set_Report request. We
// must return a pointer to a buffer large enough to receive the
// report into.
//
case USBD_HID_EVENT_GET_REPORT_BUFFER:
{
//
// Are we being asked for a report that is shorter than the storage
// we have set aside for this? The only output report we define is
// 8 bits long so we really expect to see a length of 1 passed.
//
if((unsigned long)pvMsgData == KEYB_OUT_REPORT_SIZE )
{
//
// Yes - return our pointer.
//
return((unsigned long)psInst->pucDataBuffer);
}
else
{
//
// We are being passed a report that is longer than the
// only report we expect so return NULL. This causes the
// device class driver to stall the request.
//
return(0);
}
}
//
// This event indicates that the host has sent us an Output or
// Feature report and that the report is now in the buffer we provided
// on the previous USBD_HID_EVENT_GET_REPORT_BUFFER callback.
//
case USBD_HID_EVENT_SET_REPORT:
{
//
// Inform the application if the keyboard LEDs have changed.
//
if(psInst->ucLEDStates != psInst->pucDataBuffer[0])
{
//
// Note the new LED states.
//
psInst->ucLEDStates = psInst->pucDataBuffer[0];
//
// Pass the information on to the client.
//
psDevice->pfnCallback(psDevice->pvCBData,
USBD_HID_KEYB_EVENT_SET_LEDS,
psInst->pucDataBuffer[0], (void *)0);
}
break;
}
//
// The host is asking us to set either boot or report protocol (not
// that it makes any difference to this particular mouse).
//
case USBD_HID_EVENT_SET_PROTOCOL:
{
psInst->ucProtocol = ulMsgData;
break;
}
//
// The host is asking us to tell it which protocol we are currently
// using, boot or request.
//
case USBD_HID_EVENT_GET_PROTOCOL:
{
return(psInst->ucProtocol);
}
//
// Pass ERROR, SUSPEND and RESUME to the client unchanged.
//
case USB_EVENT_ERROR:
case USB_EVENT_SUSPEND:
case USB_EVENT_RESUME:
{
return(psDevice->pfnCallback(psDevice->pvCBData, ulEvent,
ulMsgData, pvMsgData));
}
//
// We ignore all other events.
//
default:
{
break;
}
}
return(0);
}
//*****************************************************************************
//
// HID device class transmit channel event handler function.
//
// \param pvCBData is the event callback pointer provided during USBDHIDInit().
// This is a pointer to our HID device structure (&g_sHIDKeybDevice).
// \param ulEvent identifies the event we are being called back for.
// \param ulMsgData is an event-specific value.
// \param pvMsgData is an event-specific pointer.
//
// This function is called by the HID device class driver to inform the
// application of particular asynchronous events related to report
// transmissions made using the interrupt IN endpoint.
//
// \return Returns a value which is event-specific.
//
//*****************************************************************************
static unsigned long
HIDKeyboardTxHandler(void *pvCBData, unsigned long ulEvent,
unsigned long ulMsgData, void *pvMsgData)
{
tHIDKeyboardInstance *psInst;
tUSBDHIDKeyboardDevice *psDevice;
tUSBDHIDDevice *psHIDDevice;
unsigned long ulCount;
//
// Make sure we didn't get a NULL pointer.
//
ASSERT(pvCBData);
//
// Get a pointer to our instance data
//
psDevice = (tUSBDHIDKeyboardDevice *)pvCBData;
psInst = psDevice->psPrivateHIDKbdData;
psHIDDevice = &psDevice->psPrivateHIDKbdData->sHIDDevice;
//
// Which event were we sent?
//
switch (ulEvent)
{
//
// A report transmitted via the interrupt IN endpoint was acknowledged
// by the host.
//
case USB_EVENT_TX_COMPLETE:
{
//
// Do we have any pending changes needing transmitted?
//
if(psInst->bChangeMade)
{
//
// Yes - go ahead and send another report immediately.
//
ulCount = USBDHIDReportWrite((void *)psHIDDevice,
psInst->pucReport,
KEYB_IN_REPORT_SIZE, true);
//
// If we scheduled the report for transmission, clear the
// change flag.
//
if(ulCount != 0)
{
psInst->bChangeMade = false;
}
}
else
{
//
// Our last transmission is complete and we have nothing more
// to send.
//
psInst->eKeyboardState = HID_KEYBOARD_STATE_IDLE;
}
//
// Pass the event on to the client.
//
psDevice->pfnCallback(psDevice->pvCBData, USB_EVENT_TX_COMPLETE,
ulMsgData, (void *)0);
break;
}
//
// We ignore all other events related to transmission of reports via
// the interrupt IN endpoint.
//
default:
{
break;
}
}
return(0);
}
//*****************************************************************************
//
// Add the supplied usage code to the list of keys currently in the pressed
// state.
//
// \param ucUsageCode is the HID usage code of the newly pressed key.
//
// This function adds the supplied usage code to the global list of keys which
// are currently pressed (assuming it is not already noted as pressed and that
// there is space in the list to hold the new information). The return code
// indicates success if the list did not overflow and failure if the list
// already contains as many pressed keys as can be reported.
//
// \return Returns \b true if the usage code was successfully added to the
// list or \b false if there was insufficient space to hold the new key
// press (in which case the caller should report a roll over error to the host).
//
//*****************************************************************************
static tBoolean
AddKeyToPressedList(tHIDKeyboardInstance *psInst, unsigned char ucUsageCode)
{
unsigned long ulLoop;
tBoolean bRetcode;
//
// Assume all is well until we determine otherwise.
//
bRetcode = true;
//
// Look through the list of existing pressed keys to see if the new one
// is already there.
//
for(ulLoop = 0; ulLoop < (unsigned long)psInst->ucKeyCount; ulLoop++)
{
//
// Is this key already included in the list of keys in the pressed
// state?
//
if(ucUsageCode == psInst->pucKeysPressed[ulLoop])
{
//
// Yes - drop out.
//
break;
}
}
//
// If we exited the loop at the end of the existing key presses, this
// key does not exist already so add it if space exists.
//
if(ulLoop >= psInst->ucKeyCount)
{
if(psInst->ucKeyCount < KEYB_MAX_CHARS_PER_REPORT)
{
//
// We have room so store the new key press in the list.
//
psInst->pucKeysPressed[psInst->ucKeyCount] = ucUsageCode;
psInst->ucKeyCount++;
bRetcode = true;
}
else
{
//
// We have no room for the new key - declare a rollover error.
//
bRetcode = false;
}
}
return(bRetcode);
}
//*****************************************************************************
//
// Remove the supplied usage code from the list of keys currently in the
// pressed state.
//
// \param ucUsageCode is the HID usage code of the newly released key.
//
// This function removes the supplied usage code from the global list of keys
// which are currently pressed. The return code indicates whether the key was
// found in the list. On exit, the list will have been cleaned up to ensure
// that all key presses are contiguous starting at the first entry.
//
// \return Returns \b true if the usage code was found and removed from the
// list or \b false if the code was not found. The caller need not pass a new
// report to the host if \b false is returned since the key list will not have
// changed.
//
//*****************************************************************************
static tBoolean
RemoveKeyFromPressedList(tHIDKeyboardInstance *psInst,
unsigned char ucUsageCode)
{
unsigned long ulLoop;
unsigned long ulPos;
//
// Keep the compiler happy by setting ulPos to something.
//
ulPos = 0;
//
// Find the usage code in the current list.
//
for(ulLoop = 0; ulLoop < KEYB_MAX_CHARS_PER_REPORT; ulLoop++)
{
if(psInst->pucKeysPressed[ulLoop] == ucUsageCode)
{
ulPos = ulLoop;
break;
}
}
//
// If we dropped out at the end of the loop, we couldn't find the code so
// just return false.
//
if(ulLoop == KEYB_MAX_CHARS_PER_REPORT)
{
return(false);
}
//
// Now shuffle all the values to the right of the usage code we found
// down one position to fill the gap left by removing it.
//
for(ulLoop = (ulPos + 1); ulLoop < KEYB_MAX_CHARS_PER_REPORT; ulLoop++)
{
psInst->pucKeysPressed[ulLoop - 1] = psInst->pucKeysPressed[ulLoop];
}
//
// Clear the last entry in the array and adjust the number of keys in the
// array.
//
psInst->pucKeysPressed[KEYB_MAX_CHARS_PER_REPORT - 1] =
HID_KEYB_USAGE_RESERVED;
psInst->ucKeyCount--;
//
// Tell the caller we were successful.
//
return(true);
}
//*****************************************************************************
//
//! Initializes HID keyboard device operation for a given USB controller.
//!
//! \param ulIndex is the index of the USB controller which is to be
//! initialized for HID keyboard device operation.
//! \param psDevice points to a structure containing parameters customizing
//! the operation of the HID keyboard device.
//!
//! An application wishing to offer a USB HID keyboard interface to a USB host
//! must call this function to initialize the USB controller and attach the
//! keyboard device to the USB bus. This function performs all required USB
//! initialization.
//!
//! On successful completion, this function will return the \e psDevice pointer
//! passed to it. This must be passed on all future calls to the HID keyboard
//! device driver.
//!
//! When a host connects and configures the device, the application callback
//! will receive \b USB_EVENT_CONNECTED after which calls can be made to
//! USBDHIDKeyboardKeyStateChange() to report key presses and releases to the
//! USB host.
//!
//! \note The application must not make any calls to the lower level USB device
//! interfaces if interacting with USB via the USB HID keyboard device class
//! API. Doing so will cause unpredictable (though almost certainly
//! unpleasant) behavior.
//!
//! \return Returns NULL on failure or the psDevice pointer on success.
//
//*****************************************************************************
void *
USBDHIDKeyboardInit(unsigned long ulIndex,
const tUSBDHIDKeyboardDevice *psDevice)
{
void *pvRetcode;
tUSBDHIDDevice *psHIDDevice;
//
// Check parameter validity.
//
ASSERT(psDevice);
ASSERT(psDevice->ppStringDescriptors);
ASSERT(psDevice->psPrivateHIDKbdData);
ASSERT(psDevice->pfnCallback);
//
// Get a pointer to the HID device data.
//
psHIDDevice = &psDevice->psPrivateHIDKbdData->sHIDDevice;
//
// Call the common initialization routine.
//
pvRetcode = USBDHIDKeyboardCompositeInit(ulIndex, psDevice);
//
// If we initialized the HID layer successfully, pass our device pointer
// back as the return code, otherwise return NULL to indicate an error.
//
if(pvRetcode)
{
//
// Initialize the lower layer HID driver and pass it the various
// structures and descriptors necessary to declare that we are a
// keyboard.
//
pvRetcode = USBDHIDInit(ulIndex, psHIDDevice);
return((void *)psDevice);
}
else
{
return((void *)0);
}
}
//*****************************************************************************
//
//! Initializes HID keyboard device operation for a given USB controller.
//!
//! \param ulIndex is the index of the USB controller which is to be
//! initialized for HID keyboard device operation.
//! \param psDevice points to a structure containing parameters customizing
//! the operation of the HID keyboard device.
//!
//! This call is very similar to USBDKeyboardInit() except that it is used for
//! initializing an instance of the HID keyboard device for use in a composite
//! device.
//!
//! \return Returns zero on failure or a non-zero instance value that should be
//! used with the remaining USB HID Keyboard APIs.
//
//*****************************************************************************
void *
USBDHIDKeyboardCompositeInit(unsigned long ulIndex,
const tUSBDHIDKeyboardDevice *psDevice)
{
tHIDKeyboardInstance *psInst;
unsigned long ulLoop;
tUSBDHIDDevice *psHIDDevice;
//
// Check parameter validity.
//
ASSERT(psDevice);
ASSERT(psDevice->ppStringDescriptors);
ASSERT(psDevice->psPrivateHIDKbdData);
ASSERT(psDevice->pfnCallback);
//
// Get a pointer to our instance data
//
psInst = psDevice->psPrivateHIDKbdData;
//
// Initialize the various fields in our instance structure.
//
psInst->ucUSBConfigured = 0;
psInst->ucProtocol = USB_HID_PROTOCOL_REPORT;
psInst->sReportIdle.ucDuration4mS = 125;
psInst->sReportIdle.ucReportID = 0;
psInst->sReportIdle.ulTimeSinceReportmS = 0;
psInst->sReportIdle.usTimeTillNextmS = 0;
psInst->ucLEDStates = 0;
psInst->ucKeyCount = 0;
for(ulLoop = 0; ulLoop < KEYB_MAX_CHARS_PER_REPORT; ulLoop++)
{
psInst->pucKeysPressed[ulLoop] = HID_KEYB_USAGE_RESERVED;
}
psInst->eKeyboardState = HID_KEYBOARD_STATE_UNCONFIGURED;
//
// Get a pointer to the HID device data.
//
psHIDDevice = &psDevice->psPrivateHIDKbdData->sHIDDevice;
//
// Initialize the HID device class instance structure based on input from
// the caller.
//
psHIDDevice->usPID = psDevice->usPID;
psHIDDevice->usVID = psDevice->usVID;
psHIDDevice->usMaxPowermA = psDevice->usMaxPowermA;
psHIDDevice->ucPwrAttributes = psDevice->ucPwrAttributes;
psHIDDevice->ucSubclass = USB_HID_SCLASS_BOOT;
psHIDDevice->ucProtocol = USB_HID_PROTOCOL_KEYB;
psHIDDevice->ucNumInputReports = 1;
psHIDDevice->psReportIdle = 0;
psHIDDevice->pfnRxCallback = HIDKeyboardRxHandler;
psHIDDevice->pvRxCBData = (void *)psDevice;
psHIDDevice->pfnTxCallback = HIDKeyboardTxHandler;
psHIDDevice->pvTxCBData = (void *)psDevice;
psHIDDevice->bUseOutEndpoint = false,
psHIDDevice->psHIDDescriptor = &g_sKeybHIDDescriptor;
psHIDDevice->ppClassDescriptors = g_pKeybClassDescriptors;
psHIDDevice->ppStringDescriptors = psDevice->ppStringDescriptors;
psHIDDevice->ulNumStringDescriptors = psDevice->ulNumStringDescriptors;
psHIDDevice->psPrivateHIDData = &psInst->sHIDInstance;
psHIDDevice->psReportIdle = &psInst->sReportIdle;
//
// Initialize the lower layer HID driver and pass it the various structures
// and descriptors necessary to declare that we are a keyboard.
//
return(USBDHIDCompositeInit(ulIndex, psHIDDevice));
}
//*****************************************************************************
//
//! Shuts down the HID keyboard device.
//!
//! \param pvInstance is the pointer to the device instance structure as
//! returned by USBDHIDKeyboardInit().
//!
//! This function terminates HID keyboard operation for the instance supplied
//! and removes the device from the USB bus. Following this call, the \e
//! pvInstance instance may not me used in any other call to the HID keyboard
//! device other than USBDHIDKeyboardInit().
//!
//! \return None.
//
//*****************************************************************************
void
USBDHIDKeyboardTerm(void *pvInstance)
{
tUSBDHIDKeyboardDevice *psDevice;
tUSBDHIDDevice *psHIDDevice;
ASSERT(pvInstance);
//
// Get a pointer to the device.
//
psDevice = (tUSBDHIDKeyboardDevice *)pvInstance;
//
// Get a pointer to the HID device data.
//
psHIDDevice = &psDevice->psPrivateHIDKbdData->sHIDDevice;
//
// Mark the device as no longer configured.
//
psDevice->psPrivateHIDKbdData->ucUSBConfigured = 0;
//
// Terminate the low level HID driver.
//
USBDHIDTerm(psHIDDevice);
}
//*****************************************************************************
//
//! Sets the client-specific pointer parameter for the keyboard callback.
//!
//! \param pvInstance is the pointer to the device instance structure as
//! returned by USBDHIDKeyboardInit().
//! \param pvCBData is the pointer that client wishes to be provided on each
//! event sent to the keyboard callback function.
//!
//! The client uses this function to change the callback pointer passed in
//! the first parameter on all callbacks to the \e pfnCallback function
//! passed on USBDHIDKeyboardInit().
//!
//! If a client wants to make runtime changes in the callback pointer, it must
//! ensure that the pvInstance structure passed to USBDHIDKeyboardInit() resides
//! in RAM. If this structure is in flash, callback data changes will not be
//! possible.
//!
//! \return Returns the previous callback pointer that was set for this
//! instance.
//
//*****************************************************************************
void *
USBDHIDKeyboardSetCBData(void *pvInstance, void *pvCBData)
{
void *pvOldCBData;
tUSBDHIDKeyboardDevice *psKeyboard;
//
// Check for a NULL pointer in the device parameter.
//
ASSERT(pvInstance);
//
// Get a pointer to our keyboard device.
//
psKeyboard = (tUSBDHIDKeyboardDevice *)pvInstance;
//
// Save the old callback pointer and replace it with the new value.
//
pvOldCBData = psKeyboard->pvCBData;
psKeyboard->pvCBData = pvCBData;
//
// Pass the old callback pointer back to the caller.
//
return(pvOldCBData);
}
//*****************************************************************************
//
//! Reports a key state change to the USB host.
//!
//! \param pvInstance is the pointer to the device instance structure as
//! returned by USBDHIDKeyboardInit().
//! \param ucModifiers contains the states of each of the keyboard modifiers
//! (left/right shift, ctrl, alt or GUI keys). Valid values are logical OR
//! combinations of the labels \b HID_KEYB_LEFT_CTRL, \b HID_KEYB_LEFT_SHIFT,
//! \b HID_KEYB_LEFT_ALT, \b HID_KEYB_LEFT_GUI, \b HID_KEYB_RIGHT_CTRL, \b
//! HID_KEYB_RIGHT_SHIFT, \b HID_KEYB_RIGHT_ALT and \b HID_KEYB_RIGHT_GUI.
//! Presence of one of these bit flags indicates that the relevant modifier
//! key is pressed and absence indicates that it is released.
//! \param ucUsageCode is the usage code of the key whose state has changed.
//! If only modifier keys have changed, \b HID_KEYB_USAGE_RESERVED should be
//! passed in this parameter.
//! \param bPress is \b true if the key has been pressed or \b false if it has
//! been released. If only modifier keys have changed state, this parameter is
//! ignored.
//!
//! This function adds or removes a key usage code from the list of keys
//! currently pressed and schedules a report transmission to the host to
//! inform it of the new keyboard state. If the maximum number of simultaneous
//! key presses are already recorded, the report to the host will contain the
//! rollover error code, HID_KEYB_USAGE_ROLLOVER instead of key usage codes
//! and the caller will receive return code KEYB_ERR_TOO_MANY_KEYS.
//!
//! \return Returns \b KEYB_SUCCESS if the key usage code was added to or
//! removed from the current list successfully. \b KEYB_ERR_TOO_MANY_KEYS is
//! returned if an attempt is made to press a 7th key (the BIOS keyboard
//! protocol can report no more than 6 simultaneously pressed keys). If called
//! before the USB host has configured the device, \b KEYB_ERR_NOT_CONFIGURED
//! is returned and, if an error is reported while attempting to transmit the
//! report, \b KEYB_ERR_TX_ERROR is returned. If an attempt is made to remove
//! a key from the pressed list (by setting parameter \e bPressed to \b false)
//! but the key usage code is not found, \b KEYB_ERR_NOT_FOUND is returned.
//
//*****************************************************************************
unsigned long
USBDHIDKeyboardKeyStateChange(void *pvInstance, unsigned char ucModifiers,
unsigned char ucUsageCode, tBoolean bPress)
{
tBoolean bRetcode;
unsigned long ulLoop;
unsigned long ulCount;
tHIDKeyboardInstance *psInst;
tUSBDHIDKeyboardDevice *psDevice;
tUSBDHIDDevice *psHIDDevice;
psDevice = (tUSBDHIDKeyboardDevice *)pvInstance;
//
// Get a pointer to the HID device data.
//
psHIDDevice = &psDevice->psPrivateHIDKbdData->sHIDDevice;
//
// Assume all is well until we determine otherwise.
//
bRetcode = true;
//
// Get a pointer to our instance data
//
psInst = psDevice->psPrivateHIDKbdData;
//
// Update the global keyboard report with the information passed.
//
psInst->pucReport[0] = ucModifiers;
psInst->pucReport[1] = 0;
//
// Were we passed a usage code for a new key press or release or was
// this call just telling us about a modifier change?
//
if(ucUsageCode != HID_KEYB_USAGE_RESERVED)
{
//
// Has a key been pressed or released?
//
if(bPress)
{
//
// A key has been pressed - add it to the list if there is space an
// and the key is not already in the list.
//
bRetcode = AddKeyToPressedList(psInst, ucUsageCode);
}
else
{
//
// A key has been released - remove it from the list.
//
bRetcode = RemoveKeyFromPressedList(psInst, ucUsageCode);
//
// The return code here indicates whether the key was found. If it
// wasn't, the list has not changes so merely exit at this point
// without sending anything to the host.
//
if(!bRetcode)
{
return(KEYB_ERR_NOT_FOUND);
}
}
//
// Build the report from the current list of keys. If we added a key
// and got a bad return code indicating a roll over error, we need to
// send a roll over report
//
for(ulLoop = 0; ulLoop < KEYB_MAX_CHARS_PER_REPORT; ulLoop++)
{
psInst->pucReport[2 + ulLoop] = (bRetcode ?
psInst->pucKeysPressed[ulLoop] : HID_KEYB_USAGE_ROLLOVER);
}
}
//
// If we are not configured, return an error here before trying to send
// anything.
//
if(!psInst->ucUSBConfigured)
{
return(KEYB_ERR_NOT_CONFIGURED);
}
//
// Only send a report if the transmitter is currently free.
//
if(USBDHIDTxPacketAvailable((void *)psHIDDevice))
{
//
// Send the report to the host.
//
psInst->eKeyboardState = HID_KEYBOARD_STATE_SEND;
ulCount = USBDHIDReportWrite((void *)psHIDDevice,
psInst->pucReport, KEYB_IN_REPORT_SIZE,
true);
//
// Did we schedule a packet for transmission correctly?
//
if(!ulCount)
{
//
// No - report the error to the caller.
//
return(KEYB_ERR_TX_ERROR);
}
}
else
{
//
// We can't send the report immediately so mark the instance so that
// it is sent next time the transmitter is free.
//
psInst->bChangeMade = true;
}
//
// If we get this far, the key information was sent successfully. Are
// too many keys currently pressed, though?
//
return(bRetcode ? KEYB_SUCCESS : KEYB_ERR_TOO_MANY_KEYS);
}
//*****************************************************************************
//
//! Reports the device power status (bus or self powered) to the USB library.
//!
//! \param pvInstance is the pointer to the keyboard device instance structure.
//! \param ucPower indicates the current power status, either \b
//! USB_STATUS_SELF_PWR or \b USB_STATUS_BUS_PWR.
//!
//! Applications which support switching between bus or self powered
//! operation should call this function whenever the power source changes
//! to indicate the current power status to the USB library. This information
//! is required by the USB library to allow correct responses to be provided
//! when the host requests status from the device.
//!
//! \return None.
//
//*****************************************************************************
void
USBDHIDKeyboardPowerStatusSet(void *pvInstance, unsigned char ucPower)
{
tUSBDHIDKeyboardDevice *psDevice;
tUSBDHIDDevice *psHIDDevice;
ASSERT(pvInstance);
//
// Get the keyboard device pointer.
//
psDevice = (tUSBDHIDKeyboardDevice *)pvInstance;
//
// Get a pointer to the HID device data.
//
psHIDDevice = &psDevice->psPrivateHIDKbdData->sHIDDevice;
//
// Pass the request through to the lower layer.
//
USBDHIDPowerStatusSet((void *)psHIDDevice, ucPower);
}
//*****************************************************************************
//
//! Requests a remote wake up to resume communication when in suspended state.
//!
//! \param pvInstance is the pointer to the keyboard device instance structure.
//!
//! When the bus is suspended, an application which supports remote wake up
//! (advertised to the host via the configuration descriptor) may call this
//! function to initiate remote wake up signaling to the host. If the remote
//! wake up feature has not been disabled by the host, this will cause the bus
//! to resume operation within 20mS. If the host has disabled remote wake up,
//! \b false will be returned to indicate that the wake up request was not
//! successful.
//!
//! \return Returns \b true if the remote wake up is not disabled and the
//! signaling was started or \b false if remote wake up is disabled or if
//! signaling is currently ongoing following a previous call to this function.
//
//*****************************************************************************
tBoolean
USBDHIDKeyboardRemoteWakeupRequest(void *pvInstance)
{
tUSBDHIDKeyboardDevice *psDevice;
tUSBDHIDDevice *psHIDDevice;
ASSERT(pvInstance);
//
// Get the keyboard device pointer.
//
psDevice = (tUSBDHIDKeyboardDevice *)pvInstance;
//
// Get a pointer to the HID device data.
//
psHIDDevice = &psDevice->psPrivateHIDKbdData->sHIDDevice;
//
// Pass the request through to the lower layer.
//
return(USBDHIDRemoteWakeupRequest((void *)psHIDDevice));
}
//*****************************************************************************
//
// Close the Doxygen group.
//! @}
//
//*****************************************************************************
|