From 0c4b1d6213364de950bf6c6014a1185c2cbd949c Mon Sep 17 00:00:00 2001 From: tychovrahe Date: Thu, 23 May 2024 08:48:26 +0200 Subject: [PATCH] fix(core): fix framebuffer switching, was too fast so the change was sometimes not visible [no changelog] --- core/embed/trezorhal/bg_copy.h | 2 + .../stm32f4/xdisplay/st-7789/display_driver.c | 2 +- .../stm32f4/xdisplay/st-7789/display_fb.c | 71 ++++++++++++------- .../stm32f4/xdisplay/st-7789/display_fb.h | 2 + core/embed/trezorhal/stm32u5/bg_copy.c | 10 +++ 5 files changed, 61 insertions(+), 26 deletions(-) diff --git a/core/embed/trezorhal/bg_copy.h b/core/embed/trezorhal/bg_copy.h index a3cc2f2fa5..2bbc8959d7 100644 --- a/core/embed/trezorhal/bg_copy.h +++ b/core/embed/trezorhal/bg_copy.h @@ -22,4 +22,6 @@ void bg_copy_wait(void); */ void bg_copy_start_const_out_8(const uint8_t *src, uint8_t *dst, size_t size); +void bg_copy_abort(void); + #endif diff --git a/core/embed/trezorhal/stm32f4/xdisplay/st-7789/display_driver.c b/core/embed/trezorhal/stm32f4/xdisplay/st-7789/display_driver.c index 44246ecafd..2a3cd3e6e6 100644 --- a/core/embed/trezorhal/stm32f4/xdisplay/st-7789/display_driver.c +++ b/core/embed/trezorhal/stm32f4/xdisplay/st-7789/display_driver.c @@ -84,7 +84,7 @@ void display_reinit(void) { void display_finish_actions(void) { #ifdef XFRAMEBUFFER #ifndef BOARDLOADER - bg_copy_wait(); + wait_for_fb_switch(); #endif #endif } diff --git a/core/embed/trezorhal/stm32f4/xdisplay/st-7789/display_fb.c b/core/embed/trezorhal/stm32f4/xdisplay/st-7789/display_fb.c index 8c4400b0d3..8a53c48823 100644 --- a/core/embed/trezorhal/stm32f4/xdisplay/st-7789/display_fb.c +++ b/core/embed/trezorhal/stm32f4/xdisplay/st-7789/display_fb.c @@ -63,38 +63,35 @@ void display_physical_fb_clear(void) { } #ifndef BOARDLOADER -static volatile bool pending_fb_switch = false; +static volatile uint16_t pending_fb_switch = 0; static volatile uint32_t last_fb_update_time = 0; #endif #ifndef BOARDLOADER void DISPLAY_TE_INTERRUPT_HANDLER(void) { - HAL_NVIC_DisableIRQ(DISPLAY_TE_INTERRUPT_NUM); - - if (current_frame_buffer == 1) { - bg_copy_start_const_out_8((uint8_t *)physical_frame_buffer_1, - (uint8_t *)DISPLAY_DATA_ADDRESS, - DISPLAY_RESX * DISPLAY_RESY * 2); + if (pending_fb_switch == 1) { + if (current_frame_buffer == 1) { + bg_copy_start_const_out_8((uint8_t *)physical_frame_buffer_1, + (uint8_t *)DISPLAY_DATA_ADDRESS, + DISPLAY_RESX * DISPLAY_RESY * 2); + } else { + bg_copy_start_const_out_8((uint8_t *)physical_frame_buffer_0, + (uint8_t *)DISPLAY_DATA_ADDRESS, + DISPLAY_RESX * DISPLAY_RESY * 2); + } + last_fb_update_time = HAL_GetTick(); + pending_fb_switch = 2; + } else if (pending_fb_switch == 2) { + HAL_NVIC_DisableIRQ(DISPLAY_TE_INTERRUPT_NUM); + pending_fb_switch = 0; } else { - bg_copy_start_const_out_8((uint8_t *)physical_frame_buffer_0, - (uint8_t *)DISPLAY_DATA_ADDRESS, - DISPLAY_RESX * DISPLAY_RESY * 2); + HAL_NVIC_DisableIRQ(DISPLAY_TE_INTERRUPT_NUM); + pending_fb_switch = 0; } - - pending_fb_switch = false; - last_fb_update_time = HAL_GetTick(); __HAL_GPIO_EXTI_CLEAR_FLAG(DISPLAY_TE_PIN); } -static void wait_for_fb_switch(void) { - while (pending_fb_switch) { - __WFI(); - } - bg_copy_wait(); -} -#endif - static void copy_fb_to_display(const uint16_t *fb) { for (int i = 0; i < DISPLAY_RESX * DISPLAY_RESY; i++) { // 2 bytes per pixel because we're using RGB 5-6-5 format @@ -102,6 +99,25 @@ static void copy_fb_to_display(const uint16_t *fb) { } } +void wait_for_fb_switch(void) { + if (is_mode_handler()) { + if (pending_fb_switch != 0) { + if (current_frame_buffer == 0) { + copy_fb_to_display((uint16_t *)physical_frame_buffer_1); + } else { + copy_fb_to_display((uint16_t *)physical_frame_buffer_0); + } + pending_fb_switch = 0; + } + } else { + while (pending_fb_switch != 0) { + __WFI(); + } + bg_copy_wait(); + } +} +#endif + static void switch_fb_manually(void) { // sync with the panel refresh while (GPIO_PIN_SET == HAL_GPIO_ReadPin(DISPLAY_TE_PORT, DISPLAY_TE_PIN)) { @@ -131,7 +147,7 @@ static void switch_fb_in_background(void) { memcpy(physical_frame_buffer_0, physical_frame_buffer_1, sizeof(physical_frame_buffer_0)); - pending_fb_switch = true; + pending_fb_switch = 1; __HAL_GPIO_EXTI_CLEAR_FLAG(DISPLAY_TE_PIN); svc_enableIRQ(DISPLAY_TE_INTERRUPT_NUM); } else { @@ -139,7 +155,7 @@ static void switch_fb_in_background(void) { memcpy(physical_frame_buffer_1, physical_frame_buffer_0, sizeof(physical_frame_buffer_1)); - pending_fb_switch = true; + pending_fb_switch = 1; __HAL_GPIO_EXTI_CLEAR_FLAG(DISPLAY_TE_PIN); svc_enableIRQ(DISPLAY_TE_INTERRUPT_NUM); } @@ -165,12 +181,17 @@ display_fb_info_t display_get_frame_buffer(void) { void display_refresh(void) { #ifndef BOARDLOADER - wait_for_fb_switch(); - display_panel_set_window(0, 0, DISPLAY_RESX - 1, DISPLAY_RESY - 1); if (is_mode_handler()) { + if (pending_fb_switch != 0) { + pending_fb_switch = 0; + bg_copy_abort(); + } + display_panel_set_window(0, 0, DISPLAY_RESX - 1, DISPLAY_RESY - 1); switch_fb_manually(); } else { + wait_for_fb_switch(); + display_panel_set_window(0, 0, DISPLAY_RESX - 1, DISPLAY_RESY - 1); switch_fb_in_background(); } #else diff --git a/core/embed/trezorhal/stm32f4/xdisplay/st-7789/display_fb.h b/core/embed/trezorhal/stm32f4/xdisplay/st-7789/display_fb.h index 0afa1de2d2..437b546466 100644 --- a/core/embed/trezorhal/stm32f4/xdisplay/st-7789/display_fb.h +++ b/core/embed/trezorhal/stm32f4/xdisplay/st-7789/display_fb.h @@ -29,6 +29,8 @@ void display_physical_fb_clear(void); void display_ensure_refreshed(void); +void wait_for_fb_switch(void); + #endif // XFRAMEBUFFER #endif // TREZORHAL_DISPLAY_FB_H diff --git a/core/embed/trezorhal/stm32u5/bg_copy.c b/core/embed/trezorhal/stm32u5/bg_copy.c index d47f776d25..054328c962 100644 --- a/core/embed/trezorhal/stm32u5/bg_copy.c +++ b/core/embed/trezorhal/stm32u5/bg_copy.c @@ -95,3 +95,13 @@ void bg_copy_start_const_out_8(const uint8_t *src, uint8_t *dst, size_t size) { HAL_DMA_Start_IT(&DMA_Handle, (uint32_t)src, (uint32_t)dst, data_to_send); } + +void bg_copy_abort(void) { + dma_transfer_remaining = 0; + dma_data_transferred = 0; + HAL_DMA_Abort(&DMA_Handle); + HAL_DMA_DeInit(&DMA_Handle); + HAL_NVIC_DisableIRQ(GPDMA1_Channel0_IRQn); + data_src = NULL; + data_dst = NULL; +}