view autocal/vcxomain.c @ 27:841dd03d5c85

fc-rfcal-vcxo: almost done
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 22 May 2017 23:10:34 +0000
parents a2e4da9d85cc
children ba4b6877e227
line wrap: on
line source

/*
 * This module contains the main() function for fc-rfcal-vcxo.
 */

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

#define	VCXOCAL_BAND		900
#define	VCXOCAL_BAND_RFPW	RFPW_STD_BAND(6, 0)
#define	VCXOCAL_ARFCN		40

extern float vcxo_freq_meas();

static float freq_max_neg, freq_max_pos;
static float lin_a, lin_b, lin_a2, lin_b2;
static int zero_search_dac1, zero_search_dac2, zero_search_incr;
static float zero_search_freq1, zero_search_freq2;
static float dac_min, dac_max, dac_init;
static int dac_init_int;
static float Psi_sta, Psi_st;

prepare_rf_test_system()
{
	char cmd[80];

	printf("Preparing RF test system for VCXO calibration\n");
	sprintf(cmd, "vcxo-cal-setup %d %d\n", VCXOCAL_BAND, VCXOCAL_ARFCN);
	tsid_command(cmd);
	return(0);
}

main(argc, argv)
	char **argv;
{
	socket_pathname_options(argc, argv);
	connect_rvinterf_socket();
	connect_tsid_socket();
	prepare_rf_test_system();

	printf("Putting the DUT into Test Mode\n");
	do_tms(1);
	do_rfpw(STD_BAND_FLAG, VCXOCAL_BAND_RFPW);
	do_rfpw(TCH_ARFCN, VCXOCAL_ARFCN);
	do_rfpw(AFC_ENA_FLAG, 0);
	do_txpw(TX_PWR_LEVEL, 12);
	printf("Starting RF Tx on the DUT\n");
	do_rfe(RX_TX_TCH);

	/* initial measurements at the DAC extremes */
	freq_max_neg = vcxo_freq_meas(-2048, "max-neg");
	freq_max_pos = vcxo_freq_meas(2048, "max-pos");
	lin_a = (freq_max_pos - freq_max_neg) / 4096.0f;
	lin_b = freq_max_pos - lin_a * 2048.0f;
	zero_search_dac1 = -lin_b / lin_a;
	zero_search_freq1 = vcxo_freq_meas(zero_search_dac1, "zero-search");

	/* search for zero crossing */
	if (zero_search_freq1 < 0)
		zero_search_incr = 100;
	else
		zero_search_incr = -100;
	for (;;) {
		zero_search_dac2 = zero_search_dac1 + zero_search_incr;
		zero_search_freq2 = vcxo_freq_meas(zero_search_dac2,
						   "zero-search");
		if (zero_search_incr > 0 && zero_search_freq2 >= 0)
			break;
		if (zero_search_incr < 0 && zero_search_freq2 < 0)
			break;
		zero_search_dac1 = zero_search_dac2;
		zero_search_freq1 = zero_search_freq2;
	}

	/* second linear approximation */
	lin_a2 = (zero_search_freq2 - zero_search_freq1) /
		(float)(zero_search_dac2 - zero_search_dac1);
	lin_b2 = zero_search_freq2 - lin_a2 * zero_search_dac2;

	/* DAC settings */
	dac_min = (-13500.0f - lin_b) / lin_a;
	dac_max = (13500.0f - lin_b) / lin_a;
	dac_init = -lin_b2 / lin_a2;
	dac_init_int = (int) dac_init;

	/* check the frequency offset at the final DAC value */
	vcxo_freq_meas(dac_init_int, "zero-check");

	/* done with the measurements and the Tx */
	printf("Stopping RF Tx on the DUT\n");
	do_rfe(STOP_ALL);

	/* Psi computations */
	Psi_sta = 2.0f * (float)M_PI * lin_a / 270833.0f;
	Psi_st = Psi_sta * 0.8f;

	/* afcparams output TBD */

	exit(0);
}