no-OS
Loading...
Searching...
No Matches
afe_calibration.h File Reference
#include <stdint.h>
#include <stdbool.h>
Include dependency graph for afe_calibration.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  CALIBRATION_INPUT
 Calibration input parameters (user-provided values) More...
 
struct  CALIBRATION_RESULT
 Calibration results (computed values) More...
 
struct  CALIBRATION_CONTEXT
 Calibration state structure (runtime state) More...
 

Macros

#define ADI_PQLIB_MAX_RMS_VALUE_CAL   52702092
 
#define CALIBRATION_CYCLES_50HZ   150
 
#define CALIBRATION_CYCLES_60HZ   180
 
#define SQRT_2   1.41421356f
 
#define GAIN_SCALE_FACTOR   (1LL << 27) /* 2^27 */
 
#define OFFSET_SCALE_FACTOR   (1LL << 15) /* 2^15 */
 
#define OFFSET_REG_MAX   ((1L << 23) - 1) /* +8388607 */
 
#define OFFSET_REG_MIN   (-(1L << 23)) /* -8388608 */
 

Enumerations

enum  CALIBRATION_STATE {
  CALIBRATION_STATE_IDLE ,
  CALIBRATION_STATE_INIT ,
  CALIBRATION_STATE_COLLECTING ,
  CALIBRATION_STATE_CALCULATING ,
  CALIBRATION_STATE_CALCULATE_ERROR_BEFORE ,
  CALIBRATION_STATE_WRITING ,
  CALIBRATION_STATE_VERIFY_COLLECTING ,
  CALIBRATION_STATE_VERIFY_CALCULATING ,
  CALIBRATION_STATE_DONE ,
  CALIBRATION_STATE_ERROR
}
 Calibration state machine states. More...
 
enum  CALIBRATION_TYPE {
  CALIBRATION_TYPE_GAIN ,
  CALIBRATION_TYPE_OFFSET
}
 Calibration type (gain or offset) More...
 
enum  CALIBRATION_PHASE {
  CALIBRATION_PHASE_A = 0 ,
  CALIBRATION_PHASE_B = 1 ,
  CALIBRATION_PHASE_C = 2
}
 Phase selection for calibration. More...
 

Functions

void calibration_init (void)
 Initialize calibration context with default values.
 
int calibration_start (CALIBRATION_TYPE type, CALIBRATION_PHASE phase)
 Start calibration process.
 
int calibration_process_cycle (void)
 Process one calibration cycle (called from main loop)
 
bool calibration_is_active (void)
 Check if calibration is active.
 
bool calibration_is_done (void)
 Check if calibration just completed.
 
void calibration_clear_done (void)
 Clear calibration done flag.
 
const char * calibration_get_status_string (void)
 Get current calibration status string.
 
uint32_t calculate_expected_irms (const CALIBRATION_INPUT *input, float rms_value)
 Calculate expected RMS value for current.
 
uint32_t calculate_expected_vrms (const CALIBRATION_INPUT *input, float rms_value)
 Calculate expected RMS value for voltage.
 
int32_t calculate_gain (int32_t expected, int32_t measured)
 Calculate gain coefficient.
 
int32_t calculate_rms_without_gain (int32_t measured, int32_t gain)
 Calculate RMS without gain coefficient.
 
int32_t calculate_rmsos (int32_t expected, int32_t measured)
 Calculate RMS offset coefficient.
 
int32_t calculate_rms_without_rmsos (int32_t measured, int32_t xrmsos)
 Calculate RMS value without offset coefficient.
 
float calculate_error (int32_t measured, int32_t expected)
 Calculate error percentage.
 

Variables

CALIBRATION_CONTEXT calibrationCtx
 

Macro Definition Documentation

◆ ADI_PQLIB_MAX_RMS_VALUE_CAL

#define ADI_PQLIB_MAX_RMS_VALUE_CAL   52702092

◆ CALIBRATION_CYCLES_50HZ

#define CALIBRATION_CYCLES_50HZ   150

◆ CALIBRATION_CYCLES_60HZ

#define CALIBRATION_CYCLES_60HZ   180

◆ GAIN_SCALE_FACTOR

#define GAIN_SCALE_FACTOR   (1LL << 27) /* 2^27 */

◆ OFFSET_REG_MAX

#define OFFSET_REG_MAX   ((1L << 23) - 1) /* +8388607 */

◆ OFFSET_REG_MIN

#define OFFSET_REG_MIN   (-(1L << 23)) /* -8388608 */

◆ OFFSET_SCALE_FACTOR

#define OFFSET_SCALE_FACTOR   (1LL << 15) /* 2^15 */

◆ SQRT_2

#define SQRT_2   1.41421356f

Enumeration Type Documentation

◆ CALIBRATION_PHASE

Phase selection for calibration.

Enumerator
CALIBRATION_PHASE_A 
CALIBRATION_PHASE_B 
CALIBRATION_PHASE_C 

◆ CALIBRATION_STATE

Calibration state machine states.

Enumerator
CALIBRATION_STATE_IDLE 
CALIBRATION_STATE_INIT 
CALIBRATION_STATE_COLLECTING 
CALIBRATION_STATE_CALCULATING 
CALIBRATION_STATE_CALCULATE_ERROR_BEFORE 
CALIBRATION_STATE_WRITING 
CALIBRATION_STATE_VERIFY_COLLECTING 
CALIBRATION_STATE_VERIFY_CALCULATING 
CALIBRATION_STATE_DONE 
CALIBRATION_STATE_ERROR 

◆ CALIBRATION_TYPE

Calibration type (gain or offset)

Enumerator
CALIBRATION_TYPE_GAIN 
CALIBRATION_TYPE_OFFSET 

Function Documentation

◆ calculate_error()

float calculate_error ( int32_t measured,
int32_t expected )

Calculate error percentage.

Parameters
measured- Measured value
expected- Expected value
Returns
Error as percentage (e.g., 0.01 = 1%)
Here is the caller graph for this function:

◆ calculate_expected_irms()

uint32_t calculate_expected_irms ( const CALIBRATION_INPUT * input,
float rms_value )

Calculate expected RMS value for current.

Parameters
input- Calibration input parameters
rms_value- RMS value in Arms
Returns
Expected register value
Here is the caller graph for this function:

◆ calculate_expected_vrms()

uint32_t calculate_expected_vrms ( const CALIBRATION_INPUT * input,
float rms_value )

Calculate expected RMS value for voltage.

Parameters
input- Calibration input parameters
rms_value- RMS value in Vrms
Returns
Expected register value
Here is the caller graph for this function:

◆ calculate_gain()

int32_t calculate_gain ( int32_t expected,
int32_t measured )

Calculate gain coefficient.

Parameters
expected- Expected RMS value
measured- Measured RMS value
Returns
Gain coefficient (signed 32-bit)

◆ calculate_rms_without_gain()

int32_t calculate_rms_without_gain ( int32_t measured,
int32_t gain )

Calculate RMS without gain coefficient.

Parameters
measured- Measured RMS value
gain- Gain register value
Returns
rms value without gain (signed 32-bit)

◆ calculate_rms_without_rmsos()

int32_t calculate_rms_without_rmsos ( int32_t measured,
int32_t xrmsos )

Calculate RMS value without offset coefficient.

Parameters
measured- Measured RMS value
xrmsos- RMSOS register value
Returns
rms value without offset (signed 32-bit)

◆ calculate_rmsos()

int32_t calculate_rmsos ( int32_t expected,
int32_t measured )

Calculate RMS offset coefficient.

Parameters
expected- Expected RMS value
measured- Measured RMS value
Returns
Offset coefficient (signed 32-bit)

◆ calibration_clear_done()

void calibration_clear_done ( void )

Clear calibration done flag.

Here is the caller graph for this function:

◆ calibration_get_status_string()

const char * calibration_get_status_string ( void )

Get current calibration status string.

Returns
Status string ("idle", "in_progress", "done", "error")
Here is the caller graph for this function:

◆ calibration_init()

void calibration_init ( void )

Initialize calibration context with default values.

Here is the caller graph for this function:

◆ calibration_is_active()

bool calibration_is_active ( void )

Check if calibration is active.

Returns
true if calibration is in progress
Here is the caller graph for this function:

◆ calibration_is_done()

bool calibration_is_done ( void )

Check if calibration just completed.

Returns
true if calibration was just done (result.done is true)
Here is the caller graph for this function:

◆ calibration_process_cycle()

int calibration_process_cycle ( void )

Process one calibration cycle (called from main loop)

Returns
0 on success, error code otherwise
Here is the caller graph for this function:

◆ calibration_start()

int calibration_start ( CALIBRATION_TYPE type,
CALIBRATION_PHASE phase )

Start calibration process.

Parameters
type- Calibration type (gain or offset)
phase- Phase to calibrate (A, B, or C)
Returns
0 on success, error code otherwise
Here is the caller graph for this function:

Variable Documentation

◆ calibrationCtx

CALIBRATION_CONTEXT calibrationCtx
extern