add timeout to usb serial send otherwise we can get watchdog hits
authorJim Morris <morris@wolfman.com>
Tue, 2 Oct 2018 20:46:58 +0000 (21:46 +0100)
committerJim Morris <morris@wolfman.com>
Tue, 2 Oct 2018 20:46:58 +0000 (21:46 +0100)
src/libs/USBDevice/USBSerial/USBSerial.cpp
src/libs/USBDevice/USBSerial/USBSerial.h
src/modules/communication/GcodeDispatch.cpp
src/modules/utils/player/Player.cpp

index c308d67..95fc8f3 100644 (file)
@@ -25,6 +25,8 @@
 #include "libs/SerialMessage.h"
 #include "StreamOutputPool.h"
 
+#include "mbed.h"
+
 // extern void setled(int, bool);
 #define setled(a, b) do {} while (0)
 
@@ -41,20 +43,28 @@ USBSerial::USBSerial(USB *u): USBCDC(u), rxbuf(256 + 8), txbuf(128 + 8)
     last_char_was_dollar = false;
 }
 
-void USBSerial::ensure_tx_space(int space)
+bool USBSerial::ensure_tx_space(int space)
 {
+    // we need some kind of timeout here or it will hang if upstream stalls
+    uint32_t start = us_ticker_read();
     while (txbuf.free() < space) {
+        if((us_ticker_read() - start) > 1000000) {
+            // 1 second timeout
+            return false;
+        }
         usb->endpointSetInterrupt(CDC_BulkIn.bEndpointAddress, true);
         usb->usbisr();
     }
+    return true;
 }
 
 int USBSerial::_putc(int c)
 {
     if (!attached)
         return 1;
-    ensure_tx_space(1);
-    txbuf.queue(c);
+    if(ensure_tx_space(1)) {
+        txbuf.queue(c);
+    }
 
     usb->endpointSetInterrupt(CDC_BulkIn.bEndpointAddress, true);
     return 1;
@@ -91,7 +101,7 @@ int USBSerial::puts(const char *str)
         return strlen(str);
     int i = 0;
     while (*str) {
-        ensure_tx_space(1);
+        if(!ensure_tx_space(1)) break;
         txbuf.queue(*str);
         if ((txbuf.available() % 64) == 0)
             usb->endpointSetInterrupt(CDC_BulkIn.bEndpointAddress, true);
index bf4669a..ec4b126 100644 (file)
@@ -61,7 +61,7 @@ protected:
     virtual void on_attach(void);\r
     virtual void on_detach(void);\r
 \r
-    void ensure_tx_space(int);\r
+    bool ensure_tx_space(int);\r
 \r
     // keep track of number of newlines in the buffer\r
     // this makes it trivial to detect if there's a new line available\r
index 566df56..78a1207 100644 (file)
@@ -31,7 +31,7 @@
 #define panel_checksum             CHECKSUM("panel")
 
 // goes in Flash, list of Mxxx codes that are allowed when in Halted state
-static const int allowed_mcodes[]= {2,5,9,30,105,114,119,80,81,911,503,106,107}; // get temp, get pos, get endstops etc
+static const int allowed_mcodes[]= {2,5,9,30,105,114,115,119,80,81,911,503,106,107}; // get temp, get pos, get endstops etc
 static bool is_allowed_mcode(int m) {
     for (size_t i = 0; i < sizeof(allowed_mcodes)/sizeof(int); ++i) {
         if(allowed_mcodes[i] == m) return true;
index ff22b4e..fe35307 100644 (file)
@@ -521,7 +521,7 @@ void Player::suspend_command(string parameters, StreamOutput *stream )
         return;
     }
 
-    stream->printf("Suspending print, waiting for queue to empty...\n");
+    stream->printf("// Suspending print, waiting for queue to empty...\n");
 
     // override the leave_heaters_on setting
     this->override_leave_heaters_on= (parameters == "h");
@@ -615,7 +615,7 @@ void Player::resume_command(string parameters, StreamOutput *stream )
         return;
     }
 
-    stream->printf("resuming print...\n");
+    stream->printf("// resuming print...\n");
 
     // wait for them to reach temp
     if(!this->saved_temperatures.empty()) {
@@ -624,7 +624,7 @@ void Player::resume_command(string parameters, StreamOutput *stream )
             float t= h.second;
             PublicData::set_value( temperature_control_checksum, h.first, &t );
         }
-        stream->printf("Waiting for heaters...\n");
+        stream->printf("// Waiting for heaters...\n");
         bool wait= true;
         uint32_t tus= us_ticker_read(); // mbed call
         while(wait) {
@@ -672,7 +672,7 @@ void Player::resume_command(string parameters, StreamOutput *stream )
 
     // execute optional gcode if defined
     if(!before_resume_gcode.empty()) {
-        stream->printf("Executing before resume gcode...\n");
+        stream->printf("// Executing before resume gcode...\n");
         struct SerialMessage message;
         message.message = before_resume_gcode;
         message.stream = &(StreamOutput::NullStream);
@@ -680,7 +680,7 @@ void Player::resume_command(string parameters, StreamOutput *stream )
     }
 
     // Restore position
-    stream->printf("Restoring saved XYZ positions and state...\n");
+    stream->printf("// Restoring saved XYZ positions and state...\n");
     THEROBOT->pop_state();
     bool abs_mode= THEROBOT->absolute_mode; // what mode we were in
     // force absolute mode for restoring position, then set to the saved relative/absolute mode
@@ -705,7 +705,7 @@ void Player::resume_command(string parameters, StreamOutput *stream )
         return;
     }
 
-    stream->printf("Resuming print\n");
+    stream->printf("// Resuming print\n");
 
     if(this->was_playing_file) {
         this->playing_file = true;