1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-18 05:28:40 +00:00

refactor(core): separate backlight pwm driver and display driver

[no changelog]
This commit is contained in:
tychovrahe 2023-06-23 11:06:23 +02:00 committed by TychoVrahe
parent 70d990f69a
commit 1f1680243f
5 changed files with 121 additions and 74 deletions

View File

@ -0,0 +1,93 @@
#include "backlight_pwm.h"
#include STM32_HAL_H
#include TREZOR_BOARD
#define LED_PWM_TIM_PERIOD \
(255) // little less than 4kHz with PSC = (SystemCoreClock / 1000000) - 1)
#define LED_PWM_SLOW_TIM_PERIOD \
(10000) // about 10Hz (with PSC = (SystemCoreClock / 1000000) - 1)
static int BACKLIGHT = -1;
static int pwm_period = LED_PWM_TIM_PERIOD;
int backlight_pwm_set(int val) {
if (BACKLIGHT != val && val >= 0 && val <= 255) {
BACKLIGHT = val;
TIM1->CCR1 = pwm_period * val / 255;
}
return BACKLIGHT;
}
void backlight_pwm_init(void) {
// init peripherals
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_TIM1_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStructure;
// LCD_PWM/PA7 (backlight control)
GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStructure.Alternate = GPIO_AF1_TIM1;
GPIO_InitStructure.Pin = GPIO_PIN_7;
HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
// enable PWM timer
TIM_HandleTypeDef TIM1_Handle;
TIM1_Handle.Instance = TIM1;
TIM1_Handle.Init.Period = LED_PWM_TIM_PERIOD - 1;
// TIM1/APB2 source frequency equals to SystemCoreClock in our configuration,
// we want 1 MHz
TIM1_Handle.Init.Prescaler = SystemCoreClock / 1000000 - 1;
TIM1_Handle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
TIM1_Handle.Init.CounterMode = TIM_COUNTERMODE_UP;
TIM1_Handle.Init.RepetitionCounter = 0;
HAL_TIM_PWM_Init(&TIM1_Handle);
pwm_period = LED_PWM_TIM_PERIOD;
TIM_OC_InitTypeDef TIM_OC_InitStructure;
TIM_OC_InitStructure.Pulse = 0;
TIM_OC_InitStructure.OCMode = TIM_OCMODE_PWM2;
TIM_OC_InitStructure.OCPolarity = TIM_OCPOLARITY_HIGH;
TIM_OC_InitStructure.OCFastMode = TIM_OCFAST_DISABLE;
TIM_OC_InitStructure.OCNPolarity = TIM_OCNPOLARITY_HIGH;
TIM_OC_InitStructure.OCIdleState = TIM_OCIDLESTATE_SET;
TIM_OC_InitStructure.OCNIdleState = TIM_OCNIDLESTATE_SET;
HAL_TIM_PWM_ConfigChannel(&TIM1_Handle, &TIM_OC_InitStructure, TIM_CHANNEL_1);
backlight_pwm_set(0);
HAL_TIM_PWM_Start(&TIM1_Handle, TIM_CHANNEL_1);
HAL_TIMEx_PWMN_Start(&TIM1_Handle, TIM_CHANNEL_1);
}
void backlight_pwm_reinit(void) {
uint32_t prev_arr = TIM1->ARR;
uint32_t prev_ccr1 = TIM1->CCR1;
uint8_t prev_val = (prev_ccr1 * 255) / prev_arr;
BACKLIGHT = prev_val;
pwm_period = LED_PWM_TIM_PERIOD;
TIM1->CR1 |= TIM_CR1_ARPE;
TIM1->CR2 |= TIM_CR2_CCPC;
TIM1->CCR1 = pwm_period * prev_val / 255;
TIM1->ARR = LED_PWM_TIM_PERIOD - 1;
}
void backlight_pwm_set_slow(void) {
uint32_t prev_arr = TIM1->ARR;
uint32_t prev_ccr1 = TIM1->CCR1;
uint8_t prev_val = (prev_ccr1 * 255) / prev_arr;
pwm_period = LED_PWM_SLOW_TIM_PERIOD;
TIM1->CR1 |= TIM_CR1_ARPE;
TIM1->CR2 |= TIM_CR2_CCPC;
TIM1->ARR = LED_PWM_SLOW_TIM_PERIOD - 1;
TIM1->CCR1 = pwm_period * prev_val / 255;
}

View File

@ -0,0 +1,17 @@
#ifndef CORE_BACKLIGHT_H
#define CORE_BACKLIGHT_H
#include "common.h"
int backlight_pwm_set(int val);
int backlight_pwm_get(void);
void backlight_pwm_init(void);
void backlight_pwm_reinit(void);
void backlight_pwm_set_slow(void);
#endif // CORE_BACKLIGHT_H

View File

@ -34,6 +34,10 @@
#include "mini_printf.h"
#include "stm32f4xx_ll_utils.h"
#ifdef TREZOR_MODEL_T
#include "backlight_pwm.h"
#endif
#ifdef RGB16
#define COLOR_FATAL_ERROR RGB16(0x7F, 0x00, 0x00)
#else
@ -206,7 +210,7 @@ void ensure_compatible_settings(void) {
display_set_big_endian();
display_orientation(0);
set_core_clock(CLOCK_168_MHZ);
display_set_slow_pwm();
backlight_pwm_set_slow();
#endif
}

View File

@ -20,6 +20,7 @@
#include <stdbool.h>
#include <stdint.h>
#include TREZOR_BOARD
#include "backlight_pwm.h"
#include "display_interface.h"
#include "memzero.h"
#include "st7789v.h"
@ -52,10 +53,6 @@ __IO DISP_MEM_TYPE *const DISPLAY_CMD_ADDRESS =
__IO DISP_MEM_TYPE *const DISPLAY_DATA_ADDRESS =
(__IO DISP_MEM_TYPE *const)((uint32_t)DISPLAY_MEMORY_BASE |
(DISPLAY_ADDR_SHIFT << DISPLAY_MEMORY_PIN));
#define LED_PWM_TIM_PERIOD \
(255) // little less than 4kHz with PSC = (SystemCoreClock / 1000000) - 1)
#define LED_PWM_SLOW_TIM_PERIOD \
(10000) // about 10Hz (with PSC = (SystemCoreClock / 1000000) - 1)
// section "9.1.3 RDDID (04h): Read Display ID"
// of ST7789V datasheet
@ -69,9 +66,7 @@ __IO DISP_MEM_TYPE *const DISPLAY_DATA_ADDRESS =
// of ILI9341V datasheet
#define DISPLAY_ID_ILI9341V 0x009341U
static int DISPLAY_BACKLIGHT = -1;
static int DISPLAY_ORIENTATION = -1;
static int pwm_period = LED_PWM_TIM_PERIOD;
void display_pixeldata(uint16_t c) { PIXELDATA(c); }
@ -259,13 +254,7 @@ int display_orientation(int degrees) {
int display_get_orientation(void) { return DISPLAY_ORIENTATION; }
int display_backlight(int val) {
if (DISPLAY_BACKLIGHT != val && val >= 0 && val <= 255) {
DISPLAY_BACKLIGHT = val;
TIM1->CCR1 = pwm_period * val / 255;
}
return DISPLAY_BACKLIGHT;
}
int display_backlight(int val) { return backlight_pwm_set(val); }
void display_init_seq(void) {
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_14, GPIO_PIN_RESET); // LCD_RST/PC14
@ -350,47 +339,12 @@ void display_init(void) {
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_TIM1_CLK_ENABLE();
__HAL_RCC_FMC_CLK_ENABLE();
backlight_pwm_init();
GPIO_InitTypeDef GPIO_InitStructure;
// LCD_PWM/PA7 (backlight control)
GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStructure.Alternate = GPIO_AF1_TIM1;
GPIO_InitStructure.Pin = GPIO_PIN_7;
HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
// enable PWM timer
TIM_HandleTypeDef TIM1_Handle;
TIM1_Handle.Instance = TIM1;
TIM1_Handle.Init.Period = LED_PWM_TIM_PERIOD - 1;
// TIM1/APB2 source frequency equals to SystemCoreClock in our configuration,
// we want 1 MHz
TIM1_Handle.Init.Prescaler = SystemCoreClock / 1000000 - 1;
TIM1_Handle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
TIM1_Handle.Init.CounterMode = TIM_COUNTERMODE_UP;
TIM1_Handle.Init.RepetitionCounter = 0;
HAL_TIM_PWM_Init(&TIM1_Handle);
pwm_period = LED_PWM_TIM_PERIOD;
TIM_OC_InitTypeDef TIM_OC_InitStructure;
TIM_OC_InitStructure.Pulse = 0;
TIM_OC_InitStructure.OCMode = TIM_OCMODE_PWM2;
TIM_OC_InitStructure.OCPolarity = TIM_OCPOLARITY_HIGH;
TIM_OC_InitStructure.OCFastMode = TIM_OCFAST_DISABLE;
TIM_OC_InitStructure.OCNPolarity = TIM_OCNPOLARITY_HIGH;
TIM_OC_InitStructure.OCIdleState = TIM_OCIDLESTATE_SET;
TIM_OC_InitStructure.OCNIdleState = TIM_OCNIDLESTATE_SET;
HAL_TIM_PWM_ConfigChannel(&TIM1_Handle, &TIM_OC_InitStructure, TIM_CHANNEL_1);
display_backlight(0);
HAL_TIM_PWM_Start(&TIM1_Handle, TIM_CHANNEL_1);
HAL_TIMEx_PWMN_Start(&TIM1_Handle, TIM_CHANNEL_1);
// LCD_RST/PC14
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStructure.Pull = GPIO_NOPULL;
@ -450,18 +404,9 @@ void display_reinit(void) {
// important for model T as this is not set in boardloader
display_set_little_endian();
uint32_t prev_arr = TIM1->ARR;
uint32_t prev_ccr1 = TIM1->CCR1;
uint8_t prev_val = (prev_ccr1 * 255) / prev_arr;
DISPLAY_BACKLIGHT = prev_val;
DISPLAY_ORIENTATION = 0;
pwm_period = LED_PWM_TIM_PERIOD;
TIM1->CR1 |= TIM_CR1_ARPE;
TIM1->CR2 |= TIM_CR2_CCPC;
TIM1->CCR1 = pwm_period * prev_val / 255;
TIM1->ARR = LED_PWM_TIM_PERIOD - 1;
backlight_pwm_reinit();
#ifdef TREZOR_MODEL_T
uint32_t id = display_identify();
@ -488,19 +433,6 @@ void display_sync(void) {
void display_refresh(void) {}
void display_set_slow_pwm(void) {
uint32_t prev_arr = TIM1->ARR;
uint32_t prev_ccr1 = TIM1->CCR1;
uint8_t prev_val = (prev_ccr1 * 255) / prev_arr;
pwm_period = LED_PWM_SLOW_TIM_PERIOD;
TIM1->CR1 |= TIM_CR1_ARPE;
TIM1->CR2 |= TIM_CR2_CCPC;
TIM1->ARR = LED_PWM_SLOW_TIM_PERIOD - 1;
TIM1->CCR1 = pwm_period * prev_val / 255;
}
void display_set_little_endian(void) {
uint32_t id = display_identify();
if (id == DISPLAY_ID_GC9307) {

View File

@ -20,6 +20,7 @@ def configure(
defines += [f"HW_MODEL={hw_model}"]
defines += [f"HW_REVISION={hw_revision}"]
sources += [f"embed/trezorhal/displays/{display}"]
sources += [f"embed/trezorhal/backlight_pwm.c"]
sources += [f'embed/trezorhal/displays/panels/tf15411a.c', ]
sources += [f'embed/trezorhal/displays/panels/154a.c', ]
sources += [f'embed/trezorhal/displays/panels/lx154a2411.c', ]