Skip to content

Commit

Permalink
refactor(core/embed): prepare touch drivers for low power mode
Browse files Browse the repository at this point in the history
[no changelog]
  • Loading branch information
cepetr committed Jun 19, 2024
1 parent 5509343 commit e25ade5
Show file tree
Hide file tree
Showing 29 changed files with 819 additions and 577 deletions.
6 changes: 2 additions & 4 deletions core/SConscript.bootloader_emu
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ SOURCE_MOD += [
'embed/lib/image.c',
'embed/lib/mini_printf.c',
'embed/lib/terminal.c',
'embed/lib/touch.c',
'embed/lib/unit_variant.c',
'vendor/micropython/lib/uzlib/adler32.c',
'vendor/micropython/lib/uzlib/crc32.c',
Expand Down Expand Up @@ -150,11 +149,10 @@ SOURCE_TREZORHAL = [
'embed/trezorhal/unix/fault_handlers.c',
'embed/trezorhal/unix/flash.c',
'embed/trezorhal/unix/flash_otp.c',
'embed/trezorhal/unix/touch/touch.c',
'embed/trezorhal/unix/rng.c',
'embed/trezorhal/unix/usb.c',
'embed/trezorhal/unix/random_delays.c',
'embed/trezorhal/unix/rng.c',
'embed/trezorhal/unix/secret.c',
'embed/trezorhal/unix/usb.c',
]

if NEW_RENDERING:
Expand Down
7 changes: 3 additions & 4 deletions core/SConscript.unix
Original file line number Diff line number Diff line change
Expand Up @@ -419,20 +419,19 @@ SOURCE_MICROPYTHON = [
SOURCE_UNIX = [
'embed/trezorhal/unix/boot_args.c',
'embed/trezorhal/unix/common.c',
'embed/trezorhal/unix/flash.c',
'embed/trezorhal/unix/flash_otp.c',
'embed/trezorhal/unix/flash.c',
'embed/trezorhal/unix/random_delays.c',
'embed/trezorhal/unix/rng.c',
'embed/trezorhal/unix/usb.c',
'embed/trezorhal/unix/touch/touch.c',
'embed/unix/main.c',
'embed/unix/main_main.c',
'embed/unix/main.c',
'embed/unix/profile.c',
'vendor/micropython/shared/runtime/gchelper_generic.c',
'vendor/micropython/ports/unix/alloc.c',
'vendor/micropython/ports/unix/gccollect.c',
'vendor/micropython/ports/unix/input.c',
'vendor/micropython/ports/unix/unix_mphal.c',
'vendor/micropython/shared/runtime/gchelper_generic.c',
]

if NEW_RENDERING:
Expand Down
8 changes: 4 additions & 4 deletions core/embed/bootloader/bootui.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,16 @@ static void ui_screen_boot_wait(int wait_seconds) {

void ui_click(void) {
// flush touch events if any
while (touch_read()) {
while (touch_get_event()) {
}
// wait for TOUCH_START
while ((touch_read() & TOUCH_START) == 0) {
while ((touch_get_event() & TOUCH_START) == 0) {
}
// wait for TOUCH_END
while ((touch_read() & TOUCH_END) == 0) {
while ((touch_get_event() & TOUCH_END) == 0) {
}
// flush touch events if any
while (touch_read()) {
while (touch_get_event()) {
}
}

Expand Down
22 changes: 13 additions & 9 deletions core/embed/bootloader/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,6 @@ int bootloader_main(void) {
unit_variant_init();

#ifdef USE_TOUCH
touch_power_on();
#ifdef TREZOR_MODEL_T3T1
// on T3T1, tester needs to run without touch, so making an exception
// until unit variant is written in OTP
Expand Down Expand Up @@ -514,17 +513,22 @@ int bootloader_main(void) {
uint32_t touched = 0;
#ifdef USE_TOUCH
if (firmware_present == sectrue && stay_in_bootloader != sectrue) {
touch_wait_until_ready();
// Wait until the touch controller is ready
// (on hardware this may take a while)
while (touch_ready() != sectrue) {
hal_delay(1);
}
#ifdef TREZOR_EMULATOR
hal_delay(500);
#endif
// Give the touch controller time to report events
// if someone touches the screen
for (int i = 0; i < 10; i++) {
touched = touch_is_detected() | touch_read();
if (touched) {
if (touch_activity() == sectrue) {
touched = 1;
break;
}
#ifdef TREZOR_EMULATOR
hal_delay(25);
#else
hal_delay_us(1000);
#endif
hal_delay(5);
}
}
#elif defined USE_BUTTON
Expand Down
1 change: 0 additions & 1 deletion core/embed/bootloader_ci/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ int main(void) {
random_delays_init();
#ifdef USE_TOUCH
touch_init();
touch_power_on();
#endif

#ifdef USE_HASH_PROCESSOR
Expand Down
4 changes: 1 addition & 3 deletions core/embed/extmod/modtrezorio/modtrezorio-poll.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ STATIC mp_obj_t mod_trezorio_poll(mp_obj_t ifaces, mp_obj_t list_ref,
}
#if defined USE_TOUCH
else if (iface == TOUCH_IFACE) {

const uint32_t evt = touch_read();

const uint32_t evt = touch_get_event();
if (evt) {
// ignore TOUCH_MOVE events if they are too frequent
if ((evt & TOUCH_MOVE) == 0 ||
Expand Down
22 changes: 0 additions & 22 deletions core/embed/lib/touch.c

This file was deleted.

1 change: 1 addition & 0 deletions core/embed/prodtest/.changelog.d/3900.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix TOUCH VERSION command
24 changes: 12 additions & 12 deletions core/embed/prodtest/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -312,15 +312,15 @@ static secbool touch_click_timeout(uint32_t *touch, uint32_t timeout_ms) {
uint32_t deadline = HAL_GetTick() + timeout_ms;
uint32_t r = 0;

while (touch_read())
while (touch_get_event())
;
while ((touch_read() & TOUCH_START) == 0) {
while ((touch_get_event() & TOUCH_START) == 0) {
if (HAL_GetTick() > deadline) return secfalse;
}
while (((r = touch_read()) & TOUCH_END) == 0) {
while (((r = touch_get_event()) & TOUCH_END) == 0) {
if (HAL_GetTick() > deadline) return secfalse;
}
while (touch_read())
while (touch_get_event())
;

*touch = r;
Expand Down Expand Up @@ -348,7 +348,7 @@ static void test_touch(const char *args) {
}
display_refresh();

touch_power_on();
touch_init();

uint32_t evt = 0;
if (touch_click_timeout(&evt, timeout * 1000)) {
Expand All @@ -361,20 +361,20 @@ static void test_touch(const char *args) {
display_clear();
display_refresh();

touch_power_off();
touch_deinit();
}

static void test_sensitivity(const char *args) {
int v = atoi(args);

touch_power_on();
touch_sensitivity(v & 0xFF);
touch_init();
touch_set_sensitivity(v & 0xFF);

display_clear();
display_refresh();

for (;;) {
uint32_t evt = touch_read();
uint32_t evt = touch_get_event();
if (evt & TOUCH_START || evt & TOUCH_MOVE) {
int x = touch_unpack_x(evt);
int y = touch_unpack_y(evt);
Expand All @@ -387,14 +387,14 @@ static void test_sensitivity(const char *args) {
}
}

touch_power_off();
touch_deinit();
}

static void touch_version(void) {
touch_power_on();
touch_init();
uint8_t version = touch_get_version();
vcp_println("OK %d", version);
touch_power_off();
touch_deinit();
}
#endif

Expand Down
2 changes: 1 addition & 1 deletion core/embed/rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ fn generate_trezorhal_bindings() {
//usb
.allowlist_function("usb_configured")
// touch
.allowlist_function("touch_read")
.allowlist_function("touch_get_event")
// button
.allowlist_function("button_read")
// haptic
Expand Down
4 changes: 2 additions & 2 deletions core/embed/rust/src/trezorhal/io.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::ffi;

#[cfg(feature = "touch")]
pub fn io_touch_read() -> u32 {
unsafe { ffi::touch_read() }
pub fn io_touch_get_event() -> u32 {
unsafe { ffi::touch_get_event() }
}

#[cfg(feature = "button")]
Expand Down
4 changes: 2 additions & 2 deletions core/embed/rust/src/ui/layout/simplified.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(feature = "button")]
use crate::trezorhal::io::io_button_read;
#[cfg(feature = "touch")]
use crate::trezorhal::io::io_touch_read;
use crate::trezorhal::io::io_touch_get_event;
#[cfg(feature = "button")]
use crate::ui::event::ButtonEvent;
#[cfg(feature = "touch")]
Expand Down Expand Up @@ -56,7 +56,7 @@ fn button_eval() -> Option<ButtonEvent> {

#[cfg(feature = "touch")]
fn touch_eval() -> Option<TouchEvent> {
let event = io_touch_read();
let event = io_touch_get_event();
if event == 0 {
return None;
}
Expand Down
Loading

0 comments on commit e25ade5

Please sign in to comment.