no-OS
Loading...
Searching...
No Matches
afe_calibration.h
Go to the documentation of this file.
1/*******************************************************************************
2 * @file afe_calibration.h
3 * @brief ADE9430 calibration header file
4 * @author Radu Etz (radu.etz@analog.com)
5 ********************************************************************************
6 * Copyright (c) 2026 Analog Devices, Inc.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 *
18 * 3. Neither the name of Analog Devices, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived from this
20 * software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. "AS IS" AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
25 * EVENT SHALL ANALOG DEVICES, INC. BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
28 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
31 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *******************************************************************************/
33
34#ifndef __AFE_CALIBRATION_H__
35#define __AFE_CALIBRATION_H__
36
37#include <stdint.h>
38#include <stdbool.h>
39
40/* Calibration constants */
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) /* 2^27 */
46#define OFFSET_SCALE_FACTOR (1LL << 15) /* 2^15 */
47
48/* 24-bit signed register limits for offset registers */
49#define OFFSET_REG_MAX ((1L << 23) - 1) /* +8388607 */
50#define OFFSET_REG_MIN (-(1L << 23)) /* -8388608 */
51
67
75
84
88typedef struct {
89 float nominal_current; /* Arms - for gain calibration */
90 float nominal_voltage; /* Vrms - for gain calibration */
91 float offset_current; /* Arms - for offset calibration */
92 float offset_voltage; /* Vrms - for offset calibration */
93 float current_pga_gain; /* PGA gain for current (default 1) */
94 float voltage_pga_gain; /* PGA gain for voltage (default 1) */
95 float current_scale; /* Current scale factor */
96 float voltage_scale; /* Voltage scale factor */
97 int32_t i_gain_init; /* Initial current gain coefficient */
98 int32_t v_gain_init; /* Initial voltage gain coefficient */
99 int32_t i_rmsos_init; /* Initial current RMS offset */
100 int32_t v_rmsos_init; /* Initial voltage RMS offset */
101 int32_t if_rmsos_init; /* Initial current fundamental RMS offset */
102 int32_t vf_rmsos_init; /* Initial voltage fundamental RMS offset */
104
108typedef struct {
109 int32_t i_gain; /* Computed current gain coefficient */
110 int32_t v_gain; /* Computed voltage gain coefficient */
111 int32_t i_rmsos; /* Computed current RMS offset */
112 int32_t v_rmsos; /* Computed voltage RMS offset */
113 int32_t if_rmsos; /* Computed current fundamental RMS offset */
114 int32_t vf_rmsos; /* Computed voltage fundamental RMS offset */
115 /* Gain calibration errors (persistent) */
116 float gain_i_error_before; /* Gain cal: current error before */
117 float gain_v_error_before; /* Gain cal: voltage error before */
118 float gain_i_error_after; /* Gain cal: current error after */
119 float gain_v_error_after; /* Gain cal: voltage error after */
120 /* Offset calibration errors (persistent) */
121 float offset_i_error_before; /* Offset cal: current error before */
122 float offset_v_error_before; /* Offset cal: voltage error before */
123 float offset_i_error_after; /* Offset cal: current error after */
124 float offset_v_error_after; /* Offset cal: voltage error after */
125 bool done; /* Calibration complete flag */
127
162
163/* Global calibration context */
165
169void calibration_init(void);
170
178
184
189bool calibration_is_active(void);
190
195bool calibration_is_done(void);
196
200void calibration_clear_done(void);
201
206const char *calibration_get_status_string(void);
207
214uint32_t calculate_expected_irms(const CALIBRATION_INPUT *input,
215 float rms_value);
216
223uint32_t calculate_expected_vrms(const CALIBRATION_INPUT *input,
224 float rms_value);
225
232int32_t calculate_gain(int32_t expected, int32_t measured);
233
240int32_t calculate_rms_without_gain(int32_t measured, int32_t gain);
241
248int32_t calculate_rmsos(int32_t expected, int32_t measured);
249
256int32_t calculate_rms_without_rmsos(int32_t measured, int32_t xrmsos);
257
258
265float calculate_error(int32_t measured, int32_t expected);
266
267#endif /* __AFE_CALIBRATION_H__ */
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