28
28
#include "bsp/board.h"
29
29
#include "tusb.h"
30
30
31
- #define CLKIN 14
32
- #define CLKOUT 15
33
- #define DATIN 17
34
- #define DATOUT 16
35
-
36
31
uint8_t const led2ps2 [] = { 0 , 4 , 1 , 5 , 2 , 6 , 3 , 7 };
37
32
uint8_t const mod2ps2 [] = { 0x14 , 0x12 , 0x11 , 0x1f , 0x14 , 0x59 , 0x11 , 0x27 };
38
33
uint8_t const hid2ps2 [] = {
@@ -62,7 +57,6 @@ uint32_t repeat_us = 35000;
62
57
uint16_t delay_ms = 250 ;
63
58
alarm_id_t repeater ;
64
59
65
- uint8_t comp_rpt [] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
66
60
uint8_t prev_rpt [] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
67
61
uint8_t prev_ps2 = 0 ;
68
62
uint8_t resend = 0 ;
@@ -89,6 +83,7 @@ uint16_t ps2_frame(uint8_t data) {
89
83
}
90
84
91
85
void ps2_send (uint8_t data ) {
86
+ if (DEBUG ) printf ("ps2_send: %02x\n" , data );
92
87
resend = data ;
93
88
pio_sm_put (pio , sm , ps2_frame (data ));
94
89
}
@@ -110,14 +105,16 @@ void kbd_set_leds(uint8_t data) {
110
105
}
111
106
112
107
int64_t blink_callback (alarm_id_t id , void * user_data ) {
113
- if (kbd_addr ) {
114
- if (blinking ) {
115
- kbd_set_leds ( 7 ) ;
116
- blinking = false ;
117
- return 500000 ;
118
- } else {
108
+ if (blinking ) {
109
+ if (kbd_addr ) kbd_set_leds ( 7 );
110
+ blinking = false ;
111
+ return 500000 ;
112
+ } else {
113
+ if ( kbd_addr ) {
119
114
kbd_set_leds (0 );
120
115
ps2_send (0xaa );
116
+ } else {
117
+ ps2_send (0xfc );
121
118
}
122
119
}
123
120
return 0 ;
@@ -137,6 +134,7 @@ void ps2_receive(uint32_t fifo) {
137
134
}
138
135
139
136
uint8_t data = fifo ;
137
+ if (DEBUG ) printf ("ps2_receive: %02x\n" , data );
140
138
141
139
switch (prev_ps2 ) {
142
140
case 0xed : // CMD: Set LEDs
@@ -208,7 +206,11 @@ void ps2_receive(uint32_t fifo) {
208
206
}
209
207
210
208
void tuh_hid_mount_cb (uint8_t dev_addr , uint8_t instance , uint8_t const * desc_report , uint16_t desc_len ) {
209
+ if (DEBUG ) printf ("HID device address = %d, instance = %d is mounted\n" , dev_addr , instance );
210
+
211
211
if (tuh_hid_interface_protocol (dev_addr , instance ) == HID_ITF_PROTOCOL_KEYBOARD ) {
212
+ if (DEBUG ) printf ("HID Interface Protocol = Keyboard\n" );
213
+
212
214
kbd_addr = dev_addr ;
213
215
kbd_inst = instance ;
214
216
@@ -221,6 +223,8 @@ void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_re
221
223
}
222
224
223
225
void tuh_hid_umount_cb (uint8_t dev_addr , uint8_t instance ) {
226
+ if (DEBUG ) printf ("HID device address = %d, instance = %d is unmounted\n" , dev_addr , instance );
227
+
224
228
if (dev_addr == kbd_addr && instance == kbd_inst ) {
225
229
kbd_addr = 0 ;
226
230
kbd_inst = 0 ;
@@ -236,7 +240,6 @@ void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t cons
236
240
}
237
241
238
242
board_led_write (1 );
239
- memcpy (comp_rpt , prev_rpt , sizeof (comp_rpt ));
240
243
241
244
if (report [0 ] != prev_rpt [0 ]) {
242
245
uint8_t rbits = report [0 ];
@@ -267,8 +270,6 @@ void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t cons
267
270
pbits = pbits >> 1 ;
268
271
269
272
}
270
-
271
- comp_rpt [0 ] = report [0 ];
272
273
}
273
274
274
275
for (uint8_t i = 2 ; i < 8 ; i ++ ) {
@@ -325,11 +326,9 @@ void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t cons
325
326
ps2_send (hid2ps2 [report [i ]]);
326
327
}
327
328
}
328
-
329
- comp_rpt [i ] = report [i ];
330
329
}
331
330
332
- memcpy (prev_rpt , comp_rpt , sizeof (prev_rpt ));
331
+ memcpy (prev_rpt , report , sizeof (prev_rpt ));
333
332
tuh_hid_receive_report (dev_addr , instance );
334
333
board_led_write (0 );
335
334
@@ -339,17 +338,19 @@ void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t cons
339
338
void irq_callback (uint gpio , uint32_t events ) {
340
339
if (!gpio_get (DATIN ) && !pio_interrupt_get (pio , 0 )) {
341
340
board_led_write (1 );
341
+ if (DEBUG ) printf (" IRQ " );
342
342
pio_sm_drain_tx_fifo (pio , sm );
343
343
pio_sm_exec (pio , sm , pio_encode_jmp (offset + 2 ));
344
344
}
345
345
}
346
346
347
347
void main () {
348
348
board_init ();
349
+ printf ("\n%s-%s DEBUG=%s\n" , PICO_PROGRAM_NAME , PICO_PROGRAM_VERSION_STRING , DEBUG ? "true" : "false" );
349
350
350
351
sm = pio_claim_unused_sm (pio , true);
351
- offset = pio_add_program (pio , & ps2dev_program );
352
- ps2dev_program_init (pio , sm , offset , CLKIN , CLKOUT , DATIN , DATOUT );
352
+ offset = pio_add_program (pio , & ps2device_program );
353
+ ps2device_program_init (pio , sm , offset , CLKIN , CLKOUT , DATIN , DATOUT );
353
354
354
355
gpio_set_irq_enabled_with_callback (CLKIN , GPIO_IRQ_EDGE_RISE , true, & irq_callback );
355
356
tusb_init ();
@@ -367,12 +368,12 @@ void main() {
367
368
368
369
if (repeat ) {
369
370
if (repeatmod ) {
370
- if (repeat > 3 && repeat != 6 ) ps2_send (0xe0 );
371
- ps2_send (mod2ps2 [repeat - 1 ]);
372
- } else {
373
- maybe_send_e0 (repeat );
374
- ps2_send (hid2ps2 [repeat ]);
375
- }
371
+ if (repeat > 3 && repeat != 6 ) ps2_send (0xe0 );
372
+ ps2_send (mod2ps2 [repeat - 1 ]);
373
+ } else {
374
+ maybe_send_e0 (repeat );
375
+ ps2_send (hid2ps2 [repeat ]);
376
+ }
376
377
}
377
378
}
378
379
}
0 commit comments