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 12, 2024
1 parent 40d8271 commit 266a436
Show file tree
Hide file tree
Showing 24 changed files with 803 additions and 577 deletions.
12 changes: 6 additions & 6 deletions core/SConscript.bootloader_emu
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ SOURCE_MOD += [
'embed/lib/gfx_bitblt_rgba8888.c',
'embed/lib/image.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 @@ -144,15 +143,16 @@ SOURCE_BOOTLOADER = [

SOURCE_TREZORHAL = [
'embed/trezorhal/unix/boot_args.c',
'embed/trezorhal/unix/button.c',
'embed/trezorhal/unix/common.c',
'embed/trezorhal/unix/fault_handlers.c',
'embed/trezorhal/unix/flash.c',
'embed/trezorhal/unix/flash_otp.c',
'embed/trezorhal/unix/common.c',
'embed/trezorhal/unix/touch/touch.c',
'embed/trezorhal/unix/rng.c',
'embed/trezorhal/unix/usb.c',
'embed/trezorhal/unix/flash.c',
'embed/trezorhal/unix/random_delays.c',
'embed/trezorhal/unix/rng.c',
'embed/trezorhal/unix/secret.c',
'embed/trezorhal/unix/touch.c',
'embed/trezorhal/unix/usb.c',
]

if NEW_RENDERING:
Expand Down
9 changes: 5 additions & 4 deletions core/SConscript.unix
Original file line number Diff line number Diff line change
Expand Up @@ -416,21 +416,22 @@ SOURCE_MICROPYTHON = [

SOURCE_UNIX = [
'embed/trezorhal/unix/boot_args.c',
'embed/trezorhal/unix/button.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/touch.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 @@ -399,7 +399,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 @@ -515,17 +514,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.

22 changes: 11 additions & 11 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,9 +348,9 @@ static void test_touch(const char *args) {
}
display_refresh();

touch_power_on();
touch_init();

uint32_t evt = 0;
uint32_t evt = 0;
if (touch_click_timeout(&evt, timeout * 1000)) {
uint16_t x = touch_unpack_x(evt);
uint16_t y = touch_unpack_y(evt);
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,7 +387,7 @@ static void test_sensitivity(const char *args) {
}
}

touch_power_off();
touch_deinit();
}

static void touch_version(void) {
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 266a436

Please sign in to comment.