From b9647d1296bd0091073e5d14466eb3af6fd81672 Mon Sep 17 00:00:00 2001 From: tychovrahe Date: Wed, 31 Aug 2022 13:58:50 +0200 Subject: [PATCH] fix(core): fix flashing usb disconnected error [no changelog] --- core/embed/extmod/modtrezorio/modtrezorio.c | 2 +- core/embed/trezorhal/usb.c | 18 ++++++++++++++++++ core/src/apps/homescreen/homescreen.py | 10 ++++------ core/src/trezor/ui/__init__.py | 12 ++++++------ core/src/trezor/ui/components/tt/recovery.py | 2 +- 5 files changed, 30 insertions(+), 14 deletions(-) diff --git a/core/embed/extmod/modtrezorio/modtrezorio.c b/core/embed/extmod/modtrezorio/modtrezorio.c index 20c2f748d..5dcaededc 100644 --- a/core/embed/extmod/modtrezorio/modtrezorio.c +++ b/core/embed/extmod/modtrezorio/modtrezorio.c @@ -32,7 +32,7 @@ #include "usb.h" // Whether USB data pins were connected on last check (USB configured) -__attribute__((unused)) static bool usb_connected_previously = false; +bool usb_connected_previously = true; #define CHECK_PARAM_RANGE(value, minimum, maximum) \ if (value < minimum || value > maximum) { \ diff --git a/core/embed/trezorhal/usb.c b/core/embed/trezorhal/usb.c index 45a66bc4c..0feaac1c1 100644 --- a/core/embed/trezorhal/usb.c +++ b/core/embed/trezorhal/usb.c @@ -153,8 +153,12 @@ void usb_start(void) { USBD_Start(&usb_dev_handle); } void usb_stop(void) { USBD_Stop(&usb_dev_handle); } secbool usb_configured(void) { + static uint32_t usb_configured_last_ok = 0; + uint32_t ticks = hal_ticks_ms(); + const USBD_HandleTypeDef *pdev = &usb_dev_handle; if (pdev->dev_state == USBD_STATE_CONFIGURED) { + usb_configured_last_ok = hal_ticks_ms(); return sectrue; } @@ -163,6 +167,20 @@ secbool usb_configured(void) { // configured. if (pdev->dev_state == USBD_STATE_SUSPENDED && pdev->dev_old_state == USBD_STATE_CONFIGURED) { + usb_configured_last_ok = hal_ticks_ms(); + return sectrue; + } + + if (usb_configured_last_ok == 0) { + usb_configured_last_ok = ticks; + return sectrue; + } + if (usb_configured_last_ok > ticks) { + // probably overflow of 32bit ms counter, ignore as its just once in a long + // time + return sectrue; + } + if ((hal_ticks_ms() - usb_configured_last_ok) < 2000) { return sectrue; } diff --git a/core/src/apps/homescreen/homescreen.py b/core/src/apps/homescreen/homescreen.py index 9db89d9ff..b98ab6cc5 100644 --- a/core/src/apps/homescreen/homescreen.py +++ b/core/src/apps/homescreen/homescreen.py @@ -26,7 +26,6 @@ class Homescreen(HomescreenBase): def __init__(self) -> None: super().__init__() - self.is_connected = False if not storage.device.is_initialized(): self.label = "Go to trezor.io/start" @@ -44,10 +43,8 @@ class Homescreen(HomescreenBase): async def usb_checker_task(self) -> None: usbcheck = loop.wait(io.USB_CHECK) while True: - is_connected = await usbcheck - if is_connected != self.is_connected: - self.is_connected = is_connected - self.set_repaint(True) + await usbcheck + self.set_repaint(True) def do_render(self) -> None: # warning bar on top @@ -62,7 +59,7 @@ class Homescreen(HomescreenBase): elif storage.device.get_experimental_features(): ui.header_warning("EXPERIMENTAL MODE!") else: - ui.display.bar(0, 0, ui.WIDTH, ui.HEIGHT, ui.BG) + ui.display.bar(0, 0, ui.WIDTH, ui.get_header_height(), ui.BG) # homescreen with shifted avatar and text on bottom # Differs for each model @@ -95,6 +92,7 @@ class Homescreen(HomescreenBase): def on_touch_end(self, _x: int, _y: int) -> None: if self.loader.start_ms is not None: + ui.display.clear() self.set_repaint(True) self.loader.stop() self.touch_ms = None diff --git a/core/src/trezor/ui/__init__.py b/core/src/trezor/ui/__init__.py index 4e3fe43f0..7bd3d8793 100644 --- a/core/src/trezor/ui/__init__.py +++ b/core/src/trezor/ui/__init__.py @@ -148,7 +148,7 @@ MODEL_HEADER_HEIGHTS = {"1": 12, "R": 15, "T": 30} MODEL_Y_BASELINES = {"1": 10, "R": 11, "T": 22} -def header_warning(message: str, clear: bool = True) -> None: +def header_warning(message: str) -> None: height = MODEL_HEADER_HEIGHTS[utils.MODEL] y_baseline = MODEL_Y_BASELINES[utils.MODEL] @@ -156,18 +156,18 @@ def header_warning(message: str, clear: bool = True) -> None: display.text_center( WIDTH // 2, y_baseline, message, BOLD, style.BLACK, style.YELLOW ) - if clear: - display.bar(0, height, WIDTH, HEIGHT - height, style.BG) -def header_error(message: str, clear: bool = True) -> None: +def header_error(message: str) -> None: height = MODEL_HEADER_HEIGHTS[utils.MODEL] y_baseline = MODEL_Y_BASELINES[utils.MODEL] display.bar(0, 0, WIDTH, height, style.RED) display.text_center(WIDTH // 2, y_baseline, message, BOLD, style.WHITE, style.RED) - if clear: - display.bar(0, height, WIDTH, HEIGHT - height, style.BG) + + +def get_header_height() -> int: + return MODEL_HEADER_HEIGHTS[utils.MODEL] def draw_simple(t: "Component") -> None: diff --git a/core/src/trezor/ui/components/tt/recovery.py b/core/src/trezor/ui/components/tt/recovery.py index 27365717b..480e8c7d5 100644 --- a/core/src/trezor/ui/components/tt/recovery.py +++ b/core/src/trezor/ui/components/tt/recovery.py @@ -16,7 +16,7 @@ class RecoveryHomescreen(ui.Component): heading = "SEED CHECK" else: heading = "RECOVERY MODE" - ui.header_warning(heading, clear=False) + ui.header_warning(heading) if not self.subtext: ui.display.text_center(ui.WIDTH // 2, 80, self.text, ui.BOLD, ui.FG, ui.BG)