keyboardio / Kaleidoscope-Bundle-Keyboardio

A Kaleidoscope distribution for the Keyboardio Model 01 and other keyboards.
Other
17 stars 24 forks source link

Import the AVR arduino core. #48

Closed obra closed 1 year ago

obra commented 1 year ago

Arduino's AVR core has a bug where it locks up USB serial when it receives a 384 byte serial packet from macOS due to a bug in ZLP handling. This is fixed by patch Arduino/Arduino#6886, which has been unapplied for the past 6 years.

This PR forks the Arduino AVR core so that our keyboards don't lock up when programmed on macOS.

From 34234c2c6a4cb77a60254e3db4c2d08d912684e6 Mon Sep 17 00:00:00 2001
From: Sandeep Mistry <s.mistry@bcmi-labs.cc>
Date: Fri, 3 Nov 2017 12:03:54 -0400
Subject: [PATCH] Handle receiving a ZLP in USB_Available

---
 cores/arduino/USBCore.cpp | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/hardware/arduino/avr/cores/arduino/USBCore.cpp b/hardware/arduino/avr/cores/arduino/USBCore.cpp
index e00fb028e22..4c5c50bfd16 100644
--- a/cores/arduino/USBCore.cpp
+++ b/cores/arduino/USBCore.cpp
@@ -171,6 +171,11 @@ static inline u8 FifoFree()
        return UEINTX & (1<<FIFOCON);
 }

+static inline u8 HasOUT()
+{
+       return UEINTX & (1<<RXOUTI);
+}
+
 static inline void ReleaseRX()
 {
        UEINTX = 0x6B;  // FIFOCON=0 NAKINI=1 RWAL=1 NAKOUTI=0 RXSTPI=1 RXOUTI=0 STALLEDI=1 TXINI=1
@@ -214,7 +219,12 @@ class LockEP
 u8 USB_Available(u8 ep)
 {
        LockEP lock(ep);
-       return FifoByteCount();
+       u8 n = FifoByteCount();
+
+       if (!n && HasOUT())
+               ReleaseRX(); // handle ZLP
+
+       return n;
 }

 //     Non Blocking receive