STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
battery.h
Go to the documentation of this file.
1#ifndef STAR_BATTERY_H
2#define STAR_BATTERY_H
3
4/*
5 * Battery adapter: PA0 counts -> millivolts -> health. Portable.
6 *
7 * Schematic rev 1:
8 * p.12 TPS2121 (U17) picks the supply: IN1 = VBAT, IN2 = USB_5V; the
9 * battery wins while above ~3.5 V (PR1 divider R66/R69). Its output
10 * is VDD. ST (SW_ST, PE10) is pulled up by R65: HIGH = battery,
11 * LOW = USB (TPS2121 datasheet p.21).
12 * p.2 VDD -> R1 22k / R3 10k (x 0.3125) -> TLV9004 follower -> PA0.
13 * VREF+ = VDDA = 3V3 through FB1.
14 * p.12 3V3 = TPS562201, 0.768 V x (1 + 33.2k/10k) = 3.318 V, ~ +/-2 %.
15 * So mV = counts x (3320 / 0.3125) / 65535 = counts x 10624 / 65535, the
16 * same conversion as Aaron's idle module (PR #162).
17 *
18 * On USB the reading is the USB supply, never battery health (#137).
19 * Pack: 2S Li-ion, 7.4 V nominal / 8.4 V full (DEVICE_CHECKS.md).
20 */
21
22#include <stdbool.h>
23#include <stdint.h>
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
29#define BATTERY_MV_AT_FULL_SCALE 10624U /* 3320 mV / 0.3125 */
30#define BATTERY_ADC_FULL_SCALE 65535U /* ADC1, 16-bit */
31/* Proposals, not approved (#137, PR #162): the same numbers as idle. */
32#define BATTERY_WARN_MV 7000U
33#define BATTERY_FAIL_MV 6600U
34/* Above a full 2S pack plus the +2 % reference error: wrong pack or a
35 * broken divider, so the number cannot be trusted. */
36#define BATTERY_MAX_MV 8800U
37
38typedef enum {
39 BATTERY_UNKNOWN = 0, /* not read yet */
41 BATTERY_WARN, /* below BATTERY_WARN_MV: charge before flight */
42 BATTERY_FAIL, /* below BATTERY_FAIL_MV, or above BATTERY_MAX_MV */
43 BATTERY_USB, /* on USB: the reading is not the pack */
44 BATTERY_INVALID /* ADC read failed */
46
47typedef struct {
49 uint16_t mv; /* supply in mV (USB's when on USB); 0 if not read */
50 uint16_t counts;
51} battery_t;
52
53/* Rounded to the nearest mV; 0 above full scale. */
54uint16_t battery_counts_to_mv(uint32_t counts);
55
56/* One reading -> health. valid: the ADC returned a sample. */
57battery_t battery_assess(bool valid, uint32_t counts, bool on_usb);
58
60
61#ifdef __cplusplus
62}
63#endif
64#endif
battery_state_t
Definition battery.h:38
@ BATTERY_USB
Definition battery.h:43
@ BATTERY_WARN
Definition battery.h:41
@ BATTERY_OK
Definition battery.h:40
@ BATTERY_INVALID
Definition battery.h:44
@ BATTERY_UNKNOWN
Definition battery.h:39
@ BATTERY_FAIL
Definition battery.h:42
uint16_t battery_counts_to_mv(uint32_t counts)
Definition battery.c:3
const char * battery_state_name(battery_state_t state)
Definition battery.c:31
battery_t battery_assess(bool valid, uint32_t counts, bool on_usb)
Definition battery.c:12
static ActuatorControllerState state
Definition main.cpp:57
static bool on_usb(void *ctx)
Definition idle_task.c:37
battery_state_t state
Definition battery.h:48
uint16_t counts
Definition battery.h:50
uint16_t mv
Definition battery.h:49