Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backlight refactoring + TPS61062 driver #4552

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef TREZORHAL_BACKLIGHT_H
#define TREZORHAL_BACKLIGHT_H
#pragma once

// Action to be taken when initializing or
// deinitializing the backlight driver
Expand All @@ -32,26 +31,24 @@ typedef enum {
// If the action is set to `BACKLIGHT_RESET`, the backlight level
// is set to zero level. If the action is set to `BACKLIGHT_RETAIN`,
// the backlight level is not changed (if possible).
void backlight_pwm_init(backlight_action_t action);
void backlight_init(backlight_action_t action);

// Deinitialize the backlight driver
//
// If the action is set to `BACKLIGHT_RESET`, the backlight driver
// is completely deinitialized. If the action is set to `BACKLIGHT_RETAIN`,
// the driver is deinitialized as much as possible but the backlight
// is kept on.
void backlight_pwm_deinit(backlight_action_t action);
void backlight_deinit(backlight_action_t action);

// Sets the backlight level in range 0-255 and returns the actual level set.
//
// If the level is outside the range, the function has no effect
// and just returns the actual level set. If the backlight driver
// is not initialized, the function returns 0.
int backlight_pwm_set(int val);
int backlight_set(int val);

// Gets the backlight level in range 0-255
//
// Returns 0 if the backlight driver is not initialized.
int backlight_pwm_get(void);

#endif // TREZORHAL_BACKLIGHT_H
int backlight_get(void);
103 changes: 103 additions & 0 deletions core/embed/io/backlight/stm32/backlight_pin.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* This file is part of the Trezor project, https://trezor.io/
*
* Copyright (c) SatoshiLabs
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <io/backlight.h>

#include <trezor_bsp.h>
#include <trezor_rtl.h>

typedef struct {
// Set if driver is initialized
bool initialized;
// Current backlight level in range 0-255
int current_level;

} backlight_driver_t;

static backlight_driver_t g_backlight_driver = {
.initialized = false,
};

static void backlight_on(void) {
GPIO_InitTypeDef GPIO_InitStructure = {0};
GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
GPIO_InitStructure.Pull = GPIO_PULLUP;
GPIO_InitStructure.Pin = BACKLIGHT_PIN_PIN;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(BACKLIGHT_PIN_PORT, &GPIO_InitStructure);
}

static void backlight_off(void) {
GPIO_InitTypeDef GPIO_InitStructure = {0};
GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Pin = BACKLIGHT_PIN_PIN;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(BACKLIGHT_PIN_PORT, &GPIO_InitStructure);
}

void backlight_init(backlight_action_t action) {
backlight_driver_t *drv = &g_backlight_driver;

if (drv->initialized) {
return;
}

BACKLIGHT_PIN_CLK_ENABLE();

if (action == BACKLIGHT_RESET) {
backlight_off();
};

drv->initialized = true;
}

void backlight_deinit(backlight_action_t action) {
backlight_driver_t *drv = &g_backlight_driver;
if (!drv->initialized) {
return;
}

if (action == BACKLIGHT_RESET) {
backlight_off();
}
}

int backlight_set(int val) {
backlight_driver_t *drv = &g_backlight_driver;
if (!drv->initialized) {
return 0;
}

if (val > 0) {
backlight_on();
} else {
backlight_off();
}
drv->current_level = val;
return val;
}

int backlight_get(void) {
backlight_driver_t *drv = &g_backlight_driver;
if (!drv->initialized) {
return 0;
}
return drv->current_level;
}
Loading
Loading