arduino-cmsis-dap icon indicating copy to clipboard operation
arduino-cmsis-dap copied to clipboard

Teensy hangs after a few commands due to USB serial port buffering

Open osresearch opened this issue 5 years ago • 1 comments

The firmware defaults to enabling DAP_SERIAL_LOG in DAP_config.h, which is the USB serial port on the Teensy. Things will work until some number of bytes have been buffered, and then the Teensy will hang waiting for a USB host to drain the buffer, which will never happen since the firmware is built in Raw HID mode and there is no where for these bytes to go.

image

This was a challenge to debug since I wasn't sure if I could trust the Teensy, the jumper cables, the devices under test, etc.

My quick fix was to move the debugging to Serial1, which is the hardware serial port, so that I could use the logging if I run into additional problems with OpenOCD.

diff --git a/arduino-cmsis-dap.ino b/arduino-cmsis-dap.ino
index fbb2c7d..abebf8d 100644
--- a/arduino-cmsis-dap.ino
+++ b/arduino-cmsis-dap.ino
@@ -92,7 +92,7 @@ uint8_t rawhidResponse[DAP_PACKET_SIZE];
 
 
 void setup() {
-  Serial.begin(115200);
+  Serial1.begin(115200);
 
   DAP_Setup();
 
@@ -113,17 +113,17 @@ void loop() {
 #endif
   if (bytesAvailable > 0) {
 #if DAP_SERIAL_LOG
-    Serial.print("cmd ");
-    Serial.print(rawhidRequest[0], HEX);
-    Serial.print(" ");
-    Serial.print(rawhidRequest[1], HEX);
-    Serial.print(" ");
+    Serial1.print("cmd ");
+    Serial1.print(rawhidRequest[0], HEX);
+    Serial1.print(" ");
+    Serial1.print(rawhidRequest[1], HEX);
+    Serial1.print(" ");
 #endif /* DAP_SERIAL_LOG */
     auto sz = DAP_ProcessCommand(rawhidRequest, rawhidResponse);
 #if DAP_SERIAL_LOG
-    Serial.print("rsp ");
-    Serial.print(sz);
-    Serial.println(" B");
+    Serial1.print("rsp ");
+    Serial1.print(sz);
+    Serial1.println(" B");
 #endif /* DAP_SERIAL_LOG */
 #ifdef HIDPROJECT_RAWHID
     RawHID.enable(); // signal that we're ready to receive another buffer

osresearch avatar Nov 02 '19 15:11 osresearch

Thank you! I haven't touched this project in years; I'm glad to hear it was helpful (if buggy) for someone. I'll see about getting this code in.

myelin avatar Nov 19 '19 19:11 myelin