Skip to content

Commit

Permalink
fixup! feat(core): introduce TPS61062 backlight driver
Browse files Browse the repository at this point in the history
  • Loading branch information
TychoVrahe committed Jan 31, 2025
1 parent 62e5ccc commit 515aead
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions core/embed/io/backlight/stm32u5/tps61062.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,22 @@
#define TIM_PULSE(width) \
(TIMER_PERIOD - (width) * TIMER_PERIOD / MAX_PULSE_WIDTH_US)

#define STEPS 32
#define MAX_STEPS 32
#define DEFAULT_STEP 16 // DAC value after reset

// Backlight driver state
typedef struct {
// Set if driver is initialized
bool initialized;
// Level requested (0-255)
int set_level;
// Current backlight level in range 0-32
int current_level;
// Current step in range 0-32
int current_step;

DMA_HandleTypeDef dma;
TIM_HandleTypeDef tim;

uint32_t pwm_data[STEPS + 2]; // max steps + 2 for start and end
uint32_t pwm_data[MAX_STEPS + 2]; // max steps + 2 for start and end

} backlight_driver_t;

Expand Down Expand Up @@ -91,9 +91,10 @@ void backlight_init(backlight_action_t action) {
HAL_GPIO_Init(TPS61062_EN_PORT, &GPIO_EN_InitStructure);

HAL_GPIO_WritePin(TPS61062_EN_PORT, TPS61062_EN_PIN, GPIO_PIN_RESET);
drv->current_level = 0;

__HAL_RCC_TIM3_CLK_ENABLE();
__HAL_RCC_TIM3_FORCE_RESET();
__HAL_RCC_TIM3_RELEASE_RESET();
drv->tim.State = HAL_TIM_STATE_RESET;
drv->tim.Instance = TIM3;
drv->tim.Init.Period = TIMER_PERIOD;
Expand Down Expand Up @@ -169,6 +170,10 @@ void backlight_deinit(backlight_action_t action) {
backlight_shutdown();
HAL_GPIO_DeInit(TPS61062_ILED_PORT, TPS61062_ILED_PIN);
HAL_GPIO_DeInit(TPS61062_EN_PORT, TPS61062_EN_PIN);

__HAL_RCC_TIM3_FORCE_RESET();
__HAL_RCC_TIM3_RELEASE_RESET();
__HAL_RCC_TIM3_CLK_DISABLE();
}

drv->initialized = false;
Expand All @@ -181,21 +186,21 @@ int backlight_set(int val) {
return 0;
}

if (val < 0) {
return drv->set_level;
if (val < 0 || val > 255) {
return drv->current_level;
}

drv->set_level = val;
val = STEPS * val / 255;

if (val == drv->current_level) {
return val;
}

if (val == 0) {
drv->current_level = val;
int set_step = MAX_STEPS * val / 255;

if (set_step == 0) {
backlight_shutdown();
drv->current_level = 0;
return val;
drv->current_step = 0;
return drv->current_level;
}

if (HAL_DMA_GetState(&drv->dma) == HAL_DMA_STATE_BUSY) {
Expand All @@ -207,21 +212,21 @@ int backlight_set(int val) {
int pwm_data_idx = 0;
memset(drv->pwm_data, 0, sizeof(drv->pwm_data));

if (drv->current_level == 0) {
if (drv->current_step == 0) {
HAL_GPIO_WritePin(TPS61062_EN_PORT, TPS61062_EN_PIN, GPIO_PIN_SET);
// if brightness control is shutdown, start with initial pulse
drv->pwm_data[0] = TIMER_PERIOD;
pwm_data_idx++;
drv->current_level = DEFAULT_STEP;
drv->current_step = DEFAULT_STEP;
}

if (val > drv->current_level) {
int steps = val - drv->current_level;
if (set_step > drv->current_step) {
int steps = set_step - drv->current_step;
backlight_control_up(&drv->pwm_data[pwm_data_idx], steps);
pwm_data_idx += steps;

} else if (val < drv->current_level) {
int steps = drv->current_level - val;
} else if (set_step < drv->current_step) {
int steps = drv->current_step - set_step;
backlight_control_down(&drv->pwm_data[pwm_data_idx], steps);
pwm_data_idx += steps;
}
Expand All @@ -232,7 +237,7 @@ int backlight_set(int val) {
(uint32_t)&drv->tim.Instance->CCR1,
(pwm_data_idx + 1) * sizeof(uint32_t));

drv->current_level = val;
drv->current_step = set_step;

return drv->current_level;
}
Expand All @@ -244,7 +249,7 @@ int backlight_get(void) {
return 0;
}

return drv->set_level;
return drv->current_level;
}

static void backlight_control_up(uint32_t *data, int steps) {
Expand Down

0 comments on commit 515aead

Please sign in to comment.