view autocal/vcxomain.c @ 26:a2e4da9d85cc

fc-rfcal-vcxo: search for zero crossing implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 22 May 2017 22:27:22 +0000
parents 0b6881281d86
children 841dd03d5c85
line wrap: on
line source

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

#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;

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;
	}

	printf("Stopping RF Tx on the DUT\n");
	do_rfe(STOP_ALL);
	exit(0);
}