view autocal/txlevels.c @ 117:4c3f4231a021

autocal: vout_t definition factored out of txband.h into txvout.h
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 13 Feb 2018 07:02:17 +0000
parents 1e49bb52b07e
children 9f09a7c3607a
line wrap: on
line source

/*
 * The calibration of Tx power levels is implemented here.
 */

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <rvinterf/l1tm.h>
#include <rvinterf/exitcodes.h>
#include "txvout.h"
#include "txband.h"

extern double tx_power_meas();
extern vout_t dbm_to_vout();

extern struct txcal_band *txcal_band;
extern struct tx_basis_point tx_basis[MAX_BASIS_POINTS];
extern unsigned num_basis_points;
extern struct tx_level tx_levels[MAX_TX_LEVELS];

unsigned
find_apc_for_target(target_dbm, pslope)
	double target_dbm;
	vout_t *pslope;
{
	vout_t target_vout, slope;
	unsigned n;
	int apc_delta;

	target_vout = dbm_to_vout(target_dbm);
	for (n = 0; n < num_basis_points - 1; n++)
		if (target_vout < tx_basis[n+1].vout)
			break;
	if (n == num_basis_points - 1)
		slope = tx_basis[n-1].slope;
	else
		slope = tx_basis[n].slope;
	if (pslope)
		*pslope = slope;
	apc_delta = (target_vout - tx_basis[n].vout) / slope;
	return tx_basis[n].apc + apc_delta;
}

calibrate_tx_levels()
{
	unsigned plnum, plidx, apc;
	double target, meas, error;
	int nanflag = 0, errflag = 0;

	printf("Calibrating Tx power levels\n");
	printf("Starting RF Tx on the DUT\n");
	do_rfe(RX_TX_TCH);

	for (plnum = txcal_band->start_plnum; plnum <= txcal_band->end_plnum;
	     plnum++) {
		do_txpw(TX_PWR_LEVEL, plnum);
		plidx = plnum - txcal_band->start_plnum;
		target = tx_levels[plidx].target;
		apc = find_apc_for_target(target, &tx_levels[plidx].slope);
		tx_levels[plidx].apc = apc;
		do_txpw(TX_APC_DAC, apc);
		do_txpw(TX_CHAN_CAL_TABLE,
			txcal_band->calchan_selections[plidx]);
		usleep(20000);
		meas = tx_power_meas();
		if (isnan(meas))
			nanflag = 1;
		error = meas - target;
		if (error < -2.0 || error > 2.0)
			errflag = 1;
		printf(
	"Tx power level #%u: target %.1f dBm, APC=%u, meas %.2f dBm (%+.2f)\n",
			plnum, target, apc, meas, error);
	}

	printf("Stopping RF Tx on the DUT\n");
	do_rfe(STOP_ALL);
	if (nanflag) {
		printf("Error: got NaN power measurement, aborting\n");
		exit(ERROR_RFFAIL);
	}
	if (errflag) {
		printf("Error: Tx power off by more than 2 dBm, aborting\n");
		exit(ERROR_RFFAIL);
	}
	return(0);
}