X-Git-Url: https://git.friedersdorff.com/?a=blobdiff_plain;f=tmk_core%2Fprotocol%2Flufa%2Flufa.c;h=54221591b388b3729077b68ac23cabe6b624ae7e;hb=12e5a3a13eb86852ad58c131e28ba29c5f09bb2d;hp=85fdeabdd14215a024645ca312b4275b540f4453;hpb=fdce0c9cc0b7f8e9f1497cae3ea63a6672ceaf71;p=max%2Ftmk_keyboard.git diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index 85fdeabd..54221591 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,33 @@ #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 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 TMK_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 +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 TMK_LUFA_DEBUG + xprintf("[I%d]%d", USB_ControlRequest.wIndex, (USB_ControlRequest.wValue & 0xFF00) >> 8); +#endif } break; @@ -374,6 +490,9 @@ void EVENT_USB_Device_ControlRequest(void) Endpoint_Write_8(keyboard_idle); Endpoint_ClearIN(); Endpoint_ClearStatusStage(); +#ifdef TMK_LUFA_DEBUG + print("[i]"); +#endif } break; @@ -381,7 +500,7 @@ void EVENT_USB_Device_ControlRequest(void) } /******************************************************************************* - * Host driver + * Host driver ******************************************************************************/ static uint8_t keyboard_leds(void) { @@ -390,19 +509,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 */ @@ -415,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 */ @@ -453,6 +572,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) @@ -460,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); @@ -470,10 +590,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) @@ -491,76 +613,29 @@ static void send_consumer(uint16_t data) Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL); Endpoint_ClearIN(); +#endif } /******************************************************************************* * 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; + #ifdef TMK_LUFA_DEBUG_SUART + xmit(c); + #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; - - 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); - } + #ifdef CONSOLE_ENABLE + console_putc(c); + #endif - Endpoint_SelectEndpoint(ep); - return 0; -ERROR_EXIT: - Endpoint_SelectEndpoint(ep); - return -1; -} -#else -int8_t sendchar(uint8_t c) -{ return 0; } -#endif /******************************************************************************* @@ -582,51 +657,124 @@ static void setup_usb(void) USB_Disable(); USB_Init(); - - // for Console_Task - USB_Device_EnableSOFEvents(); - print_set_sendchar(sendchar); } int main(void) __attribute__ ((weak)); int main(void) { setup_mcu(); + +#ifdef TMK_LUFA_DEBUG_SUART + SUART_OUT_DDR |= (1<