-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnabu_keyboard_usb.c
1336 lines (1190 loc) · 32.5 KB
/
nabu_keyboard_usb.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
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
/*-
* Copyright (c) 2022 Jason R. Thorpe.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* NABU Keyboard to USB Adapter
*
* This interfaces a NABU PC keyboard as a USB keyboard device
* with the standard US layout.
*
* For the most part, the NABU keyboard reports keys as ASCII characters.
* There are no key-down or key-up events except for some special keys.
* Shift, Control, and CapsLock are not themselves reported. The keyboard
* itself implements auto-repeat.
*
* Joystick data is also reported by the NABU keyboard.
*
* The NABU keyboard physical layer is RS422 8N1 @ 6992 baud.
* (Yes, it's weird, because it's derived from the 3.58MHz NTSC
* colorburst frequency - 3.58MHz input to 6803 which internally
* divides by 4 to generate E, and the internal UART is configured
* to use the /128 clock divisor to get the baud clock.)
*
* We use UART1 on the Pico to receive data from the keyboard. UART0
* is used as the console port for debugging purposes.
*
* TODO:
* - Handle the host requesting Boot protocol (rather than Report protocol).
*/
/* Pico SDK headers */
#include "pico/stdlib.h"
#include "pico/printf.h"
#include "pico/sync.h"
#include "pico/time.h"
#include "pico/multicore.h"
#include "hardware/uart.h"
/* TinyUSB SDK headers */
#include "bsp/board.h"
#include "tusb.h"
/* Standard headers */
#include <string.h>
/* Local headers */
/*
* GP22 (physical pin 29 on the DIP-40 Pico) is a debug-enable strapping
* pin that we sample when we launch. It's pulled-up internally; strap
* to ground to enable debug messages.
*/
#define DEBUG_STRAP_PIN 22
static bool debug_enabled;
#define debug_printf(...) \
do { \
if (debug_enabled) { \
printf(__VA_ARGS__); \
} \
} while (/*CONSTCOND*/0)
/*
* GPIO pins 4 and 5 are used for UART1 TX and RX, respectively.
* This maps to physical pins 6 and 7 on the DIP-40 Pico.
*/
#define UART1_TX_PIN 4
#define UART1_RX_PIN 5
/*
* GP26 (physical pin 31 on the DIP-40 Pico) is connected to the gate
* of a power MOSFET that sits between the keyboard V- and GND. Driving
* GP26 high completes the keyboard power supply circuit powers it on.
*/
#define PWREN_PIN 26
/*
* Circular queue between the the UART receiver and the USB sender.
*/
#define QUEUE_SIZE 64
#define QUEUE_MASK (QUEUE_SIZE - 1)
#define QUEUE_NEXT(n) (((n) + 1) & QUEUE_MASK)
#define QUEUE_EMPTY_P(q) ((q)->cons == (q)->prod)
#define QUEUE_FULL_P(q) (QUEUE_NEXT((q)->prod) == (q)->cons)
struct queue {
mutex_t mutex;
unsigned int prod;
unsigned int cons;
uint8_t data[QUEUE_SIZE];
};
static void
queue_init(struct queue *q)
{
memset(q, 0, sizeof(*q));
mutex_init(&q->mutex);
}
static bool
queue_add(struct queue *q, uint8_t v)
{
bool rv = true; /* "OK!" is the common-case. */
mutex_enter_blocking(&q->mutex);
if (! QUEUE_FULL_P(q)) {
q->data[q->prod] = v;
q->prod = QUEUE_NEXT(q->prod);
} else {
rv = false;
}
mutex_exit(&q->mutex);
return rv;
}
static bool
queue_consume(struct queue *q, uint8_t *vp, bool advance)
{
bool rv = false;
mutex_enter_blocking(&q->mutex);
if (! QUEUE_EMPTY_P(q)) {
*vp = q->data[q->cons];
if (advance) {
q->cons = QUEUE_NEXT(q->cons);
}
rv = true;
}
mutex_exit(&q->mutex);
return rv;
}
static bool
queue_peek(struct queue *q, uint8_t *vp)
{
return queue_consume(q, vp, false);
}
static bool
queue_get(struct queue *q, uint8_t *vp)
{
return queue_consume(q, vp, true);
}
static void
queue_drain(struct queue *q)
{
mutex_enter_blocking(&q->mutex);
q->prod = q->cons = 0;
mutex_exit(&q->mutex);
}
static bool suspended = false;
static bool mounted = false;
static bool want_remote_wakeup = false;
static bool have_nabu = false;
/*
* LED blinking patterns. Even indices are ON time, odd indices are
* OFF time. -1 means "go back to beginning".
*/
/* 250ms on, 250ms off */
static const int ledseq_not_mounted[] = {
250, 250, -1
};
/* 1000ms on, 1000ms off */
static const int ledseq_wait_nabu[] = {
1000, 1000, -1
};
/* Heartbeat pattern. */
static const int ledseq_healthy[] = {
100, 300, 100, 1000, -1
};
/* 2500ms on, 2500ms off */
static const int ledseq_suspended[] = {
2500, 2500, -1
};
struct {
const int *sequence;
uint idx;
uint32_t start_ms;
bool state;
} led_context;
static void
led_set_sequence(const int *seq)
{
if (led_context.sequence == seq) {
return;
}
led_context.sequence = seq;
led_context.idx = 0;
led_context.start_ms = board_millis();
led_context.state = true;
board_led_write(led_context.state);
}
static void
led_select_sequence(void)
{
if (led_context.sequence == NULL) {
return;
}
if (!mounted) {
led_set_sequence(ledseq_not_mounted);
return;
}
if (suspended && !want_remote_wakeup) {
led_set_sequence(ledseq_suspended);
return;
}
if (have_nabu) {
led_set_sequence(ledseq_healthy);
return;
}
led_set_sequence(ledseq_wait_nabu);
}
static void
led_task(uint32_t now)
{
int interval;
if (led_context.sequence == NULL) {
return;
}
interval = led_context.sequence[led_context.idx];
if (now - led_context.start_ms < interval) {
return;
}
led_context.start_ms += interval;
if ((interval = led_context.sequence[++led_context.idx]) == -1) {
interval = led_context.sequence[0];
led_context.idx = 0;
}
led_context.state ^= true;
board_led_write(led_context.state);
}
/*
* Map NABU keycodes to HID key codes.
*
* The HID Report array sends a report for each modifier key, in the
* seqence they are pressed / released. So, an 'A' is:
*
* Shift, Shift + A, Shift, none
*
* We encode these sequences directly in the map. The final entry in
* each sequence is always 0. For keys where we get individual Down/Up
* events from the NABU keyboard, we don't use sequences, we just send
* the individual event (those keys aren't affected by modifiers).
*
* Unassigned entries get 0, which conveniently is HID_KEY_NONE. N.B.
* the NABU keyboard reader thread won't even enqueue keystroke events
* for these unassigned keys.
*/
#define M_CTRL 0x0100 /* KEYBOARD_MODIFIER_LEFTCTRL << 8 */
#define M_SHIFT 0x0200 /* KEYBOARD_MODIFIER_LEFTSHIFT << 8 */
#define M_ALT 0x0400 /* KEYBOARD_MODIFIER_LEFTALT << 8 */
#define M_META 0x0800 /* KEYBOARD_MODIFIER_LEFTGUI << 8 */
#define M_DOWN 0x1000
#define M_UP 0x2000
#define M_ENDSEQ 0x4000
#define M_HIDKEY(m) ((m) & 0x00ff)
#define M_MODS(m) ((m) & 0x0f00)
#define NABU_CODE_JOY0 0x80
#define NABU_CODE_JOY1 0x81
#define NABU_CODE_ERR_FIRST 0x90
#define NABU_CODE_ERR_LAST 0x95
#define NABU_CODE_JOYDAT_FIRST 0xa0
#define NABU_CODE_JOYDAT_LAST 0xbf
#define NABU_CODE_JOYDAT_P(c) ((c) >= NABU_CODE_JOYDAT_FIRST && \
(c) <= NABU_CODE_JOYDAT_LAST)
#define NABU_CODE_ERR_P(c) ((c) >= NABU_CODE_ERR_FIRST && \
(c) <= NABU_CODE_ERR_LAST)
#define NABU_CODE_ERR_MKEY 0x90 /* multiple keys pressed */
#define NABU_CODE_ERR_RAM 0x91 /* faulty keyboard RAM */
#define NABU_CODE_ERR_ROM 0x92 /* faulty keyboard ROM */
#define NABU_CODE_ERR_ISR 0x93 /* illegal ISR (?) */
#define NABU_CODE_ERR_PING 0x94 /* periodic no-load ping */
#define NABU_CODE_ERR_RESET 0x95 /* keyboard power-up/reset */
#define NABU_KBD_BAUDRATE 6992
struct codeseq {
uint16_t codes[6]; /* 0-terminated */
};
static const struct codeseq nabu_to_hid[256] = {
/*
* CTRL just lops off the 2 upper bits of the keycode
* on the NABU keyboard (except for C-'<' ??), but we
* simplify to C-a, C-c, etc.
*/
[0x00] = { { M_CTRL, /* C-'@' */
M_CTRL | M_SHIFT,
M_CTRL | M_SHIFT | HID_KEY_2,
M_CTRL | M_SHIFT,
M_CTRL } },
[0x01] = { { M_CTRL,
M_CTRL | HID_KEY_A,
M_CTRL } },
[0x02] = { { M_CTRL,
M_CTRL | HID_KEY_B,
M_CTRL } },
[0x03] = { { M_CTRL,
M_CTRL | HID_KEY_C,
M_CTRL } },
[0x04] = { { M_CTRL,
M_CTRL | HID_KEY_D,
M_CTRL } },
[0x05] = { { M_CTRL,
M_CTRL | HID_KEY_E,
M_CTRL } },
[0x06] = { { M_CTRL,
M_CTRL | HID_KEY_F,
M_CTRL } },
[0x07] = { { M_CTRL,
M_CTRL | HID_KEY_G,
M_CTRL } },
[0x08] = { { HID_KEY_BACKSPACE } }, /* Backspace */
[0x09] = { { HID_KEY_TAB } }, /* Tab */
[0x0a] = { { HID_KEY_ENTER } }, /* LF */
[0x0b] = { { M_CTRL,
M_CTRL | HID_KEY_K,
M_CTRL } },
[0x0c] = { { M_CTRL,
M_CTRL | HID_KEY_L,
M_CTRL } },
[0x0d] = { { HID_KEY_ENTER } }, /* CR */
[0x0e] = { { M_CTRL,
M_CTRL | HID_KEY_N,
M_CTRL } },
[0x0f] = { { M_CTRL,
M_CTRL | HID_KEY_O,
M_CTRL } },
[0x10] = { { M_CTRL,
M_CTRL | HID_KEY_P,
M_CTRL } },
[0x11] = { { M_CTRL,
M_CTRL | HID_KEY_Q,
M_CTRL } },
[0x12] = { { M_CTRL,
M_CTRL | HID_KEY_R,
M_CTRL } },
[0x13] = { { M_CTRL,
M_CTRL | HID_KEY_S,
M_CTRL } },
[0x14] = { { M_CTRL,
M_CTRL | HID_KEY_T,
M_CTRL } },
[0x15] = { { M_CTRL,
M_CTRL | HID_KEY_U,
M_CTRL } },
[0x16] = { { M_CTRL,
M_CTRL | HID_KEY_V,
M_CTRL } },
[0x17] = { { M_CTRL,
M_CTRL | HID_KEY_W,
M_CTRL } },
[0x18] = { { M_CTRL,
M_CTRL | HID_KEY_X,
M_CTRL } },
[0x19] = { { M_CTRL,
M_CTRL | HID_KEY_Y,
M_CTRL } },
[0x1a] = { { M_CTRL,
M_CTRL | HID_KEY_Z,
M_CTRL } },
[0x1b] = { { HID_KEY_ESCAPE } }, /* ESC */
[0x1c] = { { M_CTRL, /* C-'<' */
M_CTRL | M_SHIFT,
M_CTRL | M_SHIFT | HID_KEY_COMMA,
M_CTRL | M_SHIFT,
M_CTRL } },
[0x1d] = { { M_CTRL,
M_CTRL | HID_KEY_BRACKET_RIGHT,
M_CTRL } },
[0x1e] = { { M_CTRL, /* C-'^' */
M_CTRL | M_SHIFT,
M_CTRL | M_SHIFT | HID_KEY_6,
M_CTRL | M_SHIFT,
M_CTRL } },
[0x1f] = { { M_CTRL, /* C-'_' */
M_CTRL | M_SHIFT,
M_CTRL | M_SHIFT | HID_KEY_MINUS,
M_CTRL | M_SHIFT,
M_CTRL } },
[0x20] = { { HID_KEY_SPACE } },
[0x21] = { { M_SHIFT, /* ! */
M_SHIFT | HID_KEY_1,
M_SHIFT } },
[0x22] = { { M_SHIFT, /* " */
M_SHIFT | HID_KEY_APOSTROPHE,
M_SHIFT } },
[0x23] = { { M_SHIFT, /* # */
M_SHIFT | HID_KEY_3,
M_SHIFT } },
[0x24] = { { M_SHIFT, /* $ */
M_SHIFT | HID_KEY_4,
M_SHIFT } },
[0x25] = { { M_SHIFT, /* % */
M_SHIFT | HID_KEY_5,
M_SHIFT } },
[0x26] = { { M_SHIFT, /* & */
M_SHIFT | HID_KEY_7,
M_SHIFT } },
[0x27] = { { HID_KEY_APOSTROPHE } },
[0x28] = { { M_SHIFT, /* ( */
M_SHIFT | HID_KEY_9,
M_SHIFT } },
[0x29] = { { M_SHIFT, /* ) */
M_SHIFT | HID_KEY_0,
M_SHIFT } },
[0x2a] = { { M_SHIFT, /* * */
M_SHIFT | HID_KEY_8,
M_SHIFT } },
[0x2b] = { { M_SHIFT, /* + */
M_SHIFT | HID_KEY_EQUAL,
M_SHIFT } },
[0x2c] = { { HID_KEY_COMMA } }, /* , */
[0x2d] = { { HID_KEY_MINUS } }, /* - */
[0x2e] = { { HID_KEY_PERIOD } }, /* . */
[0x2f] = { { HID_KEY_SLASH } }, /* / */
[0x30] = { { HID_KEY_0 } },
[0x31] = { { HID_KEY_1 } },
[0x32] = { { HID_KEY_2 } },
[0x33] = { { HID_KEY_3 } },
[0x34] = { { HID_KEY_4 } },
[0x35] = { { HID_KEY_5 } },
[0x36] = { { HID_KEY_6 } },
[0x37] = { { HID_KEY_7 } },
[0x38] = { { HID_KEY_8 } },
[0x39] = { { HID_KEY_9 } },
[0x3a] = { { M_SHIFT, /* : */
M_SHIFT | HID_KEY_SEMICOLON,
M_SHIFT } },
[0x3b] = { { HID_KEY_SEMICOLON } },
[0x3c] = { { M_SHIFT, /* < */
M_SHIFT | HID_KEY_COMMA,
M_SHIFT } },
[0x3d] = { { HID_KEY_EQUAL } },
[0x3e] = { { M_SHIFT, /* > */
M_SHIFT | HID_KEY_PERIOD,
M_SHIFT } },
[0x3f] = { { M_SHIFT, /* ? */
M_SHIFT | HID_KEY_SLASH,
M_SHIFT } },
[0x40] = { { M_SHIFT, /* @ */
M_SHIFT | HID_KEY_2,
M_SHIFT } },
[0x41] = { { M_SHIFT,
M_SHIFT | HID_KEY_A,
M_SHIFT } },
[0x42] = { { M_SHIFT,
M_SHIFT | HID_KEY_B,
M_SHIFT } },
[0x43] = { { M_SHIFT,
M_SHIFT | HID_KEY_C,
M_SHIFT } },
[0x44] = { { M_SHIFT,
M_SHIFT | HID_KEY_D,
M_SHIFT } },
[0x45] = { { M_SHIFT,
M_SHIFT | HID_KEY_E,
M_SHIFT } },
[0x46] = { { M_SHIFT,
M_SHIFT | HID_KEY_F,
M_SHIFT } },
[0x47] = { { M_SHIFT,
M_SHIFT | HID_KEY_G,
M_SHIFT } },
[0x48] = { { M_SHIFT,
M_SHIFT | HID_KEY_H,
M_SHIFT } },
[0x49] = { { M_SHIFT,
M_SHIFT | HID_KEY_I,
M_SHIFT } },
[0x4a] = { { M_SHIFT,
M_SHIFT | HID_KEY_J,
M_SHIFT } },
[0x4b] = { { M_SHIFT,
M_SHIFT | HID_KEY_K,
M_SHIFT } },
[0x4c] = { { M_SHIFT,
M_SHIFT | HID_KEY_L,
M_SHIFT } },
[0x4d] = { { M_SHIFT,
M_SHIFT | HID_KEY_M,
M_SHIFT } },
[0x4e] = { { M_SHIFT,
M_SHIFT | HID_KEY_N,
M_SHIFT } },
[0x4f] = { { M_SHIFT,
M_SHIFT | HID_KEY_O,
M_SHIFT } },
[0x50] = { { M_SHIFT,
M_SHIFT | HID_KEY_P,
M_SHIFT } },
[0x51] = { { M_SHIFT,
M_SHIFT | HID_KEY_Q,
M_SHIFT } },
[0x52] = { { M_SHIFT,
M_SHIFT | HID_KEY_R,
M_SHIFT } },
[0x53] = { { M_SHIFT,
M_SHIFT | HID_KEY_S,
M_SHIFT } },
[0x54] = { { M_SHIFT,
M_SHIFT | HID_KEY_T,
M_SHIFT } },
[0x55] = { { M_SHIFT,
M_SHIFT | HID_KEY_U,
M_SHIFT } },
[0x56] = { { M_SHIFT,
M_SHIFT | HID_KEY_V,
M_SHIFT } },
[0x57] = { { M_SHIFT,
M_SHIFT | HID_KEY_W,
M_SHIFT } },
[0x58] = { { M_SHIFT,
M_SHIFT | HID_KEY_X,
M_SHIFT } },
[0x59] = { { M_SHIFT,
M_SHIFT | HID_KEY_Y,
M_SHIFT } },
[0x5a] = { { M_SHIFT,
M_SHIFT | HID_KEY_Z,
M_SHIFT } },
[0x5b] = { { HID_KEY_BRACKET_LEFT } }, /* [ */
/* 0x5c */
[0x5d] = { { HID_KEY_BRACKET_RIGHT } }, /* ] */
[0x5e] = { { M_SHIFT, /* ^ */
M_SHIFT | HID_KEY_6,
M_SHIFT } },
[0x5f] = { { M_SHIFT, /* _ */
M_SHIFT | HID_KEY_MINUS,
M_SHIFT } },
/* 0x60 */
[0x61] = { { HID_KEY_A } },
[0x62] = { { HID_KEY_B } },
[0x63] = { { HID_KEY_C } },
[0x64] = { { HID_KEY_D } },
[0x65] = { { HID_KEY_E } },
[0x66] = { { HID_KEY_F } },
[0x67] = { { HID_KEY_G } },
[0x68] = { { HID_KEY_H } },
[0x69] = { { HID_KEY_I } },
[0x6a] = { { HID_KEY_J } },
[0x6b] = { { HID_KEY_K } },
[0x6c] = { { HID_KEY_L } },
[0x6d] = { { HID_KEY_M } },
[0x6e] = { { HID_KEY_N } },
[0x6f] = { { HID_KEY_O } },
[0x70] = { { HID_KEY_P } },
[0x71] = { { HID_KEY_Q } },
[0x72] = { { HID_KEY_R } },
[0x73] = { { HID_KEY_S } },
[0x74] = { { HID_KEY_T } },
[0x75] = { { HID_KEY_U } },
[0x76] = { { HID_KEY_V } },
[0x77] = { { HID_KEY_W } },
[0x78] = { { HID_KEY_X } },
[0x79] = { { HID_KEY_Y } },
[0x7a] = { { HID_KEY_Z } },
[0x7b] = { { M_SHIFT, /* { */
M_SHIFT | HID_KEY_BRACKET_LEFT,
M_SHIFT } },
/* 0x7c */
[0x7d] = { { M_SHIFT, /* } */
M_SHIFT | HID_KEY_BRACKET_RIGHT,
M_SHIFT } },
/* 0x7e */
[0x7f] = { { HID_KEY_BACKSPACE } }, /* DEL */
/* 0x80 - 0x9f */
/* 0xa0 - 0xbf */
/* 0xc0 - 0xdf */
[0xe0] = { { M_DOWN | HID_KEY_ARROW_RIGHT } },
[0xe1] = { { M_DOWN | HID_KEY_ARROW_LEFT } },
[0xe2] = { { M_DOWN | HID_KEY_ARROW_UP } },
[0xe3] = { { M_DOWN | HID_KEY_ARROW_DOWN } },
[0xe4] = { { M_DOWN | HID_KEY_PAGE_DOWN } }, /* |||> */
[0xe5] = { { M_DOWN | HID_KEY_PAGE_UP } }, /* <||| */
/*
* There isn't really a good alternative for \ and |, so we steal
* the NO and YES keys, respectively. Because these keys don't
* self-repeat, we end their key-down sequences without unwinding
* to HID_KEY_NONE, and let the USB host do the key repeat itself.
* We do this by ending the sequence with whatever HID key code
* is present with M_ENDSEQ.
*/
[0xe6] = { { M_ENDSEQ | HID_KEY_BACKSLASH } }, /* NO */
[0xe7] = { { M_SHIFT, /* YES */
M_SHIFT | M_ENDSEQ | HID_KEY_BACKSLASH } },
[0xe8] = { { M_DOWN | M_META } }, /* SYM */
[0xe9] = { { M_DOWN | HID_KEY_PAUSE } }, /* PAUSE */
[0xea] = { { M_DOWN | M_ALT } }, /* TV/NABU */
/* 0xeb - 0xef */
[0xf0] = { { M_UP | HID_KEY_ARROW_RIGHT } },
[0xf1] = { { M_UP | HID_KEY_ARROW_LEFT } },
[0xf2] = { { M_UP | HID_KEY_ARROW_UP } },
[0xf3] = { { M_UP | HID_KEY_ARROW_DOWN } },
[0xf4] = { { M_UP | HID_KEY_PAGE_DOWN } }, /* |||> */
[0xf5] = { { M_UP | HID_KEY_PAGE_UP } }, /* <||| */
[0xf6] = { { M_ENDSEQ } }, /* NO */
[0xf7] = { { M_SHIFT } }, /* YES */
[0xf8] = { { M_UP | M_META } }, /* SYM */
[0xf9] = { { M_UP | HID_KEY_PAUSE } }, /* PAUSE */
[0xfa] = { { M_UP | M_ALT } }, /* TV/NABU */
/* 0xfb - 0xff */
};
/*
* Joystick data packets have the format:
*
* 1 0 1 F U R D L
* i p i o e
* r g w f
* e h n t
* t
*/
#define JOY_LEFT (1U << 0)
#define JOY_DOWN (1U << 1)
#define JOY_RIGHT (1U << 2)
#define JOY_UP (1U << 3)
#define JOY_FIRE (1U << 4)
#define JOY_DIR_MASK (JOY_LEFT | JOY_DOWN | JOY_RIGHT | JOY_UP)
/*
* GAMEPAD_HAT_CENTERED is, conveniently, 0. We'll also use that
* for physically impossible combinations on a real joystick / dpad.
*/
static const uint8_t joy_to_dpad[JOY_DIR_MASK + 1] = {
[JOY_UP] = GAMEPAD_HAT_UP,
[JOY_UP | JOY_RIGHT] = GAMEPAD_HAT_UP_RIGHT,
[JOY_RIGHT] = GAMEPAD_HAT_RIGHT,
[JOY_DOWN | JOY_RIGHT] = GAMEPAD_HAT_DOWN_RIGHT,
[JOY_DOWN] = GAMEPAD_HAT_DOWN,
[JOY_DOWN | JOY_LEFT] = GAMEPAD_HAT_DOWN_LEFT,
[JOY_LEFT] = GAMEPAD_HAT_LEFT,
[JOY_UP | JOY_LEFT] = GAMEPAD_HAT_UP_LEFT,
};
/*
* We keep 2 joystick contexts so we can report "simultaneous" movements
* on both sticks more accurately, but we still need to have a global for
* the "instance" we're processing while the data is coming in.
*/
static struct joy_context {
struct queue queue;
bool zombie;
} joy_context[2];
static void
joy_init(int which)
{
queue_init(&joy_context[which].queue);
joy_context[which].zombie = false;
}
static inline bool
joy_has_data_unlocked(int which)
{
return !QUEUE_EMPTY_P(&joy_context[which].queue) ||
joy_context[which].zombie;
}
static void
send_joy_report(int which, uint8_t data)
{
uint8_t dpad = joy_to_dpad[data & JOY_DIR_MASK];
uint8_t buttons = (data & JOY_FIRE) ? GAMEPAD_BUTTON_A : 0;
hid_gamepad_report_t report = {
.hat = dpad,
.buttons = buttons,
};
tud_hid_n_report(ITF_NUM_JOY0 + which, 0, &report, sizeof(report));
}
static struct {
struct queue queue;
const uint16_t *next;
uint16_t modifiers;
bool zombie;
} kbd_context;
static void
kbd_init(void)
{
queue_init(&kbd_context.queue);
kbd_context.next = NULL;
kbd_context.modifiers = 0;
kbd_context.zombie = false;
}
static inline bool
kbd_has_data_unlocked(void)
{
return kbd_context.next != NULL ||
!QUEUE_EMPTY_P(&kbd_context.queue) ||
kbd_context.zombie;
}
static inline uint8_t
keymod_to_hid(uint16_t code)
{
return M_MODS(code) >> 8;
}
static uint16_t
kbd_modifier(uint16_t code)
{
if (code & M_DOWN) {
/* Set the sticky modifier. */
debug_printf("DEBUG: %s: setting sticky modifier 0x%04x\n",
__func__, M_MODS(code));
kbd_context.modifiers |= M_MODS(code);
} else if (code & M_UP) {
/* Clear the sticky modifier. */
debug_printf("DEBUG: %s: clearing sticky modifier 0x%04x\n",
__func__, M_MODS(code));
kbd_context.modifiers &= ~M_MODS(code);
} else {
/* Nonsensical. */
return code;
}
/*
* Return an empty keycode to give the updated modifiers
* to the host.
*/
return HID_KEY_NONE;
}
static void
send_kbd_report(uint16_t code)
{
uint8_t keymod = keymod_to_hid(code | kbd_context.modifiers);
uint8_t keycode = (uint8_t)code;
hid_keyboard_report_t report = {
.modifier = keymod,
.keycode = { [0] = keycode },
};
tud_hid_n_report(ITF_NUM_KBD, 0, &report, sizeof(report));
}
/*
* The reader thread updates this timestamp each time it gets a
* byte from the keyboard.
*/
static volatile uint32_t last_kbd_message_time; /* in milliseconds */
static bool kbd_powerstate;
static void
kbd_setpower(bool enabled)
{
kbd_powerstate = enabled;
gpio_put(PWREN_PIN, enabled);
if (! enabled) {
have_nabu = false;
led_select_sequence();
}
}
static void
kbd_reboot(void)
{
/* Power down the keyboard. */
kbd_setpower(false);
/* Wait for 4 seconds. */
sleep_ms(4000);
/* Reset all of the queues. */
queue_drain(&kbd_context.queue);
queue_drain(&joy_context[0].queue);
queue_drain(&joy_context[1].queue);
/*
* Pretend we got a message while we wait for the power-up
* packet.
*/
last_kbd_message_time = board_millis();
/*
* hid_task() will see these later and rectify any zombie state
* the host has.
*/
kbd_context.zombie =
joy_context[0].zombie = joy_context[1].zombie = true;
/* Power up the keyboard. */
kbd_setpower(true);
}
#define DEADCHECK_WARN_MS 5000
#define DEADCHECK_DECLARE_MS 10000
static void
kbd_deadcheck(uint32_t now)
{
static bool deadcheck_warned;
if (now - last_kbd_message_time < DEADCHECK_WARN_MS) {
deadcheck_warned = false;
return;
}
/*
* A deadcheck when we haven't yet seen the keyboard or when the
* keyboard is powered off is pointless.
*/
if (!have_nabu || !kbd_powerstate) {
/* Suppress for another deadcheck interval. */
last_kbd_message_time = now;
printf("[%10u] INFO: waiting for keyboard.\n", board_millis());
return;
}
if (now - last_kbd_message_time < DEADCHECK_DECLARE_MS) {
if (! deadcheck_warned) {
printf("[%10u] WARNING: keyboard failed to ping.\n",
board_millis());
deadcheck_warned = true;
}
return;
}
/* Declare the keyboard dead and reboot it. */
printf("[%10u] ERROR: keyboard appears dead, rebooting...\n",
board_millis());
kbd_reboot();
deadcheck_warned = false;
}
static bool
kbd_err_task(uint8_t c)
{
switch (c) {
case NABU_CODE_ERR_MKEY:
printf("[%10u] INFO: multi-keypress, sending HID_KEY_NONE.\n",
board_millis());
send_kbd_report(HID_KEY_NONE);
return false;
case NABU_CODE_ERR_RAM:
printf("[%10u] ERROR: keyboard RAM error, rebooting...\n",
board_millis());
break;
case NABU_CODE_ERR_ROM:
printf("[%10u] ERROR: keyboard ROM error, rebooting...\n",
board_millis());
break;
case NABU_CODE_ERR_ISR:
printf("[%10u] ERROR: keyboard ISR error, rebooting...\n",
board_millis());
break;
case NABU_CODE_ERR_PING:
have_nabu = true;
led_select_sequence();
debug_printf("DEBUG: %s: received PING from keyboard.\n",
__func__);
return false;
case NABU_CODE_ERR_RESET:
/* Keyboard has announced itself! */
have_nabu = true;
led_select_sequence();
printf(
"[%10u] INFO: received RESET notification from keyboard.\n",
board_millis());
return false;
default:
/* This won't ever happen; just ignore. */
return false;
}
/* If we got here, we're rebooting the keyboard. */
kbd_reboot();
return true;
}
#define REPORT_INTERVAL_MS 10
static void
hid_task(uint32_t now)
{
uint8_t c;
/* This is good for ~139 years of uptime. */
static uint32_t start_ms;
if (now - start_ms < REPORT_INTERVAL_MS) {
return;
}
start_ms += REPORT_INTERVAL_MS;
/*
* Quick unlocked queue-empty checks to see if there's
* work to do.
*/
if (kbd_has_data_unlocked() ||
joy_has_data_unlocked(0) || joy_has_data_unlocked(1)) {
debug_printf("DEBUG: %s: have work to do (k=%d j0=%d j1=%d)\n",
__func__, kbd_has_data_unlocked(),
joy_has_data_unlocked(0), joy_has_data_unlocked(1));
} else {
/* No data to send. */
return;
}
/*
* We have at least one report to send. If we're suspended,
* wake up the host. We'll send the report the next time
* around.
*/
if (tud_suspended()) {
/*
* Peek at the keyboard; if it's an error code,
* process it and get out.
*/
if (queue_peek(&kbd_context.queue, &c) &&
NABU_CODE_ERR_P(c) &&
c != NABU_CODE_ERR_MKEY /* this is a key-press */) {
queue_get(&kbd_context.queue, &c);
kbd_err_task(c);
return;
}
if (want_remote_wakeup) {
tud_remote_wakeup();
want_remote_wakeup = false;
}
return;
}
if (tud_hid_n_ready(ITF_NUM_KBD)) {