# HG changeset patch # User Mychaela Falconia # Date 1694458147 0 # Node ID d4d3531d342a1fe0f32552ad12509a08da178eac # Parent 61cdcf2eb17b230240dfcd427f7d58667fd8b4d5 cp2102-read-eeprom program written, compiles diff -r 61cdcf2eb17b -r d4d3531d342a .hgignore --- a/.hgignore Mon Sep 11 17:48:53 2023 +0000 +++ b/.hgignore Mon Sep 11 18:49:07 2023 +0000 @@ -2,6 +2,7 @@ \.[oa]$ +^cp2102/cp2102-read-eeprom$ ^cp2102/cp2102-read-partno$ ^duart28/fc-duart28-conf$ diff -r 61cdcf2eb17b -r d4d3531d342a cp2102/Makefile --- a/cp2102/Makefile Mon Sep 11 17:48:53 2023 +0000 +++ b/cp2102/Makefile Mon Sep 11 18:49:07 2023 +0000 @@ -1,14 +1,19 @@ CC= gcc CFLAGS= -O2 -PROGS= cp2102-read-partno +PROGS= cp2102-read-eeprom cp2102-read-partno LIBS= ../libuwrap/libuwrap.a INSTALL_PREFIX= /opt/freecalypso INSTBIN=${INSTALL_PREFIX}/bin +READ_EEPROM_OBJS= intel_hex_out.o read_eeprom.o read_eeprom_main.o + all: ${PROGS} +cp2102-read-eeprom: ${READ_EEPROM_OBJS} ${LIBS} + ${CC} ${CFLAGS} -o $@ ${READ_EEPROM_OBJS} ${LIBS} -lusb + cp2102-read-partno: read_partno.o ${LIBS} ${CC} ${CFLAGS} -o $@ read_partno.o ${LIBS} -lusb diff -r 61cdcf2eb17b -r d4d3531d342a cp2102/cp210x_defs.h --- a/cp2102/cp210x_defs.h Mon Sep 11 17:48:53 2023 +0000 +++ b/cp2102/cp210x_defs.h Mon Sep 11 18:49:07 2023 +0000 @@ -26,6 +26,8 @@ #define SIZE_BAUDRATE_TABLE (SIZE_BAUDRATES * SIZE_BAUDRATE_CFG) #define SIZE_VENDOR_STRING 50 +#define EEPROM_START_ADDR 0x3600 + /* a little bit adapted from libftdi */ #define DEVICE_OUT_REQTYPE (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT) diff -r 61cdcf2eb17b -r d4d3531d342a cp2102/intel_hex_out.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cp2102/intel_hex_out.c Mon Sep 11 18:49:07 2023 +0000 @@ -0,0 +1,45 @@ +/* + * This module implements a function for writing out the same Intel HEX + * format as used by cp210x-program-1.0, the Python-language tool from 2014. + */ + +#include +#include +#include +#include +#include +#include "cp210x_defs.h" + +void +intel_hex_out(eeprom, outf) + u_char *eeprom; + FILE *outf; +{ + unsigned linecnt, bytecnt; + unsigned addr; + u_char *sp; + u_char record[21], *dp, csum; + + sp = eeprom; + addr = EEPROM_START_ADDR; + for (linecnt = 0; linecnt < 64; linecnt++) { + dp = record; + *dp++ = 0x10; + *dp++ = addr >> 8; + *dp++ = addr; + *dp++ = 0x00; + for (bytecnt = 0; bytecnt < 16; bytecnt++) + *dp++ = *sp++; + csum = 0; + for (bytecnt = 0; bytecnt < 20; bytecnt++) + csum = record[bytecnt]; + csum = 0x100 - csum; + record[20] = csum; + putc(':', outf); + for (bytecnt = 0; bytecnt < 21; bytecnt++) + fprintf(outf, "%02X", record[bytecnt]); + putc('\n', outf); + addr += 16; + } + fputs(":00000001FF\n", outf); +} diff -r 61cdcf2eb17b -r d4d3531d342a cp2102/read_eeprom.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cp2102/read_eeprom.c Mon Sep 11 18:49:07 2023 +0000 @@ -0,0 +1,30 @@ +/* + * This module implements the elementary function for reading the EEPROM + * content out of a CP2102 device. + */ + +#include +#include +#include +#include +#include +#include "cp210x_defs.h" + +extern u_char eeprom[SIZE_EEPROM]; + +void +read_eeprom(usbh) + usb_dev_handle *usbh; +{ + int rc; + + rc = usb_control_msg(usbh, DEVICE_IN_REQTYPE, CP210x_CONFIG, + REG_EEPROM, 0, (char *) eeprom, SIZE_EEPROM, + USB_READ_TIMEOUT); + if (rc != SIZE_EEPROM) { + fprintf(stderr, + "EEPROM read error: usb_control_msg() returned %d\n", + rc); + exit(1); + } +} diff -r 61cdcf2eb17b -r d4d3531d342a cp2102/read_eeprom_main.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cp2102/read_eeprom_main.c Mon Sep 11 18:49:07 2023 +0000 @@ -0,0 +1,44 @@ +/* + * This program locates a CP2102 device via libusb, reads its internal + * 1024-byte EEPROM and emits (on stdout) an image of this EEPROM + * in the same Intel HEX format as used by cp210x-program-1.0, + * the Python-language tool from 2014. + */ + +#include +#include +#include +#include +#include +#include +#include +#include "../libuwrap/find_dev.h" +#include "cp210x_defs.h" + +u_char eeprom[SIZE_EEPROM]; + +main(argc, argv) + char **argv; +{ + struct usb_device *dev; + usb_dev_handle *usbh; + + if (argc != 2) { + fprintf(stderr, "usage: %s device-selector\n", argv[0]); + exit(1); + } + dev = find_usbdev_by_desc_string(argv[1]); + if (!dev) { + fprintf(stderr, "error: specified USB device not found\n"); + exit(1); + } + usbh = usb_open(dev); + if (!usbh) { + fprintf(stderr, "error: usb_open() failed\n"); + exit(1); + } + read_eeprom(usbh); + usb_close(usbh); + intel_hex_out(eeprom, stdout); + exit(0); +}