summaryrefslogtreecommitdiff
path: root/util/prog.c
blob: 0227682999ab199aaa001e9901c2c1f39e6efd0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ftdi.h>

#define FTDI_MAX_EEPROM_SIZE 256

int main(int argc, char *argv[]) {
    int ret;
    int val;
    struct ftdi_context *ftdi;
    struct ftdi_version_info version;
    unsigned char buf[FTDI_MAX_EEPROM_SIZE];

    if ((ftdi = ftdi_new()) == 0) {
        fprintf(stderr, "ftdi_new failed\n");
        return EXIT_FAILURE;
    }

    version = ftdi_get_library_version();
    printf("Initialized libftdi %s (major: %d, minor: %d, micro: %d, snapshot ver: %s)\n",
        version.version_str, version.major, version.minor, version.micro,
        version.snapshot_str);

    if ((ret = ftdi_usb_open(ftdi, 0x0403, 0x6015)) < 0) {
        fprintf(stderr, "Unable to open ftdi device: %d (%s)\n", ret, ftdi_get_error_string(ftdi));
        ftdi_free(ftdi);
        return EXIT_FAILURE;
    }

    ret = ftdi_read_eeprom(ftdi);
    if (ret < 0) goto ftdi_fin;

    ret = ftdi_eeprom_decode(ftdi, 1);
    if (ret < 0) goto ftdi_fin;

    ftdi_get_eeprom_value(ftdi, SELF_POWERED, &val);
    printf("POWER: %x\n", val);
    ftdi_get_eeprom_value(ftdi, CBUS_FUNCTION_0, &val);
    printf("CBUS0: %d\n", val);
    ftdi_get_eeprom_value(ftdi, CBUS_FUNCTION_1, &val);
    printf("CBUS1: %d\n", val);
    ftdi_get_eeprom_value(ftdi, CBUS_FUNCTION_2, &val);
    printf("CBUS2: %d\n", val);
    ftdi_get_eeprom_value(ftdi, CBUS_FUNCTION_3, &val);
    printf("CBUS3: %d\n", val);

    ftdi_set_eeprom_value(ftdi, SELF_POWERED, 1);
    ftdi_set_eeprom_value(ftdi, CBUS_FUNCTION_0, CBUSX_PWREN);
    ftdi_set_eeprom_value(ftdi, CBUS_FUNCTION_1, CBUSX_IOMODE);
    /* next rev hw
    ftdi_set_eeprom_value(ftdi, CBUS_FUNCTION_3, CBUSX_VBUS_SENSE);
    */
    ftdi_set_eeprom_value(ftdi, CBUS_FUNCTION_2, CBUSX_TRISTATE);
    ftdi_set_eeprom_value(ftdi, CBUS_FUNCTION_3, CBUSX_TRISTATE);

    ret = ftdi_eeprom_build(ftdi);
    if (ret < 0) goto ftdi_fin;

    ret = ftdi_get_eeprom_buf(ftdi, buf, FTDI_MAX_EEPROM_SIZE);
    if (ret < 0) goto ftdi_fin;

    printf("MISC: %x\n", *buf);

    if ((argc > 1) && (strcmp(argv[1], "do") == 0)) {
        ret = ftdi_write_eeprom(ftdi);
        if (ret < 0) goto ftdi_fin;
    }

    ret = ftdi_usb_close(ftdi);

ftdi_fin:
    if (ret < 0) {
        fprintf(stderr, "ERR: %d (%s)\n", ret, ftdi_get_error_string(ftdi));
    }
    ftdi_free(ftdi);

    return ret < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}