-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathft232_test.c
131 lines (110 loc) · 3.58 KB
/
ft232_test.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*-----------------------------------------------------------------------------
Based on: libftdi - A library (using libusb) to talk to FTDI's UART/FIFO chips
Original Source: https://www.intra2net.com/en/developer/libftdi/
by:
www.intra2net.com 2003-2017 Intra2net AG
Midas
-------------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "include/ftdi.h"
/*------------------------------------
write to GPIOH0 -GPIOH7
--------------------------------------*/
int mpsse_write_highbits(struct ftdi_context *ftdi, unsigned char val)
{
int ret;
unsigned char buf[3];
buf[0]=SET_BITS_HIGH;
buf[1]=val;
buf[2]=0xff;//1-out
ret = ftdi_write_data(ftdi, buf, 3);
if (ret < 0)
{
fprintf(stderr,"mpsse_write_highbits() write failed, error %d (%s)\n",ret, ftdi_get_error_string(ftdi));
}
return ret;
}
/*------------------------------------
write to GPIOL0 -GPIOL7
--------------------------------------*/
int mpsse_write_lowbits(struct ftdi_context *ftdi, unsigned char val)
{
int ret;
unsigned char buf[3];
buf[0]=SET_BITS_LOW;
buf[1]=val;
buf[2]=0xff;//1-out
ret = ftdi_write_data(ftdi, buf, 3);
if (ret < 0)
{
fprintf(stderr,"mpsse_write_lowbits() write failed, error %d (%s)\n",ret, ftdi_get_error_string(ftdi));
}
return ret;
}
/*===================== MAIN ======================*/
int main(int argc, char **argv)
{
struct ftdi_context *ftdi;
int fret;
unsigned char buf[2],val;
int retval = 0;
int i,k;
unsigned short usb_val;
//------ Allocate a ftdi device struct -----
if ((ftdi = ftdi_new()) == 0)
{
fprintf(stderr, "ftdi_new failed\n");
return EXIT_FAILURE;
}
//------ open a ftdi usb device -------
fret = ftdi_usb_open(ftdi, 0x0403, 0x6014); //VID and PID
if (fret < 0 && fret != -5) // ??? -5
{
fprintf(stderr, "unable to open ftdi device: %d (%s)\n", fret, ftdi_get_error_string(ftdi));
retval = 1;
goto done;
}
printf("ftdi open succeeded: %d\n",fret);
//------- configure to BITMODE_MPSSE mode -----
printf("configure to BITMODE_MPSSE mode\n");
ftdi_set_bitmode(ftdi, 0x00, BITMODE_MPSSE);
//usleep(100000); // Is it necessary ?
//----- run a LED test -----
mpsse_write_highbits(ftdi,0x00);
mpsse_write_lowbits(ftdi,0xee);
//sleep(3); // pause for a while
//------- configure to BITMODE_BITBANG mode -----
//!!!! no disable BITMODE_MPSSE MODE here !!!!!
printf("configure to BITMODE_BITBANG mode\n");
ftdi_set_bitmode(ftdi, 0xFF, BITMODE_BITBANG);
//usleep(100000); // Is it necessary ?
//----- set baudrate here ------
//------ purge rx buffer in FT232H ------
ftdi_usb_purge_rx_buffer(ftdi);// ineffective ??
//------ set chunk_size, default is 4096
//chunk_size=1024*64;// >=1024*32 same effect. default is 4096
//ftdi_write_data_set_chunksize(ftdi,chunk_size);
//ftdi_write_data_get_chunksize(ftdi, &chunk_size);
//----- run a LED test -----
for(i=0;i<30;i++)
{
buf[0] = 0xFF-(1<<(i%8));
if(buf[0] == 0) buf[0]=0xFF;
fret = ftdi_write_data(ftdi, buf, 1);
if (fret < 0)
{
fprintf(stderr,"ftdi_write_data() failed, error %d (%s)\n",fret, ftdi_get_error_string(ftdi));
}
usleep(200000);
}
//------- disable bitbang mode ---------
printf("disabling bitbang mode\n");
ftdi_disable_bitbang(ftdi);
//------ close ftdi usb device and free mem. -------
ftdi_usb_close(ftdi);
done:
ftdi_free(ftdi);
return retval;
}