forked from felis/lightweight-usb-host
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.c
749 lines (727 loc) · 26.2 KB
/
cli.c
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
/* CLI functions */
#define _CLI_C_
#include <string.h>
#include "project_config.h"
#include "cli_constants.h"
extern DWORD uptime;
extern HID_DEVICE hid_device;
extern DEV_RECORD devtable[];
#define NUM_MENUS 6 //number of top-level menus
/* menu position in function pointer array */
static enum {
CLI_MAIN_MENU = 0,
CLI_SHOW_MENU = 1,
CLI_SET_MENU = 2,
CLI_USBQ_MENU = 3,
CLI_USBT_MENU = 4,
CLI_UTIL_MENU = 5
} cli_state = CLI_MAIN_MENU; //CLI states
/* array of function pointers to top-level menus */
static void ( * const rom top_menu[ NUM_MENUS ] )( void ) = {
CLI_main_menu,
CLI_show_menu,
CLI_set_menu,
CLI_usbq_menu,
CLI_usbt_menu,
CLI_util_menu
};
char bigbuf[ 256 ];
static BYTE devaddr = 1; //temporary assignment for the only device supported atm
/* Local prototypes */
BOOL prevCodeComp( BYTE data, BOOT_KBD_REPORT* buf );
BYTE HIDtoa( BOOT_KBD_REPORT* buf, BYTE index );
/* CLI main loop state machine */
void CLI_Task( void )
{
top_menu[ cli_state ](); //call top-level menu according to current state
return;
}
/* root level CLI */
/* switches state to next level menus */
void CLI_main_menu( void )
{
BYTE temp;
if(!CharInQueue()) {
return;
}
temp = recvchar();
if ( temp > 0x1f ) {
sendchar( temp ); //echo back
}
switch( temp ) {
case( 0x0d ): //"Enter"
send_string(cli_prompt_main);
break;
case( 0x1b ): //"ESC"
cli_state = CLI_MAIN_MENU;
send_string(cli_prompt_main);
break;
case( 0x31 ): //"1 - show menu"
cli_state = CLI_SHOW_MENU;
send_string(cli_prompt_show);
break;
case( 0x32 ): //"2 - set menu"
cli_state = CLI_SET_MENU;
send_string(cli_prompt_set);
break;
case( 0x33 ): //"3 - USB queries menu"
cli_state = CLI_USBQ_MENU;
send_string(cli_prompt_usbq);
break;
case( 0x34 ): //"4 - USB transfers menu"
cli_state = CLI_USBT_MENU;
send_string(cli_prompt_usbt);
break;
case( 0x35 ): //"5 - Utilities menu"
cli_state = CLI_UTIL_MENU;
send_string(cli_prompt_util);
break;
case( 0x3f ): //Question mark
send_string(cli_root_menu_help);
send_string(cli_prompt_main);
break;
default:
send_string(cli_invalid_key);
send_string(cli_prompt_main);
break;
} //end switch( temp )
}
/* show level CLI */
/* state CLI_SHOW_MENU */
void CLI_show_menu( void )
{
BYTE i;
BYTE temp;
DEV_RECORD* tpl_ptr;
if(!CharInQueue()) {
return;
}
temp = recvchar();
if ( temp > 0x1f ) {
sendchar( temp ); //echo back
}
switch( temp ) {
case( 0x0d ): //"Enter"
send_string(cli_prompt_show);
break;
case( 0x1b ): //"ESC"
cli_state = CLI_MAIN_MENU; //change state to "Main"
send_string(cli_prompt_main);
break;
case( 0x31 ): //"1 - print MAX3421E registers"
i = 0;
while( register_format[ i ].name != NULL ) {
send_string( register_format[i].name );
send_hexbyte( MAXreg_rd( register_format[i].number ));
i++;
}
send_string(cli_prompt_show);
break;
case( 0x32 ): //2 - Print USB task state
send_string("\r\nUSB task state: ");
send_hexbyte( GetUsbTaskState());
send_string(cli_prompt_show);
break;
case( 0x33 ): //3 - Print device table
for( i = 1; i < USB_NUMDEVICES; i++ ) {
tpl_ptr = GetDevtable( i );
if( tpl_ptr->epinfo != NULL ) {
send_string( crlf );
send_string("Device: ");
send_decword( i );
send_string( devclasses[ tpl_ptr->devclass ] );
send_string( crlf );
}
}//for( i = 1; i < USB_NUMDEVICES; i++
send_string(cli_prompt_show);
break;
case( 0x3f ): //Question mark
send_string(cli_show_menu_help);
send_string(esc_prev_lvl);
send_string(cli_prompt_show);
break;
default:
send_string(cli_invalid_key);
send_string(cli_prompt_show);
break;
} //end switch( temp )
}
/* SET level CLI */
/* state CLI_SET_MENU */
void CLI_set_menu( void )
{
BYTE temp;
BYTE rcode;
if(!CharInQueue()) {
return;
}
temp = recvchar();
if ( temp > 0x1f ) {
sendchar( temp ); //echo back
}
switch( temp ) {
case( 0x0d ): //"Enter"
send_string(cli_prompt_set);
break;
case( 0x1b ): //"ESC"
cli_state = CLI_MAIN_MENU; //change state to "Main"
send_string(cli_prompt_main);
break;
case( 0x31 ): //"1" - reset MAX3421E
MAX3421E_reset();
send_string ("\r\nMAX3421E reset\r\n");
break;
case( 0x32 ): //"2" - turn Vbus on
if (!Vbus_power( ON )) {
send_string ("\r\nVbus overload");
}
else {
send_string("\r\nVbus is on\r\n");
}
send_string(cli_set_menu_help);
break;
case( 0x33 ): //"3" - turn Vbus off
Vbus_power( OFF );
send_string("\r\nVbus is off\r\n");
send_string(cli_set_menu_help);
break;
case( 0x34 ): //set HID boot protocol
rcode = XferSetProto( 1, 0, hid_device.interface, BOOT_PROTOCOL );
if( rcode ) { //error handling
send_string("\r\nError setting boot protocol. Rcode ");
send_hexbyte( rcode );
}
else {
send_string("\r\nBoot protocol set.");
}
send_string(cli_prompt_set);
break;
case( 0x35 ): //set HID report protocol
rcode = XferSetProto( 1, 0, hid_device.interface, RPT_PROTOCOL );
if( rcode ) { //error handling
send_string("\r\nError setting report protocol. Rcode ");
send_hexbyte( rcode );
}
else {
send_string("\r\nReport protocol set.");
}
send_string(cli_prompt_set);
break;
case( 0x3f ): //Question mark
send_string(cli_set_menu_help);
send_string(esc_prev_lvl);
send_string(cli_prompt_set);
break;
default:
send_string(cli_invalid_key);
send_string(cli_prompt_set);
break;
} //end switch( temp )
}
/* usb queries menu */
void CLI_usbq_menu( void )
{
BYTE temp;
if(!CharInQueue()) {
return;
}
temp = recvchar();
if ( temp > 0x1f ) {
sendchar( temp ); //echo back
}
switch( temp ) {
case( 0x0d ): //"Enter"
send_string(cli_prompt_usbq);
break;
case( 0x1b ): //"ESC"
cli_state = CLI_MAIN_MENU; //change state to "Main"
send_string(cli_prompt_main);
break;
case( 0x31 ): //"1" - print device descriptor
printDevDescr( 1 );
send_string(cli_prompt_usbq);
break;
case( 0x32 ):
printConfDescr( 1, 0 ); //"2" - print configuration
send_string(cli_prompt_usbq);
break;
case( 0x33 ): //"3" -
break;
case( 0x3f ): //Question mark
send_string(cli_usbq_menu_help);
send_string(esc_prev_lvl);
send_string(cli_prompt_usbq);
break;
default:
send_string(cli_invalid_key);
send_string(cli_prompt_usbq);
break;
} //end switch( temp )
}
void CLI_usbt_menu( void )
{
BYTE temp;
if(!CharInQueue()) {
return;
}
temp = recvchar();
if ( temp > 0x1f ) {
sendchar( temp ); //echo back
}
switch( temp ) {
case( 0x0d ): //"Enter"
send_string(cli_prompt_usbt);
break;
case( 0x1b ): //"ESC"
cli_state = CLI_MAIN_MENU; //change state to "Main"
send_string(cli_prompt_main);
break;
case( 0x31 ): //"1" - test mouse
testMouse( 1 );
break;
case( 0x32 ): //"2" - test keyboard
testKbd( 1 );
break;
case( 0x33 ): //"3" -
break;
case( 0x3f ): //Question mark
send_string(cli_usbt_menu_help);
send_string(esc_prev_lvl);
send_string(cli_prompt_usbt);
break;
default:
send_string(cli_invalid_key);
send_string(cli_prompt_usbt);
break;
} //end switch( temp )
}
/* util level CLI */
/* state CLI_UTIL_MENU */
void CLI_util_menu( void )
{
BYTE temp;
if(!CharInQueue())
return;
temp = recvchar();
if ( temp > 0x1f )
sendchar( temp ); //echo back
send_string( cli_prompt_util);
switch( temp ) {
case( 0x0d ): //"Enter"
//send_string(cli_prompt_util);
break;
case( 0x1b ): //"ESC"
cli_state = CLI_MAIN_MENU; //change state to "Main"
send_string(cli_prompt_main);
break;
case( 0x31 ): //"1 - test SPI"
SPI_test();
send_string( cli_prompt_util );
break;
case( 0x32 ):
//print_registers(); //"2 - show registers"
//send_string(cli_prompt_show);
break;
case( 0x3f ): //Question mark
send_string(cli_util_menu_help);
send_string(esc_prev_lvl);
send_string(cli_prompt_util);
break;
default:
send_string(cli_invalid_key);
send_string(cli_prompt_util);
break;
} //end switch( temp )
}
void CLI_init( void )
{
send_string(cli_banner);
send_string(cli_root_menu_help);
send_string(cli_prompt_main);
}
/* prints device descriptor */
void printDevDescr( BYTE addr )
{
BYTE rcode;
USB_DEVICE_DESCRIPTOR buf;
send_string( crlf );
rcode = XferGetDevDescr( addr, 0, DEV_DESCR_LEN, ( char *)&buf ); //get device descriptor
if( rcode ) {
send_string("\r\nDevice descriptor request error. Return code: ");
send_hexbyte( rcode );
send_string( crlf );
return;
}
/* First line */
send_string("Vendor ID: ");
send_hexbyte( HIBYTE( buf.idVendor ));
send_hexbyte( LOBYTE( buf.idVendor ));
send_string(" Product ID: ");
send_hexbyte( HIBYTE( buf.idProduct ));
send_hexbyte( LOBYTE( buf.idProduct ));
send_string(" Rev.: ");
send_decword(( WORD )HIBYTE( buf.bcdDevice ));
sendchar('.');
send_decword(( WORD )LOBYTE( buf.bcdDevice ));
send_string(" USB version: ");
send_decword(( WORD )HIBYTE( buf.bcdUSB ));
sendchar('.');
send_decword(( WORD )LOBYTE( buf.bcdUSB ));
/* Second line */
send_string("\r\nClass: ");
send_hexbyte( buf.bDeviceClass );
send_string(" Subclass: ");
send_hexbyte( buf.bDeviceSubClass );
send_string(" Protocol: ");
send_hexbyte( buf.bDeviceProtocol );
/* Third line */
send_string("\r\nMax.packet size: ");
send_decword(( WORD )buf.bMaxPacketSize0 );
send_string(" bytes. Number of configurations: ");
send_decword(( WORD )buf.bNumConfigurations );
}
/* Configuraton descriptor parser/printer */
/* Makes sure configuration is less than 256 bytes which may not be the case */
/* for very complex devices */
void printConfDescr( BYTE addr, BYTE conf )
{
WORD totallength;
BYTE rcode;
USB_DESCR *data_ptr = ( USB_DESCR * )bigbuf; //pointer to any descriptor
char *byte_ptr = bigbuf;
rcode = XferGetConfDescr( addr, 0, CONF_DESCR_LEN, conf, bigbuf ); //get configuration descriptor
if( rcode ) {
send_string("\r\nConfiguration descriptor request error 01. Return code: ");
send_hexbyte( rcode );
send_string( crlf );
return;
}
//data_ptr = ( USB_DESCR * )bigbuf;
totallength = data_ptr->descr.config.wTotalLength; //get total length of configuration
if( totallength > 256 ) {
send_string("\r\nConfiguration descriptor data length error. Total length: ");
send_decword( totallength );
send_string( crlf );
return;
}
rcode = XferGetConfDescr( addr, 0, totallength, conf, bigbuf ); //get the whole configuration
if( rcode ) {
send_string("\r\nConfiguration descriptor request error 02. Return code: ");
send_hexbyte( rcode );
send_string( crlf );
return;
}
/* Print configuration descriptor */
send_string("\r\nConfiguration descriptor\r\n");
/* First line */
send_string("\r\nTotal configuration size: ");
send_decword( totallength );
send_string(" bytes. Max.power: ");
send_decword( 2 * data_ptr->descr.config.bMaxPower ); //max.value of this field is 250
send_string(" ma. Number of interfaces: ");
send_decword( data_ptr->descr.config.bNumInterfaces );
/* Second line */
send_string("\r\nAttributes: ");
if( data_ptr->descr.config.bmAttributes & USB_CFG_DSC_SELF_PWR ) {
send_string("Self-powered ");
}
if( data_ptr->descr.config.bmAttributes & USB_CFG_DSC_REM_WAKE ) {
send_string("Remote wakeup ");
}
send_string(". Configuration value: ");
send_decword( data_ptr->descr.config.bConfigurationValue );
send_string( crlf );
/* Finish printing configuration descriptor. Parse the rest */
byte_ptr = byte_ptr + CONF_DESCR_LEN;
while( byte_ptr < ( bigbuf + totallength )) {
data_ptr = ( USB_DESCR * )byte_ptr;
switch( data_ptr->descr.config.bDescriptorType ) {
case( USB_DESCRIPTOR_INTERFACE ):
printIntrDescr( byte_ptr );
byte_ptr = byte_ptr + INTR_DESCR_LEN;
break;
case( USB_DESCRIPTOR_ENDPOINT ):
printEpDescr( byte_ptr );
byte_ptr = byte_ptr + EP_DESCR_LEN;
break;
case( HID_DESCRIPTOR_HID ): //HID descriptor is variable length
printHIDdescr( byte_ptr );
byte_ptr = byte_ptr + data_ptr->descr.config.bLength;
break;
default: //unknown descriptor
send_string("Unknown descriptor: ");
send_hexbyte( data_ptr->descr.config.bDescriptorType );
send_string( crlf );
byte_ptr = byte_ptr + data_ptr->descr.config.bLength;
break;
}//switch( data_ptr->swcr.config.bDescriptorType
}
}
/* prints interface descriptor */
void printIntrDescr( char *byteptr )
{
USB_DESCR* dataptr = ( USB_DESCR* )byteptr;
send_string("\r\nInterface Descriptor\r\n");
/* First line */
send_string("Interface number: ");
send_decword( dataptr->descr.interface.bInterfaceNumber );
send_string(" Alternate setting: ");
send_decword( dataptr->descr.interface.bAlternateSetting );
send_string(" Number of endpoints: ");
send_decword( dataptr->descr.interface.bNumEndpoints );
/* Second line */
send_string("\r\nClass: ");
send_hexbyte( dataptr->descr.interface.bInterfaceClass );
send_string(" Subclass: ");
send_hexbyte( dataptr->descr.interface.bInterfaceSubClass );
send_string(" Protocol: ");
send_hexbyte( dataptr->descr.interface.bInterfaceProtocol );
send_string( crlf );
}
/* prints endpoint descriptor */
void printEpDescr( char* byteptr )
{
USB_DESCR* dataptr = ( USB_DESCR* )byteptr;
send_string("\r\nEndpoint Descriptor\r\n");
/* First line */
send_string("Endpoint number: ");
send_hexbyte( dataptr->descr.endpoint.bEndpointAddress & 0x0f );
send_string(" Direction: ");
if( dataptr->descr.endpoint.bEndpointAddress & 0x80 ) {
send_string("IN");
}
else {
send_string("OUT");
}
/* Second line */
send_string("\r\nAttributes: ");
send_hexbyte( dataptr->descr.endpoint.bmAttributes );
send_string(" Max.packet size: ");
send_decword( dataptr->descr.endpoint.wMaxPacketSize );
send_string(" Polling interval: ");
send_decword( dataptr->descr.endpoint.bInterval );
send_string( crlf );
}
/* prints HID descriptor. Won't parse past first( i.e. report ) descriptor fields, */
/* but will show correct length and descriptor number */
void printHIDdescr( char* byteptr )
{
USB_DESCR* dataptr = ( USB_DESCR* )byteptr;
send_string("\r\nHID Descriptor\r\n");
/* First line */
send_string("Descriptor length: ");
send_decword( dataptr->descr.HID.bLength );
send_string(" bytes. HID version: ");
send_decword(( WORD )HIBYTE( dataptr->descr.HID.bcdHID ));
sendchar('.');
send_decword(( WORD )LOBYTE( dataptr->descr.HID.bcdHID ));
send_string(" Country code: ");
send_hexbyte( dataptr->descr.HID.bCountryCode );
/* Second line */
send_string("\r\nNumber of descriptors: ");
send_decword( dataptr->descr.HID.bNumDescriptors );
/* Third line */
send_string("\r\nDescriptor type: ");
send_hexbyte( dataptr->descr.HID.bDescrType );
send_string(" Descriptor length: ");
send_decword( dataptr->descr.HID.wDescriptorLength );
if( dataptr->descr.HID.bLength > 9 ) {
send_string("Skipping the rest of the descriptor...\r\n");
}
send_string( crlf );
}
/* test mouse communication */
/* coordinates report work */
/* Issue: in boot protocol mode buttons are not reported. Button press, however, generates an update. I can't find boot protocol description for mouse, */
/* so maybe it's the way it should be */
void testMouse ( BYTE addr )
{
//DWORD delay;
BYTE rcode;
char tmpbyte;
BOOT_MOUSE_REPORT buf;
rcode = XferGetIdle( addr, 0, hid_device.interface, 0, &tmpbyte );
if( rcode ) { //error handling
send_string("\r\nGetIdle Error. Error code ");
send_hexbyte( rcode );
}
else {
send_string("\r\nUpdate rate: ");
send_decword( tmpbyte );
}
send_string("\r\nProtocol: ");
rcode = XferGetProto( addr, 0, hid_device.interface, &tmpbyte );
if( rcode ) { //error handling
send_string("\r\nGetProto Error. Error code ");
send_hexbyte( rcode );
}
else {
send_decword( tmpbyte );
send_string( crlf );
}
/* Polling interrupt endpoint */
while( !CharInQueue() ) {
//delay = uptime + 5;
//while( uptime < delay ); //wait polling interval
rcode = mousePoll( &buf );
if( rcode == hrNAK ) { //NAK means no new data
continue;
}
if( rcode ) {
send_string("\r\nRcode: ");
send_hexbyte( rcode );
}
send_string("\r\nX displacement: ");
send_decword( buf.Xdispl );
send_string(" Y displacement: ");
send_decword( buf.Ydispl );
send_string(" Buttons: ");
send_hexbyte( buf.button );
}
}
/* keyboard communication demo */
/* only basic functions/keys are supported */
void testKbd( BYTE addr )
{
char i;
BYTE rcode;
char tmpbyte;
char* byteptr;
BOOT_KBD_REPORT kbdbuf;
BOOT_KBD_REPORT localbuf;
rcode = XferGetIdle( addr, 0, hid_device.interface, 0, &tmpbyte );
if( rcode ) { //error handling
send_string("\r\nGetIdle Error. Error code ");
send_hexbyte( rcode );
}
else {
send_string("\r\nUpdate rate: ");
send_decword( tmpbyte );
}
send_string("\r\nProtocol: ");
rcode = XferGetProto( addr, 0, hid_device.interface, &tmpbyte );
if( rcode ) { //error handling
send_string("\r\nGetProto Error. Error code ");
send_hexbyte( rcode );
}
else {
send_decword( tmpbyte );
send_string( crlf );
}
send_string("\r\nType something on PIC keyboard. Press any key on PC keyboard to stop.\r\n");
/* Polling interrupt endpoint */
while( !CharInQueue() ) {
//delay = uptime + 5;
//while( uptime < delay ); //wait polling interval
rcode = kbdPoll( &kbdbuf );
if( rcode == hrNAK ) { //NAK means no new data
continue;
}
// byteptr = ( char *)&kbdbuf;
// send_string( crlf );
// for( i = 0; i < 8; i++ ) {
// send_hexbyte( *byteptr );
// byteptr++;
// }
//send_string( crlf );
for( i = 0; i < 6; i++ ) {
if( kbdbuf.keycode[ i ] == 0 ) { //empty position means it and all subsequent positions are empty
break;
}
if( prevCodeComp( kbdbuf.keycode[ i ], &localbuf ) == FALSE ) {
// send_hexbyte( kbdbuf.keycode[ i ] );
sendchar( HIDtoa( &kbdbuf, i ));
// send_string( crlf );
}
}
memcpy(( far char* )&localbuf, ( const far char* )&kbdbuf, sizeof( BOOT_KBD_REPORT ));
}//while(CharInQueue()...
}
/* function compares a key code with keycodes from previous report */
/* returns TRUE if match is found */
BOOL prevCodeComp( BYTE data, BOOT_KBD_REPORT* buf )
{
BYTE i;
for( i = 0; i < 6; i++ ) {
if( buf->keycode[ i ] == data ) {
return( TRUE );
}
}
return( FALSE );
}
/* function to convert HID keyboard code to ASCII */
BYTE HIDtoa( BOOT_KBD_REPORT* buf, BYTE index )
{
BYTE HIDcode = buf->keycode[ index ];
//BYTE AsciiVal;
//BYTE ShiftkeyStatus = 0;
/* symbols a-z,A-Z */
if( HIDcode >= 0x04 && HIDcode <= 0x1d ) {
if( buf->mod.LShift || buf->mod.RShift ) { //uppercase
return( HIDcode + 0x3d );
}
if( buf->mod.LCtrl || buf->mod.RCtrl ) { //Ctrl
return( HIDcode - 3 );
}
return( HIDcode + 0x5d ); //lowercase
}
/* Numbers 1-9,0 */
if( HIDcode >= 0x1e && HIDcode <= 0x27 ) {
if( buf->mod.LShift || buf->mod.RShift ) { //uppercase
switch( HIDcode ) {
case( 0x1f ): //HID code for '2'
return('@');
case( 0x23 ): //HID code for '6'
return('^');
case( 0x24 ): //HID code for '7'
return('&');
case( 0x25 ): //HID code for '8'
return('*');
case( 0x26 ): //HID code for '9'
return('(');
case( 0x27 ): //HID code for '0'
return(')');
default: //1,3,4,5
return( HIDcode + 3 );
}
}
return( HIDcode + 0x13 );
}
/* Misc. non-modifiable keys in no particular order */
switch( HIDcode ) {
case( 0x28 ): //Enter
return( 0x0d ); //CR
case( 0x29 ): //ESC
return( 0x1b ); //ESC
case( 0x2c ): //spacebar
return( 0x20 ); //
case( 0x36 ): //comma
return(',');
case( 0x37 ): //dot
return('.');
}
return( 0x07 ); //Bell
}
/* tests SPI transfers for errors. Cycles indefinitely until a key is pressed. */
/* Prints "." every 64K of transferred data */
void SPI_test( void )
{
BYTE i, j, gpinpol_copy;
gpinpol_copy = MAXreg_rd( rGPINPOL ); //save GPINPOL
send_string("Testing SPI transfers. Press any key to stop.\r\n");
while( !CharInQueue()) { //loop until any key is pressed
for( i = 0; i < 255; i++ ) {
MAXreg_wr( rGPINPOL, i );
if( MAXreg_rd( rGPINPOL ) != i ) {
send_string("SPI transmit/receive mismatch\r\n");
return;
}
}
j++;
if ( j == 0 )
send_string(".");
}
i = recvchar();
MAXreg_wr( rGPINPOL, gpinpol_copy );
}
//end cli.c