Midnight Sun Firmware
Loading...
Searching...
No Matches
max17261_fuel_gauge.h
1#pragma once
2
3/************************************************************************************************
4 * @file max17261_fuel_gauge.h
5 *
6 * @brief Max17261 Fuel Gauge
7 *
8 * @date 2025-04-29
9 * @author Midnight Sun Team #24 - MSXVI
10 ************************************************************************************************/
11
12/* Standard library Headers */
13#include <stdint.h>
14
15/* Inter-component Headers */
16#include "i2c.h"
17#include "max17261_fuel_gauge_defs.h"
18
19/* Intra-component Headers */
20
27/* Notes:
28 * 3 things are required for configuring the max17261:
29 * - Design Capacity (in milliamp hours)
30 * - Empty Voltage
31 * - Charge Termination Current
32 *
33 * Some of the units of the registers (like capacity and current) depend on RSense,
34 * it looks like max17261 automatically determines this value, it should be provided
35 * to the driver for conversions from register values to actual units
36 *
37 * See data sheet P.15 for more info on how registers are formatted
38 * Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/max17261.pdf
39 */
40
41typedef struct {
42 I2CPort i2c_port;
43 I2CAddress i2c_address;
44
45 uint32_t pack_design_cap_mah;
46 uint16_t cell_empty_voltage_v;
47 uint16_t charge_term_current_ma; // ref end-of-charge detection
48 // https://web.archive.org/web/20220121025712mp_/https://pdfserv.maximintegrated.com/en/an/user-guide-6597-max1726x-m5-ez-rev3-p4.pdf
49
50 uint16_t i_thresh_max_a;
51 int16_t i_thresh_min_a;
52 uint16_t temp_thresh_max_c;
53
54 float sense_resistor_mohms;
56
57typedef struct {
58 Max17261Settings *settings;
60
61// Storage for parameters learned by fuel guage
62// Must be stored in flash to keep up to date after power cycle
63typedef struct Max27261Params {
64 uint16_t rcomp0;
65 uint16_t tempco;
66 uint16_t fullcaprep;
67 uint16_t cycles;
68 uint16_t fullcapnom;
70
71/* @brief Gets the current state of charge given by the max17261 in percentage
72 * @param storage - a pointer to an already initialized Max17261Storage struct
73 * @param soc_pct - state of charge in percentage will be returned in this var
74 * @return STATUS_CODE_OK on success
75 */
76StatusCode max17261_state_of_charge(Max17261Storage *storage, uint16_t *soc_pct);
77
78/* @brief Gets the current remaining capactity in micro amp hours
79 * @param storage - a pointer to an already initialized Max17261Storage struct
80 * @param soc_pct - remaining capactity in micro amp hours returned in this var
81 * @return STATUS_CODE_OK on success
82 */
83StatusCode max17261_remaining_capacity(Max17261Storage *storage, uint32_t *rem_cap_mAh);
84
85/* @brief Gets the full charge capacity of the battery in micro amp hours
86 * @param storage - a pointer to an already initialized Max17261Storage struct
87 * @param soc_pct - full charge capacitry in micro amp hours returned in this var
88 * @return STATUS_CODE_OK on success
89 */
90StatusCode max17261_full_capacity(Max17261Storage *storage, uint32_t *full_cap_mAh);
91
92/* @brief Gets the time to empty in milliseconds
93 * @param storage - a pointer to an already initialized Max17261Storage struct
94 * @param soc_pct - time to empty in milliseconds returned in this var
95 * @return STATUS_CODE_OK on success
96 */
97StatusCode max17261_time_to_empty(Max17261Storage *storage, uint16_t *tte_ms);
98
99/* @brief Gets the time to full in milliseconds
100 * @param storage - a pointer to an already initialized Max17261Storage struct
101 * @param soc_pct - time to full in milliseconds returned in this var
102 * @return STATUS_CODE_OK on success
103 */
104StatusCode max17261_time_to_full(Max17261Storage *storage, uint16_t *ttf_ms);
105
106/* @brief Gets a current reading in amps
107 * @param storage - a pointer to an already initialized Max17261Storage struct
108 * @param soc_pct - current in amps returned in this var
109 * @return STATUS_CODE_OK on success
110 */
111StatusCode max17261_current(Max17261Storage *storage, int32_t *current_a);
112
113/* @brief Gets a single cell's voltage in mV
114 * @param storage - a pointer to an already initialized Max17261Storage struct
115 * @param soc_pct - voltage in mV returned in this var
116 * @return STATUS_CODE_OK on success
117 */
118StatusCode max17261_voltage(Max17261Storage *storage, uint16_t *vcell_mv);
119
120/* @brief Gets a temperature reading in celcius
121 * @param storage - a pointer to an already initialized Max17261Storage struct
122 * @param soc_pct - temperature in celcius returned in this var
123 * @return STATUS_CODE_OK on success
124 */
125StatusCode max17261_temp(Max17261Storage *storage, uint16_t *temp_c);
126
127/* @brief Gets the time to full in milliseconds
128 * @param storage - a pointer to an uninitialized Max17261Storage struct
129 * @param settings - populated settings struct
130 * @return STATUS_CODE_OK on success
131 */
132StatusCode max17261_init(Max17261Storage *storage, Max17261Settings *settings, Max27261Params *params);
133
134StatusCode max17261_set_learned_params(Max17261Storage *storage, Max27261Params *params);
135StatusCode max17261_get_learned_params(Max17261Storage *storage, Max27261Params *params);
136
uint8_t I2CAddress
I2C address type.
Definition: i2c.h:34
I2CPort
I2C Port selection.
Definition: i2c.h:37
StatusCode
StatusCodes for various errors.
Definition: status.h:27
Definition: max17261_fuel_gauge.h:41
Definition: max17261_fuel_gauge.h:57
Definition: max17261_fuel_gauge.h:63