X-Git-Url: https://git.friedersdorff.com/?a=blobdiff_plain;f=tmk_core%2Fprotocol%2Flufa%2Flufa.c;h=cfd76c2edfa6c6503325b5d77475c9fc51175df1;hb=5b9da20efa837973ed4e9cde49b173bcd05e6ce6;hp=cdfc7bc6ad990bfd5a0031cb71dec93bbf99067c;hpb=d9fee5571d7de08e76dff5ce75816faf522240f6;p=max%2Ftmk_keyboard.git diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index cdfc7bc6..cfd76c2e 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -1,4 +1,4 @@ -/* +/* * Copyright 2012 Jun Wako * This file is based on: * LUFA-120219/Demos/Device/Lowlevel/KeyboardMouse @@ -43,16 +43,28 @@ #include "action.h" #include "led.h" #include "sendchar.h" +#include "ringbuf.h" #include "debug.h" #ifdef SLEEP_LED_ENABLE #include "sleep_led.h" #endif #include "suspend.h" +#include "hook.h" + +#ifdef LUFA_DEBUG_SUART +#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; static uint8_t keyboard_led_stats = 0; @@ -78,60 +90,97 @@ host_driver_t lufa_driver = { * Console ******************************************************************************/ #ifdef CONSOLE_ENABLE -static void Console_Task(void) +#define SENDBUF_SIZE 256 +static uint8_t sbuf[SENDBUF_SIZE]; +static ringbuf_t sendbuf = { + .buffer = sbuf, + .head = 0, + .tail = 0, + .size_mask = SENDBUF_SIZE - 1 +}; + +static bool console_putc(uint8_t c) { - /* Device must be connected and configured for the task to run */ + // return immediately if called while interrupt + if (!(SREG & (1<> 8); +#ifdef LUFA_DEBUG + xprintf("[I%d]%d", USB_ControlRequest.wIndex, (USB_ControlRequest.wValue & 0xFF00) >> 8); +#endif } break; @@ -360,6 +424,9 @@ void EVENT_USB_Device_ControlRequest(void) Endpoint_Write_8(keyboard_idle); Endpoint_ClearIN(); Endpoint_ClearStatusStage(); +#ifdef LUFA_DEBUG + print("[i]"); +#endif } break; @@ -367,7 +434,7 @@ void EVENT_USB_Device_ControlRequest(void) } /******************************************************************************* - * Host driver + * Host driver ******************************************************************************/ static uint8_t keyboard_leds(void) { @@ -376,19 +443,19 @@ static uint8_t keyboard_leds(void) static void send_keyboard(report_keyboard_t *report) { - uint8_t timeout = 255; + uint8_t timeout = 128; if (USB_DeviceState != DEVICE_STATE_Configured) return; /* Select the Keyboard Report Endpoint */ #ifdef NKRO_ENABLE - if (keyboard_nkro) { + if (keyboard_protocol && keyboard_nkro) { /* Report protocol - NKRO */ Endpoint_SelectEndpoint(NKRO_IN_EPNUM); /* Check if write ready for a polling interval around 1ms */ - while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(4); + while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(8); if (!Endpoint_IsReadWriteAllowed()) return; /* Write Keyboard Report Data */ @@ -401,7 +468,7 @@ static void send_keyboard(report_keyboard_t *report) Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM); /* Check if write ready for a polling interval around 10ms */ - while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40); + while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(80); if (!Endpoint_IsReadWriteAllowed()) return; /* Write Keyboard Report Data */ @@ -439,6 +506,7 @@ static void send_mouse(report_mouse_t *report) static void send_system(uint16_t data) { +#ifdef EXTRAKEY_ENABLE uint8_t timeout = 255; if (USB_DeviceState != DEVICE_STATE_Configured) @@ -456,10 +524,12 @@ static void send_system(uint16_t data) Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL); Endpoint_ClearIN(); +#endif } static void send_consumer(uint16_t data) { +#ifdef EXTRAKEY_ENABLE uint8_t timeout = 255; if (USB_DeviceState != DEVICE_STATE_Configured) @@ -477,6 +547,7 @@ static void send_consumer(uint16_t data) Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL); Endpoint_ClearIN(); +#endif } @@ -484,58 +555,21 @@ static void send_consumer(uint16_t data) * sendchar ******************************************************************************/ #ifdef CONSOLE_ENABLE -#define SEND_TIMEOUT 5 int8_t sendchar(uint8_t c) { - // Not wait once timeouted. - // Because sendchar() is called so many times, waiting each call causes big lag. - static bool timeouted = false; - - if (USB_DeviceState != DEVICE_STATE_Configured) - return -1; + #ifdef LUFA_DEBUG_SUART + xmit(c); + #endif - uint8_t ep = Endpoint_GetCurrentEndpoint(); - Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM); - if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) { - goto ERROR_EXIT; - } - - if (timeouted && !Endpoint_IsReadWriteAllowed()) { - goto ERROR_EXIT; - } - - timeouted = false; - - uint8_t timeout = SEND_TIMEOUT; - while (!Endpoint_IsReadWriteAllowed()) { - if (USB_DeviceState != DEVICE_STATE_Configured) { - goto ERROR_EXIT; - } - if (Endpoint_IsStalled()) { - goto ERROR_EXIT; - } - if (!(timeout--)) { - timeouted = true; - goto ERROR_EXIT; - } - _delay_ms(1); - } - - Endpoint_Write_8(c); - - // send when bank is full - if (!Endpoint_IsReadWriteAllowed()) - Endpoint_ClearIN(); - - Endpoint_SelectEndpoint(ep); - return 0; -ERROR_EXIT: - Endpoint_SelectEndpoint(ep); - return -1; + bool r = console_putc(c); + return (r ? 0 : -1); } #else int8_t sendchar(uint8_t c) { + #ifdef LUFA_DEBUG_SUART + xmit(c); + #endif return 0; } #endif @@ -544,7 +578,7 @@ int8_t sendchar(uint8_t c) /******************************************************************************* * main ******************************************************************************/ -static void SetupHardware(void) +static void setup_mcu(void) { /* Disable watchdog if enabled by bootloader/fuses */ MCUSR &= ~(1 << WDRF); @@ -552,24 +586,43 @@ static void SetupHardware(void) /* Disable clock division */ clock_prescale_set(clock_div_1); +} +static void setup_usb(void) +{ // Leonardo needs. Without this USB device is not recognized. USB_Disable(); USB_Init(); - // for Console_Task USB_Device_EnableSOFEvents(); - print_set_sendchar(sendchar); } int main(void) __attribute__ ((weak)); int main(void) { - SetupHardware(); + setup_mcu(); + +#ifdef LUFA_DEBUG_SUART + SUART_OUT_DDR |= (1<