From a881401e4d4bfa04964b49195cc35a3c06b74e86 Mon Sep 17 00:00:00 2001 From: cepetr Date: Mon, 13 Jan 2025 19:08:16 +0100 Subject: [PATCH] feat(core): improve pwm backlight driver peripherals init/deinit [no changelog] --- .../display/backlight/stm32/backlight_pwm.c | 24 ++++++++++++++----- core/embed/models/T2T1/boards/trezor_t.h | 3 +++ .../models/T3T1/boards/trezor_t3t1_revE.h | 3 +++ .../models/T3W1/boards/trezor_t3w1_revA.h | 3 +++ .../models/T3W1/boards/trezor_t3w1_revA0.h | 3 +++ 5 files changed, 30 insertions(+), 6 deletions(-) diff --git a/core/embed/io/display/backlight/stm32/backlight_pwm.c b/core/embed/io/display/backlight/stm32/backlight_pwm.c index 8aeb40eb79..e5cd4df063 100644 --- a/core/embed/io/display/backlight/stm32/backlight_pwm.c +++ b/core/embed/io/display/backlight/stm32/backlight_pwm.c @@ -68,11 +68,9 @@ void backlight_pwm_init(backlight_action_t action) { } } - // Enable peripheral clocks - BACKLIGHT_PWM_PORT_CLK_EN(); - BACKLIGHT_PWM_TIM_CLK_EN(); - // Initialize PWM GPIO + BACKLIGHT_PWM_PORT_CLK_EN(); + GPIO_InitTypeDef GPIO_InitStructure = {0}; GPIO_InitStructure.Mode = GPIO_MODE_AF_PP; GPIO_InitStructure.Pull = GPIO_NOPULL; @@ -82,6 +80,9 @@ void backlight_pwm_init(backlight_action_t action) { HAL_GPIO_Init(BACKLIGHT_PWM_PORT, &GPIO_InitStructure); // Initialize PWM timer + BACKLIGHT_PWM_TIM_FORCE_RESET(); + BACKLIGHT_PWM_TIM_RELEASE_RESET(); + BACKLIGHT_PWM_TIM_CLK_EN(); uint32_t tmpcr1 = 0; @@ -220,8 +221,19 @@ void backlight_pwm_deinit(backlight_action_t action) { #else if (action == BACKLIGHT_RESET) { - // TODO: reset TIMER and GPIO - BACKLIGHT_PWM_TIM->BACKLIGHT_PWM_TIM_CCR = 0; + // Deinitialize PWM GPIO + GPIO_InitTypeDef GPIO_InitStructure = {0}; + GPIO_InitStructure.Mode = GPIO_MODE_ANALOG; + GPIO_InitStructure.Pull = GPIO_NOPULL; + GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStructure.Pin = BACKLIGHT_PWM_PIN; + HAL_GPIO_Init(BACKLIGHT_PWM_PORT, &GPIO_InitStructure); + + // Deinitialize PWM timer + BACKLIGHT_PWM_TIM_FORCE_RESET(); + BACKLIGHT_PWM_TIM_RELEASE_RESET(); + BACKLIGHT_PWM_TIM_CLK_DIS(); + } else { // action == BACKLIGHT_RETAIN BACKLIGHT_PWM_TIM->BACKLIGHT_PWM_TIM_CCR = (LED_PWM_TIM_PERIOD * drv->current_level) / 255; diff --git a/core/embed/models/T2T1/boards/trezor_t.h b/core/embed/models/T2T1/boards/trezor_t.h index 3435424f26..fca04f62d5 100644 --- a/core/embed/models/T2T1/boards/trezor_t.h +++ b/core/embed/models/T2T1/boards/trezor_t.h @@ -11,6 +11,9 @@ #define BACKLIGHT_PWM_FREQ 10000 #define BACKLIGHT_PWM_TIM TIM1 #define BACKLIGHT_PWM_TIM_CLK_EN __HAL_RCC_TIM1_CLK_ENABLE +#define BACKLIGHT_PWM_TIM_CLK_DIS __HAL_RCC_TIM1_CLK_DISABLE +#define BACKLIGHT_PWM_TIM_FORCE_RESET __HAL_RCC_TIM1_FORCE_RESET +#define BACKLIGHT_PWM_TIM_RELEASE_RESET __HAL_RCC_TIM1_RELEASE_RESET #define BACKLIGHT_PWM_TIM_AF GPIO_AF1_TIM1 #define BACKLIGHT_PWM_TIM_OCMODE TIM_OCMODE_PWM2 #define BACKLIGHT_PWM_TIM_CHANNEL TIM_CHANNEL_1 diff --git a/core/embed/models/T3T1/boards/trezor_t3t1_revE.h b/core/embed/models/T3T1/boards/trezor_t3t1_revE.h index 051ffb71f4..ee8ce34674 100644 --- a/core/embed/models/T3T1/boards/trezor_t3t1_revE.h +++ b/core/embed/models/T3T1/boards/trezor_t3t1_revE.h @@ -18,6 +18,9 @@ #define BACKLIGHT_PWM_FREQ 12500 #define BACKLIGHT_PWM_TIM TIM17 #define BACKLIGHT_PWM_TIM_CLK_EN __HAL_RCC_TIM17_CLK_ENABLE +#define BACKLIGHT_PWM_TIM_CLK_DIS __HAL_RCC_TIM17_CLK_DISABLE +#define BACKLIGHT_PWM_TIM_FORCE_RESET __HAL_RCC_TIM17_FORCE_RESET +#define BACKLIGHT_PWM_TIM_RELEASE_RESET __HAL_RCC_TIM17_RELEASE_RESET #define BACKLIGHT_PWM_TIM_AF GPIO_AF14_TIM17 #define BACKLIGHT_PWM_TIM_OCMODE TIM_OCMODE_PWM1 #define BACKLIGHT_PWM_TIM_CHANNEL TIM_CHANNEL_1 diff --git a/core/embed/models/T3W1/boards/trezor_t3w1_revA.h b/core/embed/models/T3W1/boards/trezor_t3w1_revA.h index acc6fa549c..e88d2f3a99 100644 --- a/core/embed/models/T3W1/boards/trezor_t3w1_revA.h +++ b/core/embed/models/T3W1/boards/trezor_t3w1_revA.h @@ -26,6 +26,9 @@ #define BACKLIGHT_PWM_FREQ 1000 #define BACKLIGHT_PWM_TIM TIM17 #define BACKLIGHT_PWM_TIM_CLK_EN __HAL_RCC_TIM17_CLK_ENABLE +#define BACKLIGHT_PWM_TIM_CLK_DIS __HAL_RCC_TIM17_CLK_DISABLE +#define BACKLIGHT_PWM_TIM_FORCE_RESET __HAL_RCC_TIM17_FORCE_RESET +#define BACKLIGHT_PWM_TIM_RELEASE_RESET __HAL_RCC_TIM17_RELEASE_RESET #define BACKLIGHT_PWM_TIM_AF GPIO_AF14_TIM17 #define BACKLIGHT_PWM_TIM_OCMODE TIM_OCMODE_PWM1 #define BACKLIGHT_PWM_TIM_CHANNEL TIM_CHANNEL_1 diff --git a/core/embed/models/T3W1/boards/trezor_t3w1_revA0.h b/core/embed/models/T3W1/boards/trezor_t3w1_revA0.h index 64387315d7..f87cc28273 100644 --- a/core/embed/models/T3W1/boards/trezor_t3w1_revA0.h +++ b/core/embed/models/T3W1/boards/trezor_t3w1_revA0.h @@ -25,6 +25,9 @@ #define BACKLIGHT_PWM_FREQ 1000 #define BACKLIGHT_PWM_TIM TIM17 #define BACKLIGHT_PWM_TIM_CLK_EN __HAL_RCC_TIM17_CLK_ENABLE +#define BACKLIGHT_PWM_TIM_CLK_DIS __HAL_RCC_TIM17_CLK_DISABLE +#define BACKLIGHT_PWM_TIM_FORCE_RESET __HAL_RCC_TIM17_FORCE_RESET +#define BACKLIGHT_PWM_TIM_RELEASE_RESET __HAL_RCC_TIM17_RELEASE_RESET #define BACKLIGHT_PWM_TIM_AF GPIO_AF14_TIM17 #define BACKLIGHT_PWM_TIM_OCMODE TIM_OCMODE_PWM1 #define BACKLIGHT_PWM_TIM_CHANNEL TIM_CHANNEL_1