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 "ltc_afe_regs.h"
33#include "status.h"
34
41/* Removes padding, if possible */
42#if defined(__GNUC__)
43#define _PACKED __attribute__((packed))
44#else
45#define _PACKED
46#endif
47
52#define LTC_AFE_MAX_DEVICES 3
53
58#define LTC_AFE_MAX_CELLS_PER_DEVICE 12
59#define LTC_AFE_MAX_THERMISTORS_PER_DEVICE 8
60
64#define LTC_AFE_MAX_CELLS (LTC_AFE_MAX_DEVICES * LTC_AFE_MAX_CELLS_PER_DEVICE)
65#define LTC_AFE_MAX_THERMISTORS (LTC_AFE_MAX_DEVICES * LTC_AFE_MAX_THERMISTORS_PER_DEVICE)
66
72typedef enum {
73 LTC_AFE_ADC_MODE_422KHZ = 0,
77 LTC_AFE_ADC_MODE_1KHZ,
78 LTC_AFE_ADC_MODE_14KHZ,
79 LTC_AFE_ADC_MODE_3KHZ,
80 LTC_AFE_ADC_MODE_2KHZ,
81 NUM_LTC_AFE_ADC_MODES
83
90typedef struct LtcAfeSettings {
96 size_t num_devices;
97 size_t num_cells;
103
112typedef struct LtcAfeStorage {
114 uint16_t aux_voltages[LTC_AFE_MAX_THERMISTORS];
117 uint16_t aux_result_lookup[LTC_AFE_MAX_THERMISTORS];
121 LtcAfeWriteConfigPacket *device_configs;
123
136
148
160
172StatusCode ltc_afe_trigger_aux_conv(LtcAfeStorage *afe, uint8_t device_cell);
173
185
199StatusCode ltc_afe_read_aux(LtcAfeStorage *afe, uint8_t device_cell);
200
212StatusCode ltc_afe_toggle_cell_discharge(LtcAfeStorage *afe, uint16_t cell, bool discharge);
213
226
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:64
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:72
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:52
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:75
@ LTC_AFE_ADC_MODE_26HZ
Definition: ltc_afe.h:76
@ LTC_AFE_ADC_MODE_27KHZ
Definition: ltc_afe.h:74
SpiPort
SPI Port selection.
Definition: spi.h:34
StatusCode
StatusCodes for various errors.
Definition: status.h:27
Afe Settings Data.
Definition: ltc_afe.h:90
SpiSettings spi_settings
Definition: ltc_afe.h:100
const SpiPort spi_port
Definition: ltc_afe.h:101
LtcAfeAdcMode adc_mode
Definition: ltc_afe.h:91
size_t num_thermistors
Definition: ltc_afe.h:98
uint16_t aux_bitset[LTC_AFE_MAX_DEVICES]
Definition: ltc_afe.h:94
size_t num_devices
Definition: ltc_afe.h:96
uint16_t cell_bitset[LTC_AFE_MAX_DEVICES]
Definition: ltc_afe.h:93
size_t num_cells
Definition: ltc_afe.h:97
Runtime Data Storage.
Definition: ltc_afe.h:112
uint16_t aux_result_lookup[LTC_AFE_MAX_THERMISTORS]
Definition: ltc_afe.h:117
uint16_t cell_voltages[LTC_AFE_MAX_CELLS]
Definition: ltc_afe.h:113
LtcAfeWriteConfigPacket * device_configs
Definition: ltc_afe.h:121
uint16_t discharge_cell_lookup[LTC_AFE_MAX_CELLS]
Definition: ltc_afe.h:118
LtcAfeSettings * settings
Definition: ltc_afe.h:120
uint16_t aux_voltages[LTC_AFE_MAX_THERMISTORS]
Definition: ltc_afe.h:114
uint16_t cell_result_lookup[LTC_AFE_MAX_CELLS]
Definition: ltc_afe.h:116
SPI settings struct.
Definition: spi.h:76