From ec2a38105bd7e20445aaf3ef858fcb59780ebae3 Mon Sep 17 00:00:00 2001 From: tychovrahe Date: Thu, 19 May 2022 10:26:00 +0200 Subject: [PATCH] feat(core/rust): expose touch processing functions to rust --- core/embed/rust/build.rs | 11 ++++++++++- core/embed/rust/src/trezorhal/io.rs | 14 ++++++++++++++ core/embed/rust/src/trezorhal/mod.rs | 1 + core/embed/rust/trezorhal.h | 2 ++ core/embed/trezorhal/touch.c | 3 +++ core/embed/trezorhal/touch.h | 9 +++------ 6 files changed, 33 insertions(+), 7 deletions(-) create mode 100644 core/embed/rust/src/trezorhal/io.rs diff --git a/core/embed/rust/build.rs b/core/embed/rust/build.rs index 6de3bd83fc..da6829e85d 100644 --- a/core/embed/rust/build.rs +++ b/core/embed/rust/build.rs @@ -337,7 +337,16 @@ fn generate_trezorhal_bindings() { .allowlist_var("text_buffer_height") .allowlist_var("buffer_width") //usb - .allowlist_function("usb_configured"); + .allowlist_function("usb_configured") + // touch + .allowlist_function("touch_read") + // button + .allowlist_function("button_read") + .allowlist_var("BTN_EVT_DOWN") + .allowlist_var("BTN_EVT_UP") + .allowlist_var("BTN_RIGHT") + .allowlist_var("BTN_LEFT"); + // Write the bindings to a file in the OUR_DIR. bindings .generate() diff --git a/core/embed/rust/src/trezorhal/io.rs b/core/embed/rust/src/trezorhal/io.rs new file mode 100644 index 0000000000..d8aa3aa749 --- /dev/null +++ b/core/embed/rust/src/trezorhal/io.rs @@ -0,0 +1,14 @@ +use super::ffi; + +#[cfg(feature = "buttons")] +pub use super::ffi::{BTN_EVT_DOWN, BTN_EVT_UP, BTN_LEFT, BTN_RIGHT}; + +#[cfg(feature = "touch")] +pub fn io_touch_read() -> u32 { + unsafe { ffi::touch_read() } +} + +#[cfg(feature = "buttons")] +pub fn io_button_read() -> u32 { + unsafe { ffi::button_read() } +} diff --git a/core/embed/rust/src/trezorhal/mod.rs b/core/embed/rust/src/trezorhal/mod.rs index 45122be2a5..5abee6f189 100644 --- a/core/embed/rust/src/trezorhal/mod.rs +++ b/core/embed/rust/src/trezorhal/mod.rs @@ -7,6 +7,7 @@ pub mod display; #[cfg(feature = "dma2d")] pub mod dma2d; mod ffi; +pub mod io; pub mod qr; pub mod random; #[cfg(feature = "model_tr")] diff --git a/core/embed/rust/trezorhal.h b/core/embed/rust/trezorhal.h index 5497f29ffa..f8bb797a94 100644 --- a/core/embed/rust/trezorhal.h +++ b/core/embed/rust/trezorhal.h @@ -1,5 +1,6 @@ #include TREZOR_BOARD #include "buffers.h" +#include "button.h" #include "common.h" #include "display.h" #include "display_interface.h" @@ -9,6 +10,7 @@ #include "rgb_led.h" #include "secbool.h" #include "storage.h" +#include "touch.h" #include "usb.h" #include "bip39.h" diff --git a/core/embed/trezorhal/touch.c b/core/embed/trezorhal/touch.c index b75a0ecd40..340f84a80f 100644 --- a/core/embed/trezorhal/touch.c +++ b/core/embed/trezorhal/touch.c @@ -378,3 +378,6 @@ uint32_t touch_click(void) { // return last touch coordinate return r; } + +uint16_t touch_unpack_x(uint32_t evt) { return (evt >> 12) & 0xFFF; } +uint16_t touch_unpack_y(uint32_t evt) { return (evt >> 0) & 0xFFF; } diff --git a/core/embed/trezorhal/touch.h b/core/embed/trezorhal/touch.h index 4b438d8e18..aa62d12954 100644 --- a/core/embed/trezorhal/touch.h +++ b/core/embed/trezorhal/touch.h @@ -33,12 +33,9 @@ void touch_sensitivity(uint8_t value); uint32_t touch_read(void); uint32_t touch_click(void); uint32_t touch_is_detected(void); -static inline uint16_t touch_unpack_x(uint32_t evt) { - return (evt >> 12) & 0xFFF; -} -static inline uint16_t touch_unpack_y(uint32_t evt) { - return (evt >> 0) & 0xFFF; -} +uint16_t touch_unpack_x(uint32_t evt); +uint16_t touch_unpack_y(uint32_t evt); + static inline uint32_t touch_pack_xy(uint16_t x, uint16_t y) { return ((x & 0xFFF) << 12) | (y & 0xFFF); }