X-Git-Url: https://git.friedersdorff.com/?a=blobdiff_plain;f=tmk_core%2Fprotocol%2Flufa%2Flufa.c;h=b1567fa9bdac242dfbe9a12395c7db8212177748;hb=b7d80d8b0e59b697ad1e0f58f9fcb0d1247ef46e;hp=beec6b2be31d3f30d926f0fbcc652f4bce839473;hpb=3fe8e1c238fc8e15dacda1b03c0c1745a7b8e8e7;p=max%2Ftmk_keyboard.git diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index beec6b2b..b1567fa9 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,6 +43,7 @@ #include "action.h" #include "led.h" #include "sendchar.h" +#include "ringbuf.h" #include "debug.h" #ifdef SLEEP_LED_ENABLE #include "sleep_led.h" @@ -50,9 +51,18 @@ #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; @@ -80,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; @@ -367,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; @@ -374,7 +434,7 @@ void EVENT_USB_Device_ControlRequest(void) } /******************************************************************************* - * Host driver + * Host driver ******************************************************************************/ static uint8_t keyboard_leds(void) { @@ -383,7 +443,7 @@ 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; @@ -395,7 +455,7 @@ static void send_keyboard(report_keyboard_t *report) 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 */ @@ -408,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 */ @@ -446,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) @@ -463,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) @@ -484,6 +547,7 @@ static void send_consumer(uint16_t data) Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL); Endpoint_ClearIN(); +#endif } @@ -491,66 +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; - - // prevents Console_Task() from running during sendchar() runs. - // or char will be lost. These two function is mutually exclusive. - CONSOLE_FLUSH_SET(false); - - if (USB_DeviceState != DEVICE_STATE_Configured) - return -1; - - 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; - } + #ifdef LUFA_DEBUG_SUART + xmit(c); + #endif - 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()) { - while (!(Endpoint_IsINReady())); - Endpoint_ClearIN(); - } else { - CONSOLE_FLUSH_SET(true); - } - - 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 @@ -576,21 +595,34 @@ static void setup_usb(void) USB_Init(); - // for Console_Task USB_Device_EnableSOFEvents(); - print_set_sendchar(sendchar); } int main(void) __attribute__ ((weak)); int main(void) { setup_mcu(); + +#ifdef LUFA_DEBUG_SUART + SUART_OUT_DDR |= (1<