From be8ad46fc966f526be3d164df9ecb257fc0ea06f Mon Sep 17 00:00:00 2001
From: Julian Oes <julian@oes.ch>
Date: Tue, 26 Mar 2019 15:13:18 +0100
Subject: [PATCH] px_uploader.py: write timeout workaround
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This is a workaround for the write timeout that we have seen for some
host computers trying to flash the firmware.

We don't know the root cause of the problem but we do observed the
following:

- For blocking writes with timeout (Pyserial write_timeout=0.5):
  write() throws SerialTimeoutException. In systrace we see that the
  select() call after write waiting for the write to be finished hangs
  and finally times out.
- For blocking writes without timeout (Pyserial write_timeout=None):
  write() hangs indefinitely. In systrace we see that the
  select() call after write waiting for the write to be finished hangs.
- For non-blocking writes:
  write() works but flush() hangs. In systrace we see that
  ioctl(fd, TCSBRK, 1) which is (correctly) triggered by termios tcdrain
  hangs.

Inspecting USB traffic using usbmon, we can see that the data which is
written actually seems to be sent and looking at responses from the
Pixhawk bootloader and the timings it looks like all the data has
arrived.

This workaround uses non-blocking writes without flushing and this
seemed to prevent the issue from happening so far.

Debugging was done in collaboration with Beat Küng and David Sidrane.
---
 Tools/px_uploader.py | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)

diff --git a/Tools/px_uploader.py b/Tools/px_uploader.py
index 6ea4432890..550524de2d 100755
--- a/Tools/px_uploader.py
+++ b/Tools/px_uploader.py
@@ -205,7 +205,7 @@ class uploader(object):
         self.window_max = 256
         self.window_per = 2  # Sync,<result>
         self.ackWindowedMode = False  # Assume Non Widowed mode for all USB CDC
-        self.port = serial.Serial(portname, baudrate_bootloader, timeout=0.5, write_timeout=0.5)
+        self.port = serial.Serial(portname, baudrate_bootloader, timeout=0.5, write_timeout=0)
         self.otp = b''
         self.sn = b''
         self.baudrate_bootloader = baudrate_bootloader
@@ -247,14 +247,7 @@ class uploader(object):
 
     def __send(self, c):
         # print("send " + binascii.hexlify(c))
-        while True:
-            try:
-                self.port.write(c)
-                break
-            except serial.SerialTimeoutException as e:
-                print("Write timeout (%s), trying again" % e)
-                time.sleep(0.04)
-                continue
+        self.port.write(c)
 
     def __recv(self, count=1):
         c = self.port.read(count)
@@ -440,7 +433,7 @@ class uploader(object):
         self.__send(data)
         self.__send(uploader.EOC)
         if (not windowMode):
-            self.__getSync()
+            self.__getSync(False)
         else:
             # The following is done to have minimum delay on the transmission
             # of the ne fw. The per block cost of __getSync was about 16 mS per.
-- 
GitLab