Skip to content

Commit

Permalink
feat(core/prodtest): add hw-revision command and driver
Browse files Browse the repository at this point in the history
  • Loading branch information
TychoVrahe committed Mar 3, 2025
1 parent 13d9d01 commit 519bce6
Show file tree
Hide file tree
Showing 15 changed files with 283 additions and 2 deletions.
3 changes: 2 additions & 1 deletion core/SConscript.prodtest
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ BOOTLOADER_DEVEL = ARGUMENTS.get('BOOTLOADER_DEVEL', '0') == '1'
HW_REVISION = ARGUMENTS.get('HW_REVISION', None)
UI_DEBUG_OVERLAY = ARGUMENTS.get('UI_DEBUG_OVERLAY', '0') == '1'

FEATURES_WANTED = ["input", "sbu", "nfc", "sd_card", "rgb_led", "usb", "consumption_mask", "optiga", "haptic", "tropic", "ble"]
FEATURES_WANTED = ["input", "sbu", "nfc", "sd_card", "rgb_led", "usb", "consumption_mask", "optiga", "haptic", "tropic", "ble", "hw_revision"]

CCFLAGS_MOD = ''
CPPPATH_MOD = []
Expand Down Expand Up @@ -108,6 +108,7 @@ SOURCE_PRODTEST = [
'embed/projects/prodtest/cmd/prodtest_get_cpuid.c',
'embed/projects/prodtest/cmd/prodtest_haptic.c',
'embed/projects/prodtest/cmd/prodtest_help.c',
'embed/projects/prodtest/cmd/prodtest_hw_revision.c',
'embed/projects/prodtest/cmd/prodtest_nfc.c',
'embed/projects/prodtest/cmd/prodtest_nrf.c',
'embed/projects/prodtest/cmd/prodtest_optiga.c',
Expand Down
14 changes: 14 additions & 0 deletions core/embed/models/T3B1/boards/trezor_t3b1_revB.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,18 @@
#define SBU_2_PORT GPIOA
#define SBU_2_CLK_ENA __HAL_RCC_GPIOA_CLK_ENABLE

#define HW_REVISION_PUPD GPIO_PULLUP
#define HW_REVISION_0_PIN GPIO_PIN_0
#define HW_REVISION_0_PORT GPIOF
#define HW_REVISION_0_CLOCK_ENABLE() __HAL_RCC_GPIOF_CLK_ENABLE()
#define HW_REVISION_1_PIN GPIO_PIN_1
#define HW_REVISION_1_PORT GPIOF
#define HW_REVISION_1_CLOCK_ENABLE() __HAL_RCC_GPIOF_CLK_ENABLE()
#define HW_REVISION_2_PIN GPIO_PIN_2
#define HW_REVISION_2_PORT GPIOF
#define HW_REVISION_2_CLOCK_ENABLE() __HAL_RCC_GPIOF_CLK_ENABLE()
#define HW_REVISION_3_PIN GPIO_PIN_3
#define HW_REVISION_3_PORT GPIOF
#define HW_REVISION_3_CLOCK_ENABLE() __HAL_RCC_GPIOF_CLK_ENABLE()

#endif //_TREZOR_T3B1_H
16 changes: 15 additions & 1 deletion core/embed/models/T3T1/boards/trezor_t3t1_revE.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,18 @@
#define SBU_2_PORT GPIOA
#define SBU_2_CLK_ENA __HAL_RCC_GPIOA_CLK_ENABLE

#endif //_TREZOR_T_H
#define HW_REVISION_PUPD GPIO_PULLUP
#define HW_REVISION_0_PIN GPIO_PIN_0
#define HW_REVISION_0_PORT GPIOF
#define HW_REVISION_0_CLOCK_ENABLE() __HAL_RCC_GPIOF_CLK_ENABLE()
#define HW_REVISION_1_PIN GPIO_PIN_1
#define HW_REVISION_1_PORT GPIOF
#define HW_REVISION_1_CLOCK_ENABLE() __HAL_RCC_GPIOF_CLK_ENABLE()
#define HW_REVISION_2_PIN GPIO_PIN_2
#define HW_REVISION_2_PORT GPIOF
#define HW_REVISION_2_CLOCK_ENABLE() __HAL_RCC_GPIOF_CLK_ENABLE()
#define HW_REVISION_3_PIN GPIO_PIN_3
#define HW_REVISION_3_PORT GPIOF
#define HW_REVISION_3_CLOCK_ENABLE() __HAL_RCC_GPIOF_CLK_ENABLE()

#endif //_TREZOR_T3T1_H
11 changes: 11 additions & 0 deletions core/embed/models/T3W1/boards/trezor_t3w1_revA.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,15 @@
#define NFC_EXTI_INTERRUPT_NUM EXTI10_IRQn
#define NFC_EXTI_INTERRUPT_HANDLER EXTI10_IRQHandler

#define HW_REVISION_PUPD GPIO_PULLDOWN
#define HW_REVISION_0_PIN GPIO_PIN_1
#define HW_REVISION_0_PORT GPIOI
#define HW_REVISION_0_CLOCK_ENABLE() __HAL_RCC_GPIOI_CLK_ENABLE()
#define HW_REVISION_1_PIN GPIO_PIN_2
#define HW_REVISION_1_PORT GPIOI
#define HW_REVISION_1_CLOCK_ENABLE() __HAL_RCC_GPIOI_CLK_ENABLE()
#define HW_REVISION_2_PIN GPIO_PIN_3
#define HW_REVISION_2_PORT GPIOI
#define HW_REVISION_2_CLOCK_ENABLE() __HAL_RCC_GPIOI_CLK_ENABLE()

#endif // TREZOR_T3W1_REVA_H_
11 changes: 11 additions & 0 deletions core/embed/models/T3W1/boards/trezor_t3w1_revA0.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,15 @@
#define NRF_OUT_FW_RUNNING_PORT GPIOE
#define NRF_OUT_FW_RUNNING_CLK_ENA __HAL_RCC_GPIOE_CLK_ENABLE

#define HW_REVISION_PUPD GPIO_PULLDOWN
#define HW_REVISION_0_PIN GPIO_PIN_1
#define HW_REVISION_0_PORT GPIOI
#define HW_REVISION_0_CLOCK_ENABLE() __HAL_RCC_GPIOI_CLK_ENABLE()
#define HW_REVISION_1_PIN GPIO_PIN_2
#define HW_REVISION_1_PORT GPIOI
#define HW_REVISION_1_CLOCK_ENABLE() __HAL_RCC_GPIOI_CLK_ENABLE()
#define HW_REVISION_2_PIN GPIO_PIN_3
#define HW_REVISION_2_PORT GPIOI
#define HW_REVISION_2_CLOCK_ENABLE() __HAL_RCC_GPIOI_CLK_ENABLE()

#endif // TREZOR_T3W1_REVA_H_
11 changes: 11 additions & 0 deletions core/embed/models/T3W1/boards/trezor_t3w1_revB.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,15 @@
#define NFC_EXTI_INTERRUPT_NUM EXTI10_IRQn
#define NFC_EXTI_INTERRUPT_HANDLER EXTI10_IRQHandler

#define HW_REVISION_PUPD GPIO_PULLDOWN
#define HW_REVISION_0_PIN GPIO_PIN_1
#define HW_REVISION_0_PORT GPIOI
#define HW_REVISION_0_CLOCK_ENABLE() __HAL_RCC_GPIOI_CLK_ENABLE()
#define HW_REVISION_1_PIN GPIO_PIN_2
#define HW_REVISION_1_PORT GPIOI
#define HW_REVISION_1_CLOCK_ENABLE() __HAL_RCC_GPIOI_CLK_ENABLE()
#define HW_REVISION_2_PIN GPIO_PIN_3
#define HW_REVISION_2_PORT GPIOI
#define HW_REVISION_2_CLOCK_ENABLE() __HAL_RCC_GPIOI_CLK_ENABLE()

#endif // TREZOR_T3W1_REVA_H_
1 change: 1 addition & 0 deletions core/embed/projects/prodtest/.changelog.d/4682.added
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[T3B1,T3T1] Added hw-revision command.
9 changes: 9 additions & 0 deletions core/embed/projects/prodtest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,15 @@ haptic-test 3000
OK
```

### hw-revision
Retrieves the hardware revision of the device. The command returns `OK` followed by the hardware revision.

Example:
```
hw-revision
OK 1
```

### nrf-communication
Tests the internal communication between the main MCU and NRF MCU. The command returns `OK` if the communication is successful.

Expand Down
45 changes: 45 additions & 0 deletions core/embed/projects/prodtest/cmd/prodtest_hw_revision.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifdef USE_HW_REVISION
#include <trezor_rtl.h>

#include <rtl/cli.h>
#include <util/hw_revision.h>

static void prodtest_hw_revision(cli_t* cli) {
uint8_t rev = hw_revision_get();

if (cli_arg_count(cli) > 0) {
cli_error_arg_count(cli);
return;
}

cli_ok(cli, "%d", rev);
}

// clang-format off

PRODTEST_CLI_CMD(
.name = "hw-revision",
.func = prodtest_hw_revision,
.info = "Read the HW revision",
.args = ""
);

#endif
7 changes: 7 additions & 0 deletions core/embed/projects/prodtest/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@
#include "cmd/prodtest_ble.h"
#endif

#ifdef USE_HW_REVISION
#include <util/hw_revision.h>
#endif

#ifdef TREZOR_MODEL_T2T1
#define MODEL_IDENTIFIER "TREZOR2-"
#else
Expand Down Expand Up @@ -207,6 +211,9 @@ static void drivers_init(void) {
#ifdef USE_TROPIC
tropic_init();
#endif
#ifdef USE_HW_REVISION
hw_revision_init();
#endif
}

#define BACKLIGHT_NORMAL 150
Expand Down
28 changes: 28 additions & 0 deletions core/embed/util/hw_revision/inc/util/hw_revision.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <trezor_types.h>

void hw_revision_init(void);

void hw_revision_deinit(void);

uint8_t hw_revision_get(void);
114 changes: 114 additions & 0 deletions core/embed/util/hw_revision/stm32/hw_revision.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifdef KERNEL_MODE

#include <trezor_bsp.h>
#include <trezor_model.h>
#include <trezor_rtl.h>

#include <util/hw_revision.h>

typedef struct {
uint8_t revision;
bool initialized;
} hw_revision_t;

static hw_revision_t g_hw_revision;

static uint8_t hw_revision_read(void) {
bool rev0 =
GPIO_PIN_SET == HAL_GPIO_ReadPin(HW_REVISION_0_PORT, HW_REVISION_0_PIN);
bool rev1 =
GPIO_PIN_SET == HAL_GPIO_ReadPin(HW_REVISION_1_PORT, HW_REVISION_1_PIN);
bool rev2 =
GPIO_PIN_SET == HAL_GPIO_ReadPin(HW_REVISION_2_PORT, HW_REVISION_2_PIN);
bool rev3 = false;
#ifdef HW_REVISION_3_PIN
rev3 =
GPIO_PIN_SET == HAL_GPIO_ReadPin(HW_REVISION_3_PORT, HW_REVISION_3_PIN);
#endif

uint8_t revision = 0;
revision |= rev0 ? 1 : 0;
revision |= rev1 ? 2 : 0;
revision |= rev2 ? 4 : 0;
revision |= rev3 ? 8 : 0;

return revision;
}

void hw_revision_init(void) {
GPIO_InitTypeDef GPIO_InitStructure = {0};

HW_REVISION_0_CLOCK_ENABLE();
GPIO_InitStructure.Pin = HW_REVISION_0_PIN;
GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
GPIO_InitStructure.Pull = HW_REVISION_PUPD;
GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
HAL_GPIO_Init(HW_REVISION_0_PORT, &GPIO_InitStructure);

HW_REVISION_1_CLOCK_ENABLE();
GPIO_InitStructure.Pin = HW_REVISION_1_PIN;
GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
GPIO_InitStructure.Pull = HW_REVISION_PUPD;
GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
HAL_GPIO_Init(HW_REVISION_1_PORT, &GPIO_InitStructure);

HW_REVISION_2_CLOCK_ENABLE();
GPIO_InitStructure.Pin = HW_REVISION_2_PIN;
GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
GPIO_InitStructure.Pull = HW_REVISION_PUPD;
GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
HAL_GPIO_Init(HW_REVISION_2_PORT, &GPIO_InitStructure);

#ifdef HW_REVISION_3_PIN
GPIO_InitStructure.Pin = HW_REVISION_3_PIN;
GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
GPIO_InitStructure.Pull = HW_REVISION_PUPD;
GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
HAL_GPIO_Init(HW_REVISION_3_PORT, &GPIO_InitStructure);
#endif

memset(&g_hw_revision, 0, sizeof(hw_revision_t));
g_hw_revision.revision = hw_revision_read();
g_hw_revision.initialized = true;

// deinit the GPIOs to save power
HAL_GPIO_DeInit(HW_REVISION_0_PORT, HW_REVISION_0_PIN);
HAL_GPIO_DeInit(HW_REVISION_1_PORT, HW_REVISION_1_PIN);
HAL_GPIO_DeInit(HW_REVISION_2_PORT, HW_REVISION_2_PIN);
#ifdef HW_REVISION_3_PIN
HAL_GPIO_DeInit(HW_REVISION_3_PORT, HW_REVISION_3_PIN);
#endif
}

void hw_revision_deinit(void) {
memset(&g_hw_revision, 0, sizeof(hw_revision_t));
}

uint8_t hw_revision_get(void) {
hw_revision_t *hw_revision = &g_hw_revision;
if (!hw_revision->initialized) {
hw_revision_init();
}

return hw_revision->revision;
}

#endif
5 changes: 5 additions & 0 deletions core/site_scons/models/T3B1/trezor_t3b1_revB.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ def configure(
paths += ["embed/sec/consumption_mask/inc"]
defines += [("USE_CONSUMPTION_MASK", "1")]

if "hw_revision" in features_wanted:
defines += [("USE_HW_REVISION", "1")]
paths += ["embed/util/hw_revision/inc"]
sources += ["embed/util/hw_revision/stm32/hw_revision.c"]

defines += [
("USE_HASH_PROCESSOR", "1"),
("USE_STORAGE_HWKEY", "1"),
Expand Down
5 changes: 5 additions & 0 deletions core/site_scons/models/T3T1/trezor_t3t1_revE.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ def configure(
features_available.append("optiga")
defines += [("USE_OPTIGA", "1")]

if "hw_revision" in features_wanted:
defines += [("USE_HW_REVISION", "1")]
paths += ["embed/util/hw_revision/inc"]
sources += ["embed/util/hw_revision/stm32/hw_revision.c"]

defines += [
("USE_HASH_PROCESSOR", "1"),
("USE_STORAGE_HWKEY", "1"),
Expand Down
5 changes: 5 additions & 0 deletions core/site_scons/models/T3W1/trezor_t3w1_revB.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ def configure(
paths += ["embed/io/usb/inc"]
defines += [("USE_USB", "1")]

if "hw_revision" in features_wanted:
defines += [("USE_HW_REVISION", "1")]
paths += ["embed/util/hw_revision/inc"]
sources += ["embed/util/hw_revision/stm32/hw_revision.c"]

defines += [
"FRAMEBUFFER",
"DISPLAY_RGBA8888",
Expand Down

0 comments on commit 519bce6

Please sign in to comment.