Browse Source

Fix ChibiOS virtual serial short packet receive (#26356)

* Core: fix ChibiOS virtual serial short packet receive

Signed-off-by: Giridhar <giridharpavan593@gmail.com>

* Add handwired/onekey virtser test keymap

---------

Signed-off-by: Giridhar <giridharpavan593@gmail.com>
Co-authored-by: Giridhar <giridharpavan593@gmail.com>
Co-authored-by: zvecr <git@zvecr.com>
Giridhar 2 days ago
parent
commit
bc744bbcaa

+ 40 - 0
keyboards/handwired/onekey/keymaps/virtser/keymap.c

@@ -0,0 +1,40 @@
+// Copyright 2026 QMK
+// SPDX-License-Identifier: GPL-2.0-or-later
+#include QMK_KEYBOARD_H
+#include "virtser.h"
+
+const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
+    LAYOUT_ortho_1x1(KC_A)
+};
+
+static uint32_t counter = 0;
+
+void virtser_recv(const uint8_t ch) {
+    static const char *lut = "0123456789ABCDEF";
+
+    counter++;
+
+    virtser_send(lut[(ch & 0xF0) >> 4]);
+    virtser_send(lut[ch & 0xF]);
+}
+
+#ifdef CONSOLE_ENABLE
+
+void keyboard_post_init_user(void) {
+    // Customise these values to desired behaviour
+    debug_enable = true;
+    // debug_matrix=true;
+    // debug_keyboard=true;
+    // debug_mouse=true;
+}
+
+void housekeeping_task_user(void) {
+    static uint32_t last = 0;
+    if (timer_elapsed32(last) > 1000) {
+        uprintf("recv: %ld!\n", counter);
+
+        last = timer_read32();
+    }
+}
+
+#endif

+ 12 - 0
keyboards/handwired/onekey/keymaps/virtser/keymap.json

@@ -0,0 +1,12 @@
+{
+    "config": {
+        "usb": {
+            "shared_endpoint": {
+                "keyboard": true
+            }
+        },
+        "features": {
+            "virtser": true
+        }
+    }
+}

+ 7 - 3
tmk_core/protocol/chibios/usb_driver.c

@@ -320,13 +320,13 @@ bool usb_endpoint_in_is_inactive(usb_endpoint_in_t *endpoint) {
     return inactive;
     return inactive;
 }
 }
 
 
-bool usb_endpoint_out_receive(usb_endpoint_out_t *endpoint, uint8_t *data, size_t size, sysinterval_t timeout) {
+size_t usb_endpoint_out_receive_bytes(usb_endpoint_out_t *endpoint, uint8_t *data, size_t size, sysinterval_t timeout) {
     osalDbgCheck((endpoint != NULL) && (data != NULL) && (size > 0U));
     osalDbgCheck((endpoint != NULL) && (data != NULL) && (size > 0U));
 
 
     osalSysLock();
     osalSysLock();
     if (usbGetDriverStateI(endpoint->config.usbp) != USB_ACTIVE) {
     if (usbGetDriverStateI(endpoint->config.usbp) != USB_ACTIVE) {
         osalSysUnlock();
         osalSysUnlock();
-        return false;
+        return 0;
     }
     }
 
 
     if (endpoint->timed_out && timeout != TIME_INFINITE) {
     if (endpoint->timed_out && timeout != TIME_INFINITE) {
@@ -337,5 +337,9 @@ bool usb_endpoint_out_receive(usb_endpoint_out_t *endpoint, uint8_t *data, size_
     const size_t received = ibqReadTimeout(&endpoint->ibqueue, data, size, timeout);
     const size_t received = ibqReadTimeout(&endpoint->ibqueue, data, size, timeout);
     endpoint->timed_out   = received == 0;
     endpoint->timed_out   = received == 0;
 
 
-    return received == size;
+    return received;
+}
+
+bool usb_endpoint_out_receive(usb_endpoint_out_t *endpoint, uint8_t *data, size_t size, sysinterval_t timeout) {
+    return usb_endpoint_out_receive_bytes(endpoint, data, size, timeout) == size;
 }
 }

+ 2 - 1
tmk_core/protocol/chibios/usb_driver.h

@@ -197,7 +197,8 @@ void usb_endpoint_out_init(usb_endpoint_out_t *endpoint);
 void usb_endpoint_out_start(usb_endpoint_out_t *endpoint);
 void usb_endpoint_out_start(usb_endpoint_out_t *endpoint);
 void usb_endpoint_out_stop(usb_endpoint_out_t *endpoint);
 void usb_endpoint_out_stop(usb_endpoint_out_t *endpoint);
 
 
-bool usb_endpoint_out_receive(usb_endpoint_out_t *endpoint, uint8_t *data, size_t size, sysinterval_t timeout);
+bool   usb_endpoint_out_receive(usb_endpoint_out_t *endpoint, uint8_t *data, size_t size, sysinterval_t timeout);
+size_t usb_endpoint_out_receive_bytes(usb_endpoint_out_t *endpoint, uint8_t *data, size_t size, sysinterval_t timeout);
 
 
 void usb_endpoint_out_suspend_cb(usb_endpoint_out_t *endpoint);
 void usb_endpoint_out_suspend_cb(usb_endpoint_out_t *endpoint);
 void usb_endpoint_out_wakeup_cb(usb_endpoint_out_t *endpoint);
 void usb_endpoint_out_wakeup_cb(usb_endpoint_out_t *endpoint);

+ 3 - 2
tmk_core/protocol/chibios/usb_main.c

@@ -584,8 +584,9 @@ __attribute__((weak)) void virtser_recv(uint8_t c) {
 
 
 void virtser_task(void) {
 void virtser_task(void) {
     uint8_t buffer[CDC_EPSIZE];
     uint8_t buffer[CDC_EPSIZE];
-    while (receive_report(USB_ENDPOINT_OUT_CDC_DATA, buffer, sizeof(buffer))) {
-        for (int i = 0; i < sizeof(buffer); i++) {
+    size_t  received;
+    while ((received = usb_endpoint_out_receive_bytes(&usb_endpoints_out[USB_ENDPOINT_OUT_CDC_DATA], buffer, sizeof(buffer), TIME_IMMEDIATE)) > 0) {
+        for (size_t i = 0; i < received; i++) {
             virtser_recv(buffer[i]);
             virtser_recv(buffer[i]);
         }
         }
     }
     }