108 lines
2.9 KiB
C
108 lines
2.9 KiB
C
#ifndef BATT_MGMT_H
|
|
#define BATT_MGMT_H
|
|
|
|
#include <zephyr/types.h>
|
|
#include <stdbool.h>
|
|
|
|
#define BATT_MGMT_OVERSAMPLING_8X 8U
|
|
#define BATT_MGMT_OVERSAMPLING_16X 16U
|
|
|
|
typedef enum {
|
|
BATT_STATE_DISCHARGING = 0,
|
|
BATT_STATE_FULL,
|
|
BATT_STATE_CHARGING,
|
|
BATT_STATE_ERROR,
|
|
BATT_STATE_UNKNOWN,
|
|
} batt_mgmt_state_t;
|
|
|
|
typedef struct {
|
|
batt_mgmt_state_t state;
|
|
uint8_t level;
|
|
uint8_t percent;
|
|
int32_t voltage_mv;
|
|
} batt_mgmt_info_t;
|
|
|
|
/**
|
|
* @brief Measure battery VDDH voltage.
|
|
*
|
|
* @param oversampling Oversampling factor (BATT_MGMT_OVERSAMPLING_8X or BATT_MGMT_OVERSAMPLING_16X).
|
|
* @param vddh_mv Pointer to store the result in millivolts.
|
|
* @return 0 on success, negative errno on failure.
|
|
*/
|
|
int batt_mgmt_measure_vddh_mv(uint8_t oversampling, int32_t *vddh_mv);
|
|
|
|
/**
|
|
* @brief Get display voltage in millivolts.
|
|
*
|
|
* Returns the voltage value intended for UI/protocol display. Currently this is
|
|
* the latest measured VDDH value cached by batt_mgmt.
|
|
*
|
|
* @param vddh_mv Pointer receiving display voltage in mV.
|
|
* @return 0 on success, negative errno on failure.
|
|
*/
|
|
int batt_mgmt_get_display_voltage_mv(int32_t *vddh_mv);
|
|
|
|
/**
|
|
* @brief Get battery level bucket (0..4) from configured thresholds.
|
|
*
|
|
* Level mapping:
|
|
* - 0: below 20% threshold
|
|
* - 1: 20%..49%
|
|
* - 2: 50%..79%
|
|
* - 3: 80%..99%
|
|
* - 4: full and above
|
|
*
|
|
* @param level Pointer receiving level in range 0..4.
|
|
* @return 0 on success, negative errno on failure.
|
|
*/
|
|
int batt_mgmt_get_battery_level(uint8_t *level);
|
|
|
|
/**
|
|
* @brief Get battery percentage (0-100) from configured voltage thresholds.
|
|
*
|
|
* Uses piecewise linear interpolation over:
|
|
* - EMPTY..20%
|
|
* - 20%..50%
|
|
* - 50%..80%
|
|
* - 80%..FULL
|
|
*
|
|
* @param percent Pointer receiving percentage in range 0..100.
|
|
* @return 0 on success, negative errno on failure.
|
|
*/
|
|
int batt_mgmt_get_battery_percent(uint8_t *percent);
|
|
|
|
/**
|
|
* @brief Get raw charger status (GPIO level).
|
|
*
|
|
* Returns the current logic level of the charger status pin (typically from ETA6003).
|
|
* This is the **raw GPIO status**, not a processed state machine result.
|
|
*
|
|
* @return true if charger pin is high (e.g., battery is "charging"), false if low.
|
|
*/
|
|
bool batt_mgmt_get_charger_status(void);
|
|
|
|
/**
|
|
* @brief Get processed battery state with blinking detection.
|
|
*
|
|
* Returns a derived state based on:
|
|
* - USB VBUS presence (from usb_mgmt)
|
|
* - Charger status GPIO level (from batt_mgmt_get_charger_status)
|
|
* - Blinking detection (state changes within CONFIG_BATT_MGMT_CHG_BLINKING_WINDOW_MS)
|
|
*
|
|
* Note: This differs from batt_mgmt_get_charger_status() which returns the **raw** GPIO level.
|
|
*
|
|
* @return Battery state enum (DISCHARGING, FULL, CHARGING, or ERROR if blinking detected).
|
|
*/
|
|
batt_mgmt_state_t batt_mgmt_get_battery_state(void);
|
|
|
|
/**
|
|
* @brief Get all battery information in one call.
|
|
*
|
|
* @param info Pointer receiving state, level (0..4), percent (0..100), and voltage in mV.
|
|
* @return 0 on success, negative errno on failure.
|
|
*/
|
|
int batt_mgmt_get_info(batt_mgmt_info_t *info);
|
|
|
|
#endif
|
|
|