view cp2102/decode_baudtab.c @ 68:5cbde3c80c24

fteeprom-{erase,prog}: detach logic: change to detach by default As it turns out, detaching all ttyUSB interfaces of a multichannel device does not require outside knowledge of how many channels there are, as in our previous -d option design that is being removed here - instead we can read the bNumInterfaces constant from the USB device's config descriptor and thus know how many interfaces there are in total. Based on this discovery, change the design of fteeprom-{erase,prog} as follows: * remove -d option; * flip the default to where we detach all interfaces by default; * add -n option to NOT detach any interfaces.
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 13 Sep 2023 06:37:03 +0000
parents 672c7adc8bc9
children
line wrap: on
line source

/*
 * The function implemented in this module decodes the baud rate table
 * contained in the captured CP2102 EEPROM image.
 */

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include "cp210x_defs.h"

extern u_char eeprom[SIZE_EEPROM];

void
decode_baud_table(outf)
	FILE *outf;
{
	unsigned entry;
	u_char *dp;
	unsigned baudrate, baudgen, timegen, prescaler, sparebyte;
	unsigned baud_div, calc_baud, time_us;

	dp = eeprom;
	for (entry = 0; entry < SIZE_BAUDRATES; entry++) {
		baudgen = (dp[0] << 8) | dp[1];
		timegen = (dp[2] << 8) | dp[3];
		prescaler = dp[4];
		sparebyte = dp[5];
		baudrate = dp[6] | (dp[7] << 8) | (dp[8] << 16) | (dp[9] << 24);
		dp += SIZE_BAUDRATE_CFG;
		fprintf(outf, "baud-entry %2u: %7u = %04X, %04X, %u, %u",
			entry, baudrate, baudgen, timegen, prescaler,
			sparebyte);
		if (prescaler) {
			baud_div = (0x10000 - baudgen) * prescaler;
			calc_baud = 24000000 / baud_div;
			fprintf(outf, "\t# %u baud, ", calc_baud);
			time_us = (0x10000 - timegen) * 2;
			if (time_us >= 1000)
				fprintf(outf, "%u.%03u ms", time_us / 1000,
					time_us % 1000);
			else
				fprintf(outf, "%u us", time_us);
		}
		putc('\n', outf);
	}
}