From 6632802c7903b4aed1f91dd0b25f7227523f110a Mon Sep 17 00:00:00 2001 From: tmk Date: Wed, 25 Jul 2018 18:53:38 +0900 Subject: [PATCH 1/6] core: Add ring buffer file --- tmk_core/common/ringbuf.h | 73 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 tmk_core/common/ringbuf.h diff --git a/tmk_core/common/ringbuf.h b/tmk_core/common/ringbuf.h new file mode 100644 index 00000000..23bb41e0 --- /dev/null +++ b/tmk_core/common/ringbuf.h @@ -0,0 +1,73 @@ +#ifndef RINGBUF_H +#define RINGBUF_H + +#include +#include + +// NOTE: buffer size must be 2^n and up to 255. size_mask should be 2^n - 1 due to using &(AND) instead of %(modulo) +typedef struct { + uint8_t *buffer; + uint8_t head; + uint8_t tail; + uint8_t size_mask; +} ringbuf_t; + +static inline void ringbuf_init(ringbuf_t *buf, uint8_t *array, uint8_t size); +static inline int16_t ringbuf_get(ringbuf_t *buf); +static inline bool ringbuf_put(ringbuf_t *buf, uint8_t data); +static inline void ringbuf_write(ringbuf_t *buf, uint8_t data); +static inline bool ringbuf_is_empty(ringbuf_t *buf); +static inline bool ringbuf_is_full(ringbuf_t *buf); +static inline void ringbuf_reset(ringbuf_t *buf); + +static inline void ringbuf_init(ringbuf_t *buf, uint8_t *array, uint8_t size) +{ + buf->buffer = array; + buf->head = 0; + buf->tail = 0; + buf->size_mask = size - 1; +} +static inline int16_t ringbuf_get(ringbuf_t *buf) +{ + if (ringbuf_is_empty(buf)) return -1; + uint8_t data = buf->buffer[buf->tail]; + buf->tail++; + buf->tail &= buf->size_mask; + return data; +} +static inline bool ringbuf_put(ringbuf_t *buf, uint8_t data) +{ + if (ringbuf_is_full(buf)) { + return false; + } + buf->buffer[buf->head] = data; + buf->head++; + buf->head &= buf->size_mask; + return true; +} +// this overrides data in buffer when it is full +static inline void ringbuf_write(ringbuf_t *buf, uint8_t data) +{ + buf->buffer[buf->head] = data; + buf->head++; + buf->head &= buf->size_mask; + // eat tail: override data yet to be consumed + if (buf->head == buf->tail) { + buf->tail++; + buf->tail &= buf->size_mask; + } +} +static inline bool ringbuf_is_empty(ringbuf_t *buf) +{ + return (buf->head == buf->tail); +} +static inline bool ringbuf_is_full(ringbuf_t *buf) +{ + return (((buf->head + 1) & buf->size_mask) == buf->tail); +} +static inline void ringbuf_reset(ringbuf_t *buf) +{ + buf->head = 0; + buf->tail = 0; +} +#endif From ffb52ab0c98540f6305a7c1d82b50c8d6611a727 Mon Sep 17 00:00:00 2001 From: tmk Date: Mon, 10 Sep 2018 09:05:21 +0900 Subject: [PATCH 2/6] core: lufa: Fix console output and init sequence console_putc: Linux: works very well in general Windows: also works very well and connection seems to be faster than Linux Mac: to be: confirmed NOTE: long session of matrix_print still blocks keyboard_task in main loop and prevents it from sending keyboard report. XT protocol buffer overflow occurs when slamng on keys TODO: check when print functions are called in ISR --- tmk_core/protocol/lufa/lufa.c | 218 +++++++++++++++------------------- 1 file changed, 98 insertions(+), 120 deletions(-) diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index 60849aea..4dce8adc 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -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" @@ -89,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) +{ + // return immediately if called while interrupt + if (!(SREG & (1< Date: Thu, 20 Sep 2018 22:14:03 +0900 Subject: [PATCH 3/6] core: lufa: Fix timeout of send_keyboard Change 128*40us(5.12ms) to 128*80us(10.24ms) for 6KRO --- tmk_core/protocol/lufa/lufa.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index 4dce8adc..cfd76c2e 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -468,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 */ From b7d80d8b0e59b697ad1e0f58f9fcb0d1247ef46e Mon Sep 17 00:00:00 2001 From: tmk Date: Sun, 23 Sep 2018 11:40:06 +0900 Subject: [PATCH 4/6] core: lufa: Fix wait for console startup --- tmk_core/protocol/lufa/lufa.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index cfd76c2e..b1567fa9 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -634,15 +634,19 @@ int main(void) keyboard_init(); /* wait for Console startup */ - // TODO: long delay often works anyhoo but proper startup would be better - // 1000ms delay of hid_listen may affect this - uint16_t delay = 2000; - while (delay--) { -#ifndef INTERRUPT_CONTROL_ENDPOINT - USB_USBTask(); -#endif - _delay_ms(1); + // TODO: 2000ms delay often works anyhoo but proper startup would be better + // 1000ms delay of hid_listen affects this probably + #ifdef CONSOLE_ENABLE + if (debug_enable) { + uint16_t delay = 2000; + while (delay--) { + #ifndef INTERRUPT_CONTROL_ENDPOINT + USB_USBTask(); + #endif + _delay_ms(1); + } } + #endif hook_late_init(); From b6cc5394b86ddc8ada190778032604257bedbf1b Mon Sep 17 00:00:00 2001 From: tmk Date: Tue, 11 Sep 2018 01:11:06 +0900 Subject: [PATCH 5/6] xt_usb: Change ring buffer and control Data line --- tmk_core/protocol/xt_interrupt.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/tmk_core/protocol/xt_interrupt.c b/tmk_core/protocol/xt_interrupt.c index a0c55e9c..ebaa436b 100644 --- a/tmk_core/protocol/xt_interrupt.c +++ b/tmk_core/protocol/xt_interrupt.c @@ -39,10 +39,20 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include -#include "pbuff.h" #include "xt.h" #include "wait.h" #include "print.h" +#include "ringbuf.h" + + +#define BUF_SIZE 16 +static uint8_t buf[BUF_SIZE]; +static ringbuf_t rb = { + .buffer = buf, + .head = 0, + .tail = 0, + .size_mask = BUF_SIZE - 1 +}; void xt_host_init(void) { @@ -69,10 +79,12 @@ void xt_host_init(void) /* get data received by interrupt */ uint8_t xt_host_recv(void) { - if (pbuf_has_data()) { - return pbuf_dequeue(); - } else { + if (ringbuf_is_empty(&rb)) { return 0; + } else { + int16_t d = ringbuf_get(&rb); + XT_DATA_IN(); // ready to receive from keyboard + return d; } } @@ -111,7 +123,11 @@ ISR(XT_INT_VECT) break; } if (state++ == BIT7) { - pbuf_enqueue(data); + ringbuf_put(&rb, data); + if (ringbuf_is_full(&rb)) { + XT_DATA_LO(); // inhibit keyboard sending + print("Full"); + } state = START; data = 0; } From 06e3f8485612e5f7e46711530a63312c5f62a80b Mon Sep 17 00:00:00 2001 From: tmk Date: Sun, 23 Sep 2018 12:09:39 +0900 Subject: [PATCH 6/6] xt_usb: Fix scan code print for debug --- converter/xt_usb/matrix.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/converter/xt_usb/matrix.c b/converter/xt_usb/matrix.c index fe4eebf3..f6784573 100644 --- a/converter/xt_usb/matrix.c +++ b/converter/xt_usb/matrix.c @@ -92,7 +92,7 @@ uint8_t matrix_scan(void) uint8_t code = xt_host_recv(); if (!code) return 0; - xprintf("%02X ", code); + dprintf("%02X ", code); switch (state) { case INIT: switch (code) {