1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-18 20:38:10 +00:00

feat(core): added button support for Model R

This commit is contained in:
tychovrahe 2022-04-25 11:46:09 +02:00 committed by matejcik
parent 7804893179
commit 936c84bac6
6 changed files with 102 additions and 10 deletions

View File

@ -99,7 +99,6 @@ SOURCE_TREZORHAL = [
'embed/trezorhal/rng.c', 'embed/trezorhal/rng.c',
'embed/trezorhal/stm32.c', 'embed/trezorhal/stm32.c',
'embed/trezorhal/systick.c', 'embed/trezorhal/systick.c',
'embed/trezorhal/touch.c',
'embed/trezorhal/usb.c', 'embed/trezorhal/usb.c',
'embed/trezorhal/usbd_conf.c', 'embed/trezorhal/usbd_conf.c',
'embed/trezorhal/usbd_core.c', 'embed/trezorhal/usbd_core.c',
@ -109,6 +108,12 @@ SOURCE_TREZORHAL = [
'embed/trezorhal/vectortable.s', 'embed/trezorhal/vectortable.s',
] ]
if TREZOR_MODEL in ['1', 'R']:
SOURCE_TREZORHAL.append('embed/trezorhal/button.c')
if TREZOR_MODEL in ('T',):
SOURCE_TREZORHAL.append('embed/trezorhal/touch.c')
env = Environment(ENV=os.environ, CFLAGS='%s -DPRODUCTION=%s' % (ARGUMENTS.get('CFLAGS', ''), ARGUMENTS.get('PRODUCTION', '0'))) env = Environment(ENV=os.environ, CFLAGS='%s -DPRODUCTION=%s' % (ARGUMENTS.get('CFLAGS', ''), ARGUMENTS.get('PRODUCTION', '0')))
env.Replace( env.Replace(

View File

@ -25,7 +25,9 @@
#include "flash.h" #include "flash.h"
#include "image.h" #include "image.h"
#include "rng.h" #include "rng.h"
#ifdef TREZOR_MODEL_T
#include "sdcard.h" #include "sdcard.h"
#endif
#include "lowlevel.h" #include "lowlevel.h"
#include "version.h" #include "version.h"
@ -50,6 +52,7 @@ static const uint8_t * const BOARDLOADER_KEYS[] = {
extern uint32_t sram_start[]; extern uint32_t sram_start[];
#define sdcard_buf sram_start #define sdcard_buf sram_start
#if defined TREZOR_MODEL_T
static uint32_t check_sdcard(void) { static uint32_t check_sdcard(void) {
if (sectrue != sdcard_power_on()) { if (sectrue != sdcard_power_on()) {
return 0; return 0;
@ -165,6 +168,7 @@ static secbool copy_sdcard(void) {
return sectrue; return sectrue;
} }
#endif
int main(void) { int main(void) {
reset_flags_reset(); reset_flags_reset();
@ -184,11 +188,14 @@ int main(void) {
clear_otg_hs_memory(); clear_otg_hs_memory();
display_init(); display_init();
#if defined TREZOR_MODEL_T
sdcard_init(); sdcard_init();
if (check_sdcard()) { if (check_sdcard()) {
return copy_sdcard() == sectrue ? 0 : 3; return copy_sdcard() == sectrue ? 0 : 3;
} }
#endif
image_header hdr; image_header hdr;

View File

@ -32,9 +32,16 @@
#include "icon_welcome.h" #include "icon_welcome.h"
#include "icon_wipe.h" #include "icon_wipe.h"
#include "mini_printf.h" #include "mini_printf.h"
#include "touch.h"
#include "version.h" #include "version.h"
#if defined TREZOR_MODEL_T
#include "touch.h"
#elif defined TREZOR_MODEL_R
#include "button.h"
#else
#error Unknown Trezor model
#endif
#define BACKLIGHT_NORMAL 150 #define BACKLIGHT_NORMAL 150
#define COLOR_BL_BG COLOR_WHITE // background #define COLOR_BL_BG COLOR_WHITE // background
@ -342,6 +349,7 @@ void ui_fadeout(void) {
int ui_user_input(int zones) { int ui_user_input(int zones) {
for (;;) { for (;;) {
#if defined TREZOR_MODEL_T
uint32_t evt = touch_click(); uint32_t evt = touch_click();
uint16_t x = touch_unpack_x(evt); uint16_t x = touch_unpack_x(evt);
uint16_t y = touch_unpack_y(evt); uint16_t y = touch_unpack_y(evt);
@ -365,5 +373,16 @@ int ui_user_input(int zones) {
y < 54 + 32) { y < 54 + 32) {
return INPUT_INFO; return INPUT_INFO;
} }
#elif defined TREZOR_MODEL_R
uint32_t evt = button_read();
if (evt == (BTN_LEFT | BTN_EVT_DOWN)) {
return INPUT_CANCEL;
}
if (evt == (BTN_RIGHT | BTN_EVT_DOWN)) {
return INPUT_CONFIRM;
}
#else
#error Unknown Trezor model
#endif
} }
} }

View File

@ -29,7 +29,12 @@
#include "mpu.h" #include "mpu.h"
#include "random_delays.h" #include "random_delays.h"
#include "secbool.h" #include "secbool.h"
#ifdef TREZOR_MODEL_T
#include "touch.h" #include "touch.h"
#endif
#if defined TREZOR_MODEL_R
#include "button.h"
#endif
#include "usb.h" #include "usb.h"
#include "version.h" #include "version.h"
@ -239,8 +244,14 @@ static void check_bootloader_version(void) {
int main(void) { int main(void) {
random_delays_init(); random_delays_init();
// display_init_seq(); // display_init_seq();
#if defined TREZOR_MODEL_T
touch_init(); touch_init();
touch_power_on(); touch_power_on();
#endif
#if defined TREZOR_MODEL_R
button_init();
#endif
mpu_config_bootloader(); mpu_config_bootloader();
@ -252,6 +263,7 @@ int main(void) {
// delay to detect touch // delay to detect touch
uint32_t touched = 0; uint32_t touched = 0;
#if defined TREZOR_MODEL_T
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
touched = touch_is_detected() | touch_read(); touched = touch_is_detected() | touch_read();
if (touched) { if (touched) {
@ -259,6 +271,12 @@ int main(void) {
} }
hal_delay(1); hal_delay(1);
} }
#elif defined TREZOR_MODEL_R
button_read();
if (button_state_left() == 1) {
touched = 1;
}
#endif
vendor_header vhdr; vendor_header vhdr;
image_header hdr; image_header hdr;
@ -359,7 +377,24 @@ int main(void) {
if ((vhdr.vtrust & VTRUST_CLICK) == 0) { if ((vhdr.vtrust & VTRUST_CLICK) == 0) {
ui_screen_boot_click(); ui_screen_boot_click();
#if defined TREZOR_MODEL_T
touch_click(); touch_click();
#elif defined TREZOR_MODEL_R
for (;;) {
button_read();
if (button_state_left() != 0 && button_state_right() != 0) {
break;
}
}
for (;;) {
button_read();
if (button_state_left() != 1 && button_state_right() != 1) {
break;
}
}
#else
#error Unknown Trezor model
#endif
} }
ui_fadeout(); ui_fadeout();

View File

@ -1,25 +1,45 @@
#include STM32_HAL_H #include STM32_HAL_H
#include "button.h" #include "button.h"
#define BTN_PIN_LEFT GPIO_PIN_5 #if defined TREZOR_MODEL_1
#define BTN_PIN_RIGHT GPIO_PIN_2 #define BTN_LEFT_PIN GPIO_PIN_5
#define BTN_LEFT_PORT GPIOC
#define BTN_LEFT_CLK_ENA __HAL_RCC_GPIOC_CLK_ENABLE
#define BTN_RIGHT_PIN GPIO_PIN_2
#define BTN_RIGHT_PORT GPIOC
#define BTN_RIGHT_CLK_ENA __HAL_RCC_GPIOC_CLK_ENABLE
#elif defined TREZOR_MODEL_R
#define BTN_LEFT_PIN GPIO_PIN_0
#define BTN_LEFT_PORT GPIOA
#define BTN_LEFT_CLK_ENA __HAL_RCC_GPIOA_CLK_ENABLE
#define BTN_RIGHT_PIN GPIO_PIN_15
#define BTN_RIGHT_PORT GPIOE
#define BTN_RIGHT_CLK_ENA __HAL_RCC_GPIOE_CLK_ENABLE
#else
#error Unknown Trezor model
#endif
static char last_left = 0, last_right = 0;
void button_init(void) { void button_init(void) {
__HAL_RCC_GPIOC_CLK_ENABLE(); BTN_LEFT_CLK_ENA();
BTN_RIGHT_CLK_ENA();
GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.Mode = GPIO_MODE_INPUT; GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
GPIO_InitStructure.Pull = GPIO_PULLUP; GPIO_InitStructure.Pull = GPIO_PULLUP;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStructure.Pin = BTN_PIN_LEFT | BTN_PIN_RIGHT; GPIO_InitStructure.Pin = BTN_LEFT_PIN;
HAL_GPIO_Init(GPIOC, &GPIO_InitStructure); HAL_GPIO_Init(BTN_LEFT_PORT, &GPIO_InitStructure);
GPIO_InitStructure.Pin = BTN_RIGHT_PIN;
HAL_GPIO_Init(BTN_RIGHT_PORT, &GPIO_InitStructure);
} }
uint32_t button_read(void) { uint32_t button_read(void) {
static char last_left = 0, last_right = 0; char left = (GPIO_PIN_RESET == HAL_GPIO_ReadPin(BTN_LEFT_PORT, BTN_LEFT_PIN));
char left = (GPIO_PIN_RESET == HAL_GPIO_ReadPin(GPIOC, BTN_PIN_LEFT)); char right =
char right = (GPIO_PIN_RESET == HAL_GPIO_ReadPin(GPIOC, BTN_PIN_RIGHT)); (GPIO_PIN_RESET == HAL_GPIO_ReadPin(BTN_RIGHT_PORT, BTN_RIGHT_PIN));
if (last_left != left) { if (last_left != left) {
last_left = left; last_left = left;
if (left) { if (left) {
@ -38,3 +58,7 @@ uint32_t button_read(void) {
} }
return 0; return 0;
} }
char button_state_left(void) { return last_left; }
char button_state_right(void) { return last_right; }

View File

@ -30,5 +30,7 @@
void button_init(void); void button_init(void);
uint32_t button_read(void); uint32_t button_read(void);
char button_state_left(void);
char button_state_right(void);
#endif #endif