X-Git-Url: https://git.friedersdorff.com/?a=blobdiff_plain;f=tmk_core%2Fprotocol%2Flufa%2Flufa.c;h=54221591b388b3729077b68ac23cabe6b624ae7e;hb=12e5a3a13eb86852ad58c131e28ba29c5f09bb2d;hp=60849aea4c935da108839c6e583d596540624719;hpb=c8d67963586eaa90b4b673a493557fe88df9febc;p=max%2Ftmk_keyboard.git diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index 60849aea..54221591 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -43,23 +43,29 @@ #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" +#include "timer.h" -#ifdef LUFA_DEBUG_SUART +#ifdef TMK_LUFA_DEBUG_SUART #include "avr/suart.h" #endif +#ifdef TMK_LUFA_DEBUG_UART +#include "uart.h" +#endif + #include "matrix.h" #include "descriptor.h" #include "lufa.h" -//#define LUFA_DEBUG +//#define TMK_LUFA_DEBUG uint8_t keyboard_idle = 0; @@ -89,60 +95,160 @@ 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 +}; + +// TODO: Around 2500ms delay often works anyhoo but proper startup would be better +// 1000ms delay of hid_listen affects this probably +/* wait for Console startup */ +static bool console_is_ready(void) { - /* Device must be connected and configured for the task to run */ - if (USB_DeviceState != DEVICE_STATE_Configured) - return; + static bool hid_listen_ready = false; + if (!hid_listen_ready) { + if (timer_read32() < 2500) + return false; + hid_listen_ready = true; + } + return true; +} + +static bool console_putc(uint8_t c) +{ + // return immediately if called while interrupt + if (!(SREG & (1<> 8); -#ifdef LUFA_DEBUG +#ifdef TMK_LUFA_DEBUG xprintf("[I%d]%d", USB_ControlRequest.wIndex, (USB_ControlRequest.wValue & 0xFF00) >> 8); #endif } @@ -400,7 +490,7 @@ void EVENT_USB_Device_ControlRequest(void) Endpoint_Write_8(keyboard_idle); Endpoint_ClearIN(); Endpoint_ClearStatusStage(); -#ifdef LUFA_DEBUG +#ifdef TMK_LUFA_DEBUG print("[i]"); #endif } @@ -419,7 +509,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; @@ -444,7 +534,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 */ @@ -490,7 +580,7 @@ static void send_system(uint16_t data) report_extra_t r = { .report_id = REPORT_ID_SYSTEM, - .usage = data + .usage = data - SYSTEM_POWER_DOWN + 1 }; Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM); @@ -530,76 +620,22 @@ static void send_consumer(uint16_t data) /******************************************************************************* * sendchar ******************************************************************************/ -#ifdef CONSOLE_ENABLE -#define SEND_TIMEOUT 5 int8_t sendchar(uint8_t c) { -#ifdef LUFA_DEBUG_SUART + #ifdef TMK_LUFA_DEBUG_SUART xmit(c); -#endif - // Not wait once timeouted. - // Because sendchar() is called so many times, waiting each call causes big lag. - static bool timeouted = false; + #endif - // prevents Console_Task() from running during sendchar() runs. - // or char will be lost. These two function is mutually exclusive. - CONSOLE_FLUSH_SET(false); + #ifdef TMK_LUFA_DEBUG_UART + uart_putchar(c); + #endif - if (USB_DeviceState != DEVICE_STATE_Configured) - return -1; + #ifdef CONSOLE_ENABLE + console_putc(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()) { - while (!(Endpoint_IsINReady())); - Endpoint_ClearIN(); - } else { - CONSOLE_FLUSH_SET(true); - } - - Endpoint_SelectEndpoint(ep); - return 0; -ERROR_EXIT: - Endpoint_SelectEndpoint(ep); - return -1; -} -#else -int8_t sendchar(uint8_t c) -{ -#ifdef LUFA_DEBUG_SUART - xmit(c); -#endif return 0; } -#endif /******************************************************************************* @@ -621,9 +657,6 @@ static void setup_usb(void) USB_Disable(); USB_Init(); - - // for Console_Task - USB_Device_EnableSOFEvents(); } int main(void) __attribute__ ((weak)); @@ -631,58 +664,60 @@ int main(void) { setup_mcu(); -#ifdef LUFA_DEBUG_SUART +#ifdef TMK_LUFA_DEBUG_SUART SUART_OUT_DDR |= (1<