1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-06-28 02:42:34 +00:00

refactor(core): split pmic as a separate feature

[no changelog]
This commit is contained in:
kopecdav 2025-05-08 15:24:14 +02:00 committed by kopecdav
parent 58e31be4ec
commit 4b9fb839cd
15 changed files with 165 additions and 144 deletions

View File

@ -29,7 +29,7 @@
#define TPS61062_EN_PORT GPIOB #define TPS61062_EN_PORT GPIOB
#define TPS61062_EN_CLK_ENA __HAL_RCC_GPIOB_CLK_ENABLE #define TPS61062_EN_CLK_ENA __HAL_RCC_GPIOB_CLK_ENABLE
#define NPM1300_I2C_INSTANCE 0 #define PMIC_I2C_INSTANCE 0
#define STWLC38_I2C_INSTANCE 1 #define STWLC38_I2C_INSTANCE 1

View File

@ -29,7 +29,7 @@
#define TPS61062_EN_PORT GPIOB #define TPS61062_EN_PORT GPIOB
#define TPS61062_EN_CLK_ENA __HAL_RCC_GPIOB_CLK_ENABLE #define TPS61062_EN_CLK_ENA __HAL_RCC_GPIOB_CLK_ENABLE
#define NPM1300_I2C_INSTANCE 0 #define PMIC_I2C_INSTANCE 0
#define STWLC38_I2C_INSTANCE 1 #define STWLC38_I2C_INSTANCE 1

View File

@ -29,7 +29,7 @@
#define TPS61062_EN_PORT GPIOB #define TPS61062_EN_PORT GPIOB
#define TPS61062_EN_CLK_ENA __HAL_RCC_GPIOB_CLK_ENABLE #define TPS61062_EN_CLK_ENA __HAL_RCC_GPIOB_CLK_ENABLE
#define NPM1300_I2C_INSTANCE 0 #define PMIC_I2C_INSTANCE 0
#define STWLC38_I2C_INSTANCE 1 #define STWLC38_I2C_INSTANCE 1

View File

@ -32,8 +32,8 @@
#include <util/option_bytes.h> #include <util/option_bytes.h>
#include <util/rsod.h> #include <util/rsod.h>
#ifdef USE_POWER_MANAGER #ifdef USE_PMIC
#include "../npm1300/npm1300.h" #include <sys/pmic.h>
#endif #endif
#ifdef USE_PVD #ifdef USE_PVD
@ -72,8 +72,8 @@ static const uint8_t * const BOARDLOADER_KEYS[] = {
}; };
static void drivers_init(void) { static void drivers_init(void) {
#ifdef USE_POWER_MANAGER #ifdef USE_PMIC
npm1300_init(); pmic_init();
#endif #endif
#ifdef USE_PVD #ifdef USE_PVD
pvd_init(); pvd_init();
@ -98,8 +98,8 @@ static void drivers_deinit(void) {
// TODO // TODO
#endif #endif
display_deinit(DISPLAY_JUMP_BEHAVIOR); display_deinit(DISPLAY_JUMP_BEHAVIOR);
#ifdef USE_POWER_MANAGER #ifdef USE_PMIC
npm1300_deinit(); pmic_deinit();
#endif #endif
} }

View File

@ -40,6 +40,7 @@ haptic = []
sd_card = [] sd_card = []
rgb_led = [] rgb_led = []
power_manager = [] power_manager = []
pmic = []
backlight = [] backlight = []
usb = [] usb = []
optiga = [] optiga = []

View File

@ -17,17 +17,16 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef TREZORHAL_NPM1300_H #pragma once
#define TREZORHAL_NPM1300_H
#include <trezor_types.h> #include <trezor_types.h>
// Charging current limits // Charging current limits
// - range of np1300 is 32-800mA // - range of pmic is 32-800mA
// - used battery limit is 180mA // - used battery limit is 180mA
#define NPM1300_CHARGING_LIMIT_MIN 32 // mA #define PMIC_CHARGING_LIMIT_MIN 32 // mA
#define NPM1300_CHARGING_LIMIT_MAX 180 // mA #define PMIC_CHARGING_LIMIT_MAX 180 // mA
#define NPM1300_CHARGING_LIMIT_DEFAULT 180 // mA #define PMIC_CHARGING_LIMIT_DEFAULT 180 // mA
typedef struct { typedef struct {
// Battery voltage [V] // Battery voltage [V]
@ -43,25 +42,24 @@ typedef struct {
// Die temperature [°C] // Die temperature [°C]
float die_temp; float die_temp;
// IBAT_MEAS_STATUS register value // IBAT_MEAS_STATUS register value
// (for debugging purposes, see the NPM1300 datasheet) // (for debugging purposes, see the datasheet)
uint8_t ibat_meas_status; uint8_t ibat_meas_status;
// BUCKSTATUS register value // BUCKSTATUS register value
// (for debugging purposes, see the NPM1300 datasheet) // (for debugging purposes, see the datasheet)
uint8_t buck_status; uint8_t buck_status;
uint8_t usb_status; uint8_t usb_status;
} npm1300_report_t; } pmic_report_t;
typedef void (*npm1300_report_callback_t)(void* context, typedef void (*pmic_report_callback_t)(void* context, pmic_report_t* report);
npm1300_report_t* report);
// Initializes NPM1300 PMIC driver // Initializes PMIC driver
bool npm1300_init(void); bool pmic_init(void);
// Deinitializes NPM1300 PMIC driver // Deinitializes PMIC driver
void npm1300_deinit(void); void pmic_deinit(void);
// Gets the cause of the last restart // Gets the cause of the last restart
uint8_t npm1300_restart_cause(void); uint8_t pmic_restart_cause(void);
// Switches the device to ship mode. // Switches the device to ship mode.
// //
@ -78,7 +76,7 @@ uint8_t npm1300_restart_cause(void);
// //
// Returns `false` if the operation fails (likely due to uninitialized power // Returns `false` if the operation fails (likely due to uninitialized power
// management). // management).
bool npm1300_enter_shipmode(void); bool pmic_enter_shipmode(void);
// Starts the asynchronous measurement // Starts the asynchronous measurement
// //
@ -88,7 +86,7 @@ bool npm1300_enter_shipmode(void);
// The function returns `false` if the measurement cannot be started // The function returns `false` if the measurement cannot be started
// (e.g. because the previous measurement is still in progress or // (e.g. because the previous measurement is still in progress or
// the the driver is not initialized). // the the driver is not initialized).
bool npm1300_measure(npm1300_report_callback_t callback, void* context); bool pmic_measure(pmic_report_callback_t callback, void* context);
// Synchroneous version of the `pmic_measure` function. // Synchroneous version of the `pmic_measure` function.
// //
@ -97,12 +95,12 @@ bool npm1300_measure(npm1300_report_callback_t callback, void* context);
// //
// Returns `true` if the measurement was successful and the report // Returns `true` if the measurement was successful and the report
// is stored in the `report` structure. // is stored in the `report` structure.
bool npm1300_measure_sync(npm1300_report_t* report); bool pmic_measure_sync(pmic_report_t* report);
// Enables or disables the charging. // Enables or disables the charging.
// //
// The function returns `false` if the operation cannot be performed. // The function returns `false` if the operation cannot be performed.
bool npm1300_set_charging(bool enable); bool pmic_set_charging(bool enable);
// Sets the charging current limit [mA]. // Sets the charging current limit [mA].
// //
@ -110,18 +108,16 @@ bool npm1300_set_charging(bool enable);
// `NPM1300_CHARGING_LIMIT_MIN` and `NPM1300_CHARGING_LIMIT_MAX` constants. // `NPM1300_CHARGING_LIMIT_MIN` and `NPM1300_CHARGING_LIMIT_MAX` constants.
// //
// The function returns `false` if the operation cannot be performed. // The function returns `false` if the operation cannot be performed.
bool npm1300_set_charging_limit(int i_charge); bool pmic_set_charging_limit(int i_charge);
// Gets the charging current limit [mA]. // Gets the charging current limit [mA].
int npm1300_get_charging_limit(void); int pmic_get_charging_limit(void);
typedef enum { typedef enum {
NPM1300_BUCK_MODE_AUTO, PMIC_BUCK_MODE_AUTO,
NPM1300_BUCK_MODE_PWM, PMIC_BUCK_MODE_PWM,
NPM1300_BUCK_MODE_PFM, PMIC_BUCK_MODE_PFM,
} npm1300_buck_mode_t; } pmic_buck_mode_t;
// Set the buck voltage regulator mode // Set the buck voltage regulator mode
bool npm1300_set_buck_mode(npm1300_buck_mode_t buck_mode); bool pmic_set_buck_mode(pmic_buck_mode_t buck_mode);
#endif // TREZORHAL_NPM1300_H

View File

@ -24,9 +24,9 @@
#include <io/i2c_bus.h> #include <io/i2c_bus.h>
#include <sys/irq.h> #include <sys/irq.h>
#include <sys/pmic.h>
#include <sys/systimer.h> #include <sys/systimer.h>
#include "npm1300.h"
#include "npm1300_defs.h" #include "npm1300_defs.h"
#ifdef KERNEL_MODE #ifdef KERNEL_MODE
@ -111,9 +111,9 @@ typedef struct {
bool charging_requested; bool charging_requested;
// Buck voltage regulator mode // Buck voltage regulator mode
npm1300_buck_mode_t buck_mode; // written value pmic_buck_mode_t buck_mode; // written value
npm1300_buck_mode_t buck_mode_requested; // requested value pmic_buck_mode_t buck_mode_requested; // requested value
npm1300_buck_mode_t buck_mode_set; // value beeing written pmic_buck_mode_t buck_mode_set; // value beeing written
// Enter ship mode // Enter ship mode
bool shipmode_requested; bool shipmode_requested;
@ -123,7 +123,7 @@ typedef struct {
bool adc_readout_requested; bool adc_readout_requested;
// Report callback used for asynchronous measurements // Report callback used for asynchronous measurements
npm1300_report_callback_t report_callback; pmic_report_callback_t report_callback;
void* report_callback_context; void* report_callback_context;
} npm1300_driver_t; } npm1300_driver_t;
@ -295,7 +295,7 @@ static bool npm1300_initialize(i2c_bus_t* bus, uint16_t i_charge,
return true; return true;
} }
bool npm1300_init(void) { bool pmic_init(void) {
npm1300_driver_t* drv = &g_npm1300_driver; npm1300_driver_t* drv = &g_npm1300_driver;
if (drv->initialized) { if (drv->initialized) {
@ -304,17 +304,17 @@ bool npm1300_init(void) {
memset(drv, 0, sizeof(npm1300_driver_t)); memset(drv, 0, sizeof(npm1300_driver_t));
drv->i_charge = NPM1300_CHARGING_LIMIT_DEFAULT; // mA drv->i_charge = PMIC_CHARGING_LIMIT_DEFAULT; // mA
drv->i_limit = 500; // mA (268mA-1340mA) drv->i_limit = 500; // mA (268mA-1340mA)
drv->i_charge_set = drv->i_charge; drv->i_charge_set = drv->i_charge;
drv->i_charge_requested = drv->i_charge; drv->i_charge_requested = drv->i_charge;
drv->buck_mode_requested = NPM1300_BUCK_MODE_AUTO; drv->buck_mode_requested = PMIC_BUCK_MODE_AUTO;
drv->buck_mode_set = NPM1300_BUCK_MODE_AUTO; drv->buck_mode_set = PMIC_BUCK_MODE_AUTO;
drv->buck_mode = NPM1300_BUCK_MODE_AUTO; drv->buck_mode = PMIC_BUCK_MODE_AUTO;
drv->i2c_bus = i2c_bus_open(NPM1300_I2C_INSTANCE); drv->i2c_bus = i2c_bus_open(PMIC_I2C_INSTANCE);
if (drv->i2c_bus == NULL) { if (drv->i2c_bus == NULL) {
goto cleanup; goto cleanup;
} }
@ -337,11 +337,11 @@ bool npm1300_init(void) {
return true; return true;
cleanup: cleanup:
npm1300_deinit(); pmic_deinit();
return false; return false;
} }
void npm1300_deinit(void) { void pmic_deinit(void) {
npm1300_driver_t* drv = &g_npm1300_driver; npm1300_driver_t* drv = &g_npm1300_driver;
i2c_bus_close(drv->i2c_bus); i2c_bus_close(drv->i2c_bus);
@ -350,7 +350,7 @@ void npm1300_deinit(void) {
memset(drv, 0, sizeof(npm1300_driver_t)); memset(drv, 0, sizeof(npm1300_driver_t));
} }
bool npm1300_enter_shipmode(void) { bool pmic_enter_shipmode(void) {
npm1300_driver_t* drv = &g_npm1300_driver; npm1300_driver_t* drv = &g_npm1300_driver;
if (!drv->initialized) { if (!drv->initialized) {
@ -365,7 +365,7 @@ bool npm1300_enter_shipmode(void) {
return true; return true;
} }
int npm1300_get_charging_limit(void) { int pmic_get_charging_limit(void) {
npm1300_driver_t* drv = &g_npm1300_driver; npm1300_driver_t* drv = &g_npm1300_driver;
if (!drv->initialized) { if (!drv->initialized) {
@ -375,15 +375,15 @@ int npm1300_get_charging_limit(void) {
return drv->i_charge_requested; return drv->i_charge_requested;
} }
bool npm1300_set_charging_limit(int i_charge) { bool pmic_set_charging_limit(int i_charge) {
npm1300_driver_t* drv = &g_npm1300_driver; npm1300_driver_t* drv = &g_npm1300_driver;
if (!drv->initialized) { if (!drv->initialized) {
return false; return false;
} }
if (i_charge < NPM1300_CHARGING_LIMIT_MIN || if (i_charge < PMIC_CHARGING_LIMIT_MIN ||
i_charge > NPM1300_CHARGING_LIMIT_MAX) { i_charge > PMIC_CHARGING_LIMIT_MAX) {
// The value is out of range // The value is out of range
return false; return false;
} }
@ -396,7 +396,7 @@ bool npm1300_set_charging_limit(int i_charge) {
return true; return true;
} }
bool npm1300_set_charging(bool enable) { bool pmic_set_charging(bool enable) {
npm1300_driver_t* drv = &g_npm1300_driver; npm1300_driver_t* drv = &g_npm1300_driver;
if (!drv->initialized) { if (!drv->initialized) {
@ -411,7 +411,7 @@ bool npm1300_set_charging(bool enable) {
return true; return true;
} }
bool npm1300_set_buck_mode(npm1300_buck_mode_t buck_mode) { bool pmic_set_buck_mode(pmic_buck_mode_t buck_mode) {
npm1300_driver_t* drv = &g_npm1300_driver; npm1300_driver_t* drv = &g_npm1300_driver;
if (!drv->initialized) { if (!drv->initialized) {
@ -436,7 +436,7 @@ uint8_t npm1300_restart_cause(void) {
return drv->restart_cause; return drv->restart_cause;
} }
bool npm1300_measure(npm1300_report_callback_t callback, void* context) { bool pmic_measure(pmic_report_callback_t callback, void* context) {
npm1300_driver_t* drv = &g_npm1300_driver; npm1300_driver_t* drv = &g_npm1300_driver;
if (!drv->initialized) { if (!drv->initialized) {
@ -470,25 +470,25 @@ typedef struct {
// Set when the measurement is done // Set when the measurement is done
volatile bool done; volatile bool done;
// Report structure where the measurement is stored // Report structure where the measurement is stored
npm1300_report_t* report; pmic_report_t* report;
} npm1300_sync_measure_t; } npm1300_sync_measure_t;
// Callback for the synchronous measurement // Callback for the synchronous measurement
static void npm1300_sync_measure_callback(void* context, static void npm1300_sync_measure_callback(void* context,
npm1300_report_t* report) { pmic_report_t* report) {
npm1300_sync_measure_t* ctx = (npm1300_sync_measure_t*)context; npm1300_sync_measure_t* ctx = (npm1300_sync_measure_t*)context;
*ctx->report = *report; *ctx->report = *report;
ctx->done = true; ctx->done = true;
} }
bool npm1300_measure_sync(npm1300_report_t* report) { bool pmic_measure_sync(pmic_report_t* report) {
npm1300_sync_measure_t measure = { npm1300_sync_measure_t measure = {
.done = false, .done = false,
.report = report, .report = report,
}; };
// Start asynchronous measurement // Start asynchronous measurement
if (!npm1300_measure(npm1300_sync_measure_callback, &measure)) { if (!pmic_measure(npm1300_sync_measure_callback, &measure)) {
return false; return false;
} }
@ -505,8 +505,8 @@ bool npm1300_measure_sync(npm1300_report_t* report) {
// //
// This function is called in the irq context. // This function is called in the irq context.
static void npm1300_calculate_report(npm1300_driver_t* drv, static void npm1300_calculate_report(npm1300_driver_t* drv,
npm1300_report_t* report) { pmic_report_t* report) {
memset(report, 0, sizeof(npm1300_report_t)); memset(report, 0, sizeof(pmic_report_t));
npm1300_adc_regs_t* r = &drv->adc_regs; npm1300_adc_regs_t* r = &drv->adc_regs;
@ -784,11 +784,11 @@ static void npm1300_i2c_callback(void* context, i2c_packet_t* packet) {
case NPM1300_STATE_ADC_READOUT: case NPM1300_STATE_ADC_READOUT:
drv->adc_readout_requested = false; drv->adc_readout_requested = false;
npm1300_report_t report; pmic_report_t report;
npm1300_calculate_report(drv, &report); npm1300_calculate_report(drv, &report);
// Invoke report callback // Invoke report callback
npm1300_report_callback_t report_callback = drv->report_callback; pmic_report_callback_t report_callback = drv->report_callback;
void* report_callback_context = drv->report_callback_context; void* report_callback_context = drv->report_callback_context;
// Clear the report callback before invoking it // Clear the report callback before invoking it
@ -848,9 +848,9 @@ static void npm1300_fsm_continue(npm1300_driver_t* drv) {
} }
} else if (drv->buck_mode != drv->buck_mode_requested) { } else if (drv->buck_mode != drv->buck_mode_requested) {
drv->buck_mode_set = drv->buck_mode_requested; drv->buck_mode_set = drv->buck_mode_requested;
if (drv->buck_mode_set == NPM1300_BUCK_MODE_PWM) { if (drv->buck_mode_set == PMIC_BUCK_MODE_PWM) {
npm1300_i2c_submit(drv, npm1300_ops_buck_pwm); npm1300_i2c_submit(drv, npm1300_ops_buck_pwm);
} else if (drv->buck_mode_set == NPM1300_BUCK_MODE_PFM) { } else if (drv->buck_mode_set == PMIC_BUCK_MODE_PFM) {
npm1300_i2c_submit(drv, npm1300_ops_buck_pfm); npm1300_i2c_submit(drv, npm1300_ops_buck_pfm);
} else { } else {
npm1300_i2c_submit(drv, npm1300_ops_buck_auto); npm1300_i2c_submit(drv, npm1300_ops_buck_auto);

View File

@ -23,9 +23,9 @@
#include <io/display.h> #include <io/display.h>
#include <io/usb.h> #include <io/usb.h>
#include <sys/irq.h> #include <sys/irq.h>
#include <sys/pmic.h>
#include <sys/systick.h> #include <sys/systick.h>
#include "../npm1300/npm1300.h"
#include "power_manager_internal.h" #include "power_manager_internal.h"
#ifdef USE_OPTIGA #ifdef USE_OPTIGA
@ -60,8 +60,8 @@ static void pm_background_tasks_resume(void);
pm_status_t pm_control_hibernate() { pm_status_t pm_control_hibernate() {
// TEMPORARY FIX: // TEMPORARY FIX:
// Enable Backup domain retentaion in VBAT mode before entering the // Enable Backup domain retention in VBAT mode before entering the
// hiberbation. BREN bit can be accessed only in LDO mode. // hibernation. BREN bit can be accessed only in LDO mode.
__HAL_RCC_PWR_CLK_ENABLE(); __HAL_RCC_PWR_CLK_ENABLE();
// Switch to LDO regulator // Switch to LDO regulator
@ -72,7 +72,7 @@ pm_status_t pm_control_hibernate() {
// Enable backup domain retention // Enable backup domain retention
PWR->BDCR1 |= PWR_BDCR1_BREN; PWR->BDCR1 |= PWR_BDCR1_BREN;
if (!npm1300_enter_shipmode()) { if (!pmic_enter_shipmode()) {
return PM_ERROR; return PM_ERROR;
} }
@ -210,7 +210,7 @@ void pm_control_suspend() {
static void pm_background_tasks_suspend(void) { static void pm_background_tasks_suspend(void) {
// stwlc38 // stwlc38
// npm1300 // pmic
// nrf52 // nrf52
// ble // ble
// powerctl // powerctl

View File

@ -19,11 +19,11 @@
#include <sys/backup_ram.h> #include <sys/backup_ram.h>
#include <sys/irq.h> #include <sys/irq.h>
#include <sys/pmic.h>
#include <sys/systick.h> #include <sys/systick.h>
#include <sys/systimer.h> #include <sys/systimer.h>
#include <trezor_rtl.h> #include <trezor_rtl.h>
#include "../npm1300/npm1300.h"
#include "../stwlc38/stwlc38.h" #include "../stwlc38/stwlc38.h"
#include "power_manager_internal.h" #include "power_manager_internal.h"
@ -49,7 +49,7 @@ pm_status_t pm_init(bool inherit_state) {
memset(drv, 0, sizeof(pm_driver_t)); memset(drv, 0, sizeof(pm_driver_t));
// Initialize hardware subsystems // Initialize hardware subsystems
if (!npm1300_init() || !stwlc38_init()) { if (!pmic_init() || !stwlc38_init()) {
pm_deinit(); pm_deinit();
return PM_ERROR; return PM_ERROR;
} }
@ -67,7 +67,7 @@ pm_status_t pm_init(bool inherit_state) {
drv->shutdown_timer = systimer_create(pm_shutdown_timer_handler, NULL); drv->shutdown_timer = systimer_create(pm_shutdown_timer_handler, NULL);
// Initial power source measurement // Initial power source measurement
npm1300_measure(pm_pmic_data_ready, NULL); pmic_measure(pm_pmic_data_ready, NULL);
// Try to recover SoC from the backup RAM // Try to recover SoC from the backup RAM
backup_ram_power_manager_data_t pm_recovery_data; backup_ram_power_manager_data_t pm_recovery_data;
@ -84,7 +84,7 @@ pm_status_t pm_init(bool inherit_state) {
} }
if (inherit_state) { if (inherit_state) {
// Inherit power manager state left in beckup RAM from bootloader. // Inherit power manager state left in backup RAM from bootloader.
// in case of error, start with PM_STATE_POWER_SAVE as a lowest state in // in case of error, start with PM_STATE_POWER_SAVE as a lowest state in
// active mode. // active mode.
if (status != BACKUP_RAM_OK && if (status != BACKUP_RAM_OK &&
@ -140,7 +140,7 @@ void pm_deinit(void) {
pm_store_data_to_backup_ram(); pm_store_data_to_backup_ram();
} }
npm1300_deinit(); pmic_deinit();
stwlc38_deinit(); stwlc38_deinit();
drv->initialized = false; drv->initialized = false;

View File

@ -19,12 +19,12 @@
#pragma once #pragma once
#include <sys/pmic.h>
#include <sys/power_manager.h> #include <sys/power_manager.h>
#include <sys/systimer.h> #include <sys/systimer.h>
#include <trezor_types.h> #include <trezor_types.h>
#include "../fuel_gauge/fuel_gauge.h" #include "../fuel_gauge/fuel_gauge.h"
#include "../npm1300/npm1300.h"
#include "../stwlc38/stwlc38.h" #include "../stwlc38/stwlc38.h"
// Power manager thresholds & timings // Power manager thresholds & timings
@ -50,7 +50,7 @@
#define PM_CLEAR_EVENT(flags, event) ((flags) &= ~(event)) #define PM_CLEAR_EVENT(flags, event) ((flags) &= ~(event))
#define PM_CLEAR_ALL_EVENTS(flags) ((flags) = 0) #define PM_CLEAR_ALL_EVENTS(flags) ((flags) = 0)
// Power manager battery sampling data structure) // Power manager battery sampling data structure
typedef struct { typedef struct {
float vbat; // Battery voltage [V] float vbat; // Battery voltage [V]
float ibat; // Battery current [mA] float ibat; // Battery current [mA]
@ -78,7 +78,7 @@ typedef struct {
uint32_t charging_target_timestamp; uint32_t charging_target_timestamp;
// Power source hardware state // Power source hardware state
npm1300_report_t pmic_data; pmic_report_t pmic_data;
stwlc38_report_t wireless_data; stwlc38_report_t wireless_data;
uint32_t pmic_last_update_ms; uint32_t pmic_last_update_ms;
uint32_t pmic_sampling_period_ms; uint32_t pmic_sampling_period_ms;
@ -118,7 +118,7 @@ extern pm_driver_t g_pm;
// Internal function declarations // Internal function declarations
void pm_monitor_power_sources(void); void pm_monitor_power_sources(void);
void pm_process_state_machine(void); void pm_process_state_machine(void);
void pm_pmic_data_ready(void* context, npm1300_report_t* report); void pm_pmic_data_ready(void* context, pmic_report_t* report);
void pm_charging_controller(pm_driver_t* drv); void pm_charging_controller(pm_driver_t* drv);
void pm_battery_sampling(float vbat, float ibat, float ntc_temp); void pm_battery_sampling(float vbat, float ibat, float ntc_temp);
void pm_battery_initial_soc_guess(void); void pm_battery_initial_soc_guess(void);

View File

@ -19,11 +19,11 @@
*/ */
#include <sys/backup_ram.h> #include <sys/backup_ram.h>
#include <sys/pmic.h>
#include <sys/systick.h> #include <sys/systick.h>
#include <trezor_rtl.h> #include <trezor_rtl.h>
#include "../fuel_gauge/fuel_gauge.h" #include "../fuel_gauge/fuel_gauge.h"
#include "../npm1300/npm1300.h"
#include "../stwlc38/stwlc38.h" #include "../stwlc38/stwlc38.h"
#include "power_manager_internal.h" #include "power_manager_internal.h"
@ -80,7 +80,7 @@ void pm_monitor_power_sources(void) {
drv->pmic_data.ntc_temp); drv->pmic_data.ntc_temp);
// Request fresh measurements // Request fresh measurements
npm1300_measure(pm_pmic_data_ready, NULL); pmic_measure(pm_pmic_data_ready, NULL);
drv->pmic_measurement_ready = false; drv->pmic_measurement_ready = false;
return; return;
@ -111,14 +111,14 @@ void pm_monitor_power_sources(void) {
pm_store_data_to_backup_ram(); pm_store_data_to_backup_ram();
// Request fresh measurements // Request fresh measurements
npm1300_measure(pm_pmic_data_ready, NULL); pmic_measure(pm_pmic_data_ready, NULL);
drv->pmic_measurement_ready = false; drv->pmic_measurement_ready = false;
drv->state_machine_stabilized = true; drv->state_machine_stabilized = true;
} }
// PMIC measurement callback // PMIC measurement callback
void pm_pmic_data_ready(void* context, npm1300_report_t* report) { void pm_pmic_data_ready(void* context, pmic_report_t* report) {
pm_driver_t* drv = &g_pm; pm_driver_t* drv = &g_pm;
// Store measurement timestamp // Store measurement timestamp
@ -132,7 +132,7 @@ void pm_pmic_data_ready(void* context, npm1300_report_t* report) {
drv->pmic_last_update_ms = systick_ms(); drv->pmic_last_update_ms = systick_ms();
// Copy PMIC data // Copy PMIC data
memcpy(&drv->pmic_data, report, sizeof(npm1300_report_t)); memcpy(&drv->pmic_data, report, sizeof(pmic_report_t));
// Get wireless charger data // Get wireless charger data
stwlc38_get_report(&drv->wireless_data); stwlc38_get_report(&drv->wireless_data);
@ -152,22 +152,22 @@ void pm_charging_controller(pm_driver_t* drv) {
} }
} else if (drv->usb_connected) { } else if (drv->usb_connected) {
// USB connected, set maximum charging current right away // USB connected, set maximum charging current right away
drv->charging_current_target_ma = NPM1300_CHARGING_LIMIT_MAX; drv->charging_current_target_ma = PMIC_CHARGING_LIMIT_MAX;
} else if (drv->wireless_connected) { } else if (drv->wireless_connected) {
// Gradually increase charging current to the maximum // Gradually increase charging current to the maximum
if (drv->charging_current_target_ma == NPM1300_CHARGING_LIMIT_MAX) { if (drv->charging_current_target_ma == PMIC_CHARGING_LIMIT_MAX) {
// No action required // No action required
} else if (drv->charging_current_target_ma == 0) { } else if (drv->charging_current_target_ma == 0) {
drv->charging_current_target_ma = NPM1300_CHARGING_LIMIT_MIN; drv->charging_current_target_ma = PMIC_CHARGING_LIMIT_MIN;
drv->charging_target_timestamp = systick_ms(); drv->charging_target_timestamp = systick_ms();
} else if (systick_ms() - drv->charging_target_timestamp > } else if (systick_ms() - drv->charging_target_timestamp >
PM_WPC_CHARGE_CURR_STEP_TIMEOUT_MS) { PM_WPC_CHARGE_CURR_STEP_TIMEOUT_MS) {
drv->charging_current_target_ma += PM_WPC_CHARGE_CURR_STEP_MA; drv->charging_current_target_ma += PM_WPC_CHARGE_CURR_STEP_MA;
drv->charging_target_timestamp = systick_ms(); drv->charging_target_timestamp = systick_ms();
if (drv->charging_current_target_ma > NPM1300_CHARGING_LIMIT_MAX) { if (drv->charging_current_target_ma > PMIC_CHARGING_LIMIT_MAX) {
drv->charging_current_target_ma = NPM1300_CHARGING_LIMIT_MAX; drv->charging_current_target_ma = PMIC_CHARGING_LIMIT_MAX;
} }
} }
@ -177,15 +177,15 @@ void pm_charging_controller(pm_driver_t* drv) {
} }
// Set charging target // Set charging target
if (drv->charging_current_target_ma != npm1300_get_charging_limit()) { if (drv->charging_current_target_ma != pmic_get_charging_limit()) {
// Set charging current limit // Set charging current limit
npm1300_set_charging_limit(drv->charging_current_target_ma); pmic_set_charging_limit(drv->charging_current_target_ma);
} }
if (drv->charging_current_target_ma == 0) { if (drv->charging_current_target_ma == 0) {
npm1300_set_charging(false); pmic_set_charging(false);
} else { } else {
npm1300_set_charging(true); pmic_set_charging(true);
} }
} }

View File

@ -18,14 +18,11 @@
*/ */
#include <io/backlight.h> #include <io/backlight.h>
#ifdef USE_BUTTON
#include <io/button.h>
#endif
#include <sys/bootutils.h> #include <sys/bootutils.h>
#include <sys/pmic.h>
#include <sys/systick.h> #include <sys/systick.h>
#include <sys/systimer.h> #include <sys/systimer.h>
#include "../npm1300/npm1300.h"
#include "power_manager_internal.h" #include "power_manager_internal.h"
// State handler lookup table // State handler lookup table
@ -140,12 +137,11 @@ pm_internal_state_t pm_handle_state_charging(pm_driver_t* drv) {
return PM_STATE_CHARGING; return PM_STATE_CHARGING;
} }
// Not implemented yet
return drv->state; return drv->state;
} }
pm_internal_state_t pm_handle_state_suspend(pm_driver_t* drv) { pm_internal_state_t pm_handle_state_suspend(pm_driver_t* drv) {
// immediatelly return to power save state after wakeup // immediately return to power save state after wakeup
return PM_STATE_POWER_SAVE; return PM_STATE_POWER_SAVE;
} }
@ -158,7 +154,6 @@ pm_internal_state_t pm_handle_state_startup_rejected(pm_driver_t* drv) {
return PM_STATE_HIBERNATE; return PM_STATE_HIBERNATE;
} }
// Not implemented yet
return drv->state; return drv->state;
} }

View File

@ -235,19 +235,28 @@ def configure(
("USE_OEM_KEYS_CHECK", "1"), ("USE_OEM_KEYS_CHECK", "1"),
] ]
sources += [ if ("pmic" in features_wanted) or ("power_manager" in features_wanted):
"embed/sys/power_manager/stm32u5/power_manager.c", sources += [
"embed/sys/power_manager/stm32u5/power_monitoring.c", "embed/sys/power_manager/npm1300/npm1300.c"
"embed/sys/power_manager/stm32u5/power_states.c", ]
"embed/sys/power_manager/stm32u5/power_control.c", paths += ["embed/sys/power_manager/inc"]
"embed/sys/power_manager/npm1300/npm1300.c", defines += ["USE_PMIC"]
"embed/sys/power_manager/fuel_gauge/fuel_gauge.c", features_available.append("pmic")
"embed/sys/power_manager/fuel_gauge/battery_model.c",
"embed/sys/power_manager/stwlc38/stwlc38.c", if "power_manager" in features_wanted:
"embed/sys/power_manager/stwlc38/stwlc38_patching.c", sources += [
] "embed/sys/power_manager/stm32u5/power_manager.c",
paths += ["embed/sys/power_manager/inc"] "embed/sys/power_manager/stm32u5/power_monitoring.c",
defines += [("USE_POWER_MANAGER", "1")] "embed/sys/power_manager/stm32u5/power_states.c",
"embed/sys/power_manager/stm32u5/power_control.c",
"embed/sys/power_manager/fuel_gauge/fuel_gauge.c",
"embed/sys/power_manager/fuel_gauge/battery_model.c",
"embed/sys/power_manager/stwlc38/stwlc38.c",
"embed/sys/power_manager/stwlc38/stwlc38_patching.c",
]
paths += ["embed/sys/power_manager/inc"]
defines += [("USE_POWER_MANAGER", "1")]
features_available.append("power_manager")
env.get("ENV")["LINKER_SCRIPT"] = linker_script env.get("ENV")["LINKER_SCRIPT"] = linker_script

View File

@ -240,19 +240,29 @@ def configure(
("USE_OEM_KEYS_CHECK", "1"), ("USE_OEM_KEYS_CHECK", "1"),
] ]
sources += [ if ("pmic" in features_wanted) or ("power_manager" in features_wanted):
"embed/sys/power_manager/stm32u5/power_manager.c", sources += [
"embed/sys/power_manager/stm32u5/power_monitoring.c", "embed/sys/power_manager/npm1300/npm1300.c"
"embed/sys/power_manager/stm32u5/power_states.c", ]
"embed/sys/power_manager/stm32u5/power_control.c", paths += ["embed/sys/power_manager/inc"]
"embed/sys/power_manager/npm1300/npm1300.c", defines += ["USE_PMIC"]
"embed/sys/power_manager/fuel_gauge/fuel_gauge.c", features_available.append("pmic")
"embed/sys/power_manager/fuel_gauge/battery_model.c",
"embed/sys/power_manager/stwlc38/stwlc38.c", if "power_manager" in features_wanted:
"embed/sys/power_manager/stwlc38/stwlc38_patching.c", sources += [
] "embed/sys/power_manager/stm32u5/power_manager.c",
paths += ["embed/sys/power_manager/inc"] "embed/sys/power_manager/stm32u5/power_monitoring.c",
defines += [("USE_POWER_MANAGER", "1")] "embed/sys/power_manager/stm32u5/power_states.c",
"embed/sys/power_manager/stm32u5/power_control.c",
"embed/sys/power_manager/fuel_gauge/fuel_gauge.c",
"embed/sys/power_manager/fuel_gauge/battery_model.c",
"embed/sys/power_manager/stwlc38/stwlc38.c",
"embed/sys/power_manager/stwlc38/stwlc38_patching.c",
]
paths += ["embed/sys/power_manager/inc"]
defines += [("USE_POWER_MANAGER", "1")]
features_available.append("power_manager")
env.get("ENV")["LINKER_SCRIPT"] = linker_script env.get("ENV")["LINKER_SCRIPT"] = linker_script

View File

@ -240,19 +240,29 @@ def configure(
("USE_OEM_KEYS_CHECK", "1"), ("USE_OEM_KEYS_CHECK", "1"),
] ]
sources += [ if ("pmic" in features_wanted) or ("power_manager" in features_wanted):
"embed/sys/power_manager/stm32u5/power_manager.c", sources += [
"embed/sys/power_manager/stm32u5/power_monitoring.c", "embed/sys/power_manager/npm1300/npm1300.c"
"embed/sys/power_manager/stm32u5/power_states.c", ]
"embed/sys/power_manager/stm32u5/power_control.c", paths += ["embed/sys/power_manager/inc"]
"embed/sys/power_manager/npm1300/npm1300.c", defines += ["USE_PMIC"]
"embed/sys/power_manager/fuel_gauge/fuel_gauge.c", features_available.append("pmic")
"embed/sys/power_manager/fuel_gauge/battery_model.c",
"embed/sys/power_manager/stwlc38/stwlc38.c", if "power_manager" in features_wanted:
"embed/sys/power_manager/stwlc38/stwlc38_patching.c", sources += [
] "embed/sys/power_manager/stm32u5/power_manager.c",
paths += ["embed/sys/power_manager/inc"] "embed/sys/power_manager/stm32u5/power_monitoring.c",
defines += [("USE_POWER_MANAGER", "1")] "embed/sys/power_manager/stm32u5/power_states.c",
"embed/sys/power_manager/stm32u5/power_control.c",
"embed/sys/power_manager/fuel_gauge/fuel_gauge.c",
"embed/sys/power_manager/fuel_gauge/battery_model.c",
"embed/sys/power_manager/stwlc38/stwlc38.c",
"embed/sys/power_manager/stwlc38/stwlc38_patching.c",
]
paths += ["embed/sys/power_manager/inc"]
defines += [("USE_POWER_MANAGER", "1")]
features_available.append("power_manager")
env.get("ENV")["LINKER_SCRIPT"] = linker_script env.get("ENV")["LINKER_SCRIPT"] = linker_script