X-Git-Url: http://developer.intra2net.com/git/?p=libftdi;a=blobdiff_plain;f=examples%2Feeprom.c;h=e2c8052669a9594e23a6ab19f05d108b541173c9;hp=6be43f68fd550f01f65dba228736268e5196e46c;hb=05c2e40a3c720c63a75d36a33d0d5de0677f8f57;hpb=2db3a766fd94a852d95f46efadf3a0dacccd29f5 diff --git a/examples/eeprom.c b/examples/eeprom.c index 6be43f6..e2c8052 100644 --- a/examples/eeprom.c +++ b/examples/eeprom.c @@ -13,164 +13,224 @@ int main(int argc, char **argv) { - struct ftdi_context ftdic; - struct ftdi_eeprom eeprom; - unsigned char buf[2048]; - int size; + struct ftdi_context *ftdi; + unsigned char buf[256]; int f, i, j; int vid = 0x0403; int pid = 0x6010; char const *desc = 0; char const *serial = 0; int erase = 0; - int use_defaults; + int use_defaults = 0; int large_chip = 0; + int do_write = 0; + int size; + int value; + + if ((ftdi = ftdi_new()) == 0) + { + fprintf(stderr, "Failed to allocate ftdi structure :%s \n", + ftdi_get_error_string(ftdi)); + return EXIT_FAILURE; + } - while ((i = getopt(argc, argv, "d::ev:p:P:S:")) != -1) + while ((i = getopt(argc, argv, "d::ev:p:l:P:S:w")) != -1) { switch (i) { - case 'd': - use_defaults = 1; - if(optarg) - large_chip = 0x66; - break; - case 'e': - erase = 1; - break; - case 'v': - vid = strtoul(optarg, NULL, 0); - break; - case 'p': - pid = strtoul(optarg, NULL, 0); - break; - case 'P': + case 'd': + use_defaults = 1; + if (optarg) + large_chip = 0x66; + break; + case 'e': + erase = 1; + break; + case 'v': + vid = strtoul(optarg, NULL, 0); + break; + case 'p': + pid = strtoul(optarg, NULL, 0); + break; + case 'P': desc = optarg; - break; - case 'S': - serial = optarg; - break; - default: - fprintf(stderr, "usage: %s [options]\n", *argv); - fprintf(stderr, "\t-d[num] Work with default valuesfor 128 Byte " - "EEPROM or for 256 Byte EEPROm if some [num] is given\n"); - fprintf(stderr, "\t-e erase\n"); - fprintf(stderr, "\t-v verbose decoding\n"); - fprintf(stderr, "\t-p Search for device with PID == number\n"); - fprintf(stderr, "\t-v Search for device with VID == number\n"); - fprintf(stderr, "\t-P Search for device with PID == number\n"); + fprintf(stderr, "\t-v Search for device with VID == number\n"); + fprintf(stderr, "\t-P chip == -1) + if (ftdi_get_eeprom_value(ftdi, CHIP_TYPE, & value) <0) + { + fprintf(stderr, "ftdi_get_eeprom_value: %d (%s)\n", + f, ftdi_get_error_string(ftdi)); + } + if (value == -1) fprintf(stderr, "No EEPROM\n"); - else if (ftdic.eeprom->chip == 0) + else if (value == 0) fprintf(stderr, "Internal EEPROM\n"); else - fprintf(stderr, "Found 93x%02x\n",ftdic.eeprom->chip); + fprintf(stderr, "Found 93x%02x\n", value); return 0; - } + } - size = 2048; - memset(buf,0, size); - ftdic.eeprom = &eeprom; - if(use_defaults) + if (use_defaults) + { + ftdi_eeprom_initdefaults(ftdi, "IKDA", "FTDIJTAG", "0001"); + ftdi_eeprom_initdefaults(ftdi, "IKDA", "FTDIJTAG", "0001"); + if (ftdi_set_eeprom_value(ftdi, MAX_POWER, 500) <0) + { + fprintf(stderr, "ftdi_set_eeprom_value: %d (%s)\n", + f, ftdi_get_error_string(ftdi)); + } + if (large_chip) + if (ftdi_set_eeprom_value(ftdi, CHIP_TYPE, 0x66) <0) + { + fprintf(stderr, "ftdi_set_eeprom_value: %d (%s)\n", + f, ftdi_get_error_string(ftdi)); + } + f=(ftdi_eeprom_build(ftdi)); + if (f < 0) + { + fprintf(stderr, "ftdi_eeprom_build: %d (%s)\n", + f, ftdi_get_error_string(ftdi)); + exit(-1); + } + } + else if (do_write) { - ftdi_eeprom_initdefaults(&ftdic); - ftdic.eeprom->manufacturer="IKDA"; - ftdic.eeprom->product="CPS-CONN"; - ftdic.eeprom->serial="0001"; - ftdic.eeprom->chip= large_chip; - ftdic.eeprom->cbus_function[0]= CBUS_BB_RD; - ftdic.eeprom->cbus_function[1]= CBUS_CLK48; - ftdic.eeprom->cbus_function[2]= CBUS_IOMODE; - ftdic.eeprom->cbus_function[3]= CBUS_BB; - ftdic.eeprom->cbus_function[4]= CBUS_CLK6; - f=(ftdi_eeprom_build(&ftdic, buf)); + ftdi_eeprom_initdefaults(ftdi, "IKDA", "FTDIJTAG", "0001"); + f = ftdi_erase_eeprom(ftdi); + if (ftdi_set_eeprom_value(ftdi, MAX_POWER, 500) <0) + { + fprintf(stderr, "ftdi_set_eeprom_value: %d (%s)\n", + f, ftdi_get_error_string(ftdi)); + } + f = ftdi_erase_eeprom(ftdi);/* needed to determine EEPROM chip type */ + if (ftdi_get_eeprom_value(ftdi, CHIP_TYPE, & value) <0) + { + fprintf(stderr, "ftdi_get_eeprom_value: %d (%s)\n", + f, ftdi_get_error_string(ftdi)); + } + if (value == -1) + fprintf(stderr, "No EEPROM\n"); + else if (value == 0) + fprintf(stderr, "Internal EEPROM\n"); + else + fprintf(stderr, "Found 93x%02x\n", value); + f=(ftdi_eeprom_build(ftdi)); + if (f < 0) + { + fprintf(stderr, "Erase failed: %s", + ftdi_get_error_string(ftdi)); + return -2; + } + f = ftdi_write_eeprom(ftdi); + { + fprintf(stderr, "ftdi_eeprom_decode: %d (%s)\n", + f, ftdi_get_error_string(ftdi)); + exit(-1); + } + f = ftdi_read_eeprom(ftdi); if (f < 0) { - fprintf(stderr, "ftdi_eeprom_build: %d (%s)\n", - f, ftdi_get_error_string(&ftdic)); + fprintf(stderr, "ftdi_read_eeprom: %d (%s)\n", + f, ftdi_get_error_string(ftdi)); exit(-1); } } else { - f = ftdi_read_eeprom(&ftdic, buf); + f = ftdi_read_eeprom(ftdi); if (f < 0) { - fprintf(stderr, "ftdi_read_eeprom: %d (%s)\n", - f, ftdi_get_error_string(&ftdic)); + fprintf(stderr, "ftdi_read_eeprom: %d (%s)\n", + f, ftdi_get_error_string(ftdi)); exit(-1); } } - fprintf(stderr, "Chip type %d ftdi_eeprom_size: %d\n", ftdic.type, ftdic.eeprom->size); - for(i=0; i < ftdic.eeprom->size; i += 16) + + + ftdi_get_eeprom_value(ftdi, CHIP_SIZE, & value); + fprintf(stderr, "Chip type %d ftdi_eeprom_size: %d\n", ftdi->type, value); + if (ftdi->type == TYPE_R) + size = 0xa0; + else + size = value; + ftdi_get_eeprom_buf(ftdi, buf, size); + for (i=0; i < size; i += 16) { - fprintf(stdout,"0x%03x:", i); - - for (j = 0; j< 8; j++) - fprintf(stdout," %02x", buf[i+j]); - fprintf(stdout," "); - for (; j< 16; j++) - fprintf(stdout," %02x", buf[i+j]); - fprintf(stdout," "); - for (j = 0; j< 8; j++) - fprintf(stdout,"%c", isprint(buf[i+j])?buf[i+j]:'.'); - fprintf(stdout," "); - for (; j< 16; j++) - fprintf(stdout,"%c", isprint(buf[i+j])?buf[i+j]:'.'); - fprintf(stdout,"\n"); + fprintf(stdout,"0x%03x:", i); + + for (j = 0; j< 8; j++) + fprintf(stdout," %02x", buf[i+j]); + fprintf(stdout," "); + for (; j< 16; j++) + fprintf(stdout," %02x", buf[i+j]); + fprintf(stdout," "); + for (j = 0; j< 8; j++) + fprintf(stdout,"%c", isprint(buf[i+j])?buf[i+j]:'.'); + fprintf(stdout," "); + for (; j< 16; j++) + fprintf(stdout,"%c", isprint(buf[i+j])?buf[i+j]:'.'); + fprintf(stdout,"\n"); } - f = ftdi_eeprom_decode(&ftdic,buf, size, 1); + f = ftdi_eeprom_decode(ftdi, 1); + if (f < 0) { - fprintf(stderr, "ftdi_eeprom_decode: %d (%s)\n", - f, ftdi_get_error_string(&ftdic)); + fprintf(stderr, "ftdi_eeprom_decode: %d (%s)\n", + f, ftdi_get_error_string(ftdi)); exit(-1); } - - ftdi_usb_close(&ftdic); - ftdi_deinit(&ftdic); + ftdi_usb_close(ftdi); + ftdi_free(ftdi); + return 0; }