Protocol rework

This commit is contained in:
ThePetrovich 2026-05-10 15:33:08 +08:00
parent 3e77d34ccc
commit 2b713a3e3a
15 changed files with 1347 additions and 1155 deletions

View file

@ -1,6 +1,7 @@
/*
* @file config.h
* @brief System configuration and constants
* @brief System configuration, persisted-config struct definition, and
* compile-time constants shared between modules.
*
* Created: 21.09.2025
* Author: ThePetrovich
@ -14,86 +15,122 @@
#ifndef CONFIG_H
#define CONFIG_H
// Firmware version
#define FIRMWARE_VERSION_MAJOR 2
#define FIRMWARE_VERSION_MINOR 03
#include <stdint.h>
// Serial communication
/* Firmware version */
#define FIRMWARE_VERSION_MAJOR 3
#define FIRMWARE_VERSION_MINOR 00
/* Serial communication */
#define SERIAL_BAUD_RATE 38400
// Timing constants
/* Timing constants */
#define STATUS_LED_BLINK_PERIOD 500U
#define MAIN_LOOP_DELAY 10
#define CPM_UPDATE_PERIOD 10000U
#define RSENSE_UPDATE_PERIOD 1000U
#define RSENSE_CHANNEL_16_MASK 0x7FFU // 11-bit channel mask
#define RSENSE_CHANNEL_32_MASK 0x3FFU // 10-bit channel mask
#define RSENSE_CHANNEL_COUNT 2048
#define RSENSE_CHANNEL_32_COUNT 1024
/* Spectrum channel masks and counts */
#define RSENSE_CHANNEL_MASK 0x3FFU /* 10-bit channel mask */
#define RSENSE_CHANNEL_COUNT 1024
// Temperature sensor constants (MCP9700)
#define MCP9700_OFFSET_MV 500L // 500 mV at 0°C
#define MCP9700_SCALE_MV 10L // 10 mV per degree
#define ADC_OVERSAMPLING_BITS 2 // 16x oversampling -> 2 extra bits
#define ADC_VREF_MV 3000L // 3.0V reference
#define ADC_RESOLUTION 1024L // 10-bit ADC
/* 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
/* Voltage divider constants */
#define VOLTAGE_DIVIDER_RATIO 20.0f /* 200k and 10k resistors */
#define VOLTAGE_MV_PER_STEP 14.6484375f /* (3.0 * 20) / 4096 */
// EEPROM addresses for potentiometer settings
#define EEPROM_POT_HV_ADDR 0
#define EEPROM_POT_AMP_ADDR 1
#define EEPROM_POT_DET_ADDR 2
#define EEPROM_MAGIC_ADDR 3
#define EEPROM_MAGIC_VALUE 0xA5 // Magic value to check if EEPROM is initialized
// Default potentiometer values
/* Default potentiometer wiper value (mid-scale, 127/255) */
#define DEFAULT_POT_VALUE 127
// Light sensor constants
/* 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 to indicate valid config
uint8_t magic2; // 0x5A to indicate valid config
uint8_t pot_hv;
uint8_t pot_amp;
uint8_t pot_det;
struct {
uint16_t adccal0; // ADC offset at GND
uint16_t adccal3; // ADC reading at 3.0V reference
uint16_t tempcal;
uint16_t tempdrift_mvK; // SiPM temperature drift compensation in mV/K, normally around 20 mV/K
float gainlow; // gain value for pot_amp = 0
float gainhigh; // gain value for pot_amp = 255
} calibration __attribute__((packed));
union {
struct {
uint8_t spectrum_oversample_bits
: 2; // 0, 1, 2, or 3 for 1x, 4x, 16x, or 64x oversampling, only for spectrum measurement
uint8_t spectrum_channel_bits : 1; // 0 for 16-bit channels, 1 for 32-bit channels
uint8_t temperature_drift_compensation
: 1; // 0 to disable, 1 to enable temperature compensation
uint8_t adc_oversample_bits : 2; // 0, 1, 2, or 3 for 1x, 4x, 16x, or 64x ADC oversampling for
// temperature and voltage measurements
uint8_t reserved : 2;
} fields __attribute__((packed));
uint8_t value;
} flags __attribute__((packed));
struct {
uint8_t tempdrift_pot_hv_low;
uint8_t tempdrift_pot_hv_high;
uint8_t tempdrift_pot_amp_low;
uint8_t tempdrift_pot_amp_high;
uint8_t tempdrift_pot_det_low;
uint8_t tempdrift_pot_det_high;
} temp_drift_compensation_ranges __attribute__((packed));
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
#endif /* CONFIG_H */