Commit | Line | Data |
---|---|---|
bf956a00 TJ |
1 | /* simple.c |
2 | ||
3 | Simple libftdi usage example | |
4 | ||
5 | This program is distributed under the GPL, version 2 | |
6 | */ | |
7 | ||
ad397a4b | 8 | #include <stdio.h> |
579b006f | 9 | #include <stdlib.h> |
ad397a4b TJ |
10 | #include <ftdi.h> |
11 | ||
6ac169ea | 12 | int main(void) |
ad397a4b TJ |
13 | { |
14 | int ret; | |
cd2ead2f | 15 | struct ftdi_context *ftdi; |
0220adfa | 16 | struct ftdi_version_info version; |
cd2ead2f UB |
17 | if ((ftdi = ftdi_new()) == 0) |
18 | { | |
19 | fprintf(stderr, "ftdi_new failed\n"); | |
6ac169ea TJ |
20 | return EXIT_FAILURE; |
21 | } | |
ad397a4b | 22 | |
0220adfa TJ |
23 | version = ftdi_get_library_version(); |
24 | printf("Initialized libftdi %s (major: %d, minor: %d, micro: %d, snapshot ver: %s)\n", | |
25 | version.version_str, version.major, version.minor, version.micro, | |
26 | version.snapshot_str); | |
27 | ||
cd2ead2f | 28 | if ((ret = ftdi_usb_open(ftdi, 0x0403, 0x6001)) < 0) |
22d12cda | 29 | { |
cd2ead2f UB |
30 | fprintf(stderr, "unable to open ftdi device: %d (%s)\n", ret, ftdi_get_error_string(ftdi)); |
31 | ftdi_free(ftdi); | |
ad397a4b TJ |
32 | return EXIT_FAILURE; |
33 | } | |
34 | ||
cb6250fa | 35 | // Read out FTDIChip-ID of R type chips |
cd2ead2f | 36 | if (ftdi->type == TYPE_R) |
22d12cda | 37 | { |
cb6250fa | 38 | unsigned int chipid; |
cd2ead2f | 39 | printf("ftdi_read_chipid: %d\n", ftdi_read_chipid(ftdi, &chipid)); |
cb6250fa TJ |
40 | printf("FTDI chipid: %X\n", chipid); |
41 | } | |
42 | ||
cd2ead2f | 43 | if ((ret = ftdi_usb_close(ftdi)) < 0) |
6ac169ea | 44 | { |
cd2ead2f UB |
45 | fprintf(stderr, "unable to close ftdi device: %d (%s)\n", ret, ftdi_get_error_string(ftdi)); |
46 | ftdi_free(ftdi); | |
6ac169ea TJ |
47 | return EXIT_FAILURE; |
48 | } | |
49 | ||
cd2ead2f | 50 | ftdi_free(ftdi); |
ad397a4b TJ |
51 | |
52 | return EXIT_SUCCESS; | |
53 | } |