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
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
|
//*****************************************************************************
//
// usb_host_keyboard.c - main application code for the host keyboard example.
//
// Copyright (c) 2012-2014 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 2.1.0.12573 of the EK-LM4F232 Firmware Package.
//
//*****************************************************************************
#include <stdbool.h>
#include <stdint.h>
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/pin_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/systick.h"
#include "driverlib/rom.h"
#include "driverlib/uart.h"
#include "grlib/grlib.h"
#include "usblib/usblib.h"
#include "usblib/usbhid.h"
#include "usblib/host/usbhost.h"
#include "usblib/host/usbhhid.h"
#include "usblib/host/usbhhidkeyboard.h"
#include "utils/uartstdio.h"
#include "drivers/cfal96x64x16.h"
//*****************************************************************************
//
//! \addtogroup example_list
//! <h1>USB HID Keyboard Host (usb_host_keyboard)</h1>
//!
//! This example application demonstrates how to support a USB keyboard
//! attached to the evaluation kit board. The display will show if a keyboard
//! is currently connected and the current state of the Caps Lock key on the
//! keyboard that is connected on the bottom status area of the screen.
//! Pressing any keys on the keyboard will cause them to be printed on the
//! screen and to be sent out the UART at 115200 baud with no parity, 8 bits
//! and 1 stop bit. Any keyboard that supports the USB HID BIOS protocol
//! should work with this demo application.
//
//*****************************************************************************
//*****************************************************************************
//
// The ASCII code for a backspace character.
//
//*****************************************************************************
#define ASCII_BACKSPACE 0x08
//*****************************************************************************
//
// The size of the host controller's memory pool in bytes.
//
//*****************************************************************************
#define HCD_MEMORY_SIZE 128
//*****************************************************************************
//
// The memory pool to provide to the Host controller driver.
//
//*****************************************************************************
uint8_t g_pui8HCDPool[HCD_MEMORY_SIZE];
//*****************************************************************************
//
// The size of the keyboard device interface's memory pool in bytes.
//
//*****************************************************************************
#define KEYBOARD_MEMORY_SIZE 128
//*****************************************************************************
//
// The memory pool to provide to the keyboard device.
//
//*****************************************************************************
uint8_t g_pui8Buffer[KEYBOARD_MEMORY_SIZE];
//*****************************************************************************
//
// Declare the USB Events driver interface.
//
//*****************************************************************************
DECLARE_EVENT_DRIVER(g_sUSBEventDriver, 0, 0, USBHCDEvents);
//*****************************************************************************
//
// The global that holds all of the host drivers in use in the application.
// In this case, only the Keyboard class is loaded.
//
//*****************************************************************************
static tUSBHostClassDriver const * const g_ppHostClassDrivers[] =
{
&g_sUSBHIDClassDriver,
&g_sUSBEventDriver
};
//*****************************************************************************
//
// This global holds the number of class drivers in the g_ppHostClassDrivers
// list.
//
//*****************************************************************************
static const uint32_t g_ui32NumHostClassDrivers =
sizeof(g_ppHostClassDrivers) / sizeof(tUSBHostClassDriver *);
//*****************************************************************************
//
// The number of SysTick ticks per second.
//
//*****************************************************************************
#define TICKS_PER_SECOND 100
#define MS_PER_SYSTICK (1000 / TICKS_PER_SECOND)
//*****************************************************************************
//
// Our running system tick counter and a global used to determine the time
// elapsed since last call to GetTickms().
//
//*****************************************************************************
uint32_t g_ui32SysTickCount;
uint32_t g_ui32LastTick;
//*****************************************************************************
//
// Graphics context used to show text on the CSTN display.
//
//*****************************************************************************
tContext g_sContext;
//*****************************************************************************
//
// The global value used to store the keyboard instance value.
//
//*****************************************************************************
static tUSBHKeyboard *g_psKeyboardInstance;
//*****************************************************************************
//
// This enumerated type is used to hold the states of the keyboard.
//
//*****************************************************************************
enum
{
//
// No device is present.
//
STATE_NO_DEVICE,
//
// Keyboard has been detected and needs to be initialized in the main
// loop.
//
STATE_KEYBOARD_INIT,
//
// Keyboard is connected and waiting for events.
//
STATE_KEYBOARD_CONNECTED,
//
// Keyboard has received a key press that requires updating the keyboard
// in the main loop.
//
STATE_KEYBOARD_UPDATE,
//
// An unsupported device has been attached.
//
STATE_UNKNOWN_DEVICE,
//
// A power fault has occurred.
//
STATE_POWER_FAULT
}
g_eUSBState;
extern const tHIDKeyboardUsageTable g_sUSKeyboardMap;
//*****************************************************************************
//
// The current USB operating mode - Host, Device or unknown.
//
//*****************************************************************************
tUSBMode g_eCurrentUSBMode;
//*****************************************************************************
//
// These defines are used to define the screen constraints to the application.
//
//*****************************************************************************
#define DISPLAY_BANNER_HEIGHT 10
#define DISPLAY_BANNER_BG ClrDarkBlue
#define DISPLAY_TEXT_BORDER 2
#define DISPLAY_TEXT_FG ClrWhite
#define DISPLAY_TEXT_BG ClrBlack
//*****************************************************************************
//
// This variable holds the current status of the modifiers keys.
//
//*****************************************************************************
uint32_t g_ui32Modifiers = 0;
//*****************************************************************************
//
// This is the number of characters that will fit on a line in the text area.
//
//*****************************************************************************
uint32_t g_ui32CharsPerLine;
//*****************************************************************************
//
// This is the number of lines that will fit in the text area.
//
//*****************************************************************************
uint32_t g_ui32LinesPerScreen;
//*****************************************************************************
//
// This is the current line for printing in the text area.
//
//*****************************************************************************
uint32_t g_ui32Line = 0;
//*****************************************************************************
//
// This is the current column for printing in the text area.
//
//*****************************************************************************
uint32_t g_ui32Column = 0;
//*****************************************************************************
//
// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
#ifdef DEBUG
void
__error__(char *pcFilename, uint32_t ui32Line)
{
}
#endif
//*****************************************************************************
//
// This is the handler for this SysTick interrupt.
//
//*****************************************************************************
void
SysTickIntHandler(void)
{
//
// Update our tick counter.
//
g_ui32SysTickCount++;
}
//*****************************************************************************
//
// This function prints the character out the UART and into the text area of
// the screen.
//
// \param cChar is the character to print out.
//
// This function handles all of the detail of printing a character to both the
// UART and to the text area of the screen on the evaluation board. The text
// area of the screen will be cleared any time the text goes beyond the end
// of the text area.
//
// \return None.
//
//*****************************************************************************
void
PrintChar(const char cChar)
{
tRectangle sRect;
//
// If both the line and column have gone to zero then clear the screen.
//
if((g_ui32Line == 0) && (g_ui32Column == 0))
{
//
// Form the rectangle that makes up the text box.
//
sRect.i16XMin = 0;
sRect.i16YMin = (2 * DISPLAY_BANNER_HEIGHT) + DISPLAY_TEXT_BORDER;
sRect.i16XMax = GrContextDpyWidthGet(&g_sContext) - DISPLAY_TEXT_BORDER;
sRect.i16YMax = GrContextDpyHeightGet(&g_sContext) -
DISPLAY_BANNER_HEIGHT - DISPLAY_TEXT_BORDER;
//
// Change the foreground color to black and draw black rectangle to
// clear the screen.
//
GrContextForegroundSet(&g_sContext, DISPLAY_TEXT_BG);
GrRectFill(&g_sContext, &sRect);
//
// Reset the foreground color to the text color.
//
GrContextForegroundSet(&g_sContext, DISPLAY_TEXT_FG);
}
//
// Send the character to the UART.
//
UARTprintf("%c", cChar);
//
// Allow new lines to cause the column to go back to zero.
//
if(cChar != '\n')
{
//
// Did we get a backspace character?
//
if(cChar != ASCII_BACKSPACE)
{
//
// This is not a backspace so print the character to the screen.
//
GrStringDraw(&g_sContext, &cChar, 1,
GrFontMaxWidthGet(g_psFontFixed6x8) * g_ui32Column,
(2 * DISPLAY_BANNER_HEIGHT) + DISPLAY_TEXT_BORDER +
(g_ui32Line * GrFontHeightGet(g_psFontFixed6x8)), 0);
}
else
{
//
// We got a backspace. If we are at the top left of the screen,
// return since we don't need to do anything.
//
if(g_ui32Column || g_ui32Line)
{
//
// Adjust the cursor position to erase the last character.
//
if(g_ui32Column)
{
g_ui32Column--;
}
else
{
g_ui32Column = g_ui32CharsPerLine;
g_ui32Line--;
}
//
// Print a space at this position then return without fixing up
// the cursor again.
//
GrStringDraw(&g_sContext, " ", 1,
GrFontMaxWidthGet(g_psFontFixed6x8) * g_ui32Column,
(2 * DISPLAY_BANNER_HEIGHT) + DISPLAY_TEXT_BORDER +
(g_ui32Line * GrFontHeightGet(g_psFontFixed6x8)),
true);
}
return;
}
}
else
{
//
// This will allow the code below to properly handle the new line.
//
g_ui32Column = g_ui32CharsPerLine;
}
//
// Update the text row and column that the next character will use.
//
if(g_ui32Column < g_ui32CharsPerLine)
{
//
// No line wrap yet so move one column over.
//
g_ui32Column++;
}
else
{
//
// Line wrapped so go back to the first column and update the line.
//
g_ui32Column = 0;
g_ui32Line++;
//
// The line has gone past the end so go back to the first line.
//
if(g_ui32Line >= g_ui32LinesPerScreen)
{
g_ui32Line = 0;
}
}
}
//*****************************************************************************
//
// This function updates the status area of the screen. It uses the current
// state of the application to print the status bar.
//
//*****************************************************************************
void
UpdateStatus(void)
{
tRectangle sRect;
//
// Fill the bottom rows of the screen with blue to create the status area.
//
sRect.i16XMin = 0;
sRect.i16YMin = GrContextDpyHeightGet(&g_sContext) -
DISPLAY_BANNER_HEIGHT - 1;
sRect.i16XMax = GrContextDpyWidthGet(&g_sContext) - 1;
sRect.i16YMax = sRect.i16YMin + DISPLAY_BANNER_HEIGHT;
GrContextForegroundSet(&g_sContext, DISPLAY_BANNER_BG);
GrRectFill(&g_sContext, &sRect);
//
// Put a white box around the banner.
//
GrContextForegroundSet(&g_sContext, ClrWhite);
GrRectDraw(&g_sContext, &sRect);
//
// Put the application name in the middle of the banner.
//
GrContextFontSet(&g_sContext, g_psFontFixed6x8);
//
// Update the status on the screen.
//
if(g_eUSBState == STATE_NO_DEVICE)
{
//
// Keyboard is currently disconnected.
//
GrStringDrawCentered(&g_sContext, "no device", -1,
GrContextDpyWidthGet(&g_sContext) / 2,
sRect.i16YMin + 5, 0);
}
else if(g_eUSBState == STATE_UNKNOWN_DEVICE)
{
//
// Unknown device is currently connected.
//
GrStringDrawCentered(&g_sContext, "unknown device", -1,
GrContextDpyWidthGet(&g_sContext) / 2,
sRect.i16YMin + 5, 0);
}
else if(g_eUSBState == STATE_POWER_FAULT)
{
//
// Something caused a power fault.
//
GrStringDrawCentered(&g_sContext, "power fault", -1,
GrContextDpyWidthGet(&g_sContext) / 2,
sRect.i16YMin + 5, 0);
}
else if((g_eUSBState == STATE_KEYBOARD_CONNECTED) ||
(g_eUSBState == STATE_KEYBOARD_UPDATE))
{
//
// Keyboard is connected.
//
GrStringDrawCentered(&g_sContext, "connected", -1,
GrContextDpyWidthGet(&g_sContext) / 2,
sRect.i16YMin + 5, 0);
//
// Update the CAPS Lock status.
//
if(g_ui32Modifiers & HID_KEYB_CAPS_LOCK)
{
GrStringDrawCentered(&g_sContext, "C",
GrContextDpyWidthGet(&g_sContext) / 2,
sRect.i16XMax - 10,
sRect.i16YMin + 5, 0);
}
}
}
//*****************************************************************************
//
// This is the generic callback from host stack.
//
// pvData is actually a pointer to a tEventInfo structure.
//
// This function will be called to inform the application when a USB event has
// occurred that is outside those related to the keyboard device. At this
// point this is used to detect unsupported devices being inserted and removed.
// It is also used to inform the application when a power fault has occurred.
// This function is required when the g_USBGenericEventDriver is included in
// the host controller driver array that is passed in to the
// USBHCDRegisterDrivers() function.
//
//*****************************************************************************
void
USBHCDEvents(void *pvData)
{
tEventInfo *pEventInfo;
//
// Cast this pointer to its actual type.
//
pEventInfo = (tEventInfo *)pvData;
switch(pEventInfo->ui32Event)
{
//
// New keyboard detected.
//
case USB_EVENT_CONNECTED:
{
//
// See if this is a HID Keyboard.
//
if((USBHCDDevClass(pEventInfo->ui32Instance, 0) == USB_CLASS_HID) &&
(USBHCDDevProtocol(pEventInfo->ui32Instance, 0) ==
USB_HID_PROTOCOL_KEYB))
{
//
// Indicate that the keyboard has been detected.
//
UARTprintf("Keyboard Connected\n");
//
// Proceed to the STATE_KEYBOARD_INIT state so that the main
// loop can finish initialized the mouse since
// USBHKeyboardInit() cannot be called from within a callback.
//
g_eUSBState = STATE_KEYBOARD_INIT;
}
break;
}
//
// Unsupported device detected.
//
case USB_EVENT_UNKNOWN_CONNECTED:
{
UARTprintf("Unsupported Device Class (0x%02x) Connected.\n",
pEventInfo->ui32Instance);
//
// An unknown device was detected.
//
g_eUSBState = STATE_UNKNOWN_DEVICE;
UpdateStatus();
break;
}
//
// Device has been unplugged.
//
case USB_EVENT_DISCONNECTED:
{
//
// Indicate that the device has been disconnected.
//
UARTprintf("Device Disconnected\n");
//
// Change the state so that the main loop knows that the device
// is no longer present.
//
g_eUSBState = STATE_NO_DEVICE;
//
// Update the screen.
//
UpdateStatus();
break;
}
//
// Power Fault occurred.
//
case USB_EVENT_POWER_FAULT:
{
UARTprintf("Power Fault\n");
//
// No power means no device is present.
//
g_eUSBState = STATE_POWER_FAULT;
UpdateStatus();
break;
}
default:
{
break;
}
}
}
//*****************************************************************************
//
// USB Mode callback
//
// \param ui32Index is the zero-based index of the USB controller making the
// callback.
// \param eMode indicates the new operating mode.
//
// This function is called by the USB library whenever an OTG mode change
// occurs and, if a connection has been made, informs us of whether we are to
// operate as a host or device.
//
// \return None.
//
//*****************************************************************************
void
ModeCallback(uint32_t ui32Index, tUSBMode eMode)
{
//
// Save the new mode.
//
g_eCurrentUSBMode = eMode;
switch(eMode)
{
case eUSBModeHost:
{
UARTprintf("\nHost Mode.\n");
break;
}
case eUSBModeDevice:
{
UARTprintf("\nDevice Mode.\n");
break;
}
case eUSBModeNone:
{
UARTprintf("\nIdle Mode.\n");
break;
}
default:
{
UARTprintf("ERROR: Bad Mode!\n");
break;
}
}
}
//*****************************************************************************
//
// This is the callback from the USB HID keyboard handler.
//
// \param psKbInstance is ignored by this function.
// \param ui32Event is one of the valid events for a keyboard device.
// \param ui32MsgParam is defined by the event that occurs.
// \param pvMsgData is a pointer to data that is defined by the event that
// occurs.
//
// This function will be called to inform the application when a keyboard has
// been plugged in or removed and any time a key is pressed or released.
//
// \return None.
//
//*****************************************************************************
void
KeyboardCallback(tUSBHKeyboard *psKbInstance, uint32_t ui32Event,
uint32_t ui32MsgParam, void *pvMsgData)
{
char cChar;
switch(ui32Event)
{
//
// New Key press detected.
//
case USBH_EVENT_HID_KB_PRESS:
{
//
// If this was a Caps Lock key then update the Caps Lock state.
//
if(ui32MsgParam == HID_KEYB_USAGE_CAPSLOCK)
{
//
// The main loop needs to update the keyboard's Caps Lock
// state.
//
g_eUSBState = STATE_KEYBOARD_UPDATE;
//
// Toggle the current Caps Lock state.
//
g_ui32Modifiers ^= HID_KEYB_CAPS_LOCK;
//
// Update the screen based on the Caps Lock status.
//
UpdateStatus();
}
else if(ui32MsgParam == HID_KEYB_USAGE_SCROLLOCK)
{
//
// The main loop needs to update the keyboard's Scroll Lock
// state.
//
g_eUSBState = STATE_KEYBOARD_UPDATE;
//
// Toggle the current Scroll Lock state.
//
g_ui32Modifiers ^= HID_KEYB_SCROLL_LOCK;
}
else if(ui32MsgParam == HID_KEYB_USAGE_NUMLOCK)
{
//
// The main loop needs to update the keyboard's Scroll Lock
// state.
//
g_eUSBState = STATE_KEYBOARD_UPDATE;
//
// Toggle the current Num Lock state.
//
g_ui32Modifiers ^= HID_KEYB_NUM_LOCK;
}
else
{
//
// Was this the backspace key?
//
if((uint8_t)ui32MsgParam == HID_KEYB_USAGE_BACKSPACE)
{
//
// Yes - set the ASCII code for a backspace key. This is
// not returned by USBHKeyboardUsageToChar since this only
// returns printable characters.
//
cChar = ASCII_BACKSPACE;
}
else
{
//
// This is not backspace so try to map the usage code to a
// printable ASCII character.
//
cChar = (char)
USBHKeyboardUsageToChar(g_psKeyboardInstance,
&g_sUSKeyboardMap,
(uint8_t)ui32MsgParam);
}
//
// A zero value indicates there was no textual mapping of this
// usage code.
//
if(cChar != 0)
{
PrintChar(cChar);
}
}
break;
}
case USBH_EVENT_HID_KB_MOD:
{
//
// This application ignores the state of the shift or control
// and other special keys.
//
break;
}
case USBH_EVENT_HID_KB_REL:
{
//
// This applications ignores the release of keys as well.
//
break;
}
}
}
//*****************************************************************************
//
// This function returns the number of ticks since the last time this function
// was called.
//
//*****************************************************************************
uint32_t
GetTickms(void)
{
uint32_t ui32RetVal;
uint32_t ui32Saved;
ui32RetVal = g_ui32SysTickCount;
ui32Saved = ui32RetVal;
if(ui32Saved > g_ui32LastTick)
{
ui32RetVal = ui32Saved - g_ui32LastTick;
}
else
{
ui32RetVal = g_ui32LastTick - ui32Saved;
}
//
// This could miss a few milliseconds but the timings here are on a
// much larger scale.
//
g_ui32LastTick = ui32Saved;
//
// Return the number of milliseconds since the last time this was called.
//
return(ui32RetVal * MS_PER_SYSTICK);
}
//*****************************************************************************
//
// Configure the UART and its pins. This must be called before UARTprintf().
//
//*****************************************************************************
void
ConfigureUART(void)
{
//
// Enable the GPIO Peripheral used by the UART.
//
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
//
// Enable UART0
//
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
//
// Configure GPIO Pins for UART mode.
//
ROM_GPIOPinConfigure(GPIO_PA0_U0RX);
ROM_GPIOPinConfigure(GPIO_PA1_U0TX);
ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
//
// Use the internal 16MHz oscillator as the UART clock source.
//
UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
//
// Initialize the UART for console I/O.
//
UARTStdioConfig(0, 115200, 16000000);
}
//*****************************************************************************
//
// This is the main loop that runs the application.
//
//*****************************************************************************
int
main(void)
{
tRectangle sRect;
tUSBMode eLastMode;
char *pcString;
//
// Enable lazy stacking for interrupt handlers. This allows floating-point
// instructions to be used within interrupt handlers, but at the expense of
// extra stack usage.
//
ROM_FPULazyStackingEnable();
//
// Set the system clock to run at 50MHz from the PLL.
//
ROM_SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
SYSCTL_XTAL_16MHZ);
//
// Initially wait for device connection.
//
g_eUSBState = STATE_NO_DEVICE;
eLastMode = eUSBModeOTG;
g_eCurrentUSBMode = eUSBModeOTG;
//
// Enable Clocking to the USB controller.
//
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_USB0);
//
// Configure the required pins for USB operation.
//
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG);
ROM_GPIOPinConfigure(GPIO_PG4_USB0EPEN);
ROM_GPIOPinTypeUSBDigital(GPIO_PORTG_BASE, GPIO_PIN_4);
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOL);
ROM_GPIOPinTypeUSBAnalog(GPIO_PORTL_BASE, GPIO_PIN_6 | GPIO_PIN_7);
ROM_GPIOPinTypeUSBAnalog(GPIO_PORTB_BASE, GPIO_PIN_0 | GPIO_PIN_1);
//
// Configure SysTick for a 100Hz interrupt.
//
ROM_SysTickPeriodSet(ROM_SysCtlClockGet() / TICKS_PER_SECOND);
ROM_SysTickEnable();
ROM_SysTickIntEnable();
//
// Enable Interrupts
//
ROM_IntMasterEnable();
//
// Configure UART0 for debug output.
//
ConfigureUART();
//
// Initialize the USB stack mode and pass in a mode callback.
//
USBStackModeSet(0, eUSBModeOTG, ModeCallback);
//
// Register the host class drivers.
//
USBHCDRegisterDrivers(0, g_ppHostClassDrivers, g_ui32NumHostClassDrivers);
//
// Open an instance of the keyboard driver. The keyboard does not need
// to be present at this time, this just save a place for it and allows
// the applications to be notified when a keyboard is present.
//
g_psKeyboardInstance = USBHKeyboardOpen(KeyboardCallback, g_pui8Buffer,
KEYBOARD_MEMORY_SIZE);
//
// Initialize the power configuration. This sets the power enable signal
// to be active high and does not enable the power fault.
//
USBHCDPowerConfigInit(0, USBHCD_VBUS_AUTO_HIGH | USBHCD_VBUS_FILTER);
//
// Initialize the USB controller for OTG operation with a 2ms polling
// rate.
//
USBOTGModeInit(0, 2000, g_pui8HCDPool, HCD_MEMORY_SIZE);
//
// Initialize the display driver.
//
CFAL96x64x16Init();
//
// Initialize the graphics context.
//
GrContextInit(&g_sContext, &g_sCFAL96x64x16);
//
// Fill the top part of the screen with blue to create the banner.
//
sRect.i16XMin = 0;
sRect.i16YMin = 0;
sRect.i16XMax = GrContextDpyWidthGet(&g_sContext) - 1;
sRect.i16YMax = (2 * DISPLAY_BANNER_HEIGHT) - 1;
GrContextForegroundSet(&g_sContext, DISPLAY_BANNER_BG);
GrRectFill(&g_sContext, &sRect);
//
// Change foreground for white text.
//
GrContextForegroundSet(&g_sContext, DISPLAY_TEXT_FG);
//
// Put the application name in the middle of the banner.
//
GrContextFontSet(&g_sContext, g_psFontFixed6x8);
GrStringDrawCentered(&g_sContext, "usb-host-", -1,
GrContextDpyWidthGet(&g_sContext) / 2, 4, 0);
GrStringDrawCentered(&g_sContext, "keyboard", -1,
GrContextDpyWidthGet(&g_sContext) / 2, 14, 0);
//
// Calculate the number of characters that will fit on a line.
// Make sure to leave a small border for the text box.
//
g_ui32CharsPerLine = (GrContextDpyWidthGet(&g_sContext) - 4) /
GrFontMaxWidthGet(g_psFontFixed6x8);
//
// Calculate the number of lines per usable text screen. This requires
// taking off space for the top and bottom banners and adding a small bit
// for a border.
//
g_ui32LinesPerScreen = (GrContextDpyHeightGet(&g_sContext) -
(3*(DISPLAY_BANNER_HEIGHT + 1)))/
GrFontHeightGet(g_psFontFixed6x8);
//
// Open and instance of the keyboard class driver.
//
UARTprintf("Host Keyboard Application\n");
//
// Initial update of the screen.
//
UpdateStatus();
//
// The main loop for the application.
//
while(1)
{
//
// Tell the OTG library code how much time has passed in
// milliseconds since the last call.
//
USBOTGMain(GetTickms());
//
// Has the USB mode changed since last time we checked?
//
if(g_eCurrentUSBMode != eLastMode)
{
//
// Remember the new mode.
//
eLastMode = g_eCurrentUSBMode;
switch(eLastMode)
{
case eUSBModeHost:
pcString = "HOST";
break;
case eUSBModeDevice:
pcString = "DEVICE";
break;
case eUSBModeNone:
pcString = "NONE";
break;
default:
pcString = "UNKNOWN";
break;
}
UARTprintf("USB mode changed to %s\n", pcString);
}
switch(g_eUSBState)
{
//
// This state is entered when they keyboard is first detected.
//
case STATE_KEYBOARD_INIT:
{
//
// Initialized the newly connected keyboard.
//
USBHKeyboardInit(g_psKeyboardInstance);
//
// Proceed to the keyboard connected state.
//
g_eUSBState = STATE_KEYBOARD_CONNECTED;
//
// Update the screen now that the keyboard has been
// initialized.
//
UpdateStatus();
USBHKeyboardModifierSet(g_psKeyboardInstance, g_ui32Modifiers);
break;
}
case STATE_KEYBOARD_UPDATE:
{
//
// If the application detected a change that required an
// update to be sent to the keyboard to change the modifier
// state then call it and return to the connected state.
//
g_eUSBState = STATE_KEYBOARD_CONNECTED;
USBHKeyboardModifierSet(g_psKeyboardInstance, g_ui32Modifiers);
break;
}
case STATE_KEYBOARD_CONNECTED:
{
//
// Nothing is currently done in the main loop when the keyboard
// is connected.
//
break;
}
case STATE_UNKNOWN_DEVICE:
{
//
// Nothing to do as the device is unknown.
//
break;
}
case STATE_NO_DEVICE:
{
//
// Nothing is currently done in the main loop when the keyboard
// is not connected.
//
break;
}
default:
{
break;
}
}
}
}
|