Midnight Sun Firmware
Loading...
Searching...
No Matches
ltc_afe.h
1#pragma once
2
3/************************************************************************************************
4 * @file ltc_afe.h
5 *
6 * @brief Header file for the LTC8611 AFE driver
7 *
8 * @date 2025-04-13
9 * @author Midnight Sun Team #24 - MSXVI
10 ************************************************************************************************/
11
21/* Standard library Headers */
22#include <assert.h>
23#include <stdbool.h>
24#include <stdint.h>
25#include <stdlib.h>
26
27/* Inter-component Headers */
28#include "gpio.h"
29#include "spi.h"
30
31/* Intra-component Headers */
32#include "fsm.h"
33#include "ltc_afe_regs.h"
34#include "status.h"
35
42/* Removes padding, if possible */
43#if defined(__GNUC__)
44#define _PACKED __attribute__((packed))
45#else
46#define _PACKED
47#endif
48
53#define LTC_AFE_MAX_DEVICES 3
54
59#define LTC_AFE_MAX_CELLS_PER_DEVICE 12
60#define LTC_AFE_MAX_THERMISTORS_PER_DEVICE 8
61
65#define LTC_AFE_MAX_CELLS (LTC_AFE_MAX_DEVICES * LTC_AFE_MAX_CELLS_PER_DEVICE)
66#define LTC_AFE_MAX_THERMISTORS (LTC_AFE_MAX_DEVICES * LTC_AFE_MAX_THERMISTORS_PER_DEVICE)
67
73typedef enum {
74 LTC_AFE_ADC_MODE_422KHZ = 0,
78 LTC_AFE_ADC_MODE_1KHZ,
79 LTC_AFE_ADC_MODE_14KHZ,
80 LTC_AFE_ADC_MODE_3KHZ,
81 LTC_AFE_ADC_MODE_2KHZ,
82 NUM_LTC_AFE_ADC_MODES
84
91typedef struct LtcAfeSettings {
97 size_t num_devices;
98 size_t num_cells;
104
113typedef struct LtcAfeStorage {
115 uint16_t aux_voltages[LTC_AFE_MAX_THERMISTORS];
118 uint16_t aux_result_lookup[LTC_AFE_MAX_THERMISTORS];
122 LtcAfeWriteConfigPacket *device_configs;
124
137
149
161
173StatusCode ltc_afe_trigger_aux_conv(LtcAfeStorage *afe, uint8_t device_cell);
174
186
200StatusCode ltc_afe_read_aux(LtcAfeStorage *afe, uint8_t device_cell);
201
213StatusCode ltc_afe_toggle_cell_discharge(LtcAfeStorage *afe, uint16_t cell, bool discharge);
214
227
StatusCode ltc_afe_toggle_cell_discharge(LtcAfeStorage *afe, uint16_t cell, bool discharge)
Mark cell for discharging in each device.
Definition: ltc_afe.c:334
StatusCode ltc_afe_trigger_cell_conv(LtcAfeStorage *afe)
Triggers ADC conversion for all enabled cell voltage inputs.
Definition: ltc_afe.c:206
StatusCode ltc_afe_trigger_aux_conv(LtcAfeStorage *afe, uint8_t device_cell)
Triggers ADC conversion for an auxiliary (thermistor) input.
Definition: ltc_afe.c:223
StatusCode ltc_afe_set_discharge_pwm_cycle(LtcAfeStorage *afe, uint8_t duty_cycle)
Sets the same discharge PWM duty cycle for all cells across all AFE devices.
Definition: ltc_afe.c:357
StatusCode ltc_afe_init(LtcAfeStorage *afe, const LtcAfeSettings *config)
Initializes the LTC AFE system with provided configuration settings.
Definition: ltc_afe.c:139
#define LTC_AFE_MAX_CELLS
Maximum across all devices.
Definition: ltc_afe.h:65
StatusCode ltc_afe_read_aux(LtcAfeStorage *afe, uint8_t device_cell)
Reads and stores auxillary input from GPIO4 of each afe.
Definition: ltc_afe.c:289
LtcAfeAdcMode
Select the ADC mode.
Definition: ltc_afe.h:73
StatusCode ltc_afe_write_config(LtcAfeStorage *afe)
Writes configuration bits onto CFGR (Configuration Register Group)
Definition: ltc_afe.c:184
#define LTC_AFE_MAX_DEVICES
Maximum AFE devices in daisy chain config.
Definition: ltc_afe.h:53
StatusCode ltc_afe_read_cells(LtcAfeStorage *afe)
Reads and stores cell voltages from the voltage cell registers of each afe.
Definition: ltc_afe.c:242
@ LTC_AFE_ADC_MODE_7KHZ
Definition: ltc_afe.h:76
@ LTC_AFE_ADC_MODE_26HZ
Definition: ltc_afe.h:77
@ LTC_AFE_ADC_MODE_27KHZ
Definition: ltc_afe.h:75
SpiPort
SPI Port selection.
Definition: spi.h:34
StatusCode
StatusCodes for various errors.
Definition: status.h:27
Afe Settings Data.
Definition: ltc_afe.h:91
SpiSettings spi_settings
Definition: ltc_afe.h:101
const SpiPort spi_port
Definition: ltc_afe.h:102
LtcAfeAdcMode adc_mode
Definition: ltc_afe.h:92
size_t num_thermistors
Definition: ltc_afe.h:99
uint16_t aux_bitset[LTC_AFE_MAX_DEVICES]
Definition: ltc_afe.h:95
size_t num_devices
Definition: ltc_afe.h:97
uint16_t cell_bitset[LTC_AFE_MAX_DEVICES]
Definition: ltc_afe.h:94
size_t num_cells
Definition: ltc_afe.h:98
Runtime Data Storage.
Definition: ltc_afe.h:113
uint16_t aux_result_lookup[LTC_AFE_MAX_THERMISTORS]
Definition: ltc_afe.h:118
uint16_t cell_voltages[LTC_AFE_MAX_CELLS]
Definition: ltc_afe.h:114
LtcAfeWriteConfigPacket * device_configs
Definition: ltc_afe.h:122
uint16_t discharge_cell_lookup[LTC_AFE_MAX_CELLS]
Definition: ltc_afe.h:119
LtcAfeSettings * settings
Definition: ltc_afe.h:121
uint16_t aux_voltages[LTC_AFE_MAX_THERMISTORS]
Definition: ltc_afe.h:115
uint16_t cell_result_lookup[LTC_AFE_MAX_CELLS]
Definition: ltc_afe.h:117
SPI settings struct.
Definition: spi.h:76