BSB Developer Docs
Loading...
Searching...
No Matches
power_i.h
1#pragma once
2
3#include "power.h"
4#include "settings/settings_i.h"
5#include <furi_hal.h>
6#include <toolbox/api_lock.h>
7
8// =========================================
9// Battery state of charge (power_battery.c)
10// =========================================
11
12typedef struct {
13 uint8_t* curves;
14 uint16_t percent_points;
15 uint16_t current_points;
16 uint16_t current_range;
17 uint16_t tolerance;
18 bool is_in_flash;
20
21PowerBatCalibration* power_get_crude_calibration(void);
22
23PowerBatCalibration* power_load_bat_calibration(const char* path);
24
25void power_unload_bat_calibration(PowerBatCalibration* cal);
26
27uint8_t power_get_battery_charge(
28 const PowerBatCalibration* cal,
29 int32_t voltage_mv,
30 int32_t current_ma);
31
32// ===================================
33// USB Power Delivery (power_usb_pd.c)
34// ===================================
35
36#define PDO_NUMBER_MAX 7
37
38typedef struct {
39 uint32_t passive_mode_current;
40 size_t cap_number;
41 uint8_t cc_line;
42 uint8_t cap_id_current;
43 struct {
44 uint32_t voltage_min;
45 uint32_t voltage_max;
46 uint32_t current_max;
47 uint8_t pdo_id;
48 bool is_fixed;
49 } cap[PDO_NUMBER_MAX];
51
52typedef struct PowerUsbPd PowerUsbPd;
53
54PowerUsbPd* power_usb_pd_alloc(FuriMessageQueue** pd_queue);
55
56void power_usb_pd_msg_handler(FuriEventLoopObject* object, void* context);
57
58void power_usb_pd_start(PowerUsbPd* pd);
59
60void power_usb_pd_get_capabilities(PowerUsbPd* pd, PowerUsbPdCapability* caps);
61
62void power_usb_pd_request_power(PowerUsbPd* pd, uint32_t voltage_mv, uint32_t current_ma);
63
64// ==========
65// Public API
66// ==========
67
68typedef enum {
69 PowerMessageTypeOff,
70 PowerMessageTypeReboot,
71 PowerMessageTypeGetInfo,
72 PowerMessageTypeIsUsbConnected,
73 PowerMessageTypeIsBatteryReady,
74 PowerMessageTypeChargeEnable,
75 PowerMessageTypeSetChargeLimit,
76 PowerMessageTypeSetChargeCurrent,
77 PowerMessageTypePdGetInfo,
78 PowerMessageTypePdRequest,
79 PowerMessageTypeLoadBatCal,
80
81 // TODO: separate queue for internal messages?
82 PowerMessageTypeUsbPdUpdate,
83} PowerMessageType;
84
85typedef struct {
86 PowerMessageType type;
87 FuriApiLock lock;
88 union {
89 PowerRebootMode reboot_mode;
90 struct {
91 uint32_t voltage;
92 uint32_t current;
93 } pd_mode;
94 PowerInfo* power_info;
95 PowerPdInfo* pd_info;
96 bool* param_bool;
97 int32_t* param_int;
98 char* param_str_owned;
99 };
101
102// ==============
103// Service struct
104// ==============
105
106typedef enum {
107 PowerBatteryStateNormal,
108 PowerBatteryStateLow,
109 PowerBatteryStateCritical,
110} PowerBatteryState;
111
112struct Power {
113 FuriEventLoop* event_loop;
114 FuriMessageQueue* message_queue;
115 FuriSemaphore* gpio_semaphore;
116 PowerUsbPd* usb_pd;
117 FuriPubSub* event_pubsub;
118 struct {
119 bool charger_alive;
120 bool battery_ready;
121 bool usb_connected;
122 bool pd_initialized;
123 } state;
124 PowerInfo info;
125 PowerPdInfo pd_info;
126 uint32_t input_current_limit;
127 uint32_t charger_current_limit;
128 bool charger_enabled;
129 PowerBatteryState battery_state;
130 PowerBatCalibration* bat_cal;
131 bool tried_to_load_storage_cal;
132
133 float charge_last;
134 bool settings_first_loaded;
135 PowerSettings settings;
136
137#ifndef FURI_RAM_EXEC
138 bool shipping_mode_wait;
139#endif
140};
141
142#if defined(SRV_STORAGE) && !defined(FURI_RAM_EXEC)
143#define POWER_USE_SETTINGS
144#endif
Definition power_i.h:12
Definition power_i.h:112
Definition power.h:53
Definition power_i.h:85
Definition power.h:78
Definition services/power/power_service/settings/interface_v1.h:9
Definition power_i.h:38
Definition power_usb_pd.c:251