view miscprog/pircalextr.c @ 222:63c5bb42eca5

osmo2psi test program written
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 23 Nov 2017 22:24:18 +0000
parents e3bbb8cfbbc0
children
line wrap: on
line source

/*
 * This program extracts the RF calibration tables from a dump of Pirelli's
 * factory sector and saves them in FreeCalypso ASCII format.
 */

#include <sys/types.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdint.h>
#include <endian.h>
#include <stdlib.h>
#include <unistd.h>

u_char factbuf[0x2000];

read_factory_struct(filename)
	char *filename;
{
	int fd;
	struct stat st;

	fd = open(filename, O_RDONLY);
	if (fd < 0) {
		perror(filename);
		exit(1);
	}
	fstat(fd, &st);
	if (!S_ISREG(st.st_mode)) {
		fprintf(stderr, "error: %s is not a regular file\n", filename);
		exit(1);
	}
	if (st.st_size != 0x10000) {
		fprintf(stderr,
			"error: %s is of the wrong size (64 KiB expected)\n",
			filename);
		exit(1);
	}
	read(fd, factbuf, sizeof factbuf);
	close(fd);
	return(0);
}

unsigned
get_u32(bin)
	u_char *bin;
{
	return le32toh(*(uint32_t *)bin);
}

unsigned
get_u16(bin)
	u_char *bin;
{
	return le16toh(*(uint16_t *)bin);
}

get_s16(bin)
	u_char *bin;
{
	int i;

	i = le16toh(*(uint16_t *)bin);
	if (i >= 32768)
		i -= 65536;
	return(i);
}

void
write_tx_levels_table(bin, outf)
	u_char *bin;
	FILE *outf;
{
	int i;
	u_char *p = bin;

	fputs("rf_table tx-levels\n\n", outf);
	fputs("# Fields in each entry: apc, ramp_index, chan_cal_index\n\n",
		outf);
	for (i = 0; i < 32; i++) {
		fprintf(outf, "%5u %3u %3u\t# entry %d\n",
			get_u16(p), p[2], p[3], i);
		p += 4;
	}
}

void
write_tx_calchan_table(bin, outf)
	u_char *bin;
	FILE *outf;
{
	int i, j;
	u_char *p = bin;

	fputs("rf_table tx-calchan\n", outf);
	for (i = 0; i < 4; i++) {
		fprintf(outf, "\n# Channel calibration table %d:\n\n", i);
		for (j = 0; j < 8; j++) {
			fprintf(outf, "%5u %6d\n", get_u16(p), get_s16(p + 2));
			p += 4;
		}
	}
}

void
write_rx_calchan_table(bin, outf)
	u_char *bin;
	FILE *outf;
{
	int i;
	u_char *p = bin;

	fputs("rf_table rx-calchan\n\n", outf);
	for (i = 0; i < 10; i++) {
		fprintf(outf, "%5u %6d\n", get_u16(p), get_s16(p + 2));
		p += 4;
	}
}

void
write_rx_agcparams_table(bin, outf)
	u_char *bin;
	FILE *outf;
{
	fputs("rf_table rx-agc-params\n\n", outf);
	fprintf(outf, "%5u\t# g_magic\n", get_u16(bin));
	fprintf(outf, "%5u\t# lna_att\n", get_u16(bin + 2));
	fprintf(outf, "%5u\t# lna_switch_thr_low\n", get_u16(bin + 4));
	fprintf(outf, "%5u\t# lna_switch_thr_high\n", get_u16(bin + 6));
}

struct calmap {
	unsigned	offset;
	char		*outfile;
	void		(*fmtfunc)();
} pirelli_cal_map[] = {
	{0x092C, "tx-levels.900",     write_tx_levels_table},
	{0x09AD, "tx-calchan.900",    write_tx_calchan_table},
	{0x0C2F, "tx-levels.1800",    write_tx_levels_table},
	{0x0CB0, "tx-calchan.1800",   write_tx_calchan_table},
	{0x0F32, "tx-levels.1900",    write_tx_levels_table},
	{0x0FB3, "tx-calchan.1900",   write_tx_calchan_table},
	{0x10AF, "rx-calchan.900",    write_rx_calchan_table},
	{0x10D8, "rx-agcparams.900",  write_rx_agcparams_table},
	{0x10E1, "rx-calchan.1800",   write_rx_calchan_table},
	{0x110A, "rx-agcparams.1800", write_rx_agcparams_table},
	{0x1113, "rx-calchan.1900",   write_rx_calchan_table},
	{0x113C, "rx-agcparams.1900", write_rx_agcparams_table},
	{0, 0, 0}
};

main(argc, argv)
	char **argv;
{
	struct calmap *tp;
	FILE *of;

	if (argc != 3) {
		fprintf(stderr, "usage: %s fact.bin outdir\n", argv[0]);
		exit(1);
	}
	read_factory_struct(argv[1]);
	if (chdir(argv[2]) < 0) {
		perror(argv[2]);
		exit(1);
	}
	for (tp = pirelli_cal_map; tp->outfile; tp++) {
		of = fopen(tp->outfile, "w");
		if (!of) {
			perror(tp->outfile);
			exit(1);
		}
		tp->fmtfunc(factbuf + tp->offset, of);
		fclose(of);
	}
	exit(0);
}