diff --git a/core/embed/rust/src/trezorhal/display.rs b/core/embed/rust/src/trezorhal/display.rs index 26be093a813..30481b16e7a 100644 --- a/core/embed/rust/src/trezorhal/display.rs +++ b/core/embed/rust/src/trezorhal/display.rs @@ -12,6 +12,14 @@ pub use ffi::{ DISPLAY_FRAMEBUFFER_WIDTH, }; +#[cfg(all(feature = "framebuffer", not(feature = "framebuffer32bit")))] +#[derive(Copy, Clone)] +pub struct FrameBuffer(*mut u16); + +#[cfg(all(feature = "framebuffer", feature = "framebuffer32bit"))] +#[derive(Copy, Clone)] +pub struct FrameBuffer(*mut u32); + #[derive(PartialEq, Debug, Eq, FromPrimitive, Clone, Copy)] pub enum ToifFormat { FullColorBE = ffi::toif_format_t_TOIF_FULL_COLOR_BE as _, @@ -89,8 +97,8 @@ pub fn pixeldata(c: u16) { } #[cfg(feature = "framebuffer")] -pub fn get_fb_addr() -> u32 { - unsafe { ffi::display_get_fb_addr() as u32 } +pub fn get_fb_addr() -> FrameBuffer { + unsafe { FrameBuffer(ffi::display_get_fb_addr() as _) } } #[inline(always)] @@ -104,27 +112,25 @@ pub fn pixeldata(c: u16) { #[inline(always)] #[cfg(all(feature = "framebuffer", not(feature = "framebuffer32bit")))] -pub fn pixel(fb: u32, x: i16, y: i16, c: u16) { +pub fn pixel(fb: FrameBuffer, x: i16, y: i16, c: u16) { unsafe { - let addr = fb - + ((y as u32 + DISPLAY_FRAMEBUFFER_OFFSET_Y) * DISPLAY_FRAMEBUFFER_WIDTH - + (x as u32 + DISPLAY_FRAMEBUFFER_OFFSET_X)) - * 2; - let ptr = addr as *mut u16; - ptr.write_volatile(c); + let addr = fb.0.offset( + ((y as u32 + DISPLAY_FRAMEBUFFER_OFFSET_Y) * DISPLAY_FRAMEBUFFER_WIDTH + + (x as u32 + DISPLAY_FRAMEBUFFER_OFFSET_X)) as isize, + ); + addr.write_volatile(c); } } #[inline(always)] #[cfg(all(feature = "framebuffer", feature = "framebuffer32bit"))] -pub fn pixel(fb: u32, x: i16, y: i16, c: u32) { +pub fn pixel(fb: FrameBuffer, x: i16, y: i16, c: u32) { unsafe { - let addr = fb - + ((y as u32 + DISPLAY_FRAMEBUFFER_OFFSET_Y) * DISPLAY_FRAMEBUFFER_WIDTH - + (x as u32 + DISPLAY_FRAMEBUFFER_OFFSET_X)) - * 4; - let ptr = addr as *mut u32; - ptr.write_volatile(c); + let addr = fb.0.offset( + ((y as u32 + DISPLAY_FRAMEBUFFER_OFFSET_Y) * DISPLAY_FRAMEBUFFER_WIDTH + + (x as u32 + DISPLAY_FRAMEBUFFER_OFFSET_X)) as isize, + ); + addr.write_volatile(c); } }