edu16_sbc/sbc_fw/config.h
2026-05-10 15:33:08 +08:00

136 lines
4.2 KiB
C

/*
* @file config.h
* @brief System configuration, persisted-config struct definition, and
* compile-time constants shared between modules.
*
* Created: 21.09.2025
* Author: ThePetrovich
*
* Copyright YKSA - Sakha Aerospace Systems, LLC.
* See the LICENSE file for details.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#ifndef CONFIG_H
#define CONFIG_H
#include <stdint.h>
/* Firmware version */
#define FIRMWARE_VERSION_MAJOR 3
#define FIRMWARE_VERSION_MINOR 00
/* Serial communication */
#define SERIAL_BAUD_RATE 38400
/* Timing constants */
#define STATUS_LED_BLINK_PERIOD 500U
#define MAIN_LOOP_DELAY 10
#define RSENSE_UPDATE_PERIOD 1000U
/* Spectrum channel masks and counts */
#define RSENSE_CHANNEL_MASK 0x3FFU /* 10-bit channel mask */
#define RSENSE_CHANNEL_COUNT 1024
/* MCP9700 temperature sensor calibration constants */
#define MCP9700_OFFSET_MV 500L /* 500 mV at 0 °C */
#define MCP9700_SCALE_MV 10L /* 10 mV per °C */
#define ADC_OVERSAMPLING_BITS 2 /* 16x oversampling -> 2 extra bits */
#define ADC_VREF_MV 3000L /* 3.0 V reference */
#define ADC_RESOLUTION 1024L /* 10-bit ADC */
/* Voltage divider constants */
#define VOLTAGE_DIVIDER_RATIO 20.0f /* 200k and 10k resistors */
#define VOLTAGE_MV_PER_STEP 14.6484375f /* (3.0 * 20) / 4096 */
/* Default potentiometer wiper value (mid-scale, 127/255) */
#define DEFAULT_POT_VALUE 127
/* Light sensor I2C constants */
#define LSENSE_I2C_BASE_ADDR 0x38
#define LSENSE_EXPECTED_ID 0xE0
#define LSENSE_DATA_SIZE 12
/**
* @brief AD5160 wiper settings for the three digital potentiometers.
*/
typedef struct potentiometers_t {
uint8_t pot_hv; /**< HV regulator wiper. */
uint8_t pot_amp; /**< Amplifier gain wiper. */
uint8_t pot_det; /**< Detector threshold wiper. */
} __attribute__((packed)) potentiometers_t;
/**
* @brief ADC and temperature-sensor calibration coefficients.
* Reused as the payload of #CMD_SET_CALIBRATION.
*/
typedef struct calibration_t {
uint16_t adccal0; /**< ADC reading at GND (offset). */
uint16_t adccal3; /**< ADC reading at 3.0 V reference. */
uint16_t tempcal; /**< Temperature offset correction. */
} __attribute__((packed)) calibration_t;
/**
* @brief Runtime feature flags packed into a single byte.
* Reused as the payload of #CMD_SET_FLAGS.
*/
typedef union config_flags_t {
struct __attribute__((packed)) {
/** Spectrum oversampling: 0/1/2/3 = 1x/4x/16x/64x. */
uint8_t spectrum_oversample_bits : 2;
uint8_t reserved1 : 1;
/** 0 = disable, 1 = enable temperature drift compensation. */
uint8_t temperature_drift_compensation : 1;
/** ADC oversampling for temperature/voltage: 0/1/2/3 = 1x/4x/16x/64x. */
uint8_t adc_oversample_bits : 2;
uint8_t reserved2 : 2;
} fields;
uint8_t value;
} __attribute__((packed)) config_flags_t;
/**
* @brief Temperature drift compensation parameters.
* Reused as the payload of #CMD_SET_TEMP_DRIFT_COMPENSATION.
*
* Drift coefficients are in pot_units/255 per °C. When @c pot_X_low equals
* @c pot_X_high, drift compensation for that channel is disabled.
*
* Sensor → potentiometer mapping:
* - temp_drift : DET_TEMP (SiPM carrier) → pot_hv
* - vbias_drift : HV_TEMP (+28V supply) → pot_hv
* - gain_drift : AMP_TEMP (amplifier) → pot_amp
* - threshold_drift : AMP_TEMP (amplifier) → pot_det
*/
typedef struct temp_drift_compensation_t {
int16_t temp_drift;
int16_t vbias_drift;
int16_t gain_drift;
int16_t threshold_drift;
uint32_t drift_period_ms; /**< Minimum interval between adjustments, in ms. */
uint8_t pot_hv_low;
uint8_t pot_hv_high;
uint8_t pot_amp_low;
uint8_t pot_amp_high;
uint8_t pot_det_low;
uint8_t pot_det_high;
} __attribute__((packed)) temp_drift_compensation_t;
/**
* @brief Persistent device configuration.
*
* Stored in EEPROM with dual-partition wear leveling (see eeprom.cpp).
* The pair @c magic1 / @c magic2 (0xA5 / 0x5A) marks a partition as valid.
*/
typedef struct config_t {
uint8_t magic1; /**< 0xA5 — valid-partition marker, byte 1. */
uint8_t magic2; /**< 0x5A — valid-partition marker, byte 2. */
potentiometers_t pots;
calibration_t calibration;
config_flags_t flags;
temp_drift_compensation_t temp_drift_compensation;
} __attribute__((packed)) config_t;
extern config_t config;
#endif /* CONFIG_H */