view fteeprom/fteeprom-read.c @ 0:11b8a30333b3

fteeprom: initial import from freecalypso-hwlab
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 03 Sep 2023 18:08:22 +0000
parents
children 1d76deae1e74
line wrap: on
line source

#include <sys/types.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ftdi.h>

char *device_selector;
unsigned eeprom_size = 64;

process_cmdline(argc, argv)
	char **argv;
{
	int c;
	extern int optind;

	while ((c = getopt(argc, argv, "bB")) != EOF) {
		switch (c) {
		case 'b':
			eeprom_size = 128;
			continue;
		case 'B':
			eeprom_size = 256;
			continue;
		default:
			/* error msg already printed */
			exit(1);
		}
	}
	if (argc != optind + 1) {
		fprintf(stderr, "usage: %s [options] device-selector\n",
			argv[0]);
		exit(1);
	}
	device_selector = argv[optind];
}

main(argc, argv)
	char **argv;
{
	struct ftdi_context ftdi;
	u_short word;
	unsigned n, col;

	process_cmdline(argc, argv);
	ftdi_init(&ftdi);
	if (ftdi_usb_open_string(&ftdi, device_selector) < 0) {
		fprintf(stderr, "FTDI USB open failed: %s\n", ftdi.error_str);
		exit(1);
	}
	for (n = 0; n < eeprom_size; n++) {
		if (ftdi_read_eeprom_location(&ftdi, n, &word) < 0) {
			fprintf(stderr, "EEPROM read error: %s\n",
				ftdi.error_str);
			exit(1);
		}
		col = n & 7;
		if (col == 0)
			printf("%02X:", n * 2);
		printf(" %04X", word);
		if (col == 7)
			putchar('\n');
	}
	ftdi_usb_close(&ftdi);
	exit(0);
}