view ffstools/caltools/fc-rftab2c.c @ 632:ae4330d86029

loadtool.help: description of program-m0 updated
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 29 Feb 2020 23:53:33 +0000
parents 5bcf12be0834
children a684dd7799f8
line wrap: on
line source

/*
 * This utility reads an RF parameter table of one of the supported types
 * in FreeCalypso ASCII format (it has to be one of the tables that go
 * into the T_RF_BAND structure) and converts it into a C code snippet
 * suitable for insertion into the firmware source in the L1 RF "customization"
 * code where compiled-in default RF parameter tables are defined.
 *
 * This tool is primarily intended for use with tx-ramps tables and maybe
 * tx-levels, but it also supports tx-calchan, tx-caltemp, rx-agc-params,
 * rx-calchan and rx-caltemp tables.
 *
 * This program is based on the calextract tool from 2014 (freecalypso-reveng
 * repository) and the generated C code snippets feature the same style,
 * indentation and comments.
 */

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

u_char binbuf[512];

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

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

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

void
do_rx_cal_params()
{
	u_char *bp = binbuf;
	int i;

	puts("    { /* T_RX_CAL_PARAMS */");
	for (i = 0; i < 4; i++) {
		printf("%10u,\n", get_u16(bp));
		bp += 2;
	}
	puts("    },");
}

void
do_rx_agc_bands()
{
	u_char *bp = binbuf;
	int i, s;
	unsigned u;

	puts("    { /* T_RF_AGC_BANDs */");
	for (i = 0; i < 10; i++) {
		u = get_u16(bp);
		bp += 2;
		s = get_s16(bp);
		bp += 2;
		printf("      {%5u,%6d},\n", u, s);
	}
	puts("    },");
}

void
do_rx_temp_comp()
{
	u_char *bp = binbuf;
	int i, s1, s2;

	puts("    { /* Rx temperature compensation */");
	for (i = 0; i < 11; i++) {
		s1 = get_s16(bp);
		bp += 2;
		s2 = get_s16(bp);
		bp += 2;
		printf("      {%6d,%6d},\n", s1, s2);
	}
	puts("    },");
}

void
do_tx_levels()
{
	u_char *bp = binbuf;
	unsigned i, u, b1, b2;

	puts("    { /* levels */");
	for (i = 0; i < 32; i++) {
		u = get_u16(bp);
		bp += 2;
		b1 = *bp++;
		b2 = *bp++;
		printf("      {%5u,%3u,%3u}, /* %u */\n", u, b1, b2, i);
	}
	puts("    },");
}

void
do_tx_calchan()
{
	u_char *bp = binbuf;
	int i, j, s;
	unsigned u;

	puts("    { /* channel calibration tables */");
	for (i = 0; i < 4; i++) {
		printf("      { /* calibration table %d */\n", i);
		for (j = 0; j < 8; j++) {
			u = get_u16(bp);
			bp += 2;
			s = get_s16(bp);
			bp += 2;
			printf("\t{%5u,%6d},\n", u, s);
		}
		puts("      },");
	}
	puts("    },");
}

static void
do_ramp_16bytes(bin)
	u_char *bin;
{
	u_char *bp = bin;
	int i, b;

	putchar('\t');
	putchar('{');
	for (i = 0; i < 16; i++) {
		b = *bp++;
		printf("%3d%c", b, i == 15 ? '}' : ',');
	}
	putchar(',');
	putchar('\n');
}

void
do_tx_ramps()
{
	u_char *bp = binbuf;
	int i;

	puts("    { /* ramps */");
	for (i = 0; i < 16; i++) {
		printf("      { /* profile %d */\n", i);
		puts("\t/* ramp-up */");
		do_ramp_16bytes(bp);
		bp += 16;
		puts("\t/* ramp-down */");
		do_ramp_16bytes(bp);
		bp += 16;
		puts("      },");
	}
	puts("    },");
}

void
do_tx_temp_comp()
{
	u_char *bp = binbuf;
	int i, j, s[4];

	puts("    { /* Tx temperature compensation */");
	for (i = 0; i < 5; i++) {
		for (j = 0; j < 4; j++) {
			s[j] = get_s16(bp);
			bp += 2;
		}
		printf("      {%6d,%6d,%6d,%6d},\n", s[0], s[1], s[2], s[3]);
	}
	puts("    },");
}

static struct map {
	char	*format;
	void	(*func)();
} map_table[] = {
	{"tx-ramps",		do_tx_ramps},
	{"tx-levels",		do_tx_levels},
	{"tx-calchan",		do_tx_calchan},
	{"tx-caltemp",		do_tx_temp_comp},
	{"rx-calchan",		do_rx_agc_bands},
	{"rx-caltemp",		do_rx_temp_comp},
	{"rx-agc-params",	do_rx_cal_params},
	{0,			0}
};

main(argc, argv)
	char **argv;
{
	char *format;
	struct map *map;

	if (argc < 2 || argc > 3) {
		fprintf(stderr, "usage: %s ascii-rftab-file [C-output-file]\n",
			argv[0]);
		exit(1);
	}
	if (read_rf_table_ext(argv[1], binbuf, 1, &format, (unsigned *) 0))
		exit(1);
	for (map = map_table; map->format; map++)
		if (!strcmp(map->format, format))
			break;
	if (!map->func) {
		printf("error: %s tables are not supported\n", format);
		exit(1);
	}
	if (argc >= 3 && !freopen(argv[2], "w", stdout)) {
		perror(argv[2]);
		exit(1);
	}
	map->func();
	exit(0);
}