1
- /*
1
+ /*
2
2
* Copyright 2012 Jun Wako <[email protected] >
3
3
* This file is based on:
4
4
* LUFA-120219/Demos/Device/Lowlevel/KeyboardMouse
54
54
#include "avr/suart.h"
55
55
#endif
56
56
57
+ #include "matrix.h"
57
58
#include "descriptor.h"
58
59
#include "lufa.h"
59
60
61
+
62
+ //#define LUFA_DEBUG
63
+
64
+
60
65
uint8_t keyboard_idle = 0 ;
61
66
/* 0: Boot Protocol, 1: Report Protocol(default) */
62
67
uint8_t keyboard_protocol = 1 ;
@@ -104,10 +109,10 @@ static void Console_Task(void)
104
109
{
105
110
/* Create a temporary buffer to hold the read in report from the host */
106
111
uint8_t ConsoleData [CONSOLE_EPSIZE ];
107
-
112
+
108
113
/* Read Console Report Data */
109
114
Endpoint_Read_Stream_LE (& ConsoleData , sizeof (ConsoleData ), NULL );
110
-
115
+
111
116
/* Process Console Report Data */
112
117
//ProcessConsoleHIDReport(ConsoleData);
113
118
}
@@ -168,7 +173,7 @@ void EVENT_USB_Device_Disconnect(void)
168
173
print ("[D]" );
169
174
/* For battery powered device */
170
175
USB_IsInitialized = false;
171
- /* TODO: This doesn't work. After several plug in/outs can not be enumerated.
176
+ /* TODO: This doesn't work. After several plug in/outs can not be enumerated.
172
177
if (USB_IsInitialized) {
173
178
USB_Disable(); // Disable all interrupts
174
179
USB_Controller_Enable();
@@ -179,18 +184,24 @@ void EVENT_USB_Device_Disconnect(void)
179
184
180
185
void EVENT_USB_Device_Reset (void )
181
186
{
187
+ #ifdef LUFA_DEBUG
182
188
print ("[R]" );
189
+ #endif
183
190
}
184
191
185
192
void EVENT_USB_Device_Suspend ()
186
193
{
194
+ #ifdef LUFA_DEBUG
187
195
print ("[S]" );
196
+ #endif
188
197
hook_usb_suspend_entry ();
189
198
}
190
199
191
200
void EVENT_USB_Device_WakeUp ()
192
201
{
202
+ #ifdef LUFA_DEBUG
193
203
print ("[W]" );
204
+ #endif
194
205
hook_usb_wakeup ();
195
206
}
196
207
@@ -221,7 +232,9 @@ void EVENT_USB_Device_StartOfFrame(void)
221
232
*/
222
233
void EVENT_USB_Device_ConfigurationChanged (void )
223
234
{
235
+ #ifdef LUFA_DEBUG
224
236
print ("[c]" );
237
+ #endif
225
238
bool ConfigSuccess = true;
226
239
227
240
/* Setup Keyboard HID Report Endpoints */
@@ -275,7 +288,6 @@ Other Device Required Optional Optional Optional Optional Opti
275
288
*/
276
289
void EVENT_USB_Device_ControlRequest (void )
277
290
{
278
- print ("[r]" );
279
291
uint8_t * ReportData = NULL ;
280
292
uint8_t ReportSize = 0 ;
281
293
@@ -299,6 +311,9 @@ void EVENT_USB_Device_ControlRequest(void)
299
311
/* Write the report data to the control endpoint */
300
312
Endpoint_Write_Control_Stream_LE (ReportData , ReportSize );
301
313
Endpoint_ClearOUT ();
314
+ #ifdef LUFA_DEBUG
315
+ xprintf ("[r%d]" , USB_ControlRequest .wIndex );
316
+ #endif
302
317
}
303
318
304
319
break ;
@@ -322,6 +337,9 @@ void EVENT_USB_Device_ControlRequest(void)
322
337
323
338
Endpoint_ClearOUT ();
324
339
Endpoint_ClearStatusStage ();
340
+ #ifdef LUFA_DEBUG
341
+ xprintf ("[L%d]" , USB_ControlRequest .wIndex );
342
+ #endif
325
343
break ;
326
344
}
327
345
@@ -338,6 +356,9 @@ void EVENT_USB_Device_ControlRequest(void)
338
356
Endpoint_Write_8 (keyboard_protocol );
339
357
Endpoint_ClearIN ();
340
358
Endpoint_ClearStatusStage ();
359
+ #ifdef LUFA_DEBUG
360
+ print ("[p]" );
361
+ #endif
341
362
}
342
363
}
343
364
@@ -351,6 +372,9 @@ void EVENT_USB_Device_ControlRequest(void)
351
372
352
373
keyboard_protocol = (USB_ControlRequest .wValue & 0xFF );
353
374
clear_keyboard ();
375
+ #ifdef LUFA_DEBUG
376
+ print ("[P]" );
377
+ #endif
354
378
}
355
379
}
356
380
@@ -362,6 +386,9 @@ void EVENT_USB_Device_ControlRequest(void)
362
386
Endpoint_ClearStatusStage ();
363
387
364
388
keyboard_idle = ((USB_ControlRequest .wValue & 0xFF00 ) >> 8 );
389
+ #ifdef LUFA_DEBUG
390
+ xprintf ("[I%d]%d" , USB_ControlRequest .wIndex , (USB_ControlRequest .wValue & 0xFF00 ) >> 8 );
391
+ #endif
365
392
}
366
393
367
394
break ;
@@ -373,14 +400,17 @@ void EVENT_USB_Device_ControlRequest(void)
373
400
Endpoint_Write_8 (keyboard_idle );
374
401
Endpoint_ClearIN ();
375
402
Endpoint_ClearStatusStage ();
403
+ #ifdef LUFA_DEBUG
404
+ print ("[i]" );
405
+ #endif
376
406
}
377
407
378
408
break ;
379
409
}
380
410
}
381
411
382
412
/*******************************************************************************
383
- * Host driver
413
+ * Host driver
384
414
******************************************************************************/
385
415
static uint8_t keyboard_leds (void )
386
416
{
@@ -595,11 +625,15 @@ static void setup_usb(void)
595
625
int main (void ) __attribute__ ((weak ));
596
626
int main (void )
597
627
{
628
+ setup_mcu ();
629
+
598
630
#ifdef LUFA_DEBUG_SUART
599
- DDRD |= (1 <<SUART_OUT_BIT );
631
+ SUART_OUT_DDR |= (1 <<SUART_OUT_BIT );
632
+ SUART_OUT_PORT |= (1 <<SUART_OUT_BIT );
600
633
#endif
601
634
print_set_sendchar (sendchar );
602
- setup_mcu ();
635
+ print ("\r\ninit\n" );
636
+
603
637
hook_early_init ();
604
638
keyboard_setup ();
605
639
setup_usb ();
@@ -626,7 +660,9 @@ int main(void)
626
660
hook_late_init ();
627
661
while (1 ) {
628
662
while (USB_DeviceState == DEVICE_STATE_Suspended ) {
663
+ #ifdef LUFA_DEBUG
629
664
print ("[s]" );
665
+ #endif
630
666
hook_usb_suspend_loop ();
631
667
}
632
668
@@ -646,9 +682,19 @@ void hook_early_init(void) {}
646
682
__attribute__((weak ))
647
683
void hook_late_init (void ) {}
648
684
685
+ static uint8_t _led_stats = 0 ;
649
686
__attribute__((weak ))
650
687
void hook_usb_suspend_entry (void )
651
688
{
689
+ // Turn LED off to save power
690
+ // Set 0 with putting aside status before suspend and restore
691
+ // it after wakeup, then LED is updated at keyboard_task() in main loop
692
+ _led_stats = keyboard_led_stats ;
693
+ keyboard_led_stats = 0 ;
694
+ led_set (keyboard_led_stats );
695
+
696
+ matrix_init ();
697
+ clear_keyboard ();
652
698
#ifdef SLEEP_LED_ENABLE
653
699
sleep_led_enable ();
654
700
#endif
@@ -659,7 +705,7 @@ void hook_usb_suspend_loop(void)
659
705
{
660
706
suspend_power_down ();
661
707
if (USB_Device_RemoteWakeupEnabled && suspend_wakeup_condition ()) {
662
- USB_Device_SendRemoteWakeup ();
708
+ USB_Device_SendRemoteWakeup ();
663
709
}
664
710
}
665
711
@@ -669,7 +715,12 @@ void hook_usb_wakeup(void)
669
715
suspend_wakeup_init ();
670
716
#ifdef SLEEP_LED_ENABLE
671
717
sleep_led_disable ();
672
- // NOTE: converters may not accept this
673
- led_set (host_keyboard_leds ());
674
718
#endif
719
+
720
+ // Restore LED status
721
+ // BIOS/grub won't recognize/enumerate if led_set() takes long(around 40ms?)
722
+ // Converters fall into the case and miss wakeup event(timeout to reply?) in the end.
723
+ //led_set(host_keyboard_leds());
724
+ // Instead, restore stats and update at keyboard_task() in main loop
725
+ keyboard_led_stats = _led_stats ;
675
726
}
0 commit comments