-/*
+/*
* Copyright 2012 Jun Wako <wakojun@gmail.com>
* This file is based on:
* LUFA-120219/Demos/Device/Lowlevel/KeyboardMouse
#include "avr/suart.h"
#endif
+#include "matrix.h"
#include "descriptor.h"
#include "lufa.h"
+
+//#define LUFA_DEBUG
+
+
uint8_t keyboard_idle = 0;
/* 0: Boot Protocol, 1: Report Protocol(default) */
uint8_t keyboard_protocol = 1;
{
/* Create a temporary buffer to hold the read in report from the host */
uint8_t ConsoleData[CONSOLE_EPSIZE];
-
+
/* Read Console Report Data */
Endpoint_Read_Stream_LE(&ConsoleData, sizeof(ConsoleData), NULL);
-
+
/* Process Console Report Data */
//ProcessConsoleHIDReport(ConsoleData);
}
print("[D]");
/* For battery powered device */
USB_IsInitialized = false;
-/* TODO: This doesn't work. After several plug in/outs can not be enumerated.
+/* TODO: This doesn't work. After several plug in/outs can not be enumerated.
if (USB_IsInitialized) {
USB_Disable(); // Disable all interrupts
USB_Controller_Enable();
void EVENT_USB_Device_Reset(void)
{
+#ifdef LUFA_DEBUG
print("[R]");
+#endif
}
void EVENT_USB_Device_Suspend()
{
+#ifdef LUFA_DEBUG
print("[S]");
+#endif
hook_usb_suspend_entry();
}
void EVENT_USB_Device_WakeUp()
{
+#ifdef LUFA_DEBUG
print("[W]");
+#endif
hook_usb_wakeup();
}
*/
void EVENT_USB_Device_ConfigurationChanged(void)
{
+#ifdef LUFA_DEBUG
print("[c]");
+#endif
bool ConfigSuccess = true;
/* Setup Keyboard HID Report Endpoints */
*/
void EVENT_USB_Device_ControlRequest(void)
{
- print("[r]");
uint8_t* ReportData = NULL;
uint8_t ReportSize = 0;
/* Write the report data to the control endpoint */
Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
Endpoint_ClearOUT();
+#ifdef LUFA_DEBUG
+ xprintf("[r%d]", USB_ControlRequest.wIndex);
+#endif
}
break;
Endpoint_ClearOUT();
Endpoint_ClearStatusStage();
+#ifdef LUFA_DEBUG
+ xprintf("[L%d]", USB_ControlRequest.wIndex);
+#endif
break;
}
Endpoint_Write_8(keyboard_protocol);
Endpoint_ClearIN();
Endpoint_ClearStatusStage();
+#ifdef LUFA_DEBUG
+ print("[p]");
+#endif
}
}
keyboard_protocol = (USB_ControlRequest.wValue & 0xFF);
clear_keyboard();
+#ifdef LUFA_DEBUG
+ print("[P]");
+#endif
}
}
Endpoint_ClearStatusStage();
keyboard_idle = ((USB_ControlRequest.wValue & 0xFF00) >> 8);
+#ifdef LUFA_DEBUG
+ xprintf("[I%d]%d", USB_ControlRequest.wIndex, (USB_ControlRequest.wValue & 0xFF00) >> 8);
+#endif
}
break;
Endpoint_Write_8(keyboard_idle);
Endpoint_ClearIN();
Endpoint_ClearStatusStage();
+#ifdef LUFA_DEBUG
+ print("[i]");
+#endif
}
break;
}
/*******************************************************************************
- * Host driver
+ * Host driver
******************************************************************************/
static uint8_t keyboard_leds(void)
{
int main(void) __attribute__ ((weak));
int main(void)
{
+ setup_mcu();
+
#ifdef LUFA_DEBUG_SUART
- DDRD |= (1<<SUART_OUT_BIT);
+ SUART_OUT_DDR |= (1<<SUART_OUT_BIT);
+ SUART_OUT_PORT |= (1<<SUART_OUT_BIT);
#endif
print_set_sendchar(sendchar);
- setup_mcu();
+ print("\r\ninit\n");
+
hook_early_init();
keyboard_setup();
setup_usb();
hook_late_init();
while (1) {
while (USB_DeviceState == DEVICE_STATE_Suspended) {
+#ifdef LUFA_DEBUG
print("[s]");
+#endif
hook_usb_suspend_loop();
}
__attribute__((weak))
void hook_late_init(void) {}
+static uint8_t _led_stats = 0;
__attribute__((weak))
void hook_usb_suspend_entry(void)
{
+ // Turn LED off to save power
+ // Set 0 with putting aside status before suspend and restore
+ // it after wakeup, then LED is updated at keyboard_task() in main loop
+ _led_stats = keyboard_led_stats;
+ keyboard_led_stats = 0;
+ led_set(keyboard_led_stats);
+
+ matrix_init();
+ clear_keyboard();
#ifdef SLEEP_LED_ENABLE
sleep_led_enable();
#endif
{
suspend_power_down();
if (USB_Device_RemoteWakeupEnabled && suspend_wakeup_condition()) {
- USB_Device_SendRemoteWakeup();
+ USB_Device_SendRemoteWakeup();
}
}
suspend_wakeup_init();
#ifdef SLEEP_LED_ENABLE
sleep_led_disable();
- // NOTE: converters may not accept this
- led_set(host_keyboard_leds());
#endif
+
+ // Restore LED status
+ // BIOS/grub won't recognize/enumerate if led_set() takes long(around 40ms?)
+ // Converters fall into the case and miss wakeup event(timeout to reply?) in the end.
+ //led_set(host_keyboard_leds());
+ // Instead, restore stats and update at keyboard_task() in main loop
+ keyboard_led_stats = _led_stats;
}