-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathposix_libftdi_reset.c
71 lines (56 loc) · 1.63 KB
/
posix_libftdi_reset.c
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
/*
* Copyright (c) 2021 dresden elektronik ingenieurtechnik gmbh.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
* style license a copy of which has been included with this distribution in
* the LICENSE.txt file.
*
*/
#ifdef HAS_LIBFTDI
#include <ftdi.h>
int plResetLibFtdi()
{
int ret;
struct ftdi_context *ftdi;
if ((ftdi = ftdi_new()) == NULL)
{
fprintf(stderr, "ftdi_new failed\n");
return -1;
}
ftdi->module_detach_mode = AUTO_DETACH_REATACH_SIO_MODULE;
ret = ftdi_usb_open(ftdi, 0x0403, 0x6015);
if (ret < 0 && ret != -5)
{
fprintf(stderr, "unable to open ftdi device: %d (%s)\n", ret, ftdi_get_error_string(ftdi));
ftdi_deinit(ftdi);
return -2;
}
uint8_t bitmask[3] = { 0xf1, 0xf0, 0xf1 };
for (uint8_t i = 0 ; i < sizeof(bitmask); i++)
{
ret = ftdi_set_bitmode(ftdi, bitmask[i], BITMODE_CBUS);
if (ret < 0)
{
fprintf(stderr, "set_bitmode failed for 0x%x, error %d (%s)\n", bitmask[i], ret, ftdi_get_error_string(ftdi));
break;
}
PL_MSleep(10);
// read CBUS
uint8_t buf;
ret = ftdi_read_pins(ftdi, &buf);
if (ret < 0)
{
fprintf(stderr, "read_pins failed, error %d (%s)\n", ret, ftdi_get_error_string(ftdi));
continue;
}
printf("read returned 0x%02x\n", buf);
}
// ftdi_set_bitmode(ftdi, 0, 0x00); // RESET
printf("disabling bitbang mode\n");
ftdi_disable_bitbang(ftdi);
ftdi_usb_close(ftdi);
ftdi_free(ftdi);
return 0;
}
#endif