summaryrefslogtreecommitdiff
path: root/util/switch.c
blob: 2fc0eb6a97365e25030a355069c2b7dcf8a86c43 (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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ftdi.h>

#define DEV_FE310   1
#define DEV_ESP32   2

void usage(char *p) {
    fprintf(stderr, "Usage: %s fe310|esp32\n", p);
    exit(1);
}

int main(int argc, char *argv[]) {
    int dev;
    int ret;
    struct ftdi_context *ftdi;

    if (argc != 2) {
        usage(argv[0]);
    }
    if (strcmp(argv[1], "fe310") == 0) {
        dev = DEV_FE310;
    } else if (strcmp(argv[1], "esp32") == 0) {
        dev = DEV_ESP32;
    } else {
        usage(argv[0]);
    }

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

    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;
    }

    switch (dev) {
        case DEV_FE310: {
            printf("Switch to fe310\n");
            ret = ftdi_set_bitmode(ftdi, 0x20, BITMODE_CBUS);
            break;
        }

        case DEV_ESP32: {
            printf("Switch to esp32\n");
            ret = ftdi_set_bitmode(ftdi, 0x00, BITMODE_CBUS);
            break;
        }
    }
    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;
}