From 46da3b45a4606d56952226834b7d29bb1c8dc7cf Mon Sep 17 00:00:00 2001 From: tychovrahe Date: Fri, 9 Jun 2023 23:20:24 +0200 Subject: [PATCH] fixup! feat(core): add hardware support for T3W1 --- core/embed/rust/src/ui/event.rs | 2 + core/embed/trezorhal/boards/trezor_t3w1_d1.h | 12 ++++ core/embed/trezorhal/button.c | 76 +++++++++++++++----- core/embed/trezorhal/button.h | 16 ++++- core/site_scons/boards/trezor_t3w1_d1.py | 2 + 5 files changed, 88 insertions(+), 20 deletions(-) diff --git a/core/embed/rust/src/ui/event.rs b/core/embed/rust/src/ui/event.rs index fd1f555543..cf20005edc 100644 --- a/core/embed/rust/src/ui/event.rs +++ b/core/embed/rust/src/ui/event.rs @@ -5,6 +5,7 @@ use core::convert::TryInto; pub enum PhysicalButton { Left, Right, + Power, } #[derive(Copy, Clone, PartialEq, Eq)] @@ -24,6 +25,7 @@ impl ButtonEvent { let button = match button { 0 => PhysicalButton::Left, 1 => PhysicalButton::Right, + 2 => PhysicalButton::Power, _ => return Err(error::Error::OutOfRange), }; let result = match event { diff --git a/core/embed/trezorhal/boards/trezor_t3w1_d1.h b/core/embed/trezorhal/boards/trezor_t3w1_d1.h index 63e4e9d4bb..01ca280f67 100644 --- a/core/embed/trezorhal/boards/trezor_t3w1_d1.h +++ b/core/embed/trezorhal/boards/trezor_t3w1_d1.h @@ -7,6 +7,7 @@ #define USE_SD_CARD 1 #define USE_I2C 1 #define USE_TOUCH 1 +#define USE_BUTTON 1 #define USE_SBU 1 #define USE_RGB_COLORS 1 #define USE_DISP_I8080_16BIT_DW 1 @@ -43,4 +44,15 @@ #define SD_ENABLE_PORT GPIOE #define SD_ENABLE_PIN GPIO_PIN_1 +#define GPIO_1_PORT GPIOC +#define GPIO_1_PIN GPIO_PIN_1 +#define GPIO_2_PORT GPIOC +#define GPIO_2_PIN GPIO_PIN_6 +#define GPIO_3_PORT GPIOC +#define GPIO_3_PIN GPIO_PIN_7 + +#define BTN_POWER_CLK_ENA __HAL_RCC_GPIOE_CLK_ENABLE +#define BTN_POWER_PORT GPIOE +#define BTN_POWER_PIN GPIO_PIN_0 + #endif //_TREZOR_T3W1_H diff --git a/core/embed/trezorhal/button.c b/core/embed/trezorhal/button.c index 7555781d4e..9221f60595 100644 --- a/core/embed/trezorhal/button.c +++ b/core/embed/trezorhal/button.c @@ -1,28 +1,54 @@ + +#include "stdbool.h" + #include STM32_HAL_H #include "button.h" -#include TREZOR_BOARD - -static char last_left = 0, last_right = 0; - -void button_init(void) { - BTN_LEFT_CLK_ENA(); - BTN_RIGHT_CLK_ENA(); +static void init_btn(GPIO_TypeDef *port, uint16_t pin) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.Mode = GPIO_MODE_INPUT; GPIO_InitStructure.Pull = GPIO_PULLUP; GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW; - GPIO_InitStructure.Pin = BTN_LEFT_PIN; - HAL_GPIO_Init(BTN_LEFT_PORT, &GPIO_InitStructure); - GPIO_InitStructure.Pin = BTN_RIGHT_PIN; - HAL_GPIO_Init(BTN_RIGHT_PORT, &GPIO_InitStructure); + GPIO_InitStructure.Pin = pin; + HAL_GPIO_Init(port, &GPIO_InitStructure); +} + +#ifdef BTN_LEFT_CLK_ENA +static bool last_left = 0; +bool button_state_left(void) { return last_left; } +#endif + +#ifdef BTN_RIGHT_CLK_ENA +static bool last_right = 0; +bool button_state_right(void) { return last_right; } +#endif + +#ifdef BTN_POWER_CLK_ENA +static bool last_power = 0; +bool button_state_power(void) { return last_power; } +#endif + +void button_init(void) { +#ifdef BTN_LEFT_CLK_ENA + BTN_LEFT_CLK_ENA(); + init_btn(BTN_LEFT_PORT, BTN_LEFT_PIN); +#endif + +#ifdef BTN_RIGHT_CLK_ENA + BTN_RIGHT_CLK_ENA(); + init_btn(BTN_RIGHT_PORT, BTN_RIGHT_PIN); +#endif + +#ifdef BTN_POWER_CLK_ENA + BTN_POWER_CLK_ENA(); + init_btn(BTN_POWER_PORT, BTN_POWER_PIN); +#endif } uint32_t button_read(void) { - char left = (GPIO_PIN_RESET == HAL_GPIO_ReadPin(BTN_LEFT_PORT, BTN_LEFT_PIN)); - char right = - (GPIO_PIN_RESET == HAL_GPIO_ReadPin(BTN_RIGHT_PORT, BTN_RIGHT_PIN)); +#ifdef BTN_LEFT_CLK_ENA + bool left = (GPIO_PIN_RESET == HAL_GPIO_ReadPin(BTN_LEFT_PORT, BTN_LEFT_PIN)); if (last_left != left) { last_left = left; if (left) { @@ -31,6 +57,10 @@ uint32_t button_read(void) { return BTN_EVT_UP | BTN_LEFT; } } +#endif +#ifdef BTN_RIGHT_CLK_ENA + bool right = + (GPIO_PIN_RESET == HAL_GPIO_ReadPin(BTN_RIGHT_PORT, BTN_RIGHT_PIN)); if (last_right != right) { last_right = right; if (right) { @@ -39,9 +69,19 @@ uint32_t button_read(void) { return BTN_EVT_UP | BTN_RIGHT; } } +#endif +#ifdef BTN_POWER_CLK_ENA + bool power = + (GPIO_PIN_RESET == HAL_GPIO_ReadPin(BTN_POWER_PORT, BTN_POWER_PIN)); + if (last_power != power) { + last_power = power; + if (power) { + return BTN_EVT_DOWN | BTN_POWER; + } else { + return BTN_EVT_UP | BTN_POWER; + } + } +#endif + return 0; } - -char button_state_left(void) { return last_left; } - -char button_state_right(void) { return last_right; } diff --git a/core/embed/trezorhal/button.h b/core/embed/trezorhal/button.h index f1389f7f4e..ee4953a5fe 100644 --- a/core/embed/trezorhal/button.h +++ b/core/embed/trezorhal/button.h @@ -20,6 +20,9 @@ #ifndef TREZORHAL_BUTTON_H #define TREZORHAL_BUTTON_H +#include TREZOR_BOARD + +#include #include #define BTN_EVT_DOWN (1U << 24) @@ -27,10 +30,19 @@ #define BTN_LEFT 0 #define BTN_RIGHT 1 +#define BTN_POWER 2 void button_init(void); uint32_t button_read(void); -char button_state_left(void); -char button_state_right(void); + +#ifdef BTN_LEFT_CLK_ENA +bool button_state_left(void); +#endif +#ifdef BTN_RIGHT_CLK_ENA +bool button_state_right(void); +#endif +#ifdef BTN_POWER_CLK_ENA +bool button_state_power(void); +#endif #endif diff --git a/core/site_scons/boards/trezor_t3w1_d1.py b/core/site_scons/boards/trezor_t3w1_d1.py index 3b3642b917..2468ddc0c1 100644 --- a/core/site_scons/boards/trezor_t3w1_d1.py +++ b/core/site_scons/boards/trezor_t3w1_d1.py @@ -20,6 +20,8 @@ def configure(env, features_wanted, defines, sources): sources += ['embed/trezorhal/touch/touch.c', ] sources += ['embed/trezorhal/touch/ft6x36.c', ] features_available.append("touch") + sources += ['embed/trezorhal/button.c'] + features_available.append("button") if "sd_card" in features_wanted: sources += ['embed/trezorhal/sdcard.c', ]