34#ifndef __AFE_CALIBRATION_H__
35#define __AFE_CALIBRATION_H__
41#define ADI_PQLIB_MAX_RMS_VALUE_CAL 52702092
42#define CALIBRATION_CYCLES_50HZ 150
43#define CALIBRATION_CYCLES_60HZ 180
44#define SQRT_2 1.41421356f
45#define GAIN_SCALE_FACTOR (1LL << 27)
46#define OFFSET_SCALE_FACTOR (1LL << 15)
49#define OFFSET_REG_MAX ((1L << 23) - 1)
50#define OFFSET_REG_MIN (-(1L << 23))
CALIBRATION_CONTEXT calibrationCtx
Definition afe_calibration.c:44
bool calibration_is_active(void)
Check if calibration is active.
Definition afe_calibration.c:721
uint32_t calculate_expected_vrms(const CALIBRATION_INPUT *input, float rms_value)
Calculate expected RMS value for voltage.
Definition afe_calibration.c:142
CALIBRATION_STATE
Calibration state machine states.
Definition afe_calibration.h:55
@ CALIBRATION_STATE_COLLECTING
Definition afe_calibration.h:58
@ CALIBRATION_STATE_VERIFY_CALCULATING
Definition afe_calibration.h:63
@ CALIBRATION_STATE_INIT
Definition afe_calibration.h:57
@ CALIBRATION_STATE_WRITING
Definition afe_calibration.h:61
@ CALIBRATION_STATE_DONE
Definition afe_calibration.h:64
@ CALIBRATION_STATE_ERROR
Definition afe_calibration.h:65
@ CALIBRATION_STATE_VERIFY_COLLECTING
Definition afe_calibration.h:62
@ CALIBRATION_STATE_CALCULATING
Definition afe_calibration.h:59
@ CALIBRATION_STATE_IDLE
Definition afe_calibration.h:56
@ CALIBRATION_STATE_CALCULATE_ERROR_BEFORE
Definition afe_calibration.h:60
void calibration_clear_done(void)
Clear calibration done flag.
Definition afe_calibration.c:733
bool calibration_is_done(void)
Check if calibration just completed.
Definition afe_calibration.c:728
float calculate_error(int32_t measured, int32_t expected)
Calculate error percentage.
Definition afe_calibration.c:216
int32_t calculate_rmsos(int32_t expected, int32_t measured)
Calculate RMS offset coefficient.
Definition afe_calibration.c:180
const char * calibration_get_status_string(void)
Get current calibration status string.
Definition afe_calibration.c:738
int32_t calculate_rms_without_rmsos(int32_t measured, int32_t xrmsos)
Calculate RMS value without offset coefficient.
Definition afe_calibration.c:197
int calibration_start(CALIBRATION_TYPE type, CALIBRATION_PHASE phase)
Start calibration process.
Definition afe_calibration.c:224
int32_t calculate_rms_without_gain(int32_t measured, int32_t gain)
Calculate RMS without gain coefficient.
Definition afe_calibration.c:166
CALIBRATION_PHASE
Phase selection for calibration.
Definition afe_calibration.h:79
@ CALIBRATION_PHASE_C
Definition afe_calibration.h:82
@ CALIBRATION_PHASE_A
Definition afe_calibration.h:80
@ CALIBRATION_PHASE_B
Definition afe_calibration.h:81
uint32_t calculate_expected_irms(const CALIBRATION_INPUT *input, float rms_value)
Calculate expected RMS value for current.
Definition afe_calibration.c:131
int calibration_process_cycle(void)
Process one calibration cycle (called from main loop)
Definition afe_calibration.c:466
CALIBRATION_TYPE
Calibration type (gain or offset)
Definition afe_calibration.h:71
@ CALIBRATION_TYPE_OFFSET
Definition afe_calibration.h:73
@ CALIBRATION_TYPE_GAIN
Definition afe_calibration.h:72
void calibration_init(void)
Initialize calibration context with default values.
Definition afe_calibration.c:116
int32_t calculate_gain(int32_t expected, int32_t measured)
Calculate gain coefficient.
Definition afe_calibration.c:153
Calibration state structure (runtime state)
Definition afe_calibration.h:131
uint32_t irms_expected
Definition afe_calibration.h:147
uint32_t vrms_measured_no_off
Definition afe_calibration.h:154
CALIBRATION_INPUT input
Definition afe_calibration.h:159
uint32_t vrms_expected
Definition afe_calibration.h:148
uint32_t vrms_measured
Definition afe_calibration.h:152
int64_t vrms_sum
Definition afe_calibration.h:140
uint16_t cycle_count
Definition afe_calibration.h:135
uint32_t irms_measured
Definition afe_calibration.h:149
uint32_t vfrms_measured_no_off
Definition afe_calibration.h:158
int64_t irms_sum_no_off
Definition afe_calibration.h:139
int64_t irms_sum
Definition afe_calibration.h:137
int64_t ifrms_sum_no_off
Definition afe_calibration.h:144
uint16_t target_cycles
Definition afe_calibration.h:136
int64_t ifrms_sum
Definition afe_calibration.h:143
CALIBRATION_TYPE type
Definition afe_calibration.h:133
CALIBRATION_STATE state
Definition afe_calibration.h:132
int64_t vrms_sum_no_gain
Definition afe_calibration.h:141
uint32_t irms_measured_no_gain
Definition afe_calibration.h:150
uint32_t ifrms_measured
Definition afe_calibration.h:155
uint32_t ifrms_measured_no_off
Definition afe_calibration.h:156
uint32_t irms_measured_no_off
Definition afe_calibration.h:151
uint32_t vrms_measured_no_gain
Definition afe_calibration.h:153
CALIBRATION_PHASE phase
Definition afe_calibration.h:134
int64_t vfrms_sum_no_off
Definition afe_calibration.h:146
uint32_t vfrms_measured
Definition afe_calibration.h:157
int64_t irms_sum_no_gain
Definition afe_calibration.h:138
int64_t vrms_sum_no_off
Definition afe_calibration.h:142
int64_t vfrms_sum
Definition afe_calibration.h:145
CALIBRATION_RESULT result
Definition afe_calibration.h:160
Calibration input parameters (user-provided values)
Definition afe_calibration.h:88
int32_t i_gain_init
Definition afe_calibration.h:97
float voltage_pga_gain
Definition afe_calibration.h:94
int32_t i_rmsos_init
Definition afe_calibration.h:99
float nominal_voltage
Definition afe_calibration.h:90
float nominal_current
Definition afe_calibration.h:89
float current_pga_gain
Definition afe_calibration.h:93
float voltage_scale
Definition afe_calibration.h:96
int32_t vf_rmsos_init
Definition afe_calibration.h:102
float offset_voltage
Definition afe_calibration.h:92
int32_t v_gain_init
Definition afe_calibration.h:98
int32_t if_rmsos_init
Definition afe_calibration.h:101
float offset_current
Definition afe_calibration.h:91
int32_t v_rmsos_init
Definition afe_calibration.h:100
float current_scale
Definition afe_calibration.h:95
Calibration results (computed values)
Definition afe_calibration.h:108
int32_t v_rmsos
Definition afe_calibration.h:112
int32_t if_rmsos
Definition afe_calibration.h:113
float offset_i_error_after
Definition afe_calibration.h:123
float gain_i_error_before
Definition afe_calibration.h:116
float offset_v_error_before
Definition afe_calibration.h:122
float gain_i_error_after
Definition afe_calibration.h:118
float gain_v_error_after
Definition afe_calibration.h:119
int32_t v_gain
Definition afe_calibration.h:110
float offset_i_error_before
Definition afe_calibration.h:121
float gain_v_error_before
Definition afe_calibration.h:117
bool done
Definition afe_calibration.h:125
int32_t i_gain
Definition afe_calibration.h:109
int32_t vf_rmsos
Definition afe_calibration.h:114
int32_t i_rmsos
Definition afe_calibration.h:111
float offset_v_error_after
Definition afe_calibration.h:124