From 8ea957b8e1db802b14409df92d2bf55058edd4f4 Mon Sep 17 00:00:00 2001 From: tychovrahe Date: Thu, 20 Mar 2025 14:47:25 +0100 Subject: [PATCH] feat(core/prodtest): add hibernation on power button long-press [no changelog] --- core/embed/io/rgb_led/inc/io/rgb_led.h | 5 +++ .../projects/prodtest/cmd/prodtest_rgbled.c | 5 +++ core/embed/projects/prodtest/main.c | 40 +++++++++++++++++++ core/embed/projects/prodtest/prodtest.h | 23 +++++++++++ 4 files changed, 73 insertions(+) create mode 100644 core/embed/projects/prodtest/prodtest.h diff --git a/core/embed/io/rgb_led/inc/io/rgb_led.h b/core/embed/io/rgb_led/inc/io/rgb_led.h index 02c425f611..8f12c00402 100644 --- a/core/embed/io/rgb_led/inc/io/rgb_led.h +++ b/core/embed/io/rgb_led/inc/io/rgb_led.h @@ -32,6 +32,11 @@ void rgb_led_deinit(void); #endif +#define RGBLED_GREEN 0x00FF00 +#define RGBLED_RED 0xFF0000 +#define RGBLED_BLUE 0x0000FF +#define RGBLED_YELLOW 0xFFFF00 + // Set RGB LED color // color: 24-bit RGB color, 0x00RRGGBB void rgb_led_set_color(uint32_t color); diff --git a/core/embed/projects/prodtest/cmd/prodtest_rgbled.c b/core/embed/projects/prodtest/cmd/prodtest_rgbled.c index 56ac01ef66..63b5756197 100644 --- a/core/embed/projects/prodtest/cmd/prodtest_rgbled.c +++ b/core/embed/projects/prodtest/cmd/prodtest_rgbled.c @@ -24,6 +24,8 @@ #include #include +#include "../prodtest.h" + static void prodtest_rgbled_set(cli_t* cli) { uint32_t r = 0; uint32_t g = 0; @@ -51,6 +53,9 @@ static void prodtest_rgbled_set(cli_t* cli) { cli_trace(cli, "Setting the RGB LED color to [%d, %d, %d]...", r, g, b); + // Disable automatic control of RGB LED in prodtest main loop + prodtest_disable_rgbled_control(); + uint32_t rgb = (r << 16) | (g << 8) | b; rgb_led_set_color(rgb); diff --git a/core/embed/projects/prodtest/main.c b/core/embed/projects/prodtest/main.c index 292580eaca..8482274309 100644 --- a/core/embed/projects/prodtest/main.c +++ b/core/embed/projects/prodtest/main.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -176,6 +177,11 @@ static void show_welcome_screen(void) { } } +// Set if the RGB LED must not be controlled by the main loop +static bool g_rgbled_control_disabled = false; + +void prodtest_disable_rgbled_control(void) { g_rgbled_control_disabled = true; } + static void drivers_init(void) { #ifdef USE_POWERCTL powerctl_init(); @@ -248,10 +254,44 @@ int main(void) { pair_optiga(&g_cli); #endif +#if defined USE_BUTTON && defined USE_POWERCTL + uint32_t btn_deadline = 0; +#endif + +#ifdef USE_RGB_LED + uint32_t led_start_deadline = ticks_timeout(1000); + rgb_led_set_color(RGBLED_GREEN); +#endif + while (true) { if (usb_vcp_can_read(VCP_IFACE)) { cli_process_io(&g_cli); } + +#if defined USE_BUTTON && defined USE_POWERCTL + button_event_t btn_event = {0}; + if (button_get_event(&btn_event) && btn_event.button == BTN_POWER) { + if (btn_event.event_type == BTN_EVENT_DOWN) { + btn_deadline = ticks_timeout(1000); + } else if (btn_event.event_type == BTN_EVENT_UP) { + if (ticks_expired(btn_deadline)) { + powerctl_hibernate(); + rgb_led_set_color(RGBLED_YELLOW); + systick_delay_ms(1000); + rgb_led_set_color(0); + } + } + } + if (button_is_down(BTN_POWER) && ticks_expired(btn_deadline)) { + rgb_led_set_color(RGBLED_RED); + } +#endif + +#ifdef USE_RGB_LED + if (ticks_expired(led_start_deadline) && !g_rgbled_control_disabled) { + rgb_led_set_color(0); + } +#endif } return 0; diff --git a/core/embed/projects/prodtest/prodtest.h b/core/embed/projects/prodtest/prodtest.h new file mode 100644 index 0000000000..1f319145cd --- /dev/null +++ b/core/embed/projects/prodtest/prodtest.h @@ -0,0 +1,23 @@ +/* + * This file is part of the Trezor project, https://trezor.io/ + * + * Copyright (c) SatoshiLabs + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +// Disable automatic control of RGB LED in prodtest main loop +void prodtest_disable_rgbled_control(void);