From 2958b568a30399937e7f578a0040255731592e52 Mon Sep 17 00:00:00 2001 From: Enrik Berkhan Date: Mon, 7 Sep 2020 23:15:35 +0200 Subject: [PATCH] DFU mode: improve programming robustness. dfu.get_status() might return usb.core.USBError with errno == EPIPE, when called in quick succession. This is the case when called from dfu.block_on_state(). The next call usually works. This change tries to catch this specific error and just re-tries in case it happens. Without this change, the error (and programming abort) might get undetected, as click catches IOerror/EPIPE silently. This has significant potential to brick a device. --- solo/dfu.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/solo/dfu.py b/solo/dfu.py index a6e1bdc..0ad9cf7 100644 --- a/solo/dfu.py +++ b/solo/dfu.py @@ -7,6 +7,7 @@ # http://opensource.org/licenses/MIT>, at your option. This file may not be # copied, modified, or distributed except according to those terms. +import errno import struct import time @@ -210,7 +211,10 @@ def block_on_state(self, state): s = self.get_status() while s.state == state: time.sleep(s.timeout / 1000.0) - s = self.get_status() + try: + s = self.get_status() + except usb.core.USBError as e: + if e.errno != errno.EPIPE: raise def read_option_bytes(self,): ptr = 0x1FFF7800 # option byte address for STM32l432