mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-07-24 15:38:22 +00:00
feat(core): add power manager to revison C [no changelog]
This commit is contained in:
parent
311a8f8d7c
commit
90468a2220
@ -59,7 +59,7 @@ power_manager_status_t power_manager_init(power_manager_state_t initial_state) {
|
||||
}
|
||||
|
||||
// Clear fuel gauge state
|
||||
memcpy(drv->fuel_gauge, 0, sizeof(fuel_gauge_state_t));
|
||||
memset(&drv->fuel_gauge, 0, sizeof(fuel_gauge_state_t));
|
||||
|
||||
// Initialize fuel gauge
|
||||
fuel_gauge_init(&(drv->fuel_gauge), POWER_MANAGER_FUEL_GAUGE_R,
|
||||
|
@ -68,8 +68,8 @@ typedef struct {
|
||||
bool fuel_gauge_initialized;
|
||||
power_manager_sampling_data_t bat_sampling_buf[
|
||||
POWER_MANAGER_BATTERY_SAMPLING_BUF_SIZE];
|
||||
uint8_t bat_sampling_buf_tail_idx = 0;
|
||||
uint8_t bat_sampling_buf_head_idx = 0;
|
||||
uint8_t bat_sampling_buf_tail_idx;
|
||||
uint8_t bat_sampling_buf_head_idx;
|
||||
|
||||
|
||||
// Battery charging state
|
||||
@ -114,6 +114,7 @@ void pm_monitor_power_sources(void);
|
||||
void pm_process_state_machine(void);
|
||||
void pm_pmic_data_ready(void* context, npm1300_report_t* report);
|
||||
void pm_charging_controller(power_manager_driver_t* drv);
|
||||
void pm_battery_sampling(float vbat, float ibat, float ntc_temp);
|
||||
void pm_battery_initial_soc_guess(void);
|
||||
|
||||
// State handlers
|
||||
|
@ -41,6 +41,9 @@ void pm_monitor_power_sources(void) {
|
||||
drv->pmic_data.ntc_temp);
|
||||
} else {
|
||||
|
||||
pm_battery_sampling(drv->pmic_data.vbat, drv->pmic_data.ibat,
|
||||
drv->pmic_data.ntc_temp);
|
||||
|
||||
// Battery sampling period, collect data before initial guess is made.
|
||||
fuel_gauge_initial_guess(&drv->fuel_gauge, drv->pmic_data.vbat,
|
||||
drv->pmic_data.ibat, drv->pmic_data.ntc_temp);
|
||||
@ -169,22 +172,22 @@ void pm_battery_sampling(float vbat, float ibat, float ntc_temp) {
|
||||
power_manager_driver_t* drv = &g_power_manager;
|
||||
|
||||
// Store battery data in the buffer
|
||||
drv->bat_sampling_buf[bat_sampling_buf_head_idx].vbat = vbat;
|
||||
drv->bat_sampling_buf[bat_sampling_buf_head_idx].ibat = ibat;
|
||||
drv->bat_sampling_buf[bat_sampling_buf_head_idx].ntc_temp = ntc_temp;
|
||||
drv->bat_sampling_buf[drv->bat_sampling_buf_head_idx].vbat = vbat;
|
||||
drv->bat_sampling_buf[drv->bat_sampling_buf_head_idx].ibat = ibat;
|
||||
drv->bat_sampling_buf[drv->bat_sampling_buf_head_idx].ntc_temp = ntc_temp;
|
||||
|
||||
// Update head index
|
||||
drv->bat_sampling_buf_head_idx++;
|
||||
if (bat_sampling_buf_head_idx >= POWER_MANAGER_BAT_DATA_BUF_SIZE) {
|
||||
bat_sampling_buf_head_idx = 0;
|
||||
if (drv->bat_sampling_buf_head_idx >= POWER_MANAGER_BATTERY_SAMPLING_BUF_SIZE) {
|
||||
drv->bat_sampling_buf_head_idx = 0;
|
||||
}
|
||||
|
||||
// Check if the buffer is full
|
||||
if (bat_sampling_buf_head_idx == bat_sampling_buf_tail_idx) {
|
||||
if (drv->bat_sampling_buf_head_idx == drv->bat_sampling_buf_tail_idx) {
|
||||
// Buffer is full, move tail index forward
|
||||
bat_sampling_buf_tail_idx++;
|
||||
if (bat_sampling_buf_tail_idx >= POWER_MANAGER_BAT_DATA_BUF_SIZE) {
|
||||
bat_sampling_buf_tail_idx = 0;
|
||||
drv->bat_sampling_buf_tail_idx++;
|
||||
if (drv->bat_sampling_buf_tail_idx >= POWER_MANAGER_BATTERY_SAMPLING_BUF_SIZE) {
|
||||
drv->bat_sampling_buf_tail_idx = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@ -194,24 +197,26 @@ void pm_battery_initial_soc_guess(void){
|
||||
power_manager_driver_t* drv = &g_power_manager;
|
||||
|
||||
// Check if the buffer is full
|
||||
if (bat_sampling_buf_head_idx == bat_sampling_buf_tail_idx) {
|
||||
if (drv->bat_sampling_buf_head_idx == drv->bat_sampling_buf_tail_idx) {
|
||||
// Buffer is empty, no data to process
|
||||
return;
|
||||
}
|
||||
|
||||
// Calculate average voltage, current and temperature from the sampling
|
||||
// buffer and run the fuel gauge initial guess
|
||||
uint8_t buf_idx = bat_sampling_buf_tail_idx;
|
||||
uint8_t buf_idx = drv->bat_sampling_buf_tail_idx;
|
||||
uint8_t samples_count = 0;
|
||||
float vbat_g, ibat_g, ntc_temp_g = 0.0f;
|
||||
while(bat_sampling_buf_head_idx != buf_idx) {
|
||||
float vbat_g = 0.0f;
|
||||
float ibat_g = 0.0f;
|
||||
float ntc_temp_g = 0.0f;
|
||||
while(drv->bat_sampling_buf_head_idx != buf_idx) {
|
||||
|
||||
vbat_g += drv->bat_sampling_buf[buf_idx].vbat;
|
||||
ibat_g += drv->bat_sampling_buf[buf_idx].ibat;
|
||||
ntc_temp_g += drv->bat_sampling_buf[buf_idx].ntc_temp;
|
||||
|
||||
buf_idx++;
|
||||
if (buf_idx >= POWER_MANAGER_BAT_DATA_BUF_SIZE) {
|
||||
if (buf_idx >= POWER_MANAGER_BATTERY_SAMPLING_BUF_SIZE) {
|
||||
buf_idx = 0;
|
||||
}
|
||||
|
||||
|
@ -208,6 +208,17 @@ def configure(
|
||||
paths += ["embed/util/hw_revision/inc"]
|
||||
sources += ["embed/util/hw_revision/stm32/hw_revision.c"]
|
||||
|
||||
if "power_manager" in features_wanted:
|
||||
sources += [
|
||||
"embed/sys/power_manager/stm32u5/power_manager.c",
|
||||
"embed/sys/power_manager/stm32u5/power_monitoring.c",
|
||||
"embed/sys/power_manager/stm32u5/power_states.c",
|
||||
]
|
||||
paths += ["embed/sys/power_manager/stm32u5/inc/"]
|
||||
paths += ["embed/sys/power_manager/inc"]
|
||||
defines += [("USE_POWER_MANAGER", "1")]
|
||||
features_available.append("power_manager")
|
||||
|
||||
defines += [
|
||||
"FRAMEBUFFER",
|
||||
"DISPLAY_RGBA8888",
|
||||
|
Loading…
Reference in New Issue
Block a user